From acf32c570f838c3929ed59049bba2d5e94894584 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Fri, 9 Jun 2017 13:50:57 -0600 Subject: [PATCH 01/12] Add random noise initial temperature plugin --- doc/modules/changes/20170609_gassmoeller | 4 + .../initial_temperature/random_perturbation.h | 90 +++++++++++++++++ .../random_perturbation.cc | 97 +++++++++++++++++++ tests/random_temperature_perturbation.prm | 57 +++++++++++ .../screen-output | 19 ++++ .../statistics | 14 +++ 6 files changed, 281 insertions(+) create mode 100644 doc/modules/changes/20170609_gassmoeller create mode 100644 include/aspect/initial_temperature/random_perturbation.h create mode 100644 source/initial_temperature/random_perturbation.cc create mode 100644 tests/random_temperature_perturbation.prm create mode 100644 tests/random_temperature_perturbation/screen-output create mode 100644 tests/random_temperature_perturbation/statistics diff --git a/doc/modules/changes/20170609_gassmoeller b/doc/modules/changes/20170609_gassmoeller new file mode 100644 index 00000000000..2de3a394cd4 --- /dev/null +++ b/doc/modules/changes/20170609_gassmoeller @@ -0,0 +1,4 @@ +New: A new initial temperature condition was added that adds a random +noise of user prescribed magnitude to the initial temperature field. +
+(Rene Gassmoeller, 2017/06/19) diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h new file mode 100644 index 00000000000..0b88c4b0c8e --- /dev/null +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -0,0 +1,90 @@ +/* + Copyright (C) 2017 by the authors of the ASPECT code. + + This file is part of ASPECT. + + ASPECT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + ASPECT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ASPECT; see the file doc/COPYING. If not see + . +*/ + + +#ifndef _aspect_initial_temperature_random_perturbation_h +#define _aspect_initial_temperature_random_perturbation_h + +#include + +DEAL_II_DISABLE_EXTRA_DIAGNOSTICS +#include +DEAL_II_ENABLE_EXTRA_DIAGNOSTICS + +namespace aspect +{ + namespace InitialTemperature + { + using namespace dealii; + + /** + * A class that describes a perturbation for the initial temperature field + * for any geometry model or dimension in shape. The perturbation follows + * a random noise of a prescribed magnitude. + * + * @ingroup InitialTemperatures + */ + template + class RandomPerturbation : public Interface + { + public: + /** + * Constructor. + */ + RandomPerturbation(); + + /** + * Return the initial temperature as a function of position. + */ + virtual + double initial_temperature (const Point &position) const; + + /** + * Declare the parameters this class takes through input files. + */ + static + void + declare_parameters (ParameterHandler &prm); + + /** + * Read the parameters this class declares from the parameter file. + */ + virtual + void + parse_parameters (ParameterHandler &prm); + + + private: + /** + * The maximal magnitude of the random noise. + */ + double magnitude; + + /** + * Random number generator. + */ + mutable boost::mt19937 random_number_generator; + + + }; + } +} + +#endif diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc new file mode 100644 index 00000000000..b639e202805 --- /dev/null +++ b/source/initial_temperature/random_perturbation.cc @@ -0,0 +1,97 @@ +/* + Copyright (C) 2017 by the authors of the ASPECT code. + + This file is part of ASPECT. + + ASPECT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + ASPECT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ASPECT; see the file doc/COPYING. If not see + . +*/ + + +#include + +namespace aspect +{ + namespace InitialTemperature + { + template + RandomPerturbation:: + RandomPerturbation () + : + random_number_generator(5432) + { + } + + template + double + RandomPerturbation:: + initial_temperature (const Point &/*position*/) const + { + // Uniform distribution on the interval [0,1]. This + // will be used to generate random particle locations. + boost::uniform_01 uniform_distribution_01; + + return magnitude * 2.0 * (uniform_distribution_01(random_number_generator) - 0.5); + } + + template + void + RandomPerturbation::declare_parameters (ParameterHandler &prm) + { + prm.enter_subsection ("Initial temperature model"); + { + prm.enter_subsection("Random perturbation"); + { + prm.declare_entry ("Magnitude", "1.0", + Patterns::Double (0), + "The magnitude of the random perturbation."); + } + prm.leave_subsection (); + } + prm.leave_subsection (); + } + + + template + void + RandomPerturbation::parse_parameters (ParameterHandler &prm) + { + prm.enter_subsection ("Initial temperature model"); + { + prm.enter_subsection("Random perturbation"); + { + magnitude = prm.get_double ("Magnitude"); + } + prm.leave_subsection (); + } + prm.leave_subsection (); + } + } +} + +// explicit instantiations +namespace aspect +{ + namespace InitialTemperature + { + ASPECT_REGISTER_INITIAL_TEMPERATURE_MODEL(RandomPerturbation, + "random perturbation", + "An initial temperature field in which the temperature " + "is perturbed following a harmonic function (spherical " + "harmonic or sine depending on geometry and dimension) " + "in lateral and radial direction from an otherwise " + "constant temperature (incompressible model) or adiabatic " + "reference profile (compressible model).") + } +} diff --git a/tests/random_temperature_perturbation.prm b/tests/random_temperature_perturbation.prm new file mode 100644 index 00000000000..46412b2ae62 --- /dev/null +++ b/tests/random_temperature_perturbation.prm @@ -0,0 +1,57 @@ +# Test to check the status of the random initial temperature perturbation +# plugin. + +set End time = 0 + +subsection Boundary temperature model + set List of model names = box +end + + +subsection Gravity model + set Model name = vertical +end + + +subsection Geometry model + set Model name = box + + subsection Box + set X extent = 1 + set Y extent = 1 + end +end + + +subsection Initial temperature model + set List of model names = function, random perturbation + + subsection Random perturbation + set Magnitude = 0.2 + end + + subsection Function + set Function expression = 0.5 + end +end + + +subsection Material model + set Model name = simpler +end + + +subsection Mesh refinement + set Initial adaptive refinement = 0 + set Initial global refinement = 3 +end + + +subsection Model settings + set Tangential velocity boundary indicators = left, right, bottom, top +end + +subsection Postprocess + set List of postprocessors = temperature statistics +end + diff --git a/tests/random_temperature_perturbation/screen-output b/tests/random_temperature_perturbation/screen-output new file mode 100644 index 00000000000..88bdd9e8546 --- /dev/null +++ b/tests/random_temperature_perturbation/screen-output @@ -0,0 +1,19 @@ + +Number of active cells: 64 (on 4 levels) +Number of degrees of freedom: 948 (578+81+289) + +*** Timestep 0: t=0 years + Solving temperature system... 0 iterations. + Rebuilding Stokes preconditioner... + Solving Stokes system... 17+0 iterations. + + Postprocessing: + Temperature min/avg/max: 0.3001 K, 0.5056 K, 0.6973 K + +Termination requested by criterion: end time + + ++---------------------------------------------+------------+------------+ ++---------------------------------+-----------+------------+------------+ ++---------------------------------+-----------+------------+------------+ + diff --git a/tests/random_temperature_perturbation/statistics b/tests/random_temperature_perturbation/statistics new file mode 100644 index 00000000000..fa519b561bc --- /dev/null +++ b/tests/random_temperature_perturbation/statistics @@ -0,0 +1,14 @@ +# 1: Time step number +# 2: Time (years) +# 3: Time step size (years) +# 4: Number of mesh cells +# 5: Number of Stokes degrees of freedom +# 6: Number of temperature degrees of freedom +# 7: Iterations for temperature solver +# 8: Iterations for Stokes solver +# 9: Velocity iterations in Stokes preconditioner +# 10: Schur complement iterations in Stokes preconditioner +# 11: Minimal temperature (K) +# 12: Average temperature (K) +# 13: Maximal temperature (K) +0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 17 18 17 3.00129639e-01 5.05619087e-01 6.97305210e-01 From 640a89cedcaa7aa689e8bf8e8817e30a5f9c7c2e Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Fri, 9 Jun 2017 14:09:23 -0600 Subject: [PATCH 02/12] Add random composition plugin --- doc/modules/changes/20170609_gassmoeller | 1 + .../initial_composition/random_perturbation.h | 89 +++++++++++++++++ .../initial_temperature/random_perturbation.h | 2 +- .../random_perturbation.cc | 95 +++++++++++++++++++ .../random_perturbation.cc | 15 ++- tests/random_composition_perturbation.prm | 69 ++++++++++++++ .../screen-output | 21 ++++ .../statistics | 20 ++++ 8 files changed, 302 insertions(+), 10 deletions(-) create mode 100644 include/aspect/initial_composition/random_perturbation.h create mode 100644 source/initial_composition/random_perturbation.cc create mode 100644 tests/random_composition_perturbation.prm create mode 100644 tests/random_composition_perturbation/screen-output create mode 100644 tests/random_composition_perturbation/statistics diff --git a/doc/modules/changes/20170609_gassmoeller b/doc/modules/changes/20170609_gassmoeller index 2de3a394cd4..0732c8ceb67 100644 --- a/doc/modules/changes/20170609_gassmoeller +++ b/doc/modules/changes/20170609_gassmoeller @@ -1,4 +1,5 @@ New: A new initial temperature condition was added that adds a random noise of user prescribed magnitude to the initial temperature field. +The same type of plugin was added for initial compositions.
(Rene Gassmoeller, 2017/06/19) diff --git a/include/aspect/initial_composition/random_perturbation.h b/include/aspect/initial_composition/random_perturbation.h new file mode 100644 index 00000000000..49dbca0f644 --- /dev/null +++ b/include/aspect/initial_composition/random_perturbation.h @@ -0,0 +1,89 @@ +/* + Copyright (C) 2017 by the authors of the ASPECT code. + + This file is part of ASPECT. + + ASPECT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + ASPECT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ASPECT; see the file doc/COPYING. If not see + . +*/ + + +#ifndef _aspect_initial_composition_random_perturbation_h +#define _aspect_initial_composition_random_perturbation_h + +#include + +DEAL_II_DISABLE_EXTRA_DIAGNOSTICS +#include +DEAL_II_ENABLE_EXTRA_DIAGNOSTICS + +namespace aspect +{ + namespace InitialComposition + { + using namespace dealii; + + /** + * A class that describes a perturbation for the initial composition fields + * for any geometry model or dimension. The perturbation follows + * a random noise of a prescribed magnitude. + * + * @ingroup InitialCompositions + */ + template + class RandomPerturbation : public Interface + { + public: + /** + * Constructor. + */ + RandomPerturbation(); + + /** + * Return the initial temperature as a function of position. + */ + virtual + double initial_composition (const Point &position, + const unsigned int compositional_index) const; + + /** + * Declare the parameters this class takes through input files. + */ + static + void + declare_parameters (ParameterHandler &prm); + + /** + * Read the parameters this class declares from the parameter file. + */ + virtual + void + parse_parameters (ParameterHandler &prm); + + + private: + /** + * The maximal magnitude of the random noise. + */ + double magnitude; + + /** + * Random number generator. + */ + mutable boost::mt19937 random_number_generator; + }; + } +} + +#endif diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index 0b88c4b0c8e..28602a7f78d 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -36,7 +36,7 @@ namespace aspect /** * A class that describes a perturbation for the initial temperature field - * for any geometry model or dimension in shape. The perturbation follows + * for any geometry model or dimension. The perturbation follows * a random noise of a prescribed magnitude. * * @ingroup InitialTemperatures diff --git a/source/initial_composition/random_perturbation.cc b/source/initial_composition/random_perturbation.cc new file mode 100644 index 00000000000..18c6dda6e82 --- /dev/null +++ b/source/initial_composition/random_perturbation.cc @@ -0,0 +1,95 @@ +/* + Copyright (C) 2017 by the authors of the ASPECT code. + + This file is part of ASPECT. + + ASPECT is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + ASPECT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ASPECT; see the file doc/COPYING. If not see + . +*/ + + +#include + +namespace aspect +{ + namespace InitialComposition + { + template + RandomPerturbation:: + RandomPerturbation () + : + random_number_generator(5432) + { + } + + template + double + RandomPerturbation:: + initial_composition (const Point &/*position*/, + const unsigned int /*compositional_index*/) const + { + // Uniform distribution on the interval [0,1]. This + // will be used to generate the random composition perturbation. + boost::uniform_01 uniform_distribution_01; + + return magnitude * 2.0 * (uniform_distribution_01(random_number_generator) - 0.5); + } + + template + void + RandomPerturbation::declare_parameters (ParameterHandler &prm) + { + prm.enter_subsection ("Initial composition model"); + { + prm.enter_subsection("Random perturbation"); + { + prm.declare_entry ("Magnitude", "1.0", + Patterns::Double (0), + "The magnitude of the random perturbation."); + } + prm.leave_subsection (); + } + prm.leave_subsection (); + } + + + template + void + RandomPerturbation::parse_parameters (ParameterHandler &prm) + { + prm.enter_subsection ("Initial composition model"); + { + prm.enter_subsection("Random perturbation"); + { + magnitude = prm.get_double ("Magnitude"); + } + prm.leave_subsection (); + } + prm.leave_subsection (); + } + } +} + +// explicit instantiations +namespace aspect +{ + namespace InitialComposition + { + ASPECT_REGISTER_INITIAL_COMPOSITION_MODEL(RandomPerturbation, + "random perturbation", + "An initial composition anomaly that perturbs the composition " + "following a random noise with uniform distribution and user " + "specified magnitude.") + } +} diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index b639e202805..ba106e80389 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -28,8 +28,8 @@ namespace aspect template RandomPerturbation:: RandomPerturbation () - : - random_number_generator(5432) + : + random_number_generator(5432) { } @@ -39,7 +39,7 @@ namespace aspect initial_temperature (const Point &/*position*/) const { // Uniform distribution on the interval [0,1]. This - // will be used to generate random particle locations. + // will be used to generate the random temperature perturbation. boost::uniform_01 uniform_distribution_01; return magnitude * 2.0 * (uniform_distribution_01(random_number_generator) - 0.5); @@ -87,11 +87,8 @@ namespace aspect { ASPECT_REGISTER_INITIAL_TEMPERATURE_MODEL(RandomPerturbation, "random perturbation", - "An initial temperature field in which the temperature " - "is perturbed following a harmonic function (spherical " - "harmonic or sine depending on geometry and dimension) " - "in lateral and radial direction from an otherwise " - "constant temperature (incompressible model) or adiabatic " - "reference profile (compressible model).") + "An initial tempeature anomaly that perturbs the temperature " + "following a random noise with uniform distribution and user " + "specified magnitude.") } } diff --git a/tests/random_composition_perturbation.prm b/tests/random_composition_perturbation.prm new file mode 100644 index 00000000000..e23c5c681bd --- /dev/null +++ b/tests/random_composition_perturbation.prm @@ -0,0 +1,69 @@ +# Test to check the status of the random initial temperature perturbation +# plugin. + +set End time = 0 + +subsection Boundary temperature model + set List of model names = box +end + + +subsection Gravity model + set Model name = vertical +end + + +subsection Geometry model + set Model name = box + + subsection Box + set X extent = 1 + set Y extent = 1 + end +end + + +subsection Initial temperature model + set List of model names = function + + subsection Function + set Function expression = 0.5 + end +end + +subsection Compositional fields + set Number of fields = 2 +end + +subsection Initial composition model + set List of model names = function, random perturbation + + subsection Random perturbation + set Magnitude = 0.2 + end + + subsection Function + set Function expression = 0.5;0.0 + end +end + + +subsection Material model + set Model name = simpler +end + + +subsection Mesh refinement + set Initial adaptive refinement = 0 + set Initial global refinement = 3 +end + + +subsection Model settings + set Tangential velocity boundary indicators = left, right, bottom, top +end + +subsection Postprocess + set List of postprocessors = composition statistics +end + diff --git a/tests/random_composition_perturbation/screen-output b/tests/random_composition_perturbation/screen-output new file mode 100644 index 00000000000..a8a8dc6da65 --- /dev/null +++ b/tests/random_composition_perturbation/screen-output @@ -0,0 +1,21 @@ + +Number of active cells: 64 (on 4 levels) +Number of degrees of freedom: 1,526 (578+81+289+289+289) + +*** Timestep 0: t=0 years + Solving temperature system... 0 iterations. + Solving C_1 system ... 0 iterations. + Solving C_2 system ... 0 iterations. + Rebuilding Stokes preconditioner... + Solving Stokes system... 2+0 iterations. + + Postprocessing: + Compositions min/max/mass: 0.3001/0.6984/0.4938 // -0.2/0.1996/-0.01184 + +Termination requested by criterion: end time + + ++---------------------------------------------+------------+------------+ ++---------------------------------+-----------+------------+------------+ ++---------------------------------+-----------+------------+------------+ + diff --git a/tests/random_composition_perturbation/statistics b/tests/random_composition_perturbation/statistics new file mode 100644 index 00000000000..992b20ca031 --- /dev/null +++ b/tests/random_composition_perturbation/statistics @@ -0,0 +1,20 @@ +# 1: Time step number +# 2: Time (years) +# 3: Time step size (years) +# 4: Number of mesh cells +# 5: Number of Stokes degrees of freedom +# 6: Number of temperature degrees of freedom +# 7: Number of degrees of freedom for all compositions +# 8: Iterations for temperature solver +# 9: Iterations for composition solver 1 +# 10: Iterations for composition solver 2 +# 11: Iterations for Stokes solver +# 12: Velocity iterations in Stokes preconditioner +# 13: Schur complement iterations in Stokes preconditioner +# 14: Minimal value for composition C_1 +# 15: Maximal value for composition C_1 +# 16: Global mass for composition C_1 +# 17: Minimal value for composition C_2 +# 18: Maximal value for composition C_2 +# 19: Global mass for composition C_2 +0 0.000000000000e+00 0.000000000000e+00 64 659 289 578 0 0 0 2 3 2 3.00090365e-01 6.98414874e-01 4.93781411e-01 -1.99952361e-01 1.99564844e-01 -1.18366501e-02 From fc17d23d570811405f4139b00d15883cabb9c528 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Tue, 20 Jun 2017 11:21:52 -0600 Subject: [PATCH 03/12] Add option for specific or random seeds --- .../initial_composition/random_perturbation.h | 5 ---- .../initial_temperature/random_perturbation.h | 7 ----- .../random_perturbation.cc | 28 +++++++++++++------ .../random_perturbation.cc | 28 +++++++++++++------ 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/include/aspect/initial_composition/random_perturbation.h b/include/aspect/initial_composition/random_perturbation.h index 49dbca0f644..f2acffc3acb 100644 --- a/include/aspect/initial_composition/random_perturbation.h +++ b/include/aspect/initial_composition/random_perturbation.h @@ -45,11 +45,6 @@ namespace aspect class RandomPerturbation : public Interface { public: - /** - * Constructor. - */ - RandomPerturbation(); - /** * Return the initial temperature as a function of position. */ diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index 28602a7f78d..fdab2296133 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -45,11 +45,6 @@ namespace aspect class RandomPerturbation : public Interface { public: - /** - * Constructor. - */ - RandomPerturbation(); - /** * Return the initial temperature as a function of position. */ @@ -81,8 +76,6 @@ namespace aspect * Random number generator. */ mutable boost::mt19937 random_number_generator; - - }; } } diff --git a/source/initial_composition/random_perturbation.cc b/source/initial_composition/random_perturbation.cc index 18c6dda6e82..50a56b1ee16 100644 --- a/source/initial_composition/random_perturbation.cc +++ b/source/initial_composition/random_perturbation.cc @@ -21,18 +21,12 @@ #include +#include + namespace aspect { namespace InitialComposition { - template - RandomPerturbation:: - RandomPerturbation () - : - random_number_generator(5432) - { - } - template double RandomPerturbation:: @@ -57,6 +51,19 @@ namespace aspect prm.declare_entry ("Magnitude", "1.0", Patterns::Double (0), "The magnitude of the random perturbation."); + prm.declare_entry ("Use random seed", "false", + Patterns::Bool(), + "Whether to use a random seed for the random " + "number generator. This parameter controls whether " + "this plugin generates different or identical " + "perturbations for different model runs."); + prm.declare_entry ("Random seed", "5432", + Patterns::Integer(0), + "The initial seed for the random perturbation. If " + "'Use random seed' is set to 'false' model runs " + "with identical seed will lead to identical " + "perturbations, while model runs with different " + "seeds will generate different perturbations."); } prm.leave_subsection (); } @@ -73,6 +80,11 @@ namespace aspect prm.enter_subsection("Random perturbation"); { magnitude = prm.get_double ("Magnitude"); + + if (prm.get_bool("Use random seed")) + random_number_generator.seed(time(NULL)); + else + random_number_generator.seed(prm.get_integer("Random seed")); } prm.leave_subsection (); } diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index ba106e80389..2b3a3b1486e 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -21,18 +21,12 @@ #include +#include + namespace aspect { namespace InitialTemperature { - template - RandomPerturbation:: - RandomPerturbation () - : - random_number_generator(5432) - { - } - template double RandomPerturbation:: @@ -56,6 +50,19 @@ namespace aspect prm.declare_entry ("Magnitude", "1.0", Patterns::Double (0), "The magnitude of the random perturbation."); + prm.declare_entry ("Use random seed", "false", + Patterns::Bool(), + "Whether to use a random seed for the random " + "number generator. This parameter controls whether " + "this plugin generates different or identical " + "perturbations for different model runs."); + prm.declare_entry ("Random seed", "5432", + Patterns::Integer(0), + "The initial seed for the random perturbation. If " + "'Use random seed' is set to 'false' model runs " + "with identical seed will lead to identical " + "perturbations, while model runs with different " + "seeds will generate different perturbations."); } prm.leave_subsection (); } @@ -72,6 +79,11 @@ namespace aspect prm.enter_subsection("Random perturbation"); { magnitude = prm.get_double ("Magnitude"); + + if (prm.get_bool("Use random seed")) + random_number_generator.seed(time(NULL)); + else + random_number_generator.seed(prm.get_integer("Random seed")); } prm.leave_subsection (); } From 0dfed6cfb43feb2a3831605267985312df7a8346 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Fri, 23 Jun 2017 12:40:36 -0600 Subject: [PATCH 04/12] Use position hash as pseudo-random seed --- doc/modules/changes/20170609_gassmoeller | 2 +- .../initial_composition/random_perturbation.h | 8 ++- .../initial_temperature/random_perturbation.h | 8 ++- .../random_perturbation.cc | 49 ++++++++++------- .../random_perturbation.cc | 44 +++++++++------ tests/random_composition_perturbation.prm | 53 +++++++------------ .../screen-output | 2 +- .../statistics | 2 +- tests/random_temperature_perturbation.prm | 45 ++++++---------- .../screen-output | 2 +- .../statistics | 2 +- 11 files changed, 110 insertions(+), 107 deletions(-) diff --git a/doc/modules/changes/20170609_gassmoeller b/doc/modules/changes/20170609_gassmoeller index 0732c8ceb67..44e74f158cb 100644 --- a/doc/modules/changes/20170609_gassmoeller +++ b/doc/modules/changes/20170609_gassmoeller @@ -1,4 +1,4 @@ -New: A new initial temperature condition was added that adds a random +New: A new initial temperature condition was added that adds random noise of user prescribed magnitude to the initial temperature field. The same type of plugin was added for initial compositions.
diff --git a/include/aspect/initial_composition/random_perturbation.h b/include/aspect/initial_composition/random_perturbation.h index f2acffc3acb..3d2eafe9dbe 100644 --- a/include/aspect/initial_composition/random_perturbation.h +++ b/include/aspect/initial_composition/random_perturbation.h @@ -74,9 +74,13 @@ namespace aspect double magnitude; /** - * Random number generator. + * Whether to use a random seed for the random + * number generator. This parameter controls whether + * this plugin generates different or identical + * perturbations for subsequent model runs of + * the same setup. */ - mutable boost::mt19937 random_number_generator; + bool use_random_seed; }; } } diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index fdab2296133..fba16800947 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -73,9 +73,13 @@ namespace aspect double magnitude; /** - * Random number generator. + * Whether to use a random seed for the random + * number generator. This parameter controls whether + * this plugin generates different or identical + * perturbations for subsequent model runs of + * the same setup. */ - mutable boost::mt19937 random_number_generator; + bool use_random_seed; }; } } diff --git a/source/initial_composition/random_perturbation.cc b/source/initial_composition/random_perturbation.cc index 50a56b1ee16..cd63fbf31ac 100644 --- a/source/initial_composition/random_perturbation.cc +++ b/source/initial_composition/random_perturbation.cc @@ -21,23 +21,44 @@ #include +#include #include namespace aspect { namespace InitialComposition { + namespace + { + template + std::size_t point_hash(const Point &position) + { + std::size_t hash; + + for (unsigned int i = 0; i < dim; ++i) + boost::hash_combine(hash,position[i]); + + return hash; + } + } + template double RandomPerturbation:: - initial_composition (const Point &/*position*/, - const unsigned int /*compositional_index*/) const + initial_composition (const Point &position, + const unsigned int compositional_index) const { - // Uniform distribution on the interval [0,1]. This - // will be used to generate the random composition perturbation. - boost::uniform_01 uniform_distribution_01; + std::size_t seed = point_hash(position); + boost::hash_combine(seed,compositional_index); + if (use_random_seed) + boost::hash_combine(seed,time(NULL)); + + boost::mt19937 random_number_generator(seed); - return magnitude * 2.0 * (uniform_distribution_01(random_number_generator) - 0.5); + // Uniform distribution on the interval [-magnitude,magnitude). This + // will be used to generate the random composition perturbation. + boost::random::uniform_real_distribution uniform_distribution(-magnitude,magnitude); + return uniform_distribution(random_number_generator); } template @@ -56,14 +77,8 @@ namespace aspect "Whether to use a random seed for the random " "number generator. This parameter controls whether " "this plugin generates different or identical " - "perturbations for different model runs."); - prm.declare_entry ("Random seed", "5432", - Patterns::Integer(0), - "The initial seed for the random perturbation. If " - "'Use random seed' is set to 'false' model runs " - "with identical seed will lead to identical " - "perturbations, while model runs with different " - "seeds will generate different perturbations."); + "perturbations for subsequent model runs of" + "the same setup."); } prm.leave_subsection (); } @@ -80,11 +95,7 @@ namespace aspect prm.enter_subsection("Random perturbation"); { magnitude = prm.get_double ("Magnitude"); - - if (prm.get_bool("Use random seed")) - random_number_generator.seed(time(NULL)); - else - random_number_generator.seed(prm.get_integer("Random seed")); + use_random_seed = prm.get_bool("Use random seed"); } prm.leave_subsection (); } diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index 2b3a3b1486e..f09bb4e1c98 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -21,22 +21,43 @@ #include +#include #include namespace aspect { namespace InitialTemperature { + namespace + { + template + std::size_t point_hash(const Point &position) + { + std::size_t hash; + + for (unsigned int i = 0; i < dim; ++i) + boost::hash_combine(hash,position[i]); + + return hash; + } + } + template double RandomPerturbation:: - initial_temperature (const Point &/*position*/) const + initial_temperature (const Point &position) const { - // Uniform distribution on the interval [0,1]. This - // will be used to generate the random temperature perturbation. - boost::uniform_01 uniform_distribution_01; + std::size_t seed = point_hash(position); + + if (use_random_seed) + boost::hash_combine(seed,time(NULL)); - return magnitude * 2.0 * (uniform_distribution_01(random_number_generator) - 0.5); + boost::mt19937 random_number_generator(seed); + + // Uniform distribution on the interval [-magnitude,magnitude). This + // will be used to generate the random temperature perturbation. + boost::random::uniform_real_distribution uniform_distribution(-magnitude,magnitude); + return uniform_distribution(random_number_generator); } template @@ -56,13 +77,6 @@ namespace aspect "number generator. This parameter controls whether " "this plugin generates different or identical " "perturbations for different model runs."); - prm.declare_entry ("Random seed", "5432", - Patterns::Integer(0), - "The initial seed for the random perturbation. If " - "'Use random seed' is set to 'false' model runs " - "with identical seed will lead to identical " - "perturbations, while model runs with different " - "seeds will generate different perturbations."); } prm.leave_subsection (); } @@ -79,11 +93,7 @@ namespace aspect prm.enter_subsection("Random perturbation"); { magnitude = prm.get_double ("Magnitude"); - - if (prm.get_bool("Use random seed")) - random_number_generator.seed(time(NULL)); - else - random_number_generator.seed(prm.get_integer("Random seed")); + use_random_seed = prm.get_bool("Use random seed"); } prm.leave_subsection (); } diff --git a/tests/random_composition_perturbation.prm b/tests/random_composition_perturbation.prm index e23c5c681bd..6852e5721cd 100644 --- a/tests/random_composition_perturbation.prm +++ b/tests/random_composition_perturbation.prm @@ -1,43 +1,45 @@ -# Test to check the status of the random initial temperature perturbation +# Test to check the status of the random initial composition perturbation # plugin. -set End time = 0 +subsection Model settings + set Tangential velocity boundary indicators = left, right, bottom, top +end -subsection Boundary temperature model - set List of model names = box +subsection Mesh refinement + set Initial global refinement = 3 end +subsection Postprocess + set List of postprocessors = composition statistics +end -subsection Gravity model - set Model name = vertical +subsection Compositional fields + set Number of fields = 2 end +subsection Material model + set Model name = simpler +end +set End time = 0 subsection Geometry model set Model name = box - - subsection Box - set X extent = 1 - set Y extent = 1 - end end +subsection Gravity model + set Model name = vertical +end subsection Initial temperature model set List of model names = function - subsection Function set Function expression = 0.5 end -end -subsection Compositional fields - set Number of fields = 2 end subsection Initial composition model set List of model names = function, random perturbation - subsection Random perturbation set Magnitude = 0.2 end @@ -45,25 +47,10 @@ subsection Initial composition model subsection Function set Function expression = 0.5;0.0 end -end - -subsection Material model - set Model name = simpler end - -subsection Mesh refinement - set Initial adaptive refinement = 0 - set Initial global refinement = 3 -end - - -subsection Model settings - set Tangential velocity boundary indicators = left, right, bottom, top -end - -subsection Postprocess - set List of postprocessors = composition statistics +subsection Boundary temperature model + set List of model names = box end diff --git a/tests/random_composition_perturbation/screen-output b/tests/random_composition_perturbation/screen-output index a8a8dc6da65..1a746fdca3b 100644 --- a/tests/random_composition_perturbation/screen-output +++ b/tests/random_composition_perturbation/screen-output @@ -10,7 +10,7 @@ Number of degrees of freedom: 1,526 (578+81+289+289+289) Solving Stokes system... 2+0 iterations. Postprocessing: - Compositions min/max/mass: 0.3001/0.6984/0.4938 // -0.2/0.1996/-0.01184 + Compositions min/max/mass: 0.3002/0.6997/0.5069 // -0.199/0.1999/-0.007404 Termination requested by criterion: end time diff --git a/tests/random_composition_perturbation/statistics b/tests/random_composition_perturbation/statistics index 992b20ca031..ad7b7e757d9 100644 --- a/tests/random_composition_perturbation/statistics +++ b/tests/random_composition_perturbation/statistics @@ -17,4 +17,4 @@ # 17: Minimal value for composition C_2 # 18: Maximal value for composition C_2 # 19: Global mass for composition C_2 -0 0.000000000000e+00 0.000000000000e+00 64 659 289 578 0 0 0 2 3 2 3.00090365e-01 6.98414874e-01 4.93781411e-01 -1.99952361e-01 1.99564844e-01 -1.18366501e-02 +0 0.000000000000e+00 0.000000000000e+00 64 659 289 578 0 0 0 2 3 2 3.00222459e-01 6.99744700e-01 5.06886136e-01 -1.99012748e-01 1.99867409e-01 -7.40357014e-03 diff --git a/tests/random_temperature_perturbation.prm b/tests/random_temperature_perturbation.prm index 46412b2ae62..52afa7b456a 100644 --- a/tests/random_temperature_perturbation.prm +++ b/tests/random_temperature_perturbation.prm @@ -1,31 +1,33 @@ # Test to check the status of the random initial temperature perturbation # plugin. -set End time = 0 - -subsection Boundary temperature model - set List of model names = box +subsection Model settings + set Tangential velocity boundary indicators = left, right, bottom, top end +subsection Mesh refinement + set Initial global refinement = 3 +end -subsection Gravity model - set Model name = vertical +subsection Postprocess + set List of postprocessors = temperature statistics end +subsection Material model + set Model name = simpler +end +set End time = 0 subsection Geometry model set Model name = box - - subsection Box - set X extent = 1 - set Y extent = 1 - end end +subsection Gravity model + set Model name = vertical +end subsection Initial temperature model set List of model names = function, random perturbation - subsection Random perturbation set Magnitude = 0.2 end @@ -33,25 +35,10 @@ subsection Initial temperature model subsection Function set Function expression = 0.5 end -end - - -subsection Material model - set Model name = simpler -end - - -subsection Mesh refinement - set Initial adaptive refinement = 0 - set Initial global refinement = 3 -end - -subsection Model settings - set Tangential velocity boundary indicators = left, right, bottom, top end -subsection Postprocess - set List of postprocessors = temperature statistics +subsection Boundary temperature model + set List of model names = box end diff --git a/tests/random_temperature_perturbation/screen-output b/tests/random_temperature_perturbation/screen-output index 88bdd9e8546..ecaf9b513bd 100644 --- a/tests/random_temperature_perturbation/screen-output +++ b/tests/random_temperature_perturbation/screen-output @@ -8,7 +8,7 @@ Number of degrees of freedom: 948 (578+81+289) Solving Stokes system... 17+0 iterations. Postprocessing: - Temperature min/avg/max: 0.3001 K, 0.5056 K, 0.6973 K + Temperature min/avg/max: 0.3017 K, 0.5073 K, 0.6952 K Termination requested by criterion: end time diff --git a/tests/random_temperature_perturbation/statistics b/tests/random_temperature_perturbation/statistics index fa519b561bc..e71bc4ffc51 100644 --- a/tests/random_temperature_perturbation/statistics +++ b/tests/random_temperature_perturbation/statistics @@ -11,4 +11,4 @@ # 11: Minimal temperature (K) # 12: Average temperature (K) # 13: Maximal temperature (K) -0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 17 18 17 3.00129639e-01 5.05619087e-01 6.97305210e-01 +0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 17 18 17 3.01709841e-01 5.07329437e-01 6.95230736e-01 From e21480a8964d17972cc0eeebc277fa53ef556f91 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Thu, 6 Jul 2017 11:17:29 -0600 Subject: [PATCH 05/12] Update tests --- source/initial_temperature/random_perturbation.cc | 3 ++- tests/random_temperature_perturbation/screen-output | 2 +- tests/random_temperature_perturbation/statistics | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index f09bb4e1c98..f1b519e29f7 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -48,6 +48,7 @@ namespace aspect initial_temperature (const Point &position) const { std::size_t seed = point_hash(position); + boost::hash_combine(seed,0); if (use_random_seed) boost::hash_combine(seed,time(NULL)); @@ -109,7 +110,7 @@ namespace aspect { ASPECT_REGISTER_INITIAL_TEMPERATURE_MODEL(RandomPerturbation, "random perturbation", - "An initial tempeature anomaly that perturbs the temperature " + "An initial temperature anomaly that perturbs the temperature " "following a random noise with uniform distribution and user " "specified magnitude.") } diff --git a/tests/random_temperature_perturbation/screen-output b/tests/random_temperature_perturbation/screen-output index ecaf9b513bd..8c84b221dbc 100644 --- a/tests/random_temperature_perturbation/screen-output +++ b/tests/random_temperature_perturbation/screen-output @@ -8,7 +8,7 @@ Number of degrees of freedom: 948 (578+81+289) Solving Stokes system... 17+0 iterations. Postprocessing: - Temperature min/avg/max: 0.3017 K, 0.5073 K, 0.6952 K + Temperature min/avg/max: 0.3006 K, 0.4981 K, 0.6998 K Termination requested by criterion: end time diff --git a/tests/random_temperature_perturbation/statistics b/tests/random_temperature_perturbation/statistics index e71bc4ffc51..1acb9b4bc8e 100644 --- a/tests/random_temperature_perturbation/statistics +++ b/tests/random_temperature_perturbation/statistics @@ -11,4 +11,4 @@ # 11: Minimal temperature (K) # 12: Average temperature (K) # 13: Maximal temperature (K) -0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 17 18 17 3.01709841e-01 5.07329437e-01 6.95230736e-01 +0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 17 18 17 3.00591720e-01 4.98143186e-01 6.99842861e-01 From 1e2ebdcafb6262e2a377b5a0c6aaa2a2beddfa89 Mon Sep 17 00:00:00 2001 From: Rene Gassmoeller Date: Fri, 10 May 2019 19:19:15 -0700 Subject: [PATCH 06/12] Rebase and update tests --- tests/random_composition_perturbation.prm | 2 +- tests/random_composition_perturbation/statistics | 2 +- tests/random_temperature_perturbation.prm | 2 +- tests/random_temperature_perturbation/screen-output | 4 ++-- tests/random_temperature_perturbation/statistics | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/random_composition_perturbation.prm b/tests/random_composition_perturbation.prm index 6852e5721cd..40c627c759c 100644 --- a/tests/random_composition_perturbation.prm +++ b/tests/random_composition_perturbation.prm @@ -1,7 +1,7 @@ # Test to check the status of the random initial composition perturbation # plugin. -subsection Model settings +subsection Boundary velocity model set Tangential velocity boundary indicators = left, right, bottom, top end diff --git a/tests/random_composition_perturbation/statistics b/tests/random_composition_perturbation/statistics index ad7b7e757d9..9743ff8bf2b 100644 --- a/tests/random_composition_perturbation/statistics +++ b/tests/random_composition_perturbation/statistics @@ -17,4 +17,4 @@ # 17: Minimal value for composition C_2 # 18: Maximal value for composition C_2 # 19: Global mass for composition C_2 -0 0.000000000000e+00 0.000000000000e+00 64 659 289 578 0 0 0 2 3 2 3.00222459e-01 6.99744700e-01 5.06886136e-01 -1.99012748e-01 1.99867409e-01 -7.40357014e-03 +0 0.000000000000e+00 0.000000000000e+00 64 659 289 578 0 0 0 1 3 2 3.00222459e-01 6.99744700e-01 5.06886136e-01 -1.99012748e-01 1.99867409e-01 -7.40357014e-03 diff --git a/tests/random_temperature_perturbation.prm b/tests/random_temperature_perturbation.prm index 52afa7b456a..9e560a64693 100644 --- a/tests/random_temperature_perturbation.prm +++ b/tests/random_temperature_perturbation.prm @@ -1,7 +1,7 @@ # Test to check the status of the random initial temperature perturbation # plugin. -subsection Model settings +subsection Boundary velocity model set Tangential velocity boundary indicators = left, right, bottom, top end diff --git a/tests/random_temperature_perturbation/screen-output b/tests/random_temperature_perturbation/screen-output index 8c84b221dbc..944cd96a0d8 100644 --- a/tests/random_temperature_perturbation/screen-output +++ b/tests/random_temperature_perturbation/screen-output @@ -5,10 +5,10 @@ Number of degrees of freedom: 948 (578+81+289) *** Timestep 0: t=0 years Solving temperature system... 0 iterations. Rebuilding Stokes preconditioner... - Solving Stokes system... 17+0 iterations. + Solving Stokes system... 14+0 iterations. Postprocessing: - Temperature min/avg/max: 0.3006 K, 0.4981 K, 0.6998 K + Temperature min/avg/max: 0.302 K, 0.4957 K, 0.6998 K Termination requested by criterion: end time diff --git a/tests/random_temperature_perturbation/statistics b/tests/random_temperature_perturbation/statistics index 1acb9b4bc8e..b5bfb68e845 100644 --- a/tests/random_temperature_perturbation/statistics +++ b/tests/random_temperature_perturbation/statistics @@ -11,4 +11,4 @@ # 11: Minimal temperature (K) # 12: Average temperature (K) # 13: Maximal temperature (K) -0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 17 18 17 3.00591720e-01 4.98143186e-01 6.99842861e-01 +0 0.000000000000e+00 0.000000000000e+00 64 659 289 0 13 15 14 3.02047032e-01 4.95676273e-01 6.99758663e-01 From 2611b01177e1967f2d0546c3984256b9b4b557e7 Mon Sep 17 00:00:00 2001 From: Madeleine Kerr Date: Thu, 6 Jun 2024 13:59:45 +0000 Subject: [PATCH 07/12] Add comments for point_hash function --- source/initial_temperature/random_perturbation.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index f1b519e29f7..c63b5b5aecc 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -31,8 +31,14 @@ namespace aspect namespace { template - std::size_t point_hash(const Point &position) + std::size_t point_hash(const Point &position) { + // Generates one combined hash function for a d dimension point + // by creating a hash and combining it with each coordinate element + // of the Point position (d hash combines total). Returns a single + // hash with the combined effect of all elements of a point's position + // such that the same d dimensional point object generates the same hash. + std::size_t hash; for (unsigned int i = 0; i < dim; ++i) From 3dcea95a576ca12044164453ee812a576184ed4e Mon Sep 17 00:00:00 2001 From: Madeleine Kerr Date: Thu, 6 Jun 2024 19:49:30 +0000 Subject: [PATCH 08/12] Comment out position hash to test RNG seed=1 --- .../aspect/initial_temperature/random_perturbation.h | 9 ++++++++- source/initial_temperature/random_perturbation.cc | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index fba16800947..a4c919ae5d0 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -65,7 +65,6 @@ namespace aspect void parse_parameters (ParameterHandler &prm); - private: /** * The maximal magnitude of the random noise. @@ -80,6 +79,14 @@ namespace aspect * the same setup. */ bool use_random_seed; + + /** + * A random number generator used by this class to get + * random temperature perturbations. Should return the same + * random numbers every time since it is seeded with one. + */ + boost::mt19937 random_number_generator(1); + }; } } diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index c63b5b5aecc..954bd7efd51 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -53,13 +53,13 @@ namespace aspect RandomPerturbation:: initial_temperature (const Point &position) const { - std::size_t seed = point_hash(position); - boost::hash_combine(seed,0); + //std::size_t seed = point_hash(position); + //boost::hash_combine(seed,0); - if (use_random_seed) - boost::hash_combine(seed,time(NULL)); + //if (use_random_seed) + // boost::hash_combine(seed,time(NULL)); - boost::mt19937 random_number_generator(seed); + //boost::mt19937 random_number_generator(seed); // Uniform distribution on the interval [-magnitude,magnitude). This // will be used to generate the random temperature perturbation. From a30aeed1813c98b93d7b9ef4c0ffc404c7b6ba08 Mon Sep 17 00:00:00 2001 From: Madeleine Kerr Date: Thu, 6 Jun 2024 21:22:15 +0000 Subject: [PATCH 09/12] Replace boost functions with std random functions --- .../initial_temperature/random_perturbation.h | 5 ++-- .../random_perturbation.cc | 25 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index a4c919ae5d0..0b223de4ff7 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -25,7 +25,8 @@ #include DEAL_II_DISABLE_EXTRA_DIAGNOSTICS -#include +// #include +#include DEAL_II_ENABLE_EXTRA_DIAGNOSTICS namespace aspect @@ -85,7 +86,7 @@ namespace aspect * random temperature perturbations. Should return the same * random numbers every time since it is seeded with one. */ - boost::mt19937 random_number_generator(1); + std::mt19937 random_number_generator(1); }; } diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index 954bd7efd51..d270a03a2c2 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -21,7 +21,8 @@ #include -#include +// #include +#include #include namespace aspect @@ -30,6 +31,14 @@ namespace aspect { namespace { + // This is where you would put hash combine function + template + inline void hash_combine(std::size_t& seed1, const T& v) + { + std::hash hasher; + seed1 ^= hasher(v) + 0x9e3779b9 + (seed1<<6) + (seed1>>2); + } + template std::size_t point_hash(const Point &position) { @@ -42,7 +51,7 @@ namespace aspect std::size_t hash; for (unsigned int i = 0; i < dim; ++i) - boost::hash_combine(hash,position[i]); + hash_combine(hash,position[i]); return hash; } @@ -53,17 +62,17 @@ namespace aspect RandomPerturbation:: initial_temperature (const Point &position) const { - //std::size_t seed = point_hash(position); - //boost::hash_combine(seed,0); + std::size_t seed = point_hash(position); + hash_combine(seed,0); - //if (use_random_seed) - // boost::hash_combine(seed,time(NULL)); + if (use_random_seed) + hash_combine(seed,time(NULL)); - //boost::mt19937 random_number_generator(seed); + std::mt19937 random_number_generator(seed); // Uniform distribution on the interval [-magnitude,magnitude). This // will be used to generate the random temperature perturbation. - boost::random::uniform_real_distribution uniform_distribution(-magnitude,magnitude); + std::uniform_real_distribution uniform_distribution(-magnitude,magnitude); return uniform_distribution(random_number_generator); } From c4cc10fc4636cc296058d6722c4b4b21c007c446 Mon Sep 17 00:00:00 2001 From: Madeleine Kerr Date: Thu, 6 Jun 2024 21:50:24 +0000 Subject: [PATCH 10/12] Replace boost with std for random functions, + cleanup --- .../initial_composition/random_perturbation.h | 9 +++++++- .../initial_temperature/random_perturbation.h | 1 - .../random_perturbation.cc | 21 +++++++++++++------ .../random_perturbation.cc | 1 - 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/aspect/initial_composition/random_perturbation.h b/include/aspect/initial_composition/random_perturbation.h index 3d2eafe9dbe..5b4d41c6c1d 100644 --- a/include/aspect/initial_composition/random_perturbation.h +++ b/include/aspect/initial_composition/random_perturbation.h @@ -25,7 +25,7 @@ #include DEAL_II_DISABLE_EXTRA_DIAGNOSTICS -#include +#include DEAL_II_ENABLE_EXTRA_DIAGNOSTICS namespace aspect @@ -81,6 +81,13 @@ namespace aspect * the same setup. */ bool use_random_seed; + + /** + * A random number generator used by this class to get + * random temperature perturbations. Should return the same + * random numbers every time since it is seeded with one. + */ + std::mt19937 random_number_generator(1); }; } } diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index 0b223de4ff7..f5bdf72455e 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -25,7 +25,6 @@ #include DEAL_II_DISABLE_EXTRA_DIAGNOSTICS -// #include #include DEAL_II_ENABLE_EXTRA_DIAGNOSTICS diff --git a/source/initial_composition/random_perturbation.cc b/source/initial_composition/random_perturbation.cc index cd63fbf31ac..4da7ff0d0b4 100644 --- a/source/initial_composition/random_perturbation.cc +++ b/source/initial_composition/random_perturbation.cc @@ -21,7 +21,8 @@ #include -#include +// #include +#include #include namespace aspect @@ -30,13 +31,20 @@ namespace aspect { namespace { + template + inline void hash_combine(std::size_t& seed1, const T& v) + { + std::hash hasher; + seed1 ^= hasher(v) + 0x9e3779b9 + (seed1<<6) + (seed1>>2); + } + template std::size_t point_hash(const Point &position) { std::size_t hash; for (unsigned int i = 0; i < dim; ++i) - boost::hash_combine(hash,position[i]); + hash_combine(hash,position[i]); return hash; } @@ -49,15 +57,16 @@ namespace aspect const unsigned int compositional_index) const { std::size_t seed = point_hash(position); - boost::hash_combine(seed,compositional_index); + hash_combine(seed,compositional_index); + if (use_random_seed) - boost::hash_combine(seed,time(NULL)); + hash_combine(seed,time(NULL)); - boost::mt19937 random_number_generator(seed); + std::mt19937 random_number_generator(seed); // Uniform distribution on the interval [-magnitude,magnitude). This // will be used to generate the random composition perturbation. - boost::random::uniform_real_distribution uniform_distribution(-magnitude,magnitude); + std::uniform_real_distribution uniform_distribution(-magnitude,magnitude); return uniform_distribution(random_number_generator); } diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index d270a03a2c2..04c53c1934e 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -31,7 +31,6 @@ namespace aspect { namespace { - // This is where you would put hash combine function template inline void hash_combine(std::size_t& seed1, const T& v) { From b280c970e3eb4cf539559b4af77660c1d0c4ec61 Mon Sep 17 00:00:00 2001 From: Madeleine Kerr Date: Thu, 6 Jun 2024 21:54:51 +0000 Subject: [PATCH 11/12] Fix comment typo --- include/aspect/initial_composition/random_perturbation.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/aspect/initial_composition/random_perturbation.h b/include/aspect/initial_composition/random_perturbation.h index 5b4d41c6c1d..da4edd634a6 100644 --- a/include/aspect/initial_composition/random_perturbation.h +++ b/include/aspect/initial_composition/random_perturbation.h @@ -84,7 +84,7 @@ namespace aspect /** * A random number generator used by this class to get - * random temperature perturbations. Should return the same + * random composition perturbations. Should return the same * random numbers every time since it is seeded with one. */ std::mt19937 random_number_generator(1); From 2a1f7bcc3cc2801ea7f4cc8a488c084cc25fbb8f Mon Sep 17 00:00:00 2001 From: Madeleine Kerr Date: Fri, 7 Jun 2024 04:38:38 +0000 Subject: [PATCH 12/12] Updated random perturbation to std and to be testable --- .../initial_composition/random_perturbation.h | 19 +++++---- .../initial_temperature/random_perturbation.h | 15 +++---- .../random_perturbation.cc | 40 +++++------------- .../random_perturbation.cc | 41 ++++--------------- tests/random_composition_perturbation.prm | 1 - tests/random_temperature_perturbation.prm | 1 - 6 files changed, 38 insertions(+), 79 deletions(-) diff --git a/include/aspect/initial_composition/random_perturbation.h b/include/aspect/initial_composition/random_perturbation.h index da4edd634a6..7a0c113e8aa 100644 --- a/include/aspect/initial_composition/random_perturbation.h +++ b/include/aspect/initial_composition/random_perturbation.h @@ -23,6 +23,7 @@ #define _aspect_initial_composition_random_perturbation_h #include +#include DEAL_II_DISABLE_EXTRA_DIAGNOSTICS #include @@ -42,15 +43,16 @@ namespace aspect * @ingroup InitialCompositions */ template - class RandomPerturbation : public Interface + class RandomPerturbation : public Interface, public aspect::SimulatorAccess + { public: + void initialize () override; /** - * Return the initial temperature as a function of position. + * Return the initial composition as a function of position. */ - virtual - double initial_composition (const Point &position, - const unsigned int compositional_index) const; + + double initial_composition (const Point &position, const unsigned int composition_index) const override; /** * Declare the parameters this class takes through input files. @@ -62,9 +64,8 @@ namespace aspect /** * Read the parameters this class declares from the parameter file. */ - virtual void - parse_parameters (ParameterHandler &prm); + parse_parameters (ParameterHandler &prm) override; private: @@ -83,11 +84,11 @@ namespace aspect bool use_random_seed; /** - * A random number generator used by this class to get + * A random number generator used by this class to get * random composition perturbations. Should return the same * random numbers every time since it is seeded with one. */ - std::mt19937 random_number_generator(1); + mutable std::mt19937 random_number_generator; }; } } diff --git a/include/aspect/initial_temperature/random_perturbation.h b/include/aspect/initial_temperature/random_perturbation.h index f5bdf72455e..d1fdf0b1cdd 100644 --- a/include/aspect/initial_temperature/random_perturbation.h +++ b/include/aspect/initial_temperature/random_perturbation.h @@ -23,6 +23,7 @@ #define _aspect_initial_temperature_random_perturbation_h #include +#include DEAL_II_DISABLE_EXTRA_DIAGNOSTICS #include @@ -42,14 +43,15 @@ namespace aspect * @ingroup InitialTemperatures */ template - class RandomPerturbation : public Interface + class RandomPerturbation : public Interface, public aspect::SimulatorAccess { public: + void initialize () override; /** * Return the initial temperature as a function of position. */ - virtual - double initial_temperature (const Point &position) const; + + double initial_temperature (const Point &position) const override; /** * Declare the parameters this class takes through input files. @@ -61,9 +63,8 @@ namespace aspect /** * Read the parameters this class declares from the parameter file. */ - virtual void - parse_parameters (ParameterHandler &prm); + parse_parameters (ParameterHandler &prm) override; private: /** @@ -81,11 +82,11 @@ namespace aspect bool use_random_seed; /** - * A random number generator used by this class to get + * A random number generator used by this class to get * random temperature perturbations. Should return the same * random numbers every time since it is seeded with one. */ - std::mt19937 random_number_generator(1); + mutable std::mt19937 random_number_generator; }; } diff --git a/source/initial_composition/random_perturbation.cc b/source/initial_composition/random_perturbation.cc index 4da7ff0d0b4..425dff106df 100644 --- a/source/initial_composition/random_perturbation.cc +++ b/source/initial_composition/random_perturbation.cc @@ -29,43 +29,25 @@ namespace aspect { namespace InitialComposition { - namespace + template + void + RandomPerturbation:: + initialize() { - template - inline void hash_combine(std::size_t& seed1, const T& v) - { - std::hash hasher; - seed1 ^= hasher(v) + 0x9e3779b9 + (seed1<<6) + (seed1>>2); - } - - template - std::size_t point_hash(const Point &position) - { - std::size_t hash; - - for (unsigned int i = 0; i < dim; ++i) - hash_combine(hash,position[i]); - - return hash; - } + const unsigned int my_rank = Utilities::MPI::this_mpi_process(this->get_mpi_communicator()); + if (use_random_seed) + random_number_generator.seed(time(NULL)+my_rank); + else + random_number_generator.seed(9088+my_rank); } template double RandomPerturbation:: - initial_composition (const Point &position, - const unsigned int compositional_index) const + initial_composition (const Point &position, const unsigned int composition_index) const { - std::size_t seed = point_hash(position); - hash_combine(seed,compositional_index); - - if (use_random_seed) - hash_combine(seed,time(NULL)); - - std::mt19937 random_number_generator(seed); - // Uniform distribution on the interval [-magnitude,magnitude). This - // will be used to generate the random composition perturbation. + // will be used to generate the random temperature perturbation. std::uniform_real_distribution uniform_distribution(-magnitude,magnitude); return uniform_distribution(random_number_generator); } diff --git a/source/initial_temperature/random_perturbation.cc b/source/initial_temperature/random_perturbation.cc index 04c53c1934e..24f186af38f 100644 --- a/source/initial_temperature/random_perturbation.cc +++ b/source/initial_temperature/random_perturbation.cc @@ -29,31 +29,16 @@ namespace aspect { namespace InitialTemperature { - namespace + template + void + RandomPerturbation:: + initialize() { - template - inline void hash_combine(std::size_t& seed1, const T& v) - { - std::hash hasher; - seed1 ^= hasher(v) + 0x9e3779b9 + (seed1<<6) + (seed1>>2); - } - - template - std::size_t point_hash(const Point &position) - { - // Generates one combined hash function for a d dimension point - // by creating a hash and combining it with each coordinate element - // of the Point position (d hash combines total). Returns a single - // hash with the combined effect of all elements of a point's position - // such that the same d dimensional point object generates the same hash. - - std::size_t hash; - - for (unsigned int i = 0; i < dim; ++i) - hash_combine(hash,position[i]); - - return hash; - } + const unsigned int my_rank = Utilities::MPI::this_mpi_process(this->get_mpi_communicator()); + if (use_random_seed) + random_number_generator.seed(time(NULL)+my_rank); + else + random_number_generator.seed(9088+my_rank); } template @@ -61,14 +46,6 @@ namespace aspect RandomPerturbation:: initial_temperature (const Point &position) const { - std::size_t seed = point_hash(position); - hash_combine(seed,0); - - if (use_random_seed) - hash_combine(seed,time(NULL)); - - std::mt19937 random_number_generator(seed); - // Uniform distribution on the interval [-magnitude,magnitude). This // will be used to generate the random temperature perturbation. std::uniform_real_distribution uniform_distribution(-magnitude,magnitude); diff --git a/tests/random_composition_perturbation.prm b/tests/random_composition_perturbation.prm index 40c627c759c..bd82910303d 100644 --- a/tests/random_composition_perturbation.prm +++ b/tests/random_composition_perturbation.prm @@ -53,4 +53,3 @@ end subsection Boundary temperature model set List of model names = box end - diff --git a/tests/random_temperature_perturbation.prm b/tests/random_temperature_perturbation.prm index 9e560a64693..9d8eeded384 100644 --- a/tests/random_temperature_perturbation.prm +++ b/tests/random_temperature_perturbation.prm @@ -41,4 +41,3 @@ end subsection Boundary temperature model set List of model names = box end -