GRINS-0.7.0
generic_amr_testing_app.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // GRINS - General Reacting Incompressible Navier-Stokes
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Roy H. Stogner
7 // Copyright (C) 2010-2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include "grins_config.h"
26 
27 // GRINS
28 #include "grins/grins_enums.h"
29 #include "grins/mesh_builder.h"
30 #include "grins/simulation.h"
32 #include "grins/multiphysics_sys.h"
33 
34 //libMesh
35 #include "libmesh/exact_solution.h"
36 #include "libmesh/parsed_function.h"
37 #include "libmesh/composite_function.h"
38 
39 int test_error_norm( libMesh::ExactSolution& exact_sol,
40  const std::string& system_name,
41  const std::string& var,
42  const std::string& norm,
43  const libMesh::Real exact_error,
44  const double tol );
45 
46 int main(int argc, char* argv[])
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_prefix") )
58  {
59  std::cerr << "ERROR: Must specify solution test data filename prefix on command line with test_data_prefix=<string>."
60  << std::endl;
61  exit(1);
62  }
63 
64  if( !command_line.have_variable("mesh_data_prefix") )
65  {
66  std::cerr << "ERROR: Must specify solution mesh data filename prefix on command line with test_data_prefix=<string>."
67  << std::endl;
68  exit(1);
69  }
70 
71  if( !command_line.have_variable("gold_data_prefix") )
72  {
73  std::cerr << "ERROR: Must specify solution gold data filename prefix on command line with gold_data_prefix=<string>."
74  << std::endl;
75  exit(1);
76  }
77 
78  if( !command_line.have_variable("gold_mesh_prefix") )
79  {
80  std::cerr << "ERROR: Must specify gold mesh data filename prefix on command line with gold_mesh_prefix=<string>."
81  << std::endl;
82  exit(1);
83  }
84 
85  if( !command_line.have_variable("vars") )
86  {
87  std::cerr << "ERROR: Must specify variables on command line with vars='var1 var2'"
88  << std::endl;
89  exit(1);
90  }
91 
92  if( !command_line.have_variable("norms") )
93  {
94  std::cerr << "ERROR: Must specify error norms on command line with norms='L2 H1'"
95  << std::endl;
96  exit(1);
97  }
98 
99  if( !command_line.have_variable("tol") )
100  {
101  std::cerr << "ERROR: Must specify test tolerance on command line with tol=<double>"
102  << std::endl;
103  exit(1);
104  }
105 
106  if( !command_line.have_variable("n_steps") )
107  {
108  std::cerr << "ERROR: Must specify n_steps on command line with n_steps=<int>"
109  << std::endl;
110  exit(1);
111  }
112 
113 
114 
115  // Grab the variables for which we want to compare the errors
116  unsigned int n_vars = command_line.vector_variable_size("vars");
117  std::vector<std::string> vars(n_vars);
118  for( unsigned int v = 0; v < n_vars; v++ )
119  {
120  vars[v] = command_line("vars", "DIE!", v);
121  }
122 
123  // Now grab the norms to compute for each variable error
124  unsigned int n_norms = command_line.vector_variable_size("norms");
125  std::vector<std::string> norms(n_norms);
126  for( unsigned int n = 0; n < n_norms; n++ )
127  {
128  norms[n] = command_line("norms", "DIE!", n);
129  if( norms[n] != std::string("L2") )
130  {
131  std::cerr << "ERROR: Invalid norm input " << norms[n] << std::endl
132  << " Valid values are: L2" << std::endl;
133  }
134  }
135 
136  // Grab the input filename
137  std::string input_filename = command_line("input", "DIE!");
138 
139  // Check that the input file is actually there
140  {
141  std::ifstream i(input_filename.c_str());
142  if (!i)
143  {
144  std::cerr << "Error: Could not read from input file "
145  << input_filename << std::endl;
146  exit(1);
147  }
148  }
149 
150  // Create our GetPot object.
151  GetPot input( input_filename );
152 
153  // Initialize libMesh library
154  libMesh::LibMeshInit libmesh_init(argc, argv);
155 
156  std::string test_data_prefix = command_line("test_data_prefix", "DIE!" );
157  std::string mesh_data_prefix = command_line("mesh_data_prefix", "DIE!" );
158 
159  std::string gold_data_prefix = command_line("gold_data_prefix", "DIE!" );
160  std::string gold_mesh_prefix = command_line("gold_mesh_prefix", "DIE!" );
161 
162  unsigned int n_steps = command_line("n_steps", 0);
163 
164  int return_flag = 0;
165 
166  std::string system_name = "GRINS-TEST";
167 
168  for( unsigned int s = 0; s < n_steps; s++ )
169  {
170  std::stringstream step_string;
171  step_string << s;
172 
173  //FIXME: Need to support different input formats for meshes
174  std::string mesh_filename = mesh_data_prefix+"."+step_string.str()+"_mesh.xda";
175  {
176  std::ifstream i( mesh_filename.c_str());
177  if (!i)
178  {
179  std::cerr << "Error: Could not read from mesh_data file "
180  << mesh_filename << std::endl;
181  exit(1);
182  }
183  }
184 
185  libMesh::Mesh mesh(libmesh_init.comm());
186  mesh.read(mesh_filename);
187 
188  // This needs to match the read counter-part in GRINS::Simulation
189  //FIXME: Need to support different input formats for restarts
190  std::string test_data = test_data_prefix+"."+step_string.str()+".xdr";
191 
192  {
193  std::ifstream i(test_data.c_str());
194  if (!i)
195  {
196  std::cerr << "Error: Could not read from test_data file "
197  << test_data << std::endl;
198  exit(1);
199  }
200  }
201 
202  libMesh::EquationSystems es(mesh);
203  es.read(test_data,
204  GRINSEnums::DECODE,
205  libMesh::EquationSystems::READ_HEADER |
206  libMesh::EquationSystems::READ_DATA |
207  libMesh::EquationSystems::READ_ADDITIONAL_DATA);
208 
209  // Now grab the "gold" mesh and data
210  //FIXME: Need to support different input formats for meshes
211  std::string gold_mesh_filename = gold_mesh_prefix+"_mesh."+step_string.str()+".exo";
212  {
213  std::ifstream i( gold_mesh_filename.c_str());
214  if (!i)
215  {
216  std::cerr << "Error: Could not read from gold_mesh file "
217  << gold_mesh_filename << std::endl;
218  exit(1);
219  }
220  }
221 
222  libMesh::Mesh gold_mesh(libmesh_init.comm());
223  gold_mesh.read(gold_mesh_filename);
224 
225  // This needs to match the read counter-part in GRINS::Simulation
226  //FIXME: Need to support different input formats for restarts
227  std::string gold_data = gold_data_prefix+"."+step_string.str()+".xdr";
228 
229  {
230  std::ifstream i(gold_data.c_str());
231  if (!i)
232  {
233  std::cerr << "Error: Could not read from test_data file "
234  << gold_data << std::endl;
235  exit(1);
236  }
237  }
238 
239  libMesh::EquationSystems es_gold(gold_mesh);
240  es_gold.read(gold_data,
241  GRINSEnums::DECODE,
242  libMesh::EquationSystems::READ_HEADER |
243  libMesh::EquationSystems::READ_DATA |
244  libMesh::EquationSystems::READ_ADDITIONAL_DATA);
245 
246  // Create Exact solution object and attach "gold" EquationSystems
247  libMesh::ExactSolution exact_sol(es);
248 
249  exact_sol.attach_reference_solution( &es_gold );
250 
251  // Now compute error for each variable
252  for( unsigned int v = 0; v < n_vars; v++ )
253  {
254  exact_sol.compute_error(system_name, vars[v]);
255  }
256 
257  double tol = command_line("tol", 1.0e-10);
258 
259  return_flag = 0;
260  int test_flag = 0;
261 
262  // We're just diffing against gold data, so error should be 0
263  double exact_error = 0.0;
264 
265  // Now test error for each variable, for each norm
266  for( unsigned int v = 0; v < n_vars; v++ )
267  {
268  for( unsigned int n = 0; n < n_norms; n++ )
269  {
270  test_flag = test_error_norm( exact_sol, system_name, vars[v], norms[n], exact_error, tol );
271 
272  if( test_flag == 1 )
273  return_flag = 1;
274  }
275  }
276 
277  // Break out if one of the tests failed
278  if( return_flag == 1 )
279  break;
280  }
281 
282  return return_flag;
283 }
284 
285 int test_error_norm( libMesh::ExactSolution& exact_sol,
286  const std::string& system_name,
287  const std::string& var,
288  const std::string& norm,
289  const libMesh::Real exact_error,
290  const double tol )
291 {
292  int return_flag = 0;
293 
294  double error = 0.0;
295 
296  std::cout << "==========================================================" << std::endl
297  << "Checking variable " << var << " using error norm " << norm << " with tol " << tol << "...";
298 
299  if( norm == std::string("L2") )
300  {
301  error = exact_sol.l2_error(system_name, var);
302  }
303  else if( norm == std::string("H1") )
304  {
305  error = exact_sol.h1_error(system_name, var);
306  }
307  else
308  {
309  std::cerr << "ERROR: Invalid norm " << norm << std::endl;
310  exit(1);
311  }
312 
313  if( std::abs(error-exact_error) > tol )
314  {
315  return_flag = 1;
316 
317  std::cerr << "Tolerance exceeded for generic regression test!" << std::endl
318  << "tolerance = " << tol << std::endl
319  << "norm of error = " << error << std::endl
320  << "exact error = " << exact_error << std::endl
321  << "norm type = " << norm << std::endl
322  << "var = " << var << std::endl;
323  }
324  else
325  {
326  std::cout << "PASSED!" << std::endl
327  << "==========================================================" << std::endl;
328  }
329 
330  return return_flag;
331 }
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[])

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