Neko 1.99.1
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
json_utils.f90
Go to the documentation of this file.
1! Copyright (c) 2019-2024, 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!
35 use num_types, only : rp, dp, sp
36 use json_module, only : json_file, json_value, json_core
37 use utils, only : neko_error
38 implicit none
39 private
40
43
45 logical :: json_no_defaults = .false.
46
53 end interface json_get
54
61 end interface json_get_or_default
62
65 end interface json_extract_item
66contains
67
72 subroutine json_get_real(json, name, value)
73 type(json_file), intent(inout) :: json
74 character(len=*), intent(in) :: name
75 real(kind=sp), intent(out) :: value
76
77 if (.not. json%valid_path(name)) then
78 call neko_error("Parameter " // name // " missing from the case file")
79 end if
80
81 call json%get(name, value)
82 end subroutine json_get_real
83
88 subroutine json_get_double(json, name, value)
89 type(json_file), intent(inout) :: json
90 character(len=*), intent(in) :: name
91 real(kind=dp), intent(out) :: value
92
93 if (.not. json%valid_path(name)) then
94 call neko_error("Parameter " // name // " missing from the case file")
95 end if
96
97 call json%get(name, value)
98 end subroutine json_get_double
99
104 subroutine json_get_integer(json, name, value)
105 type(json_file), intent(inout) :: json
106 character(len=*), intent(in) :: name
107 integer, intent(out) :: value
108
109 if (.not. json%valid_path(name)) then
110 call neko_error("Parameter " // name // " missing from the case file")
111 end if
112
113 call json%get(name, value)
114 end subroutine json_get_integer
115
120 subroutine json_get_logical(json, name, value)
121 type(json_file), intent(inout) :: json
122 character(len=*), intent(in) :: name
123 logical, intent(out) :: value
124
125 if (.not. json%valid_path(name)) then
126 call neko_error("Parameter " // name // " missing from the case file")
127 end if
128
129 call json%get(name, value)
130 end subroutine json_get_logical
131
136 subroutine json_get_string(json, name, value)
137 type(json_file), intent(inout) :: json
138 character(len=*), intent(in) :: name
139 character(len=:), allocatable, intent(out) :: value
140
141 if (.not. json%valid_path(name)) then
142 call neko_error("Parameter " // name // " missing from the case file")
143 end if
144
145 call json%get(name, value)
146 end subroutine json_get_string
147
152 subroutine json_get_real_array(json, name, value)
153 type(json_file), intent(inout) :: json
154 character(len=*), intent(in) :: name
155 real(kind=sp), allocatable, intent(out) :: value(:)
156
157 if (.not. json%valid_path(name)) then
158 call neko_error("Parameter " // name // " missing from the case file")
159 end if
160
161 call json%get(name, value)
162 end subroutine json_get_real_array
163
168 subroutine json_get_double_array(json, name, value)
169 type(json_file), intent(inout) :: json
170 character(len=*), intent(in) :: name
171 real(kind=dp), allocatable, intent(out) :: value(:)
172
173 if (.not. json%valid_path(name)) then
174 call neko_error("Parameter " // name // " missing from the case file")
175 end if
176
177 call json%get(name, value)
178 end subroutine json_get_double_array
179
184 subroutine json_get_integer_array(json, name, value)
185 type(json_file), intent(inout) :: json
186 character(len=*), intent(in) :: name
187 integer, allocatable, intent(out) :: value(:)
188
189 if (.not. json%valid_path(name)) then
190 call neko_error("Parameter " // name // " missing from the case file")
191 end if
192
193 call json%get(name, value)
194 end subroutine json_get_integer_array
195
200 subroutine json_get_logical_array(json, name, value)
201 type(json_file), intent(inout) :: json
202 character(len=*), intent(in) :: name
203 logical, allocatable, intent(out) :: value(:)
204
205 if (.not. json%valid_path(name)) then
206 call neko_error("Parameter " // name // " missing from the case file")
207 end if
208
209 call json%get(name, value)
210 end subroutine json_get_logical_array
211
217 subroutine json_get_string_array(json, name, value, filler)
218 type(json_file), intent(inout) :: json
219 character(len=*), intent(in) :: name
220 character(len=*), allocatable, intent(out) :: value(:)
221 character(len=*), optional, intent(in) :: filler
222 logical :: found
223 type(json_value), pointer :: json_val, val_ptr
224 type(json_core) :: core
225 character(len=:), allocatable :: string_value
226 integer :: i, n_children
227
228 if (.not. json%valid_path(name)) then
229 call neko_error("Parameter " // name // " missing from the case file")
230 end if
231 call json%info(name, n_children = n_children)
232
233 if (.not. allocated(value)) then
234 allocate(value(n_children))
235 else if (len(value) .lt. n_children) then
236 deallocate(value)
237 allocate(value(n_children))
238 end if
239
240 call json%get(name, json_val, found)
241 call json%get_core(core)
242
243 do i = 1, n_children
244 call core%get_child(json_val, i, val_ptr, found)
245 call core%get(val_ptr, string_value)
246
247 if (len(string_value) .gt. 0) then
248 value(i) = string_value
249 else if (present(filler)) then
250 value(i) = filler
251 end if
252 end do
253
254 end subroutine json_get_string_array
255
257 subroutine json_get_subdict(json, key, output)
258 type(json_file), intent(inout) :: json
259 character(len=*), intent(in) :: key
260 type(json_file), intent(inout) :: output
261
262 type(json_value), pointer :: ptr
263 type(json_core) :: core
264 logical :: found
265 character(len=:), allocatable :: buffer
266
267 call json%get_core(core)
268 call json%get(key, ptr, found)
269
270 if (.not. found) then
271 call neko_error("Parameter " // &
272 trim(key) // " missing from the case file")
273 end if
274
275 call core%print_to_string(ptr, buffer)
276 call output%load_from_string(buffer)
277
278 end subroutine json_get_subdict
279
285 subroutine json_get_or_default_real(json, name, value, default)
286 type(json_file), intent(inout) :: json
287 character(len=*), intent(in) :: name
288 real(kind=sp), intent(out) :: value
289 real(kind=sp), intent(in) :: default
290 logical :: found
291
292 call json%get(name, value, found)
293
294 if ((.not. found) .and. (json_no_defaults .eqv. .false.)) then
295 value = default
296 call json%add(name, value)
297 else if (.not. found) then
298 call neko_error("Parameter " // name // " missing from the case file")
299 end if
300 end subroutine json_get_or_default_real
301
307 subroutine json_get_or_default_double(json, name, value, default)
308 type(json_file), intent(inout) :: json
309 character(len=*), intent(in) :: name
310 real(kind=dp), intent(out) :: value
311 real(kind=dp), intent(in) :: default
312 logical :: found
313
314 call json%get(name, value, found)
315
316 if ((.not. found) .and. (json_no_defaults .eqv. .false.)) then
317 value = default
318 call json%add(name, value)
319 else if (.not. found) then
320 call neko_error("Parameter " // name // " missing from the case file")
321 end if
322 end subroutine json_get_or_default_double
323
329 subroutine json_get_or_default_integer(json, name, value, default)
330 type(json_file), intent(inout) :: json
331 character(len=*), intent(in) :: name
332 integer, intent(out) :: value
333 integer, intent(in) :: default
334 logical :: found
335
336 call json%get(name, value, found)
337
338 if ((.not. found) .and. (json_no_defaults .eqv. .false.)) then
339 value = default
340 call json%add(name, value)
341 else if (.not. found) then
342 call neko_error("Parameter " // name // " missing from the case file")
343 end if
344 end subroutine json_get_or_default_integer
345
351 subroutine json_get_or_default_logical(json, name, value, default)
352 type(json_file), intent(inout) :: json
353 character(len=*), intent(in) :: name
354 logical, intent(out) :: value
355 logical, intent(in) :: default
356 logical :: found
357
358 call json%get(name, value, found)
359
360 if ((.not. found) .and. (json_no_defaults .eqv. .false.)) then
361 value = default
362 call json%add(name, value)
363 else if (.not. found) then
364 call neko_error("Parameter " // name // " missing from the case file")
365 end if
366 end subroutine json_get_or_default_logical
367
373 subroutine json_get_or_default_string(json, name, value, default)
374 type(json_file), intent(inout) :: json
375 character(len=*), intent(in) :: name
376 character(len=:), allocatable, intent(out) :: value
377 character(len=*), intent(in) :: default
378 logical :: found
379
380 call json%get(name, value, found)
381
382 if ((.not. found) .and. (json_no_defaults .eqv. .false.)) then
383 value = default
384 call json%add(name, value)
385 else if (.not. found) then
386 call neko_error("Parameter " // name // " missing from the case file")
387 end if
388 end subroutine json_get_or_default_string
389
395 subroutine json_extract_item_from_array(core, array, i, item)
396 type(json_core), intent(inout) :: core
397 type(json_value), pointer, intent(in) :: array
398 integer, intent(in) :: i
399 type(json_file), intent(inout) :: item
400 type(json_value), pointer :: ptr
401 logical :: found
402 character(len=:), allocatable :: buffer
403
404 call core%get_child(array, i, ptr, found)
405 call core%print_to_string(ptr, buffer)
406 call item%load_from_string(buffer)
407
408 end subroutine json_extract_item_from_array
409
415 subroutine json_extract_item_from_name(json, name, i, item)
416 type(json_file), intent(inout) :: json
417 character(len=*), intent(in) :: name
418 integer, intent(in) :: i
419 type(json_file), intent(out) :: item
420
421 type(json_core) :: core
422 type(json_value), pointer :: array
423 type(json_value), pointer :: ptr
424 logical :: found
425 character(len=:), allocatable :: buffer
426
427 call json%get_core(core)
428 call json%get(name, array, found)
429
430 if (.not. found) then
431 call neko_error("Parameter " // name // " missing from the case file")
432 end if
433
434 call core%get_child(array, i, ptr, found)
435 call core%print_to_string(ptr, buffer)
436 call item%load_from_string(buffer)
437
438 end subroutine json_extract_item_from_name
439
440end module json_utils
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Retrieves a parameter by name or throws an error.
Generic buffer that is extended with buffers of varying rank.
Definition buffer.F90:34
Utilities for retrieving parameters from the case files.
subroutine json_extract_item_from_name(json, name, i, item)
Extract ith item from a JSON array as a separate JSON object.
subroutine json_get_logical(json, name, value)
Retrieves a logical parameter by name or throws an error.
subroutine json_get_or_default_string(json, name, value, default)
Retrieves a string parameter by name or assigns a provided default value. In the latter case also add...
subroutine json_get_real_array(json, name, value)
Retrieves a real array parameter by name or throws an error.
subroutine json_get_subdict(json, key, output)
Extract a sub-object from a json object.
subroutine json_extract_item_from_array(core, array, i, item)
Extract ith item from a JSON array as a separate JSON object.
subroutine json_get_string(json, name, value)
Retrieves a string parameter by name or throws an error.
subroutine json_get_or_default_real(json, name, value, default)
Retrieves a real parameter by name or assigns a provided default value. In the latter case also adds ...
subroutine json_get_or_default_double(json, name, value, default)
Retrieves a real parameter by name or assigns a provided default value. In the latter case also adds ...
subroutine json_get_or_default_logical(json, name, value, default)
Retrieves a logical parameter by name or assigns a provided default value. In the latter case also ad...
subroutine json_get_double_array(json, name, value)
Retrieves a real array parameter by name or throws an error.
subroutine json_get_or_default_integer(json, name, value, default)
Retrieves an integer parameter by name or assigns a provided default value. In the latter case also a...
subroutine json_get_double(json, name, value)
Retrieves a double precision real parameter by name or throws an error.
subroutine json_get_string_array(json, name, value, filler)
Retrieves a string array parameter by name or throws an error.
subroutine json_get_logical_array(json, name, value)
Retrieves a logical array parameter by name or throws an error.
subroutine json_get_real(json, name, value)
Retrieves a real parameter by name or throws an error.
logical, public json_no_defaults
If true, the json_get_or_default routines will not add missing parameters.
subroutine json_get_integer(json, name, value)
Retrieves an integer parameter by name or throws an error.
subroutine json_get_integer_array(json, name, value)
Retrieves a integer array parameter by name or throws an error.
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 an output.
Definition output.f90:34
Utilities.
Definition utils.f90:35