-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathMultiParticleContainer.cpp
1707 lines (1469 loc) · 64.2 KB
/
MultiParticleContainer.cpp
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2019-2020 Andrew Myers, Ann Almgren, Axel Huebl
* David Grote, Jean-Luc Vay, Luca Fedeli
* Mathieu Lobet, Maxence Thevenet, Neil Zaim
* Remi Lehe, Revathi Jambunathan, Weiqun Zhang
* Yinjian Zhao
*
*
* This file is part of WarpX.
*
* License: BSD-3-Clause-LBNL
*/
#include "MultiParticleContainer.H"
#include "Fields.H"
#include "Particles/ElementaryProcess/Ionization.H"
#ifdef WARPX_QED
# include "Particles/ElementaryProcess/QEDInternals/BreitWheelerEngineWrapper.H"
# include "Particles/ElementaryProcess/QEDInternals/QuantumSyncEngineWrapper.H"
# include "Particles/ElementaryProcess/QEDSchwingerProcess.H"
# include "Particles/ElementaryProcess/QEDPairGeneration.H"
# include "Particles/ElementaryProcess/QEDPhotonEmission.H"
#endif
#include "Particles/LaserParticleContainer.H"
#include "Particles/ParticleCreation/FilterCopyTransform.H"
#ifdef WARPX_QED
# include "Particles/ParticleCreation/FilterCreateTransformFromFAB.H"
#endif
#include "Particles/ParticleCreation/SmartCopy.H"
#include "Particles/ParticleCreation/SmartCreate.H"
#include "Particles/ParticleCreation/SmartUtils.H"
#include "Particles/PhotonParticleContainer.H"
#include "Particles/PhysicalParticleContainer.H"
#include "Particles/RigidInjectedParticleContainer.H"
#include "Particles/WarpXParticleContainer.H"
#include "SpeciesPhysicalProperties.H"
#include "Utils/Parser/ParserUtils.H"
#include "Utils/TextMsg.H"
#include "Utils/WarpXAlgorithmSelection.H"
#include "Utils/WarpXProfilerWrapper.H"
#include "Utils/WarpXUtil.H"
#include "EmbeddedBoundary/ParticleScraper.H"
#include "EmbeddedBoundary/ParticleBoundaryProcess.H"
#include "WarpX.H"
#include <ablastr/fields/MultiFabRegister.H>
#include <ablastr/utils/Communication.H>
#include <ablastr/warn_manager/WarnManager.H>
#include <AMReX.H>
#include <AMReX_Array.H>
#include <AMReX_Array4.H>
#include <AMReX_BoxArray.H>
#include <AMReX_DistributionMapping.H>
#include <AMReX_FArrayBox.H>
#include <AMReX_FabArray.H>
#include <AMReX_Geometry.H>
#include <AMReX_GpuAtomic.H>
#include <AMReX_GpuDevice.H>
#include <AMReX_IntVect.H>
#include <AMReX_LayoutData.H>
#include <AMReX_MultiFab.H>
#include <AMReX_PODVector.H>
#include <AMReX_ParIter.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_ParmParse.H>
#include <AMReX_ParticleTile.H>
#include <AMReX_Particles.H>
#include <AMReX_Print.H>
#include <AMReX_StructOfArrays.H>
#include <AMReX_Utility.H>
#include <AMReX_Vector.H>
#include <algorithm>
#include <cmath>
#include <limits>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace amrex;
using warpx::fields::FieldType;
namespace
{
/** A little collection to transport six Array4 that point to the EM fields */
struct MyFieldList
{
Array4< amrex::Real const > Ex, Ey, Ez, Bx, By, Bz;
};
}
MultiParticleContainer::MultiParticleContainer (AmrCore* amr_core)
{
ReadParameters();
auto const nspecies = static_cast<int>(species_names.size());
auto const nlasers = static_cast<int>(lasers_names.size());
allcontainers.resize(nspecies + nlasers);
for (int i = 0; i < nspecies; ++i) {
if (species_types[i] == PCTypes::Physical) {
allcontainers[i] = std::make_unique<PhysicalParticleContainer>(amr_core, i, species_names[i]);
}
else if (species_types[i] == PCTypes::RigidInjected) {
allcontainers[i] = std::make_unique<RigidInjectedParticleContainer>(amr_core, i, species_names[i]);
}
else if (species_types[i] == PCTypes::Photon) {
allcontainers[i] = std::make_unique<PhotonParticleContainer>(amr_core, i, species_names[i]);
}
allcontainers[i]->m_deposit_on_main_grid = m_deposit_on_main_grid[i];
allcontainers[i]->m_gather_from_main_grid = m_gather_from_main_grid[i];
}
for (int i = nspecies; i < nspecies+nlasers; ++i) {
allcontainers[i] = std::make_unique<LaserParticleContainer>(amr_core, i, lasers_names[i-nspecies]);
allcontainers[i]->m_deposit_on_main_grid = m_laser_deposit_on_main_grid[i-nspecies];
}
pc_tmp = std::make_unique<PhysicalParticleContainer>(amr_core);
// Setup particle collisions
collisionhandler = std::make_unique<CollisionHandler>(this);
}
void
MultiParticleContainer::ReadParameters ()
{
static bool initialized = false;
if (!initialized)
{
const ParmParse pp_particles("particles");
// default values of E_external_particle and B_external_particle
// are used to set the E and B field when "constant" or "parser"
// is not explicitly used in the input
pp_particles.query("B_ext_particle_init_style", m_B_ext_particle_s);
std::transform(m_B_ext_particle_s.begin(),
m_B_ext_particle_s.end(),
m_B_ext_particle_s.begin(),
::tolower);
pp_particles.query("E_ext_particle_init_style", m_E_ext_particle_s);
std::transform(m_E_ext_particle_s.begin(),
m_E_ext_particle_s.end(),
m_E_ext_particle_s.begin(),
::tolower);
// if the input string for B_ext_particle_s is
// "parse_b_ext_particle_function" then the mathematical expression
// for the Bx_, By_, Bz_external_particle_function(x,y,z)
// must be provided in the input file.
if (m_B_ext_particle_s == "parse_b_ext_particle_function") {
// store the mathematical expression as string
std::string str_Bx_ext_particle_function;
std::string str_By_ext_particle_function;
std::string str_Bz_ext_particle_function;
utils::parser::Store_parserString(
pp_particles, "Bx_external_particle_function(x,y,z,t)",
str_Bx_ext_particle_function);
utils::parser::Store_parserString(
pp_particles, "By_external_particle_function(x,y,z,t)",
str_By_ext_particle_function);
utils::parser::Store_parserString(
pp_particles, "Bz_external_particle_function(x,y,z,t)",
str_Bz_ext_particle_function);
// Parser for B_external on the particle
m_Bx_particle_parser = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_Bx_ext_particle_function,{"x","y","z","t"}));
m_By_particle_parser = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_By_ext_particle_function,{"x","y","z","t"}));
m_Bz_particle_parser = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_Bz_ext_particle_function,{"x","y","z","t"}));
}
// if the input string for E_ext_particle_s is
// "parse_e_ext_particle_function" then the mathematical expression
// for the Ex_, Ey_, Ez_external_particle_function(x,y,z)
// must be provided in the input file.
if (m_E_ext_particle_s == "parse_e_ext_particle_function") {
// store the mathematical expression as string
std::string str_Ex_ext_particle_function;
std::string str_Ey_ext_particle_function;
std::string str_Ez_ext_particle_function;
utils::parser::Store_parserString(
pp_particles, "Ex_external_particle_function(x,y,z,t)",
str_Ex_ext_particle_function);
utils::parser::Store_parserString(
pp_particles, "Ey_external_particle_function(x,y,z,t)",
str_Ey_ext_particle_function);
utils::parser::Store_parserString(
pp_particles, "Ez_external_particle_function(x,y,z,t)",
str_Ez_ext_particle_function);
// Parser for E_external on the particle
m_Ex_particle_parser = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_Ex_ext_particle_function,{"x","y","z","t"}));
m_Ey_particle_parser = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_Ey_ext_particle_function,{"x","y","z","t"}));
m_Ez_particle_parser = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_Ez_ext_particle_function,{"x","y","z","t"}));
}
// if the input string for E_ext_particle_s or B_ext_particle_s is
// "repeated_plasma_lens" then the plasma lens properties
// must be provided in the input file.
if (m_E_ext_particle_s == "repeated_plasma_lens" ||
m_B_ext_particle_s == "repeated_plasma_lens") {
utils::parser::getWithParser(
pp_particles, "repeated_plasma_lens_period",
m_repeated_plasma_lens_period);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(m_repeated_plasma_lens_period > 0._rt,
"The period of the repeated plasma lens must be greater than zero");
utils::parser::getArrWithParser(
pp_particles, "repeated_plasma_lens_starts",
h_repeated_plasma_lens_starts);
utils::parser::getArrWithParser(
pp_particles, "repeated_plasma_lens_lengths",
h_repeated_plasma_lens_lengths);
const auto n_lenses = static_cast<int>(h_repeated_plasma_lens_starts.size());
d_repeated_plasma_lens_starts.resize(n_lenses);
d_repeated_plasma_lens_lengths.resize(n_lenses);
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
h_repeated_plasma_lens_starts.begin(), h_repeated_plasma_lens_starts.end(),
d_repeated_plasma_lens_starts.begin());
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
h_repeated_plasma_lens_lengths.begin(), h_repeated_plasma_lens_lengths.end(),
d_repeated_plasma_lens_lengths.begin());
h_repeated_plasma_lens_strengths_E.resize(n_lenses);
h_repeated_plasma_lens_strengths_B.resize(n_lenses);
if (m_E_ext_particle_s == "repeated_plasma_lens") {
utils::parser::getArrWithParser(
pp_particles, "repeated_plasma_lens_strengths_E",
h_repeated_plasma_lens_strengths_E);
}
if (m_B_ext_particle_s == "repeated_plasma_lens") {
utils::parser::getArrWithParser(
pp_particles, "repeated_plasma_lens_strengths_B",
h_repeated_plasma_lens_strengths_B);
}
d_repeated_plasma_lens_strengths_E.resize(n_lenses);
d_repeated_plasma_lens_strengths_B.resize(n_lenses);
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
h_repeated_plasma_lens_strengths_E.begin(), h_repeated_plasma_lens_strengths_E.end(),
d_repeated_plasma_lens_strengths_E.begin());
amrex::Gpu::copyAsync(amrex::Gpu::hostToDevice,
h_repeated_plasma_lens_strengths_B.begin(), h_repeated_plasma_lens_strengths_B.end(),
d_repeated_plasma_lens_strengths_B.begin());
amrex::Gpu::synchronize();
}
// particle species
pp_particles.queryarr("species_names", species_names);
auto const nspecies = species_names.size();
if (nspecies > 0) {
// Get species to deposit on main grid
m_deposit_on_main_grid.resize(nspecies, false);
std::vector<std::string> tmp;
pp_particles.queryarr("deposit_on_main_grid", tmp);
for (auto const& name : tmp) {
auto it = std::find(species_names.begin(), species_names.end(), name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
it != species_names.end(),
"species '" + name
+ "' in particles.deposit_on_main_grid must be part of particles.species_names");
const auto i = static_cast<int>(std::distance(species_names.begin(), it));
m_deposit_on_main_grid[i] = true;
}
m_gather_from_main_grid.resize(nspecies, false);
std::vector<std::string> tmp_gather;
pp_particles.queryarr("gather_from_main_grid", tmp_gather);
for (auto const& name : tmp_gather) {
auto it = std::find(species_names.begin(), species_names.end(), name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
it != species_names.end(),
"species '" + name
+ "' in particles.gather_from_main_grid must be part of particles.species_names");
const auto i = static_cast<int>(std::distance(species_names.begin(), it));
m_gather_from_main_grid.at(i) = true;
}
species_types.resize(nspecies, PCTypes::Physical);
// Get rigid-injected species
std::vector<std::string> rigid_injected_species;
pp_particles.queryarr("rigid_injected_species", rigid_injected_species);
if (!rigid_injected_species.empty()) {
for (auto const& name : rigid_injected_species) {
auto it = std::find(species_names.begin(), species_names.end(), name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
it != species_names.end(),
"species '" + name
+ "' in particles.rigid_injected_species must be part of particles.species_names");
const auto i = static_cast<int>(std::distance(species_names.begin(), it));
species_types[i] = PCTypes::RigidInjected;
}
}
// Get photon species
std::vector<std::string> photon_species;
pp_particles.queryarr("photon_species", photon_species);
if (!photon_species.empty()) {
for (auto const& name : photon_species) {
auto it = std::find(species_names.begin(), species_names.end(), name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
it != species_names.end(),
"species '" + name
+ "' in particles.photon_species must be part of particles.species_names");
const auto i = static_cast<int>(std::distance(species_names.begin(), it));
species_types[i] = PCTypes::Photon;
}
}
}
pp_particles.query("use_fdtd_nci_corr", WarpX::use_fdtd_nci_corr);
#ifdef WARPX_DIM_RZ
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(WarpX::use_fdtd_nci_corr==0,
"ERROR: use_fdtd_nci_corr is not supported in RZ");
#endif
#ifdef WARPX_DIM_1D_Z
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(WarpX::use_fdtd_nci_corr==0,
"ERROR: use_fdtd_nci_corr is not supported in 1D");
#endif
const ParmParse pp_lasers("lasers");
pp_lasers.queryarr("names", lasers_names);
auto const nlasers = lasers_names.size();
// Get lasers to deposit on main grid
m_laser_deposit_on_main_grid.resize(nlasers, false);
std::vector<std::string> tmp;
pp_lasers.queryarr("deposit_on_main_grid", tmp);
for (auto const& name : tmp) {
auto it = std::find(lasers_names.begin(), lasers_names.end(), name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
it != lasers_names.end(),
"laser '" + name
+ "' in lasers.deposit_on_main_grid must be part of lasers.lasers_names");
const auto i = static_cast<int>(std::distance(lasers_names.begin(), it));
m_laser_deposit_on_main_grid[i] = true;
}
#ifdef WARPX_QED
const ParmParse pp_warpx("warpx");
pp_warpx.query("do_qed_schwinger", m_do_qed_schwinger);
if (m_do_qed_schwinger) {
const ParmParse pp_qed_schwinger("qed_schwinger");
pp_qed_schwinger.get("ele_product_species", m_qed_schwinger_ele_product_name);
pp_qed_schwinger.get("pos_product_species", m_qed_schwinger_pos_product_name);
#if defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
utils::parser::getWithParser(
pp_qed_schwinger, "y_size",m_qed_schwinger_y_size);
#endif
utils::parser::queryWithParser(
pp_qed_schwinger, "threshold_poisson_gaussian",
m_qed_schwinger_threshold_poisson_gaussian);
utils::parser::queryWithParser(
pp_qed_schwinger, "xmin", m_qed_schwinger_xmin);
utils::parser::queryWithParser(
pp_qed_schwinger, "xmax", m_qed_schwinger_xmax);
#if defined(WARPX_DIM_3D)
utils::parser::queryWithParser(
pp_qed_schwinger, "ymin", m_qed_schwinger_ymin);
utils::parser::queryWithParser(
pp_qed_schwinger, "ymax", m_qed_schwinger_ymax);
#endif
utils::parser::queryWithParser(
pp_qed_schwinger, "zmin", m_qed_schwinger_zmin);
utils::parser::queryWithParser(
pp_qed_schwinger, "zmax", m_qed_schwinger_zmax);
}
#endif
initialized = true;
}
}
WarpXParticleContainer&
MultiParticleContainer::GetParticleContainerFromName (const std::string& name) const
{
auto it = std::find(species_names.begin(), species_names.end(), name);
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
it != species_names.end(),
"unknown species name");
const auto i = static_cast<int>(std::distance(species_names.begin(), it));
return *allcontainers[i];
}
amrex::ParticleReal
MultiParticleContainer::maxParticleVelocity() {
amrex::ParticleReal max_v = 0.0_prt;
for (const auto &pc : allcontainers) {
max_v = std::max(max_v, pc->maxParticleVelocity());
}
return max_v;
}
void
MultiParticleContainer::AllocData ()
{
for (auto& pc : allcontainers) {
pc->AllocData();
}
pc_tmp->AllocData();
}
void
MultiParticleContainer::InitData ()
{
InitMultiPhysicsModules();
for (auto& pc : allcontainers) {
pc->InitData();
}
pc_tmp->InitData();
}
void
MultiParticleContainer::PostRestart ()
{
InitMultiPhysicsModules();
for (auto& pc : allcontainers) {
pc->PostRestart();
}
pc_tmp->PostRestart();
}
void
MultiParticleContainer::InitMultiPhysicsModules ()
{
// Init ionization module here instead of in the MultiParticleContainer
// constructor because dt is required to compute ionization rate pre-factors
for (auto& pc : allcontainers) {
pc->InitIonizationModule();
}
// For each species, get the ID of its product species.
// This is used for ionization and pair creation processes.
mapSpeciesProduct();
CheckIonizationProductSpecies();
#ifdef WARPX_QED
CheckQEDProductSpecies();
InitQED();
#endif
}
void
MultiParticleContainer::Evolve (ablastr::fields::MultiFabRegister& fields,
int lev,
std::string const& current_fp_string,
Real t, Real dt, DtType a_dt_type, bool skip_deposition,
PushType push_type)
{
if (! skip_deposition) {
using ablastr::fields::Direction;
fields.get(current_fp_string, Direction{0}, lev)->setVal(0.0);
fields.get(current_fp_string, Direction{1}, lev)->setVal(0.0);
fields.get(current_fp_string, Direction{2}, lev)->setVal(0.0);
if (fields.has(FieldType::current_buf, Direction{0}, lev)) { fields.get(FieldType::current_buf, Direction{0}, lev)->setVal(0.0); }
if (fields.has(FieldType::current_buf, Direction{1}, lev)) { fields.get(FieldType::current_buf, Direction{1}, lev)->setVal(0.0); }
if (fields.has(FieldType::current_buf, Direction{2}, lev)) { fields.get(FieldType::current_buf, Direction{2}, lev)->setVal(0.0); }
if (fields.has(FieldType::rho_fp, lev)) { fields.get(FieldType::rho_fp, lev)->setVal(0.0); }
if (fields.has(FieldType::rho_buf, lev)) { fields.get(FieldType::rho_buf, lev)->setVal(0.0); }
}
for (auto& pc : allcontainers) {
pc->Evolve(fields, lev, current_fp_string, t, dt, a_dt_type, skip_deposition, push_type);
}
}
void
MultiParticleContainer::PushX (Real dt)
{
for (auto& pc : allcontainers) {
pc->PushX(dt);
}
}
void
MultiParticleContainer::PushP (int lev, Real dt,
const MultiFab& Ex, const MultiFab& Ey, const MultiFab& Ez,
const MultiFab& Bx, const MultiFab& By, const MultiFab& Bz)
{
for (auto& pc : allcontainers) {
pc->PushP(lev, dt, Ex, Ey, Ez, Bx, By, Bz);
}
}
std::unique_ptr<MultiFab>
MultiParticleContainer::GetZeroChargeDensity (const int lev)
{
const WarpX& warpx = WarpX::GetInstance();
BoxArray nba = warpx.boxArray(lev);
const DistributionMapping dmap = warpx.DistributionMap(lev);
const int ng_rho = warpx.get_ng_depos_rho().max();
#ifdef WARPX_DIM_RZ
const bool is_PSATD_RZ =
(WarpX::electromagnetic_solver_id == ElectromagneticSolverAlgo::PSATD);
#else
const bool is_PSATD_RZ = false;
#endif
if( !is_PSATD_RZ ) {
nba.surroundingNodes();
}
auto zero_rho = std::make_unique<MultiFab>(nba, dmap, WarpX::ncomps, ng_rho);
zero_rho->setVal(amrex::Real(0.0));
return zero_rho;
}
void
MultiParticleContainer::DepositCurrent (
ablastr::fields::MultiLevelVectorField const & J,
const amrex::Real dt, const amrex::Real relative_time)
{
// Reset the J arrays
for (const auto& J_lev : J)
{
J_lev[0]->setVal(0.0_rt);
J_lev[1]->setVal(0.0_rt);
J_lev[2]->setVal(0.0_rt);
}
// Call the deposition kernel for each species
for (auto& pc : allcontainers)
{
pc->DepositCurrent(J, dt, relative_time);
}
#ifdef WARPX_DIM_RZ
for (int lev = 0; lev < J.size(); ++lev)
{
WarpX::GetInstance().ApplyInverseVolumeScalingToCurrentDensity(
J[lev][0], J[lev][1], J[lev][2], lev);
}
#endif
}
void
MultiParticleContainer::DepositCharge (
const ablastr::fields::MultiLevelScalarField& rho,
const amrex::Real relative_time)
{
// Reset the rho array
for (const auto& rho_lev : rho)
{
rho_lev->setVal(0.0_rt);
}
// Push the particles in time, if needed
if (relative_time != 0.) { PushX(relative_time); }
bool const local = true;
bool const reset = false;
bool const apply_boundary_and_scale_volume = false;
bool const interpolate_across_levels = false;
// Call the deposition kernel for each species
for (auto& pc : allcontainers)
{
if (pc->do_not_deposit) { continue; }
pc->DepositCharge(rho, local, reset, apply_boundary_and_scale_volume,
interpolate_across_levels);
}
// Push the particles back in time
if (relative_time != 0.) { PushX(-relative_time); }
#ifdef WARPX_DIM_RZ
for (int lev = 0; lev < rho.size(); ++lev)
{
WarpX::GetInstance().ApplyInverseVolumeScalingToChargeDensity(rho[lev], lev);
}
#endif
}
std::unique_ptr<MultiFab>
MultiParticleContainer::GetChargeDensity (int lev, bool local)
{
std::unique_ptr<MultiFab> rho = GetZeroChargeDensity(lev);
for (auto& container : allcontainers) {
if (container->do_not_deposit) { continue; }
const std::unique_ptr<MultiFab> rhoi = container->GetChargeDensity(lev, true);
MultiFab::Add(*rho, *rhoi, 0, 0, rho->nComp(), rho->nGrowVect());
}
if (!local) {
const Geometry& gm = allcontainers[0]->Geom(lev);
// Possible performance optimization:
// pass less than `rho->nGrowVect()` in the fifth input variable `dst_ng`
ablastr::utils::communication::SumBoundary(
*rho, 0, rho->nComp(), rho->nGrowVect(), rho->nGrowVect(),
WarpX::do_single_precision_comms, gm.periodicity());
}
return rho;
}
void
MultiParticleContainer::SortParticlesByBin (
const amrex::IntVect& bin_size,
const bool sort_particles_for_deposition,
const amrex::IntVect& sort_idx_type)
{
for (auto& pc : allcontainers) {
if (sort_particles_for_deposition) {
pc->SortParticlesForDeposition(sort_idx_type);
} else {
pc->SortParticlesByBin(bin_size);
}
}
}
void
MultiParticleContainer::Redistribute ()
{
for (auto& pc : allcontainers) {
pc->Redistribute();
}
}
void
MultiParticleContainer::defineAllParticleTiles ()
{
for (auto& pc : allcontainers) {
pc->defineAllParticleTiles();
}
}
void
MultiParticleContainer::deleteInvalidParticles ()
{
for (auto& pc : allcontainers) {
pc->deleteInvalidParticles();
}
}
void
MultiParticleContainer::RedistributeLocal (const int num_ghost)
{
for (auto& pc : allcontainers) {
pc->Redistribute(0, 0, 0, num_ghost);
}
}
void
MultiParticleContainer::ApplyBoundaryConditions ()
{
for (auto& pc : allcontainers) {
pc->ApplyBoundaryConditions();
}
}
Vector<Long>
MultiParticleContainer::GetZeroParticlesInGrid (const int lev) const
{
const WarpX& warpx = WarpX::GetInstance();
const auto num_boxes = static_cast<int>(warpx.boxArray(lev).size());
Vector<Long> r(num_boxes, 0);
return r;
}
Vector<Long>
MultiParticleContainer::NumberOfParticlesInGrid (int lev) const
{
if (allcontainers.empty())
{
Vector<Long> r = GetZeroParticlesInGrid(lev);
return r;
}
else
{
const bool only_valid=true, only_local=true;
Vector<Long> r = allcontainers[0]->NumberOfParticlesInGrid(lev,only_valid,only_local);
for (unsigned i = 1, n = allcontainers.size(); i < n; ++i) {
const auto& ri = allcontainers[i]->NumberOfParticlesInGrid(lev,only_valid,only_local);
for (unsigned j=0, m=ri.size(); j<m; ++j) {
r[j] += ri[j];
}
}
ParallelDescriptor::ReduceLongSum(r.data(),static_cast<int>(r.size()));
return r;
}
}
void
MultiParticleContainer::Increment (MultiFab& mf, int lev)
{
for (auto& pc : allcontainers) {
pc->Increment(mf,lev);
}
}
void
MultiParticleContainer::SetParticleBoxArray (int lev, BoxArray& new_ba)
{
for (auto& pc : allcontainers) {
pc->SetParticleBoxArray(lev,new_ba);
}
}
void
MultiParticleContainer::SetParticleDistributionMap (int lev, DistributionMapping& new_dm)
{
for (auto& pc : allcontainers) {
pc->SetParticleDistributionMap(lev,new_dm);
}
}
/* \brief Continuous injection for particles initially outside of the domain.
* \param injection_box: Domain where new particles should be injected.
* Loop over all WarpXParticleContainer in MultiParticleContainer and
* calls virtual function ContinuousInjection.
*/
void
MultiParticleContainer::ContinuousInjection (const RealBox& injection_box) const
{
for (const auto& pc : allcontainers){
if (pc->do_continuous_injection){
pc->ContinuousInjection(injection_box);
}
}
}
void
MultiParticleContainer::UpdateAntennaPosition (const amrex::Real dt) const
{
for (const auto& pc : allcontainers){
if (pc->do_continuous_injection){
pc->UpdateAntennaPosition(dt);
}
}
}
int
MultiParticleContainer::doContinuousInjection () const
{
int warpx_do_continuous_injection = 0;
for (const auto& pc : allcontainers){
if (pc->do_continuous_injection){
warpx_do_continuous_injection = 1;
}
}
return warpx_do_continuous_injection;
}
/* \brief Continuous injection of a flux of particles
* Loop over all WarpXParticleContainer in MultiParticleContainer and
* calls virtual function ContinuousFluxInjection.
*/
void
MultiParticleContainer::ContinuousFluxInjection (amrex::Real t, amrex::Real dt) const
{
for (const auto& pc : allcontainers){
pc->ContinuousFluxInjection(t, dt);
}
}
/* \brief Get ID of product species of each species.
* The users specifies the name of the product species,
* this routine get its ID.
*/
void
MultiParticleContainer::mapSpeciesProduct ()
{
for (int i=0; i < static_cast<int>(species_names.size()); i++){
auto& pc = allcontainers[i];
// If species pc has ionization on, find species with name
// pc->ionization_product_name and store its ID into
// pc->ionization_product.
if (pc->do_field_ionization){
const int i_product = getSpeciesID(pc->ionization_product_name);
pc->ionization_product = i_product;
}
#ifdef WARPX_QED
if (pc->has_breit_wheeler()){
const int i_product_ele = getSpeciesID(
pc->m_qed_breit_wheeler_ele_product_name);
pc->m_qed_breit_wheeler_ele_product = i_product_ele;
const int i_product_pos = getSpeciesID(
pc->m_qed_breit_wheeler_pos_product_name);
pc->m_qed_breit_wheeler_pos_product = i_product_pos;
}
if(pc->has_quantum_sync()){
const int i_product_phot = getSpeciesID(
pc->m_qed_quantum_sync_phot_product_name);
pc->m_qed_quantum_sync_phot_product = i_product_phot;
}
#endif
}
#ifdef WARPX_QED
if (m_do_qed_schwinger) {
m_qed_schwinger_ele_product =
getSpeciesID(m_qed_schwinger_ele_product_name);
m_qed_schwinger_pos_product =
getSpeciesID(m_qed_schwinger_pos_product_name);
}
#endif
}
/* \brief Given a species name, return its ID.
*/
int
MultiParticleContainer::getSpeciesID (const std::string& product_str) const
{
auto species_and_lasers_names = GetSpeciesAndLasersNames();
int i_product = 0;
bool found = false;
// Loop over species
for (int i=0; i < static_cast<int>(species_and_lasers_names.size()); i++){
// If species name matches, store its ID
// into i_product
if (species_and_lasers_names[i] == product_str){
found = true;
i_product = i;
}
}
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
found != 0,
"could not find the ID of product species '"
+ product_str + "'" + ". Wrong name?");
return i_product;
}
void
MultiParticleContainer::SetDoBackTransformedParticles (const bool do_back_transformed_particles) {
m_do_back_transformed_particles = do_back_transformed_particles;
}
void
MultiParticleContainer::SetDoBackTransformedParticles (const std::string& species_name, const bool do_back_transformed_particles) {
auto species_names_list = GetSpeciesNames();
bool found = false;
// Loop over species
for (int i = 0; i < static_cast<int>(species_names.size()); ++i) {
// If species name matches, set back-transformed particles parameters
if (species_names_list[i] == species_name) {
found = true;
auto& pc = allcontainers[i];
pc->SetDoBackTransformedParticles(do_back_transformed_particles);
}
}
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
found != 0,
"ERROR: could not find the ID of product species '"
+ species_name + "'" + ". Wrong name?");
}
void
MultiParticleContainer::doFieldIonization (int lev,
const MultiFab& Ex,
const MultiFab& Ey,
const MultiFab& Ez,
const MultiFab& Bx,
const MultiFab& By,
const MultiFab& Bz)
{
WARPX_PROFILE("MultiParticleContainer::doFieldIonization()");
amrex::LayoutData<amrex::Real>* cost = WarpX::getCosts(lev);
// Loop over all species.
// Ionized particles in pc_source create particles in pc_product
for (auto& pc_source : allcontainers)
{
if (!pc_source->do_field_ionization){ continue; }
auto& pc_product = allcontainers[pc_source->ionization_product];
const SmartCopyFactory copy_factory(*pc_source, *pc_product);
auto *phys_pc_ptr = static_cast<PhysicalParticleContainer*>(pc_source.get());
auto Copy = copy_factory.getSmartCopy();
auto Transform = IonizationTransformFunc();
pc_source ->defineAllParticleTiles();
pc_product->defineAllParticleTiles();
auto info = getMFItInfo(*pc_source, *pc_product);
#ifdef AMREX_USE_OMP
#pragma omp parallel if (Gpu::notInLaunchRegion())
#endif
for (WarpXParIter pti(*pc_source, lev, info); pti.isValid(); ++pti)
{
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
amrex::Gpu::synchronize();
}
auto wt = static_cast<amrex::Real>(amrex::second());
auto& src_tile = pc_source ->ParticlesAt(lev, pti);
auto& dst_tile = pc_product->ParticlesAt(lev, pti);
auto Filter = phys_pc_ptr->getIonizationFunc(pti, lev, Ex.nGrowVect(),
Ex[pti], Ey[pti], Ez[pti],
Bx[pti], By[pti], Bz[pti]);
const auto np_dst = dst_tile.numParticles();
const auto num_added = filterCopyTransformParticles<1>(*pc_product, dst_tile, src_tile, np_dst,
Filter, Copy, Transform);
setNewParticleIDs(dst_tile, np_dst, num_added);
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
amrex::Gpu::synchronize();
wt = static_cast<amrex::Real>(amrex::second()) - wt;
amrex::HostDevice::Atomic::Add( &(*cost)[pti.index()], wt);
}
}
}
}
void
MultiParticleContainer::doCollisions ( Real cur_time, amrex::Real dt )
{
WARPX_PROFILE("MultiParticleContainer::doCollisions()");
collisionhandler->doCollisions(cur_time, dt, this);
}
void MultiParticleContainer::doResampling (const int timestep, const bool verbose)
{
for (auto& pc : allcontainers)
{
// do_resampling can only be true for PhysicalParticleContainers
if (!pc->do_resampling){ continue; }
pc->resample(timestep, verbose);
}
}
void MultiParticleContainer::CheckIonizationProductSpecies()
{
for (int i=0; i < static_cast<int>(species_names.size()); i++){
if (allcontainers[i]->do_field_ionization){
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
i != allcontainers[i]->ionization_product,
"ERROR: ionization product cannot be the same species");
}
}
}
void MultiParticleContainer::ScrapeParticlesAtEB (
ablastr::fields::MultiLevelScalarField const& distance_to_eb)
{
for (auto& pc : allcontainers) {
scrapeParticlesAtEB(*pc, distance_to_eb, ParticleBoundaryProcess::Absorb());
}
}
#ifdef WARPX_QED
void MultiParticleContainer::InitQED ()
{
m_shr_p_qs_engine = std::make_shared<QuantumSynchrotronEngine>();
m_shr_p_bw_engine = std::make_shared<BreitWheelerEngine>();
m_nspecies_quantum_sync = 0;
m_nspecies_breit_wheeler = 0;
for (auto& pc : allcontainers) {
if(pc->has_quantum_sync()){
pc->set_quantum_sync_engine_ptr
(m_shr_p_qs_engine);
m_nspecies_quantum_sync++;
}
if(pc->has_breit_wheeler()){
pc->set_breit_wheeler_engine_ptr
(m_shr_p_bw_engine);
m_nspecies_breit_wheeler++;
}
}
if(m_nspecies_quantum_sync != 0) {
InitQuantumSync();
}
if(m_nspecies_breit_wheeler !=0) {
InitBreitWheeler();