proteus 1.9.1
C/C++/Fortran libraries
Loading...
Searching...
No Matches
Richards.h
Go to the documentation of this file.
1#ifndef Richards_H
2#define Richards_H
3#include <cmath>
4#include <iostream>
5#include <valarray>
6#include <vector>
7#include "CompKernel.h"
8#include "ModelFactory.h"
9#include "../pskRelations.h"
11#include "xtensor-python/pyarray.hpp"
12#define nnz nSpace
13
14namespace py = pybind11;
15#define POWER_SMOOTHNESS_INDICATOR 2
16#define IS_BETAij_ONE 0
17#define GLOBAL_FCT 0
18namespace proteus
19{
20enum class STABILIZATION : int {
25};
26
27namespace richards
28{
29//Coefficients(FCT_BND_KRW=...), the weak-Dirichlet boundary block that feeds the HIGH order FCT flux fH.
30//True -> fH's boundary block uses a_ext = rho*k_rw*Ks, matching the cell loop's `a`,
31// so it cancels against fL's boundary block (as_ext scaled by the nodal
32// upwind k_rw) in fA = fH - fL, exactly as the interior blocks do.
33//False (default) -> the old behaviour: fH's boundary block uses as_ext = rho*Ks, leaving a
34// residue in fA of order 1/k_rw times the interior antidiffusion.
35//Only TransportMatrixConsistent depends on it, so FCT=False is unaffected either way.
36//Coefficients(FCT_BND_DIFF=...), step B: keep the weak-Dirichlet block in its own matrix TB instead of folding it into
37//TransportMatrix where the interior fmax(0,-T_ij) clip mangles it, and apply it
38//identically in the low and the high order flux,
39// fH_ij = fL_ij = -TB_ij dPhi (boundary block),
40//so it cancels out of fA: the antidiffusive flux is the interior one alone, antisymmetric,
41//and the limited correction conserves mass. TB gets NO graph viscosity: one acting on
42//dPhi (gravity included) is an O(1) inconsistency at the boundary rows that only the
43//limiter could remove, and the limiter clips exactly there
44//(test/salinity/mms_coupled/MMS_PLAN.md).
45//The price is that the low order boundary rows are not an M-matrix.
46//FCT_BND_DIFF=False keeps the old routing (TB folded into TransportMatrix).
47//Coefficients(FCT_BND_RESET=True): reset the FCT boundary bounds min_m_bc/max_m_bc every call
48//(see calculateResidual_entropy_viscosity); False keeps the old accumulate-forever window.
49//Coefficients(BC_CONS=True): hand the transport model the water flux this scheme really applies on the exterior
50//boundary instead of the pointwise interior trace -(a/rho)(grad psi - rho_r g).n. See
51//the consBoundaryFlux comment in calculateResidual_entropy_viscosity for the algebra and
52//for the numbers that motivated it. BC_CONS=False restores the pointwise trace.
53
54//cek todo: revisit entry for mass transport form
55// Power entropy //
56inline double ENTROPY(const double &phi, const double &phiL, const double &phiR)
57{
58 return 1. / 2. * std::pow(fabs(phi), 2.);
59}
60inline double DENTROPY(const double &phi, const double &phiL, const double &phiR)
61{
62 return fabs(phi) * (phi >= 0 ? 1 : -1);
63}
64// Log entropy // for level set from 0 to 1
65inline double ENTROPY_LOG(const double &phi, const double &phiL, const double &phiR)
66{
67 return std::log(fabs((phi - phiL) * (phiR - phi)) + 1E-14);
68}
69inline double DENTROPY_LOG(const double &phi, const double &phiL, const double &phiR)
70{
71 return (phiL + phiR - 2 * phi) * ((phi - phiL) * (phiR - phi) >= 0 ? 1 : -1) / (fabs((phi - phiL) * (phiR - phi)) + 1E-14);
72}
73} // namespace richards
74} // namespace proteus
75namespace proteus
76{
77namespace richards
78{
80 //The base class defining the interface
81public:
82 virtual ~Richards_base() { double anb_seepage_flux = 1e-16; }
83 virtual void calculateResidual(arguments_dict &args) = 0;
84 virtual void calculateJacobian(arguments_dict &args) = 0;
85 virtual void invert(arguments_dict &args) = 0;
86 virtual void FCTStep(arguments_dict &args) = 0;
87 virtual void ImplicitFCTStep(arguments_dict &args) = 0;
88 virtual void kth_FCT_step(arguments_dict &args) = 0;
90 virtual void calculateMassMatrix(arguments_dict &args) = 0;
91 virtual void calculateEdgeVelocity(arguments_dict &args) = 0;
92};
93
94template <class CompKernelType, int nSpace, int nQuadraturePoints_element, int nDOF_mesh_trial_element, int nDOF_trial_element, int nDOF_test_element, int nQuadraturePoints_elementBoundary>
95class Richards : public Richards_base {
96public:
98 CompKernelType ck;
99 // Per-DOF density projected from q_rho in calculateResidual_entropy_viscosity.
100 // Reused by invert() so the m -> u inversion uses the same variable density
101 // that built the forward mass.
102 std::vector<double> rho_dof_member;
103 // Scratch arrays sized by the GLOBAL DOF count, held as members and resized
104 // in place rather than declared as stack VLAs inside the kernels.
105 //
106 // They used to be `double psi[numDOFs], ...` etc. numDOFs is global, so on a
107 // refined mesh those are megabytes of automatic storage: at numDOFs = 361501
108 // (Elder 1201x301) the four in calculateResidual_entropy_viscosity are
109 // 4 x 361501 x 8 = 11.6 MB against a default `ulimit -s` of 8 MB, and the
110 // function segfaulted on its very first call with no other symptom. The
111 // cliff sits at 8 MB/(4 x 8 B) = 262144 DOFs -- below it everything works and
112 // nothing is ever noticed, which is why it only bites after a refinement, and
113 // why a case can run under a scheduler (which usually raises or unlimits the
114 // stack) yet crash on a workstation with identical code.
115 //
116 // resize() only reallocates when the size grows, so after the first call this
117 // costs nothing; the explicit zeroing at each use site is kept because eta and
118 // global_entropy_residual are written only under EV_Stab, and a reused buffer
119 // would otherwise hand the next call the previous one's values.
123 // Pore size distribution / relative permeability model selected from Python
124 // (Coefficients.PSK_type). 0 = van Genuchten-Mualem (default), 1 = Brooks-
125 // Corey-Burdine, 2 = Brooks-Corey-Mualem, 3 = Gardner, 4 = Hornung-Messing.
126 // Refreshed from args at
127 // the top of every kernel that evaluates the closure.
129 // Air-entry head h_e >= 0 of the van Genuchten-Mualem closure (PSK_TYPE 0), refreshed with
130 // PSK_TYPE. h_e > 0 selects the modification of Ippisch, Vogel & Bastian (2006): saturated for
131 // psiC <= h_e, Se = sBar(psiC)/S_c and k_r = sqrt(Se) (vBar/V_c)^2 above, S_c = sBar(h_e),
132 // V_c = vBar(h_e). theta and k_r then have bounded slopes at saturation; for n < 2 the
133 // unmodified closure's are unbounded there, which stalls Newton at nodes crossing psi = 0.
135 inline void vgAirEntryScales(const double alpha, const double n_vg, double &Sc, double &Vc)
136 {
137 const double m_vg = 1.0 - 1.0 / n_vg;
138 const double pcBar = alpha * VG_AIR_ENTRY_member;
139 Sc = pow(1.0 + pow(pcBar, n_vg), -m_vg);
140 Vc = 1.0 - pow(pcBar, n_vg - 1.0) * Sc;
141 }
142 Richards() : nDOF_test_X_trial_element(nDOF_test_element * nDOF_trial_element), ck() { }
143 inline void evaluateCoefficients(const int rowptr[nSpace], const int colind[nnz], const double rho0, const double rho_transport, const double beta, const double gravity[nSpace], const double alpha, const double n_vg, const double thetaR, const double thetaSR, const double KWs[nnz], const double &u, double &m, double &dm, double f[nSpace], double df[nSpace], double a[nnz], double da[nnz], double as[nnz], double &kr, double &dkr, double &thetaW_out)
144 {
145 const double psiC = -u;
146 double thetaW, DthetaW_DpsiC, KWr, DKWr_DpsiC;
147 if (PSK_TYPE_member == 1 || PSK_TYPE_member == 2) {
148 // Same Brooks-Corey retention curve either way; PSK_TYPE only picks which
149 // k_rw closure supplies the exponent (see pskRelations.h).
151 psiC, alpha, n_vg, thetaR, thetaSR,
152 thetaW, DthetaW_DpsiC, KWr, DKWr_DpsiC,
155 } else if (PSK_TYPE_member == 3) {
157 psiC, alpha, n_vg, thetaR, thetaSR,
158 thetaW, DthetaW_DpsiC, KWr, DKWr_DpsiC);
159 } else if (PSK_TYPE_member == 4) {
161 psiC, alpha, n_vg, thetaR, thetaSR,
162 thetaW, DthetaW_DpsiC, KWr, DKWr_DpsiC);
163 } else if (VG_AIR_ENTRY_member > 0.0) {
164 if (psiC <= VG_AIR_ENTRY_member) {
165 thetaW = thetaR + thetaSR;
166 DthetaW_DpsiC = 0.0;
167 KWr = 1.0;
168 DKWr_DpsiC = 0.0;
169 } else {
170 double Sc, Vc;
171 vgAirEntryScales(alpha, n_vg, Sc, Vc);
173 psiC, alpha, n_vg, thetaR, thetaSR,
174 thetaW, DthetaW_DpsiC, KWr, DKWr_DpsiC);
175 thetaW = thetaR + (thetaW - thetaR) / Sc;
176 DthetaW_DpsiC /= Sc;
177 const double krScale = 1.0 / (sqrt(Sc) * Vc * Vc);
178 KWr *= krScale;
179 DKWr_DpsiC *= krScale;
180 }
181 } else {
183 psiC, alpha, n_vg, thetaR, thetaSR,
184 thetaW, DthetaW_DpsiC, KWr, DKWr_DpsiC);
185 }
186 thetaW_out = thetaW;
187 // Density uses transported salinity scaled by the compressibility factor.
188 const double rhom = rho_transport * exp(beta * u);
189 const double drhom = beta * rhom;
190 m = rhom * thetaW;
191 dm = -rhom * DthetaW_DpsiC + drhom * thetaW;
192 const double rho_ratio = rhom / rho0;
193 for (int I = 0; I < nSpace; I++) {
194 f[I] = 0.0;
195 df[I] = 0.0;
196 for (int ii = rowptr[I]; ii < rowptr[I + 1]; ii++) {
197 a[ii] = rhom * KWr * KWs[ii];
198 da[ii] = (drhom * KWr - rhom * DKWr_DpsiC) * KWs[ii];
199 f[I] += rhom * rho_ratio * KWr * KWs[ii] * gravity[colind[ii]];
200 df[I] += (drhom * rho_ratio * KWr +
201 rhom * (drhom / rho0) * KWr -
202 rhom * rho_ratio * DKWr_DpsiC) * KWs[ii] * gravity[colind[ii]];
203 as[ii] = rhom * KWs[ii];
204 kr = KWr;
205 dkr = -DKWr_DpsiC;
206 }
207 }
208 }
209
210 inline void evaluateInverseCoefficients(const int rowptr[nSpace], const int colind[nnz], const double rho, const double beta, const double gravity[nSpace], const double alpha, const double n_vg, const double thetaR, const double thetaSR, const double KWs[nnz], double &u, const double &m, const double &dm, const double f[nSpace], const double df[nSpace], const double a[nnz], const double da[nnz])
211 {
212 (void)rowptr; (void)colind; (void)beta; (void)gravity; (void)KWs;
213 (void)dm; (void)f; (void)df; (void)a; (void)da;
214
215 // Both Brooks-Corey codes share one retention curve, so both invert with
216 // bc_*; PSK_TYPE 1 vs 2 differs only in k_rw, which plays no part here.
217 if (PSK_TYPE_member == 1 || PSK_TYPE_member == 2) {
218 proteus::richards::psk::bc_invert_analytic(m, rho, alpha, n_vg, thetaR, thetaSR, u);
219 } else if (PSK_TYPE_member == 3) {
220 proteus::richards::psk::gardner_invert_analytic(m, rho, alpha, n_vg, thetaR, thetaSR, u);
221 } else if (PSK_TYPE_member == 4) {
222 proteus::richards::psk::hm_invert_analytic(m, rho, alpha, n_vg, thetaR, thetaSR, u);
223 } else if (VG_AIR_ENTRY_member > 0.0) {
224 // theta_vg = thetaR + S_c (theta - thetaR) is the unmodified curve's water content at the same psiC
225 double Sc, Vc;
226 vgAirEntryScales(alpha, n_vg, Sc, Vc);
227 proteus::richards::psk::vgm_invert_analytic(Sc * m + (1.0 - Sc) * rho * thetaR, rho, alpha, n_vg, thetaR, thetaSR, u);
228 } else {
229 proteus::richards::psk::vgm_invert_analytic(m, rho, alpha, n_vg, thetaR, thetaSR, u);
230 }
231 }
232
233inline void evaluateInverseCoefficients_Newton(const int rowptr[nSpace],
234 const int colind[nnz],
235 const double rho,
236 const double beta,
237 const double gravity[nSpace],
238 const double alpha,
239 const double n_vg,
240 const double thetaR,
241 const double thetaSR,
242 const double KWs[nnz],
243 double &u, // in/out
244 const double &m,
245 const double &dm,
246 const double f[nSpace],
247 const double df[nSpace],
248 const double a[nnz],
249 const double da[nnz])
250{
251 (void)rowptr; (void)colind; (void)gravity; (void)KWs;
252 (void)dm; (void)f; (void)df; (void)a; (void)da;
253
254 // Newton inversion of the full forward mass m = rho(u)*theta_w(u), so the
255 // exp(beta*u) factor the analytic inverse drops is included. The retention
256 // curve it solves against is the selected PSK model.
257 if (PSK_TYPE_member == 1 || PSK_TYPE_member == 2) {
258 proteus::richards::psk::bc_invert_newton(m, rho, beta, alpha, n_vg, thetaR, thetaSR, u);
259 } else if (PSK_TYPE_member == 3) {
260 proteus::richards::psk::gardner_invert_newton(m, rho, beta, alpha, n_vg, thetaR, thetaSR, u);
261 } else if (PSK_TYPE_member == 4) {
262 proteus::richards::psk::hm_invert_newton(m, rho, beta, alpha, n_vg, thetaR, thetaSR, u);
263 } else if (VG_AIR_ENTRY_member > 0.0) {
264 // as in evaluateInverseCoefficients; the shift uses rho, not rho exp(beta u) (O(beta) in thetaR)
265 double Sc, Vc;
266 vgAirEntryScales(alpha, n_vg, Sc, Vc);
267 proteus::richards::psk::vgm_invert_newton(Sc * m + (1.0 - Sc) * rho * thetaR, rho, beta, alpha, n_vg, thetaR, thetaSR, u);
268 } else {
269 proteus::richards::psk::vgm_invert_newton(m, rho, beta, alpha, n_vg, thetaR, thetaSR, u);
270 }
271}
272
273 inline void calculateCFL(const double &elementDiameter, const double df[nSpace], double &cfl)
274 {
275 double h, nrm_v;
276 h = elementDiameter;
277 nrm_v = 0.0;
278 for (int I = 0; I < nSpace; I++) nrm_v += df[I] * df[I];
279 nrm_v = sqrt(nrm_v);
280 cfl = nrm_v / h;
281 }
282
283 inline void calculateSubgridError_tau(const double &elementDiameter, const double &dmt, const double dH[nSpace], double &cfl, double &tau)
284 {
285 double h, nrm_v, oneByAbsdt;
286 h = elementDiameter;
287 nrm_v = 0.0;
288 for (int I = 0; I < nSpace; I++) nrm_v += dH[I] * dH[I];
289 nrm_v = sqrt(nrm_v);
290 cfl = nrm_v / h;
291 oneByAbsdt = fabs(dmt);
292 tau = 1.0 / (2.0 * nrm_v / h + oneByAbsdt + 1.0e-8);
293 }
294
295 inline void calculateSubgridError_tau(const double &Ct_sge, const double G[nSpace * nSpace], const double &A0, const double Ai[nSpace], double &tau_v, double &cfl)
296 {
297 double v_d_Gv = 0.0;
298 for (int I = 0; I < nSpace; I++)
299 for (int J = 0; J < nSpace; J++) v_d_Gv += Ai[I] * G[I * nSpace + J] * Ai[J];
300 // Floored like the 1.0e-8 in the elementDiameter version above: in a
301 // saturated region without gravity A0 = dm_t = 0 and Ai = df = 0, and an
302 // infinite tau_v turns useMetrics*tau1 = 0*inf into NaN in the residual.
303 tau_v = 1.0 / sqrt(Ct_sge * A0 * A0 + v_d_Gv + 1.0e-16);
304 }
305
306 inline void calculateNumericalDiffusion(const double &shockCapturingDiffusion, const double &elementDiameter, const double &strong_residual, const double grad_u[nSpace], double &numDiff)
307 {
308 double h, num, den, n_grad_u;
309 h = elementDiameter;
310 n_grad_u = 0.0;
311 for (int I = 0; I < nSpace; I++) n_grad_u += grad_u[I] * grad_u[I];
312 num = shockCapturingDiffusion * 0.5 * h * fabs(strong_residual);
313 den = sqrt(n_grad_u) + 1.0e-8;
314 numDiff = num / den;
315 }
316
317 inline void exteriorNumericalFlux(const double &bc_flux, int rowptr[nSpace], int colind[nnz], int isSeepageFace, int &isDOFBoundary, double n[nSpace], double bc_u, double K[nnz], double grad_psi[nSpace], double u, double K_rho_g[nSpace], double penalty, double &flux)
318 {
319 double v_I, bc_u_seepage = 0.0;
320 if (isSeepageFace || isDOFBoundary) {
321 flux = 0.0;
322 for (int I = 0; I < nSpace; I++) {
323 //gravity
324 v_I = K_rho_g[I];
325 //pressure head
326 for (int m = rowptr[I]; m < rowptr[I + 1]; m++) { v_I -= K[m] * grad_psi[colind[m]]; }
327 flux += v_I * n[I];
328 }
329 if (isSeepageFace) bc_u = bc_u_seepage;
330 flux += penalty * (u - bc_u);
331 //flux -= penalty * bc_u;
332 if (isSeepageFace) {
333 if (flux > 0.0) {
334 isDOFBoundary = 1;
335 bc_u = bc_u_seepage;
336 } else {
337 isDOFBoundary = 0;
338 flux = 0.0;
339 }
340 }
341 } else flux = bc_flux;
342 }
343
344 void exteriorNumericalFluxJacobian(const int rowptr[nSpace], const int colind[nnz], const int isDOFBoundary, const double n[nSpace], const double K[nnz], const double dK[nnz], const double grad_psi[nSpace], const double grad_v[nSpace], const double dK_rho_g[nSpace], const double v, const double penalty, double &fluxJacobian)
345 {
346 if (isDOFBoundary) {
347 fluxJacobian = 0.0;
348 for (int I = 0; I < nSpace; I++) {
349 //gravity
350 fluxJacobian += dK_rho_g[I] * v * n[I];
351 //pressure head
352 for (int m = rowptr[I]; m < rowptr[I + 1]; m++) { fluxJacobian -= (K[m] * grad_v[colind[m]] + dK[m] * v * grad_psi[colind[m]]) * n[I]; }
353 }
354 //Dirichlet penalty
355 fluxJacobian += penalty * v;
356 } else fluxJacobian = 0.0;
357 }
358
359inline void exteriorNumericalFlux2(const double &bc_flux, int rowptr[nSpace], int colind[nnz], int isSeepageFace, int &isDOFBoundary, double n[nSpace], double bc_u, double K[nnz], double grad_psi[nSpace], double u, double K_rho_g[nSpace], double penalty, double &flux, double &bflux)
360 {
361 double v_I, bc_u_seepage = 0.0;
362 if (isSeepageFace || isDOFBoundary) {
363 flux = 0.0;
364 bflux = 0.0;
365 for (int I = 0; I < nSpace; I++) {
366 //gravity
367 v_I = K_rho_g[I];
368 //pressure head
369 for (int m = rowptr[I]; m < rowptr[I + 1]; m++) { v_I -= K[m] * grad_psi[colind[m]]; }
370 flux += v_I * n[I];
371 }
372 if (isSeepageFace) bc_u = bc_u_seepage;
373 flux += penalty * (u - bc_u);
374 bflux += penalty * (u - bc_u);
375 if (isSeepageFace) {
376 if (flux > 0.0) {
377 isDOFBoundary = 1;
378 } else {
379 isDOFBoundary = 0;
380 flux = 0.0;
381 bflux = 0.0;
382 }
383 }
384 } else {
385 flux = bc_flux;
386 bflux = bc_flux;
387 }
388 }
389
390 //cfluxJacobian is the SAME IIPG consistency term as fluxJacobian but built from the
391 //relative-permeability-weighted conductivity K = rho*k_rw*Ks instead of the saturated
392 //Ks. It feeds TransportMatrixConsistent, i.e. the HIGH order flux fH, exactly the way
393 //the cell loop feeds it from `a` while TransportMatrix gets `as`. Without the split the
394 //boundary block enters fH with weight 1 and enters fL multiplied by the nodal upwind
395 //k_rw, so it does not cancel in fA = fH - fL and leaves a spurious antidiffusive flux
396 //~1/k_rw times the interior one at every weakly imposed Dirichlet row.
397 void exteriorNumericalFluxJacobian2(const int rowptr[nSpace], const int colind[nnz], const int isDOFBoundary, const double n[nSpace], const double Ks[nnz], const double K[nnz], const double dK[nnz], const double grad_psi[nSpace], const double grad_v[nSpace], const double dK_rho_g[nSpace], const double v, const double penalty, double &fluxJacobian, double &bfluxJacobian, double &cfluxJacobian)
398 {
399 if (isDOFBoundary) {
400 fluxJacobian = 0.0;
401 bfluxJacobian = 0.0;
402 cfluxJacobian = 0.0;
403 for (int I = 0; I < nSpace; I++) {
404 for (int m = rowptr[I]; m < rowptr[I + 1]; m++) {
405 fluxJacobian -= Ks[m] * grad_v[colind[m]] * n[I];
406 cfluxJacobian -= K[m] * grad_v[colind[m]] * n[I];
407 }
408 }
409 //Dirichlet penalty
410 bfluxJacobian = penalty * v;
411 } else {
412 fluxJacobian = 0.0;
413 bfluxJacobian = 0.0;
414 cfluxJacobian = 0.0;
415 }
416 }
417
418 double seepagefluxcalculator(double anb_seepage_flux, int isSeepageFace, double dS, double flux_ext)
419 {
420 if (isSeepageFace) { anb_seepage_flux += flux_ext * dS; }
421 return anb_seepage_flux;
422 }
423
425 {
426 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
427 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
428 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
429 xt::pyarray<double> &mesh_velocity_dof = args.array<double>("mesh_velocity_dof");
430 double MOVING_DOMAIN = args.scalar<double>("MOVING_DOMAIN");
431 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
432 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
433 xt::pyarray<double> &u_trial_ref = args.array<double>("u_trial_ref");
434 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
435 xt::pyarray<double> &u_test_ref = args.array<double>("u_test_ref");
436 xt::pyarray<double> &u_grad_test_ref = args.array<double>("u_grad_test_ref");
437 xt::pyarray<double> &mesh_trial_trace_ref = args.array<double>("mesh_trial_trace_ref");
438 xt::pyarray<double> &mesh_grad_trial_trace_ref = args.array<double>("mesh_grad_trial_trace_ref");
439 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
440 xt::pyarray<double> &u_trial_trace_ref = args.array<double>("u_trial_trace_ref");
441 xt::pyarray<double> &u_grad_trial_trace_ref = args.array<double>("u_grad_trial_trace_ref");
442 xt::pyarray<double> &u_test_trace_ref = args.array<double>("u_test_trace_ref");
443 xt::pyarray<double> &u_grad_test_trace_ref = args.array<double>("u_grad_test_trace_ref");
444 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
445 xt::pyarray<double> &boundaryJac_ref = args.array<double>("boundaryJac_ref");
446 int nElements_global = args.scalar<int>("nElements_global");
447 xt::pyarray<double> &ebqe_penalty_ext = args.array<double>("ebqe_penalty_ext");
448 xt::pyarray<int> &elementMaterialTypes = args.array<int>("elementMaterialTypes");
449 xt::pyarray<int> &isSeepageFace = args.array<int>("isSeepageFace");
450 xt::pyarray<int> &a_rowptr = args.array<int>("a_rowptr");
451 xt::pyarray<int> &a_colind = args.array<int>("a_colind");
452 double rho = args.scalar<double>("rho");
453 double beta = args.scalar<double>("beta");
454
456 xt::pyarray<double> &q_rho = args.array<double>("q_rho");
457 xt::pyarray<double> &ebqe_rho = args.array<double>("ebqe_rho");
458 // mu/mu_0 from the transport model: the conductivity at a quadrature point is K_s/mu
459 xt::pyarray<double> &q_mu = args.array<double>("q_mu");
460 xt::pyarray<double> &ebqe_mu = args.array<double>("ebqe_mu");
461
462 xt::pyarray<double> &gravity = args.array<double>("gravity");
463 xt::pyarray<double> &alpha = args.array<double>("alpha");
464 xt::pyarray<double> &n = args.array<double>("n");
465 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
466 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
467 PSK_TYPE_member = args.scalar<int>("PSK_TYPE");
468 VG_AIR_ENTRY_member = args.scalar<double>("VG_AIR_ENTRY");
469 xt::pyarray<double> &KWs = args.array<double>("KWs");
470 double useMetrics = args.scalar<double>("useMetrics");
471 double alphaBDF = args.scalar<double>("alphaBDF");
472 int lag_shockCapturing = args.scalar<int>("lag_shockCapturing");
473 double shockCapturingDiffusion = args.scalar<double>("shockCapturingDiffusion");
474 double sc_uref = args.scalar<double>("sc_uref");
475 double sc_alpha = args.scalar<double>("sc_alpha");
476 xt::pyarray<int> &u_l2g = args.array<int>("u_l2g");
477 xt::pyarray<double> &elementDiameter = args.array<double>("elementDiameter");
478 xt::pyarray<double> &u_dof = args.array<double>("u_dof");
479 xt::pyarray<double> &u_dof_old = args.array<double>("u_dof_old");
480 xt::pyarray<double> &velocity = args.array<double>("velocity");
481 xt::pyarray<double> &q_m = args.array<double>("q_m");
482 xt::pyarray<double> &q_theta = args.array<double>("q_theta");
483 xt::pyarray<double> &q_u = args.array<double>("q_u");
484 xt::pyarray<double> &q_dV = args.array<double>("q_dV");
485 xt::pyarray<double> &q_m_betaBDF = args.array<double>("q_m_betaBDF");
486 xt::pyarray<double> &cfl = args.array<double>("cfl");
487 xt::pyarray<double> &q_numDiff_u = args.array<double>("q_numDiff_u");
488 xt::pyarray<double> &q_numDiff_u_last = args.array<double>("q_numDiff_u_last");
489 int offset_u = args.scalar<int>("offset_u");
490 int stride_u = args.scalar<int>("stride_u");
491 xt::pyarray<double> &globalResidual = args.array<double>("globalResidual");
492 int nExteriorElementBoundaries_global = args.scalar<int>("nExteriorElementBoundaries_global");
493 xt::pyarray<int> &exteriorElementBoundariesArray = args.array<int>("exteriorElementBoundariesArray");
494 xt::pyarray<int> &elementBoundaryElementsArray = args.array<int>("elementBoundaryElementsArray");
495 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray = args.array<int>("elementBoundaryLocalElementBoundariesArray");
496 xt::pyarray<double> &ebqe_velocity_ext = args.array<double>("ebqe_velocity_ext");
497 xt::pyarray<int> &isDOFBoundary_u = args.array<int>("isDOFBoundary_u");
498 xt::pyarray<double> &ebqe_bc_u_ext = args.array<double>("ebqe_bc_u_ext");
499 xt::pyarray<int> &isFluxBoundary_u = args.array<int>("isFluxBoundary_u");
500 xt::pyarray<double> &ebqe_bc_flux_ext = args.array<double>("ebqe_bc_flux_ext");
501 xt::pyarray<double> &ebqe_phi = args.array<double>("ebqe_phi");
502 double epsFact = args.scalar<double>("epsFact");
503 xt::pyarray<double> &ebqe_u = args.array<double>("ebqe_u");
504 xt::pyarray<double> &ebqe_theta = args.array<double>("ebqe_theta");
505 xt::pyarray<double> &ebqe_flux = args.array<double>("ebqe_flux");
506 // VMS
507 double VMS = args.scalar<double>("VMS");
508 // Time-weighting of the flux terms (ThetaScheme): 1 = backward Euler,
509 // 1/2 = Crank-Nicolson. The mass term is always (m - m^n)/dt.
510 const double Theta = args.scalar<double>("Theta");
511 // PARAMETERS FOR EDGE BASED STABILIZATION
512 double cE = args.scalar<double>("cE");
513 double cK = args.scalar<double>("cK");
514 // PARAMETERS FOR LOG BASED ENTROPY FUNCTION
515 double uL = args.scalar<double>("uL");
516 double uR = args.scalar<double>("uR");
517 // PARAMETERS FOR EDGE VISCOSITY
518 int numDOFs = args.scalar<int>("numDOFs");
519 int NNZ = args.scalar<int>("NNZ");
520 xt::pyarray<int> &csrRowIndeces_DofLoops = args.array<int>("csrRowIndeces_DofLoops");
521 xt::pyarray<int> &csrColumnOffsets_DofLoops = args.array<int>("csrColumnOffsets_DofLoops");
522 xt::pyarray<int> &csrRowIndeces_CellLoops = args.array<int>("csrRowIndeces_CellLoops");
523 xt::pyarray<int> &csrColumnOffsets_CellLoops = args.array<int>("csrColumnOffsets_CellLoops");
524 xt::pyarray<int> &csrColumnOffsets_eb_CellLoops = args.array<int>("csrColumnOffsets_eb_CellLoops");
525 // C matrices
526 xt::pyarray<double> &Cx = args.array<double>("Cx");
527 xt::pyarray<double> &Cy = args.array<double>("Cy");
528 xt::pyarray<double> &Cz = args.array<double>("Cz");
529 xt::pyarray<double> &CTx = args.array<double>("CTx");
530 xt::pyarray<double> &CTy = args.array<double>("CTy");
531 xt::pyarray<double> &CTz = args.array<double>("CTz");
532 xt::pyarray<double> &ML = args.array<double>("ML");
533 xt::pyarray<double> &delta_x_ij = args.array<double>("delta_x_ij");
534 // PARAMETERS FOR 1st or 2nd ORDER MPP METHOD
535 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
536 STABILIZATION STABILIZATION_TYPE{static_cast<STABILIZATION>(args.scalar<int>("STABILIZATION_TYPE"))};
537 int ENTROPY_TYPE = args.scalar<int>("ENTROPY_TYPE");
538 // FOR FCT
539 xt::pyarray<double> &dLow = args.array<double>("dLow");
540 xt::pyarray<double> &fluxMatrix = args.array<double>("fluxMatrix");
541 // AUX QUANTITIES OF INTEREST
542 xt::pyarray<double> &quantDOFs = args.array<double>("quantDOFs");
543
544 assert(a_rowptr.data()[nSpace] == nnz);
545 assert(a_rowptr.data()[nSpace] == nSpace);
546 //cek should this be read in?
547 double Ct_sge = 4.0;
548
549 xt::pyarray<double> &anb_seepage_flux_n = args.array<double>("anb_seepage_flux_n");
550
551 xt::pyarray<double> &velocity_couple = args.array<double>("velocity_couple");
552 xt::pyarray<double> &ebqe_velocity_ext_couple = args.array<double>("ebqe_velocity_ext_couple");
553
554 // xt::pyarray<double> &q_x = args.array<double>("q_x");
555 // xt::pyarray<double> &ebqe_x = args.array<double>("ebqe_x");
556
557 //double anb_seepage_flux=0.0;
558 double &anb_seepage_flux(args.scalar<double>("anb_seepage_flux"));
559 xt::pyarray<double> &q_velocity = args.array<double>("q_velocity");
560 anb_seepage_flux = 0.0;
561
562 //loop over elements to compute volume integrals and load them into element and global residual
563 //
564 //eN is the element index
565 //eN_k is the quadrature point index for a scalar
566 //eN_k_nSpace is the quadrature point index for a vector
567 //eN_i is the element test function index
568 //eN_j is the element trial function index
569 //eN_k_j is the quadrature point index for a trial function
570 //eN_k_i is the quadrature point index for a trial function
571 for (int eN = 0; eN < nElements_global; eN++) {
572 //declare local storage for element residual and initialize
573 double elementResidual_u[nDOF_test_element];
574 for (int i = 0; i < nDOF_test_element; i++) { elementResidual_u[i] = 0.0; } //i
575 //loop over quadrature points and compute integrands
576 for (int k = 0; k < nQuadraturePoints_element; k++) {
577 //compute indeces and declare local storage
578 int eN_k = eN * nQuadraturePoints_element + k, eN_k_nSpace = eN_k * nSpace, eN_nDOF_trial_element = eN * nDOF_trial_element;
579 double u = 0.0, grad_u[nSpace], grad_u_old[nSpace], m = 0.0, dm = 0.0, f[nSpace], df[nSpace], a[nnz], da[nnz], as[nnz], m_t = 0.0, dm_t = 0.0, pdeResidual_u = 0.0, Lstar_u[nDOF_test_element], subgridError_u = 0.0, tau = 0.0, tau0 = 0.0, tau1 = 0.0, numDiff0 = 0.0, numDiff1 = 0.0, jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace], u_grad_trial[nDOF_trial_element * nSpace], u_test_dV[nDOF_trial_element], u_grad_test_dV[nDOF_test_element * nSpace], dV, x, y, z, xt, yt, zt, G[nSpace * nSpace], G_dd_G, tr_G, norm_Rv;
580 //
581 //compute solution and gradients at quadrature points
582 //
583 ck.calculateMapping_element(eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y, z);
584 ck.calculateMappingVelocity_element(eN, k, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), xt, yt, zt);
585 //get the physical integration weight
586 dV = fabs(jacDet) * dV_ref.data()[k];
587 q_dV.data()[eN_k] = dV;
588 ck.calculateG(jacInv, G, G_dd_G, tr_G);
589 //get the trial function gradients
590 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
591 //get the solution
592 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_ref.data()[k * nDOF_trial_element], u);
593 //get the solution gradients
594 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial, grad_u);
595 //old-time solution and gradient for the (1-Theta) flux terms
596 double un = 0.0;
597 ck.valFromDOF(u_dof_old.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_ref.data()[k * nDOF_trial_element], un);
598 ck.gradFromDOF(u_dof_old.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial, grad_u_old);
599
600 // //populate q_x
601 // const int eN_k_3d = eN_k * 3;
602 // q_x.data()[eN_k_3d + 0] = x;
603 // q_x.data()[eN_k_3d + 1] = y;
604 // q_x.data()[eN_k_3d + 2] = z;
605
606 //precalculate test function products with integration weights
607 for (int j = 0; j < nDOF_trial_element; j++) {
608 u_test_dV[j] = u_test_ref.data()[k * nDOF_trial_element + j] * dV;
609 for (int I = 0; I < nSpace; I++) {
610 u_grad_test_dV[j * nSpace + I] = u_grad_trial[j * nSpace + I] * dV; //cek warning won't work for Petrov-Galerkin
611 }
612 }
613 //
614 //calculate pde coefficients at quadrature points
615 //
616 double Kr, dKr, thetaW;
617 const double rho_local = q_rho.data()[eN_k];
618 const double rho_velocity = std::fabs(rho_local) > 1.0e-12 ? rho_local : rho;
619 double KWs_mu[nnz];
620 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes.data()[eN] * nnz + ii] / q_mu.data()[eN_k];
621 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_local, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
622 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, u, m, dm, f, df, a, da, as, Kr, dKr, thetaW);
623 q_theta.data()[eN_k] = thetaW;
624 double mn_q, dmn_q, fn[nSpace], dfn[nSpace], an[nnz], dan[nnz], asn[nnz], Krn, dKrn, thetaWn;
625 if (Theta < 1.0) {
626 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_local, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
627 thetaSR.data()[elementMaterialTypes.data()[eN]], &KWs.data()[elementMaterialTypes.data()[eN] * nnz], un, mn_q, dmn_q, fn, dfn, an, dan, asn, Krn, dKrn, thetaWn);
628 }
629
630 for (int I = 0; I < nSpace; ++I) {
631 q_velocity.data()[eN_k_nSpace + I] = grad_u[I];
632 }
633 // Darcy Velocity
634 double pressure_gradient[nSpace];
635 const double rho_ratio = rho_velocity / rho;
636 for (int J=0; J<nSpace; ++J)
637 pressure_gradient[J] = grad_u[J] - rho_ratio * gravity.data()[J];
638 // for each row I, acc = sum_j (a_{Ij}/rho) * gp[j]
639 for (int I=0; I<nSpace; ++I) {
640 double acc = 0.0;
641 for (int ii = a_rowptr.data()[I]; ii < a_rowptr.data()[I+1]; ++ii) {
642 const int J = a_colind.data()[ii];
643 acc += (a[ii] / rho_velocity) * pressure_gradient[J];
644 }
645 velocity.data()[eN_k_nSpace + I] = -acc;
646 velocity_couple.data()[eN_k_nSpace + I] = -acc ;
647 }
648 //
649 //calculate time derivative at quadrature points
650 //
651 ck.bdf(alphaBDF, q_m_betaBDF.data()[eN_k], m, dm, m_t, dm_t);
652 //
653 //calculate subgrid error (strong residual and adjoint)
654 //
655 //calculate strong residual
656 pdeResidual_u = ck.Mass_strong(m_t) + ck.Advection_strong(df, grad_u);
657 //calculate adjoint
658 for (int i = 0; i < nDOF_test_element; i++) {
659 int i_nSpace = i * nSpace;
660 Lstar_u[i] = ck.Advection_adjoint(df, &u_grad_test_dV[i_nSpace]);
661 }
662 //calculate tau and tau*Res
663 calculateSubgridError_tau(elementDiameter[eN], dm_t, df, cfl[eN_k], tau0);
664 calculateSubgridError_tau(Ct_sge, G, dm_t, df, tau1, cfl[eN_k]);
665
666 tau = useMetrics * tau1 + (1.0 - useMetrics) * tau0;
667
668 subgridError_u = -tau * pdeResidual_u;
669 // //
670 // //calculate shock capturing diffusion
671 // //
672 // ck.calculateNumericalDiffusion(shockCapturingDiffusion, elementDiameter[eN], pdeResidual_u, grad_u, numDiff0);
673 // ck.calculateNumericalDiffusion(shockCapturingDiffusion, sc_uref, sc_alpha, G, G_dd_G, pdeResidual_u, grad_u, numDiff1);
674 // q_numDiff_u[eN_k] = useMetrics * numDiff1 + (1.0 - useMetrics) * numDiff0;
675 //
676 //update element residual
677 //
678 for (int i = 0; i < nDOF_test_element; i++) {
679 int eN_k_i = eN_k * nDOF_test_element + i, eN_k_i_nSpace = eN_k_i * nSpace, i_nSpace = i * nSpace;
680
681 elementResidual_u[i] += ck.Mass_weak(m_t, u_test_dV[i]) + Theta * (ck.Advection_weak(f, &u_grad_test_dV[i_nSpace]) + ck.Diffusion_weak(a_rowptr.data(), a_colind.data(), a, grad_u, &u_grad_test_dV[i_nSpace])) + VMS * ck.SubgridError(subgridError_u, Lstar_u[i]) + VMS * ck.NumericalDiffusion(q_numDiff_u_last[eN_k], grad_u, &u_grad_test_dV[i_nSpace]);
682 if (Theta < 1.0)
683 elementResidual_u[i] += (1.0 - Theta) * (ck.Advection_weak(fn, &u_grad_test_dV[i_nSpace]) + ck.Diffusion_weak(a_rowptr.data(), a_colind.data(), an, grad_u_old, &u_grad_test_dV[i_nSpace]));
684 } //i
685 //
686 q_m.data()[eN_k] = m;
687 q_u.data()[eN_k] = u;
688 }
689 //
690 //load element into global residual and save element residual
691 //
692 for (int i = 0; i < nDOF_test_element; i++) {
693 int eN_i = eN * nDOF_test_element + i;
694
695 globalResidual.data()[offset_u + stride_u * u_l2g.data()[eN_i]] += elementResidual_u[i];
696 } //i
697 } //elements
698 //
699 //loop over exterior element boundaries to calculate surface integrals and load into element and global residuals
700 //
701 //ebNE is the Exterior element boundary INdex
702 //ebN is the element boundary INdex
703 //eN is the element index
704 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++) {
705 int ebN = exteriorElementBoundariesArray.data()[ebNE], eN = elementBoundaryElementsArray.data()[ebN * 2 + 0], ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + 0], eN_nDOF_trial_element = eN * nDOF_trial_element;
706 double elementResidual_u[nDOF_test_element];
707 for (int i = 0; i < nDOF_test_element; i++) { elementResidual_u[i] = 0.0; }
708 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++) {
709 int ebNE_kb = ebNE * nQuadraturePoints_elementBoundary + kb, ebNE_kb_nSpace = ebNE_kb * nSpace, ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb, ebN_local_kb_nSpace = ebN_local_kb * nSpace;
710 double u_ext = 0.0, grad_u_ext[nSpace], m_ext = 0.0, dm_ext = 0.0, f_ext[nSpace], df_ext[nSpace], a_ext[nnz], da_ext[nnz], as_ext[nnz], flux_ext = 0.0,
711 //anb_seepage_flux=0.0, // for flux calculation
712 bc_u_ext = 0.0, bc_grad_u_ext[nSpace], bc_m_ext = 0.0, bc_dm_ext = 0.0, bc_f_ext[nSpace], bc_df_ext[nSpace], bc_a_ext[nnz], bc_da_ext[nnz], bc_as_ext[nnz], jac_ext[nSpace * nSpace], jacDet_ext, jacInv_ext[nSpace * nSpace], boundaryJac[nSpace * (nSpace - 1)], metricTensor[(nSpace - 1) * (nSpace - 1)], metricTensorDetSqrt, dS, u_test_dS[nDOF_test_element], u_grad_trial_trace[nDOF_trial_element * nSpace], normal[3], x_ext, y_ext, z_ext, xt_ext, yt_ext, zt_ext, integralScaling, G[nSpace * nSpace], G_dd_G, tr_G;
713 //
714 //calculate the solution and gradients at quadrature points
715 //
716 //compute information about mapping from reference element to physical element
717 ck.calculateMapping_elementBoundary(eN, ebN_local, kb, ebN_local_kb, mesh_dof.data(), mesh_l2g.data(), mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(), boundaryJac_ref.data(), jac_ext, jacDet_ext, jacInv_ext, boundaryJac, metricTensor, metricTensorDetSqrt,
718 normal_ref.data(), normal, x_ext, y_ext, z_ext);
719 ck.calculateMappingVelocity_elementBoundary(eN, ebN_local, kb, ebN_local_kb, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_trace_ref.data(), xt_ext, yt_ext, zt_ext, normal, boundaryJac, metricTensor, integralScaling);
720 dS = ((1.0 - MOVING_DOMAIN) * metricTensorDetSqrt + MOVING_DOMAIN * integralScaling) * dS_ref.data()[kb];
721 //get the metric tensor
722 //cek todo use symmetry
723 ck.calculateG(jacInv_ext, G, G_dd_G, tr_G);
724 //compute shape and solution information
725 //shape
726 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_ext, u_grad_trial_trace);
727 //solution and gradient
728 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);
729 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial_trace, grad_u_ext);
730
731 //populate ebqe_x
732 // const int ebNE_kb_3d = ebNE_kb * 3;
733 // ebqe_x.data()[ebNE_kb_3d + 0] = x_ext;
734 // ebqe_x.data()[ebNE_kb_3d + 1] = y_ext;
735 // ebqe_x.data()[ebNE_kb_3d + 2] = z_ext;
736
737 //precalculate test function products with integration weights
738 for (int j = 0; j < nDOF_trial_element; j++) { u_test_dS[j] = u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + j] * dS; }
739 //
740 //load the boundary values
741 //
742 bc_u_ext = isDOFBoundary_u.data()[ebNE_kb] * ebqe_bc_u_ext.data()[ebNE_kb] + (1 - isDOFBoundary_u.data()[ebNE_kb]) * u_ext;
743 //
744 //calculate the pde coefficients using the solution and the boundary values for the solution
745 //
746 const double rho_ext = ebqe_rho.data()[ebNE_kb];
747 const double rho_velocity_ext = std::fabs(rho_ext) > 1.0e-12 ? rho_ext : rho;
748 double Kr, dKr, thetaW_ext, thetaW_bc;
749 double KWs_mu[nnz];
750 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes.data()[eN] * nnz + ii] / ebqe_mu.data()[ebNE_kb];
751 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
752 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, u_ext, m_ext, dm_ext, f_ext, df_ext, a_ext, da_ext, as_ext, Kr, dKr, thetaW_ext);
753 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
754 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, bc_u_ext, bc_m_ext, bc_dm_ext, bc_f_ext, bc_df_ext, bc_a_ext, bc_da_ext, bc_as_ext, Kr, dKr, thetaW_bc);
755 ebqe_theta.data()[ebNE_kb] = thetaW_ext;
756
757 //
758 //Calculate Darcy velocity on exterior face : v_ext = -(a_ext/rho) * (grad_u_ext + gravity) ---
759 //
760 double ext_pressure_gradient[nSpace];
761 const double rho_ratio_ext = rho_velocity_ext / rho;
762 for (int J=0; J<nSpace; ++J)
763 ext_pressure_gradient[J] = grad_u_ext[J] - rho_ratio_ext * gravity.data()[J];
764
765 for (int I=0; I<nSpace; ++I) {
766 double acc = 0.0;
767 for (int ii = a_rowptr.data()[I]; ii < a_rowptr.data()[I+1]; ++ii) {
768 const int J = a_colind.data()[ii];
769 acc += (a_ext[ii] / rho_velocity_ext) * ext_pressure_gradient[J];
770 }
771 ebqe_velocity_ext.data()[ebNE_kb_nSpace + I] = -acc;
772 ebqe_velocity_ext_couple.data()[ebNE_kb_nSpace + I] = -acc ; // store vector at this boundary qp
773 }
774
775
776 //
777 //calculate the numerical fluxes
778 //
779 exteriorNumericalFlux(ebqe_bc_flux_ext[ebNE_kb], a_rowptr.data(), a_colind.data(),
780 isSeepageFace.data()[ebNE], //tricky, this is a face flag not face quad
781 isDOFBoundary_u.data()[ebNE_kb], normal, bc_u_ext, a_ext, grad_u_ext, u_ext, f_ext,
782 ebqe_penalty_ext.data()[ebNE_kb], // penalty,
783 flux_ext);
784 ebqe_flux.data()[ebNE_kb] = flux_ext;
785
786 anb_seepage_flux = seepagefluxcalculator(anb_seepage_flux, isSeepageFace.data()[ebNE], dS, flux_ext);
787 anb_seepage_flux_n.data()[0] = anb_seepage_flux;
788 ebqe_u.data()[ebNE_kb] = u_ext;
789 //
790 //update residuals
791 //
792 for (int i = 0; i < nDOF_test_element; i++) {
793 elementResidual_u[i] += ck.ExteriorElementBoundaryFlux(flux_ext, u_test_dS[i]);
794 } //i
795 } //kb
796
797 //
798 //update the element and global residual storage
799 //
800 for (int i = 0; i < nDOF_test_element; i++) {
801 int eN_i = eN * nDOF_test_element + i;
802 globalResidual.data()[offset_u + stride_u * u_l2g.data()[eN_i]] += elementResidual_u[i];
803 } //i
804 } //ebNE
805 }
806
808 {
809 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
810 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
811 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
812 xt::pyarray<double> &mesh_velocity_dof = args.array<double>("mesh_velocity_dof");
813 double MOVING_DOMAIN = args.scalar<double>("MOVING_DOMAIN");
814 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
815 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
816 xt::pyarray<double> &u_trial_ref = args.array<double>("u_trial_ref");
817 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
818 xt::pyarray<double> &u_test_ref = args.array<double>("u_test_ref");
819 xt::pyarray<double> &u_grad_test_ref = args.array<double>("u_grad_test_ref");
820 xt::pyarray<double> &mesh_trial_trace_ref = args.array<double>("mesh_trial_trace_ref");
821 xt::pyarray<double> &mesh_grad_trial_trace_ref = args.array<double>("mesh_grad_trial_trace_ref");
822 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
823 xt::pyarray<double> &u_trial_trace_ref = args.array<double>("u_trial_trace_ref");
824 xt::pyarray<double> &u_grad_trial_trace_ref = args.array<double>("u_grad_trial_trace_ref");
825 xt::pyarray<double> &u_test_trace_ref = args.array<double>("u_test_trace_ref");
826 xt::pyarray<double> &u_grad_test_trace_ref = args.array<double>("u_grad_test_trace_ref");
827 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
828 xt::pyarray<double> &boundaryJac_ref = args.array<double>("boundaryJac_ref");
829 int nElements_global = args.scalar<int>("nElements_global");
830 xt::pyarray<double> &ebqe_penalty_ext = args.array<double>("ebqe_penalty_ext");
831 xt::pyarray<int> &elementMaterialTypes = args.array<int>("elementMaterialTypes");
832 xt::pyarray<int> &isSeepageFace = args.array<int>("isSeepageFace");
833 xt::pyarray<int> &a_rowptr = args.array<int>("a_rowptr");
834 xt::pyarray<int> &a_colind = args.array<int>("a_colind");
835 double rho = args.scalar<double>("rho");
836 double beta = args.scalar<double>("beta");
837
839 xt::pyarray<double> &q_rho = args.array<double>("q_rho");
840 xt::pyarray<double> &ebqe_rho = args.array<double>("ebqe_rho");
841 xt::pyarray<double> &q_mu = args.array<double>("q_mu");
842 xt::pyarray<double> &ebqe_mu = args.array<double>("ebqe_mu");
844
845 xt::pyarray<double> &gravity = args.array<double>("gravity");
846 xt::pyarray<double> &alpha = args.array<double>("alpha");
847 xt::pyarray<double> &n = args.array<double>("n");
848 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
849 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
850 PSK_TYPE_member = args.scalar<int>("PSK_TYPE");
851 VG_AIR_ENTRY_member = args.scalar<double>("VG_AIR_ENTRY");
852 xt::pyarray<double> &KWs = args.array<double>("KWs");
853 double useMetrics = args.scalar<double>("useMetrics");
854 double alphaBDF = args.scalar<double>("alphaBDF");
855 int lag_shockCapturing = args.scalar<int>("lag_shockCapturing");
856 double shockCapturingDiffusion = args.scalar<double>("shockCapturingDiffusion");
857 // VMS
858 double VMS = args.scalar<double>("VMS");
859 const double Theta = args.scalar<double>("Theta"); // see calculateResidual
860 xt::pyarray<int> &u_l2g = args.array<int>("u_l2g");
861 xt::pyarray<double> &elementDiameter = args.array<double>("elementDiameter");
862 xt::pyarray<double> &u_dof = args.array<double>("u_dof");
863 xt::pyarray<double> &velocity = args.array<double>("velocity");
864 xt::pyarray<double> &q_m_betaBDF = args.array<double>("q_m_betaBDF");
865 xt::pyarray<double> &cfl = args.array<double>("cfl");
866 xt::pyarray<double> &q_numDiff_u = args.array<double>("q_numDiff_u");
867 xt::pyarray<double> &q_numDiff_u_last = args.array<double>("q_numDiff_u_last");
868 xt::pyarray<int> &csrRowIndeces_u_u = args.array<int>("csrRowIndeces_u_u");
869 xt::pyarray<int> &csrColumnOffsets_u_u = args.array<int>("csrColumnOffsets_u_u");
870 xt::pyarray<double> &globalJacobian = args.array<double>("globalJacobian");
871 int nExteriorElementBoundaries_global = args.scalar<int>("nExteriorElementBoundaries_global");
872 xt::pyarray<int> &exteriorElementBoundariesArray = args.array<int>("exteriorElementBoundariesArray");
873 xt::pyarray<int> &elementBoundaryElementsArray = args.array<int>("elementBoundaryElementsArray");
874 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray = args.array<int>("elementBoundaryLocalElementBoundariesArray");
875 xt::pyarray<double> &ebqe_velocity_ext = args.array<double>("ebqe_velocity_ext");
876 xt::pyarray<int> &isDOFBoundary_u = args.array<int>("isDOFBoundary_u");
877 xt::pyarray<double> &ebqe_bc_u_ext = args.array<double>("ebqe_bc_u_ext");
878 xt::pyarray<int> &isFluxBoundary_u = args.array<int>("isFluxBoundary_u");
879 xt::pyarray<double> &ebqe_bc_flux_ext = args.array<double>("ebqe_bc_flux_ext");
880 xt::pyarray<int> &csrColumnOffsets_eb_u_u = args.array<int>("csrColumnOffsets_eb_u_u");
881 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
882 assert(a_rowptr.data()[nSpace] == nnz);
883 assert(a_rowptr.data()[nSpace] == nSpace);
884 double Ct_sge = 4.0;
885
886 //
887 //loop over elements to compute volume integrals and load them into the element Jacobians and global Jacobian
888 //
889 for (int eN = 0; eN < nElements_global; eN++) {
890 double elementJacobian_u_u[nDOF_test_element][nDOF_trial_element];
891 for (int i = 0; i < nDOF_test_element; i++) {
892 for (int j = 0; j < nDOF_trial_element; j++) { elementJacobian_u_u[i][j] = 0.0; }
893 }
894 for (int k = 0; k < nQuadraturePoints_element; k++) {
895 int eN_k = eN * nQuadraturePoints_element + k, //index to a scalar at a quadrature point
896 eN_k_nSpace = eN_k * nSpace,
897 eN_nDOF_trial_element = eN * nDOF_trial_element; //index to a vector at a quadrature point
898
899 //declare local storage
900 double u = 0.0, grad_u[nSpace], m = 0.0, dm = 0.0, f[nSpace], df[nSpace], a[nnz], da[nnz], as[nnz], m_t = 0.0, dm_t = 0.0, dpdeResidual_u_u[nDOF_trial_element], Lstar_u[nDOF_test_element], dsubgridError_u_u[nDOF_trial_element], tau = 0.0, tau0 = 0.0, tau1 = 0.0, jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace], u_grad_trial[nDOF_trial_element * nSpace], dV, u_test_dV[nDOF_test_element], u_grad_test_dV[nDOF_test_element * nSpace], x, y, z, xt, yt, zt, G[nSpace * nSpace], G_dd_G, tr_G;
901 //
902 //calculate solution and gradients at quadrature points
903 //
904 //get jacobian, etc for mapping reference element
905 ck.calculateMapping_element(eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y, z);
906 ck.calculateMappingVelocity_element(eN, k, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), xt, yt, zt);
907 //get the physical integration weight
908 dV = fabs(jacDet) * dV_ref.data()[k];
909 ck.calculateG(jacInv, G, G_dd_G, tr_G);
910 //get the trial function gradients
911 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
912 //get the solution
913 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_ref.data()[k * nDOF_trial_element], u);
914 //get the solution gradients
915 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial, grad_u);
916 //precalculate test function products with integration weights
917 for (int j = 0; j < nDOF_trial_element; j++) {
918 u_test_dV[j] = u_test_ref.data()[k * nDOF_trial_element + j] * dV;
919 for (int I = 0; I < nSpace; I++) {
920 u_grad_test_dV[j * nSpace + I] = u_grad_trial[j * nSpace + I] * dV; //cek warning won't work for Petrov-Galerkin
921 }
922 }
923 //
924 //calculate pde coefficients and derivatives at quadrature points
925 //
926 double Kr, dKr, thetaW;
927 //const double rho_local = q_rho.data()[eN_k];
928 double KWs_mu[nnz];
929 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes.data()[eN] * nnz + ii] / q_mu.data()[eN_k];
930
931 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, q_rho.data()[eN_k], beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
932 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, u, m, dm, f, df, a, da, as, Kr, dKr, thetaW);
933 //
934 //calculate time derivatives
935 //
936 ck.bdf(alphaBDF, q_m_betaBDF.data()[eN_k], m, dm, m_t, dm_t);
937 //
938 //calculate subgrid error contribution to the Jacobian (strong residual, adjoint, jacobian of strong residual)
939 //
940 //calculate the adjoint times the test functions
941 for (int i = 0; i < nDOF_test_element; i++) {
942 int i_nSpace = i * nSpace;
943 Lstar_u[i] = ck.Advection_adjoint(df, &u_grad_test_dV[i_nSpace]);
944 }
945 //calculate the Jacobian of strong residual
946 for (int j = 0; j < nDOF_trial_element; j++) {
947 int j_nSpace = j * nSpace;
948 dpdeResidual_u_u[j] = ck.MassJacobian_strong(dm_t, u_trial_ref[k * nDOF_trial_element + j]) + ck.AdvectionJacobian_strong(df, &u_grad_trial[j_nSpace]);
949 }
950 //tau and tau*Res
951 calculateSubgridError_tau(elementDiameter[eN], dm_t, df, cfl[eN_k], tau0);
952 calculateSubgridError_tau(Ct_sge, G, dm_t, df, tau1, cfl[eN_k]);
953 tau = useMetrics * tau1 + (1.0 - useMetrics) * tau0;
954 for (int j = 0; j < nDOF_trial_element; j++) dsubgridError_u_u[j] = -tau * dpdeResidual_u_u[j];
955 for (int i = 0; i < nDOF_test_element; i++) {
956 for (int j = 0; j < nDOF_trial_element; j++) {
957 int j_nSpace = j * nSpace;
958 int i_nSpace = i * nSpace;
959 elementJacobian_u_u[i][j] += ck.MassJacobian_weak(dm_t, u_trial_ref.data()[k * nDOF_trial_element + j], u_test_dV[i]) + Theta * (ck.AdvectionJacobian_weak(df, u_trial_ref.data()[k * nDOF_trial_element + j], &u_grad_test_dV[i_nSpace]) +
960 ck.DiffusionJacobian_weak(a_rowptr.data(), a_colind.data(), a, da, grad_u, &u_grad_test_dV[i_nSpace], 1.0, u_trial_ref.data()[k * nDOF_trial_element + j], &u_grad_trial[j_nSpace])) + VMS * ck.SubgridErrorJacobian(dsubgridError_u_u[j], Lstar_u[i]) + VMS * ck.NumericalDiffusionJacobian(q_numDiff_u_last[eN_k], &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]);
961 } //j
962 } //i
963 } //k
964 //
965 //load into element Jacobian into global Jacobian
966 //
967 for (int i = 0; i < nDOF_test_element; i++) {
968 int eN_i = eN * nDOF_test_element + i;
969 for (int j = 0; j < nDOF_trial_element; j++) {
970 int eN_i_j = eN_i * nDOF_trial_element + j;
971 globalJacobian.data()[csrRowIndeces_u_u[eN_i] + csrColumnOffsets_u_u[eN_i_j]] += elementJacobian_u_u[i][j];
972 } //j
973 } //i
974 } //elements
975 //
976 //loop over exterior element boundaries to compute the surface integrals and load them into the global Jacobian
977 //
978 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++) {
979 int ebN = exteriorElementBoundariesArray.data()[ebNE];
980 int eN = elementBoundaryElementsArray.data()[ebN * 2 + 0], ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + 0], eN_nDOF_trial_element = eN * nDOF_trial_element;
981 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++) {
982 int ebNE_kb = ebNE * nQuadraturePoints_elementBoundary + kb, ebNE_kb_nSpace = ebNE_kb * nSpace, ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb, ebN_local_kb_nSpace = ebN_local_kb * nSpace;
983
984 double u_ext = 0.0, grad_u_ext[nSpace], m_ext = 0.0, dm_ext = 0.0, f_ext[nSpace], df_ext[nSpace], a_ext[nnz], da_ext[nnz], as_ext[nnz], dflux_u_u_ext = 0.0, bc_u_ext = 0.0,
985 //bc_grad_u_ext[nSpace],
986 bc_m_ext = 0.0, bc_dm_ext = 0.0, bc_f_ext[nSpace], bc_df_ext[nSpace], bc_a_ext[nnz], bc_da_ext[nnz], bc_as_ext[nnz], fluxJacobian_u_u[nDOF_trial_element], jac_ext[nSpace * nSpace], jacDet_ext, jacInv_ext[nSpace * nSpace], boundaryJac[nSpace * (nSpace - 1)], metricTensor[(nSpace - 1) * (nSpace - 1)], metricTensorDetSqrt, dS, u_test_dS[nDOF_test_element], u_grad_trial_trace[nDOF_trial_element * nSpace], normal[3], x_ext, y_ext, z_ext, xt_ext, yt_ext, zt_ext, integralScaling, G[nSpace * nSpace], G_dd_G, tr_G;
987 //
988 //calculate the solution and gradients at quadrature points
989 //
990 ck.calculateMapping_elementBoundary(eN, ebN_local, kb, ebN_local_kb, mesh_dof.data(), mesh_l2g.data(), mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(), boundaryJac_ref.data(), jac_ext, jacDet_ext, jacInv_ext, boundaryJac, metricTensor, metricTensorDetSqrt,
991 normal_ref.data(), normal, x_ext, y_ext, z_ext);
992 ck.calculateMappingVelocity_elementBoundary(eN, ebN_local, kb, ebN_local_kb, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_trace_ref.data(), xt_ext, yt_ext, zt_ext, normal, boundaryJac, metricTensor, integralScaling);
993 dS = ((1.0 - MOVING_DOMAIN) * metricTensorDetSqrt + MOVING_DOMAIN * integralScaling) * dS_ref.data()[kb];
994 ck.calculateG(jacInv_ext, G, G_dd_G, tr_G);
995 //compute shape and solution information
996 //shape
997 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_ext, u_grad_trial_trace);
998 //solution and gradients
999 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);
1000 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial_trace, grad_u_ext);
1001 //precalculate test function products with integration weights
1002 for (int j = 0; j < nDOF_trial_element; j++) { u_test_dS[j] = u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + j] * dS; }
1003 //
1004 //load the boundary values
1005 //
1006 bc_u_ext = isDOFBoundary_u.data()[ebNE_kb] * ebqe_bc_u_ext.data()[ebNE_kb] + (1 - isDOFBoundary_u.data()[ebNE_kb]) * u_ext;
1007 //
1008 //calculate the internal and external trace of the pde coefficients
1009 //
1010 double Kr, dKr, thetaW, thetaW_bc;
1011 const double rho_ext = ebqe_rho.data()[ebNE_kb];
1012 double KWs_mu[nnz];
1013 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes.data()[eN] * nnz + ii] / ebqe_mu.data()[ebNE_kb];
1014
1015 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
1016 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, u_ext, m_ext, dm_ext, f_ext, df_ext, a_ext, da_ext, as_ext, Kr, dKr, thetaW);
1017 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
1018 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, bc_u_ext, bc_m_ext, bc_dm_ext, bc_f_ext, bc_df_ext, bc_a_ext, bc_da_ext, bc_as_ext, Kr, dKr, thetaW_bc);
1019 //
1020 //calculate the flux jacobian
1021 //
1022 for (int j = 0; j < nDOF_trial_element; j++) {
1023 exteriorNumericalFluxJacobian(a_rowptr.data(), a_colind.data(), isDOFBoundary_u.data()[ebNE_kb], normal, a_ext, da_ext, grad_u_ext, &u_grad_trial_trace[j * nSpace], df_ext, u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element + j],
1024 ebqe_penalty_ext.data()[ebNE_kb], //penalty,
1025 fluxJacobian_u_u[j]);
1026 } //j
1027 //
1028 //update the global Jacobian from the flux Jacobian
1029 //
1030 for (int i = 0; i < nDOF_test_element; i++) {
1031 int eN_i = eN * nDOF_test_element + i;
1032 for (int j = 0; j < nDOF_trial_element; j++) {
1033 int ebN_i_j = ebN * 4 * nDOF_test_X_trial_element + i * nDOF_trial_element + j;
1034 globalJacobian.data()[csrRowIndeces_u_u[eN_i] + csrColumnOffsets_eb_u_u[ebN_i_j]] += fluxJacobian_u_u[j] * u_test_dS[i];
1035 } //j
1036 } //i
1037 } //kb
1038 } //ebNE
1039 } //computeJacobian
1040
1042{
1043 xt::pyarray<double> &bc_mask = args.array<double>("bc_mask");
1044 int NNZ = args.scalar<int>("NNZ"); // number of non-zero entries
1045 int numDOFs = args.scalar<int>("numDOFs"); // number of DOFs
1046 double dt = args.scalar<double>("dt");
1047 xt::pyarray<double> &ML = args.array<double>("ML"); // lumped mass matrix (as vector)
1048 xt::pyarray<double> &mn = args.array<double>("mn"); // DOFs at time tn
1049 xt::pyarray<double> &mHigh = args.array<double>("mHigh"); // high-order mass at t^{n+1}
1050 xt::pyarray<double> &mLow = args.array<double>("mLow"); // low-order mass at t^{n+1}
1051 xt::pyarray<double> &mDotLow = args.array<double>("mDotLow");
1052 xt::pyarray<double> &limited_solution = args.array<double>("limited_solution");
1053 xt::pyarray<int> &csrRowIndeces_DofLoops = args.array<int>("csrRowIndeces_DofLoops");
1054 xt::pyarray<int> &csrColumnOffsets_DofLoops = args.array<int>("csrColumnOffsets_DofLoops");
1055 xt::pyarray<double> &MC = args.array<double>("MC"); // consistent mass matrix
1056 xt::pyarray<double> &dt_times_fH_minus_fL = args.array<double>("dt_times_fH_minus_fL");
1057 xt::pyarray<double> &min_m_bc = args.array<double>("min_m_bc");
1058 xt::pyarray<double> &max_m_bc = args.array<double>("max_m_bc");
1059 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
1060 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
1061 xt::pyarray<int> &freeDOFMaterialTypes = args.array<int>("freeDOFMaterialTypes");
1062 xt::pyarray<double> &fluxCorrection = args.array<double>("fluxCorrection");
1063 //owned by Python so they survive between the two passes below
1064 xt::pyarray<double> &Rpos = args.array<double>("Rpos");
1065 xt::pyarray<double> &Rneg = args.array<double>("Rneg");
1066 xt::pyarray<double> &FluxCorrectionMatrix = args.array<double>("FluxCorrectionMatrix");
1067 // realized corrections alpha_ij f_ij (row i, times bc_mask_i) and their spatial part
1068 // alpha_ij dt (f^H - f^L)_ij, i.e. without the consistent-mass term; read by the transport coupling
1069 xt::pyarray<double> &limitedFluxMatrix = args.array<double>("limitedFluxMatrix");
1070 xt::pyarray<double> &limitedSpatialFluxMatrix = args.array<double>("limitedSpatialFluxMatrix");
1071 // OUT: the limited correction applied on each edge (mass*ML), exported to a
1072 // coupled transport model; written in pass 2.
1073 xt::pyarray<double> &limitedFlux = args.array<double>("limitedFlux");
1074 // flags
1075 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
1076 int MONOLITHIC = args.scalar<int>("MONOLITHIC");
1077 int fct_pass = args.scalar<int>("fct_pass");
1078 // 1 at DOFs whose low-order predictor is saturated (Coefficients.FCT_SATURATED_LOW_ORDER)
1079 xt::pyarray<double> &satLow = args.array<double>("satLow");
1080 //Bound the WATER CONTENT, not the mass m = theta*rho: a neighbour's low-order
1081 //mass is compared at THIS node's density, mLow_j*rho_i/rho_j = theta_j*rho_i. With
1082 //a variable density a heavier neighbour's m exceeds rho_i*theta_s, so bounding m
1083 //let the limiter hand node i more water than it can hold; invert() then clipped
1084 //it and the water was lost (up to 24% at a node on UHD). With a uniform density
1085 //rho_i/rho_j = 1 and this is the plain mass bound.
1086 const bool rhoBounds = rho_dof_member.size() == static_cast<std::size_t>(numDOFs);
1087
1088 //PARALLEL. The limiter is L_ij = min(Rpos_i, Rneg_j), so an owned row i reads the ratio of a column
1089 //j that may be a ghost, and everything a ratio is built from -- the row's own sparsity, ML, mDotLow,
1090 //min/max_m_bc -- is INCOMPLETE at a ghost DOF: with one layer of overlap a ghost's element star and
1091 //its boundary faces are only partly on this rank. Both ranks sharing a cut edge would then apply a
1092 //different L_ij, f_ij = -f_ji dies and the correction manufactures mass along the partition.
1093 //The cure is to let the owners' values cross the cut in the middle of the limiter:
1094 // fct_pass = 1 -> FluxCorrectionMatrix + the local ratios Rpos/Rneg
1095 // [Python forward-inserts Rpos/Rneg, owner -> ghost; the inputs above are scattered before pass 1]
1096 // fct_pass = 2 -> apply the limiter
1097 // fct_pass = 0 -> both back to back; this is the serial path and is byte-identical to the old
1098 // single-pass function.
1099 //mDot is pointwise in (mLow, mn) so it is rebuilt in whichever pass needs it rather than carried.
1100 const bool doPass1 = (fct_pass != 2);
1101 const bool doPass2 = (fct_pass != 1);
1102
1103 std::vector<double> mDot(numDOFs, 0.0);
1104 for (int i = 0; i < numDOFs; i++)
1105 mDot.at(i) = (mLow.at(i) - mn.at(i)) / dt; // local time derivative from low-order mass
1106
1108 // PASS 1: antidiffusive fluxes and Zalesak's ratios //
1110 if (doPass1) {
1111 int ij = 0;
1112 for (int i = 0; i < numDOFs; i++) {
1113
1114 // initialize local min/max from BC
1115 double mini = min_m_bc.at(i);
1116 double maxi = max_m_bc.at(i);
1117
1118 double Pposi = 0.0, Pnegi = 0.0;
1119
1120 // LOOP OVER THE SPARSITY PATTERN (j-LOOP)
1121 for (int offset = csrRowIndeces_DofLoops.at(i); offset < csrRowIndeces_DofLoops.at(i + 1);offset++)
1122 {
1123 int j = csrColumnOffsets_DofLoops.at(offset);
1124
1126 // COMPUTE THE BOUNDS //
1128 if (GLOBAL_FCT == 0) {
1129 if (MONOLITHIC == 0) {
1130 const double mLow_j = rhoBounds ? mLow.at(j) * (rho_dof_member[i] / rho_dof_member[j]) : mLow.at(j);
1131 mini = fmin(mini, mLow_j);
1132 maxi = fmax(maxi, mLow_j);
1133 } else {
1134 mini = fmin(mini, mn.at(j));
1135 maxi = fmax(maxi, mn.at(j));
1136 }
1137 }
1138
1139 if (MONOLITHIC == 0) {
1140 FluxCorrectionMatrix.at(ij) = (LUMPED_MASS_MATRIX == 1 ? 0. : 1.) * dt * MC.at(ij) * (mDotLow.at(i) - mDotLow.at(j)) + dt_times_fH_minus_fL.at(ij);
1141 } else {
1142 FluxCorrectionMatrix.at(ij) = dt_times_fH_minus_fL.at(ij);
1143 }
1144
1146 // COMPUTE P VECTORS //
1148 Pposi += FluxCorrectionMatrix.at(ij) * ((FluxCorrectionMatrix.at(ij) > 0) ? 1. : 0.);
1149 Pnegi += FluxCorrectionMatrix.at(ij) * ((FluxCorrectionMatrix.at(ij) < 0) ? 1. : 0.);
1150
1151 // update ij
1152 ij += 1;
1153 } // j-loop
1154
1155 //Cap the bounds at what node i can hold, rho_i*[theta_r, theta_s] of its own soil. The
1156 //neighbour bound above is rho_i*max_j theta_j, and in a layered soil theta_j can lie in a
1157 //soil with a larger theta_s: the limiter then handed node i more water than its retention
1158 //curve can take, invert() clipped it at theta_s, and the water was lost (4.4% of the fluid
1159 //on the bioswale, nil with one theta_s). mLow_i is kept inside the cap so the bounds stay
1160 //feasible where compressible storage (beta > 0) has already put it above rho_i*theta_s.
1161 if (rhoBounds && GLOBAL_FCT == 0 && MONOLITHIC == 0) {
1162 const int mat_i = freeDOFMaterialTypes.at(i);
1163 const double rho_i = rho_dof_member[i];
1164 mini = fmax(mini, fmin(rho_i * thetaR.at(mat_i), mLow.at(i)));
1165 maxi = fmin(maxi, fmax(rho_i * (thetaR.at(mat_i) + thetaSR.at(mat_i)), mLow.at(i)));
1166 }
1167
1169 // COMPUTE Q VECTORS //
1171 double gamma;
1172 double Qposi;
1173 double Qnegi;
1174
1175 if (MONOLITHIC == 0) {
1176 Qposi = ML.at(i) * (maxi - mLow.at(i));
1177 Qnegi = ML.at(i) * (mini - mLow.at(i));
1178 } else {
1179 // cek todo: don't think this is right for Richards
1180 gamma = 10.0 * ML.at(i);
1181 Qposi = fmin(0.5 * ML.at(i) * (1.0 - mn.at(i)), gamma * (maxi - mn.at(i)));
1182 Qnegi = fmax(0.5 * ML.at(i) * (0.0 - mn.at(i)), gamma * (mini - mn.at(i)));
1183 }
1184
1186 // COMPUTE R VECTORS //
1188 // Saturated predictor: no admissible change of theta at this DOF, so every
1189 // flux touching it is limited to zero (alpha_ij = alpha_ji, still conservative).
1190 if (satLow.at(i) > 0.0) { Qposi = 0.0; Qnegi = 0.0; }
1191 Rpos.at(i) = ((Pposi == 0.0) ? 1.0 : fmin(1.0, Qposi / Pposi));
1192 Rneg.at(i) = ((Pnegi == 0.0) ? 1.0 : fmin(1.0, Qnegi / Pnegi));
1193 } // i DOFs
1194 } // pass 1
1195
1197 // COMPUTE LIMITERS //
1199 if (doPass2) {
1200 int ij = 0;
1201 // std::cout << "FCT: entering second DOF loop (applying limiters)...\n";
1202
1203 for (int i = 0; i < numDOFs; i++) {
1204 double ith_Limiter_times_FluxCorrectionMatrix = 0.0;
1205 double alpha_fA, alpha_dot, beta_ij = 1.0;
1206 // LOOP OVER THE SPARSITY PATTERN (j-LOOP)
1207 for (int offset = csrRowIndeces_DofLoops.at(i); offset < csrRowIndeces_DofLoops.at(i + 1); offset++)
1208 {
1209 int j = csrColumnOffsets_DofLoops.at(offset);
1210 const double alpha_ij = (FluxCorrectionMatrix.at(ij) > 0.0) ? fmin(Rpos.at(i), Rneg.at(j)) : fmin(Rneg.at(i), Rpos.at(j));
1211 alpha_fA = alpha_ij * FluxCorrectionMatrix.at(ij);
1212 limitedFluxMatrix.at(ij) = alpha_fA * bc_mask.at(i);
1213 limitedSpatialFluxMatrix.at(ij) = alpha_ij * dt_times_fH_minus_fL.at(ij) * bc_mask.at(i);
1214 alpha_dot = fmin(1.0, beta_ij * fabs(alpha_fA) / MC.at(ij) / fmax(1.0e-8, fabs(mDot.at(i) - mDot.at(j))));
1215
1216 if (MONOLITHIC == 0) {
1217 ith_Limiter_times_FluxCorrectionMatrix += alpha_fA;
1218 limitedFlux.at(ij) = alpha_fA * bc_mask.at(i);
1219 } else {
1220 ith_Limiter_times_FluxCorrectionMatrix += alpha_fA + (LUMPED_MASS_MATRIX == 1 ? 0. : 1.) * dt * alpha_dot * MC.at(ij) * (mDot.at(i) - mDot.at(j));
1221 limitedFlux.at(ij) = (alpha_fA + (LUMPED_MASS_MATRIX == 1 ? 0. : 1.) * dt * alpha_dot * MC.at(ij) * (mDot.at(i) - mDot.at(j))) * bc_mask.at(i);
1222 }
1223 ij += 1;
1224 }
1225 fluxCorrection.at(i) = -ith_Limiter_times_FluxCorrectionMatrix * bc_mask.at(i) / dt;
1226 limited_solution.at(i) = mLow.at(i) + 1.0 / ML.at(i) * ith_Limiter_times_FluxCorrectionMatrix * bc_mask.at(i);
1227 }
1228 }
1229}
1230
1231 //EDGE VELOCITY FOR THE COUPLED TRANSPORT MODEL. A velocity at the quadrature
1232 //points whose nodal fluxes are exactly the water this step moved along the edges,
1233 //
1234 // int rho v.grad(w_i) dV = sum_j E_ij, E_ij = edgeFluxLow_ij + limitedFlux_ij/dt
1235 //
1236 //(E = fL with FCT off; low order plus the limited correction with FCT on), so a
1237 //transport model advecting with v satisfies this model's discrete continuity
1238 //equation node by node. No global solve; element by element:
1239 // 1. split E_ij among the elements sharing edge ij with the symmetric weights
1240 // |S^e_ij| / sum_e |S^e_ij|, S^e_ij = int_e rho K_s grad(w_j).grad(w_i) (equal
1241 // shares where every S^e_ij is zero), so f^e_ij = -f^e_ji and
1242 // sum_e f^e_ij = E_ij;
1243 // 2. R^e_a = sum_b f^e_ab sums to zero over the element, and on a P1 simplex the
1244 // G_a = int_e grad(w_a) dV span exactly the zero-sum vectors, so one constant
1245 // mass flux q_e solves q_e.G_a = R^e_a for every vertex a;
1246 // 3. v = q_e/rho at each quadrature point.
1247 //With uniform kr on an acute mesh and no FCT correction, q_e is the element mean
1248 //of the Darcy mass flux -rho kr K_s (grad psi - (rho/rho0) g).
1249 //
1250 //Must run after the Newton solve and FCTStep, since it needs the final
1251 //limitedFlux.
1252 //
1253 //PARALLEL: an owned row's edges and their element stars are all local, so its sum
1254 //is exact. Each edge takes its value from its lower-numbered row, and owned DOFs
1255 //are numbered before ghosts, so a ghost row never overrides an owned one and every
1256 //element stays antisymmetric.
1258 {
1259 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
1260 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
1261 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
1262 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
1263 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
1264 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
1265 int nElements_global = args.scalar<int>("nElements_global");
1266 int numDOFs = args.scalar<int>("numDOFs");
1267 int NNZ = args.scalar<int>("NNZ");
1268 xt::pyarray<int> &a_rowptr = args.array<int>("a_rowptr");
1269 xt::pyarray<int> &a_colind = args.array<int>("a_colind");
1270 xt::pyarray<int> &elementMaterialTypes = args.array<int>("elementMaterialTypes");
1271 xt::pyarray<double> &KWs = args.array<double>("KWs");
1272 xt::pyarray<double> &q_rho = args.array<double>("q_rho");
1273 xt::pyarray<double> &q_mu = args.array<double>("q_mu");
1274 double dt = args.scalar<double>("dt");
1275 xt::pyarray<double> &edgeFluxLow = args.array<double>("edgeFluxLow");
1276 xt::pyarray<double> &limitedFlux = args.array<double>("limitedFlux");
1277 xt::pyarray<int> &csrRowIndeces_DofLoops = args.array<int>("csrRowIndeces_DofLoops");
1278 xt::pyarray<int> &csrColumnOffsets_DofLoops = args.array<int>("csrColumnOffsets_DofLoops");
1279 xt::pyarray<int> &csrTransposeIndices = args.array<int>("csrTransposeIndices");
1280 xt::pyarray<int> &csrRowIndeces_CellLoops = args.array<int>("csrRowIndeces_CellLoops");
1281 xt::pyarray<int> &csrColumnOffsets_CellLoops = args.array<int>("csrColumnOffsets_CellLoops");
1282 xt::pyarray<double> &velocity_edge = args.array<double>("velocity_edge");
1283 //step 2 relies on the vertex gradients of a P1 simplex
1284 assert(nDOF_trial_element == nSpace + 1);
1285
1286 //antisymmetric realized edge flux; the lower-numbered row wins
1287 std::vector<double> E(NNZ, 0.0);
1288 for (int i = 0; i < numDOFs; i++)
1289 for (int ij = csrRowIndeces_DofLoops.data()[i]; ij < csrRowIndeces_DofLoops.data()[i + 1]; ij++) {
1290 const int j = csrColumnOffsets_DofLoops.data()[ij];
1291 const int ji = csrTransposeIndices.data()[ij];
1292 E[ij] = (i <= j) ? (edgeFluxLow.data()[ij] + limitedFlux.data()[ij] / dt)
1293 : -(edgeFluxLow.data()[ji] + limitedFlux.data()[ji] / dt);
1294 }
1295 //per edge: sum over its elements of |S^e_ij|, and the number of elements
1296 std::vector<double> W_sum(NNZ, 0.0), n_e(NNZ, 0.0);
1297 //pass 0 fills W_sum and n_e; pass 1 splits E, solves for q_e and writes v
1298 for (int pass = 0; pass < 2; pass++)
1299 for (int eN = 0; eN < nElements_global; eN++) {
1300 const int mat = elementMaterialTypes.data()[eN];
1301 double S[nDOF_test_element][nDOF_trial_element], G[nDOF_test_element][nSpace];
1302 for (int a = 0; a < nDOF_test_element; a++) {
1303 for (int b = 0; b < nDOF_trial_element; b++) S[a][b] = 0.0;
1304 for (int I = 0; I < nSpace; I++) G[a][I] = 0.0;
1305 }
1306 for (int k = 0; k < nQuadraturePoints_element; k++) {
1307 const int eN_k = eN * nQuadraturePoints_element + k;
1308 double jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace], x, y, z;
1309 double u_grad_trial[nDOF_trial_element * nSpace], as_w[nnz];
1310 ck.calculateMapping_element(eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y, z);
1311 const double dV = fabs(jacDet) * dV_ref.data()[k];
1312 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
1313 //the same K_s/mu the residual built T from
1314 for (int ii = 0; ii < nnz; ii++) as_w[ii] = q_rho.data()[eN_k] * KWs.data()[mat * nnz + ii] / q_mu.data()[eN_k];
1315 for (int a = 0; a < nDOF_test_element; a++) {
1316 double grad_w_dV[nSpace];
1317 for (int I = 0; I < nSpace; I++) {
1318 grad_w_dV[I] = u_grad_trial[a * nSpace + I] * dV;
1319 G[a][I] += grad_w_dV[I];
1320 }
1321 for (int b = 0; b < nDOF_trial_element; b++)
1322 S[a][b] += ck.SimpleDiffusionJacobian_weak(a_rowptr.data(), a_colind.data(), as_w, &u_grad_trial[b * nSpace], grad_w_dV);
1323 }
1324 }
1325 double R[nDOF_test_element];
1326 for (int a = 0; a < nDOF_test_element; a++) {
1327 const int eN_a = eN * nDOF_test_element + a;
1328 R[a] = 0.0;
1329 for (int b = 0; b < nDOF_trial_element; b++) {
1330 if (a == b) continue;
1331 const int ab = csrRowIndeces_CellLoops.data()[eN_a] + csrColumnOffsets_CellLoops.data()[eN_a * nDOF_trial_element + b];
1332 if (pass == 0) {
1333 W_sum[ab] += fabs(S[a][b]);
1334 n_e[ab] += 1.0;
1335 } else {
1336 const double share = (W_sum[ab] > 0.0) ? fabs(S[a][b]) / W_sum[ab] : 1.0 / n_e[ab];
1337 R[a] += share * E[ab];
1338 }
1339 }
1340 }
1341 if (pass == 0) continue;
1342 //G q = R is consistent (sum_a R_a = 0 = sum_a G_a), so the normal equations
1343 //(G^T G) q = G^T R give its exact solution
1344 double N[nSpace][nSpace], q[nSpace];
1345 for (int I = 0; I < nSpace; I++) {
1346 q[I] = 0.0;
1347 for (int a = 0; a < nDOF_test_element; a++) q[I] += G[a][I] * R[a];
1348 for (int J = 0; J < nSpace; J++) {
1349 N[I][J] = 0.0;
1350 for (int a = 0; a < nDOF_test_element; a++) N[I][J] += G[a][I] * G[a][J];
1351 }
1352 }
1353 //N is symmetric positive definite, so elimination needs no pivoting
1354 for (int I = 0; I < nSpace; I++)
1355 for (int J = I + 1; J < nSpace; J++) {
1356 const double l = N[J][I] / N[I][I];
1357 for (int K = I; K < nSpace; K++) N[J][K] -= l * N[I][K];
1358 q[J] -= l * q[I];
1359 }
1360 for (int I = nSpace - 1; I >= 0; I--) {
1361 for (int K = I + 1; K < nSpace; K++) q[I] -= N[I][K] * q[K];
1362 q[I] /= N[I][I];
1363 }
1364 for (int k = 0; k < nQuadraturePoints_element; k++) {
1365 const int eN_k = eN * nQuadraturePoints_element + k;
1366 for (int I = 0; I < nSpace; I++)
1367 velocity_edge.data()[eN_k * nSpace + I] = q[I] / q_rho.data()[eN_k];
1368 }
1369 }
1370 }
1371
1372
1373 // Limiter of the fully implicit FCT (Dielmann, Kuzmin & Kees, JNMA), steps 2-5:
1374 // raw fluxes at the high-order predictor p^H, nodal-variation prelimiter, Zalesak with GLOBAL
1375 // bounds [s_min, s_max] relative to s^n, and the corrected datum
1376 // s^{n,FCT}_i = s^n_i + (1/m_i) sum_j alpha_ij fbar_ij.
1377 // It returns fluxCorrection_i = -bc_mask_i (s^{n,FCT}_i - s^n_i) m_i / dt, which the low-order
1378 // residual adds under STABILIZATION_TYPE=Implicit_FCT, i.e. the final solve
1379 // M_L (s(p) - s^{n,FCT})/dt + A~(p) u(p) = 0.
1380 // Inputs are evaluated by calculateResidual_entropy_viscosity at p^H: mLow = s(p^H), mn = s(p^n),
1381 // dt_times_fH_minus_fL = dt (theta a^H + (1-theta) a^n - a~^H)_ij (u_i - u_j), rho_dof_member.
1382 // fct_pass (parallel; ghost values of the outputs must be scattered between the passes):
1383 // 1 -> alpha_nv; 2 -> FluxCorrectionMatrix (= dt fbar), Rpos, Rneg; 3 -> fluxCorrection;
1384 // 0 -> all three back to back (serial).
1386 {
1387 xt::pyarray<double> &bc_mask = args.array<double>("bc_mask");
1388 int numDOFs = args.scalar<int>("numDOFs");
1389 double dt = args.scalar<double>("dt");
1390 double rho = args.scalar<double>("rho");
1391 xt::pyarray<double> &gravity = args.array<double>("gravity");
1392 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
1393 xt::pyarray<double> &u_dof = args.array<double>("u_dof"); // p^H
1394 xt::pyarray<double> &ML = args.array<double>("ML");
1395 xt::pyarray<double> &MC = args.array<double>("MC");
1396 xt::pyarray<double> &mn = args.array<double>("mn"); // s(p^n)
1397 xt::pyarray<double> &mLow = args.array<double>("mLow"); // s(p^H)
1398 xt::pyarray<double> &dt_times_fH_minus_fL = args.array<double>("dt_times_fH_minus_fL");
1399 xt::pyarray<int> &csrRowIndeces_DofLoops = args.array<int>("csrRowIndeces_DofLoops");
1400 xt::pyarray<int> &csrColumnOffsets_DofLoops = args.array<int>("csrColumnOffsets_DofLoops");
1401 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
1402 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
1403 xt::pyarray<int> &freeDOFMaterialTypes = args.array<int>("freeDOFMaterialTypes");
1404 double prelimiter_power = args.scalar<double>("prelimiter_power");
1405 // detector variable: 0 = pressure head p^H (JNMA MFEM), 1 = hydraulic head p^H + z
1406 int prelimiter_head = args.scalar<int>("prelimiter_head");
1407 int fct_pass = args.scalar<int>("fct_pass");
1408 xt::pyarray<double> &alpha_nv = args.array<double>("alpha_nv");
1409 xt::pyarray<double> &Rpos = args.array<double>("Rpos");
1410 xt::pyarray<double> &Rneg = args.array<double>("Rneg");
1411 xt::pyarray<double> &FluxCorrectionMatrix = args.array<double>("FluxCorrectionMatrix");
1412 xt::pyarray<double> &fluxCorrection = args.array<double>("fluxCorrection");
1413 xt::pyarray<double> &limited_solution = args.array<double>("limited_solution"); // s^{n,FCT}
1414 xt::pyarray<double> &limitedFluxMatrix = args.array<double>("limitedFluxMatrix");
1415 xt::pyarray<double> &limitedSpatialFluxMatrix = args.array<double>("limitedSpatialFluxMatrix");
1416 const bool have_rho_dof = (rho_dof_member.size() == static_cast<std::size_t>(numDOFs));
1417
1418 if (fct_pass == 0 || fct_pass == 1) {
1419 // nodal-variation prelimiter over N_i* = {j != i : m_ij > 0}, on the pressure head p^H
1420 // (prelimiter_head = 0) or on the hydraulic head p - (rho_i/rho) g.x as in the residual's
1421 // edge loop (prelimiter_head = 1). prelimiter_power <= 0 switches it off.
1422 std::vector<double> head(numDOFs);
1423 for (int i = 0; i < numDOFs; i++) {
1424 const double rho_i = have_rho_dof ? rho_dof_member[i] : rho;
1425 head[i] = u_dof.data()[i];
1426 if (prelimiter_head)
1427 for (int I = 0; I < nSpace; I++) head[i] -= (rho_i / rho) * gravity.data()[I] * mesh_dof.data()[i * 3 + I];
1428 }
1429 for (int i = 0; i < numDOFs; i++) {
1430 double num = 0.0, den = 0.0;
1431 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1432 const int j = csrColumnOffsets_DofLoops.data()[offset];
1433 if (j == i || MC.data()[offset] <= 0.0) continue;
1434 num += head[i] - head[j];
1435 den += fabs(head[i] - head[j]);
1436 }
1437 alpha_nv.data()[i] = (den > 0.0 && prelimiter_power > 0.0) ? 1.0 - std::pow(fabs(num) / den, prelimiter_power) : 1.0;
1438 }
1439 }
1440 if (fct_pass == 0 || fct_pass == 2) {
1441 int ij = 0;
1442 for (int i = 0; i < numDOFs; i++) {
1443 const int mat_i = freeDOFMaterialTypes.data()[i];
1444 const double rho_i = have_rho_dof ? rho_dof_member[i] : rho;
1445 // global bounds of the retention curve (beta = 0): s in [rho thetaR, rho (thetaR + thetaSR)]
1446 const double s_min = rho_i * thetaR.data()[mat_i];
1447 const double s_max = rho_i * (thetaR.data()[mat_i] + thetaSR.data()[mat_i]);
1448 const double sDot_i = (mLow.data()[i] - mn.data()[i]) / dt;
1449 double Pposi = 0.0, Pnegi = 0.0;
1450 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1451 const int j = csrColumnOffsets_DofLoops.data()[offset];
1452 const double sDot_j = (mLow.data()[j] - mn.data()[j]) / dt;
1453 const double f = dt * MC.data()[ij] * (sDot_i - sDot_j) + dt_times_fH_minus_fL.data()[ij];
1454 FluxCorrectionMatrix.data()[ij] = (j == i) ? 0.0 : fmin(alpha_nv.data()[i], alpha_nv.data()[j]) * f;
1455 Pposi += fmax(0.0, FluxCorrectionMatrix.data()[ij]);
1456 Pnegi += fmin(0.0, FluxCorrectionMatrix.data()[ij]);
1457 ij += 1;
1458 }
1459 const double Qposi = ML.data()[i] * fmax(0.0, s_max - mn.data()[i]);
1460 const double Qnegi = ML.data()[i] * fmin(0.0, s_min - mn.data()[i]);
1461 Rpos.data()[i] = (Pposi == 0.0) ? 1.0 : fmin(1.0, Qposi / Pposi);
1462 Rneg.data()[i] = (Pnegi == 0.0) ? 1.0 : fmin(1.0, Qnegi / Pnegi);
1463 }
1464 }
1465 if (fct_pass == 0 || fct_pass == 3) {
1466 int ij = 0;
1467 for (int i = 0; i < numDOFs; i++) {
1468 double sum = 0.0;
1469 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1470 const int j = csrColumnOffsets_DofLoops.data()[offset];
1471 const double f = FluxCorrectionMatrix.data()[ij];
1472 const double L_ij = (f > 0.0) ? fmin(Rpos.data()[i], Rneg.data()[j]) : fmin(Rneg.data()[i], Rpos.data()[j]);
1473 const double alpha_ij = (j == i) ? 0.0 : L_ij * fmin(alpha_nv.data()[i], alpha_nv.data()[j]);
1474 sum += L_ij * f;
1475 limitedFluxMatrix.data()[ij] = bc_mask.data()[i] * L_ij * f;
1476 limitedSpatialFluxMatrix.data()[ij] = bc_mask.data()[i] * alpha_ij * dt_times_fH_minus_fL.data()[ij];
1477 ij += 1;
1478 }
1479 limited_solution.data()[i] = mn.data()[i] + bc_mask.data()[i] * sum / ML.data()[i];
1480 fluxCorrection.data()[i] = -bc_mask.data()[i] * sum / dt;
1481 }
1482 }
1483 }
1484
1486 {
1487 int NNZ = args.scalar<int>("NNZ"); //number on non-zero entries on sparsity pattern
1488 int numDOFs = args.scalar<int>("numDOFs"); //number of DOFs
1489 int num_fct_iter = args.scalar<int>("num_fct_iter");
1490 double dt = args.scalar<double>("dt");
1491 xt::pyarray<double> &lumped_mass_matrix = args.array<double>("lumped_mass_matrix"); //lumped mass matrix (as vector)
1492 xt::pyarray<double> &soln = args.array<double>("soln"); //DOFs of solution at time tn
1493 xt::pyarray<double> &pn = args.array<double>("pn"); //DOFs of solution at time tn
1494 xt::pyarray<double> &solH = args.array<double>("solH"); //DOFs of high order solution at tnp1
1495 xt::pyarray<double> &uLow = args.array<double>("uLow");
1496 xt::pyarray<double> &uDotLow = args.array<double>("uDotLow");
1497 xt::pyarray<double> &dLow = args.array<double>("dLow");
1498 xt::pyarray<double> &solLim = args.array<double>("limited_solution");
1499 xt::pyarray<double> &MC = args.array<double>("MC");
1500 xt::pyarray<double> &ML = args.array<double>("ML");
1501 xt::pyarray<double> &FluxMatrix = args.array<double>("FluxMatrix");
1502 xt::pyarray<double> &limitedFlux = args.array<double>("limited_Flux");
1503 xt::pyarray<int> &csrRowIndeces_DofLoops = args.array<int>("csrRowIndeces_DofLoops"); //csr row indeces
1504 xt::pyarray<int> &csrColumnOffsets_DofLoops = args.array<int>("csrColumnOffsets_DofLoops"); //csr column offsets
1505 xt::pyarray<double> &MassMatrix = args.array<double>("MassMatrix"); //mass matrix
1506 xt::pyarray<double> &dt_times_fH_minus_fL = args.array<double>("dt_times_fH_minus_fL"); //low minus high order dissipative matrices
1507 xt::pyarray<double> &min_m_bc = args.array<double>("min_m_bc"); //min/max value at BCs. If DOF is not at boundary then min=1E10, max=-1E10
1508 xt::pyarray<double> &max_m_bc = args.array<double>("max_m_bc");
1509 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
1510 int MONOLITHIC = args.scalar<int>("MONOLITHIC");
1511 //Heap-backed members, not stack VLAs -- see the declarations in the class body.
1512 Rpos_member.assign(numDOFs, 0.0);
1513 Rneg_member.assign(numDOFs, 0.0);
1514 std::vector<double> &Rpos = Rpos_member;
1515 std::vector<double> &Rneg = Rneg_member;
1516 int ij = 0;
1517
1519 // ********** COMPUTE LOW ORDER SOLUTION ********** //
1521 if (num_fct_iter == 0) { // No FCT for global bounds
1522 for (int i = 0; i < numDOFs; i++) { solLim.data()[i] = uLow.data()[i]; }
1523 } else // do FCT iterations (with global bounds) on low order solution
1524 {
1525 for (int iter = 0; iter < num_fct_iter; iter++) {
1526 ij = 0;
1527 for (int i = 0; i < numDOFs; i++) {
1528 double maxi = 1.0, Pposi = 0;
1529 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1530 int j = csrColumnOffsets_DofLoops.data()[offset];
1531 // compute Flux correction
1532 double Fluxij = FluxMatrix.data()[ij] - limitedFlux.data()[ij];
1533 Pposi += Fluxij * ((Fluxij > 0) ? 1. : 0.);
1534 // update ij
1535 ij += 1;
1536 }
1537 // compute Q vectors
1538 double mi = ML.data()[i];
1539 double solLimi = solLim.data()[i];
1540 double Qposi = mi * (maxi - solLimi);
1541 // compute R vectors
1542 Rpos[i] = ((Pposi == 0) ? 1. : fmin(1.0, Qposi / Pposi));
1543 }
1544 ij = 0;
1545 for (int i = 0; i < numDOFs; i++) {
1546 double ith_Limiter_times_FluxCorrectionMatrix = 0.;
1547 double Rposi = Rpos[i];
1548 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1549 int j = csrColumnOffsets_DofLoops.data()[offset];
1550 // Flux Correction
1551 double Fluxij = FluxMatrix.data()[ij] - limitedFlux.data()[ij];
1552 // compute limiter
1553 double Lij = 1.0;
1554 Lij = (Fluxij > 0 ? Rposi : Rpos[j]);
1555 // compute limited flux
1556 ith_Limiter_times_FluxCorrectionMatrix += Lij * Fluxij;
1557
1558 // update limited flux
1559 limitedFlux.data()[ij] = Lij * Fluxij;
1560
1561 //update FluxMatrix
1562 FluxMatrix.data()[ij] = Fluxij;
1563
1564 //update ij
1565 ij += 1;
1566 }
1567 //update limited solution
1568 double mi = ML.data()[i];
1569 }
1570 }
1571 }
1572
1573 // ***************************************** //
1574 // ********** HIGH ORDER SOLUTION ********** //
1575 // ***************************************** //
1576 ij = 0;
1577 for (int i = 0; i < numDOFs; i++) {
1578 double mini = soln.data()[i], maxi = soln.data()[i];
1579 double Pposi = 0, Pnegi = 0.;
1580 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1581 int j = csrColumnOffsets_DofLoops.data()[offset];
1582 // compute local bounds //
1583 mini = fmin(mini, soln.data()[j]);
1584 maxi = fmax(maxi, soln.data()[j]);
1585 // compute P vectors //
1586 double fij = (MC.data()[ij] * (uDotLow.data()[i] - uDotLow.data()[j]) / dt + dLow.data()[ij] * (uLow.data()[i] - uLow.data()[j]));
1587 Pposi += fij * (fij > 0 ? 1. : 0.);
1588 Pnegi += fij * (fij < 0 ? 1. : 0.);
1589 //update ij
1590 ij += 1;
1591 }
1592 // compute Q vectors //
1593 double mi = ML.data()[i];
1594 double Qposi = mi * (maxi - solLim.data()[i]);
1595 double Qnegi = mi * (mini - solLim.data()[i]);
1596 // compute R vectors //
1597 Rpos[i] = ((Pposi == 0) ? 1. : fmin(1.0, Qposi / Pposi));
1598 Rneg[i] = ((Pnegi == 0) ? 1. : fmin(1.0, Qnegi / Pnegi));
1599 }
1600
1601 // COMPUTE LIMITERS //
1602 ij = 0;
1603 for (int i = 0; i < numDOFs; i++) {
1604 double ith_limited_flux_correction = 0;
1605 double Rposi = Rpos[i];
1606 double Rnegi = Rneg[i];
1607 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
1608 int j = csrColumnOffsets_DofLoops.data()[offset];
1609 // compute flux correction
1610 double fij = (MC.data()[ij] * (uDotLow.data()[i] - uDotLow.data()[j]) / dt + dLow.data()[ij] * (uLow.data()[i] - uLow.data()[j]));
1611
1612 // compute limiters
1613 double Lij = 1.0;
1614 Lij = fij > 0 ? fmin(Rposi, Rneg[j]) : fmin(Rnegi, Rpos[j]);
1615 // compute ith_limited_flux_correction
1616 ith_limited_flux_correction += Lij * fij;
1617 ij += 1;
1618 }
1619 double mi = ML.data()[i];
1620 solLim[i] += 1. / mi * ith_limited_flux_correction;
1621 }
1622 }
1623
1625 {
1626 xt::pyarray<double> &globalJacobian = args.array<double>("globalJacobian");
1627 double Theta = args.scalar<double>("Theta");
1628 // L-scheme Jacobian (Pop, Radu & Knabner; List & Radu; JNMA defect correction): LSCHEME_L > 0 assembles
1629 // J = (L/dt) M_L + A~(u^k) -- dm/du replaced by L, upwind k_r frozen (no dk_r terms). The residual is unchanged.
1630 const double LSCHEME_L = args.scalar<double>("LSCHEME_L");
1631 const double dKr_on = (LSCHEME_L > 0.0) ? 0.0 : 1.0;
1632 double Theta_h = args.scalar<double>("Theta_h");
1633 xt::pyarray<double> &bc_mask = args.array<double>("bc_mask");
1634 double dt = args.scalar<double>("dt");
1635 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
1636 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
1637 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
1638 xt::pyarray<double> &mesh_velocity_dof = args.array<double>("mesh_velocity_dof");
1639 double MOVING_DOMAIN = args.scalar<double>("MOVING_DOMAIN");
1640 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
1641 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
1642 xt::pyarray<double> &u_trial_ref = args.array<double>("u_trial_ref");
1643 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
1644 xt::pyarray<double> &u_test_ref = args.array<double>("u_test_ref");
1645 xt::pyarray<double> &u_grad_test_ref = args.array<double>("u_grad_test_ref");
1646 xt::pyarray<double> &mesh_trial_trace_ref = args.array<double>("mesh_trial_trace_ref");
1647 xt::pyarray<double> &mesh_grad_trial_trace_ref = args.array<double>("mesh_grad_trial_trace_ref");
1648 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
1649 xt::pyarray<double> &u_trial_trace_ref = args.array<double>("u_trial_trace_ref");
1650
1651 xt::pyarray<double> &u_grad_trial_trace_ref = args.array<double>("u_grad_trial_trace_ref");
1652 xt::pyarray<double> &u_test_trace_ref = args.array<double>("u_test_trace_ref");
1653 xt::pyarray<double> &u_grad_test_trace_ref = args.array<double>("u_grad_test_trace_ref");
1654 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
1655 xt::pyarray<double> &boundaryJac_ref = args.array<double>("boundaryJac_ref");
1656 int nElements_global = args.scalar<int>("nElements_global");
1657 xt::pyarray<double> &ebqe_penalty_ext = args.array<double>("ebqe_penalty_ext");
1658 xt::pyarray<int> &elementMaterialTypes = args.array<int>("elementMaterialTypes");
1659 xt::pyarray<int> &isSeepageFace = args.array<int>("isSeepageFace");
1660 xt::pyarray<int> &a_rowptr = args.array<int>("a_rowptr");
1661 xt::pyarray<int> &a_colind = args.array<int>("a_colind");
1662 double rho = args.scalar<double>("rho");
1663 double beta = args.scalar<double>("beta");
1665 xt::pyarray<double> &q_rho = args.array<double>("q_rho");
1666 xt::pyarray<double> &ebqe_rho = args.array<double>("ebqe_rho");
1667 xt::pyarray<double> &q_mu = args.array<double>("q_mu");
1668 xt::pyarray<double> &ebqe_mu = args.array<double>("ebqe_mu");
1669 xt::pyarray<double> &gravity = args.array<double>("gravity");
1670 xt::pyarray<double> &alpha = args.array<double>("alpha");
1671 xt::pyarray<double> &n = args.array<double>("n");
1672 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
1673 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
1674 PSK_TYPE_member = args.scalar<int>("PSK_TYPE");
1675 VG_AIR_ENTRY_member = args.scalar<double>("VG_AIR_ENTRY");
1676 xt::pyarray<double> &KWs = args.array<double>("KWs");
1677 double useMetrics = args.scalar<double>("useMetrics");
1678 double alphaBDF = args.scalar<double>("alphaBDF");
1679 int lag_shockCapturing = args.scalar<int>("lag_shockCapturing");
1680 double shockCapturingDiffusion = args.scalar<double>("shockCapturingDiffusion");
1681 double sc_uref = args.scalar<double>("sc_uref");
1682 double sc_alpha = args.scalar<double>("sc_alpha");
1683 xt::pyarray<int> &u_l2g = args.array<int>("u_l2g");
1684 xt::pyarray<int> &r_l2g = args.array<int>("r_l2g");
1685 xt::pyarray<double> &elementDiameter = args.array<double>("elementDiameter");
1686 int degree_polynomial = args.scalar<int>("degree_polynomial");
1687 xt::pyarray<double> &u_dof = args.array<double>("u_dof");
1688 xt::pyarray<double> &u_dof_old = args.array<double>("u_dof_old");
1689 xt::pyarray<double> &velocity = args.array<double>("velocity");
1690 xt::pyarray<double> &q_m = args.array<double>("q_m");
1691 xt::pyarray<double> &q_theta = args.array<double>("q_theta");
1692 xt::pyarray<double> &q_u = args.array<double>("q_u");
1693 xt::pyarray<double> &q_dV = args.array<double>("q_dV");
1694 xt::pyarray<double> &q_m_betaBDF = args.array<double>("q_m_betaBDF");
1695 xt::pyarray<double> &cfl = args.array<double>("cfl");
1696 xt::pyarray<double> &q_numDiff_u = args.array<double>("q_numDiff_u");
1697 xt::pyarray<double> &q_numDiff_u_last = args.array<double>("q_numDiff_u_last");
1698 int offset_u = args.scalar<int>("offset_u");
1699 int stride_u = args.scalar<int>("stride_u");
1700 xt::pyarray<double> &globalResidual = args.array<double>("globalResidual");
1701 int nExteriorElementBoundaries_global = args.scalar<int>("nExteriorElementBoundaries_global");
1702 xt::pyarray<int> &exteriorElementBoundariesArray = args.array<int>("exteriorElementBoundariesArray");
1703 xt::pyarray<int> &elementBoundaryElementsArray = args.array<int>("elementBoundaryElementsArray");
1704 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray = args.array<int>("elementBoundaryLocalElementBoundariesArray");
1705 xt::pyarray<double> &ebqe_velocity_ext = args.array<double>("ebqe_velocity_ext");
1706 xt::pyarray<int> &isDOFBoundary_u = args.array<int>("isDOFBoundary_u");
1707 xt::pyarray<double> &ebqe_bc_u_ext = args.array<double>("ebqe_bc_u_ext");
1708 xt::pyarray<int> &isFluxBoundary_u = args.array<int>("isFluxBoundary_u");
1709 xt::pyarray<double> &ebqe_bc_flux_ext = args.array<double>("ebqe_bc_flux_ext");
1710 xt::pyarray<double> &ebqe_phi = args.array<double>("ebqe_phi");
1711 double epsFact = args.scalar<double>("epsFact");
1712 xt::pyarray<double> &ebqe_u = args.array<double>("ebqe_u");
1713 xt::pyarray<double> &ebqe_theta = args.array<double>("ebqe_theta");
1714 xt::pyarray<double> &ebqe_flux = args.array<double>("ebqe_flux");
1715 // PARAMETERS FOR EDGE BASED STABILIZATION
1716 double cE = args.scalar<double>("cE");
1717 double cK = args.scalar<double>("cK");
1718 // PARAMETERS FOR LOG BASED ENTROPY FUNCTION
1719 double uL = args.scalar<double>("uL");
1720 double uR = args.scalar<double>("uR");
1721 // PARAMETERS FOR EDGE VISCOSITY
1722 int numDOFs = args.scalar<int>("numDOFs");
1723 int NNZ = args.scalar<int>("NNZ");
1724 xt::pyarray<int> &csrRowIndeces_DofLoops = args.array<int>("csrRowIndeces_DofLoops");
1725 xt::pyarray<int> &csrColumnOffsets_DofLoops = args.array<int>("csrColumnOffsets_DofLoops");
1726 xt::pyarray<int> &csrRowIndeces_CellLoops = args.array<int>("csrRowIndeces_CellLoops");
1727 xt::pyarray<int> &csrColumnOffsets_CellLoops = args.array<int>("csrColumnOffsets_CellLoops");
1728 xt::pyarray<int> &csrColumnOffsets_eb_CellLoops = args.array<int>("csrColumnOffsets_eb_CellLoops");
1729 // C matrices
1730 xt::pyarray<double> &Cx = args.array<double>("Cx");
1731 xt::pyarray<double> &Cy = args.array<double>("Cy");
1732 xt::pyarray<double> &Cz = args.array<double>("Cz");
1733 xt::pyarray<double> &CTx = args.array<double>("CTx");
1734 xt::pyarray<double> &CTy = args.array<double>("CTy");
1735 xt::pyarray<double> &CTz = args.array<double>("CTz");
1736 xt::pyarray<double> &ML = args.array<double>("ML");
1737 xt::pyarray<double> &MC = args.array<double>("MC");
1738
1739 xt::pyarray<double> &delta_x_ij = args.array<double>("delta_x_ij");
1740 // PARAMETERS FOR 1st or 2nd ORDER MPP METHOD
1741 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
1742 STABILIZATION STABILIZATION_TYPE{static_cast<STABILIZATION>(args.scalar<int>("STABILIZATION_TYPE"))};
1743
1744 int ENTROPY_TYPE = args.scalar<int>("ENTROPY_TYPE");
1745 // FOR FCT
1746 xt::pyarray<double> &dLow = args.array<double>("dLow");
1747 xt::pyarray<double> &fluxMatrix = args.array<double>("fluxMatrix");
1748 xt::pyarray<double> &mDotLow = args.array<double>("mDotLow");
1749 xt::pyarray<double> &mLow = args.array<double>("mLow");
1750 xt::pyarray<double> &dt_times_fH_minus_fL = args.array<double>("dt_times_fH_minus_fL");
1751 xt::pyarray<double> &min_m_bc = args.array<double>("min_m_bc");
1752 xt::pyarray<double> &max_m_bc = args.array<double>("max_m_bc");
1753 //weak-Dirichlet block of the transport operator, used only when FCT_BND_DIFF
1754 //is on. See the flag's comment above.
1755 xt::pyarray<double> &TransportBoundary = args.array<double>("TransportBoundary");
1756 //Step B changes the RESIDUAL (TB unclipped instead of through the fmax clip), so it is
1757 //tied to FCT, where it keeps the limited correction conservative; an FCT=False run keeps
1758 //the old routing. Python passes coefficients.FCT_BND_DIFF and coefficients.FCT here.
1759 const bool useBndDiff = args.scalar<int>("FCT_BND_DIFF") != 0;
1760 const bool useBndKrw = args.scalar<int>("FCT_BND_KRW") != 0;
1761 //Coefficients(FCT_BND_RESET=True) and (BC_CONS=True), see their comments at the top
1762 const bool fctBndReset = args.scalar<int>("FCT_BND_RESET") != 0;
1763 const bool bcCons = args.scalar<int>("BC_CONS") != 0;
1764 // AUX QUANTITIES OF INTEREST
1765 xt::pyarray<double> &quantDOFs = args.array<double>("quantDOFs");
1766 xt::pyarray<double> &mn = args.array<double>("mn");
1767 // 1 => rho_nodal holds the transport model's nodal rho(c_i) and replaces the
1768 // L2 projection of q_rho below.
1769 xt::pyarray<double> &rho_nodal = args.array<double>("rho_nodal");
1770 const int USE_RHO_NODAL = args.scalar<int>("USE_RHO_NODAL");
1771 // 1 => rho_nodal_old holds the nodal rho of the previous accepted step, and
1772 // the old-time mass mn is evaluated with it (see the DOF loop).
1773 xt::pyarray<double> &rho_nodal_old = args.array<double>("rho_nodal_old");
1774 const int USE_RHO_NODAL_OLD = args.scalar<int>("USE_RHO_NODAL_OLD");
1775 // NODAL water content theta(psi^n), the theta of mn. Exported for coupled
1776 // transport models. Not read by this model.
1777 xt::pyarray<double> &thetaW_dof = args.array<double>("thetaW_dof");
1778 xt::pyarray<double> &fluxCorrection = args.array<double>("fluxCorrection");
1779 xt::pyarray<double> &limited_solution = args.array<double>("limited_solution");
1780 xt::pyarray<int> &freeDOFMaterialTypes = args.array<int>("freeDOFMaterialTypes");
1781
1782 xt::pyarray<double> &velocity_couple = args.array<double>("velocity_couple");
1783 xt::pyarray<double> &ebqe_velocity_ext_couple = args.array<double>("ebqe_velocity_ext_couple");
1784 // BOUNDARY-FLUX DIAGNOSTIC. ebqe_flux gets the FULL flux from
1785 // exteriorNumericalFlux2, but with useConsistentFlux = false the residual
1786 // integrates bflux_ext, which that routine sets to the PENALTY TERM ALONE
1787 // (Richards.h:320-321) -- the Darcy consistency term v.n is dropped. So
1788 // ebqe_flux is NOT the flux this scheme applies, and neither is the
1789 // velocity_couple trace handed to TADR, which is the consistency term and
1790 // nothing else. Python cannot see bflux_ext at all, so export it, plus the
1791 // quadrature weight, and let the diagnostic in Richards.py do the split
1792 // exactly instead of estimating it from penalty*(u - bc_u).
1793 xt::pyarray<double> &ebqe_bflux = args.array<double>("ebqe_bflux");
1794 xt::pyarray<double> &ebqe_dS = args.array<double>("ebqe_dS");
1795 // CONSERVATIVE NODAL BOUNDARY FLUX, exported for the transport coupling.
1796 // At a boundary DOF i the water flux this scheme applies is the row's
1797 // penalty part (bflux, integrated below) PLUS the IIPG consistency rows
1798 // that are folded into TransportMatrix (see the exterior loop) and reach
1799 // the residual as non-telescoping edge fluxes. At convergence their sum
1800 // equals the flux of the SYMMETRIC interior operator out of node i,
1801 // F_i = sum_j Kr_up max(0, -T^int_ij) dphi_ij, T^int = T - TB,
1802 // i.e. the Hughes consistent flux, which telescopes exactly with the
1803 // interior edge fluxes. The pointwise trace -(a/rho)(grad psi - rho_r g).n
1804 // that ebqe_velocity_ext_couple carries is NOT that number: on Simmons SMD
1805 // level 4 it reads 0.29x of the corner singularity, 1.5-3x the flux across a
1806 // one-cell density jump, and 0.83-0.86x the open-top outflow. The second
1807 // exterior pass at the end of this function therefore replaces the normal
1808 // component of the coupled trace by F_i/(rho S_i), the same way the cell
1809 // loop builds the interior trace -- no Python post-processing, one extra
1810 // sweep over the boundary quadrature points per residual call. Units are
1811 // the residual's (rho-weighted); outward positive. Only the Theta part is
1812 // carried -- Theta is hardwired to 1 in Richards.py.
1813 xt::pyarray<double> &consBoundaryFlux = args.array<double>("consBoundaryFlux");
1814 // OUT: per edge, the low-order water flux into row i from column j on the
1815 // INTERIOR (symmetric) part of the transport matrix, same units as the
1816 // residual's edge flux. Antisymmetric by construction, so a coupled
1817 // transport model can advect with it and conserve. Theta part only
1818 // (Theta is hardwired to 1).
1819 xt::pyarray<double> &edgeFluxLow = args.array<double>("edgeFluxLow");
1820 // the interior low-order flux fL_int of edge ij alone (boundary part excluded); read by the
1821 // edge-flux TADR path (STABILIZATION_TYPE 2-5) and calculateEdgeVelocity
1822 xt::pyarray<double> &edgeFluxInterior = args.array<double>("edgeFluxInterior");
1823 // IN: nodal water source [mass/(volume time)], added to row i as ML_i*nodalSource_i
1824 // (lumped). Zero unless the deck passes Coefficients(mms_source=...), e.g. the
1825 // flow source of a manufactured solution.
1826 xt::pyarray<double> &nodalSource = args.array<double>("nodalSource");
1827 // xt::pyarray<double> &q_x = args.array<double>("q_x");
1828 // xt::pyarray<double> &ebqe_x = args.array<double>("ebqe_x");
1829
1830 xt::pyarray<double> &anb_seepage_flux_n = args.array<double>("anb_seepage_flux_n");
1831 xt::pyarray<double> &q_velocity = args.array<double>("q_velocity");
1832 double &anb_seepage_flux(args.scalar<double>("anb_seepage_flux"));
1833 anb_seepage_flux = 0.0;
1834 xt::pyarray<int> &csrRowIndeces_u_u = args.array<int>("csrRowIndeces_u_u");
1835 xt::pyarray<int> &csrColumnOffsets_u_u = args.array<int>("csrColumnOffsets_u_u");
1836 xt::pyarray<int> &csrColumnOffsets_eb_u_u = args.array<int>("csrColumnOffsets_eb_u_u");
1837 // Compatible flow -> transport coupling (TADR STABILIZATION_TYPE=Compatible):
1838 // USE_NODAL_RHO -> rho_dof = rho_dof_in, the transport's nodal rho_T(c_i), instead of the
1839 // lumped projection of q_rho (which is not the nodal value at boundaries)
1840 // edgeFluxLow_ij <- the low-order edge flux of row i (what the residual sums over j)
1841 // boundaryFluxDOF_i<- the boundary integral of row i, positive into the domain
1842 const int USE_NODAL_RHO = args.scalar<int>("USE_NODAL_RHO");
1843 xt::pyarray<double> &rho_dof_in = args.array<double>("rho_dof_in");
1844 xt::pyarray<double> &boundaryFluxDOF = args.array<double>("boundaryFluxDOF");
1845 std::fill(boundaryFluxDOF.begin(), boundaryFluxDOF.end(), 0.0);
1846 // double Rpos[numDOFs], Rneg[numDOFs];
1847 std::vector<double> Rpos(numDOFs, 0.0), Rneg(numDOFs, 0.0);
1848 std::vector<double> TransportMatrix(NNZ, 0.0),
1849 TransportMatrixConsistent(NNZ, 0.0),
1850 TransportMatrixn(NNZ, 0.0),
1851 TransportMatrixConsistentn(NNZ, 0.0);
1852 //double FluxCorrectionMatrix[NNZ];
1853 // NOTE: This function follows a different (but equivalent) implementation of the smoothness based indicator than NCLS.h
1854 // Allocate space for the transport matrices
1855 // This is used for first order KUZMIN'S METHOD
1856 // double TransportMatrix[NNZ], TransportMatrixConsistent[NNZ];
1857 // double TransportMatrixn[NNZ], TransportMatrixConsistentn[NNZ];
1858 std::valarray<double> u_free_dof(numDOFs);
1859 std::valarray<double> u_free_dof_old(numDOFs);
1860 std::valarray<double> ML2(numDOFs);
1861 // Lumped L2 projection buffers for density
1862 std::vector<double> rho_dof(numDOFs, 0.0);
1863 std::vector<double> ML_rho(numDOFs, 0.0);
1864 std::fill(velocity_couple.data(), velocity_couple.data() + velocity_couple.size(), 0.0);
1865 std::fill(ebqe_velocity_ext_couple.data(), ebqe_velocity_ext_couple.data() + ebqe_velocity_ext_couple.size(), 0.0);
1866 std::fill(consBoundaryFlux.data(), consBoundaryFlux.data() + consBoundaryFlux.size(), 0.0);
1867 // rows that own at least one exterior-face quadrature point; only these get
1868 // the consBoundaryFlux accumulation in the DOF loop (it needs an extra
1869 // closure evaluation per edge, so it is not done on interior rows)
1870 std::vector<char> is_bdry_dof(numDOFs, 0);
1871 // Bookkeeping for the conservative boundary velocity below: per DOF, the
1872 // measure of the weakly imposed (Dirichlet) part of its boundary star and
1873 // the flux the prescribed-flux faces already pin there; per boundary
1874 // quadrature point, the outward normal, cached so the second pass does not
1875 // have to redo the element boundary mapping.
1876 std::vector<double> bdryDirichletArea(numDOFs, 0.0);
1877 std::vector<double> bdryPrescribedFlux(numDOFs, 0.0);
1878 std::vector<double> ebqe_normal_cache(bcCons ? nExteriorElementBoundaries_global * nQuadraturePoints_elementBoundary * nSpace : 0, 0.0);
1879
1880 for (int eN = 0; eN < nElements_global; eN++)
1881 for (int j = 0; j < nDOF_trial_element; j++) {
1882 int eN_nDOF_trial_element = eN * nDOF_trial_element;
1883 u_free_dof[r_l2g.data()[eN_nDOF_trial_element + j]] = u_dof.data()[u_l2g.data()[eN_nDOF_trial_element + j]];
1884 u_free_dof_old[r_l2g.data()[eN_nDOF_trial_element + j]] = u_dof_old.data()[u_l2g.data()[eN_nDOF_trial_element + j]];
1885 }
1886 for (int i = 0; i < NNZ; i++) {
1887 TransportMatrix[i] = 0.;
1888 TransportMatrixConsistent[i] = 0.;
1889 TransportMatrixn[i] = 0.;
1890 TransportMatrixConsistentn[i] = 0.;
1891 TransportBoundary.data()[i] = 0.;
1892 }
1893 //min/max_m_bc are only ever fmin/fmax'd into below, so without this they would be the
1894 //widest window ever seen rather than this step's: a time dependent Dirichlet or a
1895 //moving seepage active set would keep loosening the FCT bound and never tighten it.
1896 //FCT_BND_RESET=False restores the old accumulate-forever behaviour.
1897 if (fctBndReset)
1898 for (int i = 0; i < numDOFs; i++) {
1899 min_m_bc.data()[i] = 1.0e10;
1900 max_m_bc.data()[i] = -1.0e10;
1901 }
1902
1903 // Project quadrature density to nodal DOFs before constructing nodal
1904 // stabilization potentials so Phi uses a true nodal density field.
1905 // With USE_RHO_NODAL the transport model's nodal rho(c_i) is used instead:
1906 // lumped mass evaluates storage AT the node, where the value is rho(c_i),
1907 // and the projection smooths it. (Linear EOS: interpolating rho(c_i)
1908 // reproduces q_rho exactly, so nodes and quadrature points then agree.)
1909 if (USE_RHO_NODAL) {
1910 for (int i = 0; i < numDOFs; ++i) rho_dof[i] = rho_nodal.data()[i];
1911 } else {
1912 for (int eN = 0; eN < nElements_global; eN++) {
1913 const int eN_nDOF_trial_element = eN * nDOF_trial_element;
1914 for (int k = 0; k < nQuadraturePoints_element; k++) {
1915 const int eN_k = eN * nQuadraturePoints_element + k;
1916 double jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace], x, y, z;
1917 ck.calculateMapping_element(eN, k, mesh_dof.data(), mesh_l2g.data(),
1918 mesh_trial_ref.data(), mesh_grad_trial_ref.data(),
1919 jac, jacDet, jacInv, x, y, z);
1920 const double dV = fabs(jacDet) * dV_ref.data()[k];
1921 for (int i = 0; i < nDOF_test_element; i++) {
1922 const int eN_i = eN * nDOF_test_element + i;
1923 const int free_gi = r_l2g.data()[eN_i];
1924 const double u_test_dV = u_test_ref.data()[k * nDOF_trial_element + i] * dV;
1925 rho_dof[free_gi] += q_rho.data()[eN_k] * u_test_dV;
1926 ML_rho[free_gi] += u_test_dV;
1927 }
1928 }
1929 }
1930 for (int i = 0; i < numDOFs; ++i) {
1931 if (ML_rho[i] > 0.0) rho_dof[i] /= ML_rho[i];
1932 else rho_dof[i] = rho;
1933 }
1934 } // USE_RHO_NODAL
1935 if (USE_NODAL_RHO)
1936 for (int i = 0; i < numDOFs; ++i) rho_dof[i] = rho_dof_in.data()[i];
1937 // Cache the projected, salinity-coupled density for use in invert() so
1938 // the m -> u inversion is consistent with the forward residual.
1939 rho_dof_member = rho_dof;
1940
1941 // compute entropy and init global_entropy_residual and boundary_integral
1942 //Heap-backed members, not stack VLAs -- see the declarations in the class body.
1943 psi_member.assign(numDOFs, 0.0);
1944 eta_member.assign(numDOFs, 0.0);
1945 global_entropy_residual_member.assign(numDOFs, 0.0);
1946 boundary_integral_member.assign(numDOFs, 0.0);
1947 std::vector<double> &psi = psi_member;
1948 std::vector<double> &eta = eta_member;
1949 std::vector<double> &global_entropy_residual = global_entropy_residual_member;
1950 std::vector<double> &boundary_integral = boundary_integral_member;
1951 for (int i = 0; i < numDOFs; i++) {
1952 // NODAL ENTROPY //
1953 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) //EV stab
1954 {
1955 double solni = 1.0 * u_free_dof_old[i];
1956 eta[i] = ENTROPY_TYPE == 1 ? ENTROPY(solni, uL, uR) : ENTROPY_LOG(solni, uL, uR);
1957 global_entropy_residual[i] = 0.;
1958 }
1959 boundary_integral[i] = 0.;
1960 ML2[i] = 0.0;
1961 }
1962
1964 // ** LOOP IN CELLS FOR CELL BASED TERMS ** //
1966 // HERE WE COMPUTE:
1967 // * Time derivative term. u_t
1968 // * cell based CFL (for reference)
1969 // * Entropy residual
1970 // * Transport matrices
1971
1972 for (int eN = 0; eN < nElements_global; eN++) {
1973 const int eN_nDOF_trial_element = eN * nDOF_trial_element;
1974 const int eN_nDOF_mesh_trial_element = eN * nDOF_mesh_trial_element;
1975 //declare local storage for local contributions and initialize
1976 double elementResidual_u[nDOF_test_element], element_entropy_residual[nDOF_test_element], Phi[nDOF_trial_element], Phi_n[nDOF_trial_element];
1977 double elementTransport[nDOF_test_element][nDOF_trial_element], elementTransportConsistent[nDOF_test_element][nDOF_trial_element];
1978 double elementTransportn[nDOF_test_element][nDOF_trial_element], elementTransportConsistentn[nDOF_test_element][nDOF_trial_element];
1979 for (int j = 0; j < nDOF_trial_element; j++) {
1980 const int u_gj = u_l2g.data()[eN_nDOF_trial_element + j];
1981 const int free_gj = r_l2g.data()[eN_nDOF_trial_element + j];
1982 const int x_gj = mesh_l2g.data()[eN_nDOF_mesh_trial_element + j];
1983 const double rho_node_j = rho_dof[free_gj];
1984 Phi[j] = u_dof.data()[u_gj];
1985 Phi_n[j] = u_dof_old.data()[u_gj];
1986 for (int I = 0; I < nSpace; I++) {
1987 // Match the main variable-density operator: grad(u) - (rho/rho0) g.
1988 Phi[j] -= (rho_node_j / rho) * mesh_dof.data()[x_gj * 3 + I] * gravity[I];
1989 Phi_n[j] -= (rho_node_j / rho) * mesh_dof.data()[x_gj * 3 + I] * gravity[I];
1990 }
1991 }
1992 for (int i = 0; i < nDOF_test_element; i++) {
1993 elementResidual_u[i] = 0.0;
1994 element_entropy_residual[i] = 0.0;
1995 for (int j = 0; j < nDOF_trial_element; j++) {
1996 elementTransport[i][j] = 0.0;
1997 elementTransportConsistent[i][j] = 0.0;
1998 elementTransportn[i][j] = 0.0;
1999 elementTransportConsistentn[i][j] = 0.0;
2000 }
2001 }
2002 //loop over quadrature points and compute integrands
2003 for (int k = 0; k < nQuadraturePoints_element; k++) {
2004 //compute indeces and declare local storage
2005 int eN_k = eN * nQuadraturePoints_element + k, eN_k_nSpace = eN_k * nSpace;
2006 double
2007 // for entropy residual
2008 aux_entropy_residual = 0.,
2009 DENTROPY_un, DENTROPY_uni,
2010 //for mass matrix contributions
2011 u = 0.0, un = 0.0, grad_phi[nSpace], grad_phi_n[nSpace], grad_u_velocity[nSpace], velocity_loc[nSpace], u_test_dV[nDOF_trial_element], u_grad_trial[nDOF_trial_element * nSpace], u_grad_test_dV[nDOF_test_element * nSpace],
2012 //for general use
2013 jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace], dV, x, y, z, xt, yt, zt, m, dm, f[nSpace], df[nSpace], a[nnz], da[nnz], as[nnz], mn, dmn, fn[nSpace], dfn[nSpace], an[nnz], dan[nnz], asn[nnz];
2014 //get the physical integration weight
2015 ck.calculateMapping_element(eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y, z);
2016 ck.calculateMappingVelocity_element(eN, k, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), xt, yt, zt);
2017 dV = fabs(jacDet) * dV_ref.data()[k];
2018 //get the solution (of Newton's solver). To compute time derivative term
2019 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_ref.data()[k * nDOF_trial_element], u);
2020 //get the solution at quad point at tn and tnm1 for entropy viscosity
2021 ck.valFromDOF(u_dof_old.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_ref.data()[k * nDOF_trial_element], un);
2022 //get the solution gradients at tn for entropy viscosity
2023 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
2024 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial, grad_u_velocity);
2025
2026 //populate q_x
2027 // const int eN_k_3d = eN_k * 3;
2028 // q_x.data()[eN_k_3d + 0] = x;
2029 // q_x.data()[eN_k_3d + 1] = y;
2030 // q_x.data()[eN_k_3d + 2] = z;
2031 //precalculate test function products with integration weights for mass matrix terms
2032 for (int I = 0; I < nSpace; I++) {
2033 grad_phi[I] = 0.0;
2034 grad_phi_n[I] = 0.0;
2035 }
2036 for (int j = 0; j < nDOF_trial_element; j++) {
2037 u_test_dV[j] = u_test_ref.data()[k * nDOF_trial_element + j] * dV;
2038 for (int I = 0; I < nSpace; I++) {
2039 grad_phi_n[I] += Phi_n[j] * u_grad_trial[j * nSpace + I];
2040 grad_phi[I] += Phi[j] * u_grad_trial[j * nSpace + I];
2041 u_grad_test_dV[j * nSpace + I] = u_grad_trial[j * nSpace + I] * dV; //cek warning won't work for Petrov-Galerkin
2042 }
2043 }
2044 //
2045 //calculate pde coefficients at quadrature points
2046 //
2047 double Kr, dKr, Krn, dKrn, thetaW, thetaWn;
2048 const double rho_local = q_rho.data()[eN_k];
2049 const double rho_velocity = std::fabs(rho_local) > 1.0e-12 ? rho_local : rho;
2050 // conductivity K_s/mu with the transported viscosity: T, a and the coupled
2051 // velocity below all carry it, the nodal upwind k_r and storage do not
2052 double KWs_mu[nnz];
2053 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes[eN] * nnz + ii] / q_mu.data()[eN_k];
2054
2055 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_local, beta, gravity.data(), alpha.data()[elementMaterialTypes[eN]], n.data()[elementMaterialTypes[eN]], thetaR.data()[elementMaterialTypes[eN]], thetaSR.data()[elementMaterialTypes[eN]],
2056 KWs_mu, un, mn, dmn, fn, dfn, an, dan, asn, Krn, dKrn, thetaWn);
2057 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_local, beta, gravity.data(), alpha.data()[elementMaterialTypes[eN]], n.data()[elementMaterialTypes[eN]], thetaR.data()[elementMaterialTypes[eN]], thetaSR.data()[elementMaterialTypes[eN]],
2058 KWs_mu, u, m, dm, f, df, a, da, as, Kr, dKr, thetaW);
2059 q_theta.data()[eN_k] = thetaW;
2060
2061 // Darcy velocity for coupling should use the direct FE gradient of the
2062 // pressure head. The Phi-based gradients are only for stabilization.
2063 for (int I = 0; I < nSpace; ++I) {
2064 q_velocity.data()[eN_k_nSpace + I] = grad_u_velocity[I];
2065 }
2066
2067 double pressure_gradient[nSpace];
2068 const double rho_ratio = rho_velocity / rho;
2069 for (int J = 0; J < nSpace; ++J)
2070 pressure_gradient[J] = grad_u_velocity[J] - rho_ratio * gravity.data()[J];
2071
2072 for (int I = 0; I < nSpace; ++I) {
2073 double acc = 0.0;
2074 for (int ii = a_rowptr.data()[I]; ii < a_rowptr.data()[I+1]; ++ii) {
2075 const int J = a_colind.data()[ii];
2076 acc += (a[ii] / rho_velocity) * pressure_gradient[J];
2077 }
2078 velocity.data()[eN_k_nSpace + I] = -acc;
2079 velocity_couple.data()[eN_k_nSpace + I] = -acc;
2080 }
2081 //
2082 //moving mesh
2083 //
2084 double mesh_velocity[3];
2085 mesh_velocity[0] = xt;
2086 mesh_velocity[1] = yt;
2087 mesh_velocity[2] = zt;
2088 //relative velocity at tn
2089 for (int I = 0; I < nSpace; I++) {
2090 f[I] -= MOVING_DOMAIN * m * mesh_velocity[I];
2091 velocity_loc[I] = df[I] * (2.0 * dm * dm / (dm * dm + fmax(1.0e-16, dm * dm)));
2092 }
2094 // CALCULATE CELL BASED CFL //
2096 calculateCFL(elementDiameter.data()[eN] / degree_polynomial, velocity_loc, cfl.data()[eN_k]);
2098 // CALCULATE ENTROPY RESIDUAL AT QUAD POINT //
2100 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) // EV stab
2101 {
2102 for (int I = 0; I < nSpace; I++) aux_entropy_residual += velocity_loc[I] * grad_phi_n[I];
2103 DENTROPY_un = ENTROPY_TYPE == 1 ? DENTROPY(un, uL, uR) : DENTROPY_LOG(un, uL, uR);
2104 }
2106 // ith-LOOP //
2108 for (int i = 0; i < nDOF_test_element; i++) {
2109 // VECTOR OF ENTROPY RESIDUAL //
2110 int eN_i = eN * nDOF_test_element + i;
2111 ML2[u_l2g.data()[eN_i]] += u_test_dV[i];
2112 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) // EV stab
2113 {
2114 int gi = offset_u + stride_u * u_l2g.data()[eN_i]; //global i-th index
2115 double uni = u_dof_old.data()[gi];
2116 DENTROPY_uni = ENTROPY_TYPE == 1 ? DENTROPY(uni, uL, uR) : DENTROPY_LOG(uni, uL, uR);
2117 element_entropy_residual[i] += (DENTROPY_un - DENTROPY_uni) * aux_entropy_residual * u_test_dV[i];
2118 }
2119
2120 elementResidual_u[i] += m * u_test_dV[i];
2122 // j-th LOOP // To construct transport matrices
2124
2125 for (int j = 0; j < nDOF_trial_element; j++) {
2126 int j_nSpace = j * nSpace;
2127 int i_nSpace = i * nSpace;
2128 elementTransport[i][j] += ck.SimpleDiffusionJacobian_weak(a_rowptr.data(), a_colind.data(), as, &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]);
2129 elementTransportConsistent[i][j] += ck.SimpleDiffusionJacobian_weak(a_rowptr.data(), a_colind.data(), a, &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]);
2130 elementTransportn[i][j] += ck.SimpleDiffusionJacobian_weak(a_rowptr.data(), a_colind.data(), asn, &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]);
2131 elementTransportConsistentn[i][j] += ck.SimpleDiffusionJacobian_weak(a_rowptr.data(), a_colind.data(), an, &u_grad_trial[j_nSpace], &u_grad_test_dV[i_nSpace]);
2132 }
2133 } //i
2134 //save solution for other models
2135 q_u.data()[eN_k] = u;
2136 q_m.data()[eN_k] = m;
2137 }
2139 // DISTRIBUTE // load cell based element into global residual
2141 for (int i = 0; i < nDOF_test_element; i++) {
2142 int eN_i = eN * nDOF_test_element + i;
2143 int gi = offset_u + stride_u * r_l2g.data()[eN_i]; //global i-th index
2144 // distribute entropy_residual
2145 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) // EV Stab
2146 global_entropy_residual[gi] += element_entropy_residual[i];
2147 // distribute transport matrices
2148 for (int j = 0; j < nDOF_trial_element; j++) {
2149 int eN_i_j = eN_i * nDOF_trial_element + j;
2150 TransportMatrix[csrRowIndeces_CellLoops.data()[eN_i] + csrColumnOffsets_CellLoops.data()[eN_i_j]] += elementTransport[i][j];
2151 TransportMatrixConsistent[csrRowIndeces_CellLoops.data()[eN_i] + csrColumnOffsets_CellLoops.data()[eN_i_j]] += elementTransportConsistent[i][j];
2152 TransportMatrixn[csrRowIndeces_CellLoops.data()[eN_i] + csrColumnOffsets_CellLoops.data()[eN_i_j]] += elementTransportn[i][j];
2153 TransportMatrixConsistentn[csrRowIndeces_CellLoops.data()[eN_i] + csrColumnOffsets_CellLoops.data()[eN_i_j]] += elementTransportConsistentn[i][j];
2154 } //j
2155 } //i
2156
2157 }
2158
2159 //loop over exterior element boundaries to calculate surface integrals and load into element and global residuals
2160 //
2161 //ebNE is the Exterior element boundary INdex
2162 //ebN is the element boundary INdex
2163 //eN is the element index
2164 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++) {
2165 int ebN = exteriorElementBoundariesArray.data()[ebNE], eN = elementBoundaryElementsArray.data()[ebN * 2 + 0], ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + 0], eN_nDOF_trial_element = eN * nDOF_trial_element;
2166 double elementResidual_u[nDOF_test_element];
2167 for (int i = 0; i < nDOF_test_element; i++) { elementResidual_u[i] = 0.0; }
2168 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++) {
2169 int ebNE_kb = ebNE * nQuadraturePoints_elementBoundary + kb, ebNE_kb_nSpace = ebNE_kb * nSpace, ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb, ebN_local_kb_nSpace = ebN_local_kb * nSpace;
2170 double u_ext = 0.0, un_ext, grad_u_ext[nSpace], m_ext = 0.0, dm_ext = 0.0, f_ext[nSpace], df_ext[nSpace], a_ext[nnz], da_ext[nnz], as_ext[nnz],
2171 mn_ext = 0.0, dmn_ext = 0.0, fn_ext[nSpace], dfn_ext[nSpace], an_ext[nnz], dan_ext[nnz], asn_ext[nnz], flux_ext = 0.0, bflux_ext = 0.0,
2172 //anb_seepage_flux=0.0, // for flux calculation
2173 bc_u_ext = 0.0, bc_grad_u_ext[nSpace], bc_m_ext = 0.0, bc_dm_ext = 0.0, bc_f_ext[nSpace], bc_df_ext[nSpace], bc_a_ext[nnz], bc_da_ext[nnz], bc_as_ext[nnz], jac_ext[nSpace * nSpace], jacDet_ext, jacInv_ext[nSpace * nSpace], boundaryJac[nSpace * (nSpace - 1)], metricTensor[(nSpace - 1) * (nSpace - 1)], metricTensorDetSqrt, dS, u_test_dS[nDOF_test_element], u_grad_trial_trace[nDOF_trial_element * nSpace], normal[3], x_ext, y_ext, z_ext, xt_ext, yt_ext, zt_ext, integralScaling, G[nSpace * nSpace], G_dd_G, tr_G, fluxJacobian_u_u[nDOF_trial_element], bfluxJacobian_u_u[nDOF_trial_element], fluxJacobian_un_un[nDOF_trial_element], cfluxJacobian_u_u[nDOF_trial_element];
2174 for (int j = 0; j < nDOF_trial_element; j++) {
2175 fluxJacobian_u_u[j] = 0.0; bfluxJacobian_u_u[j] = 0.0; fluxJacobian_un_un[j] = 0.0; cfluxJacobian_u_u[j] = 0.0;
2176 }
2177 //\
2178 //calculate the solution and gradients at quadrature points
2179 //
2180 //compute information about mapping from reference element to physical element
2181 ck.calculateMapping_elementBoundary(eN, ebN_local, kb, ebN_local_kb, mesh_dof.data(), mesh_l2g.data(), mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(), boundaryJac_ref.data(), jac_ext, jacDet_ext, jacInv_ext, boundaryJac, metricTensor, metricTensorDetSqrt,
2182 normal_ref.data(), normal, x_ext, y_ext, z_ext);
2183 ck.calculateMappingVelocity_elementBoundary(eN, ebN_local, kb, ebN_local_kb, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_trace_ref.data(), xt_ext, yt_ext, zt_ext, normal, boundaryJac, metricTensor, integralScaling);
2184 dS = ((1.0 - MOVING_DOMAIN) * metricTensorDetSqrt + MOVING_DOMAIN * integralScaling) * dS_ref.data()[kb];
2185 //get the metric tensor
2186 //cek todo use symmetry
2187 ck.calculateG(jacInv_ext, G, G_dd_G, tr_G);
2188 //compute shape and solution information
2189 //shape
2190 ck.gradTrialFromRef(&u_grad_trial_trace_ref.data()[ebN_local_kb_nSpace * nDOF_trial_element], jacInv_ext, u_grad_trial_trace);
2191 //solution and gradient
2192 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);
2193 ck.valFromDOF(u_dof_old.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element], un_ext);
2194 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial_trace, grad_u_ext);
2195
2196 //populate ebqe_x
2197 // const int ebNE_kb_3d = ebNE_kb * 3;
2198 // ebqe_x.data()[ebNE_kb_3d + 0] = x_ext;
2199 // ebqe_x.data()[ebNE_kb_3d + 1] = y_ext;
2200 // ebqe_x.data()[ebNE_kb_3d + 2] = z_ext;
2201
2202
2203 //precalculate test function products with integration weights
2204 for (int j = 0; j < nDOF_trial_element; j++) { u_test_dS[j] = u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + j] * dS; }
2205 //
2206 //load the boundary values
2207 //
2208 bc_u_ext = isDOFBoundary_u.data()[ebNE_kb] * ebqe_bc_u_ext.data()[ebNE_kb] + (1 - isDOFBoundary_u.data()[ebNE_kb]) * u_ext;
2209 //
2210 //calculate the pde coefficients using the solution and the boundary values for the solution
2211 //
2212 double bc_Kr, bc_dKr,bc_Kr_ext, bc_dKr_ext, bc_Krn, bc_dKrn, thetaW_ext, thetaWn_ext, thetaW_bc_ext;
2213 const double rho_ext = ebqe_rho.data()[ebNE_kb];
2214 const double rho_velocity_ext = std::fabs(rho_ext) > 1.0e-12 ? rho_ext : rho;
2215 double KWs_mu[nnz];
2216 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes.data()[eN] * nnz + ii] / ebqe_mu.data()[ebNE_kb];
2217
2218 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
2219 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, u_ext, m_ext, dm_ext, f_ext, df_ext, a_ext, da_ext, as_ext, bc_Kr, bc_dKr, thetaW_ext);
2220 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
2221 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, un_ext, mn_ext, dmn_ext, fn_ext, dfn_ext, an_ext, dan_ext, asn_ext, bc_Krn, bc_dKrn, thetaWn_ext);
2222 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_ext, beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
2223 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, bc_u_ext, bc_m_ext, bc_dm_ext, bc_f_ext, bc_df_ext, bc_a_ext, bc_da_ext, bc_as_ext, bc_Kr_ext,bc_dKr_ext, thetaW_bc_ext);
2224 ebqe_theta.data()[ebNE_kb] = thetaW_ext;
2225 //
2226 //Calculate Darcy Velocity at external faces
2227 //
2228
2229 // double darcy_velocity_loc_ext[nSpace];
2230 // for (int I = 0; I < nSpace; I++) { darcy_velocity_loc_ext[I] = 0.0; }
2231
2232 // for (int I = 0; I < nSpace; I++) {
2233 // for (int J = 0; J < nSpace; J++) { darcy_velocity_loc_ext[I] -= bc_Kr * KWs.data()[elementMaterialTypes[eN] * nSpace * nSpace + I * nSpace + J] * (grad_u_ext[J]+ gravity.data()[J]); }
2234 // }
2235 // for (int I = 0; I < nSpace; I++) { ebqe_velocity_ext_couple.data()[ebNE_kb_nSpace + I] = darcy_velocity_loc_ext[I] ; }
2236
2237 double ext_pressure_gradient[nSpace];
2238 const double rho_ratio_ext = rho_velocity_ext / rho;
2239 for (int J = 0; J < nSpace; ++J)
2240 ext_pressure_gradient[J] = grad_u_ext[J] - rho_ratio_ext * gravity.data()[J];
2241
2242 for (int I = 0; I < nSpace; ++I) {
2243 double acc = 0.0;
2244 for (int ii = a_rowptr.data()[I]; ii < a_rowptr.data()[I+1]; ++ii) {
2245 const int J = a_colind.data()[ii];
2246 acc += (a_ext[ii] / rho_velocity_ext) * ext_pressure_gradient[J];
2247 }
2248 ebqe_velocity_ext.data()[ebNE_kb_nSpace + I] = -acc;
2249 ebqe_velocity_ext_couple.data()[ebNE_kb_nSpace + I] = -acc;
2250 }
2251
2252
2253 //
2254 //calculate the numerical fluxes
2255 //
2256 bool useConsistentFlux=false;
2257 if (useConsistentFlux) {
2258 exteriorNumericalFlux(ebqe_bc_flux_ext[ebNE_kb], a_rowptr.data(), a_colind.data(),
2259 isSeepageFace.data()[ebNE], //tricky, this is a face flag not face quad
2260 isDOFBoundary_u.data()[ebNE_kb], normal, bc_u_ext, a_ext, grad_u_ext, u_ext, f_ext,
2261 ebqe_penalty_ext.data()[ebNE_kb], // penalty,
2262 flux_ext);
2263 } else {
2264 exteriorNumericalFlux2(ebqe_bc_flux_ext[ebNE_kb], a_rowptr.data(), a_colind.data(),
2265 isSeepageFace.data()[ebNE], //tricky, this is a face flag not face quad
2266 isDOFBoundary_u.data()[ebNE_kb], normal, bc_u_ext, a_ext, grad_u_ext, u_ext, f_ext,
2267 ebqe_penalty_ext.data()[ebNE_kb], // penalty,
2268 flux_ext, bflux_ext);
2269 }
2270
2271 ebqe_flux.data()[ebNE_kb] = flux_ext;
2272
2273 anb_seepage_flux = seepagefluxcalculator(anb_seepage_flux, isSeepageFace.data()[ebNE], dS, flux_ext);
2274 anb_seepage_flux_n.data()[0] = anb_seepage_flux;
2275 ebqe_u.data()[ebNE_kb] = u_ext;
2276 // see the note at the ebqe_bflux declaration: bflux_ext, not flux_ext,
2277 // is what the residual below integrates when useConsistentFlux = false
2278 ebqe_bflux.data()[ebNE_kb] = useConsistentFlux ? flux_ext : bflux_ext;
2279 ebqe_dS.data()[ebNE_kb] = dS;
2280 //the coupled-velocity pass below needs the outward normal, and ebqe here
2281 //is hand-built and carries none
2282 if (bcCons)
2283 for (int I = 0; I < nSpace; I++) ebqe_normal_cache[ebNE_kb_nSpace + I] = normal[I];
2284 //seed the FCT bounds with the imposed value. min_m_bc/max_m_bc are otherwise left at +-1e10, so
2285 //Zalesak's bounds at a weakly imposed Dirichlet DOF come only from the neighbours' low order masses
2286 //and the limiter clamps the boundary layer. theta(bc_u) uses the DOF's own soil and density so the
2287 //bound lies on the same retention curve as mLow in the edge loop below. done after the flux call so
2288 //the seepage active set is current
2289 if (isDOFBoundary_u.data()[ebNE_kb]) {
2290 double bc_u_dof = isSeepageFace.data()[ebNE] ? 0.0 : ebqe_bc_u_ext.data()[ebNE_kb];
2291 for (int i = 0; i < nDOF_test_element; i++) {
2292 if (u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + i] <= 1.0e-12) continue; //DOF not on this face
2293 int free_gi = r_l2g.data()[eN * nDOF_test_element + i], mat_gi = freeDOFMaterialTypes.data()[free_gi];
2294 double m_bc, dm_bc, f_bc[nSpace], df_bc[nSpace], a_bc[nnz], da_bc[nnz], as_bc[nnz], Kr_bc, dKr_bc, thetaW_bc;
2295 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_dof[free_gi], beta, gravity.data(), alpha.data()[mat_gi],
2296 n.data()[mat_gi], thetaR.data()[mat_gi], thetaSR.data()[mat_gi], &KWs.data()[mat_gi * nnz], bc_u_dof,
2297 m_bc, dm_bc, f_bc, df_bc, a_bc, da_bc, as_bc, Kr_bc, dKr_bc, thetaW_bc);
2298 min_m_bc.data()[free_gi] = fmin(min_m_bc.data()[free_gi], m_bc);
2299 max_m_bc.data()[free_gi] = fmax(max_m_bc.data()[free_gi], m_bc);
2300 }
2301 }
2302 //
2303 //update residuals
2304 //
2305 for (int i = 0; i < nDOF_test_element; i++) {
2306 if (useConsistentFlux) {
2307 elementResidual_u[i] += ck.ExteriorElementBoundaryFlux(flux_ext, u_test_dS[i]);
2308 } else {
2309 elementResidual_u[i] += ck.ExteriorElementBoundaryFlux(bflux_ext, u_test_dS[i]);
2310 }
2311 // see the consBoundaryFlux note: mark the rows the DOF loop must
2312 // accumulate the conservative boundary flux for (face DOFs only, the
2313 // off-face vertex has a zero trace), and split this DOF's boundary
2314 // star into the weakly imposed part, whose flux the conservative pass
2315 // rewrites, and the part a flux BC already pins exactly. isDOFBoundary
2316 // is read AFTER the flux call, so a seepage face that just shut counts
2317 // as prescribed (zero), which is what the residual got.
2318 if (u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + i] > 1.0e-12) {
2319 const int gi = r_l2g.data()[eN * nDOF_test_element + i];
2320 is_bdry_dof[gi] = 1;
2321 if (isDOFBoundary_u.data()[ebNE_kb])
2322 bdryDirichletArea[gi] += u_test_dS[i];
2323 else
2324 //prescribed: a flux BC, a seepage face that shut, or the
2325 //subdomain's artificial boundary -- all three carry bflux_ext
2326 //into the residual, and it is zero for the last two
2327 bdryPrescribedFlux[gi] += bflux_ext * u_test_dS[i];
2328 }
2329 } //i
2330 for (int j = 0; j < nDOF_trial_element; j++) {
2331 if (useConsistentFlux) {
2332 exteriorNumericalFluxJacobian(a_rowptr.data(), a_colind.data(), isDOFBoundary_u.data()[ebNE_kb], normal, a_ext, da_ext, grad_u_ext, &u_grad_trial_trace[j * nSpace], df_ext, u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element + j],
2333 ebqe_penalty_ext.data()[ebNE_kb], //penalty,
2334 fluxJacobian_u_u[j]);
2335 } else {
2336 exteriorNumericalFluxJacobian2(a_rowptr.data(), a_colind.data(), isDOFBoundary_u.data()[ebNE_kb], normal, as_ext, a_ext, da_ext, grad_u_ext, &u_grad_trial_trace[j * nSpace], df_ext, u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element + j],
2337 ebqe_penalty_ext.data()[ebNE_kb], //penalty,
2338 fluxJacobian_u_u[j],bfluxJacobian_u_u[j],cfluxJacobian_u_u[j]);
2339 }
2340 //probably need isDOFBoundary_un here
2341 //exteriorNumericalFluxJacobian(a_rowptr.data(), a_colind.data(), isDOFBoundary_u.data()[ebNE_kb], normal, asn_ext, dan_ext, grad_u_ext, &u_grad_trial_trace[j * nSpace], dfn_ext, u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element + j],
2342 // ebqe_penalty_ext.data()[ebNE_kb], //penalty,
2343 // fluxJacobian_un_un[j]);
2344 } //j
2345 //
2346 //update the element and global residual storage
2347 //
2348 for (int i = 0; i < nDOF_test_element; i++) {
2349 int eN_i = eN * nDOF_test_element + i;
2350 for (int j = 0; j < nDOF_trial_element; j++) {
2351 int ebN_i_j = ebN * 4 * nDOF_test_X_trial_element + i * nDOF_trial_element + j;
2352 if (useConsistentFlux) {
2353 globalJacobian.data()[csrRowIndeces_u_u[eN_i] + csrColumnOffsets_eb_u_u[ebN_i_j]] += fluxJacobian_u_u[j] * u_test_dS[i];
2354 } else {
2355 globalJacobian.data()[csrRowIndeces_u_u[eN_i] + csrColumnOffsets_eb_u_u[ebN_i_j]] += bfluxJacobian_u_u[j] * u_test_dS[i];
2356 const int ij_eb = csrRowIndeces_u_u[eN_i] + csrColumnOffsets_eb_u_u[ebN_i_j];
2357 if (useBndDiff) {
2358 //Step B: park the boundary block in TB. TransportMatrix/Consistent stay pure
2359 //interior, so they keep the symmetry and zero row sums the edge loop relies on.
2360 TransportBoundary.data()[ij_eb] += cfluxJacobian_u_u[j] * u_test_dS[i];
2361 } else {
2362 TransportMatrix[ij_eb] += fluxJacobian_u_u[j] * u_test_dS[i];
2363 //also keep the IIPG block on its own, so the DOF loop can recover the
2364 //SYMMETRIC interior operator T = TransportMatrix - TransportBoundary for
2365 //the conservative boundary flux export (consBoundaryFlux). Nothing else
2366 //reads TransportBoundary when useBndDiff is off, so the residual, the
2367 //Jacobian and the FCT=False solution are unchanged by this line.
2368 TransportBoundary.data()[ij_eb] += fluxJacobian_u_u[j] * u_test_dS[i];
2369 //k_rw-weighted: this array is read ONLY by fH in the edge loop below, so this
2370 //line cannot change the residual, the Jacobian or the FCT=False solution.
2371 //FCT_BND_KRW=False keeps the old Ks-based boundary block.
2372 TransportMatrixConsistent[ij_eb] +=
2373 (useBndKrw ? cfluxJacobian_u_u[j] : fluxJacobian_u_u[j]) * u_test_dS[i];
2374 }
2375 TransportMatrixn[ij_eb] += fluxJacobian_un_un[j] * u_test_dS[i];
2376 TransportMatrixConsistentn[ij_eb] += fluxJacobian_un_un[j] * u_test_dS[i];
2377 }
2378 } //j
2379 } //i
2380 //Step B Jacobian, dK part. TB_ij = int -(K grad(w_j)).n w_i dS with K = a_ext(psi_h),
2381 //so row i's boundary term Theta*sum_j TB_ij*dphi_ij also depends on psi through K.
2382 //The edge loop sees TB only as assembled numbers and adds Theta*TB_ij alone; the
2383 //derivative of K needs da_ext and the trace w_k at this quadrature point, so it is
2384 //added here: d/du_k = Theta * sum_q w_k w_i dS * sum_j G_j dphi_ij,
2385 //G_j = -(dK grad(w_j)).n, with dphi_ij built exactly as in the edge loop.
2386 if (useBndDiff && !useConsistentFlux && isDOFBoundary_u.data()[ebNE_kb]) {
2387 for (int i = 0; i < nDOF_test_element; i++) {
2388 const int eN_i = eN * nDOF_test_element + i;
2389 const int gi = r_l2g.data()[eN_i];
2390 double dK_flux = 0.0;
2391 for (int j = 0; j < nDOF_trial_element; j++) {
2392 const int gj = r_l2g.data()[eN_nDOF_trial_element + j];
2393 double G_j = 0.0;
2394 for (int I = 0; I < nSpace; I++)
2395 for (int m = a_rowptr.data()[I]; m < a_rowptr.data()[I + 1]; m++)
2396 G_j -= da_ext[m] * u_grad_trial_trace[j * nSpace + a_colind.data()[m]] * normal[I];
2397 const double rho_edge = 0.5 * (rho_dof[gi] + rho_dof[gj]);
2398 double dphi = u_free_dof[gj] - u_free_dof[gi];
2399 for (int I = 0; I < nSpace; I++)
2400 dphi -= (rho_edge / rho) * gravity.data()[I] * (mesh_dof.data()[gj * 3 + I] - mesh_dof.data()[gi * 3 + I]);
2401 dK_flux += G_j * dphi;
2402 }
2403 for (int k = 0; k < nDOF_trial_element; k++) {
2404 const int ebN_i_k = ebN * 4 * nDOF_test_X_trial_element + i * nDOF_trial_element + k;
2405 globalJacobian.data()[csrRowIndeces_u_u[eN_i] + csrColumnOffsets_eb_u_u[ebN_i_k]] +=
2406 bc_mask.data()[gi] * Theta * dK_flux * u_trial_trace_ref.data()[ebN_local_kb * nDOF_test_element + k] * u_test_dS[i];
2407 }
2408 }
2409 }
2410 } //kb
2411 for (int i = 0; i < nDOF_test_element; i++) {
2412 int eN_i = eN * nDOF_test_element + i;
2413 globalResidual.data()[offset_u + stride_u * u_l2g.data()[eN_i]] += elementResidual_u[i];
2414 boundaryFluxDOF.data()[r_l2g.data()[eN_i]] -= elementResidual_u[i];
2415 }//i
2416 } //ebNE
2418 // COMPUTE SMOOTHNESS INDICATOR and NORMALIZE ENTROPY RESIDUAL //
2420 // NOTE: see NCLS.h for a different but equivalent implementation of this.
2421 int ij = 0;
2422 //Heap-backed member, not a stack VLA -- see the declarations in the class body.
2423 cflux_member.assign(numDOFs, 0.0);
2424 std::vector<double> &cflux = cflux_member;
2425 for (int i = 0; i < numDOFs; i++) {
2426 double gi[nSpace], Cij[nSpace], xi[nSpace], etaMaxi, etaMini;
2427 double solni = u_free_dof_old[i];
2428 for (int I = 0; I < nSpace; I++) {
2429 solni -= (rho_dof[i] / rho) * gravity.data()[I] * mesh_dof.data()[i * 3 + I];
2430 }
2431 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) //EV Stabilization
2432 {
2433 // For eta min and max
2434 etaMaxi = fabs(eta[i]);
2435 etaMini = fabs(eta[i]);
2436 }
2437 // initialize gi and compute xi
2438 for (int I = 0; I < nSpace; I++) {
2439 gi[I] = 0.;
2440 xi[I] = mesh_dof.data()[i * 3 + I];
2441 }
2442 // for smoothness indicator //
2443 double alpha_numerator_pos = 0., alpha_numerator_neg = 0., alpha_denominator_pos = 0., alpha_denominator_neg = 0.;
2444 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) { // First loop in j (sparsity pattern)
2445 int j = csrColumnOffsets_DofLoops.data()[offset];
2446 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) //EV Stabilization
2447 {
2448 // COMPUTE ETA MIN AND ETA MAX //
2449 etaMaxi = fmax(etaMaxi, fabs(eta[j]));
2450 etaMini = fmin(etaMini, fabs(eta[j]));
2451 }
2452 double solnj = u_free_dof_old[j];
2453 for (int I = 0; I < nSpace; I++) {
2454 solnj -= (rho_dof[j] / rho) * gravity.data()[I] * mesh_dof.data()[j * 3 + I];
2455 }
2456 // Update Cij matrices
2457 Cij[0] = Cx[ij];
2458#if nSpace == 2
2459 Cij[1] = Cy[ij];
2460#endif
2461#if nSpace == 3
2462 Cij[2] = Cz[ij];
2463#endif
2464 // COMPUTE gi VECTOR. gi=1/mi*sum_j(Cij*solj)
2465 for (int I = 0; I < nSpace; I++) gi[I] += Cij[I] * solnj;
2466
2467 // COMPUTE numerator and denominator of smoothness indicator
2468 double alpha_num = solni - solnj;
2469 if (alpha_num >= 0.) {
2470 alpha_numerator_pos += alpha_num;
2471 alpha_denominator_pos += alpha_num;
2472 } else {
2473 alpha_numerator_neg += alpha_num;
2474 alpha_denominator_neg += fabs(alpha_num);
2475 }
2476 //update ij
2477 ij += 1;
2478 }
2479 // scale g vector by lumped mass matrix
2480 //double mass_matrix_error = abs(ML.data()[i] - ML2[i]);
2481 //if (mass_matrix_error > 1.0e-16) std::cout << mass_matrix_error<<" ML " << ML.data()[i] << '\t' << ML2[i] << std::endl;
2482 for (int I = 0; I < nSpace; I++) gi[I] /= ML.data()[i];
2483 if (STABILIZATION_TYPE == STABILIZATION::EV_Stab) //EV Stab
2484 {
2485 // Normalizae entropy residual
2486 global_entropy_residual[i] *= etaMini == etaMaxi ? 0. : 2 * cE / (etaMaxi - etaMini);
2487 quantDOFs.data()[i] = fabs(global_entropy_residual[i]);
2488 }
2489
2490 // Now that I have the gi vectors, I can use them for the current i-th DOF
2491 double SumPos = 0., SumNeg = 0.;
2492 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) { // second loop in j (sparsity pattern)
2493 int j = csrColumnOffsets_DofLoops.data()[offset];
2494 // compute xj
2495 double xj[nSpace];
2496 for (int I = 0; I < nSpace; I++) xj[I] = mesh_dof.data()[j * 3 + I];
2497 // compute gi*(xi-xj)
2498 double gi_times_x = 0.;
2499 for (int I = 0; I < nSpace; I++) {
2500 gi_times_x += gi[I] * delta_x_ij.data()[offset * 3 + I];
2501 }
2502 // compute the positive and negative part of gi*(xi-xj)
2503 SumPos += gi_times_x > 0 ? gi_times_x : 0;
2504 SumNeg += gi_times_x < 0 ? gi_times_x : 0;
2505 }
2506 double sigmaPosi = fmin(1., (fabs(SumNeg) + 1E-15) / (SumPos + 1E-15));
2507 double sigmaNegi = fmin(1., (SumPos + 1E-15) / (fabs(SumNeg) + 1E-15));
2508 double alpha_numi = fabs(sigmaPosi * alpha_numerator_pos + sigmaNegi * alpha_numerator_neg);
2509 double alpha_deni = sigmaPosi * alpha_denominator_pos + sigmaNegi * alpha_denominator_neg;
2510 if (IS_BETAij_ONE == 1) {
2511 alpha_numi = fabs(alpha_numerator_pos + alpha_numerator_neg);
2512 alpha_deni = alpha_denominator_pos + alpha_denominator_neg;
2513 }
2514 double alphai = alpha_numi / (alpha_deni + 1E-15);
2515 quantDOFs.data()[i] = alphai;
2516
2517 if (POWER_SMOOTHNESS_INDICATOR == 0) psi[i] = 1.0;
2518 else psi[i] = std::pow(alphai, POWER_SMOOTHNESS_INDICATOR); //NOTE: they use alpha^2 in the paper
2519 }
2521 // ** LOOP IN DOFs FOR EDGE BASED TERMS ** //
2523 ij = 0;
2524 for (int i = 0; i < numDOFs; i++) {
2525 int ii;
2526 double sum_abs_dt_times_fH_minus_fL = 0.0, MLi = ML.data()[i];
2527 double Kr, dKr, Krn, dKrn;
2528 double J_ii = 0.0;
2529 double ith_dissipative_term = 0;
2530 double ith_low_order_dissipative_term = 0;
2531 double ith_flux_term = 0;
2532 double ith_consistent_flux_term = 0;
2533 double dLii = 0.;
2534 double m, dm, f[nSpace], df[nSpace], a[nnz], da[nnz], as[nnz];
2535 double dmn, fn[nSpace], dfn[nSpace], an[nnz], dan[nnz], asn[nnz];
2536
2537 const double rho_i = rho_dof[i];
2538 // Rock region of each DOF. The nodal m, mn and upwind k_rw below are
2539 // evaluated at a DOF, so they take that DOF's soil; a single
2540 // elementMaterialTypes[0] here would put the whole mesh on one retention
2541 // curve and, since invert() maps m back with the per-DOF soil, would make
2542 // psi -> m -> psi something other than the identity.
2543 const int mat_i = freeDOFMaterialTypes.data()[i];
2544
2545 double thetaW_tmp = 0.0;
2546 // loop over the sparsity pattern of the i-th DOF
2547 for (int offset = csrRowIndeces_DofLoops.data()[i]; offset < csrRowIndeces_DofLoops.data()[i + 1]; offset++) {
2548 int j = csrColumnOffsets_DofLoops.data()[offset];
2549 if (i == j) ii = ij;
2550 const double ith_flux_term_before = ith_flux_term;
2551 const double rho_j = rho_dof[j];
2552 const int mat_j = freeDOFMaterialTypes.data()[j];
2553 const double rho_edge = 0.5 * (rho_i + rho_j);
2554 double delta_phi = u_free_dof[j] - u_free_dof[i];
2555 double delta_phin = u_free_dof_old[j] - u_free_dof_old[i];
2556 // Match evaluateCoefficients(): use an edge-based hydrostatic jump
2557 // with local density scaling rho/rho0, rather than grad(rho g·x),
2558 // which would introduce a spurious (g·x) grad(rho) term.
2559 for (int I = 0; I < nSpace; I++) {
2560 const double delta_x = mesh_dof.data()[j * 3 + I] - mesh_dof.data()[i * 3 + I];
2561 const double hydrostatic_jump = (rho_edge / rho) * gravity.data()[I] * delta_x;
2562 delta_phi -= hydrostatic_jump;
2563 delta_phin -= hydrostatic_jump;
2564 }
2565 double dLowij, dLij, dEVij, dHij, fH, fL, fA=0.0;
2566 double fL_CN =0.0, fA_CN=0.0 ;
2567 // High-order (Galerkin) flux with its own time weight Theta_h (ThetaScheme.theta);
2568 // the low-order fL below keeps Theta, which LevelModel sets to 1 (backward Euler).
2569 fH = -Theta_h * TransportMatrixConsistent[ij] * delta_phi - (1 - Theta_h) * TransportMatrixConsistentn[ij] * delta_phin;
2570 ith_consistent_flux_term += fH;
2571 fA = fH;
2572 fA_CN = fH;
2573
2574 if (-TransportMatrix[ij] * delta_phi <= 0.0) {
2575 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_i, beta, gravity.data(),
2576 alpha.data()[mat_i],
2577 n.data()[mat_i], thetaR.data()[mat_i], thetaSR.data()[mat_i], &KWs.data()[mat_i * nnz], u_free_dof[i], m, dm, f, df, a, da, as, Kr, dKr, thetaW_tmp);
2578 fL = Theta * Kr * fmax(0.0, -TransportMatrix[ij]) * delta_phi;
2579 fL_CN = Theta_h * Kr * fmax(0.0, -TransportMatrix[ij]) * delta_phi;
2580
2581 if (i != j) {
2582 globalJacobian.data()[ij] -= Theta * Kr * fmax(0.0, -TransportMatrix[ij]);
2583 J_ii -= -Theta * Kr * fmax(0.0, -TransportMatrix[ij]) + dKr_on * Theta * dKr * fmax(0.0, -TransportMatrix[ij]) * delta_phi;
2584 }
2585 ith_flux_term += fL;
2586 fA -= fL;
2587 } else {
2588 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_j, beta, gravity.data(),
2589 alpha.data()[mat_j],
2590 n.data()[mat_j], thetaR.data()[mat_j], thetaSR.data()[mat_j], &KWs.data()[mat_j * nnz], u_free_dof[j], m, dm, f, df, a, da, as, Kr, dKr, thetaW_tmp);
2591 fL = Theta * Kr * fmax(0.0, -TransportMatrix[ij]) * delta_phi;
2592 fL_CN = Theta_h * Kr * fmax(0.0, -TransportMatrix[ij]) * delta_phi;
2593
2594 if (i != j) {
2595 globalJacobian.data()[ij] -= Theta * Kr * fmax(0.0, -TransportMatrix[ij]) + dKr_on * Theta * dKr * fmax(0.0, -TransportMatrix[ij]) * delta_phi;
2596 J_ii -= -Theta * Kr * fmax(0.0, -TransportMatrix[ij]);
2597 }
2598 ith_flux_term += fL;
2599 fA -= fL;
2600 }
2601 // new-time low-order flux; on a boundary row it is replaced below by its
2602 // interior part (the weak-Dirichlet block is not an edge exchange)
2603 double fL_int = fL;
2604 if (-TransportMatrixn[ij] * delta_phin <= 0.0) {
2605 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_i, beta, gravity.data(),
2606 alpha.data()[mat_i],
2607 n.data()[mat_i], thetaR.data()[mat_i], thetaSR.data()[mat_i], &KWs.data()[mat_i * nnz], u_free_dof_old[i], m, dm, f, df, a, da, as, Kr, dKr, thetaW_tmp);
2608 fL = (1 - Theta) * Kr * fmax(0.0, -TransportMatrixn[ij]) * delta_phin;
2609 fL_CN += (1 - Theta_h) * Kr * fmax(0.0, -TransportMatrixn[ij]) * delta_phin;
2610 ith_flux_term += fL;
2611 fA -= fL;
2612 fA_CN -= fL_CN;
2613 } else {
2614 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_j, beta, gravity.data(),
2615 alpha.data()[mat_j],
2616 n.data()[mat_j], thetaR.data()[mat_j], thetaSR.data()[mat_j], &KWs.data()[mat_j * nnz], u_free_dof_old[j], m, dm, f, df, a, da, as, Kr, dKr, thetaW_tmp);
2617 fL = (1 - Theta) * Kr * fmax(0.0, -TransportMatrixn[ij]) * delta_phin;
2618 fL_CN += (1 - Theta_h) * Kr * fmax(0.0, -TransportMatrixn[ij]) * delta_phin;
2619 ith_flux_term += fL;
2620 fA -= fL;
2621 fA_CN -= fL_CN;
2622 }
2623 if (useBndDiff) {
2624 //Weak-Dirichlet block, kept out of the fmax() clip above and applied identically
2625 //in the low and the high order flux, so it never enters fA (see Step B above).
2626 const double TB_ij = TransportBoundary.data()[ij];
2627 const double f_b = -Theta * TB_ij * delta_phi;
2628 ith_consistent_flux_term += f_b;
2629 ith_flux_term += f_b;
2630 if (i != j) {
2631 //the low order boundary operator is what the residual and hence Newton sees
2632 globalJacobian.data()[ij] -= Theta * (-TB_ij);
2633 J_ii -= Theta * TB_ij;
2634 }
2635 }
2636 // CONSERVATIVE BOUNDARY FLUX (see the consBoundaryFlux note at the top):
2637 // the symmetric part of this row's edge flux, i.e. fL rebuilt on
2638 // T^int = T - TB with the same upwind rule. T^int is symmetric and
2639 // dphi antisymmetric, so this flux telescopes exactly against the
2640 // interior and is the water node i really exchanges with the domain.
2641 // With useBndDiff on TransportMatrix is already pure interior.
2642 if (is_bdry_dof[i] && i != j) {
2643 const double Tint_ij = TransportMatrix[ij] - (useBndDiff ? 0.0 : TransportBoundary.data()[ij]);
2644 double Kr_c = 0.0, dKr_c = 0.0;
2645 if (-Tint_ij * delta_phi <= 0.0)
2646 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_i, beta, gravity.data(),
2647 alpha.data()[mat_i],
2648 n.data()[mat_i], thetaR.data()[mat_i], thetaSR.data()[mat_i], &KWs.data()[mat_i * nnz], u_free_dof[i], m, dm, f, df, a, da, as, Kr_c, dKr_c, thetaW_tmp);
2649 else
2650 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_j, beta, gravity.data(),
2651 alpha.data()[mat_j],
2652 n.data()[mat_j], thetaR.data()[mat_j], thetaSR.data()[mat_j], &KWs.data()[mat_j * nnz], u_free_dof[j], m, dm, f, df, a, da, as, Kr_c, dKr_c, thetaW_tmp);
2653 fL_int = Theta * Kr_c * fmax(0.0, -Tint_ij) * delta_phi;
2654 consBoundaryFlux.data()[i] += fL_int;
2655 }
2656 edgeFluxInterior.data()[ij] = fL_int;
2657 dt_times_fH_minus_fL.data()[ij] = dt * fA;
2658 //dt_times_fH_minus_fL.data()[ij] = dt * fA_CN;
2659 edgeFluxLow.data()[ij] = ith_flux_term - ith_flux_term_before;
2660 ij += 1;
2661 }
2662 // The nodal source is part of the low-order rate, so the consistent-mass FCT
2663 // term built from mDotLow sees it as well as the residual. It is not an edge
2664 // exchange: it stays out of edgeFluxLow, and a coupled transport model finds
2665 // it in its nodal boundary flux B_i.
2666 ith_flux_term += MLi * nodalSource.data()[i];
2667 cflux[i] = ith_consistent_flux_term;
2668 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_i, beta, gravity.data(),
2669 alpha.data()[mat_i],
2670 n.data()[mat_i], thetaR.data()[mat_i], thetaSR.data()[mat_i], &KWs.data()[mat_i * nnz], u_free_dof[i], m, dm, f, df, a, da, as, Kr, dKr, thetaW_tmp);
2671 // Old-time mass with the OLD density: mn = theta(psi^n)*rho^{n-1}, the mass
2672 // the previous step produced. With the current rho_i here, both levels of
2673 // the storage term shared one rho and theta*d(rho)/dt was dropped.
2674 const double rho_old_i = USE_RHO_NODAL_OLD ? rho_nodal_old.data()[i] : rho_i;
2675 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, rho_old_i, beta, gravity.data(),
2676 alpha.data()[mat_i],
2677 n.data()[mat_i], thetaR.data()[mat_i], thetaSR.data()[mat_i], &KWs.data()[mat_i * nnz], u_free_dof_old[i], mn.data()[i], dmn, fn, dfn, an, dan, asn, Krn, dKrn, thetaW_tmp);
2678 // mDotLow enters the FCT mass antidiffusion MC_ij (mDotLow_i - mDotLow_j). At a strong-Dirichlet
2679 // DOF the row is masked, so the flux divergence is not dm/dt there; use the time difference of the
2680 // imposed data instead. (Without this the WRR FCT is only first order on Q1, Hornung-Messing.)
2681 mDotLow.data()[i] = (bc_mask.data()[i] == 0.0) ? (m - mn.data()[i]) / dt : ith_flux_term / MLi;
2682 mLow.data()[i] = m;
2683 // thetaW_tmp is whatever the LAST evaluateCoefficients left, i.e. the old-time
2684 // call immediately above -- so this is theta(psi^n)_i, the partner of mn_i.
2685 thetaW_dof.data()[i] = thetaW_tmp;
2686 globalResidual.data()[i] += bc_mask.data()[i] * (MLi * (m - mn.data()[i]) / dt - ith_flux_term);
2687 globalJacobian.data()[ii] += bc_mask.data()[i] * (MLi * ((LSCHEME_L > 0.0) ? LSCHEME_L : dm) / dt + J_ii) + (1.0 - bc_mask.data()[i]);
2688 }
2689 if (STABILIZATION_TYPE == STABILIZATION::Implicit_FCT) {
2690 //FCTStep(args);
2691 for (int i = 0; i < numDOFs; i++) {
2692 globalResidual.data()[i] += fluxCorrection.data()[i];
2693 }
2694 }
2696 // CONSERVATIVE BOUNDARY VELOCITY FOR THE COUPLED TRANSPORT MODEL
2697 //
2698 // The cell loop above hands the transport model a pointwise Darcy trace
2699 // -(a/rho)(grad psi - rho_r g); on the boundary that is NOT the water this
2700 // scheme moves, because the head is imposed weakly (penalty + IIPG rows in
2701 // TransportMatrix). consBoundaryFlux, assembled in the DOF loop, IS that
2702 // water: F_i = sum_j Kr_up max(0,-T^int_ij) dphi_ij, the flux of the
2703 // symmetric interior operator out of node i, which telescopes exactly with
2704 // the interior edge fluxes and equals bflux_i minus the IIPG rows at
2705 // convergence. Keep the tangential trace, replace the normal component:
2706 //
2707 // v.n = (F_i - prescribed part) / (rho S_i) weakly imposed (Dirichlet)
2708 // v.n = bc_flux / rho prescribed flux faces
2709 //
2710 // interpolated back to the quadrature points with the trace basis, S_i
2711 // being the Dirichlet measure of node i. Faces with neither flag (the
2712 // subdomain's artificial boundary with its neighbours) are left alone.
2713 // Nothing in the flow model reads this array, so the Richards solution and
2714 // its Jacobian are untouched -- this only changes what TADR is told.
2715 //
2716 // PARALLEL: F_i, S_i and the prescribed part are assembled from this rank's
2717 // elements/faces, so they are complete for OWNED rows only. With the nodal
2718 // (vertex) boundary rule both models use -- checkCoupledQuadrature ties them
2719 // together -- a boundary quadrature point feeds only its own node's row, so
2720 // every value that reaches an owned transport row is complete. A non-nodal
2721 // boundary rule would need the three arrays ghost-summed first.
2723 if (bcCons) {
2724 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++) {
2725 const int ebN = exteriorElementBoundariesArray.data()[ebNE],
2726 eN = elementBoundaryElementsArray.data()[ebN * 2 + 0],
2727 ebN_local = elementBoundaryLocalElementBoundariesArray.data()[ebN * 2 + 0];
2728 for (int kb = 0; kb < nQuadraturePoints_elementBoundary; kb++) {
2729 const int ebNE_kb = ebNE * nQuadraturePoints_elementBoundary + kb,
2730 ebNE_kb_nSpace = ebNE_kb * nSpace,
2731 ebN_local_kb = ebN_local * nQuadraturePoints_elementBoundary + kb;
2732 const bool isDir = isDOFBoundary_u.data()[ebNE_kb] != 0;
2733 //A qp with neither flag is the subdomain's artificial boundary with a
2734 //neighbouring rank, not a physical one: leave it alone. (It only ever
2735 //touches ghost nodes, whose transport rows are overwritten by the
2736 //scatter, but zeroing a velocity there would be a lie either way.)
2737 if (!isDir && !isFluxBoundary_u.data()[ebNE_kb] && !isSeepageFace.data()[ebNE]) continue;
2738 double vn_cons = 0.0;
2739 if (isDir) {
2740 double w_sum = 0.0;
2741 for (int i = 0; i < nDOF_test_element; i++) {
2742 const double w = u_test_trace_ref.data()[ebN_local_kb * nDOF_test_element + i];
2743 if (w <= 1.0e-12) continue; //DOF not on this face
2744 const int gi = r_l2g.data()[eN * nDOF_test_element + i];
2745 if (bdryDirichletArea[gi] <= 0.0) continue;
2746 vn_cons += w * (consBoundaryFlux.data()[gi] - bdryPrescribedFlux[gi]) / bdryDirichletArea[gi];
2747 w_sum += w;
2748 }
2749 if (w_sum <= 1.0e-12) continue;
2750 vn_cons /= w_sum; //= 1 with a full face star
2751 } else {
2752 //not weakly imposed: the residual integrated exactly ebqe_bflux here
2753 //(the prescribed flux, or 0 on a seepage face that shut), so that is
2754 //the water the transport model must see
2755 vn_cons = ebqe_bflux.data()[ebNE_kb];
2756 }
2757 //the residual's fluxes are rho-weighted; the coupled trace is a Darcy velocity
2758 const double rho_ext_c = ebqe_rho.data()[ebNE_kb];
2759 vn_cons /= (std::fabs(rho_ext_c) > 1.0e-12 ? rho_ext_c : rho);
2760 double vn_old = 0.0;
2761 for (int I = 0; I < nSpace; I++)
2762 vn_old += ebqe_velocity_ext_couple.data()[ebNE_kb_nSpace + I] * ebqe_normal_cache[ebNE_kb_nSpace + I];
2763 for (int I = 0; I < nSpace; I++)
2764 ebqe_velocity_ext_couple.data()[ebNE_kb_nSpace + I] += (vn_cons - vn_old) * ebqe_normal_cache[ebNE_kb_nSpace + I];
2765 } //kb
2766 } //ebNE
2767 }
2768
2769 }
2770
2772 {
2773 xt::pyarray<int> &a_rowptr = args.array<int>("a_rowptr");
2774 xt::pyarray<int> &a_colind = args.array<int>("a_colind");
2775 double rho = args.scalar<double>("rho"); // freshwater reference (fallback)
2776 double beta = args.scalar<double>("beta");
2777 xt::pyarray<double> &gravity = args.array<double>("gravity");
2778 xt::pyarray<double> &alpha = args.array<double>("alpha");
2779 xt::pyarray<double> &n = args.array<double>("n");
2780 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
2781 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
2782 PSK_TYPE_member = args.scalar<int>("PSK_TYPE");
2783 VG_AIR_ENTRY_member = args.scalar<double>("VG_AIR_ENTRY");
2784 xt::pyarray<double> &KWs = args.array<double>("KWs");
2785 xt::pyarray<int> &elementMaterialTypes = args.array<int>("elementMaterialTypes");
2786 xt::pyarray<int> &freeDOFMaterialTypes = args.array<int>("freeDOFMaterialTypes");
2787 int numDOFs = args.scalar<int>("numDOFs");
2788 xt::pyarray<double> &mIn = args.array<double>("limited_solution");
2789 xt::pyarray<double> &pOut = args.array<double>("u_dof");
2790 // Per-DOF density projected from q_rho in
2791 // calculateResidual_entropy_viscosity (cached as a class member there).
2792 // If the cache is empty (e.g. invert called before the first residual
2793 // evaluation, or coupling disabled) fall back to the scalar `rho`.
2794 const bool have_rho_dof = (rho_dof_member.size() == static_cast<std::size_t>(numDOFs));
2795 int USE_NEWTON_INVERT = args.scalar<int>("USE_NEWTON_INVERT");
2796
2797 for (int i = 0; i < numDOFs; i++) {
2798 const int material_i = freeDOFMaterialTypes.data()[i];
2799 // Use the salinity-coupled density at this DOF so the m -> u inversion
2800 // is consistent with the variable-density forward residual
2801 // m_forward = theta * rho_local * (...).
2802 const double rho_i = have_rho_dof ? rho_dof_member[i] : rho;
2803 double dm, f[nSpace], df[nSpace], a[nnz], da[nnz];
2804 if (USE_NEWTON_INVERT){
2805 evaluateInverseCoefficients_Newton(a_rowptr.data(), a_colind.data(), rho_i, beta, gravity.data(), alpha.data()[material_i], n.data()[material_i], thetaR.data()[material_i],
2806 thetaSR.data()[material_i], &KWs.data()[material_i * nnz],
2807 pOut.data()[i], mIn.data()[i],
2808 dm, f, df, a, da);
2809 }
2810 else{
2811 evaluateInverseCoefficients(a_rowptr.data(), a_colind.data(), rho_i, beta, gravity.data(), alpha.data()[material_i], n.data()[material_i], thetaR.data()[material_i],
2812 thetaSR.data()[material_i], &KWs.data()[material_i * nnz],
2813 pOut.data()[i], mIn.data()[i],
2814 dm, f, df, a, da);
2815 }
2816 }
2817 }
2818
2820 {
2821 //element
2822 double dt = args.scalar<double>("dt");
2823 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
2824 xt::pyarray<double> &mesh_grad_trial_ref = args.array<double>("mesh_grad_trial_ref");
2825 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
2826 xt::pyarray<double> &mesh_velocity_dof = args.array<double>("mesh_velocity_dof");
2827 double MOVING_DOMAIN = args.scalar<double>("MOVING_DOMAIN");
2828 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
2829 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
2830 xt::pyarray<double> &u_trial_ref = args.array<double>("u_trial_ref");
2831 xt::pyarray<double> &u_grad_trial_ref = args.array<double>("u_grad_trial_ref");
2832 xt::pyarray<double> &u_test_ref = args.array<double>("u_test_ref");
2833 xt::pyarray<double> &u_grad_test_ref = args.array<double>("u_grad_test_ref");
2834 //element boundary
2835 xt::pyarray<double> &mesh_trial_trace_ref = args.array<double>("mesh_trial_trace_ref");
2836 xt::pyarray<double> &mesh_grad_trial_trace_ref = args.array<double>("mesh_grad_trial_trace_ref");
2837 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
2838 xt::pyarray<double> &u_trial_trace_ref = args.array<double>("u_trial_trace_ref");
2839 xt::pyarray<double> &u_grad_trial_trace_ref = args.array<double>("u_grad_trial_trace_ref");
2840 xt::pyarray<double> &u_test_trace_ref = args.array<double>("u_test_trace_ref");
2841 xt::pyarray<double> &u_grad_test_trace_ref = args.array<double>("u_grad_test_trace_ref");
2842 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
2843 xt::pyarray<double> &boundaryJac_ref = args.array<double>("boundaryJac_ref");
2844 //physics
2845 int nElements_global = args.scalar<int>("nElements_global");
2846 //new
2847 xt::pyarray<double> &ebqe_penalty_ext = args.array<double>("ebqe_penalty_ext");
2848 xt::pyarray<int> &elementMaterialTypes = args.array<int>("elementMaterialTypes");
2849 xt::pyarray<int> &isSeepageFace = args.array<int>("isSeepageFace");
2850 xt::pyarray<int> &a_rowptr = args.array<int>("a_rowptr");
2851 xt::pyarray<int> &a_colind = args.array<int>("a_colind");
2852 double rho = args.scalar<double>("rho");
2853 double beta = args.scalar<double>("beta");
2854
2855 xt::pyarray<double> &q_rho = args.array<double>("q_rho");
2856 xt::pyarray<double> &q_mu = args.array<double>("q_mu");
2857
2858 xt::pyarray<double> &gravity = args.array<double>("gravity");
2859 xt::pyarray<double> &alpha = args.array<double>("alpha");
2860 xt::pyarray<double> &n = args.array<double>("n");
2861 xt::pyarray<double> &thetaR = args.array<double>("thetaR");
2862 xt::pyarray<double> &thetaSR = args.array<double>("thetaSR");
2863 PSK_TYPE_member = args.scalar<int>("PSK_TYPE");
2864 VG_AIR_ENTRY_member = args.scalar<double>("VG_AIR_ENTRY");
2865 xt::pyarray<double> &KWs = args.array<double>("KWs");
2866 //end new
2867 double useMetrics = args.scalar<double>("useMetrics");
2868 double alphaBDF = args.scalar<double>("alphaBDF");
2869 int lag_shockCapturing = args.scalar<int>("lag_shockCapturing");
2870 double shockCapturingDiffusion = args.scalar<double>("shockCapturingDiffusion");
2871 xt::pyarray<int> &u_l2g = args.array<int>("u_l2g");
2872 xt::pyarray<int> &r_l2g = args.array<int>("r_l2g");
2873 xt::pyarray<double> &elementDiameter = args.array<double>("elementDiameter");
2874 int degree_polynomial = args.scalar<int>("degree_polynomial");
2875 xt::pyarray<double> &u_dof = args.array<double>("u_dof");
2876 xt::pyarray<double> &velocity = args.array<double>("velocity");
2877 xt::pyarray<double> &q_m_betaBDF = args.array<double>("q_m_betaBDF");
2878 xt::pyarray<double> &cfl = args.array<double>("cfl");
2879 xt::pyarray<double> &q_numDiff_u_last = args.array<double>("q_numDiff_u_last");
2880 xt::pyarray<int> &csrRowIndeces_u_u = args.array<int>("csrRowIndeces_u_u");
2881 xt::pyarray<int> &csrColumnOffsets_u_u = args.array<int>("csrColumnOffsets_u_u");
2882 xt::pyarray<double> &globalJacobian = args.array<double>("globalJacobian");
2883 xt::pyarray<double> &delta_x_ij = args.array<double>("delta_x_ij");
2884 int nExteriorElementBoundaries_global = args.scalar<int>("nExteriorElementBoundaries_global");
2885 xt::pyarray<int> &exteriorElementBoundariesArray = args.array<int>("exteriorElementBoundariesArray");
2886 xt::pyarray<int> &elementBoundaryElementsArray = args.array<int>("elementBoundaryElementsArray");
2887 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray = args.array<int>("elementBoundaryLocalElementBoundariesArray");
2888 xt::pyarray<double> &ebqe_velocity_ext = args.array<double>("ebqe_velocity_ext");
2889 xt::pyarray<int> &isDOFBoundary_u = args.array<int>("isDOFBoundary_u");
2890 xt::pyarray<double> &ebqe_bc_u_ext = args.array<double>("ebqe_bc_u_ext");
2891 xt::pyarray<int> &isFluxBoundary_u = args.array<int>("isFluxBoundary_u");
2892 xt::pyarray<double> &ebqe_bc_flux_ext = args.array<double>("ebqe_bc_flux_ext");
2893 xt::pyarray<int> &csrColumnOffsets_eb_u_u = args.array<int>("csrColumnOffsets_eb_u_u");
2894 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
2895 double Ct_sge = 4.0;
2896 //
2897 //loop over elements to compute volume integrals and load them into the element Jacobians and global Jacobian
2898 //
2899 for (int eN = 0; eN < nElements_global; eN++) {
2900 double elementJacobian_u_u[nDOF_test_element][nDOF_trial_element];
2901 for (int i = 0; i < nDOF_test_element; i++)
2902 for (int j = 0; j < nDOF_trial_element; j++) { elementJacobian_u_u[i][j] = 0.0; }
2903 for (int k = 0; k < nQuadraturePoints_element; k++) {
2904 int eN_k = eN * nQuadraturePoints_element + k, //index to a scalar at a quadrature point
2905 eN_k_nSpace = eN_k * nSpace,
2906 eN_nDOF_trial_element = eN * nDOF_trial_element; //index to a vector at a quadrature point
2907 //declare local storage
2908 double u = 0.0, grad_u[nSpace], m = 0.0, dm = 0.0, f[nSpace], df[nSpace], a[nnz], da[nnz], as[nnz], m_t = 0.0, dm_t = 0.0, dpdeResidual_u_u[nDOF_trial_element], Lstar_u[nDOF_test_element], dsubgridError_u_u[nDOF_trial_element], tau = 0.0, tau0 = 0.0, tau1 = 0.0, jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace], u_grad_trial[nDOF_trial_element * nSpace], dV, u_test_dV[nDOF_test_element], u_grad_test_dV[nDOF_test_element * nSpace], x, y, z, xt, yt, zt,
2909 G[nSpace * nSpace], G_dd_G, tr_G;
2910
2911 //get jacobian, etc for mapping reference element
2912 ck.calculateMapping_element(eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y, z);
2913 ck.calculateMappingVelocity_element(eN, k, mesh_velocity_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(), xt, yt, zt);
2914 //get the physical integration weight
2915 dV = fabs(jacDet) * dV_ref.data()[k];
2916 ck.calculateG(jacInv, G, G_dd_G, tr_G);
2917 //get the trial function gradients
2918 ck.gradTrialFromRef(&u_grad_trial_ref.data()[k * nDOF_trial_element * nSpace], jacInv, u_grad_trial);
2919 //get the solution
2920 ck.valFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], &u_trial_ref.data()[k * nDOF_trial_element], u);
2921 //get the solution gradients
2922 ck.gradFromDOF(u_dof.data(), &u_l2g.data()[eN_nDOF_trial_element], u_grad_trial, grad_u);
2923 //precalculate test function products with integration weights
2924 for (int j = 0; j < nDOF_trial_element; j++) {
2925 u_test_dV[j] = u_test_ref.data()[k * nDOF_trial_element + j] * dV;
2926 for (int I = 0; I < nSpace; I++) {
2927 u_grad_test_dV[j * nSpace + I] = u_grad_trial[j * nSpace + I] * dV; //cek warning won't work for Petrov-Galerkin
2928 }
2929 }
2930 //
2931 //calculate pde coefficients and derivatives at quadrature points
2932 //
2933 double Kr, dKr, thetaW;
2934 //const double rho_local = q_rho.data()[eN_k];
2935 double KWs_mu[nnz];
2936 for (int ii = 0; ii < nnz; ii++) KWs_mu[ii] = KWs.data()[elementMaterialTypes.data()[eN] * nnz + ii] / q_mu.data()[eN_k];
2937 evaluateCoefficients(a_rowptr.data(), a_colind.data(), rho, q_rho.data()[eN_k], beta, gravity.data(), alpha.data()[elementMaterialTypes.data()[eN]], n.data()[elementMaterialTypes.data()[eN]], thetaR.data()[elementMaterialTypes.data()[eN]],
2938 thetaSR.data()[elementMaterialTypes.data()[eN]], KWs_mu, u, m, dm, f, df, a, da, as, Kr, dKr, thetaW);
2939 //
2940 //moving mesh
2941 //
2942 double mesh_velocity[3];
2943 mesh_velocity[0] = xt;
2944 mesh_velocity[1] = yt;
2945 mesh_velocity[2] = zt;
2946 for (int I = 0; I < nSpace; I++) {
2947 f[I] -= MOVING_DOMAIN * m * mesh_velocity[I];
2948 df[I] -= MOVING_DOMAIN * dm * mesh_velocity[I];
2949 }
2950 //
2951 //calculate time derivatives
2952 //
2953 //cek hack
2954 dm = 1.0;
2955 ck.bdf(alphaBDF,
2956 q_m_betaBDF.data()[eN_k], //since m_t isn't used, we don't have to correct mass
2957 m, dm, m_t, dm_t);
2958 //
2959 //calculate subgrid error contribution to the Jacobian (strong residual, adjoint, jacobian of strong residual)
2960 //
2961 //calculate the adjoint times the test functions
2962 for (int i = 0; i < nDOF_test_element; i++) {
2963 int i_nSpace = i * nSpace;
2964 Lstar_u[i] = ck.Advection_adjoint(df, &u_grad_test_dV[i_nSpace]);
2965 }
2966 //calculate the Jacobian of strong residual
2967 for (int j = 0; j < nDOF_trial_element; j++) {
2968 int j_nSpace = j * nSpace;
2969 dpdeResidual_u_u[j] = ck.MassJacobian_strong(dm_t, u_trial_ref.data()[k * nDOF_trial_element + j]) + ck.AdvectionJacobian_strong(df, &u_grad_trial[j_nSpace]);
2970 }
2971 //tau and tau*Res
2972 calculateSubgridError_tau(elementDiameter.data()[eN], dm_t, df, cfl.data()[eN_k], tau0);
2973
2974 calculateSubgridError_tau(Ct_sge, G, dm_t, df, tau1, cfl.data()[eN_k]);
2975 tau = useMetrics * tau1 + (1.0 - useMetrics) * tau0;
2976
2977 for (int j = 0; j < nDOF_trial_element; j++) dsubgridError_u_u[j] = -tau * dpdeResidual_u_u[j];
2978 for (int i = 0; i < nDOF_test_element; i++) {
2979 for (int j = 0; j < nDOF_trial_element; j++) {
2980 if (LUMPED_MASS_MATRIX == 1) {
2981 if (i == j) elementJacobian_u_u[i][j] += u_test_dV[i];
2982 } else {
2983 int j_nSpace = j * nSpace;
2984 int i_nSpace = i * nSpace;
2985 dm_t = 1.0; //we are solving for continuum density explicitly
2986 elementJacobian_u_u[i][j] += ck.MassJacobian_weak(dm_t, u_trial_ref.data()[k * nDOF_trial_element + j], u_test_dV[i]);
2987 }
2988 } //j
2989 } //i
2990 } //k
2991 //
2992 //load into element Jacobian into global Jacobian
2993 //
2994 for (int i = 0; i < nDOF_test_element; i++) {
2995 int eN_i = eN * nDOF_test_element + i;
2996 int I = u_l2g.data()[eN_i];
2997 for (int j = 0; j < nDOF_trial_element; j++) {
2998 int eN_i_j = eN_i * nDOF_trial_element + j;
2999 int J = u_l2g.data()[eN * nDOF_trial_element + j];
3000 //globalJacobian.data()[csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_u_u.data()[eN_i_j]] += elementJacobian_u_u[i][j];
3001 delta_x_ij.data()[3 * (csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_u_u.data()[eN_i_j]) + 0] = mesh_dof.data()[I * 3 + 0] - mesh_dof.data()[J * 3 + 0];
3002 delta_x_ij.data()[3 * (csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_u_u.data()[eN_i_j]) + 1] = mesh_dof.data()[I * 3 + 1] - mesh_dof.data()[J * 3 + 1];
3003 delta_x_ij.data()[3 * (csrRowIndeces_u_u.data()[eN_i] + csrColumnOffsets_u_u.data()[eN_i_j]) + 2] = mesh_dof.data()[I * 3 + 2] - mesh_dof.data()[J * 3 + 2];
3004 } //j
3005 } //i
3006 } //elements
3007 } //computeMassMatrix
3008}; //Richards
3009
3010inline Richards_base *newRichards(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_test_elementIn, int nQuadraturePoints_elementBoundaryIn, int CompKernelFlag)
3011{
3012 if (nSpaceIn == 1)
3013 return proteus::chooseAndAllocateDiscretization1D<Richards_base, Richards, CompKernel>(nSpaceIn, nQuadraturePoints_elementIn, nDOF_mesh_trial_elementIn, nDOF_trial_elementIn, nDOF_test_elementIn, nQuadraturePoints_elementBoundaryIn, CompKernelFlag);
3014 else if (nSpaceIn == 2)
3015 return proteus::chooseAndAllocateDiscretization2D<Richards_base, Richards, CompKernel>(nSpaceIn, nQuadraturePoints_elementIn, nDOF_mesh_trial_elementIn, nDOF_trial_elementIn, nDOF_test_elementIn, nQuadraturePoints_elementBoundaryIn, CompKernelFlag);
3016 else {
3017 assert(nSpaceIn == 3);
3018 return proteus::chooseAndAllocateDiscretization<Richards_base, Richards, CompKernel>(nSpaceIn, nQuadraturePoints_elementIn, nDOF_mesh_trial_elementIn, nDOF_trial_elementIn, nDOF_test_elementIn, nQuadraturePoints_elementBoundaryIn, CompKernelFlag);
3019 }
3020}
3021} // namespace richards
3022} // namespace proteus
3023#endif
Int n
Definition Headers.h:28
Double q
Definition Headers.h:81
Double sum
Definition Headers.h:85
Double u
Definition Headers.h:89
Int num
Definition Headers.h:32
Double * z
Definition Headers.h:49
Double v
Definition Headers.h:95
Double R
Definition Headers.h:82
Double psi
Definition Headers.h:78
#define cE
Definition NCLS3P.h:10
virtual void FCTStep(arguments_dict &args)=0
virtual void kth_FCT_step(arguments_dict &args)=0
virtual void calculateJacobian(arguments_dict &args)=0
virtual void calculateResidual_entropy_viscosity(arguments_dict &args)=0
virtual void calculateEdgeVelocity(arguments_dict &args)=0
virtual void ImplicitFCTStep(arguments_dict &args)=0
virtual void invert(arguments_dict &args)=0
virtual void calculateMassMatrix(arguments_dict &args)=0
virtual void calculateResidual(arguments_dict &args)=0
std::vector< double > boundary_integral_member
Definition Richards.h:121
void exteriorNumericalFlux(const double &bc_flux, int rowptr[nSpace], int colind[nnz], int isSeepageFace, int &isDOFBoundary, double n[nSpace], double bc_u, double K[nnz], double grad_psi[nSpace], double u, double K_rho_g[nSpace], double penalty, double &flux)
Definition Richards.h:317
void calculateNumericalDiffusion(const double &shockCapturingDiffusion, const double &elementDiameter, const double &strong_residual, const double grad_u[nSpace], double &numDiff)
Definition Richards.h:306
void calculateResidual_entropy_viscosity(arguments_dict &args)
Definition Richards.h:1624
double seepagefluxcalculator(double anb_seepage_flux, int isSeepageFace, double dS, double flux_ext)
Definition Richards.h:418
void exteriorNumericalFluxJacobian2(const int rowptr[nSpace], const int colind[nnz], const int isDOFBoundary, const double n[nSpace], const double Ks[nnz], const double K[nnz], const double dK[nnz], const double grad_psi[nSpace], const double grad_v[nSpace], const double dK_rho_g[nSpace], const double v, const double penalty, double &fluxJacobian, double &bfluxJacobian, double &cfluxJacobian)
Definition Richards.h:397
void invert(arguments_dict &args)
Definition Richards.h:2771
void kth_FCT_step(arguments_dict &args)
Definition Richards.h:1485
const int nDOF_test_X_trial_element
Definition Richards.h:97
void evaluateInverseCoefficients(const int rowptr[nSpace], const int colind[nnz], const double rho, const double beta, const double gravity[nSpace], const double alpha, const double n_vg, const double thetaR, const double thetaSR, const double KWs[nnz], double &u, const double &m, const double &dm, const double f[nSpace], const double df[nSpace], const double a[nnz], const double da[nnz])
Definition Richards.h:210
std::vector< double > Rpos_member
Definition Richards.h:122
std::vector< double > Rneg_member
Definition Richards.h:122
std::vector< double > cflux_member
Definition Richards.h:121
void evaluateInverseCoefficients_Newton(const int rowptr[nSpace], const int colind[nnz], const double rho, const double beta, const double gravity[nSpace], const double alpha, const double n_vg, const double thetaR, const double thetaSR, const double KWs[nnz], double &u, const double &m, const double &dm, const double f[nSpace], const double df[nSpace], const double a[nnz], const double da[nnz])
Definition Richards.h:233
void vgAirEntryScales(const double alpha, const double n_vg, double &Sc, double &Vc)
Definition Richards.h:135
void evaluateCoefficients(const int rowptr[nSpace], const int colind[nnz], const double rho0, const double rho_transport, const double beta, const double gravity[nSpace], const double alpha, const double n_vg, const double thetaR, const double thetaSR, const double KWs[nnz], const double &u, double &m, double &dm, double f[nSpace], double df[nSpace], double a[nnz], double da[nnz], double as[nnz], double &kr, double &dkr, double &thetaW_out)
Definition Richards.h:143
void exteriorNumericalFlux2(const double &bc_flux, int rowptr[nSpace], int colind[nnz], int isSeepageFace, int &isDOFBoundary, double n[nSpace], double bc_u, double K[nnz], double grad_psi[nSpace], double u, double K_rho_g[nSpace], double penalty, double &flux, double &bflux)
Definition Richards.h:359
void FCTStep(arguments_dict &args)
Definition Richards.h:1041
void calculateMassMatrix(arguments_dict &args)
Definition Richards.h:2819
void calculateSubgridError_tau(const double &elementDiameter, const double &dmt, const double dH[nSpace], double &cfl, double &tau)
Definition Richards.h:283
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 Richards.h:295
std::vector< double > psi_member
Definition Richards.h:120
void calculateCFL(const double &elementDiameter, const double df[nSpace], double &cfl)
Definition Richards.h:273
std::vector< double > rho_dof_member
Definition Richards.h:102
void exteriorNumericalFluxJacobian(const int rowptr[nSpace], const int colind[nnz], const int isDOFBoundary, const double n[nSpace], const double K[nnz], const double dK[nnz], const double grad_psi[nSpace], const double grad_v[nSpace], const double dK_rho_g[nSpace], const double v, const double penalty, double &fluxJacobian)
Definition Richards.h:344
void calculateResidual(arguments_dict &args)
Definition Richards.h:424
void ImplicitFCTStep(arguments_dict &args)
Definition Richards.h:1385
void calculateEdgeVelocity(arguments_dict &args)
Definition Richards.h:1257
std::vector< double > global_entropy_residual_member
Definition Richards.h:120
void calculateJacobian(arguments_dict &args)
Definition Richards.h:807
std::vector< double > eta_member
Definition Richards.h:120
double df(double C, double b, double a, int q, int r)
#define w(x)
Definition jf.h:22
#define POWER_SMOOTHNESS_INDICATOR
Definition m_comp_co2.h:22
#define nnz
Definition m_comp_co2.h:19
#define GLOBAL_FCT
Definition m_comp_co2.h:24
#define IS_BETAij_ONE
Definition m_comp_co2.h:23
void vgm_invert_analytic(const double m, const double rho, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void gardner_invert_analytic(const double m, const double rho, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void bc_wetting(const double psiC, const double alpha, const double lam, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC, const bc_kr kr_model=bc_kr::burdine)
void vgm_wetting(const double psiC, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC)
void bc_invert_newton(const double m, const double rho, const double beta, const double alpha, const double lam, const double thetaR, const double thetaSR, double &u)
void gardner_invert_newton(const double m, const double rho, const double beta, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void vgm_invert_newton(const double m, const double rho, const double beta, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void hm_wetting(const double psiC, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC)
void hm_invert_newton(const double m, const double rho, const double beta, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
void gardner_wetting(const double psiC, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &thetaW, double &DthetaW_DpsiC, double &KWr, double &DKWr_DpsiC)
void bc_invert_analytic(const double m, const double rho, const double alpha, const double lam, const double thetaR, const double thetaSR, double &u)
void hm_invert_analytic(const double m, const double rho, const double alpha, const double n_vg, const double thetaR, const double thetaSR, double &u)
double DENTROPY_LOG(const double &phi, const double &phiL, const double &phiR)
Definition Richards.h:69
double ENTROPY(const double &phi, const double &phiL, const double &phiR)
Definition Richards.h:56
double DENTROPY(const double &phi, const double &phiL, const double &phiR)
Definition Richards.h:60
double ENTROPY_LOG(const double &phi, const double &phiL, const double &phiR)
Definition Richards.h:65
Richards_base * newRichards(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_test_elementIn, int nQuadraturePoints_elementBoundaryIn, int CompKernelFlag)
Definition Richards.h:3010
Definition ADR.h:21
double phi(const double &g, const double &h, const double &hL, const double &hR, const double &uL, const double &uR)
Definition SW2DCV.h:62
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 * chooseAndAllocateDiscretization1D(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_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
Richards free-function PSK closures (moved here from proteus/richards/psk_models.h).
T & scalar(const std::string &key)
xt::pyarray< T > & array(const std::string &key)