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