Neko 1.99.6
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
generic_file.f90
Go to the documentation of this file.
1! Copyright (c) 2019-2023, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32!
34 use num_types, only : rp
35 use utils, only : neko_warning, neko_error, &
37 use comm, only : pe_rank, neko_comm
38 use mpi_f08, only : mpi_bcast, mpi_logical
39 implicit none
40
42 type, abstract :: generic_file_t
43 character(len=1024), private :: fname
44 integer, private :: counter = -1
45 integer, private :: start_counter = 0
47 logical :: serial = .false.
48 logical :: overwrite = .false.
49 contains
51 procedure :: init => generic_file_init
52
53 procedure(generic_file_write), deferred :: write
54
55 procedure(generic_file_read), deferred :: read
57 procedure :: get_fname => generic_file_get_fname
59 procedure :: get_next_output_fname => generic_file_get_next_output_fname
61 procedure :: get_base_fname => generic_file_get_base_fname
63 procedure :: set_counter => generic_file_set_counter
65 procedure :: get_counter => generic_file_get_counter
67 procedure :: set_start_counter => generic_file_set_start_counter
69 procedure :: get_start_counter => generic_file_get_start_counter
71 procedure :: increment_counter => generic_file_increment_counter
73 procedure :: check_exists => generic_file_check_exists
75 procedure :: set_overwrite => generic_file_set_overwrite
76 end type generic_file_t
77
78 abstract interface
79 subroutine generic_file_write(this, data, t)
80 import :: generic_file_t
81 import :: rp
82 class(generic_file_t), intent(inout) :: this
83 class(*), target, intent(in) :: data
84 real(kind=rp), intent(in), optional :: t
85 end subroutine generic_file_write
86 end interface
87
88 abstract interface
89 subroutine generic_file_read(this, data)
90 import :: generic_file_t
91 class(generic_file_t) :: this
92 class(*), target, intent(inout) :: data
93 end subroutine generic_file_read
94 end interface
95
96contains
97
100 subroutine generic_file_init(this, fname)
101 class(generic_file_t) :: this
102 character(len=*) :: fname
103
104 this%fname = fname
105 this%counter = -1
106
107 end subroutine generic_file_init
108
110 function generic_file_get_fname(this) result(fname)
111 class(generic_file_t), intent(in) :: this
112 character(len=1024) :: fname
113 integer :: suffix_pos
114 character(len=5) :: id_str
115
116 if (this%counter .ne. -1) then
117 suffix_pos = filename_suffix_pos(this%fname)
118 write(id_str, '(i5.5)') this%counter
119 fname = trim(this%fname(1:suffix_pos-1)) // id_str // &
120 this%fname(suffix_pos:)
121 else
122 fname = this%fname
123 end if
124
125 end function generic_file_get_fname
126
128 function generic_file_get_next_output_fname(this) result(fname)
129 class(generic_file_t), intent(in) :: this
130 character(len=1024) :: fname
131
132 fname = this%get_fname()
133
135
137 function generic_file_get_base_fname(this) result(fname)
138 class(generic_file_t), intent(in) :: this
139 character(len=1024) :: fname
140 integer :: suffix_pos
141
142 fname = trim(this%fname)
143
144 end function generic_file_get_base_fname
145
147 subroutine generic_file_set_counter(this, n)
148 class(generic_file_t), intent(inout) :: this
149 integer, intent(in) :: n
150 this%counter = n
151 end subroutine generic_file_set_counter
152
155 class(generic_file_t), intent(inout) :: this
156
157 if (this%counter .eq. -1) then
158 this%counter = this%start_counter
159 else
160 this%counter = this%counter + 1
161 end if
162 end subroutine generic_file_increment_counter
163
166 class(generic_file_t), intent(inout) :: this
167 integer, intent(in) :: n
168 this%start_counter = n
169 end subroutine generic_file_set_start_counter
170
172 pure function generic_file_get_counter(this) result(n)
173 class(generic_file_t), intent(in) :: this
174 integer :: n
175 n = this%counter
176 end function generic_file_get_counter
177
179 pure function generic_file_get_start_counter(this) result(n)
180 class(generic_file_t), intent(in) :: this
181 integer :: n
182 n = this%start_counter
184
187 class(generic_file_t), intent(in) :: this
188 character(len=1024) :: fname
189 logical :: file_exists
190 integer :: neko_mpi_ierr
191
192 fname = this%get_fname()
193 file_exists = .false.
194
195 if (pe_rank .eq. 0 .or. this%serial) then
196 ! Stop if the file does not exist
197 inquire(file = fname, exist = file_exists)
198 end if
199
200 if (.not. this%serial) then
201 call mpi_bcast(file_exists, 1, mpi_logical, 0, neko_comm, neko_mpi_ierr)
202 end if
203
204 if (.not. file_exists) then
205 call neko_error('File does not exist: ' // trim(fname))
206 end if
207
208 end subroutine generic_file_check_exists
209
211 subroutine generic_file_set_overwrite(this, overwrite)
212 class(generic_file_t), intent(inout) :: this
213 logical, intent(in) :: overwrite
214 character(len=80) :: suffix
215
216 call filename_suffix(this%get_fname(), suffix)
217 call neko_warning("No set_overwrite defined for " // trim(suffix) // " yet")
218 end subroutine generic_file_set_overwrite
219end module generic_file
Definition comm.F90:1
integer, public pe_rank
MPI rank.
Definition comm.F90:59
type(mpi_comm), public neko_comm
MPI communicator.
Definition comm.F90:46
Module for file I/O operations.
Definition file.f90:34
character(len=1024) function generic_file_get_fname(this)
Get a file's name.
subroutine generic_file_check_exists(this)
check if the file exists
pure integer function generic_file_get_counter(this)
Get the file counter.
subroutine generic_file_set_start_counter(this, n)
Set the file start counter to n.
subroutine generic_file_increment_counter(this)
Increment the file counter by 1.
subroutine generic_file_init(this, fname)
Generic file constructor.
character(len=1024) function generic_file_get_base_fname(this)
Get base name without counter.
pure integer function generic_file_get_start_counter(this)
Get the file start counter.
character(len=1024) function generic_file_get_next_output_fname(this)
Get the file name generated by the next write.
subroutine generic_file_set_overwrite(this, overwrite)
Set overwrite mode.
subroutine generic_file_set_counter(this, n)
Set the file counter to n.
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:12
Utilities.
Definition utils.f90:35
subroutine, public neko_warning(warning_msg)
Reports a warning to standard output.
Definition utils.f90:398
subroutine, public filename_suffix(fname, suffix)
Extract a filename's suffix.
Definition utils.f90:124
pure integer function, public filename_suffix_pos(fname)
Find position (in the string) of a filename's suffix.
Definition utils.f90:74
A generic file handler.