-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathWarpXEvolve.cpp
More file actions
1581 lines (1377 loc) · 63.8 KB
/
Copy pathWarpXEvolve.cpp
File metadata and controls
1581 lines (1377 loc) · 63.8 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
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, Aurore Blelly
* Axel Huebl, Burlen Loring, David Grote
* Glenn Richardson, Jean-Luc Vay, Luca Fedeli
* Maxence Thevenet, Remi Lehe, Revathi Jambunathan
* Weiqun Zhang, Yinjian Zhao
*
* This file is part of WarpX.
*
* License: BSD-3-Clause-LBNL
*/
#include "WarpX.H"
#include "BoundaryConditions/PML.H"
#include "Diagnostics/MultiDiagnostics.H"
#include "Diagnostics/ReducedDiags/MultiReducedDiags.H"
#include "EmbeddedBoundary/Enabled.H"
#include "Fields.H"
#include "FieldSolver/FiniteDifferenceSolver/HybridPICModel/HybridPICModel.H"
#ifdef WARPX_USE_FFT
# ifdef WARPX_DIM_RZ
# include "FieldSolver/SpectralSolver/SpectralSolverRZ.H"
# else
# include "FieldSolver/SpectralSolver/SpectralSolver.H"
# endif
#endif
#include "FieldSolver/ImplicitSolvers/ImplicitSolver.H"
#include "Parallelization/GuardCellManager.H"
#include "Particles/MultiParticleContainer.H"
#include "Fluids/MultiFluidContainer.H"
#include "Fluids/WarpXFluidContainer.H"
#include "Particles/ParticleBoundaryBuffer.H"
#include "Python/callbacks.H"
#include "Utils/TextMsg.H"
#include "Utils/WarpXAlgorithmSelection.H"
#include "Utils/WarpXUtil.H"
#include "Utils/WarpXConst.H"
#include <ablastr/profiler/ProfilerWrapper.H>
#include <ablastr/utils/SignalHandling.H>
#include <ablastr/warn_manager/WarnManager.H>
#include <AMReX.H>
#include <AMReX_Array.H>
#include <AMReX_BLassert.H>
#include <AMReX_Geometry.H>
#include <AMReX_IntVect.H>
#include <AMReX_LayoutData.H>
#include <AMReX_MultiFab.H>
#include <AMReX_ParmParse.H>
#include <AMReX_Print.H>
#include <AMReX_REAL.H>
#include <AMReX_RealVect.H>
#include <AMReX_Utility.H>
#include <AMReX_Vector.H>
#include <algorithm>
#include <cmath>
#include <array>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
using namespace amrex;
using ablastr::utils::SignalHandling;
namespace
{
/** Print Unused Parameter Warnings after Step 1
*
* Instead of waiting for a simulation to end, we already do an early "unused parameter check"
* after step 1 to inform users early of potential issues with their simulation setup.
*/
void checkEarlyUnusedParams ()
{
amrex::Print() << "\n"; // better: conditional \n based on return value
amrex::ParmParse::QueryUnusedInputs();
// Print the warning list right after the first step.
amrex::Print() << ablastr::warn_manager::GetWMInstance().PrintGlobalWarnings("FIRST STEP");
}
void StoreCurrent (int lev, ablastr::fields::MultiFabRegister& fields)
{
using ablastr::fields::Direction;
using warpx::fields::FieldType;
for (int idim = 0; idim < 3; ++idim) {
const auto dir = Direction{idim};
if (fields.has(FieldType::current_store, dir,lev)) {
MultiFab::Copy(*fields.get(FieldType::current_store, dir, lev),
*fields.get(FieldType::current_fp, dir, lev),
0, 0, 1, fields.get(FieldType::current_store, dir, lev)->nGrowVect());
}
}
}
void RestoreCurrent (int lev, ablastr::fields::MultiFabRegister& fields)
{
using ablastr::fields::Direction;
using warpx::fields::FieldType;
for (int idim = 0; idim < 3; ++idim) {
const auto dir = Direction{idim};
if (fields.has(FieldType::current_store, dir, lev)) {
std::swap(
*fields.get(FieldType::current_fp, dir, lev),
*fields.get(FieldType::current_store, dir, lev)
);
}
}
}
}
void
WarpX::SynchronizeVelocityWithPosition () {
using ablastr::fields::Direction;
using warpx::fields::FieldType;
if (!m_is_synchronized) {
// This assumes that the particle boundary conditions have been checked
// so that the field gather in PushP will be correct.
FillBoundaryE(guard_cells.ng_FieldGather);
FillBoundaryB(guard_cells.ng_FieldGather);
if (fft_do_time_averaging)
{
FillBoundaryE_avg(guard_cells.ng_FieldGather);
FillBoundaryB_avg(guard_cells.ng_FieldGather);
}
UpdateAuxiliaryData();
FillBoundaryAux(guard_cells.ng_UpdateAux);
for (int lev = 0; lev <= finest_level; ++lev) {
mypc->PushP(
lev,
0.5_rt*dt[lev],
*m_fields.get(FieldType::Efield_aux, Direction{0}, lev),
*m_fields.get(FieldType::Efield_aux, Direction{1}, lev),
*m_fields.get(FieldType::Efield_aux, Direction{2}, lev),
*m_fields.get(FieldType::Bfield_aux, Direction{0}, lev),
*m_fields.get(FieldType::Bfield_aux, Direction{1}, lev),
*m_fields.get(FieldType::Bfield_aux, Direction{2}, lev),
MomentumPushType::Full
);
}
m_is_synchronized = true;
}
}
void
WarpX::Evolve (int numsteps)
{
ABLASTR_PROFILE_REGION("WarpX::Evolve()");
ABLASTR_PROFILE("WarpX::Evolve()");
using ablastr::fields::Direction;
Real cur_time = t_new[0];
// Note that the default argument is numsteps = -1
const int numsteps_max = (numsteps < 0)?(max_step):(istep[0] + numsteps);
// check typos in inputs after step 1
bool early_params_checked = false;
static Real evolve_time = 0;
const int step_begin = istep[0];
for (int step = istep[0]; step < numsteps_max && cur_time < stop_time; ++step)
{
ABLASTR_PROFILE("WarpX::Evolve::step");
const auto evolve_time_beg_step = static_cast<Real>(amrex::second());
// Check and clear signal flags and asynchronously broadcast them from process 0
SignalHandling::CheckSignals();
multi_diags->NewIteration();
bool verbose_step = (bool)verbose;
if (verbose && m_limit_verbose_step) {
int verbose_step_interval = 1;
if (step<10) { verbose_step_interval = 1; }
else if (step<100) { verbose_step_interval = 10; }
else { verbose_step_interval = 100; }
verbose_step = !((step+1)%verbose_step_interval);
}
// Start loop on time steps
if (verbose_step) {
amrex::Print() << "STEP " << step+1 << " starts ...\n";
}
ExecutePythonCallback("beforestep");
CheckLoadBalance(step);
// Update the timestep for solvers that support adaptive timestepping
// (electrostatic and theta-implicit EM), provided const_dt is not specified.
if (m_dt_update_interval.contains(step+1) || (step == 0 && m_max_dt.has_value())) {
SynchronizeVelocityWithPosition();
ApplyDtLimiters();
if (verbose_step) {
std::ostringstream oss;
oss << "updating timestep to DT = " << std::scientific << std::setprecision(6) << dt[0];
amrex::Print() << Utils::TextMsg::Info(oss.str());
}
}
// If position and velocity are synchronized, push velocity backward one half step
if (evolve_scheme == EvolveScheme::Explicit)
{
ExplicitFillBoundaryEBUpdateAux();
}
// If needed, deposit the initial ion charge and current densities that
// will be used to update the E-field in Ohm's law.
if (step == step_begin &&
electromagnetic_solver_id == ElectromagneticSolverAlgo::HybridPIC
) {
HybridPICInitializeRhoJandB();
}
// multi-physics: field ionization
doFieldIonization();
#ifdef WARPX_QED
// multi-physics: QED effects
doQEDEvents();
mypc->doQEDSchwinger();
#endif
// perform particle injection
ExecutePythonCallback("particleinjection");
// perform collisions and advance fields and particles by one time step
OneStep(cur_time, dt[0], step);
// Resample particles
// +1 is necessary here because value of step seen by user (first step is 1) is different than
// value of step in code (first step is 0)
mypc->doResampling(Geom(), istep[0]+1, verbose_step);
if (evolve_scheme == EvolveScheme::Explicit) {
applyMirrors(cur_time);
// E : guard cells are NOT up-to-date
// B : guard cells are NOT up-to-date
}
for (int lev = 0; lev <= max_level; ++lev) {
++istep[lev];
}
cur_time += dt[0];
ShiftGalileanBoundary();
// sync up time
for (int i = 0; i <= max_level; ++i) {
t_old[i] = t_new[i];
t_new[i] = cur_time;
}
multi_diags->FilterComputePackFlush( step, false, true );
const bool move_j = m_is_synchronized;
// If m_is_synchronized we need to shift j too so that next step we can evolve E by dt/2.
// We might need to move j because we are going to make a plotfile.
const int num_moved = MoveWindow(step+1, move_j);
// Update the accelerator lattice element finder if the window has moved,
// from either a moving window or a boosted frame
if (num_moved != 0 || gamma_boost > 1) {
for (int lev = 0; lev <= finest_level; ++lev) {
m_accelerator_lattice[lev]->UpdateElementFinder(lev, gett_new());
}
}
HandleParticlesAtBoundaries(step, cur_time, num_moved);
// Apply particle thermalizer (no-op until implemented)
if (m_particle_thermalizer.defined()) {
m_particle_thermalizer.applyThermalizer(*mypc);
}
if (m_implicit_solver) {
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(step, cur_time, dt[0]);
ExecutePythonCallback("aftercollisions");
}
// Electrostatic field solve step for electrostatic or Darwin solvers
if( electrostatic_solver_id != ElectrostaticSolverAlgo::None )
{
ExecutePythonCallback("beforeEsolve");
// Electrostatic solver:
// The E-field is always reset to hold just the electrostatic component
bool const reset_E_field = true;
// The B-field is also reset unless the Darwin solver is used
bool const reset_B_field = (evolve_scheme != EvolveScheme::Semi_Implicit_Darwin);
// For each species: deposit charge and add the associated space-charge
// E and B field to the grid ; this is done at the end of the PIC
// loop (i.e. immediately after a `Redistribute` and before particle
// positions are next pushed) so that the particles do not deposit out of bounds
// and so that the fields are at the correct time in the output.
ComputeSpaceChargeField( reset_E_field, reset_B_field );
if (electrostatic_solver_id == ElectrostaticSolverAlgo::LabFrameElectroMagnetostatic) {
// Call Magnetostatic Solver to solve for the vector potential A and compute the
// B field. Time varying A contribution to E field is neglected.
// This is currently a lab frame calculation.
ComputeMagnetostaticField();
}
// The external fields are added back on to the fine patch fields
// (which were overwritten by electrostatic / magnetostatic solvers)
// so that the net fields are the sum of the field solutions and any
// external fields.
// This is skipped for Darwin since in that case the "external" fields
// are just treated as initial conditions (as for other EM solvers).
if (evolve_scheme != EvolveScheme::Semi_Implicit_Darwin) {
for (int lev = 0; lev <= max_level; ++lev) {
AddExternalFields(lev);
}
}
ExecutePythonCallback("afterEsolve");
}
// Hybrid-PIC case. With the theta-implicit hybrid evolve scheme the
// fields are already advanced self-consistently with the particles in
// the implicit solver's OneStep, so this explicit field update is skipped.
if (electromagnetic_solver_id == ElectromagneticSolverAlgo::HybridPIC &&
!m_implicit_solver) {
ExecutePythonCallback("beforeEsolve");
// The particles are now at p^{n+1/2} and x^{n+1}. The fields
// are updated according to the hybrid-PIC scheme (Ohm's law
// and Ampere's law).
HybridPICEvolveFields();
ExecutePythonCallback("afterEsolve");
}
bool const do_diagnostic = (multi_diags->DoComputeAndPack(step) || reduced_diags->DoDiags(step));
bool const end_of_step_loop = (step == numsteps_max - 1) || (cur_time + dt[0] >= stop_time - 1.e-3*dt[0]);
if (synchronize_velocity_for_diagnostics &&
(do_diagnostic || end_of_step_loop)) {
// When the diagnostics require synchronization, push p by 0.5*dt to synchronize.
// Note that this will be undone at the start of the next step by the half v-push
// backwards.
SynchronizeVelocityWithPosition();
}
// afterstep callback runs with the updated global time. It is included
// in the evolve timing.
ExecutePythonCallback("afterstep");
/// reduced diags
if (reduced_diags->m_plot_rd != 0)
{
reduced_diags->LoadBalance();
reduced_diags->ComputeDiags(step);
reduced_diags->WriteToFile(step);
}
multi_diags->FilterComputePackFlush( step );
// execute afterdiagnostic callbacks
ExecutePythonCallback("afterdiagnostics");
// inputs: unused parameters (e.g. typos) check after step 1 has finished
if (!early_params_checked) {
::checkEarlyUnusedParams();
early_params_checked = true;
}
// create ending time stamp for calculating elapsed time each iteration
const auto evolve_time_end_step = static_cast<Real>(amrex::second());
evolve_time += evolve_time_end_step - evolve_time_beg_step;
HandleSignals();
if (verbose_step) {
amrex::Print()<< "STEP " << step+1 << " ends." << " TIME = " << cur_time
<< " DT = " << dt[0] << "\n";
amrex::Print()<< "Evolve time = " << evolve_time
<< " s; This step = " << evolve_time_end_step-evolve_time_beg_step
<< " s; Avg. per step = " << evolve_time/(step-step_begin+1) << " s\n\n";
}
if (checkStopSimulation(cur_time)) {
break;
}
} // End loop on time steps
// This if statement is needed for PICMI, which allows the Evolve routine to be
// called multiple times, otherwise diagnostics will be done at every call,
// regardless of the diagnostic period parameter provided in the inputs.
bool const final_time_step = (istep[0] == max_step)
|| (cur_time >= stop_time - 1.e-3*dt[0]
&& cur_time < stop_time + dt[0]);
if (final_time_step || m_exit_loop_due_to_interrupt_signal) {
multi_diags->FilterComputePackFlushLastTimestep( istep[0] );
if (m_exit_loop_due_to_interrupt_signal) { ExecutePythonCallback("onbreaksignal"); }
}
amrex::Print() <<
ablastr::warn_manager::GetWMInstance().PrintGlobalWarnings("THE END");
}
void WarpX::OneStep (
amrex::Real a_cur_time,
amrex::Real a_dt,
int a_step
)
{
ABLASTR_PROFILE("WarpX::OneStep()");
// implicit solver
if (m_implicit_solver) {
// advance fields and particles by one time step
const int exit_status = m_implicit_solver->OneStep(a_cur_time, a_dt, a_step);
if (exit_status < 0) {
std::stringstream solverMsg;
solverMsg << "ImplicitSolver::OneStep() failed at step = " << a_step
<< " using dt = " << a_dt << ".\n"
<< "Nonlinear solver failed to converge: exit status = " << exit_status;
WARPX_ABORT_WITH_MESSAGE(solverMsg.str());
}
}
// explicit solver
else {
// electrostatic solver or hybrid solver
if (electromagnetic_solver_id == ElectromagneticSolverAlgo::None ||
electromagnetic_solver_id == ElectromagneticSolverAlgo::HybridPIC) {
// with collisions placed in the middle of the momentum push
if (m_collisions_split_momentum_push) {
// push particles (half momentum)
PushParticlesandDeposit(
a_cur_time,
/*skip_deposition=*/true,
PositionPushType::None,
MomentumPushType::FirstHalf
);
// perform particle collisions
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(a_step, a_cur_time, a_dt);
ExecutePythonCallback("aftercollisions");
// push particles (full position and half momentum)
PushParticlesandDeposit(
a_cur_time,
/*skip_deposition=*/true,
PositionPushType::Full,
MomentumPushType::SecondHalf
);
}
// with collisions placed before the position and momentum push, or without collisions
else {
// perform particle collisions
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(a_step, a_cur_time, a_dt);
ExecutePythonCallback("aftercollisions");
// push particles (full position and full momentum)
PushParticlesandDeposit(
a_cur_time,
/*skip_deposition=*/true,
PositionPushType::Full,
MomentumPushType::Full
);
}
}
// electromagnetic solver
else {
// without mesh refinement
if (finest_level == 0) {
// standard PIC loop
if (!m_JRhom) {
OneStep_nosub(a_cur_time, a_dt, a_step);
}
// JRhom PIC loop
else {
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(
m_collisions_split_momentum_push == 0,
"Collisions with split momentum push not yet implemented for JRhom PIC loop."
"Set `collisions.split_momentum_push=0` to use JRhom with standard (pre-v-push collisions placement) collisions model."
);
// perform particle collisions
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(a_step, a_cur_time, a_dt);
ExecutePythonCallback("aftercollisions");
OneStep_JRhom(a_cur_time);
}
}
// with mesh refinement
else {
// without subcycling
if (!m_do_subcycling) {
OneStep_nosub(a_cur_time, a_dt, a_step);
}
// with subcycling
else {
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
finest_level == 1,
"Subcycling not implemented with more than 1 mesh refinement level"
);
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(
m_collisions_split_momentum_push == 0,
"Collisions with split momentum push not yet implemented with subcycling."
"Set `collisions.split_momentum_push=0` to use subcycling with standard (pre-v-push collisions placement) collisions model."
);
// perform particle collisions
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(a_step, a_cur_time, a_dt);
ExecutePythonCallback("aftercollisions");
OneStep_sub1(a_cur_time);
}
}
}
}
}
/**
* \brief Perform one PIC iteration, without subcycling
* i.e. all levels/patches use the same timestep (that of the finest level)
* for the field advance and particle pusher.
*/
void
WarpX::OneStep_nosub (
amrex::Real a_cur_time,
amrex::Real a_dt,
int a_step
)
{
ABLASTR_PROFILE("WarpX::OneStep_nosub()");
// Push particle from x^{n} to x^{n+1}
// from p^{n-1/2} to p^{n+1/2}
// Deposit current j^{n+1/2}
// Deposit charge density rho^{n}
ExecutePythonCallback("beforedeposition");
// with collisions placed in the middle of the momentum push
if (m_collisions_split_momentum_push) {
// push particles (half momentum)
PushParticlesandDeposit(
a_cur_time,
/*skip_deposition=*/true,
PositionPushType::None,
MomentumPushType::FirstHalf
);
// perform particle collisions
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(a_step, a_cur_time, a_dt);
ExecutePythonCallback("aftercollisions");
// push particles (full position and half momentum)
PushParticlesandDeposit(
a_cur_time,
/*skip_deposition=*/false,
PositionPushType::Full,
MomentumPushType::SecondHalf
);
}
else {
// perform particle collisions
ExecutePythonCallback("beforecollisions");
mypc->doCollisions(a_step, a_cur_time, a_dt);
ExecutePythonCallback("aftercollisions");
// push particles (full position and full momentum)
PushParticlesandDeposit(
a_cur_time,
/*skip_deposition=*/false,
PositionPushType::Full,
MomentumPushType::Full
);
}
ExecutePythonCallback("afterdeposition");
// Synchronize J and rho:
// filter (if used), exchange guard cells, interpolate across MR levels
// and apply boundary conditions
SyncCurrentAndRho();
// At this point, J is up-to-date inside the domain, and E and B are
// up-to-date including enough guard cells for first step of the field
// solve.
// For extended PML: copy J from regular grid to PML, and damp J in PML
if (do_pml && pml_has_particles) { CopyJPML(); }
if (do_pml && do_pml_j_damping) { DampJPML(); }
ExecutePythonCallback("beforeEsolve");
// Push E and B from {n} to {n+1}
// (And update guard cells immediately afterwards)
if (WarpX::electromagnetic_solver_id == ElectromagneticSolverAlgo::PSATD) {
if (use_hybrid_QED)
{
WarpX::Hybrid_QED_Push(dt);
FillBoundaryE(guard_cells.ng_alloc_EB);
}
PushPSATD(a_cur_time);
if (do_pml) {
DampPML();
}
if (use_hybrid_QED) {
FillBoundaryE(guard_cells.ng_alloc_EB);
FillBoundaryB(guard_cells.ng_alloc_EB, WarpX::sync_nodal_points);
WarpX::Hybrid_QED_Push(dt);
FillBoundaryE(guard_cells.ng_afterPushPSATD, WarpX::sync_nodal_points);
}
else {
FillBoundaryE(guard_cells.ng_afterPushPSATD, WarpX::sync_nodal_points);
FillBoundaryB(guard_cells.ng_afterPushPSATD, WarpX::sync_nodal_points);
if (WarpX::do_dive_cleaning || WarpX::do_pml_dive_cleaning) {
FillBoundaryF(guard_cells.ng_alloc_F, WarpX::sync_nodal_points);
}
if (WarpX::do_divb_cleaning || WarpX::do_pml_divb_cleaning) {
FillBoundaryG(guard_cells.ng_alloc_G, WarpX::sync_nodal_points);
}
}
} else {
EvolveF(0.5_rt * dt[0], /*rho_comp=*/0);
EvolveG(0.5_rt * dt[0]);
FillBoundaryF(guard_cells.ng_FieldSolverF);
FillBoundaryG(guard_cells.ng_FieldSolverG);
EvolveB(0.5_rt * dt[0], SubcyclingHalf::FirstHalf, a_cur_time); // We now have B^{n+1/2}
FillBoundaryB(guard_cells.ng_FieldSolver, WarpX::sync_nodal_points);
if (m_em_solver_medium == MediumForEM::Vacuum) {
// vacuum medium
EvolveE(dt[0], a_cur_time); // We now have E^{n+1}
} else if (m_em_solver_medium == MediumForEM::Macroscopic) {
// macroscopic medium
MacroscopicEvolveE(dt[0], a_cur_time); // We now have E^{n+1}
} else {
WARPX_ABORT_WITH_MESSAGE("Medium for EM is unknown");
}
FillBoundaryE(guard_cells.ng_FieldSolver, WarpX::sync_nodal_points);
EvolveF(0.5_rt * dt[0], /*rho_comp=*/1);
EvolveG(0.5_rt * dt[0]);
EvolveB(0.5_rt * dt[0], SubcyclingHalf::SecondHalf, a_cur_time + 0.5_rt * dt[0]); // We now have B^{n+1}
if (do_pml) {
DampPML();
FillBoundaryE(guard_cells.ng_MovingWindow, WarpX::sync_nodal_points);
FillBoundaryB(guard_cells.ng_MovingWindow, WarpX::sync_nodal_points);
FillBoundaryF(guard_cells.ng_MovingWindow, WarpX::sync_nodal_points);
FillBoundaryG(guard_cells.ng_MovingWindow, WarpX::sync_nodal_points);
}
// E and B are up-to-date in the domain, but all guard cells are
// outdated.
if (m_safe_guard_cells) {
FillBoundaryB(guard_cells.ng_alloc_EB);
}
} // !PSATD
ExecutePythonCallback("afterEsolve");
}
bool WarpX::checkStopSimulation (amrex::Real cur_time)
{
m_exit_loop_due_to_interrupt_signal = SignalHandling::TestAndResetActionRequestFlag(SignalHandling::SIGNAL_REQUESTS_BREAK);
return (cur_time >= stop_time - 1.e-3*dt[0]) ||
m_exit_loop_due_to_interrupt_signal;
}
void WarpX::ExplicitFillBoundaryEBUpdateAux ()
{
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(evolve_scheme == EvolveScheme::Explicit,
"Cannot call WarpX::ExplicitFillBoundaryEBUpdateAux without Explicit evolve scheme set!");
using ablastr::fields::Direction;
using warpx::fields::FieldType;
// At the beginning, we have B^{n} and E^{n}.
// Particles have p^{n} and x^{n}.
// m_is_synchronized is true.
if (m_is_synchronized) {
// Not called at each iteration, so exchange all guard cells
FillBoundaryE(guard_cells.ng_alloc_EB);
FillBoundaryB(guard_cells.ng_alloc_EB);
UpdateAuxiliaryData();
FillBoundaryAux(guard_cells.ng_UpdateAux);
// on first step, push p by -0.5*dt
for (int lev = 0; lev <= finest_level; ++lev)
{
mypc->PushP(
lev,
-0.5_rt*dt[lev],
*m_fields.get(FieldType::Efield_aux, Direction{0}, lev),
*m_fields.get(FieldType::Efield_aux, Direction{1}, lev),
*m_fields.get(FieldType::Efield_aux, Direction{2}, lev),
*m_fields.get(FieldType::Bfield_aux, Direction{0}, lev),
*m_fields.get(FieldType::Bfield_aux, Direction{1}, lev),
*m_fields.get(FieldType::Bfield_aux, Direction{2}, lev),
MomentumPushType::Full
);
}
m_is_synchronized = false;
} else {
// Beyond one step, we have E^{n} and B^{n}.
// Particles have p^{n-1/2} and x^{n}.
// E and B: enough guard cells to update Aux or call Field Gather in fp and cp
// Need to update Aux on lower levels, to interpolate to higher levels.
// E and B are up-to-date inside the domain only
FillBoundaryE(guard_cells.ng_FieldGather);
FillBoundaryB(guard_cells.ng_FieldGather);
if (electrostatic_solver_id == ElectrostaticSolverAlgo::None) {
if (fft_do_time_averaging)
{
FillBoundaryE_avg(guard_cells.ng_FieldGather);
FillBoundaryB_avg(guard_cells.ng_FieldGather);
}
// TODO Remove call to FillBoundaryAux before UpdateAuxiliaryData?
if (WarpX::electromagnetic_solver_id != ElectromagneticSolverAlgo::PSATD) {
FillBoundaryAux(guard_cells.ng_UpdateAux);
}
}
UpdateAuxiliaryData();
FillBoundaryAux(guard_cells.ng_UpdateAux);
}
}
void WarpX::HandleParticlesAtBoundaries (int step, amrex::Real cur_time, int num_moved)
{
mypc->ContinuousFluxInjection(cur_time, dt[0]);
ExecutePythonCallback("particlescraper");
mypc->ApplyBoundaryConditions();
m_particle_boundary_buffer->gatherParticlesFromDomainBoundaries(*mypc, cur_time);
// Without mesh refinement, use a local redistribute when particles can only
// have moved by a small number of cells; otherwise fall back to a global one.
if (finest_level == 0) {
// Estimate, per direction, the maximum distance a particle may have
// travelled during this step, expressed in number of cells.
// (Geom().CellSizeArray() is indexed by active dimension 0..SPACEDIM-1.)
const amrex::GpuArray<amrex::Real,AMREX_SPACEDIM> dx = Geom(0).CellSizeArray();
// Particles cannot travel faster than the speed of light, so c * dt / dx
// is a physical upper bound on the number of cells crossed per direction.
amrex::RealVect max_distance_relative_to_grid;
for (int d = 0; d < AMREX_SPACEDIM; ++d) {
max_distance_relative_to_grid[d] = PhysConst::c * dt[0] / dx[d];
}
// Moving window: particles can additionally move by the number of cells
// that the window was shifted, along the moving-window direction.
if (moving_window_dir >= 0) {
max_distance_relative_to_grid[moving_window_dir] += static_cast<amrex::Real>(num_moved);
}
// Galilean algorithm: account for the extra grid shift due to the moving
// Galilean frame. m_v_galilean is indexed by x/y/z, so map its components
// onto the active simulation dimensions.
#if defined(WARPX_DIM_3D)
const amrex::RealVect v_galilean = {m_v_galilean[0], m_v_galilean[1], m_v_galilean[2]};
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
const amrex::RealVect v_galilean = {m_v_galilean[0], m_v_galilean[2]};
#elif defined(WARPX_DIM_1D_Z)
const amrex::RealVect v_galilean(m_v_galilean[2]);
#else // WARPX_DIM_RCYLINDER, WARPX_DIM_RSPHERE: no Galilean shift
const amrex::RealVect v_galilean = amrex::RealVect::TheZeroVector();
#endif
for (int d = 0; d < AMREX_SPACEDIM; ++d) {
max_distance_relative_to_grid[d] += std::abs(v_galilean[d]) * dt[0] / dx[d];
}
// Convert to an integer number of cells (rounding up), per direction.
amrex::IntVect max_cells_travelled;
for (int d = 0; d < AMREX_SPACEDIM; ++d) {
max_cells_travelled[d] =
static_cast<int>(std::ceil(max_distance_relative_to_grid[d]));
}
// If, in any direction, max_cells_travelled reaches the domain size, the
// local search is no longer more efficient than (and may crash in lieu
// of) a full redistribute, so fall back in that case.
const amrex::IntVect domain_length = Geom(0).Domain().length();
bool use_local_redistribute = true;
for (int d = 0; d < AMREX_SPACEDIM; ++d) {
if (max_cells_travelled[d] >= domain_length[d]) { use_local_redistribute = false; }
}
if (use_local_redistribute) {
mypc->RedistributeLocal(max_cells_travelled);
} else {
mypc->Redistribute();
}
}
else {
mypc->Redistribute();
}
// interact the particles with EB walls (if present)
if (EB::enabled()) {
using warpx::fields::FieldType;
mypc->ScrapeParticlesAtEB(m_fields.get_mr_levels(FieldType::distance_to_eb, finest_level));
m_particle_boundary_buffer->gatherParticlesFromEmbeddedBoundaries(
*mypc, m_fields.get_mr_levels(FieldType::distance_to_eb, finest_level), cur_time);
if (eb_particle_boundary == ParticleBoundaryType::Absorbing) {
// If particles are simply absorbed, no need for a full Redistribute.
// Instead: simply delete the absorbed particles
mypc->deleteInvalidParticles();
} else {
// For other particle boundary conditions (e.g. reflecting),
// particles can move to a different sub-domain, so we need a full Redistribute
mypc->Redistribute();
}
}
if (sort_intervals.contains(step+1)) {
if (verbose && !m_limit_verbose_step) {
amrex::Print() << Utils::TextMsg::Info("re-sorting particles");
}
mypc->SortParticlesByBin(
sort_bin_size, m_sort_particles_for_deposition, m_sort_idx_type);
}
}
void WarpX::SyncCurrentAndRho ()
{
using ablastr::fields::Direction;
using warpx::fields::FieldType;
if (electromagnetic_solver_id == ElectromagneticSolverAlgo::PSATD)
{
if (fft_periodic_single_box)
{
// With periodic single box, synchronize J and rho here,
// even with current correction or Vay deposition
std::string const current_fp_string = (current_deposition_algo == CurrentDepositionAlgo::Vay)
? "current_fp_vay" : "current_fp";
// TODO Replace current_cp with current_cp_vay once Vay deposition is implemented with MR
SyncCurrent(current_fp_string);
SyncRho();
}
else // no periodic single box
{
// Without periodic single box, synchronize J and rho here,
// except with current correction or Vay deposition:
// in these cases, synchronize later (in WarpX::PushPSATD)
if (!current_correction &&
current_deposition_algo != CurrentDepositionAlgo::Vay)
{
SyncCurrent("current_fp");
SyncRho();
}
if (current_deposition_algo == CurrentDepositionAlgo::Vay)
{
// TODO This works only without mesh refinement
const int lev = 0;
if (use_filter) {
ApplyFilterJ(m_fields.get_mr_levels_alldirs(FieldType::current_fp_vay, finest_level), lev);
}
}
}
}
else // FDTD
{
SyncCurrent("current_fp");
SyncRho();
}
// Reflect charge and current density over PEC boundaries, if needed.
for (int lev = 0; lev <= finest_level; ++lev)
{
if (m_fields.has(FieldType::rho_fp, lev)) {
ApplyRhofieldBoundary(lev, m_fields.get(FieldType::rho_fp,lev), PatchType::fine);
}
ApplyJfieldBoundary(lev,
m_fields.get(FieldType::current_fp, Direction{0}, lev),
m_fields.get(FieldType::current_fp, Direction{1}, lev),
m_fields.get(FieldType::current_fp, Direction{2}, lev),
PatchType::fine);
if (lev > 0) {
if (m_fields.has(FieldType::rho_cp, lev)) {
ApplyRhofieldBoundary(lev, m_fields.get(FieldType::rho_cp,lev), PatchType::coarse);
}
ApplyJfieldBoundary(lev,
m_fields.get(FieldType::current_cp, Direction{0}, lev),
m_fields.get(FieldType::current_cp, Direction{1}, lev),
m_fields.get(FieldType::current_cp, Direction{2}, lev),
PatchType::coarse);
}
}
}
void
WarpX::OneStep_JRhom (const amrex::Real cur_time)
{
#ifdef WARPX_USE_FFT
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
WarpX::electromagnetic_solver_id == ElectromagneticSolverAlgo::PSATD,
"JRhom algorithm not implemented with the FDTD solver"
);
using warpx::fields::FieldType;
bool const skip_lev0_coarse_patch = true;
const int rho_mid = spectral_solver_fp[0]->m_spectral_index.rho_mid;
const int rho_new = spectral_solver_fp[0]->m_spectral_index.rho_new;
// Push particle from x^{n} to x^{n+1}
// from p^{n-1/2} to p^{n+1/2}
const bool skip_deposition = true;
PushParticlesandDeposit(cur_time, skip_deposition);
// Initialize PSATD-JRhom loop:
// 1) Prepare E,B,F,G fields in spectral space
PSATDForwardTransformEB();
if (WarpX::do_dive_cleaning) { PSATDForwardTransformF(); }
if (WarpX::do_divb_cleaning) { PSATDForwardTransformG(); }
// 2) Set the averaged fields to zero
if (WarpX::fft_do_time_averaging) { PSATDEraseAverageFields(); }
// 3) Deposit rho (in rho_new, since it will be moved during the loop)
// (after checking that pointer to rho_fp on MR level 0 is not null)
if (m_fields.has(FieldType::rho_fp, 0) && time_dependency_rho != TimeDependencyRho::Constant)
{
ablastr::fields::MultiLevelScalarField const rho_fp = m_fields.get_mr_levels(FieldType::rho_fp, finest_level);
std::string const rho_fp_string = "rho_fp";
std::string const rho_cp_string = "rho_cp";
// Deposit rho at relative time -dt
// (dt[0] denotes the time step on mesh refinement level 0)
mypc->DepositCharge(rho_fp, -dt[0]);
// Filter, exchange boundary, and interpolate across levels
SyncRho();
// Forward FFT of rho
PSATDForwardTransformRho(rho_fp_string, rho_cp_string, 0, rho_new);
}
// 4) Deposit J at relative time -dt with time step dt
// (dt[0] denotes the time step on mesh refinement level 0)
if (time_dependency_J != TimeDependencyJ::Constant)
{
std::string const current_string = (do_current_centering) ? "current_fp_nodal" : "current_fp";
mypc->DepositCurrent( m_fields.get_mr_levels_alldirs(current_string, finest_level), dt[0], -dt[0]);
// Synchronize J: filter, exchange boundary, and interpolate across levels.
// With current centering, the nodal current is deposited in 'current',
// namely 'current_fp_nodal': SyncCurrent stores the result of its centering
// into 'current_fp' and then performs both filtering, if used, and exchange
// of guard cells.
SyncCurrent("current_fp");
// Forward FFT of J
PSATDForwardTransformJ("current_fp", "current_cp");
}
// Number of depositions for multi-J scheme
const int n_deposit = WarpX::m_JRhom_subintervals;
// Time sub-step for each multi-J deposition
const amrex::Real sub_dt = dt[0] / static_cast<amrex::Real>(n_deposit);
// Whether to perform PSATD-JRhom depositions on a time interval that spans
// one or two full time steps (from n*dt to (n+1)*dt, or from n*dt to (n+2)*dt)
const int n_loop = (WarpX::fft_do_time_averaging) ? 2*n_deposit : n_deposit;
// Loop over PSATD-JRhom depositions
for (int i_deposit = 0; i_deposit < n_loop; i_deposit++)
{
// Move J from new to old if J is linear or quadratic in time
if (time_dependency_J != TimeDependencyJ::Constant) { PSATDMoveJNewToJOld(); }
const amrex::Real t_deposit_current = (time_dependency_J == TimeDependencyJ::Linear) ?
(i_deposit-n_deposit+1)*sub_dt : (i_deposit-n_deposit+0.5_rt)*sub_dt;
const amrex::Real t_deposit_charge = (time_dependency_rho == TimeDependencyRho::Linear) ?
(i_deposit-n_deposit+1)*sub_dt : (i_deposit-n_deposit+0.5_rt)*sub_dt;
// Deposit new J at relative time t_deposit_current with time step dt
// (dt[0] denotes the time step on mesh refinement level 0)
std::string const current_string = (do_current_centering) ? "current_fp_nodal" : "current_fp";
mypc->DepositCurrent( m_fields.get_mr_levels_alldirs(current_string, finest_level), dt[0], t_deposit_current);
// Synchronize J: filter, exchange boundary, and interpolate across levels.
// With current centering, the nodal current is deposited in 'current',
// namely 'current_fp_nodal': SyncCurrent stores the result of its centering