Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing random number generator function parser rand_seed #14765

Merged
merged 4 commits into from Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions source/base/mu_parser_internal.cc
Expand Up @@ -308,8 +308,10 @@ namespace internal
parser.DefineFun("log", mu_log, true);
parser.DefineFun("pow", mu_pow, true);
parser.DefineFun("erfc", mu_erfc, true);
parser.DefineFun("rand_seed", mu_rand_seed, true);
parser.DefineFun("rand", mu_rand, true);
// Disable optimizations (by passing 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);

try
{
Expand Down
67 changes: 67 additions & 0 deletions 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 <deal.II/base/function_parser.h>
#include <deal.II/base/point.h>

#include <deal.II/lac/vector.h>

#include <iostream>
#include <map>
#include <random>

#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<double, std::mt19937> rng_map;

if (rng_map.find(seed) == rng_map.end())
rng_map[seed] = std::mt19937(static_cast<unsigned int>(seed));

return uniform_distribution(rng_map[seed]);
}


int
main()
{
initlog();
double seed = 1;
std::string variables = "x,y";
std::map<std::string, double> 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;
}
2 changes: 2 additions & 0 deletions tests/base/function_parser_12.with_muparser=true.output
@@ -0,0 +1,2 @@

DEAL::OK