Neko 1.99.3
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
case_file_utils.f90
Go to the documentation of this file.
1! Copyright (c) 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!
35submodule(json_utils) case_file_utils
37 use vector, only : vector_t
38 use math, only : abscmp
39 use num_types, only : rp
40 implicit none
41
42contains
43
53 module subroutine json_get_or_lookup_real(json, name, val)
54 type(json_file), intent(inout) :: json
55 character(len=*), intent(in) :: name
56 real(kind=sp), intent(out) :: val
57
58 character(len=:), allocatable :: reg_name
59 logical :: found
60 integer :: var_type
61
62 call json%info(name, found = found, var_type = var_type)
63 if (found .and. (var_type .ne. 6) .and. (var_type .ne. 7)) then
64 call neko_error("Parameter " // name // &
65 " is neither a real nor a string.")
66 end if
67
68 ! Try to find a real
69 call json%get(name, val, found)
70 ! The value is retrieved from the JSON keyword, we are done
71 if (found) return
72
73 ! Try to find a string. It must exist
74 call json_get(json, name, reg_name)
75
76 ! Retrieve the value from the registry
77 val = real(neko_const_registry%get_real_scalar(reg_name), kind=sp)
78 end subroutine json_get_or_lookup_real
79
84 module subroutine json_get_or_lookup_double(json, name, val)
85 type(json_file), intent(inout) :: json
86 character(len=*), intent(in) :: name
87 real(kind=dp), intent(out) :: val
88
89 character(len=:), allocatable :: reg_name
90 logical :: found
91 integer :: var_type
92
93 call json%info(name, found = found, var_type = var_type)
94 if (found .and. (var_type .ne. 6) .and. (var_type .ne. 7)) then
95 call neko_error("Parameter " // name // &
96 " is neither a real nor a string.")
97 end if
98
99 ! Try to find a real
100 call json%get(name, val, found)
101 ! The value is retrieved from the JSON keyword, we are done
102 if (found) return
103
104 ! Try to find a string. It must exist
105 call json_get(json, name, reg_name)
106
107 ! Retrieve the value from the registry
108 val = real(neko_const_registry%get_real_scalar(reg_name), kind=dp)
109 end subroutine json_get_or_lookup_double
110
121 module subroutine json_get_or_lookup_integer(json, name, val)
122 type(json_file), intent(inout) :: json
123 character(len=*), intent(in) :: name
124 integer, intent(out) :: val
125
126 character(len=:), allocatable :: reg_name
127 logical :: found
128 integer :: var_type
129
130 call json%info(name, found = found, var_type = var_type)
131 if (found .and. (var_type .ne. 5) .and. (var_type .ne. 7)) then
132 call neko_error("Parameter " // name // &
133 " is neither an integer nor a string.")
134 end if
135
136 ! Try to find an int
137 call json%get(name, val, found)
138 ! The value is retrieved from the JSON keyword, we are done
139 if (found) return
140
141 ! Try to find a string. It must exist
142 call json_get(json, name, reg_name)
143
144 ! Retrieve the value from the registry
145 val = neko_const_registry%get_integer_scalar(reg_name)
146 end subroutine json_get_or_lookup_integer
147
159 module subroutine json_get_or_lookup_or_default_real(json, name,&
160 val, default)
161 type(json_file), intent(inout) :: json
162 character(len=*), intent(in) :: name
163 real(kind=sp), intent(out) :: val
164 real(kind=sp), intent(in) :: default
165
166 character(len=:), allocatable :: reg_name
167 logical :: found
168 integer :: var_type
169
170 call json%info(name, found = found, var_type = var_type)
171 if (found .and. (var_type .ne. 6) .and. (var_type .ne. 7)) then
172 call neko_error("Parameter " // name // &
173 " is neither a real nor a string.")
174 end if
175
176 call json%get(name, val, found)
177 ! The value is retrieved from the JSON keyword, we are done
178 if (found) return
179
180 ! Try to find a string.
181 call json%get(name, reg_name, found)
182 if (.not. found) then
183 ! We use another call here instead of just assigning the defaut value
184 ! to handle whether defaults are allowed as per `json_no_defaults`.
185 call json_get_or_default(json, name, val, default)
186 else
187 ! Retrieve the array from the registry
188 val = real(neko_const_registry%get_real_scalar(reg_name), kind=sp)
189 end if
190 end subroutine json_get_or_lookup_or_default_real
191
197 module subroutine json_get_or_lookup_or_default_double(json, name, &
198 val, default)
199 type(json_file), intent(inout) :: json
200 character(len=*), intent(in) :: name
201 real(kind=dp), intent(out) :: val
202 real(kind=dp), intent(in) :: default
203
204 character(len=:), allocatable :: reg_name
205 logical :: found
206 integer :: var_type
207
208 call json%info(name, found = found, var_type = var_type)
209 if (found .and. (var_type .ne. 6) .and. (var_type .ne. 7)) then
210 call neko_error("Parameter " // name // &
211 " is neither a real nor a string.")
212 end if
213
214 call json%get(name, val, found)
215 ! The value is retrieved from the JSON keyword, we are done
216 if (found) return
217
218 ! Try to find a string.
219 call json%get(name, reg_name, found)
220 if (.not. found) then
221 ! We use another call here instead of just assigning the defaut value
222 ! to handle whether defaults are allowed as per `json_no_defaults`.
223 call json_get_or_default(json, name, val, default)
224 else
225 ! Retrieve the array from the registry
226 val = real(neko_const_registry%get_real_scalar(reg_name), kind=dp)
227 end if
228 end subroutine json_get_or_lookup_or_default_double
229
242 module subroutine json_get_or_lookup_or_default_integer(json, &
243 name, val, default)
244 type(json_file), intent(inout) :: json
245 character(len=*), intent(in) :: name
246 integer, intent(out) :: val
247 integer, intent(in) :: default
248
249 character(len=:), allocatable :: reg_name
250 logical :: found
251 integer :: var_type
252
253 call json%info(name, found = found, var_type = var_type)
254 if (found .and. (var_type .ne. 5) .and. (var_type .ne. 7)) then
255 call neko_error("Parameter " // name // &
256 " is neither an integer nor a string.")
257 end if
258
259 call json%get(name, val, found)
260 ! The value is retrieved from the JSON keyword, we are done
261 if (found) return
262
263 ! Try to find a string.
264 call json%get(name, reg_name, found)
265 if (.not. found) then
266 ! We use another call here instead of just assigning the defaut value
267 ! to handle whether defaults are allowed as per `json_no_defaults`.
268 call json_get_or_default(json, name, val, default)
269 else
270 ! Retrieve the array from the registry
271 val = neko_const_registry%get_integer_scalar(reg_name)
272 end if
273 end subroutine json_get_or_lookup_or_default_integer
274
284 module subroutine json_get_or_lookup_real_array(json, name, val)
285 type(json_file), intent(inout) :: json
286 character(len=*), intent(in) :: name
287 real(kind=sp), allocatable, intent(inout) :: val(:)
288
289 type(vector_t), pointer :: vec_ptr
290 logical :: found
291 character(len=:), allocatable :: reg_name
292 integer :: var_type
293
294 call json%info(name, found = found, var_type = var_type)
295 if (found .and. (var_type .ne. 3) .and. (var_type .ne. 7)) then
296 call neko_error("Parameter " // name // &
297 " is neither an array nor a string.")
298 end if
299
300 ! Try to find a real array
301 call json%get(name, val, found)
302 ! The value is retrieved from the JSON keyword, we are done
303 if (found) return
304
305 ! Finding an array failed. Check if it is a string, otherwise error.
306 ! If found is false here, json_get will emit the correct error.
307 call json%info(name, found = found, var_type = var_type)
308 if (found .and. (var_type .ne. 7)) then
309 call neko_error("Parameter " // name // &
310 " is neither a real array nor a string.")
311 end if
312
313 ! Try to find a string. It must exist
314 call json_get(json, name, reg_name)
315
316 ! Retrieve the array from the registry
317 vec_ptr => neko_const_registry%get_vector(reg_name)
318 val = real(vec_ptr%x, kind=sp)
319 end subroutine json_get_or_lookup_real_array
320
325 module subroutine json_get_or_lookup_double_array(json, name, val)
326 type(json_file), intent(inout) :: json
327 character(len=*), intent(in) :: name
328 real(kind=dp), allocatable, intent(inout) :: val(:)
329
330 type(vector_t), pointer :: vec_ptr
331 logical :: found
332 character(len=:), allocatable :: reg_name
333 integer :: var_type
334
335 ! Try to find a real array
336 call json%get(name, val, found)
337 ! The value is retrieved from the JSON keyword, we are done
338 if (found) return
339
340 ! Finding an array failed. Check if it is a string, otherwise error.
341 ! If found is false here, json_get will emit the correct error.
342 call json%info(name, found = found, var_type = var_type)
343 if (found .and. (var_type .ne. 7)) then
344 call neko_error("Parameter " // name // &
345 " is neither a real array nor a string.")
346 end if
347
348 ! Try to find a string. It must exist
349 call json_get(json, name, reg_name)
350
351 ! Retrieve the array from the registry
352 vec_ptr => neko_const_registry%get_vector(reg_name)
353 val = real(vec_ptr%x, kind=dp)
354 end subroutine json_get_or_lookup_double_array
355
365 module subroutine json_get_or_lookup_integer_array(json, name, &
366 val)
367 type(json_file), intent(inout) :: json
368 character(len=*), intent(in) :: name
369 integer, allocatable, intent(inout) :: val(:)
370
371 type(vector_t), pointer :: vec_ptr
372 logical :: found
373 character(len=:), allocatable :: reg_name
374 integer :: i
375 integer :: var_type
376
377 ! Try to find an integer array
378 call json%get(name, val, found)
379 ! The value is retrieved from the JSON keyword, we are done
380 if (found) return
381
382 ! Finding an array failed. Check if it is a string, otherwise error.
383 ! If found is false here, json_get will emit the correct error.
384 call json%info(name, found = found, var_type = var_type)
385 if (found .and. (var_type .ne. 7)) then
386 call neko_error("Parameter " // name // &
387 " is neither an integer array nor a string.")
388 end if
389
390 ! Try to find a string. It must exist
391 call json_get(json, name, reg_name)
392
393 ! Retrieve the array from the registry
394 vec_ptr => neko_const_registry%get_vector(reg_name)
395 val = int(vec_ptr%x)
396
397 do i = 1, size(val)
398 if (.not. abscmp(real(val(i), kind=rp), vec_ptr%x(i))) then
399 call neko_error("json_get_or_lookup_integer_array: " &
400 // "Value retrieved from registry entry '" // reg_name &
401 // "' is not an integer array.")
402 end if
403 end do
404 end subroutine json_get_or_lookup_integer_array
405
406end submodule case_file_utils
double real
Utilities for retrieving parameters from the case files.
Definition math.f90:60
integer, parameter, public dp
Definition num_types.f90:9
integer, parameter, public sp
Definition num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:12
Defines a registry for storing solution fields.
Definition registry.f90:34
type(registry_t), target, public neko_const_registry
This registry is used to store user-defined scalars and vectors, provided under the constants section...
Definition registry.f90:150
Defines a vector.
Definition vector.f90:34