31 INTEGER,
PARAMETER :: pd = 12
32 INTEGER,
PARAMETER :: rd = 307
33 INTEGER,
PARAMETER :: dp = selected_real_kind(pd, rd)
34 INTEGER,
PARAMETER :: wp = dp
35 INTEGER,
PARAMETER :: mpi_real_wp = mpi_real8
36 REAL(wp),
PARAMETER :: missing_heat_flux = -999_wp
38 CHARACTER(LEN=*),
PARAMETER :: comp_name =
"simulation_component"
39 CHARACTER(LEN=*),
PARAMETER :: grid_name =
"simulation_grid"
41 INTEGER,
PARAMETER :: arg_char_length = 512
42 CHARACTER(LEN=arg_char_length) :: yaml_filename
43 CHARACTER(LEN=arg_char_length) :: grid_filename
45 INTEGER :: i, step, ierror
46 INTEGER :: comp_comm, comp_rank
48 LOGICAL :: heat_flux_received
53 REAL(wp),
ALLOCATABLE :: temperature(:,:)
54 REAL(wp),
ALLOCATABLE :: heat_flux(:,:)
55 REAL(wp),
ALLOCATABLE :: ref_heat_flux(:)
56 LOGICAL,
ALLOCATABLE :: ml_predictor_coverage(:)
57 INTEGER :: num_predicted_cells
58 REAL(wp) :: t_val, x_cell, y_cell
59 REAL(wp) :: heat_flux_min_local, heat_flux_max_local, &
60 heat_flux_sum_diff_local, &
61 heat_flux_min_global, heat_flux_max_global, &
62 heat_flux_sum_diff_global, heat_flux_mean_diff_global
65 yaml_filename =
"toy_ml_coupling.yaml"
72 comp_comm = coupler%comp_comm
73 CALL mpi_comm_rank(comp_comm, comp_rank, ierror)
74 is_root = comp_rank == 0
80 simulation_grid, grid_name, trim(grid_filename), coupler%comp_comm)
87 ALLOCATE(temperature(simulation_grid%num_cells, 1))
88 ALLOCATE(heat_flux(simulation_grid%num_cells, 1))
89 ALLOCATE(ref_heat_flux(simulation_grid%num_cells))
93 WRITE(6,
'(A)')
" ["//trim(comp_name)//
"]: Starting time loop"
95 " ======================================"
109 DO i = 1, simulation_grid%num_cells
110 x_cell = real(simulation_grid%x_cells(i), wp)
111 y_cell = real(simulation_grid%y_cells(i), wp)
113 + 6.0_wp * sin(x_cell) * cos(y_cell) &
114 + 4.0_wp * cos(2.0_wp * y_cell) &
116 temperature(i, 1) = t_val
124 coupler, temperature, heat_flux, heat_flux_received)
126 IF (heat_flux_received)
THEN
130 IF (.NOT.
ALLOCATED(ml_predictor_coverage))
THEN
133 ml_predictor_coverage = &
134 (heat_flux(:, 1) /= missing_heat_flux) .AND. &
135 simulation_grid%is_valid(:)
136 num_predicted_cells = count(ml_predictor_coverage)
138 CALL mpi_allreduce( &
139 mpi_in_place, num_predicted_cells, 1, mpi_integer, mpi_sum, &
142 WRITE(6,
'(A,I0,A)') &
143 " ["//trim(comp_name)//
"]: Number of cells covered by ML predictor: ", &
144 num_predicted_cells,
" (global across all ranks)"
146 IF (num_predicted_cells == 0)
THEN
149 " ["//trim(comp_name)//
"]: No cells covered by ML predictor. Exiting."
156 DO i = 1, simulation_grid%num_cells
157 t_val = temperature(i, 1)
158 ref_heat_flux(i) = 0.5_wp * t_val + 0.01_wp * t_val * t_val
162 WHERE (.NOT. ml_predictor_coverage) heat_flux(:, 1) = 999._wp
163 heat_flux_min_local = minval(heat_flux(:, 1))
165 heat_flux_min_local, heat_flux_min_global, 1, mpi_real_wp, &
166 mpi_min, 0, comp_comm, ierror)
167 WHERE (.NOT. ml_predictor_coverage) heat_flux(:, 1) = -999._wp
168 heat_flux_max_local = maxval(heat_flux(:, 1))
170 heat_flux_max_local, heat_flux_max_global, 1, mpi_real_wp, &
171 mpi_max, 0, comp_comm, ierror)
174 WHERE (.NOT. ml_predictor_coverage) heat_flux(:, 1) = ref_heat_flux(:)
175 heat_flux_sum_diff_local = sum(abs(heat_flux(:, 1) - ref_heat_flux(:)))
177 heat_flux_sum_diff_local, heat_flux_sum_diff_global, 1, mpi_real_wp, mpi_sum, 0, &
179 heat_flux_mean_diff_global = &
180 heat_flux_sum_diff_global / real(num_predicted_cells, wp)
183 WRITE(6,
'(A,I3,A,F0.3,A,F0.3,A,F0.3,A)') &
184 " ["//trim(comp_name)//
"]: Step ", step, &
185 ": Heat flux prediction: min=", heat_flux_min_global, &
186 " max=", heat_flux_max_global, &
187 " mean dev=", heat_flux_mean_diff_global
190 ELSE IF (is_root)
THEN
191 WRITE(6,
'(A,I3,A)') &
192 " ["//trim(comp_name)//
"]: Step ", step, &
193 ": heat_flux not coupled back (one-way coupling)"
198 DEALLOCATE(temperature, heat_flux, ref_heat_flux)
202 " ======================================"
203 WRITE(6,
'(A)')
" ["//trim(comp_name)//
"]: Finished"
220 CHARACTER(LEN=arg_char_length) :: configFilename
221 CHARACTER(LEN=arg_char_length) :: gridFilename
223 CHARACTER(LEN=arg_char_length) :: arg
225 LOGICAL :: skip_arg = .false.
227 DO i = 1, command_argument_count()
228 IF (.NOT. skip_arg)
THEN
229 CALL get_command_argument(i, arg)
232 IF (i == command_argument_count())
THEN
234 'missing parameter for option: ', arg
235 print
'(A)',
' -c configFilename'
236 print
'(A)',
' -g gridFilename'
239 CALL get_command_argument(i + 1, &
244 IF (i == command_argument_count())
THEN
246 'missing parameter for option: ', arg
247 print
'(A)',
' -c configFilename'
248 print
'(A)',
' -g gridFilename'
251 CALL get_command_argument(i + 1, &
256 print
'(2A, /)',
'unrecognised option: ', arg
257 print
'(A)',
' -c configFilename'
258 print
'(A)',
' -g gridFilename'
subroutine, public coupler_init(coupler, comp_name, yaml_filename)
Initialise the YAC coupler for the simulation component.
logical function, public ml_predictor_has_data(coupler)
subroutine, public coupler_setup(coupler, grid)
Set up the YAC coupler with the grid information.
subroutine, public exchange_ml_predictor(coupler, temperature, heat_flux, heat_flux_received)
Exchange data with the ML predictor component.
subroutine, public coupler_finalise(coupler)
subroutine, public read_icon_grid_parallel(grid, grid_name, grid_filename, comp_comm)
Read ICON grid information in parallel.
subroutine, public icon_grid_cleanup(grid)
Clean up the grid data structure.
program simulation_component
Grid data structure for the simulation component.
static void parse_arguments(int argc, char **argv, enum experiment_type *experiment)