| 1 |
!-----------------------------------------------------------------------------! |
| 2 |
! \file kba/Block_Tracer.f90 |
| 3 |
! \author Thomas M. Evans |
| 4 |
! \date Thur Jan 24 14:00 2008 |
| 5 |
! \brief FORTRAN kernel for tracing a ray through a block of mesh. |
| 6 |
! \note Copyright (C) 2008 Oak Ridge National Laboratory, UT-Battelle, LLC |
| 7 |
!-----------------------------------------------------------------------------! |
| 8 |
! $Id: Ray_Tracer_Kernel.f90,v 1.1 2008/02/07 21:27:48 9te Exp $ |
| 9 |
!-----------------------------------------------------------------------------! |
| 10 |
|
| 11 |
module Ray_Tracer |
| 12 |
|
| 13 |
use Parameters, only : DOUBLE |
| 14 |
implicit none |
| 15 |
public |
| 16 |
|
| 17 |
! Distance-to-surface arrays used by the ray tracer. |
| 18 |
real(DOUBLE), dimension(:), allocatable, save :: dx, dy, dz |
| 19 |
|
| 20 |
! Ray direction. |
| 21 |
real(DOUBLE), dimension(3), save :: omega |
| 22 |
|
| 23 |
contains |
| 24 |
|
| 25 |
!---------------------------------------------------------------------------! |
| 26 |
! Calculate distance to boundary arrays |
| 27 |
!---------------------------------------------------------------------------! |
| 28 |
|
| 29 |
subroutine calc_distances(start, edges, r, p, angle, d) |
| 30 |
|
| 31 |
implicit none |
| 32 |
|
| 33 |
! >>> INPUT/OUTPUT DATA |
| 34 |
|
| 35 |
! Starting cell position in edge array. |
| 36 |
integer, intent(in) :: start |
| 37 |
|
| 38 |
! Edges in the chosen direction. |
| 39 |
real(DOUBLE), dimension(:), intent(in) :: edges |
| 40 |
|
| 41 |
! Starting point position in the chosen direction. |
| 42 |
real(DOUBLE), intent(in) :: r |
| 43 |
|
| 44 |
! Target (ending) point position in the chosen direction. |
| 45 |
real(DOUBLE), intent(in) :: p |
| 46 |
|
| 47 |
! Inverse of the angle-of-flight. |
| 48 |
real(DOUBLE), intent(in) :: angle |
| 49 |
|
| 50 |
! Edges in the chosen direction. |
| 51 |
real(DOUBLE), dimension(:), intent(out) :: d |
| 52 |
|
| 53 |
! >>> LOCAL DATA |
| 54 |
|
| 55 |
! Indices. |
| 56 |
integer :: n, index |
| 57 |
|
| 58 |
! Number of edges. |
| 59 |
integer :: length |
| 60 |
|
| 61 |
! Inverse angle. |
| 62 |
real(DOUBLE) :: inv_angle |
| 63 |
|
| 64 |
! >>> BODY |
| 65 |
|
| 66 |
! initialize d |
| 67 |
d = 1.0e14 |
| 68 |
|
| 69 |
! calculate the size of the edges array |
| 70 |
length = size(edges) |
| 71 |
|
| 72 |
! initialize starting index |
| 73 |
n = start |
| 74 |
index = 1 |
| 75 |
|
| 76 |
! step through edges in the positive direction |
| 77 |
if (angle > 0.0) then |
| 78 |
|
| 79 |
! calculate inverse of angle |
| 80 |
inv_angle = 1.0_DOUBLE / angle |
| 81 |
|
| 82 |
! continue until we hit the point or we get to the edge of the mesh |
| 83 |
do while (edges(n) < p .and. n < length) |
| 84 |
|
| 85 |
! assign distance to boundary |
| 86 |
d(index) = (edges(n + 1) - r) * inv_angle |
| 87 |
|
| 88 |
! update the indices |
| 89 |
n = n + 1 |
| 90 |
index = index + 1 |
| 91 |
|
| 92 |
end do |
| 93 |
|
| 94 |
else if (angle < 0.0) then |
| 95 |
|
| 96 |
! calculate inverse of angle |
| 97 |
inv_angle = 1.0_DOUBLE / angle |
| 98 |
|
| 99 |
! continue until we hit the point or we get to the edge of the mesh |
| 100 |
do while (edges(n + 1) > p .and. n > 0) |
| 101 |
|
| 102 |
! assign distance to boundary |
| 103 |
d(index) = (edges(n) - r) * inv_angle |
| 104 |
|
| 105 |
! update the indices |
| 106 |
n = n - 1 |
| 107 |
index = index + 1 |
| 108 |
|
| 109 |
end do |
| 110 |
|
| 111 |
end if |
| 112 |
|
| 113 |
end subroutine calc_distances |
| 114 |
|
| 115 |
!---------------------------------------------------------------------------! |
| 116 |
! CONSTRUCT |
| 117 |
!---------------------------------------------------------------------------! |
| 118 |
|
| 119 |
subroutine construct_ray_tracer |
| 120 |
|
| 121 |
use Parameters, only : im, jm, km |
| 122 |
implicit none |
| 123 |
|
| 124 |
if (.not. allocated(dx)) allocate(dx(im + 1)) |
| 125 |
if (.not. allocated(dy)) allocate(dy(jm + 1)) |
| 126 |
if (.not. allocated(dz)) allocate(dz(km + 1)) |
| 127 |
|
| 128 |
end subroutine construct_ray_tracer |
| 129 |
|
| 130 |
!---------------------------------------------------------------------------! |
| 131 |
! DESTRUCT |
| 132 |
!---------------------------------------------------------------------------! |
| 133 |
|
| 134 |
subroutine destruct_ray_tracer |
| 135 |
|
| 136 |
implicit none |
| 137 |
|
| 138 |
if (allocated(dx)) deallocate(dx) |
| 139 |
if (allocated(dy)) deallocate(dy) |
| 140 |
if (allocated(dz)) deallocate(dz) |
| 141 |
|
| 142 |
end subroutine destruct_ray_tracer |
| 143 |
|
| 144 |
end module Ray_Tracer |
| 145 |
|
| 146 |
!-----------------------------------------------------------------------------! |
| 147 |
! Setup routines for ray-tracing |
| 148 |
!-----------------------------------------------------------------------------! |
| 149 |
|
| 150 |
subroutine setup_ray_tracing |
| 151 |
|
| 152 |
use Ray_Tracer, only : construct_ray_tracer |
| 153 |
|
| 154 |
call construct_ray_tracer |
| 155 |
|
| 156 |
end subroutine setup_ray_tracing |
| 157 |
|
| 158 |
!-----------------------------------------------------------------------------! |
| 159 |
|
| 160 |
subroutine cleanup_ray_tracing |
| 161 |
|
| 162 |
use Ray_Tracer, only : destruct_ray_tracer |
| 163 |
|
| 164 |
call destruct_ray_tracer |
| 165 |
|
| 166 |
end subroutine cleanup_ray_tracing |
| 167 |
|
| 168 |
!-----------------------------------------------------------------------------! |
| 169 |
! Ray-tracing through a block mesh |
| 170 |
!-----------------------------------------------------------------------------! |
| 171 |
|
| 172 |
! Trace through a mesh block. |
| 173 |
|
| 174 |
subroutine block_tracer(x, y, z, sigma, r, p, ijk, tau, terminator) |
| 175 |
|
| 176 |
use Parameters, only : SHORT, DOUBLE, im, jm, km, I, J, K, one |
| 177 |
use Ray_Tracer, only : dx, dy, dz, omega, calc_distances |
| 178 |
implicit none |
| 179 |
|
| 180 |
! >>> INPUT DATA |
| 181 |
|
| 182 |
! Edge arrays. |
| 183 |
real(DOUBLE), dimension(im + 1), intent(in) :: x |
| 184 |
real(DOUBLE), dimension(jm + 1), intent(in) :: y |
| 185 |
real(DOUBLE), dimension(km + 1), intent(in) :: z |
| 186 |
|
| 187 |
! Total cross sections in each cell for all groups in (1/cm). |
| 188 |
real(DOUBLE), dimension(im * jm * km), intent(in) :: sigma |
| 189 |
|
| 190 |
! Ray target (ending) point. |
| 191 |
real(DOUBLE), dimension(3), intent(in) :: p |
| 192 |
|
| 193 |
! >>> IN/OUT DATA |
| 194 |
|
| 195 |
! (i, j, k) position of ray in mesh. |
| 196 |
integer, dimension(3), intent(inout) :: ijk |
| 197 |
|
| 198 |
! Ray position. |
| 199 |
real(DOUBLE), dimension(3), intent(inout) :: r |
| 200 |
|
| 201 |
! Tau = sum_{i} sigma_i * l_i where l is the length of each step. |
| 202 |
real(DOUBLE), intent(inout) :: tau |
| 203 |
|
| 204 |
! >>> OUT DATA |
| 205 |
|
| 206 |
! Termination condition. |
| 207 |
! 0 - hit target point |
| 208 |
! 1 - exited low-X face of block |
| 209 |
! 2 - exited high-X face of block |
| 210 |
! 3 - exited low-Y face of block |
| 211 |
! 4 - exited high-Y face of block |
| 212 |
! 5 - exited low-Z face of block |
| 213 |
! 6 - exited high-Z face of block |
| 214 |
! 7 - negative distance-to-boundary |
| 215 |
integer, intent(out) :: terminator |
| 216 |
|
| 217 |
! >>> LOCAL DATA |
| 218 |
|
| 219 |
! Distance to the source point: norm = 1 / ||(r - rp)||. |
| 220 |
real(DOUBLE) :: dtarget, dtravel, dstep, d |
| 221 |
|
| 222 |
! Boolean flag for ray-tracing. |
| 223 |
logical :: done |
| 224 |
|
| 225 |
! Indices into distance arrays. |
| 226 |
integer :: ii, jj, kk |
| 227 |
|
| 228 |
! Canonical cell index. |
| 229 |
integer :: cell, step_i, step_j, step_k |
| 230 |
|
| 231 |
! >>> BODY |
| 232 |
|
| 233 |
! add 1 to ijk to adjust to FORTRAN-style indexing |
| 234 |
ijk = ijk + 1 |
| 235 |
|
| 236 |
! calculate the distance to the target and the direction of the ray |
| 237 |
dtarget = sqrt(dot_product(r - p, r - p)) |
| 238 |
omega = (p - r) / dtarget |
| 239 |
|
| 240 |
! calculate the distances to each surface |
| 241 |
call calc_distances(ijk(I), x, r(I), p(I), omega(I), dx) |
| 242 |
call calc_distances(ijk(J), y, r(J), p(J), omega(J), dy) |
| 243 |
call calc_distances(ijk(K), z, r(K), p(K), omega(K), dz) |
| 244 |
|
| 245 |
! initialize indices into distance arrays |
| 246 |
ii = 1 |
| 247 |
jj = 1 |
| 248 |
kk = 1 |
| 249 |
|
| 250 |
! steps |
| 251 |
step_i = int(sign(one, omega(I))) |
| 252 |
step_j = int(sign(one, omega(J))) |
| 253 |
step_k = int(sign(one, omega(K))) |
| 254 |
|
| 255 |
! initialize loop |
| 256 |
done = .false. |
| 257 |
dtravel = 0.0_DOUBLE |
| 258 |
|
| 259 |
! ray-trace to point or until we leave the block |
| 260 |
do while (done .eqv. .false.) |
| 261 |
|
| 262 |
! calculate the canonical cell index |
| 263 |
cell = ijk(I) + (ijk(J) - 1) * im + (ijk(K) - 1) * im * jm |
| 264 |
|
| 265 |
! get the distance |
| 266 |
d = min(dx(ii), min(dy(jj), dz(kk))) |
| 267 |
|
| 268 |
! calculate the step |
| 269 |
dstep = d - dtravel |
| 270 |
|
| 271 |
! push to next boundary or to target point |
| 272 |
if (dstep < dtarget) then |
| 273 |
|
| 274 |
! subtract the distance from the target and add to the travel |
| 275 |
dtarget = dtarget - dstep |
| 276 |
dtravel = d |
| 277 |
|
| 278 |
! add up tau |
| 279 |
tau = tau + sigma(cell) * dstep |
| 280 |
|
| 281 |
! step to next face |
| 282 |
if (d == dx(ii)) then |
| 283 |
|
| 284 |
ii = ii + 1 |
| 285 |
ijk(I) = ijk(I) + step_i |
| 286 |
|
| 287 |
! terminating conditions |
| 288 |
if (ijk(I) == 0) then |
| 289 |
terminator = 1 |
| 290 |
done = .true. |
| 291 |
else if (ijk(I) == im + 1) then |
| 292 |
terminator = 2 |
| 293 |
done = .true. |
| 294 |
end if |
| 295 |
|
| 296 |
else if (d == dy(jj)) then |
| 297 |
|
| 298 |
jj = jj + 1 |
| 299 |
ijk(J) = ijk(J) + step_j |
| 300 |
|
| 301 |
! terminating conditions |
| 302 |
if (ijk(J) == 0) then |
| 303 |
terminator = 3 |
| 304 |
done = .true. |
| 305 |
else if (ijk(J) == jm + 1) then |
| 306 |
terminator = 4 |
| 307 |
done = .true. |
| 308 |
end if |
| 309 |
|
| 310 |
else if (d == dz(kk)) then |
| 311 |
|
| 312 |
kk = kk + 1 |
| 313 |
ijk(K) = ijk(K) + step_k |
| 314 |
|
| 315 |
! terminating conditions |
| 316 |
if (ijk(K) == 0) then |
| 317 |
terminator = 5 |
| 318 |
done = .true. |
| 319 |
else if (ijk(K) == km + 1) then |
| 320 |
terminator = 6 |
| 321 |
done = .true. |
| 322 |
end if |
| 323 |
|
| 324 |
end if |
| 325 |
|
| 326 |
else |
| 327 |
|
| 328 |
! add to the travel |
| 329 |
dtravel = dtravel + dtarget |
| 330 |
|
| 331 |
! add up tau |
| 332 |
tau = tau + sigma(cell) * dtarget |
| 333 |
|
| 334 |
! we are finished |
| 335 |
done = .true. |
| 336 |
terminator = 0 |
| 337 |
|
| 338 |
end if |
| 339 |
|
| 340 |
end do |
| 341 |
|
| 342 |
! calculate final location of particle |
| 343 |
r = r + dtravel * omega |
| 344 |
|
| 345 |
! subtract 1 to ijk to adjust to C-style indexing |
| 346 |
ijk = ijk - 1 |
| 347 |
|
| 348 |
end subroutine block_tracer |
| 349 |
|
| 350 |
!-----------------------------------------------------------------------------! |
| 351 |
! end of Block_Tracer.f90 |
| 352 |
!-----------------------------------------------------------------------------! |