Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ target_include_directories(idefix-core PUBLIC
src/fluid/RiemannSolver/MHDsolvers
src/fluid/RiemannSolver/Dustsolvers
src/fluid/tracer
src/forcing
src/output
src/rkl
src/gravity
Expand Down
4 changes: 4 additions & 0 deletions doc/source/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ In this section, you will find a more detailed documentation about each module t
:ref:`pydefixModule`
The Python interaction module, that allows Idefix to interact directly with a python interpreter.

:ref:`forcingModule`
The stochastic forcing module, driving turbulence with Ornstein–Uhlenbeck Fourier modes.


.. toctree::
:maxdepth: 2
Expand All @@ -44,3 +47,4 @@ In this section, you will find a more detailed documentation about each module t
modules/braginskii.rst
modules/gridCoarsening.rst
modules/pydefix.rst
modules/forcing.rst
88 changes: 88 additions & 0 deletions doc/source/modules/forcing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
.. _forcingModule:

Stochastic forcing module
=========================

The forcing module drives turbulence by adding a stochastic external force to the fluid equations.
The force is a superposition of Fourier modes :math:`e^{i\,\mathbf{k}\cdot\mathbf{x}}` whose complex
amplitudes follow independent Ornstein–Uhlenbeck (OU) processes, one per mode and per vector component.
It is typically used to sustain turbulence in decaying-turbulence setups (see e.g. ``test/HD/forcing``).

This approach is identical to the one described in `St-Onge, D. A., M. W. Kunz, J. Squire, et
A. A. Schekochihin, *Journal of Plasma
Physics* 86 (octobre 2020), 905860503 <https://doi.org/10.1017/S0022377820000860>`_.

The forcing term is added to the right-hand side of the momentum equation
(:math:`\partial_t(\rho \mathbf{v}) \mathrel{+}= \rho\,\mathbf{f}`), and to the energy equation
(:math:`\rho\,\mathbf{v}\cdot\mathbf{f}`) when the pressure equation is enabled.

Input parameters
----------------

All parameters are set in the ``[Forcing]`` section of the input file (see :ref:`forcingSection` for
the full list):

``iso3D kmin kmax``
3D isotropic forcing: all wavevectors with :math:`k_\mathrm{min} \le |\mathbf{k}| \le
k_\mathrm{max}` are driven. Cartesian geometry with 3 dimensions and 3 components only.

``iso2D normal kmin kmax``
2D isotropic forcing in the plane perpendicular to ``normal`` (``IDIR``, ``JDIR`` or ``KDIR``),
with :math:`k_\mathrm{min} \le |\mathbf{k}| \le k_\mathrm{max}`.

``solenoidal``
When present, the forcing is projected onto its solenoidal (divergence-free) part, computed as
the curl of the pristine forcing normalised by the mean wavenumber. Not available in polar and
cylindrical geometries.

``mach M``
Alternative to ``t_corr``/``epsilon``: the amplitude is set from a target Mach number ``M``,
using the volume-averaged sound speed :math:`c_s` at :math:`t=0` (``csiso`` in isothermal runs):

.. math::
\epsilon = \frac{(M c_s)^3 k_f}{2\pi}, \qquad
t_\mathrm{corr} = \frac{2\pi}{M c_s k_f}, \qquad
k_f = \frac{k_\mathrm{min}+k_\mathrm{max}}{2}.

``t_corr`` and ``epsilon``
OU correlation time and amplitude, used when ``mach`` is not set. ``epsilon`` is internally
divided by the number of forcing modes, so the total injected amplitude does not depend on how
many modes are used.

``seed``
Seed of the random number generator (default: 0).

``stoptime``
Time after which the forcing is switched off (default: never).

``write``
If set to 1, the time series of the OU amplitudes and of the normal draws are written to
``ou_prank0_seed<seed>.dat`` and ``normal_prank0_seed<seed>.dat`` in the output folder.

How the OU processes are updated
--------------------------------

At each timestep, the complex amplitude :math:`A` of each (mode, component) pair is updated as

.. math::
A_{n+1} = \frac{\bar{A}}{\sqrt{2}}
+ \left(A_n - \frac{\bar{A}}{\sqrt{2}}\right) e^{-\Delta t / t_\mathrm{corr}}
+ \sqrt{\frac{\epsilon}{\sqrt{2}\,t_\mathrm{corr}}
\left(1 - e^{-2\Delta t / t_\mathrm{corr}}\right)}\, \mathcal{N},

where :math:`\bar{A}` is the (zero) mean and :math:`\mathcal{N}` a standard normal draw. The
resulting forcing field at a cell is :math:`\mathbf{f}(\mathbf{x}) =
\sum_l \mathrm{Re}\left[A_l\, e^{i\,\mathbf{k}_l\cdot\mathbf{x}}\right]`.

Random numbers and reproducibility
----------------------------------

Random draws are produced by ``idfx::RandomXorShift64`` (``src/utils/random.hpp``): one
self-managed xorshift64 stream per (mode, component), with standard normal draws obtained through
the Box–Muller transform. Unlike a Kokkos random pool, the sequence is deterministic on every
backend, thread count and MPI rank, and identical on all ranks, and we can restore easily
its state from a restart dump.

The complete forcing state (OU amplitudes ``ouValues``, random state ``ouRngState``, and the OU
parameters ``ouMeans``, ``ouTcorrs``, ``ouEpsilons``) is stored in the restart dumps, so a restart
is bit-exact: the forcing applied after a restart is identical to that of an uninterrupted run.
37 changes: 37 additions & 0 deletions doc/source/reference/idefix.ini.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,43 @@ This section enables the orbital advection algorithm provided in *Idefix*. More
| | | | Default: 10 |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+

.. _forcingSection:

``Forcing`` section
-------------------

This section enables the stochastic forcing module provided in *Idefix*, which drives turbulence with
Ornstein-Uhlenbeck Fourier modes. More information may be found in :ref:`forcingModule`

+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| Entry name | Parameter type | | Comment |
+================+=========================+=============================================================================================+
| iso3D | real, real | | 3D isotropic forcing: all wavevectors with ``kmin <= |k| <= kmax`` are driven. Cartesian |
| | | | geometry with 3 dimensions and 3 components only. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| iso2D | string, real, real | | 2D isotropic forcing in the plane perpendicular to ``normal`` (``IDIR``, ``JDIR`` or |
| | | | ``KDIR``), with ``kmin <= |k| <= kmax``. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| solenoidal | (flag) | | Projects the forcing onto its solenoidal (divergence-free) part. Not available in polar |
| | | | and cylindrical geometries. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| mach | real | | Alternative to ``t_corr``/``epsilon``: sets the amplitude from a target Mach number |
| | | | ``M``, using the volume-averaged sound speed at t=0 (``csiso`` in isothermal runs). |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| t_corr | real | | Ornstein-Uhlenbeck correlation time. Used when ``mach`` is not set. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| epsilon | real | | Ornstein-Uhlenbeck amplitude. Used when ``mach`` is not set. Internally divided by the |
| | | | number of forcing modes. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| seed | integer | | Seed of the random number generator. Default is 0. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| stoptime | real | | Time after which the forcing is switched off. Default is infinity (never). |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+
| write | integer | | If set to 1, writes the time series of the OU amplitudes and normal draws to the output |
| | | | folder (``ou_prank0_seed<seed>.dat`` and ``normal_prank0_seed<seed>.dat``). Default is 0. |
+----------------+-------------------------+---------------------------------------------------------------------------------------------+


.. _gravitySection:

``Gravity`` section
Expand Down
5 changes: 5 additions & 0 deletions pytools/dump_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
FLOAT_SIZE = 4
INT_SIZE = 4
INT64_SIZE = 8
UINT64_SIZE = 8
BOOL_SIZE = 1

HEADER_SIZE = 128
Expand Down Expand Up @@ -83,6 +84,10 @@ def __init__(self, fh, byteorder="little"):
mysize = INT64_SIZE
stringchar = "q"
dtype = "int64"
elif self.type == 7:
mysize = UINT64_SIZE
stringchar = "Q"
dtype = "uint64"
else:
raise RuntimeError(
"Found unknown data type %d for field %s" % (self.type, self.name)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

add_subdirectory(fluid)
add_subdirectory(dataBlock)
add_subdirectory(forcing)
add_subdirectory(output)
add_subdirectory(rkl)
add_subdirectory(gravity)
Expand Down
8 changes: 8 additions & 0 deletions src/dataBlock/dataBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "dataBlock.hpp"
#include "fluid.hpp"
#include "gravity.hpp"
#include "forcing.hpp"
#include "planetarySystem.hpp"
#include "vtk.hpp"
#include "dump.hpp"
Expand Down Expand Up @@ -154,6 +155,12 @@ DataBlock::DataBlock(Grid &grid, Input &input) {
this->haveGravity = true;
}

// Initialise forcing if needed
if(input.CheckBlock("Forcing")) {
this->forcing = std::make_unique<Forcing>(input, this);
this->haveForcing = true;
}

// Initialise dust grains if needed
if(input.CheckBlock("Dust")) {
haveDust = true;
Expand Down Expand Up @@ -328,6 +335,7 @@ void DataBlock::ShowConfig() {
<< std::endl;
if(haveUserStepLast) idfx::cout << "DataBlock: User's last step has been enrolled."
<< std::endl;
if(haveForcing) forcing->ShowConfig();
if(haveDust) {
idfx::cout << "DataBlock: evolving " << dust.size() << " dust species." << std::endl;
// Only show the config the first dust specie
Expand Down
6 changes: 6 additions & 0 deletions src/dataBlock/dataBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "gridHost.hpp"
#include "planetarySystem.hpp"
#include "gravity.hpp"
#include "forcing.hpp"
#include "stateContainer.hpp"

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -150,6 +151,7 @@ class DataBlock {

void EvolveStage(); ///< Evolve this DataBlock by dt
void EvolveRKLStage(); ///< Evolve this DataBlock by dt for terms impacted by RKL
void EvolveForcing(); ///< Evolve this DataBlock by dt for the forcing term
void SetBoundaries(); ///< Enforce boundary conditions to this datablock
void ConsToPrim(); ///< Convert conservative to primitive variables
void PrimToCons(); ///< Convert primitive to conservative variables
Expand All @@ -172,6 +174,10 @@ class DataBlock {
bool haveGravity{false};
std::unique_ptr<Gravity> gravity;

// Do we have Forcing ?
bool haveForcing{false};
std::unique_ptr<Forcing> forcing;

// User step functions (before or after the main integrator step)
void LaunchUserStepFirst(); ///< perform user-defined step before main integration step
void LaunchUserStepLast(); ///< Perform user-defined step after main integration step
Expand Down
17 changes: 17 additions & 0 deletions src/dataBlock/evolveStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ void DataBlock::EvolveRKLStage() {
}
idfx::popRegion();
}


void DataBlock::EvolveForcing() {
idfx::pushRegion("DataBlock::EvolveForcing");
forcing->ComputeForcing(dt);
if (forcing->write) {
forcing->oUprocesses.WriteProcessesValues(t);
forcing->oUprocesses.WriteNormalValues(t);
}

if (forcing->stillHaveForcing && t > forcing->stopTime) forcing->stillHaveForcing = false;

if(forcing->stillHaveForcing) {
hydro->EvolveForcing(t, dt);
}
idfx::popRegion();
}
11 changes: 11 additions & 0 deletions src/fluid/addForcingSourceTerms.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// ***********************************************************************************
// Idefix MHD astrophysical code
// Copyright(C) Geoffroy R. J. Lesur <geoffroy.lesur@univ-grenoble-alpes.fr>
// and other code contributors
// Licensed under CeCILL 2.1 License, see COPYING for more information
// ***********************************************************************************

#ifndef FLUID_ADDFORCINGSOURCETERMS_HPP_
#define FLUID_ADDFORCINGSOURCETERMS_HPP_

#endif // FLUID_ADDFORCINGSOURCETERMS_HPP_
33 changes: 33 additions & 0 deletions src/fluid/evolveStage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "fluid.hpp"
#include "riemannSolver.hpp"
#include "forcing.hpp"

template<typename Phys>
template<int dir>
void Fluid<Phys>::LoopDir(const real t, const real dt) {
Expand Down Expand Up @@ -95,4 +97,35 @@ void Fluid<Phys>::EvolveStage(const real t, const real dt) {

idfx::popRegion();
}


// Evolve one step forward in time of forcing
template<typename Phys>
void Fluid<Phys>::EvolveForcing(const real t, const real dt) {
idfx::pushRegion("Fluid::EvolveForcing");

auto Uc = this->Uc;
auto Vc = this->Vc;
auto forcingTerm = this->data->forcing->forcingTerm;

idefix_for("EvolveForcing",
data->beg[KDIR],data->end[KDIR],
data->beg[JDIR],data->end[JDIR],
data->beg[IDIR],data->end[IDIR],
KOKKOS_LAMBDA (int k, int j, int i) {
for(int component = 0 ; component < COMPONENTS ; component++) {
Uc(MX1+component,k,j,i) += dt * Vc(RHO,k,j,i) * forcingTerm(component,k,j,i);
if constexpr(Phys::pressure) {
// rho * v . f
Uc(ENG,k,j,i) += dt * Vc(RHO,k,j,i)
* Vc(VX1+component,k,j,i)
* forcingTerm(component,k,j,i);
} // pressure
}
});


idfx::popRegion();
}

#endif //FLUID_EVOLVESTAGE_HPP_
2 changes: 2 additions & 0 deletions src/fluid/fluid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "eos.hpp"
#include "thermalDiffusion.hpp"
#include "bragThermalDiffusion.hpp"
#include "forcing.hpp"
#include "selfGravity.hpp"
#include "vtk.hpp"
#include "dump.hpp"
Expand Down Expand Up @@ -73,6 +74,7 @@ class Fluid {
real CheckDivB();
void EvolveStage(const real, const real);
void ResetStage();
void EvolveForcing(const real, const real);
void ShowConfig();
IdefixArray4D<real> GetFlux(int dir) {return this->FluxRiemann.at(dir);}
int CheckNan();
Expand Down
6 changes: 6 additions & 0 deletions src/forcing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target_sources(idefix
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/forcing.hpp
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/forcing.cpp
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/OrnsteinUhlenbeckProcess.hpp
PUBLIC ${CMAKE_CURRENT_LIST_DIR}/OrnsteinUhlenbeckProcess.cpp
)
Loading
Loading