-<ol>
+<ol>
+ <li> New: FunctionParser now supports <code>rand()</code> and
+ <code>rand_seed(number)</code>, which return a random value in the
+ range [0,1].
+ <br>
+ (Wolfgang Bangerth, Luca Heltai, Alberto Sartori, 2016/02/09)
+ </li>
+
<li> Fixed: FullMatrix::TmTmult for matrix multiplication used to compute
wrong results for larger matrix sizes where external BLAS is called. This
has been fixed.
#include <deal.II/base/function_parser.h>
#include <deal.II/base/utilities.h>
+#include <deal.II/base/thread_management.h>
#include <deal.II/lac/vector.h>
#include <cmath>
-
+#include <boost/random.hpp>
+#include <map>
#ifdef DEAL_II_WITH_MUPARSER
DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
#else
+
+
namespace fparser
{
class FunctionParser
{
return erfc(value);
}
+
+ // returns a random value in the range [0,1] initializing the generator
+ // with the given seed
+ double mu_rand_seed(double seed)
+ {
+ static Threads::Mutex rand_mutex;
+ Threads::Mutex::ScopedLock lock(rand_mutex);
+
+ static boost::random::uniform_real_distribution<> uniform_distribution(0,1);
+
+ // for each seed an unique random number generator is created,
+ // which is initialized with the seed itself
+ static std::map<double, boost::random::mt19937> rng_map;
+
+ if (rng_map.find(seed) == rng_map.end())
+ rng_map[seed] = boost::random::mt19937(static_cast<unsigned int>(seed));
+
+ return uniform_distribution(rng_map[seed]);
+ }
+
+ // returns a random value in the range [0,1]
+ double mu_rand()
+ {
+ static Threads::Mutex rand_mutex;
+ Threads::Mutex::ScopedLock lock(rand_mutex);
+ static boost::random::uniform_real_distribution<> uniform_distribution(0,1);
+ static boost::random::mt19937 rng(static_cast<unsigned long>(std::time(0)));
+ return uniform_distribution(rng);
+ }
+
}
fp.get()[component].DefineFun("log", internal::mu_log, true);
fp.get()[component].DefineFun("pow", internal::mu_pow, true);
fp.get()[component].DefineFun("erfc", internal::mu_erfc, true);
+ fp.get()[component].DefineFun("rand_seed", internal::mu_rand_seed, true);
+ fp.get()[component].DefineFun("rand", internal::mu_rand, true);
try
{
"floor",
"sec",
"pow",
- "erfc"
+ "erfc",
+ "rand",
+ "rand_seed"
};
for (unsigned int f=0; f<sizeof(function_names)/sizeof(function_names[0]); ++f)
{
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2016 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.
+//
+// ---------------------------------------------------------------------
+
+
+// test rand function
+
+#include "../tests.h"
+#include <fstream>
+#include <iomanip>
+#include <map>
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/point.h>
+#include <deal.II/lac/vector.h>
+#include <deal.II/base/function_parser.h>
+
+
+double eval(const std::string &exp)
+{
+ std::string variables = "x,y";
+ std::map<std::string,double> constants;
+
+ FunctionParser<2> fp(1);
+ fp.initialize(variables,
+ exp,
+ constants);
+
+ Point<2> p;
+
+ return fp.value(p);
+}
+
+
+int main()
+{
+ initlog();
+
+ double random = eval("rand()"); //random seed
+
+ if (0.0 <= random && random <= 1.0)
+ deallog << "OK" << std::endl;
+
+ deallog << eval("rand_seed(10)") << std::endl;
+
+}