/home/runner/work/amr-wind/amr-wind/amr-wind/core/Field.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/core/Field.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
Field.H
Go to the documentation of this file.
1#ifndef FIELD_H
2#define FIELD_H
3
4#include <string>
5#include <memory>
6#include <unordered_map>
7#include <algorithm>
8
9#include "AMReX_MultiFab.H"
10#include "AMReX_BCRec.H"
11#include "AMReX_Gpu.H"
12
16
17namespace amr_wind {
18
19class Field;
20class FieldRepo;
21class FieldFillPatchOpsBase;
22class FieldBCIface;
23class SimTime;
24
29{
31 static constexpr int max_field_states = 5;
32
34 std::string basename,
35 const int ncomp,
36 const int ngrow,
37 const int nstates,
38 const FieldLoc floc);
39
41
43 bool bc_initialized();
44
46 void copy_bc_to_device() noexcept;
47
49 std::string m_basename;
50
53
55 amrex::IntVect m_ngrow;
56
59
62
65 amrex::GpuArray<BC, AMREX_SPACEDIM * 2> m_bc_type;
66 amrex::Vector<amrex::Vector<amrex::Real>> m_bc_values;
67 amrex::GpuArray<const amrex::Real*, AMREX_SPACEDIM * 2> m_bc_values_d;
68 amrex::Gpu::DeviceVector<amrex::Real> m_bc_values_dview;
69
70 amrex::Vector<amrex::BCRec> m_bcrec;
71 amrex::Gpu::DeviceVector<amrex::BCRec> m_bcrec_d;
73
75 amrex::Vector<Field*> m_states;
76
79
81 amrex::Vector<std::unique_ptr<FieldBCIface>> m_bc_func;
82
85};
86
115class Field
116{
117public:
118 friend class FieldRepo;
119
120 Field(const Field&) = delete;
121 Field& operator=(const Field&) = delete;
123
125 inline const std::string& name() const { return m_name; }
126
128 inline const std::string& base_name() const { return m_info->m_basename; }
129
131 inline unsigned id() const { return m_id; }
132
134 inline int num_comp() const { return m_info->m_ncomp; }
135
137 inline const amrex::IntVect& num_grow() const { return m_info->m_ngrow; }
138
140 inline int num_time_states() const { return m_info->m_nstates; }
141
143 inline int num_states() const
144 {
145 int ns = static_cast<int>(std::count_if(
146 m_info->m_states.begin(), m_info->m_states.end(),
147 [](const Field* ff) { return (ff != nullptr); }));
148 AMREX_ASSERT(num_time_states() <= ns);
149 return ns;
150 }
151
153 inline FieldLoc field_location() const { return m_info->m_floc; }
154
156 inline FieldState field_state() const { return m_state; }
157
159 inline FieldRepo& repo() const { return m_repo; }
160
162 inline bool fillpatch_on_regrid() const { return m_fillpatch_on_regrid; }
163
165 inline bool query_state(const FieldState fstate) const
166 {
167 const int fid = static_cast<int>(fstate);
168 return (m_info->m_states[fid] != nullptr);
169 }
170
171 const amrex::GpuArray<BC, AMREX_SPACEDIM * 2>& bc_type() const
172 {
173 return m_info->m_bc_type;
174 }
175
176 amrex::GpuArray<BC, AMREX_SPACEDIM * 2>& bc_type()
177 {
178 return m_info->m_bc_type;
179 }
180
182 amrex::Vector<amrex::Vector<amrex::Real>>& bc_values()
183 {
184 return m_info->m_bc_values;
185 }
186
188 amrex::Vector<amrex::BCRec>& bcrec() const { return m_info->m_bcrec; }
189
190 const amrex::GpuArray<const amrex::Real*, AMREX_SPACEDIM * 2>&
192 {
193 return m_info->m_bc_values_d;
194 }
195
196 const amrex::Gpu::DeviceVector<amrex::BCRec>& bcrec_device() const
197 {
198 return m_info->m_bcrec_d;
199 }
200
205 inline void copy_bc_to_device() { m_info->copy_bc_to_device(); }
206
208 inline bool bc_initialized() const { return m_info->m_bc_copied_to_device; }
209
211 inline bool has_fillpatch_op() const
212 {
213 return static_cast<bool>(m_info->m_fillpatch_op);
214 }
215
224 const SimTime& time,
225 const amrex::BCType::mathematicalBndryTypes bctype =
226 amrex::BCType::hoextrap) noexcept;
227
235 void to_uniform_space() noexcept;
236
244 void to_stretched_space() noexcept;
245
247 Field& state(const FieldState fstate);
248 const Field& state(const FieldState fstate) const;
249
251 amrex::MultiFab& operator()(int lev) noexcept;
252 const amrex::MultiFab& operator()(int lev) const noexcept;
253
255 amrex::Vector<amrex::MultiFab*> vec_ptrs() noexcept;
256
258 amrex::Vector<const amrex::MultiFab*> vec_const_ptrs() const noexcept;
259
261 void advance_states() noexcept;
262
264 void copy_state(FieldState to_state, FieldState from_state) noexcept;
265
267 Field& create_state(const FieldState fstate) noexcept;
268
269 // NOTE: We break naming rules for methods here to provide 1-1 mapping with
270 // the underlying MultiFab method names.
271
273 void setVal(amrex::Real value) noexcept;
274
283 void setVal(
284 amrex::Real value,
285 int start_comp,
286 int num_comp = 1,
287 int nghost = 0) noexcept;
288
291 void
292 setVal(const amrex::Vector<amrex::Real>& values, int nghost = 0) noexcept;
293
296 ViewField<Field> subview(const int scomp = 0, const int ncomp = 1)
297 {
298 return {*this, scomp, ncomp};
299 }
300
305 template <typename T, class... Args>
306 void register_fill_patch_op(Args&&... args)
307 {
308 m_info->m_fillpatch_op.reset(new T(*this, std::forward<Args>(args)...));
309 }
310
313 template <typename T, class... Args>
314 void register_custom_bc(Args&&... args)
315 {
316 m_info->m_bc_func.emplace_back(
317 new T(*this, std::forward<Args>(args)...));
318 }
319
320 void fillpatch(const amrex::Real time) noexcept;
321 void fillpatch(const amrex::Real time, amrex::IntVect ng) noexcept;
323 const amrex::Real time,
324 const amrex::IntVect ng,
325 amrex::Array<Field*, AMREX_SPACEDIM>& fields) const noexcept;
326
327 void fillphysbc(const amrex::Real time) noexcept;
328 void fillphysbc(const amrex::Real time, const amrex::IntVect ng) noexcept;
329
330 void apply_bc_funcs(const FieldState rho_state) noexcept;
331
332 void fillpatch(
333 const int lev,
334 const amrex::Real time,
335 amrex::MultiFab& mfab,
336 const amrex::IntVect& nghost) noexcept;
337
339 const int lev,
340 const amrex::Real time,
341 amrex::MultiFab& mfab,
342 const amrex::IntVect& nghost) noexcept;
343
344 void fillphysbc(
345 const int lev,
346 const amrex::Real time,
347 amrex::MultiFab& mfab,
348 const amrex::IntVect& nghost) noexcept;
349
350 void set_inflow(
351 const int lev,
352 const amrex::Real time,
353 amrex::MultiFab& mfab,
354 const amrex::IntVect& nghost) noexcept;
355
357 const int lev,
358 const amrex::Real time,
359 const amrex::Array<amrex::MultiFab*, AMREX_SPACEDIM> mfabs) noexcept;
360
361 inline void fillpatch(
362 const int lev,
363 const amrex::Real time,
364 amrex::MultiFab& mfab,
365 const int nghost) noexcept
366 {
367 fillpatch(lev, time, mfab, amrex::IntVect(nghost));
368 }
369
371 const int lev,
372 const amrex::Real time,
373 amrex::MultiFab& mfab,
374 const int nghost) noexcept
375 {
376 fillpatch_from_coarse(lev, time, mfab, amrex::IntVect(nghost));
377 }
378
379 inline void fillphysbc(
380 const int lev,
381 const amrex::Real time,
382 amrex::MultiFab& mfab,
383 const int nghost) noexcept
384 {
385 fillphysbc(lev, time, mfab, amrex::IntVect(nghost));
386 }
387
388 inline void set_inflow(
389 const int lev,
390 const amrex::Real time,
391 amrex::MultiFab& mfab,
392 const int nghost) noexcept
393 {
394 set_inflow(lev, time, mfab, amrex::IntVect(nghost));
395 }
396
397 inline bool& in_uniform_space() { return m_mesh_mapped; }
398 inline bool in_uniform_space() const { return m_mesh_mapped; }
399
401 inline bool has_inout_bndry() const { return m_inout_bndry; }
402
405
406protected:
407 Field(
409 std::string name,
410 std::shared_ptr<FieldInfo> finfo,
411 const unsigned fid,
413
416
418 const std::string m_name;
419
421 std::shared_ptr<FieldInfo> m_info;
422
424 const unsigned m_id;
425
428
432
434 bool m_mesh_mapped{false};
435
437 bool m_inout_bndry{false};
438};
439
440} // namespace amr_wind
441
442#endif /* FIELD_H */
Definition FieldBCOps.H:32
Definition FieldFillPatchOps.H:39
Definition Field.H:116
bool query_state(const FieldState fstate) const
Return true if the requested state exists for this field.
Definition Field.H:165
void copy_state(FieldState to_state, FieldState from_state) noexcept
Copy a user-specified "from_state" to "to_state".
Definition Field.cpp:321
bool m_fillpatch_on_regrid
Definition Field.H:431
void set_inflow_sibling_fields(const int lev, const amrex::Real time, const amrex::Array< amrex::MultiFab *, AMREX_SPACEDIM > mfabs) noexcept
Definition Field.cpp:290
void set_inout_bndry()
Set the inout_bndry flag.
Definition Field.H:404
void set_inflow(const int lev, const amrex::Real time, amrex::MultiFab &mfab, const int nghost) noexcept
Definition Field.H:388
void to_stretched_space() noexcept
Definition Field.cpp:421
amrex::Vector< const amrex::MultiFab * > vec_const_ptrs() const noexcept
Return vector of const MultiFab* for all levels.
Definition Field.cpp:153
bool has_inout_bndry() const
Check if any of the boundaries is a mass-inflow-outflow.
Definition Field.H:401
const amrex::Gpu::DeviceVector< amrex::BCRec > & bcrec_device() const
Definition Field.H:196
ViewField< Field > subview(const int scomp=0, const int ncomp=1)
Definition Field.H:296
const amrex::IntVect & num_grow() const
Ghost cells.
Definition Field.H:137
int num_time_states() const
Number of exact time states available for this field.
Definition Field.H:140
FieldLoc field_location() const
Cell, node, or face centered field.
Definition Field.H:153
unsigned id() const
Unique integer identifier for this field.
Definition Field.H:131
const std::string & base_name() const
Base name (without state information)
Definition Field.H:128
void fillpatch_from_coarse(const int lev, const amrex::Real time, amrex::MultiFab &mfab, const int nghost) noexcept
Definition Field.H:370
const amrex::GpuArray< const amrex::Real *, AMREX_SPACEDIM *2 > & bc_values_device() const
Definition Field.H:191
Field & create_state(const FieldState fstate) noexcept
Create a new time state after the field has been created.
Definition Field.cpp:333
std::shared_ptr< FieldInfo > m_info
Common data for all field states.
Definition Field.H:421
bool & fillpatch_on_regrid()
Definition Field.H:161
FieldRepo & repo() const
FieldRepo instance that manages this field.
Definition Field.H:159
bool & in_uniform_space()
Definition Field.H:397
void set_default_fillpatch_bc(const SimTime &time, const amrex::BCType::mathematicalBndryTypes bctype=amrex::BCType::hoextrap) noexcept
Definition Field.cpp:377
const amrex::GpuArray< BC, AMREX_SPACEDIM *2 > & bc_type() const
Definition Field.H:171
int num_states() const
Number of states available for this field.
Definition Field.H:143
Field(const Field &)=delete
void fillphysbc(const amrex::Real time) noexcept
Definition Field.cpp:264
void register_fill_patch_op(Args &&... args)
Definition Field.H:306
const FieldState m_state
State for this field.
Definition Field.H:427
FieldState field_state() const
State of this field instance.
Definition Field.H:156
bool fillpatch_on_regrid() const
Definition Field.H:162
amrex::GpuArray< BC, AMREX_SPACEDIM *2 > & bc_type()
Definition Field.H:176
void register_custom_bc(Args &&... args)
Definition Field.H:314
void copy_bc_to_device()
Definition Field.H:205
void fillpatch_sibling_fields(const amrex::Real time, const amrex::IntVect ng, amrex::Array< Field *, AMREX_SPACEDIM > &fields) const noexcept
Definition Field.cpp:211
amrex::Vector< amrex::Vector< amrex::Real > > & bc_values()
Return reference to the host view of BC values array.
Definition Field.H:182
const std::string & name() const
Name of this field (including state information)
Definition Field.H:125
void fillphysbc(const int lev, const amrex::Real time, amrex::MultiFab &mfab, const int nghost) noexcept
Definition Field.H:379
const unsigned m_id
Unique ID for this field.
Definition Field.H:424
bool bc_initialized() const
Return flag indicating whether BCs have been initialized.
Definition Field.H:208
void fillpatch(const int lev, const amrex::Real time, amrex::MultiFab &mfab, const int nghost) noexcept
Definition Field.H:361
int num_comp() const
Number of components for this field.
Definition Field.H:134
bool in_uniform_space() const
Definition Field.H:398
amrex::Vector< amrex::MultiFab * > vec_ptrs() noexcept
Return a vector of MultiFab pointers for all levels.
Definition Field.cpp:142
void setVal(amrex::Real value) noexcept
Set the field to a constant value at all levels.
Definition Field.cpp:343
bool m_inout_bndry
Flag to indicate whether any of the boundaries is mass-inflow-outflow.
Definition Field.H:437
bool m_mesh_mapped
Flag to track mesh mapping (to uniform space) of field.
Definition Field.H:434
FieldRepo & m_repo
Reference to the FieldRepository instance.
Definition Field.H:415
Field & state(const FieldState fstate)
Return field at a different time state.
Definition Field.cpp:114
void set_inflow(const int lev, const amrex::Real time, amrex::MultiFab &mfab, const amrex::IntVect &nghost) noexcept
Definition Field.cpp:277
const std::string m_name
Name of the field.
Definition Field.H:418
amrex::Vector< amrex::BCRec > & bcrec() const
Return reference to host view of BCRec array.
Definition Field.H:188
void fillpatch(const amrex::Real time) noexcept
Definition Field.cpp:206
void fillpatch_from_coarse(const int lev, const amrex::Real time, amrex::MultiFab &mfab, const amrex::IntVect &nghost) noexcept
Definition Field.cpp:179
void apply_bc_funcs(const FieldState rho_state) noexcept
Definition Field.cpp:269
Field & operator=(const Field &)=delete
void to_uniform_space() noexcept
Definition Field.cpp:391
void advance_states() noexcept
Advance timestep for fields with multiple states.
Definition Field.cpp:302
bool has_fillpatch_op() const
Return a flag indicating whether a fillpatch Op has been registered.
Definition Field.H:211
Definition FieldRepo.H:86
Definition SimTime.H:30
Definition ViewField.H:24
FieldLoc
Definition FieldDescTypes.H:27
FieldState
Definition FieldDescTypes.H:14
BC
Definition incflo_enums.H:6
Definition BCInterface.cpp:7
Definition console_io.cpp:25
Definition Field.H:29
FieldLoc m_floc
Cell, node, face centered field type.
Definition Field.H:61
void copy_bc_to_device() noexcept
Copy the BC information to device data structures.
Definition Field.cpp:62
amrex::IntVect m_ngrow
Ghost cells.
Definition Field.H:55
FieldInfo(std::string basename, const int ncomp, const int ngrow, const int nstates, const FieldLoc floc)
Definition Field.cpp:12
amrex::Vector< amrex::Vector< amrex::Real > > m_bc_values
Definition Field.H:66
std::string m_basename
Field name without state information.
Definition Field.H:49
int m_nstates
Number of states available for this field.
Definition Field.H:58
std::unique_ptr< FieldFillPatchOpsBase > m_fillpatch_op
Function that handles filling patch and physics BC data for this field.
Definition Field.H:78
bool m_bc_copied_to_device
Flag indicating whether BCs have been initialized and copied to device.
Definition Field.H:84
amrex::GpuArray< BC, AMREX_SPACEDIM *2 > m_bc_type
Definition Field.H:65
amrex::GpuArray< const amrex::Real *, AMREX_SPACEDIM *2 > m_bc_values_d
Definition Field.H:67
bool bc_initialized()
Check indicating whether BCs are being set to sane values.
Definition Field.cpp:39
amrex::Vector< Field * > m_states
Vector holding references to available states for this field.
Definition Field.H:75
static constexpr int max_field_states
Maximum number of states allowed for a field.
Definition Field.H:31
amrex::Vector< amrex::BCRec > m_bcrec
Definition Field.H:70
amrex::Vector< std::unique_ptr< FieldBCIface > > m_bc_func
Custom boundary condition actions for this field.
Definition Field.H:81
amrex::Gpu::DeviceVector< amrex::Real > m_bc_values_dview
Definition Field.H:68
int m_ncomp
Number of components (Scalar = 1, Vector = 2, etc.)
Definition Field.H:52
amrex::Gpu::DeviceVector< amrex::BCRec > m_bcrec_d
Definition Field.H:71