Skip to content

Commit 2351bbd

Browse files
Included an auxiliary function to check data
1 parent 562d9a5 commit 2351bbd

File tree

6 files changed

+194
-14
lines changed

6 files changed

+194
-14
lines changed

1D_RIEMANN/All_topology_solver.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,22 @@ int main(int argc, char* argv[]) {
120120
app.add_option("--p1L", Riemann_param.p1L, "Initial pressure phase 1 at left")->capture_default_str()->group("Initial conditions");
121121
app.add_option("--T1L", Riemann_param.T1L, "Initial temperature phase 1 at left")->capture_default_str()->group("Initial conditions");
122122
app.add_option("--u1L", Riemann_param.u1L, "Initial horizontal velocity phase 1 at left")->capture_default_str()->group("Initial conditions");
123-
app.add_option("--v1L", Riemann_param.v1L, "Initial vertical velocity phase 1 at left")->capture_default_str()->group("Initial conditions");
124123

125124
app.add_option("--rho2L", Riemann_param.rho2L, "Initial density phase 2 at left")->capture_default_str()->group("Initial conditions");
126125
app.add_option("--p2L", Riemann_param.p2L, "Initial pressure phase 2 at left")->capture_default_str()->group("Initial conditions");
127126
app.add_option("--T2L", Riemann_param.T2L, "Initial temperature phase 2 at left")->capture_default_str()->group("Initial conditions");
128127
app.add_option("--u2L", Riemann_param.u2L, "Initial horizontal velocity phase 2 at left")->capture_default_str()->group("Initial conditions");
129-
app.add_option("--v2L", Riemann_param.v2L, "Initial vertical velocity phase 2 at left")->capture_default_str()->group("Initial conditions");
130128

131129
app.add_option("--alpha1R", Riemann_param.alpha1R, "Initial volume fraction at right")->capture_default_str()->group("Initial conditions");
132130
app.add_option("--rho1R", Riemann_param.rho1R, "Initial density phase 1 at right")->capture_default_str()->group("Initial conditions");
133131
app.add_option("--p1R", Riemann_param.p1R, "Initial pressure phase 1 at right")->capture_default_str()->group("Initial conditions");
134132
app.add_option("--T1R", Riemann_param.T1R, "Initial temperature phase 1 at right")->capture_default_str()->group("Initial conditions");
135133
app.add_option("--u1R", Riemann_param.u1R, "Initial horizotnal velocity phase 1 at right")->capture_default_str()->group("Initial conditions");
136-
app.add_option("--v1R", Riemann_param.v1R, "Initial vertical velocity phase 1 at right")->capture_default_str()->group("Initial conditions");
137134

138135
app.add_option("--rho2R", Riemann_param.rho2R, "Initial density phase 2 at right")->capture_default_str()->group("Initial conditions");
139136
app.add_option("--p2R", Riemann_param.p2R, "Initial pressure phase 2 at right")->capture_default_str()->group("Initial conditions");
140137
app.add_option("--T2R", Riemann_param.T2R, "Initial temperature phase 2 at right")->capture_default_str()->group("Initial conditions");
141138
app.add_option("--u2R", Riemann_param.u2R, "Initial horizontal velocity phase 2 at right")->capture_default_str()->group("Initial conditions");
142-
app.add_option("--v2R", Riemann_param.v2R, "Initial vertical velocity phase 2 at right")->capture_default_str()->group("Initial conditions");
143139

144140
/*--- Create the instance of the class to perform the simulation ---*/
145141
CLI11_PARSE(app, argc, argv);

1D_RIEMANN/test_configuration/All_topology_solver.hpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace fs = std::filesystem;
2121
#include "schemes/Rusanov_flux.hpp"
2222
#include "schemes/non_conservative_flux.hpp"
2323

24+
/*--- Define preprocessor to check whether to control data or not ---*/
25+
#define VERBOSE
26+
2427
// This is the class for the simulation of the all-topology model
2528
//
2629
template<std::size_t dim>
@@ -118,6 +121,8 @@ class All_Topology_Solver {
118121

119122
Number get_max_lambda() const; /*--- Compute the estimate of the maximum eigenvalue ---*/
120123

124+
void check_data(); /*--- Auxiliary routine to check if (small) spurious negative values are present ---*/
125+
121126
void update_auxiliary_fields(); /*--- Routine to update auxiliary fields for output and time step update ---*/
122127
};
123128

@@ -424,6 +429,57 @@ All_Topology_Solver<dim>::get_max_lambda() const {
424429
return global_res;
425430
}
426431

432+
// Auxiliary routine to check if spurious negative values arise
433+
//
434+
template<std::size_t dim>
435+
void All_Topology_Solver<dim>::check_data() {
436+
samurai::for_each_cell(mesh,
437+
[&](const auto& cell)
438+
{
439+
// Start with the volume fraction
440+
if(conserved_variables[cell][Indices::ALPHA1_INDEX] < static_cast<Number>(0.0)) {
441+
std::cerr << "Negative volume fraction for phase 1" << std::endl;
442+
save("_diverged", conserved_variables);
443+
exit(1);
444+
}
445+
else if(conserved_variables[cell][Indices::ALPHA1_INDEX] > static_cast<Number>(1.0)) {
446+
std::cerr << "Exceeding volume fraction for phase 1" << std::endl;
447+
save("_diverged", conserved_variables);
448+
exit(1);
449+
}
450+
else if(std::isnan(conserved_variables[cell][Indices::ALPHA1_INDEX])) {
451+
std::cerr << "NaN volume fraction for phase 1" << std::endl;
452+
save("_diverged", conserved_variables);
453+
exit(1);
454+
}
455+
456+
// Sanity check for m1
457+
if(conserved_variables[cell][Indices::ALPHA1_RHO1_INDEX] < static_cast<Number>(0.0)) {
458+
std::cerr << "Negative mass for phase 1" << std::endl;
459+
save("_diverged", conserved_variables);
460+
exit(1);
461+
}
462+
else if(std::isnan(conserved_variables[cell][Indices::ALPHA1_RHO1_INDEX])) {
463+
std::cerr << "NaN mass for phase 1" << std::endl;
464+
save("_diverged", conserved_variables);
465+
exit(1);
466+
}
467+
468+
// Sanity check for m2
469+
if(conserved_variables[cell][Indices::ALPHA2_RHO2_INDEX] < static_cast<Number>(0.0)) {
470+
std::cerr << "Negative mass for phase 2" << std::endl;
471+
save("_diverged", conserved_variables);
472+
exit(1);
473+
}
474+
else if(std::isnan(conserved_variables[cell][Indices::ALPHA2_RHO2_INDEX])){
475+
std::cerr << "NaN mass for phase 2" << std::endl;
476+
save("_diverged", conserved_variables);
477+
exit(1);
478+
}
479+
}
480+
);
481+
}
482+
427483
// Update auxiliary fields after solution of the system
428484
//
429485
template<std::size_t dim>
@@ -589,10 +645,20 @@ void All_Topology_Solver<dim>::run(const std::size_t nfiles) {
589645
}
590646

591647
// Apply the numerical scheme
592-
conserved_variables_np1 = conserved_variables
593-
- dt*Rusanov_flux(conserved_variables)
594-
- dt*NonConservative_flux(conserved_variables);
648+
try {
649+
conserved_variables_np1 = conserved_variables
650+
- dt*Rusanov_flux(conserved_variables)
651+
- dt*NonConservative_flux(conserved_variables);
652+
}
653+
catch(const std::exception& e) {
654+
std::cerr << e.what() << std::endl;
655+
save("_diverged", conserved_variables);
656+
exit(1);
657+
}
595658
samurai::swap(conserved_variables, conserved_variables_np1);
659+
#ifdef VERBOSE
660+
check_data();
661+
#endif
596662

597663
// Save the results
598664
if(t >= static_cast<Number>(nsave + 1)*dt_save || t == Tf) {

1D_RIEMANN/test_configuration/containers.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,19 @@ struct Riemann_Parameters {
6060
T p1L;
6161
T T1L;
6262
T u1L;
63-
T v1L;
6463
T rho2L;
6564
T p2L;
6665
T T2L;
6766
T u2L;
68-
T v2L;
6967

7068
/*--- Right state ---*/
7169
T alpha1R;
7270
T rho1R;
7371
T p1R;
7472
T T1R;
7573
T u1R;
76-
T v1R;
7774
T rho2R;
7875
T p2R;
7976
T T2R;
8077
T u2R;
81-
T v2R;
8278
};

2D_RIEMANN/test_configuration/All_topology_solver.hpp

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace fs = std::filesystem;
2424
#include "schemes/Rusanov_flux.hpp"
2525
#include "schemes/non_conservative_flux.hpp"
2626

27+
/*--- Define preprocessor to check whether to control data or not ---*/
28+
#define VERBOSE
29+
2730
// This is the class for the simulation of the all-topology model
2831
//
2932
template<std::size_t dim>
@@ -118,6 +121,8 @@ class All_Topology_Solver {
118121

119122
Number get_max_lambda() const; /*--- Compute the estimate of the maximum eigenvalue ---*/
120123

124+
void check_data(); /*--- Auxiliary routine to check if (small) spurious negative values are present ---*/
125+
121126
void update_auxiliary_fields(); /*--- Routine to update auxiliary fields for output and time step update ---*/
122127
};
123128

@@ -433,6 +438,57 @@ All_Topology_Solver<dim>::get_max_lambda() const {
433438
return global_res;
434439
}
435440

441+
// Auxiliary routine to check if spurious negative values arise
442+
//
443+
template<std::size_t dim>
444+
void All_Topology_Solver<dim>::check_data() {
445+
samurai::for_each_cell(mesh,
446+
[&](const auto& cell)
447+
{
448+
// Start with the volume fraction
449+
if(conserved_variables[cell][Indices::ALPHA1_INDEX] < static_cast<Number>(0.0)) {
450+
std::cerr << "Negative volume fraction for phase 1" << std::endl;
451+
save("_diverged", conserved_variables);
452+
exit(1);
453+
}
454+
else if(conserved_variables[cell][Indices::ALPHA1_INDEX] > static_cast<Number>(1.0)) {
455+
std::cerr << "Exceeding volume fraction for phase 1" << std::endl;
456+
save("_diverged", conserved_variables);
457+
exit(1);
458+
}
459+
else if(std::isnan(conserved_variables[cell][Indices::ALPHA1_INDEX])) {
460+
std::cerr << "NaN volume fraction for phase 1" << std::endl;
461+
save("_diverged", conserved_variables);
462+
exit(1);
463+
}
464+
465+
// Sanity check for m1
466+
if(conserved_variables[cell][Indices::ALPHA1_RHO1_INDEX] < static_cast<Number>(0.0)) {
467+
std::cerr << "Negative mass for phase 1" << std::endl;
468+
save("_diverged", conserved_variables);
469+
exit(1);
470+
}
471+
else if(std::isnan(conserved_variables[cell][Indices::ALPHA1_RHO1_INDEX])) {
472+
std::cerr << "NaN mass for phase 1" << std::endl;
473+
save("_diverged", conserved_variables);
474+
exit(1);
475+
}
476+
477+
// Sanity check for m2
478+
if(conserved_variables[cell][Indices::ALPHA2_RHO2_INDEX] < static_cast<Number>(0.0)) {
479+
std::cerr << "Negative mass for phase 2" << std::endl;
480+
save("_diverged", conserved_variables);
481+
exit(1);
482+
}
483+
else if(std::isnan(conserved_variables[cell][Indices::ALPHA2_RHO2_INDEX])){
484+
std::cerr << "NaN mass for phase 2" << std::endl;
485+
save("_diverged", conserved_variables);
486+
exit(1);
487+
}
488+
}
489+
);
490+
}
491+
436492
// Update auxiliary fields after solution of the system
437493
//
438494
template<std::size_t dim>
@@ -595,10 +651,20 @@ void All_Topology_Solver<dim>::run(const std::size_t nfiles) {
595651
}
596652

597653
// Apply the numerical scheme
598-
conserved_variables_np1 = conserved_variables
599-
- dt*Rusanov_flux(conserved_variables)
600-
- dt*NonConservative_flux(conserved_variables);
654+
try {
655+
conserved_variables_np1 = conserved_variables
656+
- dt*Rusanov_flux(conserved_variables)
657+
- dt*NonConservative_flux(conserved_variables);
658+
}
659+
catch(const std::exception& e) {
660+
std::cerr << e.what() << std::endl;
661+
save("_diverged", conserved_variables);
662+
exit(1);
663+
}
601664
samurai::swap(conserved_variables, conserved_variables_np1);
665+
#ifdef VERBOSE
666+
check_data();
667+
#endif
602668

603669
// Save the results
604670
if(t >= static_cast<Number>(nsave + 1)*dt_save || t == Tf) {

include/schemes/Rusanov_flux.hpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "flux_base.hpp"
1010

11+
#define VERBOSE_FLUX
12+
1113
namespace samurai {
1214
/**
1315
* Implementation of a Rusanov flux
@@ -56,6 +58,22 @@ namespace samurai {
5658
const auto m2_L = qL(Indices::ALPHA2_RHO2_INDEX);
5759
const auto m2E2_L = qL(Indices::ALPHA2_RHO2_E2_INDEX);
5860

61+
// Verify if it is admissible
62+
#ifdef VERBOSE_FLUX
63+
if(m1_L < static_cast<Number>(0.0)) {
64+
throw std::runtime_error(std::string("Negative mass phase 1 left state: " + std::to_string(m1_L)));
65+
}
66+
if(m2_L < static_cast<Number>(0.0)) {
67+
throw std::runtime_error(std::string("Negative mass phase 2 left state: " + std::to_string(m2_L)));
68+
}
69+
if(alpha1_L < static_cast<Number>(0.0)) {
70+
throw std::runtime_error(std::string("Negative volume fraction phase 1 left state: " + std::to_string(alpha1_L)));
71+
}
72+
else if(alpha1_L > static_cast<Number>(1.0)) {
73+
throw std::runtime_error(std::string("Exceeding volume fraction phase 1 left state: " + std::to_string(alpha1_L)));
74+
}
75+
#endif
76+
5977
// Phase 1
6078
const auto inv_m1_L = static_cast<Number>(1.0)/m1_L; /*--- TODO: Add treatment for vanishing volume fraction ---*/
6179
const auto vel1_L_d = qL(Indices::ALPHA1_RHO1_U1_INDEX + curr_d)*inv_m1_L; /*--- TODO: Add treatment for vanishing volume fraction ---*/
@@ -68,6 +86,11 @@ namespace samurai {
6886
}
6987
const auto p1_L = this->EOS_phase1.pres_value_Rhoe(rho1_L, e1_L);
7088
const auto c1_L = this->EOS_phase1.c_value_RhoP(rho1_L, p1_L);
89+
#ifdef VERBOSE_FLUX
90+
if(std::isnan(c1_L)) {
91+
throw std::runtime_error(std::string("NaN speed of sound phase 1 left state"));
92+
}
93+
#endif
7194

7295
// Phase 2
7396
const auto inv_m2_L = static_cast<Number>(1.0)/m2_L; /*--- TODO: Add treatment for vanishing volume fraction ---*/
@@ -81,6 +104,11 @@ namespace samurai {
81104
}
82105
const auto p2_L = this->EOS_phase2.pres_value_Rhoe(rho2_L, e2_L);
83106
const auto c2_L = this->EOS_phase2.c_value_RhoP(rho2_L, p2_L);
107+
#ifdef VERBOSE_FLUX
108+
if(std::isnan(c2_L)) {
109+
throw std::runtime_error(std::string("NaN speed of sound phase 2 left state"));
110+
}
111+
#endif
84112

85113
/*--- Right state ---*/
86114
// Pre-fetch variables that will be used several times so as to exploit possible vectorization
@@ -91,6 +119,22 @@ namespace samurai {
91119
const auto m2_R = qR(Indices::ALPHA2_RHO2_INDEX);
92120
const auto m2E2_R = qR(Indices::ALPHA2_RHO2_E2_INDEX);
93121

122+
// Verify if it is admissible
123+
#ifdef VERBOSE_FLUX
124+
if(m1_R < static_cast<Number>(0.0)) {
125+
throw std::runtime_error(std::string("Negative mass phase 1 right state: " + std::to_string(m1_R)));
126+
}
127+
if(m2_R < static_cast<Number>(0.0)) {
128+
throw std::runtime_error(std::string("Negative mass phase 2 right state: " + std::to_string(m2_R)));
129+
}
130+
if(alpha1_R < static_cast<Number>(0.0)) {
131+
throw std::runtime_error(std::string("Negative volume fraction phase 1 right state: " + std::to_string(alpha1_R)));
132+
}
133+
else if(alpha1_R > static_cast<Number>(1.0)) {
134+
throw std::runtime_error(std::string("Exceeding volume fraction phase 1 right state: " + std::to_string(alpha1_R)));
135+
}
136+
#endif
137+
94138
// Phase 1
95139
const auto inv_m1_R = static_cast<Number>(1.0)/m1_R; /*--- TODO: Add treatment for vanishing volume fraction ---*/
96140
const auto vel1_R_d = qR(Indices::ALPHA1_RHO1_U1_INDEX + curr_d)*inv_m1_R; /*--- TODO: Add treatment for vanishing volume fraction ---*/
@@ -103,6 +147,11 @@ namespace samurai {
103147
}
104148
const auto p1_R = this->EOS_phase1.pres_value_Rhoe(rho1_R, e1_R);
105149
const auto c1_R = this->EOS_phase1.c_value_RhoP(rho1_R, p1_R);
150+
#ifdef VERBOSE_FLUX
151+
if(std::isnan(c1_R)) {
152+
throw std::runtime_error(std::string("NaN speed of sound phase 1 right state"));
153+
}
154+
#endif
106155

107156
// Phase 2
108157
const auto inv_m2_R = static_cast<Number>(1.0)/m2_R; /*--- TODO: Add treatment for vanishing volume fraction ---*/
@@ -116,6 +165,11 @@ namespace samurai {
116165
}
117166
const auto p2_R = this->EOS_phase2.pres_value_Rhoe(rho2_R, e2_R);
118167
const auto c2_R = this->EOS_phase2.c_value_RhoP(rho2_R, p2_R);
168+
#ifdef VERBOSE_FLUX
169+
if(std::isnan(c2_R)) {
170+
throw std::runtime_error(std::string("NaN speed of sound phase 2 right state"));
171+
}
172+
#endif
119173

120174
/*--- Compute the flux ---*/
121175
const auto lambda = std::max(std::max(std::abs(vel1_L_d) + c1_L, std::abs(vel1_R_d) + c1_R),

include/schemes/non_conservative_flux.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
#pragma once
88

9+
#include "flux_base.hpp"
10+
911
namespace samurai {
1012
/**
1113
* Implementation of the non-conservative flux

0 commit comments

Comments
 (0)