MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
ParticleTracks.f90
Go to the documentation of this file.
1!> @brief Particle track output module.
2!!
3!! Each particle's track consists of events reported as the particle is
4!! advected through the model domain. Events are snapshots of particle
5!! state, along with optional metadata, at a particular moment in time.
6!!
7!! Particles have no ID property. A particle can be uniquely identified
8!! by the unique combination of its release attributes (model, package,
9!! position, and time). This is possible because only one particle may
10!! be released from a given point at a given time.
11!!
12!! This module consumes particle events and is responsible for writing
13!! them to one or more track files, binary or CSV, and for logging the
14!! events if requested. Each track file is associated with either a PRP
15!! package or with the full PRT model (there may only be 1 such latter).
16!!
17!! Events can be buffered in memory or in a scratch file, and in either
18!! case are flushed to disk only when a time step successfully finishes.
19!<
21
22 use kindmodule, only: dp, i4b, lgp
23 use errorutilmodule, only: pstop
35 use basedismodule, only: disbasetype
36 use geomutilmodule, only: transform
40
41 implicit none
42 public :: particletrackstype, &
45
46 character(len=*), parameter, public :: trackheader = &
47 'kper,kstp,imdl,iprp,irpt,ilay,icell,izone,&
48 &istatus,ireason,trelease,t,x,y,z,name'
49
50 character(len=*), parameter, public :: trackdtypes = &
51 '<i4,<i4,<i4,<i4,<i4,<i4,<i4,<i4,&
52 &<i4,<i4,<f8,<f8,<f8,<f8,<f8,|S40'
53
54 !> @brief Selection of particle events.
56 logical(LGP) :: release !< track release events
57 logical(LGP) :: featexit !< track grid feature exits
58 logical(LGP) :: timestep !< track timestep ends
59 logical(LGP) :: terminate !< track termination events
60 logical(LGP) :: weaksink !< track weak sink exit events
61 logical(LGP) :: usertime !< track user-selected times
62 logical(LGP) :: subfexit !< track subfeature exits
63 logical(LGP) :: dropped !< track water table drops
65
66 !> @brief Particle track output manager. Handles printing as well as writing
67 !! to files. One output unit can be configured for printing. Multiple files
68 !! can be configured for writing, with each file optionally associated with
69 !! a PRP package or with the full model. Events can be filtered by type, so
70 !! that only certain event types are printed or written to files. Particle
71 !! events are buffered in memory or in a scratch file, flushed to disk only
72 !! when the time step is successfully solved for the last time (there may
73 !! be multiple solves per time step, depending on ATS and Picard options).
74 !<
76 private
77 integer(I4B), public :: iout = -1 !< log file unit
78 integer(I4B), public :: ntrackfiles = 0 !< number of track files
79 type(particletrackfiletype), public, allocatable :: files(:) !< track files
80 type(particletrackeventselectiontype), public :: selected !< event selection
81 class(particletrackeventbuffertype), allocatable :: buffer !< event buffer
82 contains
83 procedure, public :: init_file
84 procedure, public :: init_buffer
85 procedure, public :: is_selected
86 procedure, public :: select_events
87 procedure, public :: buffer_event
88 procedure, public :: flush_buffer
89 procedure, public :: discard_buffer
90 procedure, public :: destroy
91 procedure :: expand_files
92 end type particletrackstype
93
94contains
95
96 !> @brief Initialize a binary or CSV output file.
97 subroutine init_file(this, iun, csv, iprp)
98 class(particletrackstype) :: this
99 integer(I4B), intent(in) :: iun
100 logical(LGP), intent(in), optional :: csv
101 integer(I4B), intent(in), optional :: iprp
102 type(particletrackfiletype), pointer :: file
103
104 call this%expand_files(increment=1)
105
106 allocate (file)
107 file%iun = iun
108 if (present(csv)) file%csv = csv
109 if (present(iprp)) file%iprp = iprp
110 this%ntrackfiles = size(this%files)
111 this%files(this%ntrackfiles) = file
112 end subroutine init_file
113
114 !> @brief Initialize the event buffer strategy
115 subroutine init_buffer(this, scratch)
116 class(particletrackstype) :: this
117 logical(LGP), intent(in) :: scratch
118
119 if (.not. allocated(this%files)) allocate (this%files(0))
120 if (scratch) then
121 allocate (scratchfilebuffertype :: this%buffer)
122 else
123 allocate (memorybuffertype :: this%buffer)
124 end if
125 call this%buffer%init()
126 end subroutine init_buffer
127
128 !> @brief Destroy the particle track manager.
129 subroutine destroy(this)
130 class(particletrackstype) :: this
131 call this%buffer%destroy()
132 end subroutine destroy
133
134 !> @brief Grow the array of track files.
135 subroutine expand_files(this, increment)
136 class(particletrackstype) :: this
137 integer(I4B), optional, intent(in) :: increment
138 integer(I4B) :: inclocal, isize, newsize
139 type(particletrackfiletype), allocatable, dimension(:) :: temp
140
141 if (present(increment)) then
142 inclocal = increment
143 else
144 inclocal = 1
145 end if
146
147 if (allocated(this%files)) then
148 isize = size(this%files)
149 newsize = isize + inclocal
150 allocate (temp(newsize))
151 temp(1:isize) = this%files
152 deallocate (this%files)
153 call move_alloc(temp, this%files)
154 else
155 allocate (this%files(inclocal))
156 end if
157 end subroutine expand_files
158
159 !> @brief Pick events to track.
160 subroutine select_events(this, &
161 release, &
162 featexit, &
163 timestep, &
164 terminate, &
165 weaksink, &
166 usertime, &
167 subfexit, &
168 dropped)
169 class(particletrackstype) :: this
170 logical(LGP), intent(in) :: release
171 logical(LGP), intent(in) :: featexit
172 logical(LGP), intent(in) :: timestep
173 logical(LGP), intent(in) :: terminate
174 logical(LGP), intent(in) :: weaksink
175 logical(LGP), intent(in) :: usertime
176 logical(LGP), intent(in) :: subfexit
177 logical(LGP), intent(in) :: dropped
178 this%selected%release = release
179 this%selected%featexit = featexit
180 this%selected%timestep = timestep
181 this%selected%terminate = terminate
182 this%selected%weaksink = weaksink
183 this%selected%usertime = usertime
184 this%selected%subfexit = subfexit
185 this%selected%dropped = dropped
186 end subroutine select_events
187
188 !> @brief Check if a given event code is selected for tracking.
189 logical function is_selected(this, event) result(selected)
190 class(particletrackstype), intent(inout) :: this
191 class(particleeventtype), intent(in) :: event
192
193 select type (event)
194 type is (releaseeventtype)
195 selected = this%selected%release
196 type is (cellexiteventtype)
197 selected = this%selected%featexit
198 type is (subcellexiteventtype)
199 selected = this%selected%subfexit
200 type is (timestepeventtype)
201 selected = this%selected%timestep
202 type is (terminationeventtype)
203 selected = this%selected%terminate
204 type is (weaksinkeventtype)
205 selected = this%selected%weaksink
206 type is (usertimeeventtype)
207 selected = this%selected%usertime
208 type is (droppedeventtype)
209 selected = this%selected%dropped
210 class default
211 call pstop(1, "unknown event type")
212 selected = .false.
213 end select
214 end function is_selected
215
216 !> @brief Buffer an event for deferred write.
217 subroutine buffer_event(this, particle, event)
218 class(particletrackstype) :: this
219 type(particletype), pointer, intent(in) :: particle
220 class(particleeventtype), pointer, intent(in) :: event
221 type(particletrackrecordtype) :: rec
222
223 rec%kper = event%kper; rec%kstp = event%kstp
224 rec%imdl = event%imdl; rec%iprp = event%iprp
225 rec%irpt = event%irpt; rec%ilay = event%ilay
226 rec%icu = event%icu; rec%izone = event%izone
227 rec%istatus = event%istatus; rec%ireason = event%get_code()
228 rec%trelease = event%trelease; rec%ttrack = event%ttrack
229 rec%x = event%x; rec%y = event%y; rec%z = event%z
230 rec%name = trim(adjustl(particle%name))
231
232 call this%buffer%append(rec)
233 end subroutine buffer_event
234
235 !> @brief Flush the event buffer to disk.
236 subroutine flush_buffer(this)
237 class(particletrackstype) :: this
238 call this%buffer%flush(this%files)
239 end subroutine flush_buffer
240
241 !> @brief Discard buffered events without writing.
242 subroutine discard_buffer(this)
243 class(particletrackstype) :: this
244 call this%buffer%discard()
245 end subroutine discard_buffer
246
247 !> @brief Add a particle event to be written to eligible
248 !! files and printed to an output file unit if requested.
249 !! This function should be subscribed as an event handler
250 !! to particle event dispatchers. Events are buffered to
251 !! be written to output files upon successful completion
252 !! of a time step when the framework OT hook is executed.
253 function add_particle_event(context, particle, event) result(handled)
254 class(*), pointer :: context
255 type(particletype), pointer, intent(inout) :: particle
256 class(particleeventtype), pointer, intent(in) :: event
257 logical(LGP) :: handled
258
259 select type (context)
260 type is (particletrackstype)
261 if (context%iout >= 0) &
262 call event%log(context%iout)
263 if (context%ntrackfiles > 0) then
264 if (context%is_selected(event)) &
265 call context%buffer_event(particle, event)
266 end if
267 handled = .true.
268 end select
269 end function add_particle_event
270
271end module particletracksmodule
subroutine pstop(status, message)
Stop the program, optionally specifying an error status code.
Definition ErrorUtil.f90:24
subroutine, public transform(xin, yin, zin, xout, yout, zout, xorigin, yorigin, zorigin, sinrot, cosrot, invert)
Apply a 3D translation and optional 2D rotation to coordinates.
Definition GeomUtil.f90:192
This module defines variable data types.
Definition kind.f90:8
In-memory particle event buffer module.
@ usertime
user-specified tracking time
@ release
particle was released
@ terminate
particle terminated
@ timestep
time step ended
@ weaksink
particle entered a weak sink
@ dropped
particle dropped to the water table
@ featexit
particle exited a grid feature
subroutine destroy(this)
Destroy the dispatcher.
Particle event buffering strategies.
Particle track output module.
subroutine discard_buffer(this)
Discard buffered events without writing.
logical function is_selected(this, event)
Check if a given event code is selected for tracking.
subroutine init_file(this, iun, csv, iprp)
Initialize a binary or CSV output file.
subroutine buffer_event(this, particle, event)
Buffer an event for deferred write.
logical(lgp) function, public add_particle_event(context, particle, event)
Add a particle event to be written to eligible files and printed to an output file unit if requested....
character(len= *), parameter, public trackheader
subroutine flush_buffer(this)
Flush the event buffer to disk.
subroutine select_events(this, release, featexit, timestep, terminate, weaksink, usertime, subfexit, dropped)
Pick events to track.
subroutine expand_files(this, increment)
Grow the array of track files.
subroutine init_buffer(this, scratch)
Initialize the event buffer strategy.
character(len= *), parameter, public trackdtypes
Scratch-file particle event buffer module.
In-memory particle event buffer. Records are held in a dynamically growing array that doubles in capa...
Base type for particle events.
Dispatcher for particle events. Consumers subscribe handlers to the dispatcher. Events may be dispatc...
Particle tracked by the PRT model.
Definition Particle.f90:64
Output file containing all or some particle pathlines.
Particle track output manager. Handles printing as well as writing to files. One output unit can be c...
Scratch-file particle event buffer. Records are written to an unformatted sequential scratch file tha...