1#ifndef DUNE_FEM_PARAMETER_HH
2#define DUNE_FEM_PARAMETER_HH
7#include <unordered_map>
9#include <dune/fem/io/io.hh>
10#include <dune/fem/io/parameter/exceptions.hh>
11#include <dune/fem/io/parameter/container.hh>
12#include <dune/fem/io/parameter/reader.hh>
14#include <dune/fem/storage/singleton.hh>
193 static const int solverStatistics = ParameterContainerData::solverStatistics;
194 static const int extendedStatistics = ParameterContainerData::extendedStatistics;
195 static const int parameterOutput = ParameterContainerData::parameterOutput;
196 static const int diagnosticsOutput = ParameterContainerData::diagnosticsOutput;
197 static const int debugOutput = ParameterContainerData::debugOutput;
199 static ParameterContainer &container ()
204 static auto &localParameterLog ()
206 return container().localParameterLog_;
219 static void append (
int &argc,
char **argv ) { container().append( argc, argv ); }
226 static void append (
const std::string &key,
const std::string &value,
const bool force =
false ) { container().append( key, value, force ); }
233 template<
class NumberType>
234 static void append (
const std::string &key,
const NumberType &value,
const bool force =
false ) { container().append( key, value, force ); }
240 static void append (
const std::string &filename ) { container().append( filename ); }
249 static void appendDGF (
const std::string &filename ) { container().appendDGF( filename ); }
258 static bool exists (
const std::string &key ) {
return container().exists( key ); }
269 static void get (
const std::string &key, T &value )
271 container().get( key, value );
284 static void get (
const std::string &key,
const T &defaultValue, T &value )
286 container().get( key, defaultValue, value );
298 static void get (
const std::string &key,
const char* defaultValue, std::string &value )
300 container().get( key, defaultValue, value );
312 template<
class T,
class Val
idator >
313 static void getValid (
const std::string &key,
const Validator &validator, T &value )
315 container().getValid( key, validator, value );
328 template<
class T,
class Val
idator >
329 static void getValid (
const std::string &key,
const T &defaultValue,
const Validator &validator, T &value )
331 container().getValid( key, validator, value );
346 return container().getValue< T >( key );
360 static T
getValue (
const std::string &key,
const T &defaultValue )
362 return container().getValue( key, defaultValue );
375 template<
class T,
class Val
idator >
376 static T
getValidValue (
const std::string &key,
const Validator &validator )
378 return container().getValidValue( key, validator );
393 template<
class T,
class Val
idator >
394 static T
getValidValue (
const std::string &key,
const T &defaultValue,
const Validator &validator )
396 return container().getValidValue( key, defaultValue, validator );
400 static int getEnum (
const std::string &key,
const std::string (&values)[ n ] )
402 return container().getEnum( key, values );
406 static int getEnum (
const std::string &key,
const std::string (&values)[ n ],
int defaultValue )
408 return container().getEnum( key, values, defaultValue );
411 static int getEnum (
const std::string &key,
const std::vector<std::string> &values )
413 return container().getEnum( key, values );
416 static int getEnum (
const std::string &key,
const std::vector<std::string> &values,
int defaultValue )
418 return container().getEnum( key, values, defaultValue );
434 return container().commonOutputPath();
462 return container().commonInputPath();
466 static bool verbose () {
return container().verbose(); }
469 static bool verbose (
const int level ) {
return container().verbose(level ); }
486 static void write (
const std::string &filename,
const std::string &fileextension =
"",
bool writeAll =
true );
501 static void write ( std::ostream &out,
bool writeAll =
true ) {
return container().write( out, writeAll ); }
502 static auto write () {
return container().write(); }
505 friend class PersistenceManager ;
522 static void write (
const std::string &path,
const std::string &filename,
const std::string &fileextension,
bool writeAll =
true );
535 if( path[ path.length()-1 ] !=
'/' )
537 return path +
"p" + std::to_string( MPIManager::rank() );
541 inline void Parameter::write (
const std::string &filename,
const std::string &fileextension,
bool writeAll )
545 if( MPIManager::rank() == 0 )
550 inline void Parameter::write (
const std::string &path,
const std::string &filename,
const std::string &fileextension,
bool writeAll )
553 if( !directoryExists( path ) )
554 createDirectory( path );
556 std::string fullname( path );
558 fullname += filename;
559 fullname += fileextension;
561 std::ofstream file( fullname );
563 write( file, writeAll );
565 std::cerr <<
"Warning: Unable to write parameter file '" << fullname <<
"'" << std::endl;
574 template<
class ParamDefault,
class ParamImpl >
575 struct LocalParameter :
public ParamDefault
577 virtual ~LocalParameter ()
580 virtual ParamDefault *clone ()
const
582 return new ParamImpl( asImp() );
585 template <
class... Args>
586 LocalParameter( Args... args ) : ParamDefault( args... ) {}
589 const ParamImpl &asImp ()
const
591 return static_cast< const ParamImpl &
>( *this );
595 template<
class ParamDefault >
596 struct LocalParameter< ParamDefault, ParamDefault >
598 virtual ~LocalParameter ()
601 virtual ParamDefault *clone ()
const
603 return new ParamDefault(
static_cast< const ParamDefault &
>( *
this ) );
611 const std::string rmPrefix_;
612 std::unordered_map<std::string,std::string> dict_;
613 ParameterDict(
const std::string &rmPrefix,
614 const std::unordered_map<std::string,std::string> &dict )
615 : rmPrefix_(rmPrefix), dict_(dict) {}
616 const std::string* operator()(
const std::string &key,
const std::string *def )
620 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
623 assert(key.size()>rmPrefix_.size());
624 std::string reducedKey = key.substr(rmPrefix_.size(),key.size());
625 auto pos = dict_.find( reducedKey );
626 if (pos != dict_.end())
627 return &(pos->second);
633 if( *def == Dune::Fem::checkParameterExistsString() )
644 ParameterReader parameterDict (
645 const std::string &rmPrefix,
646 const std::unordered_map<std::string,std::string> &dict )
648 return Fem::ParameterReader( ParameterDict(rmPrefix,dict) );
650 ParameterReader parameterDict (
651 const std::unordered_map<std::string,std::string> &dict )
653 return parameterDict(
"",dict);
658 std::pair<std::string,std::string> convertValueToString(
const std::pair<const char*,const char*> &keyValue)
661 std::pair<std::string,std::string> convertValueToString(
const std::pair<const char*,V> &keyValue)
662 {
return std::make_pair(keyValue.first,std::to_string(keyValue.second)); }
663 std::pair<std::string,std::string> convertValueToString(
const std::pair<std::string,const char*> &keyValue)
666 std::pair<std::string,std::string> convertValueToString(
const std::pair<std::string,V> &keyValue)
667 {
return std::make_pair(keyValue.first,std::to_string(keyValue.second)); }
673 template<
class... Values>
674 ParameterReader parameterDict (
const std::string &rmPrefix, std::pair< const char*, Values >
const &... keyValues)
676 std::unordered_map<std::string,std::string> dict;
677 dict.insert({convertValueToString(keyValues)...});
678 return parameterDict( rmPrefix, dict);
684 void insertIntoMap(std::unordered_map<std::string,std::string> &dict,
685 const std::string &key,
const V &v)
686 { dict.insert(convertValueToString( std::make_pair(key,v) )); }
687 template<
class V,
class... Values>
688 void insertIntoMap(std::unordered_map<std::string,std::string> &dict,
689 const std::string &key,
const V &v, Values... keyValues)
691 dict.insert(convertValueToString( std::make_pair(key,v) ));
692 insertIntoMap(dict,keyValues...);
695 template<
class... Values>
696 ParameterReader parameterDict (
const std::string &rmPrefix, Values... keyValues)
698 std::unordered_map<std::string,std::string> dict;
699 insertIntoMap(dict,keyValues...);
700 return parameterDict( rmPrefix, dict);
705 typedef std::function<std::string()> LambdaType;
706 typedef std::unordered_map<std::string,LambdaType> DictType;
707 template <
class... KeyValues>
708 ParameterDict(
const std::string &rmPrefix, KeyValues... keyValues)
709 : rmPrefix_(rmPrefix), dict_()
711 insertIntoMap(keyValues...);
713 const std::string* operator()(
const std::string &key,
const std::string *def )
717 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
720 assert(key.size()>rmPrefix_.size());
721 std::string reducedKey = key.substr(rmPrefix_.size(),key.size());
722 auto pos = dict_.find( reducedKey );
723 if (pos != dict_.end())
725 tmp_ = pos->second();
736 else if( *def == Dune::Fem::checkParameterExistsString() )
748 static std::string convertValueToString(
const std::string &value)
750 static std::string convertValueToString(
const char* value)
753 static std::string convertValueToString(
const V &value)
754 {
return ParameterParser<double>::toString(value); }
756 template<class V, std::enable_if_t<std::is_convertible<V,const LambdaType&>::value,
int> i=0>
758 { dict_.insert( std::make_pair(key, v )); }
761 -> void_t< decltype(std::declval<const V&>()()) >
762 { dict_.insert( std::make_pair(key, [v]() {
return convertValueToString(v()); } )); }
765 { dict_.insert( std::make_pair(key, [v]() {
return convertValueToString(v); } )); }
768 void insertIntoMap(
const std::string &key,
const V &v)
769 { insertIntoMap_(key,v,PriorityTag<42>()); }
770 template<
class V,
class... Values>
771 void insertIntoMap(
const std::string &key,
const V &v, Values... keyValues)
773 insertIntoMap_(key,v,PriorityTag<42>());
774 insertIntoMap(keyValues...);
778 const std::string rmPrefix_;
782 template<
class... KeyValues>
783 ParameterReader parameterDict (
const std::string &rmPrefix, KeyValues... keyValues)
785 return Fem::ParameterReader( ParameterDict(rmPrefix,keyValues...) );
Container for User Specified Parameters.
Definition: parameter.hh:191
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:466
static T getValue(const std::string &key, const T &defaultValue)
get an optional parameter from the container
Definition: parameter.hh:360
static void getValid(const std::string &key, const Validator &validator, T &value)
get a mandatory parameter from the container
Definition: parameter.hh:313
static std::string commonOutputPath()
obtain common output path
Definition: parameter.hh:432
static T getValidValue(const std::string &key, const T &defaultValue, const Validator &validator)
get an optional parameter from the container
Definition: parameter.hh:394
static void get(const std::string &key, T &value)
get a mandatory parameter from the container
Definition: parameter.hh:269
static void append(const std::string &filename)
add parameters from a file to the container
Definition: parameter.hh:240
static bool verbose(const int level)
obtain the cached value for fem.verbose
Definition: parameter.hh:469
static T getValue(const std::string &key)
get a mandatory parameter from the container
Definition: parameter.hh:344
static std::string outputPath()
obtain unique output path for this process
Definition: parameter.hh:530
static void get(const std::string &key, const char *defaultValue, std::string &value)
get an optional parameter from the container special case for string
Definition: parameter.hh:298
static T getValidValue(const std::string &key, const Validator &validator)
get an optional parameter from the container
Definition: parameter.hh:376
static void write(std::ostream &out, bool writeAll=true)
write the parameter database to a stream
Definition: parameter.hh:501
static void append(const std::string &key, const std::string &value, const bool force=false)
add a single parameter to the container
Definition: parameter.hh:226
static bool exists(const std::string &key)
find out, whether a parameter is defined in the container
Definition: parameter.hh:258
static void get(const std::string &key, const T &defaultValue, T &value)
get an optional parameter from the container
Definition: parameter.hh:284
static void append(int &argc, char **argv)
add parameters from the command line RangeType gRight;
Definition: parameter.hh:219
static std::string commonInputPath()
obtain common input path
Definition: parameter.hh:460
static void getValid(const std::string &key, const T &defaultValue, const Validator &validator, T &value)
get an optional parameter from the container
Definition: parameter.hh:329
static void appendDGF(const std::string &filename)
add parameters from a DGF file to the container
Definition: parameter.hh:249
static void append(const std::string &key, const NumberType &value, const bool force=false)
add a single parameter to the container
Definition: parameter.hh:234
static void write(const std::string &filename, const std::string &fileextension="", bool writeAll=true)
write the parameter database to a file
Definition: parameter.hh:541
static DUNE_EXPORT Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:123
Dune namespace.
Definition: alignedallocator.hh:13
Helper class for tagging priorities.
Definition: typeutilities.hh:87
Helper class for tagging priorities.
Definition: typeutilities.hh:73