forked from cdominik/optool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optool.py
1837 lines (1620 loc) · 67.6 KB
/
optool.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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""NAME
optool
DESCRIPTION
This module provides an interface to the optool program (available
at https://github.com/cdominik/optool), and tools to plot, convert,
and to compute with the results.
It also provides tools to prepare refractive index data for use
with the tool.
EXAMPLES
Compute pyroxene with an ice mantle in 24 different grain sizes
and plot the results
import optool
p = optool.particle(’~/bin/optool pyr 0.8 -m ice 0.2 -na 24 -d’)
p.plot()
Read opacity files produced earlier by a run of optool. This is
triggered by giving an empty command string, and the directory as
second argument.
import optool
part = optool.particle('','path/to/directory')
Compute the opacities of 100 olivine silicate grain sizes and of 50
carbon grain sizes, and store the opacities in cache directories. This
works by specifying the directory as the second argument. In a new
session, if the directories still exist and were produced using the
same commands, the opacities are simply read back in.
import optool
import numpy as np
sil = optool.particle('optool -d -a 0.001 100 0 100 ol-mg50',cache='sil')
carb = optool.particle('optool -d -a 0.001 3.0 0 50 c',cache='carb')
Apply powerlaw size distributions, and limit the size of the
contributing grains. Note that a power law f(a)\propto a^{-3.5}
implies using a power a^{-2.5} when computing the number of particles
per size bin on a logarithmic size grid. No normalization is
necessary - the =sizedist= method will take care of that.
nsil = sil.a1**(-2.5) # power law, no normalization required
nsil[sil.a1<0.01] = 0 # no grains smaller than 0.01um
nsil[sil.a1>0.3] = 0 # no grains larger than 0.3um
sil_pl = sil.sizedist(nsil) # pass the relative number for each size
nc = carb.a1**(-2.5) # power law, no normalization required
nc[carb.a1>0.3]=0 # no grains larger than 0.3um
carb_pl = carb.sizedist(nc) # pass the relative number for each size
sil_pl and carb_pl are now objects with a single opacity each,
obtained by adding opacities with the weights of the size
distribution. The opacities are still per g of total grain mass.
Let's add these two opacities with mass weights, to get something
resembling an interstellar dust opacity produced by a mixture of
silicate and carbon grains:
ptot = 0.7*sil_pl + 0.3*carb_pl # weights should add up to 1
ptot.plot() # plot the resulting opacity
Now let's assume we are looking at an interstellar cloud, where the
dust is just one percent of the total mass. We want to have the
opacity per unit of /gas mass/ instead, and we need Planck and
Rosseland mean opacities:
p_ism = ptot * 0.01 # dilute the opacity
p_ism.computemean(tmax=1300) # Compute mean opacities
p_ism.plot() # Plot the results
Other size distributions can be made just as easily. Here is a
log-normal size distribution for the silicate grains, with a
peak abundance at a size of a_m=1.3 microns, and a logarithmic width
of \sigma=1.2:
sil_ln = sil.sizedist( np.exp( -0.5*(np.log(sil.a1/1.3)/1.2)**2) )
sil_ln.write('dkap_ln.dat') # write opacity to a file
"""
import copy
import numpy as np
import matplotlib.pyplot as plt
import math as m
import re
import os
import shutil
import subprocess
from distutils.spawn import find_executable
import tempfile
class particle:
"""Run optool and turn output into a python object.
Provides an interface to the optool program for computing dust
opacities. The optool program can be found on GitHub, at this address:
https://github.com/cdominik/optool .
Attributes
----------
cmd : str
The full command given in the particle() call
radmc : boolean
Output follows RADMC conventions
scat : boolean
Scattering matrix is available
nlam : int
Number of wavelength points
lam : float[nlam]
The wavelength grid
nang : int
Number of scattering angles
scatang : float[nang]
The angular grid
materials : [[[...]...]... ]
Lists with [location,m_{frac},\rho,material
np : int
Number of particles, either 1 or (with -d) n_a
fmax : float[np]
Maximum volume fraction of vacuum for DHS
pcore, pmantle : float[np]
Porosity of the core/mantle material
amin : float[np]
min grain size used for each particle
amax : float[np]
max grain size used for each particle
nsub : int[np]
Number of sizes averaged for each particle
apow : float[np]
Negative size distribution power law (e.g. 3.5)
amean : float[np]
Mean size for (log-)nornal size distributions
asig : float[np]
Standard deviation for (log-)normal distribution
a1 : float[np]
Mean grain radius
a2 : float[np]
Radius of the grain with mean surface area
a3 : float[np]
Radius of the grain with mean volume
rho : float[np]
Specific density of grains
kabs : float[np,nlam]
Absorption cross section
ksca : float[np,nlam]
Scattering cross section
kext : float[np,nlam]
Extinction cross section
gsca : float[np,nlam]
Asymmetry parameter
f11, ..., f44 : float[np,nlam,nang]
Scattering matrix element F_11, ... ,F_44
chop : float[np]
Degrees chopped off forward scattering
tmin : float
Minimum temperature for mean opacities
tmax : float
Maximum temperature for mean opacities
ntemp : int
Number of temperatures for mean opacities
temp : float[ntemp]
Temperatures used for mean opacities
kplanck : float[np,ntemp]
Planck mean opacities, after calling computemean()
kross : float[np,ntemp]
Rosseland mean opacities, after calling computemean()
norm : string
Current scattering matrix normalization
Methods
-------
plot()
Plot the opacities and the scattering matrix
computemean(tmin=10,tmax=1500,ntemp=100)
Compute Planck and Rosseland mean opacities
scatnorm(norm='')
Check or change the normalization of the scattering matrix
sizedist(N_of_a)
Compute opacity of a size distribution of elements of SELF
"""
def __init__(self,cmd,cache='',silent=False):
"""Create a new optool.particle opject.
Parameters
---------=
cmd : str or False
A shell command to run optool. The output produced by this
command will be read in and stored in an instance of the
optool.particle class.
If this is False or the empty string, just read what an
earlier run of optool has put into the directory given by
the second parameter CACHE.
cache : str, optional
The diretory to cache the optool output files in, so that
they can be read instead of recomputed the next time
the same command is used. The cache is automatically
cleared when CMD changes between runs.
If CMD was False or empty, we do not check what command
made the directory. Instead we simply read what is there.
silent : boolean, optional
If True no messages or warnings will be printed on screen.
"""
if ((not cmd) and (not cache)):
# well, you need to give me SOMETHING to work with
raise RuntimeError("Specify CMD or CACHE or both")
elif (not cmd):
# no command, only read the cache directory
cmd = ''
elif (type(cmd)==list):
self.cmd = " ".join(cmd)
elif (type(cmd)==str):
self.cmd = cmd
else:
raise RuntimeError("First argument CMD needs to be string or list")
if (cache and not cmd):
# No command, just read directory
if not silent:
print("Reading files in directory:",cache,"...")
# Set cmd to the emty string, to signal not to run a command
cmd = ''
elif (cache and checkcmd(cache,self.cmd)):
# Directory was created by the exact same command - just read
if not silent:
print("Using result cache in directory:",cache,"...")
# Set cmd to the emty string, to signal not to run a command
cmd = ''
else:
# Convert command string into list if necessary
if (isinstance(cmd, str)):
cmd = cmd.split()
if cmd[0].startswith("~"):
cmd[0] = os.path.expanduser(cmd[0])
# Find the optool executable
bin = find_executable(cmd[0])
if (not bin):
raise RuntimeError("Executable not found: "+cmd[0])
# Wrap the main part into try - finally to make sure we clean up
try:
if (cache):
dir = cache
else:
# create temporary directory in /tmp/
dir = tempfile.mkdtemp(prefix="optool_")
if cmd:
if cache:
# make sure directory is new and empty
shutil.rmtree(dir,ignore_errors=True)
os.mkdir(dir)
# Store the command line we are using. We store the
# string version of the command, not the list version.
writecmd(dir,self.cmd)
# tell optool to use the directory as writing desination
cmd.append('-o'); cmd.append(dir)
# Run optool to produce the opacities
stdout = subprocess.DEVNULL if silent else None
stderr = subprocess.DEVNULL if silent else None
cmd[0] = bin; subprocess.Popen(cmd, stdout=stdout, stderr=stderr).wait()
# Check if there is output we can use
scat,ext,translate = check_for_output(dir)
self.scat = scat
self.massscale = 1.
kabs=[]; ksca=[]; kext=[]; gg=[]
f11=[]; f12=[]; f22=[]; f33=[]; f34=[]; f44=[]
nfiles=0; header=[];
materials = []
rho = []
for i in range(5000):
if scat:
file = ("%s/dustkapscatmat_%03d.%s") % (dir,(i+1),ext)
else:
file = ("%s/dustkappa_%03d.%s") % (dir,(i+1),ext)
file = translate.get(file,file)
if (not os.path.exists(file)): break
nfiles = nfiles+1
x = readoutputfile(file,scat,silent=silent)
header.append(x[0])
lam = x[1]
kabs.append(x[2])
ksca.append(x[3])
kext.append(x[2]+x[3])
gg.append(x[4])
if scat:
scatang = x[5]
f11.append(x[6])
f12.append(x[7])
f22.append(x[8])
f33.append(x[9])
f34.append(x[10])
f44.append(x[11])
self.scat = scat
self = parse_headers(header,self)
self.nlam = len(lam)
self.kabs = np.array(kabs)
self.ksca = np.array(ksca)
self.kext = np.array(kext)
self.gsca = np.array(gg)
self.lam = lam
if scat:
self.nang = len(scatang)
self.scatang = scatang
self.f11 = np.array(f11)
self.f12 = np.array(f12)
self.f22 = np.array(f22)
self.f33 = np.array(f33)
self.f34 = np.array(f34)
self.f44 = np.array(f44)
else:
self.nang = 0
self.np = nfiles
finally:
if cache:
if not silent:
print("Files remain available in directory: "+dir)
else:
if not silent:
print("Cleaning up temporary directory "+dir)
shutil.rmtree(dir)
def plot(self,minkap=1e0):
"""Create interactive plots of the opacities in SELF.
Furthermore, a plot for the scattering matric elements and, if the
computemean() method has been called, a plot of the mean opacities
are produces as well.
"""
# Check if mean opacities have been computed
if hasattr(self, 'kplanck'):
# llamfmt = np.round(np.log10(self.lam),decimals=3)
kplanck = self.kplanck
kross = self.kross
temp = self.temp
viewarr([kplanck,kross],index=1,ylabel=['kplanck','kross'],
idxnames=['grain index','log lambda [um]'],
idxvals=[np.array(range(self.np))+1,temp])
# Extract the kappas and g
kabs = np.copy(self.kabs)
ksca = np.copy(self.ksca)
kext = kabs+ksca
gg = np.copy(self.gsca)
maxkap = np.amax(kabs)
if (maxkap<minkap*100):
print('WARNING: you may want to change minkap to ',maxkap/100,' or smaller')
# limit the kappa plotting range
kabs = np.maximum(kabs,minkap)
ksca = np.maximum(ksca,minkap)
kext = np.maximum(kext,minkap)
# We will plot the logarithms of the Kappa values
kabs = np.log10(kabs)
ksca = np.log10(ksca)
kext = np.log10(kext)
# Scale g such that it will fill the y range of the kappa plot
kmin = np.amin(np.array([np.amin(kabs),np.amin(ksca),np.amin(kext)]))
kmax = np.amax(np.array([np.amax(kabs),np.amax(ksca),np.amax(kext)]))
ggscal = gg*(kmax-kmin)+kmin
# Extract and plot the scattering matrix elements
if self.scat:
bottom = 1e-2
f11 = logscale_with_sign(np.copy(self.f11),bottom)
f12 = logscale_with_sign(np.copy(self.f12),bottom)
f22 = logscale_with_sign(np.copy(self.f22),bottom)
f33 = logscale_with_sign(np.copy(self.f33),bottom)
f34 = logscale_with_sign(np.copy(self.f34),bottom)
f44 = logscale_with_sign(np.copy(self.f44),bottom)
f00 = f11*0.
# Make version of grid variables with fewer digits
lamfmt = np.round(self.lam,decimals=3)
angfmt = np.round(self.scatang,decimals=3)
# interactive plot of the scattering matric elements
viewarr([f00,f00+2,f00-2,f00+4,f00-4,f11,f12,f22,f33,f34,f44],
index=2,ylabel=['<1e-2','±1','','±1e2','','f11','f12',
'f22','f33','f34','f44'],
idxnames=['grain index','lambda [um]','angle'],
idxvals=[np.array(range(self.np))+1,lamfmt,angfmt])
# interactive plot of kabs, ksca, kext, and g
llamfmt = np.round(np.log10(self.lam),decimals=3)
viewarr([ggscal,kext,ksca,kabs],index=1,
ylabel=['gg','kext','ksca','kabs'],
idxnames=['grain index','log lambda [um]'],
idxvals=[np.array(range(self.np))+1,llamfmt])
def plotpi(self,ymin=None,ymax=None):
"""Create interactive plots of I,P,p.
I is the intensity, f11
P it the polarization, abs(f12)
p is the degree of polarization, P/I
The y-axis scaling is based on the values of P and p, but ignores I since
it can become so large. ymax and ymin are keyword parameters to change
the y-axis scaling.
"""
# Extract and plot the scattering matrix elements
if self.scat:
bottom = 1e-2
int_I = self.f11
int_P = np.sqrt(self.f12**2) # abs would be good enough, but to remind of general case
deg_P = abs(int_P/int_I)
if (not ymin):
ymin = min(np.min(np.matrix.flatten(int_P)),np.min(np.matrix.flatten(deg_P)))
if (not ymax):
ymax = max(np.max(np.matrix.flatten(int_P)),np.max(np.matrix.flatten(deg_P)))
# Make version of grid variables with fewer digits
lamfmt = np.round(self.lam,decimals=3)
angfmt = np.round(self.scatang,decimals=3)
# interactive plot of the scattering matric elements
viewarr([int_I,int_P,deg_P],
index=2,ylabel=['f11','-f12',
'f12/f11'],
idxnames=['grain index','lambda [um]','angle'],
idxvals=[np.array(range(self.np))+1,lamfmt,angfmt],
ymin=ymin,ymax=ymax)
def select(self,i):
"""Select just one bin from a multi-particle object.
A multi-particle opject is produced when running optool with
a -d switch.
This is useful for doing particle arithmetic, which only works for
single particle objects.
"""
x = copy.deepcopy(self)
x.np = 1
j = i+1
x.fmax = x.fmax[i:j]
x.pcore = x.pcore[i:j]
x.pmantle = x.pmantle[i:j]
x.amin = x.amin[i:j]
x.amax = x.amax[i:j]
x.nsub = x.nsub[i:j]
x.apow = x.apow[i:j]
x.amean = x.amean[i:j]
x.asig = x.asig[i:j]
x.a1 = x.a1[i:j]
x.a2 = x.a2[i:j]
x.a3 = x.a3[i:j]
x.rho = x.rho[i:j]
x.chop = x.chop[i:j]
x.kabs = x.kabs[i:j,:]
x.ksca = x.ksca[i:j,:]
x.kext = x.kext[i:j,:]
x.gsca = x.gsca[i:j,:]
if x.scat:
x.f11 = x.f11[i:j,:,:]
x.f12 = x.f12[i:j,:,:]
x.f22 = x.f22[i:j,:,:]
x.f33 = x.f33[i:j,:,:]
x.f34 = x.f34[i:j,:,:]
x.f44 = x.f44[i:j,:,:]
if (hasattr(x,'kross')):
x.kplanck = x.kplanck[i:j,:]
x.kross = x.kross[i:j,:]
return x
def sizedist(self,N_of_a):
"""Compute opacity of a size distribution of elements of SELF.
Arguments
---------
N_of_a : numpy array containing the sumber of partiles of each size
available in SELF (as given by self.a1)
"""
# Check if N_of_a is compatible with self.a1
if (len(N_of_a) != len(self.a1)):
raise RuntimeError('N_of_a and a1 arrays differ in length')
# create a particle object to return
x = copy.deepcopy(self)
# Fill all attributes that make sense
x.np = 1
x.cmd = ''
x.materials = self.materials[0:1]
x.fmax = x.fmax[0:1]
x.pcore = x.pcore[0:1]
x.pmantle = x.pmantle[0:1]
x.amin = x.a1[0:1]
x.amax = x.a1[-1:]
x.nsub = self.nsub[0]*self.np
x.apow = x.apow[0:1]
x.amean = x.amean[0:1]
x.asig = x.asig[0:1]
x.a1 = x.a2 = x.a3 = -1;
x.rho = x.rho[0:1]
x.chop = x.chop[0:1]
# Turn N_of_a into mass fractions, normalized to 1
mass = (4./3.) * np.pi * (self.a1*1e-4)**3 * self.rho
m_of_a = N_of_a*mass
mtot = np.sum(m_of_a)
mfrac = m_of_a/mtot
x.massscale = 1
# add up the opacities
x.kabs = np.sum(self.kabs*mfrac[:,None],axis=0)
x.ksca = np.sum(self.ksca*mfrac[:,None],axis=0)
x.kabs = x.kabs[None,:]; x.ksca = x.ksca[None,:] # add particle size axis
x.kext = x.kabs+x.ksca
# compute gsca
x.gsca = np.sum(self.ksca*self.gsca*mfrac[:,None],axis=0) / x.ksca[0]
x.gsca = x.gsca[None,:] # add particle size axis
# compute the scattering matrix elements
if x.scat:
if self.norm == 'hovenier':
w = (self.ksca*mfrac[:,None])[:,:,None]
wn = x.ksca[0,:,None]
x.f11 = np.sum(self.f11*w,axis=0)/wn
x.f12 = np.sum(self.f12*w,axis=0)/wn
x.f22 = np.sum(self.f22*w,axis=0)/wn
x.f33 = np.sum(self.f33*w,axis=0)/wn
x.f34 = np.sum(self.f34*w,axis=0)/wn
x.f44 = np.sum(self.f44*w,axis=0)/wn
else:
w = mfrac[:,None,None]
x.f11 = np.sum(self.f11*w,axis=0)
x.f12 = np.sum(self.f12*w,axis=0)
x.f22 = np.sum(self.f22*w,axis=0)
x.f33 = np.sum(self.f33*w,axis=0)
x.f34 = np.sum(self.f34*w,axis=0)
x.f44 = np.sum(self.f44*w,axis=0)
# Add the particle size axis
x.f11 = x.f11[None,:]; x.f12 = x.f12[None,:]; x.f22 = x.f22[None,:];
x.f33 = x.f33[None,:]; x.f34 = x.f34[None,:]; x.f44 = x.f44[None,:];
# Return the new object
return x
def scatnorm(self,norm=""):
"""Check or change the normalization of the scattering matrix.
Without an argument, check the current normalization of the
scattering matrix.
p = optool.particle('./optool -s')
p.scatnorm()
Calling the method with an argument will change the normalization
to one of the following conventions
'b' Bohren & Huffman
'm' Mishchenko
'r' RADMC-3D
'h' Hovenier
"""
# analyze the NORM parameter
if (norm == ""):
renorm = False
conv = self.norm
else:
renorm = True
conv = norm
conv = conv.lower()
if (conv in ['h','hovenier']):
self.norm = "hovenier"
name = "Hovenier"
normalization = "4 pi"
units = "sr^-1"
elif (conv in ['b','bh','bohren','bohrenhuffman']):
self.norm = "bohrenhuffman"
name = "Bohren & Huffman"
normalization = "kappa_scat m_grain (2pi/lambda)^2"
units = "sr^-1"
elif (conv in ['m','mish','mishchenko']):
self.norm = "mishchenko"
name = "Mishchenko"
normalization = "kappa_scat m_grain"
units = "cm^2 sr^-1"
elif (conv in ['r','radmc','radmc3d']):
self.norm = "radmc3d"
name = "RADMC-3D"
normalization = "kappa_scat"
units = "cm^2 g^-1 sr^-1"
else:
print("ERROR: Unknown normalization ",conv)
return -1
ang = self.scatang
lam = self.lam
wav = 2.*np.pi/(lam*1e-4) # need cm here, not micrometer
ratio = np.zeros([self.np,self.nlam])
# Compute values and weights for the integration
if (self.gridtype == "boundary"):
# Matrix values are on cell boundaries
if (ang[0] != 0):
raise RuntimeError("Inconsistency between gridtype \"boundary\" and angle values")
thetab = ang*np.pi/180.
mub = np.cos(thetab)
dmu = mub[:-1]-mub[1:] # Defined negatively for mu integral
fc = 0.5*(self.f11[:,:,1:]+self.f11[:,:,:-1])
else:
# This is the standard grid with values on cell midpoints
if (ang[0] == 0):
raise RuntimeError("Inconsistency between gridtype \"center\" and angle values")
th1 = (ang-0.5)*np.pi/self.nang; mu1 = np.cos(th1)
th2 = (ang+0.5)*np.pi/self.nang; mu2 = np.cos(th2)
dmu = mu1-mu2 # Defined negatively for the mu integral
fc = self.f11
for ip in (range(self.np)):
for il in (range(self.nlam)):
integ = 2.*np.pi*np.sum(fc[ip,il,:]*dmu)
if (self.norm == "radmc3d"):
nn = self.ksca[ip,il]
elif (self.norm == "hovenier"):
nn = 4.*np.pi
elif (self.norm == "bohrenhuffman"):
mgrain = (4./3.)*np.pi * self.a3[ip]**3 * self.rho[ip]
nn = self.ksca[ip,il] * wav[il]**2 * mgrain
elif (self.norm == "mishchenko"):
mgrain = (4./3.)*np.pi * self.a3[ip]**3 * self.rho[ip]
nn = self.ksca[ip,il] * mgrain
if (norm):
self.f11[ip,il,:] = self.f11[ip,il,:] * nn/integ
self.f12[ip,il,:] = self.f12[ip,il,:] * nn/integ
self.f22[ip,il,:] = self.f22[ip,il,:] * nn/integ
self.f33[ip,il,:] = self.f33[ip,il,:] * nn/integ
self.f34[ip,il,:] = self.f34[ip,il,:] * nn/integ
self.f44[ip,il,:] = self.f44[ip,il,:] * nn/integ
ratio[ip,il] = 1.
else:
ratio[ip,il] = integ / nn
if (norm):
print("New nomalization is ",name," convention")
else:
print("Current nomalization is ",name," convention")
print("Units of matrix elements are ",units)
print("Integral F_11 d Omega = ",normalization)
if (not norm):
maxerr = np.amax(np.abs(ratio-1.))
print("Maximum deviation %7.2e" % maxerr)
def computemean(self, tmin=10., tmax=1500., ntemp=100):
"""Compupte mean opacities from the opacities in self.
Parameters
----------
tmin : float
minimum temperature for which to compute mean opacities
tmax : float
maximum temperature for which to compute mean opacities
ntemp : int
number of temperature steps between tmin and tmax
"""
self.tmin = tmin
self.tmax = tmax
self.ntemp = ntemp
self.temp = np.logspace(np.log10(tmin),np.log10(tmax),ntemp)
self.kross = np.zeros([self.np,self.ntemp])
self.kplanck = np.zeros([self.np,self.ntemp])
cl = 2.99792458e10 # Speed of light [cgs]
nu = 1e4*cl/self.lam # 10^4 because lam is in um - we need cm
dnu = -1. * np.hstack([nu[1]-nu[0],0.5 * (nu[2:]-nu[:-2]), nu[-1] - nu[-2] ])
for it in range(self.ntemp):
bnu = bplanck(self.temp[it],nu)
bnudt = bplanckdt(self.temp[it],nu)
dumbnu = np.sum(bnu*dnu)
dumdb = np.sum(bnudt*dnu)
for ip in range(self.np):
kap_p = np.sum(self.kabs[ip,:]*bnu*dnu) / dumbnu
kap_r = dumdb / np.sum(bnudt * dnu / ( self.kabs[ip,:] + self.ksca[ip,:]*(1.-self.gsca[ip,:])))
self.kplanck[ip,it] = kap_p
self.kross[ip,it] = kap_r
def __add__(s,o):
"""Addition of optool.particle objects.
This can be used to mix different grain types together
into a dust model.
# Make a silicate grain and a carbonatieous grain
p1 = optool.particle('./optool -a 0.01 0.3 pyr-mg70')
p2 = optool.particle('./optool -1 0.03 0.1 c-z')
# Mix the particles with a mass ration 0.75 : 0.25
# Make sure abundances add up to 1, or the opacities will
# not be per g of dust!
p = 0.75*p1 + 0.25*p2
# Apply a dust-to-gas ratio, so that the opacities will be
# per unit of GAS mass
dtg = 0.01
p = dtg * p
# Plot the opacities
p.plot()
"""
#
# First, check if the particles are compatible
#
if ((s.np > 1) or (o.np>1)):
raise TypeError('Cannot add multi-particle objects')
if ((s.nlam != o.nlam) or (np.abs((s.lam-o.lam)/s.lam).any()>1e-4)):
raise RuntimeError('Wavelength grids differ')
if (s.scat):
if ((s.nang != o.nang) or
(np.abs((s.scatang[1:]-o.scatang[1:])/s.scatang[1:]).any()>1e-4)):
# We don't check the first value, could be 0
raise RuntimeError('Angular grids differ')
if (s.norm != o.norm):
raise RuntimeError('Scattering normalizations differ')
#
# Now do the adding
#
x = copy.deepcopy(s)
x.kabs = x.kabs+o.kabs
x.ksca = x.ksca+o.ksca
x.kext = x.kext+o.kext
# F11 is linear in the integral for the computation of g.
# So we can just take the weighted mean for g.
x.gsca = (x.ksca*x.gsca + o.ksca*o.gsca) / (x.ksca+o.ksca)
x.massscale = s.massscale + o.massscale
if s.scat:
# There is a scattering matrix.
if s.norm == 'hovenier':
# Add, weighted by kappa_scat
ws = s.ksca[:,:,None]
wo = o.ksca[:,:,None]
wn = ws+wo
else:
# Just add the values
ws, wo, wn = 1.,1.,1.
x.f11 = (s.f11*ws + o.f11*wo) / wn
x.f12 = (s.f12*ws + o.f12*wo) / wn
x.f22 = (s.f22*ws + o.f22*wo) / wn
x.f33 = (s.f33*ws + o.f33*wo) / wn
x.f34 = (s.f34*ws + o.f34*wo) / wn
x.f44 = (s.f44*ws + o.f44*wo) / wn
#
# Invalidate attributes that no longer make sense.
#
x.materials = np.hstack((x.materials,o.materials))
if (x.fmax != o.fmax ): x.fmax = -1
if (x.pcore != o.pcore ): x.pcore = -1
if (x.pmantle != o.pmantle): x.pmantle = -1
if (x.amin != o.amin ): x.amin = -1
if (x.amax != o.amax ): x.amax = -1
if (x.nsub != o.nsub ): x.nsub = -1
if (x.apow != o.apow ): x.apow = -1
if (x.amean != o.amean ): x.amean = -1
if (x.asig != o.asig ): x.asig = -1
if (x.rho != o.rho ): x.rho = -1
if (x.chop != o.chop ): x.chop = -1
x.a1,x.a2,x.a3 = -1,-1,-1
if hasattr(s, 'kplanck'):
kplanck = -1
kross = -1
temp = -1
return x
def __mul__(s,o):
"""Multiplication for optool.particle objects.
This is intended for the multiplication of such an object with
a number. The way to think about it is like this. Such an
contains opacities in units cm^2/g. Multiplying it with a
number means that the opacities are now per a different mass.
This sounds strange, but it makes sense together with addition
of particles - which see.
"""
if (not (isinstance(o,int) or isinstance(o,float))):
raise TypeError('optool.particle object can only be multiplied by a number')
x = copy.deepcopy(s)
x.kabs = x.kabs*o; x.ksca = x.ksca*o; x.kext = x.kext*o
x.massscale = x.massscale*o
if (s.scat and (s.norm != 'hovenier')):
# We need to change the matrix as well, it's normalized to ksca
x.f11 = x.f11*o; x.f12 = x.f12*o; x.f22 = x.f22*o
x.f33 = x.f33*o; x.f34 = x.f34*o; x.f44 = x.f44*o
return x
def __rmul__(s,o):
"""Rightsided multiplication of optool.particle object by a number."""
return s*o
def __div__(s,o):
"""Division of optool.particle object by a number."""
return s * (1./o)
def __truediv__(s,o):
"""Division of optool.particle object by a number."""
return s * (1./o)
def write(s,filename,header="Opacity file written by optool.particle.write"):
"""Write a single particle object to a file.
The format of the file will be similar to the dustkappa.dat and
dustkapscatmat.dat files produced by the optool FORTRAN program,
with the difference that the header will not contain the detailed
information about the computation. But the file would be readable
with the `readoutputfile' function.
Arguments
=========
filename: String, pointing the file name to which output should
be written.
header: A string that should be put at the beginning of the
file, as a commend describing the dataset. The string
may have several lines, the # comment character will
automatically be added to the beginning of every line.
"""
if (s.np>1):
raise TypeError('Writing is not supported for multi-particle objects')
try:
wfile = open(filename, 'w')
except:
raise RuntimeError('Cannot write to file: '+filename)
headerlines = header.splitlines()
for i in range(len(headerlines)):
wfile.write("# %s\n" % headerlines[i])
if s.scat:
wfile.write(' 0\n')
wfile.write(' %d\n' % s.nlam)
wfile.write(' %d\n' % s.nang)
wfile.write('\n')
else:
wfile.write(' 3\n')
wfile.write(' %d\n' % s.nlam)
for i in range(s.nlam):
# write the lambda grid and the opacities
wfile.write(' %15.5e %15.5e %15.5e %15.5e\n' % (s.lam[i],s.kabs[0,i],s.ksca[0,i],s.gsca[0,i]))
if s.scat:
# we have a scattering matrix
wfile.write('\n')
# Write the angular grid
for i in range(s.nang):
wfile.write("%9.2f\n" % s.scatang[i])
wfile.write('\n')
# Write the scattering matrix
for il in range(s.nlam):
for ia in range(s.nang):
wfile.write(' %15.5e %15.5e %15.5e %15.5e %15.5e %15.5e\n' %
(s.f11[0,il,ia],s.f12[0,il,ia],s.f22[0,il,ia],
s.f33[0,il,ia],s.f34[0,il,ia],s.f44[0,il,ia]))
wfile.close()
class lnktable:
"""Class to work with lnk files.
lnk stands for lambda, n, and k, where and and k are the real and
imaginary components of the refractive index of a material.
Conversion
----------
The standard format of these files is described in the optool user
guide. The class can also read files that are formatted
differently, in order to create properly formatted version. For
example, if you have a file starting with 4 unimportant lines, and
then data columns where n an k are in column 1 and 2,
respectively, and the wavelength is given in units of cm^-1 in
column 3, you can do the conversion in this way:
new = optool.lnktable('x.dat',i_lnk=[3,1,2], nskip=4)
new.lam = 10000./new.lam # convert cm^-1 -> micrometer
new.sort() # sort arrays according to lambda
new.rho = 3.2 # set density in g/cm^3
new.header = "# This is a silicate from Dorschner+1995"
new.write('sil-Dorschner1995.lnk')
"""
def __init__(self,file,i_lnk=[1,2,3],nskip=0,nlam_rho=True):
"""Create a new optool.lnktable object
Parameters
----------
file : str
the file name from which to read the lnk data
i_lnk : numpy array, optional
the column numbers where to find lambda, the real part of the
refractive index and the imaginary part of it, respectively.
The default is [1,2,3] .
nskip : int, optional
Number of lines to skil at the beginning. Lines starting with
`#', `!' or `*` are stored as header lines and ar skipped in
this way. So this parameter is for dealing with files that are
not yet formatted in the standard way for optool. The default
is 0.
nlam_rho : boolean, optional
True means, the first unskipped line contains the number of
wavelengths points and the specific density of the material.
False means no such line exists, and the lines have to be
counted. Rho will be se to 0 then, to indicate that the value
is not know at this point.
"""
self.filename = file
try:
rfile = open(file, 'r')
except:
print('ERROR: File not found:',file)
return -1
print('Reading lnk file ',file,'...')
# Skip lines that are irrelevant
for i in range (nskip): dum = rfile.readline()
# Read the header/comment field
header = ''
dum = rfile.readline()
while ((dum.strip()[0]=='#') or (dum.strip()[0]=='*') or (dum.strip()[0]=='!')):
header = header + dum
dum = rfile.readline()
self.header = header
# Extract the number of wavelengths points, and the material density
if (nlam_rho):
dum = dum.split()
self.nlam = int(dum[0])
self.rho = float(dum[1])
dum = rfile.readline()
else:
self.nlam = 1
self.rho = 0.0
print("Warning: density rho is not known! Make sure to set it by hand.")
# Prepare the arrays
self.lam = []
self.n = []
self.k = []
ilam = 0
# Fill the arrays
while True:
dum = dum.split()
ilam = ilam+1
self.lam.append(float(dum[i_lnk[0]-1]))
self.n.append( float(dum[i_lnk[1]-1]))
self.k.append( float(dum[i_lnk[2]-1]))
dum = rfile.readline()
if ((len(dum) == 0) or dum.isspace()):
# No more data. Truncate the arrays and stop reading
if (not (self.nlam == ilam)):
print("WARNING: found %d lines of data, not %d" % (ilam,self.nlam))
# Convert to numpy arrays and exit
self.nlam = ilam
self.lam = np.array(self.lam)
self.n = np.array(self.n)
self.k = np.array(self.k)
break