From: Pengfei Jia Date: Tue, 7 Feb 2023 18:25:32 +0000 (-0500) Subject: Adding tests and fixing comments X-Git-Tag: v9.5.0-rc1~579^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=620db52121bad1fd858a8ef59fa92c2a5ac89b96;p=dealii.git Adding tests and fixing comments --- diff --git a/source/base/mu_parser_internal.cc b/source/base/mu_parser_internal.cc index eb3173f5cb..1190107260 100644 --- a/source/base/mu_parser_internal.cc +++ b/source/base/mu_parser_internal.cc @@ -308,8 +308,8 @@ namespace internal parser.DefineFun("log", mu_log, true); parser.DefineFun("pow", mu_pow, true); parser.DefineFun("erfc", mu_erfc, true); - // We need to set optimized=false, because rand will generate - // different number every time. + // Disable optimizations (by passign false) that assume the functions + // will always return the same value: parser.DefineFun("rand_seed", mu_rand_seed, false); parser.DefineFun("rand", mu_rand, false); diff --git a/tests/base/function_parser_12.cc b/tests/base/function_parser_12.cc new file mode 100644 index 0000000000..5fb86d9f72 --- /dev/null +++ b/tests/base/function_parser_12.cc @@ -0,0 +1,67 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2005 - 2020 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. +// +// --------------------------------------------------------------------- + + +// Document a bug that rand_seed() in the FunctionParser always +// returns the same value. + +#include +#include + +#include + +#include +#include +#include + +#include "../tests.h" + + +// This is copied from the FunctionParser implementation +double +mu_rand_seed(double seed) +{ + std::uniform_real_distribution<> uniform_distribution(0., 1.); + + static std::map rng_map; + + if (rng_map.find(seed) == rng_map.end()) + rng_map[seed] = std::mt19937(static_cast(seed)); + + return uniform_distribution(rng_map[seed]); +} + + +int +main() +{ + initlog(); + double seed = 1; + std::string variables = "x,y"; + std::map constants; + + FunctionParser<2> fp(1); + fp.initialize(variables, "rand_seed(1)", constants); + Point<2> p; + + for (int i = 0; i < 10; ++i) + { + const double manual = mu_rand_seed(seed); + const double from_fp = fp.value(p); + AssertThrow(abs(manual - from_fp) < 1e-16, ExcInternalError()); + } + + deallog << "OK" << std::endl; +} diff --git a/tests/base/function_parser_12.with_muparser=true.output b/tests/base/function_parser_12.with_muparser=true.output new file mode 100644 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/function_parser_12.with_muparser=true.output @@ -0,0 +1,2 @@ + +DEAL::OK