GRINS-0.8.0
runner.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 // This class
26 #include "grins/runner.h"
27 
28 // libMesh
29 #include "libmesh/parallel.h"
30 
31 // C++
32 #include <iomanip>
33 #include <sstream>
34 
35 namespace GRINS
36 {
37  Runner::Runner( int argc, char* argv[] )
38  : _initializer(),
39  _sim_builder(),
40  _command_line(argc,argv),
41  _libmesh_init( new libMesh::LibMeshInit(argc,argv) )
42  {
43  this->echo_version_info( libMesh::out, argc, argv );
44 
45  // Grab inputfile name and setup GetPot input file
46  std::string inputfile_name = this->check_and_get_inputfile(argc,argv,_command_line);
47  _inputfile.reset( new GetPot(inputfile_name) );
48 
49  // Allow command line options to override the inputfile
50  _inputfile->parse_command_line(argc,argv);
51  }
52 
53  void Runner::init()
54  {
55  // Initialize Simulation
56  _simulation.reset( new Simulation( *_inputfile,
59  _libmesh_init->comm() ) );
60  }
61 
62  void Runner::echo_version_info( std::ostream & out, int argc, char* argv[] )
63  {
64  out << "==========================================================" << std::endl;
65  out << "GRINS Version: " << GRINS_BUILD_VERSION << std::endl
66  << "libMesh Version: " << LIBMESH_BUILD_VERSION << std::endl
67  << "Running with command:\n";
68 
69  for (int i=0; i != argc; ++i)
70  out << argv[i] << ' ';
71 
72  out << std::endl
73  << "==========================================================" << std::endl;
74  }
75 
76  std::string Runner::check_and_get_inputfile(int argc, char* argv[], GetPot & command_line)
77  {
78  if( argc < 2 )
79  {
80  std::stringstream error_msg;
81  error_msg << "ERROR: Found only 1 command line argument, but was expecting an inputfile name!"
82  << std::endl
83  << " Please specify the name of the input file on the command line as the first" << std::endl
84  << " command line argument or using the '--input <filename>' option." << std::endl;
85  libmesh_error_msg(error_msg.str());
86  }
87 
88  std::string inputfile_name;
89  if( command_line.search("--input") )
90  inputfile_name = command_line.next(std::string("DIE!"));
91  else
92  inputfile_name = argv[1];
93 
94  std::ifstream i(inputfile_name.c_str());
95  if (!i)
96  {
97  std::string error_msg = "Error: Could not read from input file "+inputfile_name+"!\n";
98  libmesh_error_msg(error_msg);
99  }
100 
101  return inputfile_name;
102  }
103 
104  void Runner::run()
105  {
106  // First check for any unused variables
107  bool warning_only = _command_line.search("--warn-only-unused-var");
108 
109  this->check_for_unused_vars(*_inputfile, warning_only );
110 
111  _simulation->run();
112  }
113 
114  void Runner::check_for_unused_vars( const GetPot& input, bool warning_only )
115  {
116  /* Everything should be set up now, so check if there's any unused variables
117  in the input file. If so, then tell the user what they were and error out. */
118  std::vector<std::string> unused_vars = input.unidentified_variables();
119 
120  bool unused_vars_detected = false;
121 
122  // String we use to check if there is a variable in the Materials section
123  std::string mat_string = "Materials/";
124 
125  // If we do have unused variables, make sure they are in the Materials
126  // section since we're allowing those to be present and not used.
127  if( !unused_vars.empty() )
128  {
129  int n_total = unused_vars.size();
130 
131  int n_mats = 0;
132 
133  for( int v = 0; v < n_total; v++ )
134  if( (unused_vars[v]).find(mat_string) != std::string::npos )
135  n_mats += 1;
136 
137  libmesh_assert_greater_equal( n_total, n_mats );
138 
139  if( n_mats < n_total )
140  unused_vars_detected = true;
141  }
142 
143  if( unused_vars_detected )
144  {
145  libMesh::err << "==========================================================" << std::endl;
146  if( warning_only )
147  libMesh::err << "Warning: ";
148  else
149  libMesh::err << "Error: ";
150 
151  libMesh::err << "Found unused variables!" << std::endl;
152 
153  for( std::vector<std::string>::const_iterator it = unused_vars.begin();
154  it != unused_vars.end(); ++it )
155  {
156  // Don't print out any unused Material variables, since we allow these
157  if( (*it).find(mat_string) != std::string::npos )
158  continue;
159 
160  libMesh::err << *it << std::endl;
161  }
162  libMesh::err << "==========================================================" << std::endl;
163 
164  if( !warning_only )
165  libmesh_error();
166  }
167  }
168 
169 } // end namespace GRINS
void echo_version_info(std::ostream &out, int argc, char *argv[])
Echo GRINS, libMesh version info as well as command line.
Definition: runner.C:62
libMesh::UniquePtr< Simulation > _simulation
Definition: runner.h:104
void init()
Initialize the Simulation objects.
Definition: runner.C:53
void check_for_unused_vars(const GetPot &input, bool warning_only)
Check for any unused variables in GetPot input file.
Definition: runner.C:114
SimulationBuilder _sim_builder
Definition: runner.h:96
GRINS namespace.
GetPot _command_line
Definition: runner.h:98
std::string check_and_get_inputfile(int argc, char *argv[], GetPot &command_line)
Check (and error if not found) and then return GetPot input file name.
Definition: runner.C:76
libMesh::UniquePtr< GetPot > _inputfile
Definition: runner.h:102
libMesh::UniquePtr< libMesh::LibMeshInit > _libmesh_init
Definition: runner.h:100
void run()
Runs the simulation that was setup at construction time.
Definition: runner.C:104

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