dune-fem 2.8.0
Loading...
Searching...
No Matches
io/parameter.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_PARAMETER_HH
2#define DUNE_FEM_PARAMETER_HH
3
4#include <fstream>
5#include <iostream>
6#include <string>
7#include <unordered_map>
8
9#include <dune/fem/io/io.hh>
13
15
16namespace Dune
17{
18
19 namespace Fem
20 {
21
178 // Parameter
179 // ---------
180
191 {
192 public:
194 {
196 }
197
208 static void append ( int &argc, char **argv ) { container().append( argc, argv ); }
209
215 static void append ( const std::string &key, const std::string &value, const bool force = false ) { container().append( key, value, force ); }
216
222 template<class NumberType>
223 static void append ( const std::string &key, const NumberType &value, const bool force = false ) { container().append( key, value, force ); }
224
229 static void append ( const std::string &filename ) { container().append( filename ); }
230
238 static void appendDGF ( const std::string &filename ) { container().appendDGF( filename ); }
239
247 static bool exists ( const std::string &key ) { return container().exists( key ); }
248
257 template< class T >
258 static void get ( const std::string &key, T &value )
259 {
260 container().get( key, value );
261 }
262
272 template< class T >
273 static void get ( const std::string &key, const T &defaultValue, T &value )
274 {
275 container().get( key, defaultValue, value );
276 }
277
287 static void get ( const std::string &key, const char* defaultValue, std::string &value )
288 {
289 container().get( key, defaultValue, value );
290 }
291
301 template< class T, class Validator >
302 static void getValid ( const std::string &key, const Validator &validator, T &value )
303 {
304 container().getValid( key, validator, value );
305 }
306
317 template< class T, class Validator >
318 static void getValid ( const std::string &key, const T &defaultValue, const Validator &validator, T &value )
319 {
320 container().getValid( key, validator, value );
321 }
322
332 template< class T >
333 static T getValue ( const std::string &key )
334 {
335 return container().getValue< T >( key );
336 }
337
348 template< class T >
349 static T getValue ( const std::string &key, const T &defaultValue )
350 {
351 return container().getValue( key, defaultValue );
352 }
353
364 template< class T, class Validator >
365 static T getValidValue ( const std::string &key, const Validator &validator )
366 {
367 return container().getValidValue( key, validator );
368 }
369
370
382 template< class T, class Validator >
383 static T getValidValue ( const std::string &key, const T &defaultValue, const Validator &validator )
384 {
385 return container().getValidValue( key, defaultValue, validator );
386 }
387
388 template< int n >
389 static int getEnum ( const std::string &key, const std::string (&values)[ n ] )
390 {
391 return container().getEnum( key, values );
392 }
393
394 template< int n >
395 static int getEnum ( const std::string &key, const std::string (&values)[ n ], int defaultValue )
396 {
397 return container().getEnum( key, values, defaultValue );
398 }
399
411 static std::string commonOutputPath ()
412 {
413 return container().commonOutputPath();
414 }
415
430 static std::string outputPath ();
431
439 static std::string commonInputPath ()
440 {
441 return container().commonInputPath();
442 }
443
445 static bool verbose () { return container().verbose(); }
446
462 static void write ( const std::string &filename, const std::string &fileextension ="", bool writeAll = true );
463
477 static void write ( std::ostream &out, bool writeAll = true ) { return container().write( out, writeAll ); }
478
479 protected:
480 friend class PersistenceManager ;
481
497 static void write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll = true );
498 };
499
500
501
502 // Implementation of Parameter
503 // ---------------------------
504
505 inline std::string Parameter::outputPath ()
506 {
507 std::string path = commonOutputPath();
508 if( path.empty() )
509 path = "./";
510 if( path[ path.length()-1 ] != '/' )
511 path += '/';
512 return path + "p" + std::to_string( MPIManager::rank() );
513 }
514
515
516 inline void Parameter::write ( const std::string &filename, const std::string &fileextension, bool writeAll )
517 {
518 // only write one parameter log file
519 // to the common path
520 if( MPIManager::rank() == 0 )
521 write( commonOutputPath(), filename, fileextension, writeAll );
522 }
523
524
525 inline void Parameter::write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll )
526 {
527 //create path if it does not exist
528 if( !directoryExists( path ) )
530
531 std::string fullname( path );
532 fullname += "/";
533 fullname += filename;
534 fullname += fileextension;
535
536 std::ofstream file( fullname );
537 if( file.is_open() )
538 write( file, writeAll );
539 else
540 std::cerr << "Warning: Unable to write parameter file '" << fullname << "'" << std::endl;
541 }
542
543
544
545 // LocalParameter
546 // --------------
547
548 // Helper class for Parameter structures for classes
549 template< class ParamDefault, class ParamImpl >
550 struct LocalParameter : public ParamDefault
551 {
553 {}
554
555 virtual ParamDefault *clone () const
556 {
557 return new ParamImpl( asImp() );
558 }
559
560 template <class... Args>
561 LocalParameter( Args... args ) : ParamDefault( args... ) {}
562
563 protected:
564 const ParamImpl &asImp () const
565 {
566 return static_cast< const ParamImpl & >( *this );
567 }
568 };
569
570 template< class ParamDefault >
571 struct LocalParameter< ParamDefault, ParamDefault >
572 {
574 {}
575
576 virtual ParamDefault *clone () const
577 {
578 return new ParamDefault( static_cast< const ParamDefault & >( *this ) );
579 }
580 };
581
582#if 0
583 struct ParameterDict
584 {
585 std::string tmp_;
586 const std::string rmPrefix_;
587 std::unordered_map<std::string,std::string> dict_;
588 ParameterDict(const std::string &rmPrefix,
589 const std::unordered_map<std::string,std::string> &dict )
590 : rmPrefix_(rmPrefix), dict_(dict) {}
591 const std::string* operator()( const std::string &key, const std::string *def )
592 {
593 // first determine if the `prefix` of the provided key corresponds
594 // to the prefix to be removed:
595 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
596 {
597 // check the provided map - stripping prefix
598 assert(key.size()>rmPrefix_.size());
599 std::string reducedKey = key.substr(rmPrefix_.size(),key.size());
600 auto pos = dict_.find( reducedKey );
601 if (pos != dict_.end())
602 return &(pos->second);
603 }
604 // the key either does not have the correct prefix or it was not
605 // found in the provided map so check the global Parameter container
606 if( !Fem::Parameter::exists( key ) )
607 {
609 return nullptr; // this call was simply to check if key exists
610 return def; // return default
611 }
612 if (def)
613 Fem::Parameter::get( key, *def, tmp_ );
614 else
615 Fem::Parameter::get( key, tmp_ );
616 return &tmp_;
617 }
618 };
620 const std::string &rmPrefix,
621 const std::unordered_map<std::string,std::string> &dict )
622 {
623 return Fem::ParameterReader( ParameterDict(rmPrefix,dict) );
624 }
626 const std::unordered_map<std::string,std::string> &dict )
627 {
628 return parameterDict("",dict);
629 }
630#endif
631#if 0
632 namespace {
633 std::pair<std::string,std::string> convertValueToString(const std::pair<const char*,const char*> &keyValue)
634 { return keyValue; }
635 template <class V>
636 std::pair<std::string,std::string> convertValueToString(const std::pair<const char*,V> &keyValue)
637 { return std::make_pair(keyValue.first,std::to_string(keyValue.second)); }
638 std::pair<std::string,std::string> convertValueToString(const std::pair<std::string,const char*> &keyValue)
639 { return keyValue; }
640 template <class V>
641 std::pair<std::string,std::string> convertValueToString(const std::pair<std::string,V> &keyValue)
642 { return std::make_pair(keyValue.first,std::to_string(keyValue.second)); }
643 }
644#endif
645#if 0
646 // this solution needs the user to call the function using `make_pair`
647 // becuase {"key",value} will not be deduced correctly
648 template<class... Values>
649 ParameterReader parameterDict (const std::string &rmPrefix, std::pair< const char*, Values > const &... keyValues)
650 {
651 std::unordered_map<std::string,std::string> dict;
652 dict.insert({convertValueToString(keyValues)...});
653 return parameterDict( rmPrefix, dict);
654 }
655#endif
656#if 0
657 namespace {
658 template<class V>
659 void insertIntoMap(std::unordered_map<std::string,std::string> &dict,
660 const std::string &key, const V &v)
661 { dict.insert(convertValueToString( std::make_pair(key,v) )); }
662 template<class V, class... Values>
663 void insertIntoMap(std::unordered_map<std::string,std::string> &dict,
664 const std::string &key, const V &v, Values... keyValues)
665 {
666 dict.insert(convertValueToString( std::make_pair(key,v) ));
667 insertIntoMap(dict,keyValues...);
668 }
669 }
670 template<class... Values>
671 ParameterReader parameterDict (const std::string &rmPrefix, Values... keyValues)
672 {
673 std::unordered_map<std::string,std::string> dict;
674 insertIntoMap(dict,keyValues...);
675 return parameterDict( rmPrefix, dict);
676 }
677#endif
679 {
680 typedef std::function<std::string()> LambdaType;
681 typedef std::unordered_map<std::string,LambdaType> DictType;
682 template <class... KeyValues>
683 ParameterDict(const std::string &rmPrefix, KeyValues... keyValues)
684 : rmPrefix_(rmPrefix), dict_()
685 {
686 insertIntoMap(keyValues...);
687 }
688 const std::string* operator()( const std::string &key, const std::string *def )
689 {
690 // first determine if the `prefix` of the provided key corresponds
691 // to the prefix to be removed:
692 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
693 {
694 // check the provided map - stripping prefix
695 assert(key.size()>rmPrefix_.size());
696 std::string reducedKey = key.substr(rmPrefix_.size(),key.size());
697 auto pos = dict_.find( reducedKey );
698 if (pos != dict_.end())
699 {
700 tmp_ = pos->second();
701 return &tmp_;
702 }
703 // need to check global parameter set
704 }
705 // the key either does not have the correct prefix or it was not
706 // found in the provided map so check the global Parameter container
707 if( !Fem::Parameter::exists( key ) )
708 {
709 if (def == nullptr)
710 return nullptr;
711 else if( *def == Dune::Fem::checkParameterExistsString() )
712 return nullptr; // this call was simply to check if key exists
713 return def; // return default
714 }
715 if (def)
716 Fem::Parameter::get( key, *def, tmp_ );
717 else
718 Fem::Parameter::get( key, tmp_ );
719 return &tmp_;
720 }
721
722 private:
723 static std::string convertValueToString(const std::string &value)
724 { return value; }
725 static std::string convertValueToString(const char* value)
726 { return value; }
727 template <class V>
728 static std::string convertValueToString(const V &value)
729 { return ParameterParser<double>::toString(value); }
730
731 template<class V, std::enable_if_t<std::is_convertible<V,const LambdaType&>::value,int> i=0>
732 void insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<2>)
733 { dict_.insert( std::make_pair(key, v )); }
734 template<class V>
735 auto insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<1>)
736 -> void_t< decltype(std::declval<const V&>()()) >
737 { dict_.insert( std::make_pair(key, [v]() { return convertValueToString(v()); } )); }
738 template<class V>
739 void insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<0>)
740 { dict_.insert( std::make_pair(key, [v]() { return convertValueToString(v); } )); }
741
742 template<class V>
743 void insertIntoMap(const std::string &key, const V &v)
744 { insertIntoMap_(key,v,PriorityTag<42>()); }
745 template<class V, class... Values>
746 void insertIntoMap(const std::string &key, const V &v, Values... keyValues)
747 {
748 insertIntoMap_(key,v,PriorityTag<42>());
749 insertIntoMap(keyValues...);
750 }
751
752 std::string tmp_;
753 const std::string rmPrefix_;
754 DictType dict_;
755 };
756
757 template<class... KeyValues>
758 ParameterReader parameterDict (const std::string &rmPrefix, KeyValues... keyValues)
759 {
760 return Fem::ParameterReader( ParameterDict(rmPrefix,keyValues...) );
761 }
762 } // namespace Fem
763
764} // namespace Dune
765
766#endif // #ifndef DUNE_FEM_PARAMETER_HH
std::string path
Definition: readioparams.cc:156
Definition: bindguard.hh:11
ParameterReader parameterDict(const std::string &rmPrefix, KeyValues... keyValues)
Definition: io/parameter.hh:758
static const std::string & checkParameterExistsString()
Definition: reader.hh:20
bool createDirectory(const std::string &inName)
create a directory
Definition: io.cc:19
BasicParameterReader< std::function< const std::string *(const std::string &, const std::string *) > > ParameterReader
Definition: reader.hh:315
bool directoryExists(const std::string &name)
check whether a directory exists
Definition: io.cc:58
class with singleton instance managing all persistent objects
Definition: persistencemanager.hh:141
Container for User Specified Parameters.
Definition: io/parameter.hh:191
static ParameterContainer & container()
Definition: io/parameter.hh:193
static bool verbose()
obtain the cached value for fem.verbose
Definition: io/parameter.hh:445
static T getValue(const std::string &key, const T &defaultValue)
get an optional parameter from the container
Definition: io/parameter.hh:349
static void getValid(const std::string &key, const Validator &validator, T &value)
get a mandatory parameter from the container
Definition: io/parameter.hh:302
static std::string commonOutputPath()
obtain common output path
Definition: io/parameter.hh:411
static T getValidValue(const std::string &key, const T &defaultValue, const Validator &validator)
get an optional parameter from the container
Definition: io/parameter.hh:383
static int getEnum(const std::string &key, const std::string(&values)[n], int defaultValue)
Definition: io/parameter.hh:395
static void get(const std::string &key, T &value)
get a mandatory parameter from the container
Definition: io/parameter.hh:258
static void append(const std::string &filename)
add parameters from a file to the container
Definition: io/parameter.hh:229
static T getValue(const std::string &key)
get a mandatory parameter from the container
Definition: io/parameter.hh:333
static std::string outputPath()
obtain unique output path for this process
Definition: io/parameter.hh:505
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: io/parameter.hh:287
static T getValidValue(const std::string &key, const Validator &validator)
get an optional parameter from the container
Definition: io/parameter.hh:365
static void write(std::ostream &out, bool writeAll=true)
write the parameter database to a stream
Definition: io/parameter.hh:477
static void append(const std::string &key, const std::string &value, const bool force=false)
add a single parameter to the container
Definition: io/parameter.hh:215
static bool exists(const std::string &key)
find out, whether a parameter is defined in the container
Definition: io/parameter.hh:247
static void get(const std::string &key, const T &defaultValue, T &value)
get an optional parameter from the container
Definition: io/parameter.hh:273
static void append(int &argc, char **argv)
add parameters from the command line RangeType gRight;
Definition: io/parameter.hh:208
static std::string commonInputPath()
obtain common input path
Definition: io/parameter.hh:439
static void getValid(const std::string &key, const T &defaultValue, const Validator &validator, T &value)
get an optional parameter from the container
Definition: io/parameter.hh:318
static int getEnum(const std::string &key, const std::string(&values)[n])
Definition: io/parameter.hh:389
static void appendDGF(const std::string &filename)
add parameters from a DGF file to the container
Definition: io/parameter.hh:238
static void append(const std::string &key, const NumberType &value, const bool force=false)
add a single parameter to the container
Definition: io/parameter.hh:223
static void write(const std::string &filename, const std::string &fileextension="", bool writeAll=true)
write the parameter database to a file
Definition: io/parameter.hh:516
Definition: io/parameter.hh:551
virtual ParamDefault * clone() const
Definition: io/parameter.hh:555
LocalParameter(Args... args)
Definition: io/parameter.hh:561
virtual ~LocalParameter()
Definition: io/parameter.hh:552
const ParamImpl & asImp() const
Definition: io/parameter.hh:564
virtual ParamDefault * clone() const
Definition: io/parameter.hh:576
virtual ~LocalParameter()
Definition: io/parameter.hh:573
Definition: io/parameter.hh:679
ParameterDict(const std::string &rmPrefix, KeyValues... keyValues)
Definition: io/parameter.hh:683
std::unordered_map< std::string, LambdaType > DictType
Definition: io/parameter.hh:681
std::function< std::string()> LambdaType
Definition: io/parameter.hh:680
const std::string * operator()(const std::string &key, const std::string *def)
Definition: io/parameter.hh:688
Definition: container.hh:74
std::string commonInputPath() const
Definition: container.hh:179
void write(std::ostream &out, bool writeAll=true) const
write the parameter database to a stream
Definition: container.hh:584
bool verbose() const
obtain the cached value for fem.verbose
Definition: container.hh:177
void append(int &argc, char **argv)
add parameters from the command line
Definition: container.hh:529
void appendDGF(const std::string &filename)
add parameters from a DGF file
Definition: container.hh:549
std::string commonOutputPath() const
Definition: container.hh:184
static std::string toString(const T &value)
Definition: parser.hh:35
int getEnum(const std::string &key, const std::string(&values)[n]) const
Definition: reader.hh:225
void get(const std::string &key, T &value) const
get mandatory parameter
Definition: reader.hh:59
T getValue(const std::string &key) const
get mandatory parameter
Definition: reader.hh:159
bool exists(const std::string &key) const
check, whether a parameter is defined
Definition: reader.hh:44
T getValidValue(const std::string &key, const Validator &validator) const
get optional parameter
Definition: reader.hh:197
void getValid(const std::string &key, const Validator &validator, T &value) const
get mandatory parameter
Definition: reader.hh:118
static int rank()
Definition: mpimanager.hh:401
return singleton instance of given Object type.
Definition: singleton.hh:88