remove ParameterGroup code

moved to opm-common
This commit is contained in:
Arne Morten Kvarving 2017-11-17 13:09:00 +01:00
parent d4bbaa1d3e
commit acc631f504
13 changed files with 0 additions and 1817 deletions

View File

@ -85,9 +85,6 @@ list (APPEND MAIN_SOURCE_FILES
opm/core/utility/extractPvtTableIndex.cpp
opm/core/utility/miscUtilities.cpp
opm/core/utility/miscUtilitiesBlackoil.cpp
opm/core/utility/parameters/Parameter.cpp
opm/core/utility/parameters/ParameterGroup.cpp
opm/core/utility/parameters/ParameterTools.cpp
opm/core/wells/InjectionSpecification.cpp
opm/core/wells/ProductionSpecification.cpp
opm/core/wells/WellCollection.cpp
@ -114,7 +111,6 @@ list (APPEND TEST_SOURCE_FILES
tests/test_wachspresscoord.cpp
tests/test_linearsolver.cpp
tests/test_parallel_linearsolver.cpp
tests/test_param.cpp
tests/test_satfunc.cpp
tests/test_shadow.cpp
tests/test_equil.cpp
@ -134,7 +130,6 @@ list (APPEND TEST_SOURCE_FILES
# originally generated with the command:
# find tests -name '*.param' -a ! -wholename '*/not-unit/*' -printf '\t%p\n' | sort
list (APPEND TEST_DATA_FILES
tests/testdata.param
tests/liveoil.DATA
tests/capillary.DATA
tests/capillary_overlap.DATA
@ -303,13 +298,6 @@ list (APPEND PUBLIC_HEADER_FILES
opm/core/utility/miscUtilities.hpp
opm/core/utility/miscUtilitiesBlackoil.hpp
opm/core/utility/miscUtilities_impl.hpp
opm/core/utility/parameters/Parameter.hpp
opm/core/utility/parameters/ParameterGroup.hpp
opm/core/utility/parameters/ParameterGroup_impl.hpp
opm/core/utility/parameters/ParameterMapItem.hpp
opm/core/utility/parameters/ParameterRequirement.hpp
opm/core/utility/parameters/ParameterStrings.hpp
opm/core/utility/parameters/ParameterTools.hpp
opm/core/utility/share_obj.hpp
opm/core/well_controls.h
opm/core/wells.h

View File

@ -1,72 +0,0 @@
//===========================================================================
//
// File: Parameter.cpp
//
// Created: Tue Jun 2 19:18:25 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <string>
#include <opm/core/utility/parameters/Parameter.hpp>
namespace Opm {
std::string
correct_parameter_tag(const ParameterMapItem& item)
{
std::string tag = item.getTag();
if (tag != ID_xmltag__param) {
std::string error = "The XML tag was '" +
tag + "' but should be '" +
ID_xmltag__param + "'.\n";
return error;
} else {
return "";
}
}
std::string
correct_type(const Parameter& parameter,
const std::string& param_type)
{
std::string type = parameter.getType();
if ( (type != param_type) &&
(type != ID_param_type__cmdline) ) {
std::string error = "The data was of type '" + type +
"' but should be of type '" +
param_type + "'.\n";
return error;
} else {
return "";
}
}
} // namespace Opm

View File

@ -1,215 +0,0 @@
//===========================================================================
//
// File: Parameter.hpp
//
// Created: Tue Jun 2 16:00:21 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETER_HEADER
#define OPM_PARAMETER_HEADER
#include <string>
#include <sstream>
#include <opm/core/utility/parameters/ParameterMapItem.hpp>
#include <opm/core/utility/parameters/ParameterStrings.hpp>
namespace Opm {
/// @brief
/// @todo Doc me!
class Parameter : public ParameterMapItem {
public:
/// @brief
/// @todo Doc me!
virtual ~Parameter() {}
/// @brief
/// @todo Doc me!
/// @return
virtual std::string getTag() const {return ID_xmltag__param;}
/// @brief
/// @todo Doc me!
/// @param
Parameter(const std::string& value, const std::string& type)
: value_(value), type_(type) {}
/// @brief
/// @todo Doc me!
/// @return
std::string getValue() const {return value_;}
/// @brief
/// @todo Doc me!
/// @return
std::string getType() const {return type_;}
private:
std::string value_;
std::string type_;
};
/// @brief
/// @todo Doc me!
/// @param
/// @return
std::string correct_parameter_tag(const ParameterMapItem& item);
std::string correct_type(const Parameter& parameter,
const std::string& type);
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
template<>
struct ParameterMapItemTrait<int> {
static int convert(const ParameterMapItem& item,
std::string& conversion_error,
const bool)
{
conversion_error = correct_parameter_tag(item);
if (conversion_error != "") {
return 0;
}
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
conversion_error = correct_type(parameter, ID_param_type__int);
if (conversion_error != "") {
return 0;
}
std::stringstream stream;
stream << parameter.getValue();
int value;
stream >> value;
if (stream.fail()) {
conversion_error = "Conversion to '" +
ID_param_type__int +
"' failed. Data was '" +
parameter.getValue() + "'.\n";
return 0;
}
return value;
}
static std::string type() {return ID_param_type__int;}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
template<>
struct ParameterMapItemTrait<double> {
static double convert(const ParameterMapItem& item,
std::string& conversion_error,
const bool)
{
conversion_error = correct_parameter_tag(item);
if (conversion_error != "") {
return 0.0;
}
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
conversion_error = correct_type(parameter, ID_param_type__float);
if (conversion_error != "") {
return 0.0;
}
std::stringstream stream;
stream << parameter.getValue();
double value;
stream >> value;
if (stream.fail()) {
conversion_error = "Conversion to '" +
ID_param_type__float +
"' failed. Data was '" +
parameter.getValue() + "'.\n";
return 0.0;
}
return value;
}
static std::string type() {return ID_param_type__float;}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
template<>
struct ParameterMapItemTrait<bool> {
static bool convert(const ParameterMapItem& item,
std::string& conversion_error,
const bool)
{
conversion_error = correct_parameter_tag(item);
if (conversion_error != "") {
return false;
}
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
conversion_error = correct_type(parameter, ID_param_type__bool);
if (conversion_error != "") {
return false;
}
if (parameter.getValue() == ID_true) {
return true;
} else if (parameter.getValue() == ID_false) {
return false;
} else {
conversion_error = "Conversion failed. Data was '" +
parameter.getValue() +
"', but should be one of '" +
ID_true + "' or '" + ID_false + "'.\n";
return false;
}
}
static std::string type() {return ID_param_type__bool;}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
template<>
struct ParameterMapItemTrait<std::string> {
static std::string convert(const ParameterMapItem& item,
std::string& conversion_error,
const bool)
{
conversion_error = correct_parameter_tag(item);
if (conversion_error != "") {
return "";
}
const Parameter& parameter = dynamic_cast<const Parameter&>(item);
conversion_error = correct_type(parameter, ID_param_type__string);
if (conversion_error != "") {
return "";
}
return parameter.getValue();
}
static std::string type() {return ID_param_type__string;}
};
} // namespace Opm
#endif // OPM_PARAMETER_HPP

View File

@ -1,331 +0,0 @@
//===========================================================================
//
// File: ParameterGroup.cpp
//
// Created: Tue Jun 2 19:13:17 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <opm/core/utility/parameters/ParameterGroup.hpp>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <memory>
#include <opm/core/utility/parameters/Parameter.hpp>
#include <opm/core/utility/parameters/ParameterStrings.hpp>
#include <opm/core/utility/parameters/ParameterTools.hpp>
namespace Opm {
ParameterGroup::ParameterGroup()
: path_(ID_path_root), parent_(0), output_is_enabled_(true)
{
}
ParameterGroup::~ParameterGroup() {
if (output_is_enabled_) {
//TermColors::Normal();
}
}
std::string ParameterGroup::getTag() const {
return ID_xmltag__param_grp;
}
bool ParameterGroup::has(const std::string& name) const
{
std::pair<std::string, std::string> name_path = splitParam(name);
map_type::const_iterator it = map_.find(name_path.first);
if (it == map_.end()) {
return false;
} else if (name_path.second == "") {
return true;
} else {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(*it).second);
return pg.has(name_path.second);
}
}
std::string ParameterGroup::path() const {
return path_;
}
ParameterGroup::ParameterGroup(const std::string& patharg,
const ParameterGroup* parent,
const bool enable_output)
: path_(patharg), parent_(parent), output_is_enabled_(enable_output)
{
}
ParameterGroup
ParameterGroup::getGroup(const std::string& name) const
{
return get<ParameterGroup>(name);
}
namespace {
inline std::istream&
samcode_readline(std::istream& is, std::string& parameter) {
return std::getline(is, parameter);
}
} // anonymous namespace
void ParameterGroup::readParam(const std::string& param_filename) {
std::ifstream is(param_filename.c_str());
if (!is) {
std::cerr << "Error: Failed to open parameter file '" << param_filename << "'.\n";
throw std::exception();
}
std::string parameter;
int lineno = 0;
while (samcode_readline(is, parameter)) {
++lineno;
int commentpos = parameter.find(ID_comment);
if (commentpos != 0) {
if (commentpos != int(std::string::npos)) {
parameter = parameter.substr(0, commentpos);
}
int fpos = parameter.find(ID_delimiter_assignment);
if (fpos == int(std::string::npos)) {
std::cerr << "WARNING: No '" << ID_delimiter_assignment << "' found on line " << lineno << ".\n";
}
int pos = fpos + ID_delimiter_assignment.size();
int spos = parameter.find(ID_delimiter_assignment, pos);
if (spos == int(std::string::npos)) {
std::string name = parameter.substr(0, fpos);
std::string value = parameter.substr(pos, spos);
this->insertParameter(name, value);
} else {
std::cerr << "WARNING: To many '" << ID_delimiter_assignment << "' found on line " << lineno << ".\n";
}
}
}
}
void ParameterGroup::writeParam(const std::string& param_filename) const {
std::ofstream os(param_filename.c_str());
if (!os) {
std::cerr << "Error: Failed to open parameter file '"
<< param_filename << "'.\n";
throw std::exception();
}
this->writeParamToStream(os);
}
void ParameterGroup::writeParamToStream(std::ostream& os) const
{
if (map_.empty()) {
os << this->path() << "/" << "dummy"
<< ID_delimiter_assignment << "0\n";
}
for (map_type::const_iterator it = map_.begin(); it != map_.end(); ++it) {
if ( (*it).second->getTag() == ID_xmltag__param_grp ) {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(*it).second);
pg.writeParamToStream(os);
} else if ( (*it).second->getTag() == ID_xmltag__param) {
os << this->path() << "/" << (*it).first << ID_delimiter_assignment
<< dynamic_cast<Parameter&>(*(*it).second).getValue() << "\n";
} else {
os << this->path() << "/" << (*it).first << ID_delimiter_assignment
<< "<" << (*it).second->getTag() << ">" << "\n";
}
}
}
void ParameterGroup::insert(const std::string& name,
const std::shared_ptr<ParameterMapItem>& data)
{
std::pair<std::string, std::string> name_path = splitParam(name);
map_type::const_iterator it = map_.find(name_path.first);
assert(name_path.second == "");
if (it == map_.end()) {
map_[name] = data;
} else {
if ( (map_[name]->getTag() == data->getTag()) &&
(data->getTag() == ID_xmltag__param_grp) ) {
ParameterGroup& alpha = dynamic_cast<ParameterGroup&>(*(*it).second);
ParameterGroup& beta = dynamic_cast<ParameterGroup&>(*data);
for (map_type::const_iterator
item = beta.map_.begin(); item != beta.map_.end(); ++item) {
alpha.insert((*item).first, (*item).second);
}
} else {
std::cout << "WARNING : The '"
<< map_[name]->getTag()
<< "' element '"
<< name
<< "' already exist in group '"
<< this->path()
<< "'. The element will be replaced by a '"
<< data->getTag()
<< "' element.\n";
map_[name] = data;
}
}
}
void ParameterGroup::insertParameter(const std::string& name,
const std::string& value)
{
std::pair<std::string, std::string> name_path = splitParam(name);
while (name_path.first == "") {
name_path = splitParam(name_path.second);
}
map_type::const_iterator it = map_.find(name_path.first);
if (it == map_.end()) {
if (name_path.second == "") {
std::shared_ptr<ParameterMapItem> data(new Parameter(value, ID_param_type__cmdline));
map_[name_path.first] = data;
} else {
std::shared_ptr<ParameterMapItem> data(new ParameterGroup(this->path() + ID_delimiter_path + name_path.first,
this,
output_is_enabled_));
ParameterGroup& child = dynamic_cast<ParameterGroup&>(*data);
child.insertParameter(name_path.second, value);
map_[name_path.first] = data;
}
} else if (name_path.second == "") {
if ((*it).second->getTag() != ID_xmltag__param) {
std::cout << "WARNING : The "
<< map_[name_path.first]->getTag()
<< " element '"
<< name
<< "' already exist in group '"
<< this->path()
<< "'. It will be replaced by a "
<< ID_xmltag__param
<< " element.\n";
}
std::shared_ptr<ParameterMapItem> data(new Parameter(value, ID_param_type__cmdline));
map_[name_path.first] = data;
} else {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(*it).second);
pg.insertParameter(name_path.second, value);
}
}
#if 0
void ParameterGroup::display() const {
std::cout << "Begin: " << this->path() << "\n";
for (map_type::const_iterator it = map_.begin(); it != map_.end(); ++it) {
if ( (*it).second->getTag() == ID_xmltag__param_grp ) {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(*it).second);
pg.display();
} else if ( (*it).second->getTag() == ID_xmltag__param) {
std::cout << (*it).first
<< " ("
<< (*it).second->getTag()
<< ") has value "
<< dynamic_cast<Parameter&>(*(*it).second).getValue()
<< "\n";
} else {
std::cout << (*it).first
<< " ("
<< (*it).second->getTag()
<< ")\n";
}
}
std::cout << "End: " << this->path() << "\n";
}
#endif
bool ParameterGroup::anyUnused() const
{
if (!this->used()) {
return true;
}
for (map_type::const_iterator it = map_.begin(); it != map_.end(); ++it) {
if (it->second->getTag() == ID_xmltag__param_grp) {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(it->second));
if (pg.anyUnused()) {
return true;
}
} else if (it->second->getTag() == ID_xmltag__param) {
if (!it->second->used()) {
return true;
}
}
}
return false;
}
void ParameterGroup::displayUsage(bool used_params) const
{
if (this->used() == used_params) {
std::cout << this->path() << '\n';
}
for (map_type::const_iterator it = map_.begin(); it != map_.end(); ++it) {
if (it->second->getTag() == ID_xmltag__param_grp) {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(it->second));
pg.displayUsage(used_params);
} else if (it->second->getTag() == ID_xmltag__param) {
if (it->second->used() == used_params) {
std::cout << path() << '/' << it->first << '\n';
}
}
}
std::cout << std::flush;
}
void ParameterGroup::disableOutput() {
this->recursiveSetIsOutputEnabled(false);
}
void ParameterGroup::enableOutput() {
this->recursiveSetIsOutputEnabled(true);
}
bool ParameterGroup::isOutputEnabled() const {
return output_is_enabled_;
}
void ParameterGroup::recursiveSetIsOutputEnabled(bool output_is_enabled)
{
output_is_enabled_ = output_is_enabled;
for (map_type::const_iterator it = map_.begin(); it != map_.end(); ++it) {
if (it->second->getTag() == ID_xmltag__param_grp) {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(it->second));
pg.recursiveSetIsOutputEnabled(output_is_enabled);
}
}
}
const std::vector<std::string>& ParameterGroup::unhandledArguments() const
{
return unhandled_arguments_;
}
} // namespace Opm

View File

@ -1,272 +0,0 @@
//===========================================================================
//
// File: ParameterGroup.hpp
//
// Created: Tue Jun 2 19:11:11 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETERGROUP_HEADER
#define OPM_PARAMETERGROUP_HEADER
#include <exception>
#include <map>
#include <string>
#include <vector>
#include <memory>
#include <opm/core/utility/parameters/ParameterMapItem.hpp>
#include <opm/core/utility/parameters/ParameterRequirement.hpp>
namespace Opm {
/// ParameterGroup is a class that is used to provide run-time parameters.
/// The standard use of the class is to call create it with the
/// (int argc, char** argv) constructor (where the arguments are those
/// given by main). This parses the command line, where each token
/// either
/// A) specifies a parameter (by a "param=value" token).
/// B) specifies a param file to be read (by a "filename.param" token).
/// After the tokens are parsed they are stored in a tree structure
/// in the ParameterGroup object; it is worth mentioning that parameters
/// are inherited in this tree structure. Thus, if ``grid\_prefix'' is
/// given a value in the root node, this value will be visible in all
/// parts of the tree (unless the parameter is overwritten in a subtree).
/// Applications using this ParameterGroup objects will usually write out
/// a message for each node in the three that is used by the application;
/// this is one way to determine valid parameters.
///
/// Parameters specified as "param=value" on the command line
///
/// To specify a parameter on the command line, you must know where in the tree the
/// parameter resides. The syntax for specifying parameters on the command line given
/// an application called ``simulator'' is
/// simulator param1=value grp/param2=value
// for parameter ``param1'' lying at the root and ``param2'' in the group
/// ``grp''. If the same parameters are specified multiple times on the command
/// line, only the last will be used. Thus an application named ``simulator'' run with
/// the following command
/// simulator param=value1 param=value2
/// will use value2 as the value of ``param''.
///
/// param files
///
/// A param file consists of multiple lienes, where each line consists of "param=value".
/// This syntax is identical to the one for parameters specified on the command line.
class ParameterGroup : public ParameterMapItem {
public:
struct NotFoundException : public std::exception {};
struct WrongTypeException : public std::exception {};
struct ConversionFailedException : public std::exception {};
template<typename T>
struct RequirementFailedException : public std::exception {};
ParameterGroup();
ParameterGroup(const std::string& path, const ParameterGroup* parent,
const bool enable_output);
// From ParameterMapItem
virtual ~ParameterGroup();
virtual std::string getTag() const;
/// \brief A constructor typically used to initialize a
/// ParameterGroup from command-line arguments.
///
/// It is required that argv[0] is the program name, while if
/// 0 < i < argc, then argv[i] is either
/// the name of a parameter file or parametername=value.
///
/// \param argc is the number of command-line arguments,
/// including the name of the executable.
/// \param argv is an array of char*, each of which is a
/// command line argument.
/// \param verify_syntax If true (default), then it is an error to
/// pass arguments that cannot be handled by the ParameterGroup,
/// or an empty argument list. If false, such arguments are stored
/// and can be retrieved later with unhandledArguments().
/// \param enable_output Whether to enable output or not.
template <typename StringArray>
ParameterGroup(int argc, StringArray argv, const bool verify_syntax = true,
const bool enabled_output=true);
/// \brief This method checks if there is something with name
/// \p name in the parameter gropup.
///
/// \param name is the name of the parameter.
/// \return true if \p name is the name of something in the parameter
/// group, false otherwise.
bool has(const std::string& name) const;
/// \brief This method is used to read a parameter from the
/// parameter group.
///
/// NOTE: If the reading of the parameter fails, then this method
/// throws an appropriate exception.
///
/// \param name is the name of the parameter in question.
/// \return The value associated with then name in this parameter
/// group.
template<typename T>
T get(const std::string& name) const;
template<typename T, class Requirement>
T get(const std::string& name, const Requirement&) const;
/// \brief This method is used to read a parameter from the
/// parameter group.
///
/// NOTE: If the reading of the parameter fails, then either
/// a) this method returns \p default_value if there
/// was no parameter with name \p name in
/// the parameter group
/// or
/// b) this method throws an appropriate exception.
///
/// \param name is the name of the parameter in question.
/// \param default_value the default value of the parameter in
/// question.
/// \return The value associated with this name in this parameter
/// group.
template<typename T>
T getDefault(const std::string& name,
const T& default_value) const;
template<typename T, class Requirement>
T getDefault(const std::string& name,
const T& default_value,
const Requirement& r) const;
/// \brief This method returns the parameter group given by name,
/// i.e. it is an alias of get<ParameterGroup>().
///
/// \param name is the name of the parameter group sought.
/// \return the parameter group sought.
ParameterGroup getGroup(const std::string& name) const;
/// \brief Disables the output from get, getDefault and getGroup.
/// By default, such output is enabled.
void disableOutput();
/// \brief Enables the output from get, getDefault and getGroup.
/// By default, such output is enabled.
void enableOutput();
/// \brief Returs true if and only if output from get, getDefault
/// and getGroup is enabled.
///
/// \return true if and only if output from get, getDefault and
/// getGroup is enabled.
bool isOutputEnabled() const;
/// \brief Reads the contents of the param file specified by
/// param_filename into this ParameterGroup.
///
/// NOTE: A param file contains lines on the form 'a/b/c=d'.
// The '/' separates ParameterGroups and Parameters
/// (e.g. c is a Parameter in the ParameterGroup b,
/// and b is a ParameterGroup in the ParameterGroup a)
/// while '=' separates the name from the value (e.g. the
/// value of the parameter c above will be d).
/// NOTE: A param file does not store any type information about
/// its values.
///
/// \param param_filename is the name of a param file.
void readParam(const std::string& param_filename);
/// \brief Writes this ParameterGroup into a param file
/// specified by param_filename.
///
/// \param param_filename is the name of a param file.
void writeParam(const std::string& param_filename) const;
/// \brief Writes this ParameterGroup to a stream.
///
/// \param stream is the stream to write to.
void writeParamToStream(std::ostream& stream) const;
/// vki param interface - deprecated
template<typename T>
void get(const char* name, T& value, const T& default_value) const {
value = this->getDefault<T>(name, default_value);
}
/// vki param interface - deprecated
template<typename T>
void get(const char* name, T& value) const {
value = this->get<T>(name);
}
/// \brief Return true if any parameters are unused.
bool anyUnused() const;
/// \brief Shows which parameters which are used or unused.
void displayUsage(bool used_params = false) const;
/// \brief Returns the path of the parameter group.
std::string path() const;
/// Insert a new item into the group.
void insert(const std::string& name,
const std::shared_ptr<ParameterMapItem>& data);
/// Insert a new parameter item into the group.
void insertParameter(const std::string& name, const std::string& value);
/// Unhandled arguments from command line parsing.
const std::vector<std::string>& unhandledArguments() const;
private:
typedef std::shared_ptr<ParameterMapItem> data_type;
typedef std::pair<std::string, data_type> pair_type;
typedef std::map<std::string, data_type> map_type;
std::string path_;
map_type map_;
const ParameterGroup* parent_;
bool output_is_enabled_;
std::vector<std::string> unhandled_arguments_;
template<typename T, class Requirement>
T translate(const pair_type& data, const Requirement& chk) const;
template <typename StringArray>
void parseCommandLineArguments(int argc, StringArray argv, bool verify_syntax);
void recursiveSetIsOutputEnabled(bool output_is_enabled);
// helper routines to do textual I/O
template <typename T>
static std::string to_string(const T& val);
static std::pair<std::string, std::string>
filename_split(const std::string& filename);
};
} // namespace Opm
#include <opm/core/utility/parameters/ParameterGroup_impl.hpp>
#endif // OPM_PARAMETERGROUP_HEADER

View File

@ -1,311 +0,0 @@
//===========================================================================
//
// File: ParameterGroup_impl.hpp
//
// Created: Tue Jun 2 19:06:46 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETERGROUP_IMPL_HEADER
#define OPM_PARAMETERGROUP_IMPL_HEADER
#include <iostream>
#include <string>
#include <stdexcept>
#include <opm/core/utility/parameters/ParameterGroup.hpp>
#include <opm/core/utility/parameters/ParameterStrings.hpp>
#include <opm/core/utility/parameters/ParameterTools.hpp>
#include <opm/core/utility/parameters/Parameter.hpp>
#include <opm/common/ErrorMacros.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
namespace Opm {
template<>
struct ParameterMapItemTrait<ParameterGroup> {
static ParameterGroup
convert(const ParameterMapItem& item,
std::string& conversion_error,
bool enable_output)
{
std::string tag = item.getTag();
if (tag != ID_xmltag__param_grp) {
conversion_error = "The XML tag was '" + tag +
"' but should be '" +
ID_xmltag__param_grp + "'.\n";
return ParameterGroup("", 0, enable_output);
}
conversion_error = "";
const ParameterGroup& pg = dynamic_cast<const ParameterGroup&>(item);
return pg;
}
static std::string type() {return "ParameterGroup";}
};
template <typename T>
inline std::string
ParameterGroup::to_string(const T& val)
{
std::ostringstream os;
os << val;
return os.str();
}
template <>
inline std::string
ParameterGroup::to_string(const bool& b) {
if (b) {
return ID_true;
} else {
return ID_false;
}
}
template <>
inline std::string
ParameterGroup::to_string(const ParameterGroup&)
{
return std::string("<parameter group>");
}
inline std::pair<std::string, std::string>
ParameterGroup::filename_split(const std::string& filename)
{
int fpos = filename.rfind('.');
std::string name = filename.substr(0, fpos);
std::string type = filename.substr(fpos+1);
return std::make_pair(name, type);
}
template <typename StringArray>
ParameterGroup::ParameterGroup(int argc, StringArray argv, bool verify_syntax,
const bool enable_output)
: path_(ID_path_root), parent_(0), output_is_enabled_(enable_output)
{
if (verify_syntax && (argc < 2)) {
std::cerr << "Usage: " << argv[0] << " "
<< "[paramfilename1.param] "
<< "[paramfilename2.param] "
<< "[overridden_arg1=value1] "
<< "[overridden_arg2=value2] "
<< "[...]" << std::endl;
exit(EXIT_FAILURE);
}
this->parseCommandLineArguments(argc, argv, verify_syntax);
}
template <typename StringArray>
void ParameterGroup::parseCommandLineArguments(int argc, StringArray argv, bool verify_syntax)
{
std::vector<std::string> files;
std::vector<std::pair<std::string, std::string> > assignments;
for (int i = 1; i < argc; ++i) {
std::string arg(argv[i]);
int fpos = arg.find(ID_delimiter_assignment);
if (fpos == int(std::string::npos)) {
std::string filename = arg.substr(0, fpos);
files.push_back(filename);
continue;
}
int pos = fpos + ID_delimiter_assignment.size();
int spos = arg.find(ID_delimiter_assignment, pos);
if (spos == int(std::string::npos)) {
std::string name = arg.substr(0, fpos);
std::string value = arg.substr(pos, spos);
assignments.push_back(std::make_pair(name, value));
continue;
}
OpmLog::warning("Too many assignements (' "
+ ID_delimiter_assignment
+ "') detected in argument " + to_string(i));
}
for (int i = 0; i < int(files.size()); ++i) {
std::pair<std::string, std::string> file_type = filename_split(files[i]);
if (file_type.second == "param") {
this->readParam(files[i]);
} else {
if (verify_syntax) {
std::cerr << "ERROR: Input '" << files[i] << "' is not a valid name for a parameter file.\n";
std::cerr << " Valid filename extensions are 'param'.\n";
OPM_THROW(std::runtime_error, "ParameterGroup cannot handle argument: " << files[i]);
} else {
unhandled_arguments_.push_back(files[i]);
}
}
}
for (int i = 0; i < int(assignments.size()); ++i) {
this->insertParameter(assignments[i].first, assignments[i].second);
}
}
template<typename T>
inline T ParameterGroup::get(const std::string& name) const
{
return this->get<T>(name, ParameterRequirementNone());
}
template<typename T, class Requirement>
inline T ParameterGroup::get(const std::string& name,
const Requirement& r) const
{
setUsed();
std::pair<std::string, std::string> name_path = splitParam(name);
map_type::const_iterator it = map_.find(name_path.first);
if (it == map_.end()) {
if (parent_ != 0) {
// If we have a parent, ask it instead.
if (output_is_enabled_) {
OpmLog::warning(name + "not found at " + path() + ID_delimiter_path + ", asking parent.");
}
return parent_->get<T>(name, r);
} else {
// We are at the top, name has not been found.
std::cerr << "ERROR: The group '"
<< this->path()
<< "' does not contain an element named '"
<< name
<< "'.\n";
throw NotFoundException();
}
}
if (name_path.second == "") {
T val = this->translate<T>(*it, r);
it->second->setUsed();
if (output_is_enabled_) {
OpmLog::debug(name + " found at " + path() + ID_delimiter_path + ", value is " + to_string(val));
}
return val;
} else {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(*it).second);
pg.setUsed();
return pg.get<T>(name_path.second, r);
}
}
template<typename T>
inline T ParameterGroup::getDefault(const std::string& name,
const T& default_value) const
{
return this->getDefault<T>(name, default_value, ParameterRequirementNone());
}
template<typename T, class Requirement>
inline T ParameterGroup::getDefault(const std::string& name,
const T& default_value,
const Requirement& r) const
{
setUsed();
std::pair<std::string, std::string> name_path = splitParam(name);
map_type::const_iterator it = map_.find(name_path.first);
if (it == map_.end()) {
if (parent_ != 0) {
// If we have a parent, ask it instead.
if (output_is_enabled_) {
OpmLog::warning(name + " not found at " + path() + ID_delimiter_path + ", asking parent.");
}
return parent_->getDefault<T>(name, default_value, r);
} else {
// We check the requirement for the default value
std::string requirement_result = r(default_value);
if (requirement_result != "") {
std::cerr << "ERROR: The default value for the "
<< " element named '"
<< name
<< "' in the group '"
<< this->path()
<< "' failed to meet a requirenemt.\n";
std::cerr << "The requirement enforcer returned the following message:\n"
<< requirement_result
<< "\n";
throw RequirementFailedException<Requirement>();
}
}
if (output_is_enabled_) {
OpmLog::debug(name + " not found. Using default value '" + to_string(default_value) + "'.");
}
return default_value;
}
if (name_path.second == "") {
T val = this->translate<T>(*it, r);
it->second->setUsed();
if (output_is_enabled_) {
OpmLog::debug(name + " found at " + path() + ID_delimiter_path
+ ", value is '" + to_string(val) + "'.");
}
return val;
} else {
ParameterGroup& pg = dynamic_cast<ParameterGroup&>(*(*it).second);
pg.setUsed();
return pg.getDefault<T>(name_path.second, default_value, r);
}
}
template<typename T, class Requirement>
inline T ParameterGroup::translate(const pair_type& named_data,
const Requirement& chk) const
{
const std::string& name = named_data.first;
const data_type data = named_data.second;
std::string conversion_error;
T value = ParameterMapItemTrait<T>::convert(*data, conversion_error,
output_is_enabled_);
if (conversion_error != "") {
std::cerr << "ERROR: Failed to convert the element named '"
<< name
<< "' in the group '"
<< this->path()
<< "' to the type '"
<< ParameterMapItemTrait<T>::type()
<< "'.\n";
std::cerr << "The conversion routine returned the following message:\n"
<< conversion_error
<< "\n";
throw WrongTypeException();
}
std::string requirement_result = chk(value);
if (requirement_result != "") {
std::cerr << "ERROR: The element named '"
<< name
<< "' in the group '"
<< this->path()
<< "' of type '"
<< ParameterMapItemTrait<T>::type()
<< "' failed to meet a requirenemt.\n";
std::cerr << "The requirement enforcer returned the following message:\n"
<< requirement_result
<< "\n";
throw RequirementFailedException<Requirement>();
}
return value;
}
} // namespace Opm
#endif // OPM_PARAMETERGROUP_IMPL_HEADER

View File

@ -1,71 +0,0 @@
//===========================================================================
//
// File: ParameterMapItem.hpp
//
// Created: Tue Jun 2 19:05:54 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETERMAPITEM_HEADER
#define OPM_PARAMETERMAPITEM_HEADER
#include <string>
namespace Opm {
/// The parameter handlig system is structured as a tree,
/// where each node inhertis from ParameterMapItem.
///
/// The abstract virtual function getTag() is used to determine
/// which derived class the node actually is.
struct ParameterMapItem {
/// Default constructor
ParameterMapItem() : used_(false) {}
/// Destructor
virtual ~ParameterMapItem() {}
/// \brief This function returns a string describing
/// the ParameterMapItem.
virtual std::string getTag() const = 0;
void setUsed() const { used_ = true; }
bool used() const { return used_; }
private:
mutable bool used_;
};
template<typename T>
struct ParameterMapItemTrait {
static T convert(const ParameterMapItem&,
std::string& conversion_error);
static std::string type();
};
} // namespace Opm
#endif // OPM_PARAMETERMAPITEM_HEADER

View File

@ -1,241 +0,0 @@
//===========================================================================
//
// File: ParameterRequirement.hpp
//
// Created: Tue Jun 2 19:05:02 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETERREQUIREMENT_HEADER
#define OPM_PARAMETERREQUIREMENT_HEADER
#include <algorithm>
#include <cassert>
#include <sstream>
#include <string>
#include <vector>
namespace Opm {
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
struct ParameterRequirementNone {
template<typename T>
std::string operator()(const T&) const {
return "";
}
};
/// @brief
/// @todo Doc me!
/// @param
/// @return
struct ParameterRequirementProbability {
std::string operator()(double x) const {
if ( (x < 0.0) || (x > 1.0) ) {
std::ostringstream stream;
stream << "The value '" << x
<< "' is not in the interval [0, 1].";
return stream.str();
} else {
return "";
}
}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
struct ParameterRequirementPositive {
template<typename T>
std::string operator()(const T& x) const {
if (x > 0) {
return "";
} else {
std::ostringstream stream;
stream << "The value '" << x << "' is not positive.";
return stream.str();
}
}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
struct ParameterRequirementNegative {
template<typename T>
std::string operator()(const T& x) const {
if (x < 0) {
return "";
} else {
std::ostringstream stream;
stream << "The value '" << x << "' is not negative.";
return stream.str();
}
}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
struct ParameterRequirementNonPositive {
template<typename T>
std::string operator()(const T& x) const {
if (x > 0) {
std::ostringstream stream;
stream << "The value '" << x << "' is positive.";
return stream.str();
} else {
return "";
}
}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
struct ParameterRequirementNonNegative {
template<typename T>
std::string operator()(const T& x) const {
if (x < 0) {
std::ostringstream stream;
stream << "The value '" << x << "' is negative.";
return stream.str();
} else {
return "";
}
}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
struct ParameterRequirementNonZero {
template<typename T>
std::string operator()(const T& x) const {
if (x != 0) {
return "";
} else {
return "The value was zero.";
}
}
};
/// @brief
/// @todo Doc me!
/// @param
/// @return
struct ParameterRequirementNonEmpty {
std::string operator()(const std::string& x) const {
if (x != "") {
return "The string was empty.";
} else {
return "";
}
}
};
/// @brief
/// @todo Doc me!
/// @tparam
/// @param
/// @return
template<class Requirement1, class Requirement2>
struct ParameterRequirementAnd {
ParameterRequirementAnd(const Requirement1& r1, const Requirement2& r2) :
r1_(r1), r2_(r2) { }
template<typename T>
std::string operator()(const T& t) const {
std::string e1 = r1_(t);
std::string e2 = r2_(t);
if (e1 == "") {
return e2;
} else if (e2 == "") {
return e1;
} else {
return e1 + " AND " + e2;
}
}
private:
const Requirement1 r1_;
const Requirement2 r2_;
};
/// @brief
/// @todo Doc me!
/// @param
struct ParameterRequirementMemberOf {
ParameterRequirementMemberOf(const std::vector<std::string>& elements)
: elements_(elements) {
assert(elements_.size() > 0);
}
/// @brief
/// @todo Doc me!
/// @param
/// @return
std::string operator()(const std::string& x) const {
if (std::find(elements_.begin(), elements_.end(), x) == elements_.end()) {
if (elements_.size() == 1) {
return "The string '" + x + "' is not '" + elements_[0] + "'.";
}
std::ostringstream stream;
stream << "The string '" << x << "' is not among '";
for (int i = 0; i < int(elements_.size()) - 2; ++i) {
stream << elements_[i] << "', '";
}
stream << elements_[elements_.size() - 2]
<< "' and '"
<< elements_[elements_.size() - 1]
<< "'.";
return stream.str();
} else {
return "";
}
}
private:
const std::vector<std::string> elements_;
};
} // namespace Opm
#endif // OPM_PARAMETERREQUIREMENT_HEADER

View File

@ -1,63 +0,0 @@
//===========================================================================
//
// File: ParameterStrings.hpp
//
// Created: Tue Jun 2 19:04:15 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETERSTRINGS_HEADER
#define OPM_PARAMETERSTRINGS_HEADER
#include <string>
namespace Opm {
const std::string ID_true = "true";
const std::string ID_false = "false";
const std::string ID_xmltag__param_grp = "ParameterGroup";
const std::string ID_xmltag__param = "Parameter";
const std::string ID_param_type__bool = "bool";
const std::string ID_param_type__int = "int";
const std::string ID_param_type__float = "double";
const std::string ID_param_type__string = "string";
const std::string ID_param_type__file = "file";
const std::string ID_param_type__cmdline = "cmdline";
//
const std::string ID_path_root = "";
const std::string ID_delimiter_path = "/";
const std::string ID_comment = "//";
const std::string ID_delimiter_assignment = "=";
} // namespace Opm
#endif // OPM_PARAMETERSTRINGS_HEADER

View File

@ -1,53 +0,0 @@
//===========================================================================
//
// File: ParameterTools.cpp
//
// Created: Tue Jun 2 19:03:09 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <opm/core/utility/parameters/ParameterTools.hpp>
#include <opm/core/utility/parameters/ParameterStrings.hpp>
namespace Opm {
std::pair<std::string, std::string> splitParam(const std::string& name)
{
int pos = name.find(ID_delimiter_path);
if (pos == int(std::string::npos)) {
return std::make_pair(name, "");
} else {
return std::make_pair(name.substr(0, pos),
name.substr(pos + ID_delimiter_path.size()));
}
}
} // namespace Opm

View File

@ -1,46 +0,0 @@
//===========================================================================
//
// File: ParameterTools.hpp
//
// Created: Tue Jun 2 19:02:19 2009
//
// Author(s): Bård Skaflestad <bard.skaflestad@sintef.no>
// Atgeirr F Rasmussen <atgeirr@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_PARAMETERTOOLS_HEADER
#define OPM_PARAMETERTOOLS_HEADER
#include <string>
#include <utility>
namespace Opm {
std::pair<std::string, std::string> splitParam(const std::string& name);
} // namespace Opm
#endif // OPM_PARAMETERTOOLS_HEADER

View File

@ -1,124 +0,0 @@
//===========================================================================
//
// File: param_test.cpp
//
// Created: Sun Dec 13 20:08:36 2009
//
// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
// Bård Skaflestad <bard.skaflestad@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010 Statoil ASA.
This file is part of The Open Reservoir Simulator Project (OpenRS).
OpenRS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenRS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenRS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#if HAVE_DYNAMIC_BOOST_TEST
#define BOOST_TEST_DYN_LINK
#endif
#define NVERBOSE // to suppress our messages when throwing
#define BOOST_TEST_MODULE ParameterTest
#include <boost/test/unit_test.hpp>
#include <opm/core/utility/parameters/ParameterGroup.hpp>
#include <cstddef>
#include <sstream>
#include <vector>
using namespace Opm;
BOOST_AUTO_TEST_CASE(commandline_syntax_init)
{
typedef const char* cp;
std::vector<cp> argv = { "program_command",
"topitem=somestring",
"/slashtopitem=anotherstring",
"/group/item=1",
"/group/anotheritem=2",
"/group/subgroup/item=3",
"/group/subgroup/anotheritem=4",
"/group/item=overridingstring",
0 };
const std::size_t argc = argv.size() - 1;
ParameterGroup p(argc, argv.data());
BOOST_CHECK(p.get<std::string>("topitem") == "somestring");
std::ostringstream os;
p.writeParamToStream(os);
std::string correct_answer = "/group/anotheritem=2\n"
"/group/item=overridingstring\n"
"/group/subgroup/anotheritem=4\n"
"/group/subgroup/item=3\n"
"/slashtopitem=anotherstring\n"
"/topitem=somestring\n";
BOOST_CHECK(os.str() == correct_answer);
BOOST_CHECK(p.unhandledArguments().empty());
// Tests that only run in debug mode.
#ifndef NDEBUG
#endif
}
BOOST_AUTO_TEST_CASE(xml_syntax_init)
{
typedef const char* cp;
std::vector<cp> argv = { "program_command",
"testdata.param",
"/group/item=overridingstring",
"unhandledargument",
0};
const std::size_t argc = argv.size() - 1;
ParameterGroup p(argc, argv.data(), false);
BOOST_CHECK(p.get<std::string>("topitem") == "somestring");
std::ostringstream os;
p.writeParamToStream(os);
std::string correct_answer = "/group/anotheritem=2\n"
"/group/item=overridingstring\n"
"/group/subgroup/anotheritem=4\n"
"/group/subgroup/item=3\n"
"/slashtopitem=anotherstring\n"
"/topitem=somestring\n";
BOOST_CHECK(os.str() == correct_answer);
BOOST_REQUIRE(p.unhandledArguments().size() == 1);
BOOST_CHECK_EQUAL(p.unhandledArguments()[0], "unhandledargument");
// Tests that only run in debug mode.
#ifndef NDEBUG
#endif
}
BOOST_AUTO_TEST_CASE(failing_strict_xml_syntax_init)
{
typedef const char* cp;
std::vector<cp> argv = { "program_command",
"testdata.param",
"/group/item=overridingstring",
"unhandledargument",
0 };
const std::size_t argc = argv.size() - 1;
BOOST_CHECK_THROW(ParameterGroup p(argc, argv.data()), std::runtime_error);
}

View File

@ -1,6 +0,0 @@
topitem=somestring
/slashtopitem=anotherstring
/group/item=1
/group/anotheritem=2
/group/subgroup/item=3
/group/subgroup/anotheritem=4