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 
28 namespace Set
29 {
30 
31 enum Hypercube {
32  Node = 0, Edge = 1, Face = 2, Cell = 3
33 };
34 
35 using HC = Hypercube;
36 
37 //class Field : public amrex::MultiFab
38 //{
39 
40 //};
41 template <class T>
42 class Field : public amrex::Vector<std::unique_ptr<amrex::FabArray<amrex::BaseFab<T>>>>
43 {
44 public:
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  {
49  Define(a_levs,a_grids,a_dmap,a_ncomp,a_nghost);
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  {
54  Util::Assert(INFO,TEST(a_levs == a_grids.size()));
55  Util::Assert(INFO,TEST(a_levs == a_dmap.size()));
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  {
64  Util::Assert(INFO,TEST(a_lev < this->size()));
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 
87 template <typename T>
88 using Patch = amrex::Array4<T> const&;
89 
90 
91 template<>
93 void 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 }
111 template<>
113 void 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 }
131 template<>
133 void 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 }
151 template<>
153 void 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 
172 template<> ALAMO_SINGLE_DEFINITION int Field<Set::Vector>::NComp() const {return AMREX_SPACEDIM;}
173 template<> 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 
188 template<>
190 void 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) {
202  dst(i,j,k,a_dstcomp + n) = src(i,j,k)(n/AMREX_SPACEDIM,n%AMREX_SPACEDIM);
203  });
204  }
205  }
206  }
207 }
208 template<>
209 ALAMO_SINGLE_DEFINITION int Field<Set::Matrix>::NComp() const {return AMREX_SPACEDIM*AMREX_SPACEDIM;}
210 template<> 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 
235 template <>
236 class Field<Set::Scalar> : public amrex::Vector<std::unique_ptr<amrex::MultiFab>>
237 {
238 public:
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  {
253  Util::Assert(INFO,TEST(a_lev < this->size()));
254  (*this)[a_lev].reset(new amrex::MultiFab(a_grid,a_dmap,a_ncomp,a_nghost));
255  }
256  int finest_level = 0;
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 
275 namespace Util
276 {
278 Set::Scalar Gaussian(amrex::Real mean,amrex::Real std_deviation);
279 }
280 
281 namespace Set
282 {
283 namespace Constant
284 {
285 // Pi
286 static const Set::Scalar Pi = 3.14159265359;
287 // Gas constant
288 static const Set::Scalar Rg = 8.31446261815;
289 }
290 }
291 
292 #include "Set/Matrix3.H"
293 #include "Set/Matrix4.H"
294 
295 
296 #endif
Set::Field::Field
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
Set::Field< Set::Scalar >::Field
Field()
Definition: Set.H:239
Util.H
Set::Field< Set::Scalar >::Define
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
TEST
#define TEST(x)
Definition: Util.H:21
Set::Field< Set::Scalar >::Copy
void Copy(int, amrex::MultiFab &, int, int) const
Definition: Set.H:258
ALAMO_SINGLE_DEFINITION
#define ALAMO_SINGLE_DEFINITION
Definition: Util.H:25
Set::Field::Field
Field()
Definition: Set.H:45
Set::Field< Set::Scalar >::NComp
int NComp() const
Definition: Set.H:260
Constant
Base.H
Set::Field::Copy
void Copy(int, amrex::MultiFab &, int, int) const
Definition: Set.H:68
Set::Hypercube
Hypercube
Definition: Set.H:31
Set::Constant::Rg
static const Set::Scalar Rg
Definition: Set.H:288
Set::Field::Patch
amrex::Array4< T > Patch(int lev, amrex::MFIter &mfi) const &
Definition: Set.H:76
Set::Vector
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition: Base.H:20
Set::Field::AddFrom
void AddFrom(int, amrex::MultiFab &, int, int) const
Definition: Set.H:71
Util::Gaussian
Set::Scalar Gaussian(amrex::Real mean, amrex::Real std_deviation)
Definition: Set.cpp:13
Util
A collection of utility routines.
Definition: Set.cpp:7
Set::Field::Name
std::string Name(int) const
Definition: Set.H:73
Util::Random
Set::Scalar Random()
Definition: Set.cpp:9
Set::Scalar
amrex::Real Scalar
Definition: Base.H:19
Set
A collection of data types and symmetry-reduced data structures.
Definition: Base.H:17
Matrix3.H
Set::Node
@ Node
Definition: Set.H:32
Set::Edge
@ Edge
Definition: Set.H:32
Set::Field< Set::Scalar >::Define
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
Set::Field::empty
amrex::Array4< T > empty
Definition: Set.H:83
Set::Face
@ Face
Definition: Set.H:32
Util::Assert
AMREX_FORCE_INLINE void Assert(std::string file, std::string func, int line, std::string smt, bool pass, Args const &... args)
Definition: Util.H:65
Set::Field::Field
Field(int size)
Definition: Set.H:51
Set::Field< Set::Scalar >::Field
Field(int size)
Definition: Set.H:240
Util::Abort
void Abort(const char *msg)
Definition: Util.cpp:165
Set::Constant::Pi
static const Set::Scalar Pi
Definition: Set.H:286
Set::Field::CopyFrom
void CopyFrom(int, amrex::MultiFab &, int, int) const
Definition: Set.H:69
Set::Patch
amrex::Array4< T > const & Patch
Definition: Set.H:88
Set::Field::Define
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
Set::Field::Define
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
Set::Field::Add
void Add(int, amrex::MultiFab &, int, int) const
Definition: Set.H:70
Set::Field::NComp
int NComp() const
Definition: Set.H:72
Set::Field::finest_level
int finest_level
Definition: Set.H:67
Set::Cell
@ Cell
Definition: Set.H:32
INFO
#define INFO
Definition: Util.H:20
Set::Field::name
std::string name
Definition: Set.H:74
Set::Field< Set::Scalar >::Patch
amrex::Array4< Set::Scalar > Patch(int lev, amrex::MFIter &mfi) const &
Definition: Set.H:263
Set::Field
Definition: Set.H:42
Set::Field< Set::Scalar >::empty
amrex::Array4< Set::Scalar > empty
Definition: Set.H:269