REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_PC.H
Go to the documentation of this file.
1#ifndef REMORA_PC_H_
2#define REMORA_PC_H_
3
4#ifdef REMORA_USE_PARTICLES
5
6#include <string>
7#include <REMORA_Constants.H>
8#include <AMReX_Particles.H>
9
10struct REMORAParticlesIntIdxAoS
11{
12 enum {
13 k = 0,
14 ncomps
15 };
16};
17
18struct REMORAParticlesRealIdxAoS
19{
20 enum {
21 ncomps = 0
22 };
23};
24
25struct REMORAParticlesIntIdxSoA
26{
27 enum {
28 ncomps = 0
29 };
30};
31
32struct REMORAParticlesRealIdxSoA
33{
34 enum {
35 vx = 0,
36 vy,
37 vz,
38 mass,
39 ncomps
40 };
41};
42
43namespace REMORAParticleInitializations
44{
45 /* list of particle initializations */
46 const std::string init_box_uniform = "box";
47}
48
49namespace REMORAParticleNames
50{
51 const std::string tracers = "tracer_particles";
52 const std::string hydro = "hydro_particles";
53}
54
55struct REMORAParticlesAssignor
56{
57 template <typename P>
58 AMREX_GPU_HOST_DEVICE
59 amrex::IntVect operator() ( P const& p,
60 amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& plo,
61 amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& dxi,
62 const amrex::Box& domain ) const noexcept
63 {
64 amrex::IntVect iv(
65 AMREX_D_DECL( int(amrex::Math::floor((p.pos(0)-plo[0])*dxi[0])),
66 int(amrex::Math::floor((p.pos(1)-plo[1])*dxi[1])),
67 p.idata(REMORAParticlesIntIdxAoS::k) ) );
68 iv[0] += domain.smallEnd()[0];
69 iv[1] += domain.smallEnd()[1];
70 return iv;
71 }
72};
73
74template <typename P>
75AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
76void update_location_idata ( P& a_p,
77 amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& a_plo,
78 amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> const& a_dxi,
79 const amrex::Array4<amrex::Real const>& a_height_arr )
80{
81 amrex::IntVect iv( int(amrex::Math::floor((a_p.pos(0)-a_plo[0])*a_dxi[0])),
82 int(amrex::Math::floor((a_p.pos(1)-a_plo[1])*a_dxi[1])),
83 a_p.idata(REMORAParticlesIntIdxAoS::k) );
84
85 if (a_height_arr) {
86 amrex::Real lx = (a_p.pos(0)-a_plo[0])*a_dxi[0] - static_cast<amrex::Real>(iv[0]);
87 amrex::Real ly = (a_p.pos(1)-a_plo[1])*a_dxi[1] - static_cast<amrex::Real>(iv[1]);
88 auto zlo = a_height_arr(iv[0] ,iv[1] ,iv[2] ) * (one-lx) * (one-ly) +
89 a_height_arr(iv[0]+1,iv[1] ,iv[2] ) * lx * (one-ly) +
90 a_height_arr(iv[0] ,iv[1]+1,iv[2] ) * (one-lx) * ly +
91 a_height_arr(iv[0]+1,iv[1]+1,iv[2] ) * lx * ly;
92 auto zhi = a_height_arr(iv[0] ,iv[1] ,iv[2]+1) * (one-lx) * (one-ly) +
93 a_height_arr(iv[0]+1,iv[1] ,iv[2]+1) * lx * (one-ly) +
94 a_height_arr(iv[0] ,iv[1]+1,iv[2]+1) * (one-lx) * ly +
95 a_height_arr(iv[0]+1,iv[1]+1,iv[2]+1) * lx * ly;
96
97 if (a_p.pos(2) > zhi) {
98 a_p.idata(REMORAParticlesIntIdxAoS::k) += 1;
99 } else if (a_p.pos(2) <= zlo) {
100 a_p.idata(REMORAParticlesIntIdxAoS::k) -= 1;
101 }
102 }
103}
104
105class REMORAPC : public amrex::ParticleContainer< REMORAParticlesRealIdxAoS::ncomps, // AoS real attributes
106 REMORAParticlesIntIdxAoS::ncomps, // AoS integer attributes
107 REMORAParticlesRealIdxSoA::ncomps, // SoA real attributes
108 REMORAParticlesIntIdxSoA::ncomps, // SoA integer attributes
109 amrex::DefaultAllocator,
110 REMORAParticlesAssignor >
111{
112 public:
113
114 /*! Constructor */
115 REMORAPC ( amrex::ParGDBBase* a_gdb,
116 const std::string& a_name = "particles" )
117 : amrex::ParticleContainer< REMORAParticlesRealIdxAoS::ncomps, // AoS real attributes
118 REMORAParticlesIntIdxAoS::ncomps, // AoS integer attributes
119 REMORAParticlesRealIdxSoA::ncomps, // SoA real attributes
120 REMORAParticlesIntIdxSoA::ncomps, // SoA integer attributes
121 amrex::DefaultAllocator,
122 REMORAParticlesAssignor> (a_gdb)
123 {
124 BL_PROFILE("REMORAPCPC::REMORAPC()");
125 m_name = a_name;
126 readInputs();
127 }
128
129 /*! Constructor */
130 REMORAPC ( const amrex::Geometry& a_geom,
131 const amrex::DistributionMapping& a_dmap,
132 const amrex::BoxArray& a_ba,
133 const std::string& a_name = "particles" )
134 : amrex::ParticleContainer< REMORAParticlesRealIdxAoS::ncomps, // AoS real attributes
135 REMORAParticlesIntIdxAoS::ncomps, // AoS real attributes
136 REMORAParticlesRealIdxSoA::ncomps, // SoA real attributes
137 REMORAParticlesIntIdxSoA::ncomps, // SoA integer attributes
138 amrex::DefaultAllocator,
139 REMORAParticlesAssignor> ( a_geom, a_dmap, a_ba )
140 {
141 BL_PROFILE("REMORAPCPC::REMORAPC()");
142 m_name = a_name;
143 readInputs();
144 }
145
146 /*! Initialize particles in domain */
147 virtual void InitializeParticles (const std::unique_ptr<amrex::MultiFab>& a_ptr = nullptr);
148
149 /*! Evolve particles for one time step */
150 virtual void EvolveParticles ( int lev,
151 amrex::Real dt,
152 amrex::Vector<amrex::MultiFab const*>& a_flow_vel,
153 const amrex::Vector<std::unique_ptr<amrex::MultiFab>>& );
154
155 /*! Get real-type particle attribute names */
156 virtual amrex::Vector<std::string> varNames () const
157 {
158 BL_PROFILE("REMORAPCPC::varNames()");
159 return {AMREX_D_DECL("xvel","yvel","zvel"),"mass"};
160 }
161
162 /*! Get real-type particle attribute names */
163 virtual amrex::Vector<std::string> meshPlotVarNames () const
164 {
165 BL_PROFILE("REMORAPCPC::varNames()");
166 return {"mass_density"};
167 }
168
169 /*! Uses midpoint method to advance particles using flow velocity. */
170 virtual void AdvectWithFlow ( int lev,
171 amrex::Real dt,
172 amrex::Vector<amrex::MultiFab const*>&,
173 const std::unique_ptr<amrex::MultiFab>& );
174
175 /*! Compute mass density */
176 virtual void massDensity ( amrex::MultiFab&, const int&, const int& a_comp = 0) const;
177
178 /*! Compute mesh variable from particles */
179 virtual void computeMeshVar( const std::string& a_var_name,
180 amrex::MultiFab& a_mf,
181 const int a_lev) const
182 {
183 if (a_var_name == "mass_density") {
184 massDensity( a_mf, a_lev );
185 } else {
186 a_mf.setVal(zero);
187 }
188 }
189
190 /*! Specify if particles should advect with flow */
191 inline void setAdvectWithFlow (bool a_flag)
192 {
193 BL_PROFILE("REMORAPCPC::setAdvectWithFlow()");
194 m_advect_w_flow = a_flag;
195 }
196
197 // the following functions should ideally be private or protected, but need to be
198 // public due to CUDA extended lambda capture rules
199
200 /*! Default particle initialization */
201 void initializeParticlesUniformDistributionInBox (const std::unique_ptr<amrex::MultiFab>& a_ptr,
202 const amrex::RealBox& particle_box);
203
204 protected:
205
206 bool m_advect_w_flow; /*!< advect with flow velocity */
207
208 amrex::RealBox m_particle_box; /*!< box within which to place particles */
209
210 std::string m_name; /*!< name of this particle species */
211
212 std::string m_initialization_type; /*!< initial particle distribution type */
213 int m_ppc_init; /*!< initial number of particles per cell */
214
215 /*! read inputs from file */
216 virtual void readInputs ();
217
218 private:
219
220 bool place_randomly_in_cells; /*!< place particles at random positions? */
221};
222
223#endif
224#endif
constexpr amrex::Real one
constexpr amrex::Real zero