IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
AlpineManager.h
Go to the documentation of this file.
1#ifndef IPPL_ALPINE_MANAGER_H
2#define IPPL_ALPINE_MANAGER_H
3
4#include <memory>
5
6#include "FieldContainer.hpp"
7#include "FieldSolver.hpp"
8#include "LoadBalancer.hpp"
10#include "Manager/PicManager.h"
11#include "ParticleContainer.hpp"
12#include "Random/Distribution.h"
15#include "Random/Randn.h"
16
18
19template <typename T, unsigned Dim>
20class AlpineManager : public ippl::PicManager<T, Dim, ParticleContainer<T, Dim>,
21 FieldContainer<T, Dim>, LoadBalancer<T, Dim>> {
22public:
28
29protected:
31 int nt_m;
33 double lbt_m;
34 std::string solver_m;
35 std::string stepMethod_m;
36 std::vector<std::string> preconditioner_params_m;
37public:
38 AlpineManager(size_type totalP_, int nt_, Vector_t<int, Dim>& nr_, double lbt_,
39 std::string& solver_, std::string& stepMethod_,
40 std::vector<std::string> preconditioner_params = {})
42 LoadBalancer<T, Dim>>()
43 , totalP_m(totalP_)
44 , nt_m(nt_)
45 , nr_m(nr_)
46 , lbt_m(lbt_)
47 , solver_m(solver_)
48 , stepMethod_m(stepMethod_)
49 , preconditioner_params_m(preconditioner_params) {}
51
52protected:
53 double time_m;
54 double dt_m;
55 int it_m;
57 double alpha_m;
61 double Q_m;
66 std::array<bool, Dim> decomp_m;
67 double rhoNorm_m;
68
69public:
70 size_type getTotalP() const { return totalP_m; }
71
72 void setTotalP(size_type totalP_) { totalP_m = totalP_; }
73
74 int getNt() const { return nt_m; }
75
76 void setNt(int nt_) { nt_m = nt_; }
77
78 const std::string& getSolver() const { return solver_m; }
79
80 void setSolver(const std::string& solver_) { solver_m = solver_; }
81
82 double getLoadBalanceThreshold() const { return lbt_m; }
83
84 void setLoadBalanceThreshold(double lbt_) { lbt_m = lbt_; }
85
86 const std::string& getStepMethod() const { return stepMethod_m; }
87
88 void setStepMethod(const std::string& stepMethod_) { stepMethod_m = stepMethod_; }
89
90 const Vector_t<int, Dim>& getNr() const { return nr_m; }
91
92 void setNr(const Vector_t<int, Dim>& nr_) { nr_m = nr_; }
93
94 double getTime() const { return time_m; }
95
96 void setTime(double time_) { time_m = time_; }
97
98 std::vector<std::string> getPreconditionerParams() const { return preconditioner_params_m; };
99
100 virtual void dump(){/* default does nothing */};
101
102 void pre_step() override {
103 Inform m("Pre-step");
104 m << "Done" << endl;
105 }
106
107 void post_step() override {
108 // Update time
109 this->time_m += this->dt_m;
110 this->it_m++;
111 // wrtie solution to output file
112 this->dump();
113
114 Inform m("Post-step:");
115 m << "Finished time step: " << this->it_m << " time: " << this->time_m << endl;
116 }
117
118 void grid2par() override { gatherCIC(); }
119
120 void gatherCIC() {
121 gather(this->pcontainer_m->E, this->fcontainer_m->getE(), this->pcontainer_m->R);
122 }
123
124 void par2grid() override { scatterCIC(); }
125
126 void scatterCIC() {
127 Inform m("scatter ");
128 this->fcontainer_m->getRho() = 0.0;
129
131 typename Base::particle_position_type* R = &this->pcontainer_m->R;
132 Field_t<Dim>* rho = &this->fcontainer_m->getRho();
133 double Q = Q_m;
137
138 scatter(*q, *rho, *R);
139 double relError = std::fabs((Q - (*rho).sum()) / Q);
140
141 m << relError << endl;
142
143 size_type TotalParticles = 0;
144 size_type localParticles = this->pcontainer_m->getLocalNum();
145
146 ippl::Comm->reduce(localParticles, TotalParticles, 1, std::plus<size_type>());
147
148 if (ippl::Comm->rank() == 0) {
149 if (TotalParticles != totalP_m || relError > 1e-10) {
150 m << "Time step: " << it_m << endl;
151 m << "Total particles in the sim. " << totalP_m << " "
152 << "after update: " << TotalParticles << endl;
153 m << "Rel. error in charge conservation: " << relError << endl;
154 ippl::Comm->abort();
155 }
156 }
157
158 double cellVolume = std::reduce(hr.begin(), hr.end(), 1., std::multiplies<double>());
159 (*rho) = (*rho) / cellVolume;
160
161 rhoNorm_m = norm(*rho);
162
163 // rho = rho_e - rho_i (only if periodic BCs)
164 if (this->fsolver_m->getStype() != "OPEN") {
165 double size = 1;
166 for (unsigned d = 0; d < Dim; d++) {
167 size *= rmax[d] - rmin[d];
168 }
169 *rho = *rho - (Q / size);
170 }
171 }
172};
173#endif
typename ippl::detail::ViewType< ippl::Vector< double, Dim >, 1 >::view_type view_type
constexpr unsigned Dim
ippl::detail::size_type size_type
Definition datatypes.h:23
Field< double, Dim, ViewArgs... > Field_t
Definition datatypes.h:41
ippl::Vector< T, Dim > Vector_t
Definition datatypes.h:38
Inform & endl(Inform &inf)
Definition Inform.cpp:42
Definition Archive.h:20
std::unique_ptr< mpi::Communicator > Comm
Definition Ippl.h:22
A template class for managing Particle-in-Cell (PIC) simulations.
Definition PicManager.h:27
typename ippl::ParticleSpatialLayout< T, Dim >::particle_position_type particle_position_type
KOKKOS_INLINE_FUNCTION constexpr iterator begin()
Definition Vector.hpp:160
KOKKOS_INLINE_FUNCTION constexpr iterator end()
Definition Vector.hpp:165
void setStepMethod(const std::string &stepMethod_)
void setLoadBalanceThreshold(double lbt_)
Vector_t< double, Dim > kw_m
void par2grid() override
Particle-to-grid operation.
ParticleContainer< T, Dim > ParticleContainer_t
void setNr(const Vector_t< int, Dim > &nr_)
std::vector< std::string > getPreconditionerParams() const
size_type getTotalP() const
Vector_t< double, Dim > origin_m
const std::string & getStepMethod() const
ippl::NDIndex< Dim > domain_m
Vector_t< int, Dim > nr_m
Vector_t< double, Dim > hr_m
std::vector< std::string > preconditioner_params_m
void post_step() override
A method that should be used after perfoming a step of simulation.
const Vector_t< int, Dim > & getNr() const
Vector_t< double, Dim > rmin_m
void setTime(double time_)
virtual void dump()
LoadBalancer< T, Dim > LoadBalancer_t
double getTime() const
const std::string & getSolver() const
int getNt() const
void pre_step() override
A method that should be used for preparation before perfoming a step of simulation.
void setSolver(const std::string &solver_)
FieldContainer< T, Dim > FieldContainer_t
FieldSolver< T, Dim > FieldSolver_t
bool isFirstRepartition_m
ippl::ParticleBase< ippl::ParticleSpatialLayout< T, Dim > > Base
std::string stepMethod_m
void setTotalP(size_type totalP_)
std::array< bool, Dim > decomp_m
std::string solver_m
size_type totalP_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 setNt(int nt_)
void grid2par() override
Grid-to-particle operation.
double getLoadBalanceThreshold() const