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
16namespace Dune
17{
18
19 namespace Fem
20 {
21
178 // Parameter
179 // ---------
180
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
411 static int getEnum ( const std::string &key, const std::vector<std::string> &values )
412 {
413 return container().getEnum( key, values );
414 }
415
416 static int getEnum ( const std::string &key, const std::vector<std::string> &values, int defaultValue )
417 {
418 return container().getEnum( key, values, defaultValue );
419 }
420
432 static std::string commonOutputPath ()
433 {
434 return container().commonOutputPath();
435 }
436
451 static std::string outputPath ();
452
460 static std::string commonInputPath ()
461 {
462 return container().commonInputPath();
463 }
464
466 static bool verbose () { return container().verbose(); }
467
469 static bool verbose ( const int level ) { return container().verbose(level ); }
470
486 static void write ( const std::string &filename, const std::string &fileextension ="", bool writeAll = true );
487
501 static void write ( std::ostream &out, bool writeAll = true ) { return container().write( out, writeAll ); }
502 static auto write () { return container().write(); }
503
504 protected:
505 friend class PersistenceManager ;
506
522 static void write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll = true );
523 };
524
525
526
527 // Implementation of Parameter
528 // ---------------------------
529
530 inline std::string Parameter::outputPath ()
531 {
532 std::string path = commonOutputPath();
533 if( path.empty() )
534 path = "./";
535 if( path[ path.length()-1 ] != '/' )
536 path += '/';
537 return path + "p" + std::to_string( MPIManager::rank() );
538 }
539
540
541 inline void Parameter::write ( const std::string &filename, const std::string &fileextension, bool writeAll )
542 {
543 // only write one parameter log file
544 // to the common path
545 if( MPIManager::rank() == 0 )
546 write( commonOutputPath(), filename, fileextension, writeAll );
547 }
548
549
550 inline void Parameter::write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll )
551 {
552 //create path if it does not exist
553 if( !directoryExists( path ) )
554 createDirectory( path );
555
556 std::string fullname( path );
557 fullname += "/";
558 fullname += filename;
559 fullname += fileextension;
560
561 std::ofstream file( fullname );
562 if( file.is_open() )
563 write( file, writeAll );
564 else
565 std::cerr << "Warning: Unable to write parameter file '" << fullname << "'" << std::endl;
566 }
567
568
569
570 // LocalParameter
571 // --------------
572
573 // Helper class for Parameter structures for classes
574 template< class ParamDefault, class ParamImpl >
575 struct LocalParameter : public ParamDefault
576 {
577 virtual ~LocalParameter ()
578 {}
579
580 virtual ParamDefault *clone () const
581 {
582 return new ParamImpl( asImp() );
583 }
584
585 template <class... Args>
586 LocalParameter( Args... args ) : ParamDefault( args... ) {}
587
588 protected:
589 const ParamImpl &asImp () const
590 {
591 return static_cast< const ParamImpl & >( *this );
592 }
593 };
594
595 template< class ParamDefault >
596 struct LocalParameter< ParamDefault, ParamDefault >
597 {
598 virtual ~LocalParameter ()
599 {}
600
601 virtual ParamDefault *clone () const
602 {
603 return new ParamDefault( static_cast< const ParamDefault & >( *this ) );
604 }
605 };
606
607#if 0
608 struct ParameterDict
609 {
610 std::string tmp_;
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 )
617 {
618 // first determine if the `prefix` of the provided key corresponds
619 // to the prefix to be removed:
620 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
621 {
622 // check the provided map - stripping prefix
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);
628 }
629 // the key either does not have the correct prefix or it was not
630 // found in the provided map so check the global Parameter container
631 if( !Fem::Parameter::exists( key ) )
632 {
633 if( *def == Dune::Fem::checkParameterExistsString() )
634 return nullptr; // this call was simply to check if key exists
635 return def; // return default
636 }
637 if (def)
638 Fem::Parameter::get( key, *def, tmp_ );
639 else
640 Fem::Parameter::get( key, tmp_ );
641 return &tmp_;
642 }
643 };
644 ParameterReader parameterDict (
645 const std::string &rmPrefix,
646 const std::unordered_map<std::string,std::string> &dict )
647 {
648 return Fem::ParameterReader( ParameterDict(rmPrefix,dict) );
649 }
650 ParameterReader parameterDict (
651 const std::unordered_map<std::string,std::string> &dict )
652 {
653 return parameterDict("",dict);
654 }
655#endif
656#if 0
657 namespace {
658 std::pair<std::string,std::string> convertValueToString(const std::pair<const char*,const char*> &keyValue)
659 { return keyValue; }
660 template <class V>
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)
664 { return keyValue; }
665 template <class V>
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)); }
668 }
669#endif
670#if 0
671 // this solution needs the user to call the function using `make_pair`
672 // becuase {"key",value} will not be deduced correctly
673 template<class... Values>
674 ParameterReader parameterDict (const std::string &rmPrefix, std::pair< const char*, Values > const &... keyValues)
675 {
676 std::unordered_map<std::string,std::string> dict;
677 dict.insert({convertValueToString(keyValues)...});
678 return parameterDict( rmPrefix, dict);
679 }
680#endif
681#if 0
682 namespace {
683 template<class V>
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)
690 {
691 dict.insert(convertValueToString( std::make_pair(key,v) ));
692 insertIntoMap(dict,keyValues...);
693 }
694 }
695 template<class... Values>
696 ParameterReader parameterDict (const std::string &rmPrefix, Values... keyValues)
697 {
698 std::unordered_map<std::string,std::string> dict;
699 insertIntoMap(dict,keyValues...);
700 return parameterDict( rmPrefix, dict);
701 }
702#endif
703 struct ParameterDict
704 {
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_()
710 {
711 insertIntoMap(keyValues...);
712 }
713 const std::string* operator()( const std::string &key, const std::string *def )
714 {
715 // first determine if the `prefix` of the provided key corresponds
716 // to the prefix to be removed:
717 if (key.compare(0,rmPrefix_.size(),rmPrefix_) == 0)
718 {
719 // check the provided map - stripping prefix
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())
724 {
725 tmp_ = pos->second();
726 return &tmp_;
727 }
728 // need to check global parameter set
729 }
730 // the key either does not have the correct prefix or it was not
731 // found in the provided map so check the global Parameter container
732 if( !Fem::Parameter::exists( key ) )
733 {
734 if (def == nullptr)
735 return nullptr;
736 else if( *def == Dune::Fem::checkParameterExistsString() )
737 return nullptr; // this call was simply to check if key exists
738 return def; // return default
739 }
740 if (def)
741 Fem::Parameter::get( key, *def, tmp_ );
742 else
743 Fem::Parameter::get( key, tmp_ );
744 return &tmp_;
745 }
746
747 private:
748 static std::string convertValueToString(const std::string &value)
749 { return value; }
750 static std::string convertValueToString(const char* value)
751 { return value; }
752 template <class V>
753 static std::string convertValueToString(const V &value)
754 { return ParameterParser<double>::toString(value); }
755
756 template<class V, std::enable_if_t<std::is_convertible<V,const LambdaType&>::value,int> i=0>
757 void insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<2>)
758 { dict_.insert( std::make_pair(key, v )); }
759 template<class V>
760 auto insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<1>)
761 -> void_t< decltype(std::declval<const V&>()()) >
762 { dict_.insert( std::make_pair(key, [v]() { return convertValueToString(v()); } )); }
763 template<class V>
764 void insertIntoMap_(const std::string &key, const V &v, Dune::PriorityTag<0>)
765 { dict_.insert( std::make_pair(key, [v]() { return convertValueToString(v); } )); }
766
767 template<class 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)
772 {
773 insertIntoMap_(key,v,PriorityTag<42>());
774 insertIntoMap(keyValues...);
775 }
776
777 std::string tmp_;
778 const std::string rmPrefix_;
779 DictType dict_;
780 };
781
782 template<class... KeyValues>
783 ParameterReader parameterDict (const std::string &rmPrefix, KeyValues... keyValues)
784 {
785 return Fem::ParameterReader( ParameterDict(rmPrefix,keyValues...) );
786 }
787 } // namespace Fem
788
789} // namespace Dune
790
791#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: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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)