-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwater.f90
599 lines (382 loc) · 16.9 KB
/
water.f90
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
! Last change: MG 11 Apr 2003 2:18 pm
SUBROUTINE water(lam,action)
! Purpose:
! --------
! To trace the path taken by a photon within a fluid medium
!
! Description
! -----------
! this subroutine will trace the path of a single photon
! through the medium. At each interaction (with a
! boundary or a constituent of the medium), the x,y and z
! position of the photon is tracked.
!
! Record and revisions
! --------------------
! Date Programmer Description of change
! ==== ========== =====================
! 3/12/2003 Manuel Gimond Original code
USE rand_global
USE randmod
USE math_global
USE const_global
USE anglesum_mod
USE propagation_global
USE physical_global
USE log_global
IMPLICIT NONE
INTEGER,INTENT(IN) :: lam ! Wavelength
INTEGER,INTENT(OUT) :: action ! Type of interaction
INTEGER :: optlayer ! Optical layer within which the photon is traveling
INTEGER :: layer ! Layer interface
INTEGER :: ii
INTEGER :: switch ! Status at air-water interface (not needed in this sub)
INTEGER :: coptlayer !actual optical layer location of photon
CHARACTER(LEN=11) :: state
REAL :: op ! Optical pathlength between each event.
! (In this subroutine, an event can be (1) an
! interaction with a physical boundary, (2)
! absorption, (3) scattering, or (4) a change in
! IOP of the layer)
REAL :: intx, inty ! x,y,z values of intersection with a boundary
REAL :: random ! Temporary palceholder for random number
REAL :: xb,yb ! point location of photon's interaction with bottom
REAL :: thetascat ! scattering angle determined from VSF
REAL :: phiscat ! scattering angle randomly generated
REAL :: xs,ys ! X,Y,Z location on the surface
REAL :: b,b2
REAL :: theta_orig
! initialize variables
action = 0
x(0)=x(1)
y(0)=y(1)
z(0)=z(1)
op = 0.0
layer = 1
optlayer = 1
coptlayer = 1
thetascat = 0.0
! The following will determine the max optical pathlength
! traveled by the photon before interacting with a particle.
op = LOG( rand() ) / atten ( lam, coptlayer ) * ( -1 )
! The following will randomly pick a distance along the max OP.
! the resulting location of the photon is then calculated. this
! is not part of the loop. it is for use only with the first
! appearance of the photon in the water body.
x(1) = x(0) + (op * SIN( theta ) * COS( phi ))
y(1) = y(0) + (op * SIN( theta ) * SIN( phi ))
z(1) = (op * COS( theta ) )
! The following loop tracks the photon until it is gone
! from the system. the position of the photon in space is
! also calculated.
! ********************* depth intervals **************************
! Has the photon crossed any of the depth intervals (markers)
! specified by the user? if so, the angles theta and phi and their
! associated optical pathlength will have to be recorded. Also, if
! the model is running in heterogeneous mode, the optical
! pathlength will have to be adjusted for the new inherent optical
! properties associated with that depth.
! ****************************************************************
PHOTONLIFE_LOOP: DO
state = 'attenuation' !Default
! Does the photon cross a layer of differing optical property
IF (numlycst > 1) THEN
IF ( theta >= PI/2.) THEN !Photon traveling up
OPTIC_LAYER_UP2: DO optlayer = numlycst ,2 ,-1
IF ( z(1) < dptlycst(optlayer) .AND. dptlycst(optlayer) < z(0) ) THEN
CALL olayer(lam,optlayer,op)
END IF
END DO OPTIC_LAYER_UP2
ELSE ! Photon traveling down
OPTIC_LAYER_DN2: DO optlayer = 1 , (numlycst - 1)
IF ( z(1) > dptlycst(optlayer+1) .AND. dptlycst(optlayer+1) > z(0) ) THEN
CALL olayer(lam,optlayer,op)
END IF
END DO OPTIC_LAYER_DN2
END IF
! Determine the location of the photon vis-a-vis the optical layers
DO ii = numlycst , 2, -1
IF ( z(1) > dptlycst(ii) ) THEN
coptlayer = ii
EXIT
END IF
END DO
END IF
! Check that all logging is conducted as needed
LAYER_LOOP: DO
! Downward traveling photon
LAYER_IF: IF ( z(1) > layval(layer) .AND. layval(layer)> z(0) ) THEN
b = ABS( layval( layer ) - z(0)) * TAN( theta )
intx = x(0) + b * COS(phi)
inty = y(0) + b * SIN(phi)
! Does the photon reach one of the side boundaries
IF ( (ABS( intx) > ( sideb / 2.)) .OR. (ABS( inty) > ( sideb / 2.)) ) THEN
state = 'side'
EXIT PHOTONLIFE_LOOP
END IF
! Now log photon crossing
CALL logbin(intx,inty,layer,lam)
IF (layer >= numlogly) THEN
IF (z(1) >= depthb) THEN
state = 'bottom'
END IF
EXIT LAYER_LOOP
END IF
layer = layer + 1
! -------------- Upward traveling photon -------------------------
ELSE IF ( (z(1) < layval(layer)) .AND. (layval(layer) <= z(0)) ) THEN
b = ABS( layval( layer ) - z(0)) * TAN(PI - theta)
intx = x(0) + b * COS(phi)
inty = y(0) + b * SIN(phi)
! Does the photon reach one of the side boundaries
IF ( (ABS( intx) > ( sideb / 2.)) .OR. (ABS( inty) > ( sideb / 2.)) ) THEN
state = 'side'
EXIT PHOTONLIFE_LOOP
END IF
! Now log photon crossing
IF (layer > 0 ) THEN
CALL logbin(intx,inty,layer,lam)
layer = layer - 1
ELSE ! layer == 0
state = 'surface'
EXIT LAYER_LOOP
END IF
! -------------- No more logging interfaces crossed ------------
ELSE
IF ( (ABS( x(1)) > ( sideb / 2.)) .OR. (ABS( y(1)) > ( sideb / 2.)) ) THEN
state = 'side'
END IF
IF (( theta >= PI/2.) .AND. (z(1) <= 0.) ) THEN
state = 'surface'
END IF
IF (( theta < PI/2.) .AND. (z(1) >= depthb) ) THEN
state = 'bottom'
END IF
EXIT LAYER_LOOP
END IF LAYER_IF
END DO LAYER_LOOP
! Does the photon reach the bottom
IF (z(1) > depthb) THEN
state = 'bottom'
END IF
! Sum the OP traveled by the photon
totalop = totalop + op
! ----------------------------------------------------------
! Select type of interaction between photon
! and medium/boundary
! ----------------------------------------------------------
SELECT CASE (state)
! ----------------------------------------------------------
! Bottom boundary is reached
! ----------------------------------------------------------
CASE ('bottom')
! Calculate x,y and z location where photon collides with bottom
b = ABS( depthb - z(0) ) * TAN( theta )
xb = x(0) + b * COS( phi )
yb = y(0) + b * SIN( phi )
! Calculate the remaining optical pathlength
op = op - SQRT( B**(2) + (depthb - z(0))**(2) )
! Determine outcome of photon's collision with bottom
IF ( (targbot == 1) .AND. ( ABS(xb) < targx/2.) .AND. &
( ABS(yb) < targy/2.) ) THEN
IF (rand() <= targref(lam) ) THEN !photon is reflected off target
IF (rand() > targspc(lam) ) THEN !photon is reflected isotropically
theta = ACOS( rand() * 2. - 1 ) / 2. + PI / 2. !new reflected angle
phi = anglesum(1, (rand() * 2 * PI) , 0.)
x(0) = xb
y(0) = yb
z(0) = depthb
x(1) = (op * SIN( theta ) * COS( phi )) + xb
y(1) = (op * SIN( theta ) * SIN( phi )) + yb
z(1) = depthb + (op * COS( theta ) )
ELSE ! Photon is reflected specularly
theta = PI - theta
x(0) = xb
y(0) = yb
z(0) = depthb
x(1) = (op * SIN( theta ) * COS( phi )) + xb
y(1) = (op * SIN( theta ) * SIN( phi )) + yb
z(1) = depthb + (op * COS( theta ) )
END IF
ELSE ! Photon is absorbed by target
x(1) = xb
y(1) = yb
z(1) = depthb
totalop = totalop - op ! Remove untraveled path from total OP
EXIT PHOTONLIFE_LOOP
END IF
ELSE
IF (rand() <= bottomr(lam) ) THEN !photon is reflected off bottom
IF (rand() > bottomspc(lam) ) THEN !photon is reflected isotropically
theta = ACOS( rand() * 2. - 1 ) / 2. + PI / 2. !new reflected angle
phi = anglesum(1, (rand() * 2 * PI) , 0.)
x(0) = xb
y(0) = yb
z(0) = depthb
x(1) = (op * SIN( theta ) * COS( phi )) + xb
y(1) = (op * SIN( theta ) * SIN( phi )) + yb
z(1) = depthb + (op * COS( theta ) )
ELSE ! Photon is reflected specularly
theta = PI - theta
x(0) = xb
y(0) = yb
z(0) = depthb
x(1) = (op * SIN( theta ) * COS( phi )) + xb
y(1) = (op * SIN( theta ) * SIN( phi )) + yb
z(1) = depthb + (op * COS( theta ) )
END IF
ELSE ! Photon is absorbed by bottom
x(1) = xb
y(1) = yb
z(1) = depthb
totalop = totalop - op ! Remove untraveled path from total OP
EXIT PHOTONLIFE_LOOP
END IF
END IF
! ----------------------------------------------------------
! Surface boundary is reached
! ----------------------------------------------------------
CASE ('surface')
b = ABS( z(0) ) * TAN( PI - theta )
xs = x(0) + b * COS( phi )
ys = y(0) + b * SIN( phi )
op = op - SQRT( z(0)**2 + b**2) ! remaining OP if photon remains in water
CALL logbin(x(1),y(1),0,lam) ! Log the photon as it 'hits' the interface
CALL interface_sub(refr(1),refr(0),switch)
IF (theta >= PI/2.) THEN !photon is not reflected back into the medium
z(1) = 0.0
x(1) = xs
y(1) = ys
CALL logbin(x(1),y(1),-1,lam)
totalop = totalop - op !Adjust total OP
EXIT PHOTONLIFE_LOOP !photon has left the medium
ELSE ! photon is reflected back into medium
b2 = (ABS( z(1) ) + z(0) ) * TAN( theta )
x(0)= xs
y(0)= ys
z(0)= 0.
x(1) = (b2 - b) * COS( phi ) + xs
y(1) = (b2 - b) * SIN( phi ) + ys
z(1)= (ABS(COS( theta )) * op )
CALL logbin(x(0),y(0),0,lam)
layer = 1
END IF
! ----------------------------------------------------------
! Side boundary is reached
! ----------------------------------------------------------
CASE ('side')
EXIT PHOTONLIFE_LOOP
! ----------------------------------------------------------
! Photon is absorbed or scattered by the medium
! ----------------------------------------------------------
CASE ('attenuation')
! Is the photon scattered or absorbed
IF (rand() <= salbedo(lam,coptlayer)) THEN
! ******************* scatter *******************
!
op = LOG(rand()) / atten(lam,coptlayer) * (-1) !Determine new optical pathlength
theta_orig = theta
CALL vsf(lam,thetascat,coptlayer) !Determine angle of scatter
!relative to incident direction.
IF (COS( theta_orig ) > 0.) THEN
IF(thetascat < (PI / 2.)) totalfwd(lam, layer - 1) = &
totalfwd(lam, layer - 1) + 1 !log % sf
IF(thetascat > (PI / 2.)) totalback(lam, layer - 1) = &
totalback(lam, layer - 1) + 1 !log % sb
ELSE
IF(thetascat < (PI / 2.)) totalfwd(lam,layer) = &
totalfwd(lam,layer) + 1 !log % bf
IF(thetascat > (PI / 2.)) totalback(lam,layer) = &
totalback(lam,layer) + 1 !log % bb
END IF
PHI_LOOP: DO
random = rand()
IF (random /= 1.0) THEN
phiscat = random * 2 * PI
EXIT PHI_LOOP
END IF
END DO PHI_LOOP
! Determine angle of scatter relative to the parent
! coordinate system.
IF( op > 0.0001) THEN ! If pathlength is very small,
! assume no movement from the photon
CALL geom2(thetascat,phiscat,op)
phi = anglesum(1, phi, 0.)
END IF
! Add the contribution of the scattering event to the shape factors
IF (COS( theta_orig ) > 0.) THEN
fdown( lam, layer - 1 ) = fdown( lam, layer -1 ) + 1
ELSE
fup( lam,layer ) = fup( lam,layer ) + 1
END IF
IF ((COS( theta ) * COS( theta_orig )) < 0) THEN
! Photon is traveling in the opposite stream
IF( COS( theta ) > 0 ) THEN
! Photon is scattered in downward direction
bup( lam,layer ) = bup( lam,layer ) + 1
ELSE
! Photon is scattered in upward direction
bdown( lam,layer - 1 )= bdown( lam,layer - 1 ) + 1
ENDIF
END IF
! Adjust layer location if photon trajectory switched from downward
! to upward
IF ( (theta_orig < ( PI / 2.)) .AND. (theta >= ( PI / 2.)) ) THEN
layer = layer - 1
END IF
IF ( (theta_orig >= ( PI / 2.)) .AND. (theta < ( PI / 2.)) ) THEN
layer = layer + 1
END IF
! ****************** absorb *********************
ELSE
action = 1
EXIT PHOTONLIFE_LOOP
! *************** END of all possible outcome ***
END IF
END SELECT
END DO PHOTONLIFE_LOOP
END SUBROUTINE water
! ********************************************************************
! ********************************************************************
!
! Subroutine to adjust the photon's trajectory in an optically
! different medium.
!
! ********************************************************************
! ********************************************************************
SUBROUTINE olayer(lam,optlayer, op)
USE rand_global
USE randmod
USE const_global
USE propagation_global
USE physical_global
IMPLICIT NONE
INTEGER,INTENT(IN) :: lam
INTEGER,INTENT(INOUT) :: optlayer
REAL,INTENT(INOUT) :: op
REAL :: opl1,opl2
! ******** for downwelling light ************************************
IF(z(1) > z(0))THEN
! Calculate the part of the OP traveled in the new optical layer
opl1 = ABS( dptlycst( optlayer +1) - z(1) ) / COS( theta )
! Now calculate the new OP for the new optical layer using that
! layer's IOP and the fraction of the original untraveled photon
opl2 = atten( lam , (optlayer) ) / atten(lam, (optlayer+1)) * opl1
op = (op - opl1) + opl2
! Recalculate the end-point of the photon's OP in the main
! Coordinate system
x(1) = x(0) + ( op * SIN( theta ) * COS( phi ))
y(1) = y(0) + ( op * SIN( theta ) * SIN( phi ))
z(1) = z(0) + ( op * COS( theta ))
! ******** for upwelling light ************************************
ELSE
! Calculate the coordinate where the photon interacts with the
! layer interface crossed
opl1 = ABS( dptlycst( optlayer ) - z(1)) / ABS(COS( theta ))
opl2 = atten( lam , (optlayer) ) / atten(lam, (optlayer-1)) * opl1
op = (op - opl1) + opl2
x(1) = x(0) + ( op * SIN( theta ) * COS( phi ))
y(1) = y(0) + ( op * SIN( theta ) * SIN( phi ))
z(1) = z(0) + ( op * COS( theta ))
END IF
END SUBROUTINE olayer