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 std::ostringstream os;
83 ParameterGroup::to_string(
const bool& b) {
93 ParameterGroup::to_string(
const ParameterGroup&)
95 return std::string(
"<parameter group>");
98 inline std::pair<std::string, std::string>
99 ParameterGroup::filename_split(
const std::string& filename)
101 int fpos = filename.rfind(
'.');
102 std::string name = filename.substr(0, fpos);
103 std::string type = filename.substr(fpos+1);
104 return std::make_pair(name, type);
107 template <
typename StringArray>
108 ParameterGroup::ParameterGroup(
int argc, StringArray argv,
bool verify_syntax,
109 const bool enable_output)
110 : path_(ID_path_root), parent_(0), output_is_enabled_(enable_output)
112 if (verify_syntax && (argc < 2)) {
113 std::cerr <<
"Usage: " << argv[0] <<
" "
114 <<
"[paramfilename1.param] "
115 <<
"[paramfilename2.param] "
116 <<
"[overridden_arg1=value1] "
117 <<
"[overridden_arg2=value2] "
118 <<
"[...]" << std::endl;
121 this->parseCommandLineArguments(argc, argv, verify_syntax);
124 template <
typename StringArray>
125 void ParameterGroup::parseCommandLineArguments(
int argc, StringArray argv,
bool verify_syntax)
127 std::vector<std::string> files;
128 std::vector<std::pair<std::string, std::string> > assignments;
129 for (
int i = 1; i < argc; ++i) {
130 std::string arg(argv[i]);
131 int fpos = arg.find(ID_delimiter_assignment);
132 if (fpos ==
int(std::string::npos)) {
133 std::string filename = arg.substr(0, fpos);
134 files.push_back(filename);
137 int pos = fpos + ID_delimiter_assignment.size();
138 int spos = arg.find(ID_delimiter_assignment, pos);
139 if (spos ==
int(std::string::npos)) {
140 std::string name = arg.substr(0, fpos);
141 std::string value = arg.substr(pos, spos);
142 assignments.push_back(std::make_pair(name, value));
145 OpmLog::warning(
"Too many assignments (' "
146 + ID_delimiter_assignment
147 +
"') detected in argument " + to_string(i));
149 for (
int i = 0; i < int(files.size()); ++i) {
150 std::pair<std::string, std::string> file_type = filename_split(files[i]);
151 if (file_type.second ==
"param") {
155 std::cerr <<
"ERROR: Input '" << files[i] <<
"' is not a valid name for a parameter file.\n";
156 std::cerr <<
" Valid filename extensions are 'param'.\n";
157 OPM_THROW(std::runtime_error,
"ParameterGroup cannot handle argument: " << files[i]);
159 unhandled_arguments_.push_back(files[i]);
163 for (
int i = 0; i < int(assignments.size()); ++i) {
175 template<
typename T,
class Requirement>
177 const Requirement& r)
const
180 std::pair<std::string, std::string> name_path = splitParam(name);
181 map_type::const_iterator it = map_.find(name_path.first);
182 if (it == map_.end()) {
185 if (output_is_enabled_) {
186 OpmLog::warning(name +
"not found at " +
path() + ID_delimiter_path +
", asking parent.");
188 return parent_->
get<T>(name, r);
191 std::cerr <<
"ERROR: The group '"
193 <<
"' does not contain an element named '"
196 throw NotFoundException();
199 if (name_path.second ==
"") {
200 T val = this->translate<T>(*it, r);
201 it->second->setUsed();
202 if (output_is_enabled_) {
203 OpmLog::debug(name +
" found at " +
path() + ID_delimiter_path +
", value is " + to_string(val));
207 ParameterGroup& pg =
dynamic_cast<ParameterGroup&
>(*(*it).second);
209 return pg.get<T>(name_path.second, r);
215 const T& default_value)
const
220 template<
typename T,
class Requirement>
222 const T& default_value,
223 const Requirement& r)
const
226 std::pair<std::string, std::string> name_path = splitParam(name);
227 map_type::const_iterator it = map_.find(name_path.first);
228 if (it == map_.end()) {
231 if (output_is_enabled_) {
232 OpmLog::warning(name +
" not found at " +
path() + ID_delimiter_path +
", asking parent.");
234 return parent_->
getDefault<T>(name, default_value, r);
237 std::string requirement_result = r(default_value);
238 if (requirement_result !=
"") {
239 std::cerr <<
"ERROR: The default value for the "
240 <<
" element named '"
242 <<
"' in the group '"
244 <<
"' failed to meet a requirenemt.\n";
245 std::cerr <<
"The requirement enforcer returned the following message:\n"
246 << requirement_result
248 throw RequirementFailedException<Requirement>();
251 if (output_is_enabled_) {
252 OpmLog::debug(name +
" not found. Using default value '" + to_string(default_value) +
"'.");
254 return default_value;
256 if (name_path.second ==
"") {
257 T val = this->translate<T>(*it, r);
258 it->second->setUsed();
259 if (output_is_enabled_) {
260 OpmLog::debug(name +
" found at " +
path() + ID_delimiter_path
261 +
", value is '" + to_string(val) +
"'.");
265 ParameterGroup& pg =
dynamic_cast<ParameterGroup&
>(*(*it).second);
267 return pg.getDefault<T>(name_path.second, default_value, r);
271 template<
typename T,
class Requirement>
272 inline T ParameterGroup::translate(
const pair_type& named_data,
273 const Requirement& chk)
const
275 const std::string& name = named_data.first;
276 const data_type data = named_data.second;
277 std::string conversion_error;
278 T value = ParameterMapItemTrait<T>::convert(*data, conversion_error,
280 if (conversion_error !=
"") {
281 std::cerr <<
"ERROR: Failed to convert the element named '"
283 <<
"' in the group '"
286 << ParameterMapItemTrait<T>::type()
288 std::cerr <<
"The conversion routine returned the following message:\n"
291 throw WrongTypeException();
293 std::string requirement_result = chk(value);
294 if (requirement_result !=
"") {
295 std::cerr <<
"ERROR: The element named '"
297 <<
"' in the group '"
300 << ParameterMapItemTrait<T>::type()
301 <<
"' failed to meet a requirenemt.\n";
302 std::cerr <<
"The requirement enforcer returned the following message:\n"
303 << requirement_result
305 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.
void insertParameter(const std::string &name, const std::string &value)
Insert a new parameter item into the group.
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:214
void readParam(const std::string ¶m_filename)
Reads the contents of the param file specified by param_filename into this ParameterGroup.
T get(const std::string &name) const
This method is used to read a parameter from the parameter group.
Definition: ParameterGroup_impl.hpp:170
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:29
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:51