Alamo
Set.H
Go to the documentation of this file.
1//
2// The Set namespace defines the Alamo datatypes.
3// This includes scalars (:code:`Set::Scalar`), vectors (:code:`Set::Vector`), and nD tensors (:code:`Set::Tensor`).
4// Scalars are an alias for AMReX :code:`Real`.
5// Vectors and tensors are an alias for the Tuxfamily Eigen vectors and matrices, and so you can use
6// Eigen operations on them (like :code:`A.determinant()`, etc.)
7// See the Eigen documentation for more.
8//
9// The Set namespace defines the :code:`Matrix4` datatype for fourth order tensors.
10// These can have isotropic, major, minor, majorminor, etc., symmetries.
11//
12// The Set namespace also defines the :code:`Field` datatype, which is where most data is stored.
13// It is templated so that :code:`Set::Field<Set::Scalar>` is a field of scalars, :code:`Set::Field<Set::Vector>`
14// is a vector field, etc.
15// One can also have, for instance, a field of models, like :code:`Set::Field<Model::Solid::Linear::Iostropic>`.
16//
17
18#ifndef SET_SET_
19#define SET_SET_
20
21#include <iomanip>
22
23#include "Util/Util.H"
24
25#include "Set/Base.H"
26/// \brief A collection of data types and symmetry-reduced data structures
27
28namespace Set
29{
30
32 Node = 0, Edge = 1, Face = 2, Cell = 3
33};
34
35using HC = Hypercube;
36
37//class Field : public amrex::MultiFab
38//{
39
40//};
41template <class T>
42class Field : public amrex::Vector<std::unique_ptr<amrex::FabArray<amrex::BaseFab<T>>>>
43{
44public:
45 Field() {}
46 //~Field() {}
47 Field(int a_levs, const amrex::Vector<amrex::BoxArray> & a_grids, const amrex::Vector<amrex::DistributionMapping> & a_dmap, int a_ncomp, int a_nghost)
48 {
50 }
51 Field(int size) : amrex::Vector<std::unique_ptr<amrex::FabArray<amrex::BaseFab<T>>>>(size) {}
52 void Define(int a_levs, const amrex::Vector<amrex::BoxArray> & a_grids, const amrex::Vector<amrex::DistributionMapping> & a_dmap, int a_ncomp, int a_nghost)
53 {
56 this->resize(a_levs);
57 for (int lev = 0; lev < a_levs; lev++)
58 {
59 (*this)[lev].reset(new amrex::FabArray<amrex::BaseFab<T>>(a_grids[lev],a_dmap[lev],a_ncomp,a_nghost));
60 }
61 }
62 void Define(int a_lev, const amrex::BoxArray & a_grid, const amrex::DistributionMapping & a_dmap, int a_ncomp, int a_nghost)
63 {
65 (*this)[a_lev].reset(new amrex::FabArray<amrex::BaseFab<T>>(a_grid,a_dmap,a_ncomp,a_nghost));
66 }
67 int finest_level = 0;
68 void Copy(int /*a_lev*/, amrex::MultiFab &/*a_dst*/, int /*a_dstcomp*/, int /*a_nghost*/) const {}
69 void CopyFrom(int /*a_lev*/, amrex::MultiFab &/*a_dst*/, int /*a_dstcomp*/, int /*a_nghost*/) const {}
70 void Add(int /*a_lev*/, amrex::MultiFab &/*a_dst*/, int /*a_dstcomp*/, int /*a_nghost*/) const {}
71 void AddFrom(int /*a_lev*/, amrex::MultiFab &/*a_src*/, int /*a_srccomp*/, int /*a_nghost*/) const {}
72 int NComp() const {return 0;}
73 std::string Name(int) const {return name;}
74 std::string name;
75
76 amrex::Array4<T> Patch (int lev, amrex::MFIter &mfi) const &
77 {
78 if (this->size()) return (*(*this)[lev]).array(mfi);
79 else return empty;
80 }
81
82
83 amrex::Array4<T> empty;
84};
85
86
87template <typename T>
88using Patch = amrex::Array4<T> const&;
89
90
91template<>
93void Field<Set::Vector>::Copy(int a_lev, amrex::MultiFab &a_dst, int a_dstcomp, int a_nghost) const
94{
95 for (amrex::MFIter mfi(a_dst, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
96 {
97 const amrex::Box& bx = mfi.growntilebox(amrex::IntVect(a_nghost));
98 if (bx.ok())
99 {
100 amrex::Array4<const Set::Vector> const & src = ((*this)[a_lev])->array(mfi);
101 amrex::Array4<Set::Scalar> const & dst = a_dst.array(mfi);
102 for (int n = 0; n < AMREX_SPACEDIM; n++)
103 {
104 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
105 dst(i,j,k,a_dstcomp + n) = src(i,j,k)(n);
106 });
107 }
108 }
109 }
110}
111template<>
113void Field<Set::Vector>::CopyFrom(int a_lev, amrex::MultiFab &a_src, int a_dstcomp, int a_nghost) const
114{
115 for (amrex::MFIter mfi(a_src, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
116 {
117 const amrex::Box& bx = mfi.growntilebox(amrex::IntVect(a_nghost));
118 if (bx.ok())
119 {
120 amrex::Array4<Set::Vector> const & dst = ((*this)[a_lev])->array(mfi);
121 amrex::Array4<const Set::Scalar> const & src = a_src.array(mfi);
122 for (int n = 0; n < AMREX_SPACEDIM; n++)
123 {
124 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
125 dst(i,j,k)(n) = src(i,j,k,a_dstcomp + n);
126 });
127 }
128 }
129 }
130}
131template<>
133void Field<Set::Vector>::Add(int a_lev, amrex::MultiFab &a_dst, int a_dstcomp, int a_nghost) const
134{
135 for (amrex::MFIter mfi(a_dst, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
136 {
137 const amrex::Box& bx = mfi.growntilebox(amrex::IntVect(a_nghost));
138 if (bx.ok())
139 {
140 amrex::Array4<const Set::Vector> const & src = ((*this)[a_lev])->array(mfi);
141 amrex::Array4<Set::Scalar> const & dst = a_dst.array(mfi);
142 for (int n = 0; n < AMREX_SPACEDIM; n++)
143 {
144 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
145 dst(i,j,k,a_dstcomp + n) += src(i,j,k)(n);
146 });
147 }
148 }
149 }
150}
151template<>
153void Field<Set::Vector>::AddFrom(int a_lev, amrex::MultiFab &a_src, int a_srccomp, int a_nghost) const
154{
155 for (amrex::MFIter mfi(a_src, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
156 {
157 const amrex::Box& bx = mfi.growntilebox(amrex::IntVect(a_nghost));
158 if (bx.ok())
159 {
160 amrex::Array4<Set::Vector> const & dst = ((*this)[a_lev])->array(mfi);
161 amrex::Array4<const Set::Scalar> const & src = a_src.array(mfi);
162 for (int n = 0; n < AMREX_SPACEDIM; n++)
163 {
164 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
165 dst(i,j,k)(n) += src(i,j,k,a_srccomp + n);
166 });
167 }
168 }
169 }
170}
171
173template<> ALAMO_SINGLE_DEFINITION std::string Field<Set::Vector>::Name(int i) const
174{
175 #if AMREX_SPACEDIM>0
176 if (i==0) return name + "_x";
177 #endif
178 #if AMREX_SPACEDIM>1
179 else if (i==1) return name + "_y";
180 #endif
181 #if AMREX_SPACEDIM>2
182 else if (i==2) return name + "_z";
183 #endif
184 else Util::Abort(INFO,"Invalid component");
185 return "";
186}
187
188template<>
190void Field<Set::Matrix>::Copy(int a_lev, amrex::MultiFab &a_dst, int a_dstcomp, int a_nghost) const
191{
192 for (amrex::MFIter mfi(a_dst, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
193 {
194 const amrex::Box& bx = mfi.growntilebox(amrex::IntVect(a_nghost));
195 if (bx.ok())
196 {
197 amrex::Array4<const Set::Matrix> const & src = ((*this)[a_lev])->array(mfi);
198 amrex::Array4<Set::Scalar> const & dst = a_dst.array(mfi);
199 for (int n = 0; n < AMREX_SPACEDIM*AMREX_SPACEDIM; n++)
200 {
201 amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
203 });
204 }
205 }
206 }
207}
208template<>
210template<> ALAMO_SINGLE_DEFINITION std::string Field<Set::Matrix>::Name(int i) const
211{
212 #if AMREX_SPACEDIM==1
213 if (i==0) return name + "_xx";
214 #elif AMREX_SPACEDIM==2
215 if (i==0) return name + "_xx";
216 else if (i==1) return name + "_xy";
217 else if (i==2) return name + "_yx";
218 else if (i==3) return name + "_yy";
219 #elif AMREX_SPACEDIM==3
220 if (i==0) return name + "_xx";
221 else if (i==1) return name + "_xy";
222 else if (i==2) return name + "_xz";
223 else if (i==3) return name + "_yx";
224 else if (i==4) return name + "_yy";
225 else if (i==5) return name + "_yz";
226 else if (i==6) return name + "_zx";
227 else if (i==7) return name + "_zy";
228 else if (i==8) return name + "_zz";
229 #endif
230 else Util::Abort(INFO, "Invalid component");
231 return "";
232}
233
234
235template <>
236class Field<Set::Scalar> : public amrex::Vector<std::unique_ptr<amrex::MultiFab>>
237{
238public:
239 Field() {}
240 Field(int size) : amrex::Vector<std::unique_ptr<amrex::MultiFab>>(size) {}
241 void Define(int a_levs, const amrex::Vector<amrex::BoxArray> & a_grids, const amrex::Vector<amrex::DistributionMapping> & a_dmap, int a_ncomp, int a_nghost)
242 {
243 Util::Assert(INFO,TEST(a_levs == a_grids.size()));
244 Util::Assert(INFO,TEST(a_levs == a_dmap.size()));
245 this->resize(a_levs);
246 for (int lev = 0; lev < a_levs; lev++)
247 {
248 (*this)[lev].reset(new amrex::MultiFab(a_grids[lev],a_dmap[lev],a_ncomp,a_nghost));
249 }
250 }
251 void Define(int a_lev, const amrex::BoxArray & a_grid, const amrex::DistributionMapping & a_dmap, int a_ncomp, int a_nghost)
252 {
254 (*this)[a_lev].reset(new amrex::MultiFab(a_grid,a_dmap,a_ncomp,a_nghost));
255 }
257
258 void Copy(int /*a_lev*/, amrex::MultiFab &/*a_dst*/, int /*a_dstcomp*/, int /*a_nghost*/) const
259 {Util::Abort(INFO,"This should never get called");}
260 int NComp() const
261 {Util::Abort(INFO,"This should never be called");return -1;}
262
263 amrex::Array4<Set::Scalar> Patch (int lev, amrex::MFIter &mfi) const &
264 {
265 if (this->size()) return (*(*this)[lev]).array(mfi);
266 else return empty;
267 }
268
269 amrex::Array4<Set::Scalar> empty;
270};
271
272
273}
274
275namespace Util
276{
278Set::Scalar Gaussian(amrex::Real mean,amrex::Real std_deviation);
279}
280
281namespace Set
282{
283namespace Constant
284{
285// Pi
286static const Set::Scalar Pi = 3.14159265359;
287// Gas constant
288static const Set::Scalar Rg = 8.31446261815;
289}
290}
291
292#include "Set/Matrix3.H"
293#include "Set/Matrix4.H"
294
295
296#endif
#define TEST(x)
Definition Util.H:21
#define ALAMO_SINGLE_DEFINITION
Definition Util.H:25
#define INFO
Definition Util.H:20
amrex::Array4< Set::Scalar > empty
Definition Set.H:269
void Define(int a_lev, const amrex::BoxArray &a_grid, const amrex::DistributionMapping &a_dmap, int a_ncomp, int a_nghost)
Definition Set.H:251
void Copy(int, amrex::MultiFab &, int, int) const
Definition Set.H:258
amrex::Array4< Set::Scalar > Patch(int lev, amrex::MFIter &mfi) const &
Definition Set.H:263
void Define(int a_levs, const amrex::Vector< amrex::BoxArray > &a_grids, const amrex::Vector< amrex::DistributionMapping > &a_dmap, int a_ncomp, int a_nghost)
Definition Set.H:241
int NComp() const
Definition Set.H:260
void Add(int, amrex::MultiFab &, int, int) const
Definition Set.H:70
std::string Name(int) const
Definition Set.H:73
void Copy(int, amrex::MultiFab &, int, int) const
Definition Set.H:68
std::string name
Definition Set.H:74
amrex::Array4< T > Patch(int lev, amrex::MFIter &mfi) const &
Definition Set.H:76
void AddFrom(int, amrex::MultiFab &, int, int) const
Definition Set.H:71
int finest_level
Definition Set.H:67
void Define(int a_levs, const amrex::Vector< amrex::BoxArray > &a_grids, const amrex::Vector< amrex::DistributionMapping > &a_dmap, int a_ncomp, int a_nghost)
Definition Set.H:52
int NComp() const
Definition Set.H:72
Field(int size)
Definition Set.H:51
Field()
Definition Set.H:45
Field(int a_levs, const amrex::Vector< amrex::BoxArray > &a_grids, const amrex::Vector< amrex::DistributionMapping > &a_dmap, int a_ncomp, int a_nghost)
Definition Set.H:47
void Define(int a_lev, const amrex::BoxArray &a_grid, const amrex::DistributionMapping &a_dmap, int a_ncomp, int a_nghost)
Definition Set.H:62
void CopyFrom(int, amrex::MultiFab &, int, int) const
Definition Set.H:69
amrex::Array4< T > empty
Definition Set.H:83
static const Set::Scalar Rg
Definition Set.H:288
static const Set::Scalar Pi
Definition Set.H:286
A collection of data types and symmetry-reduced data structures.
Definition Base.H:18
amrex::Real Scalar
Definition Base.H:19
Hypercube
Definition Set.H:31
@ Cell
Definition Set.H:32
@ Edge
Definition Set.H:32
@ Node
Definition Set.H:32
@ Face
Definition Set.H:32
amrex::Array4< T > const & Patch
Definition Set.H:88
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition Base.H:20
A collection of utility routines.
Definition Set.cpp:8
void Abort(const char *msg)
Definition Util.cpp:170
AMREX_FORCE_INLINE void Assert(std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
Definition Util.H:70
Set::Scalar Random()
Definition Set.cpp:9
Set::Scalar Gaussian(amrex::Real mean, amrex::Real std_deviation)
Definition Set.cpp:13