-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputePart.f
3370 lines (3001 loc) · 112 KB
/
ComputePart.f
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
subroutine ComputePart(p,ii,isize,iT)
use GlobalSetup
use Constants
IMPLICIT NONE
type(Particle) p
integer MAXMAT
parameter(MAXMAT=20)
integer ii,iT,isize
real cext,csca,maxf
real minlog,maxlog,pow,e1blend,e2blend,cabs,totA
real,allocatable :: f11(:,:),f12(:,:)
real,allocatable :: f22(:,:),f33(:,:)
real,allocatable :: f34(:,:),f44(:,:)
real e1av,e2av,rad,r1,r2,tot,lmax,lmin,Mass,tot2
real lambda,Vol,rho_av
real,allocatable :: r(:),nr(:,:),f(:),wf(:),rho(:)
real,allocatable :: e1(:,:),e2(:,:),frac(:)
real*8,allocatable :: e1d(:),e2d(:)
integer i,j,k,l,na,nf,ns,nm,ilam,Err,spheres,toolarge
complex m,min,mav,alpha
real QEXT, QSCA, QBS, GQSC,rcore,wvno,scale
real,allocatable :: mu(:),M1(:,:),M2(:,:),S21(:,:),D21(:,:)
character*3 meth
character*500 input,filename(100),grid,tmp,tmp2,partfile,lnkfile
real*8 rmie,lmie,e1mie,e2mie,csmie,cemie,KR,theta,dummy,amin,amax
real*8,allocatable :: Mief11(:),Mief12(:),Mief22(:)
real*8,allocatable :: Mief33(:),Mief34(:),Mief44(:)
logical truefalse,checkparticlefile,lnkloglog
external Carbon_BE_Zubko1996,Mg07Fe03SiO3_Dorschner1995,AstroSilicate
external Ice_Warren2008,OrganicsHenning,FeS
write(tmp,'("mkdir -p ",a)') particledir(1:len_trim(particledir)-1)
call system(tmp)
write(meth,100)
100 format('DHS')
na=180
lnkloglog=.true.
allocate(e1(MAXMAT,nlam))
allocate(e2(MAXMAT,nlam))
allocate(Mief11(na))
allocate(Mief12(na))
allocate(Mief22(na))
allocate(Mief33(na))
allocate(Mief34(na))
allocate(Mief44(na))
allocate(mu(na))
allocate(M1(na,2))
allocate(M2(na,2))
allocate(S21(na,2))
allocate(D21(na,2))
allocate(frac(MAXMAT))
allocate(rho(MAXMAT))
allocate(f11(nlam,na))
allocate(f12(nlam,na))
allocate(f22(nlam,na))
allocate(f33(nlam,na))
allocate(f34(nlam,na))
allocate(f44(nlam,na))
amin=10d0**(log10(p%amin)+log10(p%amax/p%amin)*real(isize-1)/real(p%nsize))
amax=10d0**(log10(p%amin)+log10(p%amax/p%amin)*real(isize)/real(p%nsize))
minlog=log10(amin)
maxlog=log10(amax)
pow=-p%apow
maxf=p%fmax
input=p%file(iT)
if(p%standard.eq.'FILE') then
open(unit=30,file=input,RECL=5000)
call ignorestar(30)
read(30,*) ns
call ignorestar(30)
read(30,*) nf
if(maxf.eq.0e0) nf=1
allocate(r(ns))
allocate(nr(MAXMAT,ns))
allocate(f(nf))
allocate(wf(nf))
nm=1
1 call ignorestar(30)
read(30,*,end=2) filename(nm)
call ignorestar(30)
read(30,*) frac(nm),rho(nm)
call ignorestar(30)
c change this to the input abundances if they are set
if(p%inp_abun(nm).ge.0d0) then
frac(nm)=p%inp_abun(nm)
endif
c changed this to mass fractions (11-05-2010)
frac(nm)=frac(nm)/rho(nm)
call readrefindCP(filename(nm),lam(1:nlam),e1(nm,1:nlam),e2(nm,1:nlam),nlam,lnkloglog)
nm=nm+1
goto 1
2 nm=nm-1
close(unit=30)
else if(p%standard.eq.'DIANA') then
input='DIANA'
ns=p%nsubgrains
nf=20
if(maxf.eq.0e0) nf=1
allocate(r(ns))
allocate(nr(MAXMAT,ns))
allocate(f(nf))
allocate(wf(nf))
nm=2
rho(1)=3.01
rho(2)=1.80
frac(1)=(1d0-p%fcarbon)/rho(1)
frac(2)=p%fcarbon/rho(2)
allocate(e1d(nlam))
allocate(e2d(nlam))
filename(1)='Mg07Fe03SiO3_Dorschner1995'
call RegridDataLNK(Mg07Fe03SiO3_Dorschner1995,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(1,1:nlam)=e1d(1:nlam)
e2(1,1:nlam)=e2d(1:nlam)
filename(2)='Carbon_BE_Zubko1996'
call RegridDataLNK(Carbon_BE_Zubko1996,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(2,1:nlam)=e1d(1:nlam)
e2(2,1:nlam)=e2d(1:nlam)
deallocate(e1d)
deallocate(e2d)
else if(p%standard.eq.'DSHARP') then
input='DSHARP'
ns=p%nsubgrains
nf=1
maxf=0e0
if(maxf.eq.0e0) nf=1
allocate(r(ns))
allocate(nr(MAXMAT,ns))
allocate(f(nf))
allocate(wf(nf))
nm=4
p%porosity=0d0
p%blend=.true.
rho(1)=0.92
rho(2)=3.30
rho(3)=4.83
rho(4)=1.50
frac(1)=0.3642
frac(2)=0.1670
frac(3)=0.0258
frac(4)=0.4430
allocate(e1d(nlam))
allocate(e2d(nlam))
filename(1)='Ice_Warren2008'
call RegridDataLNK(Ice_Warren2008,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(1,1:nlam)=e1d(1:nlam)
e2(1,1:nlam)=e2d(1:nlam)
filename(2)='AstroSilicate'
call RegridDataLNK(AstroSilicate,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(2,1:nlam)=e1d(1:nlam)
e2(2,1:nlam)=e2d(1:nlam)
filename(3)='FeS'
call RegridDataLNK(FeS,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(3,1:nlam)=e1d(1:nlam)
e2(3,1:nlam)=e2d(1:nlam)
filename(4)='OrganicsHenning'
call RegridDataLNK(OrganicsHenning,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(4,1:nlam)=e1d(1:nlam)
e2(4,1:nlam)=e2d(1:nlam)
deallocate(e1d)
deallocate(e2d)
else if(p%standard.eq.'ASTROSIL') then
input='ASTROSIL'
ns=p%nsubgrains
nf=20
if(maxf.eq.0e0) nf=1
allocate(r(ns))
allocate(nr(MAXMAT,ns))
allocate(f(nf))
allocate(wf(nf))
nm=1
rho(1)=3.0
frac(1)=1d0/rho(1)
allocate(e1d(nlam))
allocate(e2d(nlam))
filename(1)='AstroSilicate'
call RegridDataLNK(AstroSilicate,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(1,1:nlam)=e1d(1:nlam)
e2(1,1:nlam)=e2d(1:nlam)
deallocate(e1d)
deallocate(e2d)
endif
min=dcmplx(1d0,0d0)
tot=0d0
do i=1,nm
tot=tot+frac(i)
enddo
frac=frac/tot
partfile=trim(particledir) // "particle"
& // trim(int2string(ii,'(i0.4)')) // "_" // trim(int2string(isize,'(i0.4)'))
& // "_" // trim(int2string(iT,'(i0.4)'))
if(abun_in_name.gt.0) then
do i=1,nm
select case(abun_in_name)
case(1)
partfile=trim(partfile) // "_f" // trim(dbl2string(1d0*frac(i),'(f3.1)'))
case(2)
partfile=trim(partfile) // "_f" // trim(dbl2string(1d0*frac(i),'(f4.2)'))
case(3)
partfile=trim(partfile) // "_f" // trim(dbl2string(1d0*frac(i),'(f5.3)'))
case(4)
partfile=trim(partfile) // "_f" // trim(dbl2string(1d0*frac(i),'(f6.4)'))
case default
partfile=trim(partfile) // "_f" // trim(dbl2string(1d0*frac(i),'(f7.5)'))
end select
enddo
endif
partfile=trim(partfile) // ".fits.gz"
inquire(file=partfile,exist=truefalse)
if(truefalse) then
if(checkparticlefile(partfile,amin,amax,p%apow,ns,p%fmax,p%blend,p%porosity,frac,rho,nm,filename,(abun_in_name.le.0))) then
call ReadParticleFits(partfile,p,isize,iT)
return
endif
endif
call output("Computing particle:" // trim(int2string(ii,'(i0.4)')))
call output("(size, T): (" // trim(int2string(isize,'(i0.4)')) // "," // trim(int2string(iT,'(i0.4)')) // ")")
call output("Size: " // trim(dbl2string(amin,'(f10.3)')) // " - " // trim(dbl2string(amax,'(f10.3)')) // " micron")
c do j=1,nm
c write(lnkfile,'(a,a,i0.3,".lnk")') trim(particledir),trim(input),j
c open(unit=30,file=lnkfile,RECL=200)
c write(30,'("# ",a)') trim(filename(j))
c do i=1,nlam
c write(30,*) lam(i),e1(j,i),e2(j,i)
c enddo
c close(unit=30)
c enddo
if(p%blend) then
nm=nm+1
e1(nm,1:nlam)=1d0
e2(nm,1:nlam)=0d0
rho(nm)=0d0
frac(nm)=p%porosity
frac(1:nm-1)=frac(1:nm-1)*(1d0-p%porosity)
do i=1,nlam
call Blender(e1(1:nm,i),e2(1:nm,i),frac,nm,e1blend,e2blend)
e1(1,i)=e1blend
e2(1,i)=e2blend
enddo
rho_av=0d0
do i=1,nm
rho_av=rho_av+frac(i)*rho(i)
enddo
rho(1)=rho_av
frac(1)=1d0
nm=1
write(lnkfile,'(a,a,".lnk")') trim(particledir),trim(input)
open(unit=30,file=lnkfile,RECL=200)
do i=1,nlam
write(30,*) lam(i),e1(1,i),e2(1,i)
enddo
close(unit=30)
endif
do i=1,nlam
do j=1,na
f11(i,j)=0d0
f12(i,j)=0d0
f22(i,j)=0d0
f33(i,j)=0d0
f34(i,j)=0d0
f44(i,j)=0d0
enddo
enddo
if(nf.gt.1.and.maxf.gt.0.01e0) then
call gauleg2(0.01e0,maxf,f(1:nf),wf(1:nf),nf)
else if(maxf.eq.0e0) then
f(1:nf)=0d0
wf(1:nf)=1d0/real(nf)
else
f(1)=maxf
wf(1)=1d0
endif
do ilam=1,nlam
call tellertje(ilam,nlam)
csca=0d0
cabs=0d0
cext=0d0
Mass=0d0
Vol=0d0
do i=1,na/2
theta=(real(i)-0.5)/real(na/2)*3.1415926536/2d0
mu(i)=cos(theta)
enddo
p%rv(isize)=0d0
do l=1,nm
if(ns.eq.1) then
r(1)=10d0**((minlog+maxlog)/2d0)
nr(l,1)=frac(l)
else
tot=0d0
do k=1,ns
r(k)=10d0**(minlog
& +(maxlog-minlog)*real(k-1)/real(ns-1))
nr(l,k)=r(k)**(pow+1d0)
tot=tot+nr(l,k)*r(k)**3
enddo
do k=1,ns
nr(l,k)=frac(l)*nr(l,k)/tot
p%rv(isize)=p%rv(isize)+nr(l,k)*r(k)**2
enddo
endif
enddo
p%rv(isize)=sqrt(p%rv(isize))
do l=1,nm
if(frac(l).eq.0d0) goto 10
do k=1,ns
r1=r(k)
Err=0
spheres=0
toolarge=0
do i=1,nf
rad=r1/(1d0-f(i))**(1d0/3d0)
m=dcmplx(e1(l,ilam),-e2(l,ilam))
wvno=2d0*3.1415926536/lam(ilam)
if(f(i).eq.0d0) then
spheres=1
goto 20
endif
if(r1*wvno.gt.10000d0) then
c if(i.eq.1) then
c print*,'Particle too large for hollow spheres'
c print*,'Using homogeneous spheres (r=',r1,', lam=',lam(ilam),')'
c endif
toolarge=1
goto 20
endif
c print*,'Using hollow spheres'
if(meth(1:3).eq.'DHS') then
rcore=rad*f(i)**(1d0/3d0)
call DMiLay(RCORE, rad, WVNO, m, min, MU,
& NA/2, QEXT, QSCA, QBS, GQSC,
& M1, M2, S21, D21, NA ,Err)
else
rcore=rad*0.999
call DMiLay(RCORE, rad, WVNO, min, m, MU,
& NA/2, QEXT, QSCA, QBS, GQSC,
& M1, M2, S21, D21, NA ,Err)
endif
c if(Err.eq.1) then
c print*,'Error in hollow spheres'
c print*,'Using homogeneous spheres (r=',r1,', lam=',lam(ilam),')'
c endif
20 if(Err.eq.1.or.spheres.eq.1.or.toolarge.eq.1) then
rad=r1
rcore=rad
rmie=rad
lmie=lam(ilam)
e1mie=e1(l,ilam)
e2mie=e2(l,ilam)
if(Err.eq.1.or.i.eq.1) then
if(rmie/lmie.lt.5000d0) then
call MeerhoffMie(rmie,lmie,e1mie,e2mie,csmie,cemie
& ,Mief11,Mief12,Mief33,Mief34,na)
else
call MeerhoffMie(rmie,rmie/5000d0,e1mie,e2mie,csmie,cemie
& ,Mief11,Mief12,Mief33,Mief34,na)
endif
endif
Mief22=Mief11
Mief44=Mief33
else
cemie=qext*pi*rad**2
csmie=qsca*pi*rad**2
do j=1,na/2
Mief11(j)=(M2(j,1)+M1(j,1))/csmie/wvno**2*2d0*pi
Mief12(j)=(M2(j,1)-M1(j,1))/csmie/wvno**2*2d0*pi
Mief22(j)=(M2(j,1)+M1(j,1))/csmie/wvno**2*2d0*pi
Mief33(j)=(S21(j,1))/csmie/wvno**2*2d0*pi
Mief34(j)=(-D21(j,1))/csmie/wvno**2*2d0*pi
Mief44(j)=(S21(j,1))/csmie/wvno**2*2d0*pi
Mief11(na-j+1)=(M2(j,2)+M1(j,2))/csmie/wvno**2*2d0*pi
Mief12(na-j+1)=(M2(j,2)-M1(j,2))/csmie/wvno**2*2d0*pi
Mief22(na-j+1)=(M2(j,2)+M1(j,2))/csmie/wvno**2*2d0*pi
Mief33(na-j+1)=(S21(j,2))/csmie/wvno**2*2d0*pi
Mief34(na-j+1)=(-D21(j,2))/csmie/wvno**2*2d0*pi
Mief44(na-j+1)=(S21(j,2))/csmie/wvno**2*2d0*pi
enddo
endif
c make sure the scattering matrix is properly normalized by adjusting the forward peak.
tot=0d0
tot2=0d0
do j=1,na
tot=tot+Mief11(j)*sin(pi*(real(j)-0.5)/real(na))
tot2=tot2+sin(pi*(real(j)-0.5)/real(na))
enddo
Mief11(1)=Mief11(1)+(tot2-tot)/sin(pi*(0.5)/real(na))
if(Mief11(1).lt.0d0) Mief11(1)=0d0
do j=1,na
f11(ilam,j)=f11(ilam,j)+wf(i)*nr(l,k)*Mief11(j)*csmie
f12(ilam,j)=f12(ilam,j)+wf(i)*nr(l,k)*Mief12(j)*csmie
f22(ilam,j)=f22(ilam,j)+wf(i)*nr(l,k)*Mief22(j)*csmie
f33(ilam,j)=f33(ilam,j)+wf(i)*nr(l,k)*Mief33(j)*csmie
f34(ilam,j)=f34(ilam,j)+wf(i)*nr(l,k)*Mief34(j)*csmie
f44(ilam,j)=f44(ilam,j)+wf(i)*nr(l,k)*Mief44(j)*csmie
enddo
cext=cext+wf(i)*nr(l,k)*cemie
csca=csca+wf(i)*nr(l,k)*csmie
cabs=cabs+wf(i)*nr(l,k)*(cemie-csmie)
Mass=Mass+wf(i)*nr(l,k)*rho(l)*4d0*pi*r1**3/3d0
Vol=Vol+wf(i)*nr(l,k)*4d0*pi*r1**3/3d0
enddo
enddo
10 continue
enddo
p%rho(iT)=Mass/Vol
rho_av=Mass/Vol
p%Kabs(isize,iT,ilam)=1d4*cabs/Mass
p%Ksca(isize,iT,ilam)=1d4*csca/Mass
p%Kext(isize,iT,ilam)=1d4*cext/Mass
p%F(isize,iT,ilam)%F11(1:180)=f11(ilam,1:180)/csca
p%F(isize,iT,ilam)%F12(1:180)=f12(ilam,1:180)/csca
p%F(isize,iT,ilam)%F22(1:180)=f22(ilam,1:180)/csca
p%F(isize,iT,ilam)%F33(1:180)=f33(ilam,1:180)/csca
p%F(isize,iT,ilam)%F34(1:180)=f34(ilam,1:180)/csca
p%F(isize,iT,ilam)%F44(1:180)=f44(ilam,1:180)/csca
enddo
if(p%standard.eq.'FILE') then
open(unit=30,file=input,RECL=5000)
call ignorestar(30)
read(30,*) ns
call ignorestar(30)
read(30,*) nf
nm=1
3 call ignorestar(30)
read(30,*,end=4) filename(nm)
call ignorestar(30)
read(30,*) frac(nm),rho(nm)
call ignorestar(30)
c change this to the input abundances if they are set
if(p%inp_abun(nm).ge.0d0) then
frac(nm)=p%inp_abun(nm)
endif
c changed this to mass fractions (11-05-2010)
frac(nm)=frac(nm)/rho(nm)
nm=nm+1
goto 3
4 nm=nm-1
close(unit=30)
else if(p%standard.eq.'DIANA') then
ns=1
nf=20
nm=2
rho(1)=3.01
rho(2)=1.80
frac(1)=(1d0-p%fcarbon)/rho(1)
frac(2)=p%fcarbon/rho(2)
allocate(e1d(nlam))
allocate(e2d(nlam))
filename(1)='Mg07Fe03SiO3_Dorschner1995'
call RegridDataLNK(Mg07Fe03SiO3_Dorschner1995,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(1,1:nlam)=e1d(1:nlam)
e2(1,1:nlam)=e2d(1:nlam)
filename(2)='Carbon_BE_Zubko1996'
call RegridDataLNK(Carbon_BE_Zubko1996,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(2,1:nlam)=e1d(1:nlam)
e2(2,1:nlam)=e2d(1:nlam)
deallocate(e1d)
deallocate(e2d)
else if(p%standard.eq.'DSHARP') then
input='DSHARP'
ns=p%nsubgrains
nf=1
maxf=0e0
nm=4
p%porosity=0d0
p%blend=.true.
rho(1)=0.92
rho(2)=3.30
rho(3)=4.83
rho(4)=1.50
frac(1)=0.3642
frac(2)=0.1670
frac(3)=0.0258
frac(4)=0.4430
allocate(e1d(nlam))
allocate(e2d(nlam))
filename(1)='Ice_Warren2008'
call RegridDataLNK(Ice_Warren2008,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(1,1:nlam)=e1d(1:nlam)
e2(1,1:nlam)=e2d(1:nlam)
filename(2)='AstroSilicate'
call RegridDataLNK(AstroSilicate,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(2,1:nlam)=e1d(1:nlam)
e2(2,1:nlam)=e2d(1:nlam)
filename(3)='FeS'
call RegridDataLNK(FeS,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(3,1:nlam)=e1d(1:nlam)
e2(3,1:nlam)=e2d(1:nlam)
filename(4)='OrganicsHenning'
call RegridDataLNK(OrganicsHenning,lam(1:nlam),e1d(1:nlam),e2d(1:nlam),nlam,.true.)
e1(4,1:nlam)=e1d(1:nlam)
e2(4,1:nlam)=e2d(1:nlam)
deallocate(e1d)
deallocate(e2d)
endif
tot=0d0
do i=1,nm
tot=tot+frac(i)
enddo
frac=frac/tot
call ParticleFITS(p,r,nr(1:nm,1:ns),nm,ns,rho_av,ii,amin,amax,p%apow,p%fmax,p%blend,p%porosity,frac,rho,filename,isize,iT)
deallocate(e1)
deallocate(e2)
deallocate(Mief11)
deallocate(Mief12)
deallocate(Mief22)
deallocate(Mief33)
deallocate(Mief34)
deallocate(Mief44)
deallocate(mu)
deallocate(M1)
deallocate(M2)
deallocate(S21)
deallocate(D21)
deallocate(frac)
deallocate(rho)
deallocate(f11)
deallocate(f12)
deallocate(f22)
deallocate(f33)
deallocate(f34)
deallocate(f44)
deallocate(r)
deallocate(nr)
deallocate(f)
deallocate(wf)
return
end
c-----------------------------------------------------------------------
c-----------------------------------------------------------------------
logical function checkparticlefile(partfile,amin,amax,apow,ns,fmax,blend,porosity,frac,rho,nm,filename,checkfrac)
IMPLICIT NONE
integer nm,ns
character*500 partfile,filename(nm),filetest
real*8 amin,amax,apow,fmax,porosity
real frac(nm),rho(nm)
character*6 word
character*14 key1,key2
character*80 comment,errmessage
logical blend,checkfrac
integer*4 :: status,readwrite,unit,blocksize,nfound,group
integer*4 :: firstpix,nbuffer,npixels,hdunum,hdutype
real*8 :: nullval
logical*4 :: anynull
integer*4, dimension(4) :: naxes
real x
integer i,j,ia,iopac,iread,nl_read
! Get an unused Logical Unit Number to use to open the FITS file.
status=0
checkparticlefile=.true.
call ftgiou (unit,status)
! Open file
readwrite=0
call ftopen(unit,partfile,readwrite,blocksize,status)
if (status /= 0) then
checkparticlefile=.false.
goto 1
endif
group=1
firstpix=1
nullval=-999
call output("Checking particle file: " // trim(partfile) )
!------------------------------------------------------------------------
! HDU0 : opacities
!------------------------------------------------------------------------
! Check dimensions
call ftgknj(unit,'NAXIS',1,2,naxes,nfound,status)
nl_read = naxes(1)
npixels=naxes(1)*naxes(2)
! Read model info
call ftgkye(unit,'r_min',x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(amin)
if(key1.ne.key2.or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
call ftgkye(unit,'r_max',x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(amax)
if(key1.ne.key2.or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
call ftgkye(unit,'r_pow',x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(apow)
if(ns.gt.1.and.(key1.ne.key2.or.status.ne.0)) then
checkparticlefile=.false.
goto 1
endif
call ftgkye(unit,'f_max',x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(fmax)
if(key1.ne.key2.or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
if(blend) then
call ftgkye(unit,'porosity',x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(porosity)
if(key1.ne.key2.or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
endif
if(checkfrac) then
do i=1,nm
write(word,'("frac",i0.2)') i
call ftgkye(unit,word,x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(frac(i))
if(key1.ne.key2.or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
enddo
endif
do i=1,nm
write(word,'("rho",i0.2)') i
call ftgkye(unit,word,x,comment,status)
write(key1,'(e14.8)') x
write(key2,'(e14.8)') real(rho(i))
if(key1.ne.key2.or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
enddo
do i=1,nm
write(word,'("file",i0.2)') i
call ftgkys(unit,word,filetest,comment,status)
if(trim(filetest).ne.trim(filename(i)).or.status.ne.0) then
checkparticlefile=.false.
goto 1
endif
enddo
1 continue
! Close the file and free the unit number.
call ftclos(unit, status)
call ftfiou(unit, status)
return
end
subroutine ParticleFITS(p,r,nr,nm,na,rho_av,ii,amin,amax,apow,fmax,blend,porosity,frac,rho,lnkfiles,isize,iT)
use GlobalSetup
use Constants
IMPLICIT NONE
character*500 lnkfiles(nm)
real*8 amin,amax,apow,fmax,porosity
real frac(nm),rho(nm)
logical blend
character*6 word
type(particle) p
integer nm,na,i,j,ii,iT,nm2,isize
real r(na),nr(nm,na)
real a0,a1,a2,a3,rho_av,rmin,rmax
real*8,allocatable :: array(:,:,:)
integer status,unit,blocksize,bitpix,naxis,naxes(3)
integer group,fpixel,nelements
logical simple,extend,truefalse
character*500 filename
filename=trim(particledir) // "particle"
& // trim(int2string(ii,'(i0.4)')) // "_" // trim(int2string(isize,'(i0.4)'))
& // "_" // trim(int2string(iT,'(i0.4)'))
if(abun_in_name.gt.0) then
do i=1,nm
select case(abun_in_name)
case(1)
filename=trim(filename) // "_f" // trim(dbl2string(1d0*frac(i),'(f3.1)'))
case(2)
filename=trim(filename) // "_f" // trim(dbl2string(1d0*frac(i),'(f4.2)'))
case(3)
filename=trim(filename) // "_f" // trim(dbl2string(1d0*frac(i),'(f5.3)'))
case(4)
filename=trim(filename) // "_f" // trim(dbl2string(1d0*frac(i),'(f6.4)'))
case default
filename=trim(filename) // "_f" // trim(dbl2string(1d0*frac(i),'(f7.5)'))
end select
enddo
endif
filename=trim(filename) // ".fits.gz"
inquire(file=filename,exist=truefalse)
if(truefalse) then
call output("FITS file already exists, overwriting")
open(unit=90,file=filename)
close(unit=90,status='delete')
endif
a0=0d0
a1=0d0
a2=0d0
a3=0d0
rmin=r(1)
rmax=r(1)
nm2=nm
if(blend) nm2=1
do i=1,nm2
do j=1,na
a0=a0+nr(i,j)
a1=a1+nr(i,j)*r(j)
a2=a2+nr(i,j)*r(j)**2
a3=a3+nr(i,j)*r(j)**3
if(r(j).lt.rmin) rmin=r(j)
if(r(j).gt.rmax) rmax=r(j)
enddo
enddo
a1=a1/a0
a2=a2/a0
a3=a3/a0
p%dust_moment1=a1
p%dust_moment2=a2
p%dust_moment3=a3
p%rv(isize)=sqrt(p%dust_moment2)*1d-4
p%rvmin=amin*1d-4
p%rvmax=amax*1d-4
status=0
C Get an unused Logical Unit Number to use to create the FITS file
call ftgiou(unit,status)
C create the new empty FITS file
blocksize=1
call ftinit(unit,filename,blocksize,status)
simple=.true.
extend=.true.
group=1
fpixel=1
bitpix=-64
naxis=2
naxes(1)=nlam
naxes(2)=4
nelements=naxes(1)*naxes(2)
allocate(array(nlam,4,1))
! Write the required header keywords.
call ftphpr(unit,simple,bitpix,naxis,naxes,0,1,extend,status)
! Write optional keywords to the header
call ftpkye(unit,'r_min',real(amin),8,'[micron]',status)
call ftpkye(unit,'r_max',real(amax),8,'[micron]',status)
call ftpkye(unit,'r_pow',real(apow),8,'',status)
call ftpkye(unit,'f_max',real(fmax),8,'',status)
call ftpkye(unit,'a1',real(a1),8,'[micron]',status)
call ftpkye(unit,'a2',real(a2),8,'[micron^2]',status)
call ftpkye(unit,'a3',real(a3),8,'[micron^3]',status)
call ftpkye(unit,'density',real(rho_av),8,'[g/cm^3]',status)
if(blend) call ftpkye(unit,'porosity',real(porosity),8,'[g/cm^3]',status)
do i=1,nm
write(word,'("file",i0.2)') i
call ftpkys(unit,word,trim(lnkfiles(i)),'',status)
enddo
do i=1,nm
write(word,'("frac",i0.2)') i
call ftpkye(unit,word,real(frac(i)),8,'[volume fraction]',status)
enddo
do i=1,nm
write(word,'("rho",i0.2)') i
call ftpkye(unit,word,real(rho(i)),8,'[g/cm^3]',status)
enddo
call ftpkyj(unit,'n_radii',na,' ',status)
call ftpkyj(unit,'n_mat',nm,' ',status)
! Write the array to the FITS file.
!------------------------------------------------------------------------------
! HDU 0: opacities
!------------------------------------------------------------------------------
do i=1,nlam
array(i,1,1)=lam(i)
array(i,2,1)=p%Kext(isize,iT,i)
array(i,3,1)=p%Kabs(isize,iT,i)
array(i,4,1)=p%Ksca(isize,iT,i)
enddo
call ftpprd(unit,group,fpixel,nelements,array(1:nlam,1:4,1),status)
deallocate(array)
!------------------------------------------------------------------------------
! HDU 1: Temperature
!------------------------------------------------------------------------------
bitpix=-64
naxis=3
naxes(1)=nlam
naxes(2)=6
naxes(3)=180
nelements=naxes(1)*naxes(2)*naxes(3)
allocate(array(nlam,6,180))
! create new hdu
call ftcrhd(unit, status)
! Write the required header keywords.
call ftphpr(unit,simple,bitpix,naxis,naxes,0,1,extend,status)
do i=1,nlam
do j=1,180
array(i,1,j)=p%F(isize,iT,i)%F11(j)
array(i,2,j)=p%F(isize,iT,i)%F12(j)
array(i,3,j)=p%F(isize,iT,i)%F22(j)
array(i,4,j)=p%F(isize,iT,i)%F33(j)
array(i,5,j)=p%F(isize,iT,i)%F34(j)
array(i,6,j)=p%F(isize,iT,i)%F44(j)
enddo
enddo
! Write the array to the FITS file.
call ftpprd(unit,group,fpixel,nelements,array,status)
deallocate(array)
! Close the file and free the unit number.
call ftclos(unit, status)
call ftfiou(unit, status)
! Check for any error, and if so print out error messages
if (status.gt.0) then
call output('error in export to fits file' // int2string(status,'(i6)'))
end if
return
end
c-----------------------------------------------------------------------
c-----------------------------------------------------------------------
subroutine readrefindCP(input,grid,e1,e2,n,loglog)
IMPLICIT NONE
real*8 grid(n)
real e1(n),e2(n),x0,y01,y02,x1,y11,y12,wp,gamma
complex*16 m0,m1,m
integer i,j,n
character*500 input
logical loglog
open(unit=20,file=input)
i=1
1 read(20,*,end=102,err=1) x0,y01,y02
if(y02.lt.1d-8) y02=1d-8
wp=(1d0-y01)/x0**2
gamma=y02/x0**3
103 if(x0.ge.grid(i)) then
e1(i)=1d0-wp*grid(i)**2
e2(i)=gamma*grid(i)**3
e1(i)=y01
e2(i)=y02
i=i+1
goto 103
endif
100 read(20,*,end=102) x1,y11,y12
if(y12.lt.1d-8) y12=1d-8
101 if(grid(i).le.x1.and.grid(i).gt.x0) then
e1(i)=10d0**(log10(y11)+(log10(grid(i)/x1))*(log10(y01/y11))/(log10(x0/x1)))
e2(i)=10d0**(log10(y12)+(log10(grid(i)/x1))*(log10(y02/y12))/(log10(x0/x1)))
i=i+1
if(i.gt.n) goto 102
goto 101
endif
x0=x1
y01=y11
y02=y12
goto 100
102 continue
if(loglog) then
m0=dcmplx(e1(i-1),e2(i-1))
if(abs(m0).gt.2d0.and..false.) then
c don't use the conducting extrapolation since it is not very accurate
do j=i,n
m=m0*sqrt(grid(j)/grid(i-1))
e1(j)=real(m)
e2(j)=dimag(m)
enddo
else
c use loglog extrapolation
m0=dcmplx(e1(i-2),e2(i-2))
m1=dcmplx(e1(i-1),e2(i-1))
c GFORTRAN intrinsic function log10 does not like complex numbers, so do it by "hand"
do j=i,n
m=10d0**(log(m0)/log(10.d0)+log(m1/m0)/log(10.d0)*log10(grid(i-2)/grid(j))/log10(grid(i-2)/grid(i-1)))
e1(j)=real(m)
e2(j)=dimag(m)
e1(j)=10d0**(log10(e1(i-2))+log10(e1(i-1)/e1(i-2))*log10(grid(i-2)/grid(j))/log10(grid(i-2)/grid(i-1)))
e2(j)=10d0**(log10(e2(i-2))+log10(e2(i-1)/e2(i-2))*log10(grid(i-2)/grid(j))/log10(grid(i-2)/grid(i-1)))
enddo
endif
else
c use the dielectric extrapolation, this is the default
do j=i,n
e1(j)=e1(i-1)
e2(j)=e2(i-1)*grid(i-1)/grid(j)
enddo
endif
close(unit=20)
return
end
c-----------------------------------------------------------------------
c-----------------------------------------------------------------------
subroutine Blender(e1in,e2in,abun,nm,e1out,e2out)
IMPLICIT NONE
integer nm,j,iter
real e1in(nm),e2in(nm),e1out,e2out,abun(nm)
complex*16 mm,m(nm),me,sum
mm=dcmplx(1d0,0d0)
do j=1,nm
m(j)=dcmplx(e1in(j),e2in(j))
enddo
do iter=1,100
sum=0d0
do j=1,nm
sum=sum+((m(j)**2-mm**2)/(m(j)**2+2d0*mm**2))*abun(j)
enddo
me=(2d0*sum+1d0)/(1d0-sum)
me=mm*cdsqrt(me)
mm=me
enddo
e1out=dreal(me)