1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_MLMG.H>
#include <AMReX_MLNodeTensorLaplacian.H>
#include <WarpX.H>
using namespace amrex;
void
WarpX::InitSpaceChargeField (WarpXParticleContainer& pc)
{
#ifdef WARPX_DIM_RZ
amrex::Abort("The initialization of space-charge field has not yet been implemented in RZ geometry.");
#endif
// Allocate fields for charge and potential
const int num_levels = max_level + 1;
Vector<std::unique_ptr<MultiFab> > rho(num_levels);
Vector<std::unique_ptr<MultiFab> > phi(num_levels);
const int ng = WarpX::nox;
for (int lev = 0; lev <= max_level; lev++) {
BoxArray nba = boxArray(lev);
nba.surroundingNodes();
rho[lev].reset(new MultiFab(nba, dmap[lev], 1, ng)); // Make ng big enough/use rho from sim
phi[lev].reset(new MultiFab(nba, dmap[lev], 1, 1));
phi[lev]->setVal(0.);
}
// Deposit particle charge density (source of Poisson solver)
bool const local = false;
bool const reset = true;
bool const do_rz_volume_scaling = true;
pc.DepositCharge(rho, local, reset, do_rz_volume_scaling);
// Get the particle beta vector
bool const local_average = false; // Average across all MPI ranks
std::array<Real, 3> beta = pc.meanParticleVelocity(local_average);
for (Real& beta_comp : beta) beta_comp /= PhysConst::c; // Normalize
// Compute the potential phi, by solving the Poisson equation
computePhi( rho, phi, beta, pc.self_fields_required_precision );
// Compute the corresponding electric and magnetic field, from the potential phi
computeE( Efield_fp, phi, beta );
computeB( Bfield_fp, phi, beta );
}
/* Compute the potential `phi` by solving the Poisson equation with `rho` as
a source, assuming that the source moves at a constant speed \f$\vec{\beta}\f$.
This uses the amrex solver.
More specifically, this solves the equation
\f[
\vec{\nabla}^2\phi - (\vec{\beta}\cdot\vec{\nabla})^2\phi = -\frac{\rho}{\epsilon_0}
\f]
\param[in] rho The charge density a given species
\param[out] phi The potential to be computed by this function
\param[in] beta Represents the velocity of the source of `phi`
*/
void
WarpX::computePhi (const amrex::Vector<std::unique_ptr<amrex::MultiFab> >& rho,
amrex::Vector<std::unique_ptr<amrex::MultiFab> >& phi,
std::array<Real, 3> const beta,
Real const required_precision) const
{
// Define the boundary conditions
Array<LinOpBCType,AMREX_SPACEDIM> lobc, hibc;
for (int idim=0; idim<AMREX_SPACEDIM; idim++){
if ( Geom(0).isPeriodic(idim) ) {
lobc[idim] = LinOpBCType::Periodic;
hibc[idim] = LinOpBCType::Periodic;
} else {
// Use Dirichlet boundary condition by default.
// Ideally, we would often want open boundary conditions here.
lobc[idim] = LinOpBCType::Dirichlet;
hibc[idim] = LinOpBCType::Dirichlet;
}
}
// Define the linear operator (Poisson operator)
MLNodeTensorLaplacian linop( Geom(), boxArray(), DistributionMap() );
linop.setDomainBC( lobc, hibc );
// Set the value of beta
amrex::Array<amrex::Real,AMREX_SPACEDIM> beta_solver =
#if (AMREX_SPACEDIM==2)
{{ beta[0], beta[2] }}; // beta_x and beta_z
#else
{{ beta[0], beta[1], beta[2] }};
#endif
linop.setBeta( beta_solver );
// Solve the Poisson equation
MLMG mlmg(linop);
mlmg.setVerbose(2);
mlmg.solve( GetVecOfPtrs(phi), GetVecOfConstPtrs(rho), required_precision, 0.0);
// Normalize by the correct physical constant
for (int lev=0; lev < rho.size(); lev++){
phi[lev]->mult(-1./PhysConst::ep0);
}
}
/* \bried Compute the electric field that corresponds to `phi`, and
add it to the set of MultiFab `E`.
The electric field is calculated by assuming that the source that
produces the `phi` potential is moving with a constant speed \f$\vec{\beta}\f$:
\f[
\vec{E} = -\vec{\nabla}\phi + (\vec{\beta}\cdot\vec{\beta})\phi \vec{\beta}
\f]
(where the second term represent the term \f$\partial_t \vec{A}\f$, in
the case of a moving source)
\param[inout] E Electric field on the grid
\param[in] phi The potential from which to compute the electric field
\param[in] beta Represents the velocity of the source of `phi`
*/
void
WarpX::computeE (amrex::Vector<std::array<std::unique_ptr<amrex::MultiFab>, 3> >& E,
const amrex::Vector<std::unique_ptr<amrex::MultiFab> >& phi,
std::array<amrex::Real, 3> const beta ) const
{
for (int lev = 0; lev <= max_level; lev++) {
const Real* dx = Geom(lev).CellSize();
#ifdef _OPENMP
#pragma omp parallel if (Gpu::notInLaunchRegion())
#endif
for ( MFIter mfi(*phi[lev], TilingIfNotGPU()); mfi.isValid(); ++mfi )
{
const Real inv_dx = 1./dx[0];
#if (AMREX_SPACEDIM == 3)
const Real inv_dy = 1./dx[1];
const Real inv_dz = 1./dx[2];
#else
const Real inv_dz = 1./dx[1];
#endif
const Box& tbx = mfi.tilebox(Ex_nodal_flag);
const Box& tby = mfi.tilebox(Ey_nodal_flag);
const Box& tbz = mfi.tilebox(Ez_nodal_flag);
const auto& phi_arr = phi[lev]->array(mfi);
const auto& Ex_arr = (*E[lev][0])[mfi].array();
const auto& Ey_arr = (*E[lev][1])[mfi].array();
const auto& Ez_arr = (*E[lev][2])[mfi].array();
Real beta_x = beta[0];
Real beta_y = beta[1];
Real beta_z = beta[2];
// Calculate the electric field
// Use discretized derivative that matches the staggering of the grid.
#if (AMREX_SPACEDIM == 3)
amrex::ParallelFor( tbx, tby, tbz,
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Ex_arr(i,j,k) +=
+(beta_x*beta_x-1)*inv_dx*( phi_arr(i+1,j,k)-phi_arr(i,j,k) )
+beta_x*beta_y*0.25*inv_dy*(phi_arr(i ,j+1,k)-phi_arr(i ,j-1,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i+1,j-1,k))
+beta_x*beta_z*0.25*inv_dz*(phi_arr(i ,j,k+1)-phi_arr(i ,j,k-1)
+ phi_arr(i+1,j,k+1)-phi_arr(i+1,j,k-1));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Ey_arr(i,j,k) +=
+beta_y*beta_x*0.25*inv_dx*(phi_arr(i+1,j ,k)-phi_arr(i-1,j ,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i-1,j+1,k))
+(beta_y*beta_y-1)*inv_dy*( phi_arr(i,j+1,k)-phi_arr(i,j,k) )
+beta_y*beta_z*0.25*inv_dz*(phi_arr(i,j ,k+1)-phi_arr(i,j ,k-1)
+ phi_arr(i,j+1,k+1)-phi_arr(i,j+1,k-1));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Ez_arr(i,j,k) +=
+beta_z*beta_x*0.25*inv_dx*(phi_arr(i+1,j,k )-phi_arr(i-1,j,k )
+ phi_arr(i+1,j,k+1)-phi_arr(i-1,j,k+1))
+beta_z*beta_y*0.25*inv_dy*(phi_arr(i,j+1,k )-phi_arr(i,j-1,k )
+ phi_arr(i,j+1,k+1)-phi_arr(i,j-1,k+1))
+(beta_y*beta_z-1)*inv_dz*( phi_arr(i,j,k+1)-phi_arr(i,j,k) );
}
);
#else
amrex::ParallelFor( tbx, tbz,
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Ex_arr(i,j,k) +=
+(beta_x*beta_x-1)*inv_dx*( phi_arr(i+1,j,k)-phi_arr(i,j,k) )
+beta_x*beta_z*0.25*inv_dz*(phi_arr(i ,j+1,k)-phi_arr(i ,j-1,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i+1,j-1,k));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Ez_arr(i,j,k) +=
+beta_z*beta_x*0.25*inv_dx*(phi_arr(i+1,j ,k)-phi_arr(i-1,j ,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i-1,j+1,k))
+(beta_y*beta_z-1)*inv_dz*( phi_arr(i,j+1,k)-phi_arr(i,j,k) );
}
);
#endif
}
}
}
/* \bried Compute the magnetic field that corresponds to `phi`, and
add it to the set of MultiFab `B`.
The magnetic field is calculated by assuming that the source that
produces the `phi` potential is moving with a constant speed \f$\vec{\beta}\f$:
\f[
\vec{B} = -\frac{1}{c}\vec{\beta}\times\vec{\nabla}\phi
\f]
(this represents the term \f$\vec{\nabla} \times \vec{A}\f$, in the case of a moving source)
\param[inout] E Electric field on the grid
\param[in] phi The potential from which to compute the electric field
\param[in] beta Represents the velocity of the source of `phi`
*/
void
WarpX::computeB (amrex::Vector<std::array<std::unique_ptr<amrex::MultiFab>, 3> >& B,
const amrex::Vector<std::unique_ptr<amrex::MultiFab> >& phi,
std::array<amrex::Real, 3> const beta ) const
{
for (int lev = 0; lev <= max_level; lev++) {
const Real* dx = Geom(lev).CellSize();
#ifdef _OPENMP
#pragma omp parallel if (Gpu::notInLaunchRegion())
#endif
for ( MFIter mfi(*phi[lev], TilingIfNotGPU()); mfi.isValid(); ++mfi )
{
const Real inv_dx = 1./dx[0];
#if (AMREX_SPACEDIM == 3)
const Real inv_dy = 1./dx[1];
const Real inv_dz = 1./dx[2];
#else
const Real inv_dz = 1./dx[1];
#endif
const Box& tbx = mfi.tilebox(Bx_nodal_flag);
const Box& tby = mfi.tilebox(By_nodal_flag);
const Box& tbz = mfi.tilebox(Bz_nodal_flag);
const auto& phi_arr = phi[0]->array(mfi);
const auto& Bx_arr = (*B[lev][0])[mfi].array();
const auto& By_arr = (*B[lev][1])[mfi].array();
const auto& Bz_arr = (*B[lev][2])[mfi].array();
Real beta_x = beta[0];
Real beta_y = beta[1];
Real beta_z = beta[2];
constexpr Real inv_c = 1./PhysConst::c;
// Calculate the magnetic field
// Use discretized derivative that matches the staggering of the grid.
#if (AMREX_SPACEDIM == 3)
amrex::ParallelFor( tbx, tby, tbz,
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Bx_arr(i,j,k) += inv_c * (
-beta_y*inv_dz*0.5*(phi_arr(i,j ,k+1)-phi_arr(i,j ,k)
+ phi_arr(i,j+1,k+1)-phi_arr(i,j+1,k))
+beta_z*inv_dy*0.5*(phi_arr(i,j+1,k )-phi_arr(i,j,k )
+ phi_arr(i,j+1,k+1)-phi_arr(i,j,k+1)));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
By_arr(i,j,k) += inv_c * (
-beta_z*inv_dx*0.5*(phi_arr(i+1,j,k )-phi_arr(i,j,k )
+ phi_arr(i+1,j,k+1)-phi_arr(i,j,k+1))
+beta_x*inv_dz*0.5*(phi_arr(i ,j,k+1)-phi_arr(i ,j,k)
+ phi_arr(i+1,j,k+1)-phi_arr(i+1,j,k)));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Bz_arr(i,j,k) += inv_c * (
-beta_x*inv_dy*0.5*(phi_arr(i ,j+1,k)-phi_arr(i ,j,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i+1,j,k))
+beta_y*inv_dx*0.5*(phi_arr(i+1,j ,k)-phi_arr(i,j ,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i,j+1,k)));
}
);
#else
amrex::ParallelFor( tbx, tby, tbz,
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Bx_arr(i,j,k) += inv_c * (
-beta_y*inv_dz*( phi_arr(i,j+1,k)-phi_arr(i,j,k) ));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
By_arr(i,j,k) += inv_c * (
-beta_z*inv_dx*0.5*(phi_arr(i+1,j ,k)-phi_arr(i,j ,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i,j+1,k))
+beta_x*inv_dz*0.5*(phi_arr(i ,j+1,k)-phi_arr(i ,j,k)
+ phi_arr(i+1,j+1,k)-phi_arr(i+1,j,k)));
},
[=] AMREX_GPU_DEVICE (int i, int j, int k) {
Bz_arr(i,j,k) += inv_c * (
+beta_y*inv_dx*( phi_arr(i+1,j,k)-phi_arr(i,j,k) ));
}
);
#endif
}
}
}
|