OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
AmrBoxLib.h
Go to the documentation of this file.
1 //
2 // Class AmrBoxLib
3 // Concrete AMR object. It is based on the AMReX library
4 // (cf. https://amrex-codes.github.io/ or https://ccse.lbl.gov/AMReX/).
5 // AMReX is the successor of BoxLib. This class represents the interface
6 // to AMReX and the AMR framework in OPAL. It implements the functions of
7 // the AmrObject class.
8 //
9 // Copyright (c) 2016 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
10 // All rights reserved
11 //
12 // Implemented as part of the PhD thesis
13 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
14 //
15 // This file is part of OPAL.
16 //
17 // OPAL is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
24 //
25 #ifndef AMR_BOXLIB_H
26 #define AMR_BOXLIB_H
27 
28 #include "Amr/AmrObject.h"
29 
30 class AmrPartBunch;
31 
32 // AMReX headers
33 #include <AMReX_AmrMesh.H>
34 #include <AMReX.H>
35 
36 class AmrBoxLib : public AmrObject,
37  public amrex::AmrMesh
38 {
39 
40 public:
54 
55  typedef amrex::FArrayBox FArrayBox_t;
56  typedef amrex::Box Box_t;
57  typedef amrex::TagBox TagBox_t;
58  typedef amrex::TagBoxArray TagBoxArray_t;
59  typedef amrex::MFIter MFIter_t;
60 
61 
65  enum Strategy {
66  RANK_ZERO = 0, // all grids to processor zero
67  PFC = 1,
68  RANDOM = 2,
69  KNAPSACK = 3
70  };
71 
72 public:
73 
81  AmrBoxLib(const AmrDomain_t& domain,
82  const AmrIntArray_t& nGridPts,
83  int maxLevel,
84  AmrPartBunch* bunch_p);
85 
93  static std::unique_ptr<AmrBoxLib> create(const AmrInfo& info,
94  AmrPartBunch* bunch_p);
95 
99  void regrid(double time);
100 
101  void getGridStatistics(std::map<int, long>& gridPtsPerCore,
102  std::vector<int>& gridsPerLevel) const;
103 
107  void initFineLevels();
108 
110 
111  double getRho(int x, int y, int z);
112 
113  void computeSelfFields();
114 
115  void computeSelfFields(int bin);
116 
117  void computeSelfFields_cycl(double gamma);
118 
119  void computeSelfFields_cycl(int bin);
120 
121  void updateMesh();
122 
127  const Vector_t& getMeshScaling() const;
128 
130 
131  const int& maxLevel() const;
132  const int& finestLevel() const;
133 
137  double getT() const;
138 
139  void redistributeGrids(int how);
140 
141 
142 protected:
143  /*
144  * AmrMesh functions
145  */
146 
155  void RemakeLevel (int lev, AmrReal_t time,
156  const AmrGrid_t& new_grids, const AmrProcMap_t& new_dmap);
157 
165  void MakeNewLevel (int lev, AmrReal_t time,
166  const AmrGrid_t& new_grids, const AmrProcMap_t& new_dmap);
167 
172  void ClearLevel(int lev);
173 
177  virtual void ErrorEst(int lev, TagBoxArray_t& tags,
178  AmrReal_t time, int ngrow) override;
179 
180 
193  void MakeNewLevelFromScratch (int lev, AmrReal_t time,
194  const AmrGrid_t& ba,
195  const AmrProcMap_t& dm);
196 
208  void MakeNewLevelFromCoarse (int lev, AmrReal_t time,
209  const AmrGrid_t& ba,
210  const AmrProcMap_t& dm);
211 
212 private:
219  void doRegrid_m(int lbase, double time);
220 
221 
227  void preRegrid_m();
228 
234  void postRegrid_m(int old_finest);
235 
236 
237  double solvePoisson_m();
238 
239 
240  /* ATTENTION
241  * The tagging routines assume the particles to be in the
242  * AMR domain, i.e. [-1, 1]^3
243  */
244 
254  void tagForChargeDensity_m(int lev, TagBoxArray_t& tags,
255  AmrReal_t time, int ngrow);
256 
267  void tagForPotentialStrength_m(int lev, TagBoxArray_t& tags,
268  AmrReal_t time, int ngrow);
269 
280  void tagForEfield_m(int lev, TagBoxArray_t& tags,
281  AmrReal_t time, int ngrow);
282 
294  void tagForMomenta_m(int lev, TagBoxArray_t& tags,
295  AmrReal_t time, int ngrow);
296 
306  void tagForMaxNumParticles_m(int lev, TagBoxArray_t& tags,
307  AmrReal_t time, int ngrow);
308 
318  void tagForMinNumParticles_m(int lev, TagBoxArray_t& tags,
319  AmrReal_t time, int ngrow);
320 
327  void initBaseLevel_m(const AmrIntArray_t& nGridPts);
328 
338  static void initParmParse_m(const AmrInfo& info, AmrLayout_t* layout_p);
339 
345  void fillPhysbc_m(AmrField_t& mf, int lev = 0);
346 
347 
348 // void gradient(int lev) {
349 //
350 // phi_m[lev]->FillBoundary(geom[lev].periodicity());
351 //
352 // for (MFIter_t mfi(*phi_m[lev], true); mfi.isValid(); ++mfi) {
353 // const Box_t& tilebx = mfi.tilebox();
354 // FArrayBox_t& fab = (*phi_m[lev])[mfi];
355 // FArrayBox_t& efab = (*efield_m[lev])[mfi];
356 //
357 // const int* tlo = tilebx.loVect();
358 // const int* thi = tilebx.hiVect();
359 //
360 // for (int i = tlo[0]; i <= thi[0]; ++i) {
361 // for (int j = tlo[1]; j <= thi[1]; ++j) {
362 // for (int k = tlo[2]; k <= thi[2]; ++k) {
363 //
364 // amrex::IntVect iv(D_DECL(i,j,k));
365 //
366 // // x-field
367 // amrex::IntVect liv(D_DECL(i-1,j,k));
368 // amrex::IntVect riv(D_DECL(i+1,j,k));
369 // efab(iv, 0) = 0.5 * (fab(liv) - fab(riv));
370 //
371 // // y-field
372 // liv = amrex::IntVect(D_DECL(i,j-1,k));
373 // riv = amrex::IntVect(D_DECL(i,j+1,k));
374 // efab(iv, 1) = 0.5 * (fab(liv) - fab(riv));
375 //
376 // // z-field
377 // liv = amrex::IntVect(D_DECL(i,j-1,k));
378 // riv = amrex::IntVect(D_DECL(i,j+1,k));
379 // efab(iv, 2) = 0.5 * (fab(liv) - fab(riv));
380 // }
381 // }
382 // }
383 // }
384 // }
385 
386 private:
389 
390  // the layout of the bunch
392 
395 
398 
401 
404 
405  // only used in case of potential and efield tagging
406  std::vector<bool> isFirstTagging_m;
407 
409 };
410 
411 #endif
amr::AmrField_t AmrField_t
Definition: PBunchDefs.h:34
amrex::Vector< AmrVectorField_t > AmrVectorFieldContainer_t
Definition: AmrDefs.h:42
amrex::DistributionMapping AmrProcMap_t
Definition: AmrDefs.h:38
amrex::Geometry AmrGeometry_t
Definition: AmrDefs.h:39
amrex::RealBox AmrDomain_t
Definition: AmrDefs.h:46
amrex::Vector< AmrGeometry_t > AmrGeomContainer_t
Definition: AmrDefs.h:43
amrex::IntVect AmrIntVect_t
Definition: AmrDefs.h:48
amrex::Real AmrReal_t
Definition: AmrDefs.h:51
amrex::MultiFab AmrField_t
Definition: AmrDefs.h:34
amrex::Vector< std::unique_ptr< AmrField_t > > AmrScalarFieldContainer_t
Definition: AmrDefs.h:41
amrex::Vector< int > AmrIntArray_t
Definition: AmrDefs.h:47
amrex::BoxArray AmrGrid_t
Definition: AmrDefs.h:40
amrex::Vector< AmrGrid_t > AmrGridContainer_t
Definition: AmrDefs.h:44
amrex::Vector< AmrProcMap_t > AmrProcMapContainer_t
Definition: AmrDefs.h:45
bool info
Info flag.
Definition: Options.cpp:28
amr::AmrGeometry_t AmrGeometry_t
Definition: AmrBoxLib.h:52
amr::AmrProcMap_t AmrProcMap_t
Definition: AmrBoxLib.h:51
Vector_t meshScaling_m
in particle rest frame, the longitudinal length enlarged
Definition: AmrBoxLib.h:403
const Vector_t & getMeshScaling() const
Definition: AmrBoxLib.cpp:496
void redistributeGrids(int how)
Definition: AmrBoxLib.cpp:528
void MakeNewLevelFromScratch(int lev, AmrReal_t time, const AmrGrid_t &ba, const AmrProcMap_t &dm)
Definition: AmrBoxLib.cpp:703
virtual void ErrorEst(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow) override
Definition: AmrBoxLib.cpp:670
const int & maxLevel() const
Definition: AmrBoxLib.cpp:513
void preRegrid_m()
Definition: AmrBoxLib.cpp:755
AmrBoxLib(const AmrDomain_t &domain, const AmrIntArray_t &nGridPts, int maxLevel, AmrPartBunch *bunch_p)
Definition: AmrBoxLib.cpp:44
amrex::Box Box_t
Definition: AmrBoxLib.h:56
AmrScalarFieldContainer_t rho_m
charge density on the grid for all levels
Definition: AmrBoxLib.h:394
void tagForPotentialStrength_m(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow)
Definition: AmrBoxLib.cpp:912
VectorPair_t getEExtrema()
Definition: AmrBoxLib.cpp:196
void getGridStatistics(std::map< int, long > &gridPtsPerCore, std::vector< int > &gridsPerLevel) const
Definition: AmrBoxLib.cpp:135
void tagForMaxNumParticles_m(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow)
Definition: AmrBoxLib.cpp:1090
amrex::MFIter MFIter_t
Definition: AmrBoxLib.h:59
void postRegrid_m(int old_finest)
Definition: AmrBoxLib.cpp:771
amr::AmrVectorFieldContainer_t AmrVectorFieldContainer_t
Definition: AmrBoxLib.h:43
void RemakeLevel(int lev, AmrReal_t time, const AmrGrid_t &new_grids, const AmrProcMap_t &new_dmap)
Definition: AmrBoxLib.cpp:589
AmrScalarFieldContainer_t phi_m
scalar potential on the grid for all levels
Definition: AmrBoxLib.h:397
amr::AmrField_t AmrField_t
Definition: AmrBoxLib.h:41
void computeSelfFields()
Definition: AmrBoxLib.cpp:226
amr::AmrIntArray_t AmrIntArray_t
Definition: AmrBoxLib.h:48
void updateMesh()
Definition: AmrBoxLib.cpp:484
std::vector< bool > isFirstTagging_m
Definition: AmrBoxLib.h:406
amr::AmrReal_t AmrReal_t
Definition: AmrBoxLib.h:49
void computeSelfFields_cycl(double gamma)
Definition: AmrBoxLib.cpp:238
amrex::FArrayBox FArrayBox_t
Definition: AmrBoxLib.h:55
double solvePoisson_m()
Definition: AmrBoxLib.cpp:810
double getT() const
Definition: AmrBoxLib.cpp:523
amr::AmrProcMapContainer_t AmrProcMapContainer_t
Definition: AmrBoxLib.h:46
AmrLayout_t * layout_mp
Definition: AmrBoxLib.h:391
amr::AmrGrid_t AmrGrid_t
Definition: AmrBoxLib.h:50
AmrPartBunch * bunch_mp
bunch used for tagging strategies
Definition: AmrBoxLib.h:388
amr::AmrScalarFieldContainer_t AmrScalarFieldContainer_t
Definition: AmrBoxLib.h:42
void MakeNewLevel(int lev, AmrReal_t time, const AmrGrid_t &new_grids, const AmrProcMap_t &new_dmap)
Definition: AmrBoxLib.cpp:622
void tagForEfield_m(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow)
Definition: AmrBoxLib.cpp:966
void tagForMinNumParticles_m(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow)
Definition: AmrBoxLib.cpp:1140
amrex::TagBoxArray TagBoxArray_t
Definition: AmrBoxLib.h:58
void initFineLevels()
Definition: AmrBoxLib.cpp:162
void regrid(double time)
Definition: AmrBoxLib.cpp:104
void initBaseLevel_m(const AmrIntArray_t &nGridPts)
Definition: AmrBoxLib.cpp:1192
void MakeNewLevelFromCoarse(int lev, AmrReal_t time, const AmrGrid_t &ba, const AmrProcMap_t &dm)
Definition: AmrBoxLib.cpp:711
amr::AmrGeomContainer_t AmrGeomContainer_t
Definition: AmrBoxLib.h:44
const int & finestLevel() const
Definition: AmrBoxLib.cpp:518
void doRegrid_m(int lbase, double time)
Definition: AmrBoxLib.cpp:719
void tagForMomenta_m(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow)
Definition: AmrBoxLib.cpp:1031
@ KNAPSACK
Definition: AmrBoxLib.h:69
@ RANK_ZERO
Definition: AmrBoxLib.h:66
void fillPhysbc_m(AmrField_t &mf, int lev=0)
Definition: AmrBoxLib.cpp:1519
amrex::TagBox TagBox_t
Definition: AmrBoxLib.h:57
static std::unique_ptr< AmrBoxLib > create(const AmrInfo &info, AmrPartBunch *bunch_p)
Definition: AmrBoxLib.cpp:71
void tagForChargeDensity_m(int lev, TagBoxArray_t &tags, AmrReal_t time, int ngrow)
Definition: AmrBoxLib.cpp:859
amr::AmrDomain_t AmrDomain_t
Definition: AmrBoxLib.h:47
AmrVectorFieldContainer_t efield_m
vector field on the grid for all levels
Definition: AmrBoxLib.h:400
static void initParmParse_m(const AmrInfo &info, AmrLayout_t *layout_p)
Definition: AmrBoxLib.cpp:1218
void ClearLevel(int lev)
Definition: AmrBoxLib.cpp:657
Vektor< int, 3 > getBaseLevelGridPoints() const
Definition: AmrBoxLib.cpp:501
bool isPoissonSolved_m
Definition: AmrBoxLib.h:408
amr::AmrGridContainer_t AmrGridContainer_t
Definition: AmrBoxLib.h:45
amr::AmrIntVect_t AmrIntVect_t
Definition: AmrBoxLib.h:53
double getRho(int x, int y, int z)
Definition: AmrBoxLib.cpp:219
std::pair< Vector_t, Vector_t > VectorPair_t
Definition: AmrObject.h:35