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