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> 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());
56 std::string
getShadowKey (
const std::string key,
const char delimter, std::string &value )
const;
60 mutable std::map< std::string, Value >
map;
77 static std::string stripComment (
const std::string &line );
79 const std::string &insert (
const std::string &key,
const std::string &
value );
80 bool insert (
const std::string &s, std::queue< std::string > &includes );
82 void processFile (
const std::string &filename );
83 void processIncludes( std::queue< std::string > &includes );
109 void append (
int &argc,
char **argv );
116 void append (
const std::string &filename )
118 processFile( filename );
127 void append (
const std::string &key,
const std::string &value )
129 if( key !=
"paramfile" )
131 curFileName_ =
"program code";
132 insert( key, value );
146 void appendDGF (
const std::string &filename );
149 void clear () { parameter_.map.clear(); }
152 bool verbose ()
const {
return parameter_.verbose(); }
156 return getValue(
"fem.prefix.input", std::string(
"." ) );
161 return getValue(
"fem.prefix", std::string(
"." ) );
178 void write ( std::ostream &out,
bool writeAll =
true )
const;
181 std::string curFileName_;
193 explicit DGFBlock ( std::istream &in ) : BasicBlock( in,
"FemParameter" ) {}
196 std::string
getLine ()
const {
return line.str(); }
209 std::map< std::string, Value >::iterator pos;
212 auto info =
map.insert( std::make_pair( key,
Value( *defaultValue,
"default" ) ) );
214 std::cout <<
"Adding default: " << key <<
" = " << *defaultValue << std::endl;
218 pos =
map.find( key );
220 if( pos ==
map.end() )
222 Value &val = pos->second;
226 if( val.
hasDefault != static_cast< bool >( defaultValue ) )
227 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' used with and without default" );
228 if( defaultValue && (val.
defaultValue != *defaultValue) )
229 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' used with different default values" );
247 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' contains trailing '$'." );
249 const char escapedChar = value[ 0 ];
250 value.replace( 0, 1,
"" );
252 switch( escapedChar )
257 return std::string(
"" ) + escapedChar;
262 if( pos ==
map.end() )
265 return pos->second.value;
279 std::string &realValue = val.
value;
284 DUNE_THROW(
ParameterInvalid,
"Parameter '" << key <<
"' invalid, contains infinite loop" );
287 std::string realValueHelper;
288 realValue.swap( realValueHelper );
290 while( !realValueHelper.empty() )
292 std::size_t startPoint = realValueHelper.find_first_of(
'$' );
293 realValue += realValueHelper.substr( 0, startPoint );
295 if( startPoint == std::string::npos )
298 realValueHelper.replace( 0, startPoint+1,
"" );
308 std::string shadowKey;
312 std::size_t startPoint = value.find_first_of( std::string(
"$" ) + delimiter );
314 if( startPoint == std::string::npos )
317 shadowKey += value.substr( 0, startPoint );
318 const char startChar = value[ startPoint ];
320 value.replace( 0, startPoint+1,
"" );
322 if( startChar == delimiter )
324 assert( startChar ==
'$' );
335 inline const std::string &ParameterContainer::insert (
const std::string &key,
const std::string &
value )
337 auto info = parameter_.map.insert( std::make_pair( key,
Value( value, curFileName_ ) ) );
338 Value &val = info.first->second;
342 std::cout << curFileName_ <<
"[" << curLineNumber_ <<
"]: ";
344 std::cout <<
"Adding " << key <<
" = " << value << std::endl;
346 std::cout <<
"Ignored " << key <<
" = " << value <<
", using " << val.
value << std::endl;
353 inline std::string ParameterContainer::stripComment (
const std::string &line )
355 std::size_t size = line.size();
356 std::size_t end = line.find_first_of (
"%#$" );
358 while( (end != std::string::npos) && (line[end] ==
'$') )
361 end = line.find_first_of (
"%#$", end+2 );
363 end = std::string::npos;
370 inline bool ParameterContainer::insert (
const std::string &s, std::queue< std::string > &includes )
372 const std::size_t size = s.size();
374 std::size_t key_start = 0;
375 for( ; key_start < size; ++key_start )
377 if( (s[ key_start ] !=
' ') && (s[ key_start ] !=
'\t') )
381 std::size_t key_end = key_start;
382 for( ; key_end < size; ++key_end )
384 const char &c = s[ key_end ];
385 if( (c ==
' ') || (c ==
'\t') || (c ==
':') )
389 std::size_t value_start = key_end;
390 for( ; value_start < size ; ++value_start )
392 if( s[ value_start ] ==
':' )
397 for( ; value_start < size; ++value_start )
399 if( (s[ value_start ] !=
' ') && (s[ value_start ] !=
'\t') )
403 std::size_t value_end = value_start;
404 for( std::size_t i = 0; i < size; ++i )
406 if( (s[ i ] !=
' ') && (s[ i ] !=
'\t') )
410 if( value_start >= size )
413 std::string key = s.substr( key_start, key_end - key_start );
414 std::string value = s.substr( value_start, value_end - value_start );
416 if( key ==
"paramfile" )
417 includes.push( commonInputPath() +
"/" + value );
418 else if( key ==
"deprecated" )
419 parameter_.deprecated.insert( value );
422 const std::string &actual_value = insert( key, value );
423 if( key ==
"fem.verboserank" )
426 if( (parameter_.verboseRank < -1) || (parameter_.verboseRank >=
MPIManager::size() ) )
427 std::cout <<
"Warning: Parameter 'fem.verboserank' is neither a " <<
"valid rank nor -1." << std::endl;
435 int old = parameter_.verboseRank;
440 inline void ParameterContainer::processFile (
const std::string &filename )
443 std::cout <<
"Parameter: Processing '" << filename <<
"'..." << std::endl;
445 std::ifstream file( filename );
446 if( !file.is_open() )
448 std::cerr <<
"Warning: Unable to read parameter file '" << filename <<
"'" << std::endl;
452 curFileName_ = filename;
454 std::queue< std::string > includes;
459 std::getline( file, line );
461 line = stripComment( line );
463 insert( line, includes );
467 processIncludes( includes );
471 inline void ParameterContainer::processIncludes( std::queue< std::string > &includes )
473 while( !includes.empty() )
476 val.
value = includes.front();
478 parameter_.resolveShadows(
"paramfile", val );
479 processFile( val.
value );
486 std::queue< std::string > includes;
487 curFileName_ =
"program arguments";
489 for(
int i = 1 ; i < argc; ++i )
492 if( !insert( std::string( argv[ i ] ), includes ) )
495 std::copy( argv + (i+1), argv + argc, argv + i );
500 processIncludes( includes );
507 std::cout <<
"Parameter: Processing DGF '" << filename <<
"'..." << std::endl;
509 std::ifstream file( filename );
510 if( !file.is_open() )
512 std::cerr <<
"Warning: Unable to read DGF file '" << filename <<
"'" << std::endl;
516 if( !DuneGridFormatParser::isDuneGridFormat( file ) )
520 if( !block.isactive() )
523 curFileName_ = filename;
525 std::queue< std::string > includes;
530 const std::string line = stripComment( block.
getLine() );
532 insert( line, includes );
535 processIncludes( includes );
541 std::map< std::string, std::map<std::string, std::string> > writeMap;
542 for(
const auto ¶m : parameter_.map )
544 const Value &val = param.second;
549 for(
const auto &source : writeMap )
551 out <<
"# from " << source.first << std::endl;
552 for(
const auto ¶m : source.second )
553 out << param.first <<
": " << param.second << std::endl;
562 #endif // #ifndef DUNE_FEM_IO_PARAMETER_CONTAINER_HH
Value(std::string v, std::string fn)
Definition: container.hh:39
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
int verboseRank
Definition: container.hh:62
Definition: container.hh:190
BasicParameterReader< std::function< const std::string *(const std::string &, const std::string *) > > ParameterReader
Definition: reader.hh:264
static int size()
Definition: mpimanager.hh:121
Definition: io/parameter/exceptions.hh:24
void resolveShadows(const std::string &key, Value &val) const
Definition: container.hh:277
static std::string trim(const std::string &s)
Definition: container.hh:48
ShadowStatus
Definition: container.hh:35
DGFBlock(std::istream &in)
Definition: container.hh:193
std::string fileName
Definition: container.hh:41
const std::string * operator()(const std::string &key, const std::string *defaultValue) const
Definition: container.hh:204
std::string commonInputPath() const
Definition: container.hh:154
Definition: container.hh:35
Definition: container.hh:33
void appendDGF(const std::string &filename)
add parameters from a DGF file
Definition: container.hh:504
static bool parse(const std::string &s, T &value)
Definition: parser.hh:22
void append(int &argc, char **argv)
add parameters from the command line
Definition: container.hh:484
Definition: coordinate.hh:4
Definition: container.hh:31
std::string getShadowKey(const std::string key, const char delimter, std::string &value) const
Definition: container.hh:306
std::map< std::string, Value > map
Definition: container.hh:60
Definition: container.hh:35
std::string resolveEscape(const std::string &key, std::string &value) const
Definition: container.hh:244
void append(const std::string &key, const std::string &value)
add a single parameter to the container
Definition: container.hh:127
void move(ArrayInterface< T > &array, const unsigned int oldOffset, const unsigned int newOffset, const unsigned int length)
Definition: array_inline.hh:38
std::string getLine() const
Definition: container.hh:196
Definition: container.hh:35
bool verbose() const
obtain the cached value for fem.verbose
Definition: container.hh:152
void append(const std::string &filename)
add parameters from a file
Definition: container.hh:116
Definition: io/parameter/exceptions.hh:15
ShadowStatus shadowStatus
Definition: container.hh:43
bool used
Definition: container.hh:42
bool advance()
Definition: container.hh:195
std::string defaultValue
Definition: container.hh:41
std::string value
Definition: container.hh:41
Definition: container.hh:70
bool hasDefault
Definition: container.hh:42
std::string commonOutputPath() const
Definition: container.hh:159
std::set< std::string > deprecated
Definition: container.hh:61
std::string executeCommand(const std::string &command)
executes a command and return the output
Definition: io.cc:69
bool verbose() const
Definition: container.hh:58
void clear()
clear all parameters
Definition: container.hh:149
int setVerboseRank(int verboseRank)
set the rank for verbose output
Definition: container.hh:433