[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
rrk_numerical_entropy_conservation_check.cpp
1 #include "rrk_numerical_entropy_conservation_check.h"
2 #include "flow_solver/flow_solver_factory.h"
3 #include "flow_solver/flow_solver_cases/periodic_1D_unsteady.h"
4 #include "flow_solver/flow_solver_cases/periodic_turbulence.h"
5 #include "physics/exact_solutions/exact_solution.h"
6 #include "cmath"
7 
8 namespace PHiLiP {
9 namespace Tests {
10 
11 template <int dim, int nstate>
13  const PHiLiP::Parameters::AllParameters *const parameters_input,
14  const dealii::ParameterHandler &parameter_handler_input)
15  : TestsBase::TestsBase(parameters_input),
16  parameter_handler(parameter_handler_input)
17 {}
18 
19 template <int dim, int nstate>
21 {
23 
24  parameters.ode_solver_param.initial_time_step*=time_step_size_factor;
25  parameters.flow_solver_param.courant_friedrichs_lewy_number*=time_step_size_factor;
26 
27  using ODESolverEnum = Parameters::ODESolverParam::ODESolverEnum;
28  if (use_rrk) {parameters.ode_solver_param.ode_solver_type = ODESolverEnum::rrk_explicit_solver;}
29  else {parameters.ode_solver_param.ode_solver_type = ODESolverEnum::runge_kutta_solver;}
30 
31  return parameters;
32 }
33 
34 template <int dim, int nstate>
36  const std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> &flow_solver,
37  const double initial_numerical_entropy,
38  const double final_time_actual,
39  bool expect_conservation
40  ) const{
41 
42  //pointer to flow_solver_case for computing numerical_entropy
43  // Using PHILIP_DIM as an indicator for whether the test is using Burgers, in
44  // which case we want to use Periodic1DUnsteady, or Euler, in which case we
45  // want to use PeriodicTurbulence
46 #if PHILIP_DIM==1
47  std::shared_ptr<FlowSolver::Periodic1DUnsteady<dim, nstate>> flow_solver_case = std::dynamic_pointer_cast<FlowSolver::Periodic1DUnsteady<dim, nstate>>(flow_solver->flow_solver_case);
48 #else
49  std::shared_ptr<FlowSolver::PeriodicTurbulence<dim, nstate>> flow_solver_case = std::dynamic_pointer_cast<FlowSolver::PeriodicTurbulence<dim, nstate>>(flow_solver->flow_solver_case);
50 #endif
51 
52  const double final_numerical_entropy = flow_solver_case->get_numerical_entropy(flow_solver->dg);
53  const double numerical_entropy_change = abs(initial_numerical_entropy-final_numerical_entropy);
54  pcout << "At end time t = " << final_time_actual << ", numerical entropy change at end was " << std::fixed << std::setprecision(16) << numerical_entropy_change <<std::endl;
55  if (expect_conservation && (numerical_entropy_change< 1E-13)){
56  pcout << "Numerical entropy was conserved, as expected." << std::endl;
57  return 0; //pass test
58  } else if (!expect_conservation && (numerical_entropy_change > 1E-13)){
59  pcout << "Numerical entropy was NOT conserved, as expected." << std::endl;
60  return 0; //pass test
61  }else if (expect_conservation && (numerical_entropy_change > 1E-13)){
62  pcout << "Numerical entropy was NOT conserved, but was expected to be conserved." << std::endl;
63  pcout << " Unexpected result! Test failing." << std::endl;
64  return 1; //fail test
65  }else{
66  pcout << "Numerical entropy was conserved, but was expected NOT to be conserved." << std::endl;
67  pcout << " Unexpected result! Test failing." << std::endl;
68  return 1; //fail test (included for completeness, but not expected to be used)
69  }
70 }
71 
72 template <int dim, int nstate>
74  const Parameters::AllParameters params,
75  const double numerical_entropy_initial,
76  bool expect_conservation
77  ) const
78 {
79  std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> flow_solver = FlowSolver::FlowSolverFactory<dim,nstate>::select_flow_case(&params, parameter_handler);
80  static_cast<void>(flow_solver->run());
81  const double final_time_actual = flow_solver->ode_solver->current_time;
82  int failed_this_calculation = compare_numerical_entropy_to_initial(flow_solver, numerical_entropy_initial, final_time_actual, expect_conservation);
83  return failed_this_calculation;
84 }
85 
86 template <int dim, int nstate>
88 {
89 
90  double final_time = this->all_parameters->flow_solver_param.final_time;
91  double time_step_large = this->all_parameters->ode_solver_param.initial_time_step;
92  double time_step_reduction_factor = 1E-2;
93 
94  int n_steps = round(final_time/time_step_large);
95  if (n_steps * time_step_large!= final_time){
96  pcout << "WARNING: final_time is not evenly divisible by initial_time_step!" << std::endl
97  << "Remainder is " << fmod(final_time, time_step_large)
98  << ". Consider modifying parameters." << std::endl;
99  }
100 
101  int testfail = 0;
102  int failed_this_calculation = 0;
103 
104  pcout << "\n\n-------------------------------------------------------------" << std::endl;
105  pcout << " Calculating initial numerical entropy..." << std::endl;
106  pcout << "-------------------------------------------------------------" << std::endl;
107  std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> flow_solver = FlowSolver::FlowSolverFactory<dim,nstate>::select_flow_case((this->all_parameters), parameter_handler);
108 
109  // Using PHILIP_DIM as an indicator for whether the test is using Burgers, in
110  // which case we want to use Periodic1DUnsteady, or Euler, in which case we
111  // want to use PeriodicTurbulence
112 #if PHILIP_DIM==1
113  std::shared_ptr<FlowSolver::Periodic1DUnsteady<dim, nstate>> flow_solver_case = std::dynamic_pointer_cast<FlowSolver::Periodic1DUnsteady<dim, nstate>>(flow_solver->flow_solver_case);
114 #else
115  std::shared_ptr<FlowSolver::PeriodicTurbulence<dim, nstate>> flow_solver_case = std::dynamic_pointer_cast<FlowSolver::PeriodicTurbulence<dim, nstate>>(flow_solver->flow_solver_case);
116 #endif
117 
118  const double numerical_entropy_initial = flow_solver_case->get_numerical_entropy(flow_solver->dg); //no need to run as ode_solver is allocated during construction
119  pcout << " Initial numerical_entropy : " << numerical_entropy_initial << std::endl;
120 
121  // Run four main tests
122  pcout << "\n\n-------------------------------------------------------------" << std::endl;
123  pcout << " Using large timestep and RRK" << std::endl;
124  pcout << "-------------------------------------------------------------" << std::endl;
125 
126  const Parameters::AllParameters params_large_rrk = reinit_params(true, 1.0);
127  failed_this_calculation = get_numerical_entropy_and_compare_to_initial(params_large_rrk,
128  numerical_entropy_initial,
129  true); //expect_conservation = true
130  if (failed_this_calculation) testfail = 1;
131 
132  pcout << "\n\n-------------------------------------------------------------" << std::endl;
133  pcout << " Using large timestep without RRK" << std::endl;
134  pcout << "-------------------------------------------------------------" << std::endl;
135 
136  const Parameters::AllParameters params_large_norrk = reinit_params(false, 1.0);
137  failed_this_calculation = get_numerical_entropy_and_compare_to_initial(params_large_norrk,
138  numerical_entropy_initial,
139  false); //expect_conservation = false
140  if (failed_this_calculation) testfail = 1;
141 
142  pcout << "\n\n-------------------------------------------------------------" << std::endl;
143  pcout << " Using small timestep, reducing by " << time_step_reduction_factor << " and RRK" << std::endl;
144  pcout << "-------------------------------------------------------------" << std::endl;
145 
146  const Parameters::AllParameters params_small_rrk = reinit_params(true, time_step_reduction_factor);
147  failed_this_calculation = get_numerical_entropy_and_compare_to_initial(params_small_rrk,
148  numerical_entropy_initial,
149  true); //expect_conservation = true
150  if (failed_this_calculation) testfail = 1;
151 
152  pcout << "\n\n-------------------------------------------------------------" << std::endl;
153  pcout << " Using small timestep, reducing by " << time_step_reduction_factor << " without RRK" << std::endl;
154  pcout << "-------------------------------------------------------------" << std::endl;
155 
156  const Parameters::AllParameters params_small_norrk = reinit_params(false, time_step_reduction_factor);
157  failed_this_calculation = get_numerical_entropy_and_compare_to_initial(params_small_norrk,
158  numerical_entropy_initial,
159  true); //expect_conservation = true
160  if (failed_this_calculation) testfail = 1;
161 
162  return testfail;
163 }
164 #if PHILIP_DIM == 1
166 #elif PHILIP_DIM == 3
168 #endif
169 } // Tests namespace
170 } // PHiLiP namespace
double final_time
Final solution time.
double courant_friedrichs_lewy_number
Courant-Friedrich-Lewy (CFL) number for constant time step.
FlowSolverParam flow_solver_param
Contains the parameters for simulation cases (flow solver test)
Selects which flow case to simulate.
Definition: flow_solver.h:64
Files for the baseline physics.
Definition: ADTypes.hpp:10
double get_numerical_entropy(const std::shared_ptr< DGBase< dim, double >>) const
Retrieves cumulative_numerical_entropy_change_FRcorrected.
const dealii::ParameterHandler & parameter_handler
Parameter handler for storing the .prm file being ran.
Main parameter class that contains the various other sub-parameter classes.
int compare_numerical_entropy_to_initial(const std::unique_ptr< FlowSolver::FlowSolver< dim, nstate >> &flow_solver, const double initial_numerical_entropy, const double final_time_actual, bool expect_conservation) const
Compare the numerical_entropy after flow simulation to initial, and return test fail int...
ODESolverParam ode_solver_param
Contains parameters for ODE solver.
double initial_time_step
Time step used in ODE solver.
static std::unique_ptr< FlowSolver< dim, nstate > > select_flow_case(const Parameters::AllParameters *const parameters_input, const dealii::ParameterHandler &parameter_handler_input)
Factory to return the correct flow solver given input file.
const Parameters::AllParameters *const all_parameters
Pointer to all parameters.
Definition: tests.h:20
int get_numerical_entropy_and_compare_to_initial(const Parameters::AllParameters params, const double numerical_entropy_initial, bool expect_conservation) const
runs flow solver. Returns 0 (pass) or 1 (fail) based on numerical_entropy conservation of calculation...
RRKNumericalEntropyConservationCheck(const Parameters::AllParameters *const parameters_input, const dealii::ParameterHandler &parameter_handler_input)
Constructor.
Parameters::AllParameters reinit_params(bool use_rrk, double time_step_size) const
Reinitialize parameters. Necessary because all_parameters is constant.
ODESolverEnum ode_solver_type
ODE solver type.
dealii::ConditionalOStream pcout
ConditionalOStream.
Definition: tests.h:45
Verify numerical_entropy conservation for inviscid Burgers using split form and RRK.
Base class of all the tests.
Definition: tests.h:17