Neko 1.99.2
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!
34!
35! These are json-fortran type codes:
36! json_unknown = 0
37! json_null = 1
38! json_object = 2
39! json_array = 3
40! json_logical = 4
41! json_integer = 5
42! json_real = 6
43! json_string = 7
45 use num_types, only : rp, dp, sp
46 use json_module, only : json_file, json_value, json_core
47 use utils, only : neko_error
48 use math, only : abscmp
49 implicit none
50 private
51
54
56 logical :: json_no_defaults = .false.
57
64 end interface json_get
65
72 end interface json_get_or_default
73
75 module procedure json_get_or_lookup_real, json_get_or_lookup_double, &
76 json_get_or_lookup_real_array, json_get_or_lookup_double_array, &
77 json_get_or_lookup_integer, json_get_or_lookup_integer_array
78 end interface json_get_or_lookup
79
81 module procedure json_get_or_lookup_or_default_real, &
82 json_get_or_lookup_or_default_double, &
83 json_get_or_lookup_or_default_integer
85
88 end interface json_extract_item
89
90 interface
91 module subroutine json_get_or_lookup_real(json, name, val)
92 type(json_file), intent(inout) :: json
93 character(len=*), intent(in) :: name
94 real(kind=sp), intent(out) :: val
95 end subroutine json_get_or_lookup_real
96
97 module subroutine json_get_or_lookup_double(json, name, val)
98 type(json_file), intent(inout) :: json
99 character(len=*), intent(in) :: name
100 real(kind=dp), intent(out) :: val
101 end subroutine json_get_or_lookup_double
102
103 module subroutine json_get_or_lookup_integer(json, name, val)
104 type(json_file), intent(inout) :: json
105 character(len=*), intent(in) :: name
106 integer, intent(out) :: val
107 end subroutine json_get_or_lookup_integer
108
109 module subroutine json_get_or_lookup_or_default_real(json, &
110 name, val, default)
111 type(json_file), intent(inout) :: json
112 character(len=*), intent(in) :: name
113 real(kind=sp), intent(out) :: val
114 real(kind=sp), intent(in) :: default
115 end subroutine json_get_or_lookup_or_default_real
116
117 module subroutine json_get_or_lookup_or_default_double(json, &
118 name, val, default)
119 type(json_file), intent(inout) :: json
120 character(len=*), intent(in) :: name
121 real(kind=dp), intent(out) :: val
122 real(kind=dp), intent(in) :: default
123 end subroutine json_get_or_lookup_or_default_double
124
125 module subroutine json_get_or_lookup_or_default_integer(json,&
126 name, val, default)
127 type(json_file), intent(inout) :: json
128 character(len=*), intent(in) :: name
129 integer, intent(out) :: val
130 integer, intent(in) :: default
131 end subroutine json_get_or_lookup_or_default_integer
132
133 module subroutine json_get_or_lookup_real_array(json, name, val)
134 type(json_file), intent(inout) :: json
135 character(len=*), intent(in) :: name
136 real(kind=sp), allocatable, intent(inout) :: val(:)
137 end subroutine json_get_or_lookup_real_array
138
139 module subroutine json_get_or_lookup_double_array(json, name, val)
140 type(json_file), intent(inout) :: json
141 character(len=*), intent(in) :: name
142 real(kind=dp), allocatable, intent(inout) :: val(:)
143 end subroutine json_get_or_lookup_double_array
144
145 module subroutine json_get_or_lookup_integer_array(json, name, val)
146 type(json_file), intent(inout) :: json
147 character(len=*), intent(in) :: name
148 integer, allocatable, intent(inout) :: val(:)
149 end subroutine json_get_or_lookup_integer_array
150
151 end interface
152contains
153
158 subroutine json_get_real(json, name, value)
159 type(json_file), intent(inout) :: json
160 character(len=*), intent(in) :: name
161 real(kind=sp), intent(out) :: value
162 logical :: found
163 integer :: var_type
164
165 call json%info(name, found = found, var_type = var_type)
166 if (.not. found) then
167 call neko_error("Parameter " // name // " missing from the case file")
168 else if (var_type .ne. 6) then
169 call neko_error("Parameter " // name // " is not a real")
170 end if
171
172 call json%get(name, value)
173 end subroutine json_get_real
174
179 subroutine json_get_double(json, name, value)
180 type(json_file), intent(inout) :: json
181 character(len=*), intent(in) :: name
182 real(kind=dp), intent(out) :: value
183 logical :: found
184 integer :: var_type
185
186 call json%info(name, found = found, var_type = var_type)
187 if (.not. found) then
188 call neko_error("Parameter " // name // " missing from the case file")
189 else if (var_type .ne. 6) then
190 call neko_error("Parameter " // name // " is not a real")
191 end if
192
193 call json%get(name, value)
194 end subroutine json_get_double
195
200 subroutine json_get_integer(json, name, value)
201 type(json_file), intent(inout) :: json
202 character(len=*), intent(in) :: name
203 integer, intent(out) :: value
204 logical :: found
205 integer :: var_type
206
207 call json%info(name, found = found, var_type = var_type)
208 if (.not. found) then
209 call neko_error("Parameter " // name // " missing from the case file")
210 else if (var_type .ne. 5) then
211 call neko_error("Parameter " // name // " is not an integer")
212 end if
213
214 call json%get(name, value)
215 end subroutine json_get_integer
216
221 subroutine json_get_logical(json, name, value)
222 type(json_file), intent(inout) :: json
223 character(len=*), intent(in) :: name
224 logical, intent(out) :: value
225 logical :: found
226 integer :: var_type
227
228 call json%info(name, found = found, var_type = var_type)
229 if (.not. found) then
230 call neko_error("Parameter " // name // " missing from the case file")
231 else if (var_type .ne. 4) then
232 call neko_error("Parameter " // name // " is not a logical")
233 end if
234
235 call json%get(name, value)
236 end subroutine json_get_logical
237
242 subroutine json_get_string(json, name, value)
243 type(json_file), intent(inout) :: json
244 character(len=*), intent(in) :: name
245 character(len=:), allocatable, intent(out) :: value
246 logical :: found
247 integer :: var_type
248
249 call json%info(name, found = found, var_type = var_type)
250 if (.not. found) then
251 call neko_error("Parameter " // name // " missing from the case file")
252 else if (var_type .ne. 7) then
253 call neko_error("Parameter " // name // " is not a string")
254 end if
255
256 call json%get(name, value)
257 end subroutine json_get_string
258
263 subroutine json_get_real_array(json, name, value)
264 type(json_file), intent(inout) :: json
265 character(len=*), intent(in) :: name
266 real(kind=sp), allocatable, intent(out) :: value(:)
267 logical :: found
268 integer :: var_type
269
270 call json%info(name, found = found, var_type = var_type)
271 if (.not. found) then
272 call neko_error("Parameter " // name // " missing from the case file")
273 else if (var_type .ne. 3) then
274 call neko_error("Parameter " // name // " is not an array")
275 end if
276
277 call json%get(name, value)
278 end subroutine json_get_real_array
279
284 subroutine json_get_double_array(json, name, value)
285 type(json_file), intent(inout) :: json
286 character(len=*), intent(in) :: name
287 real(kind=dp), allocatable, intent(out) :: value(:)
288 logical :: found
289 integer :: var_type
290
291 call json%info(name, found = found, var_type = var_type)
292 if (.not. found) then
293 call neko_error("Parameter " // name // " missing from the case file")
294 else if (var_type .ne. 3) then
295 call neko_error("Parameter " // name // " is not an array")
296 end if
297
298 call json%get(name, value)
299 end subroutine json_get_double_array
300
305 subroutine json_get_integer_array(json, name, value)
306 type(json_file), intent(inout) :: json
307 character(len=*), intent(in) :: name
308 integer, allocatable, intent(out) :: value(:)
309
310 real(kind=rp), allocatable :: test_real(:)
311 integer :: i
312
313 if (.not. json%valid_path(name)) then
314 call neko_error("Parameter " // name // " missing from the case file")
315 end if
316
317 call json%get(name, value)
318 end subroutine json_get_integer_array
319
324 subroutine json_get_logical_array(json, name, value)
325 type(json_file), intent(inout) :: json
326 character(len=*), intent(in) :: name
327 logical, allocatable, intent(out) :: value(:)
328 logical :: found
329 integer :: var_type
330
331 call json%info(name, found = found, var_type = var_type)
332 if (.not. found) then
333 call neko_error("Parameter " // name // " missing from the case file")
334 else if (var_type .ne. 3) then
335 call neko_error("Parameter " // name // " is not a array")
336 end if
337
338 call json%get(name, value)
339 end subroutine json_get_logical_array
340
346 subroutine json_get_string_array(json, name, value, filler)
347 type(json_file), intent(inout) :: json
348 character(len=*), intent(in) :: name
349 character(len=*), allocatable, intent(out) :: value(:)
350 character(len=*), optional, intent(in) :: filler
351 logical :: found
352 type(json_value), pointer :: json_val, val_ptr
353 type(json_core) :: core
354 character(len=:), allocatable :: string_value
355 integer :: i, n_children
356 integer :: var_type
357
358 call json%info(name, found = found, var_type = var_type, &
359 n_children = n_children)
360 if (.not. found) then
361 call neko_error("Parameter " // name // " missing from the case file")
362 else if (var_type .ne. 3) then
363 call neko_error("Parameter " // name // " is not an array")
364 end if
365
366 if (.not. allocated(value)) then
367 allocate(value(n_children))
368 else if (len(value) .lt. n_children) then
369 deallocate(value)
370 allocate(value(n_children))
371 end if
372
373 call json%get(name, json_val, found)
374 call json%get_core(core)
375
376 do i = 1, n_children
377 call core%get_child(json_val, i, val_ptr, found)
378 call core%get(val_ptr, string_value)
379
380 if (len(string_value) .gt. 0) then
381 value(i) = string_value
382 else if (present(filler)) then
383 value(i) = filler
384 end if
385 end do
386
387 end subroutine json_get_string_array
388
390 subroutine json_get_subdict(json, key, output)
391 type(json_file), intent(inout) :: json
392 character(len=*), intent(in) :: key
393 type(json_file), intent(inout) :: output
394
395 type(json_value), pointer :: ptr
396 type(json_core) :: core
397 logical :: found
398 character(len=:), allocatable :: buffer
399
400 call json%get_core(core)
401 call json%get(key, ptr, found)
402
403 if (.not. found) then
404 call neko_error("Parameter " // &
405 trim(key) // " missing from the case file")
406 end if
407
408 call core%print_to_string(ptr, buffer)
409 call output%initialize(strict_type_checking = .true.)
410 call output%load_from_string(buffer)
411
412 end subroutine json_get_subdict
413
419 subroutine json_get_or_default_real(json, name, value, default)
420 type(json_file), intent(inout) :: json
421 character(len=*), intent(in) :: name
422 real(kind=sp), intent(out) :: value
423 real(kind=sp), intent(in) :: default
424 logical :: found
425 integer :: var_type
426
427 call json%info(name, found = found, var_type = var_type)
428
429 if (found .and. (var_type .ne. 6)) then
430 call neko_error("Parameter " // name // " present, but is not a real")
431 end if
432
433 call json%get(name, value, found)
434
435 if ((.not. found) .and. (.not. json_no_defaults)) then
436 value = default
437 call json%add(name, value)
438 else if (.not. found) then
439 call neko_error("Parameter " // name // " missing from the case file")
440 end if
441 end subroutine json_get_or_default_real
442
449 subroutine json_get_or_default_double(json, name, value, default)
450 type(json_file), intent(inout) :: json
451 character(len=*), intent(in) :: name
452 real(kind=dp), intent(out) :: value
453 real(kind=dp), intent(in) :: default
454 logical :: found
455 integer :: var_type
456
457 call json%info(name, found = found, var_type = var_type)
458
459 if (found .and. (var_type .ne. 6)) then
460 call neko_error("Parameter " // name // " present, but is not a real")
461 end if
462
463 call json%get(name, value, found)
464
465 if ((.not. found) .and. (.not. json_no_defaults)) then
466 value = default
467 call json%add(name, value)
468 else if (.not. found) then
469 call neko_error("Parameter " // name // " missing from the case file")
470 end if
471 end subroutine json_get_or_default_double
472
478 subroutine json_get_or_default_integer(json, name, value, default)
479 type(json_file), intent(inout) :: json
480 character(len=*), intent(in) :: name
481 integer, intent(out) :: value
482 integer, intent(in) :: default
483 logical :: found
484 integer :: var_type
485
486 call json%info(name, found = found, var_type = var_type)
487
488 if (found .and. (var_type .ne. 5)) then
489 call neko_error("Parameter " // name // " present, but " // &
490 "is not an integer")
491 end if
492
493 call json%get(name, value, found)
494
495 if ((.not. found) .and. (.not. json_no_defaults)) then
496 value = default
497 call json%add(name, value)
498 else if (.not. found) then
499 call neko_error("Parameter " // name // " missing from the case file")
500 end if
501 end subroutine json_get_or_default_integer
502
508 subroutine json_get_or_default_logical(json, name, value, default)
509 type(json_file), intent(inout) :: json
510 character(len=*), intent(in) :: name
511 logical, intent(out) :: value
512 logical, intent(in) :: default
513 logical :: found
514 integer :: var_type
515
516 call json%info(name, found = found, var_type = var_type)
517
518 if ((found) .and. (var_type .ne. 4)) then
519 call neko_error("Parameter " // name // " present, but is not a logical")
520 end if
521
522 call json%get(name, value, found)
523
524 if ((.not. found) .and. (.not. json_no_defaults)) then
525 value = default
526 call json%add(name, value)
527 else if (.not. found) then
528 call neko_error("Parameter " // name // " missing from the case file")
529 end if
530 end subroutine json_get_or_default_logical
531
537 subroutine json_get_or_default_string(json, name, value, default)
538 type(json_file), intent(inout) :: json
539 character(len=*), intent(in) :: name
540 character(len=:), allocatable, intent(out) :: value
541 character(len=*), intent(in) :: default
542 logical :: found
543 integer :: var_type
544
545 call json%info(name, found = found, var_type = var_type)
546
547 if (found .and. (var_type .ne. 7)) then
548 call neko_error("Parameter " // name // &
549 " present, but is not a string")
550 end if
551
552 call json%get(name, value, found)
553
554 if ((.not. found) .and. (.not. json_no_defaults)) then
555 value = default
556 call json%add(name, value)
557 else if (.not. found) then
558 call neko_error("Parameter " // name // " missing from the case file")
559 end if
560 end subroutine json_get_or_default_string
561
567 subroutine json_extract_item_from_array(core, array, i, item)
568 type(json_core), intent(inout) :: core
569 type(json_value), pointer, intent(in) :: array
570 integer, intent(in) :: i
571 type(json_file), intent(inout) :: item
572 type(json_value), pointer :: ptr
573 logical :: found
574 character(len=:), allocatable :: buffer
575
576 call core%get_child(array, i, ptr, found)
577 call core%print_to_string(ptr, buffer)
578 call item%initialize(strict_type_checking = .true.)
579 call item%load_from_string(buffer)
580
581 end subroutine json_extract_item_from_array
582
588 subroutine json_extract_item_from_name(json, name, i, item)
589 type(json_file), intent(inout) :: json
590 character(len=*), intent(in) :: name
591 integer, intent(in) :: i
592 type(json_file), intent(out) :: item
593
594 type(json_core) :: core
595 type(json_value), pointer :: array
596 type(json_value), pointer :: ptr
597 logical :: found
598 character(len=:), allocatable :: buffer
599
600 call json%get_core(core)
601 call json%get(name, array, found)
602
603 if (.not. found) then
604 call neko_error("Parameter " // name // " missing from the case file")
605 end if
606
607 call core%get_child(array, i, ptr, found)
608 call core%print_to_string(ptr, buffer)
609 call item%initialize(strict_type_checking = .true.)
610 call item%load_from_string(buffer)
611
612 end subroutine json_extract_item_from_name
613
614end 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 double precision parameter by name or assigns a provided default value....
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 double precision 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.
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 an output.
Definition output.f90:34
Utilities.
Definition utils.f90:35