Classes | |
class | Entry |
class | UserClass |
Public Member Functions | |
MultipleParameterLoop () | |
virtual | ~MultipleParameterLoop () |
virtual bool | read_input (std::istream &Input) |
virtual bool | read_input (const std::string &FileName, const bool optional=false, const bool write_stripped_file=false) |
virtual bool | read_input_from_string (const char *s) |
void | loop (UserClass &uc) |
unsigned int | memory_consumption () const |
Private Member Functions | |
void | init_branches () |
void | init_branches_section (const ParameterHandler::Section &sec) |
void | fill_entry_values (const unsigned int run_no) |
Private Attributes | |
std::vector< Entry > | multiple_choices |
unsigned int | n_branches |
The class MultipleParameterLoop offers an easy possibility to test several parameter sets during one run of the program. For this it uses the ParameterHandler class to read in data in a standardized form, searches for variant entry values and performs a loop over all combinations of parameters.
Variant entry values are given like this:
* set Time step size = { 0.1 | 0.2 | 0.3 } *
The loop will then perform three runs of the program, one for each value of Time step size
, while all other parameters are as specified or with their default value. If there are several variant entry values in the input a loop is performed for each combination of variant values:
* set Time step size = { 0.1 | 0.2 } * set Solver = { CG | GMRES } *
will result in four runs of the programs, with time step 0.1 and 0.2 for each of the two solvers.
Opposite to a variant entry, an array entry looks like this:
* set Output file = ofile.{{ 1 | 2 | 3 | 4 }} *
This indicates that if there are variant entries producing a total of four different runs will write their results to the files ofile.1
, ofile.2
, ofile.3
and ofile.4
, respectively. Array entries do not generate multiple runs of the main loop themselves, but if there are variant entries, then in the nth run of the main loop, also the nth value of an array is returned.
Since the different variants are constructed in the order of declaration, not in the order in which the variat entries appear in the input file, it may be difficult to guess the mapping between the different variants and the appropriate entry in an array. You will have to check the order of declaration, or use only one variant entry.
It is guaranteed that only selections which match the regular expression given upon declaration of an entry are given back to the program. If a variant value does not match the regular expression, the default value is stored and an error is issued. Before the first run of the loop, all possible values are checked for their conformance, so that the error is issued at the very beginning of the program.
The usage of this class is similar to the ParameterHandler class. First the entries and subsections have to be declared, then a loop is performed in which the different parameter sets are set, a new instance of a user class is created which is then called. Taking the classes of the example for the ParameterHandler class, the extended program would look like this:
class HelperClass : public MultipleParameterLoop::UserClass { public: HelperClass (); virtual void create_new (unsigned int runNo); virtual void declare_parameters (ParameterHandler &prm); virtual void run (ParameterHandler &prm); private: Problem *p; }; HelperClass::HelperClass () : p(0) {} void HelperClass::create_new (unsigned int runNo) { if (p) delete p; p = new Problem; } void HelperClass::declare_parameters (ParameterHandler &prm) { // entries of the problem class // note: must be static member! Problem::declare_parameters (prm); } void HelperClass::run (ParameterHandler &prm) { p->get_parameters (prm); p->do_useful_work (); } void main () { class MultipleParameterLoop prm; HelperClass h; h.declare_parameters (prm); prm.read_input ("prmtest.prm"); prm.loop (h); }
As can be seen, first a new helper class has to be set up. This must contain a virtual constructor for a problem class. You can also derive your problem class from MultipleParameterLoop::UserClass and let create_new
clear all member variables. If you have access to all inherited member variables in some way this is the recommended procedure. A third possibility is to use multiple inheritance and derive a helper class from both the MultipleParameterLoop::UserClass and the problem class. In any case, create_new
has to provide a clean problem object which is the problem in the second and third possibility.
The derived class also has to provide for member functions which declare the entries and which run the program. Running the program includes getting the parameters out of the ParameterHandler object.
After defining an object of this helper class and an object of the MultipleParameterLoop class, the entries have to be declared in the same way as for the ParameterHandler class. Then the input has to be read. Finally the loop is called. This executes the following steps:
for (each combination) { UserObject.create_new (runNo); // set parameters for this run UserObject.run (*this); }
UserObject
is the parameter to the loop
function. create_new
is given the number of the run (starting from one) to enable naming output files differently for each run.
Variant values are specified like prefix{ v1 | v2 | v3 | ... }postfix
. Whitespace to the right of the opening brace {
is ignored as well as to the left of the closing brace }
while whitespace on the respectively other side is not ignored. Whitespace around the mid symbols |
is also ignored. The empty selection prefix{ v1 | }postfix
is also allowed and produces the strings prefixv1postfix
and prefixpostfix
.
The syntax for array values is equal, apart from the double braces: prefix{{ v1 | v2 | v3 }}postfix
.
Given the above extensions to the example program for the ParameterHandler and the following input file
* set Equation 1 = Poisson * set Equation 2 = Navier-Stokes * set Output file= results.{{ 1 | 2 | 3 | 4 | 5 | 6 }} * * subsection Equation 1 * set Matrix type = Sparse * subsection Linear solver * set Solver = CG * set Maximum number of iterations = { 10 | 20 | 30 } * end * end * * subsection Equation 2 * set Matrix type = Full * subsection Linear solver * set Solver = { BiCGStab | GMRES } * set Maximum number of iterations = 100 * end * end *
this is the output:
* LinEq: Method=CG, MaxIterations=10 * LinEq: Method=BiCGStab, MaxIterations=100 * Problem: outfile=results.1 * eq1=Poisson, eq2=Navier-Stokes * Matrix1=Sparse, Matrix2=Full * LinEq: Method=CG, MaxIterations=20 * LinEq: Method=BiCGStab, MaxIterations=100 * Problem: outfile=results.2 * eq1=Poisson, eq2=Navier-Stokes * Matrix1=Sparse, Matrix2=Full * LinEq: Method=CG, MaxIterations=30 * LinEq: Method=BiCGStab, MaxIterations=100 * Problem: outfile=results.3 * eq1=Poisson, eq2=Navier-Stokes * Matrix1=Sparse, Matrix2=Full * LinEq: Method=CG, MaxIterations=10 * LinEq: Method=GMRES, MaxIterations=100 * Problem: outfile=results.4 * eq1=Poisson, eq2=Navier-Stokes * Matrix1=Sparse, Matrix2=Full * LinEq: Method=CG, MaxIterations=20 * LinEq: Method=GMRES, MaxIterations=100 * Problem: outfile=results.5 * eq1=Poisson, eq2=Navier-Stokes * Matrix1=Sparse, Matrix2=Full * LinEq: Method=CG, MaxIterations=30 * LinEq: Method=GMRES, MaxIterations=100 * Problem: outfile=results.6 * eq1=Poisson, eq2=Navier-Stokes * Matrix1=Sparse, Matrix2=Full *
Since create_new
gets the number of the run it would also be possible to output the number of the run.
This class is inspired by the Multipleloop
class of DiffPack
.
MultipleParameterLoop::MultipleParameterLoop | ( | ) |
Constructor
virtual MultipleParameterLoop::~MultipleParameterLoop | ( | ) | [virtual] |
Destructor. Declare this only to have a virtual destructor, which is safer as we have virtual functions. It actually does nothing spectacular.
virtual bool MultipleParameterLoop::read_input | ( | std::istream & | input | ) | [virtual] |
Read input from a stream until stream returns eof
condition or error.
Return whether the read was successful.
Reimplemented from ParameterHandler.
virtual bool MultipleParameterLoop::read_input | ( | const std::string & | filename, | |
const bool | optional = false , |
|||
const bool | write_stripped_file = false | |||
) | [virtual] |
Read input from a file the name of which is given. The PathSearch class "PARAMETERS" is used to find the file.
Return whether the read was successful.
Unless optional
is true
, this function will automatically generate the requested file with default values if the file did not exist. This file will not contain additional comments if write_stripped_file
is true
.
Reimplemented from ParameterHandler.
virtual bool MultipleParameterLoop::read_input_from_string | ( | const char * | s | ) | [virtual] |
Read input from a string in memory. The lines in memory have to be separated by \n
characters.
Reimplemented from ParameterHandler.
void MultipleParameterLoop::loop | ( | UserClass & | uc | ) |
run the central loop.
Determine an estimate for the memory consumption (in bytes) of this object.
Reimplemented from ParameterHandler.
void MultipleParameterLoop::init_branches | ( | ) | [private] |
Initialize the different branches, i.e. construct the combinations.
void MultipleParameterLoop::init_branches_section | ( | const ParameterHandler::Section & | sec | ) | [private] |
Initialize the branches in the given section.
Transfer the entry values for one run to the entry tree.
std::vector<Entry> MultipleParameterLoop::multiple_choices [private] |
List of variant entry values.
unsigned int MultipleParameterLoop::n_branches [private] |
Number of branches constructed from the different combinations of the variants. This obviously equals the number of runs to be performed.