MODFLOW 6  version 6.8.0.dev0
USGS Modular Hydrologic Model
ScratchFileBuffer.f90
Go to the documentation of this file.
1 !> @brief Scratch-file particle event buffer module.
2 !<
4 
5  use kindmodule, only: i4b
6  use errorutilmodule, only: pstop
11 
12  implicit none
13  private
14  public :: scratchfilebuffertype
15 
16  !> @brief Scratch-file particle event buffer. Records are written to
17  !! an unformatted sequential scratch file that is rewound on discard
18  !! and read back sequentially on flush.
20  integer(I4B) :: iun = 0 !< scratch file unit
21  contains
22  procedure :: init => scratch_init
23  procedure :: append => scratch_append
24  procedure :: flush => scratch_flush
25  procedure :: discard => scratch_discard
26  procedure :: destroy => scratch_destroy
27  end type scratchfilebuffertype
28 
29 contains
30 
31  subroutine scratch_init(this)
32  class(scratchfilebuffertype) :: this
33  integer(I4B) :: istat
34  open (newunit=this%iun, status='scratch', form='unformatted', &
35  access='sequential', iostat=istat)
36  if (istat /= 0) &
37  call pstop(1, 'failed to open scratch track buffer file')
38  end subroutine scratch_init
39 
40  subroutine scratch_append(this, rec)
41  class(scratchfilebuffertype) :: this
42  type(particletrackrecordtype), intent(in) :: rec
43  write (this%iun) rec
44  this%nrecords = this%nrecords + 1
45  end subroutine scratch_append
46 
47  subroutine scratch_flush(this, files)
48  class(scratchfilebuffertype) :: this
49  type(particletrackfiletype), intent(in) :: files(:)
50  integer(I4B) :: n, i
51  type(particletrackrecordtype) :: rec
52 
53  rewind(this%iun)
54  do n = 1, this%nrecords
55  read (this%iun) rec
56  do i = 1, size(files)
57  if (files(i)%iun > 0 .and. &
58  (files(i)%iprp == -1 .or. files(i)%iprp == rec%iprp)) &
59  call save_record(files(i)%iun, rec, files(i)%csv)
60  end do
61  end do
62  rewind(this%iun)
63  this%nrecords = 0
64  end subroutine scratch_flush
65 
66  subroutine scratch_discard(this)
67  class(scratchfilebuffertype) :: this
68  rewind(this%iun)
69  this%nrecords = 0
70  end subroutine scratch_discard
71 
72  subroutine scratch_destroy(this)
73  class(scratchfilebuffertype) :: this
74  if (this%iun > 0) then
75  close (this%iun)
76  this%iun = 0
77  end if
78  this%nrecords = 0
79  end subroutine scratch_destroy
80 
81 end module scratchfilebuffermodule
subroutine init()
Definition: GridSorting.f90:25
subroutine pstop(status, message)
Stop the program, optionally specifying an error status code.
Definition: ErrorUtil.f90:24
This module defines variable data types.
Definition: kind.f90:8
Particle event buffering strategies.
subroutine save_record(iun, rec, csv)
Save an event record to a binary or CSV file.
Scratch-file particle event buffer module.
subroutine scratch_destroy(this)
subroutine scratch_append(this, rec)
subroutine scratch_discard(this)
subroutine scratch_init(this)
subroutine scratch_flush(this, files)
Output file containing all or some particle pathlines.
Scratch-file particle event buffer. Records are written to an unformatted sequential scratch file tha...