1#ifndef IPPL_BUMPON_TAIL_INSTABILITY_MANAGER_H
2#define IPPL_BUMPON_TAIL_INSTABILITY_MANAGER_H
25 KOKKOS_INLINE_FUNCTION
double operator()(
double x,
unsigned int d,
26 const double* params_p)
const {
29 + (params_p[d * 2 + 0] / params_p[d * 2 + 1])
30 * Kokkos::sin(params_p[d * 2 + 1] * x);
37 KOKKOS_INLINE_FUNCTION
double operator()(
double x,
unsigned int d,
38 double const* params_p)
const {
40 return (1.0 + params_p[d * 2 + 0] * Kokkos::cos(params_p[d * 2 + 1] * x));
47 KOKKOS_INLINE_FUNCTION
double operator()(
double u,
unsigned int d,
48 double const* params_p)
const {
49 return u + params_p[d] * 0.;
54template <
typename T,
unsigned Dim>
63 std::string& solver_, std::string& stepMethod_)
65 phase_m = std::make_shared<PhaseDump>();
86 const double pi = Kokkos::numbers::pi_v<T>;
90 "Open boundaries solver incompatible with this simulation!");
93 for (
unsigned i = 0; i <
Dim; i++) {
99 if (std::strcmp(
TestName,
"TwoStreamInstability") == 0) {
108 }
else if (std::strcmp(
TestName,
"BumponTailInstability") == 0) {
110 sigma_m = 1.0 / std::sqrt(2.0);
130 std::reduce(this->
rmax_m.begin(), this->rmax_m.end(), -1., std::multiplies<double>());
132 this->
dt_m = std::min(.05, 0.5 * *std::min_element(this->
hr_m.begin(), this->hr_m.end()));
136 m <<
"Discretization:" <<
endl
146 this->
fcontainer_m->getMesh(), this->fcontainer_m->getFL()));
152 &this->fcontainer_m->getPhi()));
163 m <<
"Phase dump only supported on one rank" <<
endl;
166 phase_m->initialize(*std::max_element(this->
nr_m.begin(), this->nr_m.end()),
167 *std::max_element(this->rmax_m.begin(), this->rmax_m.end()));
196 Inform m(
"Initialize Particles");
202 double parR[2 *
Dim];
203 for (
unsigned int i = 0; i <
Dim; i++) {
204 parR[i * 2] = this->delta_m;
205 parR[i * 2 + 1] = this->
kw_m[i];
213 m <<
"Starting first repartition" <<
endl;
217 const int nghost = this->
fcontainer_m->getRho().getNghost();
222 "Assign initial rho based on PDF",
224 KOKKOS_LAMBDA(
const index_array_type& args) {
230 ippl::apply(rhoview, args) = distR.getFullPdf(xvec);
248 Kokkos::Random_XorShift64_Pool<> rand_pool64((
size_type)(seed + 100 *
ippl::Comm->rank()));
256 samplingR_t samplingR(distR, rmax, rmin, rlayout, totalP);
257 size_type nlocal = samplingR.getLocalSamplesNum();
260 double factorVelBeam = 1.0 - factorVelBulk;
263 nlocal = nlocBulk + nlocBeam;
267 MPI_Allreduce(&nlocal, &nglobal, 1, MPI_UNSIGNED_LONG, MPI_SUM,
269 int rest = (int)(totalP - nglobal);
277 samplingR.generate(*R, rand_pool64);
283 for (
unsigned int i = 0; i <
Dim; i++) {
289 Kokkos::parallel_for(Kokkos::RangePolicy<int>(0, nlocBulk),
294 Kokkos::parallel_for(Kokkos::RangePolicy<int>(nlocBulk, nlocal),
303 m <<
"particles created and initial conditions assigned " <<
endl;
325 double dt = this->
dt_m;
326 std::shared_ptr<ParticleContainer_t> pc = this->
pcontainer_m;
327 std::shared_ptr<FieldContainer_t> fc = this->
fcontainer_m;
330 pc->P = pc->P - 0.5 * dt * pc->E;
335 pc->R = pc->R + dt * pc->P;
345 bool isFirstRepartition =
false;
348 auto* mesh = &fc->getRho().get_mesh();
349 auto* FL = &fc->getFL();
367 pc->P = pc->P - 0.5 * dt * pc->E;
388 void dump(
int it_, std::shared_ptr<ParticleContainer_t> pc,
bool allDims =
false) {
389 const auto pcount = pc->getLocalNum();
390 phase.realloc(pcount);
393 for (
unsigned d = allDims ? 0 :
Dim - 1; d <
Dim; d++) {
394 Kokkos::parallel_for(
395 "Copy phase space", pcount, KOKKOS_CLASS_LAMBDA(
const size_t i) {
396 phase(i) = {Ri(i)[d], Pi(i)[d]};
402 MPI_Reduce(view.data(),
phaseSpaceBuf.getView().data(), view.size(), MPI_DOUBLE,
405 std::stringstream fname;
406 fname <<
"PhaseSpace_t=" << it_ <<
"_d=" << d <<
".csv";
451 template <
typename View>
453 const int nghostE = this->
fcontainer_m->getE().getNghost();
454 double fieldEnergy, EzAmp;
461 KOKKOS_LAMBDA(
const index_array_type& args,
double& valL) {
467 Kokkos::Sum<double>(temp));
469 double globaltemp = 0.0;
470 ippl::Comm->reduce(temp, globaltemp, 1, std::plus<double>());
473 std::reduce(this->
fcontainer_m->getHr().begin(), this->fcontainer_m->getHr().end(),
474 globaltemp, std::multiplies<double>());
476 double tempMax = 0.0;
479 KOKKOS_LAMBDA(
const index_array_type& args,
double& valL) {
487 Kokkos::Max<double>(tempMax));
490 ippl::Comm->reduce(tempMax, EzAmp, 1, std::greater<double>());
493 std::stringstream fname;
494 fname <<
"data/FieldBumponTail_";
501 csvout.
setf(std::ios::scientific, std::ios::floatfield);
503 if (std::fabs(this->
time_m) < 1e-14) {
504 csvout <<
"time, Ez_field_energy, Ez_max_norm" <<
endl;
507 csvout << this->
time_m <<
" " << fieldEnergy <<
" " << EzAmp <<
endl;
typename ippl::detail::ViewType< ippl::Vector< double, Dim >, 1 >::view_type view_type
constexpr bool EnablePhaseDump
typename ippl::detail::ViewType< ippl::Vector< double, Dim >, 1 >::view_type view_type
ippl::detail::size_type size_type
Field< double, Dim, ViewArgs... > Field_t
ippl::FieldLayout< Dim > FieldLayout_t
ippl::Vector< T, Dim > Vector_t
ippl::UniformCartesian< double, Dim > Mesh_t
Inform & endl(Inform &inf)
KOKKOS_INLINE_FUNCTION constexpr decltype(auto) apply(const View &view, const Coords &coords)
RangePolicy< View::rank, typenameView::execution_space, PolicyArgs... >::policy_type getRangePolicy(const View &view, int shift=0)
void parallel_for(const std::string &name, const ExecPolicy &policy, const FunctorType &functor)
void parallel_reduce(const std::string &name, const ExecPolicy &policy, const FunctorType &functor, ReducerArgument &&... reducer)
std::unique_ptr< mpi::Communicator > Comm
KOKKOS_FUNCTION T uniform_cdf_func(T x)
KOKKOS_FUNCTION T uniform_pdf_func()
KOKKOS_INLINE_FUNCTION Vector< int, Dim > first() const
void setFieldSolver(std::shared_ptr< ippl::FieldSolverBase< T, Dim > > fsolver)
std::shared_ptr< ParticleContainer< T, Dim > > pcontainer_m
void setParticleContainer(std::shared_ptr< ParticleContainer< T, Dim > > pcontainer)
std::shared_ptr< ippl::FieldSolverBase< T, Dim > > fsolver_m
void setLoadBalancer(std::shared_ptr< LoadBalancer< T, Dim > > loadbalancer)
std::shared_ptr< FieldContainer< T, Dim > > fcontainer_m
std::shared_ptr< LoadBalancer< T, Dim > > loadbalancer_m
void setFieldContainer(std::shared_ptr< FieldContainer< T, Dim > > fcontainer)
The class that represents a distribution.
A class for inverse transform sampling.
Functor to generate random numbers from a normal distribution.
FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field)
Timing::TimerRef TimerRef
static TimerRef getTimer(const char *nm)
static void stopTimer(TimerRef t)
static void startTimer(TimerRef t)
::ippl::Vector< index_type, Dim > index_array_type
Vector_t< double, Dim > kw_m
void par2grid() override
Particle-to-grid operation.
Vector_t< double, Dim > origin_m
ippl::NDIndex< Dim > domain_m
Vector_t< int, Dim > nr_m
Vector_t< double, Dim > hr_m
Vector_t< double, Dim > rmin_m
bool isFirstRepartition_m
std::array< bool, Dim > decomp_m
Vector_t< double, Dim > rmax_m
AlpineManager(size_type totalP_, int nt_, Vector_t< int, Dim > &nr_, double lbt_, std::string &solver_, std::string &stepMethod_, std::vector< std::string > preconditioner_params={})
void grid2par() override
Grid-to-particle operation.
KOKKOS_INLINE_FUNCTION double operator()(double x, unsigned int d, const double *params_p) const
KOKKOS_INLINE_FUNCTION double operator()(double x, unsigned int d, double const *params_p) const
KOKKOS_INLINE_FUNCTION double operator()(double u, unsigned int d, double const *params_p) const
FieldSolver< T, Dim > FieldSolver_t
void dumpBumponTailInstability(const View &Eview)
BumponTailInstabilityManager(size_type totalP_, int nt_, Vector_t< int, Dim > &nr_, double lbt_, std::string &solver_, std::string &stepMethod_)
void initializeParticles()
void pre_run() override
A method that should be used for setting up the simulation.
void advance() override
A method that should be used to execute/advance a step of simulation.
std::shared_ptr< PhaseDump > phase_m
~BumponTailInstabilityManager()
LoadBalancer< T, Dim > LoadBalancer_t
FieldContainer< T, Dim > FieldContainer_t
ParticleContainer< T, Dim > ParticleContainer_t
Field_t< 2 > phaseSpaceBuf
FieldLayout_t< 2 > layout
std::array< bool, 2 > isParallel
void initialize(size_t nr_, double domain_)
double maxRecorded() const
void dump(int it_, std::shared_ptr< ParticleContainer_t > pc, bool allDims=false)
double minRecorded() const
ippl::ParticleAttrib< Vector_t< double, 2 > > phase