-
Notifications
You must be signed in to change notification settings - Fork 707
/
parameter_handler.h
2345 lines (2214 loc) · 86.5 KB
/
parameter_handler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------
//
// Copyright (C) 1998 - 2022 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#ifndef dealii_parameter_handler_h
#define dealii_parameter_handler_h
#include <deal.II/base/config.h>
#include <deal.II/base/exceptions.h>
#include <deal.II/base/patterns.h>
#include <deal.II/base/subscriptor.h>
#include <boost/archive/basic_archive.hpp>
#include <boost/property_tree/ptree_fwd.hpp>
#include <boost/property_tree/ptree_serialization.hpp>
#include <boost/serialization/split_member.hpp>
#include <map>
#include <memory>
#include <string>
#include <vector>
DEAL_II_NAMESPACE_OPEN
// forward declarations for interfaces and friendship
#ifndef DOXYGEN
class LogStream;
class MultipleParameterLoop;
#endif
/**
* The ParameterHandler class provides a standard interface to an input file
* which provides at run-time for program parameters such as time step sizes,
* geometries, right hand sides etc. The input for the program is given in
* files, streams or strings in memory using text like
* @code
* set Time step size = 0.3
* set Geometry = [0,1]x[0,3]
* @endcode
* Input may be sorted into subsection trees in order to give the input a
* logical structure, and input files may include other files.
*
* The ParameterHandler class is discussed in step-29,
* step-33, and step-34.
*
* <h3>Declaring entries</h3>
*
* In order to use the facilities of a ParameterHandler object, one first has
* to make known the different entries the input file may or may not contain.
* This is done in the following way:
*
* @code
* ...
* ParameterHandler prm;
* prm.declare_entry ("Time step size",
* "0.2",
* Patterns::Double(),
* "Some documentation");
* prm.declare_entry ("Geometry",
* "[0,1]x[0,1]",
* Patterns::Anything());
* ...
* @endcode
* Each entry is declared using the function declare_entry(). The first
* parameter is the name of the entry (in short: the entry). The second is the
* default answer to be taken in case the entry is not specified in the input
* file. The third parameter is a regular expression which the input (and the
* default answer) has to match. Several such regular expressions are defined
* in Patterns. This parameter can be omitted, in which case it will default
* to Patterns::Anything, i.e. a pattern that matches every input string. The
* fourth parameter can be used to document the intent or expected format of
* an entry; its value is printed as a comment when writing all entries of a
* ParameterHandler object using the print_parameters() function to allow for
* easier understanding of a parameter file. It can be omitted as well, in
* which case no such documentation will be printed.
*
* Entries may be located in subsections which form a kind of input tree. For
* example input parameters for linear solver routines should be classified in
* a subsection named <tt>Linear solver</tt> or any other suitable name. This
* is accomplished in the following way:
* @code
* ...
* LinEq eq;
* eq.declare_parameters (prm);
* ...
*
* void LinEq::declare_parameters (ParameterHandler &prm)
* {
* prm.enter_subsection("Linear solver");
* {
* prm.declare_entry ("Solver",
* "CG",
* Patterns::Selection("CG|GMRES|GaussElim"),
* "Name of a linear solver for the inner iteration");
* prm.declare_entry ("Maximum number of iterations", "20",
* ParameterHandler::RegularExpressions::Integer());
* ...
* }
* prm.leave_subsection ();
* }
* @endcode
*
* Subsections may be nested. For example a nonlinear solver may have a linear
* solver as member object. Then the function call tree would be something
* like (if the class <tt>NonLinEq</tt> has a member variables <tt>eq</tt> of
* type <tt>LinEq</tt>):
* @code
* void NonLinEq::declare_parameters (ParameterHandler &prm)
* {
* prm.enter_subsection ("Nonlinear solver");
* {
* prm.declare_entry ("Nonlinear method",
* "Newton-Raphson",
* ParameterHandler::RegularExpressions::Anything());
* eq.declare_parameters (prm);
* }
* prm.leave_subsection ();
* }
* @endcode
*
* For class member functions which declare the different entries we propose
* to use the common name <tt>declare_parameters</tt>. In normal cases this
* method can be <tt>static</tt> since the entries will not depend on any
* previous knowledge. Classes for which entries should logically be grouped
* into subsections should declare these subsections themselves. If a class
* has two or more member variables of the same type both of which should have
* their own parameters, this parent class' method <tt>declare_parameters</tt>
* is responsible to group them into different subsections:
* @code
* void NonLinEq::declare_parameters (ParameterHandler &prm)
* {
* prm.enter_subsection ("Nonlinear solver");
* {
* prm.enter_subsection ("Linear solver 1");
* {
* eq1.declare_parameters (prm);
* }
* prm.leave_subsection ();
*
* prm.enter_subsection ("Linear solver 2");
* {
* eq2.declare_parameters (prm);
* }
* prm.leave_subsection ();
* }
* prm.leave_subsection ();
* }
* @endcode
*
*
* <h3>Input files and special characters</h3>
*
* For the first example above the input file would look like the following:
* @code
* ...
* subsection Nonlinear solver
* set Nonlinear method = Gradient
* # this is a comment
* subsection Linear solver
* set Solver = CG
* set Maximum number of iterations = 30
* end
* end
* ... # other stuff
* @endcode
* The words <tt>subsection</tt>, <tt>set</tt> and <tt>end</tt> may be either
* written in lowercase or uppercase letters. Leading and trailing whitespace
* is removed, multiple whitespace is condensed into only one. Since the
* latter applies also to the name of an entry, an entry name will not be
* recognized if in the declaration multiple whitespace is used.
*
* In entry names and values the following characters are not allowed:
* <tt>\#</tt>, <tt>{</tt>, <tt>}</tt>, <tt>|</tt>. Their use is reserved for
* the MultipleParameterLoop class.
*
* Comments starting with \# are skipped.
*
* Continuation lines are allowed by means of the character <tt>\\</tt>, which
* must be the last character (aside from whitespace, which is ignored) of the
* line. When a line is a continuation (i.e., the previous line ended in a
* <tt>\\</tt>), then, unlike the default behavior of the <tt>C</tt>
* preprocessor, all whitespace at the beginning of the line is ignored.
*
* We propose to use the following scheme to name entries: start the first
* word with a capital letter and use lowercase letters further on. The same
* applies to the possible entry values to the right of the <tt>=</tt> sign.
*
*
* <h3>Including other input files</h3>
*
* An input file can include other include files using the syntax
* @code
* ...
* include some_other_file.prm
* ...
* @endcode
* The file so referenced is searched for relative to the current directory
* (not relative to the directory in which the including parameter file is
* located, since this is not known to all three versions of the parse_input()
* function).
*
*
* <h3>Reading data from input sources</h3>
*
* In order to read input there are three possibilities: reading from an
* <tt>std::istream</tt> object, reading from a file of which the name is
* given and reading from a string in memory in which the lines are separated
* by <tt>@\n</tt> characters. These possibilities are used as follows:
* @code
* ParameterHandler prm;
* ...
* // declaration of entries
* ...
* prm.parse_input (std::cin); // read input from standard in,
* // or
* prm.parse_input ("simulation.prm");
* // or
* char *in = "set Time step size = 0.3 \n ...";
* prm.parse_input_from_string (in);
* ...
* @endcode
* You can use several sources of input successively. Entries which are
* changed more than once will be overwritten every time they are used.
*
* You should not try to declare entries using declare_entry() and
* enter_subsection() with as yet unknown subsection names after using
* parse_input(). The results in this case are unspecified.
*
* If an error occurs upon reading the input, error messages are written to
* <tt>std::cerr</tt> and the reader function returns with a return value of
* <code>false</code>. This is opposed to almost all other functions in
* deal.II, which would normally throw an exception if an error occurs; this
* difference in behavior is a relic of the fact that this class predates
* deal.II and had previously been written for a different project.
*
*
* <h3>Using the %ParameterHandler Graphical User Interface</h3>
*
* An alternative to using the hand-written input files shown above is to use
* the graphical user interface (GUI) that accompanies this class.
*
* See <a href="https://github.com/dealii/parameter_gui">the parameter_gui
* github repository</a> for further details.
*
* <h3>Getting entry values out of a %ParameterHandler object</h3>
*
* Each class gets its data out of a ParameterHandler object by calling the
* get() member functions like this:
* @code
* void NonLinEq::get_parameters (ParameterHandler &prm)
* {
* prm.enter_subsection ("Nonlinear solver");
* std::string method = prm.get ("Nonlinear method");
* eq.get_parameters (prm);
* prm.leave_subsection ();
* }
* @endcode
* get() returns the value of the given entry. If the entry was not specified
* in the input source(s), the default value is returned. You have to enter
* and leave subsections exactly as you did when declaring subsections. You may
* choose the order in which to traverse the subsection tree.
*
* It is possible to avoid calls to enter_subsection() and leave_subsection()
* by supplying get() with a vector of strings representing the path from
* which to get a value. For example, the following two versions of
* get_parameters() will produce the same result:
* @code
* void NonLinEq::get_parameters (ParameterHandler &prm)
* {
* prm.enter_subsection ("Equation 1 Settings");
* prm.enter_subsection ("Linear solver");
* solver_ = prm.get ("Solver");
* prm.leave_subsection ();
* prm.leave_subsection ();
* }
* @endcode
*
* @code
* void NonLinEq::get_parameters (const ParameterHandler &prm)
* {
* std::vector<std::string> path =
* {"Equation 1 Settings", "Linear solver"};
* solver_ = prm.get (path, "Solver");
* }
* @endcode
*
* The latter method allows the ParameterHandler reference to be @p const.
*
* It is guaranteed that only entries matching the given regular expression
* are returned, i.e. an input entry value which does not match the regular
* expression is not stored.
*
* You can use get() to retrieve the parameter in text form, get_integer() to
* get an integer or get_double() to get a double. You can also use
* get_bool(). It will cause an internal error if the string could not be
* converted to an integer, double or a bool. This should, though, not happen
* if you correctly specified the regular expression for this entry; you
* should not try to get out an integer or a double from an entry for which no
* according regular expression was set. The internal error is raised through
* the Assert() macro family which only works in debug mode.
*
* If you want to print out all user selectable features, use the
* print_parameters() function. It is generally a good idea to print all
* parameters at the beginning of a log file, since this way input and output
* are together in one file which makes matching at a later time easier.
* Additionally, the function also print those entries which have not been
* modified in the input file and are thus set to default values; since
* default values may change in the process of program development, you cannot
* know the values of parameters not specified in the input file.
*
*
*
* <h3>Adding Actions to Parameters</h3>
*
* It is often convenient to have something happen as soon as a parameter
* value is read. This could be a check that it is valid -- say, that a
* file that is listed in the parameter file exists -- or to initiate
* something else in response, such as setting a variable outside the
* ParameterHandler (as in the example shown below). In almost all cases,
* this "action" could also be initiated once all parameters are read
* via parse_input(), but it is sometimes <i>convenient</i> to do it
* right away.
*
* This is facilitated by the add_action() function that can be called
* after declaring a parameter via declare_entry(). "Actions" are in essence
* pointers to functions that will be called for parameters that have
* associated actions. These functions take the value of a parameter as
* argument, and can then do whatever they want with it -- e.g., save it
* somewhere outside the ParameterHandler object. (Exactly when the
* action is called is described in the documentation of the
* add_action() function.) Of course, in C++ one doesn't usually pass
* around the address of a function, but an action can be a function-like
* object (taking a string as argument) that results from calling
* such as a
* <a href="http://en.cppreference.com/w/cpp/language/lambda">lambda
* function</a> that has the form
* @code
* [] (const std::string &value) { ... do something with the value ... }
* @endcode
* and that is attached to a specific parameter.
*
* A typical example of such an action would be as follows: let's assume
* that you have a program that declares a parameter for the number
* of iterations it is going to run, say
* @code
* class MyAlgorithm
* {
* public:
* void run ();
* private:
* unsigned int n_iterations;
* };
* @endcode
* then one could obtain this parameter from a parameter file using a
* code snippet in @p run() as follows:
* @code
* void MyAlgorithm::run ()
* {
* ParameterHandler prm;
* prm.declare_entry ("Number of iterations", // name of parameter
* "10", // default value
* Patterns::Integer(1,100),// allowed values: 1...100
* "The number of ..."); // some documentation
*
* // next read the parameter from an input file...
* prm.parse_input ("my_algorithm.prm");
*
* // ...and finally get the value for use in the program:
* n_iterations = prm.get_integer ("Number of iterations");
*
* ... actual code doing something useful follows here...
* @endcode
*
* This two-step process -- first declaring the parameter, and later reading
* it -- is a bit cumbersome because one has to first declare <i>all</i>
* parameters and at a later time retrieve them from the ParameterHandler
* object. In large programs, these two things also often happen in
* different functions.
*
* To avoid this, it would be nice if we could put both the declaration
* and the retrieval into the same place. This can be done via actions,
* and the function would then look like this:
* @code
* void MyAlgorithm::run ()
* {
* ParameterHandler prm;
* prm.declare_entry ("Number of iterations", // name of parameter
* "10", // default value
* Patterns::Integer(1,100),// allowed values: 1...100
* "The number of ..."); // some documentation
* prm.add_action ("Number of iterations",
* [&](const std::string &value)
* {
* this->n_iterations = Utilities::string_to_int(value);
* });
*
* // next read the parameter from an input file...
* prm.parse_input ("my_algorithm.prm");
*
* ... actual code doing something useful follows here...
* @endcode
* Here, the action consists of a lambda function that takes the value
* for this parameter as a string, and then converts it to an integer
* to store in the variable where it belongs. This action is
* executed inside the call to <code>prm.parse_input()</code>, and so
* there is now no longer a need to extract the parameter's value
* at a later time. Furthermore, the code that sets the member variable
* is located right next to the place where the parameter is actually
* declared, so we no longer need to have two separate parts of the code
* base that deal with input parameters.
*
* Of course, it is possible to execute far more involved actions than
* just setting a member variable as shown above, even though that is
* a typical case.
*
*
* <h3>Style guide for data retrieval</h3>
*
* We propose that every class which gets data out of a ParameterHandler
* object provides a function named <tt>get_parameters</tt>. This should be
* declared <tt>virtual</tt>. <tt>get_parameters</tt> functions in derived
* classes should call the <tt>BaseClass::get_parameters</tt> function.
*
*
* <h3>Experience with large parameter lists</h3>
*
* Experience has shown that in programs defining larger numbers of parameters
* (more than, say, fifty) it is advantageous to define an additional class
* holding these parameters. This class is more like a C-style structure,
* having a large number of variables, usually public. It then has at least
* two functions, which declare and parse the parameters. In the main program,
* the main class has an object of this parameter class and delegates
* declaration and parsing of parameters to this object.
*
* The advantage of this approach is that you can keep out the technical
* details (declaration and parsing) out of the main class and additionally
* don't clutter up your main class with dozens or more variables denoting the
* parameters.
*
*
*
* <h3>Worked Example</h3>
*
* This is the code:
* @code
* #include <deal.II/base/parameter_handler.h>
*
* #include <iostream>
* #include <string>
*
* using namespace dealii;
* class LinearEquation
* {
* public:
* static void declare_parameters (ParameterHandler &prm);
* void get_parameters (ParameterHandler &prm);
* private:
* std::string method;
* int max_iterations;
* };
*
*
*
* class Problem
* {
* private:
* LinearEquation eq1, eq2;
* std::string matrix1, matrix2;
* std::string outfile;
* public:
* static void declare_parameters (ParameterHandler &prm);
* void get_parameters (ParameterHandler &prm);
*
* void do_something ();
* };
*
*
*
* void LinearEquation::declare_parameters (ParameterHandler &prm)
* {
* // declare parameters for the linear solver in a subsection
* prm.enter_subsection ("Linear solver");
* {
* prm.declare_entry ("Solver",
* "CG",
* Patterns::Selection("CG|BiCGStab|GMRES"),
* "Name of a linear solver for the inner iteration");
* prm.declare_entry ("Maximum number of iterations",
* "20",
* Patterns::Integer());
* }
* prm.leave_subsection ();
* }
*
*
*
* void LinearEquation::get_parameters (ParameterHandler &prm)
* {
* prm.enter_subsection ("Linear solver");
* {
* method = prm.get ("Solver");
* max_iterations = prm.get_integer ("Maximum number of iterations");
* }
* prm.leave_subsection ();
* std::cout << " LinearEquation: method=" << method
* << ", max_iterations=" << max_iterations
* << std::endl;
* }
*
*
*
* void Problem::declare_parameters (ParameterHandler &prm)
* {
* // first some global parameter entries
* prm.declare_entry (
* "Output file",
* "out",
* Patterns::Anything(),
* "Name of the output file, either relative or absolute");
* prm.declare_entry ("Equation 1", "Laplace",
* Patterns::Anything(),
* "String identifying the equation we want to solve");
* prm.declare_entry ("Equation 2",
* "Elasticity",
* Patterns::Anything());
*
* // declare parameters for the first equation
* prm.enter_subsection ("Equation 1 Settings");
* {
* prm.declare_entry ("Matrix type",
* "Sparse",
* Patterns::Selection("Full|Sparse|Diagonal"),
* "Type of the matrix to be used, either full, "
* "sparse, or diagonal");
* LinearEquation::declare_parameters (prm); // for eq1
* }
* prm.leave_subsection ();
*
* // declare parameters for the second equation
* prm.enter_subsection ("Equation 2 Settings");
* {
* prm.declare_entry ("Matrix type",
* "Sparse",
* Patterns::Selection("Full|Sparse|Diagonal"));
* LinearEquation::declare_parameters (prm); // for eq2
* }
* prm.leave_subsection ();
* }
*
*
*
* void Problem::get_parameters (ParameterHandler &prm)
* {
* // entries of the problem class
* outfile = prm.get ("Output file");
* std::string equation1 = prm.get ("Equation 1"),
* equation2 = prm.get ("Equation 2");
*
* // get parameters for the first equation
* prm.enter_subsection ("Equation 1 Settings");
* {
* matrix1 = prm.get ("Matrix type");
* eq1.get_parameters (prm); // for eq1
* }
* prm.leave_subsection ();
*
* // get parameters for the second equation
* prm.enter_subsection ("Equation 2 Settings");
* {
* matrix2 = prm.get ("Matrix type");
* eq2.get_parameters (prm); // for eq2
* }
* prm.leave_subsection ();
* std::cout
* << " Problem: outfile=" << outfile << '\n'
* << " eq1=" << equation1 << ", eq2=" << equation2 << '\n'
* << " matrix1=" << matrix1 << ", matrix2=" << matrix2
* << std::endl;
* }
*
*
*
* void Problem::do_something ()
* {
* // While this example does nothing here, at this point in the program
* // all of the parameters are known so we can start doing computations.
* }
*
*
*
* int main ()
* {
* ParameterHandler prm;
* Problem p;
* p.declare_parameters (prm);
* // read input from "prmtest.prm"; giving argv[1] would also be a
* // good idea
* prm.parse_input ("prmtest.prm");
* // print parameters to std::cout as ASCII text
* std::cout << "\n\n";
* prm.print_parameters (std::cout, ParameterHandler::Text);
* // get parameters into the program
* std::cout << "\n\n" << "Getting parameters:" << std::endl;
* p.get_parameters (prm);
* // now run the program with these input parameters
* p.do_something ();
* }
* @endcode
*
*
* This is the input file (named "prmtest.prm"):
* @code
* # first declare the types of equations
* set Equation 1 = Poisson
* set Equation 2 = Stokes
*
* subsection Equation 1 Settings
* set Matrix type = Sparse
* subsection Linear solver # parameters for linear solver 1
* set Solver = Gauss-Seidel
* set Maximum number of iterations = 40
* end
* end
*
* subsection Equation 2 Settings
* set Matrix type = Full
* subsection Linear solver
* set Solver = CG
* set Maximum number of iterations = 100
* end
* end
* @endcode
*
* And here is the output of the program:
* @code
* Line <8> of file <prmtest.prm>:
* The entry value
* Gauss-Seidel
* for the entry named
* Solver
* does not match the given pattern
* [Selection CG|BiCGStab|GMRES ]
*
*
* # Listing of Parameters
* # ---------------------
* # String identifying the equation we want to solve
* set Equation 1 = Poisson # default: Laplace
* set Equation 2 = Stokes # default: Elasticity
*
* # Name of the output file, either relative to the present path or absolute
* set Output file = out
*
*
* subsection Equation 1 Settings
* # Type of the matrix to be used, either full, sparse, or diagonal
* set Matrix type = Sparse
*
*
* subsection Linear solver
* set Maximum number of iterations = 40 # default: 20
* # Name of a linear solver for the inner iteration
* set Solver = CG
* end
*
* end
*
*
* subsection Equation 2 Settings
* set Matrix type = Full # default: Sparse
*
*
* subsection Linear solver
* set Maximum number of iterations = 100 # default: 20
* # Name of a linear solver for the inner iteration
* set Solver = CG
* end
*
* end
*
*
*
*
* Getting parameters:
* LinearEquation: method=CG, max_iterations=40
* LinearEquation: method=CG, max_iterations=100
* Problem: outfile=out
* eq1=Poisson, eq2=Stokes
* matrix1=Sparse, matrix2=Full
* @endcode
*
*
*
* <h3>Representation of Parameters</h3>
*
* Here is some more internal information about the representation of
* parameters:
*
* Logically, parameters and the nested sections they are arranged in can be
* thought of as a hierarchical directory structure, or a tree. Take, for
* example, the following code declaring a set of parameters and sections they
* live in:
* @code
* ParameterHandler prm;
*
* prm.declare_entry ("Maximal number of iterations",
* "10",
* Patterns::Integer (1, 1000),
* "A parameter that describes the maximal number of "
* "iterations the CG method is to take before giving "
* "up on a matrix.");
* prm.enter_subsection ("Preconditioner");
* {
* prm.declare_entry(
* "Kind",
* "SSOR",
* Patterns::Selection ("SSOR|Jacobi"),
* "A string that describes the kind of preconditioner to use.");
* prm.declare_entry(
* "Relaxation factor",
* "1.0",
* Patterns::Double (0, 1),
* "The numerical value (between zero and one) for the "
* "relaxation factor to use in the preconditioner.");
* }
* prm.leave_subsection ();
* @endcode
*
* We can think of the parameters so arranged as a file system in which every
* parameter is a directory. The name of this directory is the name of the
* parameter, and in this directory lie files that describe the parameter.
* These files are at the time of writing this documentation (other fields,
* such as those indicating "actions" may also exist in each directory):
*
* - <code>value</code>: The content of this file is the current value of this
* parameter; initially, the content of the file equals the default value of
* the parameter.
* - <code>default_value</code>: The content of this file is the default value
* of the parameter.
* - <code>pattern</code>: A textual representation of the pattern that
* describes the parameter's possible values.
* - <code>pattern_index</code>: A number that indexes the Patterns::PatternBase
* object that is used to describe the parameter.
* - <code>documentation</code>: The content of this file is the documentation
* given for a parameter as the last argument of the
* ParameterHandler::declare_entry call. With the exception of the
* <code>value</code> file, the contents of files are never changed after
* declaration of a parameter.
*
* Alternatively, a directory in this file system may not have a file called
* <code>value</code> in it. In that case, the directory represents a
* subsection as declared above, and the directory's name will correspond to
* the name of the subsection. It will then have no files in it at all, but it
* may have further directories in it: some of these directories will be
* parameters (indicates by the presence of files) or further nested
* subsections.
*
* Given this explanation, the code above will lead to a hierarchical
* representation of data that looks like this (the content of files is
* indicated at the right in a different font):
*
* @image html parameter_handler.png
*
* Once parameters have been read in, the contents of the <code>value</code>
* "files" may be different while the other files remain untouched.
*
* Using the ParameterHandler::print_parameters() function with
* ParameterHandler::XML as second argument, we can get a complete
* representation of this data structure in XML. It will look like this:
* @code
* <?xml version="1.0" encoding="utf-8"?>
* <ParameterHandler>
* <Maximal_20number_20of_20iterations>
* <value>10</value>
* <default_value>10</default_value>
* <documentation>
* A parameter that describes the maximal number of iterations the CG
* method is to take before giving up on a matrix.
* </documentation>
* <pattern>0</pattern>
* <pattern_description>
* [Integer range 1...1000 (inclusive)]
* </pattern_description>
* </Maximal_20number_20of_20iterations>
* <Preconditioner>
* <Kind><value>SSOR</value>
* <default_value>SSOR</default_value>
* <documentation>
* A string that describes the kind of preconditioner to use.
* </documentation>
* <pattern>1</pattern>
* <pattern_description>SSOR|Jacobi</pattern_description>
* </Kind>
* <Relaxation_20factor>
* <value>1.0</value>
* <default_value>1.0</default_value>
* <documentation>
* The numerical value (between zero and one) for the relaxation
* factor to use in the preconditioner.
* </documentation>
* <pattern>2</pattern>
* <pattern_description>
* [Floating point range 0...1 (inclusive)]
* </pattern_description>
* </Relaxation_20factor>
* </Preconditioner>
* <ParameterHandler>
* @endcode
* This representation closely resembles the directory/file structure
* discussed above. The only difference is that directory and file names are
* mangled: since they should only contain letters and numbers, every
* character in their names that is not a letter or number is replaced by an
* underscore followed by its two-digit hexadecimal representation. In
* addition, the special name "value" is mangled when used as the name of a
* parameter, given that this name is also used to name special files in the
* hierarchy structure. Finally, the entire tree is wrapped into a tag
* <code>%ParameterHandler</code> to satisfy the XML requirement that there be
* only a single top-level construct in each file.
*
* The tree structure (and its XML representation) is what the graphical user
* interface (see above) uses to represent parameters like a directory/file
* collection.
*
*
* @ingroup input
*/
class ParameterHandler : public Subscriptor
{
public:
/**
* List of possible output formats used for functions like
* ParameterHandler::print_parameters(). The options can be categorized into
* two groups:
* - format options: PRM, LaTeX, Description, XML, JSON
* - stylistic options: Short, KeepDeclarationOrder
*
* Only one format option may be specified at the time. Any function that
* accepts an OutputStyle as an option will throw if you specify more than
* one.
*
* A number of shortcuts of commonly used option combinations are provided.
* E.g., ShortPRM prints the parameters in the PRM format, while skipping the
* documentation.
*/
enum OutputStyle
{
/**
* Default stylistic style: print documentation and sort all parameters
* alphabetically.
*/
DefaultStyle = 0x0000,
/**
* Write input for ParameterHandler without comments or changed default
* values.
*/
Short = 0x0001,
/**
* Keep the order of the parameters as they have been declared.
*/
KeepDeclarationOrder = 0x0002,
/**
* Write human readable output suitable to be read by
* ParameterHandler::parse_input() again.
*/
PRM = 0x0010,
/**
* Write human readable output suitable to be read by
* ParameterHandler::parse_input() again.
*
* @deprecated Use `PRM` instead of `Text`.
*/
Text = PRM,
/**
* Write parameters as a LaTeX table.
*/
LaTeX = 0x0020,
/**
* Write out declared parameters with description and possible values.
*
* @note This format is not suitable to be read back again.
*/
Description = 0x0040,
/**
* Write out everything as an <a
* href="http://en.wikipedia.org/wiki/XML">XML</a> file suitable to be read
* by ParameterHandler::parse_input_from_xml() again.
*
* See the general documentation of this class for an example of output.
*/
XML = 0x0080,
/**
* Write out everything as a <a
* href="http://en.wikipedia.org/wiki/JSON">JSON</a> file suitable to be
* read by ParameterHandler::parse_input_from_json() again.
*/
JSON = 0x0100,
/**
* Write the content of ParameterHandler without comments or changed default
* values.
*/
ShortPRM = PRM | Short,
/**
* Write the content of ParameterHandler without comments or changed default
* values.
*
* @deprecated Use `ShortPRM` instead of `ShortText`.
*/
ShortText = ShortPRM,
/**
* Write the content of ParameterHandler without comments or changed default
* values as a XML file.
*/
ShortXML = XML | Short,
/**
* Write the content of ParameterHandler without comments or changed default
* values as a JSON file.
*/
ShortJSON = JSON | Short,
/**
* Write the content of ParameterHandler without comments or changed default
* values as a LaTeX file.
*/
ShortLaTeX = LaTeX | Short,
};
/**
* Constructor.
*/
ParameterHandler();
/**
* Destructor. Declare this only to have a virtual destructor, which is
* safer as we have virtual functions. It actually does nothing
* spectacular.
*/
virtual ~ParameterHandler() override = default;
/**
* Inhibit automatic CopyConstructor.
*/
ParameterHandler(const ParameterHandler &) = delete;
/**
* Inhibit automatic assignment operator.
*/
ParameterHandler &
operator=(const ParameterHandler &) = delete;
/**
* Parse each line from a stream until the stream returns the <tt>eof</tt>
* condition or error to provide values for known parameter fields. The second
* argument can be used to denote the name of the file (if that's what the
* input stream represents) we are reading from; this is only used when
* creating output for exceptions.
*
* If non-empty @p last_line is provided, the ParameterHandler object
* will stop parsing lines after encountering @p last_line .
* This is handy when adding extra data that shall be parsed manually.
*
* If @p skip_undefined is <code>true</code>, the parameter handler
* will skip undefined sections and entries. This is useful for partially
* parsing a parameter file, for example to obtain only the spatial dimension
* of the problem. By default all entries and subsections are expected to be
* declared.
*
* The function sets the value of all parameters it encounters in the
* input file to the provided value. Parameters not explicitly listed
* in the input file are left at the value they previously held, which
* will be the default value provided to declare_entry() unless one
* has previously read a different input file.
*
* Each parameter value is matched against the pattern for this