-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_SIPs.py
More file actions
2215 lines (1974 loc) · 86.9 KB
/
Copy pathinit_SIPs.py
File metadata and controls
2215 lines (1974 loc) · 86.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 3 18:33:04 2019
@author: jdesk
"""
#%% IMPORTS
import os
import math
import numpy as np
from numba import njit
import matplotlib.pyplot as plt
import constants as c
# from microphysics import compute_mass_from_radius
from microphysics import compute_radius_from_mass_vec
from microphysics import compute_radius_from_mass_jit
from microphysics import compute_mass_from_radius_vec
from microphysics import compute_mass_from_radius_jit
#%% FUNCTION DEFS
#%% DISTRIBUTIONS
# exponential distribution
# f_m(m) = number concentration density per mass
# such that int f_m(m) dm = DNC = droplet number concentration (1/m^3)
# f_m(m) = 1/LWC * exp(-m/m_avg)
# LWC = liquid water content (kg/m^3)
# m_avg = M/N = LWC/DNC
# where M = total droplet mass in dV, N = tot. # of droplets in dV
# in this function f_m(m) = conc_per_mass(m, LWC_inv, DNC_over_LWC)
# DNC_over_LWC = 1/m_avg
# m in kg
# function moments checked versus analytical values via numerical integration
def conc_per_mass_expo_np(m, DNC, DNC_over_LWC): # = f_m(m)
return DNC * DNC_over_LWC * np.exp(-DNC_over_LWC * m)
conc_per_mass_expo = njit()(conc_per_mass_expo_np)
# f_m(m) = number concentration density per mass
# lognormal distribution
# int dist(m) dm = DNC
# m = mass in kg
# mu = geometric expect.value of the PDF = mode
# sigma = geometric standard dev. of the PDF
# PDF for DNC = 1.0 tested: numeric integral = 1.0
two_pi_sqrt = np.sqrt(2.0*np.pi)
def conc_per_mass_lognormal_np(x, DNC, mu_log, sigma_log):
return DNC * np.exp( -0.5*( ( np.log( x ) - mu_log ) / sigma_log )**2 ) \
/ (x * two_pi_sqrt * sigma_log)
conc_per_mass_lognormal = njit()(conc_per_mass_lognormal_np)
def num_int_lognormal_np(x0, x1, par, steps=1E6):
dx = (x1 - x0) / steps
x = x0
intl = 0.0
f1 = conc_per_mass_lognormal(x,par[0],par[1],par[2])
# cnt = 0
while (x < x1):
f2 = conc_per_mass_lognormal(x + 0.5*dx, par[0],par[1],par[2])
f3 = conc_per_mass_lognormal(x + dx, par[0],par[1],par[2])
# intl_bef = intl
intl += 0.1666666666667 * dx * (f1 + 4.0 * f2 + f3)
x += dx
f1 = f3
# cnt += 1
# intl += dx * x * dst_expo(x,k)
# x += dx
# cnt += 1
return intl
num_int_lognormal = njit()(num_int_lognormal_np)
def num_int_lognormal_mean_np(x0, x1, par, steps=1E6):
dx = (x1 - x0) / steps
x = x0
intl = 0.0
f1 = conc_per_mass_lognormal(x,par[0],par[1],par[2]) * x
# cnt = 0
while (x < x1):
f2 = conc_per_mass_lognormal(x + 0.5*dx, par[0],par[1],par[2]) \
* (x + 0.5*dx)
f3 = conc_per_mass_lognormal(x + dx, par[0],par[1],par[2]) \
* (x + dx)
# intl_bef = intl
intl += 0.1666666666667 * dx * (f1 + 4.0 * f2 + f3)
x += dx
f1 = f3
# cnt += 1
# intl += dx * x * dst_expo(x,k)
# x += dx
# cnt += 1
return intl
num_int_lognormal_mean = njit()(num_int_lognormal_mean_np)
# x0 and x1 in microns
def num_int_lognormal_mean_mass_R_np(x0, x1, par, steps=1E6):
dx = (x1 - x0) / steps
x = x0
intl = 0.0
f1 = conc_per_mass_lognormal(x,par[0],par[1],par[2]) \
* 1.0E-18*compute_mass_from_radius_jit(x, par[3])
# cnt = 0
while (x < x1):
f2 = conc_per_mass_lognormal(x + 0.5*dx, par[0],par[1],par[2]) \
* 1.0E-18*compute_mass_from_radius_jit(x+0.5*dx, par[3])
f3 = conc_per_mass_lognormal(x + dx, par[0],par[1],par[2]) \
* 1.0E-18*compute_mass_from_radius_jit(x+dx, par[3])
# intl_bef = intl
intl += 0.1666666666667 * dx * (f1 + 4.0 * f2 + f3)
x += dx
f1 = f3
# cnt += 1
# intl += dx * x * dst_expo(x,k)
# x += dx
# cnt += 1
return intl
num_int_lognormal_mean_mass_R = njit()(num_int_lognormal_mean_mass_R_np)
##############################################################################
def moments_analytical_expo(n, DNC, DNC_over_LWC):
if n == 0:
return DNC
else:
LWC_over_DNC = 1.0 / DNC_over_LWC
return math.factorial(n) * DNC * LWC_over_DNC**n
def moments_analytical_lognormal_m(n, DNC, mu_m_log, sigma_m_log):
if n == 0:
return DNC
else:
return DNC * np.exp(n * mu_m_log + 0.5 * n*n * sigma_m_log*sigma_m_log)
def moments_analytical_lognormal_R(n, DNC, mu_R_log, sigma_R_log):
if n == 0:
return DNC
else:
return DNC * np.exp(n * mu_R_log + 0.5 * n*n * sigma_R_log*sigma_R_log)
# nth moment of f_m(m) -> mom_n = int(dm * m^k * f_m(m))
# function checked versus analytical values via numerical integration
def moments_f_m_num_expo_np(n, DNC, DNC_over_LWC, steps=1E6):
m_avg = 1.0/DNC_over_LWC
# m_high = m_avg * steps**0.7
m_high = m_avg * 1.0E4
dm = m_high / steps
m = 0.0
intl = 0.0
# cnt = 0
if n == 0:
f1 = conc_per_mass_expo(m, DNC, DNC_over_LWC)
while (m < m_high):
f2 = conc_per_mass_expo(m + 0.5*dm, DNC, DNC_over_LWC)
f3 = conc_per_mass_expo(m + dm, DNC, DNC_over_LWC)
# intl_bef = intl
intl += 0.1666666666667 * dm * (f1 + 4.0 * f2 + f3)
m += dm
f1 = f3
# cnt += 1
# intl += dx * x * dst_expo(x,k)
# x += dx
# cnt += 1
else:
f1 = conc_per_mass_expo(m, DNC, DNC_over_LWC) * m**n
while (m < m_high):
f2 = conc_per_mass_expo(m + 0.5*dm, DNC, DNC_over_LWC) * (m + 0.5*dm)**n
f3 = conc_per_mass_expo(m + dm, DNC, DNC_over_LWC) * (m + dm)**n
# intl_bef = intl
intl += 0.1666666666667 * dm * (f1 + 4.0 * f2 + f3)
m += dm
f1 = f3
# cnt += 1
# intl += dx * x * dst_expo(x,k)
# x += dx
# cnt += 1
return intl
moments_f_m_num_expo = njit()(moments_f_m_num_expo_np)
#%% GENERATION OF SIP ENSEMBLES
### SingleSIP probabilistic
# r_critmin => m_low = m_0
# m_{l+1} = m_l * 10^(1/kappa)
# dm_l = m_{l+1} - m_l
# mu_l = m_l + rnd() * dm_l
# xi_l = f_m(mu_l) * dm_l * dV
def generate_SIP_ensemble_SingleSIP_Unt_expo_np(
DNC0, DNC0_over_LWC0,
mass_density,
dV, kappa, eta, weak_threshold, r_critmin,
m_high_over_m_low=1.0E6,
seed=3711, setseed=True):
if setseed: np.random.seed(seed)
m_low = 1.0E-18 * compute_mass_from_radius_jit(r_critmin,
mass_density)
bin_factor = 10**(1.0/kappa)
m_high = m_low * m_high_over_m_low
m_left = m_low
l_max = int(kappa * np.log10(m_high_over_m_low))
rnd = np.random.rand( l_max )
if weak_threshold:
rnd2 = np.random.rand( l_max )
xis = np.zeros(l_max, dtype = np.float64)
masses = np.zeros(l_max, dtype = np.float64)
bins = np.zeros(l_max+1, dtype = np.float64)
bins[0] = m_left
bin_n = 0
while m_left < m_high:
m_right = m_left * bin_factor
bin_width = m_right - m_left
mu = m_left + rnd[bin_n] * bin_width
xi = conc_per_mass_expo(mu, DNC0, DNC0_over_LWC0) * bin_width * dV
xis[bin_n] = xi
masses[bin_n] = mu
m_left = m_right
bin_n += 1
bins[bin_n] = m_left
xi_max = xis.max()
xi_critmin = xi_max * eta
valid_ids = np.ones(l_max, dtype = np.int64)
for bin_n in range(l_max):
if xis[bin_n] < xi_critmin:
if weak_threshold:
if rnd2[bin_n] < xis[bin_n] / xi_critmin:
xis[bin_n] = xi_critmin
else: valid_ids[bin_n] = 0
else: valid_ids[bin_n] = 0
xis = xis[np.nonzero(valid_ids)[0]]
masses = masses[np.nonzero(valid_ids)[0]]
return masses, xis, m_low, bins
generate_SIP_ensemble_SingleSIP_Unt_expo =\
njit()(generate_SIP_ensemble_SingleSIP_Unt_expo_np)
# r_critmin -> m_low = m_0
# m_{l+1} = m_l * 10^(1/kappa)
# dm_l = m_{l+1} - m_l
# mu_l = m_l + rnd() * dm_l
# xi_l = f_m(mu_l) * dm_l * dV
def generate_SIP_ensemble_SingleSIP_Unt_lognormal_np(
DNC0, mu_m_log, sigma_m_log,
mass_density,
dV, kappa, eta, weak_threshold, r_critmin,
m_high_over_m_low=1.0E6, seed=3711, setseed=True):
if setseed: np.random.seed(seed)
m_low = 1.0E-18 * compute_mass_from_radius_jit(r_critmin, mass_density)
bin_factor = 10**(1.0/kappa)
m_high = m_low * m_high_over_m_low
m_left = m_low
l_max = int(kappa * np.log10(m_high_over_m_low))
rnd = np.random.rand( l_max )
if weak_threshold:
rnd2 = np.random.rand( l_max )
xis = np.zeros(l_max, dtype = np.float64)
masses = np.zeros(l_max, dtype = np.float64)
bins = np.zeros(l_max+1, dtype = np.float64)
bins[0] = m_left
bin_n = 0
while m_left < m_high:
m_right = m_left * bin_factor
bin_width = m_right - m_left
mu = m_left + rnd[bin_n] * bin_width
xi = conc_per_mass_lognormal(mu, DNC0, mu_m_log, sigma_m_log) \
* bin_width * dV
xis[bin_n] = xi
masses[bin_n] = mu
m_left = m_right
bin_n += 1
bins[bin_n] = m_left
xi_max = xis.max()
xi_critmin = xi_max * eta
valid_ids = np.ones(l_max, dtype = np.int64)
for bin_n in range(l_max):
if xis[bin_n] < xi_critmin:
if weak_threshold:
if rnd2[bin_n] < xis[bin_n] / xi_critmin:
xis[bin_n] = xi_critmin
else: valid_ids[bin_n] = 0
else: valid_ids[bin_n] = 0
xis = xis[np.nonzero(valid_ids)[0]]
masses = masses[np.nonzero(valid_ids)[0]]
return masses, xis, m_low, bins
generate_SIP_ensemble_SingleSIP_Unt_lognormal =\
njit()(generate_SIP_ensemble_SingleSIP_Unt_lognormal_np)
### GENERATE AND SAVE SIP ENSEMBLES SINGLE SIP UNTERSTRASSER
# r_critmin in mu
def generate_and_save_SIP_ensembles_SingleSIP_prob(
dist, dist_par, mass_density, dV, kappa, eta, weak_threshold, r_critmin,
m_high_over_m_low, no_sims, start_seed, ensemble_dir):
if dist == "expo":
generate_SIP_ensemble_SingleSIP_Unt = \
generate_SIP_ensemble_SingleSIP_Unt_expo
DNC0 = dist_par[0]
DNC0_over_LWC0 = dist_par[1]
ensemble_parameters = [dV, DNC0, DNC0_over_LWC0, r_critmin,
kappa, eta, no_sims, start_seed]
elif dist == "lognormal":
generate_SIP_ensemble_SingleSIP_Unt = \
generate_SIP_ensemble_SingleSIP_Unt_lognormal
DNC0 = dist_par[0]
mu_m_log = dist_par[1]
sigma_m_log = dist_par[2]
# mass_density = dist_par[3]
ensemble_parameters = [dV, DNC0, mu_m_log, sigma_m_log, mass_density,
r_critmin, kappa, eta, no_sims, start_seed]
m_low = 1.0E-18 * compute_mass_from_radius_jit(r_critmin, mass_density)
seed_list = np.arange(start_seed, start_seed+no_sims*2, 2)
if not os.path.exists(ensemble_dir):
os.makedirs(ensemble_dir)
for i,seed in enumerate(seed_list):
masses, xis, m_low, bins =\
generate_SIP_ensemble_SingleSIP_Unt(
*dist_par, mass_density, dV, kappa, eta, weak_threshold,
r_critmin, m_high_over_m_low, seed)
bins_rad = compute_radius_from_mass_vec(1.0E18*bins, mass_density)
radii = compute_radius_from_mass_vec(1.0E18*masses, mass_density)
# bins_rad = compute_radius_from_mass(1.0E18*bins,
# mass_density)
# radii = compute_radius_from_mass(1.0E18*masses,
# mass_density)
np.save(ensemble_dir + f"masses_seed_{seed}", masses)
np.save(ensemble_dir + f"radii_seed_{seed}", radii)
np.save(ensemble_dir + f"xis_seed_{seed}", xis)
if i == 0:
np.save(ensemble_dir + f"bins_mass", bins)
np.save(ensemble_dir + f"bins_rad", bins_rad)
np.save(ensemble_dir + "ensemble_parameters", ensemble_parameters)
### ANALYZE EXPO SIP ENSEMBLE DATA FROM DATA STORED IN FILES
# masses is a list of [masses0, masses1, ..., masses_no_sims]
# where masses[i] = array of masses of a spec. SIP ensemble
# use moments_an[1] for LWC0
def generate_myHisto_SIP_ensemble_np(masses, xis, m_min, m_max,
dV, DNC0, LWC0,
no_bins, no_sims,
bin_mode, spread_mode,
shift_factor, overflow_factor,
scale_factor):
# g_m_num = []
# g_ln_r_num = []
if bin_mode == 1:
bin_factor = (m_max/m_min)**(1.0/no_bins)
bin_log_dist = np.log(bin_factor)
# bin_log_dist_half = 0.5 * bin_log_dist
# add dummy bins for overflow
# bins_mass = np.zeros(no_bins+3,dtype=np.float64)
bins_mass = np.zeros(no_bins+1,dtype=np.float64)
bins_mass[0] = m_min
# bins_mass[0] = m_min / bin_factor
for bin_n in range(1,no_bins+1):
bins_mass[bin_n] = bins_mass[bin_n-1] * bin_factor
# the factor 1.01 is for numerical stability: to be sure
# that m_max does not contribute to a bin larger than the
# last bin
bins_mass[-1] *= 1.0001
# the factor 0.99 is for numerical stability: to be sure
# that m_min does not contribute to a bin smaller than the
# 0-th bin
bins_mass[0] *= 0.9999
# m_0 = m_min / np.sqrt(bin_factor)
bins_mass_log = np.log(bins_mass)
bins_mass_width = np.zeros(no_bins+2,dtype=np.float64)
bins_mass_width[1:-1] = bins_mass[1:]-bins_mass[:-1]
# modify for overflow bins
bins_mass_width[0] = bins_mass_width[1]
bins_mass_width[-1] = bins_mass_width[-2]
dm0 = 0.5*bins_mass_width[0]
dmN = 0.5*bins_mass_width[-1]
# dm0 = 0.5*(bins_mass[0] - bins_mass[0] / bin_factor)
# dmN = 0.5*(bins_mass[-1] * bin_factor - bins_mass[-1])
f_m_num = np.zeros( (no_sims,no_bins+2), dtype=np.float64 )
g_m_num = np.zeros( (no_sims,no_bins), dtype=np.float64 )
h_m_num = np.zeros( (no_sims,no_bins), dtype=np.float64 )
for i,mass in enumerate(masses):
histo = np.zeros(no_bins+2, dtype=np.float64)
histo_g = np.zeros(no_bins+2, dtype=np.float64)
histo_h = np.zeros(no_bins+2, dtype=np.float64)
mass_log = np.log(mass)
for n,m_ in enumerate(mass):
xi = xis[i][n]
bin_n = np.nonzero(np.histogram(m_, bins=bins_mass)[0])[0][0]
# print(bin_n)
# smear functions depending on weight of data point in the bin
# on a lin base
if spread_mode == 0:
# norm_dist = (mass[n] - bins_mass[bin_n])/bins_mass_width[bin_n]
# NEW: start from right side
norm_dist = (bins_mass[bin_n+1] - mass[n]) \
/ bins_mass_width[bin_n]
# on a log base
elif spread_mode == 1:
# norm_dist = (mass_log[n] - bins_mass_log[bin_n])/bin_log_dist
norm_dist = (bins_mass_log[bin_n] - mass_log[n])/bin_log_dist
if norm_dist < 0.5:
s = 0.5 + norm_dist
# +1 because we have overflow bins left and right in "histo"-array
bin_n += 1
# print(n,s,"right")
histo[bin_n+1] += (1.0-s)*xi
histo_g[bin_n+1] += (1.0-s)*xi*m_
histo_h[bin_n+1] += (1.0-s)*xi*m_*m_
# if in last bin: no outflow,
# just EXTRAPOLATION to overflow bin!
if bin_n == no_bins:
histo[bin_n] += xi
histo_g[bin_n] += xi*m_
histo_h[bin_n] += xi*m_*m_
else:
histo[bin_n] += s*xi
histo_g[bin_n] += s*xi*m_
histo_h[bin_n] += s*xi*m_*m_
elif spread_mode == 0:
# now left side of bin
norm_dist = (mass[n] - bins_mass[bin_n]) \
/ bins_mass_width[bin_n-1]
# +1 because we have overflow bins left and right in "histo"-array
bin_n += 1
# print(n,norm_dist, "left")
if norm_dist < 0.5:
s = 0.5 + norm_dist
# print(n,s,"left")
histo[bin_n-1] += (1.0-s)*xi
histo_g[bin_n-1] += (1.0-s)*xi*m_
histo_h[bin_n-1] += (1.0-s)*xi*m_*m_
# if in first bin: no outflow,
# just EXTRAPOLATION to overflow bin!
if bin_n == 1:
histo[bin_n] += xi
histo_g[bin_n] += xi*m_
histo_h[bin_n] += xi*m_*m_
else:
histo[bin_n] += s*xi
histo_g[bin_n] += s*xi*m_
histo_h[bin_n] += s*xi*m_*m_
else:
histo[bin_n] += xi
histo_g[bin_n] += xi*m_
histo_h[bin_n] += xi*m_*m_
elif spread_mode == 1:
# +1 because we have overflow bins left and right in "histo"-array
bin_n += 1
s = 1.5 - norm_dist
histo[bin_n] += s*xi
histo[bin_n-1] += (1.0-s)*xi
histo_g[bin_n] += s*xi*m_
histo_g[bin_n-1] += (1.0-s)*xi*m_
histo_h[bin_n] += s*xi*m_*m_
histo_h[bin_n-1] += (1.0-s)*xi*m_*m_
# on a log base
# log_dist = mass_log[n] - bins_mass_log[bin_n]
# if log_dist < bin_log_dist_half:
# s = 0.5 + log_dist/bin_log_dist
# # print(n,s,"left")
# histo[bin_n] += s*xi
# histo[bin_n-1] += (1.0-s)*xi
# histo_g[bin_n] += s*xi*m_
# histo_g[bin_n-1] += (1.0-s)*xi*m_
# else:
# s = 1.5 - log_dist/bin_log_dist
# # print(n,s,"right")
# histo[bin_n] += s*xi
# histo[bin_n+1] += (1.0-s)*xi
# histo_g[bin_n] += s*xi*m_
# histo_g[bin_n+1] += (1.0-s)*xi*m_
f_m_num[i,1:-1] = histo[1:-1] / (bins_mass_width[1:-1] * dV)
# multiply the overflow-bins by factor to get an estimation of
# f_m at the position m_0 - dm0/2
# f_m at the position m_no_bins + dmN/2, where
# dm0 = 0.5*(bins_mass[0] - bins_mass[0] / bin_factor)
# dmN = 0.5*(bins_mass[-1] * bin_factor - bins_mass[-1])
f_m_num[i,0] = overflow_factor * histo[0] / (dm0 * dV)
f_m_num[i,-1] = overflow_factor * histo[-1] / (dmN * dV)
g_m_num[i] = histo_g[1:-1] / (bins_mass_width[1:-1] * dV)
h_m_num[i] = histo_h[1:-1] / (bins_mass_width[1:-1] * dV)
f_m_num_avg = np.average(f_m_num, axis=0)
f_m_num_std = np.std(f_m_num, axis=0, ddof=1) / np.sqrt(no_sims)
g_m_num_avg = np.average(g_m_num, axis=0)
g_m_num_std = np.std(g_m_num, axis=0, ddof=1) / np.sqrt(no_sims)
h_m_num_avg = np.average(h_m_num, axis=0)
h_m_num_std = np.std(h_m_num, axis=0, ddof=1) / np.sqrt(no_sims)
# define centers on lin scale
bins_mass_center_lin = np.zeros(no_bins+2, dtype=np.float64)
bins_mass_center_lin[1:-1] = 0.5 * (bins_mass[:-1] + bins_mass[1:])
# add dummy bin centers for quadratic approx
bins_mass_center_lin[0] = bins_mass[0] - 0.5*dm0
bins_mass_center_lin[-1] = bins_mass[-1] + 0.5*dmN
# define centers on the logarithmic scale
bins_mass_center_log = bins_mass[:-1] * np.sqrt(bin_factor)
# define the center of mass for each bin and set it as the "bin center"
bins_mass_center_COM = g_m_num_avg / f_m_num_avg[1:-1]
# def as 2nd moment/1st moment
bins_mass_center_h_g = h_m_num_avg / g_m_num_avg
### LINEAR APPROX OF f_m
# to get an idea of the shape
# for bin n take f[n-1], f[n], f[n+1]
# make linear approx from n-1 to n and from n to n+1
# to get idea of shape of function
# lin fct: f = a0 + a1*m
# a1 = (f[n+1]-f[n])/(m[n+1] - m[n])
# a0 = f[n] - a1*m[n]
# bins_mass_centers_lin_fit = np.zeros(no_bins, dtype = np.float64)
lin_par0 = np.zeros(no_bins+1, dtype = np.float64)
lin_par1 = np.zeros(no_bins+1, dtype = np.float64)
lin_par1 = (f_m_num_avg[1:] - f_m_num_avg[:-1]) \
/ (bins_mass_center_lin[1:] - bins_mass_center_lin[:-1])
lin_par0 = f_m_num_avg[:-1] - lin_par1 * bins_mass_center_lin[:-1]
f_bin_border = lin_par0 + lin_par1 * bins_mass
# f_bin_border_delta_left = np.zeros(no_bins+1, dtype = np.float64)
# f_bin_border_delta_left = np.abs(f_m_num_avg[1:-1]-f_bin_border[:-1])
# f_bin_border_delta_right = np.abs(f_bin_border[1:] - f_m_num_avg[1:-1])
### FIRST CORRECTION:
# by my method of spreading over several bins the bins with higher f_avg
# "loose" counts to bins with smaller f_avg
# by a loss/gain analysis, one can estimate the lost counts
# using the linear approximation of f_m(m) calc. above
# delta of counts (estimated)
delta_N = np.zeros(no_bins, dtype=np.float64)
delta_N[1:-1] = 0.25 * bins_mass_width[1:-3] \
* ( f_m_num_avg[1:-3] - f_bin_border[1:-2] ) \
+ 0.25 * bins_mass_width[2:-2] \
* ( -f_m_num_avg[2:-2] + f_bin_border[2:-1] ) \
+ 0.083333333 \
* ( lin_par1[1:-2] * bins_mass_width[1:-3]**2
- lin_par1[2:-1] * bins_mass_width[2:-2]**2)
# first bin: only exchange with the bin to the right
delta_N[0] = 0.25 * bins_mass_width[1] \
* ( -f_m_num_avg[1] + f_bin_border[1] ) \
- 0.083333333 \
* ( lin_par1[1] * bins_mass_width[1]**2 )
# last bin: only exchange with the bin to the left
# bin_n = no_bins-1
delta_N[no_bins-1] = 0.25 * bins_mass_width[no_bins-1] \
* (f_m_num_avg[no_bins-1] - f_bin_border[no_bins-1]) \
+ 0.083333333 \
* ( lin_par1[no_bins-1]
* bins_mass_width[no_bins-1]**2 )
scale = delta_N / (f_m_num_avg[1:-1] * bins_mass_width[1:-1])
scale = np.where(scale < -0.9,
-0.9,
scale)
scale *= scale_factor
print("scale")
print(scale)
f_m_num_avg[1:-1] = f_m_num_avg[1:-1] / (1.0 + scale)
f_m_num_avg[0] = f_m_num_avg[0] / (1.0 + scale[0])
f_m_num_avg[-1] = f_m_num_avg[-1] / (1.0 + scale[-1])
## REPEAT LIN APPROX AFTER FIRST CORRECTION
lin_par0 = np.zeros(no_bins+1, dtype = np.float64)
lin_par1 = np.zeros(no_bins+1, dtype = np.float64)
lin_par1 = (f_m_num_avg[1:] - f_m_num_avg[:-1]) \
/ (bins_mass_center_lin[1:] - bins_mass_center_lin[:-1])
lin_par0 = f_m_num_avg[:-1] - lin_par1 * bins_mass_center_lin[:-1]
f_bin_border = lin_par0 + lin_par1 * bins_mass
### SECOND CORRECTION:
# try to estimate the position of m in the bin where f(m) = f_avg (of bin)
# bin avg based on the linear approximations
# NOTE that this is just to get an idea of the function FORM
# f_bin_border_delta_left = np.zeros(no_bins+1, dtype = np.float64)
f_bin_border_delta_left = np.abs(f_m_num_avg[1:-1]-f_bin_border[:-1])
f_bin_border_delta_right = np.abs(f_bin_border[1:] - f_m_num_avg[1:-1])
bins_mass_centers_lin_fit = np.zeros(no_bins, dtype = np.float64)
f_avg2 = 0.25 * (f_bin_border[:-1] + f_bin_border[1:]) \
+ 0.5 * f_m_num_avg[1:-1]
for bin_n in range(no_bins):
if f_bin_border_delta_left[bin_n] >= f_bin_border_delta_right[bin_n]:
m_c = (f_avg2[bin_n] - lin_par0[bin_n]) / lin_par1[bin_n]
else:
m_c = (f_avg2[bin_n] - lin_par0[bin_n+1]) / lin_par1[bin_n+1]
# if f_bin_border_abs[bin_n] >= f_bin_border_abs[bin_n+1]:
# # take left side of current bin
# m_c = 0.5 * ( (bins_mass[bin_n] + 0.25*bins_mass_width[bin_n]) \
# + lin_par1[bin_n+1]/lin_par1[bin_n] \
# * (bins_mass[bin_n+1] - 0.25*bins_mass_width[bin_n]) \
# + (lin_par0[bin_n+1] - lin_par0[bin_n]))
# else:
# m_c = 0.5 * ( lin_par1[bin_n]/lin_par1[bin_n+1] \
# * (bins_mass[bin_n] + 0.25*bins_mass_width[bin_n]) \
# + (bins_mass[bin_n+1] - 0.25*bins_mass_width[bin_n])\
# + (lin_par0[bin_n] - lin_par0[bin_n+1]) )
# add additional shift because of two effects:
# 1) adding xi-"mass" to bins with smaller f_avg
# 2) wrong setting of "center" if f_avg[n] > f_avg[n+1]
m_c = shift_factor * m_c \
+ bins_mass_center_lin[bin_n+1] * (1.0 - shift_factor)
if m_c < bins_mass[bin_n]:
m_c = bins_mass[bin_n]
elif m_c > bins_mass[bin_n+1]:
m_c = bins_mass[bin_n+1]
bins_mass_centers_lin_fit[bin_n] = m_c
# shift more to center: -> is covered by shift_factor=0.5
# bins_mass_centers_lin_fit[bin_n] = \
# 0.5 * (m_c + bins_mass_center_lin[bin_n+1])
### bin mass center quad approx: -->>> BIG ISSUES: no monoton. interpol.
# possible for three given points with quadr. fct.
# for every bin:
# assume that the coordinate pairs are right with
# (m_center_lin, f_avg)
# approximate the function f_m(m) locally with a parabola to get
# an estimate of the form of the function
# assume this parabola in the bin and calculate bin_center_exact
D_10 = bins_mass_center_lin[1:-1] - bins_mass_center_lin[0:-2]
D_20 = bins_mass_center_lin[2:] - bins_mass_center_lin[0:-2]
D_21 = bins_mass_center_lin[2:] - bins_mass_center_lin[1:-1]
CD_10 = (bins_mass_center_lin[1:-1] + bins_mass_center_lin[0:-2])*D_10
CD_20 = (bins_mass_center_lin[2:] + bins_mass_center_lin[0:-2])*D_20
CD_21 = (bins_mass_center_lin[2:] + bins_mass_center_lin[1:-1])*D_21
a2 = f_m_num_avg[2:]/(D_21*D_20) - f_m_num_avg[1:-1]/(D_21*D_10) \
+ f_m_num_avg[:-2]/(D_10*D_20)
a1_a2 = (-f_m_num_avg[0:-2]*CD_21 + f_m_num_avg[1:-1]*CD_20
- f_m_num_avg[2:]*CD_10 ) \
/ (f_m_num_avg[0:-2]*D_21 - f_m_num_avg[1:-1]*D_20
+ f_m_num_avg[2:]*D_10 )
a1 = a2 * a1_a2
a0 = f_m_num_avg[1:-1] - a1*bins_mass_center_lin[1:-1] \
- a2*bins_mass_center_lin[1:-1]**2
bins_mass_sq = bins_mass*bins_mass
bins_mass_centers_qfit =\
-0.5*a1_a2 \
+ np.sqrt( 0.25*(a1_a2)**2
+ 0.5*a1_a2 * (bins_mass[:-1] + bins_mass[1:])
+ 0.33333333 * (bins_mass_sq[:-1]
+ bins_mass[:-1]*bins_mass[1:]
+ bins_mass_sq[1:]) )
bins_mass_center_lin2 = bins_mass_center_lin[1:-1]
bins_mass_width = bins_mass_width[1:-1]
# set the bin "mass centers" at the right spot for exponential dist
# such that f_avg_i in bin in = f(mm_i), where mm_i is the "mass center"
# use moments_an[1] for LWC0 if not given (e.g. for lognormal distr.)
m_avg = LWC0 / DNC0
bins_mass_center_exact = bins_mass[:-1]\
+ m_avg * np.log(bins_mass_width\
/ (m_avg * (1-np.exp(-bins_mass_width/m_avg))))
bins_mass_centers = np.array((bins_mass_center_lin2,
bins_mass_center_log,
bins_mass_center_COM,
bins_mass_center_exact,
bins_mass_centers_lin_fit,
bins_mass_centers_qfit,
bins_mass_center_h_g))
return f_m_num_avg, f_m_num_std, g_m_num_avg, g_m_num_std,\
h_m_num_avg, h_m_num_std, \
bins_mass, bins_mass_width, \
bins_mass_centers, bins_mass_center_lin, \
np.array((lin_par0,lin_par1)), np.array((a0,a1,a2))
# generate_myHisto_SIP_ensemble = njit()(generate_myHisto_SIP_ensemble_np)
def analyze_ensemble_data(dist, mass_density, kappa, no_sims, ensemble_dir,
no_bins, bin_mode,
spread_mode, shift_factor, overflow_factor,
scale_factor, act_plot_ensembles):
if dist == "expo":
conc_per_mass_np = conc_per_mass_expo_np
dV, DNC0, DNC0_over_LWC0, r_critmin, kappa, eta, no_sims00, start_seed = \
tuple(np.load(ensemble_dir + "ensemble_parameters.npy"))
LWC0_over_DNC0 = 1.0 / DNC0_over_LWC0
dist_par = (DNC0, DNC0_over_LWC0)
moments_analytical = moments_analytical_expo
elif dist =="lognormal":
conc_per_mass_np = conc_per_mass_lognormal_np
dV, DNC0, mu_m_log, sigma_m_log, mass_density, r_critmin, \
kappa, eta, no_sims00, start_seed = \
tuple(np.load(ensemble_dir + "ensemble_parameters.npy"))
dist_par = (DNC0, mu_m_log, sigma_m_log)
moments_analytical = moments_analytical_lognormal_m
start_seed = int(start_seed)
no_sims00 = int(no_sims00)
# kappa = int(kappa)
seed_list = np.arange(start_seed, start_seed+no_sims*2, 2)
### ANALYSIS START
masses = []
xis = []
radii = []
moments_sampled = []
for i,seed in enumerate(seed_list):
masses.append(np.load(ensemble_dir + f"masses_seed_{seed}.npy"))
xis.append(np.load(ensemble_dir + f"xis_seed_{seed}.npy"))
radii.append(np.load(ensemble_dir + f"radii_seed_{seed}.npy"))
moments = np.zeros(4,dtype=np.float64)
moments[0] = xis[i].sum() / dV
for n in range(1,4):
moments[n] = np.sum(xis[i]*masses[i]**n) / dV
moments_sampled.append(moments)
masses_sampled = np.concatenate(masses)
radii_sampled = np.concatenate(radii)
xis_sampled = np.concatenate(xis)
# moments analysis
moments_sampled = np.transpose(moments_sampled)
moments_an = np.zeros(4,dtype=np.float64)
for n in range(4):
moments_an[n] = moments_analytical(n, *dist_par)
print(f"######## kappa {kappa} ########")
print("moments_an: ", moments_an)
for n in range(4):
print(n, (np.average(moments_sampled[n])-moments_an[n])/moments_an[n] )
moments_sampled_avg_norm = np.average(moments_sampled, axis=1) / moments_an
moments_sampled_std_norm = np.std(moments_sampled, axis=1) \
/ np.sqrt(no_sims) / moments_an
m_min = masses_sampled.min()
m_max = masses_sampled.max()
R_min = radii_sampled.min()
R_max = radii_sampled.max()
# if sample_mode == "given_bins":
bins_mass = np.load(ensemble_dir + "bins_mass.npy")
bins_rad = np.load(ensemble_dir + "bins_rad.npy")
bin_factor = 10**(1.0/kappa)
### build log bins "intuitively" = "auto"
# elif sample_mode == "auto_bins":
if bin_mode == 1:
bin_factor_auto = (m_max/m_min)**(1.0/no_bins)
# bin_log_dist = np.log(bin_factor)
# bin_log_dist_half = 0.5 * bin_log_dist
# add dummy bins for overflow
# bins_mass = np.zeros(no_bins+3,dtype=np.float64)
bins_mass_auto = np.zeros(no_bins+1,dtype=np.float64)
bins_mass_auto[0] = m_min
# bins_mass[0] = m_min / bin_factor
for bin_n in range(1,no_bins+1):
bins_mass_auto[bin_n] = bins_mass_auto[bin_n-1] * bin_factor_auto
# the factor 1.01 is for numerical stability: to be sure
# that m_max does not contribute to a bin larger than the
# last bin
bins_mass_auto[-1] *= 1.0001
# the factor 0.99 is for numerical stability: to be sure
# that m_min does not contribute to a bin smaller than the
# 0-th bin
bins_mass_auto[0] *= 0.9999
# m_0 = m_min / np.sqrt(bin_factor)
# bins_mass_log = np.log(bins_mass)
bins_rad_auto = compute_radius_from_mass_vec(bins_mass_auto*1.0E18, mass_density)
###################################################
### histogram generation for given bins
f_m_counts = np.histogram(masses_sampled,bins_mass)[0]
f_m_ind = np.nonzero(f_m_counts)[0]
f_m_ind = np.arange(f_m_ind[0],f_m_ind[-1]+1)
no_SIPs_avg = f_m_counts.sum()/no_sims
bins_mass_ind = np.append(f_m_ind, f_m_ind[-1]+1)
bins_mass = bins_mass[bins_mass_ind]
bins_rad = bins_rad[bins_mass_ind]
bins_rad_log = np.log(bins_rad)
bins_mass_width = (bins_mass[1:]-bins_mass[:-1])
bins_rad_width = (bins_rad[1:]-bins_rad[:-1])
bins_rad_width_log = (bins_rad_log[1:]-bins_rad_log[:-1])
### approximate the functions f_m, f_lnR = 3*m*f_m, g_lnR=3*m^2*f_m
# estimate f_m(m) by binning:
# DNC_i = f_m(m_i) * dm_i = droplet number conc in bin i with size dm_i
f_m_num_sampled = np.histogram(masses_sampled,bins_mass,
weights=xis_sampled)[0]
g_m_num_sampled = np.histogram(masses_sampled,bins_mass,
weights=xis_sampled*masses_sampled)[0]
f_m_num_sampled = f_m_num_sampled / (bins_mass_width * dV * no_sims)
g_m_num_sampled = g_m_num_sampled / (bins_mass_width * dV * no_sims)
# build g_ln_r = 3*m*g_m DIRECTLY from data
g_ln_r_num_sampled = np.histogram(radii_sampled,
bins_rad,
weights=xis_sampled*masses_sampled)[0]
g_ln_r_num_sampled = g_ln_r_num_sampled \
/ (bins_rad_width_log * dV * no_sims)
# g_ln_r_num_derived = 3 * bins_mass_center * g_m_num * 1000.0
# define centers on lin scale
bins_mass_center_lin = 0.5 * (bins_mass[:-1] + bins_mass[1:])
bins_rad_center_lin = 0.5 * (bins_rad[:-1] + bins_rad[1:])
# define centers on the logarithmic scale
bins_mass_center_log = bins_mass[:-1] * np.sqrt(bin_factor)
bins_rad_center_log = bins_rad[:-1] * np.sqrt(bin_factor)
# bins_mass_center_log = bins_mass[:-1] * 10**(1.0/(2.0*kappa))
# bins_rad_center_log = bins_rad[:-1] * 10**(1.0/(2.0*kappa))
# define the center of mass for each bin and set it as the "bin center"
bins_mass_center_COM = g_m_num_sampled/f_m_num_sampled
bins_rad_center_COM =\
compute_radius_from_mass_vec(bins_mass_center_COM*1.0E18, mass_density)
# set the bin "mass centers" at the right spot such that
# f_avg_i in bin in = f(mm_i), where mm_i is the "mass center"
if dist == "expo":
m_avg = LWC0_over_DNC0
elif dist == "lognormal":
m_avg = moments_an[1] / dist_par[0]
bins_mass_center_exact = bins_mass[:-1] \
+ m_avg * np.log(bins_mass_width\
/ (m_avg * (1-np.exp(-bins_mass_width/m_avg))))
bins_rad_center_exact =\
compute_radius_from_mass_vec(bins_mass_center_exact*1.0E18, mass_density)
bins_mass_centers = np.array((bins_mass_center_lin,
bins_mass_center_log,
bins_mass_center_COM,
bins_mass_center_exact))
bins_rad_centers = np.array((bins_rad_center_lin,
bins_rad_center_log,
bins_rad_center_COM,
bins_rad_center_exact))
###################################################
### histogram generation for auto bins
f_m_counts_auto = np.histogram(masses_sampled,bins_mass_auto)[0]
f_m_ind_auto = np.nonzero(f_m_counts_auto)[0]
f_m_ind_auto = np.arange(f_m_ind_auto[0],f_m_ind_auto[-1]+1)
# no_SIPs_avg_auto = f_m_counts_auto.sum()/no_sims
bins_mass_ind_auto = np.append(f_m_ind_auto, f_m_ind_auto[-1]+1)
bins_mass_auto = bins_mass_auto[bins_mass_ind_auto]
bins_rad_auto = bins_rad_auto[bins_mass_ind_auto]
bins_rad_log_auto = np.log(bins_rad_auto)
bins_mass_width_auto = (bins_mass_auto[1:]-bins_mass_auto[:-1])
# bins_rad_width_auto = (bins_rad_auto[1:]-bins_rad_auto[:-1])
bins_rad_width_log_auto = (bins_rad_log_auto[1:]-bins_rad_log_auto[:-1])
### approximate the functions f_m, f_lnR = 3*m*f_m, g_lnR=3*m^2*f_m
# estimate f_m(m) by binning:
# DNC_i = f_m(m_i) * dm_i = droplet number conc in bin i with size dm_i
f_m_num_sampled_auto = np.histogram(masses_sampled,bins_mass_auto,
weights=xis_sampled)[0]
g_m_num_sampled_auto = np.histogram(masses_sampled,bins_mass_auto,
weights=xis_sampled*masses_sampled)[0]
f_m_num_sampled_auto = f_m_num_sampled_auto / (bins_mass_width_auto * dV * no_sims)
g_m_num_sampled_auto = g_m_num_sampled_auto / (bins_mass_width_auto * dV * no_sims)
# build g_ln_r = 3*m*g_m DIRECTLY from data
g_ln_r_num_sampled_auto = np.histogram(radii_sampled,
bins_rad_auto,
weights=xis_sampled*masses_sampled)[0]
g_ln_r_num_sampled_auto = g_ln_r_num_sampled_auto \
/ (bins_rad_width_log_auto * dV * no_sims)
# g_ln_r_num_derived = 3 * bins_mass_center * g_m_num * 1000.0
# define centers on lin scale
bins_mass_center_lin_auto = 0.5 * (bins_mass_auto[:-1] + bins_mass_auto[1:])
bins_rad_center_lin_auto = 0.5 * (bins_rad_auto[:-1] + bins_rad_auto[1:])
# define centers on the logarithmic scale
bins_mass_center_log_auto = bins_mass_auto[:-1] * np.sqrt(bin_factor)
bins_rad_center_log_auto = bins_rad_auto[:-1] * np.sqrt(bin_factor)
# bins_mass_center_log = bins_mass[:-1] * 10**(1.0/(2.0*kappa))
# bins_rad_center_log = bins_rad[:-1] * 10**(1.0/(2.0*kappa))
# define the center of mass for each bin and set it as the "bin center"
bins_mass_center_COM_auto = g_m_num_sampled_auto/f_m_num_sampled_auto
bins_rad_center_COM_auto =\
compute_radius_from_mass_vec(bins_mass_center_COM_auto*1.0E18, mass_density)
# set the bin "mass centers" at the right spot such that
# f_avg_i in bin in = f(mm_i), where mm_i is the "mass center"
if dist == "expo":
m_avg = LWC0_over_DNC0
elif dist == "lognormal":
m_avg = moments_an[1] / dist_par[0]
bins_mass_center_exact_auto = bins_mass_auto[:-1] \
+ m_avg * np.log(bins_mass_width_auto\
/ (m_avg * (1-np.exp(-bins_mass_width_auto/m_avg))))
bins_rad_center_exact_auto =\
compute_radius_from_mass_vec(bins_mass_center_exact_auto*1.0E18,
mass_density)
bins_mass_centers_auto = np.array((bins_mass_center_lin_auto,
bins_mass_center_log_auto,
bins_mass_center_COM_auto,
bins_mass_center_exact_auto))
bins_rad_centers_auto = np.array((bins_rad_center_lin_auto,
bins_rad_center_log_auto,
bins_rad_center_COM_auto,
bins_rad_center_exact_auto))
###################################################
###################################################
### STATISTICAL ANALYSIS OVER no_sim runs given bins
# get f(m_i) curve for each "run" with same bins for all ensembles
f_m_num = []
g_m_num = []
g_ln_r_num = []
for i,mass in enumerate(masses):
f_m_num.append(np.histogram(mass,bins_mass,weights=xis[i])[0] \
/ (bins_mass_width * dV))
g_m_num.append(np.histogram(mass,bins_mass,
weights=xis[i]*mass)[0] \
/ (bins_mass_width * dV))
# build g_ln_r = 3*m*g_m DIRECTLY from data
g_ln_r_num.append(np.histogram(radii[i],
bins_rad,
weights=xis[i]*mass)[0] \
/ (bins_rad_width_log * dV))
f_m_num = np.array(f_m_num)
g_m_num = np.array(g_m_num)
g_ln_r_num = np.array(g_ln_r_num)
f_m_num_avg = np.average(f_m_num, axis=0)
f_m_num_std = np.std(f_m_num, axis=0, ddof=1) / np.sqrt(no_sims)
g_m_num_avg = np.average(g_m_num, axis=0)
g_m_num_std = np.std(g_m_num, axis=0, ddof=1) / np.sqrt(no_sims)
g_ln_r_num_avg = np.average(g_ln_r_num, axis=0)
g_ln_r_num_std = np.std(g_ln_r_num, axis=0, ddof=1) / np.sqrt(no_sims)