Skip to content

Instantly share code, notes, and snippets.

@tjhei
Created January 18, 2023 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjhei/360edba1d62bb61ecd63ebb16d82ecc0 to your computer and use it in GitHub Desktop.
Save tjhei/360edba1d62bb61ecd63ebb16d82ecc0 to your computer and use it in GitHub Desktop.
test muparser rand_seed(1)
##
# CMake script for the step-1 tutorial program:
##
# Set the name of the project and target:
set(TARGET "function_parser_09")
# Declare all source files the target consists of. Here, this is only
# the one step-X.cc file, but as you expand your project you may wish
# to add other source files as well. If your project becomes much larger,
# you may want to either replace the following statement by something like
# file(GLOB_RECURSE TARGET_SRC "source/*.cc")
# file(GLOB_RECURSE TARGET_INC "include/*.h")
# set(TARGET_SRC ${TARGET_SRC} ${TARGET_INC})
# or switch altogether to the large project CMakeLists.txt file discussed
# in the "CMake in user projects" page accessible from the "User info"
# page of the documentation.
set(TARGET_SRC
${TARGET}.cc
)
# Usually, you will not need to modify anything beyond this point...
cmake_minimum_required(VERSION 3.3.0)
find_package(deal.II 9.4.0
HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
)
if(NOT ${deal.II_FOUND})
message(FATAL_ERROR "\n"
"*** Could not locate a (sufficiently recent) version of deal.II. ***\n\n"
"You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II to cmake\n"
"or set an environment variable \"DEAL_II_DIR\" that contains this path."
)
endif()
deal_ii_initialize_cached_variables()
project(${TARGET})
deal_ii_invoke_autopilot()
// ---------------------------------------------------------------------
//
// 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.
//
// ---------------------------------------------------------------------
// test rand function
#include <deal.II/base/function_parser.h>
#include <deal.II/base/point.h>
#include <deal.II/lac/vector.h>
#include <map>
using namespace dealii;
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);
}
bool
satisfies_randomness(std::vector<double> &v)
{
// check that consecutive numbers are not the same
for (unsigned int i = 1; i < v.size(); ++i)
if (v[i - 1] == v[i])
return false;
// Check that the numbers are between 0 and 1
return std::all_of(v.begin(), v.end(), [](double n) {
return (n >= 0.) && (n <= 1.);
});
}
int
main()
{
std::vector<double> rands{eval("rand_seed(10)"),
eval("rand()"),
eval("rand_seed(10)"),
eval("rand_seed(10)")};
for (unsigned int i = 0; i < rands.size(); ++i)
std::cout << rands[i] << std::endl;
if (satisfies_randomness(rands))
std::cout << "OK" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment