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
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
39 function json_read_file(filename) result(json)
40 character(len=*), intent(in) :: filename
41 type(json_file) :: json
42
43 logical :: mpi_is_initialized
44 integer :: rank, ierr, length
45 character(len=:), allocatable :: json_buffer
46 character(len=4) :: suffix
47
48 ! Check if the file is a json or Neko case file.
49 call filename_suffix(filename, suffix)
50
51 if (trim(suffix) .ne. 'json' .and. trim(suffix) .ne. 'case' ) then
52 call neko_error('Invalid case file')
53 end if
54
55 ! Initialize the mpi variables.
56 rank = 0
57 mpi_is_initialized = .false.
58
59 ! Check if MPI is initialized and get the rank if it is.
60 call mpi_initialized(mpi_is_initialized, ierr)
61 if (mpi_is_initialized) call mpi_comm_rank(mpi_comm_world, rank, ierr)
62
63 ! Read the json file and broadcast it to all ranks.
64 if (rank .eq. 0) then
65 call json%load_file(filename = trim(filename))
66 end if
67
68 ! Serialize the json object to a string so it can be broadcast.
69 if (mpi_is_initialized) then
70 if (rank .eq. 0) call json%print_to_string(json_buffer)
71
72 length = len(json_buffer)
73 call mpi_bcast(length, 1, mpi_integer, 0, mpi_comm_world, ierr)
74
75 if (rank .ne. 0) allocate(character(len=length) :: json_buffer)
76 call mpi_bcast(json_buffer, length, mpi_character, 0, mpi_comm_world, &
77 ierr)
78
79 if (rank .ne. 0) then
80 call json%load_from_string(json_buffer)
81 deallocate(json_buffer)
82 end if
83 end if
84
85 end function json_read_file
86
87end module json_utils_ext