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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
|
/* Copyright 2019-2020 Andrew Myers, Aurore Blelly, Axel Huebl
* David Grote, Glenn Richardson, Jean-Luc Vay
* Ligia Diana Amorim, Luca Fedeli, Maxence Thevenet
* Michael Rowan, Remi Lehe, Revathi Jambunathan
* Weiqun Zhang, Yinjian Zhao
*
* This file is part of WarpX.
*
* License: BSD-3-Clause-LBNL
*/
#include "PhysicalParticleContainer.H"
#include "Filter/NCIGodfreyFilter.H"
#include "Initialization/InjectorDensity.H"
#include "Initialization/InjectorMomentum.H"
#include "Initialization/InjectorPosition.H"
#include "MultiParticleContainer.H"
#ifdef WARPX_QED
# include "Particles/ElementaryProcess/QEDInternals/BreitWheelerEngineWrapper.H"
# include "Particles/ElementaryProcess/QEDInternals/QuantumSyncEngineWrapper.H"
#endif
#include "Particles/Gather/FieldGather.H"
#include "Particles/Gather/GetExternalFields.H"
#include "Particles/Pusher/CopyParticleAttribs.H"
#include "Particles/Pusher/GetAndSetPosition.H"
#include "Particles/Pusher/PushSelector.H"
#include "Particles/Pusher/UpdateMomentumBoris.H"
#include "Particles/Pusher/UpdateMomentumBorisWithRadiationReaction.H"
#include "Particles/Pusher/UpdateMomentumHigueraCary.H"
#include "Particles/Pusher/UpdateMomentumVay.H"
#include "Particles/Pusher/UpdatePosition.H"
#include "Particles/SpeciesPhysicalProperties.H"
#include "Particles/WarpXParticleContainer.H"
#include "Utils/Parser/ParserUtils.H"
#include "Utils/Physics/IonizationEnergiesTable.H"
#include "Utils/TextMsg.H"
#include "Utils/WarpXAlgorithmSelection.H"
#include "Utils/WarpXConst.H"
#include "Utils/WarpXProfilerWrapper.H"
#include "WarpX.H"
#include <ablastr/warn_manager/WarnManager.H>
#include <AMReX.H>
#include <AMReX_Algorithm.H>
#include <AMReX_Array.H>
#include <AMReX_Array4.H>
#include <AMReX_ArrayOfStructs.H>
#include <AMReX_BLassert.H>
#include <AMReX_Box.H>
#include <AMReX_BoxArray.H>
#include <AMReX_Config.H>
#include <AMReX_Dim3.H>
#include <AMReX_Extension.H>
#include <AMReX_FArrayBox.H>
#include <AMReX_FabArray.H>
#include <AMReX_Geometry.H>
#include <AMReX_GpuAtomic.H>
#include <AMReX_GpuControl.H>
#include <AMReX_GpuDevice.H>
#include <AMReX_GpuElixir.H>
#include <AMReX_GpuLaunch.H>
#include <AMReX_GpuQualifiers.H>
#include <AMReX_INT.H>
#include <AMReX_IndexType.H>
#include <AMReX_IntVect.H>
#include <AMReX_LayoutData.H>
#include <AMReX_MFIter.H>
#include <AMReX_Math.H>
#include <AMReX_MultiFab.H>
#include <AMReX_PODVector.H>
#include <AMReX_ParGDB.H>
#include <AMReX_ParIter.H>
#include <AMReX_ParallelDescriptor.H>
#include <AMReX_ParmParse.H>
#include <AMReX_Particle.H>
#include <AMReX_ParticleContainerBase.H>
#include <AMReX_AmrParticles.H>
#include <AMReX_ParticleTile.H>
#include <AMReX_Print.H>
#include <AMReX_Random.H>
#include <AMReX_SPACE.H>
#include <AMReX_Scan.H>
#include <AMReX_StructOfArrays.H>
#include <AMReX_TinyProfiler.H>
#include <AMReX_Utility.H>
#include <AMReX_Vector.H>
#include <AMReX_Parser.H>
#ifdef AMREX_USE_OMP
# include <omp.h>
#endif
#ifdef WARPX_USE_OPENPMD
# include <openPMD/openPMD.hpp>
#endif
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <map>
#include <random>
#include <string>
#include <utility>
#include <vector>
#include <sstream>
using namespace amrex;
namespace
{
using ParticleType = WarpXParticleContainer::ParticleType;
// Since the user provides the density distribution
// at t_lab=0 and in the lab-frame coordinates,
// we need to find the lab-frame position of this
// particle at t_lab=0, from its boosted-frame coordinates
// Assuming ballistic motion, this is given by:
// z0_lab = gamma*( z_boost*(1-beta*betaz_lab) - ct_boost*(betaz_lab-beta) )
// where betaz_lab is the speed of the particle in the lab frame
//
// In order for this equation to be solvable, betaz_lab
// is explicitly assumed to have no dependency on z0_lab
//
// Note that we use the bulk momentum to perform the ballistic correction
// Assume no z0_lab dependency
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Real applyBallisticCorrection(const XDim3& pos, const InjectorMomentum* inj_mom,
Real gamma_boost, Real beta_boost, Real t) noexcept
{
const XDim3 u_bulk = inj_mom->getBulkMomentum(pos.x, pos.y, pos.z);
const Real gamma_bulk = std::sqrt(1._rt +
(u_bulk.x*u_bulk.x+u_bulk.y*u_bulk.y+u_bulk.z*u_bulk.z));
const Real betaz_bulk = u_bulk.z/gamma_bulk;
const Real z0 = gamma_boost * ( pos.z*(1.0_rt-beta_boost*betaz_bulk)
- PhysConst::c*t*(betaz_bulk-beta_boost) );
return z0;
}
struct PDim3 {
ParticleReal x, y, z;
AMREX_GPU_HOST_DEVICE
PDim3(const PDim3&) = default;
AMREX_GPU_HOST_DEVICE
PDim3(const amrex::XDim3& a) : x(a.x), y(a.y), z(a.z) {}
AMREX_GPU_HOST_DEVICE
PDim3& operator=(const PDim3&) = default;
AMREX_GPU_HOST_DEVICE
PDim3& operator=(const amrex::XDim3 &a) { x = a.x; y = a.y; z = a.z; return *this; }
};
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
XDim3 getCellCoords (const GpuArray<Real, AMREX_SPACEDIM>& lo_corner,
const GpuArray<Real, AMREX_SPACEDIM>& dx,
const XDim3& r, const IntVect& iv) noexcept
{
XDim3 pos;
#if defined(WARPX_DIM_3D)
pos.x = lo_corner[0] + (iv[0]+r.x)*dx[0];
pos.y = lo_corner[1] + (iv[1]+r.y)*dx[1];
pos.z = lo_corner[2] + (iv[2]+r.z)*dx[2];
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
pos.x = lo_corner[0] + (iv[0]+r.x)*dx[0];
pos.y = 0.0_rt;
#if defined WARPX_DIM_XZ
pos.z = lo_corner[1] + (iv[1]+r.y)*dx[1];
#elif defined WARPX_DIM_RZ
// Note that for RZ, r.y will be theta
pos.z = lo_corner[1] + (iv[1]+r.z)*dx[1];
#endif
#else
pos.x = 0.0_rt;
pos.y = 0.0_rt;
pos.z = lo_corner[0] + (iv[0]+r.z)*dx[0];
#endif
return pos;
}
/**
* \brief This function is called in AddPlasma when we want a particle to be removed at the
* next call to redistribute. It initializes all the particle properties to zero (to be safe
* and avoid any possible undefined behavior before the next call to redistribute) and sets
* the particle id to -1 so that it can be effectively deleted.
*
* \param p particle aos data
* \param pa particle soa data
* \param ip index for soa data
* \param do_field_ionization whether species has ionization
* \param pi ionization level data
* \param has_quantum_sync whether species has quantum synchrotron
* \param p_optical_depth_QSR quantum synchrotron optical depth data
* \param has_breit_wheeler whether species has Breit-Wheeler
* \param p_optical_depth_BW Breit-Wheeler optical depth data
*/
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
void ZeroInitializeAndSetNegativeID (
ParticleType& p, const GpuArray<ParticleReal*,PIdx::nattribs>& pa, long& ip,
const bool& do_field_ionization, int* pi
#ifdef WARPX_QED
,const bool& has_quantum_sync, amrex::ParticleReal* p_optical_depth_QSR
,const bool& has_breit_wheeler, amrex::ParticleReal* p_optical_depth_BW
#endif
) noexcept
{
p.pos(0) = 0._rt;
#if (AMREX_SPACEDIM >= 2)
p.pos(1) = 0._rt;
#endif
#if defined(WARPX_DIM_3D)
p.pos(2) = 0._rt;
#endif
pa[PIdx::w ][ip] = 0._rt;
pa[PIdx::ux][ip] = 0._rt;
pa[PIdx::uy][ip] = 0._rt;
pa[PIdx::uz][ip] = 0._rt;
#ifdef WARPX_DIM_RZ
pa[PIdx::theta][ip] = 0._rt;
#endif
if (do_field_ionization) {pi[ip] = 0;}
#ifdef WARPX_QED
if (has_quantum_sync) {p_optical_depth_QSR[ip] = 0._rt;}
if (has_breit_wheeler) {p_optical_depth_BW[ip] = 0._rt;}
#endif
p.id() = -1;
}
}
PhysicalParticleContainer::PhysicalParticleContainer (AmrCore* amr_core, int ispecies,
const std::string& name)
: WarpXParticleContainer(amr_core, ispecies),
species_name(name)
{
BackwardCompatibility();
plasma_injector = std::make_unique<PlasmaInjector>(species_id, species_name);
physical_species = plasma_injector->getPhysicalSpecies();
charge = plasma_injector->getCharge();
mass = plasma_injector->getMass();
ParmParse pp_species_name(species_name);
pp_species_name.query("boost_adjust_transverse_positions", boost_adjust_transverse_positions);
pp_species_name.query("do_backward_propagation", do_backward_propagation);
pp_species_name.query("random_theta", m_rz_random_theta);
// Initialize splitting
pp_species_name.query("do_splitting", do_splitting);
pp_species_name.query("split_type", split_type);
pp_species_name.query("do_not_deposit", do_not_deposit);
pp_species_name.query("do_not_gather", do_not_gather);
pp_species_name.query("do_not_push", do_not_push);
pp_species_name.query("do_continuous_injection", do_continuous_injection);
pp_species_name.query("initialize_self_fields", initialize_self_fields);
utils::parser::queryWithParser(
pp_species_name, "self_fields_required_precision", self_fields_required_precision);
utils::parser::queryWithParser(
pp_species_name, "self_fields_absolute_tolerance", self_fields_absolute_tolerance);
utils::parser::queryWithParser(
pp_species_name, "self_fields_max_iters", self_fields_max_iters);
pp_species_name.query("self_fields_verbosity", self_fields_verbosity);
pp_species_name.query("do_field_ionization", do_field_ionization);
pp_species_name.query("do_resampling", do_resampling);
if (do_resampling) m_resampler = Resampling(species_name);
//check if Radiation Reaction is enabled and do consistency checks
pp_species_name.query("do_classical_radiation_reaction", do_classical_radiation_reaction);
//if the species is not a lepton, do_classical_radiation_reaction
//should be false
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
!(do_classical_radiation_reaction &&
!(AmIA<PhysicalSpecies::electron>() ||
AmIA<PhysicalSpecies::positron>() )),
"can't enable classical radiation reaction for non lepton species '"
+ species_name + "'.");
//Only Boris pusher is compatible with radiation reaction
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
!(do_classical_radiation_reaction &&
WarpX::particle_pusher_algo != ParticlePusherAlgo::Boris),
"Radiation reaction can be enabled only if Boris pusher is used");
//_____________________________
#ifdef WARPX_QED
pp_species_name.query("do_qed_quantum_sync", m_do_qed_quantum_sync);
if (m_do_qed_quantum_sync)
AddRealComp("opticalDepthQSR");
pp_species_name.query("do_qed_breit_wheeler", m_do_qed_breit_wheeler);
if (m_do_qed_breit_wheeler)
AddRealComp("opticalDepthBW");
if(m_do_qed_quantum_sync){
pp_species_name.get("qed_quantum_sync_phot_product_species",
m_qed_quantum_sync_phot_product_name);
}
#endif
// User-defined integer attributes
pp_species_name.queryarr("addIntegerAttributes", m_user_int_attribs);
int n_user_int_attribs = m_user_int_attribs.size();
std::vector< std::string > str_int_attrib_function;
str_int_attrib_function.resize(n_user_int_attribs);
m_user_int_attrib_parser.resize(n_user_int_attribs);
for (int i = 0; i < n_user_int_attribs; ++i) {
utils::parser::Store_parserString(
pp_species_name, "attribute."+m_user_int_attribs.at(i)+"(x,y,z,ux,uy,uz,t)",
str_int_attrib_function.at(i));
m_user_int_attrib_parser.at(i) = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_int_attrib_function.at(i),{"x","y","z","ux","uy","uz","t"}));
AddIntComp(m_user_int_attribs.at(i));
}
// User-defined real attributes
pp_species_name.queryarr("addRealAttributes", m_user_real_attribs);
int n_user_real_attribs = m_user_real_attribs.size();
std::vector< std::string > str_real_attrib_function;
str_real_attrib_function.resize(n_user_real_attribs);
m_user_real_attrib_parser.resize(n_user_real_attribs);
for (int i = 0; i < n_user_real_attribs; ++i) {
utils::parser::Store_parserString(
pp_species_name, "attribute."+m_user_real_attribs.at(i)+"(x,y,z,ux,uy,uz,t)",
str_real_attrib_function.at(i));
m_user_real_attrib_parser.at(i) = std::make_unique<amrex::Parser>(
utils::parser::makeParser(str_real_attrib_function.at(i),{"x","y","z","ux","uy","uz","t"}));
AddRealComp(m_user_real_attribs.at(i));
}
// If old particle positions should be saved add the needed components
pp_species_name.query("save_previous_position", m_save_previous_position);
if (m_save_previous_position) {
#if (AMREX_SPACEDIM >= 2)
AddRealComp("prev_x");
#endif
#if defined(WARPX_DIM_3D)
AddRealComp("prev_y");
#endif
AddRealComp("prev_z");
#ifdef WARPX_DIM_RZ
amrex::Abort("Saving previous particle positions not yet implemented in RZ");
#endif
}
// Read reflection models for absorbing boundaries; defaults to a zero
pp_species_name.query("reflection_model_xlo(E)", m_boundary_conditions.reflection_model_xlo_str);
pp_species_name.query("reflection_model_xhi(E)", m_boundary_conditions.reflection_model_xhi_str);
pp_species_name.query("reflection_model_ylo(E)", m_boundary_conditions.reflection_model_ylo_str);
pp_species_name.query("reflection_model_yhi(E)", m_boundary_conditions.reflection_model_yhi_str);
pp_species_name.query("reflection_model_zlo(E)", m_boundary_conditions.reflection_model_zlo_str);
pp_species_name.query("reflection_model_zhi(E)", m_boundary_conditions.reflection_model_zhi_str);
m_boundary_conditions.BuildReflectionModelParsers();
ParmParse pp_boundary("boundary");
bool flag = false;
pp_boundary.query("reflect_all_velocities", flag);
m_boundary_conditions.Set_reflect_all_velocities(flag);
}
PhysicalParticleContainer::PhysicalParticleContainer (AmrCore* amr_core)
: WarpXParticleContainer(amr_core, 0)
{
plasma_injector = std::make_unique<PlasmaInjector>();
}
void
PhysicalParticleContainer::BackwardCompatibility ()
{
ParmParse pp_species_name(species_name);
std::vector<std::string> backward_strings;
if (pp_species_name.queryarr("plot_vars", backward_strings)){
amrex::Abort("<species>.plot_vars is not supported anymore. "
"Please use the new syntax for diagnostics, see documentation.");
}
int backward_int;
if (pp_species_name.query("plot_species", backward_int)){
amrex::Abort("<species>.plot_species is not supported anymore. "
"Please use the new syntax for diagnostics, see documentation.");
}
}
void PhysicalParticleContainer::InitData ()
{
AddParticles(0); // Note - add on level 0
Redistribute(); // We then redistribute
}
void PhysicalParticleContainer::MapParticletoBoostedFrame (
ParticleReal& x, ParticleReal& y, ParticleReal& z, ParticleReal& ux, ParticleReal& uy, ParticleReal& uz)
{
// Map the particles from the lab frame to the boosted frame.
// This boosts the particle to the lab frame and calculates
// the particle time in the boosted frame. It then maps
// the position to the time in the boosted frame.
// For now, start with the assumption that this will only happen
// at the start of the simulation.
const ParticleReal t_lab = 0._prt;
const ParticleReal uz_boost = WarpX::gamma_boost*WarpX::beta_boost*PhysConst::c;
// tpr is the particle's time in the boosted frame
ParticleReal tpr = WarpX::gamma_boost*t_lab - uz_boost*z/(PhysConst::c*PhysConst::c);
// The particle's transformed location in the boosted frame
ParticleReal xpr = x;
ParticleReal ypr = y;
ParticleReal zpr = WarpX::gamma_boost*z - uz_boost*t_lab;
// transform u and gamma to the boosted frame
ParticleReal gamma_lab = std::sqrt(1._rt + (ux*ux + uy*uy + uz*uz)/(PhysConst::c*PhysConst::c));
// ux = ux;
// uy = uy;
uz = WarpX::gamma_boost*uz - uz_boost*gamma_lab;
ParticleReal gammapr = std::sqrt(1._rt + (ux*ux + uy*uy + uz*uz)/(PhysConst::c*PhysConst::c));
ParticleReal vxpr = ux/gammapr;
ParticleReal vypr = uy/gammapr;
ParticleReal vzpr = uz/gammapr;
if (do_backward_propagation){
uz = -uz;
}
// Move the particles to where they will be at t = 0 in the boosted frame
if (boost_adjust_transverse_positions) {
x = xpr - tpr*vxpr;
y = ypr - tpr*vypr;
}
z = zpr - tpr*vzpr;
}
void
PhysicalParticleContainer::AddGaussianBeam (
const Real x_m, const Real y_m, const Real z_m,
const Real x_rms, const Real y_rms, const Real z_rms,
const Real x_cut, const Real y_cut, const Real z_cut,
const Real q_tot, long npart,
const int do_symmetrize) {
// Declare temporary vectors on the CPU
Gpu::HostVector<ParticleReal> particle_x;
Gpu::HostVector<ParticleReal> particle_y;
Gpu::HostVector<ParticleReal> particle_z;
Gpu::HostVector<ParticleReal> particle_ux;
Gpu::HostVector<ParticleReal> particle_uy;
Gpu::HostVector<ParticleReal> particle_uz;
Gpu::HostVector<ParticleReal> particle_w;
int np = 0;
if (ParallelDescriptor::IOProcessor()) {
// If do_symmetrize, create 4x fewer particles, and
// Replicate each particle 4 times (x,y) (-x,y) (x,-y) (-x,-y)
if (do_symmetrize){
npart /= 4;
}
for (long i = 0; i < npart; ++i) {
#if defined(WARPX_DIM_3D) || defined(WARPX_DIM_RZ)
const Real weight = q_tot/(npart*charge);
const Real x = amrex::RandomNormal(x_m, x_rms);
const Real y = amrex::RandomNormal(y_m, y_rms);
const Real z = amrex::RandomNormal(z_m, z_rms);
#elif defined(WARPX_DIM_XZ)
const Real weight = q_tot/(npart*charge*y_rms);
const Real x = amrex::RandomNormal(x_m, x_rms);
constexpr Real y = 0._prt;
const Real z = amrex::RandomNormal(z_m, z_rms);
#elif defined(WARPX_DIM_1D_Z)
const Real weight = q_tot/(npart*charge*x_rms*y_rms);
constexpr Real x = 0._prt;
constexpr Real y = 0._prt;
const Real z = amrex::RandomNormal(z_m, z_rms);
#endif
if (plasma_injector->insideBounds(x, y, z) &&
std::abs( x - x_m ) <= x_cut * x_rms &&
std::abs( y - y_m ) <= y_cut * y_rms &&
std::abs( z - z_m ) <= z_cut * z_rms ) {
XDim3 u = plasma_injector->getMomentum(x, y, z);
u.x *= PhysConst::c;
u.y *= PhysConst::c;
u.z *= PhysConst::c;
if (do_symmetrize){
// Add four particles to the beam:
CheckAndAddParticle(x, y, z, u.x, u.y, u.z, weight/4._rt,
particle_x, particle_y, particle_z,
particle_ux, particle_uy, particle_uz,
particle_w);
CheckAndAddParticle(x, -y, z, u.x, -u.y, u.z, weight/4._rt,
particle_x, particle_y, particle_z,
particle_ux, particle_uy, particle_uz,
particle_w);
CheckAndAddParticle(-x, y, z, -u.x, u.y, u.z, weight/4._rt,
particle_x, particle_y, particle_z,
particle_ux, particle_uy, particle_uz,
particle_w);
CheckAndAddParticle(-x, -y, z, -u.x, -u.y, u.z, weight/4._rt,
particle_x, particle_y, particle_z,
particle_ux, particle_uy, particle_uz,
particle_w);
} else {
CheckAndAddParticle(x, y, z, u.x, u.y, u.z, weight,
particle_x, particle_y, particle_z,
particle_ux, particle_uy, particle_uz,
particle_w);
}
}
}
}
// Add the temporary CPU vectors to the particle structure
np = particle_z.size();
AddNParticles(0,np,
particle_x.dataPtr(), particle_y.dataPtr(), particle_z.dataPtr(),
particle_ux.dataPtr(), particle_uy.dataPtr(), particle_uz.dataPtr(),
1, particle_w.dataPtr(), 0, nullptr, 1);
}
void
PhysicalParticleContainer::AddPlasmaFromFile(ParticleReal q_tot,
ParticleReal z_shift)
{
// Declare temporary vectors on the CPU
Gpu::HostVector<ParticleReal> particle_x;
Gpu::HostVector<ParticleReal> particle_z;
Gpu::HostVector<ParticleReal> particle_ux;
Gpu::HostVector<ParticleReal> particle_uz;
Gpu::HostVector<ParticleReal> particle_w;
Gpu::HostVector<ParticleReal> particle_y;
Gpu::HostVector<ParticleReal> particle_uy;
#ifdef WARPX_USE_OPENPMD
//TODO: Make changes for read/write in multiple MPI ranks
if (ParallelDescriptor::IOProcessor()) {
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(plasma_injector,
"AddPlasmaFromFile: plasma injector not initialized.\n");
// take ownership of the series and close it when done
auto series = std::move(plasma_injector->m_openpmd_input_series);
// assumption asserts: see PlasmaInjector
openPMD::Iteration it = series->iterations.begin()->second;
std::string const ps_name = it.particles.begin()->first;
openPMD::ParticleSpecies ps = it.particles.begin()->second;
auto const npart = ps["position"]["x"].getExtent()[0];
#if !defined(WARPX_DIM_1D_Z)
std::shared_ptr<ParticleReal> ptr_x = ps["position"]["x"].loadChunk<ParticleReal>();
double const position_unit_x = ps["position"]["x"].unitSI();
#endif
std::shared_ptr<ParticleReal> ptr_z = ps["position"]["z"].loadChunk<ParticleReal>();
double const position_unit_z = ps["position"]["z"].unitSI();
std::shared_ptr<ParticleReal> ptr_ux = ps["momentum"]["x"].loadChunk<ParticleReal>();
double const momentum_unit_x = ps["momentum"]["x"].unitSI();
std::shared_ptr<ParticleReal> ptr_uz = ps["momentum"]["z"].loadChunk<ParticleReal>();
double const momentum_unit_z = ps["momentum"]["z"].unitSI();
# if !(defined(WARPX_DIM_XZ) || defined(WARPX_DIM_1D_Z))
std::shared_ptr<ParticleReal> ptr_y = ps["position"]["y"].loadChunk<ParticleReal>();
double const position_unit_y = ps["position"]["y"].unitSI();
#endif
std::shared_ptr<ParticleReal> ptr_uy = nullptr;
double momentum_unit_y = 1.0;
if (ps["momentum"].contains("y")) {
ptr_uy = ps["momentum"]["y"].loadChunk<ParticleReal>();
momentum_unit_y = ps["momentum"]["y"].unitSI();
}
series->flush(); // shared_ptr data can be read now
ParticleReal weight = 1.0_prt; // base standard: no info means "real" particles
if (q_tot != 0.0) {
weight = std::abs(q_tot) / ( std::abs(charge) * ParticleReal(npart) );
if (ps.contains("weighting")) {
std::stringstream ss;
ss << "Both '" << ps_name << ".q_tot' and '"
<< ps_name << ".injection_file' specify a total charge.\n'"
<< ps_name << ".q_tot' will take precedence.";
ablastr::warn_manager::WMRecordWarning("Species", ss.str());
}
}
// ED-PIC extension?
else if (ps.contains("weighting")) {
// TODO: Add ASSERT_WITH_MESSAGE to test if weighting is a constant record
// TODO: Add ASSERT_WITH_MESSAGE for macroWeighted value in ED-PIC
ParticleReal w = ps["weighting"][openPMD::RecordComponent::SCALAR].loadChunk<ParticleReal>().get()[0];
double const w_unit = ps["weighting"][openPMD::RecordComponent::SCALAR].unitSI();
weight = w * w_unit;
}
for (auto i = decltype(npart){0}; i<npart; ++i){
#if !defined(WARPX_DIM_1D_Z)
ParticleReal const x = ptr_x.get()[i]*position_unit_x;
#else
ParticleReal const x = 0.0_prt;
#endif
ParticleReal const z = ptr_z.get()[i]*position_unit_z+z_shift;
#if defined(WARPX_DIM_3D) || defined(WARPX_DIM_RZ)
ParticleReal const y = ptr_y.get()[i]*position_unit_y;
#else
ParticleReal const y = 0.0_prt;
#endif
if (plasma_injector->insideBounds(x, y, z)) {
ParticleReal const ux = ptr_ux.get()[i]*momentum_unit_x/PhysConst::m_e;
ParticleReal const uz = ptr_uz.get()[i]*momentum_unit_z/PhysConst::m_e;
ParticleReal uy = 0.0_prt;
if (ps["momentum"].contains("y")) {
uy = ptr_uy.get()[i]*momentum_unit_y/PhysConst::m_e;
}
CheckAndAddParticle(x, y, z, ux, uy, uz, weight,
particle_x, particle_y, particle_z,
particle_ux, particle_uy, particle_uz,
particle_w);
}
}
auto const np = particle_z.size();
if (np < npart) {
ablastr::warn_manager::WMRecordWarning("Species",
"Simulation box doesn't cover all particles",
ablastr::warn_manager::WarnPriority::high);
}
} // IO Processor
auto const np = particle_z.size();
AddNParticles(0, np,
particle_x.dataPtr(), particle_y.dataPtr(), particle_z.dataPtr(),
particle_ux.dataPtr(), particle_uy.dataPtr(), particle_uz.dataPtr(),
1, particle_w.dataPtr(), 0, nullptr, 1);
#endif // WARPX_USE_OPENPMD
ignore_unused(q_tot, z_shift);
return;
}
void
PhysicalParticleContainer::DefaultInitializeRuntimeAttributes (
amrex::ParticleTile<NStructReal, NStructInt, NArrayReal,
NArrayInt,amrex::PinnedArenaAllocator>& pinned_tile,
const int n_external_attr_real,
const int n_external_attr_int,
const amrex::RandomEngine& engine)
{
using namespace amrex::literals;
const int np = pinned_tile.numParticles();
// Preparing data needed for user defined attributes
const int n_user_real_attribs = m_user_real_attribs.size();
const int n_user_int_attribs = m_user_int_attribs.size();
const auto get_position = GetParticlePosition(pinned_tile);
const auto soa = pinned_tile.getParticleTileData();
const amrex::ParticleReal* AMREX_RESTRICT ux = soa.m_rdata[PIdx::ux];
const amrex::ParticleReal* AMREX_RESTRICT uy = soa.m_rdata[PIdx::uy];
const amrex::ParticleReal* AMREX_RESTRICT uz = soa.m_rdata[PIdx::uz];
constexpr int lev = 0;
const amrex::Real t = WarpX::GetInstance().gett_new(lev);
#ifndef WARPX_QED
amrex::ignore_unused(engine);
#endif
// Initialize the last NumRuntimeRealComps() - n_external_attr_real runtime real attributes
for (int j = PIdx::nattribs + n_external_attr_real; j < NumRealComps() ; ++j)
{
amrex::Vector<amrex::ParticleReal> attr_temp(np, 0.0_prt);
#ifdef WARPX_QED
// Current runtime comp is quantum synchrotron optical depth
if (particle_comps.find("opticalDepthQSR") != particle_comps.end() &&
particle_comps["opticalDepthQSR"] == j)
{
const QuantumSynchrotronGetOpticalDepth quantum_sync_get_opt =
m_shr_p_qs_engine->build_optical_depth_functor();;
for (int i = 0; i < np; ++i) {
attr_temp[i] = quantum_sync_get_opt(engine);
}
}
// Current runtime comp is Breit-Wheeler optical depth
if (particle_comps.find("opticalDepthBW") != particle_comps.end() &&
particle_comps["opticalDepthBW"] == j)
{
const BreitWheelerGetOpticalDepth breit_wheeler_get_opt =
m_shr_p_bw_engine->build_optical_depth_functor();;
for (int i = 0; i < np; ++i) {
attr_temp[i] = breit_wheeler_get_opt(engine);
}
}
#endif
for (int ia = 0; ia < n_user_real_attribs; ++ia)
{
// Current runtime comp is ia-th user defined attribute
if (particle_comps.find(m_user_real_attribs[ia]) != particle_comps.end() &&
particle_comps[m_user_real_attribs[ia]] == j)
{
amrex::ParticleReal xp, yp, zp;
const amrex::ParserExecutor<7> user_real_attrib_parserexec =
m_user_real_attrib_parser[ia]->compile<7>();
for (int i = 0; i < np; ++i) {
get_position(i, xp, yp, zp);
attr_temp[i] = user_real_attrib_parserexec(xp, yp, zp,
ux[i], uy[i], uz[i], t);
}
}
}
pinned_tile.push_back_real(j, attr_temp.data(), attr_temp.data() + np);
}
// Initialize the last NumRuntimeIntComps() - n_external_attr_int runtime int attributes
for (int j = n_external_attr_int; j < NumIntComps() ; ++j)
{
amrex::Vector<int> attr_temp(np, 0);
// Current runtime comp is ionization level
if (particle_icomps.find("ionizationLevel") != particle_icomps.end() &&
particle_icomps["ionizationLevel"] == j)
{
for (int i = 0; i < np; ++i) {
attr_temp[i] = ionization_initial_level;
}
}
for (int ia = 0; ia < n_user_int_attribs; ++ia)
{
// Current runtime comp is ia-th user defined attribute
if (particle_icomps.find(m_user_int_attribs[ia]) != particle_icomps.end() &&
particle_icomps[m_user_int_attribs[ia]] == j)
{
amrex::ParticleReal xp, yp, zp;
const amrex::ParserExecutor<7> user_int_attrib_parserexec =
m_user_int_attrib_parser[ia]->compile<7>();
for (int i = 0; i < np; ++i) {
get_position(i, xp, yp, zp);
attr_temp[i] = static_cast<int>(
user_int_attrib_parserexec(xp, yp, zp, ux[i], uy[i], uz[i], t));
}
}
}
pinned_tile.push_back_int(j, attr_temp.data(), attr_temp.data() + np);
}
}
void
PhysicalParticleContainer::CheckAndAddParticle (
ParticleReal x, ParticleReal y, ParticleReal z,
ParticleReal ux, ParticleReal uy, ParticleReal uz,
ParticleReal weight,
Gpu::HostVector<ParticleReal>& particle_x,
Gpu::HostVector<ParticleReal>& particle_y,
Gpu::HostVector<ParticleReal>& particle_z,
Gpu::HostVector<ParticleReal>& particle_ux,
Gpu::HostVector<ParticleReal>& particle_uy,
Gpu::HostVector<ParticleReal>& particle_uz,
Gpu::HostVector<ParticleReal>& particle_w)
{
if (WarpX::gamma_boost > 1.) {
MapParticletoBoostedFrame(x, y, z, ux, uy, uz);
}
particle_x.push_back(x);
particle_y.push_back(y);
particle_z.push_back(z);
particle_ux.push_back(ux);
particle_uy.push_back(uy);
particle_uz.push_back(uz);
particle_w.push_back(weight);
}
void
PhysicalParticleContainer::AddParticles (int lev)
{
WARPX_PROFILE("PhysicalParticleContainer::AddParticles()");
if (plasma_injector->add_single_particle) {
if (WarpX::gamma_boost > 1.) {
MapParticletoBoostedFrame(plasma_injector->single_particle_pos[0],
plasma_injector->single_particle_pos[1],
plasma_injector->single_particle_pos[2],
plasma_injector->single_particle_vel[0],
plasma_injector->single_particle_vel[1],
plasma_injector->single_particle_vel[2]);
}
AddNParticles(lev, 1,
&(plasma_injector->single_particle_pos[0]),
&(plasma_injector->single_particle_pos[1]),
&(plasma_injector->single_particle_pos[2]),
&(plasma_injector->single_particle_vel[0]),
&(plasma_injector->single_particle_vel[1]),
&(plasma_injector->single_particle_vel[2]),
1, &(plasma_injector->single_particle_weight), 0, nullptr, 0);
return;
}
if (plasma_injector->add_multiple_particles) {
if (WarpX::gamma_boost > 1.) {
for (int i=0 ; i < plasma_injector->multiple_particles_pos_x.size() ; i++) {
MapParticletoBoostedFrame(plasma_injector->multiple_particles_pos_x[i],
plasma_injector->multiple_particles_pos_y[i],
plasma_injector->multiple_particles_pos_z[i],
plasma_injector->multiple_particles_vel_x[i],
plasma_injector->multiple_particles_vel_y[i],
plasma_injector->multiple_particles_vel_z[i]);
}
}
AddNParticles(lev, plasma_injector->multiple_particles_pos_x.size(),
plasma_injector->multiple_particles_pos_x.dataPtr(),
plasma_injector->multiple_particles_pos_y.dataPtr(),
plasma_injector->multiple_particles_pos_z.dataPtr(),
plasma_injector->multiple_particles_vel_x.dataPtr(),
plasma_injector->multiple_particles_vel_y.dataPtr(),
plasma_injector->multiple_particles_vel_z.dataPtr(),
1, plasma_injector->multiple_particles_weight.dataPtr(), 0, nullptr, 0);
return;
}
if (plasma_injector->gaussian_beam) {
AddGaussianBeam(plasma_injector->x_m,
plasma_injector->y_m,
plasma_injector->z_m,
plasma_injector->x_rms,
plasma_injector->y_rms,
plasma_injector->z_rms,
plasma_injector->x_cut,
plasma_injector->y_cut,
plasma_injector->z_cut,
plasma_injector->q_tot,
plasma_injector->npart,
plasma_injector->do_symmetrize);
return;
}
if (plasma_injector->external_file) {
AddPlasmaFromFile(plasma_injector->q_tot,
plasma_injector->z_shift);
return;
}
if ( plasma_injector->doInjection() ) {
AddPlasma( lev );
}
}
void
PhysicalParticleContainer::AddPlasma (int lev, RealBox part_realbox)
{
WARPX_PROFILE("PhysicalParticleContainer::AddPlasma()");
// If no part_realbox is provided, initialize particles in the whole domain
const Geometry& geom = Geom(lev);
if (!part_realbox.ok()) part_realbox = geom.ProbDomain();
int num_ppc = plasma_injector->num_particles_per_cell;
#ifdef WARPX_DIM_RZ
Real rmax = std::min(plasma_injector->xmax, part_realbox.hi(0));
#endif
const auto dx = geom.CellSizeArray();
const auto problo = geom.ProbLoArray();
defineAllParticleTiles();
amrex::LayoutData<amrex::Real>* cost = WarpX::getCosts(lev);
const int nlevs = numLevels();
static bool refine_injection = false;
static Box fine_injection_box;
static int rrfac = 1;
// This does not work if the mesh is dynamic. But in that case, we should
// not use refined injected either. We also assume there is only one fine level.
if (WarpX::moving_window_active(WarpX::GetInstance().getistep(0)+1) and WarpX::refine_plasma
and do_continuous_injection and nlevs == 2)
{
refine_injection = true;
fine_injection_box = ParticleBoxArray(1).minimalBox();
fine_injection_box.setSmall(WarpX::moving_window_dir, std::numeric_limits<int>::lowest());
fine_injection_box.setBig(WarpX::moving_window_dir, std::numeric_limits<int>::max());
rrfac = m_gdb->refRatio(0)[0];
fine_injection_box.coarsen(rrfac);
}
InjectorPosition* inj_pos = plasma_injector->getInjectorPosition();
InjectorDensity* inj_rho = plasma_injector->getInjectorDensity();
InjectorMomentum* inj_mom = plasma_injector->getInjectorMomentum();
Real gamma_boost = WarpX::gamma_boost;
Real beta_boost = WarpX::beta_boost;
Real t = WarpX::GetInstance().gett_new(lev);
Real density_min = plasma_injector->density_min;
Real density_max = plasma_injector->density_max;
#ifdef WARPX_DIM_RZ
const int nmodes = WarpX::n_rz_azimuthal_modes;
bool radially_weighted = plasma_injector->radially_weighted;
#endif
MFItInfo info;
if (do_tiling && Gpu::notInLaunchRegion()) {
info.EnableTiling(tile_size);
}
#ifdef AMREX_USE_OMP
info.SetDynamic(true);
#pragma omp parallel if (not WarpX::serialize_initial_conditions)
#endif
for (MFIter mfi = MakeMFIter(lev, info); mfi.isValid(); ++mfi)
{
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
amrex::Gpu::synchronize();
}
Real wt = amrex::second();
const Box& tile_box = mfi.tilebox();
const RealBox tile_realbox = WarpX::getRealBox(tile_box, lev);
// Find the cells of part_box that overlap with tile_realbox
// If there is no overlap, just go to the next tile in the loop
RealBox overlap_realbox;
Box overlap_box;
IntVect shifted;
bool no_overlap = false;
for (int dir=0; dir<AMREX_SPACEDIM; dir++) {
if ( tile_realbox.lo(dir) <= part_realbox.hi(dir) ) {
Real ncells_adjust = std::floor( (tile_realbox.lo(dir) - part_realbox.lo(dir))/dx[dir] );
overlap_realbox.setLo( dir, part_realbox.lo(dir) + std::max(ncells_adjust, 0._rt) * dx[dir]);
} else {
no_overlap = true; break;
}
if ( tile_realbox.hi(dir) >= part_realbox.lo(dir) ) {
Real ncells_adjust = std::floor( (part_realbox.hi(dir) - tile_realbox.hi(dir))/dx[dir] );
overlap_realbox.setHi( dir, part_realbox.hi(dir) - std::max(ncells_adjust, 0._rt) * dx[dir]);
} else {
no_overlap = true; break;
}
// Count the number of cells in this direction in overlap_realbox
overlap_box.setSmall( dir, 0 );
overlap_box.setBig( dir,
int( std::round((overlap_realbox.hi(dir)-overlap_realbox.lo(dir))
/dx[dir] )) - 1);
shifted[dir] =
static_cast<int>(std::round((overlap_realbox.lo(dir)-problo[dir])/dx[dir]));
// shifted is exact in non-moving-window direction. That's all we care.
}
if (no_overlap == 1) {
continue; // Go to the next tile
}
const int grid_id = mfi.index();
const int tile_id = mfi.LocalTileIndex();
const GpuArray<Real,AMREX_SPACEDIM> overlap_corner
{AMREX_D_DECL(overlap_realbox.lo(0),
overlap_realbox.lo(1),
overlap_realbox.lo(2))};
// count the number of particles that each cell in overlap_box could add
Gpu::DeviceVector<int> counts(overlap_box.numPts(), 0);
Gpu::DeviceVector<int> offset(overlap_box.numPts());
auto pcounts = counts.data();
int lrrfac = rrfac;
Box fine_overlap_box; // default Box is NOT ok().
if (refine_injection) {
fine_overlap_box = overlap_box & amrex::shift(fine_injection_box, -shifted);
}
amrex::ParallelFor(overlap_box, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
{
IntVect iv(AMREX_D_DECL(i, j, k));
auto lo = getCellCoords(overlap_corner, dx, {0._rt, 0._rt, 0._rt}, iv);
auto hi = getCellCoords(overlap_corner, dx, {1._rt, 1._rt, 1._rt}, iv);
lo.z = applyBallisticCorrection(lo, inj_mom, gamma_boost, beta_boost, t);
hi.z = applyBallisticCorrection(hi, inj_mom, gamma_boost, beta_boost, t);
if (inj_pos->overlapsWith(lo, hi))
{
auto index = overlap_box.index(iv);
int r;
if (fine_overlap_box.ok() && fine_overlap_box.contains(iv)) {
r = AMREX_D_TERM(lrrfac,*lrrfac,*lrrfac);
} else {
r = 1;
}
pcounts[index] = num_ppc*r;
}
#if defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
amrex::ignore_unused(k);
#endif
#if defined(WARPX_DIM_1D_Z)
amrex::ignore_unused(j,k);
#endif
});
// Max number of new particles. All of them are created,
// and invalid ones are then discarded
int max_new_particles = Scan::ExclusiveSum(counts.size(), counts.data(), offset.data());
// Update NextID to include particles created in this function
Long pid;
#ifdef AMREX_USE_OMP
#pragma omp critical (add_plasma_nextid)
#endif
{
pid = ParticleType::NextID();
ParticleType::NextID(pid+max_new_particles);
}
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
static_cast<Long>(pid + max_new_particles) < LastParticleID,
"ERROR: overflow on particle id numbers");
const int cpuid = ParallelDescriptor::MyProc();
auto& particle_tile = GetParticles(lev)[std::make_pair(grid_id,tile_id)];
if ( (NumRuntimeRealComps()>0) || (NumRuntimeIntComps()>0) ) {
DefineAndReturnParticleTile(lev, grid_id, tile_id);
}
auto old_size = particle_tile.GetArrayOfStructs().size();
auto new_size = old_size + max_new_particles;
particle_tile.resize(new_size);
ParticleType* pp = particle_tile.GetArrayOfStructs()().data() + old_size;
auto& soa = particle_tile.GetStructOfArrays();
GpuArray<ParticleReal*,PIdx::nattribs> pa;
for (int ia = 0; ia < PIdx::nattribs; ++ia) {
pa[ia] = soa.GetRealData(ia).data() + old_size;
}
// user-defined integer and real attributes
const int n_user_int_attribs = m_user_int_attribs.size();
const int n_user_real_attribs = m_user_real_attribs.size();
amrex::Gpu::DeviceVector<int*> pa_user_int(n_user_int_attribs);
amrex::Gpu::DeviceVector<ParticleReal*> pa_user_real(n_user_real_attribs);
amrex::Gpu::DeviceVector< amrex::ParserExecutor<7> > user_int_attrib_parserexec(n_user_int_attribs);
amrex::Gpu::DeviceVector< amrex::ParserExecutor<7> > user_real_attrib_parserexec(n_user_real_attribs);
for (int ia = 0; ia < n_user_int_attribs; ++ia) {
pa_user_int[ia] = soa.GetIntData(particle_icomps[m_user_int_attribs[ia]]).data() + old_size;
user_int_attrib_parserexec[ia] = m_user_int_attrib_parser[ia]->compile<7>();
}
for (int ia = 0; ia < n_user_real_attribs; ++ia) {
pa_user_real[ia] = soa.GetRealData(particle_comps[m_user_real_attribs[ia]]).data() + old_size;
user_real_attrib_parserexec[ia] = m_user_real_attrib_parser[ia]->compile<7>();
}
int** pa_user_int_data = pa_user_int.dataPtr();
ParticleReal** pa_user_real_data = pa_user_real.dataPtr();
amrex::ParserExecutor<7> const* user_int_parserexec_data = user_int_attrib_parserexec.dataPtr();
amrex::ParserExecutor<7> const* user_real_parserexec_data = user_real_attrib_parserexec.dataPtr();
int* pi = nullptr;
if (do_field_ionization) {
pi = soa.GetIntData(particle_icomps["ionizationLevel"]).data() + old_size;
}
#ifdef WARPX_QED
//Pointer to the optical depth component
amrex::ParticleReal* p_optical_depth_QSR = nullptr;
amrex::ParticleReal* p_optical_depth_BW = nullptr;
// If a QED effect is enabled, the corresponding optical depth
// has to be initialized
bool loc_has_quantum_sync = has_quantum_sync();
bool loc_has_breit_wheeler = has_breit_wheeler();
if (loc_has_quantum_sync)
p_optical_depth_QSR = soa.GetRealData(
particle_comps["opticalDepthQSR"]).data() + old_size;
if(loc_has_breit_wheeler)
p_optical_depth_BW = soa.GetRealData(
particle_comps["opticalDepthBW"]).data() + old_size;
//If needed, get the appropriate functors from the engines
QuantumSynchrotronGetOpticalDepth quantum_sync_get_opt;
BreitWheelerGetOpticalDepth breit_wheeler_get_opt;
if(loc_has_quantum_sync){
quantum_sync_get_opt =
m_shr_p_qs_engine->build_optical_depth_functor();
}
if(loc_has_breit_wheeler){
breit_wheeler_get_opt =
m_shr_p_bw_engine->build_optical_depth_functor();
}
#endif
bool loc_do_field_ionization = do_field_ionization;
int loc_ionization_initial_level = ionization_initial_level;
// Loop over all new particles and inject them (creates too many
// particles, in particular does not consider xmin, xmax etc.).
// The invalid ones are given negative ID and are deleted during the
// next redistribute.
const auto poffset = offset.data();
#ifdef WARPX_DIM_RZ
const bool rz_random_theta = m_rz_random_theta;
#endif
amrex::ParallelForRNG(overlap_box,
[=] AMREX_GPU_DEVICE (int i, int j, int k, amrex::RandomEngine const& engine) noexcept
{
IntVect iv = IntVect(AMREX_D_DECL(i, j, k));
const auto index = overlap_box.index(iv);
#ifdef WARPX_DIM_RZ
Real theta_offset = 0._rt;
if (rz_random_theta) theta_offset = amrex::Random(engine) * 2._rt * MathConst::pi;
#endif
Real scale_fac = 0.0_rt;
if( pcounts[index] != 0) {
#if defined(WARPX_DIM_3D)
scale_fac = dx[0]*dx[1]*dx[2]/pcounts[index];
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
scale_fac = dx[0]*dx[1]/pcounts[index];
#elif defined(WARPX_DIM_1D_Z)
scale_fac = dx[0]/pcounts[index];
#endif
}
for (int i_part = 0; i_part < pcounts[index]; ++i_part)
{
long ip = poffset[index] + i_part;
ParticleType& p = pp[ip];
p.id() = pid+ip;
p.cpu() = cpuid;
const XDim3 r = (fine_overlap_box.ok() && fine_overlap_box.contains(iv)) ?
// In the refined injection region: use refinement ratio `lrrfac`
inj_pos->getPositionUnitBox(i_part, lrrfac, engine) :
// Otherwise: use 1 as the refinement ratio
inj_pos->getPositionUnitBox(i_part, 1, engine);
auto pos = getCellCoords(overlap_corner, dx, r, iv);
#if defined(WARPX_DIM_3D)
if (!tile_realbox.contains(XDim3{pos.x,pos.y,pos.z})) {
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
amrex::ignore_unused(k);
if (!tile_realbox.contains(XDim3{pos.x,pos.z,0.0_rt})) {
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
#else
amrex::ignore_unused(j,k);
if (!tile_realbox.contains(XDim3{pos.z,0.0_rt,0.0_rt})) {
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
#endif
// Save the x and y values to use in the insideBounds checks.
// This is needed with WARPX_DIM_RZ since x and y are modified.
Real xb = pos.x;
Real yb = pos.y;
#ifdef WARPX_DIM_RZ
// Replace the x and y, setting an angle theta.
// These x and y are used to get the momentum and density
Real theta;
if (nmodes == 1) {
// With only 1 mode, the angle doesn't matter so
// choose it randomly.
theta = 2._rt*MathConst::pi*amrex::Random(engine);
} else {
theta = 2._rt*MathConst::pi*r.y + theta_offset;
}
pos.x = xb*std::cos(theta);
pos.y = xb*std::sin(theta);
#endif
Real dens;
XDim3 u;
if (gamma_boost == 1._rt) {
// Lab-frame simulation
// If the particle is not within the species's
// xmin, xmax, ymin, ymax, zmin, zmax, go to
// the next generated particle.
// include ballistic correction for plasma species with bulk motion
const Real z0 = applyBallisticCorrection(pos, inj_mom, gamma_boost,
beta_boost, t);
if (!inj_pos->insideBounds(xb, yb, z0)) {
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
u = inj_mom->getMomentum(pos.x, pos.y, z0, engine);
dens = inj_rho->getDensity(pos.x, pos.y, z0);
// Remove particle if density below threshold
if ( dens < density_min ){
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
// Cut density if above threshold
dens = amrex::min(dens, density_max);
} else {
// Boosted-frame simulation
const Real z0_lab = applyBallisticCorrection(pos, inj_mom, gamma_boost,
beta_boost, t);
// If the particle is not within the lab-frame zmin, zmax, etc.
// go to the next generated particle.
if (!inj_pos->insideBounds(xb, yb, z0_lab)) {
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
// call `getDensity` with lab-frame parameters
dens = inj_rho->getDensity(pos.x, pos.y, z0_lab);
// Remove particle if density below threshold
if ( dens < density_min ){
ZeroInitializeAndSetNegativeID(p, pa, ip, loc_do_field_ionization, pi
#ifdef WARPX_QED
,loc_has_quantum_sync, p_optical_depth_QSR
,loc_has_breit_wheeler, p_optical_depth_BW
#endif
);
continue;
}
// Cut density if above threshold
dens = amrex::min(dens, density_max);
// get the full momentum, including thermal motion
u = inj_mom->getMomentum(pos.x, pos.y, 0._rt, engine);
const Real gamma_lab = std::sqrt( 1._rt+(u.x*u.x+u.y*u.y+u.z*u.z) );
const Real betaz_lab = u.z/(gamma_lab);
// At this point u and dens are the lab-frame quantities
// => Perform Lorentz transform
dens = gamma_boost * dens * ( 1.0_rt - beta_boost*betaz_lab );
u.z = gamma_boost * ( u.z -beta_boost*gamma_lab );
}
if (loc_do_field_ionization) {
pi[ip] = loc_ionization_initial_level;
}
#ifdef WARPX_QED
if(loc_has_quantum_sync){
p_optical_depth_QSR[ip] = quantum_sync_get_opt(engine);
}
if(loc_has_breit_wheeler){
p_optical_depth_BW[ip] = breit_wheeler_get_opt(engine);
}
#endif
// Initialize user-defined integers with user-defined parser
for (int ia = 0; ia < n_user_int_attribs; ++ia) {
pa_user_int_data[ia][ip] = static_cast<int>(user_int_parserexec_data[ia](pos.x, pos.y, pos.z, u.x, u.y, u.z, t));
}
// Initialize user-defined real attributes with user-defined parser
for (int ia = 0; ia < n_user_real_attribs; ++ia) {
pa_user_real_data[ia][ip] = user_real_parserexec_data[ia](pos.x, pos.y, pos.z, u.x, u.y, u.z, t);
}
u.x *= PhysConst::c;
u.y *= PhysConst::c;
u.z *= PhysConst::c;
Real weight = dens * scale_fac;
#ifdef WARPX_DIM_RZ
if (radially_weighted) {
weight *= 2._rt*MathConst::pi*xb;
} else {
// This is not correct since it might shift the particle
// out of the local grid
pos.x = std::sqrt(xb*rmax);
weight *= dx[0];
}
#endif
pa[PIdx::w ][ip] = weight;
pa[PIdx::ux][ip] = u.x;
pa[PIdx::uy][ip] = u.y;
pa[PIdx::uz][ip] = u.z;
#if defined(WARPX_DIM_3D)
p.pos(0) = pos.x;
p.pos(1) = pos.y;
p.pos(2) = pos.z;
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
#ifdef WARPX_DIM_RZ
pa[PIdx::theta][ip] = theta;
#endif
p.pos(0) = xb;
p.pos(1) = pos.z;
#else
p.pos(0) = pos.z;
#endif
}
});
amrex::Gpu::synchronize();
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
wt = amrex::second() - wt;
amrex::HostDevice::Atomic::Add( &(*cost)[mfi.index()], wt);
}
}
// The function that calls this is responsible for redistributing particles.
}
void
PhysicalParticleContainer::AddPlasmaFlux (amrex::Real dt)
{
WARPX_PROFILE("PhysicalParticleContainer::AddPlasmaFlux()");
const Geometry& geom = Geom(0);
const amrex::RealBox& part_realbox = geom.ProbDomain();
amrex::Real num_ppc_real = plasma_injector->num_particles_per_cell_real;
#ifdef WARPX_DIM_RZ
Real rmax = std::min(plasma_injector->xmax, geom.ProbDomain().hi(0));
#endif
const auto dx = geom.CellSizeArray();
const auto problo = geom.ProbLoArray();
Real scale_fac = 0._rt;
// Scale particle weight by the area of the emitting surface, within one cell
#if defined(WARPX_DIM_3D)
scale_fac = dx[0]*dx[1]*dx[2]/dx[plasma_injector->flux_normal_axis]/num_ppc_real;
#elif defined(WARPX_DIM_RZ) || defined(WARPX_DIM_XZ)
scale_fac = dx[0]*dx[1]/num_ppc_real;
// When emission is in the r direction, the emitting surface is a cylinder.
// The factor 2*pi*r is added later below.
if (plasma_injector->flux_normal_axis == 0) scale_fac /= dx[0];
// When emission is in the z direction, the emitting surface is an annulus
// The factor 2*pi*r is added later below.
if (plasma_injector->flux_normal_axis == 2) scale_fac /= dx[1];
// When emission is in the theta direction (flux_normal_axis == 1),
// the emitting surface is a rectangle, within the plane of the simulation
#elif defined(WARPX_DIM_1D_Z)
scale_fac = dx[0]/num_ppc_real;
if (plasma_injector->flux_normal_axis == 2) scale_fac /= dx[0];
#endif
amrex::LayoutData<amrex::Real>* cost = WarpX::getCosts(0);
// Create temporary particle container to which particles will be added;
// we will then call Redistribute on this new container and finally
// add the new particles to the original container.
PhysicalParticleContainer tmp_pc(&WarpX::GetInstance());
for (int ic = 0; ic < NumRuntimeRealComps(); ++ic) { tmp_pc.AddRealComp(false); }
for (int ic = 0; ic < NumRuntimeIntComps(); ++ic) { tmp_pc.AddIntComp(false); }
tmp_pc.defineAllParticleTiles();
const int nlevs = numLevels();
static bool refine_injection = false;
static Box fine_injection_box;
static int rrfac = 1;
// This does not work if the mesh is dynamic. But in that case, we should
// not use refined injected either. We also assume there is only one fine level.
if (WarpX::refine_plasma && nlevs == 2)
{
refine_injection = true;
fine_injection_box = ParticleBoxArray(1).minimalBox();
rrfac = m_gdb->refRatio(0)[0];
fine_injection_box.coarsen(rrfac);
}
InjectorPosition* inj_pos = plasma_injector->getInjectorPosition();
InjectorDensity* inj_rho = plasma_injector->getInjectorDensity();
InjectorMomentum* inj_mom = plasma_injector->getInjectorMomentum();
const amrex::Real density_min = plasma_injector->density_min;
const amrex::Real density_max = plasma_injector->density_max;
constexpr int level_zero = 0;
const amrex::Real t = WarpX::GetInstance().gett_new(level_zero);
#ifdef WARPX_DIM_RZ
const int nmodes = WarpX::n_rz_azimuthal_modes;
bool radially_weighted = plasma_injector->radially_weighted;
#endif
MFItInfo info;
if (do_tiling && Gpu::notInLaunchRegion()) {
info.EnableTiling(tile_size);
}
#ifdef AMREX_USE_OMP
info.SetDynamic(true);
#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
#endif
for (MFIter mfi = MakeMFIter(0, info); mfi.isValid(); ++mfi)
{
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
amrex::Gpu::synchronize();
}
Real wt = amrex::second();
const Box& tile_box = mfi.tilebox();
const RealBox tile_realbox = WarpX::getRealBox(tile_box, 0);
// Find the cells of part_realbox that overlap with tile_realbox
// If there is no overlap, just go to the next tile in the loop
RealBox overlap_realbox;
Box overlap_box;
IntVect shifted;
bool no_overlap = false;
for (int dir=0; dir<AMREX_SPACEDIM; dir++) {
#if (defined(WARPX_DIM_3D))
if (dir == plasma_injector->flux_normal_axis) {
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
if (2*dir == plasma_injector->flux_normal_axis) {
// The above formula captures the following cases:
// - flux_normal_axis=0 (emission along x/r) and dir=0
// - flux_normal_axis=2 (emission along z) and dir=1
#elif defined(WARPX_DIM_1D_Z)
if ( (dir==0) && (plasma_injector->flux_normal_axis==2) ) {
#endif
if (plasma_injector->flux_direction > 0) {
if (plasma_injector->surface_flux_pos < tile_realbox.lo(dir) ||
plasma_injector->surface_flux_pos >= tile_realbox.hi(dir)) {
no_overlap = true;
break;
}
} else {
if (plasma_injector->surface_flux_pos <= tile_realbox.lo(dir) ||
plasma_injector->surface_flux_pos > tile_realbox.hi(dir)) {
no_overlap = true;
break;
}
}
overlap_realbox.setLo( dir, plasma_injector->surface_flux_pos );
overlap_realbox.setHi( dir, plasma_injector->surface_flux_pos );
overlap_box.setSmall( dir, 0 );
overlap_box.setBig( dir, 0 );
shifted[dir] =
static_cast<int>(std::round((overlap_realbox.lo(dir)-problo[dir])/dx[dir]));
} else {
if ( tile_realbox.lo(dir) <= part_realbox.hi(dir) ) {
Real ncells_adjust = std::floor( (tile_realbox.lo(dir) - part_realbox.lo(dir))/dx[dir] );
overlap_realbox.setLo( dir, part_realbox.lo(dir) + std::max(ncells_adjust, 0._rt) * dx[dir]);
} else {
no_overlap = true; break;
}
if ( tile_realbox.hi(dir) >= part_realbox.lo(dir) ) {
Real ncells_adjust = std::floor( (part_realbox.hi(dir) - tile_realbox.hi(dir))/dx[dir] );
overlap_realbox.setHi( dir, part_realbox.hi(dir) - std::max(ncells_adjust, 0._rt) * dx[dir]);
} else {
no_overlap = true; break;
}
// Count the number of cells in this direction in overlap_realbox
overlap_box.setSmall( dir, 0 );
overlap_box.setBig( dir,
int( std::round((overlap_realbox.hi(dir)-overlap_realbox.lo(dir))
/dx[dir] )) - 1);
shifted[dir] =
static_cast<int>(std::round((overlap_realbox.lo(dir)-problo[dir])/dx[dir]));
// shifted is exact in non-moving-window direction. That's all we care.
}
}
if (no_overlap == 1) {
continue; // Go to the next tile
}
const int grid_id = mfi.index();
const int tile_id = mfi.LocalTileIndex();
const GpuArray<Real,AMREX_SPACEDIM> overlap_corner
{AMREX_D_DECL(overlap_realbox.lo(0),
overlap_realbox.lo(1),
overlap_realbox.lo(2))};
// count the number of particles that each cell in overlap_box could add
Gpu::DeviceVector<int> counts(overlap_box.numPts(), 0);
Gpu::DeviceVector<int> offset(overlap_box.numPts());
auto pcounts = counts.data();
int lrrfac = rrfac;
Box fine_overlap_box; // default Box is NOT ok().
if (refine_injection) {
fine_overlap_box = overlap_box & amrex::shift(fine_injection_box, -shifted);
}
amrex::ParallelForRNG(overlap_box, [=] AMREX_GPU_DEVICE (int i, int j, int k, amrex::RandomEngine const& engine) noexcept
{
IntVect iv(AMREX_D_DECL(i, j, k));
auto lo = getCellCoords(overlap_corner, dx, {0._rt, 0._rt, 0._rt}, iv);
auto hi = getCellCoords(overlap_corner, dx, {1._rt, 1._rt, 1._rt}, iv);
int num_ppc_int = static_cast<int>(num_ppc_real + amrex::Random(engine));
if (inj_pos->overlapsWith(lo, hi))
{
auto index = overlap_box.index(iv);
int r;
if (fine_overlap_box.ok() && fine_overlap_box.contains(iv)) {
r = AMREX_D_TERM(lrrfac,*lrrfac,*lrrfac);
} else {
r = 1;
}
pcounts[index] = num_ppc_int*r;
}
#if defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
amrex::ignore_unused(k);
#elif defined(WARPX_DIM_1D_Z)
amrex::ignore_unused(j,k);
#endif
});
// Max number of new particles. All of them are created,
// and invalid ones are then discarded
int max_new_particles = Scan::ExclusiveSum(counts.size(), counts.data(), offset.data());
// Update NextID to include particles created in this function
Long pid;
#ifdef AMREX_USE_OMP
#pragma omp critical (add_plasma_nextid)
#endif
{
pid = ParticleType::NextID();
ParticleType::NextID(pid+max_new_particles);
}
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
static_cast<Long>(pid + max_new_particles) < LastParticleID,
"overflow on particle id numbers");
const int cpuid = ParallelDescriptor::MyProc();
auto& particle_tile = tmp_pc.DefineAndReturnParticleTile(0, grid_id, tile_id);
auto old_size = particle_tile.GetArrayOfStructs().size();
auto new_size = old_size + max_new_particles;
particle_tile.resize(new_size);
ParticleType* pp = particle_tile.GetArrayOfStructs()().data() + old_size;
auto& soa = particle_tile.GetStructOfArrays();
GpuArray<ParticleReal*,PIdx::nattribs> pa;
for (int ia = 0; ia < PIdx::nattribs; ++ia) {
pa[ia] = soa.GetRealData(ia).data() + old_size;
}
// user-defined integer and real attributes
const int n_user_int_attribs = m_user_int_attribs.size();
const int n_user_real_attribs = m_user_real_attribs.size();
amrex::Gpu::DeviceVector<int*> pa_user_int(n_user_int_attribs);
amrex::Gpu::DeviceVector<ParticleReal*> pa_user_real(n_user_real_attribs);
amrex::Gpu::DeviceVector< amrex::ParserExecutor<7> > user_int_attrib_parserexec(n_user_int_attribs);
amrex::Gpu::DeviceVector< amrex::ParserExecutor<7> > user_real_attrib_parserexec(n_user_real_attribs);
for (int ia = 0; ia < n_user_int_attribs; ++ia) {
pa_user_int[ia] = soa.GetIntData(particle_icomps[m_user_int_attribs[ia]]).data() + old_size;
user_int_attrib_parserexec[ia] = m_user_int_attrib_parser[ia]->compile<7>();
}
for (int ia = 0; ia < n_user_real_attribs; ++ia) {
pa_user_real[ia] = soa.GetRealData(particle_comps[m_user_real_attribs[ia]]).data() + old_size;
user_real_attrib_parserexec[ia] = m_user_real_attrib_parser[ia]->compile<7>();
}
int** pa_user_int_data = pa_user_int.dataPtr();
ParticleReal** pa_user_real_data = pa_user_real.dataPtr();
amrex::ParserExecutor<7> const* user_int_parserexec_data = user_int_attrib_parserexec.dataPtr();
amrex::ParserExecutor<7> const* user_real_parserexec_data = user_real_attrib_parserexec.dataPtr();
int* p_ion_level = nullptr;
if (do_field_ionization) {
p_ion_level = soa.GetIntData(particle_icomps["ionizationLevel"]).data() + old_size;
}
#ifdef WARPX_QED
//Pointer to the optical depth component
amrex::ParticleReal* p_optical_depth_QSR = nullptr;
amrex::ParticleReal* p_optical_depth_BW = nullptr;
// If a QED effect is enabled, the corresponding optical depth
// has to be initialized
bool loc_has_quantum_sync = has_quantum_sync();
bool loc_has_breit_wheeler = has_breit_wheeler();
if (loc_has_quantum_sync)
p_optical_depth_QSR = soa.GetRealData(
particle_comps["opticalDepthQSR"]).data() + old_size;
if(loc_has_breit_wheeler)
p_optical_depth_BW = soa.GetRealData(
particle_comps["opticalDepthBW"]).data() + old_size;
//If needed, get the appropriate functors from the engines
QuantumSynchrotronGetOpticalDepth quantum_sync_get_opt;
BreitWheelerGetOpticalDepth breit_wheeler_get_opt;
if(loc_has_quantum_sync){
quantum_sync_get_opt =
m_shr_p_qs_engine->build_optical_depth_functor();
}
if(loc_has_breit_wheeler){
breit_wheeler_get_opt =
m_shr_p_bw_engine->build_optical_depth_functor();
}
#endif
bool loc_do_field_ionization = do_field_ionization;
int loc_ionization_initial_level = ionization_initial_level;
#ifdef WARPX_DIM_RZ
int const loc_flux_normal_axis = plasma_injector->flux_normal_axis;
#endif
// Loop over all new particles and inject them (creates too many
// particles, in particular does not consider xmin, xmax etc.).
// The invalid ones are given negative ID and are deleted during the
// next redistribute.
const auto poffset = offset.data();
amrex::ParallelForRNG(overlap_box,
[=] AMREX_GPU_DEVICE (int i, int j, int k, amrex::RandomEngine const& engine) noexcept
{
IntVect iv = IntVect(AMREX_D_DECL(i, j, k));
const auto index = overlap_box.index(iv);
for (int i_part = 0; i_part < pcounts[index]; ++i_part)
{
long ip = poffset[index] + i_part;
ParticleType& p = pp[ip];
p.id() = pid+ip;
p.cpu() = cpuid;
// This assumes the inj_pos is of type InjectorPositionRandomPlane
const XDim3 r = (fine_overlap_box.ok() && fine_overlap_box.contains(iv)) ?
// In the refined injection region: use refinement ratio `lrrfac`
inj_pos->getPositionUnitBox(i_part, lrrfac, engine) :
// Otherwise: use 1 as the refinement ratio
inj_pos->getPositionUnitBox(i_part, 1, engine);
auto pos = getCellCoords(overlap_corner, dx, r, iv);
auto ppos = PDim3(pos);
// inj_mom would typically be InjectorMomentumGaussianFlux
XDim3 u;
u = inj_mom->getMomentum(pos.x, pos.y, pos.z, engine);
auto pu = PDim3(u);
pu.x *= PhysConst::c;
pu.y *= PhysConst::c;
pu.z *= PhysConst::c;
#if defined(WARPX_DIM_3D)
if (!tile_realbox.contains(XDim3{ppos.x,ppos.y,ppos.z})) {
p.id() = -1;
continue;
}
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
amrex::ignore_unused(k);
if (!tile_realbox.contains(XDim3{ppos.x,ppos.z,0.0_prt})) {
p.id() = -1;
continue;
}
#else
amrex::ignore_unused(j,k);
if (!tile_realbox.contains(XDim3{ppos.z,0.0_prt,0.0_prt})) {
p.id() = -1;
continue;
}
#endif
// Lab-frame simulation
// If the particle is not within the species's
// xmin, xmax, ymin, ymax, zmin, zmax, go to
// the next generated particle.
if (!inj_pos->insideBounds(ppos.x, ppos.y, ppos.z)) {
p.id() = -1;
continue;
}
#ifdef WARPX_DIM_RZ
// Conversion from cylindrical to Cartesian coordinates
// Replace the x and y, setting an angle theta.
// These x and y are used to get the momentum and density
Real theta;
if (nmodes == 1) {
// With only 1 mode, the angle doesn't matter so
// choose it randomly.
theta = 2._prt*MathConst::pi*amrex::Random(engine);
} else {
theta = 2._prt*MathConst::pi*r.y;
}
Real const cos_theta = std::cos(theta);
Real const sin_theta = std::sin(theta);
// Rotate the position
amrex::Real radial_position = ppos.x;
ppos.x = radial_position*cos_theta;
ppos.y = radial_position*sin_theta;
if (loc_flux_normal_axis != 2) {
// Rotate the momentum
// This because, when the flux direction is e.g. "r"
// the `inj_mom` objects generates a v*Gaussian distribution
// along the Cartesian "x" directionm by default. This
// needs to be rotated along "r".
Real ur = pu.x;
Real ut = pu.y;
pu.x = cos_theta*ur - sin_theta*ut;
pu.y = sin_theta*ur + cos_theta*ut;
}
#endif
Real dens = inj_rho->getDensity(ppos.x, ppos.y, ppos.z);
// Remove particle if density below threshold
if ( dens < density_min ){
p.id() = -1;
continue;
}
// Cut density if above threshold
dens = amrex::min(dens, density_max);
if (loc_do_field_ionization) {
p_ion_level[ip] = loc_ionization_initial_level;
}
#ifdef WARPX_QED
if(loc_has_quantum_sync){
p_optical_depth_QSR[ip] = quantum_sync_get_opt(engine);
}
if(loc_has_breit_wheeler){
p_optical_depth_BW[ip] = breit_wheeler_get_opt(engine);
}
#endif
// Initialize user-defined integers with user-defined parser
for (int ia = 0; ia < n_user_int_attribs; ++ia) {
pa_user_int_data[ia][ip] = static_cast<int>(user_int_parserexec_data[ia](pos.x, pos.y, pos.z, u.x, u.y, u.z, t));
}
// Initialize user-defined real attributes with user-defined parser
for (int ia = 0; ia < n_user_real_attribs; ++ia) {
pa_user_real_data[ia][ip] = user_real_parserexec_data[ia](pos.x, pos.y, pos.z, u.x, u.y, u.z, t);
}
Real weight = dens * scale_fac * dt;
#ifdef WARPX_DIM_RZ
// The particle weight is proportional to the user-specified
// flux (denoted as `dens` here) and the emission surface within
// one cell (captured partially by `scale_fac`).
// For cylindrical emission (flux_normal_axis==0
// or flux_normal_axis==2), the emission surface depends on
// the radius ; thus, the calculation is finalized here
if (loc_flux_normal_axis != 1) {
if (radially_weighted) {
weight *= 2._rt*MathConst::pi*radial_position;
} else {
// This is not correct since it might shift the particle
// out of the local grid
ppos.x = std::sqrt(radial_position*rmax);
weight *= dx[0];
}
}
#endif
pa[PIdx::w ][ip] = weight;
pa[PIdx::ux][ip] = pu.x;
pa[PIdx::uy][ip] = pu.y;
pa[PIdx::uz][ip] = pu.z;
// Update particle position by a random `t_fract`
// so as to produce a continuous-looking flow of particles
const amrex::Real t_fract = amrex::Random(engine)*dt;
UpdatePosition(ppos.x, ppos.y, ppos.z, pu.x, pu.y, pu.z, t_fract);
#if defined(WARPX_DIM_3D)
p.pos(0) = ppos.x;
p.pos(1) = ppos.y;
p.pos(2) = ppos.z;
#elif defined(WARPX_DIM_RZ)
pa[PIdx::theta][ip] = std::atan2(ppos.y, ppos.x);
p.pos(0) = std::sqrt(ppos.x*ppos.x + ppos.y*ppos.y);
p.pos(1) = ppos.z;
#elif defined(WARPX_DIM_XZ)
p.pos(0) = ppos.x;
p.pos(1) = ppos.z;
#else
p.pos(0) = ppos.z;
#endif
}
});
amrex::Gpu::synchronize();
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
wt = amrex::second() - wt;
amrex::HostDevice::Atomic::Add( &(*cost)[mfi.index()], wt);
}
}
// Redistribute the new particles that were added to the temporary container.
// (This eliminates invalid particles, and makes sure that particles
// are in the right tile.)
tmp_pc.Redistribute();
// Add the particles to the current container, tile by tile
for (int lev=0; lev<numLevels(); lev++) {
#ifdef AMREX_USE_OMP
#pragma omp parallel if (amrex::Gpu::notInLaunchRegion())
#endif
for (MFIter mfi = MakeMFIter(lev, info); mfi.isValid(); ++mfi)
{
// Extract tiles
const int grid_id = mfi.index();
const int tile_id = mfi.LocalTileIndex();
auto& src_tile = tmp_pc.DefineAndReturnParticleTile(lev, grid_id, tile_id);
auto& dst_tile = DefineAndReturnParticleTile(lev, grid_id, tile_id);
// Resize container and copy particles
auto old_size = dst_tile.numParticles();
auto n_new = src_tile.numParticles();
dst_tile.resize( old_size+n_new );
amrex::copyParticles(dst_tile, src_tile, 0, old_size, n_new);
}
}
}
void
PhysicalParticleContainer::Evolve (int lev,
const MultiFab& Ex, const MultiFab& Ey, const MultiFab& Ez,
const MultiFab& Bx, const MultiFab& By, const MultiFab& Bz,
MultiFab& jx, MultiFab& jy, MultiFab& jz,
MultiFab* cjx, MultiFab* cjy, MultiFab* cjz,
MultiFab* rho, MultiFab* crho,
const MultiFab* cEx, const MultiFab* cEy, const MultiFab* cEz,
const MultiFab* cBx, const MultiFab* cBy, const MultiFab* cBz,
Real /*t*/, Real dt, DtType a_dt_type, bool skip_deposition)
{
WARPX_PROFILE("PhysicalParticleContainer::Evolve()");
WARPX_PROFILE_VAR_NS("PhysicalParticleContainer::Evolve::GatherAndPush", blp_fg);
BL_ASSERT(OnSameGrids(lev,jx));
amrex::LayoutData<amrex::Real>* cost = WarpX::getCosts(lev);
const iMultiFab* current_masks = WarpX::CurrentBufferMasks(lev);
const iMultiFab* gather_masks = WarpX::GatherBufferMasks(lev);
bool has_buffer = cEx || cjx;
if (m_do_back_transformed_particles)
{
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
{
const auto np = pti.numParticles();
const auto t_lev = pti.GetLevel();
const auto index = pti.GetPairIndex();
tmp_particle_data.resize(finestLevel()+1);
for (int i = 0; i < TmpIdx::nattribs; ++i)
tmp_particle_data[t_lev][index][i].resize(np);
}
}
#ifdef AMREX_USE_OMP
#pragma omp parallel
#endif
{
#ifdef AMREX_USE_OMP
int thread_num = omp_get_thread_num();
#else
int thread_num = 0;
#endif
FArrayBox filtered_Ex, filtered_Ey, filtered_Ez;
FArrayBox filtered_Bx, filtered_By, filtered_Bz;
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
{
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
amrex::Gpu::synchronize();
}
Real wt = amrex::second();
const Box& box = pti.validbox();
// Extract particle data
auto& attribs = pti.GetAttribs();
auto& wp = attribs[PIdx::w];
auto& uxp = attribs[PIdx::ux];
auto& uyp = attribs[PIdx::uy];
auto& uzp = attribs[PIdx::uz];
const long np = pti.numParticles();
// Data on the grid
FArrayBox const* exfab = &Ex[pti];
FArrayBox const* eyfab = &Ey[pti];
FArrayBox const* ezfab = &Ez[pti];
FArrayBox const* bxfab = &Bx[pti];
FArrayBox const* byfab = &By[pti];
FArrayBox const* bzfab = &Bz[pti];
Elixir exeli, eyeli, ezeli, bxeli, byeli, bzeli;
if (WarpX::use_fdtd_nci_corr)
{
// Filter arrays Ex[pti], store the result in
// filtered_Ex and update pointer exfab so that it
// points to filtered_Ex (and do the same for all
// components of E and B).
applyNCIFilter(lev, pti.tilebox(), exeli, eyeli, ezeli, bxeli, byeli, bzeli,
filtered_Ex, filtered_Ey, filtered_Ez,
filtered_Bx, filtered_By, filtered_Bz,
Ex[pti], Ey[pti], Ez[pti], Bx[pti], By[pti], Bz[pti],
exfab, eyfab, ezfab, bxfab, byfab, bzfab);
}
// Determine which particles deposit/gather in the buffer, and
// which particles deposit/gather in the fine patch
long nfine_current = np;
long nfine_gather = np;
if (has_buffer && !do_not_push) {
// - Modify `nfine_current` and `nfine_gather` (in place)
// so that they correspond to the number of particles
// that deposit/gather in the fine patch respectively.
// - Reorder the particle arrays,
// so that the `nfine_current`/`nfine_gather` first particles
// deposit/gather in the fine patch
// and (thus) the `np-nfine_current`/`np-nfine_gather` last particles
// deposit/gather in the buffer
PartitionParticlesInBuffers( nfine_current, nfine_gather, np,
pti, lev, current_masks, gather_masks );
}
const long np_current = (cjx) ? nfine_current : np;
if (rho && ! skip_deposition && ! do_not_deposit) {
// Deposit charge before particle push, in component 0 of MultiFab rho.
int* AMREX_RESTRICT ion_lev;
if (do_field_ionization){
ion_lev = pti.GetiAttribs(particle_icomps["ionizationLevel"]).dataPtr();
} else {
ion_lev = nullptr;
}
DepositCharge(pti, wp, ion_lev, rho, 0, 0,
np_current, thread_num, lev, lev);
if (has_buffer){
DepositCharge(pti, wp, ion_lev, crho, 0, np_current,
np-np_current, thread_num, lev, lev-1);
}
}
if (! do_not_push)
{
const long np_gather = (cEx) ? nfine_gather : np;
int e_is_nodal = Ex.is_nodal() and Ey.is_nodal() and Ez.is_nodal();
//
// Gather and push for particles not in the buffer
//
WARPX_PROFILE_VAR_START(blp_fg);
PushPX(pti, exfab, eyfab, ezfab,
bxfab, byfab, bzfab,
Ex.nGrowVect(), e_is_nodal,
0, np_gather, lev, lev, dt, ScaleFields(false), a_dt_type);
if (np_gather < np)
{
const IntVect& ref_ratio = WarpX::RefRatio(lev-1);
const Box& cbox = amrex::coarsen(box,ref_ratio);
// Data on the grid
FArrayBox const* cexfab = &(*cEx)[pti];
FArrayBox const* ceyfab = &(*cEy)[pti];
FArrayBox const* cezfab = &(*cEz)[pti];
FArrayBox const* cbxfab = &(*cBx)[pti];
FArrayBox const* cbyfab = &(*cBy)[pti];
FArrayBox const* cbzfab = &(*cBz)[pti];
if (WarpX::use_fdtd_nci_corr)
{
// Filter arrays (*cEx)[pti], store the result in
// filtered_Ex and update pointer cexfab so that it
// points to filtered_Ex (and do the same for all
// components of E and B)
applyNCIFilter(lev-1, cbox, exeli, eyeli, ezeli, bxeli, byeli, bzeli,
filtered_Ex, filtered_Ey, filtered_Ez,
filtered_Bx, filtered_By, filtered_Bz,
(*cEx)[pti], (*cEy)[pti], (*cEz)[pti],
(*cBx)[pti], (*cBy)[pti], (*cBz)[pti],
cexfab, ceyfab, cezfab, cbxfab, cbyfab, cbzfab);
}
// Field gather and push for particles in gather buffers
e_is_nodal = cEx->is_nodal() and cEy->is_nodal() and cEz->is_nodal();
PushPX(pti, cexfab, ceyfab, cezfab,
cbxfab, cbyfab, cbzfab,
cEx->nGrowVect(), e_is_nodal,
nfine_gather, np-nfine_gather,
lev, lev-1, dt, ScaleFields(false), a_dt_type);
}
WARPX_PROFILE_VAR_STOP(blp_fg);
// Current Deposition
if (skip_deposition == false)
{
// Deposit at t_{n+1/2}
amrex::Real relative_time = -0.5_rt * dt;
int* AMREX_RESTRICT ion_lev;
if (do_field_ionization){
ion_lev = pti.GetiAttribs(particle_icomps["ionizationLevel"]).dataPtr();
} else {
ion_lev = nullptr;
}
// Deposit inside domains
DepositCurrent(pti, wp, uxp, uyp, uzp, ion_lev, &jx, &jy, &jz,
0, np_current, thread_num,
lev, lev, dt, relative_time);
if (has_buffer)
{
// Deposit in buffers
DepositCurrent(pti, wp, uxp, uyp, uzp, ion_lev, cjx, cjy, cjz,
np_current, np-np_current, thread_num,
lev, lev-1, dt, relative_time);
}
} // end of "if electrostatic_solver_id == ElectrostaticSolverAlgo::None"
} // end of "if do_not_push"
if (rho && ! skip_deposition && ! do_not_deposit) {
// Deposit charge after particle push, in component 1 of MultiFab rho.
// (Skipped for electrostatic solver, as this may lead to out-of-bounds)
if (WarpX::electrostatic_solver_id == ElectrostaticSolverAlgo::None) {
int* AMREX_RESTRICT ion_lev;
if (do_field_ionization){
ion_lev = pti.GetiAttribs(particle_icomps["ionizationLevel"]).dataPtr();
} else {
ion_lev = nullptr;
}
DepositCharge(pti, wp, ion_lev, rho, 1, 0,
np_current, thread_num, lev, lev);
if (has_buffer){
DepositCharge(pti, wp, ion_lev, crho, 1, np_current,
np-np_current, thread_num, lev, lev-1);
}
}
}
amrex::Gpu::synchronize();
if (cost && WarpX::load_balance_costs_update_algo == LoadBalanceCostsUpdateAlgo::Timers)
{
wt = amrex::second() - wt;
amrex::HostDevice::Atomic::Add( &(*cost)[pti.index()], wt);
}
}
}
// Split particles at the end of the timestep.
// When subcycling is ON, the splitting is done on the last call to
// PhysicalParticleContainer::Evolve on the finest level, i.e., at the
// end of the large timestep. Otherwise, the pushes on different levels
// are not consistent, and the call to Redistribute (inside
// SplitParticles) may result in split particles to deposit twice on the
// coarse level.
if (do_splitting && (a_dt_type == DtType::SecondHalf || a_dt_type == DtType::Full) ){
SplitParticles(lev);
}
}
void
PhysicalParticleContainer::applyNCIFilter (
int lev, const Box& box,
Elixir& exeli, Elixir& eyeli, Elixir& ezeli,
Elixir& bxeli, Elixir& byeli, Elixir& bzeli,
FArrayBox& filtered_Ex, FArrayBox& filtered_Ey, FArrayBox& filtered_Ez,
FArrayBox& filtered_Bx, FArrayBox& filtered_By, FArrayBox& filtered_Bz,
const FArrayBox& Ex, const FArrayBox& Ey, const FArrayBox& Ez,
const FArrayBox& Bx, const FArrayBox& By, const FArrayBox& Bz,
FArrayBox const * & ex_ptr, FArrayBox const * & ey_ptr,
FArrayBox const * & ez_ptr, FArrayBox const * & bx_ptr,
FArrayBox const * & by_ptr, FArrayBox const * & bz_ptr)
{
// Get instances of NCI Godfrey filters
const auto& nci_godfrey_filter_exeybz = WarpX::GetInstance().nci_godfrey_filter_exeybz;
const auto& nci_godfrey_filter_bxbyez = WarpX::GetInstance().nci_godfrey_filter_bxbyez;
#if defined(WARPX_DIM_1D_Z)
const Box& tbox = amrex::grow(box, static_cast<int>(WarpX::noz));
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
const Box& tbox = amrex::grow(box, {static_cast<int>(WarpX::nox),
static_cast<int>(WarpX::noz)});
#else
const Box& tbox = amrex::grow(box, {static_cast<int>(WarpX::nox),
static_cast<int>(WarpX::noy),
static_cast<int>(WarpX::noz)});
#endif
// Filter Ex (Both 2D and 3D)
filtered_Ex.resize(amrex::convert(tbox,Ex.box().ixType()));
// Safeguard for GPU
exeli = filtered_Ex.elixir();
// Apply filter on Ex, result stored in filtered_Ex
nci_godfrey_filter_exeybz[lev]->ApplyStencil(filtered_Ex, Ex, filtered_Ex.box());
// Update ex_ptr reference
ex_ptr = &filtered_Ex;
// Filter Ez
filtered_Ez.resize(amrex::convert(tbox,Ez.box().ixType()));
ezeli = filtered_Ez.elixir();
nci_godfrey_filter_bxbyez[lev]->ApplyStencil(filtered_Ez, Ez, filtered_Ez.box());
ez_ptr = &filtered_Ez;
// Filter By
filtered_By.resize(amrex::convert(tbox,By.box().ixType()));
byeli = filtered_By.elixir();
nci_godfrey_filter_bxbyez[lev]->ApplyStencil(filtered_By, By, filtered_By.box());
by_ptr = &filtered_By;
#if defined(WARPX_DIM_3D)
// Filter Ey
filtered_Ey.resize(amrex::convert(tbox,Ey.box().ixType()));
eyeli = filtered_Ey.elixir();
nci_godfrey_filter_exeybz[lev]->ApplyStencil(filtered_Ey, Ey, filtered_Ey.box());
ey_ptr = &filtered_Ey;
// Filter Bx
filtered_Bx.resize(amrex::convert(tbox,Bx.box().ixType()));
bxeli = filtered_Bx.elixir();
nci_godfrey_filter_bxbyez[lev]->ApplyStencil(filtered_Bx, Bx, filtered_Bx.box());
bx_ptr = &filtered_Bx;
// Filter Bz
filtered_Bz.resize(amrex::convert(tbox,Bz.box().ixType()));
bzeli = filtered_Bz.elixir();
nci_godfrey_filter_exeybz[lev]->ApplyStencil(filtered_Bz, Bz, filtered_Bz.box());
bz_ptr = &filtered_Bz;
#else
amrex::ignore_unused(eyeli, bxeli, bzeli,
filtered_Ey, filtered_Bx, filtered_Bz,
Ey, Bx, Bz, ey_ptr, bx_ptr, bz_ptr);
#endif
}
// Loop over all particles in the particle container and
// split particles tagged with p.id()=DoSplitParticleID
void
PhysicalParticleContainer::SplitParticles (int lev)
{
auto& mypc = WarpX::GetInstance().GetPartContainer();
auto& pctmp_split = mypc.GetPCtmp();
RealVector psplit_x, psplit_y, psplit_z, psplit_w;
RealVector psplit_ux, psplit_uy, psplit_uz;
long np_split_to_add = 0;
long np_split;
if(split_type==0)
{
#if defined(WARPX_DIM_3D)
np_split = 8;
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
np_split = 4;
#else
np_split = 2;
#endif
} else {
np_split = 2*AMREX_SPACEDIM;
}
// Loop over particle interator
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
{
const auto GetPosition = GetParticlePosition(pti);
const amrex::Vector<int> ppc_nd = plasma_injector->num_particles_per_cell_each_dim;
const std::array<Real,3>& dx = WarpX::CellSize(lev);
amrex::Vector<Real> split_offset = {dx[0]/2._rt,
dx[1]/2._rt,
dx[2]/2._rt};
if (ppc_nd[0] > 0){
// offset for split particles is computed as a function of cell size
// and number of particles per cell, so that a uniform distribution
// before splitting results in a uniform distribution after splitting
split_offset[0] /= ppc_nd[0];
split_offset[1] /= ppc_nd[1];
split_offset[2] /= ppc_nd[2];
}
// particle Array Of Structs data
auto& particles = pti.GetArrayOfStructs();
// particle Struct Of Arrays data
auto& attribs = pti.GetAttribs();
auto& wp = attribs[PIdx::w ];
auto& uxp = attribs[PIdx::ux];
auto& uyp = attribs[PIdx::uy];
auto& uzp = attribs[PIdx::uz];
const long np = pti.numParticles();
for(int i=0; i<np; i++){
ParticleReal xp, yp, zp;
GetPosition(i, xp, yp, zp);
auto& p = particles[i];
if (p.id() == DoSplitParticleID){
// If particle is tagged, split it and put the
// split particles in local arrays psplit_x etc.
np_split_to_add += np_split;
#if defined(WARPX_DIM_1D_Z)
// Split particle in two along z axis
// 2 particles in 1d, split_type doesn't matter? Discuss with Remi
for (int ishift = -1; ishift < 2; ishift +=2 ){
// Add one particle with offset in z
psplit_x.push_back( xp );
psplit_y.push_back( yp );
psplit_z.push_back( zp + ishift*split_offset[2] );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
}
#elif defined(WARPX_DIM_XZ) || defined(WARPX_DIM_RZ)
if (split_type==0){
// Split particle in two along each diagonals
// 4 particles in 2d
for (int ishift = -1; ishift < 2; ishift +=2 ){
for (int kshift = -1; kshift < 2; kshift +=2 ){
// Add one particle with offset in x and z
psplit_x.push_back( xp + ishift*split_offset[0] );
psplit_y.push_back( yp );
psplit_z.push_back( zp + kshift*split_offset[2] );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
}
}
} else {
// Split particle in two along each axis
// 4 particles in 2d
for (int ishift = -1; ishift < 2; ishift +=2 ){
// Add one particle with offset in x
psplit_x.push_back( xp + ishift*split_offset[0] );
psplit_y.push_back( yp );
psplit_z.push_back( zp );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
// Add one particle with offset in z
psplit_x.push_back( xp );
psplit_y.push_back( yp );
psplit_z.push_back( zp + ishift*split_offset[2] );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
}
}
#elif defined(WARPX_DIM_3D)
if (split_type==0){
// Split particle in two along each diagonals
// 8 particles in 3d
for (int ishift = -1; ishift < 2; ishift +=2 ){
for (int jshift = -1; jshift < 2; jshift +=2 ){
for (int kshift = -1; kshift < 2; kshift +=2 ){
// Add one particle with offset in x, y and z
psplit_x.push_back( xp + ishift*split_offset[0] );
psplit_y.push_back( yp + jshift*split_offset[1] );
psplit_z.push_back( zp + kshift*split_offset[2] );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
}
}
}
} else {
// Split particle in two along each axis
// 6 particles in 3d
for (int ishift = -1; ishift < 2; ishift +=2 ){
// Add one particle with offset in x
psplit_x.push_back( xp + ishift*split_offset[0] );
psplit_y.push_back( yp );
psplit_z.push_back( zp );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
// Add one particle with offset in y
psplit_x.push_back( xp );
psplit_y.push_back( yp + ishift*split_offset[1] );
psplit_z.push_back( zp );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
// Add one particle with offset in z
psplit_x.push_back( xp );
psplit_y.push_back( yp );
psplit_z.push_back( zp + ishift*split_offset[2] );
psplit_ux.push_back( uxp[i] );
psplit_uy.push_back( uyp[i] );
psplit_uz.push_back( uzp[i] );
psplit_w.push_back( wp[i]/np_split );
}
}
#endif
// invalidate the particle
p.id() = -p.id();
}
}
}
// Add local arrays psplit_x etc. to the temporary
// particle container pctmp_split. Split particles
// are tagged with p.id()=NoSplitParticleID so that
// they are not re-split when entering a higher level
// AddNParticles calls Redistribute, so that particles
// in pctmp_split are in the proper grids and tiles
pctmp_split.AddNParticles(lev,
np_split_to_add,
psplit_x.dataPtr(),
psplit_y.dataPtr(),
psplit_z.dataPtr(),
psplit_ux.dataPtr(),
psplit_uy.dataPtr(),
psplit_uz.dataPtr(),
1,
psplit_w.dataPtr(),
0, nullptr,
1, NoSplitParticleID);
// Copy particles from tmp to current particle container
addParticles(pctmp_split,1);
// Clear tmp container
pctmp_split.clearParticles();
}
void
PhysicalParticleContainer::PushP (int lev, Real dt,
const MultiFab& Ex, const MultiFab& Ey, const MultiFab& Ez,
const MultiFab& Bx, const MultiFab& By, const MultiFab& Bz)
{
WARPX_PROFILE("PhysicalParticleContainer::PushP()");
if (do_not_push) return;
const std::array<amrex::Real,3>& dx = WarpX::CellSize(std::max(lev,0));
#ifdef AMREX_USE_OMP
#pragma omp parallel
#endif
{
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
{
amrex::Box box = pti.tilebox();
box.grow(Ex.nGrowVect());
const long np = pti.numParticles();
// Data on the grid
const FArrayBox& exfab = Ex[pti];
const FArrayBox& eyfab = Ey[pti];
const FArrayBox& ezfab = Ez[pti];
const FArrayBox& bxfab = Bx[pti];
const FArrayBox& byfab = By[pti];
const FArrayBox& bzfab = Bz[pti];
const auto getPosition = GetParticlePosition(pti);
const auto getExternalEB = GetExternalEBField(pti);
const std::array<amrex::Real,3>& xyzmin = WarpX::LowerCorner(box, lev, 0._rt);
const Dim3 lo = lbound(box);
bool galerkin_interpolation = WarpX::galerkin_interpolation;
int nox = WarpX::nox;
int n_rz_azimuthal_modes = WarpX::n_rz_azimuthal_modes;
amrex::GpuArray<amrex::Real, 3> dx_arr = {dx[0], dx[1], dx[2]};
amrex::GpuArray<amrex::Real, 3> xyzmin_arr = {xyzmin[0], xyzmin[1], xyzmin[2]};
amrex::Array4<const amrex::Real> const& ex_arr = exfab.array();
amrex::Array4<const amrex::Real> const& ey_arr = eyfab.array();
amrex::Array4<const amrex::Real> const& ez_arr = ezfab.array();
amrex::Array4<const amrex::Real> const& bx_arr = bxfab.array();
amrex::Array4<const amrex::Real> const& by_arr = byfab.array();
amrex::Array4<const amrex::Real> const& bz_arr = bzfab.array();
amrex::IndexType const ex_type = exfab.box().ixType();
amrex::IndexType const ey_type = eyfab.box().ixType();
amrex::IndexType const ez_type = ezfab.box().ixType();
amrex::IndexType const bx_type = bxfab.box().ixType();
amrex::IndexType const by_type = byfab.box().ixType();
amrex::IndexType const bz_type = bzfab.box().ixType();
auto& attribs = pti.GetAttribs();
ParticleReal* const AMREX_RESTRICT ux = attribs[PIdx::ux].dataPtr();
ParticleReal* const AMREX_RESTRICT uy = attribs[PIdx::uy].dataPtr();
ParticleReal* const AMREX_RESTRICT uz = attribs[PIdx::uz].dataPtr();
int* AMREX_RESTRICT ion_lev = nullptr;
if (do_field_ionization) {
ion_lev = pti.GetiAttribs(particle_icomps["ionizationLevel"]).dataPtr();
}
// Loop over the particles and update their momentum
const amrex::Real q = this->charge;
const amrex::Real m = this-> mass;
const auto pusher_algo = WarpX::particle_pusher_algo;
const auto do_crr = do_classical_radiation_reaction;
const auto t_do_not_gather = do_not_gather;
amrex::ParallelFor( np, [=] AMREX_GPU_DEVICE (long ip)
{
amrex::ParticleReal xp, yp, zp;
getPosition(ip, xp, yp, zp);
amrex::ParticleReal Exp = 0._rt, Eyp = 0._rt, Ezp = 0._rt;
amrex::ParticleReal Bxp = 0._rt, Byp = 0._rt, Bzp = 0._rt;
if (!t_do_not_gather){
// first gather E and B to the particle positions
doGatherShapeN(xp, yp, zp, Exp, Eyp, Ezp, Bxp, Byp, Bzp,
ex_arr, ey_arr, ez_arr, bx_arr, by_arr, bz_arr,
ex_type, ey_type, ez_type, bx_type, by_type, bz_type,
dx_arr, xyzmin_arr, lo, n_rz_azimuthal_modes,
nox, galerkin_interpolation);
}
// Externally applied E and B-field in Cartesian co-ordinates
getExternalEB(ip, Exp, Eyp, Ezp, Bxp, Byp, Bzp);
if (do_crr) {
amrex::Real qp = q;
if (ion_lev) { qp *= ion_lev[ip]; }
UpdateMomentumBorisWithRadiationReaction(ux[ip], uy[ip], uz[ip],
Exp, Eyp, Ezp, Bxp,
Byp, Bzp, qp, m, dt);
} else if (pusher_algo == ParticlePusherAlgo::Boris) {
amrex::Real qp = q;
if (ion_lev) { qp *= ion_lev[ip]; }
UpdateMomentumBoris( ux[ip], uy[ip], uz[ip],
Exp, Eyp, Ezp, Bxp,
Byp, Bzp, qp, m, dt);
} else if (pusher_algo == ParticlePusherAlgo::Vay) {
amrex::Real qp = q;
if (ion_lev){ qp *= ion_lev[ip]; }
UpdateMomentumVay( ux[ip], uy[ip], uz[ip],
Exp, Eyp, Ezp, Bxp,
Byp, Bzp, qp, m, dt);
} else if (pusher_algo == ParticlePusherAlgo::HigueraCary) {
amrex::Real qp = q;
if (ion_lev){ qp *= ion_lev[ip]; }
UpdateMomentumHigueraCary( ux[ip], uy[ip], uz[ip],
Exp, Eyp, Ezp, Bxp,
Byp, Bzp, qp, m, dt);
} else {
amrex::Abort("Unknown particle pusher");
}
});
}
}
}
/* \brief Inject particles during the simulation
* \param injection_box: domain where particles should be injected.
*/
void
PhysicalParticleContainer::ContinuousInjection (const RealBox& injection_box)
{
// Inject plasma on level 0. Paticles will be redistributed.
const int lev=0;
AddPlasma(lev, injection_box);
}
/* \brief Inject a flux of particles during the simulation
*/
void
PhysicalParticleContainer::ContinuousFluxInjection (amrex::Real t, amrex::Real dt)
{
if (plasma_injector->surface_flux){
// Check the optional parameters for start and stop of injection
if ( ((plasma_injector->flux_tmin<0) || (t>=plasma_injector->flux_tmin)) &&
((plasma_injector->flux_tmax<0) || (t< plasma_injector->flux_tmax)) ){
AddPlasmaFlux(dt);
}
}
}
/* \brief Perform the field gather and particle push operations in one fused kernel
*
*/
void
PhysicalParticleContainer::PushPX (WarpXParIter& pti,
amrex::FArrayBox const * exfab,
amrex::FArrayBox const * eyfab,
amrex::FArrayBox const * ezfab,
amrex::FArrayBox const * bxfab,
amrex::FArrayBox const * byfab,
amrex::FArrayBox const * bzfab,
const amrex::IntVect ngEB, const int /*e_is_nodal*/,
const long offset,
const long np_to_push,
int lev, int gather_lev,
amrex::Real dt, ScaleFields scaleFields,
DtType a_dt_type)
{
WARPX_ALWAYS_ASSERT_WITH_MESSAGE((gather_lev==(lev-1)) ||
(gather_lev==(lev )),
"Gather buffers only work for lev-1");
// If no particles, do not do anything
if (np_to_push == 0) return;
// Get cell size on gather_lev
const std::array<Real,3>& dx = WarpX::CellSize(std::max(gather_lev,0));
// Get box from which field is gathered.
// If not gathering from the finest level, the box is coarsened.
Box box;
if (lev == gather_lev) {
box = pti.tilebox();
} else {
const IntVect& ref_ratio = WarpX::RefRatio(gather_lev);
box = amrex::coarsen(pti.tilebox(),ref_ratio);
}
// Add guard cells to the box.
box.grow(ngEB);
const auto getPosition = GetParticlePosition(pti, offset);
auto setPosition = SetParticlePosition(pti, offset);
const auto getExternalEB = GetExternalEBField(pti, offset);
// Lower corner of tile box physical domain (take into account Galilean shift)
const std::array<amrex::Real, 3>& xyzmin = WarpX::LowerCorner(box, gather_lev, 0._rt);
const Dim3 lo = lbound(box);
bool galerkin_interpolation = WarpX::galerkin_interpolation;
int nox = WarpX::nox;
int n_rz_azimuthal_modes = WarpX::n_rz_azimuthal_modes;
amrex::GpuArray<amrex::Real, 3> dx_arr = {dx[0], dx[1], dx[2]};
amrex::GpuArray<amrex::Real, 3> xyzmin_arr = {xyzmin[0], xyzmin[1], xyzmin[2]};
amrex::Array4<const amrex::Real> const& ex_arr = exfab->array();
amrex::Array4<const amrex::Real> const& ey_arr = eyfab->array();
amrex::Array4<const amrex::Real> const& ez_arr = ezfab->array();
amrex::Array4<const amrex::Real> const& bx_arr = bxfab->array();
amrex::Array4<const amrex::Real> const& by_arr = byfab->array();
amrex::Array4<const amrex::Real> const& bz_arr = bzfab->array();
amrex::IndexType const ex_type = exfab->box().ixType();
amrex::IndexType const ey_type = eyfab->box().ixType();
amrex::IndexType const ez_type = ezfab->box().ixType();
amrex::IndexType const bx_type = bxfab->box().ixType();
amrex::IndexType const by_type = byfab->box().ixType();
amrex::IndexType const bz_type = bzfab->box().ixType();
auto& attribs = pti.GetAttribs();
ParticleReal* const AMREX_RESTRICT ux = attribs[PIdx::ux].dataPtr() + offset;
ParticleReal* const AMREX_RESTRICT uy = attribs[PIdx::uy].dataPtr() + offset;
ParticleReal* const AMREX_RESTRICT uz = attribs[PIdx::uz].dataPtr() + offset;
int do_copy = (m_do_back_transformed_particles && (a_dt_type!=DtType::SecondHalf) );
CopyParticleAttribs copyAttribs;
if (do_copy) {
copyAttribs = CopyParticleAttribs(pti, tmp_particle_data, offset);
}
int* AMREX_RESTRICT ion_lev = nullptr;
if (do_field_ionization) {
ion_lev = pti.GetiAttribs(particle_icomps["ionizationLevel"]).dataPtr() + offset;
}
const bool save_previous_position = m_save_previous_position;
ParticleReal* x_old = nullptr;
ParticleReal* y_old = nullptr;
ParticleReal* z_old = nullptr;
if (save_previous_position) {
#if (AMREX_SPACEDIM >= 2)
x_old = pti.GetAttribs(particle_comps["prev_x"]).dataPtr() + offset;
#else
amrex::ignore_unused(x_old);
#endif
#if defined(WARPX_DIM_3D)
y_old = pti.GetAttribs(particle_comps["prev_y"]).dataPtr() + offset;
#else
amrex::ignore_unused(y_old);
#endif
z_old = pti.GetAttribs(particle_comps["prev_z"]).dataPtr() + offset;
}
// Loop over the particles and update their momentum
const amrex::ParticleReal q = this->charge;
const amrex::ParticleReal m = this-> mass;
const auto pusher_algo = WarpX::particle_pusher_algo;
const auto do_crr = do_classical_radiation_reaction;
#ifdef WARPX_QED
const auto do_sync = m_do_qed_quantum_sync;
amrex::Real t_chi_max = 0.0;
if (do_sync) t_chi_max = m_shr_p_qs_engine->get_minimum_chi_part();
QuantumSynchrotronEvolveOpticalDepth evolve_opt;
amrex::ParticleReal* AMREX_RESTRICT p_optical_depth_QSR = nullptr;
const bool local_has_quantum_sync = has_quantum_sync();
if (local_has_quantum_sync) {
evolve_opt = m_shr_p_qs_engine->build_evolve_functor();
p_optical_depth_QSR = pti.GetAttribs(particle_comps["opticalDepthQSR"]).dataPtr() + offset;
}
#endif
const auto t_do_not_gather = do_not_gather;
amrex::ParallelFor( np_to_push, [=] AMREX_GPU_DEVICE (long ip)
{
amrex::ParticleReal xp, yp, zp;
getPosition(ip, xp, yp, zp);
if (save_previous_position) {
#if (AMREX_SPACEDIM >= 2)
x_old[ip] = xp;
#endif
#if defined(WARPX_DIM_3D)
y_old[ip] = yp;
#endif
z_old[ip] = zp;
}
amrex::ParticleReal Exp = 0._rt, Eyp = 0._rt, Ezp = 0._rt;
amrex::ParticleReal Bxp = 0._rt, Byp = 0._rt, Bzp = 0._rt;
if(!t_do_not_gather){
// first gather E and B to the particle positions
doGatherShapeN(xp, yp, zp, Exp, Eyp, Ezp, Bxp, Byp, Bzp,
ex_arr, ey_arr, ez_arr, bx_arr, by_arr, bz_arr,
ex_type, ey_type, ez_type, bx_type, by_type, bz_type,
dx_arr, xyzmin_arr, lo, n_rz_azimuthal_modes,
nox, galerkin_interpolation);
}
// Externally applied E and B-field in Cartesian co-ordinates
getExternalEB(ip, Exp, Eyp, Ezp, Bxp, Byp, Bzp);
scaleFields(xp, yp, zp, Exp, Eyp, Ezp, Bxp, Byp, Bzp);
doParticlePush(getPosition, setPosition, copyAttribs, ip,
ux[ip], uy[ip], uz[ip],
Exp, Eyp, Ezp, Bxp, Byp, Bzp,
ion_lev ? ion_lev[ip] : 0,
m, q, pusher_algo, do_crr, do_copy,
#ifdef WARPX_QED
do_sync,
t_chi_max,
#endif
dt);
#ifdef WARPX_QED
if (local_has_quantum_sync) {
evolve_opt(ux[ip], uy[ip], uz[ip],
Exp, Eyp, Ezp,Bxp, Byp, Bzp,
dt, p_optical_depth_QSR[ip]);
}
#endif
});
}
void
PhysicalParticleContainer::InitIonizationModule ()
{
if (!do_field_ionization) return;
ParmParse pp_species_name(species_name);
if (charge != PhysConst::q_e){
ablastr::warn_manager::WMRecordWarning("Species",
"charge != q_e for ionizable species '" +
species_name + "':" +
"overriding user value and setting charge = q_e.");
charge = PhysConst::q_e;
}
utils::parser::queryWithParser(
pp_species_name, "ionization_initial_level", ionization_initial_level);
pp_species_name.get("ionization_product_species", ionization_product_name);
pp_species_name.get("physical_element", physical_element);
// Add runtime integer component for ionization level
AddIntComp("ionizationLevel");
// Get atomic number and ionization energies from file
const int ion_element_id = utils::physics::ion_map_ids.at(physical_element);
ion_atomic_number = utils::physics::ion_atomic_numbers[ion_element_id];
Vector<Real> h_ionization_energies(ion_atomic_number);
const int offset = utils::physics::ion_energy_offsets[ion_element_id];
for(int i=0; i<ion_atomic_number; i++){
h_ionization_energies[i] =
utils::physics::table_ionization_energies[i+offset];
}
// Compute ADK prefactors (See Chen, JCP 236 (2013), equation (2))
// For now, we assume l=0 and m=0.
// The approximate expressions are used,
// without Gamma function
constexpr auto a3 = PhysConst::alpha*PhysConst::alpha*PhysConst::alpha;
constexpr auto a4 = a3 * PhysConst::alpha;
constexpr Real wa = a3 * PhysConst::c / PhysConst::r_e;
constexpr Real Ea = PhysConst::m_e * PhysConst::c*PhysConst::c /PhysConst::q_e *
a4/PhysConst::r_e;
constexpr Real UH = utils::physics::table_ionization_energies[0];
const Real l_eff = std::sqrt(UH/h_ionization_energies[0]) - 1._rt;
const Real dt = WarpX::GetInstance().getdt(0);
ionization_energies.resize(ion_atomic_number);
adk_power.resize(ion_atomic_number);
adk_prefactor.resize(ion_atomic_number);
adk_exp_prefactor.resize(ion_atomic_number);
Gpu::copyAsync(Gpu::hostToDevice,
h_ionization_energies.begin(), h_ionization_energies.end(),
ionization_energies.begin());
Real const* AMREX_RESTRICT p_ionization_energies = ionization_energies.data();
Real * AMREX_RESTRICT p_adk_power = adk_power.data();
Real * AMREX_RESTRICT p_adk_prefactor = adk_prefactor.data();
Real * AMREX_RESTRICT p_adk_exp_prefactor = adk_exp_prefactor.data();
amrex::ParallelFor(ion_atomic_number, [=] AMREX_GPU_DEVICE (int i) noexcept
{
Real n_eff = (i+1) * std::sqrt(UH/p_ionization_energies[i]);
Real C2 = std::pow(2._rt,2._rt*n_eff)/(n_eff*std::tgamma(n_eff+l_eff+1._rt)*std::tgamma(n_eff-l_eff));
p_adk_power[i] = -(2._rt*n_eff - 1._rt);
Real Uion = p_ionization_energies[i];
p_adk_prefactor[i] = dt * wa * C2 * ( Uion/(2._rt*UH) )
* std::pow(2._rt*std::pow((Uion/UH),3._rt/2._rt)*Ea,2._rt*n_eff - 1._rt);
p_adk_exp_prefactor[i] = -2._rt/3._rt * std::pow( Uion/UH,3._rt/2._rt) * Ea;
});
Gpu::synchronize();
}
IonizationFilterFunc
PhysicalParticleContainer::getIonizationFunc (const WarpXParIter& pti,
int lev,
amrex::IntVect ngEB,
const amrex::FArrayBox& Ex,
const amrex::FArrayBox& Ey,
const amrex::FArrayBox& Ez,
const amrex::FArrayBox& Bx,
const amrex::FArrayBox& By,
const amrex::FArrayBox& Bz)
{
WARPX_PROFILE("PhysicalParticleContainer::getIonizationFunc()");
return IonizationFilterFunc(pti, lev, ngEB, Ex, Ey, Ez, Bx, By, Bz,
ionization_energies.dataPtr(),
adk_prefactor.dataPtr(),
adk_exp_prefactor.dataPtr(),
adk_power.dataPtr(),
particle_icomps["ionizationLevel"],
ion_atomic_number);
}
void PhysicalParticleContainer::resample (const int timestep)
{
// In heavily load imbalanced simulations, MPI processes with few particles will spend most of
// the time at the MPI synchronization in TotalNumberOfParticles(). Having two profiler entries
// here is thus useful to avoid confusing time spent waiting for other processes with time
// spent doing actual resampling.
WARPX_PROFILE_VAR_NS("MultiParticleContainer::doResampling::MPI_synchronization",
blp_resample_synchronization);
WARPX_PROFILE_VAR_NS("MultiParticleContainer::doResampling::ActualResampling",
blp_resample_actual);
WARPX_PROFILE_VAR_START(blp_resample_synchronization);
const amrex::Real global_numparts = TotalNumberOfParticles();
WARPX_PROFILE_VAR_STOP(blp_resample_synchronization);
WARPX_PROFILE_VAR_START(blp_resample_actual);
if (m_resampler.triggered(timestep, global_numparts))
{
amrex::Print() << Utils::TextMsg::Info("Resampling " + species_name);
for (int lev = 0; lev <= maxLevel(); lev++)
{
for (WarpXParIter pti(*this, lev); pti.isValid(); ++pti)
{
m_resampler(pti, lev, this);
}
}
}
WARPX_PROFILE_VAR_STOP(blp_resample_actual);
}
#ifdef WARPX_QED
bool PhysicalParticleContainer::has_quantum_sync () const
{
return m_do_qed_quantum_sync;
}
bool PhysicalParticleContainer::has_breit_wheeler () const
{
return m_do_qed_breit_wheeler;
}
void
PhysicalParticleContainer::
set_breit_wheeler_engine_ptr (std::shared_ptr<BreitWheelerEngine> ptr)
{
m_shr_p_bw_engine = ptr;
}
void
PhysicalParticleContainer::
set_quantum_sync_engine_ptr (std::shared_ptr<QuantumSynchrotronEngine> ptr)
{
m_shr_p_qs_engine = ptr;
}
PhotonEmissionFilterFunc
PhysicalParticleContainer::getPhotonEmissionFilterFunc ()
{
WARPX_PROFILE("PhysicalParticleContainer::getPhotonEmissionFunc()");
return PhotonEmissionFilterFunc{particle_runtime_comps["opticalDepthQSR"]};
}
PairGenerationFilterFunc
PhysicalParticleContainer::getPairGenerationFilterFunc ()
{
WARPX_PROFILE("PhysicalParticleContainer::getPairGenerationFunc()");
return PairGenerationFilterFunc{particle_runtime_comps["opticalDepthBW"]};
}
#endif
|