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

Random initial temperature and compositional plugins #5831

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions doc/modules/changes/20170609_gassmoeller
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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.
<br>
(Rene Gassmoeller, 2017/06/19)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to update the date and add your name here as first author.

96 changes: 96 additions & 0 deletions include/aspect/initial_composition/random_perturbation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
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
<http://www.gnu.org/licenses/>.
*/


#ifndef _aspect_initial_composition_random_perturbation_h
#define _aspect_initial_composition_random_perturbation_h

#include <aspect/initial_composition/interface.h>
#include <aspect/simulator_access.h>

DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know there is no need for DEAL_II_DISABLE_EXTRA_DIAGNOSTICS anymore and also DEAL_II_ENABLE_EXTRA_DIAGNOSTICS. (keep the include).

#include <random>
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 <int dim>
class RandomPerturbation : public Interface<dim>, public aspect::SimulatorAccess<dim>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line

{
public:
void initialize () override;
/**
* Return the initial composition as a function of position.
*/

double initial_composition (const Point<dim> &position, const unsigned int composition_index) const override;

/**
* 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.
*/
void
parse_parameters (ParameterHandler &prm) override;


private:
/**
* The maximal magnitude of the random noise.
*/
double magnitude;

/**
* 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.
*/
bool use_random_seed;

/**
* 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.
Comment on lines +88 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the sentence, since the behavior depends on the input parameters.

*/
mutable std::mt19937 random_number_generator;
};
}
}

#endif
95 changes: 95 additions & 0 deletions include/aspect/initial_temperature/random_perturbation.h
Original file line number Diff line number Diff line change
@@ -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
<http://www.gnu.org/licenses/>.
*/


#ifndef _aspect_initial_temperature_random_perturbation_h
#define _aspect_initial_temperature_random_perturbation_h

#include <aspect/initial_temperature/interface.h>
#include <aspect/simulator_access.h>

DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

#include <random>
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. The perturbation follows
* a random noise of a prescribed magnitude.
*
* @ingroup InitialTemperatures
*/
template <int dim>
class RandomPerturbation : public Interface<dim>, public aspect::SimulatorAccess<dim>
{
public:
void initialize () override;
/**
* Return the initial temperature as a function of position.
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line, instead add one before the comment. Also please add documentation for the initialize function above (check the other plugins, you can likely copy the documentation).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also check the initial temperature plugin for the same function

double initial_temperature (const Point<dim> &position) const override;

/**
* 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.
*/
void
parse_parameters (ParameterHandler &prm) override;

private:
/**
* The maximal magnitude of the random noise.
*/
double magnitude;

/**
* 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.
*/
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.
*/
mutable std::mt19937 random_number_generator;

};
}
}

#endif
109 changes: 109 additions & 0 deletions source/initial_composition/random_perturbation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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
<http://www.gnu.org/licenses/>.
*/


#include <aspect/initial_composition/random_perturbation.h>

// #include <boost/functional/hash.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this header file

#include <random>
#include <time.h>

namespace aspect
{
namespace InitialComposition
{
template <int dim>
void
RandomPerturbation<dim>::
initialize()
{
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);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our typical code style is to have 3 empty lines between function implementations in the source file (and 1 empty line between function declarations in the header file).

template <int dim>
double
RandomPerturbation<dim>::
initial_composition (const Point<dim> &position, const unsigned int composition_index) const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler complains because none of the input parameters are used, do this instead:

Suggested change
initial_composition (const Point<dim> &position, const unsigned int composition_index) const
initial_composition (const Point<dim> &/*position*/, const unsigned int /*composition_index*/) const

{
// Uniform distribution on the interval [-magnitude,magnitude). This
// will be used to generate the random temperature perturbation.
std::uniform_real_distribution<double> uniform_distribution(-magnitude,magnitude);
return uniform_distribution(random_number_generator);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above, check empty lines here and for the other plugins. this was likely wrong in the earlier pull request already.

template <int dim>
void
RandomPerturbation<dim>::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.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 subsequent model runs of"
"the same setup.");
}
prm.leave_subsection ();
}
prm.leave_subsection ();
}


template <int dim>
void
RandomPerturbation<dim>::parse_parameters (ParameterHandler &prm)
{
prm.enter_subsection ("Initial composition model");
{
prm.enter_subsection("Random perturbation");
{
magnitude = prm.get_double ("Magnitude");
use_random_seed = prm.get_bool("Use random seed");
}
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.")
}
}
Loading
Loading