Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
json_utils_ext.f90
1module json_utils_ext
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
11 public :: json_key_fallback, json_read_file
12
14 module procedure json_key_fallback_string
15 module procedure json_key_fallback_json
16 end interface json_key_fallback
17
18contains
19
24 function json_key_fallback_string(json, lookup, fallback) result(string)
25 type(json_file), intent(inout) :: json
26 character(len=*), intent(in) :: lookup
27 character(len=*), intent(in) :: fallback
28 character(len=:), allocatable :: string
29
30 if ((lookup .in. json)) then
31 string = lookup
32 else if (fallback .in. json) then
33 string = fallback
34 else
35 string = lookup
36 end if
37
38 end function json_key_fallback_string
39
44 function json_key_fallback_json(lookup_json, fallback_json, key) &
45 result(json_pointer)
46 type(json_file), target, intent(inout) :: lookup_json
47 type(json_file), target, intent(inout) :: fallback_json
48 character(len=*), intent(in) :: key
49 type(json_file), pointer :: json_pointer
50
51 if ((key .in. lookup_json)) then
52 json_pointer => lookup_json
53 else if (key .in. fallback_json) then
54 json_pointer => fallback_json
55 else
56 json_pointer => lookup_json
57 end if
58
59 end function json_key_fallback_json
60
65 function json_read_file(filename) result(json)
66 character(len=*), intent(in) :: filename
67 type(json_file) :: json
68
69 logical :: mpi_is_initialized
70 integer :: rank, ierr, length
71 character(len=:), allocatable :: json_buffer
72 character(len=4) :: suffix
73
74 ! Check if the file is a json or Neko case file.
75 call filename_suffix(filename, suffix)
76
77 if (trim(suffix) .ne. 'json' .and. trim(suffix) .ne. 'case' ) then
78 call neko_error('Invalid case file')
79 end if
80
81 ! Initialize the mpi variables.
82 rank = 0
83 mpi_is_initialized = .false.
84
85 ! Check if MPI is initialized and get the rank if it is.
86 call mpi_initialized(mpi_is_initialized, ierr)
87 if (mpi_is_initialized) call mpi_comm_rank(mpi_comm_world, rank, ierr)
88
89 ! Read the json file and broadcast it to all ranks.
90 if (rank .eq. 0) then
91 call json%load_file(filename = trim(filename))
92 end if
93
94 ! Serialize the json object to a string so it can be broadcast.
95 if (mpi_is_initialized) then
96 if (rank .eq. 0) call json%print_to_string(json_buffer)
97
98 length = len(json_buffer)
99 call mpi_bcast(length, 1, mpi_integer, 0, mpi_comm_world, ierr)
100
101 if (rank .ne. 0) allocate(character(len=length) :: json_buffer)
102 call mpi_bcast(json_buffer, length, mpi_character, 0, mpi_comm_world, &
103 ierr)
104
105 if (rank .ne. 0) then
106 call json%load_from_string(json_buffer)
107 deallocate(json_buffer)
108 end if
109 end if
110
111 end function json_read_file
112
113end module json_utils_ext