-
Notifications
You must be signed in to change notification settings - Fork 0
/
ionChem.py
982 lines (793 loc) · 37.5 KB
/
ionChem.py
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
# Ion Chemistry
"""
Preparation (could be written as module)
- load file with reaction and species information, inkl. stochiometry and charges etc.
- produce classes with sepcies concentration etc
- produce classes with reaction information
"""
import numpy as np
import matplotlib.pyplot as plt
from labellines import labelLine, labelLines
from scipy.interpolate import CubicSpline, interp1d, PchipInterpolator
import import_ipynb
from organizing import pcfg
con = pcfg
class constituent():
"""
For every constituent of the ionosphere, this class is generated to record its
-density
-production
-loss
and from that calculate the density of the new timestep.
All these values are in the form of an array as a height profile.
"""
def __init__(self, c_ID, name, charge, density):
self.c_ID = int(c_ID)
self.name = name
self.density = density
self.charge = int(charge)
self.loss = np.zeros(len(self.density))
self.prod = np.zeros(len(self.density))
self.dndt = np.zeros(len(self.density))
self.Te = np.nan
self.Tn = np.nan
self.Tr = np.nan
def log(self, density):
raise NotImplementedError
def integrate(self, prod, loss):
self.dndt = prod - loss
return NotImplementedError
def iterate_time(self, dt):
self.dndt = self.prod - self.loss
self.density = self.density + self.dndt * dt
self.density[self.density < 0] = 0
self.prod = np.zeros(len(self.density))
self.loss = np.zeros(len(self.density))
return NotImplementedError('Dont use Euler for integartion.')
#self.log(density)
class reaction():
"""
Defines all parameters of a chemical reaction in the ionosphere
Parameters:
r_ID: Name of the reaction (according to Brekke of Schunk and Nagy)
string
r_stoch: Reaction in chemical symbols, with stochiometry
string
educts: constituents that go into the reaction (lefthand side of r_stoch)
list of strings
products: constituents that come out of the reaction (righthand side of r_stoch)
list of strings
r_rate_string: the reaction rate, ready to be interpreted as a equation in python (with variables)
string
Methods:
r_rate: evaluates the reaction rate at a given temperature T
float
"""
def __init__(self, ionChem_inst, r_ID, r_name, r_stoch, educts, products, r_rate_string, all_species, branching):
self.r_ID = int(r_ID)
self.r_name = r_name
self.r_stoch = r_stoch
self.educts = educts
self.educts_ID = ionChem_inst.getConstituentsIDByName(self.educts)
self.products = products
self.products_ID = ionChem_inst.getConstituentsIDByName(self.products)
try: self.branching = np.array(branching, dtype = float)
except ValueError: self.branching = np.array([1])
self.r_rate_string = r_rate_string
self.ionChem_inst = ionChem_inst
def r_rate(self):
Te = self.ionChem_inst.Te
Tn = self.ionChem_inst.Tn
Tr = self.ionChem_inst.Tr
#evaluating the function and ensuring it has the dimension of z_model
self.rr = eval(self.r_rate_string) #* np.ones(self.ionChem_inst.n_heights)
return self.rr
def r_rate_t(self, Tn, Ti, Te):
Tr = (Ti + Tn)/2
#evaluating the function and ensuring it has the dimension of z_model
self.rr = eval(self.r_rate_string) * np.ones(self.ionChem_inst.n_heights)
return self.rr
def r_rate_t2(self, Tn, Ti, Te):
Tr = (Ti + Tn)/2
#evaluating the function and ensuring it has the dimension of z_model
self.rr = eval(self.r_rate_string)
return self.rr
class ionChem:
"""
Defines the ion chemistry model.
"""
def __init__(self, reactions_file, z_model):
self.z_model = z_model
self.n_heights = len(z_model)
self.all_species = []
self.load(reactions_file)
self.time = 0
self.iteration_step = 0
self.recording = []
def getConstituentsIDByName(self, names):
"""
Gets the ID from a constituents name
"""
ids = np.array([], dtype = int)
for name in names:
for c in self.all_species:
if c.name == name: ids = np.append(ids, c.c_ID)
return ids
def load(self, reactions_file):
"""
Load config file, specifying the species and reactions to be observed
"""
species_raw = np.array([], dtype = object)
reactions_raw = np.array([], dtype = str)
with open(reactions_file, 'r') as f:
content = f.read()
lines = [line for line in content.split('\n')]
for line_no, line in enumerate(lines):
if line[:4] == '--Co':
start_consprint = line_no
if line[:11] == '--Reactions':
start_reactions = line_no
for line_no, line in enumerate(lines):
if line_no > start_consprint+1 and line == '': break
if line_no > start_consprint+1:
species_raw = np.append(species_raw, line)
for line_no, line in enumerate(lines):
if line_no > start_reactions+1 and line == '': break
if line_no > start_reactions:
#print(line)
if line[0] != '#': reactions_raw = np.append(reactions_raw, line)
else: pass
species_str = np.array([c.replace('\t', '').replace(' ', '').split(';') for c in species_raw])
if con.print: print(species_str, '\n')
reactions_str = np.array([r.replace('\t', '').split(';') for r in reactions_raw], dtype = object)
if con.print: print(reactions_raw)
if con.print: print(reactions_str)
for c in species_str:
self.all_species.append(constituent(c[1], c[0], int(c[2]), np.zeros(self.n_heights)))
name = c[0].replace('+', 'p').replace('-', '').replace('(', '_').replace(')', '')
exec(f'self.{name} = self.all_species[-1]')
for ind, c in enumerate(self.all_species):
if con.print: print(c.name, c.c_ID)
if c.c_ID != ind: raise RuntimeError
self.ions = [c for c in self.all_species if c.charge == 1]
self.all_reactions = []
"""
arrays:
axis 0: reactions
axis 1: ID, reaction rate, educts, products
axis 2: e1, ... p1, p2, ...
"""
for i, r in enumerate(reactions_str):
r_ID = i
r_name = r[0]
r_rate_string = r[2].replace('m3s-1', '')#.replace(' ', '')
r_stoch = r[1][1:].replace('-', '')
r_branching = np.array(r[3].split(','))
educts, products = r_stoch.split('=>')
educts = educts.split(' + ')
educts = np.char.replace(educts, ' ', '')
products = products.split(' + ')
products = np.char.replace(products, ' ', '')
if con.print: print(r_ID, r_rate_string, educts, products)
#print(educts, products)
self.all_reactions.append(reaction(self, r_ID, r_name, r_stoch, educts, products, r_rate_string, self.all_species, r_branching))
exec(f'self.{r_name} = self.all_reactions[-1]')
def assign_densities(self
, z_model
, z_msis
, n_o1_msis
, n_n2_msis
, n_o2_msis
, z_iri
, ne_iri
, rel_o_p
, rel_n_p
, rel_h_p
, rel_he_p
, rel_o2_p
, rel_no_p):
"""
Assigns densities at the very beginning of the simulation.
"""
def mix_interpolation(density, z_iri, z_model, mixing_height):
nop_cs = CubicSpline(z_iri, density)(z_model) #produces negative electron density
nop_exp_int_log = np.exp(interp1d(z_iri[1:], np.log(density[1:]), fill_value = 'extrapolate')(z_model))
nop_mix = np.array([ *nop_exp_int_log[z_model < mixing_height], *nop_cs[z_model >= mixing_height]])
return nop_mix
ne_mix = mix_interpolation(ne_iri, z_iri, z_model, 90000)
self.e.density = ne_mix
nop_iri = ne_iri*rel_no_p
self.NOp.density = mix_interpolation(nop_iri, z_iri, z_model, 90000)
o2p_iri = ne_iri*rel_o2_p
self.O2p.density = mix_interpolation(o2p_iri, z_iri, z_model, 90000)
#self.O2p.density = self.e.density*np.exp(interp1d(z_iri, np.log(rel_o2_p), fill_value='extrapolate')(z_model))
op_iri = ne_iri*rel_o_p
self.Op.density = mix_interpolation(op_iri[rel_o_p > 0], z_iri[rel_o_p > 0], z_model, 150000) #[rel_o_p > 0] to avoid being tied to 0 => interpolation can produce densities larger than 0, even when rel_o_p = 0 in IRI model
self.Np.density = self.e.density*0
self.N2p.density = self.e.density*0
self.N.density = self.e.density*0
self.NO.density = self.e.density*0
self.N2.density = np.exp(PchipInterpolator(z_msis, np.log(n_n2_msis))(z_model))
self.O2.density = np.exp(PchipInterpolator(z_msis, np.log(n_o2_msis))(z_model))
self.O.density = np.exp(PchipInterpolator(z_msis, np.log(n_o1_msis))(z_model))
#check charge neutrality:
if con.print:
plt.figure()
plt.plot(self.e.density, z_model/1e3, label = 'ne_isi_int')
plt.plot(np.sum([c.density for c in self.all_species if c.charge ==1], axis = 0), z_model/1e3, label = 'ne_charge_neutral')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e (before and after charge neutrality)')
plt.legend()
plt.figure()
plt.plot(np.sum([c.density for c in self.all_species if c.charge ==1], axis = 0), z_model/1e3, label = 'ne_charge_neutral')
for c in self.ions:
plt.plot(c.density, z_model/1e3, label = 'ne_charge_neutral')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e and ions after charge neutrality')
plt.legend()
self.e.density = np.sum([c.density for c in self.all_species if c.charge ==1], axis = 0)
if con.print:
plt.figure()
plt.plot(self.all_species[0].density, z_model/1e3, label='e')
plt.plot(np.sum([i.density for i in self.all_species if i.charge == 1], axis = 0), z_model/1e3, label='p')
plt.legend()
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Comparison e- and ion+ density')
for c in self.all_species:
plt.figure()
plt.plot(c.density, self.z_model/1e3, label=c.name)
plt.legend()
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
def assign_temperatures(self, z_iri, t_neutral, t_ion, t_e):
"""
Assigns temperatures at the very beginning of the simulation
"""
self.Tn = PchipInterpolator(z_iri, t_neutral)(self.z_model)
self.Te = PchipInterpolator(z_iri, t_e)(self.z_model)
#self.Te = interp1d(z_iri, t_e, fill_value='extrapolate')(self.z_model)
self.Tr = PchipInterpolator(z_iri, t_ion)(self.z_model)
def update_timestep(self, dt):
"""
iteration step over timeintervalls using eulers method (unused at the moment)
#for i in range(2000):
# model.update_timestep(dt)
# #plot_snapshot(model)
# #if model.time%1 == 0: setup_.datadump(model)
# if model.iteration_step%1 == 0:
# setup_.datadump(model)
# if model.iteration_step%1000 == 0:
# print(model.time)
"""
for r in self.all_reactions:
#if con.print: print(r.educts, ' => ', r.products, r.r_rate_string)
#update Te!!
Te = 300
rr = r.r_rate(Te)
for e_ind in r.educts_ID: #refer to educts by index instead of name
n = self.all_species[e_ind].density
rr = rr*n
for e_ind in r.educts_ID:
self.all_species[e_ind].loss += rr
for p_ind in r.products_ID:
self.all_species[p_ind].prod += rr
for c in self.all_species:
c.iterate_time(dt)
self.time = self.time + dt
self.iteration_step += 1
self.check_chargeNeutrality()
raise RuntimeError('Dont use Euler integration!')
"""
1. check reaction rates
- if over threshold (1e2), treat as instantaneous (not in ODE)
1. build ODE
- separate independent species
- evalueate rr
2. solve it
"""
raise NotImplementedError('not yet implemented')
for c in self.all_species:
eff_rr = np.zeros([len(model.all_species), len(z_model)])
for r in self.all_reactions:
if c.c_ID in r.educts_ID:
if con.print:
print(r.r_stoch, '\n', c.name, r.educts[r.educts != c.name], '\n', c.c_ID, r.educts_ID[r.educts_ID != c.c_ID], '\n')
[id_other_const] = r.educts_ID[r.educts_ID != c.c_ID]
eff_rr[c.c_ID] = r.r_rate(300)*self.all_species[id_other_const].density
if any(eff_rr[c.c_ID, :] > 1e2):
filter_ = np.array(eff_rr[c.c_ID, :] > 1e2)
def check_chargeNeutrality(self):
"""
checks charge neutrality by adding up all charge densities, and checking if it balances to 0.
The charge density is divided by the density for normalization.
"""
total_charge_density = np.zeros(self.all_species[0].density.shape)
density_cc = np.zeros(self.all_species[0].density.shape)
for c in self.all_species:
if c.charge != 0:
if c.density.dtype == 'complex128':
breakpoint()
raise RuntimeError('Complex Density')
total_charge_density += c.density * c.charge
density_cc += c.density
reduced_tot_charge = total_charge_density / density_cc
if any(reduced_tot_charge.flat > 1e-10):
import matplotlib.pyplot as plt
plt.figure()
plt.plot(reduced_tot_charge, self.z_model/1e3)
plt.xlabel('Relative Charge density deviation [C]')
plt.ylabel('Altitude [km]')
plt.title('Relative Charge Density profile at time ' + str(self.time) + ', step: ' + str(self.iteration_step) + '\n (should be 0, -1 means double as many electrons)')
breakpoint()
raise RuntimeError('Charge neutrality violated')
if con.print:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(reduced_tot_charge, self.z_model/1e3)
plt.xlabel('Reduced Charge density [C]')
plt.ylabel('Altitude [km]')
plt.title('Relative Charge Density profile at time ' + str(self.time) + ', step: ' + str(self.iteration_step) + '\n (should be 0, -1 means double as many electrons)')
def plot_density(self):
plt.figure()
for c in self.all_species:
plt.plot(c.density, self.z_model/1e3, label = c.name)
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles')
plt.legend()
labelLines(plt.gca().get_lines(), align = False, xvals = [c.density[-1] for c in self.all_species])
def lossTable(self):
"""
generates a lookup table for raction rates from a list of reactions.
as well as a lookup table for reaction partners, such that the product a_i,j * n_i * n_j quickly can be computed
is intended to be called everytime the temperature is updated.
"""
self.loss_table_rr = np.zeros([len(self.all_reactions), len(self.all_species), self.n_heights])
self.loss_table_ind = np.zeros([len(self.all_reactions), len(self.all_species)])
for r in self.all_reactions:
i, j = r.educts_ID
self.loss_table_rr[r.r_ID, i] = r.r_rate()
self.loss_table_rr[r.r_ID, j] = r.r_rate()
self.loss_table_ind[r.r_ID, i] = j
self.loss_table_ind[r.r_ID, j] = i
def prodTable(self):
"""
generates a lookup table for raction rates from a list of reactions.
as well as a lookup table for reaction partners, such that the product a_i,j * n_i * n_j quickly can be computed
is intended to be called everytime the temperature is updated, tp update the reaction rate
"""
self.prod_table_rr = np.zeros([len(self.all_reactions), len(self.all_species), self.n_heights])
self.prod_table_ind = np.zeros([len(self.all_reactions), len(self.all_species), 2])
for r in self.all_reactions:
k, l = r.products_ID
i, j = r.educts_ID
if k == l:
self.prod_table_rr[r.r_ID, k] = 2 * r.r_rate()
self.prod_table_ind[r.r_ID, k, 1] = i
self.prod_table_ind[r.r_ID, k, 0] = j
else:
self.prod_table_rr[r.r_ID, k] = r.r_rate()
self.prod_table_rr[r.r_ID, l] = r.r_rate()
self.prod_table_ind[r.r_ID, k, 0] = j
self.prod_table_ind[r.r_ID, l, 0] = j
self.prod_table_ind[r.r_ID, k, 1] = i
self.prod_table_ind[r.r_ID, l, 1] = i
def effectiveReactionRate(self):
eff_rr = np.zeros([len(self.all_species), self.n_heights])
for c in self.all_species:
rr_species = np.zeros([len(self.all_species), self.n_heights], dtype = 'float64')
for r in self.all_reactions:
if c.c_ID in r.educts_ID:
#if con.print(): print(r.r_stoch, '\n', c.name, *r.educts[r.educts != c.name], '\n', c.c_ID, *r.educts_ID[r.educts_ID != c.c_ID], '\n')
[cid2] = r.educts_ID[r.educts_ID != c.c_ID]
rr_species[cid2] = r.r_rate()*self.all_species[cid2].density
eff_rr[c.c_ID, :] = np.sum(rr_species, axis = 0)
return eff_rr
def plot_decayTime(self):
"""
Plots the decay time of all species, taking all reactions into account, with the reaction rate at the current temperature,
as well as the current density of the other involved sepcies
decay time tau_i = 1/(sum(rr_ij * n_j))
"""
plt.figure()
for c in self.all_species:
rr_species = np.zeros([len(self.all_species), *self.all_species[0].density.shape])
for r in self.all_reactions:
if c.c_ID in r.educts_ID:
#if con.print: print(r.r_stoch, '\n', c.name, r.educts[r.educts != c.name], '\n', c.c_ID, r.educts_ID[r.educts_ID != c.c_ID], '\n')
[cid2] = r.educts_ID[r.educts_ID != c.c_ID]
rr_species[c.c_ID] = r.r_rate()*self.all_species[cid2].density
decay_time = 1/np.sum(rr_species, axis = 0)
line = plt.plot(decay_time, self.z_model/1e3, label = c.name)
plt.text(decay_time[0], self.z_model[1]/1e3, c.name, color = line[0].get_color())
plt.xscale('log')
plt.xlabel('Decay Time [s]')
plt.ylabel('Altitude [km]')
plt.show()
#plt.legend()
def plot_lossRate(self):
"""
Plots the loss rate of all species, taking all reactions into account, with the reaction rate at the current temperature,
as well as the current density of the other involved sepcies
decay time tau_i = sum(rr_ij * n_j)
"""
plt.figure()
for c in self.all_species:
rr_species = np.zeros([len(self.all_species), self.n_heights])
for r in self.all_reactions:
if c.c_ID in r.educts_ID:
#if con.print: print(r.r_stoch, '\n', c.c_ID, r.educts_ID[r.educts_ID != c.c_ID], '\n')
[cid2] = r.educts_ID[r.educts_ID != c.c_ID]
rr_species[c.c_ID] = r.r_rate()*self.all_species[cid2].density*self.all_species[c.c_ID].density
lossRate = np.sum(rr_species, axis = 0)
line = plt.plot(lossRate, self.z_model/1e3, label = c.name)
plt.text(lossRate[0], self.z_model[1]/1e3, c.name, color = line[0].get_color())
plt.xscale('log')
plt.xlabel('Loss rate [m-3 s-1]')
plt.ylabel('Altitude [km]')
#plt.legend()
plt.show()
def update_production(self, ne_prod):
p_Op = ne_prod * 0.56 * self.O.density / (0.92 * self.N2.density + self.O2.density + 0.56 * self.O.density)
p_O2p = ne_prod * 1.00 * self.O2.density / (0.92 * self.N2.density + self.O2.density + 0.56 * self.O.density)
p_N2p = ne_prod * 0.92 * self.N2.density / (0.92 * self.N2.density + self.O2.density + 0.56 * self.O.density)
self.e.prod = ne_prod
self.Op.prod = p_Op
self.O2p.prod = p_O2p
self.N2p.prod = p_N2p
# In[2]:
if con.print:
#defining file paths
msis_config = '/Users/ost051/Documents/PhD/Electron Precipitation/example/Meta-data/msis.txt'
iri_config = '/Users/ost051/Documents/PhD/Electron Precipitation/example/Meta-data/iri.txt'
import loadMSIS
import loadIRI
#load neutroal atmopshere model
[z_msis
, n_o1_msis
, n_n2_msis
, n_o2_msis
, mass_density
, temp_n_msis
, scale_height_msis] = loadMSIS.loadMSIS(msis_config)
#load ionosphere model
[z_iri
, ne_iri
, t_neutral
, t_ion
, t_e
, rel_o_p
, rel_n_p
, rel_h_p
, rel_he_p
, rel_o2_p
, rel_no_p] = loadIRI.loadIRI(iri_config)
z_model = np.arange(70000, 200000, 100)
# In[3]:
if con.print:
plt.figure()
plt.plot(t_e, z_iri/1e3, 'x', label = 'Te_iri')
plt.plot(PchipInterpolator(z_iri, t_e)(z_model), z_model/1e3, label = 'Te_pchip')
plt.xlabel('Temperature [K]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e')
plt.legend()
# self.Tn = interp1d(z_iri, t_neutral, fill_value = 'extrapolate')(self.z_model)
# self.Te = interp1d(z_iri, t_e, fill_value = 'extrapolate')(self.z_model)
# self.Tr = interp1d(z_iri, t_ion, fill_value = 'extrapolate')(self.z_model)
# In[4]:
if con.print:
def mix_interpolation(density, z_iri, z_model, mixing_height):
nop_cs = CubicSpline(z_iri, density)(z_model) #produces negative electron density
nop_exp_int_log = np.exp(interp1d(z_iri[1:], np.log(density[1:]), fill_value = 'extrapolate')(z_model))
nop_mix = np.array([ *nop_exp_int_log[z_model < mixing_height], *nop_cs[z_model >= mixing_height]])
return nop_mix
'''
Proper documentation in how to extrapolate densities from IRI model:
General problem is that IRI model only has data down to 80 km, while we need to extrapolate down to 70km.
Furthermore, IRI model says ne = 0 at 80km
Electron density:
np.exp(inter1d(..., np.log())) produces bumps in the density, discontinous in first derivative.
CubicSpline produces negative densities below 80km
Try:
1. mix between CubicSpline (above 80km) and exp(inter1d(log)) (below 80km) => selected
2. np.exp(CubicSpline(..., np.log(ne))) => no, behaves too jumpy
'''
ne_cs = CubicSpline(z_iri, ne_iri)(z_model) #produces negative electron density
ne_exp_int_log = np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]), fill_value = 'extrapolate')(z_model)) #bumps, i.e. discontnous in first derivative
ne_exp_cs_log = np.exp(CubicSpline(z_iri[1:], np.log(ne_iri[1:]))(z_model)) #behaves too jumpy
ne_mix = mix_interpolation(ne_iri, z_iri, z_model, 90000)
ne_pchip = np.exp(PchipInterpolator(z_iri[1:], np.log(ne_iri[1:]))(z_model))
#e
plt.figure()
plt.plot(ne_iri, z_iri/1e3, 'x', label = 'ne_iri')
plt.plot(ne_exp_int_log, z_model/1e3, label = 'int_exp(lin(log))')
plt.plot(ne_cs, z_model/1e3, label = 'int_CubSp')
plt.plot(ne_mix, z_model/1e3, label = 'mix')
plt.plot(ne_pchip, z_model/1e3, label = 'ne_pchip')
#plt.plot(ne_exp_cs_log, z_model/1e3, label = 'ne_exp_cs_log')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e')
plt.legend()
plt.ylim(70, 200)
# In[5]:
if con.print:
#NO+
'''
np.exp(interp1d(z_iri[1:], np.log(rel_no_p[1:]) is too choppy
'''
ne_model = ne_mix
nop_iri = ne_iri*rel_no_p
nop_mix = mix_interpolation(nop_iri, z_iri, z_model, 90000)
plt.figure()
plt.plot(ne_iri*rel_no_p, z_iri/1e3, 'x', label = 'n_NOp_iri')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(rel_no_p[1:]), fill_value = 'extrapolate')(z_model))*ne_model, z_model/1e3, label = 'int_exp(lin(log)) *ne')
plt.plot(CubicSpline(z_iri, rel_no_p)(z_model)*ne_model, z_model/1e3, label = 'int_CubSp * ne')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]*rel_no_p[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
plt.plot(CubicSpline(z_iri, ne_iri*rel_no_p)(z_model), z_model/1e3, label = 'int_CubSp')
plt.plot(nop_mix, z_model/1e3, label = 'mix')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of NO+')
plt.legend()
plt.ylim(70, 200)
# In[6]:
if con.print:
#O2+
'''
'''
ne_model = ne_mix
o2p_iri = ne_iri*rel_o2_p
o2p_mix = mix_interpolation(o2p_iri, z_iri, z_model, 90000)
plt.figure()
plt.plot(o2p_iri, z_iri/1e3, 'x', label = 'n_O2p_iri')
#plt.plot(np.exp(interp1d(z_iri[1:], np.log(rel_o2_p[1:]), fill_value = 'extrapolate')(z_model))*ne_model, z_model/1e3, label = 'int_exp(lin(log)) *ne')
#plt.plot(CubicSpline(z_iri, rel_o2_p)(z_model)*ne_model, z_model/1e3, label = 'int_CubSp * ne')
#plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]*rel_o2_p[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
#plt.plot(np.exp(CubicSpline(z_iri[1:], np.log(ne_iri[1:]*rel_o2_p[1:]))(z_model)), z_model/1e3, label = 'int_exp(CS(log))')
plt.plot(CubicSpline(z_iri, ne_iri*rel_o2_p)(z_model), z_model/1e3, label = 'int_CubSp')
plt.plot(o2p_mix, z_model/1e3, label = 'mix')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O2+')
plt.legend()
plt.ylim(70, 200)
# In[7]:
if con.print:
#O+
'''
'''
ne_model = ne_mix
op_iri = ne_iri*rel_o_p
#op_mix = mix_interpolation(op_iri, z_iri, z_model)
#[rel_o_p > 0] to avoid being tied to 0 => interpolation can produce densities larger than 0, even when rel_o_p = 0 in IRI model
op_mix = mix_interpolation(op_iri[rel_o_p > 0], z_iri[rel_o_p > 0], z_model, 150000)
plt.figure()
plt.plot(op_iri, z_iri/1e3, 'x', label = 'n_Op_iri')
plt.plot(np.exp(interp1d(z_iri[rel_o_p > 0], np.log(ne_iri[rel_o_p > 0]*rel_o_p[rel_o_p > 0]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'exp(int(log(!=0)))')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(rel_o_p[1:]), fill_value = 'extrapolate')(z_model))*ne_model, z_model/1e3, label = 'int_exp(lin(log)) *ne')
#plt.plot(CubicSpline(z_iri, rel_o_p)(z_model)*ne_model, z_model/1e3, label = 'int_CubSp * ne')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]*rel_o_p[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
#plt.plot(np.exp(CubicSpline(z_iri[1:], np.log(ne_iri[1:]*rel_o_p[1:]))(z_model)), z_model/1e3, label = 'int_exp(CS(log))')
#plt.plot(CubicSpline(z_iri, ne_iri*rel_o_p)(z_model), z_model/1e3, label = 'int_CubSp')
plt.plot(op_mix, z_model/1e3, label = 'mix')
#plt.plot(interp1d(z_iri, op_iri, fill_value='extrapolate')(z_model), z_model/1e3, label = 'int1d') #produces hanging structures
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O+')
plt.legend()
plt.ylim(70, 200)
print(op_mix)
# In[ ]:
# In[8]:
if con.print:
#N2
'''
'''
plt.figure()
plt.plot(n_n2_msis, z_msis/1e3, 'x', label = 'n_n2_msis')
plt.plot(np.exp(interp1d(z_msis, np.log(n_n2_msis), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'exp(int(log))')
plt.plot(CubicSpline(z_msis, n_n2_msis)(z_model), z_model/1e3, label = 'cs')
plt.plot(np.exp(PchipInterpolator(z_msis, np.log(n_n2_msis))(z_model)), z_model/1e3, label = 'pchip')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of N2')
plt.legend()
plt.ylim(70, 200)
# In[9]:
if con.print:
#O2
'''
'''
plt.figure()
plt.plot(n_o2_msis, z_msis/1e3, 'x', label = 'n_o2_msis')
plt.plot(np.exp(interp1d(z_msis, np.log(n_o2_msis), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'exp(int(log))')
plt.plot(CubicSpline(z_msis, n_o2_msis)(z_model), z_model/1e3, label = 'cs')
plt.plot(np.exp(PchipInterpolator(z_msis, np.log(n_o2_msis))(z_model)), z_model/1e3, label = 'pchip')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O2')
plt.legend()
plt.ylim(70, 200)
# In[10]:
if con.print:
#O
'''
'''
plt.figure()
plt.plot(n_o1_msis, z_msis/1e3, 'x', label = 'n_o1_msis')
plt.plot(np.exp(interp1d(z_msis, np.log(n_o1_msis), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'exp(int(log))')
#plt.plot(CubicSpline(z_msis, n_o1_msis)(z_model), z_model/1e3, label = 'cs')
plt.plot(np.exp(CubicSpline(z_msis, np.log(n_o1_msis))(z_model)), z_model/1e3, label = 'exp(cs(log))')
plt.plot(np.exp(PchipInterpolator(z_msis, np.log(n_o1_msis))(z_model)), z_model/1e3, label = 'pchip')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O')
plt.legend()
plt.ylim(70, 200)
# In[11]:
'''
plt.figure()
plt.plot(ne_iri, z_iri/1e3, label = 'ne_iri')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e')
plt.legend()
plt.figure()
plt.plot(ne_iri, z_iri/1e3, label = 'ne_iri')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e')
plt.legend()
plt.figure()
plt.plot(ne_iri, z_iri/1e3, label = 'ne_iri')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e')
plt.legend()
plt.figure()
plt.plot(ne_iri, z_iri/1e3, label = 'ne_iri')
plt.plot(np.exp(interp1d(z_iri[1:], np.log(ne_iri[1:]), fill_value = 'extrapolate')(z_model)), z_model/1e3, label = 'int_exp(lin(log))')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of e')
plt.legend()
plt.figure()
plt.plot(n_o1_msis, z_msis/1e3, label = 'MSIS data', marker = 'x')
plt.plot(np.exp(CubicSpline(z_msis, np.log(n_o1_msis))(z_model)), z_model/1e3, label = 'interp')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O &' + all_species[1].name)
plt.legend()
print(all_species[2].name)
all_species[2].density = all_species[0].density*interp1d(z_iri, rel_o_p, fill_value='extrapolate')(z_model)
if con.print:
plt.figure()
#plt.plot(alt_dens, z_model/1e3, label = 'interp_old')
plt.plot(rel_o_p, z_iri/1e3, label = 'IRI data', marker = 'x')
plt.plot(all_species[2].density/all_species[0].density, z_model/1e3, label = 'interp')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O+')
plt.legend()
print(all_species[3].name)
all_species[3].density = CubicSpline(z_msis, n_o2_msis)(z_model)
if con.print:
plt.figure()
#plt.plot(alt_dens, z_model/1e3, label = 'interp_old')
plt.plot(n_o2_msis, z_msis/1e3, label = 'MSIS data', marker = 'x')
plt.plot(all_species[3].density, z_model/1e3, label = 'interp')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O2')
plt.legend()
print(all_species[4].name)
#all_species[4].density = all_species[0].density*CubicSpline(z_iri, rel_o2_p)(z_model)
all_species[4].density = all_species[0].density*np.exp(interp1d(z_iri, np.log(rel_o2_p), fill_value='extrapolate')(z_model))
if con.print:
plt.figure()
#plt.plot(alt_dens, z_model/1e3, label = 'interp_old')
plt.plot(rel_o2_p, z_iri/1e3, label = 'IRI data', marker = 'x')
plt.plot(all_species[4].density/all_species[0].density, z_model/1e3, label = 'interp')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of O2+')
plt.legend()
print(all_species[5].name)
all_species[5].density = all_species[0].density*0
print(all_species[6].name)
all_species[6].density = all_species[0].density*0
print(all_species[7].name)
all_species[7].density = CubicSpline(z_msis, n_n2_msis)(z_model)
if con.print:
plt.figure()
#plt.plot(alt_dens, z_model/1e3, label = 'interp_old')
plt.plot(n_n2_msis, z_msis/1e3, label = 'MSIS data', marker = 'x')
plt.plot(all_species[7].density, z_model/1e3, label = 'interp')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of N2')
plt.legend()
print(all_species[8].name)
all_species[8].density = all_species[0].density*0
print(all_species[9].name)
all_species[9].density = all_species[0].density*0
print(all_species[10].name)
#all_species[10].density = all_species[0].density*CubicSpline(z_iri, rel_no_p)(z_model)
all_species[10].density = all_species[0].density*np.exp(interp1d(z_iri, np.log(rel_no_p), fill_value='extrapolate')(z_model))
if con.print:
plt.figure()
#plt.plot(alt_dens, z_model/1e3, label = 'interp_old')
plt.plot(rel_no_p, z_iri/1e3, label = 'IRI data', marker = 'x')
plt.plot(all_species[10].density/all_species[0].density, z_model/1e3, label = 'interp')
plt.xscale('log')
plt.xlabel('Density [m-3]')
plt.ylabel('Altitude [km]')
plt.title('Density profiles of NO+')
plt.legend()
'''
'''
for start, only O2+ and NO+ are looked at. => this expanded a bit (e, O, O+, O2, O2+, N, N+, N2, N2+, NO, NO+)
dn(O2+)/dt = q(O2+) - l(O2+)
with q - production and l - losses
and the same for NO+.
q => ionizing radiation (see Rees p.44) + charge exchange in chemical reactions
l => chemical reactions (charge exchange (i.e. loss in one ion species, prod in other) and recombination (pure loss of ions, prod of uncharged))
Reactions:
NO+ + e- => N + O with reaction rate NO+a1 alpha1 = 2.1e-13(T_e/300K)**(-0.85) m**3/s
O2+ + e- => O + O with O2+a2 alpha2 = 1.9e-13(T_e/300K)**(-0.5) m**3/s
O2+ + NO => NO+ + O2 with O2+k3 = 4.4e-16
O2+ + N2 => NO+ + NO with O2+k4 = 5e-22
...
see text file Reaction rates.txt
Loss:
l_i = sum(k_j,i * n_j) *n_i
with k_j,i, n_j the reaction rates with species j and its density
Production:
- Ionizing radiation:
(see P.44 in Rees)
q(N2+) = q_e * 0.92n_N2 / (0.92n_N2 + n_O2 + 0.56n_O)
q(O2+) = q_e * n_O2 / (0.92n_N2 + n_O2 + 0.56n_O)
q(O+) = q_e * 0.56n_0 / (0.92n_N2 + n_O2 + 0.56n_O)
- Charge exchange
same as loss, but the rate is dependent on the educts:
q_i = sum(k_j,m * n_j * n_m)
with j, m denoting the educt species
- Photoionization: (not relevant!!)
O2 + hv => O2+ + e-
NO + hv => NO+ + e-
q_ji(z,λ,l) = n_j(z)I(z,λ)σ_ji(λ)p_j(λ,l)dλ
with n_j - number density of species j
sigma_j - cross section of species j
p_j(l) - branching ratio into excited state l
=> sum of production NO+ and O2+ must equal production of e+
=> need a model for T_e!! => from measurements
'''