Alamo
Random.H
Go to the documentation of this file.
1//
2// Set each point in the field to a random value between 0 and 1
3//
4
5#ifndef IC_RANDOM_H_
6#define IC_RANDOM_H_
7
8#include <string>
9
10#include "AMReX_Box.H"
11#include "AMReX_Geometry.H"
12#include "AMReX_GpuQualifiers.H"
13#include "AMReX_IndexType.H"
14#include "AMReX_MFIter.H"
15#include "AMReX_Random.H"
16#include "AMReX_RandomEngine.H"
17#include "AMReX_Vector.H"
18
19#include "IC/IC.H"
20#include "IO/ParmParse.H"
21#include "Set/Set.H"
22#include "Util/Util.H"
23
24namespace IC
25{
26/// Set each point to a random value.
27class Random : public IC<Set::Scalar>
28{
29public:
30 static constexpr const char *name = "random";
31
32 Random(amrex::Vector<amrex::Geometry> &_geom, Set::Scalar a_mult) : IC(_geom), mult(a_mult)
33 {
34 }
35 Random(amrex::Vector<amrex::Geometry> &_geom, IO::ParmParse &pp, std::string name) : IC(_geom)
36 {
37 pp.queryclass(name, *this);
38 }
39
40 void Add(const int &lev, Set::Field<Set::Scalar> &a_field, Set::Scalar)
41 {
42 auto mult = this->mult;
43 auto offset = this->offset;
44
45 for (amrex::MFIter mfi(*a_field[lev], amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
46 {
47 amrex::Box bx;
48 amrex::IndexType type = a_field[lev]->ixType();
49 if (type == amrex::IndexType::TheCellType())
50 bx = mfi.growntilebox();
51 else if (type == amrex::IndexType::TheNodeType())
52 bx = mfi.grownnodaltilebox();
53 else
54 Util::Abort(INFO, "Unknown index type");
55
56 Set::Patch<Set::Scalar> field = a_field.Patch(lev, mfi);
57 for (int n = 0; n < a_field[lev]->nComp(); n++)
58 {
59 amrex::ParallelForRNG(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k, amrex::RandomEngine const &engine) {
60 field(i, j, k, n) += offset + mult * amrex::Random(engine);
61 });
62 }
63 }
64 }
65
66 using IC::Add;
67
68 static void Parse(Random &value, IO::ParmParse &pp)
69 {
70 // offset from the [0,1] random number range
71 pp.query_default("offset", value.offset, 0.0);
72 // multiplier for the [0,1] random number range
73 pp.query_default("mult", value.mult, 1.0);
74 }
75
76private:
79};
80}
81#endif
#define INFO
Definition Util.H:21
virtual void Add(const int &lev, Set::Field< T > &field, Set::Scalar time)=0
Set each point to a random value.
Definition Random.H:28
Set::Scalar offset
Definition Random.H:78
Random(amrex::Vector< amrex::Geometry > &_geom, IO::ParmParse &pp, std::string name)
Definition Random.H:35
Set::Scalar mult
Definition Random.H:77
static void Parse(Random &value, IO::ParmParse &pp)
Definition Random.H:68
void Add(const int &lev, Set::Field< Set::Scalar > &a_field, Set::Scalar)
Definition Random.H:40
Random(amrex::Vector< amrex::Geometry > &_geom, Set::Scalar a_mult)
Definition Random.H:32
static constexpr const char * name
Definition Random.H:30
void queryclass(std::string name, T *value)
Definition ParmParse.H:934
int query_default(std::string name, T &value, T defaultvalue)
Definition ParmParse.H:293
amrex::Array4< T > Patch(int lev, amrex::MFIter &mfi) const &
Definition Set.H:76
Initialize a spherical inclusion.
Definition BMP.H:20
amrex::Real Scalar
Definition Base.H:18
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition Base.H:19
void Abort(const char *msg)
Definition Util.cpp:240