proteus 1.9.1
C/C++/Fortran libraries
Loading...
Searching...
No Matches
equivalent_polynomials.h
Go to the documentation of this file.
1#ifndef EQUIVALENT_POLYNOMIALS_H
2#define EQUIVALENT_POLYNOMIALS_H
3#include <array>
4#include <cmath>
5#include <cassert>
6#include <cstring>
7#include <iostream>
8#include <iomanip>
9#include <stdexcept>
14
16{
17
18 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis = false>
20 {
21 public:
22 Regularized(bool useExact = false)
23 {
24 (void)useExact;
25 }
26 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale)
27 {
28 (void)phi_dof;
29 (void)phi_nodes;
30 (void)xi_r;
31 (void)ma;
32 (void)mb;
33 (void)jf;
34 (void)isBoundary;
35 (void)scale;
36 return 0;
37 }
38 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, bool isBoundary, bool scale)
39 {
40 return calculate(phi_dof, phi_nodes, xi_r, ma, mb, 0.0, isBoundary, scale);
41 }
42 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, bool isBoundary)
43 {
44 return calculate(phi_dof, phi_nodes, xi_r, 1.0, 1.0, 0.0, isBoundary, false);
45 }
46 inline double *get_normal()
47 {
48 return nullptr;
49 }
50 inline void set_quad(unsigned int q)
51 {
52 (void)q;
53 }
54 inline void set_boundary_quad(unsigned int ebq)
55 {
56 (void)ebq;
57 }
58 inline double H(double eps, double phi)
59 {
60 double h;
61 if (phi > eps)
62 h = 1.0;
63 else if (phi < -eps)
64 h = 0.0;
65 else if (phi == 0.0)
66 h = 0.5;
67 else
68 h = 0.5 * (1.0 + phi / eps + sin(M_PI * phi / eps) / M_PI);
69 return h;
70 }
71 inline double ImH(double eps, double phi)
72 {
73 return 1.0 - H(eps, phi);
74 }
75 inline double D(double eps, double phi)
76 {
77 double d;
78 if (phi > eps)
79 d = 0.0;
80 else if (phi < -eps)
81 d = 0.0;
82 else
83 d = 0.5 * (1.0 + cos(M_PI * phi / eps)) / eps;
84 return d;
85 }
86 inline double VA(int i) { return -1.0; };
87 inline double VA_x(int i) { return -1.0; };
88 inline double VA_y(int i) { return -1.0; };
89 inline double VA_z(int i) { return -1.0; };
90 inline double VB(int i) { return -1.0; };
91 inline double VB_x(int i) { return -1.0; };
92 inline double VB_y(int i) { return -1.0; };
93 inline double VB_z(int i) { return -1.0; };
94 inline double PSIA() { return 0.0; };
95 inline double PSIA_x() { return 0.0; };
96 inline double PSIA_y() { return 0.0; };
97 inline double PSIB() { return 0.0; };
98 inline double PSIB_x() { return 0.0; };
99 inline double PSIB_y() { return 0.0; };
100 };
101
102 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis = false>
104 {
105 public:
106 Simplex(bool useExact = true)
107 {
108 if (nSpace == 1)
109 assert(nDOF == nP + 1);
110 else if (nSpace == 2)
111 assert(nDOF == (nP + 1) * (nP + 2) / 2);
112 else if (nSpace == 3)
113 assert(nDOF == (nP + 1) * (nP + 2) * (nP + 3) / 6);
114 else
115 assert(false);
117 for (int i = 0; i < nSpace; i++)
118 level_set_normal[i] = 0.0;
119 level_set_normal[0] = 1.0;
120 }
121
122 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale);
123
124 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, bool isBoundary, bool scale)
125 {
126 return calculate(phi_dof, phi_nodes, xi_r, ma, mb, 0.0, isBoundary, false);
127 }
128
129 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, bool isBoundary)
130 {
131 return calculate(phi_dof, phi_nodes, xi_r, 1.0, 1.0, 0.0, isBoundary, false);
132 }
133
134 inline void set_quad(unsigned int q)
135 {
136 assert(q >= 0);
137 assert(q < nQ);
138 // The edge/corner special cases are an IFEM addition: pre-IFEM these degenerate
139 // cells still took their H/ImH/D straight from the moment fit, and forcing D = 0
140 // here removes the interface measure that MCorr's mass correction integrates
141 // over (vortex2D_exactHeaviside). Keep them on the IFEM path only.
142 if (useIfemBasis && (edge == -1 || corner == -1))
143 {
144 _H_q = 0.0;
145 _ImH_q = 1.0;
146 _D_q = 0.0;
147 }
148 else if (useIfemBasis && (edge == 1 || corner == 1))
149 {
150 _H_q = 1.0;
151 _ImH_q = 0.0;
152 _D_q = 0.0;
153 }
154 else if (inside_out)
155 {
156 _H_q = _ImH[q];
157 _ImH_q = _H[q];
158 _D_q = _D[q];
159 }
160 else
161 {
162 _H_q = _H[q];
163 _ImH_q = _ImH[q];
164 _D_q = _D[q];
165 }
166 // basis functions already adjusted for inside_out
167 for (int i = 0; i < nP_ifem; i++)
168 {
169 _va_q[i] = _va[q * nP_ifem + i];
170 _vb_q[i] = _vb[q * nP_ifem + i];
171 }
172 for (int i = 0; i < nP_ifem; i++)
173 {
174 _va_x_q[i] = _va_x[q * nP_ifem + i];
175 _va_y_q[i] = _va_y[q * nP_ifem + i];
176 _va_z_q[i] = _va_z[q * nP_ifem + i];
177 _vb_x_q[i] = _vb_x[q * nP_ifem + i];
178 _vb_y_q[i] = _vb_y[q * nP_ifem + i];
179 _vb_z_q[i] = _vb_z[q * nP_ifem + i];
180 }
181 const bool cut = !(edge == -1 || corner == -1 || edge == 1 || corner == 1);
182 for (int i = 0; i < 6; i++)
183 _psi_q[i] = cut ? _psi[q * 6 + i] : 0.0;
184 }
185
186 inline void set_boundary_quad(unsigned int ebq)
187 {
188 assert(ebq >= 0);
189 assert(ebq < nEBQ);
190 // The edge/corner special cases are an IFEM addition: pre-IFEM these degenerate
191 // cells still took their H/ImH/D straight from the moment fit, and forcing D = 0
192 // here removes the interface measure that MCorr's mass correction integrates
193 // over (vortex2D_exactHeaviside). Keep them on the IFEM path only.
194 if (useIfemBasis && (edge == -1 || corner == -1))
195 {
196 _H_q = 0.0;
197 _ImH_q = 1.0;
198 _D_q = 0.0;
199 }
200 else if (useIfemBasis && (edge == 1 || corner == 1))
201 {
202 _H_q = 1.0;
203 _ImH_q = 0.0;
204 _D_q = 0.0;
205 }
206 else if (inside_out)
207 {
208 _H_q = _ImH_ebq[ebq];
209 _ImH_q = _H_ebq[ebq];
210 _D_q = _D_ebq[ebq];
211 }
212 else
213 {
214 _H_q = _H_ebq[ebq];
215 _ImH_q = _ImH_ebq[ebq];
216 _D_q = _D_ebq[ebq];
217 }
218 // basis functions already adjusted for inside_out
219 for (int i = 0; i < nP_ifem; i++)
220 {
221 _va_q[i] = _va_ebq[ebq * nP_ifem + i];
222 _vb_q[i] = _vb_ebq[ebq * nP_ifem + i];
223 }
224 for (int i = 0; i < nP_ifem; i++)
225 {
226 _va_x_q[i] = _va_x_ebq[ebq * nP_ifem + i];
227 _va_y_q[i] = _va_y_ebq[ebq * nP_ifem + i];
228 _vb_x_q[i] = _vb_x_ebq[ebq * nP_ifem + i];
229 _vb_y_q[i] = _vb_y_ebq[ebq * nP_ifem + i];
230 }
231 const bool cut = !(edge == -1 || corner == -1 || edge == 1 || corner == 1);
232 for (int i = 0; i < 6; i++)
233 _psi_q[i] = cut ? _psi_ebq[ebq * 6 + i] : 0.0;
234 }
235
236 inline double *get_H() { return _H; };
237 inline double *get_ImH() { return _ImH; };
238 inline double *get_D() { return _D; };
239 inline double H(double eps, double phi) { return _H_q; };
240 inline double ImH(double eps, double phi) { return _ImH_q; };
241 inline double D(double eps, double phi) { return _D_q; };
242 inline double VA(int i) { return _va_q[i]; };
243 inline double VA_x(int i) { return _va_x_q[i]; };
244 inline double VA_y(int i) { return _va_y_q[i]; };
245 inline double VA_z(int i) { return _va_z_q[i]; };
246 inline double VB(int i) { return _vb_q[i]; };
247 inline double VB_x(int i) { return _vb_x_q[i]; };
248 inline double VB_y(int i) { return _vb_y_q[i]; };
249 inline double VB_z(int i) { return _vb_z_q[i]; };
250 // Unit flux-jump lift psi (branch a / branch b, value and physical gradient) at the current
251 // quadrature point: zero at every node, continuous across the interface, and
252 // mb*dpsi_b/dn - ma*dpsi_a/dn = 1 with n the interface normal (from branch a to branch b).
253 // The basis itself is always built for a homogeneous flux jump; a prescribed jump J_f is
254 // represented by adding J_f*psi to the trial function once (like the solution-jump lift).
255 inline double PSIA() { return _psi_q[0]; };
256 inline double PSIA_x() { return _psi_q[1]; };
257 inline double PSIA_y() { return _psi_q[2]; };
258 inline double PSIB() { return _psi_q[3]; };
259 inline double PSIB_x() { return _psi_q[4]; };
260 inline double PSIB_y() { return _psi_q[5]; };
261 inline double *get_normal()
262 {
263 return level_set_normal;
264 }
265 // Value-initialised: gf_f_cache/gf_s_cache in ADR.h are built once with
266 // assign(n, GfType()) and reused, so any entry whose calculate() was not
267 // invoked in the current pass is read with whatever the default-constructed
268 // temporary happened to hold. edge/corner in particular are *branched on*
269 // in set_quad, which turns indeterminate bytes into a discretely different
270 // H/ImH/D and a discretely different Jacobian.
271 bool inside_out = false, quad_cut = false;
272 static const unsigned int nN = nSpace + 1;
273 // Sized for both of its roles: the cut geometry needs one corrected value per
274 // topological vertex (nN), while the P2 IFEM basis needs one per trial dof
275 // (nP_ifem = 6). Non-ADR callers instantiate with nP_ifem < nN -- VOF/MCorr
276 // with 2, RANS3PF with 1 -- so sizing by nP_ifem alone left the vertex entries
277 // past the end of the array, and _calculate_cuts read them back as garbage.
278 static const unsigned int nDOF_phi = (useIfemBasis && nP_ifem > nN) ? nP_ifem : nN;
280 double cut_barycenter[3] = {0., 0., 0.};
281 int edge = 0, corner = 0;
282 bool split = false;
283 bool flip_the_cell = false;
284 // set when the P2 cut logic deliberately reverses the cell orientation (see
285 // _calculate_permutation); its D already has the intended sign, so the
286 // orientation correction in _calculate_C must not be applied again.
287 bool p2_cell_flipped = false;
288
289 private:
290 int P2_ifem_case = 0;
291 // These eight are the arrays VA()/VB()/VA_x()/... hand straight back to the
292 // caller, so they are the read path for the SCIFEM facet terms in ADR.h.
293 // 3567c2a3 initialised the storage arrays (_va, _va_ebq, _a*, _b*) but left
294 // this scratch row alone -- only the three scalars ahead of it got values.
295 double _H_q = 0., _ImH_q = 0., _D_q = 0., _va_q[nP_ifem] = {}, _vb_q[nP_ifem] = {},
296 _va_x_q[nP_ifem] = {}, _va_y_q[nP_ifem] = {}, _va_z_q[nP_ifem] = {},
297 _vb_x_q[nP_ifem] = {}, _vb_y_q[nP_ifem] = {}, _vb_z_q[nP_ifem] = {};
298 // Sized nDOF_phi for the same reason as phi_dof_corrected: the cut geometry
299 // permutes nN topological vertices, while the P2 IFEM basis also permutes its
300 // mid-side dofs (nP_ifem = 6). Sizing by nP_ifem alone left permutation[nN-1]
301 // unwritten for the nP_ifem < nN callers (VOF/MCorr 2, RANS3PF 1), and
302 // _calculate_cuts then indexed phi_dof_corrected with that garbage.
303 unsigned int root_node = 0, permutation[nDOF_phi] = {};
304 // Same nN-vs-nP_ifem split as phi_dof_corrected/permutation: the cut geometry
305 // needs one entry per topological vertex (the Jacobian reads nodes[(1+i)*3+I]
306 // for i < nN-1), while the P2 IFEM basis needs one per trial dof. Sizing by
307 // nP_ifem alone left the last vertex unfilled for nP_ifem < nN callers and the
308 // Jacobian was built from memory past the end of nodes[].
309 double phi[nDOF_phi] = {}, nodes[nDOF_phi * 3] = {};
310 double _a1[nP_ifem] = {}, _a2[nP_ifem] = {}, _a3[nP_ifem] = {}, _b1[nP_ifem] = {}, _b2[nP_ifem] = {}, _b3[nP_ifem] = {};
311 double _a4[nP_ifem] = {}, _a5[nP_ifem] = {}, _a6[nP_ifem] = {}, _b4[nP_ifem] = {}, _b5[nP_ifem] = {}, _b6[nP_ifem] = {};
312 // unit flux-jump lift: coefficients (same monomial order as _a1.._a6) and values/gradients
313 // [psi_a, psi_a_x, psi_a_y, psi_b, psi_b_x, psi_b_y] at the quadrature points
314 double _lift_a[6] = {0., 0., 0., 0., 0., 0.}, _lift_b[6] = {0., 0., 0., 0., 0., 0.};
315 double _psi[nQ * 6] = {}, _psi_ebq[nEBQ * 6] = {}, _psi_q[6] = {0., 0., 0., 0., 0., 0.};
316 inline void _calculate_lift(const double *xi, double *psi);
317 double Jac[nSpace * nSpace] = {}, inv_Jac[nSpace * nSpace] = {}, det_Jac = 0.;
318 double level_set_normal[nSpace], X_0[nSpace], phys_nodes_cut[(nN - 1) * 3], THETA_01, THETA_02, THETA_31, THETA_32, phys_nodes_cut_quad_01[3], phys_nodes_cut_quad_02[3], phys_nodes_cut_quad_31[3], phys_nodes_cut_quad_32[3];
319 static const unsigned int nDOF = ((nSpace - 1) / 2) * (nSpace - 2) * (nP + 1) * (nP + 2) * (nP + 3) / 6 + (nSpace - 1) * (3 - nSpace) * (nP + 1) * (nP + 2) / 2 + (2 - nSpace) * ((3 - nSpace) / 2) * (nP + 1);
320 double Ainv[nDOF * nDOF];
321 double C_H[nDOF] = {}, C_ImH[nDOF] = {}, C_D[nDOF] = {};
322 inline int _calculate_permutation(const double *phi_dof, const double *phi_nodes);
323 inline void _calculate_cuts();
324 inline void _calculate_cuts_quad();
325 inline void _calculate_C();
326 inline void _correct_phi(const double *phi_dof, const double *phi_nodes);
327 double _H[nQ] = {}, _ImH[nQ] = {}, _D[nQ] = {}, _va[nQ * nP_ifem] = {}, _vb[nQ * nP_ifem] = {};
328 double _H_ebq[nEBQ] = {}, _ImH_ebq[nEBQ] = {}, _D_ebq[nEBQ] = {}, _va_ebq[nEBQ * nP_ifem] = {}, _vb_ebq[nEBQ * nP_ifem] = {}; // cek hack: this is confusing because we use no suffice for the q arrays and _ebq for the ebq arrays, then use _q above for generic quad point
329 double _va_x[nQ * nP_ifem] = {}, _va_y[nQ * nP_ifem] = {}, _va_z[nQ * nP_ifem] = {}, _vb_x[nQ * nP_ifem] = {}, _vb_y[nQ * nP_ifem] = {}, _vb_z[nQ * nP_ifem] = {};
330 double _va_x_ebq[nEBQ * nP_ifem] = {}, _va_y_ebq[nEBQ * nP_ifem] = {}, _va_z_ebq[nEBQ * nP_ifem] = {}, _vb_x_ebq[nEBQ * nP_ifem] = {}, _vb_y_ebq[nEBQ * nP_ifem] = {}, _vb_z_ebq[nEBQ * nP_ifem] = {};
331 inline void _calculate_basis_coefficients(const double ma, const double mb, const double jf);
332 inline void _calculate_basis(const double *xi, double *va, double *vb);
333 inline void _calculate_basis_gradients(const double *xi, double *va_x, double *va_y, double *vb_x, double *vb_y);
334 };
335
336 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
337 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_C()
338 {
339 double b_H[nDOF], b_ImH[nDOF], b_dH[nDOF * nSpace], b_D[nDOF * nSpace];
340 if (quad_cut)
341 {
342 _calculate_b<nP>(THETA_01, THETA_02, THETA_31, THETA_32,
343 phi_dof_corrected[permutation[0]],
344 phi_dof_corrected[permutation[1]],
345 phi_dof_corrected[permutation[2]],
346 phi_dof_corrected[permutation[3]],
347 b_H, b_ImH, b_D);
348 if (inside_out) // todo handle insdie out for H/ImH/D in a simplified/unified way
349 {
350 for (unsigned int i = 0; i < nDOF; i++)
351 {
352 b_D[i] = -b_D[i];
353 }
354 }
355 for (unsigned int i = 0; i < nDOF; i++)
356 {
357 C_H[i] = 0.0;
358 C_ImH[i] = 0.0;
359 C_D[i] = 0.0;
360 for (unsigned int j = 0; j < nDOF; j++)
361 {
362 assert(!std::isnan(Ainv[i * nDOF + j]));
363 assert(!std::isnan(b_H[j]));
364 assert(!std::isnan(b_ImH[j]));
365 assert(!std::isnan(b_D[j]));
366 C_H[i] += Ainv[i * nDOF + j] * b_H[j];
367 C_ImH[i] += Ainv[i * nDOF + j] * b_ImH[j];
368 C_D[i] += Ainv[i * nDOF + j] * b_D[j];
369 }
370 // only if direct boundary integral is used
371 // C_D[i] /= det_Jac;
372 }
373 }
374 else
375 {
376 _calculate_b<nSpace, nP>(X_0, b_H, b_ImH, b_dH);
377
378 double Jt_dphi_dx[nSpace];
379 for (unsigned int I = 0; I < nSpace; I++)
380 {
381 Jt_dphi_dx[I] = 0.0;
382 for (unsigned int J = 0; J < nSpace; J++)
383 Jt_dphi_dx[I] += Jac[J * nSpace + I] * level_set_normal[J];
384 }
385 for (unsigned int i = 0; i < nDOF; i++)
386 {
387 C_H[i] = 0.0;
388 C_ImH[i] = 0.0;
389 C_D[i] = 0.0;
390 for (unsigned int j = 0; j < nDOF; j++)
391 {
392 C_H[i] += Ainv[i * nDOF + j] * b_H[j];
393 C_ImH[i] += Ainv[i * nDOF + j] * b_ImH[j];
394 for (unsigned int I = 0; I < nSpace; I++)
395 {
396 if (fabs(Jt_dphi_dx[I]) > 0.0)
397 C_D[i] -= Ainv[i * nDOF + j] * b_dH[j * nSpace + I] / (Jt_dphi_dx[I]);
398 }
399 }
400 }
401 // The Dirac is a surface measure and must be independent of the caller's node ordering,
402 // but C_D above inherits sign(det_Jac) through Jt_dphi_dx (built from Jac). Elements
403 // handed in with negative orientation therefore produced a sign-flipped D (test_2D/3D).
404 // Correct the sign here rather than reordering the permutation: the permutation feeds
405 // solve_ifem_basis_coefficients, and reordering it changes the P2 IFEM basis.
406 if (det_Jac < 0.0 && !p2_cell_flipped)
407 for (unsigned int i = 0; i < nDOF; i++)
408 C_D[i] = -C_D[i];
409 }
410 }
411
412 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
413 inline int Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_permutation(const double *phi_dof, const double *phi_nodes)
414 {
415 if (flip_the_cell)
416 {
417 int temp = permutation[1];
418 permutation[1] = permutation[2];
419 permutation[2] = temp;
420 temp = permutation[3];
421 permutation[3] = permutation[5];
422 permutation[5] = temp;
423 flip_the_cell = false;
424 p2_cell_flipped = true;
425
426 }
427 else
428 {
429 int p_i, pcount = 0, n_i, ncount = 0, z_i, zcount = 0;
430 corner = 0;
431 edge = 0;
432 root_node = 0;
433 inside_out = false;
434 quad_cut = false;
435
436 const double eps = 1.0e-8;
437
438 for (unsigned int i = 0; i < nN; i++)
439 {
440 if (phi_dof[i] > eps)
441 {
442 if (pcount == 0)
443 p_i = i;
444 pcount += 1;
445 }
446 else if (phi_dof[i] < -eps)
447 {
448 if (ncount == 0)
449 n_i = i;
450 ncount += 1;
451 }
452 else
453 {
454 if (zcount == 0)
455 z_i = i;
456 zcount += 1;
457 }
458 }
459 if (pcount == nN)
460 {
461 // All positive: element is fully in the +1 domain
462 // std::cout << "This is a fully positive element." << std::endl;
463 return 1;
464 }
465 else if (ncount == nN)
466 {
467 // All negative: element is fully in the -1 domain
468 // std::cout << "This is a fully negative element." << std::endl;
469 return -1;
470 }
471 else if (ncount == 1)
472 {
473 if (zcount == nN - 1) // for P1 ifem, interface is on an element boundary and the element is fully in the -1 domain.
474 {
475 edge = -1;
476 }
477 else if (zcount == 1 && pcount == 1)
478 {
479 split = true;
480 }
481 root_node = n_i;
482 }
483 else if (pcount == 1)
484 {
485 if (zcount == nN - 1) // for P1 ifem, interface is on an element boundary and the element is fully in the +1 domain.
486 {
487 edge = 1;
488 }
489 root_node = p_i;
490 inside_out = true;
491 }
492 else if (nSpace == 3 && pcount == 2 && ncount == 2)
493 {
494 // special case only in 3D
495 quad_cut = true;
496 root_node = n_i;
497 }
498 else
499 {
500 assert(zcount < nN - 1);
501 if (pcount && !ncount)
502 {
503 corner = 1; // The interface passes through a corner node and element is in + side
504 assert(pcount == nN - 1);
505 root_node = z_i;
506 // ADR's P2 IFEM basis needs the ma/mb swap here, but pre-IFEM this branch
507 // left inside_out false, and set_quad uses it to exchange H and ImH -- which
508 // inverts the phase fractions MCorr/VOF integrate. IFEM path only.
509 if (useIfemBasis)
510 inside_out = true;
511 }
512 else if (ncount && !pcount)
513 {
514 corner = -1; // The interface passes through a corner node and element is in - side
515 assert(ncount == nN - 1);
516 // Pre-IFEM this rooted at n_i; the IFEM basis wants z_i. Keep the old root for
517 // the non-IFEM callers so their permutation/Jacobian are unchanged.
518 root_node = useIfemBasis ? z_i : n_i;
519 }
520 else
521 assert(false);
522 }
523 for (unsigned int i = 0; i < nDOF_phi; i++)
524 {
525 // Works for both P1 and P2 IFEM. The loop bound is nDOF_phi = max(nN, nP_ifem):
526 // the geometry always needs all nN corner entries, while P2 IFEM needs its
527 // mid-side dofs on top of them. Bounding by nP_ifem alone left the corner
528 // entries past nP_ifem unwritten whenever nP_ifem < nN (VOF/MCorr 2,
529 // RANS3PF 1), and _calculate_cuts read them back as garbage indices.
530 // the cycling modulus is nN (topological vertices): the first nN entries rotate the
531 // corner nodes starting at the root, and the P2 mid-side DOFs shadow them at offset nN.
532 // The modulus must NOT be a literal 3 -- that is only nN for a triangle, and on a
533 // tetrahedron (nN=4) it produced out-of-range indices such as 3+(2+3)%3 = 5, giving a
534 // garbage Jacobian and nan H/ImH/D (test_3D, phi = [1,1,-1,-1]).
535 if (i < nN)
536 permutation[i] = (root_node + i) % nN;
537 else
538 permutation[i] = nN + (root_node + i) % nN;
539 }
540 if (quad_cut)
541 {
542 if (phi_dof[permutation[nN - 1]] > 0.0)
543 {
544 int tmp = permutation[nN - 1];
545 if (phi_dof[permutation[nN - 2]] < 0.0)
546 {
547 permutation[nN - 1] = permutation[nN - 2];
548 permutation[nN - 2] = tmp;
549 }
550 else if (phi_dof[permutation[nN - 3]] < 0.0)
551 {
552 permutation[nN - 1] = permutation[nN - 3];
553 permutation[nN - 3] = tmp;
554 }
555 else
556 assert(false);
557 }
558 assert(phi_dof[permutation[0]] < 0.0);
559 assert(phi_dof[permutation[3]] < 0.0);
560 assert(phi_dof[permutation[1]] > 0.0);
561 assert(phi_dof[permutation[2]] > 0.0);
562 }
563 }
564 for (unsigned int i = 0; i < nDOF_phi; i++)
565 {
566 phi[i] = phi_dof[permutation[i]];
567 for (unsigned int I = 0; I < 3; I++)
568 {
569 nodes[i * 3 + I] = phi_nodes[permutation[i] * 3 + I]; // nodes always 3D
570
571 }
572 }
573 double JacTest[nSpace * nSpace];
574 for (unsigned int I = 0; I < nSpace; I++)
575 {
576 for (unsigned int i = 0; i < nN - 1; i++)
577 {
578 Jac[I * nSpace + i] = nodes[(1 + i) * 3 + I] - nodes[I];
579 JacTest[I * nSpace + i] = phi_nodes[(1 + i) * 3 + I] - phi_nodes[I];
580 }
581 }
582 det_Jac = det<nSpace>(Jac);
583 double det_JacTest = det<nSpace>(JacTest);
584 /* assert(det_JacTest >= 0.0); */
585 /* assert(det_Jac >= 0.0); */
586
587 if (det_Jac < 0.0 && flip_the_cell)
588 {
589 if (quad_cut) // flip the two internal positive nodes
590 {
591 double tmp = permutation[2];
592 permutation[2] = permutation[1];
593 permutation[1] = tmp;
594 }
595 else // flip the last two nodes
596 {
597 double tmp = permutation[nN - 1];
598 permutation[nN - 1] = permutation[nN - 2];
599 permutation[nN - 2] = tmp;
600 }
601 for (unsigned int i = 0; i < nDOF_phi; i++)
602 {
603 phi[i] = phi_dof[permutation[i]];
604 for (unsigned int I = 0; I < 3; I++)
605 {
606 nodes[i * 3 + I] = phi_nodes[permutation[i] * 3 + I]; // nodes always 3D
607 }
608 }
609 for (unsigned int i = 0; i < nN - 1; i++)
610 for (unsigned int I = 0; I < nSpace; I++)
611 Jac[I * nSpace + i] = nodes[(1 + i) * 3 + I] - nodes[I];
612 det_Jac = det<nSpace>(Jac);
613 assert(det_Jac > 0);
614 if (nSpace == 1)
615 inside_out = true;
616 }
617 inv<nSpace>(Jac, inv_Jac);
618 return 0;
619 }
620
621 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
622 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_cuts()
623 {
624 const double eps = 1.0e-8;
625 for (unsigned int i = 0; i < nN - 1; i++)
626 {
627 if (useIfemBasis && (corner == 1 || corner == -1))
628 {
629 X_0[i] = 0.0;
630 for (unsigned int I = 0; I < 3; I++)
631 {
632 phys_nodes_cut[i * 3 + I] = nodes[I];
633 }
634 }
635 else if (phi[i + 1] * phi[0] < 0.0)
636 {
637 X_0[i] = 0.5 - 0.5 * (phi[i + 1] + phi[0]) / (phi[i + 1] - phi[0]);
638 assert(X_0[i] <= 1.0);
639 assert(X_0[i] >= 0.0);
640 for (unsigned int I = 0; I < 3; I++)
641 {
642 phys_nodes_cut[i * 3 + I] = (1 - X_0[i]) * nodes[I] + X_0[i] * nodes[(1 + i) * 3 + I];
643 }
644 }
645 else
646 {
647 if (phi[i + 1] < eps)
648 {
649 X_0[i] = 1.0;
650 for (unsigned int I = 0; I < 3; I++)
651 {
652 phys_nodes_cut[i * 3 + I] = nodes[(1 + i) * 3 + I];
653 }
654 }
655 else
656 {
657 X_0[i] = 0.0;
658 for (unsigned int I = 0; I < 3; I++)
659 {
660 phys_nodes_cut[i * 3 + I] = nodes[I];
661 }
662 }
663 }
664 }
665 // std::cout << "X_0: \t" << X_0[0] << ", " << X_0[1] << std::endl;
666 // std::cout << "phys_nodes_cut: " << std::endl;
667 // for (unsigned int i = 0; i < nN - 1; i++)
668 // {
669 // std::cout << "phys_nodes_cut[" << i << "]: \t" << phys_nodes_cut[i * 3 + 0] << ", " << phys_nodes_cut[i * 3 + 1] << ", " << phys_nodes_cut[i * 3 + 2] << std::endl;
670 // }
671 // Redo the permutation for the case: (x0>0.5 and y0<=0.5)
672 if (nP_ifem == 6 && (X_0[0] > 0.5 && X_0[1] <= 0.5))
673 {
674 flip_the_cell = true;
675 }
676 }
677
678 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
679 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_cuts_quad()
680 {
681 const double eps = 1.0e-8, Imeps = 1.0 - eps;
682 THETA_01 = 0.5 - 0.5 * (phi[1] + phi[0]) / (phi[1] - phi[0]);
683 THETA_02 = 0.5 - 0.5 * (phi[2] + phi[0]) / (phi[2] - phi[0]);
684 THETA_31 = 0.5 - 0.5 * (phi[1] + phi[3]) / (phi[1] - phi[3]);
685 THETA_32 = 0.5 - 0.5 * (phi[2] + phi[3]) / (phi[2] - phi[3]);
686 if ((THETA_01 < eps || THETA_01 > Imeps) || (THETA_02 < eps || THETA_02 > Imeps) || (THETA_31 < eps || THETA_31 > Imeps) || (THETA_32 < eps || THETA_32 > Imeps))
687 {
688 THETA_01 = fmin(Imeps, fmax(eps, 0.5 - 0.5 * (phi[1] + phi[0]) / (phi[1] - phi[0])));
689 THETA_02 = fmin(Imeps, fmax(eps, 0.5 - 0.5 * (phi[2] + phi[0]) / (phi[2] - phi[0])));
690 THETA_31 = fmin(Imeps, fmax(eps, 0.5 - 0.5 * (phi[1] + phi[3]) / (phi[1] - phi[3])));
691 THETA_32 = fmin(Imeps, fmax(eps, 0.5 - 0.5 * (phi[2] + phi[3]) / (phi[2] - phi[3])));
692 }
693 for (unsigned int I = 0; I < 3; I++)
694 {
695 phys_nodes_cut_quad_01[I] = (1 - THETA_01) * nodes[I] + THETA_01 * nodes[1 * 3 + I];
696 phys_nodes_cut_quad_02[I] = (1 - THETA_02) * nodes[I] + THETA_02 * nodes[2 * 3 + I];
697 phys_nodes_cut_quad_31[I] = (1 - THETA_31) * nodes[3 * 3 + I] + THETA_31 * nodes[1 * 3 + I];
698 phys_nodes_cut_quad_32[I] = (1 - THETA_32) * nodes[3 * 3 + I] + THETA_32 * nodes[2 * 3 + I];
699 }
700 }
701
702 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
703 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_correct_phi(const double *phi_dof, const double *phi_nodes)
704 {
705 memset(cut_barycenter, 0, 3 * sizeof(double));
706 const double one_by_nNm1 = 1.0 / (nN - 1.0);
707 if (quad_cut)
708 {
709 for (unsigned int I = 0; I < nSpace; I++)
710 cut_barycenter[I] += 0.25 * (phys_nodes_cut_quad_01[I] +
711 phys_nodes_cut_quad_02[I] +
712 phys_nodes_cut_quad_31[I] +
713 phys_nodes_cut_quad_32[I]);
714 }
715 else
716 {
717 for (unsigned int i = 0; i < nN - 1; i++)
718 {
719 assert(!std::isnan(phys_nodes_cut[i * 3 + 0]));
720 assert(!std::isnan(phys_nodes_cut[i * 3 + 1]));
721 assert(!std::isnan(phys_nodes_cut[i * 3 + 2]));
722 for (unsigned int I = 0; I < nSpace; I++)
723 cut_barycenter[I] += phys_nodes_cut[i * 3 + I] * one_by_nNm1;
724 }
725 }
726 for (unsigned int i = 0; i < nDOF_phi; i++)
727 {
728 phi_dof_corrected[i] = 0.0;
729 for (unsigned int I = 0; I < nSpace; I++)
730 {
731 phi_dof_corrected[i] += level_set_normal[I] * (phi_nodes[i * 3 + I] - cut_barycenter[I]);
732 }
733 // ensure sdf sign convention consistent with input phi
734 if (phi_dof_corrected[i] * phi_dof[i] < 0.0)
735 {
736 phi_dof_corrected[i] *= -1.0;
737 }
738 }
739 }
740
741 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
742 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_basis_coefficients(const double ma, const double mb, const double jf)
743 {
744 assert(nSpace == 2);
745 assert(nN == 3);
746 double nx = 0.0, ny = 0.0;
747 if (inside_out)
748 {
749 nx = -level_set_normal[0];
750 ny = -level_set_normal[1];
751 }
752 else
753 {
754 nx = level_set_normal[0];
755 ny = level_set_normal[1];
756 }
757 double Jit00 = inv_Jac[0 * nSpace + 0],
758 Jit01 = inv_Jac[1 * nSpace + 0],
759 Jit10 = inv_Jac[0 * nSpace + 1],
760 Jit11 = inv_Jac[1 * nSpace + 1];
761 double x0 = X_0[0],
762 y0 = X_0[1];
763 const double *vall = nullptr;
764
765
766 switch (nP_ifem)
767 {
768 case 3:
769 {
770 static const double vall_p1[9] = {
771 1., 0., 0.,
772 0., 1., 0.,
773 0., 0., 1.};
774 vall = vall_p1;
775 for (int j = 0; j < 3; j++)
776 {
777 int i = permutation[j];
778 double v[3] = {0.0, 0.0, 0.0};
779 v[0] = vall[j * 3 + 0];
780 v[1] = vall[j * 3 + 1];
781 v[2] = vall[j * 3 + 2];
782
783 if (corner || edge)
784 {
785 _a1[i] = v[0];
786 _a2[i] = -v[0] + v[1];
787 _a3[i] = -v[0] + v[2];
788 _b1[i] = v[0];
789 _b2[i] = -v[0] + v[1];
790 _b3[i] = -v[0] + v[2];
791 }
792 else
793 {
794 // nathawani: Implement inside out case directly here.
795 const std::array<double, 3> nodal_values = {v[0], v[1], v[2]};
796 if (inside_out)
797 {
798 const std::array<double, 6> coeffs = proteus::solve_ifem_basis_coefficients(
799 1, x0, y0, nx, ny, mb, ma, 0.0, Jit00, Jit01, Jit10, Jit11, nodal_values);
800
801 _b1[i] = coeffs[0];
802 _b2[i] = coeffs[1];
803 _b3[i] = coeffs[2];
804 _a1[i] = coeffs[3];
805 _a2[i] = coeffs[4];
806 _a3[i] = coeffs[5];
807 }
808 else
809 {
810
811 const std::array<double, 6> coeffs = proteus::solve_ifem_basis_coefficients(
812 1, x0, y0, nx, ny, ma, mb, 0.0, Jit00, Jit01, Jit10, Jit11, nodal_values);
813
814 _a1[i] = coeffs[0];
815 _a2[i] = coeffs[1];
816 _a3[i] = coeffs[2];
817 _b1[i] = coeffs[3];
818 _b2[i] = coeffs[4];
819 _b3[i] = coeffs[5];
820 }
821 }
822 }
823 break;
824 }
825 case 6:
826 {
827 static const double vall_p2[36] = {
828 1., 0., 0., 0., 0., 0.,
829 0., 1., 0., 0., 0., 0.,
830 0., 0., 1., 0., 0., 0.,
831 0., 0., 0., 1., 0., 0.,
832 0., 0., 0., 0., 1., 0.,
833 0., 0., 0., 0., 0., 1.};
834 vall = vall_p2;
835
836 for (int j = 0; j < 6; j++)
837 {
838 int i = permutation[j];
839 double v[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};//, grad_va[2] = {0.0, 0.0}, grad_vb[2] = {0.0, 0.0}, grad_va_ref[2] = {0.0, 0.0}, grad_vb_ref[2] = {0.0, 0.0};
840
841 v[0] = vall[j * 6 + 0];
842 v[1] = vall[j * 6 + 1];
843 v[2] = vall[j * 6 + 2];
844 v[3] = vall[j * 6 + 3];
845 v[4] = vall[j * 6 + 4];
846 v[5] = vall[j * 6 + 5];
847
848 if (corner || edge)
849 {
850 _a1[i] = v[0];
851 _a2[i] = - 3 * v[0] - v[1] + 4 * v[3];
852 _a3[i] = - 3 * v[0] - v[2] + 4 * v[5];
853 _a4[i] = 4 * v[0] - 4 * v[3] + 4 * v[4] - 4 * v[5];
854 _a5[i] = 2 * v[0] + 2 * v[1] - 4 * v[3];
855 _a6[i] = 2 * v[0] + 2 * v[2] - 4 * v[5];
856 _b1[i] = v[0];
857 _b2[i] = - 3 * v[0] - v[1] + 4 * v[3];
858 _b3[i] = - 3 * v[0] - v[2] + 4 * v[5];
859 _b4[i] = 4 * v[0] - 4 * v[3] + 4 * v[4] - 4 * v[5];
860 _b5[i] = 2 * v[0] + 2 * v[1] - 4 * v[3];
861 _b6[i] = 2 * v[0] + 2 * v[2] - 4 * v[5];
862 }
863 else
864 {
865 if (inside_out)
866 {
867 const std::array<double, 6> nodal_values = {v[0], v[1], v[2], v[3], v[4], v[5]};
868 const std::array<double, 12> coeffs = proteus::solve_ifem_basis_coefficients(
869 2, x0, y0, nx, ny, mb, ma, 0.0, Jit00, Jit01, Jit10, Jit11, nodal_values);
870
871 _b1[i] = coeffs[0];
872 _b2[i] = coeffs[1];
873 _b3[i] = coeffs[2];
874 _b4[i] = coeffs[3];
875 _b5[i] = coeffs[4];
876 _b6[i] = coeffs[5];
877 _a1[i] = coeffs[6];
878 _a2[i] = coeffs[7];
879 _a3[i] = coeffs[8];
880 _a4[i] = coeffs[9];
881 _a5[i] = coeffs[10];
882 _a6[i] = coeffs[11];
883 }
884 else
885 {
886 const std::array<double, 6> nodal_values = {v[0], v[1], v[2], v[3], v[4], v[5]};
887 const std::array<double, 12> coeffs = proteus::solve_ifem_basis_coefficients(
888 2, x0, y0, nx, ny, ma, mb, 0.0, Jit00, Jit01, Jit10, Jit11, nodal_values);
889
890 _a1[i] = coeffs[0];
891 _a2[i] = coeffs[1];
892 _a3[i] = coeffs[2];
893 _a4[i] = coeffs[3];
894 _a5[i] = coeffs[4];
895 _a6[i] = coeffs[5];
896 _b1[i] = coeffs[6];
897 _b2[i] = coeffs[7];
898 _b3[i] = coeffs[8];
899 _b4[i] = coeffs[9];
900 _b5[i] = coeffs[10];
901 _b6[i] = coeffs[11];
902 }
903 }
904 }
905 break;
906 }
907 default:
908 throw std::runtime_error("Simplex::_calculate_coefficients not implemented for order > 2");
909 }
910 // Unit flux-jump lift: zero nodal values, flux jump 1. The solve is linear in (nodal values,
911 // jf), so a basis built with jf != 0 would carry one copy of this function per basis function;
912 // it is kept separate and added once by the caller, scaled by the prescribed jump.
913 (void)jf;
914 for (int i = 0; i < 6; i++)
915 {
916 _lift_a[i] = 0.0;
917 _lift_b[i] = 0.0;
918 }
919 if (!(corner || edge))
920 {
921 // The solver imposes m_second*dpsi_second/dn_s - m_first*dpsi_first/dn_s = jf with
922 // n_s = +-level_set_normal, and level_set_normal is the cut tangent rotated by 90 degrees: its
923 // sign follows the order of the cut points, not the direction of phi. (The homogeneous basis
924 // never notices.) In both inside_out branches the physical jump from a (phi<0) to b (phi>0) is
925 // jf*sign(level_set_normal . grad phi), so that sign is passed as jf for a unit jump.
926 const double gphi_ref[2] = {phi[1] - phi[0], phi[2] - phi[0]};
927 double n_dot_gphi = 0.0;
928 for (int I = 0; I < 2; I++)
929 {
930 double g = 0.0;
931 for (int J = 0; J < 2; J++)
932 g += inv_Jac[J * nSpace + I] * gphi_ref[J];
933 n_dot_gphi += g * level_set_normal[I];
934 }
935 const double jf_unit = (n_dot_gphi >= 0.0) ? 1.0 : -1.0;
936 double *first = inside_out ? _lift_b : _lift_a, *second = inside_out ? _lift_a : _lift_b;
937 const double m_first = inside_out ? mb : ma, m_second = inside_out ? ma : mb;
938 if (nP_ifem == 3)
939 {
940 const std::array<double, 3> zero = {0., 0., 0.};
941 const std::array<double, 6> c = proteus::solve_ifem_basis_coefficients(
942 1, x0, y0, nx, ny, m_first, m_second, jf_unit, Jit00, Jit01, Jit10, Jit11, zero);
943 for (int i = 0; i < 3; i++)
944 {
945 first[i] = c[i];
946 second[i] = c[3 + i];
947 }
948 }
949 else if (nP_ifem == 6)
950 {
951 const std::array<double, 6> zero = {0., 0., 0., 0., 0., 0.};
952 const std::array<double, 12> c = proteus::solve_ifem_basis_coefficients(
953 2, x0, y0, nx, ny, m_first, m_second, jf_unit, Jit00, Jit01, Jit10, Jit11, zero);
954 for (int i = 0; i < 6; i++)
955 {
956 first[i] = c[i];
957 second[i] = c[6 + i];
958 }
959 }
960 }
961 }
962
963 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
964 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_lift(const double *xi, double *psi)
965 {
966 const double *c[2] = {_lift_a, _lift_b};
967 for (int s = 0; s < 2; s++)
968 {
969 const double *a = c[s];
970 const double val = a[0] + a[1] * xi[0] + a[2] * xi[1] + a[3] * xi[0] * xi[1] + a[4] * xi[0] * xi[0] + a[5] * xi[1] * xi[1];
971 const double g_ref[2] = {a[1] + a[3] * xi[1] + 2.0 * a[4] * xi[0], a[2] + a[3] * xi[0] + 2.0 * a[5] * xi[1]};
972 double g[2] = {0.0, 0.0};
973 for (int I = 0; I < nSpace && I < 2; I++)
974 for (int J = 0; J < nSpace && J < 2; J++)
975 g[I] += inv_Jac[J * nSpace + I] * g_ref[J];
976 psi[3 * s + 0] = val;
977 psi[3 * s + 1] = g[0];
978 psi[3 * s + 2] = g[1];
979 }
980 }
981
982 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
983 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_basis(const double *xi, double *va, double *vb)
984 {
985 // Switch on nP_ifem to compute basis functions at given xi
986 switch (nP_ifem)
987 {
988 case 3:
989 for (int i = 0; i < nP_ifem; i++)
990 {
991 va[i] = _a1[i] + _a2[i] * xi[0] + _a3[i] * xi[1];
992 vb[i] = _b1[i] + _b2[i] * xi[0] + _b3[i] * xi[1];
993
994
995 }
996 break;
997 case 6:
998 for (int i = 0; i < nP_ifem; i++)
999 {
1000 va[i] = _a1[i] + _a2[i] * xi[0] + _a3[i] * xi[1] + _a4[i] * xi[0] * xi[1] + _a5[i] * xi[0] * xi[0] + _a6[i] * xi[1] * xi[1];
1001 vb[i] = _b1[i] + _b2[i] * xi[0] + _b3[i] * xi[1] + _b4[i] * xi[0] * xi[1] + _b5[i] * xi[0] * xi[0] + _b6[i] * xi[1] * xi[1];
1002
1003
1004
1005 }
1006 break;
1007 default:
1008 throw std::runtime_error("Simplex::_calculate_basis not implemented for order > 2");
1009 }
1010 }
1011
1012 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
1013 inline void Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::_calculate_basis_gradients(const double *xi, double *va_x, double *va_y, double *vb_x, double *vb_y)
1014 {
1015 // Switch on nP_ifem to compute basis functions gradients at given xi
1016
1017 switch (nP_ifem)
1018 {
1019 case 3:
1020 for (int i = 0; i < nP_ifem; i++)
1021 {
1022 double grad_va[2] = {0.0, 0.0}, grad_vb[2] = {0.0, 0.0}, grad_va_ref[2] = {0.0, 0.0}, grad_vb_ref[2] = {0.0, 0.0};
1023
1024 grad_va_ref[0] = _a2[i];
1025 grad_va_ref[1] = _a3[i];
1026 grad_vb_ref[0] = _b2[i];
1027 grad_vb_ref[1] = _b3[i];
1028 for (int I = 0; I < nSpace; I++)
1029 {
1030 for (int J = 0; J < nSpace; J++)
1031 {
1032 grad_va[I] += inv_Jac[J * nSpace + I] * grad_va_ref[J];
1033 grad_vb[I] += inv_Jac[J * nSpace + I] * grad_vb_ref[J];
1034 }
1035 }
1036 va_x[i] = grad_va[0];
1037 va_y[i] = grad_va[1];
1038 vb_x[i] = grad_vb[0];
1039 vb_y[i] = grad_vb[1];
1040
1041
1042 }
1043 break;
1044 case 6:
1045 for (int i = 0; i < nP_ifem; i++)
1046 {
1047 double grad_va[2] = {0.0, 0.0}, grad_vb[2] = {0.0, 0.0}, grad_va_ref[2] = {0.0, 0.0}, grad_vb_ref[2] = {0.0, 0.0};
1048
1049 grad_va_ref[0] = _a2[i] + _a4[i] * xi[1] + 2.0 * _a5[i] * xi[0];
1050 grad_va_ref[1] = _a3[i] + _a4[i] * xi[0] + 2.0 * _a6[i] * xi[1];
1051 grad_vb_ref[0] = _b2[i] + _b4[i] * xi[1] + 2.0 * _b5[i] * xi[0];
1052 grad_vb_ref[1] = _b3[i] + _b4[i] * xi[0] + 2.0 * _b6[i] * xi[1];
1053
1054
1055 for (int I = 0; I < nSpace; I++)
1056 {
1057 for (int J = 0; J < nSpace; J++)
1058 {
1059 grad_va[I] += inv_Jac[J * nSpace + I] * grad_va_ref[J];
1060 grad_vb[I] += inv_Jac[J * nSpace + I] * grad_vb_ref[J];
1061 }
1062 }
1063 va_x[i] = grad_va[0];
1064 va_y[i] = grad_va[1];
1065 vb_x[i] = grad_vb[0];
1066 vb_y[i] = grad_vb[1];
1067 }
1068 break;
1069 default:
1070 throw std::runtime_error("Simplex::_calculate_basis_gradients not implemented for order > 2");
1071 }
1072 }
1073
1074 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis>
1075 inline int Simplex<nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis>::calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale)
1076 {
1077 p2_cell_flipped = false;
1078 // initialize phi_dof_corrected -- correction can only be actually computed on cut cells
1079 for (unsigned int i = 0; i < nDOF_phi; i++)
1080 phi_dof_corrected[i] = phi_dof[i];
1081 int icase = _calculate_permutation(phi_dof, phi_nodes); // permuation, Jac,inv_Jac...
1082 if (icase == 1)
1083 {
1084 for (unsigned int q = 0; q < nQ; q++)
1085 {
1086 _H[q] = 1.0;
1087 _ImH[q] = 0.0;
1088 _D[q] = 0.0;
1089 }
1090 for (unsigned int ebq = 0; ebq < nEBQ; ebq++)
1091 {
1092 _H_ebq[ebq] = 1.0;
1093 _ImH_ebq[ebq] = 0.0;
1094 _D_ebq[ebq] = 0.0;
1095 }
1096 return icase;
1097 }
1098 else if (icase == -1)
1099 {
1100 for (unsigned int q = 0; q < nQ; q++)
1101 {
1102 _H[q] = 0.0;
1103 _ImH[q] = 1.0;
1104 _D[q] = 0.0;
1105 }
1106 for (unsigned int ebq = 0; ebq < nEBQ; ebq++)
1107 {
1108 _H_ebq[ebq] = 0.0;
1109 _ImH_ebq[ebq] = 1.0;
1110 _D_ebq[ebq] = 0.0;
1111 }
1112 return icase;
1113 }
1114 else if (nSpace == 1 && edge == 1)
1115 {
1116 // 1D only: the "edge" here is a single node, so touching zero there
1117 // is a measure-zero point, not a genuine interface -- treat this the
1118 // same as the fully-positive icase==1 case. (In 2D/3D "edge" means an
1119 // entire element edge/face has phi==0, a real nonzero-measure
1120 // interface, so this special case does not apply there.)
1121 // inside_out was set true by the pcount==1 branch above (it flips the
1122 // dir vector for the interior-cut machinery this case never reaches);
1123 // reset it so set_quad()/set_boundary_quad() don't swap H and ImH.
1124 inside_out = false;
1125 for (unsigned int q = 0; q < nQ; q++)
1126 {
1127 _H[q] = 1.0;
1128 _ImH[q] = 0.0;
1129 _D[q] = 0.0;
1130 }
1131 for (unsigned int ebq = 0; ebq < nEBQ; ebq++)
1132 {
1133 _H_ebq[ebq] = 1.0;
1134 _ImH_ebq[ebq] = 0.0;
1135 _D_ebq[ebq] = 0.0;
1136 }
1137 return edge;
1138 }
1139 else if (nSpace == 1 && edge == -1)
1140 {
1141 // same as above, but the cell is entirely non-positive.
1142 inside_out = false;
1143 for (unsigned int q = 0; q < nQ; q++)
1144 {
1145 _H[q] = 0.0;
1146 _ImH[q] = 1.0;
1147 _D[q] = 0.0;
1148 }
1149 for (unsigned int ebq = 0; ebq < nEBQ; ebq++)
1150 {
1151 _H_ebq[ebq] = 0.0;
1152 _ImH_ebq[ebq] = 1.0;
1153 _D_ebq[ebq] = 0.0;
1154 }
1155 return edge;
1156 }
1157 if (quad_cut)
1158 {
1159 _calculate_cuts_quad(); // THETA_* for quad cut in 3D
1160 _calculate_normal_quad(phys_nodes_cut_quad_01,
1161 phys_nodes_cut_quad_02,
1162 phys_nodes_cut_quad_31,
1163 phys_nodes_cut_quad_32,
1164 level_set_normal); // normal to interface
1165 }
1166 else
1167 {
1168 _calculate_cuts(); // X_0, array of interface cuts on reference simplex
1169 _calculate_normal<nSpace>(phys_nodes_cut, level_set_normal); // normal to interface
1170 }
1171 _correct_phi(phi_dof, phi_nodes);
1172 if (flip_the_cell)
1173 {
1174 _calculate_permutation(phi_dof, phi_nodes);
1175 _calculate_cuts(); // X_0, array of interface cuts on reference simplex
1176 }
1177 _calculate_C(); // coefficients of equiv poly
1178
1179 double ma_scale, mb_scale;
1180 if (scale)
1181 {
1182 // cek hack - 2D, pressure basis for discontinuous density
1183 double jump_scale = level_set_normal[1],
1184 m_average = 0.5 * (ma + mb),
1185 m_jump = 0.5 * (mb - ma);
1186 mb_scale = m_average + jump_scale * m_jump; // mb when jump_scale=1
1187 ma_scale = m_average - jump_scale * m_jump; // ma when jump_scale=1
1188 // double mb_scale=mb, ma_scale=ma;
1189 // cek hack end
1190 }
1191 else
1192 {
1193 ma_scale = ma;
1194 mb_scale = mb;
1195 }
1196 // The two-sided IFEM basis is only implemented for 2D simplices with a P1
1197 // (nP_ifem 3) or P2 (nP_ifem 6) trial space; _calculate_basis_coefficients
1198 // asserts nSpace==2 and throws for any other nP_ifem. Every non-ADR caller
1199 // aliases GeneralizedFunctions_mix<nSpace, nP, nP, ...>, so VOF/MCorr reach
1200 // here with nP_ifem 2 and RANS3PF with 1: they need only the H/ImH/D moment
1201 // fit and must not enter the basis solve. Guard on nP_ifem, not just nSpace.
1202 if (useIfemBasis)
1203 _calculate_basis_coefficients(ma_scale, mb_scale, jf);
1204 // compute the default affine map based on phi_nodes[0]
1205 double Jac_0[nSpace * nSpace];
1206 for (unsigned int i = 0; i < nN - 1; i++)
1207 for (unsigned int I = 0; I < nSpace; I++)
1208 Jac_0[I * nSpace + i] = phi_nodes[(1 + i) * 3 + I] - phi_nodes[I];
1209
1210 if (!isBoundary)
1211 {
1212 for (unsigned int q = 0; q < nQ; q++)
1213 {
1214 // Due to the permutation, the quadrature points on the reference may be rotated
1215 // map reference to physical simplex, then back to permuted reference
1216 double x[nSpace], xi[nSpace];
1217 // to physical coordinates
1218 for (unsigned int I = 0; I < nSpace; I++)
1219 {
1220 x[I] = phi_nodes[I];
1221 for (unsigned int J = 0; J < nSpace; J++)
1222 {
1223 x[I] += Jac_0[I * nSpace + J] * xi_r[q * 3 + J];
1224 }
1225 }
1226 // back to reference coordinates on possibly permuted
1227 for (unsigned int I = 0; I < nSpace; I++)
1228 {
1229 xi[I] = 0.0;
1230 for (unsigned int J = 0; J < nSpace; J++)
1231 {
1232 xi[I] += inv_Jac[I * nSpace + J] * (x[J] - nodes[J]);
1233 }
1234 }
1235 if (nSpace == 1)
1236 _calculate_polynomial_1D<nP>(xi, C_H, C_ImH, C_D, _H[q], _ImH[q], _D[q]);
1237 else if (nSpace == 2)
1238 {
1239 _calculate_polynomial_2D<nP>(xi, C_H, C_ImH, C_D, _H[q], _ImH[q], _D[q]);
1240 // Only the IFEM instantiations have a two-sided basis; see the guard on
1241 // _calculate_basis_coefficients above.
1242 if (useIfemBasis)
1243 {
1244 _calculate_basis(xi, &_va[q * nP_ifem], &_vb[q * nP_ifem]);
1245 _calculate_basis_gradients(xi, &_va_x[q * nP_ifem], &_va_y[q * nP_ifem], &_vb_x[q * nP_ifem], &_vb_y[q * nP_ifem]);
1246 }
1247 _calculate_lift(xi, &_psi[q * 6]);
1248 }
1249 else if (nSpace == 3)
1250 _calculate_polynomial_3D<nP>(xi, C_H, C_ImH, C_D, _H[q], _ImH[q], _D[q]);
1251 }
1252 set_quad(0);
1253 }
1254 else
1255 {
1256 for (unsigned int ebq = 0; ebq < nEBQ; ebq++)
1257 {
1258 // Due to the permutation, the quadrature points on the reference may be rotated
1259 // map reference to physical simplex, then back to permuted reference
1260 double x[nSpace], xi[nSpace];
1261 // to physical coordinates
1262 for (unsigned int I = 0; I < nSpace; I++)
1263 {
1264 x[I] = phi_nodes[I];
1265 for (unsigned int J = 0; J < nSpace; J++)
1266 {
1267 x[I] += Jac_0[I * nSpace + J] * xi_r[ebq * 3 + J];
1268 }
1269 }
1270 // back to reference coordinates on possibly permuted
1271 for (unsigned int I = 0; I < nSpace; I++)
1272 {
1273 xi[I] = 0.0;
1274 for (unsigned int J = 0; J < nSpace; J++)
1275 {
1276 xi[I] += inv_Jac[I * nSpace + J] * (x[J] - nodes[J]);
1277 }
1278 }
1279 if (nSpace == 1)
1280 _calculate_polynomial_1D<nP>(xi, C_H, C_ImH, C_D, _H_ebq[ebq], _ImH_ebq[ebq], _D_ebq[ebq]);
1281 else if (nSpace == 2)
1282 {
1283 _calculate_polynomial_2D<nP>(xi, C_H, C_ImH, C_D, _H_ebq[ebq], _ImH_ebq[ebq], _D_ebq[ebq]);
1284 // Only the IFEM instantiations have a two-sided basis; see the guard on
1285 // _calculate_basis_coefficients above.
1286 if (useIfemBasis)
1287 {
1288 _calculate_basis(xi, &_va_ebq[ebq * nP_ifem], &_vb_ebq[ebq * nP_ifem]);
1289 _calculate_basis_gradients(xi, &_va_x_ebq[ebq * nP_ifem], &_va_y_ebq[ebq * nP_ifem], &_vb_x_ebq[ebq * nP_ifem], &_vb_y_ebq[ebq * nP_ifem]);
1290 }
1291 _calculate_lift(xi, &_psi_ebq[ebq * 6]);
1292 }
1293 else if (nSpace == 3)
1294 _calculate_polynomial_3D<nP>(xi, C_H, C_ImH, C_D, _H_ebq[ebq], _ImH_ebq[ebq], _D_ebq[ebq]);
1295 }
1297 }
1298 // IFEM needs the interface normal to follow its own inside_out convention, but
1299 // pre-IFEM this flip was deliberately disabled (it sat commented out inside the
1300 // old normal_sign block) and every other consumer -- VOF/MCorr/RDLS/RANS* --
1301 // still reads get_normal() expecting the unflipped normal. Re-enabling it for
1302 // them broke MCorr's mass correction. Gate it with the rest of the IFEM path.
1303 if (useIfemBasis && inside_out)
1304 for (unsigned int I = 0; I < nSpace; I++)
1305 level_set_normal[I] *= -1.0;
1306 return icase;
1307 }
1308
1309 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ, bool useIfemBasis = false>
1311 {
1312 public:
1319
1320 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale)
1321 {
1322
1323 if (useExact)
1324 return exact.calculate(phi_dof, phi_nodes, xi_r, ma, mb, jf, isBoundary, scale);
1325 else // for inexact just copy over local phi_dof
1326 {
1327 for (int i = 0; i < exact.nN; i++)
1328 exact.phi_dof_corrected[i] = phi_dof[i];
1329 return 1;
1330 }
1331 }
1332
1333 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, bool isBoundary, bool scale)
1334 {
1335 return calculate(phi_dof, phi_nodes, xi_r, ma, mb, 0.0, isBoundary, false);
1336 }
1337
1338 inline int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, bool isBoundary)
1339 {
1340 return calculate(phi_dof, phi_nodes, xi_r, 1.0, 1.0, 0.0, isBoundary, false);
1341 }
1342
1343 inline double *get_normal()
1344 {
1345 if (useExact)
1346 return exact.get_normal();
1347 else
1348 return regularized.get_normal();
1349 }
1350
1351 inline void set_quad(unsigned int q)
1352 {
1353 if (useExact)
1354 exact.set_quad(q);
1355 }
1356
1357 inline void set_boundary_quad(unsigned int ebq)
1358 {
1359 if (useExact)
1361 }
1362
1363 inline double H(double eps, double phi)
1364 {
1365 if (useExact)
1366 return exact.H(eps, phi);
1367 else
1368 return regularized.H(eps, phi);
1369 }
1370
1371 inline double ImH(double eps, double phi)
1372 {
1373 if (useExact)
1374 return exact.ImH(eps, phi);
1375 else
1376 return regularized.ImH(eps, phi);
1377 }
1378
1379 inline double D(double eps, double phi)
1380 {
1381 if (useExact)
1382 return exact.D(eps, phi);
1383 else
1384 return regularized.D(eps, phi);
1385 }
1386 inline double VA(int i)
1387 {
1388 if (useExact)
1389 return exact.VA(i);
1390 else
1391 return regularized.VA(i);
1392 }
1393 inline double VA_x(int i)
1394 {
1395 if (useExact)
1396 return exact.VA_x(i);
1397 else
1398 return regularized.VA_x(i);
1399 }
1400 inline double VA_y(int i)
1401 {
1402 if (useExact)
1403 return exact.VA_y(i);
1404 else
1405 return regularized.VA_y(i);
1406 }
1407 inline double VA_z(int i)
1408 {
1409 if (useExact)
1410 return exact.VA_z(i);
1411 else
1412 return regularized.VA_z(i);
1413 }
1414 inline double VB(int i)
1415 {
1416 if (useExact)
1417 return exact.VB(i);
1418 else
1419 return regularized.VB(i);
1420 }
1421 inline double VB_x(int i)
1422 {
1423 if (useExact)
1424 return exact.VB_x(i);
1425 else
1426 return regularized.VB_x(i);
1427 }
1428 inline double VB_y(int i)
1429 {
1430 if (useExact)
1431 return exact.VB_y(i);
1432 else
1433 return regularized.VB_y(i);
1434 }
1435 inline double VB_z(int i)
1436 {
1437 if (useExact)
1438 return exact.VB_z(i);
1439 else
1440 return regularized.VB_z(i);
1441 }
1442 inline double PSIA() { return useExact ? exact.PSIA() : regularized.PSIA(); }
1443 inline double PSIA_x() { return useExact ? exact.PSIA_x() : regularized.PSIA_x(); }
1444 inline double PSIA_y() { return useExact ? exact.PSIA_y() : regularized.PSIA_y(); }
1445 inline double PSIB() { return useExact ? exact.PSIB() : regularized.PSIB(); }
1446 inline double PSIB_x() { return useExact ? exact.PSIB_x() : regularized.PSIB_x(); }
1447 inline double PSIB_y() { return useExact ? exact.PSIB_y() : regularized.PSIB_y(); }
1448 };
1449} // equivalent_polynomials
1450
1451#endif
Double q
Definition Headers.h:81
Double s
Definition Headers.h:84
Double v
Definition Headers.h:95
Double phi
Definition Headers.h:76
Double psi
Definition Headers.h:78
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, bool isBoundary)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, bool isBoundary, bool scale)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, bool isBoundary, bool scale)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, bool isBoundary)
double ImH(double eps, double phi)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, bool isBoundary)
double ImH(double eps, double phi)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, bool isBoundary, bool scale)
double D(double eps, double phi)
int calculate(const double *phi_dof, const double *phi_nodes, const double *xi_r, double ma, double mb, double jf, bool isBoundary, bool scale)
double H(double eps, double phi)
#define c(i)
Definition jf.h:21
void _calculate_polynomial_2D(double *xi, double *C_H, double *C_ImH, double *C_D, double &_H, double &_ImH, double &_D)
void inv(const double *A, double *Ainv)
void _calculate_normal_quad(double *phys_nodes_cut_quad_01, double *phys_nodes_cut_quad_02, double *phys_nodes_cut_quad_31, double *phys_nodes_cut_quad_32, double *level_set_normal)
void _calculate_polynomial_1D(double *xi, double *C_H, double *C_ImH, double *C_D, double &_H, double &_ImH, double &_D)
double det(const double *A)
void _set_Ainv(double *Ainv)
void _calculate_polynomial_3D(double *xi, double *C_H, double *C_ImH, double *C_D, double &_H, double &_ImH, double &_D)
void _calculate_normal(double *phys_nodes_cut, double *level_set_normal)
void _calculate_b(double *X_0, double *b_H, double *b_ImH, double *b_dH)
std::array< double, 6 > solve_ifem_basis_coefficients(int basis_order, double x0, double y0, double nx, double ny, double ma, double mb, double jf, double Jit00, double Jit01, double Jit10, double Jit11, const std::array< double, 3 > &nodal_values)