Skip to content

Commit

Permalink
ParameterHandler::add_parameter(): do not call action
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrum committed Dec 11, 2022
1 parent 880edda commit 97551e8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
9 changes: 9 additions & 0 deletions doc/news/changes/incompatibilities/20221211Munch
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Fixed: The function ParamerHandler::add_parameter() used to
call the internal action. Since the action converts during
that step the default value to a string and afterwards back,
this could lead to rounding-off errors so that the default
values might change in the case of floating-point numbers.
The action is not called any more during ParamerHandler::add_parameter(),
fixing the problem.
<br>
(Peter Munch, Magdalena Schreter, 2022/12/11)
5 changes: 3 additions & 2 deletions include/deal.II/base/parameter_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,8 @@ class ParameterHandler : public Subscriptor
*/
void
add_action(const std::string & entry,
const std::function<void(const std::string &value)> &action);
const std::function<void(const std::string &value)> &action,
const bool excecute_action = true);

/**
* Declare a new entry name @p entry, set its default value to the content of
Expand Down Expand Up @@ -2337,7 +2338,7 @@ ParameterHandler::add_parameter(const std::string & entry,
parameter = Patterns::Tools::Convert<ParameterType>::to_value(
val, *patterns[pattern_index]);
};
add_action(entry, action);
add_action(entry, action, false);
}

DEAL_II_NAMESPACE_CLOSE
Expand Down
15 changes: 9 additions & 6 deletions source/base/parameter_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,8 @@ ParameterHandler::declare_entry(const std::string & entry,
void
ParameterHandler::add_action(
const std::string & entry,
const std::function<void(const std::string &)> &action)
const std::function<void(const std::string &)> &action,
const bool excecute_action)
{
actions.push_back(action);

Expand All @@ -898,11 +899,13 @@ ParameterHandler::add_action(
entries->put(get_current_full_path(entry) + path_separator + "actions",
Utilities::int_to_string(actions.size() - 1));


// as documented, run the action on the default value at the very end
const std::string default_value = entries->get<std::string>(
get_current_full_path(entry) + path_separator + "default_value");
action(default_value);
if (excecute_action)
{
// as documented, run the action on the default value at the very end
const std::string default_value = entries->get<std::string>(
get_current_full_path(entry) + path_separator + "default_value");
action(default_value);
}
}


Expand Down
42 changes: 42 additions & 0 deletions tests/parameter_handler/add_parameter_01.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// ---------------------------------------------------------------------
//
// Copyright (C) 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.
//
// ---------------------------------------------------------------------



// check that ParameterHandler::add_parameter() does not modify the
// default value

#include <deal.II/base/parameter_handler.h>

#include "../tests.h"

using namespace dealii;

int
main()
{
initlog();

double a = std::numeric_limits<double>::lowest();

AssertThrow(a == std::numeric_limits<double>::lowest(), ExcInternalError());

ParameterHandler prm;
prm.add_parameter("test", a);

AssertThrow(a == std::numeric_limits<double>::lowest(), ExcInternalError());

deallog << "OK!" << std::endl;
}
2 changes: 2 additions & 0 deletions tests/parameter_handler/add_parameter_01.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

DEAL::OK!

0 comments on commit 97551e8

Please sign in to comment.