MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
MethodSubcellPollock.f90
Go to the documentation of this file.
2 use kindmodule, only: dp, i4b, lgp
3 use errorutilmodule, only: pstop
8 use prtfmimodule, only: prtfmitype
10 use cellmodule, only: celltype
11 use constantsmodule, only: dzero, done
12 use domainmodule, only: domaintype
13 use subcellmodule, only: subcelltype
14 use listmodule, only: listtype
19 implicit none
20 private
23 public :: calculate_dt
24
25 !> @brief Rectangular subcell tracking method
27 private
28 real(dp), allocatable, public :: qextl1(:), qextl2(:), qintl(:) !< external and internal subcell flows
29 contains
30 procedure, public :: apply => apply_msp
31 procedure, public :: deallocate
32 procedure, private :: track_subcell
34
35contains
36
37 !> @brief Create a new Pollock's subcell method
39 ! dummy
40 type(methodsubcellpollocktype), pointer :: method
41 ! local
42 type(subcellrecttype), pointer :: subcell
43
44 allocate (method)
45 call create_subcell_rect(subcell)
46 method%subcell => subcell
47 method%name => method%subcell%type
48 method%delegates = .false.
50
51 !> @brief Deallocate the Pollock's subcell method
52 subroutine deallocate (this)
53 class(methodsubcellpollocktype), intent(inout) :: this
54 deallocate (this%name)
55 end subroutine deallocate
56
57 !> @brief Apply Pollock's method to a rectangular subcell
58 subroutine apply_msp(this, particle, tmax)
59 ! dummy
60 class(methodsubcellpollocktype), intent(inout) :: this
61 type(particletype), pointer, intent(inout) :: particle
62 real(DP), intent(in) :: tmax
63 ! local
64 real(DP) :: x_origin
65 real(DP) :: y_origin
66 real(DP) :: sinrot
67 real(DP) :: cosrot
68
69 select type (subcell => this%subcell)
70 type is (subcellrecttype)
71 ! Transform particle position into local subcell coordinates,
72 ! track particle across subcell, convert back to model coords
73 ! (sinrot and cosrot should be 0 and 1, respectively, i.e. no
74 ! rotation, also no z translation; only x and y translations,
75 ! since subcell%zOrigin is always 0 -- z is translated once,
76 ! at the cell level, not again here)
77 x_origin = subcell%xOrigin
78 y_origin = subcell%yOrigin
79 sinrot = subcell%sinrot
80 cosrot = subcell%cosrot
81 call particle%transform(x_origin, y_origin)
82
83 ! Clamp the particle into the subcell in case roundoff in the
84 ! model-to-local coordinate transform (e.g. under grid rotation)
85 ! left it just outside, analogous to the nudge applied to the
86 ! ternary method's barycentric coordinates.
87 particle%x = min(max(particle%x, dzero), subcell%dx)
88 particle%y = min(max(particle%y, dzero), subcell%dy)
89 particle%z = min(max(particle%z, dzero), subcell%dz)
90
91 call this%track_subcell(subcell, particle, tmax)
92 call particle%transform(x_origin, y_origin, invert=.true.)
93 end select
94 end subroutine apply_msp
95
96 !> @brief Track a particle across a rectangular subcell using Pollock's method
97 !!
98 !! This subroutine consists partly of code written by
99 !! David W. Pollock of the USGS for MODPATH 7. PRT's
100 !! authors take responsibility for its application in
101 !! this context and for any modifications or errors.
102 !<
103 subroutine track_subcell(this, subcell, particle, tmax)
106 ! dummy
107 class(methodsubcellpollocktype), intent(inout) :: this
108 class(subcellrecttype), intent(in) :: subcell
109 type(particletype), pointer, intent(inout) :: particle
110 real(DP), intent(in) :: tmax
111 ! local
112 real(DP) :: dt, dtexit, texit
113 real(DP) :: t, x, y, z
114 real(DP) :: t0, x0, y0, z0
115 integer(I4B) :: i, exit_face, exit_soln
116 type(linearexitsolutiontype) :: exit_solutions(3)
117 type(linearexitsolutiontype) :: exit_x, exit_y, exit_z
118
119 t0 = particle%ttrack
120
121 ! Find exit solution in each direction; also returns the
122 ! local subcell coordinates used, so we don't recompute them
123 call find_exits(particle, subcell, exit_solutions, x0, y0, z0)
124 exit_x = exit_solutions(1)
125 exit_y = exit_solutions(2)
126 exit_z = exit_solutions(3)
127
128 ! Set solution, face, & travel time
129 exit_soln = pick_exit(exit_solutions)
130 if (exit_soln == 0) then
131 exit_face = 0
132 dtexit = 1.0d+30
133 else
134 exit_face = exit_solutions(exit_soln)%iboundary
135 dtexit = exit_solutions(exit_soln)%dt
136 end if
137 texit = particle%ttrack + dtexit
138
139 ! Terminate if no valid exit solution was found.
140 ! MP7 is more nuanced in determining what to do
141 ! here. It considers whether this stress period
142 ! is steady state or transient, and whether the
143 ! flow is stationary, in determining whether to
144 ! terminate or allow the particle to survive to
145 ! the next time step. It does not compute paths
146 ! within the subcell even for particles it lets
147 ! remain active under this circumstance, though.
148 ! While we may consider that someday, we simply
149 ! terminate and sidestep the complexity for now.
150 if (all([exit_solutions%status] >= no_exit_stationary)) then
151 call this%terminate(particle, status=term_no_exits_sub)
152 return
153 end if
154
155 ! Select user tracking times to solve. If this is the first time step
156 ! of the simulation, include all times before it begins; if it is the
157 ! last time step include all times after it ends only if the 'extend'
158 ! option is on, otherwise times in this period and time step only.
159 call this%tracktimes%advance()
160 if (this%tracktimes%any()) then
161 do i = this%tracktimes%selection(1), this%tracktimes%selection(2)
162 t = this%tracktimes%times(i)
163 if (t < particle%ttrack) cycle
164 if (t > texit .or. t > tmax) exit
165 dt = t - t0
166 x = new_x(exit_x%v, exit_x%dvdx, subcell%vx1, subcell%vx2, &
167 dt, x0, subcell%dx, exit_x%status == 1)
168 y = new_x(exit_y%v, exit_y%dvdx, subcell%vy1, subcell%vy2, &
169 dt, y0, subcell%dy, exit_y%status == 1)
170 z = new_x(exit_z%v, exit_z%dvdx, subcell%vz1, subcell%vz2, &
171 dt, z0, subcell%dz, exit_z%status == 1)
172 particle%x = x * subcell%dx
173 particle%y = y * subcell%dy
174 particle%z = z * subcell%dz
175 particle%ttrack = t
176 call this%usertime(particle)
177 end do
178 end if
179
180 ! Computed exit time greater than the maximum time? Set the
181 ! tracking time to tmax and calculate the particle location.
182 if (texit .gt. tmax) then
183 t = tmax
184 dt = t - t0
185 x = new_x(exit_x%v, exit_x%dvdx, subcell%vx1, subcell%vx2, &
186 dt, x0, subcell%dx, exit_x%status == 1)
187 y = new_x(exit_y%v, exit_y%dvdx, subcell%vy1, subcell%vy2, &
188 dt, y0, subcell%dy, exit_y%status == 1)
189 z = new_x(exit_z%v, exit_z%dvdx, subcell%vz1, subcell%vz2, &
190 dt, z0, subcell%dz, exit_z%status == 1)
191 exit_face = 0
192 particle%advancing = .false.
193 particle%x = x * subcell%dx
194 particle%y = y * subcell%dy
195 particle%z = z * subcell%dz
196 particle%ttrack = t
197 particle%iboundary(level_subfeature) = exit_face
198 call this%timestep(particle)
199 return
200 end if
201
202 ! If we get to here, the particle is exiting the subcell.
203 ! Its exit time is less than or equal to the maximum time.
204 ! Set tracking time to the exit time and set exit location.
205 t = texit
206 dt = dtexit
207 if ((exit_face .eq. 1) .or. (exit_face .eq. 2)) then
208 x = dzero
209 y = new_x(exit_y%v, exit_y%dvdx, subcell%vy1, subcell%vy2, &
210 dt, y0, subcell%dy, exit_y%status == 1)
211 z = new_x(exit_z%v, exit_z%dvdx, subcell%vz1, subcell%vz2, &
212 dt, z0, subcell%dz, exit_z%status == 1)
213 if (exit_face .eq. 2) x = done
214 else if ((exit_face .eq. 3) .or. (exit_face .eq. 4)) then
215 x = new_x(exit_x%v, exit_x%dvdx, subcell%vx1, subcell%vx2, dt, &
216 x0, subcell%dx, exit_x%status == 1)
217 y = dzero
218 z = new_x(exit_z%v, exit_z%dvdx, subcell%vz1, subcell%vz2, dt, &
219 z0, subcell%dz, exit_z%status == 1)
220 if (exit_face .eq. 4) y = done
221 else if ((exit_face .eq. 5) .or. (exit_face .eq. 6)) then
222 x = new_x(exit_x%v, exit_x%dvdx, subcell%vx1, subcell%vx2, &
223 dt, x0, subcell%dx, exit_x%status == 1)
224 y = new_x(exit_y%v, exit_y%dvdx, subcell%vy1, subcell%vy2, &
225 dt, y0, subcell%dy, exit_y%status == 1)
226 z = dzero
227 if (exit_face .eq. 6) z = done
228 else
229 print *, "programmer error, invalid exit face", exit_face
230 call pstop(1)
231 end if
232 particle%x = x * subcell%dx
233 particle%y = y * subcell%dy
234 particle%z = z * subcell%dz
235 particle%ttrack = t
236 particle%iboundary(level_subfeature) = exit_face
237 call this%subcellexit(particle)
238
239 end subroutine track_subcell
240
241 !> @brief Pick the exit solution with the shortest travel time
242 function pick_exit(exit_solutions) result(exit_soln)
243 type(linearexitsolutiontype), intent(in) :: exit_solutions(3)
244 integer(I4B) :: exit_soln
245 ! local
246 real(dp) :: dtmin
247
248 exit_soln = 0
249 dtmin = 1.0d+30
250
251 if (exit_solutions(1)%status < 2) then
252 exit_soln = 1 ! x
253 dtmin = exit_solutions(1)%dt
254 end if
255 if (exit_solutions(2)%status < 2 .and. &
256 exit_solutions(2)%dt < dtmin) then
257 exit_soln = 2 ! y
258 dtmin = exit_solutions(2)%dt
259 end if
260 if (exit_solutions(3)%status < 2 .and. &
261 exit_solutions(3)%dt < dtmin) then
262 exit_soln = 3 ! z
263 end if
264
265 end function pick_exit
266
267 !> @brief Compute candidate exit solutions
268 !!
269 !! Also returns the local subcell coordinates used (x0, y0, z0),
270 !! so track_subcell doesn't need to recompute them from the
271 !! particle's position.
272 !<
273 subroutine find_exits(particle, domain, exit_solutions, x0, y0, z0)
274 type(particletype), pointer, intent(inout) :: particle
275 class(domaintype), intent(in) :: domain
276 type(linearexitsolutiontype), intent(out) :: exit_solutions(3)
277 real(DP), intent(out) :: x0, y0, z0
278
279 select type (domain)
280 type is (subcellrecttype)
281 ! Initial particle location in scaled subcell coordinates
282 x0 = particle%x / domain%dx
283 y0 = particle%y / domain%dy
284 z0 = particle%z / domain%dz
285
286 ! Calculate exit solutions for each coordinate direction
287 exit_solutions = [ &
288 find_exit(domain%vx1, domain%vx2, domain%dx, x0), &
289 find_exit(domain%vy1, domain%vy2, domain%dy, y0), &
290 find_exit(domain%vz1, domain%vz2, domain%dz, z0) &
291 ]
292
293 ! Set exit faces
294 if (exit_solutions(1)%v < dzero) then
295 exit_solutions(1)%iboundary = 1
296 else if (exit_solutions(1)%v > dzero) then
297 exit_solutions(1)%iboundary = 2
298 end if
299 if (exit_solutions(2)%v < dzero) then
300 exit_solutions(2)%iboundary = 3
301 else if (exit_solutions(2)%v > dzero) then
302 exit_solutions(2)%iboundary = 4
303 end if
304 if (exit_solutions(3)%v < dzero) then
305 exit_solutions(3)%iboundary = 5
306 else if (exit_solutions(3)%v > dzero) then
307 exit_solutions(3)%iboundary = 6
308 end if
309 end select
310 end subroutine find_exits
311
312 !> @brief Find an exit solution for one dimension
313 function find_exit(v1, v2, dx, xL) result(solution)
314 ! dummy
315 real(dp), intent(in) :: v1
316 real(dp), intent(in) :: v2
317 real(dp), intent(in) :: dx
318 real(dp), intent(in) :: xl
319 type(linearexitsolutiontype) :: solution
320
321 solution = linearexitsolutiontype()
322 solution%status = calculate_dt(v1, v2, dx, xl, &
323 solution%v, solution%dvdx, solution%dt)
324 end function find_exit
325
326 !> @brief Calculate particle travel time to exit and exit status.
327 !!
328 !! This subroutine consists partly of code written by and/or adapted from
329 !! David W. Pollock of the USGS for MODPATH 7. The authors of the present
330 !! code are responsible for its appropriate application in this context
331 !! and for any modifications or errors.
332 !<
333 function calculate_dt(v1, v2, dx, xL, v, dvdx, dt) result(status)
334 ! dummy
335 real(dp) :: v1
336 real(dp) :: v2
337 real(dp) :: dx
338 real(dp) :: xl
339 real(dp) :: v
340 real(dp) :: dvdx
341 real(dp) :: dt
342 ! result
343 integer(I4B) :: status
344 ! local
345 real(dp) :: v2a
346 real(dp) :: v1a
347 real(dp) :: dv
348 real(dp) :: dva
349 real(dp) :: vv
350 real(dp) :: vvv
351 real(dp) :: zro
352 real(dp) :: zrom
353 real(dp) :: x
354 real(dp) :: tol
355 real(dp) :: vr1
356 real(dp) :: vr2
357 real(dp) :: vr
358 real(dp) :: v1v2
359 logical(LGP) :: nooutflow
360
361 ! Initialize variables.
362 status = -1
363 dt = 1.0d+20
364 v2a = v2
365 if (v2a .lt. dzero) v2a = -v2a
366 v1a = v1
367 if (v1a .lt. dzero) v1a = -v1a
368 dv = v2 - v1
369 dva = dv
370 if (dva .lt. dzero) dva = -dva
371
372 ! Check for a uniform zero velocity in this direction.
373 ! If so, set status = 2 and return (dt = 1.0d+20).
374 tol = 1.0d-15
375 if ((v2a .lt. tol) .and. (v1a .lt. tol)) then
376 v = dzero
377 dvdx = dzero
378 status = no_exit_stationary
379 return
380 end if
381
382 ! Check for uniform non-zero velocity in this direction.
383 ! If so, set compute dt using the constant velocity,
384 ! set status = 1 and return.
385 vv = v1a
386 if (v2a .gt. vv) vv = v2a
387 vvv = dva / vv
388 if (vvv .lt. 1.0d-4) then
389 zro = tol
390 zrom = -zro
391 v = v1
392 x = xl * dx
393 if (v1 .gt. zro) dt = (dx - x) / v1
394 if (v1 .lt. zrom) dt = -x / v1
395 dvdx = dzero
396 status = ok_exit_constant
397 return
398 end if
399
400 ! Velocity has a linear variation.
401 ! Compute velocity corresponding to particle position.
402 dvdx = dv / dx
403 v = (done - xl) * v1 + xl * v2
404
405 ! If flow is into the cell from both sides there is no outflow.
406 ! In that case, set status = 3 and return.
407 nooutflow = .true.
408 if (v1 .lt. dzero) nooutflow = .false.
409 if (v2 .gt. dzero) nooutflow = .false.
410 if (nooutflow) then
411 status = no_exit_no_outflow
412 return
413 end if
414
415 ! If there is a divide in the cell for this flow direction, check to
416 ! see if the particle is located exactly on the divide. If it is, move
417 ! it very slightly to get it off the divide. This avoids possible
418 ! numerical problems related to stagnation points.
419 if ((v1 .le. dzero) .and. (v2 .ge. dzero)) then
420 if (abs(v) .le. dzero) then
421 v = 1.0d-20
422 if (v2 .le. dzero) v = -v
423 end if
424 end if
425
426 ! If there is a flow divide, this check finds out what side of the
427 ! divide the particle is on and sets the value of vr appropriately
428 ! to reflect that location.
429 vr1 = v1 / v
430 vr2 = v2 / v
431 vr = vr1
432 if (vr .le. dzero) then
433 vr = vr2
434 end if
435
436 ! If the product v1*v2 > 0, the velocity is in the same direction
437 ! throughout the cell (i.e. no flow divide). If so, set the value
438 ! of vr to reflect the appropriate direction.
439 v1v2 = v1 * v2
440 if (v1v2 .gt. dzero) then
441 if (v .gt. dzero) vr = vr2
442 if (v .lt. dzero) vr = vr1
443 end if
444
445 ! Check if vr is (very close to) zero.
446 ! If so, set status = 2 and return (dt = 1.0d+20).
447 if (dabs(vr) .lt. 1.0d-10) then
448 v = dzero
449 dvdx = dzero
450 status = no_exit_stationary
451 return
452 end if
453
454 ! Compute travel time to exit face. Return with status = 0.
455 dt = log(vr) / dvdx
456 status = ok_exit
457
458 end function calculate_dt
459
460 !> @brief Update a cell-local coordinate based on a time increment.
461 !!
462 !! This subroutine consists partly or entirely of code written by
463 !! David W. Pollock of the USGS for MODPATH 7. The authors of the present
464 !! code are responsible for its appropriate application in this context
465 !! and for any modifications or errors.
466 !<
467 pure function new_x(v, dvdx, v1, v2, dt, x, dx, velocity_profile) result(newx)
468 ! dummy
469 real(dp), intent(in) :: v
470 real(dp), intent(in) :: dvdx
471 real(dp), intent(in) :: v1
472 real(dp), intent(in) :: v2
473 real(dp), intent(in) :: dt
474 real(dp), intent(in) :: x
475 real(dp), intent(in) :: dx
476 logical(LGP), intent(in), optional :: velocity_profile
477 ! result
478 real(dp) :: newx
479 logical(LGP) :: lprofile
480
481 ! process optional arguments
482 if (present(velocity_profile)) then
483 lprofile = velocity_profile
484 else
485 lprofile = .false.
486 end if
487
488 ! recompute coordinate
489 newx = x
490 if (lprofile) then
491 newx = newx + (v1 * dt / dx)
492 else if (v .ne. dzero) then
493 newx = newx + (v * (exp(dvdx * dt) - done) / dvdx / dx)
494 end if
495
496 ! clamp to [0, 1]
497 if (newx .lt. dzero) newx = dzero
498 if (newx .gt. done) newx = done
499
500 end function new_x
501
This module contains simulation constants.
Definition Constants.f90:9
real(dp), parameter dzero
real constant zero
Definition Constants.f90:65
real(dp), parameter done
real constant 1
Definition Constants.f90:76
subroutine pstop(status, message)
Stop the program, optionally specifying an error status code.
Definition ErrorUtil.f90:24
@ ok_exit_constant
exit found, constant velocity
@ ok_exit
exit found using velocity interpolation
@ no_exit_stationary
no exit, zero velocity
@ no_exit_no_outflow
no exit, no outflow
This module defines variable data types.
Definition kind.f90:8
Particle tracking strategies.
Definition Method.f90:2
@ level_subfeature
Definition Method.f90:41
subroutine find_exits(particle, domain, exit_solutions, x0, y0, z0)
Compute candidate exit solutions.
pure real(dp) function new_x(v, dvdx, v1, v2, dt, x, dx, velocity_profile)
Update a cell-local coordinate based on a time increment.
type(linearexitsolutiontype) function find_exit(v1, v2, dx, xl)
Find an exit solution for one dimension.
integer(i4b) function, public calculate_dt(v1, v2, dx, xl, v, dvdx, dt)
Calculate particle travel time to exit and exit status.
integer(i4b) function pick_exit(exit_solutions)
Pick the exit solution with the shortest travel time.
subroutine track_subcell(this, subcell, particle, tmax)
Track a particle across a rectangular subcell using Pollock's method.
subroutine, public create_method_subcell_pollock(method)
Create a new Pollock's subcell method.
subroutine apply_msp(this, particle, tmax)
Apply Pollock's method to a rectangular subcell.
@ timestep
time step ended
@ featexit
particle exited a grid feature
@ term_no_exits_sub
terminated in a subcell with no exit face
Definition Particle.f90:39
subroutine, public create_subcell_rect(subcell)
Create a new rectangular subcell.
Base type for grid cells of a concrete type. Contains a cell-definition which is information shared b...
Definition Cell.f90:12
A tracking domain.
Definition Domain.f90:8
Base type for exit solutions.
Linear velocity interpolation exit solution.
A generic heterogeneous doubly-linked list.
Definition List.f90:14
Abstract base type for subcell tracking methods.
Particle tracked by the PRT model.
Definition Particle.f90:64
A subcell of a cell.
Definition Subcell.f90:10