GRINS-0.7.0
Functions
generic_exact_solution_testing_app.C File Reference
#include "grins_config.h"
#include "grins/grins_enums.h"
#include "grins/mesh_builder.h"
#include "grins/simulation.h"
#include "grins/simulation_builder.h"
#include "grins/multiphysics_sys.h"
#include "libmesh/exact_solution.h"
#include "libmesh/parsed_function.h"
#include "libmesh/composite_function.h"
Include dependency graph for generic_exact_solution_testing_app.C:

Go to the source code of this file.

Functions

int test_error_norm (libMesh::ExactSolution &exact_sol, const std::string &system_name, const std::string &var, const std::string &norm, const libMesh::Real exact_error, const double tol)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 46 of file generic_exact_solution_testing_app.C.

References GRINS::MeshBuilder::build(), and test_error_norm().

47 {
48  GetPot command_line(argc,argv);
49 
50  if( !command_line.have_variable("input") )
51  {
52  std::cerr << "ERROR: Must specify input file on command line with input=<file>."
53  << std::endl;
54  exit(1);
55  }
56 
57  if( !command_line.have_variable("test_data") )
58  {
59  std::cerr << "ERROR: Must specify solution test data on command line with test_data=<file>."
60  << std::endl;
61  exit(1);
62  }
63 
64  if( !command_line.have_variable("vars") )
65  {
66  std::cerr << "ERROR: Must specify variables on command line with vars='var1 var2'"
67  << std::endl;
68  exit(1);
69  }
70 
71  if( !command_line.have_variable("norms") )
72  {
73  std::cerr << "ERROR: Must specify error norms on command line with norms='L2 H1'"
74  << std::endl;
75  exit(1);
76  }
77 
78  if( !command_line.have_variable("tol") )
79  {
80  std::cerr << "ERROR: Must specify test tolerance on command line with tol=<tol>"
81  << std::endl;
82  exit(1);
83  }
84 
85 
86 
87  // Grab the variables for which we want to compare the errors
88  unsigned int n_vars = command_line.vector_variable_size("vars");
89  std::vector<std::string> vars(n_vars);
90  for( unsigned int v = 0; v < n_vars; v++ )
91  {
92  vars[v] = command_line("vars", "DIE!", v);
93  }
94 
95  // Now grab the norms to compute for each variable error
96  unsigned int n_norms = command_line.vector_variable_size("norms");
97  std::vector<std::string> norms(n_norms);
98  for( unsigned int n = 0; n < n_norms; n++ )
99  {
100  norms[n] = command_line("norms", "DIE!", n);
101  if( norms[n] != std::string("L2") )
102  {
103  std::cerr << "ERROR: Invalid norm input " << norms[n] << std::endl
104  << " Valid values are: L2" << std::endl;
105  }
106  }
107 
108  // Now that we have the norms, we need to grab the error values for each var, for each norm from the CLI
109  std::vector<std::vector<libMesh::Real> > error_values;
110  error_values.resize(n_vars);
111 
112  for( unsigned int v = 0; v < n_vars; v++ )
113  {
114  std::string var = vars[v];
115 
116  error_values[v].resize(n_norms);
117 
118  for( unsigned int n = 0; n < n_norms; n++ )
119  {
120  std::string norm_cli = var+"_"+norms[n]+"_error";
121 
122  if( !command_line.have_variable(norm_cli) )
123  {
124  std::cerr << "ERROR: Must specify "+norms[n]+" errors on command line with "+
125  var+"_"+norms[n]+"_error='<error>'" << std::endl;
126  exit(1);
127  }
128 
129  error_values[v][n] = command_line( norm_cli, -1.0 );
130 
131  if( error_values[v][n] < 0.0 )
132  {
133  std::cerr << "ERROR: Invalid value of error --- norms should be positive!" << std::endl
134  << " Found error value: " << error_values[v][n] << std::endl;
135  exit(1);
136  }
137  }
138  }
139 
140  // Grab the input filename
141  std::string input_filename = command_line("input", "DIE!");
142 
143  // Check that the input file is actually there
144  {
145  std::ifstream i(input_filename.c_str());
146  if (!i)
147  {
148  std::cerr << "Error: Could not read from input file "
149  << input_filename << std::endl;
150  exit(1);
151  }
152  }
153 
154  // Create our GetPot object.
155  GetPot input( input_filename );
156 
157  // Initialize libMesh library
158  libMesh::LibMeshInit libmesh_init(argc, argv);
159 
160  GRINS::MeshBuilder mesh_builder;
161  GRINS::SharedPtr<libMesh::UnstructuredMesh> mesh = mesh_builder.build(input,libmesh_init.comm());
162 
163  libMesh::EquationSystems es(*mesh);
164 
165  // This needs to match the read counter-part in GRINS::Simulation
166  std::string test_data = command_line("test_data", "DIE!" );
167 
168  {
169  std::ifstream i(test_data.c_str());
170  if (!i)
171  {
172  std::cerr << "Error: Could not read from test_data file "
173  << test_data << std::endl;
174  exit(1);
175  }
176  }
177 
178  es.read(test_data,
179  libMesh::EquationSystems::READ_HEADER |
180  libMesh::EquationSystems::READ_DATA |
181  libMesh::EquationSystems::READ_ADDITIONAL_DATA);
182 
183  std::string system_name = "GRINS-TEST";
184  libMesh::System& system = es.get_system(system_name);
185 
186  // Create Exact solution object and attach exact solution quantities
187  libMesh::ExactSolution exact_sol(es);
188 
189  // Grab the exact solution expression for each variable
190  // We'll directly construct the ParsedFunction for each of the variable exact solutions provided
191  // They are all packed together in the CompositeFunction
192  // We need to do this here because we need the variable index from the equation system
193  {
195 
196  for( unsigned int v = 0; v < n_vars; v++ )
197  {
198  std::string var = vars[v];
199  std::string soln_cli = var+"_exact_soln";
200 
201  if( !command_line.have_variable(soln_cli) )
202  {
203  std::cerr << "ERROR: Must specify the exact solution for the variable"+var+"on" << std::endl
204  << " the command line with "+soln_cli+"=<expression>"
205  << std::endl;
206  exit(1);
207  }
208 
209  unsigned int var_index = system.variable_number(var);
210 
211  std::string parsed_soln = command_line(soln_cli,"NULL");
212 
213  std::vector<unsigned int> index_map(1,var_index);
214 
215  exact_sols.attach_subfunction( libMesh::ParsedFunction<libMesh::Real>(parsed_soln), index_map );
216  }
217 
218  // This is assuming there's only 1 system
219  exact_sol.attach_exact_value(0, &exact_sols);
220  }
221 
222  // Now compute error for each variable
223  for( unsigned int v = 0; v < n_vars; v++ )
224  {
225  exact_sol.compute_error(system_name, vars[v]);
226  }
227 
228  double tol = command_line("tol", 1.0e-10);
229 
230  int return_flag = 0;
231  int test_flag = 0;
232 
233  // Now test error for each variable, for each norm
234  for( unsigned int v = 0; v < n_vars; v++ )
235  {
236  for( unsigned int n = 0; n < n_norms; n++ )
237  {
238  test_flag = test_error_norm( exact_sol, system_name, vars[v], norms[n], error_values[v][n], tol );
239 
240  if( test_flag == 1 )
241  return_flag = 1;
242  }
243  }
244 
245  return return_flag;
246 }
SharedPtr< libMesh::UnstructuredMesh > build(const GetPot &input, const libMesh::Parallel::Communicator &comm LIBMESH_CAN_DEFAULT_TO_COMMWORLD)
Builds the libMesh::Mesh according to input options.
Definition: mesh_builder.C:46
int test_error_norm(libMesh::ExactSolution &exact_sol, const std::string &system_name, const std::string &var, const std::string &norm, const libMesh::Real exact_error, const double tol)
int test_error_norm ( libMesh::ExactSolution &  exact_sol,
const std::string &  system_name,
const std::string &  var,
const std::string &  norm,
const libMesh::Real  exact_error,
const double  tol 
)

Definition at line 248 of file generic_exact_solution_testing_app.C.

Referenced by main().

254 {
255  int return_flag = 0;
256 
257  double error = 0.0;
258 
259  std::cout << "==========================================================" << std::endl
260  << "Checking variable " << var << " using error norm " << norm << " with tol " << tol << "...";
261 
262  if( norm == std::string("L2") )
263  {
264  error = exact_sol.l2_error(system_name, var);
265  }
266  else if( norm == std::string("H1") )
267  {
268  error = exact_sol.h1_error(system_name, var);
269  }
270  else
271  {
272  std::cout << "ERROR: Invalid norm " << norm << std::endl;
273  exit(1);
274  }
275 
276  if( std::abs(error-exact_error) > tol )
277  {
278  return_flag = 1;
279 
280  std::cout << "Tolerance exceeded for generic regression test!" << std::endl
281  << "tolerance = " << tol << std::endl
282  << "norm of error = " << error << std::endl
283  << "exact error = " << exact_error << std::endl
284  << "norm type = " << norm << std::endl
285  << "var = " << var << std::endl;
286  }
287  else
288  {
289  std::cout << "PASSED!" << std::endl
290  << "==========================================================" << std::endl;
291  }
292 
293  return return_flag;
294 }

Generated on Thu Jun 2 2016 21:52:29 for GRINS-0.7.0 by  doxygen 1.8.10