Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
json_utils_ext.f90
Go to the documentation of this file.
2 use mpi_f08, only: mpi_comm_rank, mpi_initialized, mpi_bcast, &
3 mpi_comm_world, mpi_integer, mpi_character
4 use json_file_module, only: json_file
5 use json_value_module, only: json_value
6 use utils, only: neko_error, filename_suffix
7
8 implicit none
9 private
10
12
13contains
14
19 function json_key_fallback(json, lookup, fallback) result(string)
20 type(json_file), intent(inout) :: json
21 character(len=*), intent(in) :: lookup
22 character(len=*), intent(in) :: fallback
23 character(len=:), allocatable :: string
24
25 if ((lookup .in. json)) then
26 string = lookup
27 else if (fallback .in. json) then
28 string = fallback
29 else
30 string = lookup
31 end if
32
33 end function json_key_fallback
34
36 subroutine json_get_subdict(json, key, output)
37 type(json_file), intent(inout) :: json
38 character(len=*), intent(in) :: key
39 type(json_file), intent(out) :: output
40
41 type(json_value), pointer :: child
42 logical :: valid
43
44 if (associated(child)) nullify(child)
45
46 valid = .false.
47 call json%get(key, child, valid)
48 if (.not. valid) then
49 call neko_error('Parameter "' // &
50 trim(key) // '" missing from the case file')
51 end if
52
53 call output%initialize()
54 call output%add(child)
55
56 if (associated(child)) nullify(child)
57
58 end subroutine json_get_subdict
59
64 function json_read_file(filename) result(json)
65 character(len=*), intent(in) :: filename
66 type(json_file) :: json
67
68 logical :: mpi_is_initialized
69 integer :: rank, ierr, length
70 character(len=:), allocatable :: json_buffer
71 character(len=4) :: suffix
72
73 ! Check if the file is a json or Neko case file.
74 call filename_suffix(filename, suffix)
75
76 if (trim(suffix) .ne. 'json' .and. trim(suffix) .ne. 'case' ) then
77 call neko_error('Invalid case file')
78 end if
79
80 ! Initialize the mpi variables.
81 rank = 0
82 mpi_is_initialized = .false.
83
84 ! Check if MPI is initialized and get the rank if it is.
85 call mpi_initialized(mpi_is_initialized, ierr)
86 if (mpi_is_initialized) call mpi_comm_rank(mpi_comm_world, rank, ierr)
87
88 ! Read the json file and broadcast it to all ranks.
89 if (rank .eq. 0) then
90 call json%load_file(filename = trim(filename))
91 end if
92
93 ! Serialize the json object to a string so it can be broadcast.
94 if (mpi_is_initialized) then
95 if (rank .eq. 0) call json%print_to_string(json_buffer)
96
97 length = len(json_buffer)
98 call mpi_bcast(length, 1, mpi_integer, 0, mpi_comm_world, ierr)
99
100 if (rank .ne. 0) allocate(character(len=length) :: json_buffer)
101 call mpi_bcast(json_buffer, length, mpi_character, 0, mpi_comm_world, &
102 ierr)
103 end if
104
105 call json%load_from_string(json_buffer)
106 deallocate(json_buffer)
107
108 end function json_read_file
109
110end module json_utils_ext
type(json_file) function, public json_read_file(filename)
Read a json file taking mpi into account. This function reads a json file and broadcasts it to all ra...
character(len=:) function, allocatable, public json_key_fallback(json, lookup, fallback)
Create a json_string based on fallback logic. If the lookup key is present in the json object,...
subroutine, public json_get_subdict(json, key, output)
Extract a sub-object from a json object.