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.
1
34module json_utils_ext
35 use mpi_f08, only: mpi_comm_rank, mpi_initialized, mpi_bcast, &
36 mpi_integer, mpi_character
37 use comm, only: neko_comm
38 use json_file_module, only: json_file
39 use json_value_module, only: json_value
40 use utils, only: neko_error, filename_suffix
41
42 implicit none
43 private
44
45 public :: json_key_fallback, json_read_file
46
48 module procedure json_key_fallback_string
49 module procedure json_key_fallback_json
50 end interface json_key_fallback
51
52contains
53
58 function json_key_fallback_string(json, lookup, fallback) result(string)
59 type(json_file), intent(inout) :: json
60 character(len=*), intent(in) :: lookup
61 character(len=*), intent(in) :: fallback
62 character(len=:), allocatable :: string
63
64 if ((lookup .in. json)) then
65 string = lookup
66 else if (fallback .in. json) then
67 string = fallback
68 else
69 string = lookup
70 end if
71
72 end function json_key_fallback_string
73
78 function json_key_fallback_json(lookup_json, fallback_json, key) &
79 result(json_pointer)
80 type(json_file), target, intent(inout) :: lookup_json
81 type(json_file), target, intent(inout) :: fallback_json
82 character(len=*), intent(in) :: key
83 type(json_file), pointer :: json_pointer
84
85 if ((key .in. lookup_json)) then
86 json_pointer => lookup_json
87 else if (key .in. fallback_json) then
88 json_pointer => fallback_json
89 else
90 json_pointer => lookup_json
91 end if
92
93 end function json_key_fallback_json
94
99 function json_read_file(filename) result(json)
100 character(len=*), intent(in) :: filename
101 type(json_file) :: json
102
103 logical :: mpi_is_initialized
104 integer :: rank, ierr, length
105 character(len=:), allocatable :: json_buffer
106 character(len=4) :: suffix
107
108 ! Check if the file is a json or Neko case file.
109 call filename_suffix(filename, suffix)
110
111 if (trim(suffix) .ne. 'json' .and. trim(suffix) .ne. 'case' ) then
112 call neko_error('Invalid case file')
113 end if
114
115 ! Initialize the mpi variables.
116 rank = 0
117 mpi_is_initialized = .false.
118
119 ! Check if MPI is initialized and get the rank if it is.
120 call mpi_initialized(mpi_is_initialized, ierr)
121 if (mpi_is_initialized) call mpi_comm_rank(neko_comm, rank, ierr)
122
123 ! Read the json file and broadcast it to all ranks.
124 if (rank .eq. 0) then
125 call json%load_file(filename = trim(filename))
126 end if
127
128 ! Serialize the json object to a string so it can be broadcast.
129 if (mpi_is_initialized) then
130 if (rank .eq. 0) call json%print_to_string(json_buffer)
131
132 length = len(json_buffer)
133 call mpi_bcast(length, 1, mpi_integer, 0, neko_comm, ierr)
134
135 if (rank .ne. 0) allocate(character(len=length) :: json_buffer)
136 call mpi_bcast(json_buffer, length, mpi_character, 0, neko_comm, ierr)
137
138 if (rank .ne. 0) then
139 call json%load_from_string(json_buffer)
140 deallocate(json_buffer)
141 end if
142 end if
143
144 end function json_read_file
145
146end module json_utils_ext