OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
BinnedFieldSolver.h
Go to the documentation of this file.
1#ifndef OPALX_BINNED_FIELD_SOLVER_H
2#define OPALX_BINNED_FIELD_SOLVER_H
3
4#include <algorithm>
5#include <cmath>
6#include <functional>
7#include <iomanip>
8#include <memory>
9#include <numeric>
10#include <string>
11#include <vector>
12
15#include "PartBunch/PartBunch.h"
17
41template <typename T, unsigned Dim>
42class BinnedFieldSolver : public FieldSolver<T, Dim> {
43 static_assert(Dim == 3, "BinnedFieldSolver currently supports Dim == 3 only.");
44
45public:
52
53 using particle_position_type = typename PartBunch_t::Base::particle_position_type;
54
60 enum class ScatterAttribute { ChargeQ };
61
70
77
92 std::string solver, Field_t<Dim>* rho, VField_t<T, Dim>* E, Field_t<Dim>* phi,
93 std::shared_ptr<BCHandler_t> bcHandler, int tablePrintFrequency, bool adaptiveBinning);
94
110
116
122
124 void setImageChargeConfiguration(bool enabled, double zPlane);
125 bool isImageChargeEnabled() const { return imageScatterController_m.isEnabled(); }
126 double getImageChargePlaneZ() const { return imageScatterController_m.getZPlane(); }
127
132 void setShiftedGreensConfiguration(bool enabled, double zPlane);
135
138 void setZerofaceMaxSteps(int maxSteps);
140
142 bool isImageChargeActiveForStep(size_t step) const;
143
146 bool isShiftedGreensActiveForStep(size_t step) const;
147
149 void setZeroFacePlaneDumpFrequency(int frequency);
151
156
157private:
161 bool adaptiveBinning_m = true;
166
167 // Shifted Green's function Dirichlet correction (alternative to image charges).
168 // Mutually exclusive with the image-charge path (enforced at config time).
171
172 // Scratch field holding the axis-flipped version of E' for the shifted-GF
173 // correction pass. Populated by buildFlippedZSlab (which delegates to
174 // opalx::detail::mirrorField) before accumulateFieldToTemp's flipped branch.
175 // Same layout/mesh/ghost count as *(this->getE()). Allocated lazily on first
176 // use; reused across bins and timesteps.
177 std::shared_ptr<VField_t<T, Dim>> flippedZSlabField_m;
178
182 struct BinStatsRow {
183 long long binNumber;
184 unsigned long long nParticles;
185 double gammaBin;
186 };
187
200 const std::string& binningCmdName, const std::vector<BinStatsRow>& rows);
201
202public:
206 static void setScalarField(Field_t<Dim>& field, double value);
207
211 static void scaleAndShiftScalarField(Field_t<Dim>& field, double scale, double shift);
212
216 static void setVectorField(VField_t<T, Dim>& field, const Vector_t<T, Dim>& value);
217
218private:
228 void dumpDirichletPlaneDiagnosticsIfRequested(PartBunch_t& bunch, const std::string& solveTag);
229
239
249
259 void rebinAndPrepare(PartBunch_t& bunch, std::shared_ptr<AdaptBins_t> bins);
260
261public:
277 PartBunch_t& bunch, std::shared_ptr<AdaptBins_t> bins, const bin_index_type binIndex,
278 const size_type nPartGlobal) const;
279
296 PartBunch_t& bunch, std::shared_ptr<AdaptBins_t> bins, const bin_index_type binIndex,
297 const size_type nPartGlobal, const double gammaBin,
299
331 const double gammaBin, const Vector_t<double, Dim>& pmean,
332 std::shared_ptr<VField_t<T, Dim>> EtmpSP, std::shared_ptr<VField_t<T, Dim>> BtmpSP,
333 double bFieldSign = 1.0, int flipAxis = -1);
334
335private:
348
357 PartBunch_t& bunch, std::shared_ptr<VField_t<T, Dim>> EtmpSP,
358 std::shared_ptr<VField_t<T, Dim>> BtmpSP);
359};
360
361// Reduce compile-time churn: instantiate the only supported concrete solver in one TU.
362extern template class BinnedFieldSolver<double, 3>;
363
364#endif // OPALX_BINNED_FIELD_SOLVER_H
ippl::Vector< T, Dim > Vector_t
constexpr unsigned Dim
Definition OPALTypes.h:7
ippl::Field< double, Dim, ViewArgs... > Field_t
Definition PBunchDefs.h:28
ippl::Field< ippl::Vector< T, Dim >, Dim, ViewArgs... > VField_t
Definition PBunchDefs.h:31
Template PIC bunch: IPPL PicManager, shared field mesh/solver, and multiple particle containers.
Handler for boundary conditions per spatial dimension.
Definition BCHandler.hpp:29
Field solver wrapper that implements the full binned self-field algorithm.
bool isShiftedGreensActiveForStep(size_t step) const
Check whether the shifted Green's function correction should run for a given timestep....
GatherAttribute gatherAttribute_m
bool warnedPlaneDumpParallelUnsupported_m
long long binNumber
Merged bin index (or -1 for legacy mode).
double getImageChargePlaneZ() const
void prepareRhoForBin(PartBunch_t &bunch, std::shared_ptr< AdaptBins_t > bins, const bin_index_type binIndex, const size_type nPartGlobal, const double gammaBin, ImageScatterMode mode=ImageScatterMode::PrimaryAndImage)
Build mesh rho for a specific merged bin and apply all corrections.
bool isImageChargeActiveForStep(size_t step) const
Check whether the explicit image-charge pass should run for a given timestep.
typename PartBunch_t::ParticleContainer_t ParticleCtr_t
typename AdaptBins_t::size_type size_type
void computeSelfFields(PartBunch_t &bunch)
Compute space-charge self-fields for the given particle bunch.
void accumulateFieldToTemp(const double gammaBin, const Vector_t< double, Dim > &pmean, std::shared_ptr< VField_t< T, Dim > > EtmpSP, std::shared_ptr< VField_t< T, Dim > > BtmpSP, double bFieldSign=1.0, int flipAxis=-1)
Accumulate Lorentz-transformed electric/magnetic fields into temporaries.
BinKinematics computeGammaBinGlobal(PartBunch_t &bunch, std::shared_ptr< AdaptBins_t > bins, const bin_index_type binIndex, const size_type nPartGlobal) const
Compute per-bin global mean momentum and gamma.
void setGatherAttribute(const GatherAttribute attr)
Set particle gather attribute (extensible; default is ElectricFieldE).
void setImageChargeConfiguration(bool enabled, double zPlane)
Configure optional image-charge scatter pass.
void computeBinnedSelfFields(PartBunch_t &bunch)
Compute self-fields using the binned algorithm.
void setShiftedGreensConfiguration(bool enabled, double zPlane)
Configure the shifted Green's function Dirichlet correction (alternative to image charges)....
void gatherFromTempToParticles(PartBunch_t &bunch, std::shared_ptr< VField_t< T, Dim > > EtmpSP, std::shared_ptr< VField_t< T, Dim > > BtmpSP)
Gather the accumulated lab-frame fields from temporaries back to particles.
static void setVectorField(VField_t< T, Dim > &field, const Vector_t< T, Dim > &value)
Set all vector field entries, including ghosts, without IPPL expression templates.
void dumpDirichletPlaneDiagnosticsIfRequested(PartBunch_t &bunch, const std::string &solveTag)
Dump and report potential values interpolated on the image-charge plane.
double getShiftedGreensPlaneZ() const
void setZerofaceMaxSteps(int maxSteps)
Set the maximum number of timesteps for which image charges are active (0 = unlimited).
typename AdaptBins_t::bin_index_type bin_index_type
std::shared_ptr< VField_t< T, Dim > > flippedZSlabField_m
bool isShiftedGreensEnabled() const
void setZeroFacePlaneDumpFrequency(int frequency)
Configure dump frequency for dirichlet-plane diagnostics (0 disables dumps).
ImageChargeScatterController< T, Dim > imageScatterController_m
typename PartBunch_t::AdaptBins_t AdaptBins_t
int getZerofaceMaxSteps() const
void rebinAndPrepare(PartBunch_t &bunch, std::shared_ptr< AdaptBins_t > bins)
Build and prepare adaptive bins for the current step.
ScatterAttribute scatterAttribute_m
ScatterAttribute
Which particle attribute to scatter from to build the mesh charge density rho.
void setScatterAttribute(const ScatterAttribute attr)
Set particle scatter attribute (extensible; default is ChargeQ).
GatherAttribute
Which particle attribute to gather the accumulated electric field into.
void buildFlippedZSlab(const VField_t< T, Dim > &src)
Populate flippedZSlab_m with the z-axis globally-flipped version of src.
void printBinStatsTable(const std::string &binningCmdName, const std::vector< BinStatsRow > &rows)
Print the bin statistics table at level 3.
typename PartBunch_t::Base::particle_position_type particle_position_type
double gammaBin
Global average gamma for the (merged) bin.
BinnedFieldSolver(std::string solver, Field_t< Dim > *rho, VField_t< T, Dim > *E, Field_t< Dim > *phi, std::shared_ptr< BCHandler_t > bcHandler, int tablePrintFrequency, bool adaptiveBinning)
Construct a binned/legacy-compatible solver.
int getZeroFacePlaneDumpFrequency() const
unsigned long long nParticles
Number of particles in the (merged) bin.
static void setScalarField(Field_t< Dim > &field, double value)
Set all scalar field entries, including ghosts, without IPPL expression templates.
bool isImageChargeEnabled() const
void computeLegacySelfFields(PartBunch_t &bunch)
Compute self-fields using the legacy monolithic algorithm.
static void scaleAndShiftScalarField(Field_t< Dim > &field, double scale, double shift)
Apply field = field * scale + shift on owned cells without expression templates.
ImageScatterMode
Controls which charges are scattered during rho preparation.
Row entry for the level-3 bin statistics table.
Orchestrates primary and image-charge scatter deposition.
ParticleContainer< T, Dim > ParticleContainer_t
Definition PartBunch.h:56
typename ParticleBinning::AdaptBinsBase< ParticleContainer_t > AdaptBins_t
Definition PartBunch.h:65
typename BunchType::bin_index_type bin_index_type
Definition AdaptBins.h:86
typename BunchType::size_type size_type
Definition AdaptBins.h:85