-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathChargedParticles.hpp
More file actions
743 lines (577 loc) · 25.4 KB
/
ChargedParticles.hpp
File metadata and controls
743 lines (577 loc) · 25.4 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// ChargedParticles header file
// Defines a particle attribute for charged particles to be used in
// test programs
//
// Copyright (c) 2021 Paul Scherrer Institut, Villigen PSI, Switzerland
// All rights reserved
//
// This file is part of IPPL.
//
// IPPL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// You should have received a copy of the GNU General Public License
// along with IPPL. If not, see <https://www.gnu.org/licenses/>.
//
#include "Ippl.h"
#include "Solver/FFTPeriodicPoissonSolver.h"
// dimension of our positions
constexpr unsigned Dim = 3;
// some typedefs
typedef ippl::ParticleSpatialLayout<double,Dim> PLayout_t;
typedef ippl::UniformCartesian<double, Dim> Mesh_t;
typedef ippl::FieldLayout<Dim> FieldLayout_t;
typedef ippl::OrthogonalRecursiveBisection<double, Dim, Mesh_t> ORB;
using size_type = ippl::detail::size_type;
template<typename T, unsigned Dim>
using Vector = ippl::Vector<T, Dim>;
template<typename T, unsigned Dim>
using Field = ippl::Field<T, Dim>;
template<typename T>
using ParticleAttrib = ippl::ParticleAttrib<T>;
typedef Vector<double, Dim> Vector_t;
typedef Field<double, Dim> Field_t;
typedef Field<Vector_t, Dim> VField_t;
typedef ippl::FFTPeriodicPoissonSolver<Vector_t, double, Dim> Solver_t;
const double pi = std::acos(-1.0);
// Test programs have to define this variable for VTK dump purposes
extern const char* TestName;
void dumpVTK(VField_t& E, int nx, int ny, int nz, int iteration,
double dx, double dy, double dz) {
typename VField_t::view_type::host_mirror_type host_view = E.getHostMirror();
std::stringstream fname;
fname << "data/ef_";
fname << std::setw(4) << std::setfill('0') << iteration;
fname << ".vtk";
Kokkos::deep_copy(host_view, E.getView());
Inform vtkout(NULL, fname.str().c_str(), Inform::OVERWRITE);
vtkout.precision(10);
vtkout.setf(std::ios::scientific, std::ios::floatfield);
// start with header
vtkout << "# vtk DataFile Version 2.0" << endl;
vtkout << TestName << endl;
vtkout << "ASCII" << endl;
vtkout << "DATASET STRUCTURED_POINTS" << endl;
vtkout << "DIMENSIONS " << nx+3 << " " << ny+3 << " " << nz+3 << endl;
vtkout << "ORIGIN " << -dx << " " << -dy << " " << -dz << endl;
vtkout << "SPACING " << dx << " " << dy << " " << dz << endl;
vtkout << "CELL_DATA " << (nx+2)*(ny+2)*(nz+2) << endl;
vtkout << "VECTORS E-Field float" << endl;
for (int z=0; z<nz+2; z++) {
for (int y=0; y<ny+2; y++) {
for (int x=0; x<nx+2; x++) {
vtkout << host_view(x,y,z)[0] << "\t"
<< host_view(x,y,z)[1] << "\t"
<< host_view(x,y,z)[2] << endl;
}
}
}
}
void dumpVTK(Field_t& rho, int nx, int ny, int nz, int iteration,
double dx, double dy, double dz) {
typename Field_t::view_type::host_mirror_type host_view = rho.getHostMirror();
std::stringstream fname;
fname << "data/scalar_";
fname << std::setw(4) << std::setfill('0') << iteration;
fname << ".vtk";
Kokkos::deep_copy(host_view, rho.getView());
Inform vtkout(NULL, fname.str().c_str(), Inform::OVERWRITE);
vtkout.precision(10);
vtkout.setf(std::ios::scientific, std::ios::floatfield);
// start with header
vtkout << "# vtk DataFile Version 2.0" << endl;
vtkout << TestName << endl;
vtkout << "ASCII" << endl;
vtkout << "DATASET STRUCTURED_POINTS" << endl;
vtkout << "DIMENSIONS " << nx+3 << " " << ny+3 << " " << nz+3 << endl;
vtkout << "ORIGIN " << -dx << " " << -dy << " " << -dz << endl;
vtkout << "SPACING " << dx << " " << dy << " " << dz << endl;
vtkout << "CELL_DATA " << (nx+2)*(ny+2)*(nz+2) << endl;
vtkout << "SCALARS Rho float" << endl;
vtkout << "LOOKUP_TABLE default" << endl;
for (int z=0; z<nz+2; z++) {
for (int y=0; y<ny+2; y++) {
for (int x=0; x<nx+2; x++) {
vtkout << host_view(x,y,z) << endl;
}
}
}
}
template<class PLayout>
class ChargedParticles : public ippl::ParticleBase<PLayout> {
public:
VField_t E_m;
Field_t rho_m;
// ORB
ORB orb;
Vector<int, Dim> nr_m;
ippl::e_dim_tag decomp_m[Dim];
Vector_t hr_m;
Vector_t rmin_m;
Vector_t rmax_m;
double Q_m;
std::string stype_m;
std::shared_ptr<Solver_t> solver_mp;
double time_m;
double rhoNorm_m;
unsigned int loadbalancefreq_m;
double loadbalancethreshold_m;
public:
ParticleAttrib<double> q; // charge
typename ippl::ParticleBase<PLayout>::particle_position_type P; // particle velocity
typename ippl::ParticleBase<PLayout>::particle_position_type E; // electric field at particle position
/*
This constructor is mandatory for all derived classes from
ParticleBase as the bunch buffer uses this
*/
ChargedParticles(PLayout& pl)
: ippl::ParticleBase<PLayout>(pl)
{
// register the particle attributes
this->addAttribute(q);
this->addAttribute(P);
this->addAttribute(E);
}
ChargedParticles(PLayout& pl,
Vector_t hr,
Vector_t rmin,
Vector_t rmax,
ippl::e_dim_tag decomp[Dim],
double Q)
: ippl::ParticleBase<PLayout>(pl)
, hr_m(hr)
, rmin_m(rmin)
, rmax_m(rmax)
, Q_m(Q)
{
// register the particle attributes
this->addAttribute(q);
this->addAttribute(P);
this->addAttribute(E);
setupBCs();
for (unsigned int i = 0; i < Dim; i++)
decomp_m[i]=decomp[i];
}
~ChargedParticles(){ }
void setupBCs() {
setBCAllPeriodic();
}
void updateLayout(FieldLayout_t& fl, Mesh_t& mesh, ChargedParticles<PLayout>& buffer,
bool& isFirstRepartition) {
// Update local fields
static IpplTimings::TimerRef tupdateLayout = IpplTimings::getTimer("updateLayout");
IpplTimings::startTimer(tupdateLayout);
this->E_m.updateLayout(fl);
this->rho_m.updateLayout(fl);
// Update layout with new FieldLayout
PLayout& layout = this->getLayout();
layout.updateLayout(fl, mesh);
IpplTimings::stopTimer(tupdateLayout);
static IpplTimings::TimerRef tupdatePLayout = IpplTimings::getTimer("updatePB");
IpplTimings::startTimer(tupdatePLayout);
if(!isFirstRepartition) {
layout.update(*this, buffer);
}
IpplTimings::stopTimer(tupdatePLayout);
}
void initializeORB(FieldLayout_t& fl, Mesh_t& mesh) {
orb.initialize(fl, mesh, rho_m);
}
void repartition(FieldLayout_t& fl, Mesh_t& mesh, ChargedParticles<PLayout>& buffer,
bool& isFirstRepartition) {
// Repartition the domains
bool res = orb.binaryRepartition(this->R, fl, isFirstRepartition);
if (res != true) {
std::cout << "Could not repartition!" << std::endl;
return;
}
// Update
this->updateLayout(fl, mesh, buffer, isFirstRepartition);
this->solver_mp->setRhs(rho_m);
}
bool balance(size_type totalP, const unsigned int nstep){
if(std::strcmp(TestName,"UniformPlasmaTest") == 0) {
return (nstep % loadbalancefreq_m == 0);
}
else {
int local = 0;
std::vector<int> res(Ippl::Comm->size());
double equalPart = (double) totalP / Ippl::Comm->size();
double dev = std::abs((double)this->getLocalNum() - equalPart) / totalP;
if (dev > loadbalancethreshold_m)
local = 1;
MPI_Allgather(&local, 1, MPI_INT, res.data(), 1, MPI_INT, Ippl::getComm());
for (unsigned int i = 0; i < res.size(); i++) {
if (res[i] == 1)
return true;
}
return false;
}
}
void gatherStatistics(size_type totalP) {
std::vector<double> imb(Ippl::Comm->size());
double equalPart = (double) totalP / Ippl::Comm->size();
double dev = (std::abs((double)this->getLocalNum() - equalPart)
/ totalP) * 100.0;
MPI_Gather(&dev, 1, MPI_DOUBLE, imb.data(), 1, MPI_DOUBLE, 0,
Ippl::getComm());
if (Ippl::Comm->rank() == 0) {
std::stringstream fname;
fname << "data/LoadBalance_";
fname << Ippl::Comm->size();
fname << ".csv";
Inform csvout(NULL, fname.str().c_str(), Inform::APPEND);
csvout.precision(5);
csvout.setf(std::ios::scientific, std::ios::floatfield);
if(time_m == 0.0) {
csvout << "time, rank, imbalance percentage" << endl;
}
for(int r=0; r < Ippl::Comm->size(); ++r) {
csvout << time_m << " "
<< r << " "
<< imb[r] << endl;
}
}
Ippl::Comm->barrier();
}
void gatherCIC() {
gather(this->E, E_m, this->R);
}
void scatterCIC(size_type totalP, unsigned int iteration, Vector_t& hrField) {
Inform m("scatter ");
rho_m = 0.0;
scatter(q, rho_m, this->R);
static IpplTimings::TimerRef sumTimer = IpplTimings::getTimer("Check");
IpplTimings::startTimer(sumTimer);
double Q_grid = rho_m.sum();
size_type Total_particles = 0;
size_type local_particles = this->getLocalNum();
MPI_Reduce(&local_particles, &Total_particles, 1,
MPI_UNSIGNED_LONG, MPI_SUM, 0, Ippl::getComm());
double rel_error = std::fabs((Q_m-Q_grid)/Q_m);
m << "Rel. error in charge conservation = " << rel_error << endl;
if(Ippl::Comm->rank() == 0) {
if(Total_particles != totalP || rel_error > 1e-10) {
m << "Time step: " << iteration << endl;
m << "Total particles in the sim. " << totalP
<< " " << "after update: "
<< Total_particles << endl;
m << "Rel. error in charge conservation: "
<< rel_error << endl;
std::abort();
}
}
rho_m = rho_m / (hrField[0] * hrField[1] * hrField[2]);
rhoNorm_m = norm(rho_m);
IpplTimings::stopTimer(sumTimer);
//dumpVTK(rho_m,nr_m[0],nr_m[1],nr_m[2],iteration,hrField[0],hrField[1],hrField[2]);
//rho = rho_e - rho_i
rho_m = rho_m - (Q_m/((rmax_m[0] - rmin_m[0]) * (rmax_m[1] - rmin_m[1]) * (rmax_m[2] - rmin_m[2])));
}
void initSolver() {
Inform m("solver ");
if(stype_m == "FFT")
initFFTSolver();
else
m << "No solver matches the argument" << endl;
}
void initFFTSolver() {
ippl::ParameterList sp;
sp.add("output_type", Solver_t::GRAD);
sp.add("use_heffte_defaults", false);
sp.add("use_pencils", true);
sp.add("use_reorder", false);
sp.add("use_gpu_aware", true);
sp.add("comm", ippl::p2p_pl);
sp.add("r2c_direction", 0);
solver_mp = std::make_shared<Solver_t>();
solver_mp->mergeParameters(sp);
solver_mp->setRhs(rho_m);
solver_mp->setLhs(E_m);
}
void dumpData() {
auto Pview = P.getView();
double Energy = 0.0;
Kokkos::parallel_reduce("Particle Energy", this->getLocalNum(),
KOKKOS_LAMBDA(const int i, double& valL){
double myVal = dot(Pview(i), Pview(i)).apply();
valL += myVal;
}, Kokkos::Sum<double>(Energy));
Energy *= 0.5;
double gEnergy = 0.0;
MPI_Reduce(&Energy, &gEnergy, 1,
MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
const int nghostE = E_m.getNghost();
auto Eview = E_m.getView();
Vector_t normE;
using mdrange_type = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
for (unsigned d=0; d<Dim; ++d) {
double temp = 0.0;
Kokkos::parallel_reduce("Vector E reduce",
mdrange_type({nghostE, nghostE, nghostE},
{Eview.extent(0) - nghostE,
Eview.extent(1) - nghostE,
Eview.extent(2) - nghostE}),
KOKKOS_LAMBDA(const size_t i, const size_t j,
const size_t k, double& valL)
{
double myVal = std::pow(Eview(i, j, k)[d], 2);
valL += myVal;
}, Kokkos::Sum<double>(temp));
double globaltemp = 0.0;
MPI_Reduce(&temp, &globaltemp, 1, MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
normE[d] = std::sqrt(globaltemp);
}
if (Ippl::Comm->rank() == 0) {
std::stringstream fname;
fname << "data/ParticleField_";
fname << Ippl::Comm->size();
fname << ".csv";
Inform csvout(NULL, fname.str().c_str(), Inform::APPEND);
csvout.precision(10);
csvout.setf(std::ios::scientific, std::ios::floatfield);
if(time_m == 0.0) {
csvout << "time, Kinetic energy, Rho_norm2, Ex_norm2, Ey_norm2, Ez_norm2" << endl;
}
csvout << time_m << " "
<< gEnergy << " "
<< rhoNorm_m << " "
<< normE[0] << " "
<< normE[1] << " "
<< normE[2] << endl;
}
Ippl::Comm->barrier();
}
void dumpLandau() {
const int nghostE = E_m.getNghost();
auto Eview = E_m.getView();
double fieldEnergy, ExAmp;
using mdrange_type = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
double temp = 0.0;
Kokkos::parallel_reduce("Ex inner product",
mdrange_type({nghostE, nghostE, nghostE},
{Eview.extent(0) - nghostE,
Eview.extent(1) - nghostE,
Eview.extent(2) - nghostE}),
KOKKOS_LAMBDA(const size_t i, const size_t j,
const size_t k, double& valL)
{
double myVal = std::pow(Eview(i, j, k)[0], 2);
valL += myVal;
}, Kokkos::Sum<double>(temp));
double globaltemp = 0.0;
MPI_Reduce(&temp, &globaltemp, 1, MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
fieldEnergy = globaltemp * hr_m[0] * hr_m[1] * hr_m[2];
double tempMax = 0.0;
Kokkos::parallel_reduce("Ex max norm",
mdrange_type({nghostE, nghostE, nghostE},
{Eview.extent(0) - nghostE,
Eview.extent(1) - nghostE,
Eview.extent(2) - nghostE}),
KOKKOS_LAMBDA(const size_t i, const size_t j,
const size_t k, double& valL)
{
double myVal = std::fabs(Eview(i, j, k)[0]);
if(myVal > valL) valL = myVal;
}, Kokkos::Max<double>(tempMax));
ExAmp = 0.0;
MPI_Reduce(&tempMax, &ExAmp, 1, MPI_DOUBLE, MPI_MAX, 0, Ippl::getComm());
if (Ippl::Comm->rank() == 0) {
std::stringstream fname;
fname << "data/FieldLandau_";
fname << Ippl::Comm->size();
fname << ".csv";
Inform csvout(NULL, fname.str().c_str(), Inform::APPEND);
csvout.precision(10);
csvout.setf(std::ios::scientific, std::ios::floatfield);
if(time_m == 0.0) {
csvout << "time, Ex_field_energy, Ex_max_norm" << endl;
}
csvout << time_m << " "
<< fieldEnergy << " "
<< ExAmp << endl;
}
Ippl::Comm->barrier();
}
void dumpLandauParticle(size_type totalP) {
auto Eview = E.getView();
double fieldEnergy, ExAmp;
double temp = 0.0;
Kokkos::parallel_reduce("Ex energy", this->getLocalNum(),
KOKKOS_LAMBDA(const int i, double& valL){
double myVal = Eview(i)[0] * Eview(i)[0];
valL += myVal;
}, Kokkos::Sum<double>(temp));
double globaltemp = 0.0;
MPI_Reduce(&temp, &globaltemp, 1, MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
double volume = (rmax_m[0] - rmin_m[0]) * (rmax_m[1] - rmin_m[1]) * (rmax_m[2] - rmin_m[2]);
fieldEnergy = globaltemp * volume / totalP ;
double tempMax = 0.0;
Kokkos::parallel_reduce("Ex max norm", this->getLocalNum(),
KOKKOS_LAMBDA(const size_t i, double& valL)
{
double myVal = std::fabs(Eview(i)[0]);
if(myVal > valL) valL = myVal;
}, Kokkos::Max<double>(tempMax));
ExAmp = 0.0;
MPI_Reduce(&tempMax, &ExAmp, 1, MPI_DOUBLE, MPI_MAX, 0, Ippl::getComm());
if (Ippl::Comm->rank() == 0) {
std::stringstream fname;
fname << "data/FieldLandau_";
fname << Ippl::Comm->size();
fname << ".csv";
Inform csvout(NULL, fname.str().c_str(), Inform::APPEND);
csvout.precision(10);
csvout.setf(std::ios::scientific, std::ios::floatfield);
if(time_m == 0.0) {
csvout << "time, Ex_field_energy, Ex_max_norm" << endl;
}
csvout << time_m << " "
<< fieldEnergy << " "
<< ExAmp << endl;
}
Ippl::Comm->barrier();
}
void dumpEnergy(size_type /*totalP*/) {
double potentialEnergy, kineticEnergy;
//auto Eview = E.getView();
double temp = 0.0;
//Kokkos::parallel_reduce("Potential energy", this->getLocalNum(),
// KOKKOS_LAMBDA(const int i, double& valL){
// double myVal = dot(Eview(i), Eview(i)).apply();
// valL += myVal;
// }, Kokkos::Sum<double>(temp));
double globaltemp = 0.0;
//MPI_Reduce(&temp, &globaltemp, 1, MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
//double volume = (rmax_m[0] - rmin_m[0]) * (rmax_m[1] - rmin_m[1]) * (rmax_m[2] - rmin_m[2]);
//potentialEnergy = 0.5 * globaltemp * volume / totalP ;
rho_m = dot(E_m, E_m);
potentialEnergy = 0.5 * hr_m[0] * hr_m[1] * hr_m[2] * rho_m.sum();
auto Pview = P.getView();
auto qView = q.getView();
temp = 0.0;
Kokkos::parallel_reduce("Kinetic Energy", this->getLocalNum(),
KOKKOS_LAMBDA(const int i, double& valL){
double myVal = dot(Pview(i), Pview(i)).apply();
myVal *= -qView(i);
valL += myVal;
}, Kokkos::Sum<double>(temp));
temp *= 0.5;
globaltemp = 0.0;
MPI_Reduce(&temp, &globaltemp, 1, MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
kineticEnergy = globaltemp;
if (Ippl::Comm->rank() == 0) {
std::stringstream fname;
fname << "data/Energy_";
fname << Ippl::Comm->size();
fname << ".csv";
Inform csvout(NULL, fname.str().c_str(), Inform::APPEND);
csvout.precision(10);
csvout.setf(std::ios::scientific, std::ios::floatfield);
if(time_m == 0.0) {
csvout << "time, Potential energy, Kinetic energy, Total energy" << endl;
}
csvout << time_m << " "
<< potentialEnergy << " "
<< kineticEnergy << " "
<< potentialEnergy + kineticEnergy << endl;
}
Ippl::Comm->barrier();
}
void dumpBumponTail() {
const int nghostE = E_m.getNghost();
auto Eview = E_m.getView();
double fieldEnergy, EzAmp;
using mdrange_type = Kokkos::MDRangePolicy<Kokkos::Rank<3>>;
double temp = 0.0;
Kokkos::parallel_reduce("Ex inner product",
mdrange_type({nghostE, nghostE, nghostE},
{Eview.extent(0) - nghostE,
Eview.extent(1) - nghostE,
Eview.extent(2) - nghostE}),
KOKKOS_LAMBDA(const size_t i, const size_t j,
const size_t k, double& valL)
{
double myVal = std::pow(Eview(i, j, k)[2], 2);
valL += myVal;
}, Kokkos::Sum<double>(temp));
double globaltemp = 0.0;
MPI_Reduce(&temp, &globaltemp, 1, MPI_DOUBLE, MPI_SUM, 0, Ippl::getComm());
fieldEnergy = globaltemp * hr_m[0] * hr_m[1] * hr_m[2];
double tempMax = 0.0;
Kokkos::parallel_reduce("Ex max norm",
mdrange_type({nghostE, nghostE, nghostE},
{Eview.extent(0) - nghostE,
Eview.extent(1) - nghostE,
Eview.extent(2) - nghostE}),
KOKKOS_LAMBDA(const size_t i, const size_t j,
const size_t k, double& valL)
{
double myVal = std::fabs(Eview(i, j, k)[2]);
if(myVal > valL) valL = myVal;
}, Kokkos::Max<double>(tempMax));
EzAmp = 0.0;
MPI_Reduce(&tempMax, &EzAmp, 1, MPI_DOUBLE, MPI_MAX, 0, Ippl::getComm());
if (Ippl::Comm->rank() == 0) {
std::stringstream fname;
fname << "data/FieldBumponTail_";
fname << Ippl::Comm->size();
fname << ".csv";
Inform csvout(NULL, fname.str().c_str(), Inform::APPEND);
csvout.precision(10);
csvout.setf(std::ios::scientific, std::ios::floatfield);
if(time_m == 0.0) {
csvout << "time, Ez_field_energy, Ez_max_norm" << endl;
}
csvout << time_m << " "
<< fieldEnergy << " "
<< EzAmp << endl;
}
Ippl::Comm->barrier();
}
void dumpParticleData() {
typename ParticleAttrib<Vector_t>::HostMirror R_host = this->R.getHostMirror();
typename ParticleAttrib<Vector_t>::HostMirror P_host = this->P.getHostMirror();
Kokkos::deep_copy(R_host, this->R.getView());
Kokkos::deep_copy(P_host, P.getView());
std::stringstream pname;
pname << "data/ParticleIC_";
pname << Ippl::Comm->rank();
pname << ".csv";
Inform pcsvout(NULL, pname.str().c_str(), Inform::OVERWRITE, Ippl::Comm->rank());
pcsvout.precision(10);
pcsvout.setf(std::ios::scientific, std::ios::floatfield);
pcsvout << "R_x, R_y, R_z, V_x, V_y, V_z" << endl;
for (size_type i = 0; i< this->getLocalNum(); i++) {
pcsvout << R_host(i)[0] << " "
<< R_host(i)[1] << " "
<< R_host(i)[2] << " "
<< P_host(i)[0] << " "
<< P_host(i)[1] << " "
<< P_host(i)[2] << endl;
}
Ippl::Comm->barrier();
}
void dumpLocalDomains(const FieldLayout_t& fl, const unsigned int step) {
if (Ippl::Comm->rank() == 0) {
const typename FieldLayout_t::host_mirror_type domains = fl.getHostLocalDomains();
std::ofstream myfile;
myfile.open("data/domains" + std::to_string(step) + ".txt");
for (unsigned int i = 0; i < domains.size(); ++i) {
myfile << domains[i][0].first() << " " << domains[i][1].first() << " " << domains[i][2].first() << " "
<< domains[i][0].first() << " " << domains[i][1].last() << " " << domains[i][2].first() << " "
<< domains[i][0].last() << " " << domains[i][1].first() << " " << domains[i][2].first() << " "
<< domains[i][0].first() << " " << domains[i][1].first() << " " << domains[i][2].last()
<< "\n";
}
myfile.close();
}
Ippl::Comm->barrier();
}
private:
void setBCAllPeriodic() {
this->setParticleBC(ippl::BC::PERIODIC);
}
};