Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
import_field_utils.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!
38 use file, only : file_t, file_free
39 use num_types, only : rp, dp
40 use field, only : field_t
41 use field_list, only : field_list_t
45 use logger, only : log_size, neko_log
46 use device, only : host_to_device
47 use json_module, only : json_file
49 implicit none
50 private
51
52 public :: import_fields
53
56 end interface import_fields
57
58contains
59
91 subroutine import_fields_from_json(fname, global_interp_subdict, mesh_fname, &
92 u, v, w, p, t, s_target_list, s_index_list, interpolate)
93 character(len=*), intent(in) :: fname
94 type(json_file), intent(inout) :: global_interp_subdict
95 character(len=*), intent(in), optional :: mesh_fname
96 type(field_t), pointer, intent(inout), optional :: u,v,w,p,t
97 type(field_list_t), intent(inout), optional :: s_target_list
98 integer, intent(in), optional :: s_index_list(:)
99 logical, intent(in), optional :: interpolate
100
101 real(kind=dp) :: tolerance, padding
102
103 call json_get_or_default(global_interp_subdict, "tolerance", &
104 tolerance, glob_interp_tol)
105 call json_get_or_default(global_interp_subdict, "padding", &
106 padding, glob_interp_pad)
107
108 call import_fields_from_params(fname, mesh_fname, &
109 u, v, w, p, t, s_target_list, s_index_list, &
110 interpolate, tolerance = tolerance, padding = padding)
111
112 end subroutine import_fields_from_json
113
147 subroutine import_fields_from_params(fname, mesh_fname, u, v, w, p, t, &
148 s_target_list, s_index_list, interpolate, tolerance, padding)
149 character(len=*), intent(in) :: fname
150 character(len=*), intent(in), optional :: mesh_fname
151 type(field_t), pointer, intent(inout), optional :: u,v,w,p,t
152 type(field_list_t), intent(inout), optional :: s_target_list
153 integer, intent(in), optional :: s_index_list(:)
154 logical, intent(in), optional :: interpolate
155 real(kind=dp), intent(in), optional :: tolerance
156 real(kind=dp), intent(in), optional :: padding
157
158 character(len=LOG_SIZE) :: log_buf
159 integer :: i
160
161 logical :: interpolate_
162
163 type(file_t) :: f
164 type(fld_file_data_t) :: fld_data
165
166 ! ---- Default values
167 interpolate_ = .false.
168 if (present(interpolate)) interpolate_ = interpolate
169 ! ----
170
171 call neko_log%section("Import fields")
172 call neko_log%message("File name : " // trim(fname))
173 write (log_buf, '(A,L1)') "Interpolation : ", interpolate_
174 call neko_log%message(log_buf)
175
176 !
177 ! Read the mesh field only when interpolation is enabled and a separate
178 ! mesh field was specified. The mesh file is optional; when omitted, the
179 ! coordinates are read from the field file itself.
180 !
181 if (interpolate_) then
182 if (present(mesh_fname)) then
183 ! For scalar IC. TODO: stop using the "none" fill info in scalar.
184 if (trim(mesh_fname) .ne. "none") then
185 call neko_log%message("Mesh file : " // trim(mesh_fname))
186 call read_fld_file(mesh_fname, fld_data)
187 end if
188 end if
189 end if
190
191 call read_fld_file(fname, fld_data)
192
193 !
194 ! Copy all field data to device (GPU) since everything is read on the CPU
195 !
196 if (present(u)) call fld_data%u%copy_from(host_to_device, .true.)
197 if (present(v)) call fld_data%v%copy_from(host_to_device, .true.)
198 if (present(w)) call fld_data%w%copy_from(host_to_device, .true.)
199 if (present(p)) call fld_data%p%copy_from(host_to_device, .true.)
200 if (present(t)) call fld_data%t%copy_from(host_to_device, .true.)
201 if (present(s_target_list)) then
202
203 if (present(s_index_list)) then
204 if (size(s_index_list) .ne. s_target_list%size()) then
205 call neko_error("Scalar lists must have same size!")
206 end if
207
208 do i = 1, size(s_index_list)
209 ! Take care that if we set i=0 we want temperature
210 if (s_index_list(i) .eq. 0) then
211 call fld_data%t%copy_from(host_to_device, .true.)
212 else
213 ! For scalar fields, require indices in 1:this%n_scalars
214 if (s_index_list(i) .lt. 1 .or. &
215 s_index_list(i) .gt. fld_data%n_scalars) then
216 call neko_error("s_index_list entry out of bounds")
217 end if
218 call fld_data%s(s_index_list(i))%copy_from(host_to_device, &
219 .true.)
220 end if
221 end do
222 else
223 do i = 1, s_target_list%size()
224 call fld_data%s(i)%copy_from(host_to_device, .true.)
225 end do
226 end if ! if present s_index_list
227
228 end if ! present s_tgt
229
230 !
231 ! Sync coordinates to device for the interpolation
232 ! NOTE: this will not throw an error if x,y,z are not allocated.
233 !
234 if (interpolate_) then
235 call fld_data%x%copy_from(host_to_device, .false.)
236 call fld_data%y%copy_from(host_to_device, .false.)
237 call fld_data%z%copy_from(host_to_device, .true.)
238 end if
239
240 ! Call the import of fields
241 call fld_data%import_fields(u, v, w, p, t, s_target_list, s_index_list, &
242 interpolate_, tolerance = tolerance, padding = padding)
243
244 call neko_log%end_section()
245 call fld_data%free()
246
247 end subroutine import_fields_from_params
248
251 subroutine read_fld_file(file_name, data)
252 character(len=*), intent(in) :: file_name
253 type(fld_file_data_t), intent(inout) :: data
254
255 type(file_t) :: f
256 integer :: sample_idx
257 character(len=NEKO_FNAME_LEN) :: fname_
258
259 ! Extract sample index from the file name
260 sample_idx = extract_fld_file_index(file_name, -1)
261
262 if (sample_idx .eq. -1) &
263 call neko_error("Invalid file name. The file format must be e.g. " // &
264 "'mean0.f00001'")
265
266 ! Change from "field0.f000*" to "field0.fld" for the fld reader
267 call filename_chsuffix(file_name, fname_, 'fld')
268
269 ! Initialize file object and set the desired sample
270 call f%init(trim(fname_))
271 call f%set_counter(sample_idx)
272
273 ! Read data and load into fld_file_data
274 call f%read(data)
275
276 call file_free(f)
277
278 end subroutine read_fld_file
279
280end module import_field_utils
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public host_to_device
Definition device.F90:48
Defines a field.
Definition field.f90:34
Module for file I/O operations.
Definition file.f90:34
subroutine file_free(this)
File operation destructor.
Definition file.f90:160
Simple module to handle fld file series. Provides an interface to the different fields sotred in a fl...
Implements global_interpolation given a dofmap.
real(kind=dp), parameter, public glob_interp_tol
real(kind=dp), parameter, public glob_interp_pad
Importation of fields from fld files.
subroutine import_fields_from_json(fname, global_interp_subdict, mesh_fname, u, v, w, p, t, s_target_list, s_index_list, interpolate)
Imports fields from an fld file, potentially with interpolation, with parameters provided in a JSON s...
subroutine import_fields_from_params(fname, mesh_fname, u, v, w, p, t, s_target_list, s_index_list, interpolate, tolerance, padding)
Imports fields from an fld file, potentially with interpolation.
subroutine read_fld_file(file_name, data)
Wrapper around the fld file reader. Translates a file name specified as "field0.f00132" into "field0....
Utilities for retrieving parameters from the case files.
Logging routines.
Definition log.f90:34
type(log_t), public neko_log
Global log stream.
Definition log.f90:80
integer, parameter, public log_size
Definition log.f90:46
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Utilities.
Definition utils.f90:35
integer function, public extract_fld_file_index(fld_filename, default_index)
Extracts the index of a field file. For example, "myfield.f00045" will return 45. If the suffix of th...
Definition utils.f90:209
integer, parameter, public neko_fname_len
Definition utils.f90:42
subroutine, public filename_chsuffix(fname, new_fname, new_suffix)
Change a filename's suffix.
Definition utils.f90:156
field_list_t, To be able to group fields together
A wrapper around a polymorphic generic_file_t that handles its init. This is essentially a factory fo...
Definition file.f90:56