proteus 1.9.1
C/C++/Fortran libraries
Loading...
Searching...
No Matches
ADR.h
Go to the documentation of this file.
1#ifndef ADR_H
2#define ADR_H
3#include <cmath>
4#include <iomanip>
5#include <iostream>
6#include <set>
7#include <stdexcept>
8#include <string>
9#include <map>
10#include <valarray>
11#include <vector>
12#include "CompKernel.h"
13#include "ModelFactory.h"
16#include "xtensor-python/pyarray.hpp"
17
18namespace py = pybind11;
19
20namespace proteus
21{
22 template <int nSpace, int nP_ifem, int nP, int nQ, int nEBQ>
24
26 {
27 public:
28 virtual ~cADR_base() {}
29 virtual void calculateResidual(arguments_dict &args) = 0;
30 virtual void calculateJacobian(arguments_dict &args) = 0;
31 };
32
33 template <class CompKernelType,
34 int nSpace,
35 int nQuadraturePoints_element,
36 int nDOF_mesh_trial_element,
37 int nDOF_trial_element,
38 int nDOF_test_element,
39 int nQuadraturePoints_elementBoundary>
40 class cADR : public cADR_base
41 {
42 public:
45 std::valarray<bool> elementIsActive;
47 CompKernelType ck;
48 // nEBQ is sized for ALL faces (nDOF_mesh_trial_element * nQuadraturePoints_elementBoundary),
49 // not one face, so the immersed-interface facet terms can evaluate va/vb at the quadrature
50 // points of the face they are actually integrating -- see the xB_ref_faces comment in
51 // calculateResidual for why passing the reference *boundary* points directly is wrong.
52 // Degree k of the fitted H/ImH/D polynomials (GfType, the triply-cut composite fits, the edge
53 // fits). Each cut-cell term is sum_q w_q*fit*F, exact when deg F <= k AND k + deg F <= the
54 // element rule's degree. The Gamma_s Nitsche penalty / Robin terms are degree 2p. P1: k=3 with
55 // the 12-point (degree-6) rule. P2: k=4 needs the 16-point (degree-8) rule (qOrder=8); with
56 // the 12-point rule k=3 is kept, which is exact through degree 2p-1=3 and still rate-optimal.
57 static const int nP_fit = (nDOF_trial_element == 6 && nQuadraturePoints_element >= 16) ? 4 : 3;
59 // Degree of the edge-restricted moment-fit Heaviside used by the edge terms (SCIFEM, the
60 // triply-cut correction). The consistency terms are degree 2p-1 and the interior penalty 2p;
61 // the 1D boundary rule (qOrder points) is exact far beyond nP_edge + 2p.
62 static const int nP_edge = nP_fit;
63 // Per-element cache of the equivalent-polynomial/IFEM reconstruction
64 // (gf_s.calculate()/gf_f.calculate()): permutation, cut classification,
65 // the IFEM basis coefficient solve, and the H/ImH/D + VA/VB (+
66 // gradients) evaluated at every quadrature point. None of this depends
67 // on the current solution u -- only on the level-set geometry
68 // (element_phi_s/element_phi_f) and mua/mub/jf -- so it is only
69 // recomputed when ifemGeometryGeneration advances (see
70 // recomputeIFEMGeometry / markIFEMGeometryDirty on the Python side).
71 // Interior (isBoundary=false) and boundary (isBoundary=true) evaluations
72 // populate different internal storage within the same GfType object, so
73 // they are tracked with separate generation/icase arrays.
74 std::vector<GfType> gf_f_cache, gf_s_cache;
77 // Composite equivalent polynomials for elements cut by BOTH level sets. The product
78 // ImH_f*H_s is not moment-exact on such an element (see .notes/EQUIVALENT_POLYNOMIALS.md
79 // section 7.1); these replace it with one fit per intersected region. Only populated when
80 // icase_s == 0 && icase_f == 0, so every single-level-set element keeps the old path
81 // bit-for-bit.
82 static const int nCompMon = (nP_fit + 1) * (nP_fit + 2) / 2; // monomials up to degree nP_fit
83 std::vector<double> comp_A_cache, comp_B_cache;
84 std::vector<double> comp_DA_cache, comp_DB_cache;
85 std::vector<char> comp_valid;
86 // Gamma_f's OWN Dirac measure, split by Gamma_s's sign: comp_DFm = the masked (solid,
87 // phi_s<0) part, comp_DFa = the active (fluid, phi_s>0) part. Needed for any Dirac-delta
88 // term integrated over Gamma_f on a triply-cut element (e.g. the prescribed-flux-jump
89 // source, [beta du/dn]*D_f) -- using the plain single-curve gf_f.D(0.,0.) there
90 // double-counts whatever part of Gamma_f's chord falls inside the masked-out solid.
91 // Tracked as its own validity flag, not folded into comp_valid: bulk area weights, Gamma_s's
92 // own Dirac measure, and Gamma_f's own Dirac measure are three independently-fitted
93 // quantities that can, in principle, succeed or fail on different degenerate geometries
94 // (see .notes/EQUIVALENT_POLYNOMIALS.md's "track independent flags, not one" rule).
95 std::vector<double> comp_DFm_cache, comp_DFa_cache;
96 std::vector<char> comp_DF_valid;
97 // Amplitude of the flux-jump lift J*psi on each cell, as used in this residual evaluation:
98 // the per-cell J_f from ADR.py minus the flux jump the solution-jump lift j_h already
99 // carries (nonzero only when J_s varies on the cell). Read by the element and face terms.
100 std::vector<double> fluxLiftAmp;
102 cADR() : nDOF_test_X_trial_element(nDOF_test_element * nDOF_trial_element), ck()
103 {
104 }
105 inline void ensureIFEMCacheSized(int nElements_global)
106 {
107 if ((int)gf_f_cache.size() == nElements_global)
108 return;
109 gf_f_cache.assign(nElements_global, GfType());
110 gf_s_cache.assign(nElements_global, GfType());
111 fluxLiftAmp.assign(nElements_global, 0.0);
112 gf_s_interior_gen.assign(nElements_global, -1);
113 gf_f_interior_gen.assign(nElements_global, -1);
114 gf_f_boundary_gen.assign(nElements_global, -1);
115 gf_s_interior_icase.assign(nElements_global, 0);
116 gf_f_interior_icase.assign(nElements_global, 0);
117 gf_f_boundary_icase.assign(nElements_global, 0);
118 comp_A_cache.assign((std::size_t)nElements_global * nCompMon, 0.0);
119 comp_B_cache.assign((std::size_t)nElements_global * nCompMon, 0.0);
120 comp_DA_cache.assign((std::size_t)nElements_global * nCompMon, 0.0);
121 comp_DB_cache.assign((std::size_t)nElements_global * nCompMon, 0.0);
122 comp_valid.assign(nElements_global, 0);
123 comp_DFm_cache.assign((std::size_t)nElements_global * nCompMon, 0.0);
124 comp_DFa_cache.assign((std::size_t)nElements_global * nCompMon, 0.0);
125 comp_DF_valid.assign(nElements_global, 0);
126 for (auto &gf : gf_f_cache)
127 gf.useExact = true;
128 for (auto &gf : gf_s_cache)
129 gf.useExact = true;
130 }
131
132 inline void exteriorNumericalDiffusiveFlux(int *rowptr,
133 int *colind,
134 const int &isDOFBoundary,
135 const int &isDiffusiveFluxBoundary,
136 const double n[nSpace],
137 double *bc_a,
138 const double &bc_u,
139 const double &bc_flux,
140 double *a,
141 const double grad_potential[nSpace],
142 const double &u,
143 const double &penalty,
144 double &flux)
145 {
146 double diffusiveVelocityComponent_I;
147 double penaltyFlux;
148 double max_a;
149 if (isDiffusiveFluxBoundary == 1)
150 {
151 flux = bc_flux;
152 }
153 else if (isDOFBoundary == 1)
154 {
155 flux = 0.0;
156 max_a = 0.0;
157 for (int I = 0; I < nSpace; I++)
158 {
159 diffusiveVelocityComponent_I = 0.0;
160 for (int m = rowptr[I]; m < rowptr[I + 1]; m++)
161 {
162 diffusiveVelocityComponent_I -= a[m] * grad_potential[colind[m]];
163 max_a = fmax(max_a, a[m]);
164 }
165 flux += diffusiveVelocityComponent_I * n[I];
166 }
167 penaltyFlux = max_a * penalty * (u - bc_u);
168 flux += penaltyFlux;
169 }
170 else
171 {
172 std::cerr << "warning, diffusion term with no boundary condition set, setting diffusive flux to 0.0" << std::endl;
173 flux = 0.0;
174 }
175 }
176
177 inline double ExteriorNumericalDiffusiveFluxJacobian(int *rowptr,
178 int *colind,
179 const int &isDOFBoundary,
180 const int &isDiffusiveFluxBoundary,
181 const double n[nSpace],
182 double *a,
183 const double &v,
184 const double grad_v[nSpace],
185 const double &penalty)
186 {
187 double dvel_I;
188 double tmp = 0.0;
189 double max_a = 0.0;
190 if ((isDiffusiveFluxBoundary == 0) && (isDOFBoundary == 1))
191 {
192 for (int I = 0; I < nSpace; I++)
193 {
194 dvel_I = 0.0;
195 for (int m = rowptr[I]; m < rowptr[I + 1]; m++)
196 {
197 dvel_I -= a[m] * grad_v[colind[m]];
198 max_a = fmax(max_a, a[m]);
199 }
200 tmp += dvel_I * n[I];
201 }
202 tmp += max_a * penalty * v;
203 }
204 return tmp;
205 }
206
207 inline void calculateSubgridError_tau(const double &elementDiameter,
208 const double &dmt,
209 const double dH[nSpace],
210 double &cfl,
211 double &tau)
212 {
213 double h;
214 double nrm_v;
215 double oneByAbsdt;
216 h = elementDiameter;
217 nrm_v = 0.0;
218 for (int I = 0; I < nSpace; I++)
219 nrm_v += dH[I] * dH[I];
220 nrm_v = sqrt(nrm_v);
221 cfl = nrm_v / h;
222 oneByAbsdt = fabs(dmt);
223 tau = 1.0 / (2.0 * nrm_v / h + oneByAbsdt + 1.0e-8);
224 }
225
226 inline void calculateSubgridError_tau(const double &Ct_sge,
227 const double G[nSpace * nSpace],
228 const double &A0,
229 const double Ai[nSpace],
230 double &tau_v,
231 double &cfl)
232 {
233 double v_d_Gv = 0.0;
234 for (int I = 0; I < nSpace; I++)
235 for (int J = 0; J < nSpace; J++)
236 v_d_Gv += Ai[I] * G[I * nSpace + J] * Ai[J];
237 tau_v = 1.0 / sqrt(Ct_sge * A0 * A0 + v_d_Gv + 1.0e-8);
238 }
239
240 inline void calculateNumericalDiffusion(const double &shockCapturingDiffusion,
241 const double &elementDiameter,
242 const double &strong_residual,
243 const double grad_u[nSpace],
244 double &numDiff)
245 {
246 double h;
247 double num;
248 double den;
249 double n_grad_u;
250 h = elementDiameter;
251 n_grad_u = 0.0;
252 for (int I = 0; I < nSpace; I++)
253 n_grad_u += grad_u[I] * grad_u[I];
254 num = shockCapturingDiffusion * 0.5 * h * fabs(strong_residual);
255 den = sqrt(n_grad_u) + 1.0e-8;
256 numDiff = num / den;
257 }
258
259 inline void exteriorNumericalAdvectiveFlux(const int &isDOFBoundary_u,
260 const int &isFluxBoundary_u,
261 const double n[nSpace],
262 const double &bc_u,
263 const double &bc_flux_u,
264 const double &u,
265 const double velocity[nSpace],
266 double &flux)
267 {
268
269 double flow = 0.0;
270 for (int I = 0; I < nSpace; I++)
271 flow += n[I] * velocity[I];
272 if (isDOFBoundary_u == 1)
273 {
274 if (flow >= 0.0)
275 {
276 flux = u * flow;
277 // flux = flow;
278 }
279 else
280 {
281 flux = bc_u * flow;
282 // flux = flow;
283 }
284 }
285 else if (isFluxBoundary_u == 1)
286 {
287 flux = bc_flux_u;
288 }
289 else
290 {
291 if (flow >= 0.0)
292 {
293 flux = u * flow;
294 }
295 else
296 {
297 flux = 0.0;
298 }
299 }
300 // flux = flow;
301 }
302
303 inline void exteriorNumericalAdvectiveFluxDerivative(const int &isDOFBoundary_u,
304 const int &isFluxBoundary_u,
305 const double n[nSpace],
306 const double velocity[nSpace],
307 double &dflux)
308 {
309 double flow = 0.0;
310 for (int I = 0; I < nSpace; I++)
311 {
312 flow += n[I] * velocity[I];
313 }
314 // double flow=n[0]*velocity[0]+n[1]*velocity[1]+n[2]*velocity[2];
315 dflux = 0.0; // default to no flux
316 if (isDOFBoundary_u == 1)
317 {
318 if (flow >= 0.0)
319 {
320 dflux = flow;
321 }
322 else
323 {
324 dflux = 0.0;
325 }
326 }
327 else if (isFluxBoundary_u == 1)
328 {
329 dflux = 0.0;
330 }
331 else
332 {
333 if (flow >= 0.0)
334 {
335 dflux = flow;
336 }
337 }
338 }
339
340 inline void updateEmbeddedBoundaryTerms(const double embeddedBoundary_penalty,
341 const double dV,
342 double *embeddedBoundary_normal,
343 const double u_s,
344 const double u,
345 const double grad_u[nSpace],
346 const double a,
347 double &r,
348 double &dr,
349 double &ham,
350 double *dham,
351 double *f,
352 double *df,
353 const double D_s)
354 {
355 // todo this doesn't look 1d/3d
356 double outward_normal[nSpace];
357 for (int I = 0; I < nSpace; I++)
358 outward_normal[I] = -embeddedBoundary_normal[I];
359
360 // diffusive flux
361 for (int I = 0; I < nSpace; I++)
362 {
363 ham -= outward_normal[I] * grad_u[I];
364 dham[I] -= D_s * a * outward_normal[I];
365 // Nitsche adjoint consistency
366 f[I] += D_s * a * outward_normal[I] * (u - u_s);
367 df[I] += D_s * a * outward_normal[I];
368 }
369 ham *= D_s * a;
370
371 // Nitsche Dirichlet penalty
372 r += D_s * a * embeddedBoundary_penalty * (u - u_s);
373 dr += D_s * a * embeddedBoundary_penalty;
374 }
375
376 inline void updateEmbeddedBoundaryRobinTerms(const double h_coeff,
377 const double u_infinity,
378 const double u,
379 double &r,
380 double &dr,
381 const double D_s)
382 {
383 // Robin/convective condition a * du/dn = -h * (u - u_infinity) on Gamma_s (n the
384 // fluid's own outward normal, into the solid): substitute this physical law directly
385 // for the natural boundary term -int a du/dn v ds that integration by parts leaves
386 // behind. Unlike updateEmbeddedBoundaryTerms above, a Robin law is already a natural
387 // condition -- nothing is being enforced weakly -- so it needs no consistency,
388 // symmetry, or penalty construction, only this one reaction-like term.
389 r += D_s * h_coeff * (u - u_infinity);
390 dr += D_s * h_coeff;
391 }
392
393 inline void updateImmersedBoundaryTerms(const double immersedBoundary_penalty,
394 const double dV,
395 double *immersedBoundary_normal,
396 double x,
397 double y,
398 double z,
399 const double u_s,
400 const double u,
401 const double grad_u[nSpace],
402 const double a,
403 double &r,
404 double &dr,
405 double &ham,
406 double *dham,
407 double *f,
408 double *df,
409 const double fluxJump,
410 const double *fluxJumpVector,
411 const double D_f)
412 {
413 // todo this doesn't look 1d/3d
414 // Prescribed flux jump across the interface, supplied by the physics file as
415 // [beta du/dn] = fluxJump(x) + fluxJumpVector(x) . n. The vector part carries
416 // the cases whose jump depends on the interface normal. Both default to zero,
417 // so a problem with no prescribed flux jump contributes nothing here.
418 double jump_flux = fluxJump;
419 for (int I = 0; I < nSpace; I++)
420 jump_flux += fluxJumpVector[I] * immersedBoundary_normal[I];
421 r += jump_flux * D_f;
422 dr = 0.0;
423 ham = 0.0;
424 dham[0] = 0.0;
425 dham[1] = 0.0;
426 f[0] = 0.0;
427 f[1] = 0.0;
428 df[0] = 0.0;
429 df[1] = 0.0;
430 }
431
432 inline void calculateElementResidual(int icase_f,
433 // nonzero when the buried-Gamma_f logic demoted this element:
434 // -1 = fluid all Omega^- (mua), +1 = all Omega^+ (mub)
435 const int demoted_f,
436 // element
437 xt::pyarray<double> &mesh_trial_ref,
438 xt::pyarray<double> &mesh_grad_trial_ref,
439 xt::pyarray<double> &mesh_dof,
440 xt::pyarray<int> &mesh_l2g,
441 xt::pyarray<double> &x_ref,
442 xt::pyarray<double> &dV_ref,
443 xt::pyarray<double> &u_trial_ref,
444 xt::pyarray<double> &u_grad_trial_ref,
445 xt::pyarray<double> &u_test_ref,
446 xt::pyarray<double> &u_grad_test_ref,
447 xt::pyarray<double> &elementDiameter,
448 xt::pyarray<double> &elementBoundaryDiameter,
449 xt::pyarray<double> &nodeDiametersArray,
450 xt::pyarray<double> &cfl,
451 double Ct_sge,
452 double sc_uref,
453 double sc_alpha,
454 double useMetrics,
455 // element boundary
456 xt::pyarray<double> &mesh_trial_trace_ref,
457 xt::pyarray<double> &mesh_grad_trial_trace_ref,
458 xt::pyarray<double> &dS_ref,
459 xt::pyarray<double> &u_trial_trace_ref,
460 xt::pyarray<double> &u_grad_trial_trace_ref,
461 xt::pyarray<double> &u_test_trace_ref,
462 xt::pyarray<double> &u_grad_test_trace_ref,
463 xt::pyarray<double> &normal_ref,
464 xt::pyarray<double> &boundaryJac_ref,
465 // physics
466 int nElements_global,
467 int nElementBoundaries_owned,
468 xt::pyarray<int> &u_l2g,
469 xt::pyarray<double> &u_dof,
470 xt::pyarray<int> &sd_rowptr,
471 xt::pyarray<int> &sd_colind,
472 xt::pyarray<double> &q_a,
473 const double *comp_A,
474 const double *comp_B,
475 const double *comp_DA,
476 const double *comp_DB,
477 const double *comp_DFa,
478 xt::pyarray<double> &q_v,
479 xt::pyarray<double> &q_r,
480 int lag_shockCapturingDiffusion,
481 double shockCapturingDiffusion,
482 xt::pyarray<double> &q_numDiff_u,
483 xt::pyarray<double> &q_numDiff_u_last,
484 int offset_u,
485 int stride_u,
486 xt::pyarray<double> &elementResidual_u,
487 int nExteriorElementBoundaries_global,
488 xt::pyarray<int> &exteriorElementBoundariesArray,
489 xt::pyarray<int> &elementBoundariesArray,
490 xt::pyarray<int> &elementBoundaryElementsArray,
491 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray,
492 xt::pyarray<double> &element_u,
493 int eN,
494 const bool embeddedBoundary,
495 const double embeddedBoundary_penalty,
496 xt::pyarray<double> &embeddedBoundary_normal_q,
497 xt::pyarray<double> &embeddedBoundary_u_q,
498 xt::pyarray<double> &embeddedBoundary_u_inner_q,
499 xt::pyarray<double> &embeddedBoundary_u_outer_q,
500 const bool embeddedBoundary_robin,
501 const double embeddedBoundary_robin_h_inner,
502 const double embeddedBoundary_robin_h_outer,
503 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_inner_q,
504 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_outer_q,
505 xt::pyarray<double> &embeddedBoundary_sdf_q,
506 const bool immersedBoundary,
507 const double immersedBoundary_penalty,
508 xt::pyarray<double> &immersedBoundary_sdf_q,
509 xt::pyarray<double> &immersedBoundary_normal_q,
510 xt::pyarray<double> &immersedBoundary_u_q,
511 xt::pyarray<double> &immersedBoundary_fluxJump_q,
512 xt::pyarray<double> &immersedBoundary_fluxJumpVector_q,
513 xt::pyarray<double> &immersedBoundary_solutionJump_nodes,
514 double *element_phi_f,
515 bool &element_active,
516 std::valarray<bool> &elementIsActive,
517 double *JA,
518 double *JB,
519 const double fluxLiftJ,
520 double &L2_error,
521 double &Linfty_error,
522 double &H1_error,
523 double mua,
524 double mub,
525 xt::pyarray<double> &q_u_exact_inner,
526 xt::pyarray<double> &q_u_exact_outer,
527 xt::pyarray<double> &q_grad_u_exact_inner,
528 xt::pyarray<double> &q_grad_u_exact_outer,
529 const bool PG)
530 {
531 // per-element cached equivalent-polynomial/IFEM reconstruction
532 // (see ensureIFEMCacheSized / ifemGeometryGeneration)
533 GfType &gf_f = gf_f_cache[eN];
534 GfType &gf_s = gf_s_cache[eN];
535 for (int i = 0; i < nDOF_test_element; i++)
536 elementResidual_u.data()[i] = 0.0;
537 // loop over quadrature points and compute integrands
538 for (int k = 0; k < nQuadraturePoints_element; k++)
539 {
540 gf_s.set_quad(k);
541 gf_f.set_quad(k);
542 // compute indeces and declare local storage
543 int eN_k = eN * nQuadraturePoints_element + k;
544 int eN_k_3d = eN_k * 3;
545 double h_phi;
546 double u = 0.0;
547 double grad_u[nSpace];
548 double ua = 0.0;
549 double grad_ua[nSpace];
550 double ub = 0.0;
551 double grad_ub[nSpace];
552 double uja = 0.0;
553 double grad_uja[nSpace];
554 double ujb = 0.0;
555 double grad_ujb[nSpace];
556 double m = 0.0;
557 double dm = 0.0;
558 double f[nSpace];
559 double df[nSpace];
560 double f_s[nSpace] = {0., 0.};
561 double df_s[nSpace] = {0., 0.};
562 double ham_s = 0.0;
563 double dham_s[nSpace] = {0., 0.};
564 double f_f[nSpace] = {0., 0.};
565 double df_f[nSpace] = {0., 0.};
566 double ham_f = 0.0;
567 double dham_f[nSpace] = {0., 0.};
568 double m_t = 0.0;
569 double dm_t = 0.0;
570 double pdeResidual_u = 0.0;
571 double Lstar_u[nDOF_test_element];
572 double subgridError_u = 0.0;
573 double tau = 0.0;
574 double tau0 = 0.0;
575 double tau1 = 0.0;
576 double numDiff0 = 0.0;
577 double numDiff1 = 0.0;
578 double *a = NULL;
579 double r = 0.0;
580 double r_s = 0.0;
581 // updateEmbeddedBoundaryTerms ACCUMULATES into its outputs (+=, -=, then ham *= D_s*a),
582 // so one private zero-initialised set per side. Sharing them silently double-counts.
583 double r_s_a = 0.0, dr_s_a = 0.0, ham_s_a = 0.0;
584 double dham_s_a[nSpace] = {0., 0.}, f_s_a[nSpace] = {0., 0.}, df_s_a[nSpace] = {0., 0.};
585 double r_s_b = 0.0, dr_s_b = 0.0, ham_s_b = 0.0;
586 double dham_s_b[nSpace] = {0., 0.}, f_s_b[nSpace] = {0., 0.}, df_s_b[nSpace] = {0., 0.};
587 double dr_s = 0.0;
588 double r_f = 0.0;
589 double dr_f = 0.0;
590 double jac[nSpace * nSpace];
591 double jacDet;
592 double jacInv[nSpace * nSpace];
593 double u_grad_trial[nDOF_trial_element * nSpace];
594 double u_test_dV[nDOF_trial_element];
595 double u_grad_test_dV[nDOF_test_element * nSpace];
596 double ua_grad_trial[nDOF_trial_element * nSpace];
597 double ua_test_dV[nDOF_trial_element];
598 double ua_grad_test_dV[nDOF_test_element * nSpace];
599 double ub_grad_trial[nDOF_trial_element * nSpace];
600 double ub_test_dV[nDOF_trial_element];
601 double ub_grad_test_dV[nDOF_test_element * nSpace];
602 double dV;
603 double x;
604 double y;
605 double z;
606 double G[nSpace * nSpace];
607 double G_dd_G;
608 double tr_G;
609 //
610 // compute solution and gradients at quadrature points
611 //
612 ck.calculateMapping_element(eN,
613 k,
614 mesh_dof.data(),
615 mesh_l2g.data(),
616 mesh_trial_ref.data(),
617 mesh_grad_trial_ref.data(),
618 jac,
619 jacDet,
620 jacInv,
621 x,
622 y,
623 z);
624 ck.calculateH_element(eN,
625 k,
626 nodeDiametersArray.data(),
627 mesh_l2g.data(),
628 mesh_trial_ref.data(),
629 h_phi);
630 // get the physical integration weight
631 dV = fabs(jacDet) * dV_ref.data()[k];
632 // get the metric tensor and friends
633 ck.calculateG(jacInv, G, G_dd_G, tr_G);
634 // get the trial function gradients
635 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
636 // get the solution
637 ck.valFromElementDOF(element_u.data(), &u_trial_ref.data()[k * nDOF_trial_element], u);
638 // get the solution gradients
639 ck.gradFromElementDOF(element_u.data(), u_grad_trial, grad_u);
640 // precalculate test function products with integration weights
641 for (int j = 0; j < nDOF_trial_element; j++)
642 {
643 u_test_dV[j] = u_test_ref.data()[k * nDOF_trial_element + j] * dV;
644 for (int I = 0; I < nSpace; I++)
645 {
646 u_grad_test_dV[j * nSpace + I] = u_grad_trial[j * nSpace + I] * dV; // cek warning won't work for Petrov-Galerkin
647 //PGIFEM
648 }
649 }
650 if (icase_f == 0)
651 {
652 double va[nDOF_trial_element], va_grad_trial[nDOF_trial_element * nSpace], vb[nDOF_trial_element], vb_grad_trial[nDOF_trial_element * nSpace];
653 for (int i = 0; i < nDOF_trial_element; i++)
654 {
655 va[i] = gf_f.VA(i);
656 va_grad_trial[i * nSpace + 0] = gf_f.VA_x(i);
657 va_grad_trial[i * nSpace + 1] = gf_f.VA_y(i);
658 vb[i] = gf_f.VB(i);
659 vb_grad_trial[i * nSpace + 0] = gf_f.VB_x(i);
660 vb_grad_trial[i * nSpace + 1] = gf_f.VB_y(i);
661 }
662
663
664 ck.valFromElementDOF(element_u.data(), va, ua);
665 ck.gradFromElementDOF(element_u.data(), va_grad_trial, grad_ua);
666 ck.valFromElementDOF(element_u.data(), vb, ub);
667 ck.gradFromElementDOF(element_u.data(), vb_grad_trial, grad_ub);
668 ck.valFromElementDOF(JA, va, uja);
669 ck.gradFromElementDOF(JA, va_grad_trial, grad_uja);
670 ck.valFromElementDOF(JB, vb, ujb);
671 ck.gradFromElementDOF(JB, vb_grad_trial, grad_ujb);
672 // Flux-jump lift J_f*psi, added once as known data (zero unless Gamma_f cuts the cell
673 // and a flux jump is prescribed). psi is the unit lift of the basis constructor.
674 uja += fluxLiftJ * gf_f.PSIA();
675 ujb += fluxLiftJ * gf_f.PSIB();
676 grad_uja[0] += fluxLiftJ * gf_f.PSIA_x();
677 grad_uja[1] += fluxLiftJ * gf_f.PSIA_y();
678 grad_ujb[0] += fluxLiftJ * gf_f.PSIB_x();
679 grad_ujb[1] += fluxLiftJ * gf_f.PSIB_y();
680 for (int i = 0; i < nDOF_test_element; i++)
681 {
682 ua_test_dV[i] = va[i] * dV;
683 ub_test_dV[i] = vb[i] * dV;
684 for (int I = 0; I < nSpace; I++)
685 {
686 ua_grad_test_dV[i * nSpace + I] = va_grad_trial[i * nSpace + I] * dV;
687 ub_grad_test_dV[i * nSpace + I] = vb_grad_trial[i * nSpace + I] * dV;
688 }
689 }
690 for (int j = 0; j < nDOF_trial_element; j++)
691 {
692 for (int I = 0; I < nSpace; I++)
693 {
694 ua_grad_trial[j * nSpace + I] = va_grad_trial[j * nSpace + I];
695 ub_grad_trial[j * nSpace + I] = vb_grad_trial[j * nSpace + I];
696 }
697 }
698 if (PG)
699 {
700 // Petrov-Galerkin: test with ordinary P1 hat functions instead of
701 // the enriched va/vb branches; trial/solution side is unchanged.
702 for (int i = 0; i < nDOF_test_element; i++)
703 {
704 ua_test_dV[i] = u_test_dV[i];
705 ub_test_dV[i] = u_test_dV[i];
706 for (int I = 0; I < nSpace; I++)
707 {
708 ua_grad_test_dV[i * nSpace + I] = u_grad_test_dV[i * nSpace + I];
709 ub_grad_test_dV[i * nSpace + I] = u_grad_test_dV[i * nSpace + I];
710 }
711 }
712 }
713 }
714 //
715 // calculate pde coefficients at quadrature points
716 //
717 // evaluateCoefficients();
718 // just set from pre-evaluated quadrature point values for now
719 a = &q_a.data()[eN_k * sd_rowptr.data()[nSpace]];
720 // On a demoted element the fluid is entirely one phase, but the pre-evaluated
721 // pointwise data still switches branch across the buried Gamma_f -- which lies inside
722 // the solid part of this element. H_s is only moment-exact against polynomials, and a
723 // coefficient that jumps mid-element is not one, so those quadrature points inject a
724 // contribution from a fluid that is not physically present. Substitute the surviving
725 // phase's constants, which makes the integrand polynomial again and H_s exact.
726 double a_dem[nSpace * nSpace];
727 double u_s_dem = embeddedBoundary_u_q.data()[eN_k];
728 if (demoted_f != 0)
729 {
730 for (int I = 0; I < nSpace * nSpace; I++) a_dem[I] = 0.0;
731 const double mu_dem = (demoted_f < 0) ? mua : mub;
732 for (int I = 0; I < nSpace; I++) a_dem[I * nSpace + I] = mu_dem;
733 a = a_dem;
734 u_s_dem = (demoted_f < 0) ? embeddedBoundary_u_inner_q.data()[eN_k]
735 : embeddedBoundary_u_outer_q.data()[eN_k];
736 }
737 r = q_r.data()[eN_k];
738 for (int I = 0; I < nSpace; I++)
739 {
740 f[I] = q_v.data()[eN_k * nSpace + I] * u;
741 df[I] = q_v.data()[eN_k * nSpace + I];
742 }
743 const double H_s = gf_s.H(0., 0.);
744 const double D_s = gf_s.D(0., 0.);
745 // Per-side Dirac for the solid surface. Weighting the single-cut D_s by a region fit
746 // (ImH_f * D_s) is the product-of-two-fits error again, and a large one -- about 31% on
747 // the segment length. Declared here because the Nitsche terms below consume it.
748 // Defaults to D_s, so elements cut by only one level set are untouched.
749 double D_s_a = D_s, D_s_b = D_s;
750 if (demoted_f != 0)
751 {
752 // The whole solid surface in this element borders the one surviving fluid region.
753 D_s_a = (demoted_f < 0) ? D_s : 0.0;
754 D_s_b = (demoted_f < 0) ? 0.0 : D_s;
755 }
756 else if (comp_DA != nullptr)
757 {
758 const double xr_d = x_ref.data()[k * 3 + 0], yr_d = x_ref.data()[k * 3 + 1];
759 D_s_a = equivalent_polynomials::composite::evaluate<nP_fit>(comp_DA, xr_d, yr_d);
760 D_s_b = equivalent_polynomials::composite::evaluate<nP_fit>(comp_DB, xr_d, yr_d);
761 }
762 if (embeddedBoundary)
763 {
764 double level_set_normal[nSpace];
765 double sign = 0.0;
766 double norm_exact = 0.0, norm_cut = 0.0;
767 for (int I = 0; I < nSpace; I++)
768 {
769 sign += embeddedBoundary_normal_q.data()[eN_k_3d + I] * gf_s.get_normal()[I];
770 level_set_normal[I] = gf_s.get_normal()[I];
771 norm_cut += level_set_normal[I] * level_set_normal[I];
772 norm_exact += embeddedBoundary_normal_q.data()[eN_k_3d + I] * embeddedBoundary_normal_q.data()[eN_k_3d + I];
773 }
774 assert(std::fabs(1.0 - norm_cut) < 1.0e-8);
775 assert(std::fabs(1.0 - norm_exact) < 1.0e-8);
776 if (sign < 0.0)
777 for (int I = 0; I < nSpace; I++)
778 level_set_normal[I] *= -1.0;
779 if (icase_f == 0)
780 {
781 // The solution here is ua on the mua side and ub on the mub side; the plain `u` is
782 // neither. Handing `u` to the penalty makes (u - u_s) nonzero even when the discrete
783 // solution is exactly right, and the O(100/h) penalty amplifies that.
784
785 if (embeddedBoundary_robin)
786 {
787 // Robin needs no jump-corrected gradient (no flux term of its own),
788 // but the jump-corrected VALUE still matters: u_infinity was derived
789 // from the exact ua/ub, not from ua+uja's jump-free counterpart.
790 // Deliberately NOT embeddedBoundary_u_inner_q/_outer_q: those are the
791 // Dirichlet data on Gamma_s, unrelated to the Robin target.
792 updateEmbeddedBoundaryRobinTerms(embeddedBoundary_robin_h_inner,
793 embeddedBoundary_robin_uInfinity_inner_q.data()[eN_k], ua + uja, r_s_a, dr_s_a, D_s_a);
794 updateEmbeddedBoundaryRobinTerms(embeddedBoundary_robin_h_outer,
795 embeddedBoundary_robin_uInfinity_outer_q.data()[eN_k], ub + ujb, r_s_b, dr_s_b, D_s_b);
796 }
797 else
798 {
799 double ga[nSpace], gb[nSpace];
800 for (int I = 0; I < nSpace; I++)
801 {
802 ga[I] = grad_ua[I] + grad_uja[I];
803 gb[I] = grad_ub[I] + grad_ujb[I];
804 }
805 updateEmbeddedBoundaryTerms(embeddedBoundary_penalty / h_phi, dV, level_set_normal,
806 embeddedBoundary_u_inner_q.data()[eN_k], ua + uja, ga, mua,
807 r_s_a, dr_s_a, ham_s_a, dham_s_a, f_s_a, df_s_a, D_s_a);
808 updateEmbeddedBoundaryTerms(embeddedBoundary_penalty / h_phi, dV, level_set_normal,
809 embeddedBoundary_u_outer_q.data()[eN_k], ub + ujb, gb, mub,
810 r_s_b, dr_s_b, ham_s_b, dham_s_b, f_s_b, df_s_b, D_s_b);
811 }
812 }
813 else
814 {
815 // icase_f != 0: this element is wholly on one side of Gamma_f (icase_f < 0
816 // water/mua, icase_f > 0 air/mub -- the same sign convention as demoted_f
817 // and mu_dem above), so a single scalar h and a single per-phase target
818 // apply throughout -- NOT u_s_dem, which (like embeddedBoundary_u_inner_q
819 // above) carries the true Dirichlet solution, not the Robin target.
820 if (embeddedBoundary_robin)
821 {
822 const double h_here = (icase_f < 0) ? embeddedBoundary_robin_h_inner
823 : embeddedBoundary_robin_h_outer;
824 const double u_inf_here = (icase_f < 0)
825 ? embeddedBoundary_robin_uInfinity_inner_q.data()[eN_k]
826 : embeddedBoundary_robin_uInfinity_outer_q.data()[eN_k];
827 updateEmbeddedBoundaryRobinTerms(h_here, u_inf_here, u, r_s, dr_s, D_s);
828 }
829 else
830 {
831 updateEmbeddedBoundaryTerms(embeddedBoundary_penalty / h_phi, dV, level_set_normal,
832 u_s_dem, u, grad_u, a[0],
833 r_s, dr_s, ham_s, dham_s, f_s, df_s, D_s);
834 }
835 }
836 }
837 const double ImH_f = gf_f.ImH(0., 0.);
838 const double H_f = gf_f.H(0., 0.);
839 // Region weights for the bulk terms. On an element cut by both level sets these
840 // come from the composite fit; otherwise they are the usual products, unchanged.
841 double wA = ImH_f * H_s, wB = H_f * H_s;
842 if (demoted_f != 0)
843 {
844 // Only one fluid region exists here; give it the whole fluid weight and the other none.
845 wA = (demoted_f < 0) ? H_s : 0.0;
846 wB = (demoted_f < 0) ? 0.0 : H_s;
847 }
848 else if (comp_A != nullptr)
849 {
850 const double xr = x_ref.data()[k * 3 + 0], yr = x_ref.data()[k * 3 + 1];
853 }
854 double D_f = gf_f.D(0., 0.);
855 if (comp_DFa != nullptr)
856 {
857 // Gamma_f's own Dirac measure, restricted to the part still inside the active
858 // fluid domain (see comp_DFa_cache's declaration) -- the plain, single-curve
859 // gf_f.D(0.,0.) above double-counts whatever part of Gamma_f's chord falls
860 // inside Gamma_s's masked-out solid on a triply-cut element.
861 const double xr_f = x_ref.data()[k * 3 + 0], yr_f = x_ref.data()[k * 3 + 1];
862 D_f = equivalent_polynomials::composite::evaluate<nP_fit>(comp_DFa, xr_f, yr_f);
863 }
864 // A demoted element's Gamma_f lies entirely inside the solid: no part of it borders
865 // fluid, so the flux-jump load must vanish exactly there (any fit would leave a
866 // spurious source).
867 if (demoted_f != 0)
868 D_f = 0.0;
869 if (H_s != 0.0 || D_s != 0.0 || D_f != 0.0) // for one embedded interface and one immersed interface
870 {
871 element_active = true;
872 elementIsActive[eN] = true;
873 }
874 if (immersedBoundary)
875 {
876 double level_set_normal[nSpace];
877 double sign = 0.0;
878 double norm_exact = 0.0, norm_cut = 0.0;
879 for (int I = 0; I < nSpace; I++)
880 {
881 sign += immersedBoundary_normal_q.data()[eN_k_3d + I] * gf_f.get_normal()[I];
882 level_set_normal[I] = gf_f.get_normal()[I];
883 norm_cut += level_set_normal[I] * level_set_normal[I];
884 norm_exact += immersedBoundary_normal_q.data()[eN_k_3d + I] * immersedBoundary_normal_q.data()[eN_k_3d + I];
885 }
886 assert(std::fabs(1.0 - norm_cut) < 1.0e-8);
887 assert(std::fabs(1.0 - norm_exact) < 1.0e-8);
888 if (sign < 0.0)
889 for (int I = 0; I < nSpace; I++)
890 level_set_normal[I] *= -1.0;
891 updateImmersedBoundaryTerms(immersedBoundary_penalty / h_phi, // penalty,
892 dV,
893 level_set_normal,
894 x,
895 y,
896 z,
897 immersedBoundary_u_q.data()[eN_k],
898 u,
899 grad_u,
900 a[0], // assume scalar diffusion for now
901 r_f,
902 dr_f,
903 ham_f,
904 dham_f,
905 f_f,
906 df_f,
907 immersedBoundary_fluxJump_q.data()[eN_k],
908 &immersedBoundary_fluxJumpVector_q.data()[eN_k_3d],
909 // Gamma_f's jump physics only applies in the fluid: on an element that
910 // sits (locally, at this quadrature point) entirely inside the embedded
911 // solid, H_s==0 and D_f alone (gf_f.D, or the raw single-curve fallback
912 // when comp_DFa isn't available) does not know that -- it only encodes
913 // where Gamma_f itself is, not whether the fluid actually exists there.
914 // Without this factor an element wholly inside the solid but crossed by
915 // Gamma_f gets a fixed, u-independent Dirac source with nothing in the
916 // Jacobian to balance it (the bulk terms are already correctly zero via
917 // wA=wB=0), so Newton can never drive that row's residual to zero.
918 // On a triply-cut cell comp_DFa is already restricted to the fluid part of
919 // Gamma_f; multiplying it by the H_s fit again would restrict twice (a
920 // product of two fits), so H_s is applied only to the single-curve D_f.
921 (comp_DFa != nullptr) ? D_f : D_f * H_s);
922 }
923 //
924 // moving mesh
925 //
926 /* double mesh_velocity[3]; */
927 /* mesh_velocity[0] = xt; */
928 /* mesh_velocity[1] = yt; */
929 /* mesh_velocity[2] = zt; */
930 /* for (int I=0;I<nSpace;I++) */
931 /* { */
932 /* f[I] -= MOVING_DOMAIN*m*mesh_velocity[I]; */
933 /* df[I] -= MOVING_DOMAIN*dm*mesh_velocity[I]; */
934 /* } */
935 //
936 // calculate time derivative at quadrature points
937 //
938 /* ck.bdf(alphaBDF, */
939 /* q_m_betaBDF.data()[eN_k], */
940 /* m, */
941 /* dm, */
942 /* m_t, */
943 /* dm_t); */
944 //
945 // calculate subgrid error (strong residual and adjoint)
946 //
947 // calculate strong residual
948 pdeResidual_u = ck.Advection_strong(df, grad_u) + ck.Reaction_strong(r); // ck.Mass_strong(m_t) + ck.Advection_strong(df,grad_u) + ck.Reaction_strong(r);
949 // calculate adjoint
950 for (int i = 0; i < nDOF_test_element; i++)
951 {
952 // int eN_k_i_nSpace = (eN_k*nDOF_trial_element+i)*nSpace;
953 // Lstar_u[i] = ck.Advection_adjoint(df,&u_grad_test_dV.data()[eN_k_i_nSpace]);
954 int i_nSpace = i * nSpace;
955 Lstar_u[i] = ck.Advection_adjoint(df, &u_grad_test_dV[i_nSpace]);
956 }
957 // calculate tau and tau*Res
958 calculateSubgridError_tau(elementDiameter.data()[eN], dm_t, df, cfl.data()[eN_k], tau0);
960 G,
961 dm_t,
962 df,
963 tau1,
964 cfl.data()[eN_k]);
965
966 tau = useMetrics * tau1 + (1.0 - useMetrics) * tau0;
967
968 subgridError_u = -tau * pdeResidual_u;
969 //
970 // calculate shock capturing diffusion
971 //
972 ck.calculateNumericalDiffusion(shockCapturingDiffusion, elementDiameter.data()[eN], pdeResidual_u, grad_u, numDiff0);
973 ck.calculateNumericalDiffusion(shockCapturingDiffusion, sc_uref, sc_alpha, G, G_dd_G, pdeResidual_u, grad_u, numDiff1);
974 q_numDiff_u.data()[eN_k] = useMetrics * numDiff1 + (1.0 - useMetrics) * numDiff0;
975 //
976 // update element residual
977 //
978 // Leveque & Li 1994, Examples 1, 3, 4, PWC, PWL, PWQ, PWcubic
979 double a_loc[nSpace * nSpace];
980 for (int I = 0; I < nSpace * nSpace; I++) a_loc[I] = 0.0;
981
982 for (int i = 0; i < nDOF_test_element; i++)
983 {
984 int i_nSpace = i * nSpace;
985 if (icase_f == 0)
986 {
987 if(!gf_f.exact.edge && !gf_f.exact.corner)//full cut or cut on boundary of negative cell
988 {
989 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mua;
990 elementResidual_u.data()[i] += wA * (ck.Advection_weak(f, &ua_grad_test_dV[i_nSpace]) +
991 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_ua, &ua_grad_test_dV[i_nSpace]) +
992 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_uja, &ua_grad_test_dV[i_nSpace]) +
993 ck.Reaction_weak(r, ua_test_dV[i]) +
994 ck.NumericalDiffusion(q_numDiff_u_last.data()[eN_k], grad_ua, &ua_grad_test_dV[i_nSpace]));
995
996 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mub;
997 elementResidual_u.data()[i] += wB * (ck.Advection_weak(f, &ub_grad_test_dV[i_nSpace]) +
998 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_ub, &ub_grad_test_dV[i_nSpace]) +
999 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_ujb, &ub_grad_test_dV[i_nSpace]) +
1000 ck.Reaction_weak(r, ub_test_dV[i]) +
1001 ck.NumericalDiffusion(q_numDiff_u_last.data()[eN_k], grad_ub, &ub_grad_test_dV[i_nSpace]));
1002 // Flux-jump load int_Gamma_f J_f v: D_f times the IFE test function, NOT weighted by
1003 // wA/wB. On Gamma_f va = vb = v, and D_f integrates va and vb (polynomials of degree
1004 // <= nP_fit) exactly; weighting by the Heaviside fits would multiply two fits and
1005 // move part of the load between branch-a and branch-b nodes.
1006 elementResidual_u.data()[i] += r_f * 0.5 * (ua_test_dV[i] + ub_test_dV[i]);
1007 }
1008 else if (gf_f.exact.edge == -1 || gf_f.exact.corner == -1)
1009 {
1010 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mua;
1011 elementResidual_u.data()[i] += wA * (ck.Advection_weak(f, &ua_grad_test_dV[i_nSpace]) +
1012 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_ua, &ua_grad_test_dV[i_nSpace]) +
1013 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_uja, &ua_grad_test_dV[i_nSpace]) +
1014 ck.Reaction_weak(r, ua_test_dV[i]) +
1015 ck.NumericalDiffusion(q_numDiff_u_last.data()[eN_k], grad_ua, &ua_grad_test_dV[i_nSpace]));
1016 elementResidual_u.data()[i] += r_f * ua_test_dV[i];
1017 }
1018 else if (gf_f.exact.edge == 1 || gf_f.exact.corner == 1)
1019 {
1020 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mub;
1021 elementResidual_u.data()[i] += wB * (ck.Advection_weak(f, &ub_grad_test_dV[i_nSpace]) +
1022 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_ub, &ub_grad_test_dV[i_nSpace]) +
1023 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a_loc, grad_ujb, &ub_grad_test_dV[i_nSpace]) +
1024 ck.Reaction_weak(r, ub_test_dV[i]) +
1025 ck.NumericalDiffusion(q_numDiff_u_last.data()[eN_k], grad_ub, &ub_grad_test_dV[i_nSpace]));
1026 elementResidual_u.data()[i] += r_f * ub_test_dV[i];
1027 }
1028 else assert(false && "Invalid gf_f.exact.edge/corner values. Should be -1, 0 or +1.");
1029 }
1030 else
1031 {
1032
1033 elementResidual_u.data()[i] += H_s * (ck.Advection_weak(f, &u_grad_test_dV[i_nSpace]) +
1034 ck.Diffusion_weak(sd_rowptr.data(), sd_colind.data(), a, grad_u, &u_grad_test_dV[i_nSpace]) +
1035 ck.Reaction_weak(r, u_test_dV[i]) +
1036 ck.SubgridError(subgridError_u, Lstar_u[i]) +
1037 ck.NumericalDiffusion(q_numDiff_u_last.data()[eN_k], grad_u, &u_grad_test_dV[i_nSpace]));
1038 }
1039 if (embeddedBoundary)
1040 {
1041 if (gf_s.exact.edge >= 0 && !gf_s.exact.corner)
1042 {
1043 if (icase_f == 0)
1044 {
1045 // Test against the reconstructed basis the bulk uses, split by which
1046 // fluid region each part of the solid surface sits in. ImH_f + H_f = 1,
1047 // so the total contribution is preserved.
1048 // Each side already carries its own piece of the solid surface via
1049 // D_s_a / D_s_b, so no ImH_f / H_f weighting here.
1050 elementResidual_u.data()[i] +=
1051 (ck.Advection_weak(f_s_a, &ua_grad_test_dV[i_nSpace]) +
1052 ck.Reaction_weak(r_s_a, ua_test_dV[i]) +
1053 ck.Hamiltonian_weak(ham_s_a, ua_test_dV[i])) +
1054 (ck.Advection_weak(f_s_b, &ub_grad_test_dV[i_nSpace]) +
1055 ck.Reaction_weak(r_s_b, ub_test_dV[i]) +
1056 ck.Hamiltonian_weak(ham_s_b, ub_test_dV[i]));
1057 }
1058 else
1059 {
1060 elementResidual_u.data()[i] += (ck.Advection_weak(f_s, &u_grad_test_dV[i_nSpace]) +
1061 ck.Reaction_weak(r_s, u_test_dV[i]) +
1062 ck.Hamiltonian_weak(ham_s, u_test_dV[i]));
1063 }
1064 }
1065 }
1066 // immersedBoundary's r_f (the flux-jump Dirac reaction) is tested above against the
1067 // IFEM test function on Gamma_f (0.5*(va+vb), = va = vb there), not against the plain,
1068 // unenriched u_test_dV (va != vb != u_test away from the vertices) and not inside the
1069 // wA/wB-weighted branch terms (a product of two fits that moves load between branches). f_f/ham_f/df_f/dham_f are always zero (see
1070 // updateImmersedBoundaryTerms), so nothing else from that call belongs here.
1071 }
1072 // Error is only meaningful where the PDE is actually assembled -- strictly, in the
1073 // active fluid domain phi_s >= 0. The old gate used H_s (the moment-fit area
1074 // weight), which is only guaranteed correct in an *integrated* sense: pointwise it
1075 // can overshoot [0,1] or go negative near a cut (confirmed directly: values like
1076 // H_s=1.14 and H_s=-0.066 were measured on a real triply-cut element), so "H_s>0"
1077 // let some genuinely-inside-the-solid quadrature points compete for the L^infty
1078 // max, and weighting L2 by H_s (rather than gating it) let their squared error
1079 // contribute in proportion to an overshot/undershot weight instead of either the
1080 // full contribution or none. Gate by the raw, pointwise level-set value instead --
1081 // exact, not a moment-fit approximation -- so no solid-side point (node or
1082 // quadrature point alike) ever enters either norm, regardless of how H_s's fit
1083 // behaves right at that point.
1084 const double phi_s_q = embeddedBoundary_sdf_q.data()[eN_k];
1085 double L2_contrib = 0.0;
1086 // H1 seminorm part: |grad(u - u_h)|^2 of the phase the point belongs to, with the same
1087 // weights and the same fluid gate as the L2 error.
1088 double H1_contrib = 0.0;
1089 const double *gex_in = &q_grad_u_exact_inner.data()[eN_k * nSpace];
1090 const double *gex_out = &q_grad_u_exact_outer.data()[eN_k * nSpace];
1091 if (icase_f == 0)
1092 {
1093 double sol_in = q_u_exact_inner.data()[eN_k];
1094 double err_in = fabs(ua + uja - sol_in);
1095 L2_contrib += ImH_f * err_in * err_in * dV;
1096 double sol_out = q_u_exact_outer.data()[eN_k];
1097 double err_out = fabs(ub + ujb - sol_out);
1098 L2_contrib += H_f * err_out * err_out * dV;
1099 for (int I = 0; I < nSpace; I++)
1100 {
1101 const double gerr_in = grad_ua[I] + grad_uja[I] - gex_in[I];
1102 const double gerr_out = grad_ub[I] + grad_ujb[I] - gex_out[I];
1103 H1_contrib += (ImH_f * gerr_in * gerr_in + H_f * gerr_out * gerr_out) * dV;
1104 }
1105 if (phi_s_q >= 0.0)
1106 {
1107 if (ImH_f >= H_f)
1108 Linfty_error = std::max(Linfty_error, err_in);
1109 else
1110 Linfty_error = std::max(Linfty_error, err_out);
1111 }
1112 }
1113 else
1114 {
1115 if (icase_f == -1)
1116 {
1117 double sol = q_u_exact_inner.data()[eN_k];
1118 double err = fabs(u - sol);
1119 L2_contrib += err * err * dV;
1120 for (int I = 0; I < nSpace; I++)
1121 H1_contrib += (grad_u[I] - gex_in[I]) * (grad_u[I] - gex_in[I]) * dV;
1122 if (phi_s_q >= 0.0)
1123 {
1124 Linfty_error = std::max(Linfty_error, err);
1125 }
1126 }
1127 if (icase_f == 1)
1128 {
1129 double sol = q_u_exact_outer.data()[eN_k];
1130 double err = fabs(u - sol);
1131 L2_contrib += err * err * dV;
1132 for (int I = 0; I < nSpace; I++)
1133 H1_contrib += (grad_u[I] - gex_out[I]) * (grad_u[I] - gex_out[I]) * dV;
1134 if (phi_s_q >= 0.0)
1135 {
1136 Linfty_error = std::max(Linfty_error, err);
1137 }
1138 }
1139 }
1140 if (phi_s_q >= 0.0)
1141 {
1142 L2_error += L2_contrib;
1143 H1_error += H1_contrib;
1144 }
1145 }
1146 }
1147
1149 {
1150 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
1151 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
1152 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
1153 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
1154 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
1155 xt::pyarray<double> &u_trial_ref = args.array<double>("u_trial_ref");
1156 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
1157 xt::pyarray<double> &u_test_ref = args.array<double>("u_test_ref");
1158 xt::pyarray<double> &u_grad_test_ref = args.array<double>("u_grad_test_ref");
1159 xt::pyarray<double> &elementDiameter = args.array<double>("elementDiameter");
1160 xt::pyarray<double> &cfl = args.array<double>("cfl");
1161 double Ct_sge = args.scalar<double>("Ct_sge");
1162 double sc_uref = args.scalar<double>("sc_uref");
1163 double sc_alpha = args.scalar<double>("sc_alpha");
1164 double useMetrics = args.scalar<double>("useMetrics");
1165 xt::pyarray<double> &mesh_trial_trace_ref = args.array<double>("mesh_trial_trace_ref");
1166 xt::pyarray<double> &mesh_grad_trial_trace_ref = args.array<double>("mesh_grad_trial_trace_ref");
1167 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
1168 xt::pyarray<double> &u_trial_trace_ref = args.array<double>("u_trial_trace_ref");
1169 xt::pyarray<double> &u_grad_trial_trace_ref = args.array<double>("u_grad_trial_trace_ref");
1170 xt::pyarray<double> &u_test_trace_ref = args.array<double>("u_test_trace_ref");
1171 xt::pyarray<double> &u_grad_test_trace_ref = args.array<double>("u_grad_test_trace_ref");
1172 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
1173 xt::pyarray<double> &boundaryJac_ref = args.array<double>("boundaryJac_ref");
1174 int nElements_global = args.scalar<int>("nElements_global");
1175 xt::pyarray<int> &u_l2g = args.array<int>("u_l2g");
1176 xt::pyarray<double> &u_dof = args.array<double>("u_dof");
1177 xt::pyarray<int> &sd_rowptr = args.array<int>("sd_rowptr");
1178 xt::pyarray<int> &sd_colind = args.array<int>("sd_colind");
1179 xt::pyarray<double> &q_a = args.array<double>("q_a");
1180 xt::pyarray<double> &q_v = args.array<double>("q_v");
1181 xt::pyarray<double> &q_r = args.array<double>("q_r");
1182 int lag_shockCapturing = args.scalar<int>("lag_shockCapturing");
1183 double shockCapturingDiffusion = args.scalar<double>("shockCapturingDiffusion");
1184 xt::pyarray<double> &q_numDiff_u = args.array<double>("q_numDiff_u");
1185 xt::pyarray<double> &q_numDiff_u_last = args.array<double>("q_numDiff_u_last");
1186 int offset_u = args.scalar<int>("offset_u");
1187 int stride_u = args.scalar<int>("stride_u");
1188 xt::pyarray<double> &globalResidual = args.array<double>("globalResidual");
1189 int nExteriorElementBoundaries_global = args.scalar<int>("nExteriorElementBoundaries_global");
1190 xt::pyarray<int> &exteriorElementBoundariesArray = args.array<int>("exteriorElementBoundariesArray");
1191 xt::pyarray<int> &elementBoundaryElementsArray = args.array<int>("elementBoundaryElementsArray");
1192 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray = args.array<int>("elementBoundaryLocalElementBoundariesArray");
1193 xt::pyarray<double> &ebqe_a = args.array<double>("ebqe_a");
1194 xt::pyarray<double> &ebqe_v = args.array<double>("ebqe_v");
1195 xt::pyarray<int> &isDOFBoundary_u = args.array<int>("isDOFBoundary_u");
1196 xt::pyarray<double> &ebqe_bc_u_ext = args.array<double>("ebqe_bc_u_ext");
1197 xt::pyarray<int> &isDiffusiveFluxBoundary_u = args.array<int>("isDiffusiveFluxBoundary_u");
1198 xt::pyarray<int> &isAdvectiveFluxBoundary_u = args.array<int>("isAdvectiveFluxBoundary_u");
1199 xt::pyarray<double> &ebqe_bc_flux_u_ext = args.array<double>("ebqe_bc_flux_u_ext");
1200 xt::pyarray<double> &ebqe_bc_advectiveFlux_u_ext = args.array<double>("ebqe_bc_advectiveFlux_u_ext");
1201 xt::pyarray<double> &ebqe_penalty_ext = args.array<double>("ebqe_penalty_ext");
1202 const bool embeddedBoundary = args.scalar<int>("embeddedBoundary");
1203 const double embeddedBoundary_penalty = args.scalar<double>("embeddedBoundary_penalty");
1204 const double embeddedBoundary_ghost_penalty = args.scalar<double>("embeddedBoundary_ghost_penalty");
1205 xt::pyarray<double> &embeddedBoundary_sdf_nodes = args.array<double>("embeddedBoundary_sdf_nodes");
1206 xt::pyarray<double> &embeddedBoundary_sdf_q = args.array<double>("embeddedBoundary_sdf_q");
1207 xt::pyarray<double> &embeddedBoundary_normal_q = args.array<double>("embeddedBoundary_normal_q");
1208 xt::pyarray<double> &embeddedBoundary_u_q = args.array<double>("embeddedBoundary_u_q");
1209 xt::pyarray<double> &embeddedBoundary_u_inner_q = args.array<double>("embeddedBoundary_u_inner_q");
1210 xt::pyarray<double> &embeddedBoundary_u_outer_q = args.array<double>("embeddedBoundary_u_outer_q");
1211 const bool embeddedBoundary_robin = args.scalar<int>("embeddedBoundary_robin");
1212 const double embeddedBoundary_robin_h_inner = args.scalar<double>("embeddedBoundary_robin_h_inner");
1213 const double embeddedBoundary_robin_h_outer = args.scalar<double>("embeddedBoundary_robin_h_outer");
1214 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_inner_q = args.array<double>("embeddedBoundary_robin_uInfinity_inner_q");
1215 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_outer_q = args.array<double>("embeddedBoundary_robin_uInfinity_outer_q");
1216 const bool immersedBoundary = args.scalar<int>("immersedBoundary");
1217 const double immersedBoundary_penalty = args.scalar<double>("immersedBoundary_penalty");
1218 const double immersedSCIFEM_switch = args.scalar<double>("immersedSCIFEM_switch");
1219 const double immersedSCIFEM_penalty = args.scalar<double>("immersedSCIFEM_penalty");
1220 const bool PG = args.scalar<int>("PG");
1221 xt::pyarray<double> &immersedBoundary_sdf_nodes = args.array<double>("immersedBoundary_sdf_nodes");
1222 xt::pyarray<double> &immersedBoundary_sdf_q = args.array<double>("immersedBoundary_sdf_q");
1223 xt::pyarray<double> &immersedBoundary_normal_q = args.array<double>("immersedBoundary_normal_q");
1224 xt::pyarray<double> &immersedBoundary_u_q = args.array<double>("immersedBoundary_u_q");
1225 xt::pyarray<double> &immersedBoundary_fluxJump_q = args.array<double>("immersedBoundary_fluxJump_q");
1226 xt::pyarray<double> &immersedBoundary_fluxJumpVector_q = args.array<double>("immersedBoundary_fluxJumpVector_q");
1227 xt::pyarray<double> &immersedBoundary_fluxJump_element = args.array<double>("immersedBoundary_fluxJump_element");
1228 xt::pyarray<double> &immersedBoundary_solutionJump_nodes = args.array<double>("immersedBoundary_solutionJump_nodes");
1229 xt::pyarray<double> &isActiveDOF = args.array<double>("isActiveDOF");
1230 const double eb_adjoint_sigma = args.scalar<double>("eb_adjoint_sigma");
1231 xt::pyarray<double> &x_ref = args.array<double>("x_ref");
1232 xt::pyarray<double> &xB_ref = args.array<double>("xB_ref");
1233 xt::pyarray<int> &elementBoundariesArray = args.array<int>("elementBoundariesArray");
1234 const int nElementBoundaries_owned = args.scalar<int>("nElementBoundaries_owned");
1235 // Owned elements are numbered first on each rank; the L2 error sums them only (the
1236 // overlap elements are some other rank's owned ones and are summed there).
1237 const int nElements_owned = args.scalar<int>("nElements_owned");
1238 xt::pyarray<double> &elementBoundaryDiameter = args.array<double>("elementBoundaryDiameter");
1239 xt::pyarray<double> &nodeDiametersArray = args.array<double>("nodeDiametersArray");
1240 xt::pyarray<double> &L2_error = args.array<double>("L2_error");
1241 xt::pyarray<double> &Linfty_error = args.array<double>("Linfty_error");
1242 const double mua = args.scalar<double>("mua");
1243 const double mub = args.scalar<double>("mub");
1244 const double jf = args.scalar<double>("jf");
1245 xt::pyarray<double> &q_u_exact_inner = args.array<double>("q_u_exact_inner");
1246 xt::pyarray<double> &q_u_exact_outer = args.array<double>("q_u_exact_outer");
1247 xt::pyarray<double> &q_grad_u_exact_inner = args.array<double>("q_grad_u_exact_inner");
1248 xt::pyarray<double> &q_grad_u_exact_outer = args.array<double>("q_grad_u_exact_outer");
1249 xt::pyarray<double> &H1_error = args.array<double>("H1_error");
1250 const bool recomputeIFEMGeometry = args.scalar<int>("recomputeIFEMGeometry");
1251 const bool fluxJumpLift = args.scalar<int>("immersedBoundary_fluxJumpLift");
1252 ensureIFEMCacheSized(nElements_global); // also (re)asserts useExact = true on (re)allocation
1253 if (recomputeIFEMGeometry)
1255 for (int eN = 0; eN < nElements_global; eN++)
1256 fluxLiftAmp[eN] = immersedBoundary_fluxJump_element.data()[eN];
1257 ifem_boundaries.clear();
1258 ifem_boundary_elements.clear();
1259 cutfem_boundaries.clear();
1261 elementIsActive.resize(nElements_global);
1262 // Coefficients.triplyCut_edgeFluidSide: take the fluid-side one-sided edge correction on
1263 // triply-cut cells (flips the sign of the correction in residual and Jacobian).
1264 const bool tcFluidMode = args.scalar<int>("triplyCut_edgeFluidSide");
1265 // Coefficients.triplyCut_edgeBoundaryOnly (use with SCIFEM on): keep the one-sided fluid-side
1266 // correction only on root-adjacent edges that lie on the exterior boundary. Interior ones are then
1267 // handled by SCIFEM's two-sided consistency term, which has no counterpart on an exterior edge.
1268 const bool tcBoundaryOnly = args.scalar<int>("triplyCut_edgeBoundaryOnly");
1269 const double tcSgn = tcFluidMode ? -1.0 : 1.0;
1270
1271 //
1272 // loop over elements to compute volume integrals and load them into element and global residual
1273 //
1274 // eN is the element index
1275 // eN_k is the quadrature point index for a scalar
1276 // eN_k_nSpace is the quadrature point index for a vector
1277 // eN_i is the element test function index
1278 // eN_j is the element trial function index
1279 // eN_k_j is the quadrature point index for a trial function
1280 // eN_k_i is the quadrature point index for a trial function
1281 // Per-face reference-ELEMENT quadrature points, taken from proteus' own trace tables.
1282 // Simplex::calculate(..., isBoundary=true) reads its xi_r argument as reference *element*
1283 // coordinates (it forms x = node0 + Jac_0*xi_r), but xB_ref holds reference *boundary*
1284 // points (t,0,0) -- passing those directly puts every face's points on the local
1285 // node0->node1 edge, i.e. correct only for local face 2. mesh_trial_trace_ref holds the P1
1286 // mesh shape functions at (face, quadrature point) and for a triangle those barycentric
1287 // functions ARE the reference coordinates (phi_0=1-xi-eta, phi_1=xi, phi_2=eta), so this
1288 // is exactly consistent with calculateMapping_elementBoundary's own convention.
1289 double xB_ref_faces[nDOF_mesh_trial_element * nQuadraturePoints_elementBoundary * 3];
1290 for (int f = 0; f < nDOF_mesh_trial_element; f++)
1291 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
1292 {
1293 const int fkb = f * nQuadraturePoints_elementBoundary + kb;
1294 const double *phi_m = &mesh_trial_trace_ref.data()[fkb * nDOF_mesh_trial_element];
1295 xB_ref_faces[fkb * 3 + 0] = phi_m[1];
1296 xB_ref_faces[fkb * 3 + 1] = phi_m[2];
1297 xB_ref_faces[fkb * 3 + 2] = 0.0;
1298 }
1299 for (int eN = 0; eN < nElements_global; eN++)
1300 {
1301 // declare local storage for element residual and initialize
1302 // double elementResidual_u[nDOF_test_element],element_u[nDOF_trial_element];
1303 auto elementResidual_u = xt::pyarray<double>::from_shape({nDOF_test_element});
1304 auto element_u = xt::pyarray<double>::from_shape({nDOF_trial_element});
1305 bool element_active = false;
1306 elementIsActive[eN] = false;
1307 for (int i = 0; i < nDOF_trial_element; i++)
1308 {
1309 int eN_i = eN * nDOF_trial_element + i;
1310 element_u.data()[i] = u_dof.data()[u_l2g.data()[eN_i]];
1311 } // i
1312 double element_phi_s[nDOF_trial_element];
1313 for (int j = 0; j < nDOF_trial_element; j++)
1314 {
1315 int eN_j = eN * nDOF_trial_element + j;
1316 element_phi_s[j] = embeddedBoundary_sdf_nodes.data()[u_l2g.data()[eN_j]];
1317 }
1318 double element_phi_f[nDOF_trial_element];
1319 for (int j = 0; j < nDOF_trial_element; j++)
1320 {
1321 int eN_j = eN * nDOF_trial_element + j;
1322 element_phi_f[j] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[eN_j]];
1323 }
1324 double element_nodes[nDOF_trial_element * 3];
1325 for (int i = 0; i < nDOF_trial_element; i++)
1326 {
1327 int eN_i = eN * nDOF_trial_element + i;
1328 for (int I = 0; I < 3; I++)
1329 element_nodes[i * 3 + I] = mesh_dof.data()[u_l2g.data()[eN_i] * 3 + I];
1330 } // i
1332 {
1333 gf_s_interior_icase[eN] = gf_s_cache[eN].calculate(element_phi_s, element_nodes, x_ref.data(), false);
1335 }
1336 int icase_s = gf_s_interior_icase[eN];
1337 if (icase_s == 0)
1338 {
1339 // only works for simplices
1340 for (int ebN_element = 0; ebN_element < nDOF_mesh_trial_element; ebN_element++)
1341 {
1342 const int ebN = elementBoundariesArray.data()[eN * nDOF_mesh_trial_element + ebN_element];
1343 // Every interior face of a Gamma_s-cut element, owned or not. The residual is assembled
1344 // with full overlap (each rank sums all of its local elements into the rows it owns
1345 // and drops the rest), so a face term has to be summed on every rank that has both
1346 // of its elements; skipping the faces another rank owns lost their contribution to
1347 // this rank's owned rows (2 ranks: 7 wrong rows, solution off by up to 3e-3).
1348 if (elementBoundaryElementsArray.data()[ebN * 2 + 1] != -1)
1349 cutfem_boundaries.insert(ebN);
1350 }
1351 }
1353 {
1354 gf_f_interior_icase[eN] = gf_f_cache[eN].calculate(element_phi_f, element_nodes, x_ref.data(), mua, mub, jf, false, false);
1356 }
1357 int icase_f = gf_f_interior_icase[eN];
1358 // Records that the buried-Gamma_f logic below demoted this element, and to which
1359 // phase: -1 = fluid is all Omega^- (mua), +1 = all Omega^+ (mub), 0 = not demoted.
1360 int demoted_f = 0;
1361 // Both level sets genuinely cut this element: the product weight ImH_f*H_s is not
1362 // moment-exact here, so fit one polynomial per intersected region instead. Any
1363 // element cut by at most one level set is left entirely alone.
1364 comp_valid[eN] = 0;
1365 comp_DF_valid[eN] = 0;
1366 if (icase_s == 0 && icase_f == 0 && nSpace == 2)
1367 {
1368 const double phi_s3[3] = {element_phi_s[0], element_phi_s[1], element_phi_s[2]};
1369 const double phi_f3[3] = {element_phi_f[0], element_phi_f[1], element_phi_f[2]};
1370 // The fluid interface can cross this element and still lie entirely inside the
1371 // solid, leaving the fluid undivided. There is then no fluid-fluid interface to
1372 // resolve *in the fluid*, so the two-sided IFEM reconstruction has nothing to
1373 // do -- and building it anyway means enforcing interface conditions along a
1374 // segment where no fluid exists. Demote the element to ordinary two-phase by
1375 // reporting the fluid's own side, which routes every downstream branch (bulk
1376 // terms, the solid's Nitsche condition, the error norm) to the standard basis.
1377 double areaA = 0.0, areaB = 0.0;
1378 equivalent_polynomials::composite::fluid_region_areas(phi_s3, phi_f3, areaA, areaB);
1379 // NOTE: we deliberately do NOT change icase_f here. Switching this element to the
1380 // standard basis would make it disagree with its neighbours about what their shared
1381 // DOFs mean: the neighbour (genuinely cut in the fluid) needs the pointwise nodal
1382 // value of u, while this element would need the surviving branch continued to all of
1383 // its nodes -- including nodes on the other side of Gamma_f. One DOF cannot be both,
1384 // so the exact solution would drop out of the discrete space entirely. Keeping the
1385 // IFEM basis costs nothing (u_ex satisfies the interface conditions globally, so
1386 // imposing them along a buried stretch of Gamma_f is harmless) and keeps the space
1387 // conforming. What we DO fix is the weights: the empty region must get exactly zero
1388 // rather than whatever a fit over an empty region would produce.
1389 //
1390 // Tolerance, not exact equality: a genuine tangency between Gamma_f and Gamma_s
1391 // (their zero-sets crossing exactly on a shared mesh edge) makes one region's true
1392 // area a single point, but floating-point roundoff in the clip almost never lands
1393 // on bit-exact 0.0 -- it lands a few ULPs away (observed: ~1e-33 against a
1394 // reference-triangle scale of 0.5). An exact-equality check misses that case
1395 // entirely and falls through to fitting a degree-4 polynomial to a numerically
1396 // degenerate sliver, which is unstable (observed: off by 64x on its own area).
1397 // areaTol is far above that roundoff floor and far below any physically meaningful
1398 // sub-area, so it can't misclassify a genuinely small-but-real fluid region.
1399 const double areaTol = 1.0e-9;
1400 if (areaB <= areaTol && areaA > areaTol)
1401 demoted_f = -1; // fluid is all Omega^- (mua side)
1402 else if (areaA <= areaTol && areaB > areaTol)
1403 demoted_f = 1; // fluid is all Omega^+ (mub side)
1404 else
1405 {
1406 const double nodes2[6] = {element_nodes[0], element_nodes[1],
1407 element_nodes[3], element_nodes[4],
1408 element_nodes[6], element_nodes[7]};
1410 phi_s3, phi_f3,
1411 &comp_A_cache[(std::size_t)eN * nCompMon],
1412 &comp_B_cache[(std::size_t)eN * nCompMon]) &&
1414 phi_s3, phi_f3, nodes2,
1415 &comp_DA_cache[(std::size_t)eN * nCompMon],
1416 &comp_DB_cache[(std::size_t)eN * nCompMon])) ? 1 : 0;
1417 // Gamma_f's own Dirac measure, split by Gamma_s's sign (see comp_DFm_cache's
1418 // declaration): comp_DFa is the part of Gamma_f's chord still inside the
1419 // active fluid, the only part any Gamma_f Dirac-delta term (e.g. the
1420 // prescribed flux jump) should ever see on this element.
1422 phi_f3, phi_s3, nodes2,
1423 &comp_DFm_cache[(std::size_t)eN * nCompMon],
1424 &comp_DFa_cache[(std::size_t)eN * nCompMon]) ? 1 : 0;
1425 }
1426 }
1427 double JA[nDOF_trial_element];
1428 double JB[nDOF_trial_element];
1429 std::fill(JA, JA + nDOF_trial_element, 0.0);
1430 std::fill(JB, JB + nDOF_trial_element, 0.0);
1431 // Prescribed flux jump of this cell for the flux-jump lift (0 if the lift is off).
1432 double fluxLiftJ = (icase_f == 0) ? immersedBoundary_fluxJump_element.data()[eN] : 0.0;
1433 if (icase_f == 0)
1434 {
1435 // only works for simplices
1436 for (int ebN_element = 0; ebN_element < nDOF_mesh_trial_element; ebN_element++)
1437 {
1438 // internal and actually a cut edge
1439 const int ebN = elementBoundariesArray.data()[eN * nDOF_mesh_trial_element + ebN_element];
1440
1441 // indexing convention for P1: edge opposite to node i is given by (i+1)%3 and (i+2)%3 nodes.
1442 // nathawani: P2 needs different indexing convention.
1443 // Do we need corner cases? (<=0) or just (<0) for cut edge detection?
1444 // The SCIFEM facet term is an inter-element consistency term: it needs the
1445 // trace from BOTH elements sharing the face. On an exterior face the second
1446 // entry of elementBoundaryElementsArray is -1, and the loops below index
1447 // elementIsActive[] and gf_f_cache[] with it before any validity test, which
1448 // is out of bounds. test=8's circle lies wholly inside the domain so this
1449 // never triggered; test=12's diagonal interface exits through the boundary
1450 // and segfaulted on arm64. Only interior faces belong in this set.
1451 if (elementBoundaryElementsArray.data()[ebN * 2 + 1] != -1 &&
1452 element_phi_f[(ebN_element + 1) % 3] * element_phi_f[(ebN_element + 2) % 3] < 0.0)
1453 {
1454 // This should give just the cut edges for simplices.
1455 ifem_boundaries.insert(ebN);
1456 }
1457 }
1458 // Leveque & Li 1994, Example 1, 2, 3, 4; Ji et. al. 2014
1459 // The jump function is, in general, not constant over the cut element, so it
1460 // must be evaluated at each node's own coordinates (not once at the cut
1461 // barycenter) -- otherwise JA/JB are constant over the element and their
1462 // interpolated gradients (grad_uja, grad_ujb) vanish identically.
1463 const double eps_phi = 1.0e-12;
1464 auto assign_jump_side = [&](int i, bool isOuter, double jump) {
1465 if (isOuter)
1466 {
1467 JA[i] = -jump;
1468 JB[i] = 0.0;
1469 }
1470 else
1471 {
1472 JA[i] = 0.0;
1473 JB[i] = jump;
1474 }
1475 };
1476 for (int i = 0; i < nDOF_trial_element; i++)
1477 {
1478
1479 int eN_i = eN * nDOF_trial_element + i;
1480 const double jump_i = immersedBoundary_solutionJump_nodes.data()[u_l2g.data()[eN_i]];
1481 if (element_phi_f[i] > 0.0)
1482 {
1483 assign_jump_side(i, true, jump_i);
1484 }
1485 else if (element_phi_f[i] <= 0.0)
1486 {
1487 assign_jump_side(i, false, jump_i);
1488 }
1489 }
1490 }
1491 double L2_elem = 0.0, H1_elem = 0.0;
1492 // j_h carries the flux jump [mu dj_h/dn] = sum_i (mub JB_i grad phi_i^b - mua JA_i grad phi_i^a).n,
1493 // which vanishes when J_s is the same at every node of the cell (partition of unity) but
1494 // not when J_s varies. The flux-jump lift supplies only the rest, J_f - [mu dj_h/dn], so
1495 // that the trial function has the prescribed flux jump either way. n is the normal of the
1496 // cell's Gamma_f chord from phi_f < 0 to phi_f > 0, as for J_f in ADR.py. Cells with a
1497 // constant J_s skip this, so they are computed exactly as before. Only cells that ADR.py
1498 // gives a lift (both signs at the vertices, none exactly zero) are corrected: on a cell
1499 // that Gamma_f only touches or crosses through a vertex there is no lift to correct.
1500 bool liftCell = false;
1501 {
1502 int nneg = 0, npos = 0, nzero = 0;
1503 for (int i = 0; i < 3; i++)
1504 {
1505 if (element_phi_f[i] < 0.0) nneg++;
1506 else if (element_phi_f[i] > 0.0) npos++;
1507 else nzero++;
1508 }
1509 liftCell = (nneg > 0 && npos > 0 && nzero == 0);
1510 }
1511 if (icase_f == 0 && fluxJumpLift && liftCell && nSpace == 2)
1512 {
1513 bool jumpVaries = false;
1514 const double jump0 = immersedBoundary_solutionJump_nodes.data()[u_l2g.data()[eN * nDOF_trial_element]];
1515 for (int i = 1; i < nDOF_trial_element; i++)
1516 if (immersedBoundary_solutionJump_nodes.data()[u_l2g.data()[eN * nDOF_trial_element + i]] != jump0)
1517 jumpVaries = true;
1518 if (jumpVaries)
1519 {
1520 const double ex0 = element_nodes[3] - element_nodes[0], ey0 = element_nodes[4] - element_nodes[1];
1521 const double ex1 = element_nodes[6] - element_nodes[0], ey1 = element_nodes[7] - element_nodes[1];
1522 const double dp0 = element_phi_f[1] - element_phi_f[0], dp1 = element_phi_f[2] - element_phi_f[0];
1523 const double det = ex0 * ey1 - ex1 * ey0;
1524 double nf[2] = {(dp0 * ey1 - dp1 * ey0) / det, (ex0 * dp1 - ex1 * dp0) / det};
1525 const double nnorm = std::sqrt(nf[0] * nf[0] + nf[1] * nf[1]);
1526 nf[0] /= nnorm;
1527 nf[1] /= nnorm;
1528 GfType &gfj = gf_f_cache[eN];
1529 auto jhFluxJump = [&](int k) {
1530 gfj.set_quad(k);
1531 double s = 0.0;
1532 for (int i = 0; i < nDOF_trial_element; i++)
1533 s += mub * JB[i] * (gfj.VB_x(i) * nf[0] + gfj.VB_y(i) * nf[1]) -
1534 mua * JA[i] * (gfj.VA_x(i) * nf[0] + gfj.VA_y(i) * nf[1]);
1535 return s;
1536 };
1537 double Fj = 0.0;
1538 if (nDOF_trial_element == 3)
1539 Fj = jhFluxJump(0); // P1: the branch gradients are constant on the cell
1540 else
1541 {
1542 // P2: average along Gamma_f, weighted by the Dirac fit
1543 double wsum = 0.0, fsum = 0.0, usum = 0.0;
1544 for (int k = 0; k < nQuadraturePoints_element; k++)
1545 {
1546 const double Fk = jhFluxJump(k);
1547 const double wk = gfj.D(0., 0.) * dV_ref.data()[k];
1548 wsum += wk;
1549 fsum += wk * Fk;
1550 usum += Fk;
1551 }
1552 Fj = (std::fabs(wsum) > 1.0e-14) ? fsum / wsum : usum / nQuadraturePoints_element;
1553 }
1554 fluxLiftJ -= Fj;
1555 }
1556 }
1557 if (icase_f == 0)
1558 fluxLiftAmp[eN] = fluxLiftJ;
1560 demoted_f,
1561 mesh_trial_ref,
1562 mesh_grad_trial_ref,
1563 mesh_dof,
1564 mesh_l2g,
1565 x_ref,
1566 dV_ref,
1567 u_trial_ref,
1568 u_grad_trial_ref,
1569 u_test_ref,
1570 u_grad_test_ref,
1571 elementDiameter,
1572 elementBoundaryDiameter,
1573 nodeDiametersArray,
1574 cfl,
1575 Ct_sge,
1576 sc_uref,
1577 sc_alpha,
1578 useMetrics,
1579 mesh_trial_trace_ref,
1580 mesh_grad_trial_trace_ref,
1581 dS_ref,
1582 u_trial_trace_ref,
1583 u_grad_trial_trace_ref,
1584 u_test_trace_ref,
1585 u_grad_test_trace_ref,
1586 normal_ref,
1587 boundaryJac_ref,
1588 nElements_global,
1589 nElementBoundaries_owned,
1590 u_l2g,
1591 u_dof,
1592 sd_rowptr,
1593 sd_colind,
1594 q_a,
1595 comp_valid[eN] ? &comp_A_cache[(std::size_t)eN * nCompMon] : nullptr,
1596 comp_valid[eN] ? &comp_B_cache[(std::size_t)eN * nCompMon] : nullptr,
1597 comp_valid[eN] ? &comp_DA_cache[(std::size_t)eN * nCompMon] : nullptr,
1598 comp_valid[eN] ? &comp_DB_cache[(std::size_t)eN * nCompMon] : nullptr,
1599 comp_DF_valid[eN] ? &comp_DFa_cache[(std::size_t)eN * nCompMon] : nullptr,
1600 q_v,
1601 q_r,
1602 lag_shockCapturing,
1603 shockCapturingDiffusion,
1604 q_numDiff_u,
1605 q_numDiff_u_last,
1606 offset_u, stride_u,
1607 elementResidual_u,
1608 nExteriorElementBoundaries_global,
1609 exteriorElementBoundariesArray,
1610 elementBoundariesArray,
1611 elementBoundaryElementsArray,
1612 elementBoundaryLocalElementBoundariesArray,
1613 element_u,
1614 eN,
1615 embeddedBoundary,
1616 embeddedBoundary_penalty,
1617 embeddedBoundary_normal_q,
1618 embeddedBoundary_u_q,
1619 embeddedBoundary_u_inner_q,
1620 embeddedBoundary_u_outer_q,
1621 embeddedBoundary_robin,
1622 embeddedBoundary_robin_h_inner,
1623 embeddedBoundary_robin_h_outer,
1624 embeddedBoundary_robin_uInfinity_inner_q,
1625 embeddedBoundary_robin_uInfinity_outer_q,
1626 embeddedBoundary_sdf_q,
1627 immersedBoundary,
1628 immersedBoundary_penalty,
1629 immersedBoundary_sdf_q,
1630 immersedBoundary_normal_q,
1631 immersedBoundary_u_q,
1632 immersedBoundary_fluxJump_q,
1633 immersedBoundary_fluxJumpVector_q,
1634 immersedBoundary_solutionJump_nodes,
1635 element_phi_f,
1636 element_active,
1638 JA,
1639 JB,
1640 fluxLiftJ,
1641 L2_elem,
1642 Linfty_error.data()[0],
1643 H1_elem,
1644 mua,
1645 mub,
1646 q_u_exact_inner,
1647 q_u_exact_outer,
1648 q_grad_u_exact_inner,
1649 q_grad_u_exact_outer,
1650 PG);
1651 if (eN < nElements_owned)
1652 {
1653 L2_error.data()[0] += L2_elem;
1654 H1_error.data()[0] += H1_elem;
1655 }
1656 //
1657 // load element into global residual and save element residual
1658 //
1659 for (int i = 0; i < nDOF_test_element; i++)
1660 {
1661 int eN_i = eN * nDOF_test_element + i;
1662 globalResidual.data()[offset_u + stride_u * u_l2g.data()[eN_i]] += elementResidual_u.data()[i];
1663 if (element_active)
1664 isActiveDOF.data()[offset_u + stride_u * u_l2g.data()[eN_i]] = 1.0;
1665 } // i
1666 // Masked root-adjacent edges leak boundary flux H_s doesn't remove (ordinary
1667 // hat functions vanish there; two-sided branches don't). Add it back for all 3
1668 // local dofs.
1669 // icase_s == -1 means this element has NO fluid anywhere (H_s == 0 identically) --
1670 // Gamma_f may still cross it geometrically, but with zero real fluid area there is
1671 // no bulk contribution to correct and no physically-meaningful va/vb reconstruction
1672 // to add back. Without this check TC_EDGE still fires there (icase_f==0 alone
1673 // doesn't rule it out), injecting a real, nonzero correction from a "dead" element
1674 // into whichever physical dofs it shares with genuinely active neighbors.
1675 if (embeddedBoundary && immersedBoundary && (nDOF_trial_element == 3 || nDOF_trial_element == 6) && icase_f == 0
1676 && icase_s != -1)
1677 {
1678 // P1 and P2. Integrated on the two Gamma_f-cut (root-adjacent) faces with the element-boundary
1679 // quadrature and the boundary evaluation of the two-branch basis, as in the SCIFEM loop. Each
1680 // branch is restricted to its piece of the face (its side of Gamma_f, and the fluid side of
1681 // Gamma_s -- or the solid side for the default TC_EDGE) by an edge moment-fit indicator, exact
1682 // for integrands of degree <= nP_edge (flux*v: degree 1 for P1, 3 for P2).
1683 const int nD = nDOF_trial_element;
1684 bool neg[3] = {element_phi_f[0] < 0.0, element_phi_f[1] < 0.0, element_phi_f[2] < 0.0};
1685 int cnt_neg = (neg[0] ? 1 : 0) + (neg[1] ? 1 : 0) + (neg[2] ? 1 : 0);
1686 int root_i = -1;
1687 for (int ii = 0; ii < 3; ii++)
1688 if ((neg[ii] && cnt_neg == 1) || (!neg[ii] && cnt_neg == 2)) { root_i = ii; break; }
1689 if (root_i >= 0)
1690 {
1692 {
1693 gf_f_boundary_icase[eN] = gf_f_cache[eN].calculate(element_phi_f, element_nodes, xB_ref_faces, mua, mub, jf, true, false);
1695 }
1696 GfType &gf_f_tc = gf_f_cache[eN];
1697 // Branch coefficients u_i + JA_i / u_i + JB_i: the solution-jump field (JA/JB) is
1698 // reconstructed through the same branch basis as everywhere else in this file, so the true
1699 // branch value is sum (u_i+JA_i) va_i (cf. line ~784, ~931).
1700 double cuA[nDOF_trial_element], cuB[nDOF_trial_element];
1701 for (int ii = 0; ii < nD; ii++)
1702 {
1703 const double u_ii = u_dof.data()[u_l2g.data()[eN * nD + ii]];
1704 cuA[ii] = u_ii + JA[ii];
1705 cuB[ii] = u_ii + JB[ii];
1706 }
1707 // Default (TC_EDGE): the solid pieces {phi_s<0}. triplyCut_edgeFluidSide: the fluid pieces {phi_s>0}, opposite sign.
1708 const double sgn_s = tcFluidMode ? 1.0 : -1.0;
1709 for (int pj = 0; pj < 3; pj++)
1710 {
1711 if (pj == root_i)
1712 continue; // the face opposite the root is not cut by Gamma_f
1713 const int ebN_tc = elementBoundariesArray.data()[eN * nDOF_mesh_trial_element + pj];
1714 if (tcBoundaryOnly && elementBoundaryElementsArray.data()[ebN_tc * 2 + 1] != -1)
1715 continue; // interior face: left to SCIFEM
1716 // face pj runs from local node (pj+1)%3 (t=0) to (pj+2)%3 (t=1), as in the SCIFEM loop
1717 const int n0 = (pj + 1) % 3, n1 = (pj + 2) % 3;
1718 double C_a[nP_edge + 1], C_b[nP_edge + 1];
1719 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(-element_phi_f[n0], -element_phi_f[n1],
1720 sgn_s * element_phi_s[n0], sgn_s * element_phi_s[n1], C_a);
1721 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(element_phi_f[n0], element_phi_f[n1],
1722 sgn_s * element_phi_s[n0], sgn_s * element_phi_s[n1], C_b);
1723 const double ex0 = element_nodes[n0 * 3 + 0], ey0 = element_nodes[n0 * 3 + 1];
1724 const double evx = element_nodes[n1 * 3 + 0] - ex0, evy = element_nodes[n1 * 3 + 1] - ey0;
1725 const double elen2 = evx * evx + evy * evy;
1726 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
1727 {
1728 const int ebN_local_kb = pj * nQuadraturePoints_elementBoundary + kb;
1729 double jac_b[nSpace * nSpace], jacDet_b, jacInv_b[nSpace * nSpace],
1730 bJac_b[nSpace * (nSpace - 1)], mT_b[(nSpace - 1) * (nSpace - 1)], mTDS_b, n_b[2], xb, yb, zb;
1731 ck.calculateMapping_elementBoundary(eN, pj, kb, ebN_local_kb,
1732 mesh_dof.data(), mesh_l2g.data(),
1733 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
1734 boundaryJac_ref.data(), jac_b, jacDet_b, jacInv_b,
1735 bJac_b, mT_b, mTDS_b, normal_ref.data(), n_b, xb, yb, zb);
1736 const double dS = mTDS_b * dS_ref.data()[kb];
1737 const double t = ((xb - ex0) * evx + (yb - ey0) * evy) / elen2;
1740 gf_f_tc.set_boundary_quad(ebN_local_kb);
1741 double fluxA = 0.0, fluxB = 0.0;
1742 for (int kk = 0; kk < nD; kk++)
1743 {
1744 fluxA += cuA[kk] * (gf_f_tc.VA_x(kk) * n_b[0] + gf_f_tc.VA_y(kk) * n_b[1]);
1745 fluxB += cuB[kk] * (gf_f_tc.VB_x(kk) * n_b[0] + gf_f_tc.VB_y(kk) * n_b[1]);
1746 }
1747 fluxA += fluxLiftJ * (gf_f_tc.PSIA_x() * n_b[0] + gf_f_tc.PSIA_y() * n_b[1]);
1748 fluxB += fluxLiftJ * (gf_f_tc.PSIB_x() * n_b[0] + gf_f_tc.PSIB_y() * n_b[1]);
1749 fluxA *= mua;
1750 fluxB *= mub;
1751 for (int jj = 0; jj < nD; jj++)
1752 {
1753 const int gdof = u_l2g.data()[eN * nD + jj];
1754 const double vA = gf_f_tc.VA(jj), vB = gf_f_tc.VB(jj);
1755 globalResidual.data()[offset_u + stride_u * gdof] += tcSgn * (Wa * fluxA * vA + Wb * fluxB * vB) * dS;
1756 }
1757 }
1758 }
1759 }
1760 }
1761 } // elements
1762 for (std::set<int>::iterator it = cutfem_boundaries.begin(); it != cutfem_boundaries.end();)
1763 {
1764 if (elementIsActive[elementBoundaryElementsArray[(*it) * 2 + 0]] && elementIsActive[elementBoundaryElementsArray[(*it) * 2 + 1]])
1765 {
1766 std::map<int, double> Dwp_Dn_jump, Dw_Dn_jump;
1767 std::map<int, double> Dw_Dn_jump_a, Dw_Dn_jump_b;
1768 double gamma_cutfem = embeddedBoundary_ghost_penalty, h_cutfem = elementBoundaryDiameter.data()[*it];
1769 // The two elements need not parametrize the shared face in the same direction, so
1770 // index kb can be DIFFERENT physical points on the two sides (see the SCIFEM loop's
1771 // orientation map). Harmless for P1, where the normal derivative is constant on a
1772 // face; for P2 it turns the variation along the face into a spurious jump. Match
1773 // side 1 to side 0 by position.
1774 int gp_kmap[nQuadraturePoints_elementBoundary];
1775 {
1776 double gp_xq[2][nQuadraturePoints_elementBoundary][3] = {}; // 2D mapping never writes z
1777 for (int s = 0; s < 2; s++)
1778 {
1779 const int eN_g = elementBoundaryElementsArray.data()[(*it) * 2 + s],
1780 ebN_local_g = elementBoundaryLocalElementBoundariesArray.data()[(*it) * 2 + s];
1781 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
1782 {
1783 double jac_g[nSpace * nSpace], jacDet_g, jacInv_g[nSpace * nSpace],
1784 bJac_g[nSpace * (nSpace - 1)], mT_g[(nSpace - 1) * (nSpace - 1)], mTDS_g, nrm_g[2];
1785 ck.calculateMapping_elementBoundary(eN_g, ebN_local_g, kb,
1786 ebN_local_g * nQuadraturePoints_elementBoundary + kb,
1787 mesh_dof.data(), mesh_l2g.data(),
1788 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
1789 boundaryJac_ref.data(), jac_g, jacDet_g, jacInv_g,
1790 bJac_g, mT_g, mTDS_g, normal_ref.data(), nrm_g,
1791 gp_xq[s][kb][0], gp_xq[s][kb][1], gp_xq[s][kb][2]);
1792 }
1793 }
1794 for (int k = 0; k < nQuadraturePoints_elementBoundary; k++)
1795 {
1796 int best = 0;
1797 double bestd = 1.0e300;
1798 for (int j = 0; j < nQuadraturePoints_elementBoundary; j++)
1799 {
1800 double d = 0.0;
1801 for (int I = 0; I < nSpace; I++)
1802 d += std::fabs(gp_xq[1][j][I] - gp_xq[0][k][I]);
1803 if (d < bestd) { bestd = d; best = j; }
1804 }
1805 gp_kmap[k] = best;
1806 }
1807 }
1808 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
1809 {
1810 double Du_Dn_jump = 0.0, dS;
1811 double Du_Dn_jump_a = 0.0, Du_Dn_jump_b = 0.0;
1812 bool phaseA_ok = true, phaseB_ok = true;
1813 for (int eN_side = 0; eN_side < 2; eN_side++)
1814 {
1815 int ebN = *it,
1816 eN = elementBoundaryElementsArray.data()[ebN * 2 + eN_side];
1817 for (int i = 0; i < nDOF_test_element; i++)
1818 {
1819 Dw_Dn_jump[u_l2g.data()[eN * nDOF_test_element + i]] = 0.0;
1820 Dw_Dn_jump_a[u_l2g.data()[eN * nDOF_test_element + i]] = 0.0;
1821 Dw_Dn_jump_b[u_l2g.data()[eN * nDOF_test_element + i]] = 0.0;
1822 }
1823 }
1824 for (int eN_side = 0; eN_side < 2; eN_side++)
1825 {
1826 int ebN = *it,
1827 eN = elementBoundaryElementsArray.data()[ebN * 2 + eN_side],
1828 ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + eN_side],
1829 eN_nDOF_trial_element = eN * nDOF_trial_element,
1830 kb_s = (eN_side == 0) ? kb : gp_kmap[kb],
1831 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb_s,
1832 ebN_local_kb_nSpace = ebN_local_kb * nSpace;
1833 double u_int = 0.0,
1834 grad_u_int[nSpace] = {0., 0.},
1835 jac_int[nSpace * nSpace],
1836 jacDet_int,
1837 jacInv_int[nSpace * nSpace],
1838 boundaryJac[nSpace * (nSpace - 1)],
1839 metricTensor[(nSpace - 1) * (nSpace - 1)],
1840 metricTensorDetSqrt,
1841 u_test_dS[nDOF_test_element],
1842 u_grad_trial_trace[nDOF_trial_element * nSpace],
1843 u_grad_test_dS[nDOF_trial_element * nSpace],
1844 normal[2], x_int, y_int, z_int, xt_int, yt_int, zt_int, integralScaling,
1845 G[nSpace * nSpace], G_dd_G, tr_G, h_phi, h_penalty, penalty,
1846 force_x, force_y, force_z, force_p_x, force_p_y, force_p_z, force_v_x, force_v_y, force_v_z, r_x, r_y, r_z;
1847 // compute information about mapping from reference element to physical element
1848 ck.calculateMapping_elementBoundary(eN,
1849 ebN_local,
1850 kb_s,
1851 ebN_local_kb,
1852 mesh_dof.data(),
1853 mesh_l2g.data(),
1854 mesh_trial_trace_ref.data(),
1855 mesh_grad_trial_trace_ref.data(),
1856 boundaryJac_ref.data(),
1857 jac_int,
1858 jacDet_int,
1859 jacInv_int,
1860 boundaryJac,
1861 metricTensor,
1862 metricTensorDetSqrt,
1863 normal_ref.data(),
1864 normal,
1865 x_int, y_int, z_int);
1866 dS = metricTensorDetSqrt * dS_ref.data()[kb_s]; // kb_s mirrors kb: same Gauss weight
1867 // compute shape and solution information
1868 // shape
1869 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_int, u_grad_trial_trace);
1870 // solution and gradients
1871 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element], u_int);
1872 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial_trace, grad_u_int);
1873 for (int I = 0; I < nSpace; I++)
1874 {
1875 Du_Dn_jump += grad_u_int[I] * normal[I];
1876 }
1877 for (int i = 0; i < nDOF_test_element; i++)
1878 {
1879 for (int I = 0; I < nSpace; I++)
1880 Dw_Dn_jump[u_l2g.data()[eN_nDOF_trial_element + i]] += u_grad_trial_trace[i * nSpace + I] * normal[I];
1881 }
1882 // --- Ghost penalty, per fluid phase. The penalty is meant to kill *spurious*
1883 // normal-derivative jumps across a cut element's face. On an element that the
1884 // fluid interface also cuts, the combined IFEM solution genuinely kinks at
1885 // Gamma_f, so penalising the combined gradient penalises physics and destroys
1886 // exactness. Each phase's own polynomial extension IS smooth across the face,
1887 // so we penalise [[d(u_a)/dn]] and [[d(u_b)/dn]] separately -- the same per-side
1888 // treatment the bulk (wA/wB) and Nitsche (D_s_a/D_s_b) terms already get.
1889 int gp_icase = -2; // -2: no fluid interface in play, single phase
1890 double gp_va_grad[nDOF_trial_element * nSpace], gp_vb_grad[nDOF_trial_element * nSpace];
1891 // Jump-lift arrays (assign_jump_side's own node pattern, ~line 1499-1524) for
1892 // THIS element eN, needed below to add grad_uja/grad_ujb to the ghost penalty's
1893 // per-phase normal derivative. Zero unless eN is genuinely Gamma_f-cut.
1894 double gp_JA[nDOF_trial_element], gp_JB[nDOF_trial_element];
1895 std::fill(gp_JA, gp_JA + nDOF_trial_element, 0.0);
1896 std::fill(gp_JB, gp_JB + nDOF_trial_element, 0.0);
1897 if (immersedBoundary)
1898 {
1899 double gp_phi_f[nDOF_trial_element], gp_nodes[nDOF_trial_element * 3];
1900 for (int i = 0; i < nDOF_trial_element; i++)
1901 {
1902 const int gp_eN_i = eN * nDOF_trial_element + i;
1903 gp_phi_f[i] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[gp_eN_i]];
1904 for (int I = 0; I < 3; I++)
1905 gp_nodes[i * 3 + I] = mesh_dof.data()[u_l2g.data()[gp_eN_i] * 3 + I];
1906 }
1908 {
1909 gf_f_boundary_icase[eN] = gf_f_cache[eN].calculate(gp_phi_f, gp_nodes, xB_ref_faces, mua, mub, jf, true, false);
1911 }
1912 gp_icase = gf_f_boundary_icase[eN];
1913 if (gp_icase == 0)
1914 {
1915 for (int i = 0; i < nDOF_trial_element; i++)
1916 {
1917 const double jump_i = immersedBoundary_solutionJump_nodes.data()[u_l2g.data()[eN * nDOF_trial_element + i]];
1918 if (gp_phi_f[i] > 0.0)
1919 gp_JA[i] = -jump_i;
1920 else
1921 gp_JB[i] = jump_i;
1922 }
1923 }
1924 }
1925 if (gp_icase == 0)
1926 {
1927 gf_f_cache[eN].set_boundary_quad(ebN_local_kb);
1928 for (int i = 0; i < nDOF_trial_element; i++)
1929 {
1930 gp_va_grad[i * nSpace + 0] = gf_f_cache[eN].VA_x(i);
1931 gp_va_grad[i * nSpace + 1] = gf_f_cache[eN].VA_y(i);
1932 gp_vb_grad[i * nSpace + 0] = gf_f_cache[eN].VB_x(i);
1933 gp_vb_grad[i * nSpace + 1] = gf_f_cache[eN].VB_y(i);
1934 }
1935 }
1936 else
1937 for (int i = 0; i < nDOF_trial_element * nSpace; i++)
1938 {
1939 gp_va_grad[i] = u_grad_trial_trace[i];
1940 gp_vb_grad[i] = u_grad_trial_trace[i];
1941 }
1942 // Which phases does this element actually carry? A face only gets a phase's penalty
1943 // if BOTH its elements have that phase, otherwise there is no extension to compare.
1944 phaseA_ok = phaseA_ok && (gp_icase == 0 || gp_icase == -1 || gp_icase == -2);
1945 phaseB_ok = phaseB_ok && (gp_icase == 0 || gp_icase == 1);
1946 for (int i = 0; i < nDOF_test_element; i++)
1947 {
1948 const int gp_dof = u_l2g.data()[eN * nDOF_trial_element + i];
1949 for (int I = 0; I < nSpace; I++)
1950 {
1951 Dw_Dn_jump_a[gp_dof] += gp_va_grad[i * nSpace + I] * normal[I];
1952 Dw_Dn_jump_b[gp_dof] += gp_vb_grad[i * nSpace + I] * normal[I];
1953 }
1954 }
1955 {
1956 double gp_eu[nDOF_trial_element], gp_gua[nSpace] = {0., 0.}, gp_gub[nSpace] = {0., 0.};
1957 for (int i = 0; i < nDOF_trial_element; i++)
1958 gp_eu[i] = u_dof.data()[u_l2g.data()[eN_nDOF_trial_element + i]];
1959 ck.gradFromElementDOF(gp_eu, gp_va_grad, gp_gua);
1960 ck.gradFromElementDOF(gp_eu, gp_vb_grad, gp_gub);
1961 // Add the jump-lift gradient (grad_uja/grad_ujb): the ghost penalty must
1962 // compare the TOTAL branch field (u_a+u_ja / u_b+u_jb, see ga/gb ~line
1963 // 865-866) across the shared face, not the jump-free piece alone, or a
1964 // genuine prescribed solution jump reads as a spurious normal-derivative
1965 // kink between elements. Zero (no-op) unless eN is Gamma_f-cut.
1966 double gp_gja[nSpace] = {0., 0.}, gp_gjb[nSpace] = {0., 0.};
1967 ck.gradFromElementDOF(gp_JA, gp_va_grad, gp_gja);
1968 ck.gradFromElementDOF(gp_JB, gp_vb_grad, gp_gjb);
1969 if (gp_icase == 0) // flux-jump lift; set_boundary_quad(ebN_local_kb) is current
1970 {
1971 const double J = fluxLiftAmp[eN];
1972 gp_gja[0] += J * gf_f_cache[eN].PSIA_x();
1973 gp_gja[1] += J * gf_f_cache[eN].PSIA_y();
1974 gp_gjb[0] += J * gf_f_cache[eN].PSIB_x();
1975 gp_gjb[1] += J * gf_f_cache[eN].PSIB_y();
1976 }
1977 for (int I = 0; I < nSpace; I++)
1978 {
1979 Du_Dn_jump_a += (gp_gua[I] + gp_gja[I]) * normal[I];
1980 Du_Dn_jump_b += (gp_gub[I] + gp_gjb[I]) * normal[I];
1981 }
1982 }
1983 } // eN_side
1984 if (phaseA_ok || phaseB_ok)
1985 {
1986 if (phaseA_ok)
1987 for (std::map<int, double>::iterator w_it = Dw_Dn_jump_a.begin(); w_it != Dw_Dn_jump_a.end(); ++w_it)
1988 globalResidual.data()[offset_u + stride_u * w_it->first] += gamma_cutfem * h_cutfem * Du_Dn_jump_a * w_it->second * dS;
1989 if (phaseB_ok)
1990 for (std::map<int, double>::iterator w_it = Dw_Dn_jump_b.begin(); w_it != Dw_Dn_jump_b.end(); ++w_it)
1991 globalResidual.data()[offset_u + stride_u * w_it->first] += gamma_cutfem * h_cutfem * Du_Dn_jump_b * w_it->second * dS;
1992 }
1993 else
1994 {
1995 // Neither phase is shared by both elements (Gamma_f runs along the face itself).
1996 // Keep the original combined penalty so the face is still stabilised.
1997 for (std::map<int, double>::iterator w_it = Dw_Dn_jump.begin(); w_it != Dw_Dn_jump.end(); ++w_it)
1998 globalResidual.data()[offset_u + stride_u * w_it->first] += gamma_cutfem * h_cutfem * Du_Dn_jump * w_it->second * dS;
1999 } // i
2000 } // kb
2001 ++it;
2002 }
2003 else
2004 {
2005 it = cutfem_boundaries.erase(it);
2006 }
2007 } // cutfem element boundaries
2008 // SCIFEM (Ji et al. 2014, eq. 3.7/3.8) symmetric consistency + adjoint-consistency terms
2009 // on cut edges: -switch*({{beta grad u}}.n_e*[[v]] + {{beta grad v}}.n_e*[[u]]).
2010 // A genuine 1D facet term -- the edge carries its own arc-length measure -- so no Dirac
2011 // surrogate is involved. The two region-wise integrands are formed separately and blended
2012 // by the edge Heaviside fit (see the prologue below), which is what makes this exact for
2013 // P1 and P2 without splitting the quadrature.
2014 for (std::set<int>::iterator it = ifem_boundaries.begin(); it != ifem_boundaries.end();)
2015 {
2016 if (elementIsActive[elementBoundaryElementsArray[(*it) * 2 + 0]] && elementIsActive[elementBoundaryElementsArray[(*it) * 2 + 1]])
2017 {
2018 int ebN = *it;
2019 int eN_s[2], ebN_local_s[2];
2020 for (int s = 0; s < 2; s++)
2021 {
2022 eN_s[s] = elementBoundaryElementsArray.data()[ebN * 2 + s];
2023 ebN_local_s[s] = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + s];
2024 }
2025
2026 // --- Edge-restricted phi (both level sets) --------------------------------
2027 double edge_nodes[2][3], phi_edge[2], phi_s_edge[2];
2028 {
2029 const int n0l = (ebN_local_s[0] + 1) % 3, n1l = (ebN_local_s[0] + 2) % 3;
2030 const int ln[2] = {n0l, n1l};
2031 for (int e = 0; e < 2; e++)
2032 {
2033 int eN_i = eN_s[0] * nDOF_trial_element + ln[e];
2034 phi_edge[e] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[eN_i]];
2035 phi_s_edge[e] = embeddedBoundary ? embeddedBoundary_sdf_nodes.data()[u_l2g.data()[eN_i]] : 1.0;
2036 for (int I = 0; I < 3; I++)
2037 edge_nodes[e][I] = mesh_dof.data()[u_l2g.data()[eN_i] * 3 + I];
2038 }
2039 }
2040 // edge diameter for the interior-penalty scaling (same measure the cutfem ghost
2041 // penalty uses); note gamma/h here for a VALUE jump, vs gamma*h there for a
2042 // gradient jump.
2043 const double h_edge = elementBoundaryDiameter.data()[ebN];
2044 double edge_vec[3];
2045 double edge_len2 = 0.0;
2046 for (int I = 0; I < 3; I++)
2047 {
2048 edge_vec[I] = edge_nodes[1][I] - edge_nodes[0][I];
2049 edge_len2 += edge_vec[I] * edge_vec[I];
2050 }
2051
2052 // --- Orientation map ------------------------------------------------------
2053 // The two elements sharing an interior face do NOT necessarily parametrize it in
2054 // the same direction, so quadrature index kb can denote DIFFERENT physical points
2055 // on the two sides (measured: 4 of 14 cut edges at test=8.0/refinement=1, with
2056 // exactly the mirror offsets |1-2t_k|*L of the Gauss rule). Pairing the sides by
2057 // raw kb then differences u at two different points, so [[u]] and {{beta grad u}}
2058 // pick up u's variation ALONG the edge instead of the genuine inter-element jump.
2059 // Build an explicit map instead: kmap[s][k] is side s's index for the physical
2060 // point that side 0 calls k. Matched by position, so no assumption about the
2061 // rule being symmetric or ascending.
2062 double xq[2][nQuadraturePoints_elementBoundary][3];
2063 for (int s = 0; s < 2; s++)
2064 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
2065 {
2066 double jac_t[nSpace * nSpace], jacDet_t, jacInv_t[nSpace * nSpace],
2067 bJac_t[nSpace * (nSpace - 1)], mT_t[(nSpace - 1) * (nSpace - 1)],
2068 mTDS_t, nrm_t[2], xt_ = 0.0, yt_ = 0.0, zt_ = 0.0;
2069 ck.calculateMapping_elementBoundary(eN_s[s], ebN_local_s[s], kb,
2070 ebN_local_s[s] * nQuadraturePoints_elementBoundary + kb,
2071 mesh_dof.data(), mesh_l2g.data(),
2072 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
2073 boundaryJac_ref.data(), jac_t, jacDet_t, jacInv_t,
2074 bJac_t, mT_t, mTDS_t, normal_ref.data(), nrm_t,
2075 xt_, yt_, zt_);
2076 xq[s][kb][0] = xt_; xq[s][kb][1] = yt_; xq[s][kb][2] = zt_;
2077 }
2078 // edge diameter: the length scale for the pairing tolerance just below,
2079 // and for the interior-penalty scaling further down (same measure the
2080 // cutfem ghost penalty uses); note gamma/h here for a VALUE jump, vs
2081 // gamma*h there for a gradient jump.
2082 // Match over nSpace components only: a component the mapping does not
2083 // define contributes nothing but noise to the distance, and the paired
2084 // points agree EXACTLY in the components it does define, so the winning
2085 // distance is 0 and the runner-up is a full quadrature spacing away.
2086 // Verify that outcome rather than trust it -- a mis-pick here silently
2087 // differences u at two different points, and assert() is compiled out by
2088 // the -DNDEBUG that reaches these translation units.
2089 int kmap[2][nQuadraturePoints_elementBoundary];
2090 bool kmap_used[nQuadraturePoints_elementBoundary] = {};
2091 for (int k = 0; k < nQuadraturePoints_elementBoundary; k++)
2092 {
2093 kmap[0][k] = k;
2094 int best = -1;
2095 double bestd = 1.0e300, nextd = 1.0e300;
2096 for (int j = 0; j < nQuadraturePoints_elementBoundary; j++)
2097 {
2098 double d = 0.0;
2099 for (int I = 0; I < nSpace; I++) // the 2D mapping never writes z
2100 d += std::fabs(xq[1][j][I] - xq[0][k][I]);
2101 if (d < bestd) { nextd = bestd; bestd = d; best = j; }
2102 else if (d < nextd) { nextd = d; }
2103 }
2104 if (best < 0 || kmap_used[best] || bestd >= 1.0e-8 * h_edge || nextd <= 2.0 * bestd)
2105 {
2106 std::cerr << "ADR immersedSCIFEM: quadrature points on face " << ebN
2107 << " between elements " << eN_s[0] << " and " << eN_s[1]
2108 << " do not pair up (k=" << k << ", best=" << best
2109 << ", d=" << bestd << ", runner-up=" << nextd
2110 << ", h=" << h_edge << "); the mesh is non-conforming or the"
2111 << " boundary mapping is inconsistent." << std::endl;
2112 throw std::runtime_error("ADR immersedSCIFEM: face quadrature pairing failed");
2113 }
2114 kmap_used[best] = true;
2115 kmap[1][k] = best;
2116 }
2117
2118 // --- Edge Heaviside moment fit -------------------------------------------
2119 // The facet integrand is DISCONTINUOUS at the interface crossing theta, so a fixed
2120 // whole-edge Gauss rule cannot integrate it directly. Weight the two one-sided
2121 // integrands by an edge-restricted moment-fit Heaviside instead: exact for any
2122 // polynomial integrand of degree <= nP_edge, hence exact for P1 (degree <= 1) and
2123 // P2 (degree <= 3) alike -- no linearity assumption, unlike a split-quadrature
2124 // scheme. ONE fit per edge, in side 0's parametrization: the two sides' fits are
2125 // mirror images, moment-equivalent but not pointwise equal, so they must not be
2126 // mixed. t below is therefore always measured along side 0's edge direction.
2127 // Branch a = {phi_f<0}, branch b = {phi_f>0}, each intersected with the fluid side
2128 // {phi_s>0} (phi_s_edge is 1 without an embedded solid, so this is then exactly the
2129 // single Gamma_f fit and its complement).
2130 double C_a_edge[nP_edge + 1], C_b_edge[nP_edge + 1];
2131 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(-phi_edge[0], -phi_edge[1], phi_s_edge[0], phi_s_edge[1], C_a_edge);
2132 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(phi_edge[0], phi_edge[1], phi_s_edge[0], phi_s_edge[1], C_b_edge);
2133
2134 for (int k = 0; k < nQuadraturePoints_elementBoundary; k++)
2135 {
2136 double dS = 0.0;
2137 double ua_s[2], ub_s[2], flux_a_s[2], flux_b_s[2];
2138 std::map<int, double> va_m[2], vb_m[2], fta_m[2], ftb_m[2];
2139 for (int eN_side = 0; eN_side < 2; eN_side++)
2140 {
2141 int eN = eN_s[eN_side],
2142 ebN_local = ebN_local_s[eN_side],
2143 kb = kmap[eN_side][k],
2144 eN_nDOF_trial_element = eN * nDOF_trial_element,
2145 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb;
2146 double ua = 0.0, ub = 0.0,
2147 grad_ua[nSpace] = {0., 0.}, grad_ub[nSpace] = {0., 0.},
2148 jac_int[nSpace * nSpace], jacDet_int, jacInv_int[nSpace * nSpace],
2149 boundaryJac[nSpace * (nSpace - 1)],
2150 metricTensor[(nSpace - 1) * (nSpace - 1)], metricTensorDetSqrt,
2151 normal[2], x_int, y_int, z_int;
2152 ck.calculateMapping_elementBoundary(eN, ebN_local, kb, ebN_local_kb,
2153 mesh_dof.data(), mesh_l2g.data(),
2154 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
2155 boundaryJac_ref.data(), jac_int, jacDet_int, jacInv_int,
2156 boundaryJac, metricTensor, metricTensorDetSqrt,
2157 normal_ref.data(), normal, x_int, y_int, z_int);
2158 // dS and the reference normal n_e are fixed from side 0; side 1's own
2159 // outward normal is -n_e, which sign_ne converts back.
2160 if (eN_side == 0)
2161 dS = metricTensorDetSqrt * dS_ref.data()[kb];
2162 double sign_ne = (eN_side == 0) ? 1.0 : -1.0;
2163
2164 auto element_u = xt::pyarray<double>::from_shape({nDOF_trial_element});
2165 double element_phi_f[nDOF_trial_element], element_nodes[nDOF_trial_element * 3];
2166 for (int i = 0; i < nDOF_trial_element; i++)
2167 {
2168 int eN_i = eN * nDOF_trial_element + i;
2169 element_u.data()[i] = u_dof.data()[u_l2g.data()[eN_i]];
2170 element_phi_f[i] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[eN_i]];
2171 for (int I = 0; I < 3; I++)
2172 element_nodes[i * 3 + I] = mesh_dof.data()[u_l2g.data()[eN_i] * 3 + I];
2173 }
2175 {
2176 gf_f_boundary_icase[eN] = gf_f_cache[eN].calculate(element_phi_f, element_nodes, xB_ref_faces, mua, mub, jf, true, false);
2178 }
2179 GfType &gf_f = gf_f_cache[eN];
2180 gf_f.set_boundary_quad(ebN_local_kb);
2181
2182 double va[nDOF_trial_element], va_grad_trial[nDOF_trial_element * nSpace],
2183 vb[nDOF_trial_element], vb_grad_trial[nDOF_trial_element * nSpace];
2184 for (int i = 0; i < nDOF_trial_element; i++)
2185 {
2186 va[i] = gf_f.VA(i);
2187 va_grad_trial[i * nSpace + 0] = gf_f.VA_x(i);
2188 va_grad_trial[i * nSpace + 1] = gf_f.VA_y(i);
2189 vb[i] = gf_f.VB(i);
2190 vb_grad_trial[i * nSpace + 0] = gf_f.VB_x(i);
2191 vb_grad_trial[i * nSpace + 1] = gf_f.VB_y(i);
2192 }
2193 // Solution-jump lift (same node pattern as assign_jump_side / the ghost penalty's
2194 // gp_JA/gp_JB): branch a is u + JA, branch b is u + JB. Without it the edge terms
2195 // see u^h instead of u^h + j_h and are inconsistent when [u] != 0.
2196 double ca_s[nDOF_trial_element], cb_s[nDOF_trial_element];
2197 for (int i = 0; i < nDOF_trial_element; i++)
2198 {
2199 ca_s[i] = element_u.data()[i];
2200 cb_s[i] = element_u.data()[i];
2201 }
2202 if (gf_f_boundary_icase[eN] == 0)
2203 {
2204 for (int i = 0; i < nDOF_trial_element; i++)
2205 {
2206 const double jump_i = immersedBoundary_solutionJump_nodes.data()[u_l2g.data()[eN * nDOF_trial_element + i]];
2207 if (element_phi_f[i] > 0.0)
2208 ca_s[i] -= jump_i;
2209 else
2210 cb_s[i] += jump_i;
2211 }
2212 }
2213 ck.valFromElementDOF(ca_s, va, ua);
2214 ck.gradFromElementDOF(ca_s, va_grad_trial, grad_ua);
2215 ck.valFromElementDOF(cb_s, vb, ub);
2216 ck.gradFromElementDOF(cb_s, vb_grad_trial, grad_ub);
2217 if (gf_f_boundary_icase[eN] == 0) // flux-jump lift, as for the solution jump
2218 {
2219 const double J = fluxLiftAmp[eN];
2220 ua += J * gf_f.PSIA();
2221 ub += J * gf_f.PSIB();
2222 grad_ua[0] += J * gf_f.PSIA_x();
2223 grad_ua[1] += J * gf_f.PSIA_y();
2224 grad_ub[0] += J * gf_f.PSIB_x();
2225 grad_ub[1] += J * gf_f.PSIB_y();
2226 }
2227
2228 ua_s[eN_side] = ua;
2229 ub_s[eN_side] = ub;
2230 double fa = 0.0, fb = 0.0;
2231 for (int I = 0; I < nSpace; I++)
2232 {
2233 fa += mua * grad_ua[I] * normal[I];
2234 fb += mub * grad_ub[I] * normal[I];
2235 }
2236 flux_a_s[eN_side] = sign_ne * fa;
2237 flux_b_s[eN_side] = sign_ne * fb;
2238
2239 for (int i = 0; i < nDOF_test_element; i++)
2240 {
2241 int dof_i = u_l2g.data()[eN_nDOF_trial_element + i];
2242 va_m[eN_side][dof_i] = va[i];
2243 vb_m[eN_side][dof_i] = vb[i];
2244 double fta = 0.0, ftb = 0.0;
2245 for (int I = 0; I < nSpace; I++)
2246 {
2247 fta += mua * va_grad_trial[i * nSpace + I] * normal[I];
2248 ftb += mub * vb_grad_trial[i * nSpace + I] * normal[I];
2249 }
2250 fta_m[eN_side][dof_i] = sign_ne * fta;
2251 ftb_m[eN_side][dof_i] = sign_ne * ftb;
2252 }
2253 } // eN_side
2254
2255 // t of this physical point along side 0's edge direction, then H_hat(t)
2256 double proj = 0.0;
2257 for (int I = 0; I < 3; I++)
2258 proj += (xq[0][k][I] - edge_nodes[0][I]) * edge_vec[I];
2259 double t_edge = (edge_len2 > 1.0e-24) ? (proj / edge_len2) : 0.0;
2260 // H_e / ImH_e: weights of branch b / branch a on its fluid piece of the edge
2261 double H_e = equivalent_polynomials::evaluate_edge_poly<nP_edge>(C_b_edge, t_edge);
2262 double ImH_e = equivalent_polynomials::evaluate_edge_poly<nP_edge>(C_a_edge, t_edge);
2263
2264 double avg_flux_a = 0.5 * (flux_a_s[0] + flux_a_s[1]);
2265 double avg_flux_b = 0.5 * (flux_b_s[0] + flux_b_s[1]);
2266 double jump_ua = ua_s[0] - ua_s[1];
2267 double jump_ub = ub_s[0] - ub_s[1];
2268
2269 // Blend the region-wise PRODUCTS, never the individual factors:
2270 // (ImH*A_f + H*B_f)*(ImH*A_v + H*B_v) != ImH*A_f*A_v + H*B_f*B_v.
2271 for (int side = 0; side < 2; side++)
2272 {
2273 double v_sign = (side == 0) ? 1.0 : -1.0;
2274 for (std::map<int, double>::iterator w = va_m[side].begin(); w != va_m[side].end(); ++w)
2275 {
2276 int dof_i = w->first;
2277 double jva_i = v_sign * va_m[side][dof_i], jvb_i = v_sign * vb_m[side][dof_i];
2278 double Pa = avg_flux_a * jva_i + 0.5 * fta_m[side][dof_i] * jump_ua;
2279 double Pb = avg_flux_b * jvb_i + 0.5 * ftb_m[side][dof_i] * jump_ub;
2280 globalResidual.data()[offset_u + stride_u * dof_i] -=
2281 immersedSCIFEM_switch * (ImH_e * Pa + H_e * Pb) * dS;
2282 // Interior-penalty stabilization gamma/h * int_e [[u]][[v]] ds. eq (3.7) is
2283 // consistency + adjoint only, so it carries no coercivity guarantee; this is
2284 // the standard remedy. Added with a PLUS sign (like the bulk stiffness) so it
2285 // is positive semi-definite, and blended by the same edge Heaviside so it
2286 // stays exact for P1 (integrand degree <= 2) and P2 (degree <= 4 = nP_edge).
2287 // Weighted by mu of each branch (like the Nitsche term), so the penalty is
2288 // of the size of the bulk term on both sides of a high-contrast interface.
2289 globalResidual.data()[offset_u + stride_u * dof_i] +=
2290 (immersedSCIFEM_penalty / h_edge) *
2291 (ImH_e * mua * jump_ua * jva_i + H_e * mub * jump_ub * jvb_i) * dS;
2292 }
2293 }
2294 } // k
2295 ++it;
2296 }
2297 else
2298 {
2299 it = ifem_boundaries.erase(it);
2300 }
2301 } // ifem element boundaries
2302 //
2303 // loop over exterior element boundaries to calculate surface integrals and load into element and global residuals
2304 //
2305 // ebNE is the Exterior element boundary INdex
2306 // ebN is the element boundary INdex
2307 // eN is the element index
2308 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++)
2309 {
2310 int ebN = exteriorElementBoundariesArray.data()[ebNE],
2311 eN = elementBoundaryElementsArray.data()[ebN * 2 + 0],
2312 ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + 0],
2313 eN_nDOF_trial_element = eN * nDOF_trial_element;
2314 double elementResidual_u[nDOF_test_element];
2315 for (int i = 0; i < nDOF_test_element; i++)
2316 {
2317 elementResidual_u[i] = 0.0;
2318 }
2319 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
2320 {
2321 int ebNE_kb = ebNE * nQuadraturePoints_elementBoundary + kb,
2322 ebNE_kb_nSpace = ebNE_kb * nSpace,
2323 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb,
2324 ebN_local_kb_nSpace = ebN_local_kb * nSpace;
2325 double u_ext = 0.0,
2326 grad_u_ext[nSpace],
2327 m_ext = 0.0,
2328 dm_ext = 0.0,
2329 *a_ext,
2330 /* *da_exxt, */
2331 f_ext[nSpace],
2332 df_ext[nSpace],
2333 r_ext = 0.0,
2334 /* dr_ext=0.0, */
2335 flux_diff_ext = 0.0,
2336 flux_advect_ext = 0.0,
2337 bc_u_ext = 0.0,
2338 // bc_grad_u_ext[nSpace],
2339 bc_m_ext = 0.0,
2340 bc_dm_ext = 0.0,
2341 bc_f_ext[nSpace],
2342 bc_df_ext[nSpace],
2343 jac_ext[nSpace * nSpace],
2344 jacDet_ext,
2345 jacInv_ext[nSpace * nSpace],
2346 boundaryJac[nSpace * (nSpace - 1)],
2347 metricTensor[(nSpace - 1) * (nSpace - 1)],
2348 metricTensorDetSqrt,
2349 dS,
2350 u_test_dS[nDOF_test_element],
2351 u_grad_trial_trace[nDOF_trial_element * nSpace],
2352 u_grad_test_dS[nDOF_trial_element * nSpace],
2353 normal[nSpace], x_ext, y_ext, z_ext, xt_ext, yt_ext, zt_ext, integralScaling,
2354 //
2355 G[nSpace * nSpace], G_dd_G, tr_G;
2356 //
2357 // calculate the solution and gradients at quadrature points
2358 //
2359 // compute information about mapping from reference element to physical element
2360 ck.calculateMapping_elementBoundary(eN,
2361 ebN_local,
2362 kb,
2363 ebN_local_kb,
2364 mesh_dof.data(),
2365 mesh_l2g.data(),
2366 mesh_trial_trace_ref.data(),
2367 mesh_grad_trial_trace_ref.data(),
2368 boundaryJac_ref.data(),
2369 jac_ext,
2370 jacDet_ext,
2371 jacInv_ext,
2372 boundaryJac,
2373 metricTensor,
2374 metricTensorDetSqrt,
2375 normal_ref.data(),
2376 normal,
2377 x_ext, y_ext, z_ext);
2378 /* ck.calculateMappingVelocity_elementBoundary(eN, */
2379 /* ebN_local, */
2380 /* kb, */
2381 /* ebN_local_kb, */
2382 /* mesh_velocity_dof, */
2383 /* mesh_l2g, */
2384 /* mesh_trial_trace_ref, */
2385 /* xt_ext,yt_ext,zt_ext, */
2386 /* normal, */
2387 /* boundaryJac, */
2388 /* metricTensor, */
2389 /* integralScaling); */
2390 /*dS = ((1.0-MOVING_DOMAIN)*metricTensorDetSqrt + MOVING_DOMAIN*integralScaling)*dS_ref.data()[kb];*/
2391 dS = metricTensorDetSqrt * dS_ref.data()[kb];
2392 // get the metric tensor
2393 // cek todo use symmetry
2394 ck.calculateG(jacInv_ext, G, G_dd_G, tr_G);
2395 // compute shape and solution information
2396 // shape
2397 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_ext, u_grad_trial_trace);
2398 // solution and gradients
2399 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element], u_ext);
2400 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial_trace, grad_u_ext);
2401 // precalculate test function products with integration weights
2402 for (int j = 0; j < nDOF_trial_element; j++)
2403 {
2404 u_test_dS[j] = u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + j] * dS;
2405 for (int I = 0; I < nSpace; I++)
2406 u_grad_test_dS[j * nSpace + I] = u_grad_trial_trace[j * nSpace + I] * dS; // cek hack, using trial
2407 }
2408 //
2409 // load the boundary values
2410 //
2411 bc_u_ext = isDOFBoundary_u.data()[ebNE_kb] * ebqe_bc_u_ext.data()[ebNE_kb] + (1 - isDOFBoundary_u.data()[ebNE_kb]) * u_ext;
2412 //
2413 //
2414 // calculate the pde coefficients using the solution and the boundary values for the solution
2415 //
2416 a_ext = &ebqe_a.data()[ebNE_kb * sd_rowptr[nSpace]];
2417 for (int I = 0; I < nSpace; I++)
2418 {
2419 f_ext[I] = ebqe_v.data()[ebNE_kb * nSpace + I] * u_ext;
2420 df_ext[I] = ebqe_v.data()[ebNE_kb * nSpace + I];
2421 bc_f_ext[I] = ebqe_v.data()[ebNE_kb * nSpace + I] * bc_u_ext;
2422 bc_df_ext[I] = ebqe_v.data()[ebNE_kb * nSpace + I];
2423 }
2424 /* evaluateCoefficients(&ebqe_velocity_ext.data()[ebNE_kb_nSpace], */
2425 /* u_ext, */
2426 /* //VRANS */
2427 /* porosity_ext, */
2428 /* // */
2429 /* m_ext, */
2430 /* dm_ext, */
2431 /* f_ext, */
2432 /* df_ext); */
2433 /* evaluateCoefficients(&ebqe_velocity_ext.data()[ebNE_kb_nSpace], */
2434 /* bc_u_ext, */
2435 /* //VRANS */
2436 /* porosity_ext, */
2437 /* // */
2438 /* bc_m_ext, */
2439 /* bc_dm_ext, */
2440 /* bc_f_ext, */
2441 /* bc_df_ext); */
2442 //
2443 // moving mesh
2444 //
2445 /* double velocity_ext[nSpace]; */
2446 /* double mesh_velocity[3]; */
2447 /* mesh_velocity[0] = xt_ext; */
2448 /* mesh_velocity[1] = yt_ext; */
2449 /* mesh_velocity[2] = zt_ext; */
2450 /* for (int I=0;I<nSpace;I++) */
2451 /* velocity_ext[I] = ebqe_velocity_ext.data()[ebNE_kb_nSpace+0] - MOVING_DOMAIN*mesh_velocity[I]; */
2452 //
2453 // calculate the numerical fluxes
2454 //
2455 exteriorNumericalDiffusiveFlux(sd_rowptr.data(),
2456 sd_colind.data(),
2457 isDOFBoundary_u.data()[ebNE_kb],
2458 isDiffusiveFluxBoundary_u.data()[ebNE_kb],
2459 normal,
2460 a_ext,
2461 bc_u_ext,
2462 ebqe_bc_flux_u_ext.data()[ebNE_kb],
2463 a_ext,
2464 grad_u_ext,
2465 u_ext,
2466 ebqe_penalty_ext.data()[ebNE_kb],
2467 flux_diff_ext);
2468 exteriorNumericalAdvectiveFlux(isDOFBoundary_u.data()[ebNE_kb],
2469 isAdvectiveFluxBoundary_u.data()[ebNE_kb],
2470 normal,
2471 bc_u_ext,
2472 ebqe_bc_flux_u_ext.data()[ebNE_kb],
2473 u_ext,
2474 df_ext,
2475 flux_advect_ext);
2476 // ebqe_flux.data()[ebNE_kb] = flux_ext;
2477 //
2478 // update residuals
2479 //
2480 for (int i = 0; i < nDOF_test_element; i++)
2481 {
2482 // int ebNE_kb_i = ebNE_kb*nDOF_test_element+i;
2483 elementResidual_u[i] += ck.ExteriorElementBoundaryFlux(flux_diff_ext + flux_advect_ext, u_test_dS[i]) +
2484 ck.ExteriorElementBoundaryDiffusionAdjoint(isDOFBoundary_u.data()[ebNE_kb],
2485 isDiffusiveFluxBoundary_u.data()[ebNE_kb],
2486 eb_adjoint_sigma,
2487 u_ext,
2488 bc_u_ext,
2489 normal,
2490 sd_rowptr.data(),
2491 sd_colind.data(),
2492 a_ext,
2493 &u_grad_test_dS[i * nSpace]);
2494 } // i
2495 } // kb
2496 //
2497 // update the element and global residual storage
2498 //
2499 for (int i = 0; i < nDOF_test_element; i++)
2500 {
2501 int eN_i = eN * nDOF_test_element + i;
2502 globalResidual.data()[offset_u + stride_u * u_l2g.data()[eN_i]] += elementResidual_u[i];
2503 } // i
2504 } // ebNE
2505 }
2506
2507 inline void calculateElementJacobian(int icase_f,
2508 const int demoted_f,
2509 // element
2510 xt::pyarray<double> &mesh_trial_ref,
2511 xt::pyarray<double> &mesh_grad_trial_ref,
2512 xt::pyarray<double> &mesh_dof,
2513 xt::pyarray<int> &mesh_l2g,
2514 xt::pyarray<double> &x_ref,
2515 xt::pyarray<double> &dV_ref,
2516 xt::pyarray<double> &u_trial_ref,
2517 xt::pyarray<double> &u_grad_trial_ref,
2518 xt::pyarray<double> &u_test_ref,
2519 xt::pyarray<double> &u_grad_test_ref,
2520 xt::pyarray<double> &elementDiameter,
2521 xt::pyarray<double> &elementBoundaryDiameter,
2522 xt::pyarray<double> &nodeDiametersArray,
2523 xt::pyarray<double> &cfl,
2524 double Ct_sge,
2525 double sc_uref,
2526 double sc_alpha,
2527 double useMetrics,
2528 // element boundary
2529 xt::pyarray<double> &mesh_trial_trace_ref,
2530 xt::pyarray<double> &mesh_grad_trial_trace_ref,
2531 xt::pyarray<double> &dS_ref,
2532 xt::pyarray<double> &u_trial_trace_ref,
2533 xt::pyarray<double> &u_grad_trial_trace_ref,
2534 xt::pyarray<double> &u_test_trace_ref,
2535 xt::pyarray<double> &u_grad_test_trace_ref,
2536 xt::pyarray<double> &normal_ref,
2537 xt::pyarray<double> &boundaryJac_ref,
2538 // physics
2539 int nElements_global,
2540 int nElementBoundaries_owned,
2541 xt::pyarray<int> &u_l2g,
2542 xt::pyarray<double> &u_dof,
2543 xt::pyarray<int> &sd_rowptr,
2544 xt::pyarray<int> &sd_colind,
2545 xt::pyarray<double> &q_a,
2546 const double *comp_A,
2547 const double *comp_B,
2548 const double *comp_DA,
2549 const double *comp_DB,
2550 const double *comp_DFa,
2551 xt::pyarray<double> &q_v,
2552 xt::pyarray<double> &q_r,
2553 int lag_shockCapturing,
2554 double shockCapturingDiffusion,
2555 xt::pyarray<double> &q_numDiff_u,
2556 xt::pyarray<double> &q_numDiff_u_last,
2557 xt::pyarray<double> &elementJacobian_u_u,
2558 xt::pyarray<double> &element_u,
2559 int eN,
2560 const bool embeddedBoundary,
2561 const double embeddedBoundary_penalty,
2562 xt::pyarray<double> &embeddedBoundary_normal_q,
2563 xt::pyarray<double> &embeddedBoundary_u_q,
2564 xt::pyarray<double> &embeddedBoundary_u_inner_q,
2565 xt::pyarray<double> &embeddedBoundary_u_outer_q,
2566 const bool embeddedBoundary_robin,
2567 const double embeddedBoundary_robin_h_inner,
2568 const double embeddedBoundary_robin_h_outer,
2569 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_inner_q,
2570 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_outer_q,
2571 const bool immersedBoundary,
2572 const double immersedBoundary_penalty,
2573 xt::pyarray<double> &immersedBoundary_sdf_q,
2574 xt::pyarray<double> &immersedBoundary_normal_q,
2575 xt::pyarray<double> &immersedBoundary_u_q,
2576 xt::pyarray<double> &immersedBoundary_fluxJump_q,
2577 xt::pyarray<double> &immersedBoundary_fluxJumpVector_q,
2578 xt::pyarray<double> &immersedBoundary_solutionJump_nodes,
2579 double *element_phi_f,
2580 double mua,
2581 double mub,
2582 const bool PG)
2583 {
2584 // per-element cached equivalent-polynomial/IFEM reconstruction
2585 // (see ensureIFEMCacheSized / ifemGeometryGeneration)
2586 GfType &gf_f = gf_f_cache[eN];
2587 GfType &gf_s = gf_s_cache[eN];
2588 for (int i = 0; i < nDOF_test_element; i++)
2589 for (int j = 0; j < nDOF_trial_element; j++)
2590 {
2591 elementJacobian_u_u.data()[i * nDOF_trial_element + j] = 0.0;
2592 }
2593 for (int k = 0; k < nQuadraturePoints_element; k++)
2594 {
2595 gf_s.set_quad(k);
2596 gf_f.set_quad(k);
2597 int eN_k = eN * nQuadraturePoints_element + k; // index to a scalar at a quadrature point
2598 int eN_k_3d = eN_k * 3;
2599 // declare local storage
2600 double u = 0.0,
2601 grad_u[nSpace],
2602 ua = 0.0,
2603 grad_ua[nSpace],
2604 ub = 0.0,
2605 grad_ub[nSpace],
2606 m = 0.0, dm = 0.0,
2607 h_phi = 0.0,
2608 r_s = 0.0, dr_s = 0.0,
2609 r_s_a = 0.0, dr_s_a = 0.0, ham_s_a = 0.0, r_s_b = 0.0, dr_s_b = 0.0, ham_s_b = 0.0,
2610 dham_s_a[nSpace] = {0., 0.}, f_s_a[nSpace] = {0., 0.}, df_s_a[nSpace] = {0., 0.},
2611 dham_s_b[nSpace] = {0., 0.}, f_s_b[nSpace] = {0., 0.}, df_s_b[nSpace] = {0., 0.},
2612 f[nSpace], df[nSpace],
2613 f_s[nSpace] = {0., 0.}, df_s[nSpace] = {0., 0.},
2614 ham_s = 0.0, dham_s[nSpace] = {0., 0.},
2615 r_f = 0.0, dr_f = 0.0,
2616 f_f[nSpace] = {0., 0.}, df_f[nSpace] = {0., 0.},
2617 ham_f = 0.0, dham_f[nSpace] = {0., 0.},
2618 m_t = 0.0, dm_t = 0.0,
2619 dpdeResidual_u_u[nDOF_trial_element],
2620 Lstar_u[nDOF_test_element],
2621 dsubgridError_u_u[nDOF_trial_element],
2622 tau = 0.0, tau0 = 0.0, tau1 = 0.0,
2623 *a = NULL,
2624 dr = 0.0,
2625 jac[nSpace * nSpace],
2626 jacDet,
2627 jacInv[nSpace * nSpace],
2628 u_grad_trial[nDOF_trial_element * nSpace],
2629 ua_grad_trial[nDOF_trial_element * nSpace],
2630 ub_grad_trial[nDOF_trial_element * nSpace],
2631 dV,
2632 ua_trial[nDOF_trial_element],
2633 ub_trial[nDOF_trial_element],
2634 u_test_dV[nDOF_test_element],
2635 ua_test_dV[nDOF_test_element],
2636 ub_test_dV[nDOF_test_element],
2637 u_grad_test_dV[nDOF_test_element * nSpace],
2638 ua_grad_test_dV[nDOF_test_element * nSpace],
2639 ub_grad_test_dV[nDOF_test_element * nSpace],
2640 x, y, z,
2641 G[nSpace * nSpace], G_dd_G, tr_G;
2642 //
2643 // calculate solution and gradients at quadrature points
2644 //
2645 ck.calculateMapping_element(eN,
2646 k,
2647 mesh_dof.data(),
2648 mesh_l2g.data(),
2649 mesh_trial_ref.data(),
2650 mesh_grad_trial_ref.data(),
2651 jac,
2652 jacDet,
2653 jacInv,
2654 x, y, z);
2655 ck.calculateH_element(eN,
2656 k,
2657 nodeDiametersArray.data(),
2658 mesh_l2g.data(),
2659 mesh_trial_ref.data(),
2660 h_phi);
2661 // get the physical integration weight
2662 dV = fabs(jacDet) * dV_ref.data()[k];
2663 // get metric tensor and friends
2664 ck.calculateG(jacInv, G, G_dd_G, tr_G);
2665 // get the trial function gradients
2666 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
2667 // get the solution
2668 ck.valFromElementDOF(element_u.data(), &u_trial_ref.data()[k * nDOF_trial_element], u);
2669 // get the solution gradients
2670 ck.gradFromElementDOF(element_u.data(), u_grad_trial, grad_u);
2671 // precalculate test function products with integration weights
2672 for (int j = 0; j < nDOF_trial_element; j++)
2673 {
2674 u_test_dV[j] = u_test_ref.data()[k * nDOF_trial_element + j] * dV;
2675 for (int I = 0; I < nSpace; I++)
2676 {
2677 u_grad_test_dV[j * nSpace + I] = u_grad_trial[j * nSpace + I] * dV; // cek warning won't work for Petrov-Galerkin
2678 //PGIFEM
2679 }
2680 }
2681 if (icase_f == 0)
2682 {
2683 double va[nDOF_trial_element], va_grad_trial[nDOF_trial_element * nSpace],
2684 vb[nDOF_trial_element], vb_grad_trial[nDOF_trial_element * nSpace];
2685 for (int i = 0; i < nDOF_trial_element; i++)
2686 {
2687 va[i] = gf_f.VA(i);
2688 va_grad_trial[i * nSpace + 0] = gf_f.VA_x(i);
2689 va_grad_trial[i * nSpace + 1] = gf_f.VA_y(i);
2690 vb[i] = gf_f.VB(i);
2691 vb_grad_trial[i * nSpace + 0] = gf_f.VB_x(i);
2692 vb_grad_trial[i * nSpace + 1] = gf_f.VB_y(i);
2693 }
2694 ck.valFromElementDOF(element_u.data(), va, ua);
2695 ck.gradFromElementDOF(element_u.data(), va_grad_trial, grad_ua);
2696 ck.valFromElementDOF(element_u.data(), vb, ub);
2697 ck.gradFromElementDOF(element_u.data(), vb_grad_trial, grad_ub);
2698 for (int i = 0; i < nDOF_test_element; i++)
2699 {
2700 ua_test_dV[i] = va[i] * dV;
2701 ub_test_dV[i] = vb[i] * dV;
2702 for (int I = 0; I < nSpace; I++)
2703 {
2704 ua_grad_test_dV[i * nSpace + I] = va_grad_trial[i * nSpace + I] * dV;
2705 ub_grad_test_dV[i * nSpace + I] = vb_grad_trial[i * nSpace + I] * dV;
2706 }
2707 }
2708 for (int j = 0; j < nDOF_trial_element; j++)
2709 {
2710 ua_trial[j] = va[j];
2711 ub_trial[j] = vb[j];
2712 for (int I = 0; I < nSpace; I++)
2713 {
2714 ua_grad_trial[j * nSpace + I] = va_grad_trial[j * nSpace + I];
2715 ub_grad_trial[j * nSpace + I] = vb_grad_trial[j * nSpace + I];
2716 }
2717 }
2718 if (PG)
2719 {
2720 // Same substitution as the residual; trial-side (j-index) arrays
2721 // are untouched, only the test side (i-index) changes.
2722 for (int i = 0; i < nDOF_test_element; i++)
2723 {
2724 ua_test_dV[i] = u_test_dV[i];
2725 ub_test_dV[i] = u_test_dV[i];
2726 for (int I = 0; I < nSpace; I++)
2727 {
2728 ua_grad_test_dV[i * nSpace + I] = u_grad_test_dV[i * nSpace + I];
2729 ub_grad_test_dV[i * nSpace + I] = u_grad_test_dV[i * nSpace + I];
2730 }
2731 }
2732 }
2733 }
2734 //
2735 // calculate pde coefficients and derivatives at quadrature points
2736 //
2737 // evaluateCoefficients()
2738 a = &q_a.data()[eN_k * sd_rowptr.data()[nSpace]];
2739 // On a demoted element the fluid is entirely one phase, but the pre-evaluated
2740 // pointwise data still switches branch across the buried Gamma_f -- which lies inside
2741 // the solid part of this element. H_s is only moment-exact against polynomials, and a
2742 // coefficient that jumps mid-element is not one, so those quadrature points inject a
2743 // contribution from a fluid that is not physically present. Substitute the surviving
2744 // phase's constants, which makes the integrand polynomial again and H_s exact.
2745 double a_dem[nSpace * nSpace];
2746 double u_s_dem = embeddedBoundary_u_q.data()[eN_k];
2747 if (demoted_f != 0)
2748 {
2749 for (int I = 0; I < nSpace * nSpace; I++) a_dem[I] = 0.0;
2750 const double mu_dem = (demoted_f < 0) ? mua : mub;
2751 for (int I = 0; I < nSpace; I++) a_dem[I * nSpace + I] = mu_dem;
2752 a = a_dem;
2753 u_s_dem = (demoted_f < 0) ? embeddedBoundary_u_inner_q.data()[eN_k]
2754 : embeddedBoundary_u_outer_q.data()[eN_k];
2755 }
2756 for (int I = 0; I < nSpace; I++)
2757 df[I] = q_v.data()[eN_k * nSpace + I];
2758 dr = 0.0;
2759 const double H_s = gf_s.H(0., 0.);
2760 const double D_s = gf_s.D(0., 0.);
2761 // Per-side Dirac for the solid surface. Weighting the single-cut D_s by a region fit
2762 // (ImH_f * D_s) is the product-of-two-fits error again, and a large one -- about 31% on
2763 // the segment length. Declared here because the Nitsche terms below consume it.
2764 // Defaults to D_s, so elements cut by only one level set are untouched.
2765 double D_s_a = D_s, D_s_b = D_s;
2766 if (demoted_f != 0)
2767 {
2768 // The whole solid surface in this element borders the one surviving fluid region.
2769 D_s_a = (demoted_f < 0) ? D_s : 0.0;
2770 D_s_b = (demoted_f < 0) ? 0.0 : D_s;
2771 }
2772 else if (comp_DA != nullptr)
2773 {
2774 const double xr_d = x_ref.data()[k * 3 + 0], yr_d = x_ref.data()[k * 3 + 1];
2775 D_s_a = equivalent_polynomials::composite::evaluate<nP_fit>(comp_DA, xr_d, yr_d);
2776 D_s_b = equivalent_polynomials::composite::evaluate<nP_fit>(comp_DB, xr_d, yr_d);
2777 }
2778 if (embeddedBoundary)
2779 {
2780 double level_set_normal[nSpace];
2781 double sign = 0.0;
2782 double norm_exact = 0.0, norm_cut = 0.0;
2783 for (int I = 0; I < nSpace; I++)
2784 {
2785 sign += embeddedBoundary_normal_q.data()[eN_k_3d + I] * gf_s.get_normal()[I];
2786 level_set_normal[I] = gf_s.get_normal()[I];
2787 norm_cut += level_set_normal[I] * level_set_normal[I];
2788 norm_exact += embeddedBoundary_normal_q.data()[eN_k_3d + I] * embeddedBoundary_normal_q.data()[eN_k_3d + I];
2789 }
2790 assert(std::fabs(1.0 - norm_cut) < 1.0e-8);
2791 assert(std::fabs(1.0 - norm_exact) < 1.0e-8);
2792 if (sign < 0.0)
2793 for (int I = 0; I < nSpace; I++)
2794 level_set_normal[I] *= -1.0;
2795 if (icase_f == 0)
2796 {
2797 // The solution here is ua on the mua side and ub on the mub side; the plain `u` is
2798 // neither. Handing `u` to the penalty makes (u - u_s) nonzero even when the discrete
2799 // solution is exactly right, and the O(100/h) penalty amplifies that.
2800 // The prescribed jump is a constant offset with zero derivative, so it is
2801 // residual-only and does not appear here.
2802
2803 if (embeddedBoundary_robin)
2804 {
2805 updateEmbeddedBoundaryRobinTerms(embeddedBoundary_robin_h_inner,
2806 embeddedBoundary_robin_uInfinity_inner_q.data()[eN_k], ua, r_s_a, dr_s_a, D_s_a);
2807 updateEmbeddedBoundaryRobinTerms(embeddedBoundary_robin_h_outer,
2808 embeddedBoundary_robin_uInfinity_outer_q.data()[eN_k], ub, r_s_b, dr_s_b, D_s_b);
2809 }
2810 else
2811 {
2812 updateEmbeddedBoundaryTerms(embeddedBoundary_penalty / h_phi, dV, level_set_normal,
2813 embeddedBoundary_u_inner_q.data()[eN_k], ua, grad_ua, mua,
2814 r_s_a, dr_s_a, ham_s_a, dham_s_a, f_s_a, df_s_a, D_s_a);
2815 updateEmbeddedBoundaryTerms(embeddedBoundary_penalty / h_phi, dV, level_set_normal,
2816 embeddedBoundary_u_outer_q.data()[eN_k], ub, grad_ub, mub,
2817 r_s_b, dr_s_b, ham_s_b, dham_s_b, f_s_b, df_s_b, D_s_b);
2818 }
2819 }
2820 else
2821 {
2822 if (embeddedBoundary_robin)
2823 {
2824 const double h_here = (icase_f < 0) ? embeddedBoundary_robin_h_inner
2825 : embeddedBoundary_robin_h_outer;
2826 const double u_inf_here = (icase_f < 0)
2827 ? embeddedBoundary_robin_uInfinity_inner_q.data()[eN_k]
2828 : embeddedBoundary_robin_uInfinity_outer_q.data()[eN_k];
2829 updateEmbeddedBoundaryRobinTerms(h_here, u_inf_here, u, r_s, dr_s, D_s);
2830 }
2831 else
2832 {
2833 updateEmbeddedBoundaryTerms(embeddedBoundary_penalty / h_phi, dV, level_set_normal,
2834 u_s_dem, u, grad_u, a[0],
2835 r_s, dr_s, ham_s, dham_s, f_s, df_s, D_s);
2836 }
2837 }
2838 }
2839 const double ImH_f = gf_f.ImH(0., 0.);
2840 const double H_f = gf_f.H(0., 0.);
2841 // Region weights for the bulk terms. On an element cut by both level sets these
2842 // come from the composite fit; otherwise they are the usual products, unchanged.
2843 double wA = ImH_f * H_s, wB = H_f * H_s;
2844 if (demoted_f != 0)
2845 {
2846 // Only one fluid region exists here; give it the whole fluid weight and the other none.
2847 wA = (demoted_f < 0) ? H_s : 0.0;
2848 wB = (demoted_f < 0) ? 0.0 : H_s;
2849 }
2850 else if (comp_A != nullptr)
2851 {
2852 const double xr = x_ref.data()[k * 3 + 0], yr = x_ref.data()[k * 3 + 1];
2855 }
2856 double D_f = gf_f.D(0., 0.);
2857 if (comp_DFa != nullptr)
2858 {
2859 // Gamma_f's own Dirac measure, restricted to the part still inside the active
2860 // fluid domain (see comp_DFa_cache's declaration) -- the plain, single-curve
2861 // gf_f.D(0.,0.) above double-counts whatever part of Gamma_f's chord falls
2862 // inside Gamma_s's masked-out solid on a triply-cut element.
2863 const double xr_f = x_ref.data()[k * 3 + 0], yr_f = x_ref.data()[k * 3 + 1];
2864 D_f = equivalent_polynomials::composite::evaluate<nP_fit>(comp_DFa, xr_f, yr_f);
2865 }
2866 // A demoted element's Gamma_f lies entirely inside the solid: no part of it borders
2867 // fluid, so the flux-jump load must vanish exactly there (any fit would leave a
2868 // spurious source).
2869 if (demoted_f != 0)
2870 D_f = 0.0;
2871 if (immersedBoundary)
2872 {
2873 double level_set_normal[nSpace];
2874 double sign = 0.0;
2875 double norm_exact = 0.0, norm_cut = 0.0;
2876 for (int I = 0; I < nSpace; I++)
2877 {
2878 sign += immersedBoundary_normal_q.data()[eN_k_3d + I] * gf_f.get_normal()[I];
2879 level_set_normal[I] = gf_f.get_normal()[I];
2880 norm_cut += level_set_normal[I] * level_set_normal[I];
2881 norm_exact += immersedBoundary_normal_q.data()[eN_k_3d + I] * immersedBoundary_normal_q.data()[eN_k_3d + I];
2882 }
2883 assert(std::fabs(1.0 - norm_cut) < 1.0e-8);
2884 assert(std::fabs(1.0 - norm_exact) < 1.0e-8);
2885 if (sign < 0.0)
2886 for (int I = 0; I < nSpace; I++)
2887 level_set_normal[I] *= -1.0;
2888 updateImmersedBoundaryTerms(immersedBoundary_penalty / h_phi, // penalty,
2889 dV,
2890 level_set_normal,
2891 x,
2892 y,
2893 z,
2894 immersedBoundary_u_q.data()[eN_k],
2895 u,
2896 grad_u,
2897 a[0], // assume scalar diffusion for now
2898 r_f,
2899 dr_f,
2900 ham_f,
2901 dham_f,
2902 f_f,
2903 df_f,
2904 immersedBoundary_fluxJump_q.data()[eN_k],
2905 &immersedBoundary_fluxJumpVector_q.data()[eN_k_3d],
2906 // Gamma_f's jump physics only applies in the fluid: on an element that
2907 // sits (locally, at this quadrature point) entirely inside the embedded
2908 // solid, H_s==0 and D_f alone (gf_f.D, or the raw single-curve fallback
2909 // when comp_DFa isn't available) does not know that -- it only encodes
2910 // where Gamma_f itself is, not whether the fluid actually exists there.
2911 // Without this factor an element wholly inside the solid but crossed by
2912 // Gamma_f gets a fixed, u-independent Dirac source with nothing in the
2913 // Jacobian to balance it (the bulk terms are already correctly zero via
2914 // wA=wB=0), so Newton can never drive that row's residual to zero.
2915 // On a triply-cut cell comp_DFa is already restricted to the fluid part of
2916 // Gamma_f; multiplying it by the H_s fit again would restrict twice (a
2917 // product of two fits), so H_s is applied only to the single-curve D_f.
2918 (comp_DFa != nullptr) ? D_f : D_f * H_s);
2919 }
2920 //
2921 // calculate subgrid error contribution to the Jacobian (strong residual, adjoint, jacobian of strong residual)
2922 //
2923 // calculate the adjoint times the test functions
2924 for (int i = 0; i < nDOF_test_element; i++)
2925 {
2926 // int eN_k_i_nSpace = (eN_k*nDOF_trial_element+i)*nSpace;
2927 // Lstar_u[i]=ck.Advection_adjoint(df,&u_grad_test_dV.data()[eN_k_i_nSpace]);
2928 int i_nSpace = i * nSpace;
2929 Lstar_u[i] = ck.Advection_adjoint(df, &u_grad_test_dV[i_nSpace]);
2930 }
2931 // calculate the Jacobian of strong residual
2932 for (int j = 0; j < nDOF_trial_element; j++)
2933 {
2934 // int eN_k_j=eN_k*nDOF_trial_element+j;
2935 // int eN_k_j_nSpace = eN_k_j*nSpace;
2936 int j_nSpace = j * nSpace;
2937 dpdeResidual_u_u[j] = ck.MassJacobian_strong(dm_t, u_trial_ref.data()[k * nDOF_trial_element + j]) +
2938 ck.AdvectionJacobian_strong(df, &u_grad_trial[j_nSpace]);
2939 }
2940 // tau and tau*Res
2941 calculateSubgridError_tau(elementDiameter.data()[eN],
2942 dm_t,
2943 df,
2944 cfl.data()[eN_k],
2945 tau0);
2946
2948 G,
2949 dm_t,
2950 df,
2951 tau1,
2952 cfl.data()[eN_k]);
2953 tau = useMetrics * tau1 + (1.0 - useMetrics) * tau0;
2954
2955 for (int j = 0; j < nDOF_trial_element; j++)
2956 dsubgridError_u_u[j] = -tau * dpdeResidual_u_u[j];
2957
2958 // Leveque & Li 1994, Examples 1, 3, 4, PWC, PWL, PWQ
2959 double a_loc[nSpace * nSpace];
2960 for (int I = 0; I < nSpace * nSpace; I++) a_loc[I] = 0.0;
2961 for (int i = 0; i < nDOF_test_element; i++)
2962 {
2963 int i_nSpace = i * nSpace;
2964 for (int j = 0; j < nDOF_trial_element; j++)
2965 {
2966 int j_nSpace = j * nSpace;
2967 if (icase_f == 0)
2968 {
2969 if(!gf_f.exact.edge && !gf_f.exact.corner)
2970 {
2971 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mua;
2972 elementJacobian_u_u.data()[i * nDOF_trial_element + j] += wA * (ck.AdvectionJacobian_weak(df, ua_trial[j], &ua_grad_test_dV[i_nSpace]) +
2973 ck.SimpleDiffusionJacobian_weak(sd_rowptr.data(), sd_colind.data(), a_loc, &ua_grad_trial[j_nSpace], &ua_grad_test_dV[i_nSpace]) +
2974 ck.ReactionJacobian_weak(dr + dr_f, ua_trial[j], ua_test_dV[i]) +
2975 ck.NumericalDiffusionJacobian(q_numDiff_u_last.data()[eN_k], &ua_grad_trial[j_nSpace], &ua_grad_test_dV[i_nSpace]));
2976
2977 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mub;
2978 elementJacobian_u_u.data()[i * nDOF_trial_element + j] += wB * (ck.AdvectionJacobian_weak(df, ub_trial[j], &ub_grad_test_dV[i_nSpace]) +
2979 ck.SimpleDiffusionJacobian_weak(sd_rowptr.data(), sd_colind.data(), a_loc, &ub_grad_trial[j_nSpace], &ub_grad_test_dV[i_nSpace]) +
2980 ck.ReactionJacobian_weak(dr + dr_f, ub_trial[j], ub_test_dV[i]) +
2981 ck.NumericalDiffusionJacobian(q_numDiff_u_last.data()[eN_k], &ub_grad_trial[j_nSpace], &ub_grad_test_dV[i_nSpace]));
2982 }
2983 else if (gf_f.exact.edge == -1 || gf_f.exact.corner == -1)
2984 {
2985 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mua;
2986 elementJacobian_u_u.data()[i * nDOF_trial_element + j] += wA * (ck.AdvectionJacobian_weak(df, ua_trial[j], &ua_grad_test_dV[i_nSpace]) +
2987 ck.SimpleDiffusionJacobian_weak(sd_rowptr.data(), sd_colind.data(), a_loc, &ua_grad_trial[j_nSpace], &ua_grad_test_dV[i_nSpace]) +
2988 ck.ReactionJacobian_weak(dr + dr_f, ua_trial[j], ua_test_dV[i]) +
2989 ck.NumericalDiffusionJacobian(q_numDiff_u_last.data()[eN_k], &ua_grad_trial[j_nSpace], &ua_grad_test_dV[i_nSpace]));
2990 }
2991 else if (gf_f.exact.edge == 1 || gf_f.exact.corner == 1)
2992 {
2993 for (int I = 0; I < nSpace; I++) a_loc[I * nSpace + I] = mub;
2994 elementJacobian_u_u.data()[i * nDOF_trial_element + j] += wB * (ck.AdvectionJacobian_weak(df, ub_trial[j], &ub_grad_test_dV[i_nSpace]) +
2995 ck.SimpleDiffusionJacobian_weak(sd_rowptr.data(), sd_colind.data(), a_loc, &ub_grad_trial[j_nSpace], &ub_grad_test_dV[i_nSpace]) +
2996 ck.ReactionJacobian_weak(dr + dr_f, ub_trial[j], ub_test_dV[i]) +
2997 ck.NumericalDiffusionJacobian(q_numDiff_u_last.data()[eN_k], &ub_grad_trial[j_nSpace], &ub_grad_test_dV[i_nSpace]));
2998 }
2999 else assert(false && "Invalid gf_f.exact.edge/corner values. Should be -1, 0 or +1.");
3000 }
3001 else
3002 {
3003 elementJacobian_u_u.data()[i * nDOF_trial_element + j] += H_s * (ck.AdvectionJacobian_weak(df, u_trial_ref.data()[k * nDOF_trial_element + j], &u_grad_test_dV[i_nSpace]) +
3004 ck.SimpleDiffusionJacobian_weak(sd_rowptr.data(), sd_colind.data(), a, &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]) +
3005 ck.ReactionJacobian_weak(dr, u_trial_ref.data()[k * nDOF_trial_element + j], u_test_dV[i]) +
3006 ck.SubgridErrorJacobian(dsubgridError_u_u[j], Lstar_u[i]) +
3007 ck.NumericalDiffusionJacobian(q_numDiff_u_last.data()[eN_k], &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]));
3008 }
3009 if (embeddedBoundary)
3010 {
3011 if (gf_s.exact.edge >=0 && !gf_s.exact.corner)
3012 {
3013 if (icase_f == 0)
3014 {
3015 elementJacobian_u_u.data()[i * nDOF_trial_element + j] +=
3016 (ck.AdvectionJacobian_weak(df_s_a, ua_trial[j], &ua_grad_test_dV[i_nSpace])
3017 + ck.ReactionJacobian_weak(dr_s_a, ua_trial[j], ua_test_dV[i])
3018 + ck.HamiltonianJacobian_weak(dham_s_a, &ua_grad_trial[j_nSpace], ua_test_dV[i])) +
3019 (ck.AdvectionJacobian_weak(df_s_b, ub_trial[j], &ub_grad_test_dV[i_nSpace])
3020 + ck.ReactionJacobian_weak(dr_s_b, ub_trial[j], ub_test_dV[i])
3021 + ck.HamiltonianJacobian_weak(dham_s_b, &ub_grad_trial[j_nSpace], ub_test_dV[i]));
3022 }
3023 else
3024 {
3025 elementJacobian_u_u.data()[i * nDOF_trial_element + j] += (ck.AdvectionJacobian_weak(df_s, u_trial_ref.data()[k * nDOF_trial_element + j], &u_grad_test_dV[i_nSpace])
3026 + ck.ReactionJacobian_weak(dr_s, u_trial_ref.data()[k * nDOF_trial_element + j], u_test_dV[i])
3027 + ck.HamiltonianJacobian_weak(dham_s, &u_grad_trial[j_nSpace], u_test_dV[i]));
3028 }
3029 }
3030 }
3031 // immersedBoundary's dr_f is folded into the wA/wB branch reaction terms
3032 // above (ck.ReactionJacobian_weak(dr + dr_f, ...)), matching the residual
3033 // restructuring -- see the comment there. dr_f/df_f/dham_f are always zero
3034 // (updateImmersedBoundaryTerms), so this was a no-op numerically either way,
3035 // but kept the wrong (unenriched) test function on the books.
3036 } // j
3037 } // i
3038 } // k
3039 }
3040
3042 {
3043 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
3044 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
3045 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
3046 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
3047 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
3048 xt::pyarray<double> &u_trial_ref = args.array<double>("u_trial_ref");
3049 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
3050 xt::pyarray<double> &u_test_ref = args.array<double>("u_test_ref");
3051 xt::pyarray<double> &u_grad_test_ref = args.array<double>("u_grad_test_ref");
3052 xt::pyarray<double> &elementDiameter = args.array<double>("elementDiameter");
3053 xt::pyarray<double> &cfl = args.array<double>("cfl");
3054 double Ct_sge = args.scalar<double>("Ct_sge");
3055 double sc_uref = args.scalar<double>("sc_uref");
3056 double sc_alpha = args.scalar<double>("sc_alpha");
3057 double useMetrics = args.scalar<double>("useMetrics");
3058 xt::pyarray<double> &mesh_trial_trace_ref = args.array<double>("mesh_trial_trace_ref");
3059 xt::pyarray<double> &mesh_grad_trial_trace_ref = args.array<double>("mesh_grad_trial_trace_ref");
3060 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
3061 xt::pyarray<double> &u_trial_trace_ref = args.array<double>("u_trial_trace_ref");
3062 xt::pyarray<double> &u_grad_trial_trace_ref = args.array<double>("u_grad_trial_trace_ref");
3063 xt::pyarray<double> &u_test_trace_ref = args.array<double>("u_test_trace_ref");
3064 xt::pyarray<double> &u_grad_test_trace_ref = args.array<double>("u_grad_test_trace_ref");
3065 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
3066 xt::pyarray<double> &boundaryJac_ref = args.array<double>("boundaryJac_ref");
3067 int nElements_global = args.scalar<int>("nElements_global");
3068 xt::pyarray<int> &u_l2g = args.array<int>("u_l2g");
3069 xt::pyarray<double> &u_dof = args.array<double>("u_dof");
3070 xt::pyarray<int> &sd_rowptr = args.array<int>("sd_rowptr");
3071 xt::pyarray<int> &sd_colind = args.array<int>("sd_colind");
3072 xt::pyarray<double> &q_a = args.array<double>("q_a");
3073 xt::pyarray<double> &q_v = args.array<double>("q_v");
3074 xt::pyarray<double> &q_r = args.array<double>("q_r");
3075 int lag_shockCapturing = args.scalar<int>("lag_shockCapturing");
3076 double shockCapturingDiffusion = args.scalar<double>("shockCapturingDiffusion");
3077 xt::pyarray<double> &q_numDiff_u = args.array<double>("q_numDiff_u");
3078 xt::pyarray<double> &q_numDiff_u_last = args.array<double>("q_numDiff_u_last");
3079 xt::pyarray<int> &csrRowIndeces_u_u = args.array<int>("csrRowIndeces_u_u");
3080 xt::pyarray<int> &csrColumnOffsets_u_u = args.array<int>("csrColumnOffsets_u_u");
3081 xt::pyarray<double> &globalJacobian = args.array<double>("globalJacobian");
3082 int nExteriorElementBoundaries_global = args.scalar<int>("nExteriorElementBoundaries_global");
3083 xt::pyarray<int> &exteriorElementBoundariesArray = args.array<int>("exteriorElementBoundariesArray");
3084 xt::pyarray<int> &elementBoundaryElementsArray = args.array<int>("elementBoundaryElementsArray");
3085 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray = args.array<int>("elementBoundaryLocalElementBoundariesArray");
3086 xt::pyarray<double> &ebqe_a = args.array<double>("ebqe_a");
3087 xt::pyarray<double> &ebqe_v = args.array<double>("ebqe_v");
3088 xt::pyarray<int> &isDOFBoundary_u = args.array<int>("isDOFBoundary_u");
3089 xt::pyarray<double> &ebqe_bc_u_ext = args.array<double>("ebqe_bc_u_ext");
3090 xt::pyarray<int> &isDiffusiveFluxBoundary_u = args.array<int>("isDiffusiveFluxBoundary_u");
3091 xt::pyarray<int> &isAdvectiveFluxBoundary_u = args.array<int>("isAdvectiveFluxBoundary_u");
3092 xt::pyarray<double> &ebqe_bc_flux_u_ext = args.array<double>("ebqe_bc_flux_u_ext");
3093 xt::pyarray<double> &ebqe_bc_advectiveFlux_u_ext = args.array<double>("ebqe_bc_advectiveFlux_u_ext");
3094 xt::pyarray<int> &csrColumnOffsets_eb_u_u = args.array<int>("csrColumnOffsets_eb_u_u");
3095 xt::pyarray<double> &ebqe_penalty_ext = args.array<double>("ebqe_penalty_ext");
3096 const bool embeddedBoundary = args.scalar<int>("embeddedBoundary");
3097 const double embeddedBoundary_penalty = args.scalar<double>("embeddedBoundary_penalty");
3098 const double embeddedBoundary_ghost_penalty = args.scalar<double>("embeddedBoundary_ghost_penalty");
3099 xt::pyarray<double> &embeddedBoundary_sdf_nodes = args.array<double>("embeddedBoundary_sdf_nodes");
3100 xt::pyarray<double> &embeddedBoundary_sdf_q = args.array<double>("embeddedBoundary_sdf_q");
3101 xt::pyarray<double> &embeddedBoundary_normal_q = args.array<double>("embeddedBoundary_normal_q");
3102 xt::pyarray<double> &embeddedBoundary_u_q = args.array<double>("embeddedBoundary_u_q");
3103 xt::pyarray<double> &embeddedBoundary_u_inner_q = args.array<double>("embeddedBoundary_u_inner_q");
3104 xt::pyarray<double> &embeddedBoundary_u_outer_q = args.array<double>("embeddedBoundary_u_outer_q");
3105 const bool embeddedBoundary_robin = args.scalar<int>("embeddedBoundary_robin");
3106 const double embeddedBoundary_robin_h_inner = args.scalar<double>("embeddedBoundary_robin_h_inner");
3107 const double embeddedBoundary_robin_h_outer = args.scalar<double>("embeddedBoundary_robin_h_outer");
3108 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_inner_q = args.array<double>("embeddedBoundary_robin_uInfinity_inner_q");
3109 xt::pyarray<double> &embeddedBoundary_robin_uInfinity_outer_q = args.array<double>("embeddedBoundary_robin_uInfinity_outer_q");
3110 const bool immersedBoundary = args.scalar<int>("immersedBoundary");
3111 const double immersedBoundary_penalty = args.scalar<double>("immersedBoundary_penalty");
3112 const double immersedSCIFEM_switch = args.scalar<double>("immersedSCIFEM_switch");
3113 const double immersedSCIFEM_penalty = args.scalar<double>("immersedSCIFEM_penalty");
3114 const bool PG = args.scalar<int>("PG");
3115 xt::pyarray<double> &immersedBoundary_sdf_nodes = args.array<double>("immersedBoundary_sdf_nodes");
3116 xt::pyarray<double> &immersedBoundary_sdf_q = args.array<double>("immersedBoundary_sdf_q");
3117 xt::pyarray<double> &immersedBoundary_normal_q = args.array<double>("immersedBoundary_normal_q");
3118 xt::pyarray<double> &immersedBoundary_u_q = args.array<double>("immersedBoundary_u_q");
3119 xt::pyarray<double> &immersedBoundary_fluxJump_q = args.array<double>("immersedBoundary_fluxJump_q");
3120 xt::pyarray<double> &immersedBoundary_fluxJumpVector_q = args.array<double>("immersedBoundary_fluxJumpVector_q");
3121 xt::pyarray<double> &immersedBoundary_fluxJump_element = args.array<double>("immersedBoundary_fluxJump_element");
3122 xt::pyarray<double> &immersedBoundary_solutionJump_nodes = args.array<double>("immersedBoundary_solutionJump_nodes");
3123 xt::pyarray<double> &isActiveDOF = args.array<double>("isActiveDOF");
3124 const double eb_adjoint_sigma = args.scalar<double>("eb_adjoint_sigma");
3125 xt::pyarray<double> &x_ref = args.array<double>("x_ref");
3126 xt::pyarray<double> &xB_ref = args.array<double>("xB_ref");
3127 xt::pyarray<int> &elementBoundariesArray = args.array<int>("elementBoundariesArray");
3128 const int nElementBoundaries_owned = args.scalar<int>("nElementBoundaries_owned");
3129 xt::pyarray<double> &elementBoundaryDiameter = args.array<double>("elementBoundaryDiameter");
3130 xt::pyarray<double> &nodeDiametersArray = args.array<double>("nodeDiametersArray");
3131 const double mua = args.scalar<double>("mua");
3132 const double mub = args.scalar<double>("mub");
3133 const double jf = args.scalar<double>("jf");
3134 const bool recomputeIFEMGeometry = args.scalar<int>("recomputeIFEMGeometry");
3135 ensureIFEMCacheSized(nElements_global); // also (re)asserts useExact = true on (re)allocation
3136 if (recomputeIFEMGeometry)
3138 //
3139 // loop over elements to compute volume integrals and load them into the element Jacobians and global Jacobian
3140 //
3141 // Coefficients.triplyCut_edgeFluidSide: take the fluid-side one-sided edge correction on
3142 // triply-cut cells (flips the sign of the correction in residual and Jacobian).
3143 const bool tcFluidMode = args.scalar<int>("triplyCut_edgeFluidSide");
3144 // Coefficients.triplyCut_edgeBoundaryOnly (use with SCIFEM on): keep the one-sided fluid-side
3145 // correction only on root-adjacent edges that lie on the exterior boundary. Interior ones are then
3146 // handled by SCIFEM's two-sided consistency term, which has no counterpart on an exterior edge.
3147 const bool tcBoundaryOnly = args.scalar<int>("triplyCut_edgeBoundaryOnly");
3148 const double tcSgn = tcFluidMode ? -1.0 : 1.0;
3149 // Per-face reference-ELEMENT quadrature points, taken from proteus' own trace tables.
3150 // Simplex::calculate(..., isBoundary=true) reads its xi_r argument as reference *element*
3151 // coordinates (it forms x = node0 + Jac_0*xi_r), but xB_ref holds reference *boundary*
3152 // points (t,0,0) -- passing those directly puts every face's points on the local
3153 // node0->node1 edge, i.e. correct only for local face 2. mesh_trial_trace_ref holds the P1
3154 // mesh shape functions at (face, quadrature point) and for a triangle those barycentric
3155 // functions ARE the reference coordinates (phi_0=1-xi-eta, phi_1=xi, phi_2=eta), so this
3156 // is exactly consistent with calculateMapping_elementBoundary's own convention.
3157 double xB_ref_faces[nDOF_mesh_trial_element * nQuadraturePoints_elementBoundary * 3];
3158 for (int f = 0; f < nDOF_mesh_trial_element; f++)
3159 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
3160 {
3161 const int fkb = f * nQuadraturePoints_elementBoundary + kb;
3162 const double *phi_m = &mesh_trial_trace_ref.data()[fkb * nDOF_mesh_trial_element];
3163 xB_ref_faces[fkb * 3 + 0] = phi_m[1];
3164 xB_ref_faces[fkb * 3 + 1] = phi_m[2];
3165 xB_ref_faces[fkb * 3 + 2] = 0.0;
3166 }
3167 for (int eN = 0; eN < nElements_global; eN++)
3168 {
3169 // double elementJacobian_u_u.data()[nDOF_test_element*nDOF_trial_element],element_u[nDOF_trial_element];
3170 auto elementJacobian_u_u = xt::pyarray<double>::from_shape({nDOF_test_element * nDOF_trial_element});
3171 auto element_u = xt::pyarray<double>::from_shape({nDOF_trial_element});
3172 for (int j = 0; j < nDOF_trial_element; j++)
3173 {
3174 int eN_j = eN * nDOF_trial_element + j;
3175 element_u.data()[j] = u_dof.data()[u_l2g.data()[eN_j]];
3176 }
3177 double element_phi_s[nDOF_trial_element];
3178 for (int j = 0; j < nDOF_trial_element; j++)
3179 {
3180 int eN_j = eN * nDOF_trial_element + j;
3181 element_phi_s[j] = embeddedBoundary_sdf_nodes.data()[u_l2g.data()[eN_j]];
3182 }
3183 double element_phi_f[nDOF_trial_element];
3184 for (int j = 0; j < nDOF_trial_element; j++)
3185 {
3186 int eN_j = eN * nDOF_trial_element + j;
3187 element_phi_f[j] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[eN_j]];
3188 }
3189 double element_nodes[nDOF_trial_element * 3];
3190 for (int i = 0; i < nDOF_trial_element; i++)
3191 {
3192 int eN_i = eN * nDOF_trial_element + i;
3193 for (int I = 0; I < 3; I++)
3194 element_nodes[i * 3 + I] = mesh_dof.data()[u_l2g.data()[eN_i] * 3 + I];
3195 } // i
3197 {
3198 gf_s_interior_icase[eN] = gf_s_cache[eN].calculate(element_phi_s, element_nodes, x_ref.data(), false);
3200 }
3201 int icase_s = gf_s_interior_icase[eN];
3203 {
3204 gf_f_interior_icase[eN] = gf_f_cache[eN].calculate(element_phi_f, element_nodes, x_ref.data(), mua, mub, jf, false, false);
3206 }
3207 int icase_f = gf_f_interior_icase[eN];
3208 // Records that the buried-Gamma_f logic below demoted this element, and to which
3209 // phase: -1 = fluid is all Omega^- (mua), +1 = all Omega^+ (mub), 0 = not demoted.
3210 int demoted_f = 0;
3211 // Both level sets genuinely cut this element: the product weight ImH_f*H_s is not
3212 // moment-exact here, so fit one polynomial per intersected region instead. Any
3213 // element cut by at most one level set is left entirely alone.
3214 comp_valid[eN] = 0;
3215 comp_DF_valid[eN] = 0;
3216 if (icase_s == 0 && icase_f == 0 && nSpace == 2)
3217 {
3218 const double phi_s3[3] = {element_phi_s[0], element_phi_s[1], element_phi_s[2]};
3219 const double phi_f3[3] = {element_phi_f[0], element_phi_f[1], element_phi_f[2]};
3220 // The fluid interface can cross this element and still lie entirely inside the
3221 // solid, leaving the fluid undivided. There is then no fluid-fluid interface to
3222 // resolve *in the fluid*, so the two-sided IFEM reconstruction has nothing to
3223 // do -- and building it anyway means enforcing interface conditions along a
3224 // segment where no fluid exists. Demote the element to ordinary two-phase by
3225 // reporting the fluid's own side, which routes every downstream branch (bulk
3226 // terms, the solid's Nitsche condition, the error norm) to the standard basis.
3227 double areaA = 0.0, areaB = 0.0;
3228 equivalent_polynomials::composite::fluid_region_areas(phi_s3, phi_f3, areaA, areaB);
3229 // NOTE: we deliberately do NOT change icase_f here. Switching this element to the
3230 // standard basis would make it disagree with its neighbours about what their shared
3231 // DOFs mean: the neighbour (genuinely cut in the fluid) needs the pointwise nodal
3232 // value of u, while this element would need the surviving branch continued to all of
3233 // its nodes -- including nodes on the other side of Gamma_f. One DOF cannot be both,
3234 // so the exact solution would drop out of the discrete space entirely. Keeping the
3235 // IFEM basis costs nothing (u_ex satisfies the interface conditions globally, so
3236 // imposing them along a buried stretch of Gamma_f is harmless) and keeps the space
3237 // conforming. What we DO fix is the weights: the empty region must get exactly zero
3238 // rather than whatever a fit over an empty region would produce.
3239 //
3240 // Tolerance, not exact equality: a genuine tangency between Gamma_f and Gamma_s
3241 // (their zero-sets crossing exactly on a shared mesh edge) makes one region's true
3242 // area a single point, but floating-point roundoff in the clip almost never lands
3243 // on bit-exact 0.0 -- it lands a few ULPs away (observed: ~1e-33 against a
3244 // reference-triangle scale of 0.5). An exact-equality check misses that case
3245 // entirely and falls through to fitting a degree-4 polynomial to a numerically
3246 // degenerate sliver, which is unstable (observed: off by 64x on its own area).
3247 // areaTol is far above that roundoff floor and far below any physically meaningful
3248 // sub-area, so it can't misclassify a genuinely small-but-real fluid region.
3249 const double areaTol = 1.0e-9;
3250 if (areaB <= areaTol && areaA > areaTol)
3251 demoted_f = -1; // fluid is all Omega^- (mua side)
3252 else if (areaA <= areaTol && areaB > areaTol)
3253 demoted_f = 1; // fluid is all Omega^+ (mub side)
3254 else
3255 {
3256 const double nodes2[6] = {element_nodes[0], element_nodes[1],
3257 element_nodes[3], element_nodes[4],
3258 element_nodes[6], element_nodes[7]};
3260 phi_s3, phi_f3,
3261 &comp_A_cache[(std::size_t)eN * nCompMon],
3262 &comp_B_cache[(std::size_t)eN * nCompMon]) &&
3264 phi_s3, phi_f3, nodes2,
3265 &comp_DA_cache[(std::size_t)eN * nCompMon],
3266 &comp_DB_cache[(std::size_t)eN * nCompMon])) ? 1 : 0;
3268 phi_f3, phi_s3, nodes2,
3269 &comp_DFm_cache[(std::size_t)eN * nCompMon],
3270 &comp_DFa_cache[(std::size_t)eN * nCompMon]) ? 1 : 0;
3271 }
3272 }
3274 demoted_f,
3275 mesh_trial_ref,
3276 mesh_grad_trial_ref,
3277 mesh_dof,
3278 mesh_l2g,
3279 x_ref,
3280 dV_ref,
3281 u_trial_ref,
3282 u_grad_trial_ref,
3283 u_test_ref,
3284 u_grad_test_ref,
3285 elementDiameter,
3286 elementBoundaryDiameter,
3287 nodeDiametersArray,
3288 cfl,
3289 Ct_sge,
3290 sc_uref,
3291 sc_alpha,
3292 useMetrics,
3293 mesh_trial_trace_ref,
3294 mesh_grad_trial_trace_ref,
3295 dS_ref,
3296 u_trial_trace_ref,
3297 u_grad_trial_trace_ref,
3298 u_test_trace_ref,
3299 u_grad_test_trace_ref,
3300 normal_ref,
3301 boundaryJac_ref,
3302 nElements_global,
3303 nElementBoundaries_owned,
3304 u_l2g,
3305 u_dof,
3306 sd_rowptr,
3307 sd_colind,
3308 q_a,
3309 comp_valid[eN] ? &comp_A_cache[(std::size_t)eN * nCompMon] : nullptr,
3310 comp_valid[eN] ? &comp_B_cache[(std::size_t)eN * nCompMon] : nullptr,
3311 comp_valid[eN] ? &comp_DA_cache[(std::size_t)eN * nCompMon] : nullptr,
3312 comp_valid[eN] ? &comp_DB_cache[(std::size_t)eN * nCompMon] : nullptr,
3313 comp_DF_valid[eN] ? &comp_DFa_cache[(std::size_t)eN * nCompMon] : nullptr,
3314 q_v,
3315 q_r,
3316 lag_shockCapturing,
3317 shockCapturingDiffusion,
3318 q_numDiff_u,
3319 q_numDiff_u_last,
3320 elementJacobian_u_u,
3321 element_u,
3322 eN,
3323 embeddedBoundary,
3324 embeddedBoundary_penalty,
3325 embeddedBoundary_normal_q,
3326 embeddedBoundary_u_q,
3327 embeddedBoundary_u_inner_q,
3328 embeddedBoundary_u_outer_q,
3329 embeddedBoundary_robin,
3330 embeddedBoundary_robin_h_inner,
3331 embeddedBoundary_robin_h_outer,
3332 embeddedBoundary_robin_uInfinity_inner_q,
3333 embeddedBoundary_robin_uInfinity_outer_q,
3334 immersedBoundary,
3335 immersedBoundary_penalty,
3336 immersedBoundary_sdf_q,
3337 immersedBoundary_normal_q,
3338 immersedBoundary_u_q,
3339 immersedBoundary_fluxJump_q,
3340 immersedBoundary_fluxJumpVector_q,
3341 immersedBoundary_solutionJump_nodes,
3342 element_phi_f,
3343 mua,
3344 mub,
3345 PG);
3346 //
3347 // load into element Jacobian into global Jacobian
3348 //
3349 for (int i = 0; i < nDOF_test_element; i++)
3350 {
3351 int eN_i = eN * nDOF_test_element + i;
3352 for (int j = 0; j < nDOF_trial_element; j++)
3353 {
3354 int eN_i_j = eN_i * nDOF_trial_element + j;
3355 globalJacobian.data()[csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_u_u.data()[eN_i_j]] += elementJacobian_u_u.data()[i * nDOF_trial_element + j];
3356 } // j
3357 } // i
3358 // Jacobian of the correction above: exact 3x3 block, linear in u_dof through
3359 // the flux only. Same gate as the residual side, including the icase_s != -1 check.
3360 if (embeddedBoundary && immersedBoundary && (nDOF_trial_element == 3 || nDOF_trial_element == 6) && icase_f == 0
3361 && icase_s != -1)
3362 {
3363 // Same faces, quadrature and piece indicators as the residual side (P1 and P2).
3364 const int nD = nDOF_trial_element;
3365 bool neg[3] = {element_phi_f[0] < 0.0, element_phi_f[1] < 0.0, element_phi_f[2] < 0.0};
3366 int cnt_neg = (neg[0] ? 1 : 0) + (neg[1] ? 1 : 0) + (neg[2] ? 1 : 0);
3367 int root_i = -1;
3368 for (int ii = 0; ii < 3; ii++)
3369 if ((neg[ii] && cnt_neg == 1) || (!neg[ii] && cnt_neg == 2)) { root_i = ii; break; }
3370 if (root_i >= 0)
3371 {
3373 {
3374 gf_f_boundary_icase[eN] = gf_f_cache[eN].calculate(element_phi_f, element_nodes, xB_ref_faces, mua, mub, jf, true, false);
3376 }
3377 GfType &gf_f_tc = gf_f_cache[eN];
3378 const double sgn_s = tcFluidMode ? 1.0 : -1.0;
3379 for (int pj = 0; pj < 3; pj++)
3380 {
3381 if (pj == root_i)
3382 continue;
3383 const int ebN_tc = elementBoundariesArray.data()[eN * nDOF_mesh_trial_element + pj];
3384 if (tcBoundaryOnly && elementBoundaryElementsArray.data()[ebN_tc * 2 + 1] != -1)
3385 continue; // interior face: left to SCIFEM
3386 const int n0 = (pj + 1) % 3, n1 = (pj + 2) % 3;
3387 double C_a[nP_edge + 1], C_b[nP_edge + 1];
3388 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(-element_phi_f[n0], -element_phi_f[n1],
3389 sgn_s * element_phi_s[n0], sgn_s * element_phi_s[n1], C_a);
3390 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(element_phi_f[n0], element_phi_f[n1],
3391 sgn_s * element_phi_s[n0], sgn_s * element_phi_s[n1], C_b);
3392 const double ex0 = element_nodes[n0 * 3 + 0], ey0 = element_nodes[n0 * 3 + 1];
3393 const double evx = element_nodes[n1 * 3 + 0] - ex0, evy = element_nodes[n1 * 3 + 1] - ey0;
3394 const double elen2 = evx * evx + evy * evy;
3395 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
3396 {
3397 const int ebN_local_kb = pj * nQuadraturePoints_elementBoundary + kb;
3398 double jac_b[nSpace * nSpace], jacDet_b, jacInv_b[nSpace * nSpace],
3399 bJac_b[nSpace * (nSpace - 1)], mT_b[(nSpace - 1) * (nSpace - 1)], mTDS_b, n_b[2], xb, yb, zb;
3400 ck.calculateMapping_elementBoundary(eN, pj, kb, ebN_local_kb,
3401 mesh_dof.data(), mesh_l2g.data(),
3402 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
3403 boundaryJac_ref.data(), jac_b, jacDet_b, jacInv_b,
3404 bJac_b, mT_b, mTDS_b, normal_ref.data(), n_b, xb, yb, zb);
3405 const double dS = mTDS_b * dS_ref.data()[kb];
3406 const double t = ((xb - ex0) * evx + (yb - ey0) * evy) / elen2;
3409 gf_f_tc.set_boundary_quad(ebN_local_kb);
3410 for (int jj = 0; jj < nD; jj++)
3411 {
3412 const int eN_jj = eN * nDOF_test_element + jj;
3413 const double vA = gf_f_tc.VA(jj), vB = gf_f_tc.VB(jj);
3414 for (int kk = 0; kk < nD; kk++)
3415 {
3416 // JA/JB need no Jacobian term: fixed data, not a function of u_dof.
3417 const double fA_k = mua * (gf_f_tc.VA_x(kk) * n_b[0] + gf_f_tc.VA_y(kk) * n_b[1]);
3418 const double fB_k = mub * (gf_f_tc.VB_x(kk) * n_b[0] + gf_f_tc.VB_y(kk) * n_b[1]);
3419 const int eN_jj_kk = eN_jj * nDOF_trial_element + kk;
3420 globalJacobian.data()[csrRowIndeces_u_u.data()[eN_jj] + csrColumnOffsets_u_u.data()[eN_jj_kk]] += tcSgn * (Wa * fA_k * vA + Wb * fB_k * vB) * dS;
3421 }
3422 }
3423 }
3424 }
3425 }
3426 }
3427 } // elements
3428 for (std::set<int>::iterator it = cutfem_boundaries.begin(); it != cutfem_boundaries.end(); ++it)
3429 {
3430 std::map<int, double> Dw_Dn_jump;
3431 std::map<int, double> Dw_Dn_jump_a, Dw_Dn_jump_b;
3432 std::map<std::pair<int, int>, int> u_u_nz;
3433 double gamma_cutfem = embeddedBoundary_ghost_penalty, h_cutfem = elementBoundaryDiameter.data()[*it];
3434 // The two elements need not parametrize the shared face in the same direction, so
3435 // index kb can be DIFFERENT physical points on the two sides (see the SCIFEM loop's
3436 // orientation map). Harmless for P1, where the normal derivative is constant on a
3437 // face; for P2 it turns the variation along the face into a spurious jump. Match
3438 // side 1 to side 0 by position.
3439 int gp_kmap[nQuadraturePoints_elementBoundary];
3440 {
3441 double gp_xq[2][nQuadraturePoints_elementBoundary][3] = {}; // 2D mapping never writes z
3442 for (int s = 0; s < 2; s++)
3443 {
3444 const int eN_g = elementBoundaryElementsArray.data()[(*it) * 2 + s],
3445 ebN_local_g = elementBoundaryLocalElementBoundariesArray.data()[(*it) * 2 + s];
3446 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
3447 {
3448 double jac_g[nSpace * nSpace], jacDet_g, jacInv_g[nSpace * nSpace],
3449 bJac_g[nSpace * (nSpace - 1)], mT_g[(nSpace - 1) * (nSpace - 1)], mTDS_g, nrm_g[2];
3450 ck.calculateMapping_elementBoundary(eN_g, ebN_local_g, kb,
3451 ebN_local_g * nQuadraturePoints_elementBoundary + kb,
3452 mesh_dof.data(), mesh_l2g.data(),
3453 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
3454 boundaryJac_ref.data(), jac_g, jacDet_g, jacInv_g,
3455 bJac_g, mT_g, mTDS_g, normal_ref.data(), nrm_g,
3456 gp_xq[s][kb][0], gp_xq[s][kb][1], gp_xq[s][kb][2]);
3457 }
3458 }
3459 for (int k = 0; k < nQuadraturePoints_elementBoundary; k++)
3460 {
3461 int best = 0;
3462 double bestd = 1.0e300;
3463 for (int j = 0; j < nQuadraturePoints_elementBoundary; j++)
3464 {
3465 double d = 0.0;
3466 for (int I = 0; I < nSpace; I++)
3467 d += std::fabs(gp_xq[1][j][I] - gp_xq[0][k][I]);
3468 if (d < bestd) { bestd = d; best = j; }
3469 }
3470 gp_kmap[k] = best;
3471 }
3472 }
3473 int eN_nDOF_trial_element = elementBoundaryElementsArray.data()[(*it) * 2 + 0] * nDOF_trial_element;
3474 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
3475 {
3476 double Dp_Dn_jump = 0.0, Du_Dn_jump = 0.0, Dv_Dn_jump = 0.0, dS;
3477 bool phaseA_ok = true, phaseB_ok = true;
3478 for (int eN_side = 0; eN_side < 2; eN_side++)
3479 {
3480 int ebN = *it,
3481 eN = elementBoundaryElementsArray.data()[ebN * 2 + eN_side];
3482 for (int i = 0; i < nDOF_test_element; i++)
3483 {
3484 Dw_Dn_jump[u_l2g.data()[eN * nDOF_test_element + i]] = 0.0;
3485 Dw_Dn_jump_a[u_l2g.data()[eN * nDOF_test_element + i]] = 0.0;
3486 Dw_Dn_jump_b[u_l2g.data()[eN * nDOF_test_element + i]] = 0.0;
3487 }
3488 }
3489 for (int eN_side = 0; eN_side < 2; eN_side++)
3490 {
3491 int ebN = *it,
3492 eN = elementBoundaryElementsArray.data()[ebN * 2 + eN_side],
3493 ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + eN_side],
3494 eN_nDOF_trial_element = eN * nDOF_trial_element,
3495 kb_s = (eN_side == 0) ? kb : gp_kmap[kb],
3496 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb_s,
3497 ebN_local_kb_nSpace = ebN_local_kb * nSpace;
3498 double
3499 u_int = 0.0,
3500 grad_u_int[nSpace] = {0., 0.},
3501 jac_int[nSpace * nSpace],
3502 jacDet_int,
3503 jacInv_int[nSpace * nSpace],
3504 boundaryJac[nSpace * (nSpace - 1)],
3505 metricTensor[(nSpace - 1) * (nSpace - 1)],
3506 metricTensorDetSqrt,
3507 u_test_dS[nDOF_test_element],
3508 u_grad_trial_trace[nDOF_trial_element * nSpace],
3509 u_grad_test_dS[nDOF_trial_element * nSpace],
3510 normal[2], x_int, y_int, z_int, xt_int, yt_int, zt_int, integralScaling,
3511 G[nSpace * nSpace], G_dd_G, tr_G, h_phi, h_penalty, penalty;
3512 // compute information about mapping from reference element to physical element
3513 ck.calculateMapping_elementBoundary(eN,
3514 ebN_local,
3515 kb_s,
3516 ebN_local_kb,
3517 mesh_dof.data(),
3518 mesh_l2g.data(),
3519 mesh_trial_trace_ref.data(),
3520 mesh_grad_trial_trace_ref.data(),
3521 boundaryJac_ref.data(),
3522 jac_int,
3523 jacDet_int,
3524 jacInv_int,
3525 boundaryJac,
3526 metricTensor,
3527 metricTensorDetSqrt,
3528 normal_ref.data(),
3529 normal,
3530 x_int, y_int, z_int);
3531 dS = metricTensorDetSqrt * dS_ref.data()[kb_s]; // kb_s mirrors kb: same Gauss weight
3532 // compute shape and solution information
3533 // shape
3534 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_int, u_grad_trial_trace);
3535 for (int i = 0; i < nDOF_test_element; i++)
3536 {
3537 int eN_i = eN * nDOF_test_element + i;
3538 for (int I = 0; I < nSpace; I++)
3539 Dw_Dn_jump[u_l2g.data()[eN_i]] += u_grad_trial_trace[i * nSpace + I] * normal[I];
3540 }
3541 // Ghost penalty per fluid phase -- see the matching comment in calculateResidual.
3542 // Must mirror the residual exactly or Newton loses one-step consistency.
3543 int gp_icase = -2;
3544 double gp_va_grad[nDOF_trial_element * nSpace], gp_vb_grad[nDOF_trial_element * nSpace];
3545 if (immersedBoundary)
3546 {
3547 double gp_phi_f[nDOF_trial_element], gp_nodes[nDOF_trial_element * 3];
3548 for (int i = 0; i < nDOF_trial_element; i++)
3549 {
3550 const int gp_eN_i = eN * nDOF_trial_element + i;
3551 gp_phi_f[i] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[gp_eN_i]];
3552 for (int I = 0; I < 3; I++)
3553 gp_nodes[i * 3 + I] = mesh_dof.data()[u_l2g.data()[gp_eN_i] * 3 + I];
3554 }
3556 {
3557 gf_f_boundary_icase[eN] = gf_f_cache[eN].calculate(gp_phi_f, gp_nodes, xB_ref_faces, mua, mub, jf, true, false);
3559 }
3560 gp_icase = gf_f_boundary_icase[eN];
3561 }
3562 if (gp_icase == 0)
3563 {
3564 gf_f_cache[eN].set_boundary_quad(ebN_local_kb);
3565 for (int i = 0; i < nDOF_trial_element; i++)
3566 {
3567 gp_va_grad[i * nSpace + 0] = gf_f_cache[eN].VA_x(i);
3568 gp_va_grad[i * nSpace + 1] = gf_f_cache[eN].VA_y(i);
3569 gp_vb_grad[i * nSpace + 0] = gf_f_cache[eN].VB_x(i);
3570 gp_vb_grad[i * nSpace + 1] = gf_f_cache[eN].VB_y(i);
3571 }
3572 }
3573 else
3574 for (int i = 0; i < nDOF_trial_element * nSpace; i++)
3575 {
3576 gp_va_grad[i] = u_grad_trial_trace[i];
3577 gp_vb_grad[i] = u_grad_trial_trace[i];
3578 }
3579 phaseA_ok = phaseA_ok && (gp_icase == 0 || gp_icase == -1 || gp_icase == -2);
3580 phaseB_ok = phaseB_ok && (gp_icase == 0 || gp_icase == 1);
3581 for (int i = 0; i < nDOF_test_element; i++)
3582 {
3583 const int gp_dof = u_l2g.data()[eN * nDOF_trial_element + i];
3584 for (int I = 0; I < nSpace; I++)
3585 {
3586 Dw_Dn_jump_a[gp_dof] += gp_va_grad[i * nSpace + I] * normal[I];
3587 Dw_Dn_jump_b[gp_dof] += gp_vb_grad[i * nSpace + I] * normal[I];
3588 }
3589 }
3590 } // eN_side
3591 for (int eN_side = 0; eN_side < 2; eN_side++)
3592 {
3593 int ebN = *it,
3594 eN = elementBoundaryElementsArray.data()[ebN * 2 + eN_side];
3595 for (int i = 0; i < nDOF_test_element; i++)
3596 {
3597 int eN_i = eN * nDOF_test_element + i;
3598 for (int eN_side2 = 0; eN_side2 < 2; eN_side2++)
3599 {
3600 int eN2 = elementBoundaryElementsArray.data()[ebN * 2 + eN_side2];
3601 for (int j = 0; j < nDOF_test_element; j++)
3602 {
3603 int eN_i_j = eN_i * nDOF_test_element + j;
3604 int eN2_j = eN2 * nDOF_test_element + j;
3605 int ebN_i_j = ebN * 4 * nDOF_test_X_trial_element +
3606 eN_side * 2 * nDOF_test_X_trial_element +
3607 eN_side2 * nDOF_test_X_trial_element +
3608 i * nDOF_trial_element +
3609 j;
3610 std::pair<int, int> ij = std::make_pair(u_l2g.data()[eN_i], u_l2g.data()[eN2_j]);
3611 if (u_u_nz.count(ij))
3612 {
3613 assert(u_u_nz[ij] == csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_eb_u_u.data()[ebN_i_j]);
3614 }
3615 else
3616 u_u_nz[ij] = csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_eb_u_u.data()[ebN_i_j];
3617 }
3618 }
3619 }
3620 }
3621 {
3622 std::map<int, double> *gp_maps[2] = {&Dw_Dn_jump_a, &Dw_Dn_jump_b};
3623 bool gp_ok[2] = {phaseA_ok, phaseB_ok};
3624 if (!phaseA_ok && !phaseB_ok)
3625 {
3626 gp_maps[0] = &Dw_Dn_jump; gp_ok[0] = true; // fall back to the combined form
3627 gp_ok[1] = false;
3628 }
3629 for (int gp_p = 0; gp_p < 2; gp_p++)
3630 {
3631 if (!gp_ok[gp_p]) continue;
3632 std::map<int, double> &M = *gp_maps[gp_p];
3633 for (std::map<int, double>::iterator wi_it = M.begin(); wi_it != M.end(); ++wi_it)
3634 for (std::map<int, double>::iterator wj_it = M.begin(); wj_it != M.end(); ++wj_it)
3635 {
3636 std::pair<int, int> ij = std::make_pair(wi_it->first, wj_it->first);
3637 globalJacobian.data()[u_u_nz.at(ij)] += gamma_cutfem * h_cutfem * wj_it->second * wi_it->second * dS;
3638 } // i,j
3639 }
3640 }
3641 } // kb
3642 } // cutfem element boundaries
3643 // Jacobian of the SCIFEM (eq. 3.7) consistency terms assembled in calculateResidual --
3644 // same construction (orientation map + edge Heaviside blend of region-wise products),
3645 // differentiated w.r.t. each side's u_dof. Everything is linear in u_dof, so
3646 // d(avg_flux_a)/d(u_dof[s,j]) = 0.5*fta_m[s][j] and d(jump_ua)/d(u_dof[s,j]) =
3647 // (+/-)va_m[s][j] -- the same per-dof quantities used for the test index i.
3648 for (std::set<int>::iterator it = ifem_boundaries.begin(); it != ifem_boundaries.end(); ++it)
3649 {
3650 {
3651 std::map<std::pair<int, int>, int> u_u_nz;
3652 int ebN0 = *it;
3653 for (int eN_side = 0; eN_side < 2; eN_side++)
3654 {
3655 int eN = elementBoundaryElementsArray.data()[ebN0 * 2 + eN_side];
3656 for (int i = 0; i < nDOF_test_element; i++)
3657 {
3658 int eN_i = eN * nDOF_test_element + i;
3659 for (int eN_side2 = 0; eN_side2 < 2; eN_side2++)
3660 {
3661 int eN2 = elementBoundaryElementsArray.data()[ebN0 * 2 + eN_side2];
3662 for (int j = 0; j < nDOF_test_element; j++)
3663 {
3664 int eN2_j = eN2 * nDOF_test_element + j;
3665 int ebN_i_j = ebN0 * 4 * nDOF_test_X_trial_element +
3666 eN_side * 2 * nDOF_test_X_trial_element +
3667 eN_side2 * nDOF_test_X_trial_element +
3668 i * nDOF_trial_element + j;
3669 std::pair<int, int> ij = std::make_pair(u_l2g.data()[eN_i], u_l2g.data()[eN2_j]);
3670 if (!u_u_nz.count(ij))
3671 u_u_nz[ij] = csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_eb_u_u.data()[ebN_i_j];
3672 }
3673 }
3674 }
3675 }
3676
3677 int ebN = *it;
3678 int eN_s[2], ebN_local_s[2];
3679 for (int s = 0; s < 2; s++)
3680 {
3681 eN_s[s] = elementBoundaryElementsArray.data()[ebN * 2 + s];
3682 ebN_local_s[s] = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + s];
3683 }
3684
3685 // --- Edge-restricted phi (both level sets) --------------------------------
3686 double edge_nodes[2][3], phi_edge[2], phi_s_edge[2];
3687 {
3688 const int n0l = (ebN_local_s[0] + 1) % 3, n1l = (ebN_local_s[0] + 2) % 3;
3689 const int ln[2] = {n0l, n1l};
3690 for (int e = 0; e < 2; e++)
3691 {
3692 int eN_i = eN_s[0] * nDOF_trial_element + ln[e];
3693 phi_edge[e] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[eN_i]];
3694 phi_s_edge[e] = embeddedBoundary ? embeddedBoundary_sdf_nodes.data()[u_l2g.data()[eN_i]] : 1.0;
3695 for (int I = 0; I < 3; I++)
3696 edge_nodes[e][I] = mesh_dof.data()[u_l2g.data()[eN_i] * 3 + I];
3697 }
3698 }
3699 // edge diameter for the interior-penalty scaling (same measure the cutfem ghost
3700 // penalty uses); note gamma/h here for a VALUE jump, vs gamma*h there for a
3701 // gradient jump.
3702 const double h_edge = elementBoundaryDiameter.data()[ebN];
3703 double edge_vec[3];
3704 double edge_len2 = 0.0;
3705 for (int I = 0; I < 3; I++)
3706 {
3707 edge_vec[I] = edge_nodes[1][I] - edge_nodes[0][I];
3708 edge_len2 += edge_vec[I] * edge_vec[I];
3709 }
3710
3711 // --- Orientation map ------------------------------------------------------
3712 // The two elements sharing an interior face do NOT necessarily parametrize it in
3713 // the same direction, so quadrature index kb can denote DIFFERENT physical points
3714 // on the two sides (measured: 4 of 14 cut edges at test=8.0/refinement=1, with
3715 // exactly the mirror offsets |1-2t_k|*L of the Gauss rule). Pairing the sides by
3716 // raw kb then differences u at two different points, so [[u]] and {{beta grad u}}
3717 // pick up u's variation ALONG the edge instead of the genuine inter-element jump.
3718 // Build an explicit map instead: kmap[s][k] is side s's index for the physical
3719 // point that side 0 calls k. Matched by position, so no assumption about the
3720 // rule being symmetric or ascending.
3721 double xq[2][nQuadraturePoints_elementBoundary][3];
3722 for (int s = 0; s < 2; s++)
3723 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
3724 {
3725 double jac_t[nSpace * nSpace], jacDet_t, jacInv_t[nSpace * nSpace],
3726 bJac_t[nSpace * (nSpace - 1)], mT_t[(nSpace - 1) * (nSpace - 1)],
3727 mTDS_t, nrm_t[2], xt_ = 0.0, yt_ = 0.0, zt_ = 0.0;
3728 ck.calculateMapping_elementBoundary(eN_s[s], ebN_local_s[s], kb,
3729 ebN_local_s[s] * nQuadraturePoints_elementBoundary + kb,
3730 mesh_dof.data(), mesh_l2g.data(),
3731 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
3732 boundaryJac_ref.data(), jac_t, jacDet_t, jacInv_t,
3733 bJac_t, mT_t, mTDS_t, normal_ref.data(), nrm_t,
3734 xt_, yt_, zt_);
3735 xq[s][kb][0] = xt_; xq[s][kb][1] = yt_; xq[s][kb][2] = zt_;
3736 }
3737 // edge diameter: the length scale for the pairing tolerance just below,
3738 // and for the interior-penalty scaling further down (same measure the
3739 // cutfem ghost penalty uses); note gamma/h here for a VALUE jump, vs
3740 // gamma*h there for a gradient jump.
3741 // Match over nSpace components only: a component the mapping does not
3742 // define contributes nothing but noise to the distance, and the paired
3743 // points agree EXACTLY in the components it does define, so the winning
3744 // distance is 0 and the runner-up is a full quadrature spacing away.
3745 // Verify that outcome rather than trust it -- a mis-pick here silently
3746 // differences u at two different points, and assert() is compiled out by
3747 // the -DNDEBUG that reaches these translation units.
3748 int kmap[2][nQuadraturePoints_elementBoundary];
3749 bool kmap_used[nQuadraturePoints_elementBoundary] = {};
3750 for (int k = 0; k < nQuadraturePoints_elementBoundary; k++)
3751 {
3752 kmap[0][k] = k;
3753 int best = -1;
3754 double bestd = 1.0e300, nextd = 1.0e300;
3755 for (int j = 0; j < nQuadraturePoints_elementBoundary; j++)
3756 {
3757 double d = 0.0;
3758 for (int I = 0; I < nSpace; I++) // the 2D mapping never writes z
3759 d += std::fabs(xq[1][j][I] - xq[0][k][I]);
3760 if (d < bestd) { nextd = bestd; bestd = d; best = j; }
3761 else if (d < nextd) { nextd = d; }
3762 }
3763 if (best < 0 || kmap_used[best] || bestd >= 1.0e-8 * h_edge || nextd <= 2.0 * bestd)
3764 {
3765 std::cerr << "ADR immersedSCIFEM: quadrature points on face " << ebN
3766 << " between elements " << eN_s[0] << " and " << eN_s[1]
3767 << " do not pair up (k=" << k << ", best=" << best
3768 << ", d=" << bestd << ", runner-up=" << nextd
3769 << ", h=" << h_edge << "); the mesh is non-conforming or the"
3770 << " boundary mapping is inconsistent." << std::endl;
3771 throw std::runtime_error("ADR immersedSCIFEM: face quadrature pairing failed");
3772 }
3773 kmap_used[best] = true;
3774 kmap[1][k] = best;
3775 }
3776
3777 // --- Edge Heaviside moment fit -------------------------------------------
3778 // The facet integrand is DISCONTINUOUS at the interface crossing theta, so a fixed
3779 // whole-edge Gauss rule cannot integrate it directly. Weight the two one-sided
3780 // integrands by an edge-restricted moment-fit Heaviside instead: exact for any
3781 // polynomial integrand of degree <= nP_edge, hence exact for P1 (degree <= 1) and
3782 // P2 (degree <= 3) alike -- no linearity assumption, unlike a split-quadrature
3783 // scheme. ONE fit per edge, in side 0's parametrization: the two sides' fits are
3784 // mirror images, moment-equivalent but not pointwise equal, so they must not be
3785 // mixed. t below is therefore always measured along side 0's edge direction.
3786 // Branch a = {phi_f<0}, branch b = {phi_f>0}, each intersected with the fluid side
3787 // {phi_s>0} (phi_s_edge is 1 without an embedded solid, so this is then exactly the
3788 // single Gamma_f fit and its complement).
3789 double C_a_edge[nP_edge + 1], C_b_edge[nP_edge + 1];
3790 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(-phi_edge[0], -phi_edge[1], phi_s_edge[0], phi_s_edge[1], C_a_edge);
3791 equivalent_polynomials::calculate_edge_interval_H<nP_edge>(phi_edge[0], phi_edge[1], phi_s_edge[0], phi_s_edge[1], C_b_edge);
3792
3793 for (int k = 0; k < nQuadraturePoints_elementBoundary; k++)
3794 {
3795 double dS = 0.0;
3796 std::map<int, double> va_m[2], vb_m[2], fta_m[2], ftb_m[2];
3797 for (int eN_side = 0; eN_side < 2; eN_side++)
3798 {
3799 int eN = eN_s[eN_side],
3800 ebN_local = ebN_local_s[eN_side],
3801 kb = kmap[eN_side][k],
3802 eN_nDOF_trial_element = eN * nDOF_trial_element,
3803 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb;
3804 double jac_int[nSpace * nSpace], jacDet_int, jacInv_int[nSpace * nSpace],
3805 boundaryJac[nSpace * (nSpace - 1)],
3806 metricTensor[(nSpace - 1) * (nSpace - 1)], metricTensorDetSqrt,
3807 normal[2], x_int, y_int, z_int;
3808 ck.calculateMapping_elementBoundary(eN, ebN_local, kb, ebN_local_kb,
3809 mesh_dof.data(), mesh_l2g.data(),
3810 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
3811 boundaryJac_ref.data(), jac_int, jacDet_int, jacInv_int,
3812 boundaryJac, metricTensor, metricTensorDetSqrt,
3813 normal_ref.data(), normal, x_int, y_int, z_int);
3814 if (eN_side == 0)
3815 dS = metricTensorDetSqrt * dS_ref.data()[kb];
3816 double sign_ne = (eN_side == 0) ? 1.0 : -1.0;
3817
3818 double element_phi_f[nDOF_trial_element], element_nodes[nDOF_trial_element * 3];
3819 for (int i = 0; i < nDOF_trial_element; i++)
3820 {
3821 int eN_i = eN * nDOF_trial_element + i;
3822 element_phi_f[i] = immersedBoundary_sdf_nodes.data()[u_l2g.data()[eN_i]];
3823 for (int I = 0; I < 3; I++)
3824 element_nodes[i * 3 + I] = mesh_dof.data()[u_l2g.data()[eN_i] * 3 + I];
3825 }
3827 {
3828 gf_f_boundary_icase[eN] = gf_f_cache[eN].calculate(element_phi_f, element_nodes, xB_ref_faces, mua, mub, jf, true, false);
3830 }
3831 GfType &gf_f = gf_f_cache[eN];
3832 gf_f.set_boundary_quad(ebN_local_kb);
3833
3834 for (int i = 0; i < nDOF_test_element; i++)
3835 {
3836 int dof_i = u_l2g.data()[eN_nDOF_trial_element + i];
3837 va_m[eN_side][dof_i] = gf_f.VA(i);
3838 vb_m[eN_side][dof_i] = gf_f.VB(i);
3839 double gax = gf_f.VA_x(i), gay = gf_f.VA_y(i),
3840 gbx = gf_f.VB_x(i), gby = gf_f.VB_y(i);
3841 fta_m[eN_side][dof_i] = sign_ne * mua * (gax * normal[0] + gay * normal[1]);
3842 ftb_m[eN_side][dof_i] = sign_ne * mub * (gbx * normal[0] + gby * normal[1]);
3843 }
3844 } // eN_side
3845
3846 // t of this physical point along side 0's edge direction, then H_hat(t)
3847 double proj = 0.0;
3848 for (int I = 0; I < 3; I++)
3849 proj += (xq[0][k][I] - edge_nodes[0][I]) * edge_vec[I];
3850 double t_edge = (edge_len2 > 1.0e-24) ? (proj / edge_len2) : 0.0;
3851 // H_e / ImH_e: weights of branch b / branch a on its fluid piece of the edge
3852 double H_e = equivalent_polynomials::evaluate_edge_poly<nP_edge>(C_b_edge, t_edge);
3853 double ImH_e = equivalent_polynomials::evaluate_edge_poly<nP_edge>(C_a_edge, t_edge);
3854
3855 for (int side_i = 0; side_i < 2; side_i++)
3856 {
3857 double vi_sign = (side_i == 0) ? 1.0 : -1.0;
3858 for (std::map<int, double>::iterator wi = va_m[side_i].begin(); wi != va_m[side_i].end(); ++wi)
3859 {
3860 int dof_i = wi->first;
3861 double jva_i = vi_sign * va_m[side_i][dof_i], jvb_i = vi_sign * vb_m[side_i][dof_i];
3862 double fta_i = fta_m[side_i][dof_i], ftb_i = ftb_m[side_i][dof_i];
3863 for (int side_j = 0; side_j < 2; side_j++)
3864 {
3865 double vj_sign = (side_j == 0) ? 1.0 : -1.0;
3866 for (std::map<int, double>::iterator wj = va_m[side_j].begin(); wj != va_m[side_j].end(); ++wj)
3867 {
3868 int dof_j = wj->first;
3869 double jva_j = vj_sign * va_m[side_j][dof_j], jvb_j = vj_sign * vb_m[side_j][dof_j];
3870 double fta_j = fta_m[side_j][dof_j], ftb_j = ftb_m[side_j][dof_j];
3871 double dPa = 0.5 * fta_j * jva_i + 0.5 * fta_i * jva_j;
3872 double dPb = 0.5 * ftb_j * jvb_i + 0.5 * ftb_i * jvb_j;
3873 std::pair<int, int> ij = std::make_pair(dof_i, dof_j);
3874 globalJacobian.data()[u_u_nz.at(ij)] -=
3875 immersedSCIFEM_switch * (ImH_e * dPa + H_e * dPb) * dS;
3876 // derivative of the interior-penalty term: d[[u]]/du_j = [[v_j]],
3877 // so the contribution is the (symmetric, PSD) gram term below.
3878 globalJacobian.data()[u_u_nz.at(ij)] +=
3879 (immersedSCIFEM_penalty / h_edge) *
3880 (ImH_e * mua * jva_j * jva_i + H_e * mub * jvb_j * jvb_i) * dS;
3881 }
3882 }
3883 }
3884 }
3885 } // k
3886 }
3887 } // ifem element boundaries
3888 //
3889 // loop over exterior element boundaries to compute the surface integrals and load them into the global Jacobian
3890 //
3891 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++)
3892 {
3893 int ebN = exteriorElementBoundariesArray.data()[ebNE];
3894 int eN = elementBoundaryElementsArray.data()[ebN * 2 + 0],
3895 ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + 0],
3896 eN_nDOF_trial_element = eN * nDOF_trial_element;
3897 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++)
3898 {
3899 int ebNE_kb = ebNE * nQuadraturePoints_elementBoundary + kb,
3900 ebNE_kb_nSpace = ebNE_kb * nSpace,
3901 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb,
3902 ebN_local_kb_nSpace = ebN_local_kb * nSpace;
3903
3904 double u_ext = 0.0,
3905 grad_u_ext[nSpace],
3906 m_ext = 0.0,
3907 dm_ext = 0.0,
3908 *a_ext,
3909 f_ext[nSpace],
3910 df_ext[nSpace],
3911 r_ext = 0.0,
3912 dflux_u_u_ext = 0.0,
3913 bc_u_ext = 0.0,
3914 // bc_grad_u_ext[nSpace],
3915 bc_m_ext = 0.0,
3916 bc_dm_ext = 0.0,
3917 bc_f_ext[nSpace],
3918 bc_df_ext[nSpace],
3919 fluxJacobian_u_u[nDOF_trial_element],
3920 jac_ext[nSpace * nSpace],
3921 jacDet_ext,
3922 jacInv_ext[nSpace * nSpace],
3923 boundaryJac[nSpace * (nSpace - 1)],
3924 metricTensor[(nSpace - 1) * (nSpace - 1)],
3925 metricTensorDetSqrt,
3926 dS,
3927 u_test_dS[nDOF_test_element],
3928 u_grad_trial_trace[nDOF_trial_element * nSpace],
3929 u_grad_test_dS[nDOF_trial_element * nSpace],
3930 normal[nSpace], x_ext, y_ext, z_ext, xt_ext, yt_ext, zt_ext, integralScaling,
3931 //
3932 G[nSpace * nSpace], G_dd_G, tr_G;
3933 ck.calculateMapping_elementBoundary(eN,
3934 ebN_local,
3935 kb,
3936 ebN_local_kb,
3937 mesh_dof.data(),
3938 mesh_l2g.data(),
3939 mesh_trial_trace_ref.data(),
3940 mesh_grad_trial_trace_ref.data(),
3941 boundaryJac_ref.data(),
3942 jac_ext,
3943 jacDet_ext,
3944 jacInv_ext,
3945 boundaryJac,
3946 metricTensor,
3947 metricTensorDetSqrt,
3948 normal_ref.data(),
3949 normal,
3950 x_ext, y_ext, z_ext);
3951 dS = metricTensorDetSqrt * dS_ref.data()[kb];
3952 ck.calculateG(jacInv_ext, G, G_dd_G, tr_G);
3953 // compute shape and solution information
3954 // shape
3955 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_ext, u_grad_trial_trace);
3956 // solution and gradients
3957 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element], u_ext);
3958 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial_trace, grad_u_ext);
3959 // precalculate test function products with integration weights
3960 for (int j = 0; j < nDOF_trial_element; j++)
3961 {
3962 u_test_dS[j] = u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + j] * dS;
3963 for (int I = 0; I < nSpace; I++)
3964 u_grad_test_dS[j * nSpace + I] = u_grad_trial_trace[j * nSpace + I] * dS; // cek hack, using trial
3965 }
3966 //
3967 // load the boundary values
3968 //
3969 bc_u_ext = isDOFBoundary_u.data()[ebNE_kb] * ebqe_bc_u_ext.data()[ebNE_kb] + (1 - isDOFBoundary_u.data()[ebNE_kb]) * u_ext;
3970 a_ext = &ebqe_a.data()[ebNE_kb * sd_rowptr.data()[nSpace]];
3971 for (int I = 0; I < nSpace; I++)
3972 {
3973 df_ext[I] = ebqe_v.data()[ebNE_kb * nSpace + I];
3974 bc_df_ext[I] = ebqe_v.data()[ebNE_kb * nSpace + I];
3975 }
3976 //
3977 // calculate the numerical fluxes
3978 //
3979 exteriorNumericalAdvectiveFluxDerivative(isDOFBoundary_u.data()[ebNE_kb],
3980 isAdvectiveFluxBoundary_u.data()[ebNE_kb],
3981 normal,
3982 df_ext,
3983 dflux_u_u_ext);
3984 //
3985 // calculate the flux jacobian
3986 //
3987 for (int j = 0; j < nDOF_trial_element; j++)
3988 {
3989 // int ebNE_kb_j = ebNE_kb*nDOF_trial_element+j;
3990 int j_nSpace = j * nSpace, ebN_local_kb_j = ebN_local_kb * nDOF_trial_element + j;
3991 fluxJacobian_u_u[j] = ExteriorNumericalDiffusiveFluxJacobian(sd_rowptr.data(),
3992 sd_colind.data(),
3993 isDOFBoundary_u.data()[ebNE_kb],
3994 isDiffusiveFluxBoundary_u.data()[ebNE_kb],
3995 normal,
3996 a_ext,
3997 u_trial_trace_ref.data()[ebN_local_kb_j],
3998 &u_grad_trial_trace[j_nSpace],
3999 ebqe_penalty_ext.data()[ebNE_kb]) +
4000 ck.ExteriorNumericalAdvectiveFluxJacobian(dflux_u_u_ext, u_trial_trace_ref.data()[ebN_local_kb_j]);
4001 } // j
4002 //
4003 // update the global Jacobian from the flux Jacobian
4004 //
4005 for (int i = 0; i < nDOF_test_element; i++)
4006 {
4007 int eN_i = eN * nDOF_test_element + i;
4008 int i_nSpace = i * nSpace;
4009 for (int j = 0; j < nDOF_trial_element; j++)
4010 {
4011 int ebN_i_j = ebN * 4 * nDOF_test_X_trial_element + i * nDOF_trial_element + j;
4012 int ebN_local_kb_j = ebN_local_kb * nDOF_trial_element + j;
4013
4014 globalJacobian.data()[csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_eb_u_u.data()[ebN_i_j]] += fluxJacobian_u_u[j] * u_test_dS[i] +
4015 ck.ExteriorElementBoundaryDiffusionAdjointJacobian(isDOFBoundary_u.data()[ebNE_kb],
4016 isDiffusiveFluxBoundary_u.data()[ebNE_kb],
4017 eb_adjoint_sigma,
4018 u_trial_trace_ref.data()[ebN_local_kb_j],
4019 normal,
4020 sd_rowptr.data(),
4021 sd_colind.data(),
4022 a_ext,
4023 &u_grad_test_dS[i_nSpace]);
4024 } // j
4025 } // i
4026 } // kb
4027 } // ebNE
4028 } // computeJacobian
4029 }; // cADR
4030
4031 inline cADR_base *newADR(int nSpaceIn,
4032 int nQuadraturePoints_elementIn,
4033 int nDOF_mesh_trial_elementIn,
4034 int nDOF_trial_elementIn,
4035 int nDOF_test_elementIn,
4036 int nQuadraturePoints_elementBoundaryIn,
4037 int CompKernelFlag)
4038 {
4039 // P2 with the degree-8 (16-point) triangle rule, qOrder 8: the rule that integrates the
4040 // degree-4 equivalent polynomials times the degree-4 Gamma_s integrands exactly. ADR-only,
4041 // so it is not added to the shared ModelFactory.
4042 if (nSpaceIn == 2 && CompKernelFlag == 0 && nDOF_mesh_trial_elementIn == 3 && nDOF_trial_elementIn == 6 &&
4043 nDOF_test_elementIn == 6 && nQuadraturePoints_elementIn == 16 && nQuadraturePoints_elementBoundaryIn == 8)
4044 return static_cast<cADR_base *>(new cADR<CompKernel<2, 3, 6, 6>, 2, 16, 3, 6, 6, 8>());
4045 if (nSpaceIn == 2)
4047 nQuadraturePoints_elementIn,
4048 nDOF_mesh_trial_elementIn,
4049 nDOF_trial_elementIn,
4050 nDOF_test_elementIn,
4051 nQuadraturePoints_elementBoundaryIn,
4052 CompKernelFlag);
4053 else
4055 nQuadraturePoints_elementIn,
4056 nDOF_mesh_trial_elementIn,
4057 nDOF_trial_elementIn,
4058 nDOF_test_elementIn,
4059 nQuadraturePoints_elementBoundaryIn,
4060 CompKernelFlag);
4061 }
4062} // proteus
4063#endif
Int n
Definition Headers.h:28
Double ** sol
Definition Headers.h:40
Double r
Definition Headers.h:83
Double s
Definition Headers.h:84
Double u
Definition Headers.h:89
Int num
Definition Headers.h:32
Double * z
Definition Headers.h:49
Double v
Definition Headers.h:95
Simplex< nSpace, nP_ifem, nP, nQ, nEBQ, useIfemBasis > exact
virtual void calculateResidual(arguments_dict &args)=0
virtual void calculateJacobian(arguments_dict &args)=0
virtual ~cADR_base()
Definition ADR.h:28
void updateImmersedBoundaryTerms(const double immersedBoundary_penalty, const double dV, double *immersedBoundary_normal, double x, double y, double z, const double u_s, const double u, const double grad_u[nSpace], const double a, double &r, double &dr, double &ham, double *dham, double *f, double *df, const double fluxJump, const double *fluxJumpVector, const double D_f)
Definition ADR.h:393
void exteriorNumericalDiffusiveFlux(int *rowptr, int *colind, const int &isDOFBoundary, const int &isDiffusiveFluxBoundary, const double n[nSpace], double *bc_a, const double &bc_u, const double &bc_flux, double *a, const double grad_potential[nSpace], const double &u, const double &penalty, double &flux)
Definition ADR.h:132
std::vector< int > gf_s_interior_gen
Definition ADR.h:75
void calculateSubgridError_tau(const double &Ct_sge, const double G[nSpace *nSpace], const double &A0, const double Ai[nSpace], double &tau_v, double &cfl)
Definition ADR.h:226
std::vector< double > comp_DB_cache
Definition ADR.h:84
std::vector< int > gf_s_interior_icase
Definition ADR.h:76
std::vector< double > comp_B_cache
Definition ADR.h:83
std::vector< double > comp_A_cache
Definition ADR.h:83
static const int nP_edge
Definition ADR.h:62
std::vector< GfType > gf_s_cache
Definition ADR.h:74
std::set< int > ifem_boundaries
Definition ADR.h:43
CompKernelType ck
Definition ADR.h:47
void calculateSubgridError_tau(const double &elementDiameter, const double &dmt, const double dH[nSpace], double &cfl, double &tau)
Definition ADR.h:207
const int nDOF_test_X_trial_element
Definition ADR.h:46
std::vector< char > comp_valid
Definition ADR.h:85
static const int nP_fit
Definition ADR.h:57
std::vector< char > comp_DF_valid
Definition ADR.h:96
std::set< int > ifem_boundary_elements
Definition ADR.h:43
std::vector< double > comp_DA_cache
Definition ADR.h:84
static const int nCompMon
Definition ADR.h:82
void ensureIFEMCacheSized(int nElements_global)
Definition ADR.h:105
void exteriorNumericalAdvectiveFluxDerivative(const int &isDOFBoundary_u, const int &isFluxBoundary_u, const double n[nSpace], const double velocity[nSpace], double &dflux)
Definition ADR.h:303
std::set< int > cutfem_boundary_elements
Definition ADR.h:44
int ifemGeometryGeneration
Definition ADR.h:101
std::valarray< bool > elementIsActive
Definition ADR.h:45
void calculateElementResidual(int icase_f, const int demoted_f, xt::pyarray< double > &mesh_trial_ref, xt::pyarray< double > &mesh_grad_trial_ref, xt::pyarray< double > &mesh_dof, xt::pyarray< int > &mesh_l2g, xt::pyarray< double > &x_ref, xt::pyarray< double > &dV_ref, xt::pyarray< double > &u_trial_ref, xt::pyarray< double > &u_grad_trial_ref, xt::pyarray< double > &u_test_ref, xt::pyarray< double > &u_grad_test_ref, xt::pyarray< double > &elementDiameter, xt::pyarray< double > &elementBoundaryDiameter, xt::pyarray< double > &nodeDiametersArray, xt::pyarray< double > &cfl, double Ct_sge, double sc_uref, double sc_alpha, double useMetrics, xt::pyarray< double > &mesh_trial_trace_ref, xt::pyarray< double > &mesh_grad_trial_trace_ref, xt::pyarray< double > &dS_ref, xt::pyarray< double > &u_trial_trace_ref, xt::pyarray< double > &u_grad_trial_trace_ref, xt::pyarray< double > &u_test_trace_ref, xt::pyarray< double > &u_grad_test_trace_ref, xt::pyarray< double > &normal_ref, xt::pyarray< double > &boundaryJac_ref, int nElements_global, int nElementBoundaries_owned, xt::pyarray< int > &u_l2g, xt::pyarray< double > &u_dof, xt::pyarray< int > &sd_rowptr, xt::pyarray< int > &sd_colind, xt::pyarray< double > &q_a, const double *comp_A, const double *comp_B, const double *comp_DA, const double *comp_DB, const double *comp_DFa, xt::pyarray< double > &q_v, xt::pyarray< double > &q_r, int lag_shockCapturingDiffusion, double shockCapturingDiffusion, xt::pyarray< double > &q_numDiff_u, xt::pyarray< double > &q_numDiff_u_last, int offset_u, int stride_u, xt::pyarray< double > &elementResidual_u, int nExteriorElementBoundaries_global, xt::pyarray< int > &exteriorElementBoundariesArray, xt::pyarray< int > &elementBoundariesArray, xt::pyarray< int > &elementBoundaryElementsArray, xt::pyarray< int > &elementBoundaryLocalElementBoundariesArray, xt::pyarray< double > &element_u, int eN, const bool embeddedBoundary, const double embeddedBoundary_penalty, xt::pyarray< double > &embeddedBoundary_normal_q, xt::pyarray< double > &embeddedBoundary_u_q, xt::pyarray< double > &embeddedBoundary_u_inner_q, xt::pyarray< double > &embeddedBoundary_u_outer_q, const bool embeddedBoundary_robin, const double embeddedBoundary_robin_h_inner, const double embeddedBoundary_robin_h_outer, xt::pyarray< double > &embeddedBoundary_robin_uInfinity_inner_q, xt::pyarray< double > &embeddedBoundary_robin_uInfinity_outer_q, xt::pyarray< double > &embeddedBoundary_sdf_q, const bool immersedBoundary, const double immersedBoundary_penalty, xt::pyarray< double > &immersedBoundary_sdf_q, xt::pyarray< double > &immersedBoundary_normal_q, xt::pyarray< double > &immersedBoundary_u_q, xt::pyarray< double > &immersedBoundary_fluxJump_q, xt::pyarray< double > &immersedBoundary_fluxJumpVector_q, xt::pyarray< double > &immersedBoundary_solutionJump_nodes, double *element_phi_f, bool &element_active, std::valarray< bool > &elementIsActive, double *JA, double *JB, const double fluxLiftJ, double &L2_error, double &Linfty_error, double &H1_error, double mua, double mub, xt::pyarray< double > &q_u_exact_inner, xt::pyarray< double > &q_u_exact_outer, xt::pyarray< double > &q_grad_u_exact_inner, xt::pyarray< double > &q_grad_u_exact_outer, const bool PG)
Definition ADR.h:432
std::vector< double > fluxLiftAmp
Definition ADR.h:100
void updateEmbeddedBoundaryRobinTerms(const double h_coeff, const double u_infinity, const double u, double &r, double &dr, const double D_s)
Definition ADR.h:376
std::vector< int > gf_f_boundary_gen
Definition ADR.h:75
std::vector< int > gf_f_interior_icase
Definition ADR.h:76
std::vector< double > comp_DFm_cache
Definition ADR.h:95
std::vector< int > gf_f_boundary_icase
Definition ADR.h:76
std::vector< double > comp_DFa_cache
Definition ADR.h:95
void calculateNumericalDiffusion(const double &shockCapturingDiffusion, const double &elementDiameter, const double &strong_residual, const double grad_u[nSpace], double &numDiff)
Definition ADR.h:240
void updateEmbeddedBoundaryTerms(const double embeddedBoundary_penalty, const double dV, double *embeddedBoundary_normal, const double u_s, const double u, const double grad_u[nSpace], const double a, double &r, double &dr, double &ham, double *dham, double *f, double *df, const double D_s)
Definition ADR.h:340
std::set< int > cutfem_boundaries
Definition ADR.h:44
double ExteriorNumericalDiffusiveFluxJacobian(int *rowptr, int *colind, const int &isDOFBoundary, const int &isDiffusiveFluxBoundary, const double n[nSpace], double *a, const double &v, const double grad_v[nSpace], const double &penalty)
Definition ADR.h:177
std::vector< int > gf_f_interior_gen
Definition ADR.h:75
void calculateResidual(arguments_dict &args)
Definition ADR.h:1148
GeneralizedFunctions< nSpace, nDOF_trial_element, nP_fit, nQuadraturePoints_element, nDOF_mesh_trial_element *nQuadraturePoints_elementBoundary > GfType
Definition ADR.h:58
void calculateJacobian(arguments_dict &args)
Definition ADR.h:3041
void calculateElementJacobian(int icase_f, const int demoted_f, xt::pyarray< double > &mesh_trial_ref, xt::pyarray< double > &mesh_grad_trial_ref, xt::pyarray< double > &mesh_dof, xt::pyarray< int > &mesh_l2g, xt::pyarray< double > &x_ref, xt::pyarray< double > &dV_ref, xt::pyarray< double > &u_trial_ref, xt::pyarray< double > &u_grad_trial_ref, xt::pyarray< double > &u_test_ref, xt::pyarray< double > &u_grad_test_ref, xt::pyarray< double > &elementDiameter, xt::pyarray< double > &elementBoundaryDiameter, xt::pyarray< double > &nodeDiametersArray, xt::pyarray< double > &cfl, double Ct_sge, double sc_uref, double sc_alpha, double useMetrics, xt::pyarray< double > &mesh_trial_trace_ref, xt::pyarray< double > &mesh_grad_trial_trace_ref, xt::pyarray< double > &dS_ref, xt::pyarray< double > &u_trial_trace_ref, xt::pyarray< double > &u_grad_trial_trace_ref, xt::pyarray< double > &u_test_trace_ref, xt::pyarray< double > &u_grad_test_trace_ref, xt::pyarray< double > &normal_ref, xt::pyarray< double > &boundaryJac_ref, int nElements_global, int nElementBoundaries_owned, xt::pyarray< int > &u_l2g, xt::pyarray< double > &u_dof, xt::pyarray< int > &sd_rowptr, xt::pyarray< int > &sd_colind, xt::pyarray< double > &q_a, const double *comp_A, const double *comp_B, const double *comp_DA, const double *comp_DB, const double *comp_DFa, xt::pyarray< double > &q_v, xt::pyarray< double > &q_r, int lag_shockCapturing, double shockCapturingDiffusion, xt::pyarray< double > &q_numDiff_u, xt::pyarray< double > &q_numDiff_u_last, xt::pyarray< double > &elementJacobian_u_u, xt::pyarray< double > &element_u, int eN, const bool embeddedBoundary, const double embeddedBoundary_penalty, xt::pyarray< double > &embeddedBoundary_normal_q, xt::pyarray< double > &embeddedBoundary_u_q, xt::pyarray< double > &embeddedBoundary_u_inner_q, xt::pyarray< double > &embeddedBoundary_u_outer_q, const bool embeddedBoundary_robin, const double embeddedBoundary_robin_h_inner, const double embeddedBoundary_robin_h_outer, xt::pyarray< double > &embeddedBoundary_robin_uInfinity_inner_q, xt::pyarray< double > &embeddedBoundary_robin_uInfinity_outer_q, const bool immersedBoundary, const double immersedBoundary_penalty, xt::pyarray< double > &immersedBoundary_sdf_q, xt::pyarray< double > &immersedBoundary_normal_q, xt::pyarray< double > &immersedBoundary_u_q, xt::pyarray< double > &immersedBoundary_fluxJump_q, xt::pyarray< double > &immersedBoundary_fluxJumpVector_q, xt::pyarray< double > &immersedBoundary_solutionJump_nodes, double *element_phi_f, double mua, double mub, const bool PG)
Definition ADR.h:2507
void exteriorNumericalAdvectiveFlux(const int &isDOFBoundary_u, const int &isFluxBoundary_u, const double n[nSpace], const double &bc_u, const double &bc_flux_u, const double &u, const double velocity[nSpace], double &flux)
Definition ADR.h:259
std::vector< GfType > gf_f_cache
Definition ADR.h:74
double df(double C, double b, double a, int q, int r)
#define sign(x, y)
Definition jf.h:44
#define w(x)
Definition jf.h:22
bool edge_weights(const double phi_target[3], const double phi_split[3], const double nodes[6], double *cLeft, double *cRight)
void fluid_region_areas(const double phi_s[3], const double phi_f[3], double &areaA, double &areaB)
double evaluate(const double *c, double x, double y)
bool weights(const double phi_s[3], const double phi_f[3], double *cA, double *cB)
bool dirac_weights(const double phi_s[3], const double phi_f[3], const double nodes[6], double *cDA, double *cDB)
double evaluate_edge_poly(const double C[nP+1], double t)
void calculate_edge_interval_H(double f0, double f1, double g0, double g1, double C[nP+1])
Definition ADR.h:21
equivalent_polynomials::GeneralizedFunctions_mix< nSpace, nP_ifem, nP, nQ, nEBQ, true > GeneralizedFunctions
Definition ADR.h:23
cADR_base * newADR(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_test_elementIn, int nQuadraturePoints_elementBoundaryIn, int CompKernelFlag)
Definition ADR.h:4031
Model_Base * chooseAndAllocateDiscretization(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_test_elementIn, int nDOF_v_trial_elementIn, int nDOF_v_test_elementIn, int nQuadraturePoints_elementBoundaryIn, int CompKernelFlag)
Model_Base * chooseAndAllocateDiscretization2D(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_test_elementIn, int nDOF_v_trial_elementIn, int nDOF_v_test_elementIn, int nQuadraturePoints_elementBoundaryIn, int CompKernelFlag)
double f(const double &g, const double &h, const double &hZ)
Definition SW2DCV.h:58
T & scalar(const std::string &key)
xt::pyarray< T > & array(const std::string &key)