From aac966cff3e20ad8f38084a4e9876135895ac2c4 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Thu, 20 Jul 2017 19:52:02 +0200 Subject: [PATCH] Added add_parameter function and tests. --- include/deal.II/base/parameter_handler.h | 46 +++++++++ tests/base/pattern_tools_07.cc | 2 +- tests/base/patterns_05.cc | 104 +++++++++++++++++++++ tests/base/patterns_05.output | 26 ++++++ tests/base/patterns_06.cc | 113 +++++++++++++++++++++++ tests/base/patterns_06.output | 53 +++++++++++ tests/base/patterns_07.cc | 55 +++++++++++ tests/base/patterns_07.output | 9 ++ 8 files changed, 407 insertions(+), 1 deletion(-) create mode 100644 tests/base/patterns_05.cc create mode 100644 tests/base/patterns_05.output create mode 100644 tests/base/patterns_06.cc create mode 100644 tests/base/patterns_06.output create mode 100644 tests/base/patterns_07.cc create mode 100644 tests/base/patterns_07.output diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index 14c5f833c9..dc03791ab1 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -1003,6 +1003,22 @@ public: void add_action (const std::string &entry, const std::function &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::to_pattern(), but a custom one can be used. + */ + template + void add_parameter(const std::string &entry, + ParameterType ¶meter, + const std::string &documentation = std::string(), + const Patterns::PatternBase &pattern = + *Patterns::Tools::Convert::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 @@ -1886,6 +1902,7 @@ private: }; +// ---------------------- inline and template functions -------------------- template inline void @@ -1926,6 +1943,35 @@ ParameterHandler::load (Archive &ar, const unsigned int) } +template +void ParameterHandler::add_parameter(const std::string &entry, + ParameterType ¶meter, + const std::string &documentation, + const Patterns::PatternBase &pattern) +{ + + static_assert(std::is_const::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::to_string( + parameter, pattern.clone()), + pattern, + documentation); + + std::string path = get_current_full_path(entry); + const unsigned int pattern_index + = entries->get (path + path_separator + "pattern"); + + auto action = [ &, pattern_index](const std::string &val) + { + parameter = + Patterns::Tools::Convert::to_value(val, patterns[pattern_index]->clone()); + }; + add_action(entry, action); +} + DEAL_II_NAMESPACE_CLOSE #endif diff --git a/tests/base/pattern_tools_07.cc b/tests/base/pattern_tools_07.cc index c43e570511..ed75686350 100644 --- a/tests/base/pattern_tools_07.cc +++ b/tests/base/pattern_tools_07.cc @@ -15,7 +15,7 @@ #include "../tests.h" -#include +#include #include #include #include diff --git a/tests/base/patterns_05.cc b/tests/base/patterns_05.cc new file mode 100644 index 0000000000..71c54b4340 --- /dev/null +++ b/tests/base/patterns_05.cc @@ -0,0 +1,104 @@ +// --------------------------------------------------------------------- +// +// 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 +#include +#include + +#include + +using namespace dealii; +using namespace Patterns::Tools; + +// Try conversion on elementary types + +template +void test(T t, std::string s) +{ + auto p = Convert::to_pattern(); + deallog << "Pattern : " << p->description() << std::endl; + deallog << "String : " << s << std::endl; + deallog << "To string: " << Convert::to_string(t) << std::endl; + deallog << "To value : " << Convert::to_string(Convert::to_value(s)) << std::endl; + +} + +int main() +{ + initlog(); + + + std::vector l0 (2,-1); + std::vector l1 (3,2); + std::vector l2 (4, 3.14); + std::vector l3 (2, "bar"); + Point<3> l4 (3, 2, 1); + std::complex l5 (2.0, 1.0); + + std::vector > vl0 (2,std::vector (3,1)); + std::vector > vl1 (2,std::vector (3,2)); + std::vector > vl2 (2,std::vector (3, 3.14)); + std::vector > vl3 (2,std::vector (3, "foo")); + std::vector > vl4 (2,Point<3> (4,3,2)); + std::vector > vl5 (2,std::complex (1.0,3.0)); + + deallog << "List of int : " << Convert::to_string(l0) << std::endl; + deallog << "List of unsigned int: " << Convert::to_string(l1) << std::endl; + deallog << "List of double : " << Convert::to_string(l2) << std::endl; + deallog << "List of string : " << Convert::to_string(l3) << std::endl; + deallog << "Point<3> : " << Convert::to_string(l4) << std::endl; + deallog << "std::complex : " << Convert::to_string(l5) << std::endl; + + deallog << "List of lists of int : " << Convert::to_string(vl0) << std::endl; + deallog << "List of lists of unsigned int: " << Convert::to_string(vl1) << std::endl; + deallog << "List of lists of double : " << Convert::to_string(vl2) << std::endl; + deallog << "List of lists of string : " << Convert::to_string(vl3) << std::endl; + deallog << "List of Point<3> : " << Convert::to_string(vl4) << std::endl; + deallog << "List of std::complex : " << Convert::to_string(vl5) << std::endl; + + deallog << "=============================" << std::endl; + + l0 = Convert::to_value("1,2,3" ); + l1 = Convert::to_value("3,4,5" ); + l2 = Convert::to_value("5,6,7" ); + l3 = Convert::to_value("8,9,8.5"); + l4 = Convert::to_value("8,9,8.5"); + l5 = Convert::to_value("2.0,9.0"); + + vl0 = Convert::to_value("1,2,3 ; 1,2,3 "); + vl1 = Convert::to_value("3,4,5 ; 3,4,5 "); + vl2 = Convert::to_value("5,6,7 ; 5,6,7 "); + vl3 = Convert::to_value("8,9,8.5; 8,9,8.5"); + vl4 = Convert::to_value("8,9,8.5; 8,9,8.5"); + vl5 = Convert::to_value("1.0,2.0; 6.0,7.0"); + + deallog << "List of int : " << Convert::to_string(l0) << std::endl; + deallog << "List of unsigned int: " << Convert::to_string(l1) << std::endl; + deallog << "List of double : " << Convert::to_string(l2) << std::endl; + deallog << "List of string : " << Convert::to_string(l3) << std::endl; + deallog << "Point<3> : " << Convert::to_string(l4) << std::endl; + deallog << "std::complex : " << Convert::to_string(l5) << std::endl; + + deallog << "List of lists of int : " << Convert::to_string(vl0) << std::endl; + deallog << "List of lists of unsigned int: " << Convert::to_string(vl1) << std::endl; + deallog << "List of lists of double : " << Convert::to_string(vl2) << std::endl; + deallog << "List of lists of string : " << Convert::to_string(vl3) << std::endl; + deallog << "List of Point<3> : " << Convert::to_string(vl4) << std::endl; + deallog << "List of std::complex : " << Convert::to_string(vl5) << std::endl; + + return 0; +} diff --git a/tests/base/patterns_05.output b/tests/base/patterns_05.output new file mode 100644 index 0000000000..1157226fe3 --- /dev/null +++ b/tests/base/patterns_05.output @@ -0,0 +1,26 @@ + +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 diff --git a/tests/base/patterns_06.cc b/tests/base/patterns_06.cc new file mode 100644 index 0000000000..591ae6c5c1 --- /dev/null +++ b/tests/base/patterns_06.cc @@ -0,0 +1,113 @@ +// --------------------------------------------------------------------- +// +// 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 +#include +#include + +#include + +using namespace dealii; + +int main() +{ + initlog(); + + int a0(-1) ; + unsigned int a1(1 ) ; + double a2(2.0) ; + std::complex 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 va0(2, -1 ); + std::vector va1(2, 1 ); + std::vector va2(2, 2.0); + std::vector > va3(2, std::complex(3.0,4.0) ); + std::vector va4(2, std::string ("foo") ); + std::vector > va5(2, Point<1> (5.0) ); + std::vector > va6(2, Point<2> (6.0, 7.0) ); + std::vector > 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; +} diff --git a/tests/base/patterns_06.output b/tests/base/patterns_06.output new file mode 100644 index 0000000000..c011853fdb --- /dev/null +++ b/tests/base/patterns_06.output @@ -0,0 +1,53 @@ + +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 diff --git a/tests/base/patterns_07.cc b/tests/base/patterns_07.cc new file mode 100644 index 0000000000..881ecaaf1c --- /dev/null +++ b/tests/base/patterns_07.cc @@ -0,0 +1,55 @@ +// --------------------------------------------------------------------- +// +// 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 +#include +#include +#include + +#include + +using namespace dealii; +using namespace Patterns::Tools; + +int main() +{ + initlog(); + + std::map 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; +} diff --git a/tests/base/patterns_07.output b/tests/base/patterns_07.output new file mode 100644 index 0000000000..85753f4cf4 --- /dev/null +++ b/tests/base/patterns_07.output @@ -0,0 +1,9 @@ + +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 -- 2.39.5