Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
file.f90
Go to the documentation of this file.
1! Copyright (c) 2019-2026, 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!
33!> Module for file I/O operations.
34module file
36 use num_types, only : dp
38 use nmsh_file, only : nmsh_file_t
39 use chkp_file, only : chkp_file_t
40 use map_file, only : map_file_t
41 use rea_file, only : rea_file_t
42 use re2_file, only : re2_file_t
43 use bp_file, only : bp_file_t
44 use fld_file, only : fld_file_t
46 use vtk_file, only : vtk_file_t
47 use vtkhdf_file, only : vtkhdf_file_t
48 use stl_file, only : stl_file_t
49 use csv_file, only : csv_file_t
50 use hdf5_file, only : hdf5_file_t
51 implicit none
52
53 !> A wrapper around a polymorphic `generic_file_t` that handles its init.
54 !! This is essentially a factory for `generic_file_t` descendants additionally
55 !! handling special CSV file parameters (header and precision).
56 type file_t
57 class(generic_file_t), allocatable :: file_type
58 contains
59 !> Constructor
60 procedure, pass (this) :: init => file_init
61 !> Writes data to a file.
62 procedure :: write => file_write
63 !> Read @a data from a file.
64 procedure :: read => file_read
65 !> Get a file's name.
66 procedure :: get_fname => file_get_fname
67 !> Get a file's base name.
68 procedure :: get_base_fname => file_get_base_fname
69 !> Get a file's counter.
70 procedure :: get_counter => file_get_counter
71 !> Set a file's counter.
72 procedure :: set_counter => file_set_counter
73 !> Set a file's start counter.
74 procedure :: set_start_counter => file_set_start_counter
75 !> Set a file's header.
76 procedure :: set_header => file_set_header
77 !> Set a file's output precision.
78 procedure :: set_precision => file_set_precision
79 !> Set a file's output layout.
80 procedure :: set_layout => file_set_layout
81 !> Sets the file's overwrite flag.
82 procedure, pass (this) :: set_overwrite => file_set_overwrite
83 !> Enable or disable subdivision of spectral elements.
84 procedure :: set_subdivide => file_set_subdivide
85 !> File operation destructor.
86 procedure, pass(this) :: free => file_free
87 end type file_t
88
89contains
90
91 !> Constructor.
92 !! @param fname Filename.
93 subroutine file_init(this, fname, header, precision, layout, overwrite)
94 class(file_t), intent(inout) :: this
95 character(len=*), intent(in) :: fname
96 character(len=*), intent(in), optional :: header
97 integer, intent(in), optional :: precision
98 integer, intent(in), optional :: layout
99 logical, intent(in), optional :: overwrite
100 character(len=80) :: suffix
101
102 call filename_suffix(fname, suffix)
103
104 call this%free()
105
106 select case (trim(suffix))
107 case ("rea")
108 allocate(rea_file_t::this%file_type)
109 case ("re2")
110 allocate(re2_file_t::this%file_type)
111 case ("map")
112 allocate(map_file_t::this%file_type)
113 case ("vtk")
114 allocate(vtk_file_t::this%file_type)
115 case ("nmsh")
116 allocate(nmsh_file_t::this%file_type)
117 case ("bp")
118 allocate(bp_file_t::this%file_type)
119 case ("fld")
120 allocate(fld_file_t::this%file_type)
121 case ("chkp")
122 allocate(chkp_file_t::this%file_type)
123 case ("stl")
124 allocate(stl_file_t::this%file_type)
125 case ("csv")
126 allocate(csv_file_t::this%file_type)
127 this%file_type%serial = .true.
128 case ("hdf5", "h5")
129 allocate(hdf5_file_t::this%file_type)
130 case ("vtkhdf")
131 allocate(vtkhdf_file_t::this%file_type)
132 case default
133 call neko_error('Unknown file format: "' // trim(suffix) // '"')
134 end select
135
136 call this%file_type%init(fname)
137
138 if (present(header)) then
139 call this%set_header(header)
140 end if
141
142 if (present(precision)) then
143 call this%set_precision(precision)
144 end if
145
146 if (present(layout) .and. (suffix .eq. "bp")) then
147 call this%set_layout(layout)
148 end if
149
150 if (present(overwrite)) then
151 call this%set_overwrite(overwrite)
152 end if
153
154 end subroutine file_init
155
156 !> File operation destructor.
157 subroutine file_free(this)
158 class(file_t), intent(inout) :: this
159
160 if (allocated(this%file_type)) then
161 ! Release resources owned by the backend before deallocating it.
162 select type (ft => this%file_type)
163 class is (fld_file_t)
164 call ft%set_mask()
165 class is (hdf5_file_t)
166 call ft%free()
167 end select
168 deallocate(this%file_type)
169 end if
170
171 end subroutine file_free
172
173 !> Writes data to a file.
174 !! @param data Data to be written.
175 subroutine file_write(this, data, t)
176 class(file_t), intent(inout) :: this
177 class(*), intent(inout) :: data
178 real(kind=dp), intent(in), optional :: t
179
180 call this%file_type%write(data, t = t)
181
182 end subroutine file_write
183
184 !> Read @a data from a file.
185 !! @param data Read data.
186 subroutine file_read(this, data)
187 class(file_t), intent(in) :: this
188 class(*), intent(inout) :: data
189
190 call this%file_type%read(data)
191
192 end subroutine file_read
193
194 !> Get a file's name.
195 function file_get_fname(this) result(fname)
196 class(file_t), intent(in) :: this
197 character(len=1024) :: fname
198
199 fname = ""
200
201 select type (ft => this%file_type)
202 class is (generic_file_t)
203 fname = ft%get_fname()
204 end select
205
206 end function file_get_fname
207
208 !> Get a file's base name.
209 function file_get_base_fname(this) result(fname)
210 class(file_t), intent(in) :: this
211 character(len=1024) :: fname
212
213 fname = ""
214
215 select type (ft => this%file_type)
216 class is (generic_file_t)
217 fname = ft%get_base_fname()
218 end select
219
220 end function file_get_base_fname
221
222 !> Get a file's counter.
223 function file_get_counter(this) result(n)
224 class(file_t), intent(inout) :: this
225 integer :: n
226 n = 0
227
228 select type (ft => this%file_type)
229 class is (generic_file_t)
230 n = ft%get_counter()
231 end select
232
233 end function file_get_counter
234
235 !> Set a file's counter.
236 subroutine file_set_counter(this, n)
237 class(file_t), intent(inout) :: this
238 integer, intent(in) :: n
239
240 select type (ft => this%file_type)
241 class is (generic_file_t)
242 call ft%set_counter(n)
243 end select
244
245 end subroutine file_set_counter
246
247 !> Set a file's start counter.
248 subroutine file_set_start_counter(this, n)
249 class(file_t), intent(inout) :: this
250 integer, intent(in) :: n
251
252 select type (ft => this%file_type)
253 class is (generic_file_t)
254 call ft%set_start_counter(n)
255 end select
256
257 end subroutine file_set_start_counter
258
259 !> Set a file's header.
260 subroutine file_set_header(this, hd)
261 class(file_t), intent(inout) :: this
262 character(len=*), intent(in) :: hd
263 character(len=80) :: suffix
264
265 select type (ft => this%file_type)
266 class is (csv_file_t)
267 call ft%set_header(hd)
268 class default
269 call filename_suffix(this%file_type%get_fname(), suffix)
270 call neko_warning("No set_header defined for " // trim(suffix) // " yet")
271 end select
272
273 end subroutine file_set_header
274
275 !> Set a file's output precision.
276 !! @param precision Precision as defined in `num_types`.
277 subroutine file_set_precision(this, precision)
278 class(file_t), intent(inout) :: this
279 integer, intent(in) :: precision
280 character(len=80) :: suffix
281
282 select type (ft => this%file_type)
283 type is (fld_file_t)
284 call ft%set_precision(precision)
285 type is (bp_file_t)
286 call ft%set_precision(precision)
287 type is (vtkhdf_file_t)
288 call ft%set_precision(precision)
289 class default
290 call filename_suffix(this%file_type%get_fname(), suffix)
291 call neko_warning("No precision strategy defined for " // trim(suffix) &
292 // " files")
293 end select
294
295 end subroutine file_set_precision
296
297 !> Set a file's output layout.
298 !! @param layout The data layout as defined in bp_file.f90 and src/io/buffer/.
299 subroutine file_set_layout(this, layout)
300 class(file_t), intent(inout) :: this
301 integer, intent(in) :: layout
302 character(len=80) :: suffix
303
304 select type (ft => this%file_type)
305 type is (bp_file_t)
306 call ft%set_layout(layout)
307 class default
308 call filename_suffix(this%file_type%get_fname(), suffix)
309 call neko_warning("No set_layout defined for " // trim(suffix) // " yet")
310 end select
311
312 end subroutine file_set_layout
313
314 !> Sets the file's overwrite flag.
315 subroutine file_set_overwrite(this, overwrite)
316 class(file_t), intent(inout) :: this
317 logical, intent(in) :: overwrite
318 character(len=80) :: suffix
319
320 select type (ft => this%file_type)
321 class is (generic_file_t)
322 call ft%set_overwrite(overwrite)
323 end select
324 end subroutine file_set_overwrite
325
326 !> Enable or disable subdivision of spectral elements into linear sub-cells.
327 !! Only has effect for VTKHDF files; warns for other formats.
328 !! @param subdivide Whether to subdivide into linear sub-cells.
329 subroutine file_set_subdivide(this, subdivide)
330 class(file_t), intent(inout) :: this
331 logical, intent(in) :: subdivide
332 character(len=80) :: suffix
333
334 select type (ft => this%file_type)
335 type is (vtkhdf_file_t)
336 call ft%set_subdivide(subdivide)
337 class default
338 if (subdivide) then
339 call filename_suffix(this%file_type%get_fname(), suffix)
340 call neko_warning("Subdivide output not supported for " // &
341 trim(suffix) // " files")
342 end if
343 end select
344 end subroutine file_set_subdivide
345
346end module file
ADIOS2 bp file format.
Definition bp_file.F90:36
Neko legacy checkpoint file format.
Definition chkp_file.f90:36
File format for .csv files, used for any read/write operations involving floating point data.
Definition csv_file.f90:35
Module for file I/O operations.
Definition file.f90:34
subroutine file_set_header(this, hd)
Set a file's header.
Definition file.f90:261
subroutine file_set_overwrite(this, overwrite)
Sets the file's overwrite flag.
Definition file.f90:316
subroutine file_set_start_counter(this, n)
Set a file's start counter.
Definition file.f90:249
subroutine file_set_counter(this, n)
Set a file's counter.
Definition file.f90:237
character(len=1024) function file_get_fname(this)
Get a file's name.
Definition file.f90:196
subroutine file_set_layout(this, layout)
Set a file's output layout.
Definition file.f90:300
integer function file_get_counter(this)
Get a file's counter.
Definition file.f90:224
subroutine file_read(this, data)
Read data from a file.
Definition file.f90:187
subroutine file_set_precision(this, precision)
Set a file's output precision.
Definition file.f90:278
subroutine file_set_subdivide(this, subdivide)
Enable or disable subdivision of spectral elements into linear sub-cells. Only has effect for VTKHDF ...
Definition file.f90:330
subroutine file_free(this)
File operation destructor.
Definition file.f90:158
subroutine file_init(this, fname, header, precision, layout, overwrite)
Constructor.
Definition file.f90:94
character(len=1024) function file_get_base_fname(this)
Get a file's base name.
Definition file.f90:210
subroutine file_write(this, data, t)
Writes data to a file.
Definition file.f90:176
Simple module to handle fld file series. Provides an interface to the different fields sotred in a fl...
NEKTON fld file format.
Definition fld_file.f90:35
HDF5 file format.
Definition hdf5_file.F90:34
NEKTON map file.
Definition map_file.f90:35
Neko binary mesh data.
Definition nmsh_file.f90:34
integer, parameter, public dp
Definition num_types.f90:10
NEKTON mesh data in re2 format.
Definition re2_file.f90:35
NEKTON session data reader.
Definition rea_file.f90:35
Stereolithography (STL) file.
Definition stl_file.f90:34
Utilities.
Definition utils.f90:35
subroutine, public neko_warning(warning_msg)
Reports a warning to standard output.
Definition utils.f90:454
subroutine, public filename_suffix(fname, suffix)
Extract a filename's suffix.
Definition utils.f90:163
Legacy VTK file format.
Definition vtk_file.f90:35
VTKHDF file format.
Interface for ADIOS2 bp files.
Definition bp_file.F90:71
Interface for Neko legacy checkpoint files.
Definition chkp_file.f90:95
A wrapper around a polymorphic generic_file_t that handles its init. This is essentially a factory fo...
Definition file.f90:56
Interface for NEKTON fld files.
Definition fld_file.f90:66
A generic file handler.
Interface for HDF5 files.
Interface for NEKTON map files.
Definition map_file.f90:45
Interface for Neko nmsh files.
Definition nmsh_file.f90:61
Interface for NEKTON re2 files.
Definition re2_file.f90:65
Interface for NEKTON ascii files.
Definition rea_file.f90:61
Interface for STL files.
Definition stl_file.f90:50
Interface for legacy VTK files.
Definition vtk_file.f90:52
Interface for HDF5 files.