36#ifndef OPM_PARAMETERGROUP_IMPL_HEADER
37#define OPM_PARAMETERGROUP_IMPL_HEADER
43#include <opm/common/utility/parameters/ParameterGroup.hpp>
44#include <opm/common/utility/parameters/ParameterStrings.hpp>
45#include <opm/common/utility/parameters/ParameterTools.hpp>
46#include <opm/common/utility/parameters/Parameter.hpp>
47#include <opm/common/ErrorMacros.hpp>
48#include <opm/common/OpmLog/OpmLog.hpp>
55 std::string& conversion_error,
58 std::string tag = item.
getTag();
59 if (tag != ID_xmltag__param_grp) {
60 conversion_error =
"The XML tag was '" + tag +
62 ID_xmltag__param_grp +
"'.\n";
65 conversion_error =
"";
69 static std::string type() {
return "ParameterGroup";}
74 ParameterGroup::to_string(
const T& val)
76 return std::to_string(val);
81 ParameterGroup::to_string<std::string>(
const std::string& val)
88 ParameterGroup::to_string(
const bool& b) {
98 ParameterGroup::to_string(
const ParameterGroup&)
100 return std::string(
"<parameter group>");
103 inline std::pair<std::string, std::string>
104 ParameterGroup::filename_split(
const std::string& filename)
106 int fpos = filename.rfind(
'.');
107 std::string name = filename.substr(0, fpos);
108 std::string type = filename.substr(fpos+1);
109 return std::make_pair(name, type);
112 template <
typename StringArray>
113 ParameterGroup::ParameterGroup(
int argc, StringArray argv,
bool verify_syntax,
114 const bool enable_output)
115 : path_(ID_path_root), parent_(0), output_is_enabled_(enable_output)
117 if (verify_syntax && (argc < 2)) {
118 std::cerr <<
"Usage: " << argv[0] <<
" "
119 <<
"[paramfilename1.param] "
120 <<
"[paramfilename2.param] "
121 <<
"[overridden_arg1=value1] "
122 <<
"[overridden_arg2=value2] "
123 <<
"[...]" << std::endl;
126 this->parseCommandLineArguments(argc, argv, verify_syntax);
129 template <
typename StringArray>
130 void ParameterGroup::parseCommandLineArguments(
int argc, StringArray argv,
bool verify_syntax)
132 std::vector<std::string> files;
133 std::vector<std::pair<std::string, std::string> > assignments;
134 for (
int i = 1; i < argc; ++i) {
135 std::string arg(argv[i]);
136 int fpos = arg.find(ID_delimiter_assignment);
137 if (fpos ==
int(std::string::npos)) {
138 std::string filename = arg.substr(0, fpos);
139 files.push_back(filename);
142 int pos = fpos + ID_delimiter_assignment.size();
143 int spos = arg.find(ID_delimiter_assignment, pos);
144 if (spos ==
int(std::string::npos)) {
145 std::string name = arg.substr(0, fpos);
146 std::string value = arg.substr(pos, spos);
147 assignments.push_back(std::make_pair(name, value));
150 OpmLog::warning(
"Too many assignments (' "
151 + ID_delimiter_assignment
152 +
"') detected in argument " + to_string(i));
154 for (
int i = 0; i < int(files.size()); ++i) {
155 std::pair<std::string, std::string> file_type = filename_split(files[i]);
156 if (file_type.second ==
"param") {
160 std::cerr <<
"ERROR: Input '" << files[i] <<
"' is not a valid name for a parameter file.\n";
161 std::cerr <<
" Valid filename extensions are 'param'.\n";
162 OPM_THROW(std::runtime_error,
"ParameterGroup cannot handle argument: " + files[i]);
164 unhandled_arguments_.push_back(files[i]);
168 for (
int i = 0; i < int(assignments.size()); ++i) {
180 template<
typename T,
class Requirement>
182 const Requirement& r)
const
185 std::pair<std::string, std::string> name_path = splitParam(name);
186 map_type::const_iterator it = map_.find(name_path.first);
187 if (it == map_.end()) {
190 if (output_is_enabled_) {
191 OpmLog::warning(name +
"not found at " +
path() + ID_delimiter_path +
", asking parent.");
193 return parent_->
get<T>(name, r);
196 std::cerr <<
"ERROR: The group '"
198 <<
"' does not contain an element named '"
201 throw NotFoundException();
204 if (name_path.second ==
"") {
205 T val = this->translate<T>(*it, r);
206 it->second->setUsed();
207 if (output_is_enabled_) {
208 OpmLog::debug(name +
" found at " +
path() + ID_delimiter_path +
", value is " + to_string(val));
212 ParameterGroup& pg =
dynamic_cast<ParameterGroup&
>(*(*it).second);
214 return pg.get<T>(name_path.second, r);
220 const T& default_value)
const
225 template<
typename T,
class Requirement>
227 const T& default_value,
228 const Requirement& r)
const
231 std::pair<std::string, std::string> name_path = splitParam(name);
232 map_type::const_iterator it = map_.find(name_path.first);
233 if (it == map_.end()) {
236 if (output_is_enabled_) {
237 OpmLog::warning(name +
" not found at " +
path() + ID_delimiter_path +
", asking parent.");
239 return parent_->
getDefault<T>(name, default_value, r);
242 std::string requirement_result = r(default_value);
243 if (requirement_result !=
"") {
244 std::cerr <<
"ERROR: The default value for the "
245 <<
" element named '"
247 <<
"' in the group '"
249 <<
"' failed to meet a requirenemt.\n";
250 std::cerr <<
"The requirement enforcer returned the following message:\n"
251 << requirement_result
253 throw RequirementFailedException<Requirement>();
256 if (output_is_enabled_) {
257 OpmLog::debug(name +
" not found. Using default value '" + to_string(default_value) +
"'.");
259 return default_value;
261 if (name_path.second ==
"") {
262 T val = this->translate<T>(*it, r);
263 it->second->setUsed();
264 if (output_is_enabled_) {
265 OpmLog::debug(name +
" found at " +
path() + ID_delimiter_path
266 +
", value is '" + to_string(val) +
"'.");
270 ParameterGroup& pg =
dynamic_cast<ParameterGroup&
>(*(*it).second);
272 return pg.getDefault<T>(name_path.second, default_value, r);
276 template<
typename T,
class Requirement>
277 inline T ParameterGroup::translate(
const pair_type& named_data,
278 const Requirement& chk)
const
280 const std::string& name = named_data.first;
281 const data_type data = named_data.second;
282 std::string conversion_error;
283 T value = ParameterMapItemTrait<T>::convert(*data, conversion_error,
285 if (conversion_error !=
"") {
286 std::cerr <<
"ERROR: Failed to convert the element named '"
288 <<
"' in the group '"
291 << ParameterMapItemTrait<T>::type()
293 std::cerr <<
"The conversion routine returned the following message:\n"
296 throw WrongTypeException();
298 std::string requirement_result = chk(value);
299 if (requirement_result !=
"") {
300 std::cerr <<
"ERROR: The element named '"
302 <<
"' in the group '"
305 << ParameterMapItemTrait<T>::type()
306 <<
"' failed to meet a requirenemt.\n";
307 std::cerr <<
"The requirement enforcer returned the following message:\n"
308 << requirement_result
310 throw RequirementFailedException<Requirement>();
ParameterGroup is a class that is used to provide run-time parameters.
Definition ParameterGroup.hpp:81
std::string path() const
Returns the path of the parameter group.
Definition ParameterGroup.cpp:82
void insertParameter(const std::string &name, const std::string &value)
Insert a new parameter item into the group.
Definition ParameterGroup.cpp:200
T getDefault(const std::string &name, const T &default_value) const
This method is used to read a parameter from the parameter group.
Definition ParameterGroup_impl.hpp:219
void readParam(const std::string ¶m_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
Definition ParameterGroup.cpp:106
T get(const std::string &name) const
This method is used to read a parameter from the parameter group.
Definition ParameterGroup_impl.hpp:175
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
Definition ParameterMapItem.hpp:64
The parameter handlig system is structured as a tree, where each node inhertis from ParameterMapItem.
Definition ParameterMapItem.hpp:47
virtual std::string getTag() const =0
This function returns a string describing the ParameterMapItem.
Definition ParameterRequirement.hpp:48