OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
AmrPartBunch.cpp
Go to the documentation of this file.
1 #include "AmrPartBunch.h"
2 
4 
6  : PartBunchBase<double, 3>(new AmrPartBunch::pbase_t(new AmrLayout_t()), ref)
7  , amrobj_mp(nullptr)
8  , amrpbase_mp(dynamic_cast<AmrPartBunch::pbase_t*>(pbase.get()))
9  , fieldlayout_m(nullptr)
10 {
12 }
13 
15  : PartBunchBase<double, 3>(new AmrPartBunch::pbase_t(new AmrLayout_t(&pbase_p->getAmrLayout())), ref)
16  , amrobj_mp(nullptr)
17  , amrpbase_mp(dynamic_cast<AmrPartBunch::pbase_t*>(pbase.get()))
18  , fieldlayout_m(nullptr)
19 {
21 }
22 
24 
25 }
26 
27 
29  return amrpbase_mp;
30 }
31 
32 
34  return amrpbase_mp;
35 }
36 
37 
39 // Layout_t* layout = static_cast<Layout_t*>(&getLayout());
40 }
41 
42 
44 
45  if ( amrobj_mp && !amrobj_mp->isRefined() ) {
46  /* In the first call to this function we
47  * intialize all fine levels
48  */
50 
51  } else {
52  bool isForbidTransform = amrpbase_mp->isForbidTransform();
53 
54  if ( !isForbidTransform ) {
55  /*
56  * regrid in boosted frame
57  */
58  this->updateLorentzFactor();
59  // Lorentz transform + mapping to [-1,1]
62  }
63 
64  this->update();
65 
66  if ( !isForbidTransform ) {
68  // map particles back + undo Lorentz transform
70  }
71  }
72 }
73 
74 
76  const double& scalefactor = amrpbase_mp->getScalingFactor();
77  return hr_m * scalefactor;
78 }
79 
80 
82  // set dh_m = dh
84 
85  // update base geometry with new mesh enlargement
86  AmrLayout_t* layout_p = &amrpbase_mp->getAmrLayout();
87  layout_p->setBoundingBox(dh);
88 
89  // if amrobj_mp != nullptr --> we need to regrid
90  this->do_binaryRepart();
91 }
92 
93 
95  return amrobj_mp->getEExtrema();
96 }
97 
98 double AmrPartBunch::getRho(int x, int y, int z) {
99  /* This function is called in
100  * H5PartWrapperForPC::writeStepData(PartBunchBase<double, 3>* bunch)
101  * and
102  * H5PartWrapperForPT::writeStepData(PartBunchBase<double, 3>* bunch)
103  * in case of Options::rhoDump = true.
104  *
105  * Currently, we do not support writing multilevel grid data that's why
106  * we average the values down to the coarsest level.
107  */
108  return amrobj_mp->getRho(x, y, z);
109 }
110 
111 
113  //TODO Implement
114  throw OpalException("AmrPartBunch::getFieldLayout() ", "Not yet Implemented.");
115  return *fieldlayout_m;
116 }
117 
118 
121  //if(!R.isDirty() && stateOfLastBoundP_ == unit_state_) return;
122  if ( !(R.isDirty() || ID.isDirty() ) && stateOfLastBoundP_ == unit_state_) return; //-DW
123 
125 
126  if ( amrobj_mp ) {
127  /* we do an explicit domain mapping of the particles and then
128  * forbid it during the regrid process, this way it's only
129  * executed ones --> saves computation
130  */
131  bool isForbidTransform = amrpbase_mp->isForbidTransform();
132 
133  if ( !isForbidTransform ) {
134  this->updateLorentzFactor();
135  // Lorentz transform + mapping to [-1,1]
138  }
139 
140  this->update();
141 
142  if ( !isForbidTransform ) {
144  // map particles back + undo Lorentz transform
145  amrpbase_mp->domainMapping(true);
146  }
147 
148  } else {
149  // At this point an amrobj_mp needs already be set
150  throw GeneralClassicException("AmrPartBunch::boundp() ",
151  "AmrObject pointer is not set.");
152  }
153 
154  R.resetDirtyFlag();
155 
157 }
158 
159 
162 
163  if ( !fs_m->hasValidSolver() )
164  throw OpalException("AmrPartBunch::computeSelfFields() ",
165  "No field solver.");
166 
168 
170 }
171 
172 
177 }
178 
179 
184 }
185 
186 
189 
190  /* make sure it is refined in multi-bunch case
191  */
192  if ( !amrobj_mp->isRefined() ) {
194  }
195 
197 
199 }
200 
201 
202 void AmrPartBunch::setAmrDomainRatio(const std::vector<double>& ratio) {
203  AmrLayout_t* layout_p = &amrpbase_mp->getAmrLayout();
204  layout_p->setDomainRatio(ratio);
205 }
206 
208  int nLevel = amrobj_mp->maxLevel() + 1;
209 
210  std::unique_ptr<size_t[]> partPerLevel( new size_t[nLevel] );
211  globalPartPerLevel_m.reset( new size_t[nLevel] );
212 
213  for (int i = 0; i < nLevel; ++i)
214  partPerLevel[i] = globalPartPerLevel_m[i] = 0.0;
215 
216  // do not modify LocalNumPerLevel in here!!!
217  auto& LocalNumPerLevel = amrpbase_mp->getLocalNumPerLevel();
218 
219  for (size_t i = 0; i < LocalNumPerLevel.size(); ++i)
220  partPerLevel[i] = LocalNumPerLevel[i];
221 
222  reduce(*partPerLevel.get(),
223  *globalPartPerLevel_m.get(),
224  nLevel, std::plus<size_t>());
225 }
226 
227 
228 const size_t& AmrPartBunch::getLevelStatistics(int l) const {
229  return globalPartPerLevel_m[l];
230 }
231 
232 
234  double gamma = this->get_gamma();
235 
236  if ( this->weHaveBins() ) {
237  gamma = this->getBinGamma(bin);
238  }
239 
240 
241  /* At the beginning of simulation the Lorentz factor
242  * is not yet set since PartBunchBase::calcBeamParameters
243  * is not yet called. Therefore, we do this ugly workaround.
244  */
245  bool init = true;
246  if ( gamma >= 1.0 ) {
247  init = false;
248  }
249 
250  if ( init ) {
251  gamma = 1.0;
252  }
253 
254  updateLorentzFactor(gamma);
255 }
256 
257 
259  // keep all 1.0, except longitudinal direction
260  Vector_t lorentzFactor(1.0, 1.0, 1.0);
261 
262  if (OpalData::getInstance()->isInOPALCyclMode()) {
263  lorentzFactor[1] = gamma;
264  } else {
265  lorentzFactor[2] = gamma;
266  }
267 
268  amrpbase_mp->setLorentzFactor(lorentzFactor);
269 }
270 
271 
273 
274 }
275 
278 }
279 
280 
281 void AmrPartBunch::updateFields(const Vector_t& hr, const Vector_t& origin) {
282  //TODO regrid; called in boundp()
283 // amrobj_mp->updateMesh();
284 }
IpplTimings::TimerRef selfFieldTimer_m
timer for selfField calculation
PLayout & getAmrLayout()
void gatherLevelStatistics()
double getRho(int x, int y, int z)
const double & getScalingFactor() const
Vector_t get_hr() const
void updateFields(const Vector_t &hr, const Vector_t &origin)
const size_t & getLevelStatistics(int l) const
virtual void set_meshEnlargement(double dh)
The base class for all OPAL exceptions.
Definition: OpalException.h:28
void updateFieldContainers_m()
Particle reference data.
Definition: PartData.h:38
AmrPartBunch(const PartData *ref)
Definition: AmrPartBunch.cpp:5
virtual void computeSelfFields()=0
const ParticleLevelCounter_t & getLocalNumPerLevel() const
virtual double getRho(int x, int y, int z)=0
bool isForbidTransform() const
void computeSelfFields()
pbase_t * amrpbase_mp
Definition: AmrPartBunch.h:130
void updateDomainLength(Vektor< int, 3 > &grid)
const bool & isRefined() const
Definition: AmrObject.cpp:74
FieldLayout_t & getFieldLayout()
pbase_t * getAmrParticleBase()
bool reduce(Communicate &, InputIterator, InputIterator, OutputIterator, const ReduceOp &, bool *IncludeVal=0)
Definition: GlobalComm.hpp:55
static OpalData * getInstance()
Definition: OpalData.cpp:209
std::pair< Vector_t, Vector_t > VectorPair_t
Definition: PartBunchBase.h:37
bool hasValidSolver()
Class: DataSink.
Definition: OpalData.h:29
IpplTimings::TimerRef boundpTimer_m
void setDomainRatio(const std::vector< double > &ratio)
void setAmrDomainRatio(const std::vector< double > &ratio)
void computeSelfFields_cycl(double gamma)
void updateLorentzFactor(int bin=0)
virtual void computeSelfFields_cycl(double gamma)=0
static void startTimer(TimerRef t)
Definition: IpplTimings.h:187
void setForbidTransform(bool forbidTransform)
void setLorentzFactor(const Vector_t &lorentzFactor)
FieldSolver * fs_m
stores the used field solver
void do_binaryRepart()
FieldLayout_t * fieldlayout_m
Definition: AmrPartBunch.h:135
const double & domainMapping(bool inverse=false)
Vector_t hr_m
meshspacing of cartesian mesh
virtual Vektor< int, 3 > getBaseLevelGridPoints() const =0
double getBinGamma(int bin)
Get gamma of one bin.
VectorPair_t getEExtrema()
calculates back the max/min of the efield on the grid
void set_meshEnlargement(double dh)
void setBoundingBox(double dh)
virtual const int & maxLevel() const =0
AmrObject * amrobj_mp
Definition: AmrPartBunch.h:129
static void stopTimer(TimerRef t)
Definition: IpplTimings.h:192
virtual VectorPair_t getEExtrema()=0
virtual void initFineLevels()=0
std::unique_ptr< size_t[]> globalPartPerLevel_m
Definition: AmrPartBunch.h:137
void initialize(FieldLayout_t *fLayout)