# An AMR-Based Liquid Film Simulation with Surfactant Transport Using PLIC-HF Method
## 4 AMR Implementation by Using GPU Parallel Computing
In this method, the grid is recursively refined based on a certain criterion:  
\[\min(|\psi_{m}|)<\sqrt{3}\times dh_{n+1}\times b \tag{56}\]  
where \(m\) represents the ID of the leaf, \(n\) represents the depth of the leaf \(m\), and \(b\) is the number of cells in one direction in one leaf. In the present research, the value of \(b\) was 4. According to Equation (56), simply speaking, the current leaf will be refined if the minimum distance shown by the level set in a leaf is less than the diagonal block length of its child leaf. To avoid complex implementation between leaves at different levels and to ensure numerical stability, the level difference between two adjacent leaves cannot be greater than one. This is called the 2:1 balance. In the GPU implementation of the tree-based AMR method proposed by [PERSON] and [PERSON] [20], there are \(4^{2}\) cells on a leaf. Since the WENO scheme accesses up to two neighboring points, while doing stencil computations, a \((4+2\times halo)^{2}\) shared memory is prepared, as shown in Figure 6. The data in the white-colored area are directly accessed in the global memory and copied to the stencil. The \(4\times 2\) dataset in the X-direction neighbor leaves (presented in light blue) is accessed and copied to the halo region of the stencil. The \(2\times 4\) dataset in the Y-direction neighbor leaves (presented in light red) is accessed and  
Figure 5: The refinement procedure of a two-dimensional tree-based AMR grid and the corresponding quadtree structure. The purple lines and circles represent leaf nodes. copied to the halo region of the stencil. If there is an AMR level difference between leaves, only one time refinement or coarsening is needed because of the 2:1 balance. This again shows the significance of the 2:1 balance, which makes the implementation less complex. The neighbor leaves in the diagonal directions (shown in light gray) are not accessed, so the values stored in the diagonal halo region cells cannot be accessed. In the present research, the reconstruction of the interface with the MYC scheme requires a \(3\times 3\) stencil that includes neighbor cells in diagonal directions. The HF method requires an even larger \(3\times 7\) or \(7\times 3\) stencil. The \(8\times 8\) stencil without a diagonal halo region is not sufficient for such computations. Thus, we propose an extended \(12\times 12\) stencil with a diagonal halo region to satisfy the stencil requirements of the MYC scheme and the HF method, as shown in Figure 7. For the halo region in each direction, a \(4\times 4\) dataset is accessed and copied from the corresponding neighbor leaf. The diagonal neighbor leaves shown in light green are accessed in a 'neighbor of neighbor' way. Since the MYC scheme and HF method are only used near to the interface to calculate the interface's normal vector and curvature, the \(12\times 12\) stencil is only used in these two computations. Additionally, since the AMR grid is interface-adapted, most refined meshes gather near the interface where there is no level difference. Thus, there is only access to diagonal neighbor leaves when the level difference is 0. This helps to reduce the need for computational resources, so the performance is not greatly compromised. Figure 6: The construction of a \(8\times 8\) stencil without a diagonal halo region in the shared memory. The white cells represent \(4\times 4\) leaf cells in a leaf node. The light blue cells represent the halo cells of the stencil taken from the X\(+\) and X\(-\) directions. The light red cells represent the halo cells of the stencil taken from the Y\(+\) and Y\(-\) directions. The light and deep gray cells are not accessed in an \(8\times 8\) stencil without a diagonal halo region.