proteus 1.9.1
C/C++/Fortran libraries
Loading...
Searching...
No Matches
SW2DCV.h
Go to the documentation of this file.
1#ifndef SW2DCV_H
2#define SW2DCV_H
3#include "ArgumentsDict.h"
4#include "CompKernel.h"
5#include "ModelFactory.h"
6#include "xtensor-python/pyarray.hpp"
7#include <assert.h>
8#include <cmath>
9#include <iostream>
10#include <valarray>
11
12namespace py = pybind11;
13
14#define POWER_SMOOTHNESS_INDICATOR 2
15#define VEL_FIX_POWER 2.
16#define REESTIMATE_MAX_EDGE_BASED_CFL 0
17#define LIMITING_ITERATION 2
18
19/* inline functions */
20namespace proteus {
21// FOR CELL BASED ENTROPY VISCOSITY
22inline double ENTROPY(const double &g, const double &h, const double &hu,
23 const double &hv, const double &z,
24 const double &one_over_hReg) {
25 return 0.5 *
26 (g * h * h + one_over_hReg * (hu * hu + hv * hv) + 2. * g * h * z);
27}
28inline double DENTROPY_DH(const double &g, const double &h, const double &hu,
29 const double &hv, const double &z,
30 const double &one_over_hReg) {
31 return g * h - 0.5 * (hu * hu + hv * hv) * std::pow(one_over_hReg, 2) + g * z;
32}
33inline double DENTROPY_DHU(const double &g, const double &h, const double &hu,
34 const double &hv, const double &z,
35 const double &one_over_hReg) {
36 return hu * one_over_hReg;
37}
38inline double DENTROPY_DHV(const double &g, const double &h, const double &hu,
39 const double &hv, const double &z,
40 const double &one_over_hReg) {
41 return hv * one_over_hReg;
42}
43inline double ENTROPY_FLUX1(const double &g, const double &h, const double &hu,
44 const double &hv, const double &z,
45 const double &one_over_hReg) {
46 return (ENTROPY(g, h, hu, hv, z, one_over_hReg) + 0.5 * g * h * h +
47 g * h * z) *
48 hu * one_over_hReg;
49}
50inline double ENTROPY_FLUX2(const double &g, const double &h, const double &hu,
51 const double &hv, const double &z,
52 const double &one_over_hReg) {
53 return (ENTROPY(g, h, hu, hv, z, one_over_hReg) + 0.5 * g * h * h +
54 g * h * z) *
55 hv * one_over_hReg;
56}
57// FOR ESTIMATING MAX WAVE SPEEDS
58inline double f(const double &g, const double &h, const double &hZ) {
59 return ((h <= hZ) ? 2. * (sqrt(g * h) - sqrt(g * hZ))
60 : (h - hZ) * sqrt(0.5 * g * (h + hZ) / h / hZ));
61}
62inline double phi(const double &g, const double &h, const double &hL,
63 const double &hR, const double &uL, const double &uR) {
64 return (f(g, h, hL) + f(g, h, hR) + uR - uL);
65}
66inline double fp(const double &g, const double &h, const double &hZ) {
67 return ((h <= hZ)
68 ? sqrt(g / h)
69 : g * (2 * h * h + h * hZ + hZ * hZ) /
70 (2 * sqrt(2 * g) * h * h * hZ * sqrt(1 / h + 1 / hZ)));
71}
72inline double phip(const double &g, const double &h, const double &hL,
73 const double &hR) {
74 return (fp(g, h, hL) + fp(g, h, hR));
75}
76inline double nu1(const double &g, const double &hStar, const double &hL,
77 const double &uL, const double &one_over_hL) {
78 return (uL - sqrt(g * hL) *
79 sqrt((1. + fmax((hStar - hL) / 2. * one_over_hL, 0.0)) *
80 (1. + fmax((hStar - hL) * one_over_hL, 0.))));
81}
82inline double nu3(const double &g, const double &hStar, const double &hR,
83 const double &uR, const double &one_over_hR) {
84 return (uR + sqrt(g * hR) *
85 sqrt((1. + fmax((hStar - hR) / 2. * one_over_hR, 0.0)) *
86 (1. + fmax((hStar - hR) * one_over_hR, 0.))));
87}
88inline double phiDiff(const double &g, const double &h1k, const double &h2k,
89 const double &hL, const double &hR, const double &uL,
90 const double &uR) {
91 return ((phi(g, h2k, hL, hR, uL, uR) - phi(g, h1k, hL, hR, uL, uR)) /
92 (h2k - h1k));
93}
94inline double phiDDiff1(const double &g, const double &h1k, const double &h2k,
95 const double &hL, const double &hR, const double &uL,
96 const double &uR) {
97 return ((phiDiff(g, h1k, h2k, hL, hR, uL, uR) - phip(g, h1k, hL, hR)) /
98 (h2k - h1k));
99}
100inline double phiDDiff2(const double &g, const double &h1k, const double &h2k,
101 const double &hL, const double &hR, const double &uL,
102 const double &uR) {
103 return ((phip(g, h2k, hL, hR) - phiDiff(g, h1k, h2k, hL, hR, uL, uR)) /
104 (h2k - h1k));
105}
106inline double hStarLFromQuadPhiFromAbove(const double &g, const double &hStarL,
107 const double &hStarR, const double &hL,
108 const double &hR, const double &uL,
109 const double &uR) {
110 return (hStarL -
111 2 * phi(g, hStarL, hL, hR, uL, uR) /
112 (phip(g, hStarL, hL, hR) +
113 sqrt(std::pow(phip(g, hStarL, hL, hR), 2) -
114 4 * phi(g, hStarL, hL, hR, uL, uR) *
115 phiDDiff1(g, hStarL, hStarR, hL, hR, uL, uR))));
116}
117inline double hStarRFromQuadPhiFromBelow(const double &g, const double &hStarL,
118 const double &hStarR, const double &hL,
119 const double &hR, const double &uL,
120 const double &uR) {
121 return (hStarR -
122 2 * phi(g, hStarR, hL, hR, uL, uR) /
123 (phip(g, hStarR, hL, hR) +
124 sqrt(std::pow(phip(g, hStarR, hL, hR), 2) -
125 4 * phi(g, hStarR, hL, hR, uL, uR) *
126 phiDDiff2(g, hStarL, hStarR, hL, hR, uL, uR))));
127}
128} // namespace proteus
129
130namespace proteus {
131
133public:
134 virtual ~SW2DCV_base() {}
135 virtual void convexLimiting(arguments_dict &args) = 0;
136 virtual double calculateEdgeBasedCFL(arguments_dict &args) = 0;
137 virtual void calculateEV(arguments_dict &args) = 0;
138 virtual void calculateResidual(arguments_dict &args) = 0;
139 virtual void calculateMassMatrix(arguments_dict &args) = 0;
141};
142
143template <class CompKernelType, int nSpace, int nQuadraturePoints_element,
144 int nDOF_mesh_trial_element, int nDOF_trial_element,
145 int nDOF_test_element, int nQuadraturePoints_elementBoundary>
146class SW2DCV : public SW2DCV_base {
147public:
149 CompKernelType ck;
151 : nDOF_test_X_trial_element(nDOF_test_element * nDOF_trial_element),
152 ck() {
153 std::cout << "Constructing SW2DCV<CompKernelTemplate<" << nSpace << ","
154 << nQuadraturePoints_element << "," << nDOF_mesh_trial_element
155 << "," << nDOF_trial_element << "," << nDOF_test_element << ","
156 << nQuadraturePoints_elementBoundary << ">());" << std::endl
157 << std::flush;
158 }
159
160 inline double maxWaveSpeedSharpInitialGuess(double g, double nx, double ny,
161 double hL, double huL, double hvL,
162 double hR, double huR, double hvR,
163 double hEps, bool debugging) {
164 double lambda1, lambda3;
165 // 1-eigenvalue: uL-sqrt(g*hL)
166 // 3-eigenvalue: uR+sqrt(g*hR)
167
168 // To avoid division by 0
169 double one_over_hL = 2.0 * hL / (hL * hL + std::pow(fmax(hL, hEps), 2.0));
170 double one_over_hR = 2.0 * hR / (hR * hR + std::pow(fmax(hR, hEps), 2.0));
171
172 double hVelL = nx * huL + ny * hvL;
173 double hVelR = nx * huR + ny * hvR;
174 double velL = one_over_hL * hVelL;
175 double velR = one_over_hR * hVelR;
176
177 double x0 = std::pow(2. * sqrt(2.) - 1., 2.);
178 double hMin = fmin(hL, hR);
179 double hMax = fmax(hL, hR);
180
181 double hStar;
182 double fMin = phi(g, x0 * hMin, hL, hR, velL, velR);
183 double fMax = phi(g, x0 * hMax, hL, hR, velL, velR);
184
185 double sqrMin = sqrt(hMin);
186 double sqrMax = sqrt(hMax);
187
188 if (0. <= fMin) {
189 hStar = fmin(
190 x0 * hMin,
191 std::pow(fmax(0., velL - velR + 2. * sqrt(g) * (sqrt(hL) + sqrt(hR))),
192 2) /
193 16. / g);
194 } else if (0. <= fMax) {
195 double a = 1.0 / (2.0 * sqrt(2.0));
196 double c = -hMin * a - sqrMin * sqrMax +
197 sqrMin * (velR - velL) / (2.0 * sqrt(g));
198 double delta = hMin - 4.0 * a * c;
199 if (delta < 0.0) {
200 std::cout << "Bug in computing lambda. Exiting." << std::endl;
201 abort();
202 }
203 hStar = fmin(x0 * hMax, std::pow((-sqrMin + sqrt(delta)) / (2. * a), 2));
204 } else {
205 hStar = sqrMin * sqrMax *
206 (1.0 + sqrt(2.0 / g) * (velL - velR) / (sqrMin + sqrMax));
207 }
208
209 // return lambda_max
210 lambda1 = nu1(g, hStar, hL, velL, one_over_hL);
211 lambda3 = nu3(g, hStar, hR, velR, one_over_hR);
212 return fmax(fabs(lambda1), fabs(lambda3));
213 }
214
215 inline void calculateCFL(const double &elementDiameter, const double &g,
216 const double &h, const double &hu, const double &hv,
217 const double hEps, double &cfl) {
218 double cflx, cfly, c = sqrt(fmax(g * hEps, g * h));
219 double u = 2 * h / (h * h + std::pow(fmax(h, hEps), 2)) * hu;
220 double v = 2 * h / (h * h + std::pow(fmax(h, hEps), 2)) * hv;
221
222 if (u > 0.0)
223 cflx = (u + c) / elementDiameter;
224 else
225 cflx = fabs(u - c) / elementDiameter;
226
227 if (v > 0.0)
228 cfly = (v + c) / elementDiameter;
229 else
230 cfly = fabs(v - c) / elementDiameter;
231 cfl = sqrt(cflx * cflx + cfly * cfly); // hack, conservative estimate
232 }
233
235 double dt = args.scalar<double>("dt");
236 int NNZ = args.scalar<int>("NNZ");
237 int numDOFs = args.scalar<int>("numDOFs");
238 xt::pyarray<double> &lumped_mass_matrix =
239 args.array<double>("lumped_mass_matrix");
240 xt::pyarray<double> &h_old = args.array<double>("h_old");
241 xt::pyarray<double> &hu_old = args.array<double>("hu_old");
242 xt::pyarray<double> &hv_old = args.array<double>("hv_old");
243 xt::pyarray<double> &b_dof = args.array<double>("b_dof");
244 xt::pyarray<double> &high_order_hnp1 =
245 args.array<double>("high_order_hnp1");
246 xt::pyarray<double> &high_order_hunp1 =
247 args.array<double>("high_order_hunp1");
248 xt::pyarray<double> &high_order_hvnp1 =
249 args.array<double>("high_order_hvnp1");
250 xt::pyarray<double> &extendedSourceTerm_hu =
251 args.array<double>("extendedSourceTerm_hu");
252 xt::pyarray<double> &extendedSourceTerm_hv =
253 args.array<double>("extendedSourceTerm_hv");
254 xt::pyarray<double> &limited_hnp1 = args.array<double>("limited_hnp1");
255 xt::pyarray<double> &limited_hunp1 = args.array<double>("limited_hunp1");
256 xt::pyarray<double> &limited_hvnp1 = args.array<double>("limited_hvnp1");
257 xt::pyarray<int> &csrRowIndeces_DofLoops =
258 args.array<int>("csrRowIndeces_DofLoops");
259 xt::pyarray<int> &csrColumnOffsets_DofLoops =
260 args.array<int>("csrColumnOffsets_DofLoops");
261 xt::pyarray<double> &MassMatrix = args.array<double>("MassMatrix");
262 xt::pyarray<double> &dH_minus_dL = args.array<double>("dH_minus_dL");
263 xt::pyarray<double> &muH_minus_muL = args.array<double>("muH_minus_muL");
264 double hEps = args.scalar<double>("hEps");
265 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
266 // diagnostic: no bed in the star states (well-balanced dissipation off)
267 int NO_STAR_STATES = args.scalar<int>("NO_STAR_STATES");
268 xt::pyarray<double> &dLow = args.array<double>("dLow");
269 xt::pyarray<double> &new_SourceTerm_hu =
270 args.array<double>("new_SourceTerm_hu");
271 xt::pyarray<double> &new_SourceTerm_hv =
272 args.array<double>("new_SourceTerm_hv");
273 xt::pyarray<double> &hLow = args.array<double>("hLow");
274 xt::pyarray<double> &huLow = args.array<double>("huLow");
275 xt::pyarray<double> &hvLow = args.array<double>("hvLow");
276 xt::pyarray<double> &h_min = args.array<double>("h_min");
277 xt::pyarray<double> &h_max = args.array<double>("h_max");
278 xt::pyarray<double> &kin_max = args.array<double>("kin_max");
279 double KE_tiny = args.scalar<double>("KE_tiny");
280
281 // The limiter runs as a sequence of calls so the caller can exchange
282 // ghost-DOF values in between (each ghost row only sees part of its
283 // stencil): per pass, stage 0 computes R+-, the caller exchanges them,
284 // stage 1 computes l_ij, applies the limited fluxes and updates the FCT
285 // matrices, and the caller exchanges hLow/huLow/hvLow before the next
286 // pass. FCT_h, FCT_hu, FCT_hv and l_ij persist in FCT_all (4 x nnz).
287 const int limit_iter_arg = args.scalar<int>("limit_iter");
288 const int limit_stage = args.scalar<int>("limit_stage");
289 xt::pyarray<double> &FCT_all = args.array<double>("FCT_all");
290 xt::pyarray<double> &Rneg = args.array<double>("Rneg_dof");
291 xt::pyarray<double> &Rpos = args.array<double>("Rpos_dof");
292 // number of neighbours of each DOF, exchanged with the ghosts: a ghost
293 // DOF's row in the local CSR pattern is truncated
294 xt::pyarray<double> &num_neighbours = args.array<double>("num_neighbours");
295 const bool first_call = (limit_iter_arg == 0 && limit_stage == 0);
296
297 // Create FCT component matrices in vector form
298 std::valarray<double> FCT_h(0.0, dH_minus_dL.size()),
299 FCT_hu(0.0, dH_minus_dL.size()), FCT_hv(0.0, dH_minus_dL.size());
300 const std::size_t nnz_fct = dH_minus_dL.size();
301
303 // Loop to define FCT matrices for each component //
305 int ij = 0;
306 for (int i = 0; i < numDOFs && first_call; i++) {
307 // Read some vectors
308 double high_order_hnp1i = high_order_hnp1[i];
309 double high_order_hunp1i = high_order_hunp1[i];
310 double high_order_hvnp1i = high_order_hvnp1[i];
311 double hi = h_old[i];
312 double huni = hu_old[i];
313 double hvni = hv_old[i];
314 double Zi = b_dof[i];
315 double mi = lumped_mass_matrix[i];
316 double one_over_hiReg =
317 2 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2)); // hEps
318
319 // LOOP OVER THE SPARSITY PATTERN (j-LOOP)//
320 for (int offset = csrRowIndeces_DofLoops[i];
321 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
322
323 int j = csrColumnOffsets_DofLoops[offset];
324
325 if (i != j) {
326 // Read some vectors
327 double hj = h_old[j];
328 double hunj = hu_old[j];
329 double hvnj = hv_old[j];
330 double Zj = b_dof[j];
331 double one_over_hjReg =
332 2. * hj / (hj * hj + std::pow(fmax(hj, hEps), 2));
333
334 // Compute star states
335 double hStarij = NO_STAR_STATES ? hi : fmax(0., hi + Zi - fmax(Zi, Zj));
336 double huStarij = huni * hStarij * one_over_hiReg;
337 double hvStarij = hvni * hStarij * one_over_hiReg;
338
339 double hStarji = NO_STAR_STATES ? hj : fmax(0., hj + Zj - fmax(Zi, Zj));
340 double huStarji = hunj * hStarji * one_over_hjReg;
341 double hvStarji = hvnj * hStarji * one_over_hjReg;
342
343 // i-th row of flux correction matrix
344 double ML_minus_MC = (LUMPED_MASS_MATRIX == 1
345 ? 0.
346 : (i == j ? 1. : 0.) * mi - MassMatrix[ij]);
347
348 FCT_h[ij] =
349 ML_minus_MC *
350 (high_order_hnp1[j] - hj - (high_order_hnp1i - hi)) +
351 dt * (dH_minus_dL[ij] - muH_minus_muL[ij]) * (hStarji - hStarij) +
352 dt * muH_minus_muL[ij] * (hj - hi);
353
354 FCT_hu[ij] = ML_minus_MC * (high_order_hunp1[j] - hunj -
355 (high_order_hunp1i - huni)) +
356 dt * (dH_minus_dL[ij] - muH_minus_muL[ij]) *
357 (huStarji - huStarij) +
358 dt * muH_minus_muL[ij] * (hunj - huni);
359
360 FCT_hv[ij] = ML_minus_MC * (high_order_hvnp1[j] - hvnj -
361 (high_order_hvnp1i - hvni)) +
362 dt * (dH_minus_dL[ij] - muH_minus_muL[ij]) *
363 (hvStarji - hvStarij) +
364 dt * muH_minus_muL[ij] * (hvnj - hvni);
365
366 } else {
367 FCT_h[ij] = 0.0;
368 FCT_hu[ij] = 0.0;
369 FCT_hv[ij] = 0.0;
370 }
371
372 // UPDATE ij //
373 ij += 1;
374 } // j loop ends here
375 } // i loop ends here
376
378 // Main loop to define limiters and computed limited solution //////
380
381 // Create Lij_array and initialize with 1
382 std::valarray<double> Lij_array(1.0, dH_minus_dL.size());
383 if (!first_call) {
384 for (std::size_t k = 0; k < nnz_fct; k++) {
385 FCT_h[k] = FCT_all[0 * nnz_fct + k];
386 FCT_hu[k] = FCT_all[1 * nnz_fct + k];
387 FCT_hv[k] = FCT_all[2 * nnz_fct + k];
388 Lij_array[k] = FCT_all[3 * nnz_fct + k];
389 }
390 }
391
392 /* One stage of one limiting pass per call */
393 for (int limit_iter = limit_iter_arg; limit_iter < limit_iter_arg + 1;
394 limit_iter++) {
395
396 /* Loop to define FCT Rpos and Rneg values */
397 if (limit_stage == 0) {
398 ij = 0;
399 for (int i = 0; i < numDOFs; i++) {
400
401 double hi = h_old[i];
402 double mi = lumped_mass_matrix[i];
403 // Initialize Pneg and Ppos quantities at ith node
404 double Pnegi = 0., Pposi = 0.;
405
406 // LOOP OVER THE SPARSITY PATTERN (j-LOOP)
407 for (int offset = csrRowIndeces_DofLoops[i];
408 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
409
410 int j = csrColumnOffsets_DofLoops[offset];
411
412 // COMPUTE P VECTORS
413 Pnegi += FCT_h[ij] * ((FCT_h[ij] < 0) ? 1. : 0.);
414 Pposi += FCT_h[ij] * ((FCT_h[ij] > 0) ? 1. : 0.);
415
416 // UPDATE ij
417 ij += 1;
418 } // j loop ends here
419
420 double psmall_h = 1E-14 * fmax(fabs(Pnegi), fabs(Pposi));
421
423 // COMPUTE Q VECTORS //
425 double Qnegi = std::min(mi * (h_min[i] - hLow[i]), 0.0);
426 double Qposi = std::max(mi * (h_max[i] - hLow[i]), 0.0);
427
429 // COMPUTE R VECTORS //
431 if (hi <= hEps) {
432 Rneg[i] = 0.;
433 Rpos[i] = 0.;
434 } else {
435 // for h
436 if (Pnegi >= -psmall_h) {
437 Rneg[i] = 1.0;
438 } else {
439 Rneg[i] = std::min(1.0, Qnegi / Pnegi);
440 }
441 if (Pposi <= psmall_h) {
442 Rpos[i] = 1.0;
443 } else {
444 Rpos[i] = std::min(1.0, Qposi / Pposi);
445 }
446 }
447 } // i loop ends here
448 break; // stage 0 done: the caller exchanges Rneg/Rpos
449 }
450
451 /* Here we compute the limiters */
452 ij = 0;
453 for (int i = 0; i < numDOFs; i++) {
454
455 double mi = lumped_mass_matrix[i];
456 double ci =
457 kin_max[i] * hLow[i] -
458 0.5 * (huLow[i] * huLow[i] + hvLow[i] * hvLow[i]); // for KE lim.
459
460 // LOOP OVER THE SPARSITY PATTERN (j-LOOP)//
461 for (int offset = csrRowIndeces_DofLoops[i];
462 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
463
464 int j = csrColumnOffsets_DofLoops[offset];
465
466 if (j != i) {
467
468 // Compute limiter based on water height
469 if (FCT_h[ij] >= 0.) {
470 Lij_array[ij] = fmin(Lij_array[ij], std::min(Rneg[j], Rpos[i]));
471 } else {
472 Lij_array[ij] = fmin(Lij_array[ij], std::min(Rneg[i], Rpos[j]));
473 }
474
475 /*======================================================*/
476 /* Kinetic Energy limiting */
477 double lambdaj =
478 num_neighbours[i];
479 double Ph_ij = FCT_h[ij] / mi / lambdaj;
480 double Phu_ij = FCT_hu[ij] / mi / lambdaj;
481 double Phv_ij = FCT_hv[ij] / mi / lambdaj;
482
483 // Here we initialize limiter based on kinetic energy
484 double KE_limiter = 1;
485 double neg_root_i = 1., neg_root_j = 1.;
486
487 // We first check if local kinetic energy > 0
488 if (kin_max[i] * hLow[i] <= KE_tiny)
489 KE_limiter = 0.;
490
491 // We then check if KE bound is already satisfied
492 double hi_with_lijPij = hLow[i] + Lij_array[ij] * Ph_ij;
493 double hui_with_lijPij = huLow[i] + Lij_array[ij] * Phu_ij;
494 double hvi_with_lijPij = hvLow[i] + Lij_array[ij] * Phv_ij;
495 double psi = kin_max[i] * hi_with_lijPij -
496 0.5 * (hui_with_lijPij * hui_with_lijPij +
497 hvi_with_lijPij * hvi_with_lijPij);
498 if (psi > -KE_tiny) {
499 KE_limiter = fmin(KE_limiter, Lij_array[ij]);
500 }
501
502 /*======================================================*/
503
504 double ai = -0.5 * (Phu_ij * Phu_ij + Phv_ij * Phv_ij);
505 double bi =
506 kin_max[i] * Ph_ij - (huLow[i] * Phu_ij + hvLow[i] * Phv_ij);
507 double delta_i = bi * bi - 4. * ai * ci;
508
509 if (delta_i < 0. || ai >= -0.) {
510 KE_limiter = fmin(KE_limiter, Lij_array[ij]);
511 } else {
512 neg_root_i = (-bi - std::sqrt(delta_i)) / 2. / ai;
513 }
514
515 // root of jth-DOF (To compute transpose component)
516 double lambdai =
517 num_neighbours[j];
518 double mj = lumped_mass_matrix[j];
519 double cj = kin_max[j] * hLow[j] -
520 0.5 * (huLow[j] * huLow[j] + hvLow[j] * hvLow[j]);
521 double Ph_ji = -FCT_h[ij] / mj / lambdai; // Aij=-Aji
522 double Phu_ji = -FCT_hu[ij] / mj / lambdai;
523 double Phv_ji = -FCT_hv[ij] / mj / lambdai;
524 double aj = -0.5 * (Phu_ji * Phu_ji + Phv_ji * Phv_ji);
525 double bj =
526 kin_max[j] * Ph_ji - (huLow[j] * Phu_ji + hvLow[j] * Phv_ji);
527 double delta_j = bj * bj - 4. * aj * cj;
528
529 if (delta_j < 0. || aj >= -0.) {
530 KE_limiter = fmin(KE_limiter, Lij_array[ij]);
531 } else {
532 neg_root_j = (-bj - std::sqrt(delta_j)) / 2. / aj;
533 }
534
535 // define final limiter based on KE
536 KE_limiter =
537 fmin(KE_limiter, fmin(fabs(neg_root_i), fabs(neg_root_j)));
538
539 // Here we set final limiter
540 Lij_array[ij] = fmin(KE_limiter, Lij_array[ij]);
541
542 } else {
543 // if i = j then lij = 0
544 Lij_array[ij] = 0.;
545 }
546
547 // update ij
548 ij += 1;
549 } // end j loop
550 } // end i loop
551
552 // The kinetic-energy checks above use row-i data only, so l_ij and
553 // l_ji can differ; the limited flux conserves mass only if they agree.
554 for (int i = 0; i < numDOFs; i++) {
555 for (int offset = csrRowIndeces_DofLoops[i];
556 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
557 int j = csrColumnOffsets_DofLoops[offset];
558 if (j <= i)
559 continue;
560 for (int offset_ji = csrRowIndeces_DofLoops[j];
561 offset_ji < csrRowIndeces_DofLoops[j + 1]; offset_ji++) {
562 if (csrColumnOffsets_DofLoops[offset_ji] == i) {
563 double l = fmin(Lij_array[offset], Lij_array[offset_ji]);
564 Lij_array[offset] = l;
565 Lij_array[offset_ji] = l;
566 break;
567 }
568 }
569 }
570 }
571
572 /* Final loop to apply limiting and then define limited solution */
573 ij = 0;
574 for (int i = 0; i < numDOFs; i++) {
575
576 double one_over_mi = 1.0 / lumped_mass_matrix[i];
577 double ith_Limiter_times_FluxCorrectionMatrix1 = 0.;
578 double ith_Limiter_times_FluxCorrectionMatrix2 = 0.;
579 double ith_Limiter_times_FluxCorrectionMatrix3 = 0.;
580
581 // LOOP OVER THE SPARSITY PATTERN (j-LOOP)//
582 for (int offset = csrRowIndeces_DofLoops[i];
583 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
584
585 int j = csrColumnOffsets_DofLoops[offset];
586
587 // COMPUTE LIMITED FLUX //
588 ith_Limiter_times_FluxCorrectionMatrix1 += Lij_array[ij] * FCT_h[ij];
589 ith_Limiter_times_FluxCorrectionMatrix2 += Lij_array[ij] * FCT_hu[ij];
590 ith_Limiter_times_FluxCorrectionMatrix3 += Lij_array[ij] * FCT_hv[ij];
591
592 // update ij
593 ij += 1;
594 } // end j loop
595
596 // then we add lij*Aij to uLow
597 hLow[i] += one_over_mi * ith_Limiter_times_FluxCorrectionMatrix1;
598 huLow[i] += one_over_mi * ith_Limiter_times_FluxCorrectionMatrix2;
599 hvLow[i] += one_over_mi * ith_Limiter_times_FluxCorrectionMatrix3;
600
601 // Finally define the limited solution
602 limited_hnp1[i] = hLow[i];
603 limited_hunp1[i] = huLow[i] + dt * one_over_mi * new_SourceTerm_hu[i];
604 limited_hvnp1[i] = hvLow[i] + dt * one_over_mi * new_SourceTerm_hv[i];
605
606 if (limited_hnp1[i] < -hEps && dt < 1.0) {
607 std::cout << "Limited water height is negative: \n "
608 << "hLow: " << hLow[i] << "\n"
609 << "hHigh: " << limited_hnp1[i] << "\n"
610 << "hEps: " << hEps << "\n"
611 << " ... aborting!" << std::endl;
612 abort();
613 } else {
614 // clean up uHigh from round off error
615 if (limited_hnp1[i] < hEps)
616 limited_hnp1[i] = 0.0;
617 double aux = fmax(limited_hnp1[i], hEps);
618 limited_hunp1[i] *= 2 * std::pow(limited_hnp1[i], VEL_FIX_POWER) /
619 (std::pow(limited_hnp1[i], VEL_FIX_POWER) +
620 std::pow(aux, VEL_FIX_POWER));
621 limited_hvnp1[i] *= 2 * std::pow(limited_hnp1[i], VEL_FIX_POWER) /
622 (std::pow(limited_hnp1[i], VEL_FIX_POWER) +
623 std::pow(aux, VEL_FIX_POWER));
624 }
625 } // end i loop
626
627 // update FCT matrices as Fct = (1 - Lij)*Fct
628 FCT_h = (1.0 - Lij_array) * FCT_h;
629 FCT_hu = (1.0 - Lij_array) * FCT_hu;
630 FCT_hv = (1.0 - Lij_array) * FCT_hv;
631 } // end loop for limiting iteration
632
633 for (std::size_t k = 0; k < nnz_fct; k++) {
634 FCT_all[0 * nnz_fct + k] = FCT_h[k];
635 FCT_all[1 * nnz_fct + k] = FCT_hu[k];
636 FCT_all[2 * nnz_fct + k] = FCT_hv[k];
637 FCT_all[3 * nnz_fct + k] = Lij_array[k];
638 }
639 } // end convex limiting function
640
641 // Porous edges: the graph viscosity on edge ij carries max(phi_i, phi_j).
642 // With fluxes phi_j F_j and storage m_i phi_i this keeps the bar states
643 // positive (phi_i/phibar <= 1) and the dissipation symmetric (conservative).
644 static inline double phiBar(int i, int j, xt::pyarray<double> &porosity_dof) {
645 return fmax(porosity_dof[i], porosity_dof[j]);
646 }
647
648 // Wall rows with wall_dof = 2 (WALL_RIEMANN): the boundary flux is the Rusanov
649 // flux against the mirror state, whose normal momentum is reversed. With
650 // nu = n_i/|n_i|, u_n = u.nu and lambda = |u_n| + sqrt(g h) the mass flux is
651 // zero and the momentum flux is (p + h u_n^2 + lambda h u_n) nu. The
652 // hydrostatic p is already carried by the pressure on c_ij, so only
653 // dp = h u_n (u_n + lambda)
654 // is added (times n_i). It raises the wall pressure when the flow runs into
655 // the wall and lowers it when the flow leaves, the impedance of the exact
656 // wall Riemann problem. rate = (u_n + lambda)|n_i| >= 0 is its damping rate
657 // on the normal momentum (for the CFL).
658 inline void wallRiemann(double g, double hi, double hui, double hvi,
659 double nx, double ny, double hEps, double &dp,
660 double &rate) {
661 dp = 0.;
662 rate = 0.;
663 const double nn = std::sqrt(nx * nx + ny * ny);
664 if (nn <= 0. || hi <= hEps)
665 return;
666 const double one_over_h = 2.0 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2));
667 const double qn = (hui * nx + hvi * ny) / nn;
668 const double un = qn * one_over_h;
669 const double lambda = std::fabs(un) + std::sqrt(g * hi);
670 dp = qn * (un + lambda);
671 rate = (un + lambda) * nn;
672 }
673
674 // Tailwater rows (tailwater_dof = 1): exact Godunov flux against a
675 // characteristic ghost state. The ghost has depth hb (the stage minus the
676 // bed, equal to h_i at rest when the stage matches), the tangential velocity
677 // of the interior and the outgoing Riemann invariant kept,
678 // u_n,b = u_n,i + 2 (sqrt(g h_i) - sqrt(g h_b)),
679 // so the Riemann problem (U_i, U_b) contains a single wave of the u_n - c
680 // family and the state at the interface is one of three:
681 // shock (hb > hi): speed s = (hb u_b - hi u_i)/(hb - hi); U_b if s < 0,
682 // else U_i (the bore is swept out of the domain);
683 // rarefaction (hb < hi): U_b if u_b - c_b <= 0, U_i if u_i - c_i >= 0,
684 // else the critical state inside the fan (c = (u_i + 2 c_i)/3, u_n = c).
685 // Supercritical outflow therefore ignores the data unless the tailwater
686 // is high enough to send a wave upstream. The natural (open) row already
687 // carries F(U_i).nu through c_ij, so only
688 // dF = F(U*).nu - F(U_i).nu (zero when U_b = U_i)
689 // times the boundary length |n_i| is added to the low-order and high-order
690 // updates. rate = (|u_n,i| + c_i + |u_n,b| + c_b)|n_i| / 2 bounds the
691 // damping rate (for the CFL).
692 inline void tailwaterFlux(double g, double hi, double hui, double hvi,
693 double hb, double nx, double ny, double hEps,
694 double &dFh, double &dFhu, double &dFhv,
695 double &rate) {
696 dFh = dFhu = dFhv = rate = 0.;
697 const double nn = std::sqrt(nx * nx + ny * ny);
698 if (nn <= 0. || (hi <= hEps && hb <= hEps))
699 return;
700 const double nux = nx / nn, nuy = ny / nn;
701 const double tx = -nuy, ty = nux;
702 const double one_over_h = 2.0 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2));
703 const double ui = hui * one_over_h, vi = hvi * one_over_h;
704 const double un_i = ui * nux + vi * nuy, ut = ui * tx + vi * ty;
705 const double c_i = std::sqrt(g * fmax(hi, 0.));
706 const double c_b = std::sqrt(g * fmax(hb, 0.));
707 const double R = un_i + 2.0 * c_i;
708 const double un_b = R - 2.0 * c_b;
709 rate = 0.5 * (std::fabs(un_i) + c_i + std::fabs(un_b) + c_b) * nn;
710 double hs, uns;
711 bool ghost_state = false;
712 if (hb >= hi) {
713 if (hb - hi <= 1e-14) // ghost = interior
714 return;
715 const double s = (hb * un_b - hi * un_i) / (hb - hi);
716 if (s < 0.) { hs = hb; uns = un_b; ghost_state = true; } else { hs = hi; uns = un_i; }
717 } else {
718 if (un_b - c_b <= 0.) { hs = hb; uns = un_b; ghost_state = true; }
719 else if (un_i - c_i >= 0.) { hs = hi; uns = un_i; }
720 else { const double c = R / 3.0; hs = c * c / g; uns = c; }
721 }
722 // inflow brings the tangential velocity of the outside (zero), outflow keeps
723 // the interior one
724 const double ut_s = (ghost_state && uns < 0.) ? 0. : ut;
725 const double p_s = 0.5 * g * hs * hs, p_i = 0.5 * g * hi * hi;
726 const double mass_s = hs * uns, mass_i = hi * un_i;
727 dFh = (mass_s - mass_i) * nn;
728 dFhu = (mass_s * (uns * nux + ut_s * tx) + p_s * nux -
729 (mass_i * (un_i * nux + ut * tx) + p_i * nux)) * nn;
730 dFhv = (mass_s * (uns * nuy + ut_s * ty) + p_s * nuy -
731 (mass_i * (un_i * nuy + ut * ty) + p_i * nuy)) * nn;
732 }
733
734 // On an edge touching a wall row, c'_ij = -c_ji no longer equals -c'_ji,
735 // so mu_ij computed from row i alone differs from mu_ji. The dissipation
736 // sum_j mu_ij (U_j - U_i) is conservative only if mu is symmetric, so take
737 // the max over both orientations; this returns the row-j orientation.
738 inline double muWallTranspose(int i, int j, int ij, double ui, double vi,
739 double uj, double vj,
740 xt::pyarray<double> &wall_dof,
741 xt::pyarray<double> &Cx,
742 xt::pyarray<double> &Cy,
743 xt::pyarray<double> &CTx,
744 xt::pyarray<double> &CTy) {
745 // c'_ji: c_ji = CTx[ij] on an ordinary row j, -c_ij on a wall row j
746 double cx_ji = wall_dof[j] > 0.5 ? -Cx[ij] : CTx[ij];
747 double cy_ji = wall_dof[j] > 0.5 ? -Cy[ij] : CTy[ij];
748 return fmax(fmax(0., -(uj * cx_ji + vj * cy_ji)),
749 fmax(0., (ui * cx_ji + vi * cy_ji)));
750 }
751
753 double g = args.scalar<double>("g");
754 int numDOFsPerEqn = args.scalar<int>("numDOFsPerEqn");
755 xt::pyarray<double> &lumped_mass_matrix =
756 args.array<double>("lumped_mass_matrix");
757 xt::pyarray<double> &h_dof_old = args.array<double>("h_dof_old");
758 xt::pyarray<double> &hu_dof_old = args.array<double>("hu_dof_old");
759 xt::pyarray<double> &hv_dof_old = args.array<double>("hv_dof_old");
760 xt::pyarray<double> &b_dof = args.array<double>("b_dof");
761 xt::pyarray<int> &csrRowIndeces_DofLoops =
762 args.array<int>("csrRowIndeces_DofLoops");
763 xt::pyarray<int> &csrColumnOffsets_DofLoops =
764 args.array<int>("csrColumnOffsets_DofLoops");
765 double hEps = args.scalar<double>("hEps");
766 xt::pyarray<double> &Cx = args.array<double>("Cx");
767 xt::pyarray<double> &Cy = args.array<double>("Cy");
768 xt::pyarray<double> &CTx = args.array<double>("CTx");
769 xt::pyarray<double> &CTy = args.array<double>("CTy");
770 xt::pyarray<double> &dLow = args.array<double>("dLow");
771 double run_cfl = args.scalar<double>("run_cfl");
772 xt::pyarray<double> &edge_based_cfl = args.array<double>("edge_based_cfl");
773 int debug = args.scalar<int>("debug");
774 xt::pyarray<double> &wall_dof = args.array<double>("wall_dof");
775 xt::pyarray<double> &tailwater_dof = args.array<double>("tailwater_dof");
776 xt::pyarray<double> &tailwater_depth_dof =
777 args.array<double>("tailwater_depth_dof");
778 xt::pyarray<double> &porosity_dof = args.array<double>("porosity_dof");
779
780 double max_edge_based_cfl = 0.;
781
782 int ij = 0;
783 for (int i = 0; i < numDOFsPerEqn; i++) {
784 // solution at time tn for the ith DOF
785 double hi = h_dof_old[i];
786 double hui = hu_dof_old[i];
787 double hvi = hv_dof_old[i];
788 double dLowii = 0.;
789 // boundary vector n_i = sum_j c_ji = int_{dOmega} phi_i n (zero inside)
790 double nix = 0., niy = 0.;
791
792 for (int offset = csrRowIndeces_DofLoops[i];
793 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
794
795 // loop in j (sparsity pattern)
796 int j = csrColumnOffsets_DofLoops[offset];
797
798 // solution at time tn for the jth DOF
799 double hj = h_dof_old[j];
800 double huj = hu_dof_old[j];
801 double hvj = hv_dof_old[j];
802
803 if (i != j) {
805 // DISSIPATIVE MATRIX //
807 double cij_norm = sqrt(Cx[ij] * Cx[ij] + Cy[ij] * Cy[ij]);
808 double cji_norm = sqrt(CTx[ij] * CTx[ij] + CTy[ij] * CTy[ij]);
809 double nxij = Cx[ij] / cij_norm, nyij = Cy[ij] / cij_norm;
810 double nxji = CTx[ij] / cji_norm, nyji = CTy[ij] / cji_norm;
811 dLow[ij] =
812 fmax(maxWaveSpeedSharpInitialGuess(g, nxij, nyij, hi, hui, hvi,
813 hj, huj, hvj, hEps, debug) *
814 cij_norm, // hEps
815 maxWaveSpeedSharpInitialGuess(g, nxji, nyji, hj, huj, hvj,
816 hi, hui, hvi, hEps, debug) *
817 cji_norm); // hEps
818 dLow[ij] *= phiBar(i, j, porosity_dof);
819 dLowii -= dLow[ij];
820 } else
821 dLow[ij] = 0.;
822 nix += CTx[ij];
823 niy += CTy[ij];
824 // update ij
825 ij += 1;
826 }
828 // CALCULATE EDGE BASED CFL //
830 double mi = lumped_mass_matrix[i];
831 // On a wall row the advective boundary term adds (u_i.n_i) U_i to the
832 // update (see calculateResidual); when the flow leaves the wall
833 // (u_i.n_i < 0) it needs that much more room for positivity.
834 double wall_margin = 0.;
835 if (wall_dof[i] > 0.5) {
836 double one_over_hiReg =
837 2.0 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2));
838 wall_margin = 0.5 * fmax(0., -porosity_dof[i] * (hui * nix + hvi * niy) *
839 one_over_hiReg);
840 }
841 if (wall_dof[i] > 1.5) {
842 double dp_w, rate_w;
843 wallRiemann(g, hi, hui, hvi, nix, niy, hEps, dp_w, rate_w);
844 wall_margin += 0.5 * porosity_dof[i] * rate_w;
845 }
846 if (tailwater_dof[i] > 0.5) {
847 double dFh_t, dFhu_t, dFhv_t, rate_t;
848 tailwaterFlux(g, hi, hui, hvi, tailwater_depth_dof[i], nix, niy, hEps,
849 dFh_t, dFhu_t, dFhv_t, rate_t);
850 wall_margin += 0.5 * porosity_dof[i] * rate_t;
851 }
852 edge_based_cfl[i] = (fabs(dLowii) + wall_margin) / mi;
853 max_edge_based_cfl = fmax(max_edge_based_cfl, edge_based_cfl[i]);
854 }
855 return max_edge_based_cfl;
856 } // End calculateEdgeBasedCFL
857
859 double g = args.scalar<double>("g");
860 xt::pyarray<double> &h_dof_old = args.array<double>("h_dof_old");
861 xt::pyarray<double> &hu_dof_old = args.array<double>("hu_dof_old");
862 xt::pyarray<double> &hv_dof_old = args.array<double>("hv_dof_old");
863 xt::pyarray<double> &b_dof = args.array<double>("b_dof");
864 xt::pyarray<double> &Cx = args.array<double>("Cx");
865 xt::pyarray<double> &Cy = args.array<double>("Cy");
866 xt::pyarray<double> &CTx = args.array<double>("CTx");
867 xt::pyarray<double> &CTy = args.array<double>("CTy");
868 int numDOFsPerEqn = args.scalar<int>("numDOFsPerEqn");
869 xt::pyarray<int> &csrRowIndeces_DofLoops =
870 args.array<int>("csrRowIndeces_DofLoops");
871 xt::pyarray<int> &csrColumnOffsets_DofLoops =
872 args.array<int>("csrColumnOffsets_DofLoops");
873 xt::pyarray<double> &lumped_mass_matrix =
874 args.array<double>("lumped_mass_matrix");
875 double eps = args.scalar<double>("eps");
876 double hEps = args.scalar<double>("hEps");
877 xt::pyarray<double> &global_entropy_residual =
878 args.array<double>("global_entropy_residual");
879 double &dij_small = args.scalar<double>("dij_small");
880
882 // ********** FIRST LOOP ON DOFs ********** //
884
885 // To compute:
886 // * Entropy at i-th node
887 std::valarray<double> eta(numDOFsPerEqn);
888 for (int i = 0; i < numDOFsPerEqn; i++) {
889 // COMPUTE ENTROPY. NOTE: WE CONSIDER A FLAT BOTTOM
890 double hi = h_dof_old[i];
891 double one_over_hiReg =
892 2 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2)); // hEps
893 eta[i] = ENTROPY(g, hi, hu_dof_old[i], hv_dof_old[i], 0., one_over_hiReg);
894 }
895
896 // ********** END OF FIRST LOOP ON DOFs ********** //
897
899 // ********** SECOND LOOP ON DOFs ********** //
901 // To compute:
902 // * global entropy residual
903 // * dij_small to avoid division by 0
904
905 int ij = 0;
906 std::valarray<double> etaMax(numDOFsPerEqn), etaMin(numDOFsPerEqn);
907
908 // speed = sqrt(g max(h_0)), I divide by h_epsilon to get max(h_0)
909 double speed = std::sqrt(g * hEps / eps);
910 dij_small = 0.0;
911
912 for (int i = 0; i < numDOFsPerEqn; i++) {
913
914 // solution at time tn for the ith DOF
915 double hi = h_dof_old[i];
916 double hui = hu_dof_old[i];
917 double hvi = hv_dof_old[i];
918 double Zi = b_dof[i];
919
920 // Define some things using above
921 double one_over_hiReg =
922 2 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2)); // hEps
923 double ui = hui * one_over_hiReg;
924 double vi = hvi * one_over_hiReg;
925 double mi = lumped_mass_matrix[i];
926
927 // initialize etaMax and etaMin
928 etaMax[i] = fabs(eta[i]);
929 etaMin[i] = fabs(eta[i]);
930
931 // FOR ENTROPY RESIDUAL, NOTE: FLAT BOTTOM //
932 double ith_flux_term1 = 0., ith_flux_term2 = 0., ith_flux_term3 = 0.;
933 double entropy_flux = 0.;
934 double sum_entprime_flux = 0.;
935 double eta_prime1 = DENTROPY_DH(g, hi, hui, hvi, 0., one_over_hiReg);
936 double eta_prime2 = DENTROPY_DHU(g, hi, hui, hvi, 0., one_over_hiReg);
937 double eta_prime3 = DENTROPY_DHV(g, hi, hui, hvi, 0., one_over_hiReg);
938
939 // loop in j (sparsity pattern)
940 for (int offset = csrRowIndeces_DofLoops[i];
941 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
942
943 int j = csrColumnOffsets_DofLoops[offset];
944
945 // solution at time tn for the jth DOF
946 double hj = h_dof_old[j];
947 double huj = hu_dof_old[j];
948 double hvj = hv_dof_old[j];
949 double Zj = b_dof[j];
950
951 // Then define some things here using above
952 double one_over_hjReg =
953 2.0 * hj / (hj * hj + std::pow(fmax(hj, hEps), 2));
954 double uj = huj * one_over_hjReg;
955 double vj = hvj * one_over_hjReg;
956
957 // auxiliary functions to compute fluxes
958 double aux_h =
959 (uj * hj - ui * hi) * Cx[ij] + (vj * hj - vi * hi) * Cy[ij];
960 double aux_hu =
961 (uj * huj - ui * hui) * Cx[ij] + (vj * huj - vi * hui) * Cy[ij];
962 double aux_hv =
963 (uj * hvj - ui * hvi) * Cx[ij] + (vj * hvj - vi * hvi) * Cy[ij];
964
965 // flux for entropy
966 ith_flux_term1 += aux_h;
967 ith_flux_term2 += aux_hu + 0.5 * g * hj * hj * Cx[ij];
968 ith_flux_term3 += aux_hv + 0.5 * g * hj * hj * Cy[ij];
969
970 // NOTE: WE CONSIDER FLAT BOTTOM
971 entropy_flux +=
972 (Cx[ij] * ENTROPY_FLUX1(g, hj, huj, hvj, 0., one_over_hjReg) +
973 Cy[ij] * ENTROPY_FLUX2(g, hj, huj, hvj, 0., one_over_hjReg));
974
975 // COMPUTE ETA MIN AND ETA MAX //
976 etaMax[i] = fmax(etaMax[i], fabs(eta[j]));
977 etaMin[i] = fmin(etaMin[i], fabs(eta[j]));
978
979 // define dij_small in j loop
980 double x = fabs(Cx[ij]) + fabs(Cy[ij]);
981 dij_small = fmax(dij_small, x * speed);
982
983 // update ij
984 ij += 1;
985 } // end j loop
986
987 // define sum of entprime*flux
988 sum_entprime_flux =
989 (ith_flux_term1 * eta_prime1 + ith_flux_term2 * eta_prime2 +
990 ith_flux_term3 * eta_prime3);
991
992 // define rescale for normalization
993 double small_rescale = g * hEps * hEps / eps;
994 double rescale = fmax(fabs(etaMax[i] - etaMin[i]) / 2., small_rescale);
995
996 // COMPUTE ENTROPY RESIDUAL //
997 double one_over_entNormFactori = 1.0 / rescale;
998 global_entropy_residual[i] =
999 one_over_entNormFactori * fabs(entropy_flux - sum_entprime_flux);
1000 if (hi <= hEps) {
1001 global_entropy_residual[i] = 1.0;
1002 }
1003 } // end i loop
1004
1005 // Finally dij_small here
1006 dij_small = 1E-14 * dij_small;
1007 // ********** END OF LOOP IN DOFs ********** //
1008 } // end calculateEV
1009
1011 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
1012 xt::pyarray<double> &mesh_grad_trial_ref =
1013 args.array<double>("mesh_grad_trial_ref");
1014 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
1015 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
1016 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
1017 xt::pyarray<double> &h_trial_ref = args.array<double>("h_trial_ref");
1018 xt::pyarray<double> &h_grad_trial_ref =
1019 args.array<double>("h_grad_trial_ref");
1020 xt::pyarray<double> &h_test_ref = args.array<double>("h_test_ref");
1021 xt::pyarray<double> &h_grad_test_ref =
1022 args.array<double>("h_grad_test_ref");
1023 xt::pyarray<double> &vel_trial_ref = args.array<double>("vel_trial_ref");
1024 xt::pyarray<double> &vel_grad_trial_ref =
1025 args.array<double>("vel_grad_trial_ref");
1026 xt::pyarray<double> &vel_test_ref = args.array<double>("vel_test_ref");
1027 xt::pyarray<double> &vel_grad_test_ref =
1028 args.array<double>("vel_grad_test_ref");
1029 xt::pyarray<double> &mesh_trial_trace_ref =
1030 args.array<double>("mesh_trial_trace_ref");
1031 xt::pyarray<double> &mesh_grad_trial_trace_ref =
1032 args.array<double>("mesh_grad_trial_trace_ref");
1033 xt::pyarray<double> &h_trial_trace_ref =
1034 args.array<double>("h_trial_trace_ref");
1035 xt::pyarray<double> &h_grad_trial_trace_ref =
1036 args.array<double>("h_grad_trial_trace_ref");
1037 xt::pyarray<double> &h_test_trace_ref =
1038 args.array<double>("h_test_trace_ref");
1039 xt::pyarray<double> &h_grad_test_trace_ref =
1040 args.array<double>("h_grad_test_trace_ref");
1041 xt::pyarray<double> &vel_trial_trace_ref =
1042 args.array<double>("vel_trial_trace_ref");
1043 xt::pyarray<double> &vel_grad_trial_trace_ref =
1044 args.array<double>("vel_grad_trial_trace_ref");
1045 xt::pyarray<double> &vel_test_trace_ref =
1046 args.array<double>("vel_test_trace_ref");
1047 xt::pyarray<double> &vel_grad_test_trace_ref =
1048 args.array<double>("vel_grad_test_trace_ref");
1049 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
1050 xt::pyarray<double> &boundaryJac_ref =
1051 args.array<double>("boundaryJac_ref");
1052 xt::pyarray<double> &elementDiameter =
1053 args.array<double>("elementDiameter");
1054 int nElements_global = args.scalar<int>("nElements_global");
1055 double g = args.scalar<double>("g");
1056 xt::pyarray<int> &h_l2g = args.array<int>("h_l2g");
1057 xt::pyarray<int> &vel_l2g = args.array<int>("vel_l2g");
1058 xt::pyarray<double> &h_dof_old = args.array<double>("h_dof_old");
1059 xt::pyarray<double> &hu_dof_old = args.array<double>("hu_dof_old");
1060 xt::pyarray<double> &hv_dof_old = args.array<double>("hv_dof_old");
1061 xt::pyarray<double> &b_dof = args.array<double>("b_dof");
1062 xt::pyarray<double> &h_dof = args.array<double>("h_dof");
1063 xt::pyarray<double> &hu_dof = args.array<double>("hu_dof");
1064 xt::pyarray<double> &hv_dof = args.array<double>("hv_dof");
1065 xt::pyarray<double> &q_cfl = args.array<double>("q_cfl");
1066 xt::pyarray<int> &sdInfo_hu_hu_rowptr =
1067 args.array<int>("sdInfo_hu_hu_rowptr");
1068 xt::pyarray<int> &sdInfo_hu_hu_colind =
1069 args.array<int>("sdInfo_hu_hu_colind");
1070 xt::pyarray<int> &sdInfo_hu_hv_rowptr =
1071 args.array<int>("sdInfo_hu_hv_rowptr");
1072 xt::pyarray<int> &sdInfo_hu_hv_colind =
1073 args.array<int>("sdInfo_hu_hv_colind");
1074 xt::pyarray<int> &sdInfo_hv_hv_rowptr =
1075 args.array<int>("sdInfo_hv_hv_rowptr");
1076 xt::pyarray<int> &sdInfo_hv_hv_colind =
1077 args.array<int>("sdInfo_hv_hv_colind");
1078 xt::pyarray<int> &sdInfo_hv_hu_rowptr =
1079 args.array<int>("sdInfo_hv_hu_rowptr");
1080 xt::pyarray<int> &sdInfo_hv_hu_colind =
1081 args.array<int>("sdInfo_hv_hu_colind");
1082 int offset_h = args.scalar<int>("offset_h");
1083 int offset_hu = args.scalar<int>("offset_hu");
1084 int offset_hv = args.scalar<int>("offset_hv");
1085 int stride_h = args.scalar<int>("stride_h");
1086 int stride_hu = args.scalar<int>("stride_hu");
1087 int stride_hv = args.scalar<int>("stride_hv");
1088 xt::pyarray<double> &globalResidual = args.array<double>("globalResidual");
1089 int nExteriorElementBoundaries_global =
1090 args.scalar<int>("nExteriorElementBoundaries_global");
1091 xt::pyarray<int> &exteriorElementBoundariesArray =
1092 args.array<int>("exteriorElementBoundariesArray");
1093 xt::pyarray<int> &elementBoundaryElementsArray =
1094 args.array<int>("elementBoundaryElementsArray");
1095 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray =
1096 args.array<int>("elementBoundaryLocalElementBoundariesArray");
1097 xt::pyarray<int> &isDOFBoundary_h = args.array<int>("isDOFBoundary_h");
1098 xt::pyarray<int> &isDOFBoundary_hu = args.array<int>("isDOFBoundary_hu");
1099 xt::pyarray<int> &isDOFBoundary_hv = args.array<int>("isDOFBoundary_hv");
1100 xt::pyarray<int> &isAdvectiveFluxBoundary_h =
1101 args.array<int>("isAdvectiveFluxBoundary_h");
1102 xt::pyarray<int> &isAdvectiveFluxBoundary_hu =
1103 args.array<int>("isAdvectiveFluxBoundary_hu");
1104 xt::pyarray<int> &isAdvectiveFluxBoundary_hv =
1105 args.array<int>("isAdvectiveFluxBoundary_hv");
1106 xt::pyarray<int> &isDiffusiveFluxBoundary_hu =
1107 args.array<int>("isDiffusiveFluxBoundary_hu");
1108 xt::pyarray<int> &isDiffusiveFluxBoundary_hv =
1109 args.array<int>("isDiffusiveFluxBoundary_hv");
1110 xt::pyarray<double> &ebqe_bc_h_ext = args.array<double>("ebqe_bc_h_ext");
1111 xt::pyarray<double> &ebqe_bc_flux_mass_ext =
1112 args.array<double>("ebqe_bc_flux_mass_ext");
1113 xt::pyarray<double> &ebqe_bc_flux_mom_hu_adv_ext =
1114 args.array<double>("ebqe_bc_flux_mom_hu_adv_ext");
1115 xt::pyarray<double> &ebqe_bc_flux_mom_hv_adv_ext =
1116 args.array<double>("ebqe_bc_flux_mom_hv_adv_ext");
1117 xt::pyarray<double> &ebqe_bc_hu_ext = args.array<double>("ebqe_bc_hu_ext");
1118 xt::pyarray<double> &ebqe_bc_flux_hu_diff_ext =
1119 args.array<double>("ebqe_bc_flux_hu_diff_ext");
1120 xt::pyarray<double> &ebqe_penalty_ext =
1121 args.array<double>("ebqe_penalty_ext");
1122 xt::pyarray<double> &ebqe_bc_hv_ext = args.array<double>("ebqe_bc_hv_ext");
1123 xt::pyarray<double> &ebqe_bc_flux_hv_diff_ext =
1124 args.array<double>("ebqe_bc_flux_hv_diff_ext");
1125 xt::pyarray<double> &q_velocity = args.array<double>("q_velocity");
1126 xt::pyarray<double> &ebqe_velocity = args.array<double>("ebqe_velocity");
1127 xt::pyarray<double> &flux = args.array<double>("flux");
1128 xt::pyarray<double> &elementResidual_h =
1129 args.array<double>("elementResidual_h");
1130 xt::pyarray<double> &Cx = args.array<double>("Cx");
1131 xt::pyarray<double> &Cy = args.array<double>("Cy");
1132 xt::pyarray<double> &CTx = args.array<double>("CTx");
1133 xt::pyarray<double> &CTy = args.array<double>("CTy");
1134 int numDOFsPerEqn = args.scalar<int>("numDOFsPerEqn");
1135 int NNZ = args.scalar<int>("NNZ");
1136 xt::pyarray<int> &csrRowIndeces_DofLoops =
1137 args.array<int>("csrRowIndeces_DofLoops");
1138 xt::pyarray<int> &csrColumnOffsets_DofLoops =
1139 args.array<int>("csrColumnOffsets_DofLoops");
1140 xt::pyarray<double> &lumped_mass_matrix =
1141 args.array<double>("lumped_mass_matrix");
1142 double cfl_run = args.scalar<double>("cfl_run");
1143 double eps = args.scalar<double>("eps");
1144 double hEps = args.scalar<double>("hEps");
1145 xt::pyarray<double> &hnp1_at_quad_point =
1146 args.array<double>("hnp1_at_quad_point");
1147 xt::pyarray<double> &hunp1_at_quad_point =
1148 args.array<double>("hunp1_at_quad_point");
1149 xt::pyarray<double> &hvnp1_at_quad_point =
1150 args.array<double>("hvnp1_at_quad_point");
1151 xt::pyarray<double> &extendedSourceTerm_hu =
1152 args.array<double>("extendedSourceTerm_hu");
1153 xt::pyarray<double> &extendedSourceTerm_hv =
1154 args.array<double>("extendedSourceTerm_hv");
1155 xt::pyarray<double> &dH_minus_dL = args.array<double>("dH_minus_dL");
1156 xt::pyarray<double> &muH_minus_muL = args.array<double>("muH_minus_muL");
1157 double cE = args.scalar<double>("cE");
1158 int LUMPED_MASS_MATRIX = args.scalar<int>("LUMPED_MASS_MATRIX");
1159 // diagnostic: no bed in the star states (well-balanced dissipation off)
1160 int NO_STAR_STATES = args.scalar<int>("NO_STAR_STATES");
1161 double dt = args.scalar<double>("dt");
1162 int LINEAR_FRICTION = args.scalar<int>("LINEAR_FRICTION");
1163 // 1: local bounds from the neighbours' bar states only (ubar_ii = u_i
1164 // excluded), so an existing odd-even mode is not within bounds; the
1165 // low-order state is still added to the bounds below.
1166 int BOUNDS_EXCLUDE_SELF = args.scalar<int>("BOUNDS_EXCLUDE_SELF");
1167 int NODAL_H_BOUNDS = args.scalar<int>("NODAL_H_BOUNDS");
1168 double mannings = args.scalar<double>("mannings");
1169 // 1 on rows whose advective terms are integrated by parts (no-flux walls)
1170 xt::pyarray<double> &wall_dof = args.array<double>("wall_dof");
1171 xt::pyarray<double> &tailwater_dof = args.array<double>("tailwater_dof");
1172 xt::pyarray<double> &tailwater_depth_dof =
1173 args.array<double>("tailwater_depth_dof");
1174 // Canopy: porosity phi (storage phi h, flux phi h u, pressure
1175 // phi g h grad(h+Z)) and Darcy-Forchheimer drag -(alpha + beta|u|) h u per
1176 // unit plan area. All nodal; phi = 1, alpha = beta = 0 outside canopies.
1177 xt::pyarray<double> &porosity_dof = args.array<double>("porosity_dof");
1178 xt::pyarray<double> &drag_alpha_dof = args.array<double>("drag_alpha_dof");
1179 xt::pyarray<double> &drag_beta_dof = args.array<double>("drag_beta_dof");
1180 xt::pyarray<double> &quantDOFs = args.array<double>("quantDOFs");
1181 int SECOND_CALL_CALCULATE_RESIDUAL =
1182 args.scalar<int>("SECOND_CALL_CALCULATE_RESIDUAL");
1183 int COMPUTE_NORMALS = args.scalar<int>("COMPUTE_NORMALS");
1184 xt::pyarray<double> &normalx = args.array<double>("normalx");
1185 xt::pyarray<double> &normaly = args.array<double>("normaly");
1186 xt::pyarray<double> &dLow = args.array<double>("dLow");
1187 int lstage = args.scalar<int>("lstage");
1188 xt::pyarray<double> &new_SourceTerm_hu =
1189 args.array<double>("new_SourceTerm_hu");
1190 xt::pyarray<double> &new_SourceTerm_hv =
1191 args.array<double>("new_SourceTerm_hv");
1192 xt::pyarray<double> &global_entropy_residual =
1193 args.array<double>("global_entropy_residual");
1194 double dij_small = args.scalar<double>("dij_small");
1195 xt::pyarray<double> &hLow = args.array<double>("hLow");
1196 xt::pyarray<double> &huLow = args.array<double>("huLow");
1197 xt::pyarray<double> &hvLow = args.array<double>("hvLow");
1198 xt::pyarray<double> &h_min = args.array<double>("h_min");
1199 xt::pyarray<double> &h_max = args.array<double>("h_max");
1200 xt::pyarray<double> &kin_max = args.array<double>("kin_max");
1201 xt::pyarray<double> &urelax = args.array<double>("urelax");
1202 xt::pyarray<double> &drelax = args.array<double>("drelax");
1203 // FOR FRICTION//
1204 double n2 = std::pow(mannings, 2.);
1205 double gamma = 4. / 3;
1206 double xi = 10.;
1207
1209 // ********** CELL LOOPS ********** //
1211 // To compute:
1212 // * Time derivative term
1213 // * Cell based CFL
1214 // * Velocity and soln at quad points (for other models)
1215 for (int eN = 0; eN < nElements_global; eN++) {
1216 // declare local storage for element residual and initialize
1217 double elementResidual_h[nDOF_test_element],
1218 elementResidual_hu[nDOF_test_element],
1219 elementResidual_hv[nDOF_test_element];
1220
1221 for (int i = 0; i < nDOF_test_element; i++) {
1222 elementResidual_h[i] = 0.0;
1223 elementResidual_hu[i] = 0.0;
1224 elementResidual_hv[i] = 0.0;
1225 }
1226 //
1227 // loop over quadrature points and compute integrands
1228 //
1229 for (int k = 0; k < nQuadraturePoints_element; k++) {
1230 // compute indices and declare local storage
1231 int eN_k = eN * nQuadraturePoints_element + k,
1232 eN_k_nSpace = eN_k * nSpace,
1233 eN_nDOF_trial_element = eN * nDOF_trial_element;
1234 double h = 0.0, hu = 0.0,
1235 hv = 0.0, // solution at current time
1236 h_old = 0.0, hu_old = 0.0, hv_old = 0.0, // solution at lstage
1237 jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace],
1238 h_test_dV[nDOF_trial_element], dV, x, y, xt, yt;
1239 // get jacobian, etc for mapping reference element
1240 ck.calculateMapping_element(
1241 eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(),
1242 mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y);
1243 // get the physical integration weight, times the porosity (storage is
1244 // phi h, phi hu, phi hv)
1245 dV = fabs(jacDet) * dV_ref[k];
1246 {
1247 double phi_q = 0.;
1248 ck.valFromDOF(porosity_dof.data(),
1249 &h_l2g.data()[eN_nDOF_trial_element],
1250 &h_trial_ref.data()[k * nDOF_trial_element], phi_q);
1251 dV *= phi_q;
1252 }
1253 // get the solution at current time
1254 ck.valFromDOF(h_dof.data(), &h_l2g.data()[eN_nDOF_trial_element],
1255 &h_trial_ref.data()[k * nDOF_trial_element], h);
1256 ck.valFromDOF(hu_dof.data(), &vel_l2g[eN_nDOF_trial_element],
1257 &vel_trial_ref.data()[k * nDOF_trial_element], hu);
1258 ck.valFromDOF(hv_dof.data(), &vel_l2g[eN_nDOF_trial_element],
1259 &vel_trial_ref.data()[k * nDOF_trial_element], hv);
1260 // get the solution at the lstage
1261 ck.valFromDOF(h_dof_old.data(), &h_l2g.data()[eN_nDOF_trial_element],
1262 &h_trial_ref.data()[k * nDOF_trial_element], h_old);
1263 ck.valFromDOF(hu_dof_old.data(), &vel_l2g.data()[eN_nDOF_trial_element],
1264 &vel_trial_ref.data()[k * nDOF_trial_element], hu_old);
1265 ck.valFromDOF(hv_dof_old.data(), &vel_l2g.data()[eN_nDOF_trial_element],
1266 &vel_trial_ref.data()[k * nDOF_trial_element], hv_old);
1267 // calculate cell based CFL to keep a reference
1268 calculateCFL(elementDiameter.data()[eN], g, h_old, hu_old, hv_old, hEps,
1269 q_cfl[eN_k]);
1270 // precalculate test function products with integration weights
1271 for (int j = 0; j < nDOF_trial_element; j++)
1272 h_test_dV[j] = h_test_ref[k * nDOF_trial_element + j] * dV;
1273
1274 // SAVE VELOCITY // at quadrature points for other models to use
1275 q_velocity[eN_k_nSpace + 0] =
1276 2 * h / (h * h + std::pow(fmax(h, hEps), 2)) * hu;
1277 q_velocity[eN_k_nSpace + 1] =
1278 2 * h / (h * h + std::pow(fmax(h, hEps), 2)) * hv;
1279 hnp1_at_quad_point[eN_k] = h;
1280 hunp1_at_quad_point[eN_k] = hu;
1281 hvnp1_at_quad_point[eN_k] = hv;
1282
1283 for (int i = 0; i < nDOF_test_element; i++) {
1284 // compute time derivative part of global residual. NOTE: no lumping
1285 elementResidual_h[i] += (h - h_old) * h_test_dV[i];
1286 elementResidual_hu[i] += (hu - hu_old) * h_test_dV[i];
1287 elementResidual_hv[i] += (hv - hv_old) * h_test_dV[i];
1288 }
1289 }
1290 // distribute
1291 for (int i = 0; i < nDOF_test_element; i++) {
1292 int eN_i = eN * nDOF_test_element + i;
1293
1294 // global i-th index for h (this is same for vel_l2g)
1295 int h_gi = h_l2g[eN_i];
1296
1297 // distribute time derivative to global residual
1298 globalResidual[offset_h + stride_h * h_gi] += elementResidual_h[i];
1299 globalResidual[offset_hu + stride_hu * h_gi] += elementResidual_hu[i];
1300 globalResidual[offset_hv + stride_hv * h_gi] += elementResidual_hv[i];
1301 }
1302 }
1303 // ********** END OF CELL LOOPS ********** //
1304
1305 if (SECOND_CALL_CALCULATE_RESIDUAL == 0) // This is to save some time
1306 {
1308 // ********** FIRST SET OF LOOP ON DOFs ********** //
1310 // To compute:
1311 // * Local bounds for limiting
1312 // * Low order solution (in terms of bar states)
1313
1314 // Walls: on rows with wall_dof = 1 the advective (inertial) terms are
1315 // integrated by parts with zero advective flux through the wall,
1316 // R_i = -sum_j F_j . c_ji = sum_j (F_j - F_i) . c'_ij - F_i . n_i,
1317 // with c'_ij = -c_ji and n_i = sum_j c_ji = int_{dOmega} phi_i n.
1318 // F_adv(U).n = (u.n) U, so the boundary term only rescales U_i and the
1319 // low-order update stays a positive combination of U_i and bar states.
1320 // The pressure stays in the non-conservative, well-balanced form on
1321 // c_ij, which carries the wall's pressure reaction itself.
1322 std::valarray<double> wall_nx(0.0, numDOFsPerEqn),
1323 wall_ny(0.0, numDOFsPerEqn);
1324 {
1325 int ij_w = 0;
1326 for (int i = 0; i < numDOFsPerEqn; i++)
1327 for (int offset = csrRowIndeces_DofLoops[i];
1328 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
1329 if (wall_dof[i] > 0.5 || tailwater_dof[i] > 0.5) {
1330 wall_nx[i] += CTx[ij_w];
1331 wall_ny[i] += CTy[ij_w];
1332 }
1333 ij_w++;
1334 }
1335 }
1336
1337 // Here we declare some arrays for local bounds. delta_Sqd_h and
1338 // delta_Sqd_kin (sum over neighbours of the difference) are computed
1339 // by the caller and exchanged with the ghost DOFs: bar_deltaSqd at row
1340 // i reads them at the neighbours, and a ghost row's own sum would miss
1341 // part of its stencil.
1342 xt::pyarray<double> &delta_Sqd_h = args.array<double>("delta_Sqd_h");
1343 xt::pyarray<double> &delta_Sqd_kin = args.array<double>("delta_Sqd_kin");
1344 std::valarray<double> bar_deltaSqd_h(0.0, numDOFsPerEqn),
1345 bar_deltaSqd_kin(0.0, numDOFsPerEqn);
1346 xt::pyarray<double> kin(numDOFsPerEqn), max_of_h_and_hEps(numDOFsPerEqn);
1347
1348 // Define kinetic energy, kin = 1/2 q^2 / h
1349 max_of_h_and_hEps = xt::where(h_dof_old > hEps, h_dof_old, hEps);
1350 kin = 0.5 * (hu_dof_old * hu_dof_old + hv_dof_old * hv_dof_old);
1351 kin = kin * (2.0 * h_dof_old /
1352 (h_dof_old * h_dof_old + max_of_h_and_hEps * max_of_h_and_hEps));
1353
1354 // Stuff for bar states here (BT = BarTilde)
1355 std::valarray<double> hBT(0.0, dH_minus_dL.size()),
1356 huBT(0.0, dH_minus_dL.size()), hvBT(0.0, dH_minus_dL.size());
1357
1358 /* Second loop to compute bar states (variable)BT */
1359 int ij = 0;
1360 for (int i = 0; i < numDOFsPerEqn; i++) {
1361
1362 // define things at ith node
1363 double hi = h_dof_old[i];
1364 double hui = hu_dof_old[i];
1365 double hvi = hv_dof_old[i];
1366 double Zi = b_dof[i];
1367 double mi = lumped_mass_matrix[i];
1368 double one_over_hiReg =
1369 2.0 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2));
1370 double ui = hui * one_over_hiReg;
1371 double vi = hvi * one_over_hiReg;
1372
1373 // define full pressure at ith node for definition of bar states
1374 double pressure_i = 0.5 * g * hi * hi;
1375 double phi_i = porosity_dof[i];
1376
1377 // loop over the sparsity pattern of the i-th DOF
1378 for (int offset = csrRowIndeces_DofLoops[i];
1379 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
1380 int j = csrColumnOffsets_DofLoops[offset];
1381
1382 // define things at jth node
1383 double hj = h_dof_old[j];
1384 double huj = hu_dof_old[j];
1385 double hvj = hv_dof_old[j];
1386 double Zj = b_dof[j];
1387 double one_over_hjReg =
1388 2.0 * hj / (hj * hj + std::pow(fmax(hj, hEps), 2));
1389 double uj = huj * one_over_hjReg;
1390 double vj = hvj * one_over_hjReg;
1391 // advective coefficient for row i: c'_ij = -c_ji on wall rows
1392 double cxa = wall_dof[i] > 0.5 ? -CTx[ij] : Cx[ij];
1393 double cya = wall_dof[i] > 0.5 ? -CTy[ij] : Cy[ij];
1394
1395 // for bar_deltaSqd_h, bar_deltaSqd_heta, bar_deltaSqd_kin
1396 double muLowij = 0., muLij = 0., dLowij = 0., dLij = 0.;
1397
1398 if (i != j) {
1399 // Put these computations first before it gets messy
1400 bar_deltaSqd_h[i] += 0.5 * delta_Sqd_h[j] + 0.5 * delta_Sqd_h[i];
1401 bar_deltaSqd_kin[i] +=
1402 0.5 * delta_Sqd_kin[j] + 0.5 * delta_Sqd_kin[i];
1403
1404 if (lstage == 0)
1405 dLowij = dLow[ij];
1406 else {
1407 double cij_norm = sqrt(Cx[ij] * Cx[ij] + Cy[ij] * Cy[ij]);
1408 double cji_norm = sqrt(CTx[ij] * CTx[ij] + CTy[ij] * CTy[ij]);
1409 double nxij = Cx[ij] / cij_norm, nyij = Cy[ij] / cij_norm;
1410 double nxji = CTx[ij] / cji_norm, nyji = CTy[ij] / cji_norm;
1411 dLowij = fmax(
1412 maxWaveSpeedSharpInitialGuess(g, nxij, nyij, hi, hui, hvi, hj,
1413 huj, hvj, hEps, false) *
1414 cij_norm,
1415 maxWaveSpeedSharpInitialGuess(g, nxji, nyji, hj, huj, hvj, hi,
1416 hui, hvi, hEps, false) *
1417 cji_norm);
1418 dLowij *= phiBar(i, j, porosity_dof);
1419 }
1420 // save dLij
1421 dLij = dLowij;
1422
1423 // compute muij
1424 muLij = fmax(fmax(0., -(ui * cxa + vi * cya)),
1425 fmax(0., (uj * cxa + vj * cya)));
1426 if (wall_dof[i] > 0.5 || wall_dof[j] > 0.5)
1427 muLij = fmax(muLij, muWallTranspose(i, j, ij, ui, vi, uj, vj,
1428 wall_dof, Cx, Cy, CTx, CTy));
1429 muLij *= phiBar(i, j, porosity_dof);
1430
1431 // Define dLij as max of dLij and muLij
1432 dLij = fmax(dLowij, muLij);
1433 dLow[ij] = fmax(dLij, muLij);
1434
1436 // COMPUTE BAR STATES //
1438
1439 // define pressure at jth node for bar states
1440 double pressure_j = 0.5 * g * hj * hj;
1441
1442 // Compute star states
1443 double hStarij = NO_STAR_STATES ? hi : fmax(0., hi + Zi - fmax(Zi, Zj));
1444 double huStarij = hui * hStarij * one_over_hiReg;
1445 double hvStarij = hvi * hStarij * one_over_hiReg;
1446 double hStarji = NO_STAR_STATES ? hj : fmax(0., hj + Zj - fmax(Zi, Zj));
1447 double huStarji = huj * hStarji * one_over_hjReg;
1448 double hvStarji = hvj * hStarji * one_over_hjReg;
1449
1450 double hBar_ij = 0., hTilde_ij = 0., huBar_ij = 0., huTilde_ij = 0.,
1451 hvBar_ij = 0., hvTilde_ij = 0.;
1452
1453 // h component (advective fluxes carry phi_j F_j, pressure phi_i)
1454 double phi_j = porosity_dof[j];
1455 hBar_ij = -1. / (2.0 * fmax(dLij, dij_small)) *
1456 ((phi_j * uj * hj - phi_i * ui * hi) * cxa +
1457 (phi_j * vj * hj - phi_i * vi * hi) * cya) +
1458 0.5 * (hj + hi);
1459 hTilde_ij = (dLij - muLij) / (2.0 * fmax(dLij, dij_small)) *
1460 (hStarji - hj - (hStarij - hi));
1461 // hu component
1462 huBar_ij =
1463 -1. / (2.0 * fmax(dLij, dij_small)) *
1464 ((phi_j * uj * huj - phi_i * ui * hui) * cxa +
1465 (phi_j * vj * huj - phi_i * vi * hui) * cya +
1466 phi_i * (pressure_j - pressure_i) * Cx[ij]) +
1467 0.5 * (huj + hui);
1468 huTilde_ij = (dLij - muLij) / (2.0 * fmax(dLij, dij_small)) *
1469 (huStarji - huj - (huStarij - hui));
1470 // hv component
1471 hvBar_ij =
1472 -1. / (2.0 * fmax(dLij, dij_small)) *
1473 ((phi_j * uj * hvj - phi_i * ui * hvi) * cxa +
1474 (phi_j * vj * hvj - phi_i * vi * hvi) * cya +
1475 phi_i * (pressure_j - pressure_i) * Cy[ij]) +
1476 0.5 * (hvj + hvi);
1477 hvTilde_ij = (dLij - muLij) / (2.0 * fmax(dLij, dij_small)) *
1478 (hvStarji - hvj - (hvStarij - hvi));
1479
1480 // Here we define uBar + uTilde
1481 hBT[ij] = hBar_ij + hTilde_ij;
1482 huBT[ij] = huBar_ij + huTilde_ij;
1483 hvBT[ij] = hvBar_ij + hvTilde_ij;
1484 } else {
1485 // i==j
1486 // Bar states by definition satisfy Utilde_ii + Ubar_ii = U_i
1487 hBT[ij] = hi;
1488 huBT[ij] = hui;
1489 hvBT[ij] = hvi;
1490 }
1491
1492 // UPDATE ij //
1493 ij += 1;
1494 } // j loop ends here
1495
1496 // for bar_deltaSqd_h, bar_deltaSqd_heta, bar_deltaSqd_kin
1497 bar_deltaSqd_h[i] =
1498 bar_deltaSqd_h[i] /
1499 (csrRowIndeces_DofLoops[i + 1] - csrRowIndeces_DofLoops[i] - 1) /
1500 2.0;
1501 bar_deltaSqd_kin[i] =
1502 bar_deltaSqd_kin[i] /
1503 (csrRowIndeces_DofLoops[i + 1] - csrRowIndeces_DofLoops[i] - 1) /
1504 2.0;
1505
1506 } // i loops ends here
1507
1508 /* Then final loop of first set to get local bounds */
1509 ij = 0;
1510 for (int i = 0; i < numDOFsPerEqn; i++) {
1511
1512 // define m_i
1513 double mi = lumped_mass_matrix[i];
1514
1515 /* Initialize hmin, hmax */
1516 h_min[i] = h_dof_old[i];
1517 h_max[i] = h_dof_old[i];
1518
1519 /* Initialize low order solution */
1520 hLow[i] = h_dof_old[i];
1521 huLow[i] = hu_dof_old[i];
1522 hvLow[i] = hv_dof_old[i];
1523 kin_max[i] = kin[i];
1524 if (BOUNDS_EXCLUDE_SELF) {
1525 h_min[i] = 1.0e300;
1526 h_max[i] = -1.0e300;
1527 kin_max[i] = 0.0;
1528 }
1529
1530 // loop in j (sparsity pattern)
1531 for (int offset = csrRowIndeces_DofLoops[i];
1532 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
1533
1534 int j = csrColumnOffsets_DofLoops[offset];
1535
1536 double one_over_hBT =
1537 2.0 * hBT[ij] /
1538 (hBT[ij] * hBT[ij] + std::pow(fmax(hBT[ij], hEps), 2));
1539 double psi_ij = one_over_hBT *
1540 (huBT[ij] * huBT[ij] + hvBT[ij] * hvBT[ij]) /
1541 2.0; // Eqn (6.31)
1542
1543 // COMPUTE LOCAL BOUNDS //
1544 if (!(BOUNDS_EXCLUDE_SELF && j == i)) {
1545 kin_max[i] = fmax(psi_ij, kin_max[i]);
1546 if (!NODAL_H_BOUNDS) {
1547 h_min[i] = std::min(h_min[i], hBT[ij]);
1548 h_max[i] = std::max(h_max[i], hBT[ij]);
1549 }
1550 }
1551 // diagnostic: bounds on h from the nodal values of the stencil
1552 // (local extremum diminishing test), no bar states, no relaxation
1553 if (NODAL_H_BOUNDS) {
1554 h_min[i] = std::min(h_min[i], h_dof_old[j]);
1555 h_max[i] = std::max(h_max[i], h_dof_old[j]);
1556 }
1557
1558 /* COMPUTE LOW ORDER SOLUTION. See EQN 6.23 in SW friction paper */
1559 // This is low order solution WITHOUT sources
1560 if (i != j) {
1561 hLow[i] += h_dof_old[i] * (-dt / mi * 2 * dLow[ij]) +
1562 dt / mi * (2 * dLow[ij] * hBT[ij]);
1563 huLow[i] += hu_dof_old[i] * (-dt / mi * 2 * dLow[ij]) +
1564 dt / mi * (2 * dLow[ij] * huBT[ij]);
1565 hvLow[i] += hv_dof_old[i] * (-dt / mi * 2 * dLow[ij]) +
1566 dt / mi * (2 * dLow[ij] * hvBT[ij]);
1567 }
1568
1569 // UPDATE ij //
1570 ij += 1;
1571 } // j loop ends here
1572
1573 // wall rows: + dt/m_i (u_i . n_i) U_i from the advective boundary term
1574 if (wall_dof[i] > 0.5) {
1575 double one_over_hiReg_w =
1576 2.0 * h_dof_old[i] /
1577 (h_dof_old[i] * h_dof_old[i] +
1578 std::pow(fmax(h_dof_old[i], hEps), 2));
1579 double un_i = (hu_dof_old[i] * wall_nx[i] + hv_dof_old[i] * wall_ny[i]) *
1580 one_over_hiReg_w;
1581 un_i *= porosity_dof[i]; // boundary flux phi_i (u_i.n_i) U_i
1582 hLow[i] += dt / mi * un_i * h_dof_old[i];
1583 huLow[i] += dt / mi * un_i * hu_dof_old[i];
1584 hvLow[i] += dt / mi * un_i * hv_dof_old[i];
1585 if (wall_dof[i] > 1.5) {
1586 double dp_w, rate_w;
1587 wallRiemann(g, h_dof_old[i], hu_dof_old[i], hv_dof_old[i],
1588 wall_nx[i], wall_ny[i], hEps, dp_w, rate_w);
1589 huLow[i] -= dt / mi * porosity_dof[i] * dp_w * wall_nx[i];
1590 hvLow[i] -= dt / mi * porosity_dof[i] * dp_w * wall_ny[i];
1591 }
1592 // the rescaled U_i makes the low-order state a positive, not
1593 // convex, combination; widen the local bounds to contain it
1594 h_min[i] = std::min(h_min[i], hLow[i]);
1595 h_max[i] = std::max(h_max[i], hLow[i]);
1596 const double one_over_hLow =
1597 2.0 * hLow[i] /
1598 (hLow[i] * hLow[i] + std::pow(fmax(hLow[i], hEps), 2));
1599 kin_max[i] = fmax(kin_max[i],
1600 0.5 * one_over_hLow *
1601 (huLow[i] * huLow[i] + hvLow[i] * hvLow[i]));
1602 }
1603
1604 // tailwater rows: Rusanov flux against the ghost state, as a
1605 // correction to the natural outflow carried by c_ij
1606 if (tailwater_dof[i] > 0.5) {
1607 double dFh_t, dFhu_t, dFhv_t, rate_t;
1608 tailwaterFlux(g, h_dof_old[i], hu_dof_old[i], hv_dof_old[i],
1609 tailwater_depth_dof[i], wall_nx[i], wall_ny[i], hEps,
1610 dFh_t, dFhu_t, dFhv_t, rate_t);
1611 hLow[i] -= dt / mi * porosity_dof[i] * dFh_t;
1612 huLow[i] -= dt / mi * porosity_dof[i] * dFhu_t;
1613 hvLow[i] -= dt / mi * porosity_dof[i] * dFhv_t;
1614 hLow[i] = fmax(hLow[i], 0.);
1615 h_min[i] = std::min(h_min[i], hLow[i]);
1616 h_max[i] = std::max(h_max[i], hLow[i]);
1617 const double one_over_hLow_t =
1618 2.0 * hLow[i] /
1619 (hLow[i] * hLow[i] + std::pow(fmax(hLow[i], hEps), 2));
1620 kin_max[i] = fmax(kin_max[i],
1621 0.5 * one_over_hLow_t *
1622 (huLow[i] * huLow[i] + hvLow[i] * hvLow[i]));
1623 }
1624
1625 // Then do relaxation of bounds here. If confused, see convex
1626 // limiting paper
1627 kin_max[i] = std::min(urelax[i] * kin_max[i],
1628 kin_max[i] + std::abs(bar_deltaSqd_kin[i]));
1629 if (!NODAL_H_BOUNDS) {
1630 h_min[i] = std::max(drelax[i] * h_min[i],
1631 h_min[i] - std::abs(bar_deltaSqd_h[i]));
1632 h_max[i] = std::min(urelax[i] * h_max[i],
1633 h_max[i] + std::abs(bar_deltaSqd_h[i]));
1634 }
1635
1636 // clean up hLow from round off error
1637 if (hLow[i] < hEps)
1638 hLow[i] = 0.0;
1639 } // i loop ends here
1640
1642 // ********** Second set of loops on dofs ********** //
1644 // To compute:
1645 // * Hyperbolic part of the flux
1646 // * Extended source terms
1647 // * Smoothness indicator
1648
1649 ij = 0;
1650 std::valarray<double> hyp_flux_h(numDOFsPerEqn),
1651 hyp_flux_hu(numDOFsPerEqn), hyp_flux_hv(numDOFsPerEqn),
1652 psi(numDOFsPerEqn), etaMax(numDOFsPerEqn), etaMin(numDOFsPerEqn);
1653
1654 for (int i = 0; i < numDOFsPerEqn; i++) {
1655 // solution at time tn for the ith DOF
1656 double hi = h_dof_old[i];
1657 double hui = hu_dof_old[i];
1658 double hvi = hv_dof_old[i];
1659 double Zi = b_dof[i];
1660 // Define some things using above
1661 double one_over_hiReg =
1662 2 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2)); // hEps
1663 double ui = hui * one_over_hiReg;
1664 double vi = hvi * one_over_hiReg;
1665 double mi = lumped_mass_matrix[i];
1666
1667 /* COMPUTE EXTENDED SOURCE TERMS for all equations:
1668 * Friction terms
1669 * NOTE: Be careful with sign of source terms. Extended sources are on
1670 * left side of equations. "new_SourceTerm" variables are on right
1671 */
1672
1673 double phi_i = porosity_dof[i];
1674
1675 // FRICTION
1676 if (LINEAR_FRICTION == 1) {
1677 extendedSourceTerm_hu[i] = mannings * hui * mi;
1678 extendedSourceTerm_hv[i] = mannings * hvi * mi;
1679 // For use in the convex limiting function
1680 // actually didn't need to do this but it helps with signs
1681 new_SourceTerm_hu[i] = -mannings * hui * mi;
1682 new_SourceTerm_hv[i] = -mannings * hvi * mi;
1683 } else {
1684 double veli_norm = std::sqrt(ui * ui + vi * vi);
1685 double hi_to_the_gamma = std::pow(fmax(hi, hEps), gamma);
1686 double friction_aux =
1687 veli_norm == 0.
1688 ? 0.
1689 : (2 * g * n2 * veli_norm * mi /
1690 (hi_to_the_gamma +
1691 fmax(hi_to_the_gamma, xi * g * n2 * dt * veli_norm)));
1692 extendedSourceTerm_hu[i] = friction_aux * hui;
1693 extendedSourceTerm_hv[i] = friction_aux * hvi;
1694 // For use in the convex limiting function
1695 new_SourceTerm_hu[i] = -friction_aux * hui;
1696 new_SourceTerm_hv[i] = -friction_aux * hvi;
1697 }
1698
1699 // CANOPY DRAG: -(alpha + beta |u|) h u per unit plan area, i.e. a
1700 // sink K hu per unit storage with K = (alpha + beta |u|) / phi. Same
1701 // semi-implicit form as the Manning term (2 K m / (1 + max(1, xi dt
1702 // K))), so dt * drag_aux / m_i <= 2 / xi and hu never changes sign.
1703 {
1704 double veli_norm = std::sqrt(ui * ui + vi * vi);
1705 double K = (drag_alpha_dof[i] + drag_beta_dof[i] * veli_norm) / phi_i;
1706 if (K > 0.) {
1707 double drag_aux = 2. * K * mi / (1. + fmax(1., xi * dt * K));
1708 extendedSourceTerm_hu[i] += drag_aux * hui;
1709 extendedSourceTerm_hv[i] += drag_aux * hvi;
1710 new_SourceTerm_hu[i] -= drag_aux * hui;
1711 new_SourceTerm_hv[i] -= drag_aux * hvi;
1712 }
1713 }
1714
1715 /* HYPERBOLIC FLUXES */
1716 hyp_flux_h[i] = 0;
1717 hyp_flux_hu[i] = 0;
1718 hyp_flux_hv[i] = 0;
1719
1720 // FOR SMOOTHNESS INDICATOR //
1721 double alphai;
1722 double alpha_numerator = 0;
1723 double alpha_denominator = 0;
1724 double alpha_zero = 0.5; // if only want smoothness
1725 double alpha_factor = 1.0 / (1.0 - alpha_zero);
1726
1727 // loop in j (sparsity pattern)
1728 for (int offset = csrRowIndeces_DofLoops[i];
1729 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
1730
1731 int j = csrColumnOffsets_DofLoops[offset];
1732
1733 // solution at time tn for the jth DOF
1734 double hj = h_dof_old[j];
1735 double huj = hu_dof_old[j];
1736 double hvj = hv_dof_old[j];
1737 double Zj = b_dof[j];
1738 // Then define some things here using above
1739 double one_over_hjReg =
1740 2.0 * hj / (hj * hj + std::pow(fmax(hj, hEps), 2));
1741 double uj = huj * one_over_hjReg;
1742 double vj = hvj * one_over_hjReg;
1743
1744 // auxiliary functions to compute fluxes (advective part on c')
1745 double cxa = wall_dof[i] > 0.5 ? -CTx[ij] : Cx[ij];
1746 double cya = wall_dof[i] > 0.5 ? -CTy[ij] : Cy[ij];
1747 double phi_j = porosity_dof[j];
1748 double aux_h = (phi_j * uj * hj - phi_i * ui * hi) * cxa +
1749 (phi_j * vj * hj - phi_i * vi * hi) * cya;
1750 double aux_hu = (phi_j * uj * huj - phi_i * ui * hui) * cxa +
1751 (phi_j * vj * huj - phi_i * vi * hui) * cya;
1752 double aux_hv = (phi_j * uj * hvj - phi_i * ui * hvi) * cxa +
1753 (phi_j * vj * hvj - phi_i * vi * hvi) * cya;
1754
1755 /* HYPERBOLIC FLUX */
1756 hyp_flux_h[i] += aux_h;
1757 hyp_flux_hu[i] += aux_hu;
1758 hyp_flux_hv[i] += aux_hv;
1759
1760 // EXTENDED SOURCE, USING 6.13 //
1761 // (porous: phi_i g h_i grad(h + Z), non-conservative at node i)
1762 extendedSourceTerm_hu[i] += phi_i * g * hi * (hj + Zj) * Cx[ij];
1763 extendedSourceTerm_hv[i] += phi_i * g * hi * (hj + Zj) * Cy[ij];
1764
1765 new_SourceTerm_hu[i] += phi_i * g *
1766 (-hi * (Zj - Zi) + 0.5 * std::pow(hj - hi, 2)) *
1767 Cx[ij];
1768 new_SourceTerm_hv[i] += phi_i * g *
1769 (-hi * (Zj - Zi) + 0.5 * std::pow(hj - hi, 2)) *
1770 Cy[ij];
1771
1772 // FOR SMOOTHNESS INDICATOR //
1773 alpha_numerator += hj - hi;
1774 alpha_denominator += fabs(hj - hi);
1775
1776 // update ij
1777 ij += 1;
1778 } // end j loop
1779
1780 // wall rows: advective boundary term -F_i . n_i = -(u_i . n_i) U_i
1781 if (wall_dof[i] > 0.5) {
1782 double un_i = phi_i * (ui * wall_nx[i] + vi * wall_ny[i]);
1783 hyp_flux_h[i] -= un_i * hi;
1784 hyp_flux_hu[i] -= un_i * hui;
1785 hyp_flux_hv[i] -= un_i * hvi;
1786 if (wall_dof[i] > 1.5) {
1787 double dp_w, rate_w;
1788 wallRiemann(g, hi, hui, hvi, wall_nx[i], wall_ny[i], hEps, dp_w,
1789 rate_w);
1790 hyp_flux_hu[i] += phi_i * dp_w * wall_nx[i];
1791 hyp_flux_hv[i] += phi_i * dp_w * wall_ny[i];
1792 }
1793 }
1794
1795 // tailwater rows: same boundary-flux correction in the high-order update
1796 if (tailwater_dof[i] > 0.5) {
1797 double dFh_t, dFhu_t, dFhv_t, rate_t;
1798 tailwaterFlux(g, hi, hui, hvi, tailwater_depth_dof[i], wall_nx[i],
1799 wall_ny[i], hEps, dFh_t, dFhu_t, dFhv_t, rate_t);
1800 hyp_flux_h[i] += phi_i * dFh_t;
1801 hyp_flux_hu[i] += phi_i * dFhu_t;
1802 hyp_flux_hv[i] += phi_i * dFhv_t;
1803 }
1804
1805 // COMPUTE SMOOTHNESS INDICATOR //
1806 if (hi <= hEps) {
1807 alphai = 1.0;
1808 psi[i] = 1.0;
1809 } else {
1810 // Force alphai=0 in constant states
1811 if (fabs(alpha_numerator) <= hEps) {
1812 alphai = 0.;
1813 } else {
1814 alphai =
1815 (fabs(alpha_numerator) - hEps) / fabs(alpha_denominator - hEps);
1816 }
1817 alphai = fmax(alphai - alpha_zero, 0.0) * alpha_factor;
1819 psi[i] = 1.0;
1820 else
1821 psi[i] = std::pow(alphai, POWER_SMOOTHNESS_INDICATOR);
1822 }
1823 }
1824 // ********** END OF 2nd LOOP ON DOFS ********** //
1825
1827 // ********** MAIN LOOP ON DOFs **********
1828 // To compute:
1829 // * dissipative terms
1830 // * bar states
1832
1833 ij = 0;
1834 for (int i = 0; i < numDOFsPerEqn; i++) {
1835 double hi = h_dof_old[i];
1836 double hui = hu_dof_old[i];
1837 double hvi = hv_dof_old[i];
1838 double Zi = b_dof[i];
1839 double mi = lumped_mass_matrix[i];
1840
1841 double one_over_hiReg =
1842 2.0 * hi / (hi * hi + std::pow(fmax(hi, hEps), 2));
1843 double ui = hui * one_over_hiReg;
1844 double vi = hvi * one_over_hiReg;
1845
1846 // Define full pressure at ith node for definition of bar states below
1847 double pressure_i = 0.5 * g * hi * hi;
1848
1849 // HIGH ORDER DISSIPATIVE TERMS, for Aij matrix
1850 double ith_dHij_minus_muHij_times_hStarStates = 0.,
1851 ith_dHij_minus_muHij_times_huStarStates = 0.,
1852 ith_dHij_minus_muHij_times_hvStarStates = 0.,
1853 ith_muHij_times_hStates = 0., ith_muHij_times_huStates = 0.,
1854 ith_muHij_times_hvStates = 0.;
1855
1856 // loop over the sparsity pattern of the i-th DOF
1857 for (int offset = csrRowIndeces_DofLoops[i];
1858 offset < csrRowIndeces_DofLoops[i + 1]; offset++) {
1859 int j = csrColumnOffsets_DofLoops[offset];
1860 double hj = h_dof_old[j];
1861 double huj = hu_dof_old[j];
1862 double hvj = hv_dof_old[j];
1863 double Zj = b_dof[j];
1864 double one_over_hjReg =
1865 2.0 * hj / (hj * hj + std::pow(fmax(hj, hEps), 2));
1866 double uj = huj * one_over_hjReg;
1867 double vj = hvj * one_over_hjReg;
1868
1869 // define pressure at jth node
1870 double pressure_j = 0.5 * g * hj * hj;
1871
1872 // COMPUTE STAR STATES
1873 double hStarij = NO_STAR_STATES ? hi : fmax(0., hi + Zi - fmax(Zi, Zj));
1874 double huStarij = hui * hStarij * one_over_hiReg;
1875 double hvStarij = hvi * hStarij * one_over_hiReg;
1876
1877 double hStarji = NO_STAR_STATES ? hj : fmax(0., hj + Zj - fmax(Zi, Zj));
1878 double huStarji = huj * hStarji * one_over_hjReg;
1879 double hvStarji = hvj * hStarji * one_over_hjReg;
1880
1881 // Dissipative well balancing term
1882 double muLowij = 0., muLij = 0., muHij = 0.;
1883 double dLowij = 0., dLij = 0., dHij = 0.;
1884 if (i != j) // This is not necessary. See formula for
1885 // ith_dissipative_terms
1886 {
1888 // DISSIPATIVE MATRIX //
1890 if (lstage == 0)
1891 dLowij = dLow[ij];
1892 else {
1893 double cij_norm = sqrt(Cx[ij] * Cx[ij] + Cy[ij] * Cy[ij]);
1894 double cji_norm = sqrt(CTx[ij] * CTx[ij] + CTy[ij] * CTy[ij]);
1895 double nxij = Cx[ij] / cij_norm, nyij = Cy[ij] / cij_norm;
1896 double nxji = CTx[ij] / cji_norm, nyji = CTy[ij] / cji_norm;
1897 dLowij = fmax(
1898 maxWaveSpeedSharpInitialGuess(g, nxij, nyij, hi, hui, hvi, hj,
1899 huj, hvj, hEps, false) *
1900 cij_norm,
1901 maxWaveSpeedSharpInitialGuess(g, nxji, nyji, hj, huj, hvj, hi,
1902 hui, hvi, hEps, false) *
1903 cji_norm);
1904 dLowij *= phiBar(i, j, porosity_dof);
1905 }
1906 // this is standard low-order dij, can you use dLij =
1907 // dLowij*fmax(psi[i],psi[j]) if want smoothness based as low order
1908 dLij = dLowij;
1909
1911 // WELL BALANCING DISSIPATIVE MATRIX //
1913 // advective coefficient for row i: c'_ij = -c_ji on wall rows
1914 double cxa = wall_dof[i] > 0.5 ? -CTx[ij] : Cx[ij];
1915 double cya = wall_dof[i] > 0.5 ? -CTy[ij] : Cy[ij];
1916 muLowij = fmax(fmax(0., -(ui * cxa + vi * cya)),
1917 fmax(0., (uj * cxa + vj * cya)));
1918 if (wall_dof[i] > 0.5 || wall_dof[j] > 0.5)
1919 muLowij = fmax(muLowij,
1920 muWallTranspose(i, j, ij, ui, vi, uj, vj, wall_dof,
1921 Cx, Cy, CTx, CTy));
1922 muLowij *= phiBar(i, j, porosity_dof);
1923 muLij = muLowij;
1924
1925 // Define dLij as low order dijs
1926 muLij = muLowij;
1927 dLij = fmax(dLowij, muLij);
1928
1929 // Then save dLow for limiting step, maybe a bit confusing
1930 dLow[ij] = fmax(dLij, muLij);
1931
1933 // ENTROPY VISCOSITY //
1935 double dEVij = cE * fmax(global_entropy_residual[i],
1936 global_entropy_residual[j]) *
1937 phiBar(i, j, porosity_dof);
1938 dHij = fmin(dLowij, dEVij);
1939 muHij = fmin(muLowij, dEVij);
1940
1941 // compute dij_minus_muij times star solution terms
1942 // see: eqn (6.13)
1943 ith_dHij_minus_muHij_times_hStarStates +=
1944 (dHij - muHij) * (hStarji - hStarij);
1945 ith_dHij_minus_muHij_times_huStarStates +=
1946 (dHij - muHij) * (huStarji - huStarij);
1947 ith_dHij_minus_muHij_times_hvStarStates +=
1948 (dHij - muHij) * (hvStarji - hvStarij);
1949
1950 // compute muij times solution terms
1951 ith_muHij_times_hStates += muHij * (hj - hi);
1952 ith_muHij_times_huStates += muHij * (huj - hui);
1953 ith_muHij_times_hvStates += muHij * (hvj - hvi);
1954
1955 // compute dH_minus_dL
1956 dH_minus_dL[ij] = dHij - dLij;
1957 muH_minus_muL[ij] = muHij - muLij;
1958 } else // i==j
1959 {
1960 dH_minus_dL[ij] =
1961 0.; // Not true but the prod of this times Uj-Ui will be zero
1962 muH_minus_muL[ij] =
1963 0.; // Not true but the prod of this times Uj-Ui will be zero
1964 }
1965 // update ij
1966 ij += 1;
1967 } // j loop ends here
1968
1969 /* Define global residual */
1970 if (LUMPED_MASS_MATRIX == 1) {
1971 globalResidual[offset_h + stride_h * i] =
1972 hi - dt / mi *
1973 (hyp_flux_h[i] - ith_dHij_minus_muHij_times_hStarStates -
1974 ith_muHij_times_hStates);
1975 globalResidual[offset_hu + stride_hu * i] =
1976 hui - dt / mi *
1977 ((hyp_flux_hu[i] + extendedSourceTerm_hu[i]) -
1978 ith_dHij_minus_muHij_times_huStarStates -
1979 ith_muHij_times_huStates);
1980 globalResidual[offset_hv + stride_hv * i] =
1981 hvi - dt / mi *
1982 ((hyp_flux_hv[i] + extendedSourceTerm_hv[i]) -
1983 ith_dHij_minus_muHij_times_hvStarStates -
1984 ith_muHij_times_hvStates);
1985 // clean up potential negative water height due to machine precision
1986 if (globalResidual[offset_h + stride_h * i] >= -hEps &&
1987 globalResidual[offset_h + stride_h * i] < hEps)
1988 globalResidual[offset_h + stride_h * i] = 0;
1989 } else {
1990 // Distribute residual
1991 // NOTE: MASS MATRIX IS CONSISTENT
1992 globalResidual[offset_h + stride_h * i] +=
1993 dt * (hyp_flux_h[i] - ith_dHij_minus_muHij_times_hStarStates -
1994 ith_muHij_times_hStates);
1995 globalResidual[offset_hu + stride_hu * i] +=
1996 dt * (hyp_flux_hu[i] + extendedSourceTerm_hu[i] -
1997 ith_dHij_minus_muHij_times_huStarStates -
1998 ith_muHij_times_huStates);
1999 globalResidual[offset_hv + stride_hv * i] +=
2000 dt * (hyp_flux_hv[i] + extendedSourceTerm_hv[i] -
2001 ith_dHij_minus_muHij_times_hvStarStates -
2002 ith_muHij_times_hvStates);
2003 }
2004 }
2005 // ********** END OF LOOP IN DOFs ********** //
2006 } // end SECOND_CALL_CALCULATE_RESIDUAL
2007
2008 // ********** COMPUTE NORMALS ********** //
2009 if (COMPUTE_NORMALS == 1) {
2010 // This is to identify the normals and create a vector of normal
2011 // components
2012 for (int ebNE = 0; ebNE < nExteriorElementBoundaries_global; ebNE++) {
2013 int
2014 ebN = exteriorElementBoundariesArray[ebNE],
2015 eN = elementBoundaryElementsArray[ebN * 2 + 0],
2016 ebN_local = elementBoundaryLocalElementBoundariesArray[ebN * 2 + 0];
2017 double normal[3];
2018 { // "Loop" in quad points
2019 int kb = 0; // NOTE: I need to consider just one quad point since
2020 // the element is not curved so the normal is constant
2021 // per element
2022 int ebN_local_kb =
2023 ebN_local * nQuadraturePoints_elementBoundary + kb;
2024 double jac_ext[nSpace * nSpace], jacDet_ext,
2025 jacInv_ext[nSpace * nSpace], boundaryJac[nSpace * (nSpace - 1)],
2026 metricTensor[(nSpace - 1) * (nSpace - 1)], metricTensorDetSqrt,
2027 x_ext, y_ext;
2028 /* compute information about mapping from reference element to
2029 * physical element */
2030 ck.calculateMapping_elementBoundary(
2031 eN, ebN_local, kb, ebN_local_kb, mesh_dof.data(), mesh_l2g.data(),
2032 mesh_trial_trace_ref.data(), mesh_grad_trial_trace_ref.data(),
2033 boundaryJac_ref.data(), jac_ext, jacDet_ext, jacInv_ext,
2034 boundaryJac, metricTensor, metricTensorDetSqrt, normal_ref.data(),
2035 normal, x_ext, y_ext);
2036 }
2037 // distribute the normal vectors
2038 for (int i = 0; i < nDOF_test_element; i++) {
2039 int eN_i = eN * nDOF_test_element + i;
2040 int gi = h_l2g[eN_i];
2041 normalx[gi] += 0.5 * normal[0] * (i == ebN_local ? 0. : 1.);
2042 normaly[gi] += 0.5 * normal[1] * (i == ebN_local ? 0. : 1.);
2043 }
2044 }
2045 // normalize
2046 for (int gi = 0; gi < numDOFsPerEqn; gi++) {
2047 double norm_factor =
2048 sqrt(std::pow(normalx[gi], 2) + std::pow(normaly[gi], 2));
2049 if (norm_factor != 0) {
2050 normalx[gi] /= norm_factor;
2051 normaly[gi] /= norm_factor;
2052 }
2053 }
2054 }
2055 // ********** END OF COMPUTING NORMALS ********** //
2056 } // end calculateResidual
2057
2059 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
2060 xt::pyarray<double> &mesh_grad_trial_ref =
2061 args.array<double>("mesh_grad_trial_ref");
2062 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
2063 xt::pyarray<double> &mesh_velocity_dof =
2064 args.array<double>("mesh_velocity_dof");
2065 double MOVING_DOMAIN = args.scalar<double>("MOVING_DOMAIN");
2066 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
2067 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
2068 xt::pyarray<double> &h_trial_ref = args.array<double>("h_trial_ref");
2069 xt::pyarray<double> &h_grad_trial_ref =
2070 args.array<double>("h_grad_trial_ref");
2071 xt::pyarray<double> &h_test_ref = args.array<double>("h_test_ref");
2072 xt::pyarray<double> &h_grad_test_ref =
2073 args.array<double>("h_grad_test_ref");
2074 xt::pyarray<double> &vel_trial_ref = args.array<double>("vel_trial_ref");
2075 xt::pyarray<double> &vel_grad_trial_ref =
2076 args.array<double>("vel_grad_trial_ref");
2077 xt::pyarray<double> &vel_test_ref = args.array<double>("vel_test_ref");
2078 xt::pyarray<double> &vel_grad_test_ref =
2079 args.array<double>("vel_grad_test_ref");
2080 xt::pyarray<double> &mesh_trial_trace_ref =
2081 args.array<double>("mesh_trial_trace_ref");
2082 xt::pyarray<double> &mesh_grad_trial_trace_ref =
2083 args.array<double>("mesh_grad_trial_trace_ref");
2084 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
2085 xt::pyarray<double> &h_trial_trace_ref =
2086 args.array<double>("h_trial_trace_ref");
2087 xt::pyarray<double> &h_grad_trial_trace_ref =
2088 args.array<double>("h_grad_trial_trace_ref");
2089 xt::pyarray<double> &h_test_trace_ref =
2090 args.array<double>("h_test_trace_ref");
2091 xt::pyarray<double> &h_grad_test_trace_ref =
2092 args.array<double>("h_grad_test_trace_ref");
2093 xt::pyarray<double> &vel_trial_trace_ref =
2094 args.array<double>("vel_trial_trace_ref");
2095 xt::pyarray<double> &vel_grad_trial_trace_ref =
2096 args.array<double>("vel_grad_trial_trace_ref");
2097 xt::pyarray<double> &vel_test_trace_ref =
2098 args.array<double>("vel_test_trace_ref");
2099 xt::pyarray<double> &vel_grad_test_trace_ref =
2100 args.array<double>("vel_grad_test_trace_ref");
2101 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
2102 xt::pyarray<double> &boundaryJac_ref =
2103 args.array<double>("boundaryJac_ref");
2104 xt::pyarray<double> &elementDiameter =
2105 args.array<double>("elementDiameter");
2106 int nElements_global = args.scalar<int>("nElements_global");
2107 double g = args.scalar<double>("g");
2108 xt::pyarray<int> &h_l2g = args.array<int>("h_l2g");
2109 xt::pyarray<int> &vel_l2g = args.array<int>("vel_l2g");
2110 xt::pyarray<double> &b_dof = args.array<double>("b_dof");
2111 xt::pyarray<double> &h_dof = args.array<double>("h_dof");
2112 xt::pyarray<double> &hu_dof = args.array<double>("hu_dof");
2113 xt::pyarray<double> &hv_dof = args.array<double>("hv_dof");
2114 xt::pyarray<double> &q_cfl = args.array<double>("q_cfl");
2115 xt::pyarray<int> &sdInfo_hu_hu_rowptr =
2116 args.array<int>("sdInfo_hu_hu_rowptr");
2117 xt::pyarray<int> &sdInfo_hu_hu_colind =
2118 args.array<int>("sdInfo_hu_hu_colind");
2119 xt::pyarray<int> &sdInfo_hu_hv_rowptr =
2120 args.array<int>("sdInfo_hu_hv_rowptr");
2121 xt::pyarray<int> &sdInfo_hu_hv_colind =
2122 args.array<int>("sdInfo_hu_hv_colind");
2123 xt::pyarray<int> &sdInfo_hv_hv_rowptr =
2124 args.array<int>("sdInfo_hv_hv_rowptr");
2125 xt::pyarray<int> &sdInfo_hv_hv_colind =
2126 args.array<int>("sdInfo_hv_hv_colind");
2127 xt::pyarray<int> &sdInfo_hv_hu_rowptr =
2128 args.array<int>("sdInfo_hv_hu_rowptr");
2129 xt::pyarray<int> &sdInfo_hv_hu_colind =
2130 args.array<int>("sdInfo_hv_hu_colind");
2131 xt::pyarray<int> &csrRowIndeces_h_h = args.array<int>("csrRowIndeces_h_h");
2132 xt::pyarray<int> &csrColumnOffsets_h_h =
2133 args.array<int>("csrColumnOffsets_h_h");
2134 xt::pyarray<int> &csrRowIndeces_h_hu =
2135 args.array<int>("csrRowIndeces_h_hu");
2136 xt::pyarray<int> &csrColumnOffsets_h_hu =
2137 args.array<int>("csrColumnOffsets_h_hu");
2138 xt::pyarray<int> &csrRowIndeces_h_hv =
2139 args.array<int>("csrRowIndeces_h_hv");
2140 xt::pyarray<int> &csrColumnOffsets_h_hv =
2141 args.array<int>("csrColumnOffsets_h_hv");
2142 xt::pyarray<int> &csrRowIndeces_hu_h =
2143 args.array<int>("csrRowIndeces_hu_h");
2144 xt::pyarray<int> &csrColumnOffsets_hu_h =
2145 args.array<int>("csrColumnOffsets_hu_h");
2146 xt::pyarray<int> &csrRowIndeces_hu_hu =
2147 args.array<int>("csrRowIndeces_hu_hu");
2148 xt::pyarray<int> &csrColumnOffsets_hu_hu =
2149 args.array<int>("csrColumnOffsets_hu_hu");
2150 xt::pyarray<int> &csrRowIndeces_hu_hv =
2151 args.array<int>("csrRowIndeces_hu_hv");
2152 xt::pyarray<int> &csrColumnOffsets_hu_hv =
2153 args.array<int>("csrColumnOffsets_hu_hv");
2154 xt::pyarray<int> &csrRowIndeces_hv_h =
2155 args.array<int>("csrRowIndeces_hv_h");
2156 xt::pyarray<int> &csrColumnOffsets_hv_h =
2157 args.array<int>("csrColumnOffsets_hv_h");
2158 xt::pyarray<int> &csrRowIndeces_hv_hu =
2159 args.array<int>("csrRowIndeces_hv_hu");
2160 xt::pyarray<int> &csrColumnOffsets_hv_hu =
2161 args.array<int>("csrColumnOffsets_hv_hu");
2162 xt::pyarray<int> &csrRowIndeces_hv_hv =
2163 args.array<int>("csrRowIndeces_hv_hv");
2164 xt::pyarray<int> &csrColumnOffsets_hv_hv =
2165 args.array<int>("csrColumnOffsets_hv_hv");
2166 xt::pyarray<double> &globalJacobian = args.array<double>("globalJacobian");
2167 xt::pyarray<double> &porosity_dof = args.array<double>("porosity_dof");
2168 int nExteriorElementBoundaries_global =
2169 args.scalar<int>("nExteriorElementBoundaries_global");
2170 xt::pyarray<int> &exteriorElementBoundariesArray =
2171 args.array<int>("exteriorElementBoundariesArray");
2172 xt::pyarray<int> &elementBoundaryElementsArray =
2173 args.array<int>("elementBoundaryElementsArray");
2174 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray =
2175 args.array<int>("elementBoundaryLocalElementBoundariesArray");
2176 xt::pyarray<int> &isDOFBoundary_h = args.array<int>("isDOFBoundary_h");
2177 xt::pyarray<int> &isDOFBoundary_hu = args.array<int>("isDOFBoundary_hu");
2178 xt::pyarray<int> &isDOFBoundary_hv = args.array<int>("isDOFBoundary_hv");
2179 xt::pyarray<int> &isAdvectiveFluxBoundary_h =
2180 args.array<int>("isAdvectiveFluxBoundary_h");
2181 xt::pyarray<int> &isAdvectiveFluxBoundary_hu =
2182 args.array<int>("isAdvectiveFluxBoundary_hu");
2183 xt::pyarray<int> &isAdvectiveFluxBoundary_hv =
2184 args.array<int>("isAdvectiveFluxBoundary_hv");
2185 xt::pyarray<int> &isDiffusiveFluxBoundary_hu =
2186 args.array<int>("isDiffusiveFluxBoundary_hu");
2187 xt::pyarray<int> &isDiffusiveFluxBoundary_hv =
2188 args.array<int>("isDiffusiveFluxBoundary_hv");
2189 xt::pyarray<double> &ebqe_bc_h_ext = args.array<double>("ebqe_bc_h_ext");
2190 xt::pyarray<double> &ebqe_bc_flux_mass_ext =
2191 args.array<double>("ebqe_bc_flux_mass_ext");
2192 xt::pyarray<double> &ebqe_bc_flux_mom_hu_adv_ext =
2193 args.array<double>("ebqe_bc_flux_mom_hu_adv_ext");
2194 xt::pyarray<double> &ebqe_bc_flux_mom_hv_adv_ext =
2195 args.array<double>("ebqe_bc_flux_mom_hv_adv_ext");
2196 xt::pyarray<double> &ebqe_bc_hu_ext = args.array<double>("ebqe_bc_hu_ext");
2197 xt::pyarray<double> &ebqe_bc_flux_hu_diff_ext =
2198 args.array<double>("ebqe_bc_flux_hu_diff_ext");
2199 xt::pyarray<double> &ebqe_penalty_ext =
2200 args.array<double>("ebqe_penalty_ext");
2201 xt::pyarray<double> &ebqe_bc_hv_ext = args.array<double>("ebqe_bc_hv_ext");
2202 xt::pyarray<double> &ebqe_bc_flux_hv_diff_ext =
2203 args.array<double>("ebqe_bc_flux_hv_diff_ext");
2204 xt::pyarray<int> &csrColumnOffsets_eb_h_h =
2205 args.array<int>("csrColumnOffsets_eb_h_h");
2206 xt::pyarray<int> &csrColumnOffsets_eb_h_hu =
2207 args.array<int>("csrColumnOffsets_eb_h_hu");
2208 xt::pyarray<int> &csrColumnOffsets_eb_h_hv =
2209 args.array<int>("csrColumnOffsets_eb_h_hv");
2210 xt::pyarray<int> &csrColumnOffsets_eb_hu_h =
2211 args.array<int>("csrColumnOffsets_eb_hu_h");
2212 xt::pyarray<int> &csrColumnOffsets_eb_hu_hu =
2213 args.array<int>("csrColumnOffsets_eb_hu_hu");
2214 xt::pyarray<int> &csrColumnOffsets_eb_hu_hv =
2215 args.array<int>("csrColumnOffsets_eb_hu_hv");
2216 xt::pyarray<int> &csrColumnOffsets_eb_hv_h =
2217 args.array<int>("csrColumnOffsets_eb_hv_h");
2218 xt::pyarray<int> &csrColumnOffsets_eb_hv_hu =
2219 args.array<int>("csrColumnOffsets_eb_hv_hu");
2220 xt::pyarray<int> &csrColumnOffsets_eb_hv_hv =
2221 args.array<int>("csrColumnOffsets_eb_hv_hv");
2222 double dt = args.scalar<double>("dt");
2223 //
2224 // loop over elements to compute volume integrals and load them into the
2225 // element Jacobians and global Jacobian
2226 //
2227 for (int eN = 0; eN < nElements_global; eN++) {
2228 double elementJacobian_h_h[nDOF_test_element]
2229 [nDOF_trial_element],
2230 elementJacobian_hu_hu[nDOF_test_element][nDOF_trial_element],
2231 elementJacobian_hv_hv[nDOF_test_element][nDOF_trial_element];
2232 for (int i = 0; i < nDOF_test_element; i++)
2233 for (int j = 0; j < nDOF_trial_element; j++) {
2234 elementJacobian_h_h[i][j] = 0.0;
2235 elementJacobian_hu_hu[i][j] = 0.0;
2236 elementJacobian_hv_hv[i][j] = 0.0;
2237 }
2238 for (int k = 0; k < nQuadraturePoints_element; k++) {
2239 int eN_k = eN * nQuadraturePoints_element +
2240 k, // index to a scalar at a quadrature point
2241 eN_k_nSpace = eN_k * nSpace,
2242 eN_nDOF_trial_element =
2243 eN *
2244 nDOF_trial_element; // index to a vector at a quadrature point
2245
2246 // declare local storage
2247 double jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace],
2248 dV, h_test_dV[nDOF_test_element], vel_test_dV[nDOF_test_element], x,
2249 y, xt, yt;
2250 // get jacobian, etc for mapping reference element
2251 ck.calculateMapping_element(
2252 eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(),
2253 mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y);
2254 // get the physical integration weight, times the porosity: storage
2255 // is phi h and phi hu, so the mass matrix is int phi_h w_i w_j
2256 dV = fabs(jacDet) * dV_ref[k];
2257 {
2258 double phi_q = 0.;
2259 ck.valFromDOF(porosity_dof.data(),
2260 &h_l2g.data()[eN * nDOF_trial_element],
2261 &h_trial_ref.data()[k * nDOF_trial_element], phi_q);
2262 dV *= phi_q;
2263 }
2264 // precalculate test function products with integration weights
2265 for (int j = 0; j < nDOF_trial_element; j++) {
2266 h_test_dV[j] = h_test_ref[k * nDOF_trial_element + j] * dV;
2267 vel_test_dV[j] = vel_test_ref[k * nDOF_trial_element + j] * dV;
2268 }
2269 for (int i = 0; i < nDOF_test_element; i++) {
2270 int i_nSpace = i * nSpace;
2271 for (int j = 0; j < nDOF_trial_element; j++) {
2272 int j_nSpace = j * nSpace;
2273 elementJacobian_h_h[i][j] +=
2274 h_trial_ref[k * nDOF_trial_element + j] * h_test_dV[i];
2275 elementJacobian_hu_hu[i][j] +=
2276 vel_trial_ref[k * nDOF_trial_element + j] * vel_test_dV[i];
2277 elementJacobian_hv_hv[i][j] +=
2278 vel_trial_ref[k * nDOF_trial_element + j] * vel_test_dV[i];
2279 } // j
2280 } // i
2281 } // k
2282 //
2283 // load into element Jacobian into global Jacobian
2284 //
2285 for (int i = 0; i < nDOF_test_element; i++) {
2286 int eN_i = eN * nDOF_test_element + i;
2287 for (int j = 0; j < nDOF_trial_element; j++) {
2288 int eN_i_j = eN_i * nDOF_trial_element + j;
2289 globalJacobian[csrRowIndeces_h_h[eN_i] +
2290 csrColumnOffsets_h_h[eN_i_j]] +=
2291 elementJacobian_h_h[i][j];
2292 globalJacobian[csrRowIndeces_hu_hu[eN_i] +
2293 csrColumnOffsets_hu_hu[eN_i_j]] +=
2294 elementJacobian_hu_hu[i][j];
2295 globalJacobian[csrRowIndeces_hv_hv[eN_i] +
2296 csrColumnOffsets_hv_hv[eN_i_j]] +=
2297 elementJacobian_hv_hv[i][j];
2298 } // j
2299 } // i
2300 } // elements
2301 }
2302
2304 xt::pyarray<double> &mesh_trial_ref = args.array<double>("mesh_trial_ref");
2305 xt::pyarray<double> &mesh_grad_trial_ref =
2306 args.array<double>("mesh_grad_trial_ref");
2307 xt::pyarray<double> &mesh_dof = args.array<double>("mesh_dof");
2308 xt::pyarray<double> &mesh_velocity_dof =
2309 args.array<double>("mesh_velocity_dof");
2310 double MOVING_DOMAIN = args.scalar<double>("MOVING_DOMAIN");
2311 xt::pyarray<int> &mesh_l2g = args.array<int>("mesh_l2g");
2312 xt::pyarray<double> &dV_ref = args.array<double>("dV_ref");
2313 xt::pyarray<double> &h_trial_ref = args.array<double>("h_trial_ref");
2314 xt::pyarray<double> &h_grad_trial_ref =
2315 args.array<double>("h_grad_trial_ref");
2316 xt::pyarray<double> &h_test_ref = args.array<double>("h_test_ref");
2317 xt::pyarray<double> &h_grad_test_ref =
2318 args.array<double>("h_grad_test_ref");
2319 xt::pyarray<double> &vel_trial_ref = args.array<double>("vel_trial_ref");
2320 xt::pyarray<double> &vel_grad_trial_ref =
2321 args.array<double>("vel_grad_trial_ref");
2322 xt::pyarray<double> &vel_test_ref = args.array<double>("vel_test_ref");
2323 xt::pyarray<double> &vel_grad_test_ref =
2324 args.array<double>("vel_grad_test_ref");
2325 xt::pyarray<double> &mesh_trial_trace_ref =
2326 args.array<double>("mesh_trial_trace_ref");
2327 xt::pyarray<double> &mesh_grad_trial_trace_ref =
2328 args.array<double>("mesh_grad_trial_trace_ref");
2329 xt::pyarray<double> &dS_ref = args.array<double>("dS_ref");
2330 xt::pyarray<double> &h_trial_trace_ref =
2331 args.array<double>("h_trial_trace_ref");
2332 xt::pyarray<double> &h_grad_trial_trace_ref =
2333 args.array<double>("h_grad_trial_trace_ref");
2334 xt::pyarray<double> &h_test_trace_ref =
2335 args.array<double>("h_test_trace_ref");
2336 xt::pyarray<double> &h_grad_test_trace_ref =
2337 args.array<double>("h_grad_test_trace_ref");
2338 xt::pyarray<double> &vel_trial_trace_ref =
2339 args.array<double>("vel_trial_trace_ref");
2340 xt::pyarray<double> &vel_grad_trial_trace_ref =
2341 args.array<double>("vel_grad_trial_trace_ref");
2342 xt::pyarray<double> &vel_test_trace_ref =
2343 args.array<double>("vel_test_trace_ref");
2344 xt::pyarray<double> &vel_grad_test_trace_ref =
2345 args.array<double>("vel_grad_test_trace_ref");
2346 xt::pyarray<double> &normal_ref = args.array<double>("normal_ref");
2347 xt::pyarray<double> &boundaryJac_ref =
2348 args.array<double>("boundaryJac_ref");
2349 xt::pyarray<double> &elementDiameter =
2350 args.array<double>("elementDiameter");
2351 int nElements_global = args.scalar<int>("nElements_global");
2352 double g = args.scalar<double>("g");
2353 xt::pyarray<int> &h_l2g = args.array<int>("h_l2g");
2354 xt::pyarray<int> &vel_l2g = args.array<int>("vel_l2g");
2355 xt::pyarray<double> &b_dof = args.array<double>("b_dof");
2356 xt::pyarray<double> &h_dof = args.array<double>("h_dof");
2357 xt::pyarray<double> &hu_dof = args.array<double>("hu_dof");
2358 xt::pyarray<double> &hv_dof = args.array<double>("hv_dof");
2359 xt::pyarray<double> &q_cfl = args.array<double>("q_cfl");
2360 xt::pyarray<int> &sdInfo_hu_hu_rowptr =
2361 args.array<int>("sdInfo_hu_hu_rowptr");
2362 xt::pyarray<int> &sdInfo_hu_hu_colind =
2363 args.array<int>("sdInfo_hu_hu_colind");
2364 xt::pyarray<int> &sdInfo_hu_hv_rowptr =
2365 args.array<int>("sdInfo_hu_hv_rowptr");
2366 xt::pyarray<int> &sdInfo_hu_hv_colind =
2367 args.array<int>("sdInfo_hu_hv_colind");
2368 xt::pyarray<int> &sdInfo_hv_hv_rowptr =
2369 args.array<int>("sdInfo_hv_hv_rowptr");
2370 xt::pyarray<int> &sdInfo_hv_hv_colind =
2371 args.array<int>("sdInfo_hv_hv_colind");
2372 xt::pyarray<int> &sdInfo_hv_hu_rowptr =
2373 args.array<int>("sdInfo_hv_hu_rowptr");
2374 xt::pyarray<int> &sdInfo_hv_hu_colind =
2375 args.array<int>("sdInfo_hv_hu_colind");
2376 xt::pyarray<int> &csrRowIndeces_h_h = args.array<int>("csrRowIndeces_h_h");
2377 xt::pyarray<int> &csrColumnOffsets_h_h =
2378 args.array<int>("csrColumnOffsets_h_h");
2379 xt::pyarray<int> &csrRowIndeces_h_hu =
2380 args.array<int>("csrRowIndeces_h_hu");
2381 xt::pyarray<int> &csrColumnOffsets_h_hu =
2382 args.array<int>("csrColumnOffsets_h_hu");
2383 xt::pyarray<int> &csrRowIndeces_h_hv =
2384 args.array<int>("csrRowIndeces_h_hv");
2385 xt::pyarray<int> &csrColumnOffsets_h_hv =
2386 args.array<int>("csrColumnOffsets_h_hv");
2387 xt::pyarray<int> &csrRowIndeces_hu_h =
2388 args.array<int>("csrRowIndeces_hu_h");
2389 xt::pyarray<int> &csrColumnOffsets_hu_h =
2390 args.array<int>("csrColumnOffsets_hu_h");
2391 xt::pyarray<int> &csrRowIndeces_hu_hu =
2392 args.array<int>("csrRowIndeces_hu_hu");
2393 xt::pyarray<int> &csrColumnOffsets_hu_hu =
2394 args.array<int>("csrColumnOffsets_hu_hu");
2395 xt::pyarray<int> &csrRowIndeces_hu_hv =
2396 args.array<int>("csrRowIndeces_hu_hv");
2397 xt::pyarray<int> &csrColumnOffsets_hu_hv =
2398 args.array<int>("csrColumnOffsets_hu_hv");
2399 xt::pyarray<int> &csrRowIndeces_hv_h =
2400 args.array<int>("csrRowIndeces_hv_h");
2401 xt::pyarray<int> &csrColumnOffsets_hv_h =
2402 args.array<int>("csrColumnOffsets_hv_h");
2403 xt::pyarray<int> &csrRowIndeces_hv_hu =
2404 args.array<int>("csrRowIndeces_hv_hu");
2405 xt::pyarray<int> &csrColumnOffsets_hv_hu =
2406 args.array<int>("csrColumnOffsets_hv_hu");
2407 xt::pyarray<int> &csrRowIndeces_hv_hv =
2408 args.array<int>("csrRowIndeces_hv_hv");
2409 xt::pyarray<int> &csrColumnOffsets_hv_hv =
2410 args.array<int>("csrColumnOffsets_hv_hv");
2411 xt::pyarray<double> &globalJacobian = args.array<double>("globalJacobian");
2412 xt::pyarray<double> &porosity_dof = args.array<double>("porosity_dof");
2413 int nExteriorElementBoundaries_global =
2414 args.scalar<int>("nExteriorElementBoundaries_global");
2415 xt::pyarray<int> &exteriorElementBoundariesArray =
2416 args.array<int>("exteriorElementBoundariesArray");
2417 xt::pyarray<int> &elementBoundaryElementsArray =
2418 args.array<int>("elementBoundaryElementsArray");
2419 xt::pyarray<int> &elementBoundaryLocalElementBoundariesArray =
2420 args.array<int>("elementBoundaryLocalElementBoundariesArray");
2421 xt::pyarray<int> &isDOFBoundary_h = args.array<int>("isDOFBoundary_h");
2422 xt::pyarray<int> &isDOFBoundary_hu = args.array<int>("isDOFBoundary_hu");
2423 xt::pyarray<int> &isDOFBoundary_hv = args.array<int>("isDOFBoundary_hv");
2424 xt::pyarray<int> &isAdvectiveFluxBoundary_h =
2425 args.array<int>("isAdvectiveFluxBoundary_h");
2426 xt::pyarray<int> &isAdvectiveFluxBoundary_hu =
2427 args.array<int>("isAdvectiveFluxBoundary_hu");
2428 xt::pyarray<int> &isAdvectiveFluxBoundary_hv =
2429 args.array<int>("isAdvectiveFluxBoundary_hv");
2430 xt::pyarray<int> &isDiffusiveFluxBoundary_hu =
2431 args.array<int>("isDiffusiveFluxBoundary_hu");
2432 xt::pyarray<int> &isDiffusiveFluxBoundary_hv =
2433 args.array<int>("isDiffusiveFluxBoundary_hv");
2434 xt::pyarray<double> &ebqe_bc_h_ext = args.array<double>("ebqe_bc_h_ext");
2435 xt::pyarray<double> &ebqe_bc_flux_mass_ext =
2436 args.array<double>("ebqe_bc_flux_mass_ext");
2437 xt::pyarray<double> &ebqe_bc_flux_mom_hu_adv_ext =
2438 args.array<double>("ebqe_bc_flux_mom_hu_adv_ext");
2439 xt::pyarray<double> &ebqe_bc_flux_mom_hv_adv_ext =
2440 args.array<double>("ebqe_bc_flux_mom_hv_adv_ext");
2441 xt::pyarray<double> &ebqe_bc_hu_ext = args.array<double>("ebqe_bc_hu_ext");
2442 xt::pyarray<double> &ebqe_bc_flux_hu_diff_ext =
2443 args.array<double>("ebqe_bc_flux_hu_diff_ext");
2444 xt::pyarray<double> &ebqe_penalty_ext =
2445 args.array<double>("ebqe_penalty_ext");
2446 xt::pyarray<double> &ebqe_bc_hv_ext = args.array<double>("ebqe_bc_hv_ext");
2447 xt::pyarray<double> &ebqe_bc_flux_hv_diff_ext =
2448 args.array<double>("ebqe_bc_flux_hv_diff_ext");
2449 xt::pyarray<int> &csrColumnOffsets_eb_h_h =
2450 args.array<int>("csrColumnOffsets_eb_h_h");
2451 xt::pyarray<int> &csrColumnOffsets_eb_h_hu =
2452 args.array<int>("csrColumnOffsets_eb_h_hu");
2453 xt::pyarray<int> &csrColumnOffsets_eb_h_hv =
2454 args.array<int>("csrColumnOffsets_eb_h_hv");
2455 xt::pyarray<int> &csrColumnOffsets_eb_hu_h =
2456 args.array<int>("csrColumnOffsets_eb_hu_h");
2457 xt::pyarray<int> &csrColumnOffsets_eb_hu_hu =
2458 args.array<int>("csrColumnOffsets_eb_hu_hu");
2459 xt::pyarray<int> &csrColumnOffsets_eb_hu_hv =
2460 args.array<int>("csrColumnOffsets_eb_hu_hv");
2461 xt::pyarray<int> &csrColumnOffsets_eb_hv_h =
2462 args.array<int>("csrColumnOffsets_eb_hv_h");
2463 xt::pyarray<int> &csrColumnOffsets_eb_hv_hu =
2464 args.array<int>("csrColumnOffsets_eb_hv_hu");
2465 xt::pyarray<int> &csrColumnOffsets_eb_hv_hv =
2466 args.array<int>("csrColumnOffsets_eb_hv_hv");
2467 double dt = args.scalar<double>("dt");
2468 //
2469 // loop over elements to compute volume integrals and load them into the
2470 // element Jacobians and global Jacobian
2471 //
2472 for (int eN = 0; eN < nElements_global; eN++) {
2473 double elementJacobian_h_h[nDOF_test_element]
2474 [nDOF_trial_element],
2475 elementJacobian_hu_hu[nDOF_test_element][nDOF_trial_element],
2476 elementJacobian_hv_hv[nDOF_test_element][nDOF_trial_element];
2477 for (int i = 0; i < nDOF_test_element; i++)
2478 for (int j = 0; j < nDOF_trial_element; j++) {
2479 elementJacobian_h_h[i][j] = 0.0;
2480 elementJacobian_hu_hu[i][j] = 0.0;
2481 elementJacobian_hv_hv[i][j] = 0.0;
2482 }
2483 for (int k = 0; k < nQuadraturePoints_element; k++) {
2484 int eN_k = eN * nQuadraturePoints_element +
2485 k, // index to a scalar at a quadrature point
2486 eN_k_nSpace = eN_k * nSpace,
2487 eN_nDOF_trial_element =
2488 eN *
2489 nDOF_trial_element; // index to a vector at a quadrature point
2490
2491 // declare local storage
2492 double jac[nSpace * nSpace], jacDet, jacInv[nSpace * nSpace],
2493 dV, h_test_dV[nDOF_test_element], vel_test_dV[nDOF_test_element], x,
2494 y, xt, yt;
2495 // get jacobian, etc for mapping reference element
2496 ck.calculateMapping_element(
2497 eN, k, mesh_dof.data(), mesh_l2g.data(), mesh_trial_ref.data(),
2498 mesh_grad_trial_ref.data(), jac, jacDet, jacInv, x, y);
2499 // get the physical integration weight, times the porosity: storage
2500 // is phi h and phi hu, so the mass matrix is int phi_h w_i w_j
2501 dV = fabs(jacDet) * dV_ref[k];
2502 {
2503 double phi_q = 0.;
2504 ck.valFromDOF(porosity_dof.data(),
2505 &h_l2g.data()[eN * nDOF_trial_element],
2506 &h_trial_ref.data()[k * nDOF_trial_element], phi_q);
2507 dV *= phi_q;
2508 }
2509 // precalculate test function products with integration weights
2510 for (int j = 0; j < nDOF_trial_element; j++) {
2511 h_test_dV[j] = h_test_ref[k * nDOF_trial_element + j] * dV;
2512 vel_test_dV[j] = vel_test_ref[k * nDOF_trial_element + j] * dV;
2513 }
2514
2515 for (int i = 0; i < nDOF_test_element; i++) {
2516 int i_nSpace = i * nSpace;
2517 for (int j = 0; j < nDOF_trial_element; j++) {
2518 int j_nSpace = j * nSpace;
2519 elementJacobian_h_h[i][j] += (i == j ? 1.0 : 0.0) * h_test_dV[i];
2520 elementJacobian_hu_hu[i][j] +=
2521 (i == j ? 1.0 : 0.0) * vel_test_dV[i];
2522 elementJacobian_hv_hv[i][j] +=
2523 (i == j ? 1.0 : 0.0) * vel_test_dV[i];
2524 } // j
2525 } // i
2526 } // k
2527 //
2528 // load into element Jacobian into global Jacobian
2529 //
2530 for (int i = 0; i < nDOF_test_element; i++) {
2531 int eN_i = eN * nDOF_test_element + i;
2532 for (int j = 0; j < nDOF_trial_element; j++) {
2533 int eN_i_j = eN_i * nDOF_trial_element + j;
2534 globalJacobian[csrRowIndeces_h_h[eN_i] +
2535 csrColumnOffsets_h_h[eN_i_j]] +=
2536 elementJacobian_h_h[i][j];
2537 globalJacobian[csrRowIndeces_hu_hu[eN_i] +
2538 csrColumnOffsets_hu_hu[eN_i_j]] +=
2539 elementJacobian_hu_hu[i][j];
2540 globalJacobian[csrRowIndeces_hv_hv[eN_i] +
2541 csrColumnOffsets_hv_hv[eN_i_j]] +=
2542 elementJacobian_hv_hv[i][j];
2543 } // j
2544 } // i
2545 } // elements
2546 }
2547}; // namespace proteus
2548
2549inline SW2DCV_base *newSW2DCV(int nSpaceIn, int nQuadraturePoints_elementIn,
2550 int nDOF_mesh_trial_elementIn,
2551 int nDOF_trial_elementIn, int nDOF_test_elementIn,
2552 int nQuadraturePoints_elementBoundaryIn,
2553 int CompKernelFlag) {
2555 CompKernel>(
2556 nSpaceIn, nQuadraturePoints_elementIn, nDOF_mesh_trial_elementIn,
2557 nDOF_trial_elementIn, nDOF_test_elementIn,
2558 nQuadraturePoints_elementBoundaryIn, CompKernelFlag);
2559}
2560} // end namespace proteus
2561
2562#endif
#define VEL_FIX_POWER
Definition GN_SW2DCV.h:14
Double s
Definition Headers.h:84
Double u
Definition Headers.h:89
Double * z
Definition Headers.h:49
Double ut
Definition Headers.h:92
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 calculateResidual(arguments_dict &args)=0
virtual void calculateEV(arguments_dict &args)=0
virtual void calculateLumpedMassMatrix(arguments_dict &args)=0
virtual double calculateEdgeBasedCFL(arguments_dict &args)=0
virtual ~SW2DCV_base()
Definition SW2DCV.h:134
virtual void calculateMassMatrix(arguments_dict &args)=0
virtual void convexLimiting(arguments_dict &args)=0
static double phiBar(int i, int j, xt::pyarray< double > &porosity_dof)
Definition SW2DCV.h:644
double calculateEdgeBasedCFL(arguments_dict &args)
Definition SW2DCV.h:752
void convexLimiting(arguments_dict &args)
Definition SW2DCV.h:234
CompKernelType ck
Definition SW2DCV.h:149
const int nDOF_test_X_trial_element
Definition SW2DCV.h:148
void calculateMassMatrix(arguments_dict &args)
Definition SW2DCV.h:2058
void wallRiemann(double g, double hi, double hui, double hvi, double nx, double ny, double hEps, double &dp, double &rate)
Definition SW2DCV.h:658
void tailwaterFlux(double g, double hi, double hui, double hvi, double hb, double nx, double ny, double hEps, double &dFh, double &dFhu, double &dFhv, double &rate)
Definition SW2DCV.h:692
double muWallTranspose(int i, int j, int ij, double ui, double vi, double uj, double vj, xt::pyarray< double > &wall_dof, xt::pyarray< double > &Cx, xt::pyarray< double > &Cy, xt::pyarray< double > &CTx, xt::pyarray< double > &CTy)
Definition SW2DCV.h:738
double maxWaveSpeedSharpInitialGuess(double g, double nx, double ny, double hL, double huL, double hvL, double hR, double huR, double hvR, double hEps, bool debugging)
Definition SW2DCV.h:160
void calculateResidual(arguments_dict &args)
Definition SW2DCV.h:1010
void calculateCFL(const double &elementDiameter, const double &g, const double &h, const double &hu, const double &hv, const double hEps, double &cfl)
Definition SW2DCV.h:215
void calculateEV(arguments_dict &args)
Definition SW2DCV.h:858
void calculateLumpedMassMatrix(arguments_dict &args)
Definition SW2DCV.h:2303
#define c(i)
Definition jf.h:21
#define POWER_SMOOTHNESS_INDICATOR
Definition m_comp_co2.h:22
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
double ENTROPY_FLUX1(const double &g, const double &h, const double &hu, const double &hv, const double &z, const double &one_over_hReg)
Definition GN_SW2DCV.h:71
double phiDiff(const double &g, const double &h1k, const double &h2k, const double &hL, const double &hR, const double &uL, const double &uR)
Definition SW2DCV.h:88
double phiDDiff2(const double &g, const double &h1k, const double &h2k, const double &hL, const double &hR, const double &uL, const double &uR)
Definition SW2DCV.h:100
double phip(const double &g, const double &h, const double &hL, const double &hR)
Definition SW2DCV.h:72
double nu1(const double &g, const double &hStar, const double &hL, const double &uL, const double &one_over_hL)
Definition SW2DCV.h:76
ENTROPY
Definition TADR.h:40
SW2DCV_base * newSW2DCV(int nSpaceIn, int nQuadraturePoints_elementIn, int nDOF_mesh_trial_elementIn, int nDOF_trial_elementIn, int nDOF_test_elementIn, int nQuadraturePoints_elementBoundaryIn, int CompKernelFlag)
Definition SW2DCV.h:2549
double DENTROPY_DHU(const double &g, const double &h, const double &hu, const double &hv, const double &z, const double &one_over_hReg)
Definition GN_SW2DCV.h:61
double hStarLFromQuadPhiFromAbove(const double &g, const double &hStarL, const double &hStarR, const double &hL, const double &hR, const double &uL, const double &uR)
Definition SW2DCV.h:106
double phiDDiff1(const double &g, const double &h1k, const double &h2k, const double &hL, const double &hR, const double &uL, const double &uR)
Definition SW2DCV.h:94
double DENTROPY_DHV(const double &g, const double &h, const double &hu, const double &hv, const double &z, const double &one_over_hReg)
Definition GN_SW2DCV.h:66
double hStarRFromQuadPhiFromBelow(const double &g, const double &hStarL, const double &hStarR, const double &hL, const double &hR, const double &uL, const double &uR)
Definition SW2DCV.h:117
double DENTROPY_DH(const double &g, const double &h, const double &hu, const double &hv, const double &z, const double &one_over_hReg)
Definition GN_SW2DCV.h:56
double ENTROPY_FLUX2(const double &g, const double &h, const double &hu, const double &hv, const double &z, const double &one_over_hReg)
Definition GN_SW2DCV.h:78
double nu3(const double &g, const double &hStar, const double &hR, const double &uR, const double &one_over_hR)
Definition SW2DCV.h:82
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 fp(const double &g, const double &h, const double &hZ)
Definition SW2DCV.h:66
double f(const double &g, const double &h, const double &hZ)
Definition SW2DCV.h:58
T & scalar(const std::string &key)
xt::pyarray< T > & array(const std::string &key)