-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnonthermal.cc
2700 lines (2247 loc) · 111 KB
/
nonthermal.cc
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
#include "nonthermal.h"
#include <gsl/gsl_blas.h>
#include <gsl/gsl_cblas.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_matrix_double.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_vector_double.h>
#include <mpi.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <functional>
#include <ios>
#include <numeric>
#include <ranges>
#include <span>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
#include "artisoptions.h"
#include "atomic.h"
#include "constants.h"
#include "decay.h"
#include "globals.h"
#include "grid.h"
#include "input.h"
#include "ltepop.h"
#include "macroatom.h"
#include "packet.h"
#include "sn3d.h"
#include "stats.h"
#include "thermalbalance.h"
namespace nonthermal {
namespace {
// minimum number fraction of the total population to include in SF solution
constexpr double minionfraction = 1.e-8;
// minimum deposition rate density (eV/s/cm^3) to solve SF equation
constexpr double MINDEPRATE = 0.;
// Bohr radius squared in cm^2
constexpr double A_naught_squared = 2.800285203e-17;
constexpr std::array<std::string, 28> shellnames{"K ", "L1", "L2", "L3", "M1", "M2", "M3", "M4", "M5", "N1",
"N2", "N3", "N4", "N5", "N6", "N7", "O1", "O2", "O3", "O4",
"O5", "O6", "O7", "P1", "P2", "P3", "P4", "Q1"};
std::vector<std::vector<double>> elements_electron_binding;
std::vector<std::vector<int>> elements_shells_q;
struct collionrow {
int Z{-1};
int ionstage{-1};
int n = -1;
int l = -1;
double ionpot_ev{NAN};
double A{NAN};
double B{NAN};
double C{NAN};
double D{NAN};
// track the statistical weight represented by the values below, so they can be updated with new g-weighted averaged
// values
double auger_g_accumulated = 0.;
// probability of 0, 1, ..., NT_MAX_AUGER_ELECTRONS Auger electrons being ejected when the shell is ionised
std::array<double, NT_MAX_AUGER_ELECTRONS + 1> prob_num_auger{};
// the average kinetic energy released in Auger electrons after making a hole in this shell
float en_auger_ev{NAN};
float n_auger_elec_avg{NAN};
collionrow() {
std::ranges::fill(prob_num_auger, 0.);
prob_num_auger[0] = 1.;
}
};
std::vector<collionrow> colliondata;
static_assert(SF_EMIN > 0.);
constexpr double DELTA_E = (SF_EMAX - SF_EMIN) / (SFPTS - 1);
// energy grid on which solution is sampled [eV]
constexpr auto engrid(int index) -> double { return SF_EMIN + (index * DELTA_E); }
const auto logengrid = []() {
std::vector<double> _logengrid(SFPTS);
for (int i = 0; i < SFPTS; i++) {
_logengrid[i] = std::log(engrid(i));
}
return _logengrid;
}();
// samples of the source function (energy distribution of deposited energy)
constexpr auto sourcevec(const int index) {
assert_testmodeonly(index >= 0 && index < SFPTS);
// spread the source over some energy width
constexpr int source_spread_pts = static_cast<int>(SFPTS * 0.03333) + 1;
constexpr double source_spread_en = source_spread_pts * DELTA_E;
constexpr int sourcestartindex = SFPTS - source_spread_pts;
return (index < sourcestartindex) ? 0. : 1. / source_spread_en;
// or put all of the source into one point at SF_EMAX
// return (index < SFPTS - 1) ? 0. : 1. / DELTA_E;
// so that E_init_ev = SF_EMAX;
};
// the energy injection rate density (integral of E * S(e) dE) in eV/s/cm3 that the Spencer-Fano equation is solved for.
// This is arbitrary and and the solution will be scaled to match the actual energy deposition rate density.
constexpr double E_init_ev = []() {
double integral = 0.;
for (int s = 0; s < SFPTS; s++) {
integral += sourcevec(s) * DELTA_E * engrid(s);
}
return integral;
}();
// rhs is the constant term (not dependent on y func) in each equation
constexpr auto rhsvec = []() {
std::array<double, SFPTS> _rhsvec{};
double source_integral_to_SF_EMAX = 0.;
for (int i = SFPTS - 1; i >= 0; i--) {
_rhsvec[i] = source_integral_to_SF_EMAX * DELTA_E;
source_integral_to_SF_EMAX += sourcevec(i);
}
return _rhsvec;
}();
// Monte Carlo result - compare to analytical expectation
double nt_energy_deposited = 0;
struct NonThermalExcitation {
double frac_deposition; // the fraction of the non-thermal deposition energy going to the excitation transition
double ratecoeffperdeposition; // the excitation rate coefficient divided by the deposition rate density
int lineindex;
};
// pointer to either local or node-shared memory excitation list of all cells
std::span<NonThermalExcitation> excitations_list_all_cells{};
// the minimum of MAX_NT_EXCITATIONS_STORED and the number of included excitation transitions in the atomic dataset
int nt_excitations_stored = 0;
struct NonThermalSolutionIon {
float eff_ionpot{0.}; // these are used to calculate the non-thermal ionization rate
double fracdep_ionization_ion{0.}; // the fraction of the non-thermal deposition energy going to ionizing each ion
// probability that one ionisation of this ion will produce n Auger electrons.
// items sum to 1.0 for a given ion
std::array<float, NT_MAX_AUGER_ELECTRONS + 1> prob_num_auger{};
// like prob_num_auger, but energy weighted. items sum to 1.0 for an ion
std::array<float, NT_MAX_AUGER_ELECTRONS + 1> ionenfrac_num_auger{};
};
std::span<NonThermalSolutionIon> ion_data_all_cells{};
struct NonThermalCellSolution {
float frac_heating = 1.; // energy fractions should add up to 1.0 if the solution is good
float frac_ionization = 0.; // fraction of deposition energy going to ionization
float frac_excitation = 0.; // fraction of deposition energy going to excitation
int frac_excitations_list_size = 0;
int timestep_last_solved = -1; // the quantities above were calculated for this timestep
float nneperion_when_solved{NAN}; // the nne when the solver was last run
};
std::span<NonThermalCellSolution> nt_solution;
std::span<double> deposition_rate_density_all_cells;
constexpr auto uppertriangular(const int i, const int j) -> int {
assert_testmodeonly(i >= 0);
assert_testmodeonly(i < SFPTS);
// sometimes you might want to get an offset for a row using j = 0 < i, so that j can be added to it.
// assert_testmodeonly(j >= i);
assert_testmodeonly(j < SFPTS);
return (SFPTS * i) - (i * (i + 1) / 2) + j;
}
constexpr void compactify_triangular_matrix(std::vector<double> &matrix) {
for (int i = 1; i < SFPTS; i++) {
const int rowoffset = uppertriangular(i, 0);
for (int j = 0; j < i; j++) {
assert_always(matrix[(i * SFPTS) + j] == 0.);
}
for (int j = i; j < SFPTS; j++) {
matrix[rowoffset + j] = matrix[(i * SFPTS) + j];
}
}
}
constexpr void decompactify_triangular_matrix(std::vector<double> &matrix) {
for (int i = SFPTS - 1; i > 0; i--) {
const int rowoffset = uppertriangular(i, 0);
for (int j = SFPTS - 1; j >= i; j--) {
matrix[(i * SFPTS) + j] = matrix[rowoffset + j];
}
for (int j = i - 1; j >= 0; j--) {
matrix[(i * SFPTS) + j] = 0.;
}
}
}
void read_shell_configs() {
assert_always(NT_WORKFUNCTION_USE_SHELL_OCCUPANCY_FILE);
auto shells_file = fstream_required("electron_shell_occupancy.txt", std::ios::in);
int nshells = 0; // number of shell in binding energy file
int n_z_binding = 0; // number of elements in file
std::string line;
assert_always(get_noncommentline(shells_file, line));
std::istringstream(line) >> nshells >> n_z_binding;
printout("Reading electron_shell_occupancy.txt with %d elements and %d shells\n", n_z_binding, nshells);
elements_shells_q.resize(n_z_binding, std::vector<int>(nshells, 0.));
assert_always(elements_shells_q.size() == elements_electron_binding.size());
int zminusone = 0;
while (get_noncommentline(shells_file, line)) {
std::istringstream ssline(line);
int z_element = 0;
assert_always(ssline >> z_element);
assert_always(elements_shells_q[zminusone].size() == elements_electron_binding[zminusone].size());
for (int shell = 0; shell < nshells; shell++) {
int q = 0;
assert_always(ssline >> q);
elements_shells_q.at(zminusone).at(shell) = q;
}
zminusone++;
}
}
void read_binding_energies() {
const bool binding_en_newformat_local = std::filesystem::exists("binding_energies_lotz_tab1and2.txt") ||
std::filesystem::exists("data/binding_energies_lotz_tab1and2.txt");
bool binding_en_newformat = binding_en_newformat_local;
// just in case the file system was faulty and the ranks disagree on the existence of the files
MPI_Allreduce(MPI_IN_PLACE, &binding_en_newformat, 1, MPI_C_BOOL, MPI_LOR, MPI_COMM_WORLD);
int nshells = 0; // number of shell in binding energy file
int n_z_binding = 0; // number of elements in binding energy file
const auto *filename = binding_en_newformat ? "binding_energies_lotz_tab1and2.txt" : "binding_energies.txt";
auto binding_energies_file = fstream_required(filename, std::ios::in);
std::string line;
assert_always(get_noncommentline(binding_energies_file, line));
std::istringstream(line) >> nshells >> n_z_binding;
printout("Reading binding energies file '%s' with %d elements and %d shells\n", filename, n_z_binding, nshells);
elements_electron_binding.resize(n_z_binding, std::vector<double>(nshells, 0.));
for (int zminusone = 0; zminusone < n_z_binding; zminusone++) {
assert_always(get_noncommentline(binding_energies_file, line));
std::istringstream ssline(line);
// new file as an atomic number column
if (binding_en_newformat) {
int z_element{-1};
ssline >> z_element;
assert_always(z_element == (zminusone + 1));
}
for (int shell = 0; shell < nshells; shell++) {
float bindingenergy = 0.;
assert_always(ssline >> bindingenergy);
elements_electron_binding.at(zminusone).at(shell) = bindingenergy * EV;
}
}
if constexpr (NT_WORKFUNCTION_USE_SHELL_OCCUPANCY_FILE) {
if (!binding_en_newformat) {
printout(
"NT_WORKFUNCTION_USE_SHELL_OCCUPANCY_FILE is true, but could not find binding_energies_lotz_tab1and2.txt\n");
}
assert_always(binding_en_newformat);
read_shell_configs();
}
}
[[nodiscard]] auto get_cell_ntexcitations(const int nonemptymgi) {
return excitations_list_all_cells.subspan(nonemptymgi * nt_excitations_stored,
nt_solution[nonemptymgi].frac_excitations_list_size);
}
[[nodiscard]] auto get_cell_ion_data(const int nonemptymgi) {
return ion_data_all_cells.subspan(nonemptymgi * get_includedions(), get_includedions());
}
auto get_auger_probability(const int nonemptymgi, const int element, const int ion, const int naugerelec) {
assert_always(naugerelec <= NT_MAX_AUGER_ELECTRONS);
const int uniqueionindex = get_uniqueionindex(element, ion);
return get_cell_ion_data(nonemptymgi)[uniqueionindex].prob_num_auger[naugerelec];
}
auto get_ion_auger_enfrac(const int nonemptymgi, const int element, const int ion, const int naugerelec) {
assert_always(naugerelec <= NT_MAX_AUGER_ELECTRONS);
const int uniqueionindex = get_uniqueionindex(element, ion);
return get_cell_ion_data(nonemptymgi)[uniqueionindex].ionenfrac_num_auger[naugerelec];
}
void check_auger_probabilities(int nonemptymgi) {
bool problem_found = false;
for (int element = 0; element < get_nelements(); element++) {
for (int ion = 0; ion < get_nions(element) - 1; ion++) {
double prob_sum = 0.;
double ionenfrac_sum = 0.;
for (int a = 0; a <= NT_MAX_AUGER_ELECTRONS; a++) {
prob_sum += get_auger_probability(nonemptymgi, element, ion, a);
ionenfrac_sum += get_ion_auger_enfrac(nonemptymgi, element, ion, a);
}
if (fabs(prob_sum - 1.0) > 0.001) {
printout("Problem with Auger probabilities for cell %d Z=%d ionstage %d prob_sum %g\n",
grid::get_mgi_of_nonemptymgi(nonemptymgi), get_atomicnumber(element), get_ionstage(element, ion),
prob_sum);
for (int a = 0; a <= NT_MAX_AUGER_ELECTRONS; a++) {
printout("%d: %g\n", a, get_auger_probability(nonemptymgi, element, ion, a));
}
problem_found = true;
}
if (fabs(ionenfrac_sum - 1.0) > 0.001) {
printout("Problem with Auger energy frac sum for cell %d Z=%d ionstage %d ionenfrac_sum %g\n",
grid::get_mgi_of_nonemptymgi(nonemptymgi), get_atomicnumber(element), get_ionstage(element, ion),
ionenfrac_sum);
for (int a = 0; a <= NT_MAX_AUGER_ELECTRONS; a++) {
printout("%d: %g\n", a, get_ion_auger_enfrac(nonemptymgi, element, ion, a));
}
problem_found = true;
}
}
}
assert_always(!problem_found);
}
void read_auger_data() {
printout("Reading Auger effect data...\n");
FILE *augerfile = fopen_required("auger-km1993-table2.txt", "r");
char line[151] = "";
// map x-ray notation shells K L1 L2 L3 M1 M2 M3 to quantum numbers n and l
const int xrayn[7] = {1, 2, 2, 2, 3, 3, 3};
const int xrayl[7] = {0, 0, 1, 1, 0, 1, 1};
const int xrayg[7] = {2, 2, 2, 4, 2, 2, 4}; // g statistical weight = 2j + 1
while (feof(augerfile) == 0) {
if (line != fgets(line, 151, augerfile)) {
break;
}
int Z = -1;
int ionstage = -1;
int shellnum = -1;
char *linepos = line;
int offset = 0;
assert_always(sscanf(linepos, "%d %d%n", &Z, &ionstage, &offset) == 2);
assert_always(offset == 5);
linepos += offset;
const int element = get_elementindex(Z);
if (element >= 0 && get_ionstage(element, 0) <= ionstage &&
ionstage < (get_ionstage(element, 0) + get_nions(element))) {
float ionpot_ev = -1;
float en_auger_ev_total_nocorrection = -1;
int epsilon_e3 = -1;
assert_always(sscanf(linepos, "%d %g %g %d%n", &shellnum, &ionpot_ev, &en_auger_ev_total_nocorrection,
&epsilon_e3, &offset) == 4);
assert_always(offset == 20);
float n_auger_elec_avg = 0;
double prob_num_auger[NT_MAX_AUGER_ELECTRONS + 1];
for (int a = 0; a < 9; a++) {
linepos = line + 26 + (a * 5);
// have to read out exactly 5 characters at a time because the columns are sometimes not separated by a space
char strprob[6] = "00000";
assert_always(sscanf(linepos, "%5c%n", strprob, &offset) == 1);
assert_always(offset == 5);
strprob[5] = '\0';
int probnaugerelece4 = -1;
assert_always(sscanf(strprob, "%d", &probnaugerelece4) == 1);
const double probnaugerelec = probnaugerelece4 / 10000.;
assert_always(probnaugerelec <= 1.0);
n_auger_elec_avg += a * probnaugerelec;
if (a <= NT_MAX_AUGER_ELECTRONS) {
prob_num_auger[a] = probnaugerelec;
} else {
// add the rates of all higher ionisations to the top one
prob_num_auger[NT_MAX_AUGER_ELECTRONS] += probnaugerelec;
}
}
// use the epsilon correction factor as in equation 7 of Kaastra & Mewe (1993)
float en_auger_ev = en_auger_ev_total_nocorrection - (epsilon_e3 / 1000. * ionpot_ev);
const int n = xrayn[shellnum - 1];
const int l = xrayl[shellnum - 1];
const int g = xrayg[shellnum - 1];
if (!std::isfinite(en_auger_ev) || en_auger_ev < 0) {
printout(" WARNING: Z=%2d ionstage %2d shellnum %d en_auger_ev is %g. Setting to zero.\n", Z, ionstage,
shellnum, en_auger_ev);
en_auger_ev = 0.;
}
// now loop through shells with impact ionisation cross sections and apply Auger data that matches n, l values
for (auto &collionrow : colliondata) {
if (collionrow.Z == Z && collionrow.ionstage == ionstage && collionrow.n == n && collionrow.l == l) {
printout(
"Z=%2d ionstage %2d shellnum %d n %d l %d ionpot %7.2f E_A %8.1f E_A' %8.1f epsilon %6d <n_Auger> %5.1f "
"P(n_Auger)",
Z, ionstage, shellnum, n, l, ionpot_ev, en_auger_ev_total_nocorrection, en_auger_ev, epsilon_e3,
n_auger_elec_avg);
double prob_sum = 0.;
for (int a = 0; a <= NT_MAX_AUGER_ELECTRONS; a++) {
prob_sum += prob_num_auger[a];
printout(" %d: %4.2f", a, prob_num_auger[a]);
}
assert_always(fabs(prob_sum - 1.0) < 0.001);
printout("\n");
const bool found_existing_data = (collionrow.auger_g_accumulated > 0.);
// keep existing data but update according to statistical weight represented by existing and new data
const double oldweight = collionrow.auger_g_accumulated / (g + collionrow.auger_g_accumulated);
const double newweight = g / (g + collionrow.auger_g_accumulated);
collionrow.auger_g_accumulated += g;
// update the statistical-weight averaged values
collionrow.en_auger_ev = oldweight * collionrow.en_auger_ev + newweight * en_auger_ev;
collionrow.n_auger_elec_avg = oldweight * collionrow.n_auger_elec_avg + newweight * n_auger_elec_avg;
prob_sum = 0.;
for (int a = 0; a <= NT_MAX_AUGER_ELECTRONS; a++) {
collionrow.prob_num_auger[a] = oldweight * collionrow.prob_num_auger[a] + newweight * prob_num_auger[a];
prob_sum += collionrow.prob_num_auger[a];
}
assert_always(fabs(prob_sum - 1.0) < 0.001);
if (found_existing_data) {
printout(" same NL shell already has data from another X-ray shell. New g-weighted values: P(n_Auger)");
for (int a = 0; a <= NT_MAX_AUGER_ELECTRONS; a++) {
printout(" %d: %4.2f", a, collionrow.prob_num_auger[a]);
}
printout("\n");
}
}
}
}
}
fclose(augerfile);
}
auto get_approx_shell_occupancies(const int nbound, const int ioncharge) {
assert_always(nbound > 0);
assert_always(ioncharge >= 0);
const int Z = nbound + ioncharge;
std::vector<int> q;
q.resize(std::max(10UZ, elements_electron_binding[Z - 1].size()), 0);
for (int electron_loop = 0; electron_loop < nbound; electron_loop++) {
if (q[0] < 2) {
q[0]++; // K 1s
} else if (q[1] < 2) {
q[1]++; // L1 2s
} else if (q[2] < 2) {
q[2]++; // L2 2p[1/2]
} else if (q[3] < 4) {
q[3]++; // L3 2p[3/2]
} else if (q[4] < 2) {
q[4]++; // M1 3s
} else if (q[5] < 2) {
q[5]++; // M2 3p[1/2]
} else if (q[6] < 4) {
q[6]++; // M3 3p[3/2]
} else if (ioncharge == 0) {
if (q[9] < 2) {
q[9]++; // N1 4s
} else if (q[7] < 4) {
q[7]++; // M4 3d[3/2]
} else if (q[8] < 6) {
q[8]++; // M5 3d[5/2]
} else {
printout("Going beyond the 4s shell in NT calculation. Abort!\n");
std::abort();
}
} else if (ioncharge == 1) {
if (q[9] < 1) {
q[9]++; // N1 4s
} else if (q[7] < 4) {
q[7]++; // M4 3d[3/2]
} else if (q[8] < 6) {
q[8]++; // M5 3d[5/2]
} else {
printout("Going beyond the 4s shell in NT calculation. Abort!\n");
std::abort();
}
} else if (ioncharge > 1) {
if (q[7] < 4) {
q[7]++; // M4 3d[3/2]
} else if (q[8] < 6) {
q[8]++; // M5 3d[5/2]
} else {
printout("Going beyond the 4s shell in NT calculation. Abort!\n");
std::abort();
}
}
}
assert_always(nbound == std::accumulate(q.begin(), q.end(), 0));
return q;
}
auto get_shell_occupancies(const int nbound, const int ioncharge) {
assert_always(nbound > 0);
assert_always(ioncharge >= 0);
const int Z = nbound + ioncharge;
if constexpr (!NT_WORKFUNCTION_USE_SHELL_OCCUPANCY_FILE) {
return get_approx_shell_occupancies(nbound, ioncharge);
}
const auto &element_shells_q_neutral = elements_shells_q.at(Z - 1);
const size_t shellcount = std::min(element_shells_q_neutral.size(), elements_electron_binding[Z - 1].size());
auto element_shells_q = std::vector<int>(shellcount);
int electron_count = 0;
for (size_t shellindex = 0; shellindex < shellcount; shellindex++) {
const int electronsinshell_neutral = element_shells_q_neutral.at(shellindex);
int electronsinshell = 0;
if ((electron_count + electronsinshell_neutral) <= nbound) {
electronsinshell = electronsinshell_neutral;
} else {
electronsinshell = nbound - electron_count;
}
assert_always(electronsinshell <= electronsinshell_neutral);
element_shells_q[shellindex] = electronsinshell;
electron_count += electronsinshell;
assert_always(electron_count <= nbound);
}
return element_shells_q;
}
auto get_sum_q_over_binding_energy(const int element, const int ion) -> double {
const int ioncharge = get_ionstage(element, ion) - 1;
const int nbound = get_atomicnumber(element) - ioncharge; // number of bound electrons
if (nbound <= 0) {
return 0.;
}
// get the approximate shell occupancy if we don't have the data file
const auto shells_q = get_shell_occupancies(nbound, ioncharge);
const auto &binding_energies = elements_electron_binding.at(get_atomicnumber(element) - 1);
double total = 0.;
for (int shellindex = 0; shellindex < std::ssize(shells_q); shellindex++) {
const int electronsinshell = shells_q[shellindex];
if (electronsinshell <= 0) {
continue;
}
double enbinding = binding_energies.at(shellindex);
const double ionpot = globals::elements[element].ions[ion].ionpot;
if (enbinding <= 0) {
// if we don't have the shell's binding energy, use the previous one
enbinding = binding_energies.at(shellindex - 1);
assert_always(enbinding > 0);
}
total += electronsinshell / std::max(ionpot, enbinding);
}
return total;
}
void read_collion_data() {
printout("Reading collisional ionization data from collion.txt...\n");
FILE *cifile = fopen_required("collion.txt", "r");
int colliondatacount = 0;
assert_always(fscanf(cifile, "%d", &colliondatacount) == 1);
printout("Reading %d collisional transition rows\n", colliondatacount);
assert_always(colliondatacount >= 0);
for (int i = 0; i < colliondatacount; i++) {
collionrow collionrow{};
int nelec = -1;
assert_always(fscanf(cifile, "%2d %2d %1d %1d %lg %lg %lg %lg %lg", &collionrow.Z, &nelec, &collionrow.n,
&collionrow.l, &collionrow.ionpot_ev, &collionrow.A, &collionrow.B, &collionrow.C,
&collionrow.D) == 9);
assert_always(nelec > 0);
collionrow.ionstage = collionrow.Z - nelec + 1;
const int element = get_elementindex(collionrow.Z);
if (element < 0 || collionrow.ionstage < get_ionstage(element, 0) ||
collionrow.ionstage > get_ionstage(element, get_nions(element) - 1)) {
continue;
}
std::ranges::fill(collionrow.prob_num_auger, 0.);
collionrow.prob_num_auger[0] = 1.;
collionrow.auger_g_accumulated = 0.;
collionrow.en_auger_ev = 0.;
collionrow.n_auger_elec_avg = 0.;
colliondata.push_back(collionrow);
}
printout("Stored %zu of %d input shell cross sections\n", colliondata.size(), colliondatacount);
for (int element = 0; element < get_nelements(); element++) {
const int Z = get_atomicnumber(element);
for (int ion = 0; ion < get_nions(element); ion++) {
const int ionstage = get_ionstage(element, ion);
const bool any_data_matched = std::ranges::any_of(colliondata, [Z, ionstage](const collionrow &collionrow) {
return collionrow.Z == Z && collionrow.ionstage == ionstage;
});
if (!any_data_matched) {
const double ionpot_ev = globals::elements[element].ions[ion].ionpot / EV;
printout("No collisional ionisation data for Z=%d ionstage %d. Using Lotz approximation with ionpot = %g eV\n",
Z, ionstage, ionpot_ev);
const int ioncharge = ionstage - 1;
const int nbound = Z - ioncharge; // number of bound electrons
// get the approximate shell occupancy if we don't have the data file
auto shells_q = get_shell_occupancies(nbound, ioncharge);
int electron_count = 0;
for (int shellindex = 0; shellindex < std::ssize(shells_q); shellindex++) {
const int electronsinshell = shells_q.at(shellindex);
electron_count += electronsinshell;
if (electronsinshell <= 0) {
continue;
}
double enbinding = elements_electron_binding.at(Z - 1).at(shellindex);
const double ionpot = ionpot_ev * EV;
if (enbinding <= 0) {
// if we don't have the shell's binding energy, use the previous one
enbinding = elements_electron_binding.at(Z - 1).at(shellindex - 1);
assert_always(enbinding > 0);
}
const double p = std::max(ionpot, enbinding);
collionrow collionrow{};
collionrow.Z = Z;
collionrow.ionstage = ionstage;
collionrow.n = -1;
collionrow.l = -shellindex;
collionrow.ionpot_ev = p / EV;
collionrow.A = -1.;
collionrow.B = -1.;
collionrow.C = -1.;
collionrow.D = -1.;
std::ranges::fill(collionrow.prob_num_auger, 0.);
collionrow.prob_num_auger[0] = 1.;
collionrow.auger_g_accumulated = 0.;
collionrow.en_auger_ev = 0.;
collionrow.n_auger_elec_avg = 0.;
colliondata.push_back(collionrow);
if (electron_count >= nbound) {
break;
}
}
}
}
}
colliondata.shrink_to_fit();
std::ranges::stable_sort(colliondata, [](const collionrow &a, const collionrow &b) {
return std::tie(a.Z, a.ionstage, a.ionpot_ev, a.n, a.l) < std::tie(b.Z, b.ionstage, b.ionpot_ev, b.n, b.l);
});
fclose(cifile);
if (NT_MAX_AUGER_ELECTRONS > 0) {
read_auger_data();
}
}
auto get_possible_nt_excitation_count() -> int {
// count the number of excitation transitions that pass the MAXNLEVELS_LOWER and MAXNLEVELS_UPPER conditions
// this count might be higher than the number of stored ratecoeffs due to the MAX_NT_EXCITATIONS_STORED limit
int ntexcitationcount = 0;
for (int element = 0; element < get_nelements(); element++) {
for (int ion = 0; ion < get_nions(element); ion++) {
const int lower_nlevels = std::min(NTEXCITATION_MAXNLEVELS_LOWER, get_nlevels(element, ion));
for (int lower = 0; lower < lower_nlevels; lower++) {
const int nuptrans = get_nuptrans(element, ion, lower);
for (int t = 0; t < nuptrans; t++) {
const int upper = get_uptranslist(element, ion, lower)[t].targetlevelindex;
if (upper < NTEXCITATION_MAXNLEVELS_UPPER) {
ntexcitationcount++;
}
}
}
}
}
return ntexcitationcount;
}
void zero_all_effionpot(const int nonemptymgi) {
for (int uniqueionindex = 0; uniqueionindex < get_includedions(); uniqueionindex++) {
auto &ion_data = get_cell_ion_data(nonemptymgi)[uniqueionindex];
ion_data.eff_ionpot = 0.;
std::ranges::fill(get_cell_ion_data(nonemptymgi)[uniqueionindex].prob_num_auger, 0.);
std::ranges::fill(get_cell_ion_data(nonemptymgi)[uniqueionindex].ionenfrac_num_auger, 0.);
ion_data.prob_num_auger[0] = 1.;
ion_data.ionenfrac_num_auger[0] = 1.;
const auto [element, ion] = get_ionfromuniqueionindex(uniqueionindex);
assert_always(fabs(get_auger_probability(nonemptymgi, element, ion, 0) - 1.0) < 1e-3);
assert_always(fabs(get_ion_auger_enfrac(nonemptymgi, element, ion, 0) - 1.0) < 1e-3);
}
check_auger_probabilities(nonemptymgi);
}
[[nodiscard]] constexpr auto get_energyindex_ev_lteq(const double energy_ev) -> int
// finds the highest energy point <= energy_ev
{
const int index = std::floor((energy_ev - SF_EMIN) / DELTA_E);
return std::clamp(index, 0, SFPTS - 1);
}
[[nodiscard]] constexpr auto get_energyindex_ev_gteq(const double energy_ev) -> int
// finds the highest energy point <= energy_ev
{
const int index = std::ceil((energy_ev - SF_EMIN) / DELTA_E);
return std::clamp(index, 0, SFPTS - 1);
}
// interpolate the y flux values to get the value at a given energy
// y has units of particles / cm2 / s / eV
[[nodiscard]] constexpr auto get_y(const std::array<double, SFPTS> &yfunc, const double energy_ev) -> double {
if (energy_ev <= 0) {
return 0.;
}
const int index = static_cast<int>((energy_ev - SF_EMIN) / DELTA_E);
// assert_always(index > 0);
if (index < 0) {
// return 0.;
assert_always(std::isfinite(yfunc[0]));
return yfunc[0];
}
if (index >= SFPTS - 1) {
return 0.;
}
const double enbelow = engrid(index);
const double enabove = engrid(index + 1);
const double ybelow = yfunc[index];
const double yabove = yfunc[index + 1];
const double x = (energy_ev - enbelow) / (enabove - enbelow);
return ((1 - x) * ybelow) + (x * yabove);
// or return the nearest neighbour
// return yfunc[index];
}
auto xs_ionization_lotz(const double en_erg, const collionrow &colliondata_ion) -> double {
const double ionpot_ev = colliondata_ion.ionpot_ev;
if (en_erg < (ionpot_ev * EV)) {
return 0.;
}
// const double gamma = (en_erg / (ME * std::pow(CLIGHT, 2))) + 1;
// const double beta = std::sqrt(1.0 - (1.0 / (std::pow(gamma, 2))));
const double beta = std::sqrt(2 * en_erg / ME) / CLIGHT;
const int ioncharge = colliondata_ion.ionstage - 1;
const int nbound = colliondata_ion.Z - ioncharge; // number of bound electrons
if (nbound <= 0) {
return 0.;
}
const int shellindex = -colliondata_ion.l;
const int electronsinshell = get_shell_occupancies(nbound, ioncharge)[shellindex];
const double p = colliondata_ion.ionpot_ev * EV;
if (en_erg > p) {
const double part_sigma_shell = (electronsinshell / p *
(std::log(std::pow(beta, 2) * ME * std::pow(CLIGHT, 2) / 2.0 / p) -
std::log10(1 - std::pow(beta, 2)) - std::pow(beta, 2)));
if (part_sigma_shell > 0.) {
constexpr double Aconst = 1.33e-14 * EV * EV;
const double sigma = 2 * Aconst / std::pow(beta, 2) / ME / std::pow(CLIGHT, 2) * part_sigma_shell;
assert_always(sigma >= 0);
return sigma;
}
}
return 0.;
}
auto get_xs_ionization_vector_lotz(std::array<double, SFPTS> &xs_vec, const collionrow &colliondata_ion) -> int {
const double ionpot_ev = colliondata_ion.ionpot_ev;
const int startindex = get_energyindex_ev_gteq(ionpot_ev);
std::fill_n(xs_vec.begin(), startindex, 0.);
for (int i = startindex; i < SFPTS; i++) {
xs_vec[i] = xs_ionization_lotz(engrid(i) * EV, colliondata_ion);
}
return startindex;
}
// xs_vec will be set with impact ionization cross sections [cm2] for E > ionpot_ev (and zeros below this energy)
// returns the index of the first energy point >= ionpot_ev
auto get_xs_ionization_vector(std::array<double, SFPTS> &xs_vec, const collionrow &colliondata_ion) -> int {
const double A = colliondata_ion.A;
if (A < 0) {
return get_xs_ionization_vector_lotz(xs_vec, colliondata_ion);
}
const double ionpot_ev = colliondata_ion.ionpot_ev;
const int startindex = get_energyindex_ev_gteq(ionpot_ev);
std::fill_n(xs_vec.begin(), startindex, 0.);
const double B = colliondata_ion.B;
const double C = colliondata_ion.C;
const double D = colliondata_ion.D;
for (int i = startindex; i < SFPTS; i++) {
const double u = engrid(i) / ionpot_ev;
const double xs_ioniz = 1e-14 *
(A * (1 - 1 / u) + B * std::pow((1 - (1 / u)), 2) + C * std::log(u) + D * std::log(u) / u) /
(u * std::pow(ionpot_ev, 2));
xs_vec[i] = xs_ioniz;
}
return startindex;
}
// distribution of secondary electron energies for primary electron with energy e_p
// Opal, Peterson, & Beaty (1971)
[[nodiscard]] constexpr auto Psecondary(const double e_p, const double epsilon, const double I, const double J)
-> double {
const double e_s = epsilon - I;
if (e_p <= I || e_s < 0.) {
return 0.;
}
assert_testmodeonly(J > 0);
assert_testmodeonly(e_p >= I);
assert_testmodeonly(e_s >= 0);
assert_testmodeonly(std::isfinite(std::atan((e_p - I) / 2 / J)));
return 1 / (J * std::atan((e_p - I) / 2 / J) * (1 + std::pow(e_s / J, 2)));
}
[[nodiscard]] constexpr auto get_J(const int Z, const int ionstage, const double ionpot_ev) -> double {
// returns an energy in eV
// values from Opal et al. 1971 as applied by Kozma & Fransson 1992
if (ionstage == 1) {
if (Z == 2) { // He I
return 15.8;
}
if (Z == 10) { // Ne I
return 24.2;
}
if (Z == 18) { // Ar I
return 10.;
}
}
return 0.6 * ionpot_ev;
}
// collisional excitation cross section in cm^2
// energies are in erg
constexpr auto xs_excitation(const int element, const int ion, const int lower, const int uptransindex,
const double epsilon_trans, const double lowerstatweight, const double energy) -> double {
if (energy < epsilon_trans) {
return 0.;
}
const auto &uptrans = get_uptranslist(element, ion, lower)[uptransindex];
if (uptrans.coll_str >= 0) {
// collision strength is available, so use it
// Li et al. 2012 equation 11
return std::pow(H_ionpot / energy, 2) / lowerstatweight * uptrans.coll_str * PI * A_naught_squared;
}
if (!uptrans.forbidden) {
// permitted E1 electric dipole transitions
const double U = energy / epsilon_trans;
// constexpr double g_bar = 0.2;
constexpr double A = 0.28;
constexpr double B = 0.15;
const double g_bar = (A * std::log(U)) + B;
constexpr double prefactor = 45.585750051; // 8 * pi^2/sqrt(3)
// Eq 4 of Mewe 1972, possibly from Seaton 1962?
return prefactor * A_naught_squared * std::pow(H_ionpot / epsilon_trans, 2) * uptrans.osc_strength * g_bar / U;
}
return 0.;
}
// -dE / dx for fast electrons
// energy is in ergs
// nne is the thermal electron density [cm^-3]
// return value has units of erg/cm
constexpr auto electron_loss_rate(const double energy, const double nne) -> double {
if (energy <= 0.) {
return 0;
}
// normally set to 1.0, but Shingles et al. (2021) boosted this to increase heating
constexpr double boostfactor = 1.;
const double omegap = std::sqrt(4 * PI * nne * std::pow(QE, 2) / ME);
const double zetae = H * omegap / 2 / PI;
if (energy > 14 * EV) {
return boostfactor * nne * 2 * PI * std::pow(QE, 4) / energy * std::log(2 * energy / zetae);
}
const double v = std::sqrt(2 * energy / ME);
return boostfactor * nne * 2 * PI * std::pow(QE, 4) / energy *
std::log(ME * std::pow(v, 3) / (EULERGAMMA * std::pow(QE, 2) * omegap));
}
// impact ionization cross section in cm^2
// energy and ionization_potential should be in eV
// fitting formula of Younger 1981
// called Q_i(E) in KF92 equation 7
constexpr auto xs_impactionization(const double energy_ev, const collionrow &colliondata_ion) -> double {
const double ionpot_ev = colliondata_ion.ionpot_ev;
const double u = energy_ev / ionpot_ev;
if (u <= 1.) {
return 0;
}
const double A = colliondata_ion.A;
if (A < 0) {
return xs_ionization_lotz(energy_ev / EV, colliondata_ion);
}
const double B = colliondata_ion.B;
const double C = colliondata_ion.C;
const double D = colliondata_ion.D;
return 1e-14 * (A * (1 - 1 / u) + B * std::pow((1 - (1 / u)), 2) + C * std::log(u) + D * std::log(u) / u) /
(u * std::pow(ionpot_ev, 2));
}
// Kozma & Fransson equation 6.
// Something related to a number of electrons, needed to calculate the heating fraction in equation 3
// not valid for energy > SF_EMIN
auto N_e(const int nonemptymgi, const double energy, const std::array<double, SFPTS> &yfunc) -> double {
const double energy_ev = energy / EV;
const double tot_nion = get_nnion_tot(nonemptymgi);
double N_e = 0.;
for (int element = 0; element < get_nelements(); element++) {
const int Z = get_atomicnumber(element);
const int nions = get_nions(element);
for (int ion = 0; ion < nions; ion++) {
double N_e_ion = 0.;
const int ionstage = get_ionstage(element, ion);
const double nnion = get_nnion(nonemptymgi, element, ion);
if (nnion < minionfraction * tot_nion) { // skip negligible ions
continue;
}