GRINS-0.8.0
generic_solution_regression.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // GRINS - General Reacting Incompressible Navier-Stokes
5 //
6 // Copyright (C) 2014-2017 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/runner.h"
29 #include "grins/mesh_builder.h"
30 #include "grins/multiphysics_sys.h"
31 
32 //libMesh
33 #include "libmesh/exact_solution.h"
34 
35 void test_error_norm( libMesh::ExactSolution& exact_sol,
36  const std::string& system_name,
37  const std::string& var,
38  const std::string& norm,
39  const double tol,
40  int& return_flag );
41 
42 int main(int argc, char* argv[])
43 {
44  GRINS::Runner grins(argc,argv);
45 
46  const GetPot & command_line = grins.get_command_line();
47 
48  if( !command_line.have_variable("soln-data") )
49  {
50  std::cerr << "ERROR: Must specify solution data on command line with soln-data=<file>." << std::endl;
51  exit(1);
52  }
53 
54  if( !command_line.have_variable("vars") )
55  {
56  std::cerr << "ERROR: Must specify variables on command line with vars='var1 var2'" << std::endl;
57  exit(1);
58  }
59 
60  if( !command_line.have_variable("norms") )
61  {
62  std::cerr << "ERROR: Must specify error norms on command line with norms='L2 H1'" << std::endl;
63  exit(1);
64  }
65 
66  if( !command_line.have_variable("tol") )
67  {
68  std::cerr << "ERROR: Must specify test tolerance on command line with tol=<tol>" << std::endl;
69  exit(1);
70  }
71 
72  const GetPot & inputfile = grins.get_input_file();
73 
74  // Don't flag our command-line-specific variables as UFOs later
75  inputfile.have_variable("soln-data");
76  inputfile.have_variable("vars");
77  inputfile.have_variable("norms");
78  inputfile.have_variable("tol");
79  inputfile.have_variable("qois");
80 
81  // Initialize Simulation
82  grins.init();
83 
84  // Do solve here
85  grins.run();
86 
87  GRINS::Simulation & sim = grins.get_simulation();
88 
89  // Get equation systems to create ExactSolution object
90  GRINS::SharedPtr<libMesh::EquationSystems> es = sim.get_equation_system();
91 
92  // Create Exact solution object and attach exact solution quantities
93  libMesh::ExactSolution exact_sol(*es);
94 
95  libMesh::EquationSystems es_ref( es->get_mesh() );
96 
97  // Filename of file where comparison solution is stashed
98  std::string solution_file = command_line("soln-data", "DIE!");
99  es_ref.read( solution_file );
100 
101  exact_sol.attach_reference_solution( &es_ref );
102 
103  // Now grab the variables for which we want to compare
104  unsigned int n_vars = command_line.vector_variable_size("vars");
105  std::vector<std::string> vars(n_vars);
106  for( unsigned int v = 0; v < n_vars; v++ )
107  {
108  vars[v] = command_line("vars", "DIE!", v);
109  }
110 
111  // Now grab the norms to compute for each variable error
112  unsigned int n_norms = command_line.vector_variable_size("norms");
113  std::vector<std::string> norms(n_norms);
114  for( unsigned int n = 0; n < n_norms; n++ )
115  {
116  norms[n] = command_line("norms", "DIE!", n);
117  if( norms[n] != std::string("L2") &&
118  norms[n] != std::string("H1") )
119  {
120  std::cerr << "ERROR: Invalid norm input " << norms[n] << std::endl
121  << " Valid values are: L2" << std::endl
122  << " H1" << std::endl;
123  }
124  }
125 
126  const std::string& system_name = sim.get_multiphysics_system_name();
127 
128  // Now compute error for each variable
129  for( unsigned int v = 0; v < n_vars; v++ )
130  {
131  exact_sol.compute_error(system_name, vars[v]);
132  }
133 
134  int return_flag = 0;
135 
136  double tol = command_line("tol", 1.0e-10);
137 
138  // Now test error for each variable, for each norm
139  for( unsigned int v = 0; v < n_vars; v++ )
140  {
141  for( unsigned int n = 0; n < n_norms; n++ )
142  {
143  test_error_norm( exact_sol, system_name, vars[v], norms[n], tol, return_flag );
144  }
145  }
146 
147  // Now grab any "gold" QoI values
148  unsigned int n_qoi_vals = command_line.vector_variable_size("qois");
149  for( unsigned int n = 0; n < n_qoi_vals; n++ )
150  {
151 
152  std::cout << "==========================================================" << std::endl
153  << "Checking qoi " << n << " with tol " << tol << "...";
154 
155  libMesh::Number gold_qoi = command_line("qois", libMesh::Number(0), n);
156 
157  libMesh::Number computed_qoi =
158  sim.get_multiphysics_system()->qoi[n];
159 
160  double error = computed_qoi - gold_qoi;
161 
162  if (std::abs(error) > tol)
163  {
164  std::cerr << "Tolerance exceeded for generic regression test!" << std::endl
165  << "tolerance = " << tol << std::endl
166  << "error = " << error << std::endl
167  << "qoi index = " << n << std::endl;
168  return_flag = 1;
169  }
170  else
171  std::cout << "PASSED!" << std::endl
172  << "==========================================================" << std::endl;
173 
174  }
175 
176 
177  return return_flag;
178 }
179 
180 void test_error_norm( libMesh::ExactSolution& exact_sol,
181  const std::string& system_name,
182  const std::string& var,
183  const std::string& norm,
184  const double tol,
185  int& return_flag )
186 {
187  // We don't set return_flag unless we are setting it 1
188  // since this function gets called multiple times and we don't
189  // want to overwrite a previous "fail" (return_flag = 1) with
190  // a "pass" (return_flag = 0)
191 
192  double error = 0.0;
193 
194  std::cout << "==========================================================" << std::endl
195  << "Checking variable " << var << " using error norm " << norm << " with tol " << tol << "...";
196 
197  if( norm == std::string("L2") )
198  {
199  error = exact_sol.l2_error(system_name, var);
200  }
201  else if( norm == std::string("H1") )
202  {
203  error = exact_sol.h1_error(system_name, var);
204  }
205  else
206  {
207  std::cerr << "ERROR: Invalid norm " << norm << std::endl;
208  exit(1);
209  }
210 
211  if( error > tol )
212  {
213  return_flag = 1;
214 
215  std::cerr << "Tolerance exceeded for generic regression test!" << std::endl
216  << "tolerance = " << tol << std::endl
217  << "norm of error = " << error << std::endl
218  << "norm type = " << norm << std::endl
219  << "var = " << var << std::endl;
220  }
221  else
222  {
223  std::cout << "PASSED!" << std::endl
224  << "==========================================================" << std::endl;
225  }
226 
227  return;
228 }
void init()
Initialize the Simulation objects.
Definition: runner.C:53
SharedPtr< libMesh::EquationSystems > get_equation_system()
Definition: simulation.C:393
const GetPot & get_input_file() const
Definition: runner.h:60
const GetPot & get_command_line() const
Definition: runner.h:63
MultiphysicsSystem * get_multiphysics_system()
Definition: simulation.h:179
int main(int argc, char *argv[])
const std::string & get_multiphysics_system_name() const
Definition: simulation.h:185
Class to encapsulate initializing and running GRINS Simulation.
Definition: runner.h:47
void test_error_norm(libMesh::ExactSolution &exact_sol, const std::string &system_name, const std::string &var, const std::string &norm, const double tol, int &return_flag)
void run()
Runs the simulation that was setup at construction time.
Definition: runner.C:104
Simulation & get_simulation()
Definition: runner.h:66

Generated on Tue Dec 19 2017 12:47:29 for GRINS-0.8.0 by  doxygen 1.8.9.1