AMReX-Hydro
AMReX-based hydro routines for low Mach number flows
Loading...
Searching...
No Matches
hydro_plic_K.H
Go to the documentation of this file.
1/**
2 * \file hydro_plic_K.H
3 *
4 * Geometric kernels of the piecewise-linear interface calculation (PLIC).
5 *
6 * The interface in a cell is the plane m.x = alpha in unit-cube cell coordinates, with the liquid
7 * on the side m.x <= alpha (m points out of the liquid). The volume <-> plane-constant relations
8 * are the analytic ones of Scardovelli & Zaleski (JCP 164, 2000).
9 *
10 * Derived from Kynema-SGF (formerly AMR-Wind),
11 * src/equation_systems/vof, BSD 3-Clause License, Copyright (c) 2020, Alliance for Energy
12 * Innovation and Regents of the University of California. Changes: 2D variants, per-face flux
13 * evaluation, AMReX-Hydro conventions, normal schemes as template parameters.
14 *
15 * \addtogroup PLIC
16 * @{
17 */
18
19#ifndef HYDRO_PLIC_K_H
20#define HYDRO_PLIC_K_H
21
22#include <AMReX_Algorithm.H>
23#include <AMReX_Array4.H>
24#include <AMReX_Geometry.H>
25#include <AMReX_REAL.H>
26#include <cmath>
27#include <limits>
28
29namespace PLIC {
30
31using namespace amrex::literals;
32
33// Cells within this of 0 or 1 count as pure. It must stay well above the noise in F: otherwise a
34// cell flips between mixed and pure from one neighbour to the next, which breaks the symmetry of
35// the flow. The noise is about 1e-11 in double precision.
36#ifdef AMREX_USE_FLOAT
37static constexpr amrex::Real pure_tol = 1.0e-4_rt;
38#else
39static constexpr amrex::Real pure_tol = 1.0e-8_rt;
40#endif
41
42/** \brief Whether a cell with volume fraction f holds the interface. */
44 return f > pure_tol && f < 1.0_rt - pure_tol;
45}
46
47/** \brief a, b and c in ascending order, as lo <= mid <= hi. */
49 const amrex::Real b,
50 const amrex::Real c,
51 amrex::Real& lo,
52 amrex::Real& mid,
53 amrex::Real& hi) noexcept {
54 lo = amrex::min(a, b);
55 hi = amrex::max(a, b);
56 mid = c;
57 if (mid < lo) {
58 amrex::Swap(lo, mid);
59 } else if (mid > hi) {
60 amrex::Swap(mid, hi);
61 }
62}
63
64/**
65 * \brief The root of the cubic of volume_intercept in its two upper volume ranges, in the
66 * trigonometric form of Scardovelli & Zaleski, without the constant offset of the range.
67 */
69 const amrex::Real q) noexcept {
70 const amrex::Real p12 = std::sqrt(amrex::max(0.0_rt, p));
71 const amrex::Real pp = p * p12;
72 const amrex::Real teta =
73 std::acos((pp > 0.0_rt) ? amrex::Clamp(q / pp, -1.0_rt, 1.0_rt) : 1.0_rt) / 3.0_rt;
74 const amrex::Real cs = std::cos(teta);
75 return p12 * (std::sqrt(3.0_rt * (1.0_rt - cs * cs)) - cs);
76}
77
78/**
79 * \brief Analytic plane constant alpha such that the region
80 * m1*x1 + m2*x2 + m3*x3 <= alpha of the unit cube has volume volF.
81 * Requires m1,m2,m3 >= 0 and m1+m2+m3 = 1. Any one (2D) or two
82 * (grid-aligned) coefficients may be zero.
83 */
85 const amrex::Real b2,
86 const amrex::Real b3,
87 const amrex::Real volF) {
88 constexpr amrex::Real tiny = std::numeric_limits<amrex::Real>::epsilon();
89
90 amrex::Real m1, m2, m3;
91 sort3(b1, b2, b3, m1, m2, m3);
92
93 // Volume at the breakpoints V1 < V2 < V3
94 const amrex::Real m12 = m1 + m2;
95 const amrex::Real pr = 6.0_rt * m1 * m2 * m3;
96 // Without a corner region (m1 = 0, a 2D or grid-aligned plane), V1 = 0
97 const amrex::Real V1 = (pr > 0.0_rt) ? m1 * m1 * m1 / pr : 0.0_rt;
98 const amrex::Real V2 = V1 + 0.5_rt * (m2 - m1) / (m3 + tiny);
99 amrex::Real V3 = 0.0_rt;
100 if (m3 < m12) {
101 // cut_volume at alpha = m3, without cancellation (m1 > 0)
102 const amrex::Real t2 = m3 - m2;
103 V3 = (3.0_rt * m3 * (m3 - m1) + m1 * m1 - t2 * t2 * (t2 / m1)) / (6.0_rt * m2 * m3);
104 } else {
105 V3 = 0.5_rt * m12 / (m3 + tiny);
106 }
107
108 // Use symmetry to work with ch <= 1/2
109 const amrex::Real ch = amrex::min(volF, 1.0_rt - volF);
110
111 amrex::Real alpha = 0.0_rt;
112 if (ch < V1) {
113 alpha = std::cbrt(pr * ch);
114 } else if (ch < V2) {
115 alpha = 0.5_rt * (m1 + std::sqrt(m1 * m1 + 8.0_rt * m2 * m3 * (ch - V1)));
116 } else if (ch < V3) {
117 const amrex::Real p = 2.0_rt * m1 * m2;
118 const amrex::Real q = 1.5_rt * m1 * m2 * (m12 - 2.0_rt * m3 * ch);
119 alpha = trig_root(p, q) + m12;
120 } else if (m12 < m3) {
121 alpha = m3 * ch + 0.5_rt * m12;
122 } else {
123 const amrex::Real p = m1 * (m2 + m3) + m2 * m3 - 0.25_rt;
124 const amrex::Real q = 1.5_rt * m1 * m2 * m3 * (0.5_rt - ch);
125 alpha = trig_root(p, q) + 0.5_rt;
126 }
127
128 if (volF > 0.5_rt) {
129 alpha = 1.0_rt - alpha;
130 }
131 return alpha;
132}
133
134/**
135 * \brief Liquid volume fraction of the sub-box [r0, r0+dr0] x [0,1] x [0,1]
136 * of the unit cube cut by m1*x1 + m2*x2 + m3*x3 <= alpha. The
137 * result is normalized by the sub-box volume dr0. Coefficients may
138 * have any sign; m3 = 0 gives the 2D result.
139 */
141 const amrex::Real m2,
142 const amrex::Real m3,
143 const amrex::Real alpha,
144 const amrex::Real r0,
145 const amrex::Real dr0) {
146 constexpr amrex::Real tiny = std::numeric_limits<amrex::Real>::epsilon();
147
148 // Move origin to r0, then reflect so that all coefficients are positive
149 amrex::Real al = alpha - m1 * r0;
150 al += amrex::max(0.0_rt, -m1 * dr0) + amrex::max(0.0_rt, -m2) + amrex::max(0.0_rt, -m3);
151
152 // Normalize to the unit cube: n1*y1 + n2*y2 + n3*y3 = al, sum(n) = 1
153 const amrex::Real tmp = amrex::max(tiny, std::abs(m1) * dr0 + std::abs(m2) + std::abs(m3));
154 const amrex::Real n1 = std::abs(m1) / tmp;
155 const amrex::Real n2 = std::abs(m2) / tmp;
156 const amrex::Real n3 = std::abs(m3) / tmp;
157 al = amrex::max(0.0_rt, amrex::min(1.0_rt, al / tmp));
158 const amrex::Real al0 = amrex::min(al, 1.0_rt - al);
159
160 amrex::Real b1, b2, b3;
161 sort3(n1 * dr0, n2, n3, b1, b2, b3);
162 // Components below eps^2 change the volume by less than that, and would underflow
163 constexpr amrex::Real negligible = tiny * tiny;
164 if (b2 < negligible) {
165 b2 = 0.0_rt;
166 }
167 if (b1 < negligible) {
168 b1 = 0.0_rt;
169 }
170 const amrex::Real b12 = b1 + b2;
171
172 // Scardovelli & Zaleski (JCP 2000), as arranged by Kawano (C&F 2016). Between b2 and b12
173 // (where b1 > 0), the cubic is divided through by b1 to avoid cancellation for small b1.
174 const amrex::Real a = al0;
175 amrex::Real v = 0.0_rt;
176 if (a > 0.0_rt) {
177 if (a < b1) {
178 v = a * a * a / (6.0_rt * b1 * b2 * b3);
179 } else if (a < b2) {
180 v = a * (a - b1) / (2.0_rt * b2 * b3) + b1 * b1 / (6.0_rt * b2 * b3);
181 } else if (a < amrex::min(b12, b3)) {
182 const amrex::Real t2 = a - b2;
183 v = (3.0_rt * a * (a - b1) + b1 * b1 - t2 * t2 * (t2 / b1)) / (6.0_rt * b2 * b3);
184 } else if (b3 < b12) {
185 const amrex::Real t2 = a - b2;
186 const amrex::Real t3 = a - b3;
187 v = (3.0_rt * a * (a - b1) + b1 * b1 - t2 * t2 * (t2 / b1) - t3 * t3 * (t3 / b1)) /
188 (6.0_rt * b2 * b3);
189 } else {
190 v = (a - 0.5_rt * b12) / b3;
191 }
192 }
193
194 return (al <= 0.5_rt) ? v : 1.0_rt - v;
195}
196
197/**
198 * \brief Liquid volume fraction of cell (i,j,k) for an implicit surface
199 * phi(x) > 0 inside the liquid, by midpoint sampling on nsub^dim
200 * sub-cells. Intended for problem initialization.
201 */
202template <typename Phi>
204cell_fraction(const int i,
205 const int j,
206 const int k,
207 amrex::GeometryData const& geomdata,
208 Phi const& phi,
209 const int nsub = 8) {
210 const amrex::Real* prob_lo = geomdata.ProbLo();
211 const amrex::Real* dx = geomdata.CellSize();
212 AMREX_ASSERT(nsub > 0);
213 const amrex::Real h = 1.0_rt / static_cast<amrex::Real>(nsub);
214 int inside = 0;
215#if AMREX_SPACEDIM == 3
216 for (int kk = 0; kk < nsub; ++kk) {
217 const amrex::Real z =
218 prob_lo[2] +
219 (static_cast<amrex::Real>(k) + (static_cast<amrex::Real>(kk) + 0.5_rt) * h) * dx[2];
220#else
222 {
223 const amrex::Real z = 0.0_rt;
224#endif
225 for (int jj = 0; jj < nsub; ++jj) {
226 const amrex::Real y =
227 prob_lo[1] +
228 (static_cast<amrex::Real>(j) + (static_cast<amrex::Real>(jj) + 0.5_rt) * h) * dx[1];
229 for (int ii = 0; ii < nsub; ++ii) {
230 const amrex::Real x = prob_lo[0] + (static_cast<amrex::Real>(i) +
231 (static_cast<amrex::Real>(ii) + 0.5_rt) * h) *
232 dx[0];
233 if (phi(x, y, z) > 0.0_rt) {
234 ++inside;
235 }
236 }
237 }
238 }
239 return static_cast<amrex::Real>(inside) /
240 static_cast<amrex::Real>(AMREX_D_TERM(nsub, *nsub, *nsub));
241}
242
243/**
244 * \brief Exact liquid volume fraction of cell (i,j,k) for the half space
245 * n.x <= d (physical coordinates, n need not be normalized).
246 * Intended for problem initialization.
247 */
249plane_fraction(const int i,
250 const int j,
251 const int k,
252 amrex::GeometryData const& geomdata,
253 const amrex::Real n[AMREX_SPACEDIM],
254 const amrex::Real d) {
255 const amrex::Real* prob_lo = geomdata.ProbLo();
256 const amrex::Real* dx = geomdata.CellSize();
257 const int idx[3] = {i, j, k};
258 amrex::Real m[3] = {0.0_rt, 0.0_rt, 0.0_rt};
259 amrex::Real alpha = d;
260 amrex::Real norm = 0.0_rt;
261 for (int a = 0; a < AMREX_SPACEDIM; ++a) {
262 m[a] = n[a] * dx[a];
263 alpha -= n[a] * (prob_lo[a] + static_cast<amrex::Real>(idx[a]) * dx[a]);
264 norm += std::abs(m[a]);
265 }
266 if (norm <= std::numeric_limits<amrex::Real>::min()) {
267 return (alpha >= 0.0_rt) ? 1.0_rt : 0.0_rt;
268 }
269 return amrex::Clamp(
270 cut_volume(m[0] / norm, m[1] / norm, m[2] / norm, alpha / norm, 0.0_rt, 1.0_rt), 0.0_rt,
271 1.0_rt);
272}
273
274} // namespace PLIC
275
276#endif
277/** @}*/
#define AMREX_ASSERT(EX)
#define AMREX_FORCE_INLINE
#define AMREX_GPU_HOST_DEVICE
while parser_exe_t p
#define AMREX_D_TERM(a, b, c)
amrex_real Real
__host__ __device__ void Swap(T &t1, T &t2) noexcept
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
__host__ __device__ constexpr T Clamp(const T &v, const T &lo, const T &hi)
Definition hydro_plic.cpp:9
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real plane_fraction(const int i, const int j, const int k, amrex::GeometryData const &geomdata, const amrex::Real n[AMREX_SPACEDIM], const amrex::Real d)
Exact liquid volume fraction of cell (i,j,k) for the half space n.x <= d (physical coordinates,...
Definition hydro_plic_K.H:249
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real volume_intercept(const amrex::Real b1, const amrex::Real b2, const amrex::Real b3, const amrex::Real volF)
Analytic plane constant alpha such that the region m1*x1 + m2*x2 + m3*x3 <= alpha of the unit cube ha...
Definition hydro_plic_K.H:84
static constexpr amrex::Real pure_tol
Definition hydro_plic_K.H:39
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real cell_fraction(const int i, const int j, const int k, amrex::GeometryData const &geomdata, Phi const &phi, const int nsub=8)
Liquid volume fraction of cell (i,j,k) for an implicit surface phi(x) > 0 inside the liquid,...
Definition hydro_plic_K.H:204
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool is_mixed(const amrex::Real f) noexcept
Whether a cell with volume fraction f holds the interface.
Definition hydro_plic_K.H:43
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real cut_volume(const amrex::Real m1, const amrex::Real m2, const amrex::Real m3, const amrex::Real alpha, const amrex::Real r0, const amrex::Real dr0)
Liquid volume fraction of the sub-box [r0, r0+dr0] x [0,1] x [0,1] of the unit cube cut by m1*x1 + m2...
Definition hydro_plic_K.H:140
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real trig_root(const amrex::Real p, const amrex::Real q) noexcept
The root of the cubic of volume_intercept in its two upper volume ranges, in the trigonometric form o...
Definition hydro_plic_K.H:68
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void sort3(const amrex::Real a, const amrex::Real b, const amrex::Real c, amrex::Real &lo, amrex::Real &mid, amrex::Real &hi) noexcept
a, b and c in ascending order, as lo <= mid <= hi.
Definition hydro_plic_K.H:48
__host__ __device__ void ignore_unused(const Ts &...)
__host__ __device__ T norm(const GpuComplex< T > &a_z) noexcept
__host__ __device__ const Real * CellSize() const noexcept
__host__ __device__ const Real * ProbLo() const noexcept