[P]arallel [Hi]gh-order [Li]brary for [P]DEs  Latest
Parallel High-Order Library for PDEs through hp-adaptive Discontinuous Galerkin methods
taylor_green_vortex_restart_check.cpp
1 #include "taylor_green_vortex_restart_check.h"
2 #include "flow_solver/flow_solver_factory.h"
3 #include "flow_solver/flow_solver_cases/periodic_turbulence.h"
4 #include <deal.II/base/table_handler.h>
5 #include <algorithm>
6 #include <iterator>
7 #include <string>
8 #include <fstream>
9 
10 namespace PHiLiP {
11 namespace Tests {
12 
13 template <int dim, int nstate>
15  const PHiLiP::Parameters::AllParameters *const parameters_input,
16  const dealii::ParameterHandler &parameter_handler_input)
17  : TestsBase::TestsBase(parameters_input)
18  , parameter_handler(parameter_handler_input)
19  , kinetic_energy_expected(parameters_input->flow_solver_param.expected_kinetic_energy_at_final_time)
20 {}
21 
22 template <int dim, int nstate>
24  const bool output_restart_files_input,
25  const bool restart_computation_from_file_input,
26  const double final_time_input,
27  const double initial_time_input,
28  const unsigned int initial_iteration_input,
29  const double initial_desired_time_for_output_solution_every_dt_time_intervals_input,
30  const double initial_time_step_input,
31  const int restart_file_index_input) const {
32 
33  // copy all parameters
35 
36  // change desired parameters based on inputs
37  parameters.flow_solver_param.output_restart_files = output_restart_files_input;
38  parameters.flow_solver_param.restart_computation_from_file = restart_computation_from_file_input;
39  parameters.flow_solver_param.final_time = final_time_input;
40  parameters.ode_solver_param.initial_time = initial_time_input;
41  parameters.ode_solver_param.initial_iteration = initial_iteration_input;
42  parameters.ode_solver_param.initial_desired_time_for_output_solution_every_dt_time_intervals = initial_desired_time_for_output_solution_every_dt_time_intervals_input;
43  parameters.ode_solver_param.initial_time_step = initial_time_step_input;
44  parameters.flow_solver_param.restart_file_index = restart_file_index_input;
45 
46  return parameters;
47 }
48 
49 template<typename InputIterator1, typename InputIterator2>
50 bool range_equal(InputIterator1 first1, InputIterator1 last1,
51  InputIterator2 first2, InputIterator2 last2)
52 {
53  // Code reference: https://stackoverflow.com/questions/15118661/in-c-whats-the-fastest-way-to-tell-whether-two-string-or-binary-files-are-di
54  while(first1 != last1 && first2 != last2)
55  {
56  if(*first1 != *first2) return false;
57  ++first1;
58  ++first2;
59  }
60  return (first1 == last1) && (first2 == last2);
61 }
62 
63 bool compare_files(const std::string& filename1, const std::string& filename2)
64 {
65  // Code reference: https://stackoverflow.com/questions/15118661/in-c-whats-the-fastest-way-to-tell-whether-two-string-or-binary-files-are-di
66  std::ifstream file1(filename1);
67  std::ifstream file2(filename2);
68 
69  std::istreambuf_iterator<char> begin1(file1);
70  std::istreambuf_iterator<char> begin2(file2);
71 
72  std::istreambuf_iterator<char> end;
73 
74  return range_equal(begin1, end, begin2, end);
75 }
76 
77 template <int dim, int nstate>
79 {
80  const double time_at_which_we_stop_the_run = 6.2831853072000017e-03;// chosen from running test MPI_VISCOUS_TAYLOR_GREEN_VORTEX_ENERGY_CHECK_QUICK
81  const int restart_file_index = 4;
82  const int initial_iteration_restart = restart_file_index; // assumes output mod for restart files is 1
83  const double time_at_which_the_run_is_complete = this->all_parameters->flow_solver_param.final_time;
84  Parameters::AllParameters params_incomplete_run = reinit_params(true,false,time_at_which_we_stop_the_run);
85 
86  // Integrate to time at which we stop the run
87  std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> flow_solver_incomplete_run = FlowSolver::FlowSolverFactory<dim,nstate>::select_flow_case(&params_incomplete_run, parameter_handler);
88  static_cast<void>(flow_solver_incomplete_run->run());
89 
90  const double time_step_at_stop_time = flow_solver_incomplete_run->flow_solver_case->get_constant_time_step(flow_solver_incomplete_run->dg);
91  const double desired_time_for_output_solution_every_dt_time_intervals_at_stop_time = flow_solver_incomplete_run->ode_solver->current_desired_time_for_output_solution_every_dt_time_intervals;
92  Parameters::AllParameters params_restart_to_complete_run = reinit_params(false,true,
93  time_at_which_the_run_is_complete,
94  time_at_which_we_stop_the_run,
95  initial_iteration_restart,
96  desired_time_for_output_solution_every_dt_time_intervals_at_stop_time,
97  time_step_at_stop_time,
98  restart_file_index);
99 
100  // INLINE SUB-TEST: Check whether the initialize_data_table_from_file() function in flow solver is working correctly
101  if(this->mpi_rank==0) {
102  std::shared_ptr<dealii::TableHandler> unsteady_data_table = std::make_shared<dealii::TableHandler>();
103  const std::string file_read = params_incomplete_run.flow_solver_param.restart_files_directory_name+std::string("/")+params_incomplete_run.flow_solver_param.unsteady_data_table_filename+std::string("-")+flow_solver_incomplete_run->get_restart_filename_without_extension(restart_file_index)+std::string(".txt");
104  flow_solver_incomplete_run->initialize_data_table_from_file(file_read,unsteady_data_table);
105  const std::string file_write = "read_table_check.txt";
106  std::ofstream unsteady_data_table_file(file_write);
107  unsteady_data_table->write_text(unsteady_data_table_file);
108  // check if files are the same (i.e. if the tables are the same)
109  bool files_are_same = compare_files(file_read,file_write);
110  if(!files_are_same) {
111  pcout << "\n Error: initialize_data_table_from_file() failed." << std::endl;
112  return 1;
113  }
114  else {
115  pcout << "\n Sub-test for initialize_data_table_from_file() passed, continuing test..." << std::endl;
116  }
117  } // END
118 
119  // Integrate to final time by restarting from where we stopped
120  std::unique_ptr<FlowSolver::FlowSolver<dim,nstate>> flow_solver_restart_to_complete_run = FlowSolver::FlowSolverFactory<dim,nstate>::select_flow_case(&params_restart_to_complete_run, parameter_handler);
121  static_cast<void>(flow_solver_restart_to_complete_run->run());
122 
123  // Compute kinetic energy at final time achieved by restarting the computation
124  std::unique_ptr<FlowSolver::PeriodicTurbulence<dim, nstate>> flow_solver_case = std::make_unique<FlowSolver::PeriodicTurbulence<dim,nstate>>(this->all_parameters);
125  flow_solver_case->compute_and_update_integrated_quantities(*(flow_solver_restart_to_complete_run->dg));
126  const double kinetic_energy_computed = flow_solver_case->get_integrated_kinetic_energy();
127 
128  const double relative_error = abs(kinetic_energy_computed - kinetic_energy_expected)/kinetic_energy_expected;
129  if (relative_error > 1.0e-10) {
130  pcout << "Computed kinetic energy is not within specified tolerance with respect to expected kinetic energy." << std::endl;
131  return 1;
132  }
133  pcout << " Test passed, computed kinetic energy is within specified tolerance." << std::endl;
134  return 0;
135 }
136 
137 #if PHILIP_DIM==3
139 #endif
140 } // Tests namespace
141 } // PHiLiP namespace
double final_time
Final solution time.
double initial_time
Initial time at which we initialize the ODE solver with.
const double kinetic_energy_expected
Expected kinetic energy at final time.
FlowSolverParam flow_solver_param
Contains the parameters for simulation cases (flow solver test)
bool restart_computation_from_file
Restart computation from restart file.
bool output_restart_files
Output the restart files.
Files for the baseline physics.
Definition: ADTypes.hpp:10
unsigned int initial_iteration
Initial iteration at which we initialize the ODE solver with.
Main parameter class that contains the various other sub-parameter classes.
Parameters::AllParameters reinit_params(const bool output_restart_files_input, const bool restart_computation_from_file_input, const double final_time_input, const double initial_time_input=0.0, const unsigned int initial_iteration_input=0, const double initial_desired_time_for_output_solution_every_dt_time_intervals_input=0.0, const double initial_time_step_input=0.0, const int restart_file_index=0) const
Renitialize parameters, necessary because parameters created for the test are constant.
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
unsigned int restart_file_index
Index of desired restart file for restarting the computation from.
const int mpi_rank
MPI rank.
Definition: tests.h:40
const dealii::ParameterHandler & parameter_handler
Parameter handler for storing the .prm file being ran.
dealii::ConditionalOStream pcout
ConditionalOStream.
Definition: tests.h:45
TaylorGreenVortexRestartCheck(const Parameters::AllParameters *const parameters_input, const dealii::ParameterHandler &parameter_handler_input)
Constructor.
std::string restart_files_directory_name
Name of directory for writing and reading restart files.
Base class of all the tests.
Definition: tests.h:17