Neko  0.8.99
A portable framework for high-order spectral element flow simulations
json_utils.f90
Go to the documentation of this file.
1 ! Copyright (c) 2019-2021, 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 module json_utils
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 
42 
44  interface json_get
45  module procedure json_get_real, json_get_double, json_get_integer, &
49  end interface json_get
50 
57  end interface json_get_or_default
58 
61  end interface json_extract_item
62 contains
63 
68  subroutine json_get_real(json, name, value)
69  type(json_file), intent(inout) :: json
70  character(len=*), intent(in) :: name
71  real(kind=sp), intent(out) :: value
72 
73  if (.not. json%valid_path(name)) then
74  call neko_error("Parameter " // name // " missing from the case file")
75  end if
76 
77  call json%get(name, value)
78  end subroutine json_get_real
79 
84  subroutine json_get_double(json, name, value)
85  type(json_file), intent(inout) :: json
86  character(len=*), intent(in) :: name
87  real(kind=dp), intent(out) :: value
88 
89  if (.not. json%valid_path(name)) then
90  call neko_error("Parameter " // name // " missing from the case file")
91  end if
92 
93  call json%get(name, value)
94  end subroutine json_get_double
95 
100  subroutine json_get_integer(json, name, value)
101  type(json_file), intent(inout) :: json
102  character(len=*), intent(in) :: name
103  integer, intent(out) :: value
104 
105  if (.not. json%valid_path(name)) then
106  call neko_error("Parameter " // name // " missing from the case file")
107  end if
108 
109  call json%get(name, value)
110  end subroutine json_get_integer
111 
116  subroutine json_get_logical(json, name, value)
117  type(json_file), intent(inout) :: json
118  character(len=*), intent(in) :: name
119  logical, intent(out) :: value
120 
121  if (.not. json%valid_path(name)) then
122  call neko_error("Parameter " // name // " missing from the case file")
123  end if
124 
125  call json%get(name, value)
126  end subroutine json_get_logical
127 
132  subroutine json_get_string(json, name, value)
133  type(json_file), intent(inout) :: json
134  character(len=*), intent(in) :: name
135  character(len=:), allocatable, intent(out) :: value
136 
137  if (.not. json%valid_path(name)) then
138  call neko_error("Parameter " // name // " missing from the case file")
139  end if
140 
141  call json%get(name, value)
142  end subroutine json_get_string
143 
148  subroutine json_get_real_array(json, name, value)
149  type(json_file), intent(inout) :: json
150  character(len=*), intent(in) :: name
151  real(kind=sp), allocatable, intent(out) :: value(:)
152 
153  if (.not. json%valid_path(name)) then
154  call neko_error("Parameter " // name // " missing from the case file")
155  end if
156 
157  call json%get(name, value)
158  end subroutine json_get_real_array
159 
164  subroutine json_get_double_array(json, name, value)
165  type(json_file), intent(inout) :: json
166  character(len=*), intent(in) :: name
167  real(kind=dp), allocatable, intent(out) :: value(:)
168 
169  if (.not. json%valid_path(name)) then
170  call neko_error("Parameter " // name // " missing from the case file")
171  end if
172 
173  call json%get(name, value)
174  end subroutine json_get_double_array
175 
180  subroutine json_get_integer_array(json, name, value)
181  type(json_file), intent(inout) :: json
182  character(len=*), intent(in) :: name
183  integer, allocatable, intent(out) :: value(:)
184 
185  if (.not. json%valid_path(name)) then
186  call neko_error("Parameter " // name // " missing from the case file")
187  end if
188 
189  call json%get(name, value)
190  end subroutine json_get_integer_array
191 
196  subroutine json_get_logical_array(json, name, value)
197  type(json_file), intent(inout) :: json
198  character(len=*), intent(in) :: name
199  logical, allocatable, intent(out) :: value(:)
200 
201  if (.not. json%valid_path(name)) then
202  call neko_error("Parameter " // name // " missing from the case file")
203  end if
204 
205  call json%get(name, value)
206  end subroutine json_get_logical_array
207 
213  subroutine json_get_string_array(json, name, value, filler)
214  type(json_file), intent(inout) :: json
215  character(len=*), intent(in) :: name
216  character(len=*), allocatable, intent(out) :: value(:)
217  character(len=*), optional, intent(in) :: filler
218  logical :: found
219  type(json_value), pointer :: json_val, val_ptr
220  type(json_core) :: core
221  character(len=:), allocatable :: string_value
222  integer :: i, n_children
223 
224  if (.not. json%valid_path(name)) then
225  call neko_error("Parameter " // name // " missing from the case file")
226  end if
227  call json%info(name, n_children=n_children)
228 
229  if (.not. allocated(value)) then
230  allocate(value(n_children))
231  else if (len(value) .lt. n_children) then
232  deallocate(value)
233  allocate(value(n_children))
234  end if
235 
236  call json%get(name, json_val, found)
237  call json%get_core(core)
238 
239  do i = 1, n_children
240  call core%get_child(json_val, i, val_ptr, found)
241  call core%get(val_ptr, string_value)
242 
243  if (len(string_value) .gt. 0) then
244  value(i) = string_value
245  else if(present(filler)) then
246  value(i) = filler
247  end if
248  end do
249 
250  end subroutine json_get_string_array
251 
257  subroutine json_get_or_default_real(json, name, value, default)
258  type(json_file), intent(inout) :: json
259  character(len=*), intent(in) :: name
260  real(kind=sp), intent(out) :: value
261  real(kind=sp), intent(in) :: default
262  logical :: found
263 
264  call json%get(name, value, found)
265 
266  if (.not. found) then
267  value = default
268  call json%add(name, value)
269  end if
270  end subroutine json_get_or_default_real
271 
277  subroutine json_get_or_default_double(json, name, value, default)
278  type(json_file), intent(inout) :: json
279  character(len=*), intent(in) :: name
280  real(kind=dp), intent(out) :: value
281  real(kind=dp), intent(in) :: default
282  logical :: found
283 
284  call json%get(name, value, found)
285 
286  if (.not. found) then
287  value = default
288  call json%add(name, value)
289  end if
290  end subroutine json_get_or_default_double
291 
297  subroutine json_get_or_default_integer(json, name, value, default)
298  type(json_file), intent(inout) :: json
299  character(len=*), intent(in) :: name
300  integer, intent(out) :: value
301  integer, intent(in) :: default
302  logical :: found
303 
304  call json%get(name, value, found)
305 
306  if (.not. found) then
307  value = default
308  call json%add(name, value)
309  end if
310  end subroutine json_get_or_default_integer
311 
317  subroutine json_get_or_default_logical(json, name, value, default)
318  type(json_file), intent(inout) :: json
319  character(len=*), intent(in) :: name
320  logical, intent(out) :: value
321  logical, intent(in) :: default
322  logical :: found
323 
324  call json%get(name, value, found)
325 
326  if (.not. found) then
327  value = default
328  call json%add(name, value)
329  end if
330  end subroutine json_get_or_default_logical
331 
337  subroutine json_get_or_default_string(json, name, value, default)
338  type(json_file), intent(inout) :: json
339  character(len=*), intent(in) :: name
340  character(len=:), allocatable, intent(out) :: value
341  character(len=*), intent(in) :: default
342  logical :: found
343 
344  call json%get(name, value, found)
345 
346  if (.not. found) then
347  value = default
348  call json%add(name, value)
349  end if
350  end subroutine json_get_or_default_string
351 
357  subroutine json_extract_item_from_array(core, array, i, item)
358  type(json_core), intent(inout) :: core
359  type(json_value), pointer, intent(in) :: array
360  integer, intent(in) :: i
361  type(json_file), intent(inout) :: item
362  type(json_value), pointer :: ptr
363  logical :: found
364  character(len=:), allocatable :: buffer
365 
366  call core%get_child(array, i, ptr, found)
367  call core%print_to_string(ptr, buffer)
368  call item%load_from_string(buffer)
369 
370  end subroutine json_extract_item_from_array
371 
377  subroutine json_extract_item_from_name(json, name, i, item)
378  type(json_file), intent(inout) :: json
379  character(len=*), intent(in) :: name
380  integer, intent(in) :: i
381  type(json_file), intent(out) :: item
382 
383  type(json_core) :: core
384  type(json_value), pointer :: array
385  type(json_value), pointer :: ptr
386  logical :: found
387  character(len=:), allocatable :: buffer
388 
389  call json%get_core(core)
390  call json%get(name, array, found)
391 
392  if (.not. found) then
393  call neko_error("Parameter " // name // " missing from the case file")
394  end if
395 
396  call core%get_child(array, i, ptr, found)
397  call core%print_to_string(ptr, buffer)
398  call item%load_from_string(buffer)
399 
400  end subroutine json_extract_item_from_name
401 
402 end module json_utils
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Definition: json_utils.f90:53
Retrieves a parameter by name or throws an error.
Definition: json_utils.f90:44
Utilities for retrieving parameters from the case files.
Definition: json_utils.f90:34
subroutine json_extract_item_from_name(json, name, i, item)
Extract ith item from a JSON array as a separate JSON object.
Definition: json_utils.f90:378
subroutine json_get_logical(json, name, value)
Retrieves a logical parameter by name or throws an error.
Definition: json_utils.f90:117
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...
Definition: json_utils.f90:338
subroutine json_get_real_array(json, name, value)
Retrieves a real array parameter by name or throws an error.
Definition: json_utils.f90:149
subroutine json_extract_item_from_array(core, array, i, item)
Extract ith item from a JSON array as a separate JSON object.
Definition: json_utils.f90:358
subroutine json_get_string(json, name, value)
Retrieves a string parameter by name or throws an error.
Definition: json_utils.f90:133
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 ...
Definition: json_utils.f90:258
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 ...
Definition: json_utils.f90:278
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...
Definition: json_utils.f90:318
subroutine json_get_double_array(json, name, value)
Retrieves a real array parameter by name or throws an error.
Definition: json_utils.f90:165
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...
Definition: json_utils.f90:298
subroutine json_get_double(json, name, value)
Retrieves a double precision real parameter by name or throws an error.
Definition: json_utils.f90:85
subroutine json_get_string_array(json, name, value, filler)
Retrieves a string array parameter by name or throws an error.
Definition: json_utils.f90:214
subroutine json_get_logical_array(json, name, value)
Retrieves a logical array parameter by name or throws an error.
Definition: json_utils.f90:197
subroutine json_get_real(json, name, value)
Retrieves a real parameter by name or throws an error.
Definition: json_utils.f90:69
subroutine json_get_integer(json, name, value)
Retrieves an integer parameter by name or throws an error.
Definition: json_utils.f90:101
subroutine json_get_integer_array(json, name, value)
Retrieves a integer array parameter by name or throws an error.
Definition: json_utils.f90:181
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
Utilities.
Definition: utils.f90:35