void add_action (const std::string &entry,
const std::function<void (const std::string &value)> &action);
+ /**
+ * Declare a new entry name @p entry, set its default value to the content of
+ * the variable @p parameter, and create an action that will fill @p
+ * parameter with updated values when a file is parsed, or the entry is set
+ * to a new value.
+ *
+ * By default, the pattern to use is obtained by calling the function
+ * Patterns::Tools::Convert<T>::to_pattern(), but a custom one can be used.
+ */
+ template <class ParameterType>
+ void add_parameter(const std::string &entry,
+ ParameterType ¶meter,
+ const std::string &documentation = std::string(),
+ const Patterns::PatternBase &pattern =
+ *Patterns::Tools::Convert<ParameterType>::to_pattern());
+
/**
* Create an alias for an existing entry. This provides a way to refer to a
* parameter in the input file using an alternate name. The alias will be in
};
+// ---------------------- inline and template functions --------------------
template <class Archive>
inline
void
}
+template <class ParameterType>
+void ParameterHandler::add_parameter(const std::string &entry,
+ ParameterType ¶meter,
+ const std::string &documentation,
+ const Patterns::PatternBase &pattern)
+{
+
+ static_assert(std::is_const<ParameterType>::value == false,
+ "You tried to add a parameter using a type "
+ "that is const. Use a non-const type.");
+
+ declare_entry(entry,
+ Patterns::Tools::Convert<ParameterType>::to_string(
+ parameter, pattern.clone()),
+ pattern,
+ documentation);
+
+ std::string path = get_current_full_path(entry);
+ const unsigned int pattern_index
+ = entries->get<unsigned int> (path + path_separator + "pattern");
+
+ auto action = [ &, pattern_index](const std::string &val)
+ {
+ parameter =
+ Patterns::Tools::Convert<ParameterType>::to_value(val, patterns[pattern_index]->clone());
+ };
+ add_action(entry, action);
+}
+
DEAL_II_NAMESPACE_CLOSE
#endif
#include "../tests.h"
-#include <deal.II/base/patterns_tools.h>
+#include <deal.II/base/patterns.h>
#include <deal.II/base/logstream.h>
#include <deal.II/base/point.h>
#include <deal.II/base/numbers.h>
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2015 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 at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/patterns.h>
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/point.h>
+
+#include <memory>
+
+using namespace dealii;
+using namespace Patterns::Tools;
+
+// Try conversion on elementary types
+
+template<class T>
+void test(T t, std::string s)
+{
+ auto p = Convert<T>::to_pattern();
+ deallog << "Pattern : " << p->description() << std::endl;
+ deallog << "String : " << s << std::endl;
+ deallog << "To string: " << Convert<T>::to_string(t) << std::endl;
+ deallog << "To value : " << Convert<T>::to_string(Convert<T>::to_value(s)) << std::endl;
+
+}
+
+int main()
+{
+ initlog();
+
+
+ std::vector<int> l0 (2,-1);
+ std::vector<unsigned int> l1 (3,2);
+ std::vector<double> l2 (4, 3.14);
+ std::vector<std::string> l3 (2, "bar");
+ Point<3> l4 (3, 2, 1);
+ std::complex<double> l5 (2.0, 1.0);
+
+ std::vector<std::vector<int> > vl0 (2,std::vector<int> (3,1));
+ std::vector<std::vector<unsigned int> > vl1 (2,std::vector<unsigned int> (3,2));
+ std::vector<std::vector<double> > vl2 (2,std::vector<double> (3, 3.14));
+ std::vector<std::vector<std::string> > vl3 (2,std::vector<std::string> (3, "foo"));
+ std::vector<Point<3> > vl4 (2,Point<3> (4,3,2));
+ std::vector<std::complex<double> > vl5 (2,std::complex<double> (1.0,3.0));
+
+ deallog << "List of int : " << Convert<decltype(l0)>::to_string(l0) << std::endl;
+ deallog << "List of unsigned int: " << Convert<decltype(l1)>::to_string(l1) << std::endl;
+ deallog << "List of double : " << Convert<decltype(l2)>::to_string(l2) << std::endl;
+ deallog << "List of string : " << Convert<decltype(l3)>::to_string(l3) << std::endl;
+ deallog << "Point<3> : " << Convert<decltype(l4)>::to_string(l4) << std::endl;
+ deallog << "std::complex : " << Convert<decltype(l5)>::to_string(l5) << std::endl;
+
+ deallog << "List of lists of int : " << Convert<decltype(vl0)>::to_string(vl0) << std::endl;
+ deallog << "List of lists of unsigned int: " << Convert<decltype(vl1)>::to_string(vl1) << std::endl;
+ deallog << "List of lists of double : " << Convert<decltype(vl2)>::to_string(vl2) << std::endl;
+ deallog << "List of lists of string : " << Convert<decltype(vl3)>::to_string(vl3) << std::endl;
+ deallog << "List of Point<3> : " << Convert<decltype(vl4)>::to_string(vl4) << std::endl;
+ deallog << "List of std::complex : " << Convert<decltype(vl5)>::to_string(vl5) << std::endl;
+
+ deallog << "=============================" << std::endl;
+
+ l0 = Convert<decltype(l0)>::to_value("1,2,3" );
+ l1 = Convert<decltype(l1)>::to_value("3,4,5" );
+ l2 = Convert<decltype(l2)>::to_value("5,6,7" );
+ l3 = Convert<decltype(l3)>::to_value("8,9,8.5");
+ l4 = Convert<decltype(l4)>::to_value("8,9,8.5");
+ l5 = Convert<decltype(l5)>::to_value("2.0,9.0");
+
+ vl0 = Convert<decltype(vl0)>::to_value("1,2,3 ; 1,2,3 ");
+ vl1 = Convert<decltype(vl1)>::to_value("3,4,5 ; 3,4,5 ");
+ vl2 = Convert<decltype(vl2)>::to_value("5,6,7 ; 5,6,7 ");
+ vl3 = Convert<decltype(vl3)>::to_value("8,9,8.5; 8,9,8.5");
+ vl4 = Convert<decltype(vl4)>::to_value("8,9,8.5; 8,9,8.5");
+ vl5 = Convert<decltype(vl5)>::to_value("1.0,2.0; 6.0,7.0");
+
+ deallog << "List of int : " << Convert<decltype(l0)>::to_string(l0) << std::endl;
+ deallog << "List of unsigned int: " << Convert<decltype(l1)>::to_string(l1) << std::endl;
+ deallog << "List of double : " << Convert<decltype(l2)>::to_string(l2) << std::endl;
+ deallog << "List of string : " << Convert<decltype(l3)>::to_string(l3) << std::endl;
+ deallog << "Point<3> : " << Convert<decltype(l4)>::to_string(l4) << std::endl;
+ deallog << "std::complex : " << Convert<decltype(l5)>::to_string(l5) << std::endl;
+
+ deallog << "List of lists of int : " << Convert<decltype(vl0)>::to_string(vl0) << std::endl;
+ deallog << "List of lists of unsigned int: " << Convert<decltype(vl1)>::to_string(vl1) << std::endl;
+ deallog << "List of lists of double : " << Convert<decltype(vl2)>::to_string(vl2) << std::endl;
+ deallog << "List of lists of string : " << Convert<decltype(vl3)>::to_string(vl3) << std::endl;
+ deallog << "List of Point<3> : " << Convert<decltype(vl4)>::to_string(vl4) << std::endl;
+ deallog << "List of std::complex : " << Convert<decltype(vl5)>::to_string(vl5) << std::endl;
+
+ return 0;
+}
--- /dev/null
+
+DEAL::List of int : -1, -1
+DEAL::List of unsigned int: 2, 2, 2
+DEAL::List of double : 3.140000, 3.140000, 3.140000, 3.140000
+DEAL::List of string : bar, bar
+DEAL::Point<3> : 3.000000, 2.000000, 1.000000
+DEAL::std::complex : 2.000000, 1.000000
+DEAL::List of lists of int : 1, 1, 1; 1, 1, 1
+DEAL::List of lists of unsigned int: 2, 2, 2; 2, 2, 2
+DEAL::List of lists of double : 3.140000, 3.140000, 3.140000; 3.140000, 3.140000, 3.140000
+DEAL::List of lists of string : foo, foo, foo; foo, foo, foo
+DEAL::List of Point<3> : 4.000000, 3.000000, 2.000000; 4.000000, 3.000000, 2.000000
+DEAL::List of std::complex : 1.000000, 3.000000; 1.000000, 3.000000
+DEAL::=============================
+DEAL::List of int : 1, 2, 3
+DEAL::List of unsigned int: 3, 4, 5
+DEAL::List of double : 5.000000, 6.000000, 7.000000
+DEAL::List of string : 8, 9, 8.5
+DEAL::Point<3> : 8.000000, 9.000000, 8.500000
+DEAL::std::complex : 2.000000, 9.000000
+DEAL::List of lists of int : 1, 2, 3; 1, 2, 3
+DEAL::List of lists of unsigned int: 3, 4, 5; 3, 4, 5
+DEAL::List of lists of double : 5.000000, 6.000000, 7.000000; 5.000000, 6.000000, 7.000000
+DEAL::List of lists of string : 8, 9, 8.5; 8, 9, 8.5
+DEAL::List of Point<3> : 8.000000, 9.000000, 8.500000; 8.000000, 9.000000, 8.500000
+DEAL::List of std::complex : 1.000000, 2.000000; 6.000000, 7.000000
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2015 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 at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/parameter_handler.h>
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/point.h>
+
+#include <memory>
+
+using namespace dealii;
+
+int main()
+{
+ initlog();
+
+ int a0(-1) ;
+ unsigned int a1(1 ) ;
+ double a2(2.0) ;
+ std::complex<double> a3(3.0,4.0) ;
+ std::string a4("foo") ;
+ Point<1> a5(5.0) ;
+ Point<2> a6(6.0, 7.0) ;
+ Point<3> a7(8.0, 9.0, 10.0);
+
+ std::vector<int > va0(2, -1 );
+ std::vector<unsigned int > va1(2, 1 );
+ std::vector<double > va2(2, 2.0);
+ std::vector<std::complex<double> > va3(2, std::complex<double>(3.0,4.0) );
+ std::vector<std::string > va4(2, std::string ("foo") );
+ std::vector<Point<1> > va5(2, Point<1> (5.0) );
+ std::vector<Point<2> > va6(2, Point<2> (6.0, 7.0) );
+ std::vector<Point<3> > va7(2, Point<3> (8.0, 9.0, 10.0));
+
+
+
+ ParameterHandler prm;
+ prm.add_parameter("A signed integer", a0);
+ prm.add_parameter("An unsigned integer", a1);
+ prm.add_parameter("A double", a2);
+ prm.add_parameter("A complex", a3);
+ prm.add_parameter("A string", a4);
+ prm.add_parameter("A Point<1>", a5);
+ prm.add_parameter("A Point<2>", a6);
+ prm.add_parameter("A Point<3>", a7);
+ prm.add_parameter("A list of signed integers", va0);
+ prm.add_parameter("A list of unsigned integers", va1);
+ prm.add_parameter("A list of doubles", va2);
+ prm.add_parameter("A list of complexes", va3);
+ prm.add_parameter("A list of strings", va4);
+ prm.add_parameter("A list of Point<1>", va5);
+ prm.add_parameter("A list of Point<2>", va6);
+ prm.add_parameter("A list of Point<3>", va7);
+
+ prm.log_parameters(deallog);
+
+ prm.set("A signed integer", "-2");
+ prm.set("An unsigned integer", "3");
+ prm.set("A double", "4.0");
+ prm.set("A complex", "5.0, 6.0");
+ prm.set("A string", "bar");
+ prm.set("A Point<1>", "7.0");
+ prm.set("A Point<2>", "8.0, 9.0");
+ prm.set("A Point<3>", "10.0, 11.0, 12.0");
+ prm.set("A list of signed integers", "-3,-4");
+ prm.set("A list of unsigned integers", "6,7");
+ prm.set("A list of doubles", "9.0,10.0");
+ prm.set("A list of complexes", "1.0,2.0; 3.0,4.0");
+ prm.set("A list of strings", "foo, bar");
+ prm.set("A list of Point<1>", "1.0; 2.0");
+ prm.set("A list of Point<2>", "3.0, 4.0; 5.0,6.0");
+ prm.set("A list of Point<3>", "7.0, 8.0, 9.0; 10.0, 11.0, 12.0");
+
+ deallog << "After ParameterHandler::set =========================="
+ << std::endl << std::endl;
+ prm.log_parameters(deallog);
+
+ deallog << "Actual variables =========================="
+ << std::endl << std::endl;
+
+ deallog << "Values: " << a0 << std::endl;
+ deallog << "Values: " << a1 << std::endl;
+ deallog << "Values: " << a2 << std::endl;
+ deallog << "Values: " << a3 << std::endl;
+ deallog << "Values: " << a4 << std::endl;
+ deallog << "Values: " << a5 << std::endl;
+ deallog << "Values: " << a6 << std::endl;
+ deallog << "Values: " << a7 << std::endl;
+
+ deallog << "Vector values: " << va0[0] << ", " << va0[1] << std::endl;
+ deallog << "Vector values: " << va1[0] << ", " << va1[1] << std::endl;
+ deallog << "Vector values: " << va2[0] << ", " << va2[1] << std::endl;
+ deallog << "Vector values: " << va3[0] << ", " << va3[1] << std::endl;
+ deallog << "Vector values: " << va4[0] << ", " << va4[1] << std::endl;
+ deallog << "Vector values: " << va5[0] << ", " << va5[1] << std::endl;
+ deallog << "Vector values: " << va6[0] << ", " << va6[1] << std::endl;
+ deallog << "Vector values: " << va7[0] << ", " << va7[1] << std::endl;
+
+ return 0;
+}
--- /dev/null
+
+DEAL:parameters::A Point<1>: 5.000000
+DEAL:parameters::A Point<2>: 6.000000, 7.000000
+DEAL:parameters::A Point<3>: 8.000000, 9.000000, 10.000000
+DEAL:parameters::A complex: 3.000000, 4.000000
+DEAL:parameters::A double: 2.000000
+DEAL:parameters::A list of Point<1>: 5.000000; 5.000000
+DEAL:parameters::A list of Point<2>: 6.000000, 7.000000; 6.000000, 7.000000
+DEAL:parameters::A list of Point<3>: 8.000000, 9.000000, 10.000000; 8.000000, 9.000000, 10.000000
+DEAL:parameters::A list of complexes: 3.000000, 4.000000; 3.000000, 4.000000
+DEAL:parameters::A list of doubles: 2.000000, 2.000000
+DEAL:parameters::A list of signed integers: -1, -1
+DEAL:parameters::A list of strings: foo, foo
+DEAL:parameters::A list of unsigned integers: 1, 1
+DEAL:parameters::A signed integer: -1
+DEAL:parameters::A string: foo
+DEAL:parameters::An unsigned integer: 1
+DEAL::After ParameterHandler::set ==========================
+DEAL::
+DEAL:parameters::A Point<1>: 7.0
+DEAL:parameters::A Point<2>: 8.0, 9.0
+DEAL:parameters::A Point<3>: 10.0, 11.0, 12.0
+DEAL:parameters::A complex: 5.0, 6.0
+DEAL:parameters::A double: 4.0
+DEAL:parameters::A list of Point<1>: 1.0; 2.0
+DEAL:parameters::A list of Point<2>: 3.0, 4.0; 5.0,6.0
+DEAL:parameters::A list of Point<3>: 7.0, 8.0, 9.0; 10.0, 11.0, 12.0
+DEAL:parameters::A list of complexes: 1.0,2.0; 3.0,4.0
+DEAL:parameters::A list of doubles: 9.0,10.0
+DEAL:parameters::A list of signed integers: -3,-4
+DEAL:parameters::A list of strings: foo, bar
+DEAL:parameters::A list of unsigned integers: 6,7
+DEAL:parameters::A signed integer: -2
+DEAL:parameters::A string: bar
+DEAL:parameters::An unsigned integer: 3
+DEAL::Actual variables ==========================
+DEAL::
+DEAL::Values: -2
+DEAL::Values: 3
+DEAL::Values: 4.00000
+DEAL::Values: (5.00000,6.00000)
+DEAL::Values: bar
+DEAL::Values: 7.00000
+DEAL::Values: 8.00000 9.00000
+DEAL::Values: 10.0000 11.0000 12.0000
+DEAL::Vector values: -3, -4
+DEAL::Vector values: 6, 7
+DEAL::Vector values: 9.00000, 10.0000
+DEAL::Vector values: (1.00000,2.00000), (3.00000,4.00000)
+DEAL::Vector values: foo, bar
+DEAL::Vector values: 1.00000, 2.00000
+DEAL::Vector values: 3.00000 4.00000, 5.00000 6.00000
+DEAL::Vector values: 7.00000 8.00000 9.00000, 10.0000 11.0000 12.0000
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2015 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 at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/parameter_handler.h>
+#include <deal.II/base/patterns.h>
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/point.h>
+
+#include <memory>
+
+using namespace dealii;
+using namespace Patterns::Tools;
+
+int main()
+{
+ initlog();
+
+ std::map<unsigned int, double> a;
+ a[3] = 1.0;
+ a[2] = 3.0;
+
+
+ ParameterHandler prm;
+ prm.add_parameter("A map", a);
+
+ prm.log_parameters(deallog);
+
+ prm.set("A map", "1:2.0, 3:4.0");
+
+ deallog << "After ParameterHandler::set =========================="
+ << std::endl << std::endl;
+ prm.log_parameters(deallog);
+
+ deallog << "Actual variables =========================="
+ << std::endl << std::endl;
+
+ for (auto i : a)
+ deallog << i.first << ":" << i.second << std::endl;
+
+ return 0;
+}
--- /dev/null
+
+DEAL:parameters::A map: 2:3.000000, 3:1.000000
+DEAL::After ParameterHandler::set ==========================
+DEAL::
+DEAL:parameters::A map: 1:2.0, 3:4.0
+DEAL::Actual variables ==========================
+DEAL::
+DEAL::1:2.00000
+DEAL::3:4.00000