queso-0.56.0
StatisticalInverseProblemOptions.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 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 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
26 #include <boost/program_options.hpp>
27 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
28 
29 #include <queso/Defines.h>
30 #include <queso/StatisticalInverseProblemOptions.h>
31 #include <queso/Miscellaneous.h>
32 
33 // -------------------------------------------------
34 // SipOptionsValues--------------------------
35 // -------------------------------------------------
36 
37 namespace QUESO {
38 
39 // Default constructor -----------------------------
41  :
42  m_prefix("ip_"),
43  m_help(UQ_SIP_HELP),
44  m_computeSolution (UQ_SIP_COMPUTE_SOLUTION_ODV ),
45  m_dataOutputFileName (UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV),
46  m_seedWithMAPEstimator(UQ_SIP_SEEDWITHMAPESTIMATOR),
47  m_useOptimizerMonitor(UQ_SIP_USEOPTIMIZERMONITOR),
48 //m_dataOutputAllowedSet(),
49 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
50  m_parser(NULL),
51 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
52  m_option_help (m_prefix + "help" ),
53  m_option_computeSolution (m_prefix + "computeSolution" ),
54  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
55  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
56 #ifdef UQ_SIP_READS_SOLVER_OPTION
57  m_option_solver (m_prefix + "solver" ),
58  m_solverString (UQ_SIP_SOLVER_ODV)
59 #endif
60  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
61  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
62 {
63 }
64 
66  prefix)
67  :
68  m_prefix((std::string)(prefix) + "ip_"),
69  m_help(UQ_SIP_HELP),
70  m_computeSolution (UQ_SIP_COMPUTE_SOLUTION_ODV ),
71  m_dataOutputFileName (UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV),
72  m_seedWithMAPEstimator(UQ_SIP_SEEDWITHMAPESTIMATOR),
73  m_useOptimizerMonitor(UQ_SIP_USEOPTIMIZERMONITOR),
74 //m_dataOutputAllowedSet(),
75 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
76  m_parser(new BoostInputOptionsParser(env->optionsInputFileName())),
77 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
78  m_option_help (m_prefix + "help" ),
79  m_option_computeSolution (m_prefix + "computeSolution" ),
80  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
81  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
82 #ifdef UQ_SIP_READS_SOLVER_OPTION
83  m_option_solver (m_prefix + "solver" ),
84  m_solverString (UQ_SIP_SOLVER_ODV)
85 #endif
86  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
87  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
88 {
89 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
90  m_parser->registerOption<std::string>(m_option_help, UQ_SIP_HELP, "produce help message for statistical inverse problem");
91  m_parser->registerOption<bool >(m_option_computeSolution, UQ_SIP_COMPUTE_SOLUTION_ODV , "compute solution process" );
92  m_parser->registerOption<std::string>(m_option_dataOutputFileName, UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV , "name of data output file" );
93  m_parser->registerOption<std::string>(m_option_dataOutputAllowedSet, UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV, "subEnvs that will write to data output file" );
94 #ifdef UQ_SIP_READS_SOLVER_OPTION
95  m_parser->registerOption<std::string>(m_option_solver, UQ_SIP_SOLVER_ODV , "algorithm for calibration" );
96 #endif
97  m_parser->registerOption<bool >(m_option_seedWithMAPEstimator, UQ_SIP_SEEDWITHMAPESTIMATOR , "toggle for seeding chain at MAP" );
98  m_parser->registerOption<bool >(m_option_useOptimizerMonitor, UQ_SIP_USEOPTIMIZERMONITOR , "toggle for using optimizer monitor (prints diagnostics");
99 
101 
102  m_parser->getOption<std::string>(m_option_help, m_help);
106 #ifdef UQ_SIP_READS_SOLVER_OPTION
107  m_parser->getOption<std::string>(m_option_solver, m_solver);
108 #endif
111 #else
115 
116  // UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV is the empty set (string) by default
117  unsigned int size = env->input().vector_variable_size(m_option_dataOutputAllowedSet);
118  for (unsigned int i = 0; i < size; i++) {
119  // We default to empty set, so the default values are actually never
120  // used here
121  unsigned int allowed = env->input()(m_option_dataOutputAllowedSet, i, i);
122  m_dataOutputAllowedSet.insert(allowed);
123  }
124 
125 #ifdef UQ_SIP_READS_SOLVER_OPTION
126  m_solver = env->input()(m_option_solver, UQ_SIP_SOLVER_ODV);
127 #endif
130 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
131 
132  checkOptions();
133 }
134 // Copy constructor - -----------------------------
136 {
137  this->copy(src);
138 }
139 // Destructor ---------------------------------------
141 {
142 }
143 
144 // Set methods --------------------------------------
147 {
148  this->copy(rhs);
149  return *this;
150 }
151 // Private methods-----------------------------------
152 void
154 {
155  // Nothing
156 }
157 
158 void
160 {
164 #ifdef UQ_SIP_READS_SOLVER_OPTION
165  m_solverString = src.m_solverString;
166 #endif
169 }
170 
171 std::ostream &
172 operator<<(std::ostream& os, const SipOptionsValues & obj)
173 {
174  os << "\n" << obj.m_option_computeSolution << " = " << obj.m_computeSolution
175  << "\n" << obj.m_option_dataOutputFileName << " = " << obj.m_dataOutputFileName;
176  os << "\n" << obj.m_option_dataOutputAllowedSet << " = ";
177  for (std::set<unsigned int>::iterator setIt = obj.m_dataOutputAllowedSet.begin(); setIt != obj.m_dataOutputAllowedSet.end(); ++setIt) {
178  os << *setIt << " ";
179  }
180 #ifdef UQ_SIP_READS_SOLVER_OPTION
181  << "\n" << obj.m_option_solver << " = " << obj.m_solverString
182 #endif
183  os << std::endl;
184  return os;
185 }
186 
187 // -------------------------------------------------
188 // StatisticalInverseProblemOptions----------
189 // -------------------------------------------------
190 
191 // Default constructor -----------------------------
193  const BaseEnvironment& env,
194  const char* prefix)
195 :
196  m_ov (),
197  m_prefix ((std::string)(prefix) + "ip_"),
198  m_env (env),
199 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
200  m_optionsDesc (new boost::program_options::options_description("Statistical Inverse Problem options")),
201 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
202  m_option_help (m_prefix + "help" ),
203  m_option_computeSolution (m_prefix + "computeSolution" ),
204  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
205  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
206 #ifdef UQ_SIP_READS_SOLVER_OPTION
207  m_option_solver (m_prefix + "solver" )
208 #endif
209  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
210  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
211 {
213  queso_require_not_equal_to_msg(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the absence of an options input file"));
214 }
215 
216 // Constructor 2------------------------------------
218  const BaseEnvironment& env,
219  const char* prefix,
220  const SipOptionsValues& alternativeOptionsValues)
221 :
222  m_ov (alternativeOptionsValues),
223  m_prefix ((std::string)(prefix) + "ip_"),
224  m_env (env),
225 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
226  m_optionsDesc (NULL),
227 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
228  m_option_help (m_prefix + "help" ),
229  m_option_computeSolution (m_prefix + "computeSolution" ),
230  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
231  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
232 #ifdef UQ_SIP_READS_SOLVER_OPTION
233  m_option_solver (m_prefix + "solver" )
234 #endif
235  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
236  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
237 {
239 
240  queso_require_equal_to_msg(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"));
241 
242  if (m_env.subDisplayFile() != NULL) {
243  *m_env.subDisplayFile() << "In StatisticalInverseProblemOptions::constructor(2)"
244  << ": after setting values of options with prefix '" << m_prefix
245  << "', state of object is:"
246  << "\n" << *this
247  << std::endl;
248  }
249 }
250 // Destructor --------------------------------------
252 {
254 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
255  if (m_optionsDesc) delete m_optionsDesc;
256 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
257 }
258 
259 // I/O methods --------------------------------------
260 void
262 {
263 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
264  queso_require_msg(m_optionsDesc, "m_optionsDesc variable is NULL");
265 
269 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
270 
271  if (m_env.subDisplayFile() != NULL) {
272  *m_env.subDisplayFile() << "In StatisticalInverseProblemOptions::scanOptionsValues()"
273  << ": after reading values of options with prefix '" << m_prefix
274  << "', state of object is:"
275  << "\n" << *this
276  << std::endl;
277  }
278 
279  return;
280 }
281 
282 // --------------------------------------------------
283 void
285 {
287 
288  os << "\n" << m_option_computeSolution << " = " << m_ov.m_computeSolution
290  os << "\n" << m_option_dataOutputAllowedSet << " = ";
291  for (std::set<unsigned int>::iterator setIt = m_ov.m_dataOutputAllowedSet.begin(); setIt != m_ov.m_dataOutputAllowedSet.end(); ++setIt) {
292  os << *setIt << " ";
293  }
294 #ifdef UQ_SIP_READS_SOLVER_OPTION
295  << "\n" << m_option_solver << " = " << m_ov.m_solverString
296 #endif
299  os << std::endl;
300 
301  return;
302 }
303 
304 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
305 void
306 StatisticalInverseProblemOptions::defineMyOptions(boost::program_options::options_description& optionsDesc) const
307 {
309 
310  optionsDesc.add_options()
311  (m_option_help.c_str(), "produce help message for statistical inverse problem")
312  (m_option_computeSolution.c_str(), boost::program_options::value<bool >()->default_value(UQ_SIP_COMPUTE_SOLUTION_ODV ), "compute solution process" )
313  (m_option_dataOutputFileName.c_str(), boost::program_options::value<std::string>()->default_value(UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV ), "name of data output file" )
314  (m_option_dataOutputAllowedSet.c_str(), boost::program_options::value<std::string>()->default_value(UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV), "subEnvs that will write to data output file" )
315 #ifdef UQ_SIP_READS_SOLVER_OPTION
316  (m_option_solver.c_str(), boost::program_options::value<std::string>()->default_value(UQ_SIP_SOLVER_ODV ), "algorithm for calibration" )
317 #endif
318  (m_option_seedWithMAPEstimator.c_str(), boost::program_options::value<bool>()->default_value(UQ_SIP_SEEDWITHMAPESTIMATOR), "toggle optimisation of posterior pdf before sampling")
319  (m_option_useOptimizerMonitor.c_str(), boost::program_options::value<bool>()->default_value(UQ_SIP_USEOPTIMIZERMONITOR), "toggle printing of optimisation progress when seed with MAP estimator")
320  ;
321 }
322 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
323 
324 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
325 //--------------------------------------------------
326 void
327 StatisticalInverseProblemOptions::getMyOptionValues(boost::program_options::options_description& optionsDesc)
328 {
330 
331  if (m_env.allOptionsMap().count(m_option_help)) {
332  if (m_env.subDisplayFile()) {
333  *m_env.subDisplayFile() << optionsDesc
334  << std::endl;
335  }
336  }
337 
339  m_ov.m_computeSolution = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_computeSolution]).as<bool>();
340  }
341 
343  m_ov.m_dataOutputFileName = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_dataOutputFileName]).as<std::string>();
344  }
345 
348  std::vector<double> tmpAllow(0,0.);
349  std::string inputString = m_env.allOptionsMap()[m_option_dataOutputAllowedSet].as<std::string>();
350  MiscReadDoublesFromString(inputString,tmpAllow);
351 
352  if (tmpAllow.size() > 0) {
353  for (unsigned int i = 0; i < tmpAllow.size(); ++i) {
354  m_ov.m_dataOutputAllowedSet.insert((unsigned int) tmpAllow[i]);
355  }
356  }
357  }
358 
359 #ifdef UQ_SIP_READS_SOLVER_OPTION
360  if (m_env.allOptionsMap().count(m_option_solver)) {
361  m_ov.m_solverString = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_solver]).as<std::string>();
362  }
363 #endif
364 
366  m_ov.m_seedWithMAPEstimator = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_seedWithMAPEstimator]).as<bool>();
367  }
368 
370  m_ov.m_useOptimizerMonitor = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_useOptimizerMonitor]).as<bool>();
371  }
372 }
373 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
374 
375 // --------------------------------------------------
376 // Operator declared outside class definition ------
377 // --------------------------------------------------
378 
379 std::ostream& operator<<(std::ostream& os, const StatisticalInverseProblemOptions& obj)
380 {
382  obj.print(os);
383 
384  return os;
385 }
386 
387 } // End namespace QUESO
This class provides options for a Statistical Inverse Problem if no input file is available...
const GetPot & input() const
The GetPot input file parser.
Definition: Environment.C:1148
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
SipOptionsValues & operator=(const SipOptionsValues &rhs)
Assignment operator; it copies rhs to this.
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
Definition: Environment.C:353
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:197
This class reads option values for a Statistical Inverse Problem from an input file.
unsigned vector_variable_size(const char *VarName) const
Definition: getpot.h:2532
void getOption(std::string &name, T &value)
Get option name from the parser and set value to the parsed value.
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Definition: Miscellaneous.C:40
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:73
boost::program_options::variables_map & allOptionsMap() const
Definition: Environment.C:385
#define UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV
#define queso_deprecated()
Definition: Defines.h:134
boost::program_options::options_description * m_optionsDesc
void scanInputFileForMyOptions(const boost::program_options::options_description &optionsDesc) const
This method scans the input file provided by the user to QUESO.
Definition: Environment.C:396
void scanInputFile()
This is the method that parses the input file.
StatisticalInverseProblemOptions(const BaseEnvironment &env, const char *prefix)
Constructor: reads options from the input file.
std::set< unsigned int > m_dataOutputAllowedSet
void getMyOptionValues(boost::program_options::options_description &optionsDesc)
Gets the option values of the SIP.
#define UQ_SIP_SEEDWITHMAPESTIMATOR
#define queso_require_not_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:74
void copy(const SipOptionsValues &src)
Copies the option values from src to this.
#define UQ_SIP_USEOPTIMIZERMONITOR
void registerOption(std::string name, T defaultValue, std::string description)
Call this to register an option with the parser.
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
#define UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV
void defineMyOptions(boost::program_options::options_description &optionsDesc) const
Define my SIP options as the default options.
void print(std::ostream &os) const
It prints the option values.
std::string m_help
If this string is non-empty, options are print to the output file.
#define UQ_SIP_COMPUTE_SOLUTION_ODV
void scanOptionsValues()
It scans the option values from the options input file.

Generated on Tue Nov 29 2016 10:53:11 for queso-0.56.0 by  doxygen 1.8.5