1#ifndef DUNE_FEM_IO_PARAMETER_CONTAINER_HH
2#define DUNE_FEM_IO_PARAMETER_CONTAINER_HH
15#include <dune/grid/io/file/dgfparser/dgfparser.hh>
46 const std::string *
operator() (
const std::string &key,
const std::string *defaultValue )
const;
48 static std::string
trim (
const std::string &s )
50 const std::size_t first = s.find_first_not_of(
" \t\n" );
51 return (first != s.npos ? s.substr( first, s.find_last_not_of(
" \t\n" ) + 1 - first ) : std::string());
54 std::string
resolveEscape (
const std::string &key, std::string &value )
const;
56 std::string
getShadowKey (
const std::string key,
const char delimter, std::string &value )
const;
62 mutable std::map< std::string, Value >
map;
79 static std::string stripComment (
const std::string &line );
81 const std::string &insert (
const std::string &key,
const std::string &value,
bool force );
82 bool insert (
const std::string &s, std::queue< std::string > &includes );
84 void processFile (
const std::string &filename );
85 void processIncludes( std::queue< std::string > &includes );
102 void append (
int &argc,
char **argv );
109 void append (
const std::string &filename )
111 processFile( filename );
121 void append (
const std::string &key,
const std::string &value,
bool force =
false )
123 if( key !=
"paramfile" )
125 curFileName_ =
"program code";
126 insert( key, value, force );
141 std::stringstream str;
142 str << std::scientific;
154 template<class NumberType, std::enable_if_t< std::is_floating_point< NumberType >::value || std::is_integral< NumberType >::value,
int> = 0 >
155 void append (
const std::string &key, NumberType value,
bool force =
false )
157 assert( key !=
"paramfile" );
158 curFileName_ =
"program code";
159 std::string valueString =
toString( value );
160 insert( key, valueString, force );
171 void appendDGF (
const std::string &filename );
181 return getValue(
"fem.prefix.input", std::string(
"." ) );
186 return getValue(
"fem.prefix", std::string(
"." ) );
203 void write ( std::ostream &
out,
bool writeAll =
true )
const;
206 std::string curFileName_;
218 explicit DGFBlock ( std::istream &
in ) : BasicBlock(
in,
"FemParameter" ) {}
221 std::string
getLine ()
const {
return line.str(); }
234 std::map< std::string, Value >::iterator pos;
237 const std::string& defaultValueStr = *defaultValue;
243 pos =
map.find( key );
244 if( pos ==
map.end() )
248 Value &val = pos->second;
253 auto info =
map.insert( std::make_pair( key,
Value( *defaultValue,
"default" ) ) );
255 std::cout <<
"Adding default: " << key <<
": " << *defaultValue << std::endl;
259 pos =
map.find( key );
261 if( pos ==
map.end() )
263 Value &val = pos->second;
267 if( val.
hasDefault !=
static_cast< bool >( defaultValue ) )
268 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' used with and without default" );
269 if( defaultValue && (val.
defaultValue != *defaultValue) )
270 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' used with different default values" );
275 val.
hasDefault =
static_cast< bool >( defaultValue );
288 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' contains trailing '$'." );
290 const char escapedChar = value[ 0 ];
291 value.replace( 0, 1,
"" );
293 switch( escapedChar )
298 return std::string(
"" ) + escapedChar;
303 if( pos ==
map.end() )
306 return pos->second.value;
320 std::string &realValue = val.
value;
325 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' invalid, contains infinite loop" );
328 std::string realValueHelper;
329 realValue.swap( realValueHelper );
331 while( !realValueHelper.empty() )
333 std::size_t startPoint = realValueHelper.find_first_of(
'$' );
334 realValue += realValueHelper.substr( 0, startPoint );
336 if( startPoint == std::string::npos )
339 realValueHelper.replace( 0, startPoint+1,
"" );
349 std::string shadowKey;
353 std::size_t startPoint = value.find_first_of( std::string(
"$" ) + delimiter );
355 if( startPoint == std::string::npos )
358 shadowKey += value.substr( 0, startPoint );
359 const char startChar = value[ startPoint ];
361 value.replace( 0, startPoint+1,
"" );
363 if( startChar == delimiter )
365 assert( startChar ==
'$' );
376 inline const std::string &ParameterContainer::insert (
const std::string &key,
const std::string &value,
bool force =
false)
380 std::string paramValue;
381 if( force && paramExists )
383 paramValue = pos->second.value;
384 if( paramValue == value )
388 auto info =
parameter_.
map.insert( std::make_pair( key, Value( value, curFileName_ ) ) );
389 Value &val =
info.first->second;
390 if( key ==
"fem.verboserank" )
394 std::cout <<
"Warning: Parameter 'fem.verboserank' is neither a " <<
"valid rank nor -1." << std::endl;
399 std::cout << curFileName_ <<
"[" << curLineNumber_ <<
"]: ";
401 std::cout <<
"Adding " << key <<
" = " << value << std::endl;
403 std::cout <<
"Ignored " << key <<
" = " << value <<
", using " << val.value << std::endl;
405 std::cout <<
"Replacing " << key <<
" = " << paramValue <<
" by " << value << std::endl;
408 return force ? value : val.value;
412 inline std::string ParameterContainer::stripComment (
const std::string &line )
414 std::size_t size = line.size();
415 std::size_t end = line.find_first_of (
"%#$" );
417 while( (end != std::string::npos) && (line[end] ==
'$') )
420 end = line.find_first_of (
"%#$", end+2 );
422 end = std::string::npos;
429 inline bool ParameterContainer::insert (
const std::string &s, std::queue< std::string > &includes )
431 const std::size_t size = s.size();
433 std::size_t key_start = 0;
434 for( ; key_start < size; ++key_start )
436 if( (s[ key_start ] !=
' ') && (s[ key_start ] !=
'\t') )
440 std::size_t key_end = key_start;
441 for( ; key_end < size; ++key_end )
443 const char &c = s[ key_end ];
444 if( (c ==
' ') || (c ==
'\t') || (c ==
':') )
448 std::size_t value_start = key_end;
449 for( ; value_start < size ; ++value_start )
451 if( s[ value_start ] ==
':' )
456 for( ; value_start < size; ++value_start )
458 if( (s[ value_start ] !=
' ') && (s[ value_start ] !=
'\t') )
462 std::size_t value_end = value_start;
463 for( std::size_t i = 0; i < size; ++i )
465 if( (s[ i ] !=
' ') && (s[ i ] !=
'\t') )
469 if( value_start >= size )
472 std::string key = s.substr( key_start, key_end - key_start );
473 std::string value = s.substr( value_start, value_end - value_start );
475 if( key ==
"paramfile" )
477 else if( key ==
"deprecated" )
480 insert( key, value );
485 inline void ParameterContainer::processFile (
const std::string &filename )
488 std::cout <<
"Parameter: Processing '" << filename <<
"'..." << std::endl;
490 std::ifstream file( filename );
491 if( !file.is_open() )
493 std::cerr <<
"Warning: Unable to read parameter file '" << filename <<
"'" << std::endl;
497 curFileName_ = filename;
499 std::queue< std::string > includes;
504 std::getline( file, line );
506 line = stripComment( line );
508 insert( line, includes );
512 processIncludes( includes );
516 inline void ParameterContainer::processIncludes( std::queue< std::string > &includes )
518 while( !includes.empty() )
521 val.
value = includes.front();
524 processFile( val.value );
531 std::queue< std::string > includes;
532 curFileName_ =
"program arguments";
534 for(
int i = 1 ; i < argc; ++i )
537 if( !insert( std::string( argv[ i ] ), includes ) )
540 std::copy( argv + (i+1), argv + argc, argv + i );
545 processIncludes( includes );
552 std::cout <<
"Parameter: Processing DGF '" << filename <<
"'..." << std::endl;
554 std::ifstream file( filename );
555 if( !file.is_open() )
557 std::cerr <<
"Warning: Unable to read DGF file '" << filename <<
"'" << std::endl;
561 if( !DuneGridFormatParser::isDuneGridFormat( file ) )
565 if( !block.isactive() )
568 curFileName_ = filename;
570 std::queue< std::string > includes;
575 const std::string line = stripComment( block.
getLine() );
577 insert( line, includes );
580 processIncludes( includes );
586 std::map< std::string, std::map<std::string, std::string> > writeMap;
589 const Value &val = param.second;
594 for(
const auto &source : writeMap )
596 out <<
"# from " << source.first << std::endl;
597 for(
const auto ¶m : source.second )
598 out << param.first <<
": " << param.second << std::endl;
Definition: bindguard.hh:11
static const std::string & checkParameterExistsString()
Definition: reader.hh:20
std::string executeCommand(const std::string &command)
executes a command and return the output
Definition: io.cc:70
BasicParameterReader< std::function< const std::string *(const std::string &, const std::string *) > > ParameterReader
Definition: reader.hh:315
Definition: container.hh:32
void resolveShadows(const std::string &key, Value &val) const
Definition: container.hh:318
std::string getShadowKey(const std::string key, const char delimter, std::string &value) const
Definition: container.hh:347
static std::string trim(const std::string &s)
Definition: container.hh:48
int verboseRank
Definition: container.hh:64
const std::string * operator()(const std::string &key, const std::string *defaultValue) const
Definition: container.hh:229
std::string resolveEscape(const std::string &key, std::string &value) const
Definition: container.hh:285
bool verbose() const
Definition: container.hh:58
std::map< std::string, Value > map
Definition: container.hh:62
std::set< std::string > deprecated
Definition: container.hh:63
Definition: container.hh:34
std::string value
Definition: container.hh:41
ShadowStatus
Definition: container.hh:35
@ resolved
Definition: container.hh:35
@ resolving
Definition: container.hh:35
@ unresolved
Definition: container.hh:35
bool hasDefault
Definition: container.hh:42
Value(std::string v, std::string fn)
Definition: container.hh:39
std::string defaultValue
Definition: container.hh:41
ShadowStatus shadowStatus
Definition: container.hh:43
bool used
Definition: container.hh:42
std::string fileName
Definition: container.hh:41
Definition: container.hh:74
std::string commonInputPath() const
Definition: container.hh:179
void clear()
clear all parameters
Definition: container.hh:174
void append(const std::string &filename)
add parameters from a file
Definition: container.hh:109
void write(std::ostream &out, bool writeAll=true) const
write the parameter database to a stream
Definition: container.hh:584
std::string toString(const T &value)
A helper function to convert numbers to scientific strings.
Definition: container.hh:139
bool verbose() const
obtain the cached value for fem.verbose
Definition: container.hh:177
void append(const std::string &key, const std::string &value, bool force=false)
add a single parameter to the container
Definition: container.hh:121
void append(const std::string &key, NumberType value, bool force=false)
add a single Floating number parameter to the container
Definition: container.hh:155
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
Definition: container.hh:217
bool advance()
Definition: container.hh:220
DGFBlock(std::istream &in)
Definition: container.hh:218
std::string getLine() const
Definition: container.hh:221
Definition: io/parameter/exceptions.hh:17
Definition: io/parameter/exceptions.hh:26
static bool parse(const std::string &s, T &value)
Definition: parser.hh:22
T getValue(const std::string &key) const
get mandatory parameter
Definition: reader.hh:159
ParameterContainerData parameter_
Definition: reader.hh:307
Definition: grcommon.hh:31
static int size()
Definition: mpimanager.hh:406
static int rank()
Definition: mpimanager.hh:401