Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
readparameters.h
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program 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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef READPARAMETERS_H
24#define READPARAMETERS_H
25
26#include <boost/program_options.hpp>
27#include <fstream>
28#include <iomanip>
29#include <iostream>
30#include <limits>
31#include <mpi.h>
32#include <stdint.h>
33#include <string>
34#include <typeinfo>
35#include <vector>
36
37#include "common.h"
38#include "version.h"
39
41public:
42 Readparameters(int cmdargc, char* cmdargv[]);
44
52 static void add(const std::string& name, const std::string& desc, const std::string& defValue) {
53 int rank;
54 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
55 if (rank == MASTER_RANK) {
56 options[name] = "";
57 isOptionParsed[name] = false;
58 descriptions->add_options()(
59 name.c_str(), boost::program_options::value<std::string>(&(options[name]))->default_value(defValue),
60 desc.c_str());
61 }
62 }
63
64 template <typename T> static void add(const std::string& name, const std::string& desc, const T& defValue) {
65 int rank;
66 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
67 if (rank == MASTER_RANK) {
68 std::stringstream ss;
69
70 static constexpr bool n = (std::is_floating_point<T>::value);
71 if (n) {
72 ss << std::setprecision(std::numeric_limits<double>::digits10 + 1) << defValue;
73 } else {
74 ss << defValue;
75 }
76 options[name] = "";
77 isOptionParsed[name] = false;
78 descriptions->add_options()(
79 name.c_str(), boost::program_options::value<std::string>(&(options[name]))->default_value(ss.str()),
80 desc.c_str());
81 }
82 }
83
90 static void get(const std::string& name, std::string& value) {
91 if (options.find(name) != options.end()) { // check if it exists
92 value = options[name];
93 } else {
94 int rank;
95 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
96 if (rank == MASTER_RANK) {
97 std::cerr << __FILE__ << ":" << __LINE__ << " " << name + " not declared using the add() function!" << std::endl;
98 MPI_Abort(MPI_COMM_WORLD, 1);
99 }
100 }
101 }
102
103 static void get(const std::string& name, std::vector<std::string>& value) {
104 if (vectorOptions.find(name) != vectorOptions.end()) { // check if it exists
105 value = vectorOptions[name];
106 } else {
107 int rank;
108 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
109 if (rank == MASTER_RANK) {
110 std::cerr << __FILE__ << ":" << __LINE__ << name + " not declared using the add() function!" << std::endl;
111 MPI_Abort(MPI_COMM_WORLD, 1);
112 }
113 }
114 }
115
116 template <typename T> static void get(const std::string& name, T& value) {
117 std::string sval;
118 get(name, sval);
119
120 try {
121 value = boost::lexical_cast<T>(sval);
122 } catch (...) {
123 int myRank;
124 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
125 if (myRank == MASTER_RANK) {
126 std::cerr << __FILE__ << ":" << __LINE__
127 << std::string(" Problems casting ") + name + " " + sval + std::string(" to ") + typeid(T).name()
128 << std::endl;
129
130 MPI_Abort(MPI_COMM_WORLD, 1);
131 }
132 }
133 }
134
141 template <typename T> static void get(const std::string& name, std::vector<T>& value) {
142 std::vector<std::string> stringValue;
143 get(name, stringValue);
144
145 for (std::vector<std::string>::iterator i = stringValue.begin(); i != stringValue.end(); ++i) {
146 try {
147 value.push_back(boost::lexical_cast<T>(*i));
148 } catch (...) {
149 int myRank;
150 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
151 if (myRank == MASTER_RANK) {
152 std::cerr << __FILE__ << ":" << __LINE__
153 << std::string(" Problems casting ") + name + *i + std::string(" to ") + typeid(T).name()
154 << std::endl;
155
156 MPI_Abort(MPI_COMM_WORLD, 1);
157 }
158 }
159 }
160 }
161
162 // Determine whether a given variable has been set.
163 static bool isSet(const std::string& name) {
164 return(options.find(name) != options.end());
165 }
166
167 static void addComposing(const std::string& name, const std::string& desc);
168
169 static void helpMessage();
170
171 static bool versionMessage();
172
173 static std::string versionInfo();
174
175 static std::string configInfo();
176
177 static bool parse(const bool needsRunConfig = true, const bool allowUnknown = true);
178
179 static bool helpRequested;
180
181private:
182 static int argc;
183 static char** argv;
184
185 static boost::program_options::options_description* descriptions;
186 static boost::program_options::variables_map* variables;
187
188 static std::map<std::string, std::string> options;
189 static std::map<std::string, bool> isOptionParsed;
190 static std::map<std::string, std::vector<std::string>> vectorOptions;
191 static std::map<std::string, bool> isVectorOptionParsed;
192
193 static std::string global_config_file_name;
194 static std::string user_config_file_name;
195 static std::string run_config_file_name;
196
197 static void addDefaultParameters();
198};
199
200#endif
for i
Definition Dispersion.m:24
static bool versionMessage()
static void addDefaultParameters()
static boost::program_options::variables_map * variables
static void get(const std::string &name, std::vector< std::string > &value)
static std::map< std::string, std::vector< std::string > > vectorOptions
static void addComposing(const std::string &name, const std::string &desc)
static std::string user_config_file_name
static void get(const std::string &name, std::vector< T > &value)
static std::map< std::string, bool > isVectorOptionParsed
static boost::program_options::options_description * descriptions
static char ** argv
static std::map< std::string, std::string > options
static std::string configInfo()
static bool helpRequested
static void get(const std::string &name, std::string &value)
static std::string global_config_file_name
static std::string run_config_file_name
static void add(const std::string &name, const std::string &desc, const T &defValue)
static void helpMessage()
static void add(const std::string &name, const std::string &desc, const std::string &defValue)
static std::string versionInfo()
static void get(const std::string &name, T &value)
static bool parse(const bool needsRunConfig=true, const bool allowUnknown=true)
static std::map< std::string, bool > isOptionParsed
Readparameters(int cmdargc, char *cmdargv[])
static bool isSet(const std::string &name)
#define MASTER_RANK
Definition common.h:67
int myRank
Definition gpu_base.cpp:48