DUNE-FEM (unstable)

parameter.hh
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>
10 #include <dune/fem/io/parameter/exceptions.hh>
11 #include <dune/fem/io/parameter/container.hh>
12 #include <dune/fem/io/parameter/reader.hh>
13 
14 #include <dune/fem/storage/singleton.hh>
15 
16 namespace Dune
17 {
18 
19  namespace Fem
20  {
21 
178  // Parameter
179  // ---------
180 
190  class Parameter
191  {
192  public:
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;
198 
199  static ParameterContainer &container ()
200  {
202  }
203 
204  static auto &localParameterLog ()
205  {
206  return container().localParameterLog_;
207  }
208 
219  static void append ( int &argc, char **argv ) { container().append( argc, argv ); }
220 
226  static void append ( const std::string &key, const std::string &value, const bool force = false ) { container().append( key, value, force ); }
227 
233  template<class NumberType>
234  static void append ( const std::string &key, const NumberType &value, const bool force = false ) { container().append( key, value, force ); }
235 
240  static void append ( const std::string &filename ) { container().append( filename ); }
241 
249  static void appendDGF ( const std::string &filename ) { container().appendDGF( filename ); }
250 
258  static bool exists ( const std::string &key ) { return container().exists( key ); }
259 
268  template< class T >
269  static void get ( const std::string &key, T &value )
270  {
271  container().get( key, value );
272  }
273 
283  template< class T >
284  static void get ( const std::string &key, const T &defaultValue, T &value )
285  {
286  container().get( key, defaultValue, value );
287  }
288 
298  static void get ( const std::string &key, const char* defaultValue, std::string &value )
299  {
300  container().get( key, defaultValue, value );
301  }
302 
312  template< class T, class Validator >
313  static void getValid ( const std::string &key, const Validator &validator, T &value )
314  {
315  container().getValid( key, validator, value );
316  }
317 
328  template< class T, class Validator >
329  static void getValid ( const std::string &key, const T &defaultValue, const Validator &validator, T &value )
330  {
331  container().getValid( key, validator, value );
332  }
333 
343  template< class T >
344  static T getValue ( const std::string &key )
345  {
346  return container().getValue< T >( key );
347  }
348 
359  template< class T >
360  static T getValue ( const std::string &key, const T &defaultValue )
361  {
362  return container().getValue( key, defaultValue );
363  }
364 
375  template< class T, class Validator >
376  static T getValidValue ( const std::string &key, const Validator &validator )
377  {
378  return container().getValidValue( key, validator );
379  }
380 
381 
393  template< class T, class Validator >
394  static T getValidValue ( const std::string &key, const T &defaultValue, const Validator &validator )
395  {
396  return container().getValidValue( key, defaultValue, validator );
397  }
398 
399  template< int n >
400  static int getEnum ( const std::string &key, const std::string (&values)[ n ] )
401  {
402  return container().getEnum( key, values );
403  }
404 
405  template< int n >
406  static int getEnum ( const std::string &key, const std::string (&values)[ n ], int defaultValue )
407  {
408  return container().getEnum( key, values, defaultValue );
409  }
410 
422  static std::string commonOutputPath ()
423  {
424  return container().commonOutputPath();
425  }
426 
441  static std::string outputPath ();
442 
450  static std::string commonInputPath ()
451  {
452  return container().commonInputPath();
453  }
454 
456  static bool verbose () { return container().verbose(); }
457 
459  static bool verbose ( const int level ) { return container().verbose(level ); }
460 
476  static void write ( const std::string &filename, const std::string &fileextension ="", bool writeAll = true );
477 
491  static void write ( std::ostream &out, bool writeAll = true ) { return container().write( out, writeAll ); }
492  static auto write () { return container().write(); }
493 
494  protected:
495  friend class PersistenceManager ;
496 
512  static void write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll = true );
513  };
514 
515 
516 
517  // Implementation of Parameter
518  // ---------------------------
519 
520  inline std::string Parameter::outputPath ()
521  {
522  std::string path = commonOutputPath();
523  if( path.empty() )
524  path = "./";
525  if( path[ path.length()-1 ] != '/' )
526  path += '/';
527  return path + "p" + std::to_string( MPIManager::rank() );
528  }
529 
530 
531  inline void Parameter::write ( const std::string &filename, const std::string &fileextension, bool writeAll )
532  {
533  // only write one parameter log file
534  // to the common path
535  if( MPIManager::rank() == 0 )
536  write( commonOutputPath(), filename, fileextension, writeAll );
537  }
538 
539 
540  inline void Parameter::write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll )
541  {
542  //create path if it does not exist
543  if( !directoryExists( path ) )
544  createDirectory( path );
545 
546  std::string fullname( path );
547  fullname += "/";
548  fullname += filename;
549  fullname += fileextension;
550 
551  std::ofstream file( fullname );
552  if( file.is_open() )
553  write( file, writeAll );
554  else
555  std::cerr << "Warning: Unable to write parameter file '" << fullname << "'" << std::endl;
556  }
557 
558 
559 
560  // LocalParameter
561  // --------------
562 
563  // Helper class for Parameter structures for classes
564  template< class ParamDefault, class ParamImpl >
565  struct LocalParameter : public ParamDefault
566  {
567  virtual ~LocalParameter ()
568  {}
569 
570  virtual ParamDefault *clone () const
571  {
572  return new ParamImpl( asImp() );
573  }
574 
575  template <class... Args>
576  LocalParameter( Args... args ) : ParamDefault( args... ) {}
577 
578  protected:
579  const ParamImpl &asImp () const
580  {
581  return static_cast< const ParamImpl & >( *this );
582  }
583  };
584 
585  template< class ParamDefault >
586  struct LocalParameter< ParamDefault, ParamDefault >
587  {
588  virtual ~LocalParameter ()
589  {}
590 
591  virtual ParamDefault *clone () const
592  {
593  return new ParamDefault( static_cast< const ParamDefault & >( *this ) );
594  }
595  };
596 
597 #if 0
598  struct ParameterDict
599  {
600  std::string tmp_;
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 )
607  {
608  // first determine if the `prefix` of the provided key corresponds
609  // to the prefix to be removed:
610  if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
611  {
612  // check the provided map - stripping prefix
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);
618  }
619  // the key either does not have the correct prefix or it was not
620  // found in the provided map so check the global Parameter container
621  if( !Fem::Parameter::exists( key ) )
622  {
623  if( *def == Dune::Fem::checkParameterExistsString() )
624  return nullptr; // this call was simply to check if key exists
625  return def; // return default
626  }
627  if (def)
628  Fem::Parameter::get( key, *def, tmp_ );
629  else
630  Fem::Parameter::get( key, tmp_ );
631  return &tmp_;
632  }
633  };
634  ParameterReader parameterDict (
635  const std::string &rmPrefix,
636  const std::unordered_map<std::string,std::string> &dict )
637  {
638  return Fem::ParameterReader( ParameterDict(rmPrefix,dict) );
639  }
640  ParameterReader parameterDict (
641  const std::unordered_map<std::string,std::string> &dict )
642  {
643  return parameterDict("",dict);
644  }
645 #endif
646 #if 0
647  namespace {
648  std::pair<std::string,std::string> convertValueToString(const std::pair<const char*,const char*> &keyValue)
649  { return keyValue; }
650  template <class V>
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)
654  { return keyValue; }
655  template <class V>
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)); }
658  }
659 #endif
660 #if 0
661  // this solution needs the user to call the function using `make_pair`
662  // becuase {"key",value} will not be deduced correctly
663  template<class... Values>
664  ParameterReader parameterDict (const std::string &rmPrefix, std::pair< const char*, Values > const &... keyValues)
665  {
666  std::unordered_map<std::string,std::string> dict;
667  dict.insert({convertValueToString(keyValues)...});
668  return parameterDict( rmPrefix, dict);
669  }
670 #endif
671 #if 0
672  namespace {
673  template<class V>
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)
680  {
681  dict.insert(convertValueToString( std::make_pair(key,v) ));
682  insertIntoMap(dict,keyValues...);
683  }
684  }
685  template<class... Values>
686  ParameterReader parameterDict (const std::string &rmPrefix, Values... keyValues)
687  {
688  std::unordered_map<std::string,std::string> dict;
689  insertIntoMap(dict,keyValues...);
690  return parameterDict( rmPrefix, dict);
691  }
692 #endif
693  struct ParameterDict
694  {
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_()
700  {
701  insertIntoMap(keyValues...);
702  }
703  const std::string* operator()( const std::string &key, const std::string *def )
704  {
705  // first determine if the `prefix` of the provided key corresponds
706  // to the prefix to be removed:
707  if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
708  {
709  // check the provided map - stripping prefix
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())
714  {
715  tmp_ = pos->second();
716  return &tmp_;
717  }
718  // need to check global parameter set
719  }
720  // the key either does not have the correct prefix or it was not
721  // found in the provided map so check the global Parameter container
722  if( !Fem::Parameter::exists( key ) )
723  {
724  if (def == nullptr)
725  return nullptr;
726  else if( *def == Dune::Fem::checkParameterExistsString() )
727  return nullptr; // this call was simply to check if key exists
728  return def; // return default
729  }
730  if (def)
731  Fem::Parameter::get( key, *def, tmp_ );
732  else
733  Fem::Parameter::get( key, tmp_ );
734  return &tmp_;
735  }
736 
737  private:
738  static std::string convertValueToString(const std::string &value)
739  { return value; }
740  static std::string convertValueToString(const char* value)
741  { return value; }
742  template <class V>
743  static std::string convertValueToString(const V &value)
744  { return ParameterParser<double>::toString(value); }
745 
746  template<class V, std::enable_if_t<std::is_convertible<V,const LambdaType&>::value,int> i=0>
747  void insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<2>)
748  { dict_.insert( std::make_pair(key, v )); }
749  template<class V>
750  auto insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<1>)
751  -> void_t< decltype(std::declval<const V&>()()) >
752  { dict_.insert( std::make_pair(key, [v]() { return convertValueToString(v()); } )); }
753  template<class V>
754  void insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<0>)
755  { dict_.insert( std::make_pair(key, [v]() { return convertValueToString(v); } )); }
756 
757  template<class 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)
762  {
763  insertIntoMap_(key,v,PriorityTag<42>());
764  insertIntoMap(keyValues...);
765  }
766 
767  std::string tmp_;
768  const std::string rmPrefix_;
769  DictType dict_;
770  };
771 
772  template<class... KeyValues>
773  ParameterReader parameterDict (const std::string &rmPrefix, KeyValues... keyValues)
774  {
775  return Fem::ParameterReader( ParameterDict(rmPrefix,keyValues...) );
776  }
777  } // namespace Fem
778 
779 } // namespace Dune
780 
781 #endif // #ifndef DUNE_FEM_PARAMETER_HH
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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)