forked from lynch829/Monte-Carlo-Radiation-Transport-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tauint2.f
290 lines (258 loc) · 9.05 KB
/
tauint2.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
subroutine tauint2(xp,yp,zp,nxp,nyp,nzp,xmax,ymax,zmax,Rr,Tr,kappa
+ ,pflag,xface,yface,zface,rhokap,phi,pi,zbins,twopi,
+ xcell,ycell,zcell,tflag,iseed,delta,rbins,Amat,jmean,numberrun
+ ,weight,sint,n2,n1,cost,sflag,xcur,ycur,cosp,sinp,abins,td_ang
+ ,dda,rd_ang,ddr,zcur,noise,cnt)
implicit none
include 'grid.txt'
integer tflag,iseed,xcell,ycell,zcell,sflag,numberrun,cnt
real*8 xp,yp,zp,nxp,nyp,nzp,xmax,ymax,zmax,phi,ddr,Rr(0:rbins-1)
real*8 flu,n1,n2,sint,cost,weight,kappa,dx,dy,dz,smax,delta,cosp
real ran2
integer celli,cellj,cellk,rbins,pflag,zbins,rcell,fflag,abins
real*8 tau,taurun,taucell,d,d1,dcell,xcur,ycur,zcur,dsx,dsy,dsz
real*8 Tr(0:rbins-1),Amat(0:rbins-1,0:zbins),pi,twopi,dda,sinp
real*8 td_ang(0:rbins-1,0:abins),taue,rd_ang(0:rbins-1,0:abins)
real*8 noise(1:cnt,1:cnt)
! pflag=1
! if(pflag.eq.0) then
! taue=kappa*2.*zmax
! tau=-log(1-ran2(iseed)*(1-exp(-taue)))
! pflag=1
! else
c***** tflag=0 means photon is in envelope
tflag=0
c**** generate random optical depth tau
tau=-alog(ran2(iseed))
! end if
c***** set the cumulative distance and optical depth (d and taurun)
c***** along the photon path to zero. set the current photon coordinates.
c***** note that the origin of the (xcur,ycur,zcur) system is at the
c***** bottom corner of the grid.
taurun=0.
xcur=xp+xmax
ycur=yp+ymax
zcur=zp+zmax
345 continue
if(fflag.eq.1)then
xp=xcur-xmax
yp=ycur-ymax
zp=zcur-zmax
xcell=int(nxg*(xp+xmax)/(2.*xmax))+1
ycell=int(nyg*(yp+ymax)/(2.*ymax))+1
zcell=int(nzg*(zp+zmax)/(2.*zmax))+1
fflag=0
end if
if (cellk.lt.1 )cellk=1
celli=xcell
cellj=ycell
cellk=zcell
d=0.
c***** calculate smax -- maximum distance photon can travel
if(nxp.gt.0.) then
dsx=(2.*xmax-xcur)/nxp
elseif(nxp.lt.0.) then
dsx=-xcur/nxp
elseif(nxp.eq.0.) then
dsx=1.e2*xmax
endif
if(nyp.gt.0.) then
dsy=(2.*ymax-ycur)/nyp
elseif(nyp.lt.0.) then
dsy=-ycur/nyp
elseif(nyp.eq.0.) then
dsy=1.e2*ymax
endif
if(nzp.gt.0.) then
dsz=(2.*zmax-zcur)/nzp
elseif(nzp.lt.0.) then
dsz=-zcur/nzp
elseif(nzp.eq.0.) then
dsz=1.e2*zmax
endif
smax=amin1(dsx,dsy,dsz)
if(smax.lt.delta) then
tflag=1
return
endif
c***** integrate through grid
do while((taurun.lt.tau).and.(d.lt.(.999*smax)))
c***** find distance to next x, y, and z cell walls.
c***** note that dx is not the x-distance, but the actual distance along
c*****the direction of travel to the next x-face, and likewise for dy and dz.
if(nxp.gt.0.) then
dx=(xface(celli+1)-xcur)/nxp
if(dx.lt.delta) then
xcur=xface(celli+1)
celli=celli+1
dx=(xface(celli+1)-xcur)/nxp
endif
elseif(nxp.lt.0.) then
dx=(xface(celli)-xcur)/nxp
if(dx.lt.delta) then
xcur=xface(celli)
dx=(xface(celli-1)-xcur)/nxp
celli=celli-1
endif
elseif(nxp.eq.0.) then
dx=1.e2*xmax
endif
if(nyp.gt.0.) then
dy=(yface(cellj+1)-ycur)/nyp
if(dy.lt.delta) then
ycur=yface(cellj+1)
cellj=cellj+1
dy=(yface(cellj+1)-ycur)/nyp
endif
elseif(nyp.lt.0.) then
dy=(yface(cellj)-ycur)/nyp
if(dy.lt.delta) then
ycur=yface(cellj)
dy=(yface(cellj-1)-ycur)/nyp
cellj=cellj-1
endif
elseif(nyp.eq.0.) then
dy=1.e2*ymax
endif
if(nzp.gt.0.) then
if(cellk.lt.1)then
print*,'cellk < 2'
cellk=2
end if
dz=(zface(cellk+1)-zcur)/nzp
if(dz.lt.delta) then
zcur=zface(cellk+1)
cellk=cellk+1
dz=(zface(cellk+1)-zcur)/nzp
endif
elseif(nzp.lt.0.) then
if (cellk.gt.nzg+3) then
cellk=nzg+3
print *, 'cellk > 204'
end if
dz=(zface(cellk)-zcur)/nzp
if(dz.lt.delta) then
zcur=zface(cellk)
if(cellk.lt.2) then
cellk=2
print *, 'cellk < 2'
end if
dz=(zface(cellk-1)-zcur)/nzp
cellk=cellk-1
if(cellk.lt.1.) then
print *, 'cellk<1'
cellk=1
end if
endif
elseif(nzp.eq.0.) then
dz=1.e2*zmax
endif
c***** distances are only zero if photon is on cell wall. if it is
c***** on cell wall then set to arbitrary large distance, since we will
c***** in fact hit another wall
if( (dx.eq.0.) .or. ((abs(dx)).lt.(delta)) ) dx=1.e2*xmax
if( (dy.eq.0.) .or. ((abs(dy)).lt.(delta)) ) dy=1.e2*ymax
if( (dz.eq.0.) .or. ((abs(dz)).lt.(delta)) ) dz=1.e2*zmax
c***** find distance to next cell wall -- minimum of dx, dy, and dz
dcell=amin1(dx,dy,dz)
if(dcell.le.0.) then
print *,'tauint2: dcell < 0'
endif
if(dx.lt.0.) dcell=amin1(dy,dz)
if(dy.lt.0.) dcell=amin1(dx,dz)
if(dz.lt.0.) dcell=amin1(dx,dy)
c***** optical depth to next cell wall is
c***** taucell= (distance to cell)*(opacity of current cell)
taucell=dcell*rhokap(celli,cellj,cellk)
c***** if taurun+taucell>tau then scatter at distance d+d1.
c***** update photon position and cell.
c***** if taurun+taucell<tau then photon moves distance dcell
c***** (i.e. ends up on next cell wall) and update photon position
c***** and cell.
if((taurun+taucell).ge.tau) then
d1=(tau-taurun)/rhokap(celli,cellj,cellk)
d=d+d1
taurun=taurun+taucell
xcur=xcur+d1*nxp
ycur=ycur+d1*nyp
zcur=zcur+d1*nzp
! rcell=floor(sqrt(real(celli*celli)+real(cellj*cellj)))+1
! if(rcell.gt.204)rcell=204
jmean(celli,cellj,cellk) = jmean(celli,cellj,cellk) + d1
c*************** Linear Grid ************************
celli=int(nxg*xcur/(2.*xmax))+1
cellj=int(nyg*ycur/(2.*ymax))+1
cellk=int(nzg*zcur/(2.*zmax))+1
c****************************************************
else
d=d+dcell
taurun=taurun+taucell
xcur=xcur+dcell*nxp
ycur=ycur+dcell*nyp
zcur=zcur+dcell*nzp
! rcell=floor(sqrt(real(celli*celli)+real(cellj*cellj)))+1
! if(rcell.gt.204)rcell=204
jmean(celli,cellj,cellk) = jmean(celli,cellj,cellk) + dcell
c*************** Linear Grid ************************
celli=int(nxg*xcur/(2.*xmax))+1
cellj=int(nyg*ycur/(2.*ymax))+1
cellk=int(nzg*zcur/(2.*zmax))+1
c****************************************************
endif
end do
c***** calculate photon final position. if it escapes envelope then
c***** set tflag=1. if photon doesn't escape leave tflag=0 and update
c***** photon position.
if((d.ge.(.99999*smax))) then
!if photon exits outsides then wrap around.
if(xcur.gt.2.*xmax*.9999)then
xcur=1.E-7
fflag=1
goto 345
elseif(xcur.lt.1E-7)then
xcur=2.*xmax*.999
fflag=1
goto 345
elseif(ycur.gt.2.*ymax*.9999)then
ycur=0.
fflag=1
goto 345
elseif(ycur.lt.1E-7)then
ycur=2.*ymax*.999
fflag=1
goto 345
end if
!update postion and cell numbers
xp=xcur-xmax
yp=ycur-ymax
zp=zcur-zmax
xcell=int(nxg*(xp+xmax)/(2.*xmax))+1
ycell=int(nyg*(yp+ymax)/(2.*ymax))+1
zcell=int(nzg*(zp+zmax)/(2.*zmax))+1
if((zcur.ge..9999999*2.*zmax).or.(zcur.le.1.0E-8)) then
call fresnel(sint,cost,sinp,cosp,nxp,nyp,nzp,tflag,
+ iseed,n1,n2,xp,yp,zp,xcur,ycur,Rr,ddr,weight,dda,noise,
+ rd_ang,xmax,ymax,zmax,zcur,rbins,Tr,sflag,td_ang,abins
+ ,cnt)
if(tflag.eq.1)then
!if photon escapes
goto 346
else
!if photon reflects
fflag=1
goto 345
end if
else
tflag=1
end if
else
xp=xp+d*nxp
yp=yp+d*nyp
zp=zp+d*nzp
xcell=int(nxg*(xp+xmax)/(2.*xmax))+1
ycell=int(nyg*(yp+ymax)/(2.*ymax))+1
zcell=int(nzg*(zp+zmax)/(2.*zmax))+1
endif
346 continue
return
end