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 );
424 return container().commonOutputPath();
452 return container().commonInputPath();
456 static bool verbose () {
return container().verbose(); }
459 static bool verbose (
const int level ) {
return container().verbose(level ); }
476 static void write (
const std::string &filename,
const std::string &fileextension =
"",
bool writeAll =
true );
491 static void write ( std::ostream &out,
bool writeAll =
true ) {
return container().write( out, writeAll ); }
492 static auto write () {
return container().write(); }
495 friend class PersistenceManager ;
512 static void write (
const std::string &path,
const std::string &filename,
const std::string &fileextension,
bool writeAll =
true );
525 if( path[ path.length()-1 ] !=
'/' )
527 return path +
"p" + std::to_string( MPIManager::rank() );
531 inline void Parameter::write (
const std::string &filename,
const std::string &fileextension,
bool writeAll )
535 if( MPIManager::rank() == 0 )
540 inline void Parameter::write (
const std::string &path,
const std::string &filename,
const std::string &fileextension,
bool writeAll )
543 if( !directoryExists( path ) )
544 createDirectory( path );
546 std::string fullname( path );
548 fullname += filename;
549 fullname += fileextension;
551 std::ofstream file( fullname );
553 write( file, writeAll );
555 std::cerr <<
"Warning: Unable to write parameter file '" << fullname <<
"'" << std::endl;
564 template<
class ParamDefault,
class ParamImpl >
565 struct LocalParameter :
public ParamDefault
567 virtual ~LocalParameter ()
570 virtual ParamDefault *clone ()
const
572 return new ParamImpl( asImp() );
575 template <
class... Args>
576 LocalParameter( Args... args ) : ParamDefault( args... ) {}
579 const ParamImpl &asImp ()
const
581 return static_cast< const ParamImpl &
>( *this );
585 template<
class ParamDefault >
586 struct LocalParameter< ParamDefault, ParamDefault >
588 virtual ~LocalParameter ()
591 virtual ParamDefault *clone ()
const
593 return new ParamDefault(
static_cast< const ParamDefault &
>( *
this ) );
601 const std::string rmPrefix_;
602 std::unordered_map<std::string,std::string> dict_;
603 ParameterDict(
const std::string &rmPrefix,
604 const std::unordered_map<std::string,std::string> &dict )
605 : rmPrefix_(rmPrefix), dict_(dict) {}
606 const std::string* operator()(
const std::string &key,
const std::string *def )
610 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
613 assert(key.size()>rmPrefix_.size());
614 std::string reducedKey = key.substr(rmPrefix_.size(),key.size());
615 auto pos = dict_.find( reducedKey );
616 if (pos != dict_.end())
617 return &(pos->second);
623 if( *def == Dune::Fem::checkParameterExistsString() )
634 ParameterReader parameterDict (
635 const std::string &rmPrefix,
636 const std::unordered_map<std::string,std::string> &dict )
638 return Fem::ParameterReader( ParameterDict(rmPrefix,dict) );
640 ParameterReader parameterDict (
641 const std::unordered_map<std::string,std::string> &dict )
643 return parameterDict(
"",dict);
648 std::pair<std::string,std::string> convertValueToString(
const std::pair<const char*,const char*> &keyValue)
651 std::pair<std::string,std::string> convertValueToString(
const std::pair<const char*,V> &keyValue)
652 {
return std::make_pair(keyValue.first,std::to_string(keyValue.second)); }
653 std::pair<std::string,std::string> convertValueToString(
const std::pair<std::string,const char*> &keyValue)
656 std::pair<std::string,std::string> convertValueToString(
const std::pair<std::string,V> &keyValue)
657 {
return std::make_pair(keyValue.first,std::to_string(keyValue.second)); }
663 template<
class... Values>
664 ParameterReader parameterDict (
const std::string &rmPrefix, std::pair< const char*, Values >
const &... keyValues)
666 std::unordered_map<std::string,std::string> dict;
667 dict.insert({convertValueToString(keyValues)...});
668 return parameterDict( rmPrefix, dict);
674 void insertIntoMap(std::unordered_map<std::string,std::string> &dict,
675 const std::string &key,
const V &v)
676 { dict.insert(convertValueToString( std::make_pair(key,v) )); }
677 template<
class V,
class... Values>
678 void insertIntoMap(std::unordered_map<std::string,std::string> &dict,
679 const std::string &key,
const V &v, Values... keyValues)
681 dict.insert(convertValueToString( std::make_pair(key,v) ));
682 insertIntoMap(dict,keyValues...);
685 template<
class... Values>
686 ParameterReader parameterDict (
const std::string &rmPrefix, Values... keyValues)
688 std::unordered_map<std::string,std::string> dict;
689 insertIntoMap(dict,keyValues...);
690 return parameterDict( rmPrefix, dict);
695 typedef std::function<std::string()> LambdaType;
696 typedef std::unordered_map<std::string,LambdaType> DictType;
697 template <
class... KeyValues>
698 ParameterDict(
const std::string &rmPrefix, KeyValues... keyValues)
699 : rmPrefix_(rmPrefix), dict_()
701 insertIntoMap(keyValues...);
703 const std::string* operator()(
const std::string &key,
const std::string *def )
707 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
710 assert(key.size()>rmPrefix_.size());
711 std::string reducedKey = key.substr(rmPrefix_.size(),key.size());
712 auto pos = dict_.find( reducedKey );
713 if (pos != dict_.end())
715 tmp_ = pos->second();
726 else if( *def == Dune::Fem::checkParameterExistsString() )
738 static std::string convertValueToString(
const std::string &value)
740 static std::string convertValueToString(
const char* value)
743 static std::string convertValueToString(
const V &value)
744 {
return ParameterParser<double>::toString(value); }
746 template<class V, std::enable_if_t<std::is_convertible<V,const LambdaType&>::value,
int> i=0>
748 { dict_.insert( std::make_pair(key, v )); }
751 -> void_t< decltype(std::declval<const V&>()()) >
752 { dict_.insert( std::make_pair(key, [v]() {
return convertValueToString(v()); } )); }
755 { dict_.insert( std::make_pair(key, [v]() {
return convertValueToString(v); } )); }
758 void insertIntoMap(
const std::string &key,
const V &v)
759 { insertIntoMap_(key,v,PriorityTag<42>()); }
760 template<
class V,
class... Values>
761 void insertIntoMap(
const std::string &key,
const V &v, Values... keyValues)
763 insertIntoMap_(key,v,PriorityTag<42>());
764 insertIntoMap(keyValues...);
768 const std::string rmPrefix_;
772 template<
class... KeyValues>
773 ParameterReader parameterDict (
const std::string &rmPrefix, KeyValues... keyValues)
775 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:456
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:422
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:459
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:520
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:491
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:450
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:531
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