dune-fem  2.4.1-rc
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 
8 #include <dune/fem/io/io.hh>
12 
13 namespace Dune
14 {
15 
16  namespace Fem
17  {
18 
175  // Parameter
176  // ---------
177 
187  class Parameter
188  {
189  public:
191  {
193  return container;
194  }
195 
203  static int setVerboseRank( int verboseRank ) { return container().setVerboseRank( verboseRank ); }
204 
215  static void append ( int &argc, char **argv ) { container().append( argc, argv ); }
216 
222  static void append ( const std::string &key, const std::string &value ) { container().append( key, value ); }
223 
228  static void append ( const std::string &filename ) { container().append( filename ); }
229 
237  static void appendDGF ( const std::string &filename ) { container().appendDGF( filename ); }
238 
246  static bool exists ( const std::string &key ) { return container().exists( key ); }
247 
256  template< class T >
257  static void get ( const std::string &key, T &value )
258  {
259  container().get( key, value );
260  }
261 
271  template< class T >
272  static void get ( const std::string &key, const T &defaultValue, T &value )
273  {
274  container().get( key, defaultValue, value );
275  }
276 
286  static void get ( const std::string &key, const char* defaultValue, std::string &value )
287  {
288  container().get( key, defaultValue, value );
289  }
290 
300  template< class T, class Validator >
301  static void getValid ( const std::string &key, const Validator &validator, T &value )
302  {
303  container().getValid( key, validator, value );
304  }
305 
316  template< class T, class Validator >
317  static void getValid ( const std::string &key, const T &defaultValue, const Validator &validator, T &value )
318  {
319  container().getValid( key, validator, value );
320  }
321 
331  template< class T >
332  static T getValue ( const std::string &key )
333  {
334  return container().getValue< T >( key );
335  }
336 
347  template< class T >
348  static T getValue ( const std::string &key, const T &defaultValue )
349  {
350  return container().getValue( key, defaultValue );
351  }
352 
363  template< class T, class Validator >
364  static T getValidValue ( const std::string &key, const Validator &validator )
365  {
366  return container().getValidValue( key, validator );
367  }
368 
369 
381  template< class T, class Validator >
382  static T getValidValue ( const std::string &key, const T &defaultValue, const Validator &validator )
383  {
384  return container().getValidValue( key, defaultValue, validator );
385  }
386 
387  template< int n >
388  static int getEnum ( const std::string &key, const std::string (&values)[ n ] )
389  {
390  return container().getEnum( key, values );
391  }
392 
393  template< int n >
394  static int getEnum ( const std::string &key, const std::string (&values)[ n ], int defaultValue )
395  {
396  return container().getEnum( key, values, defaultValue );
397  }
398 
410  static std::string commonOutputPath ()
411  {
412  return container().commonOutputPath();
413  }
414 
429  static std::string outputPath ();
430 
438  static std::string commonInputPath ()
439  {
440  return container().commonInputPath();
441  }
442 
444  static bool verbose () { return container().verbose(); }
445 
461  static void write ( const std::string &filename, const std::string &fileextension ="", bool writeAll = true );
462 
476  static void write ( std::ostream &out, bool writeAll = true ) { return container().write( out, writeAll ); }
477 
478  protected:
479  friend class PersistenceManager ;
480 
496  static void write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll = true );
497  };
498 
499 
500 
501  // Implementation of Parameter
502  // ---------------------------
503 
504  inline std::string Parameter::outputPath ()
505  {
506  std::string path = commonOutputPath();
507  if( path.empty() )
508  path = "./";
509  if( path[ path.length()-1 ] != '/' )
510  path += '/';
511  return path + "p" + std::to_string( MPIManager::rank() );
512  }
513 
514 
515  inline void Parameter::write ( const std::string &filename, const std::string &fileextension, bool writeAll )
516  {
517  // only write one parameter log file
518  // to the common path
519  if( MPIManager::rank() == 0 )
520  write( commonOutputPath(), filename, fileextension, writeAll );
521  }
522 
523 
524  inline void Parameter::write ( const std::string &path, const std::string &filename, const std::string &fileextension, bool writeAll )
525  {
526  //create path if it does not exist
527  if( !directoryExists( path ) )
528  createDirectory( path );
529 
530  std::string fullname( path );
531  fullname += "/";
532  fullname += filename;
533  fullname += fileextension;
534 
535  std::ofstream file( fullname );
536  if( file.is_open() )
537  write( file, writeAll );
538  else
539  std::cerr << "Warning: Unable to write parameter file '" << fullname << "'" << std::endl;
540  }
541 
542 
543 
544  // LocalParameter
545  // --------------
546 
547  // Helper class for Parameter structures for classes
548  template< class ParamDefault, class ParamImpl >
550  : public ParamDefault
551  {
552  virtual ~LocalParameter ()
553  {}
554 
555  virtual ParamDefault *clone () const
556  {
557  return new ParamImpl( asImp() );
558  }
559 
560  protected:
561  const ParamImpl &asImp () const
562  {
563  return static_cast< const ParamImpl & >( *this );
564  }
565  };
566 
567  template< class ParamDefault >
568  struct LocalParameter< ParamDefault, ParamDefault >
569  {
570  virtual ~LocalParameter ()
571  {}
572 
573  virtual ParamDefault *clone () const
574  {
575  return new ParamDefault( static_cast< const ParamDefault & >( *this ) );
576  }
577  };
578 
579  } // namespace Fem
580 
581 } // namespace Dune
582 
583 #endif // #ifndef DUNE_FEM_PARAMETER_HH
void write(std::ostream &out, bool writeAll=true) const
write the parameter database to a stream
Definition: container.hh:539
static int rank()
Definition: mpimanager.hh:116
static void getValid(const std::string &key, const Validator &validator, T &value)
get a mandatory parameter from the container
Definition: io/parameter.hh:301
Container for User Specified Parameters.
Definition: io/parameter.hh:187
static T getValidValue(const std::string &key, const T &defaultValue, const Validator &validator)
get an optional parameter from the container
Definition: io/parameter.hh:382
static std::string commonOutputPath()
obtain common output path
Definition: io/parameter.hh:410
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:317
bool exists(const std::string &key) const
check, whether a parameter is defined
Definition: reader.hh:37
int getEnum(const std::string &key, const std::string(&values)[n]) const
Definition: reader.hh:215
class with singleton instance managing all persistent objects
Definition: persistencemanager.hh:136
const ParamImpl & asImp() const
Definition: io/parameter.hh:561
bool directoryExists(const std::string &name)
check whether a directory exists
Definition: io.cc:58
static T getValidValue(const std::string &key, const Validator &validator)
get an optional parameter from the container
Definition: io/parameter.hh:364
std::string commonInputPath() const
Definition: container.hh:154
virtual ParamDefault * clone() const
Definition: io/parameter.hh:573
static T getValue(const std::string &key, const T &defaultValue)
get an optional parameter from the container
Definition: io/parameter.hh:348
static std::string outputPath()
obtain unique output path for this process
Definition: io/parameter.hh:504
bool createDirectory(const std::string &inName)
create a directory
Definition: io.cc:19
static int getEnum(const std::string &key, const std::string(&values)[n], int defaultValue)
Definition: io/parameter.hh:394
static void appendDGF(const std::string &filename)
add parameters from a DGF file to the container
Definition: io/parameter.hh:237
static void append(int &argc, char **argv)
add parameters from the command line RangeType gRight;
Definition: io/parameter.hh:215
void appendDGF(const std::string &filename)
add parameters from a DGF file
Definition: container.hh:504
Definition: io/parameter.hh:549
void append(int &argc, char **argv)
add parameters from the command line
Definition: container.hh:484
Definition: coordinate.hh:4
static int getEnum(const std::string &key, const std::string(&values)[n])
Definition: io/parameter.hh:388
static void write(std::ostream &out, bool writeAll=true)
write the parameter database to a stream
Definition: io/parameter.hh:476
static int setVerboseRank(int verboseRank)
set the rank for verbose output
Definition: io/parameter.hh:203
T getValidValue(const std::string &key, const Validator &validator) const
get optional parameter
Definition: reader.hh:187
static ParameterContainer & container()
Definition: io/parameter.hh:190
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:515
virtual ParamDefault * clone() const
Definition: io/parameter.hh:555
static void append(const std::string &key, const std::string &value)
add a single parameter to the container
Definition: io/parameter.hh:222
std::string path
Definition: readioparams.cc:155
virtual ~LocalParameter()
Definition: io/parameter.hh:570
static bool verbose()
obtain the cached value for fem.verbose
Definition: io/parameter.hh:444
static void append(const std::string &filename)
add parameters from a file to the container
Definition: io/parameter.hh:228
static bool exists(const std::string &key)
find out, whether a parameter is defined in the container
Definition: io/parameter.hh:246
bool verbose() const
obtain the cached value for fem.verbose
Definition: container.hh:152
T getValue(const std::string &key) const
get mandatory parameter
Definition: reader.hh:149
void get(const std::string &key, T &value) const
get mandatory parameter
Definition: reader.hh:49
static T getValue(const std::string &key)
get a mandatory parameter from the container
Definition: io/parameter.hh:332
static std::string commonInputPath()
obtain common input path
Definition: io/parameter.hh:438
Definition: container.hh:70
std::string commonOutputPath() const
Definition: container.hh:159
int setVerboseRank(int verboseRank)
set the rank for verbose output
Definition: container.hh:433
virtual ~LocalParameter()
Definition: io/parameter.hh:552
void getValid(const std::string &key, const Validator &validator, T &value) const
get mandatory parameter
Definition: reader.hh:108