|
| 1 | +!> @brief Particle track event buffer module. |
| 2 | +!! |
| 3 | +!! Defines the abstract ParticleTrackEventBufferType, the TrackRecordType |
| 4 | +!! and ParticleTrackFileType data types, and the save_record helper used |
| 5 | +!! by concrete buffer implementations. |
| 6 | +!< |
| 7 | +module ParticleTrackEventBufferModule |
| 8 | + |
| 9 | + use KindModule, only: DP, I4B, LGP |
| 10 | + use ErrorUtilModule, only: pstop |
| 11 | + |
| 12 | + implicit none |
| 13 | + |
| 14 | + character(len=*), parameter, public :: TRACKHEADER = & |
| 15 | + 'kper,kstp,imdl,iprp,irpt,ilay,icell,izone,& |
| 16 | + &istatus,ireason,trelease,t,x,y,z,name' |
| 17 | + |
| 18 | + character(len=*), parameter, public :: TRACKDTYPES = & |
| 19 | + '<i4,<i4,<i4,<i4,<i4,<i4,<i4,<i4,& |
| 20 | + &<i4,<i4,<f8,<f8,<f8,<f8,<f8,|S40' |
| 21 | + |
| 22 | + !> @brief Flat record of a particle track event. |
| 23 | + type :: TrackRecordType |
| 24 | + integer(I4B) :: kper, kstp, imdl, iprp, irpt, ilay, icu, izone |
| 25 | + integer(I4B) :: istatus, ireason |
| 26 | + real(DP) :: trelease, ttrack, x, y, z |
| 27 | + character(len=40) :: name |
| 28 | + end type TrackRecordType |
| 29 | + |
| 30 | + !> @brief Output file containing all or some particle pathlines. |
| 31 | + !! |
| 32 | + !! Can be associated with a particle release point (PRP) package |
| 33 | + !! or with an entire model, and can be binary or text (CSV). |
| 34 | + !< |
| 35 | + type :: ParticleTrackFileType |
| 36 | + private |
| 37 | + integer(I4B), public :: iun = 0 !< file unit number |
| 38 | + logical(LGP), public :: csv = .false. !< whether the file is binary or CSV |
| 39 | + integer(I4B), public :: iprp = -1 !< -1 is model-level file, 0 is exchange PRP |
| 40 | + end type ParticleTrackFileType |
| 41 | + |
| 42 | + !> @brief Event buffering strategy |
| 43 | + type, abstract :: ParticleTrackEventBufferType |
| 44 | + integer(I4B) :: nrecords = 0 !< number of records stored |
| 45 | + contains |
| 46 | + procedure :: init => buffer_init !< open/allocate resources |
| 47 | + procedure(buffer_append), deferred :: append !< buffer one record |
| 48 | + procedure(buffer_flush), deferred :: flush !< write buffered records, reset |
| 49 | + procedure(buffer_simple), deferred :: discard !< reset without writing |
| 50 | + procedure(buffer_simple), deferred :: destroy !< release resources |
| 51 | + end type ParticleTrackEventBufferType |
| 52 | + |
| 53 | + abstract interface |
| 54 | + subroutine buffer_append(this, rec) |
| 55 | + import ParticleTrackEventBufferType, TrackRecordType |
| 56 | + class(ParticleTrackEventBufferType) :: this |
| 57 | + type(TrackRecordType), intent(in) :: rec |
| 58 | + end subroutine buffer_append |
| 59 | + |
| 60 | + subroutine buffer_flush(this, files) |
| 61 | + import ParticleTrackEventBufferType, ParticleTrackFileType |
| 62 | + class(ParticleTrackEventBufferType) :: this |
| 63 | + type(ParticleTrackFileType), intent(in) :: files(:) |
| 64 | + end subroutine buffer_flush |
| 65 | + |
| 66 | + ! Shared interface for discard and destroy: both take only `this`. |
| 67 | + subroutine buffer_simple(this) |
| 68 | + import ParticleTrackEventBufferType |
| 69 | + class(ParticleTrackEventBufferType) :: this |
| 70 | + end subroutine buffer_simple |
| 71 | + end interface |
| 72 | + |
| 73 | +contains |
| 74 | + |
| 75 | + subroutine buffer_init(this) |
| 76 | + class(ParticleTrackEventBufferType) :: this |
| 77 | + end subroutine buffer_init |
| 78 | + |
| 79 | + !> @brief Save an event record to a binary or CSV file. |
| 80 | + subroutine save_record(iun, rec, csv) |
| 81 | + integer(I4B), intent(in) :: iun |
| 82 | + type(TrackRecordType), intent(in) :: rec |
| 83 | + logical(LGP), intent(in) :: csv |
| 84 | + |
| 85 | + if (csv) then |
| 86 | + write (iun, '(*(G0,:,","))') & |
| 87 | + rec%kper, rec%kstp, rec%imdl, rec%iprp, rec%irpt, & |
| 88 | + rec%ilay, rec%icu, rec%izone, rec%istatus, rec%ireason, & |
| 89 | + rec%trelease, rec%ttrack, rec%x, rec%y, rec%z, trim(rec%name) |
| 90 | + else |
| 91 | + write (iun) & |
| 92 | + rec%kper, rec%kstp, rec%imdl, rec%iprp, rec%irpt, & |
| 93 | + rec%ilay, rec%icu, rec%izone, rec%istatus, rec%ireason, & |
| 94 | + rec%trelease, rec%ttrack, rec%x, rec%y, rec%z, rec%name |
| 95 | + end if |
| 96 | + end subroutine save_record |
| 97 | + |
| 98 | +end module ParticleTrackEventBufferModule |
0 commit comments