forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.F90
1432 lines (1157 loc) · 44.8 KB
/
geometry.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
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
module geometry
use constants
use datatypes, only: dict_get_key
use error, only: fatal_error
use geometry_header, only: Cell, Surface, Universe, Lattice
use global
use output, only: write_message
use particle_header, only: LocalCoord, deallocate_coord
use string, only: to_str
use tally, only: score_surface_current
implicit none
contains
!===============================================================================
! SIMPLE_CELL_CONTAINS determines whether a given the current coordinates of the
! particle are inside a cell defined as the intersection of a series of surfaces
!===============================================================================
function simple_cell_contains(c) result(in_cell)
type(Cell), pointer :: c
logical :: in_cell
integer :: i ! index of surfaces in cell
integer :: i_surface ! index in surfaces array (with sign)
logical :: specified_sense ! specified sense of surface in list
logical :: actual_sense ! sense of particle wrt surface
type(Surface), pointer :: s => null()
SURFACE_LOOP: do i = 1, c % n_surfaces
! Lookup surface
i_surface = c % surfaces(i)
! Check if the particle is currently on the specified surface
if (i_surface == p % surface) then
! Particle is heading into the cell
cycle
elseif (i_surface == -p % surface) then
! Particle is heading out of the cell
in_cell = .false.
return
end if
! Determine the specified sense of the surface in the cell and the actual
! sense of the particle with respect to the surface
s => surfaces(abs(i_surface))
actual_sense = sense(s)
specified_sense = (c % surfaces(i) > 0)
! Compare sense of point to specified sense
if (actual_sense .neqv. specified_sense) then
in_cell = .false.
return
end if
end do SURFACE_LOOP
! If we've reached here, then the sense matched on every surface
in_cell = .true.
end function simple_cell_contains
!===============================================================================
! FIND_CELL determines what cell a source particle is in within a particular
! universe. If the base universe is passed, the particle should be found as long
! as it's within the geometry
!===============================================================================
recursive subroutine find_cell(found, search_cells)
logical, intent(inout) :: found
integer, optional :: search_cells(:)
integer :: i ! index over cells
integer :: x ! x-index for lattice
integer :: y ! y-index for lattice
integer :: n ! number of cells to search
integer :: index_cell ! index in cells array
real(8) :: xyz(3) ! temporary location
logical :: use_search_cells ! use cells provided as argument
type(Cell), pointer :: c ! pointer to cell
type(Lattice), pointer :: lat ! pointer to lattice
type(Universe), pointer :: univ ! universe to search in
! Remove coordinates for any lower levels
call deallocate_coord(p % coord % next)
! set size of list to search
if (present(search_cells)) then
use_search_cells = .true.
n = size(search_cells)
else
use_search_cells = .false.
univ => universes(p % coord % universe)
n = univ % n_cells
end if
do i = 1, n
! select cells based on whether we are searching a universe or a provided
! list of cells (this would be for lists of neighbor cells)
if (use_search_cells) then
index_cell = search_cells(i)
! check to make sure search cell is in same universe
if (cells(index_cell) % universe /= p % coord % universe) cycle
else
index_cell = univ % cells(i)
end if
! get pointer to cell
c => cells(index_cell)
if (simple_cell_contains(c)) then
! Set cell on this level
p % coord % cell = index_cell
! Show cell information on trace
if (verbosity >= 10 .or. trace) then
message = " Entering cell " // trim(to_str(c % id))
call write_message()
end if
if (c % type == CELL_NORMAL) then
! ====================================================================
! AT LOWEST UNIVERSE, TERMINATE SEARCH
! set material
p % last_material = p % material
p % material = c % material
elseif (c % type == CELL_FILL) then
! ====================================================================
! CELL CONTAINS LOWER UNIVERSE, RECURSIVELY FIND CELL
! Create new level of coordinates
allocate(p % coord % next)
p % coord % next % xyz = p % coord % xyz
p % coord % next % uvw = p % coord % uvw
! Move particle to next level and set universe
p % coord => p % coord % next
p % coord % universe = c % fill
! Apply translation
if (allocated(c % translation)) then
p % coord % xyz = p % coord % xyz - c % translation
end if
! Apply rotation
if (allocated(c % rotation)) then
p % coord % xyz = matmul(c % rotation, p % coord % xyz)
p % coord % uvw = matmul(c % rotation, p % coord % uvw)
p % coord % rotated = .true.
end if
call find_cell(found)
if (.not. found) exit
elseif (c % type == CELL_LATTICE) then
! ====================================================================
! CELL CONTAINS LATTICE, RECURSIVELY FIND CELL
! Set current lattice
lat => lattices(c % fill)
! determine universe based on lattice position
xyz = p % coord % xyz + TINY_BIT * p % coord % uvw
x = ceiling((xyz(1) - lat % x0)/lat % width_x)
y = ceiling((xyz(2) - lat % y0)/lat % width_y)
! Check if lattice coordinates are within bounds
if (x < 1 .or. x > lat % n_x .or. &
y < 1 .or. y > lat % n_y) then
! This condition should only get hit in rare circumstances where a
! neutron hits the corner of a lattice. In this case, the neutron
! may need to be moved diagonally across the lattice. To do so, we
! remove all lower coordinate levels and then search from universe
! 0.
p % coord => p % coord0
call deallocate_coord(p % coord % next)
! Reset surface and advance particle a tiny bit
p % surface = NONE
p % coord % xyz = xyz
else
! Create new level of coordinates
allocate(p % coord % next)
! adjust local position of particle
p % coord % next % xyz(1) = p % coord % xyz(1) - &
(lat%x0 + (x-0.5_8)*lat%width_x)
p % coord % next % xyz(2) = p % coord % xyz(2) - &
(lat%y0 + (y-0.5_8)*lat%width_y)
p % coord % next % xyz(3) = p % coord % xyz(3)
p % coord % next % uvw = p % coord % uvw
! Move particle to next level
p % coord => p % coord % next
! set particle lattice indices
p % coord % lattice = c % fill
p % coord % lattice_x = x
p % coord % lattice_y = y
p % coord % universe = lat % element(x,y)
end if
call find_cell(found)
if (.not. found) exit
end if
! Found cell so we can return
found = .true.
return
end if
end do
found = .false.
end subroutine find_cell
!===============================================================================
! CROSS_SURFACE handles all surface crossings, whether the particle leaks out of
! the geometry, is reflected, or crosses into a new lattice or cell
!===============================================================================
subroutine cross_surface(last_cell)
integer, intent(in) :: last_cell ! last cell particle was in
real(8) :: x ! x-x0 for sphere
real(8) :: y ! y-y0 for sphere
real(8) :: z ! z-z0 for sphere
real(8) :: R ! radius of sphere
real(8) :: u ! x-component of direction
real(8) :: v ! y-component of direction
real(8) :: w ! z-component of direction
real(8) :: n1 ! x-component of surface normal
real(8) :: n2 ! y-component of surface normal
real(8) :: n3 ! z-component of surface normal
real(8) :: dot_prod ! dot product of direction and normal
real(8) :: norm ! "norm" of surface normal
logical :: found ! particle found in universe?
type(Surface), pointer :: surf => null()
surf => surfaces(abs(p % surface))
if (verbosity >= 10 .or. trace) then
message = " Crossing surface " // trim(to_str(surf % id))
call write_message()
end if
if (surf % bc == BC_VACUUM .and. (run_mode /= MODE_PLOTTING)) then
! =======================================================================
! PARTICLE LEAKS OUT OF PROBLEM
! Kill particle
p % alive = .false.
! Score any surface current tallies -- note that the particle is moved
! forward slightly so that if the mesh boundary is on the surface, it is
! still processed
if (associated(active_current_tallies)) then
! TODO: Find a better solution to score surface currents than
! physically moving the particle forward slightly
p % coord0 % xyz = p % coord0 % xyz + TINY_BIT * p % coord0 % uvw
call score_surface_current()
end if
! Score to global leakage tally
if (tallies_on) global_tallies(LEAKAGE) % value = &
global_tallies(LEAKAGE) % value + p % wgt
! Display message
if (verbosity >= 10 .or. trace) then
message = " Leaked out of surface " // trim(to_str(surf % id))
call write_message()
end if
return
elseif (surf % bc == BC_REFLECT .and. (run_mode /= MODE_PLOTTING)) then
! =======================================================================
! PARTICLE REFLECTS FROM SURFACE
! Do not handle reflective boundary conditions on lower universes
if (.not. associated(p % coord, p % coord0)) then
message = "Cannot reflect particle " // trim(to_str(p % id)) // &
" off surface in a lower universe."
call fatal_error()
end if
! Score surface currents since reflection causes the direction of the
! particle to change -- artificially move the particle slightly back in
! case the surface crossing in coincident with a mesh boundary
if (associated(active_current_tallies)) then
p % coord0 % xyz = p % coord0 % xyz - TINY_BIT * p % coord0 % uvw
call score_surface_current()
p % coord0 % xyz = p % coord0 % xyz + TINY_BIT * p % coord0 % uvw
end if
! Copy particle's direction cosines
u = p % coord0 % uvw(1)
v = p % coord0 % uvw(2)
w = p % coord0 % uvw(3)
select case (surf%type)
case (SURF_PX)
u = -u
case (SURF_PY)
v = -v
case (SURF_PZ)
w = -w
case (SURF_PLANE)
! Find surface coefficients and norm of vector normal to surface
n1 = surf % coeffs(1)
n2 = surf % coeffs(2)
n3 = surf % coeffs(3)
norm = n1*n1 + n2*n2 + n3*n3
dot_prod = u*n1 + v*n2 + w*n3
! Reflect direction according to normal
u = u - 2*dot_prod*n1/norm
v = v - 2*dot_prod*n2/norm
w = w - 2*dot_prod*n3/norm
case (SURF_CYL_X)
! Find y-y0, z-z0 and dot product of direction and surface normal
y = p % coord0 % xyz(2) - surf % coeffs(1)
z = p % coord0 % xyz(3) - surf % coeffs(2)
R = surf % coeffs(3)
dot_prod = v*y + w*z
! Reflect direction according to normal
v = v - 2*dot_prod*y/(R*R)
w = w - 2*dot_prod*z/(R*R)
case (SURF_CYL_Y)
! Find x-x0, z-z0 and dot product of direction and surface normal
x = p % coord0 % xyz(1) - surf % coeffs(1)
z = p % coord0 % xyz(3) - surf % coeffs(2)
R = surf % coeffs(3)
dot_prod = u*x + w*z
! Reflect direction according to normal
u = u - 2*dot_prod*x/(R*R)
w = w - 2*dot_prod*z/(R*R)
case (SURF_CYL_Z)
! Find x-x0, y-y0 and dot product of direction and surface normal
x = p % coord0 % xyz(1) - surf % coeffs(1)
y = p % coord0 % xyz(2) - surf % coeffs(2)
R = surf % coeffs(3)
dot_prod = u*x + v*y
! Reflect direction according to normal
u = u - 2*dot_prod*x/(R*R)
v = v - 2*dot_prod*y/(R*R)
case (SURF_SPHERE)
! Find x-x0, y-y0, z-z0 and dot product of direction and surface
! normal
x = p % coord0 % xyz(1) - surf % coeffs(1)
y = p % coord0 % xyz(2) - surf % coeffs(2)
z = p % coord0 % xyz(3) - surf % coeffs(3)
R = surf % coeffs(4)
dot_prod = u*x + v*y + w*z
! Reflect direction according to normal
u = u - 2*dot_prod*x/(R*R)
v = v - 2*dot_prod*y/(R*R)
w = w - 2*dot_prod*z/(R*R)
case (SURF_CONE_X)
! Find x-x0, y-y0, z-z0 and dot product of direction and surface
! normal
x = p % coord0 % xyz(1) - surf % coeffs(1)
y = p % coord0 % xyz(2) - surf % coeffs(2)
z = p % coord0 % xyz(3) - surf % coeffs(3)
R = surf % coeffs(4)
dot_prod = (v*y + w*z - R*u*x)/((R + ONE)*R*x*x)
! Reflect direction according to normal
u = u + 2*dot_prod*R*x
v = v - 2*dot_prod*y
w = w - 2*dot_prod*z
case (SURF_CONE_Y)
! Find x-x0, y-y0, z-z0 and dot product of direction and surface
! normal
x = p % coord0 % xyz(1) - surf % coeffs(1)
y = p % coord0 % xyz(2) - surf % coeffs(2)
z = p % coord0 % xyz(3) - surf % coeffs(3)
R = surf % coeffs(4)
dot_prod = (u*x + w*z - R*v*y)/((R + ONE)*R*y*y)
! Reflect direction according to normal
u = u - 2*dot_prod*x
v = v + 2*dot_prod*R*y
w = w - 2*dot_prod*z
case (SURF_CONE_Z)
! Find x-x0, y-y0, z-z0 and dot product of direction and surface
! normal
x = p % coord0 % xyz(1) - surf % coeffs(1)
y = p % coord0 % xyz(2) - surf % coeffs(2)
z = p % coord0 % xyz(3) - surf % coeffs(3)
R = surf % coeffs(4)
dot_prod = (u*x + v*y - R*w*z)/((R + ONE)*R*z*z)
! Reflect direction according to normal
u = u - 2*dot_prod*x
v = v - 2*dot_prod*y
w = w + 2*dot_prod*R*z
case default
message = "Reflection not supported for surface " // &
trim(to_str(surf % id))
call fatal_error()
end select
! Set new particle direction
p % coord0 % uvw = (/ u, v, w /)
! Reassign particle's cell and surface
p % coord0 % cell = last_cell
p % surface = -p % surface
! If a reflective surface is coincident with a lattice or universe
! boundary, it is necessary to redetermine the particle's coordinates in
! the lower universes.
if (associated(p % coord0 % next)) then
call deallocate_coord(p % coord0 % next)
call find_cell(found)
if (.not. found) then
message = "Couldn't find particle after reflecting from surface."
call fatal_error()
end if
end if
! Set previous coordinate going slightly past surface crossing
p % last_xyz = p % coord0 % xyz + TINY_BIT * p % coord0 % uvw
! Diagnostic message
if (verbosity >= 10 .or. trace) then
message = " Reflected from surface " // trim(to_str(surf%id))
call write_message()
end if
return
end if
! ==========================================================================
! SEARCH NEIGHBOR LISTS FOR NEXT CELL
if (p % surface > 0 .and. allocated(surf % neighbor_pos)) then
! If coming from negative side of surface, search all the neighboring
! cells on the positive side
call find_cell(found, surf % neighbor_pos)
if (found) return
elseif (p % surface < 0 .and. allocated(surf % neighbor_neg)) then
! If coming from positive side of surface, search all the neighboring
! cells on the negative side
call find_cell(found, surf % neighbor_neg)
if (found) return
end if
! ==========================================================================
! COULDN'T FIND PARTICLE IN NEIGHBORING CELLS, SEARCH ALL CELLS
! Remove lower coordinate levels and assignment of surface
p % surface = NONE
p % coord => p % coord0
call deallocate_coord(p % coord % next)
call find_cell(found)
if (run_mode /= MODE_PLOTTING .and. (.not. found)) then
! If a cell is still not found, there are two possible causes: 1) there is
! a void in the model, and 2) the particle hit a surface at a tangent. If
! the particle is really traveling tangent to a surface, if we move it
! forward a tiny bit it should fix the problem.
p % coord => p % coord0
call deallocate_coord(p % coord % next)
p % coord % xyz = p % coord % xyz + TINY_BIT * p % coord % uvw
call find_cell(found)
! Couldn't find next cell anywhere! This probably means there is an actual
! undefined region in the geometry.
if (.not. found) then
message = "After particle " // trim(to_str(p % id)) // " crossed surface " &
// trim(to_str(surfaces(abs(p%surface)) % id)) // " it could not be &
&located in any cell and it did not leak."
call fatal_error()
end if
end if
end subroutine cross_surface
!===============================================================================
! CROSS_LATTICE moves a particle into a new lattice element
!===============================================================================
subroutine cross_lattice(lattice_crossed)
integer, intent(in) :: lattice_crossed
integer :: i_x ! x index in lattice
integer :: i_y ! y index in lattice
real(8) :: x0 ! half the width of lattice element
real(8) :: y0 ! half the height of lattice element
logical :: found ! particle found in cell?
type(Lattice), pointer :: lat => null()
lat => lattices(p % coord % lattice)
if (verbosity >= 10 .or. trace) then
message = " Crossing lattice " // trim(to_str(lat % id)) // &
". Current position (" // trim(to_str(p % coord % lattice_x)) &
// "," // trim(to_str(p % coord % lattice_y)) // ")"
call write_message()
end if
if (lat % type == LATTICE_RECT) then
x0 = lat % width_x * 0.5_8
y0 = lat % width_y * 0.5_8
select case (lattice_crossed)
case (LATTICE_LEFT)
! Move particle to left element
p % coord % lattice_x = p % coord % lattice_x - 1
p % coord % xyz(1) = x0
case (LATTICE_RIGHT)
! Move particle to right element
p % coord % lattice_x = p % coord % lattice_x + 1
p % coord % xyz(1) = -x0
case (LATTICE_BOTTOM)
! Move particle to bottom element
p % coord % lattice_y = p % coord % lattice_y - 1
p % coord % xyz(2) = y0
case (LATTICE_TOP)
! Move particle to top element
p % coord % lattice_y = p % coord % lattice_y + 1
p % coord % xyz(2) = -y0
end select
elseif (lat % type == LATTICE_HEX) then
! TODO: Add hex lattice support
end if
! Check to make sure still in lattice
i_x = p % coord % lattice_x
i_y = p % coord % lattice_y
if (i_x < 1 .or. i_x > lat % n_x .or. i_y < 1 .or. i_y > lat % n_y) then
call deallocate_coord(p % coord0 % next)
p % coord => p % coord0
! Search for particle
call find_cell(found)
if (.not. found) then
message = "Could not locate particle " // trim(to_str(p % id)) // &
" after crossing a lattice boundary."
call fatal_error()
end if
else
! Find universe for next lattice element
p % coord % universe = lat % element(i_x, i_y)
! Find cell in next lattice element
call find_cell(found)
if (.not. found) then
! In some circumstances, a particle crossing the corner of a cell may not
! be able to be found in the next universe. In this scenario we cut off
! all lower-level coordinates and search from universe zero
! Remove lower coordinates
call deallocate_coord(p % coord0 % next)
p % coord => p % coord0
! Search for particle
call find_cell(found)
if (.not. found) then
message = "Could not locate particle " // trim(to_str(p % id)) // &
" after crossing a lattice boundary."
call fatal_error()
end if
end if
end if
end subroutine cross_lattice
!===============================================================================
! DISTANCE_TO_BOUNDARY calculates the distance to the nearest boundary for a
! particle 'p' traveling in a certain direction. For a cell in a subuniverse
! that has a parent cell, also include the surfaces of the edge of the universe.
!===============================================================================
subroutine distance_to_boundary(dist, surface_crossed, lattice_crossed)
real(8), intent(out) :: dist
integer, intent(out) :: surface_crossed
integer, intent(out) :: lattice_crossed
integer :: i ! index for surface in cell
integer :: index_surf ! index in surfaces array (with sign)
real(8) :: x,y,z ! particle coordinates
real(8) :: u,v,w ! particle directions
real(8) :: d ! evaluated distance
real(8) :: x0,y0,z0 ! coefficients for surface
real(8) :: r ! radius for quadratic surfaces
real(8) :: tmp ! dot product of surface normal with direction
real(8) :: a,b,c,k ! quadratic equation coefficients
real(8) :: quad ! discriminant of quadratic equation
logical :: on_surface ! is particle on surface?
type(Cell), pointer :: cl => null()
type(Surface), pointer :: surf => null()
type(Lattice), pointer :: lat => null()
type(LocalCoord), pointer :: coord => null()
type(LocalCoord), pointer :: final_coord => null()
! inialize distance to infinity (huge)
dist = INFINITY
lattice_crossed = NONE
nullify(final_coord)
! Get pointer to top-level coordinates
coord => p % coord0
! Loop over each universe level
LEVEL_LOOP: do while(associated(coord))
! get pointer to cell on this level
cl => cells(coord % cell)
! copy directional cosines
u = coord % uvw(1)
v = coord % uvw(2)
w = coord % uvw(3)
! =======================================================================
! FIND MINIMUM DISTANCE TO SURFACE IN THIS CELL
SURFACE_LOOP: do i = 1, cl % n_surfaces
! copy local coordinates of particle
x = coord % xyz(1)
y = coord % xyz(2)
z = coord % xyz(3)
! check for coincident surface -- note that we can't skip the
! calculation in general because a particle could be on one side of a
! cylinder and still have a positive distance to the other
index_surf = cl % surfaces(i)
if (index_surf == p % surface) then
on_surface = .true.
else
on_surface = .false.
end if
! check for operators
index_surf = abs(index_surf)
if (index_surf >= OP_DIFFERENCE) cycle
! get pointer to surface
surf => surfaces(index_surf)
select case (surf % type)
case (SURF_PX)
if (on_surface .or. u == ZERO) then
d = INFINITY
else
x0 = surf % coeffs(1)
d = (x0 - x)/u
if (d < ZERO) d = INFINITY
end if
case (SURF_PY)
if (on_surface .or. v == ZERO) then
d = INFINITY
else
y0 = surf % coeffs(1)
d = (y0 - y)/v
if (d < ZERO) d = INFINITY
end if
case (SURF_PZ)
if (on_surface .or. w == ZERO) then
d = INFINITY
else
z0 = surf % coeffs(1)
d = (z0 - z)/w
if (d < ZERO) d = INFINITY
end if
case (SURF_PLANE)
A = surf % coeffs(1)
B = surf % coeffs(2)
C = surf % coeffs(3)
D = surf % coeffs(4)
tmp = A*u + B*v + C*w
if (on_surface .or. tmp == ZERO) then
d = INFINITY
else
d = -(A*x + B*y + C*w - D)/tmp
if (d < ZERO) d = INFINITY
end if
case (SURF_CYL_X)
a = ONE - u*u ! v^2 + w^2
if (a == ZERO) then
d = INFINITY
else
y0 = surf % coeffs(1)
z0 = surf % coeffs(2)
r = surf % coeffs(3)
y = y - y0
z = z - z0
k = y*v + z*w
c = y*y + z*z - r*r
quad = k*k - a*c
if (quad < ZERO) then
! no intersection with cylinder
d = INFINITY
elseif (on_surface) then
! particle is on the cylinder, thus one distance is
! positive/negative and the other is zero. The sign of k
! determines if we are facing in or out
if (k >= ZERO) then
d = INFINITY
else
d = (-k + sqrt(quad))/a
end if
elseif (c < ZERO) then
! particle is inside the cylinder, thus one distance must be
! negative and one must be positive. The positive distance
! will be the one with negative sign on sqrt(quad)
d = (-k + sqrt(quad))/a
else
! particle is outside the cylinder, thus both distances are
! either positive or negative. If positive, the smaller
! distance is the one with positive sign on sqrt(quad)
d = (-k - sqrt(quad))/a
if (d < ZERO) d = INFINITY
end if
end if
case (SURF_CYL_Y)
a = ONE - v*v ! u^2 + w^2
if (a == ZERO) then
d = INFINITY
else
x0 = surf % coeffs(1)
z0 = surf % coeffs(2)
r = surf % coeffs(3)
x = x - x0
z = z - z0
k = x*u + z*w
c = x*x + z*z - r*r
quad = k*k - a*c
if (quad < ZERO) then
! no intersection with cylinder
d = INFINITY
elseif (on_surface) then
! particle is on the cylinder, thus one distance is
! positive/negative and the other is zero. The sign of k
! determines if we are facing in or out
if (k >= ZERO) then
d = INFINITY
else
d = (-k + sqrt(quad))/a
end if
elseif (c < ZERO) then
! particle is inside the cylinder, thus one distance must be
! negative and one must be positive. The positive distance
! will be the one with negative sign on sqrt(quad)
d = (-k + sqrt(quad))/a
else
! particle is outside the cylinder, thus both distances are
! either positive or negative. If positive, the smaller
! distance is the one with positive sign on sqrt(quad)
d = (-k - sqrt(quad))/a
if (d < ZERO) d = INFINITY
end if
end if
case (SURF_CYL_Z)
a = ONE - w*w ! u^2 + v^2
if (a == ZERO) then
d = INFINITY
else
x0 = surf % coeffs(1)
y0 = surf % coeffs(2)
r = surf % coeffs(3)
x = x - x0
y = y - y0
k = x*u + y*v
c = x*x + y*y - r*r
quad = k*k - a*c
if (quad < ZERO) then
! no intersection with cylinder
d = INFINITY
elseif (on_surface) then
! particle is on the cylinder, thus one distance is
! positive/negative and the other is zero. The sign of k
! determines if we are facing in or out
if (k >= ZERO) then
d = INFINITY
else
d = (-k + sqrt(quad))/a
end if
elseif (c < ZERO) then
! particle is inside the cylinder, thus one distance must be
! negative and one must be positive. The positive distance
! will be the one with negative sign on sqrt(quad)
d = (-k + sqrt(quad))/a
else
! particle is outside the cylinder, thus both distances are
! either positive or negative. If positive, the smaller
! distance is the one with positive sign on sqrt(quad)
d = (-k - sqrt(quad))/a
if (d <= ZERO) d = INFINITY
end if
end if
case (SURF_SPHERE)
x0 = surf % coeffs(1)
y0 = surf % coeffs(2)
z0 = surf % coeffs(3)
r = surf % coeffs(4)
x = x - x0
y = y - y0
z = z - z0
k = x*u + y*v + z*w
c = x*x + y*y + z*z - r*r
quad = k*k - c
if (quad < ZERO) then
! no intersection with sphere
d = INFINITY
elseif (on_surface) then
! particle is on the sphere, thus one distance is
! positive/negative and the other is zero. The sign of k
! determines if we are facing in or out
if (k >= ZERO) then
d = INFINITY
else
d = -k + sqrt(quad)
end if
elseif (c < ZERO) then
! particle is inside the sphere, thus one distance must be
! negative and one must be positive. The positive distance will
! be the one with negative sign on sqrt(quad)
d = -k + sqrt(quad)
else
! particle is outside the sphere, thus both distances are either
! positive or negative. If positive, the smaller distance is the
! one with positive sign on sqrt(quad)
d = -k - sqrt(quad)
if (d < ZERO) d = INFINITY
end if
case (SURF_CONE_X)
x0 = surf % coeffs(1)
y0 = surf % coeffs(2)
z0 = surf % coeffs(3)
r = surf % coeffs(4)
x = x - x0
y = y - y0
z = z - z0
a = v*v + w*w - r*u*u
k = y*v + z*w + r*x*u
c = y*y + z*z - r*x*x
quad = k*k - c
if (quad < ZERO .or. a == ZERO) then
! no intersection with cone
d = INFINITY
elseif (on_surface) then
! particle is on the cone, thus one distance is
! positive/negative and the other is zero. The sign of k
! determines if we are facing in or out
if (k >= ZERO) then
d = INFINITY
else
d = (-k + sqrt(quad))/a
end if
elseif (c < ZERO) then
! particle is inside the cone, thus one distance must be
! negative and one must be positive. The positive distance will
! be the one with negative sign on sqrt(quad)
d = (-k + sqrt(quad))/a
else
! particle is outside the cone, thus both distances are either
! positive or negative. If positive, the smaller distance is the
! one with positive sign on sqrt(quad)
d = (-k - sqrt(quad))/a
if (d <= ZERO) d = INFINITY
end if
case (SURF_CONE_Y)
x0 = surf % coeffs(1)
y0 = surf % coeffs(2)
z0 = surf % coeffs(3)
r = surf % coeffs(4)
x = x - x0
y = y - y0
z = z - z0
a = u*u + w*w - r*v*v
k = x*u + z*w + r*y*v
c = x*x + z*z - r*y*y
quad = k*k - c
if (quad < ZERO .or. a == ZERO) then
! no intersection with cone
d = INFINITY
elseif (on_surface) then
! particle is on the cone, thus one distance is
! positive/negative and the other is zero. The sign of k
! determines if we are facing in or out
if (k >= ZERO) then
d = INFINITY
else
d = (-k + sqrt(quad))/a
end if
elseif (c < ZERO) then
! particle is inside the cone, thus one distance must be
! negative and one must be positive. The positive distance will
! be the one with negative sign on sqrt(quad)