Nektar++
Loading...
Searching...
No Matches
edesign/RiemannSolvers/UnitTests/TestRiemann.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestRiemann.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#define BOOST_TEST_MODULE TestRiemann
36
40
43
45#include <SolverCore/TraceFlux/TraceFluxOp.hpp>
46
49
50#if defined(_MSC_VER)
51#undef max
52#undef min
53#endif
54
55using namespace Nektar;
56using namespace Nektar::LibUtilities;
57using namespace Nektar::MultiRegions;
58using namespace Nektar::SolverCore;
59
61
63
64// Helpers: flatten/unflatten component-major
65template <typename TData>
67 const Array<OneD, Array<OneD, TData>> &a, unsigned int ncomp, size_t npts)
68{
69 Array<OneD, TData> flat(ncomp * npts, 0.0);
70 for (unsigned int c = 0; c < ncomp; ++c)
71 {
72 for (size_t i = 0; i < npts; ++i)
73 {
74 flat[c * npts + i] = a[c][i];
75 }
76 }
77 return flat;
78}
79
80template <typename TData>
81static void UnflattenCompMajor(const Array<OneD, TData> &flat,
83 unsigned int ncomp, size_t npts)
84{
85 for (unsigned int c = 0; c < ncomp; ++c)
86 {
87 for (size_t i = 0; i < npts; ++i)
88 {
89 a[c][i] = flat[c * npts + i];
90 }
91 }
92}
93
94// A primitive Euler state (for building left/right Riemann states).
95//
96// Fields:
97// rho : density
98// p : pressure
99// u : velocity vector (only first spaceDim entries used; others ignored)
101{
102 double rho;
103 double p;
104 double u[3];
105};
106
107// Convert primitive state W = (rho, u, p) to conserved Euler state U = (rho,
108// rhou..., E).
109static inline void PrimToCons(const PrimState &W, unsigned int spaceDim,
110 Array<OneD, double> &U, double gamma)
111{
112 // U layout: [rho, rhou..., E] length = spaceDim + 2
113 const unsigned int nFields = spaceDim + 2;
114 ASSERTL0(U.size() == nFields, "PrimToCons: wrong U size");
115
116 U[0] = W.rho;
117
118 double kin = 0.0;
119 for (unsigned int d = 0; d < spaceDim; ++d)
120 {
121 U[1 + d] = W.rho * W.u[d];
122 kin += 0.5 * W.rho * W.u[d] * W.u[d];
123 }
124
125 const double rhoe = W.p / (gamma - 1.0);
126 U[nFields - 1] = rhoe + kin;
127}
128
129static inline double ConsToPressure(const Array<OneD, const double> &U,
130 unsigned int spaceDim, double gamma)
131{
132 const unsigned int nFields = spaceDim + 2;
133 const double rho = U[0];
134
135 double mom2_over_rho = 0.0;
136 for (unsigned int d = 0; d < spaceDim; ++d)
137 {
138 const double m = U[1 + d];
139 mom2_over_rho += (m * m) / rho;
140 }
141
142 const double E = U[nFields - 1];
143 const double p = (gamma - 1.0) * (E - 0.5 * mom2_over_rho);
144 return p;
145}
146
147// Compute the physical flux dotted with a unit (or non-unit) normal n.
148//
149// Inputs:
150// U : conserved state [rho, rhou..., E]
151// n : normal vector length = spaceDim
152// spaceDim : spatial dimension
153// gamma : ratio of specific heats
154//
155// Output:
156// fluxDotN : length = spaceDim + 2, holding F(U)·n:
157static inline void PhysicalFluxDotN(const Array<OneD, const double> &U,
159 unsigned int spaceDim, double gamma,
160 Array<OneD, double> &fluxDotN)
161{
162 // Euler physical flux in normal direction
163 // layout: [rho, rhou..., E] length = spaceDim + 2
164 const unsigned int nFields = spaceDim + 2;
165 ASSERTL0(U.size() == nFields, "PhysicalFluxDotN: wrong U size");
166 ASSERTL0(n.size() == spaceDim, "PhysicalFluxDotN: wrong n size");
167 ASSERTL0(fluxDotN.size() == nFields,
168 "PhysicalFluxDotN: wrong fluxDotN size");
169
170 const double rho = U[0];
171 double u[3] = {0.0, 0.0, 0.0};
172
173 for (unsigned int d = 0; d < spaceDim; ++d)
174 {
175 u[d] = U[1 + d] / rho;
176 }
177
178 double un = 0.0;
179 for (unsigned int d = 0; d < spaceDim; ++d)
180 {
181 un += u[d] * n[d];
182 }
183
184 const double p = ConsToPressure(U, spaceDim, gamma);
185 const double E = U[nFields - 1];
186
187 // mass
188 fluxDotN[0] = rho * un;
189
190 // momentum
191 for (unsigned int k = 0; k < spaceDim; ++k)
192 {
193 const double momk = U[1 + k];
194 fluxDotN[1 + k] = momk * un + p * n[k];
195 }
196
197 // energy
198 fluxDotN[nFields - 1] = (E + p) * un;
199}
200
201static void FillTraceFromPrimStates(unsigned int spaceDim, size_t npts,
202 const PrimState &WL, const PrimState &WR,
205 double gamma)
206{
207 const unsigned int nFields = spaceDim + 2;
208
209 Array<OneD, double> UL(nFields, 0.0), UR(nFields, 0.0);
210 PrimToCons(WL, spaceDim, UL, gamma);
211 PrimToCons(WR, spaceDim, UR, gamma);
212
213 for (size_t i = 0; i < npts; ++i)
214 {
215 for (unsigned int c = 0; c < nFields; ++c)
216 {
217 fwd[c][i] = UL[c];
218 bwd[c][i] = UR[c];
219 }
220 }
221}
222
223static void FillTraceNormals(unsigned int spaceDim, size_t npts,
225 Array<OneD, Array<OneD, double>> &normals)
226{
227 for (unsigned int d = 0; d < spaceDim; ++d)
228 {
229 Vmath::Fill((int)npts, n[d], normals[d], 1);
230 }
231}
232
233static inline double SoundSpeedFromRhoP(double rho, double p, double gamma)
234{
235 return std::sqrt(gamma * p / rho);
236}
237
238// Build primitive state using Mach definition.
239static inline PrimState MakeStateFromMach(double rho, double p, double M,
240 double cRef)
241{
242 PrimState W;
243 W.rho = rho;
244 W.p = p;
245 // x-normal velocity only
246 W.u[0] = M * cRef;
247 W.u[1] = 0.0;
248 W.u[2] = 0.0;
249 return W;
250}
251
253 unsigned int spaceDim,
254 double gamma)
255{
256 Array<OneD, double> U(spaceDim + 2, 0.0), F(spaceDim + 2, 0.0);
257 PrimToCons(W, spaceDim, U, gamma);
258
259 Array<OneD, double> normals(spaceDim, 0.0);
260 // x-normal
261 normals[0] = 1.0;
262 PhysicalFluxDotN(U, normals, spaceDim, gamma, F);
263 return F;
264}
265
267 const Array<OneD, const double> &b, double tol)
268{
269 BOOST_REQUIRE_EQUAL(a.size(), b.size());
270 for (size_t i = 0; i < a.size(); ++i)
271 {
272 const double denom = std::max({1.0, std::abs(a[i]), std::abs(b[i])});
273 BOOST_CHECK_SMALL((a[i] - b[i]) / denom, tol);
274 }
275}
276
278{
279 for (size_t i = 0; i < f.size(); ++i)
280 {
281 BOOST_CHECK(std::isfinite(f[i]));
282 }
283}
284
285// Session + DG setup
287 const std::string &xml, const std::string &exec = "Serial")
288{
289 BOOST_TEST_MESSAGE("Creating input and output fields");
290
291 int argc = 3;
292 char **argv = new char *[argc];
293 argv[0] = strdup("TestRiemannOperator");
294 argv[1] = strdup(xml.c_str());
295 argv[2] = strdup(("--opExecSpace=" + exec).c_str());
296
298
299 for (int i = 0; i < argc; ++i)
300 {
301 free(argv[i]);
302 }
303 delete[] argv;
304
305 return session;
306}
307
315
316// Apply operator to fwd/bwd arrays, producing flux arrays
317template <typename TData>
320 const std::string &method, const std::string &execStr,
321 const MultiRegions::DisContFieldSharedPtr &dg, unsigned int spaceDim,
322 size_t npts, unsigned int nFields,
323 const Array<OneD, Array<OneD, TData>> &normals,
324 const Array<OneD, Array<OneD, TData>> &fwd,
325 const Array<OneD, Array<OneD, TData>> &bwd,
327{
328 auto traceAttr =
329 GetBlockAttributes<TData, FieldState::Phys>(dg->GetTrace());
330
331 LibUtilities::Field<TData, FieldState::Phys> fwdField("fwd", traceAttr,
332 nFields, 1);
333 LibUtilities::Field<TData, FieldState::Phys> bwdField("bwd", traceAttr,
334 nFields, 1);
335 LibUtilities::Field<TData, FieldState::Phys> flxField("flux", traceAttr,
336 nFields, 1);
337
339 "traceNormals", traceAttr, spaceDim, 1);
340
341 fwdField.template CopyArray<NektarSpaces::HostSpace>(
342 FlattenCompMajor(fwd, nFields, npts));
343 bwdField.template CopyArray<NektarSpaces::HostSpace>(
344 FlattenCompMajor(bwd, nFields, npts));
345 normalsField.template CopyArray<NektarSpaces::HostSpace>(
346 FlattenCompMajor(normals, spaceDim, npts));
347
348 auto vars = session->GetVariables();
349
350 auto op = AdvTraceFluxCFEOp<double>::Create(dg, vars, method, execStr);
351 op->ApplyUnitTest(normalsField, fwdField, bwdField, flxField);
352
353 UnflattenCompMajor(flxField.ToArray(), flx, nFields, npts);
354}
355
356// Build constant Euler state and reference physical flux F·n for any normal
357// Conserved layout: [rho, rhou, rhov, rhow, E] = spaceDim + 2
359 unsigned int spaceDim, size_t npts,
360 const Array<OneD, Array<OneD, double>> &normals,
363 Array<OneD, Array<OneD, double>> &fluxRef, double gamma)
364{
365 const unsigned int nFields = spaceDim + 2;
366
367 const double rho = 0.9;
368 const double p = 1.0;
369 double u[3] = {1.0, 2.0, 3.0};
370
371 for (size_t i = 0; i < npts; ++i)
372 {
373 // density
374 fwd[0][i] = rho;
375 bwd[0][i] = rho;
376
377 // momentum
378 double mom[3] = {0, 0, 0};
379 for (unsigned int d = 0; d < spaceDim; ++d)
380 {
381 mom[d] = rho * u[d];
382 fwd[1 + d][i] = mom[d];
383 bwd[1 + d][i] = mom[d];
384 }
385
386 // energy
387 const double rhoe = p / (gamma - 1.0);
388 double kin = 0.0;
389 for (unsigned int d = 0; d < spaceDim; ++d)
390 {
391 kin += 0.5 * mom[d] * mom[d] / rho;
392 }
393 const double E = rhoe + kin;
394 fwd[nFields - 1][i] = E;
395 bwd[nFields - 1][i] = E;
396
397 // normal velocity u_n = u dot n
398 double un = 0.0;
399 for (unsigned int d = 0; d < spaceDim; ++d)
400 {
401 un += u[d] * normals[d][i];
402 }
403
404 // reference flux F·n
405 // mass flux is rho * u_n
406 fluxRef[0][i] = rho * un; // mass
407
408 // momentum flux is rho * u * u_n + p * n
409 for (unsigned int k = 0; k < spaceDim; ++k)
410 {
411 fluxRef[1 + k][i] = mom[k] * un + p * normals[k][i];
412 }
413
414 // energy flux is (E + p) * u_n
415 fluxRef[nFields - 1][i] = (E + p) * un;
416 }
417}
418
419// Runner: setup session/dg, make normals, fill states, apply op, check flux
420// normalDir: 0=x, 1=y, 2=z (valid if normalDir < spaceDim)
421// If normalDir is invalid for that mesh dimension, we SKIP.
422static void RunConstStateRiemannTest(const std::string &xml,
423 const std::string &method,
424 const std::string &execStr,
425 unsigned int normalDir, double gamma = 1.4)
426{
427 auto session = SetSession(xml, execStr);
428 auto dg = SetExpList(session);
429
430 dg->SetDataWarehouse();
431 dg->GetTrace()->SetDataWarehouse();
432
433 const unsigned int spaceDim = dg->GetExp(0)->GetShapeDimension();
434 const size_t npts = dg->GetTrace()->GetTotPoints();
435 const unsigned int nFields = spaceDim + 2;
436
437 // If user listed a normalDir that doesn't exist for this mesh dimension,
438 // just skip (use ASSERT if you prefer strict behaviour).
439 if (normalDir >= spaceDim)
440 {
441 return;
442 }
443
444 // normals: unit along normalDir
445 Array<OneD, Array<OneD, double>> normals(spaceDim);
446 for (unsigned int d = 0; d < spaceDim; ++d)
447 {
448 normals[d] = Array<OneD, double>(npts, 0.0);
449 }
450 for (size_t i = 0; i < npts; ++i)
451 {
452 normals[normalDir][i] = 1.0;
453 }
454
455 // arrays
456 Array<OneD, Array<OneD, double>> fwd(nFields), bwd(nFields), flux(nFields),
457 fluxRef(nFields);
458 for (unsigned int c = 0; c < nFields; ++c)
459 {
460 fwd[c] = Array<OneD, double>(npts, 0.0);
461 bwd[c] = Array<OneD, double>(npts, 0.0);
462 flux[c] = Array<OneD, double>(npts, 0.0);
463 fluxRef[c] = Array<OneD, double>(npts, 0.0);
464 }
465
466 FillConstStateAndReferenceFlux(spaceDim, npts, normals, fwd, bwd, fluxRef,
467 gamma);
468
469 ApplyRiemannOperator<double>(session, method, execStr, dg, spaceDim, npts,
470 nFields, normals, fwd, bwd, flux);
471
472 // check
473 for (unsigned int c = 0; c < nFields; ++c)
474 {
475 for (size_t i = 0; i < npts; ++i)
476 {
477 BOOST_CHECK_CLOSE(fluxRef[c][i], flux[c][i], 1e-10);
478 }
479 }
480}
481
482static void RunOneRiemannCase(const std::string &xml, const std::string &method,
483 const std::string &execStr, const PrimState &WL,
484 const PrimState &WR,
486 Array<OneD, double> &flux, double gamma = 1.4)
487{
488 auto session = SetSession(xml, execStr);
489 auto dg = SetExpList(session);
490
491 dg->SetDataWarehouse();
492 dg->GetTrace()->SetDataWarehouse();
493
494 const unsigned int spaceDim = dg->GetExp(0)->GetShapeDimension();
495 const size_t npts = dg->GetTrace()->GetTotPoints();
496 const unsigned int nFields = spaceDim + 2;
497
498 ASSERTL0(n.size() == spaceDim, "RunOneRiemannCase: wrong normal size");
499
500 Array<OneD, Array<OneD, double>> normals(spaceDim);
501 for (unsigned int d = 0; d < spaceDim; ++d)
502 {
503 normals[d] = Array<OneD, double>(npts, 0.0);
504 }
505 FillTraceNormals(spaceDim, npts, n, normals);
506
507 Array<OneD, Array<OneD, double>> fwdArr(nFields), bwdArr(nFields),
508 fluxArr(nFields);
509 for (unsigned int c = 0; c < nFields; ++c)
510 {
511 fwdArr[c] = Array<OneD, double>(npts, 0.0);
512 bwdArr[c] = Array<OneD, double>(npts, 0.0);
513 fluxArr[c] = Array<OneD, double>(npts, 0.0);
514 }
515
516 FillTraceFromPrimStates(spaceDim, npts, WL, WR, fwdArr, bwdArr, gamma);
517
518 ApplyRiemannOperator<double>(session, method, execStr, dg, spaceDim, npts,
519 nFields, normals, fwdArr, bwdArr, fluxArr);
520
521 flux = Array<OneD, double>(nFields, 0.0);
522 for (unsigned int c = 0; c < nFields; ++c)
523 {
524 flux[c] = fluxArr[c][0];
525 }
526}
527
528// Antisymmetry with normal flip:
529// F(L,R;n=+x) = - F(R,L;n=-x).
531 const std::string &xml, const std::string &method,
532 const std::string &execStr, const PrimState &WL, const PrimState &WR,
533 unsigned int spaceDim, double gamma, double tol)
534{
535 Array<OneD, double> normals(spaceDim, 0.0);
536 normals[0] = 1.0;
537
538 Array<OneD, double> negNormals(spaceDim, 0.0);
539 negNormals[0] = -1.0;
540
541 Array<OneD, double> f_plus, f_minus;
542
543 // F(L,R; +n)
544 RunOneRiemannCase(xml, method, execStr, WL, WR, normals, f_plus, gamma);
545
546 // F(R,L; -n)
547 RunOneRiemannCase(xml, method, execStr, WR, WL, negNormals, f_minus, gamma);
548
549 // Check f_plus + f_minus == 0
550 BOOST_REQUIRE_EQUAL(f_plus.size(), f_minus.size());
551
552 Array<OneD, double> sum(f_plus.size(), 0.0);
553 for (size_t i = 0; i < f_plus.size(); ++i)
554 {
555 sum[i] = f_plus[i] + f_minus[i];
556 }
557
558 Array<OneD, double> zero(f_plus.size(), 0.0);
559 CheckCloseRel(sum, zero, tol);
560}
561
562[[maybe_unused]] static void RunMachTest(const std::string &xml,
563 const std::vector<std::string> &ops,
564 const std::string &execStr,
565 double gamma = 1.4)
566{
567 // Determine dimension from the mesh.
568 auto session = SetSession(xml, execStr);
569 auto dg = SetExpList(session);
570 const unsigned int spaceDim = dg->GetExp(0)->GetShapeDimension();
571 BOOST_REQUIRE(spaceDim == 2 || spaceDim == 3);
572
573 const double tol = 1e-10;
574
575 Array<OneD, double> n(spaceDim, 0.0);
576 n[0] = 1.0;
577
578 //--------------------------------------------------------------------
579 // Equation of state string - currently only setup for Ideal Gas
580 //---------------------------------------------------------------------
581 const std::vector<std::string> EoS = {"IDEALGAS"};
582
583 // ---------------------------------------------------------------------
584 // Case family 1: Consistency for WL==WR across Mach list (ALL ops).
585 // ---------------------------------------------------------------------
586 const std::vector<double> Machs = {-3.0, -1.01, -0.99, -0.2, 0.0,
587 0.2, 0.99, 1.01, 3.0};
588
589 // Pick a base state.
590 const double rho0 = 1.0;
591 const double p0 = 1.0;
592 const double a0 = SoundSpeedFromRhoP(rho0, p0, gamma);
593
594 for (double M : Machs)
595 {
596 PrimState W = MakeStateFromMach(rho0, p0, M, a0);
597 Array<OneD, double> fluxRef = PhysicalFlux(W, spaceDim, gamma);
598
599 for (auto &method : ops)
600 {
601 BOOST_TEST_CONTEXT("Case family 1 Riemann method: "
602 << method << ", SpaceDim=" << spaceDim
603 << ", Mach=" << M)
604 {
606 RunOneRiemannCase(xml, method, execStr, W, W, n, flux, gamma);
607 CheckFinite(flux);
608
609 CheckCloseRel(flux, fluxRef, tol);
610 }
611 }
612 }
613
614 // ---------------------------------------------------------------------
615 // Case family 2: Supersonic upwind limit (upwind solvers only).
616 // Exclude "Average". LaxFriedrichs might not be exact depending on alpha.
617 // ---------------------------------------------------------------------
618 const std::vector<std::string> upwindOps = {
619 "HLL", "HLLC", "AUSM0", "AUSM1", "AUSM2", "AUSM3", "Roe"};
620
621 // Make different left/right states, but same-sign supersonic
622 // Mach. Use cRef based on code-consistent cA for AUSM to hit |M|>=1
623 // reliably:
624 const double rhoL = 1.0, pL = 1.0;
625 const double rhoR = 0.4, pR = 0.7;
626 const double cL = SoundSpeedFromRhoP(rhoL, pL, gamma);
627 const double cR = SoundSpeedFromRhoP(rhoR, pR, gamma);
628 const double cA = 0.5 * (cL + cR);
629
630 auto DoSupersonic = [&](double ML, double MR) {
631 PrimState WL = MakeStateFromMach(rhoL, pL, ML, cA);
632 PrimState WR = MakeStateFromMach(rhoR, pR, MR, cA);
633
634 // Expected: + => left physical flux, - => right physical flux
635 Array<OneD, double> fluxRef = (ML > 0.0 && MR > 0.0)
636 ? PhysicalFlux(WL, spaceDim, gamma)
637 : PhysicalFlux(WR, spaceDim, gamma);
638
639 for (auto &method : upwindOps)
640 {
641 BOOST_TEST_CONTEXT("Case family 2 Riemann method: "
642 << method << ", SpaceDim=" << spaceDim
643 << ", ML=" << ML << ", MR=" << MR)
644 {
646 RunOneRiemannCase(xml, method + EoS[0], execStr, WL, WR, n,
647 flux, gamma);
648 CheckFinite(flux);
649 CheckCloseRel(flux, fluxRef, tol);
650 }
651 }
652 };
653
654 DoSupersonic(+2.5, +2.0);
655 DoSupersonic(-2.5, -2.0);
656
657 // ---------------------------------------------------------------------
658 // Case family 3: Near-sonic / subsonic mixed cases: verify antisymmetry
659 // ---------------------------------------------------------------------
660 struct Pair
661 {
662 double ML, MR;
663 };
664 const std::vector<Pair> transonicPairs = {{+0.99, +1.01},
665 {-0.99, -1.01},
666 {+0.2, -0.2},
667 {-0.2, +0.2},
668 {+0.8, +0.3}};
669
670 for (auto pr : transonicPairs)
671 {
672 PrimState WL = MakeStateFromMach(rhoL, pL, pr.ML, cA);
673 PrimState WR = MakeStateFromMach(rhoR, pR, pr.MR, cA);
674
675 for (auto &method : ops)
676 {
677 BOOST_TEST_CONTEXT("Case family 3 Riemann method: "
678 << method << ", SpaceDim=" << spaceDim
679 << ", ML=" << pr.ML << ", MR=" << pr.MR)
680 {
682 RunOneRiemannCase(xml, method, execStr, WL, WR, n, flux, gamma);
683 CheckFinite(flux);
684 CheckNormalFlipAntisymmetry(xml, method, execStr, WL, WR,
685 spaceDim, gamma, tol);
686 }
687 }
688 }
689
690 // ---------------------------------------------------------------------
691 // Case family 4: M=0 with pressure jump
692 // AUSM0/1: pressure-only (mass=0, energy=0, mom=pbar)
693 // AUSM2/3: include Mp correction (nonzero mass & energy possible)
694 // ---------------------------------------------------------------------
695 const std::vector<std::string> ausmOps = {"AUSM0", "AUSM1", "AUSM2",
696 "AUSM3"};
697
698 {
699 auto ExpectAUSM0or1M0 = [&]() {
700 const unsigned int nFields = spaceDim + 2;
701 Array<OneD, double> fluxRef(nFields, 0.0);
702 const double pbar = 0.5 * (pL + pR);
703 fluxRef[0] = 0.0; // mass
704 fluxRef[1] = pbar; // x-momentum
705 if (spaceDim > 1)
706 {
707 fluxRef[2] = 0.0;
708 }
709 if (spaceDim > 2)
710 {
711 fluxRef[3] = 0.0;
712 }
713 fluxRef[nFields - 1] = 0.0; // energy
714 return fluxRef;
715 };
716
717 auto ExpectAUSM2or3M0 = [&](bool ausm3) {
718 const double rhoA = 0.5 * (rhoL + rhoR);
719 const double pbar = 0.5 * (pL + pR);
720
721 const double Kp = 0.25;
722 double fa = 1.0;
723 if (ausm3)
724 {
725 const double Mco = 0.01;
726 fa = Mco * (2.0 - Mco);
727 }
728
729 const double Mp = -(Kp / fa) * ((pR - pL) / (rhoA * cA * cA));
730 const bool cond = (Mp >= 0.0);
731
732 const double rhoUp = cond ? rhoL : rhoR;
733 const double pUp = cond ? pL : pR;
734
735 const unsigned int nFields = spaceDim + 2;
736 Array<OneD, double> fluxRef(nFields, 0.0);
737
738 // uUp=0 so momentum convective part is 0
739 fluxRef[0] = cA * Mp * rhoUp; // mass
740 fluxRef[1] = pbar; // x-momentum
741 if (spaceDim > 1)
742 {
743 fluxRef[2] = 0.0;
744 }
745 if (spaceDim > 2)
746 {
747 fluxRef[3] = 0.0;
748 }
749
750 // EUp + pUp = gamma/(gamma-1) * pUp (since u=0)
751 fluxRef[nFields - 1] =
752 cA * Mp * (gamma / (gamma - 1.0)) * pUp; // energy
753 return fluxRef;
754 };
755
756 PrimState WL = MakeStateFromMach(rhoL, pL, 0.0, cA);
757 PrimState WR = MakeStateFromMach(rhoR, pR, 0.0, cA);
758
759 for (auto &method : ausmOps)
760 {
761 BOOST_TEST_CONTEXT("Case family 4 Riemann method: "
762 << method << ", SpaceDim=" << spaceDim)
763 {
765 RunOneRiemannCase(xml, method + EoS[0], execStr, WL, WR, n,
766 flux, gamma);
767 CheckFinite(flux);
768
769 Array<OneD, double> fluxRef =
770 (method == "AUSM0" || method == "AUSM1")
771 ? ExpectAUSM0or1M0()
772 : (method == "AUSM2" ? ExpectAUSM2or3M0(false)
773 : ExpectAUSM2or3M0(true));
774 CheckCloseRel(flux, fluxRef, tol);
775 }
776 }
777 }
778}
779
780// Case list and helper to run all cases for one operator.
781// Adding a new op = add one RunCasesForOp(...) block.
782struct Case
783{
784 std::string xml;
785 unsigned int normalDir;
786};
787
788[[maybe_unused]] static void RunCasesForOp(const std::string &method,
789 const std::string &execStr,
790 const std::vector<Case> &cases)
791{
792 for (const auto &tc : cases)
793 {
794 BOOST_TEST_CONTEXT("method=" << method << " exec=" << execStr << " xml="
795 << tc.xml << " normalDir=" << tc.normalDir)
796 {
797 RunConstStateRiemannTest(tc.xml, method, execStr, tc.normalDir);
798 }
799 }
800}
801
802BOOST_AUTO_TEST_SUITE(TestSuiteRiemann)
803
804#if defined(NEKTAR_ENABLE_DOUBLE_PRECISION)
805BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_Average)
806{
807 std::string execStr(
808 boost::unit_test::framework::master_test_suite().argv[1]);
809
810 const std::vector<Case> cases = {
811 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
812 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
813 {"run/hex_Euler.xml", 2},
814 };
815
816 RunCasesForOp("AverageIDEALGAS", execStr, cases);
817}
818
819BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_LaxFriedrichs)
820{
821 std::string execStr(
822 boost::unit_test::framework::master_test_suite().argv[1]);
823
824 const std::vector<Case> cases = {
825 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
826 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
827 {"run/hex_Euler.xml", 2},
828 };
829
830 RunCasesForOp("LaxFriedrichsIDEALGAS", execStr, cases);
831}
832
833BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_HLL)
834{
835 std::string execStr(
836 boost::unit_test::framework::master_test_suite().argv[1]);
837
838 const std::vector<Case> cases = {
839 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
840 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
841 {"run/hex_Euler.xml", 2},
842 };
843
844 RunCasesForOp("HLLIDEALGAS", execStr, cases);
845}
846
847BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_HLLC)
848{
849 std::string execStr(
850 boost::unit_test::framework::master_test_suite().argv[1]);
851
852 const std::vector<Case> cases = {
853 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
854 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
855 {"run/hex_Euler.xml", 2},
856 };
857
858 RunCasesForOp("HLLCIDEALGAS", execStr, cases);
859}
860
861BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_AUSM0)
862{
863 std::string execStr(
864 boost::unit_test::framework::master_test_suite().argv[1]);
865
866 const std::vector<Case> cases = {
867 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
868 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
869 {"run/hex_Euler.xml", 2},
870 };
871
872 RunCasesForOp("AUSM0IDEALGAS", execStr, cases);
873}
874
875BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_AUSM1)
876{
877 std::string execStr(
878 boost::unit_test::framework::master_test_suite().argv[1]);
879
880 const std::vector<Case> cases = {
881 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
882 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
883 {"run/hex_Euler.xml", 2},
884 };
885
886 RunCasesForOp("AUSM1IDEALGAS", execStr, cases);
887}
888
889BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_AUSM2)
890{
891 std::string execStr(
892 boost::unit_test::framework::master_test_suite().argv[1]);
893
894 const std::vector<Case> cases = {
895 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
896 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
897 {"run/hex_Euler.xml", 2},
898 };
899
900 RunCasesForOp("AUSM2IDEALGAS", execStr, cases);
901}
902
903BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_AUSM3)
904{
905 std::string execStr(
906 boost::unit_test::framework::master_test_suite().argv[1]);
907
908 const std::vector<Case> cases = {
909 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
910 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
911 {"run/hex_Euler.xml", 2},
912 };
913
914 RunCasesForOp("AUSM3IDEALGAS", execStr, cases);
915}
916
917BOOST_AUTO_TEST_CASE(Riemann_ConstState_AllOps_Roe)
918{
919 std::string execStr(
920 boost::unit_test::framework::master_test_suite().argv[1]);
921
922 const std::vector<Case> cases = {
923 {"run/square_Euler.xml", 0}, {"run/square_Euler.xml", 1},
924 {"run/hex_Euler.xml", 0}, {"run/hex_Euler.xml", 1},
925 {"run/hex_Euler.xml", 2},
926 };
927
928 RunCasesForOp("RoeIDEALGAS", execStr, cases);
929}
930
931BOOST_AUTO_TEST_SUITE_END()
932
933BOOST_AUTO_TEST_SUITE(TestSuiteRiemannMach)
934
935BOOST_AUTO_TEST_CASE(Riemann_MachTest_2D_square)
936{
937 std::string execStr(
938 boost::unit_test::framework::master_test_suite().argv[1]);
939
940 const std::vector<std::string> ops = {
941 "AverageIDEALGAS", "LaxFriedrichsIDEALGAS", "HLLIDEALGAS",
942 "HLLCIDEALGAS", "AUSM0IDEALGAS", "AUSM1IDEALGAS",
943 "AUSM2IDEALGAS", "AUSM3IDEALGAS", "RoeIDEALGAS"};
944
945 RunMachTest("run/square_Euler.xml", ops, execStr, 1.4);
946}
947
948BOOST_AUTO_TEST_CASE(Riemann_MachTest_3D_hex)
949{
950 std::string execStr(
951 boost::unit_test::framework::master_test_suite().argv[1]);
952
953 const std::vector<std::string> ops = {
954 "AverageIDEALGAS", "LaxFriedrichsIDEALGAS", "HLLIDEALGAS",
955 "HLLCIDEALGAS", "AUSM0IDEALGAS", "AUSM1IDEALGAS",
956 "AUSM2IDEALGAS", "AUSM3IDEALGAS", "RoeIDEALGAS"};
957
958 RunMachTest("run/hex_Euler.xml", ops, execStr, 1.4);
959}
960#endif
961
962BOOST_AUTO_TEST_SUITE_END()
#define ASSERTL0(condition, msg)
LibUtilities::SessionReaderSharedPtr session
SpatialDomains::MeshGraphSharedPtr graph
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
std::map< int64_t, double > sum
static std::shared_ptr< AdvTraceFluxCFEOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &solverType="", const std::string &execStr="")
Create the implementation registered for solverType.
A Field represents expansion data to be operated on.
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy the data to a Nektar::Array, component by component and element by element, without padding,...
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
static void PrimToCons(const PrimState &W, unsigned int spaceDim, Array< OneD, double > &U, double gamma)
static void FillConstStateAndReferenceFlux(unsigned int spaceDim, size_t npts, const Array< OneD, Array< OneD, double > > &normals, Array< OneD, Array< OneD, double > > &fwd, Array< OneD, Array< OneD, double > > &bwd, Array< OneD, Array< OneD, double > > &fluxRef, double gamma)
static MultiRegions::DisContFieldSharedPtr SetExpList(const LibUtilities::SessionReaderSharedPtr &session)
static void CheckCloseRel(const Array< OneD, const double > &a, const Array< OneD, const double > &b, double tol)
static PrimState MakeStateFromMach(double rho, double p, double M, double cRef)
static void UnflattenCompMajor(const Array< OneD, TData > &flat, Array< OneD, Array< OneD, TData > > &a, unsigned int ncomp, size_t npts)
static void ApplyRiemannOperator(const LibUtilities::SessionReaderSharedPtr &session, const std::string &method, const std::string &execStr, const MultiRegions::DisContFieldSharedPtr &dg, unsigned int spaceDim, size_t npts, unsigned int nFields, const Array< OneD, Array< OneD, TData > > &normals, const Array< OneD, Array< OneD, TData > > &fwd, const Array< OneD, Array< OneD, TData > > &bwd, Array< OneD, Array< OneD, TData > > &flx)
static void RunMachTest(const std::string &xml, const std::vector< std::string > &ops, const std::string &execStr, double gamma=1.4)
static double ConsToPressure(const Array< OneD, const double > &U, unsigned int spaceDim, double gamma)
static void RunCasesForOp(const std::string &method, const std::string &execStr, const std::vector< Case > &cases)
static void CheckNormalFlipAntisymmetry(const std::string &xml, const std::string &method, const std::string &execStr, const PrimState &WL, const PrimState &WR, unsigned int spaceDim, double gamma, double tol)
static void FillTraceFromPrimStates(unsigned int spaceDim, size_t npts, const PrimState &WL, const PrimState &WR, Array< OneD, Array< OneD, double > > &fwd, Array< OneD, Array< OneD, double > > &bwd, double gamma)
static void RunConstStateRiemannTest(const std::string &xml, const std::string &method, const std::string &execStr, unsigned int normalDir, double gamma=1.4)
static void PhysicalFluxDotN(const Array< OneD, const double > &U, const Array< OneD, const double > &n, unsigned int spaceDim, double gamma, Array< OneD, double > &fluxDotN)
static Array< OneD, double > PhysicalFlux(const PrimState &W, unsigned int spaceDim, double gamma)
static double SoundSpeedFromRhoP(double rho, double p, double gamma)
static void FillTraceNormals(unsigned int spaceDim, size_t npts, const Array< OneD, const double > &n, Array< OneD, Array< OneD, double > > &normals)
static Array< OneD, TData > FlattenCompMajor(const Array< OneD, Array< OneD, TData > > &a, unsigned int ncomp, size_t npts)
static LibUtilities::SessionReaderSharedPtr SetSession(const std::string &xml, const std::string &exec="Serial")
static void RunOneRiemannCase(const std::string &xml, const std::string &method, const std::string &execStr, const PrimState &WL, const PrimState &WR, const Array< OneD, const double > &n, Array< OneD, double > &flux, double gamma=1.4)
static void CheckFinite(const Array< OneD, const double > &f)
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< DisContField > DisContFieldSharedPtr
BOOST_AUTO_TEST_CASE(TestCanGetRawPtr)
void Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
Definition Vmath.hpp:56