YAC 3.21.0
Yet Another Coupler
Loading...
Searching...
No Matches
simulation_component.F90
Go to the documentation of this file.
1! Copyright (c) 2026 The YAC Authors
2!
3! SPDX-License-Identifier: BSD-3-Clause
4
22
24
25 USE mpi
27 USE coupler_mod
28
29 IMPLICIT NONE
30
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
37
38 CHARACTER(LEN=*), PARAMETER :: comp_name = "simulation_component"
39 CHARACTER(LEN=*), PARAMETER :: grid_name = "simulation_grid"
40
41 INTEGER, PARAMETER :: arg_char_length = 512
42 CHARACTER(LEN=arg_char_length) :: yaml_filename
43 CHARACTER(LEN=arg_char_length) :: grid_filename
44
45 INTEGER :: i, step, ierror
46 INTEGER :: comp_comm, comp_rank
47 LOGICAL :: is_root
48 LOGICAL :: heat_flux_received
49
50 TYPE(icon_grid_t) :: simulation_grid
51 TYPE(coupler_t) :: coupler
52
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
63
64 ! ---- Parse command-line arguments ---------------------------------------
65 yaml_filename = "toy_ml_coupling.yaml"
66 grid_filename = ""
67 CALL parse_arguments(yaml_filename, grid_filename)
68
69 ! ---- Coupler initialisation --------------------------------------------
70 CALL coupler_init(coupler, comp_name, yaml_filename)
71
72 comp_comm = coupler%comp_comm
73 CALL mpi_comm_rank(comp_comm, comp_rank, ierror)
74 is_root = comp_rank == 0
75
76 ! ---- Component setup ---------------------------------------------------
77
78 ! Read the grid information from the ICON file in parallel
80 simulation_grid, grid_name, trim(grid_filename), coupler%comp_comm)
81
82 ! Set up coupling configuration
83 CALL coupler_setup(coupler, simulation_grid)
84
85 ! ---- Time loop ---------------------------------------------------------
86
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))
90
91 IF (is_root) THEN
92 WRITE(6, '(A)') " "
93 WRITE(6, '(A)') " ["//trim(comp_name)//"]: Starting time loop"
94 WRITE(6, '(A)') &
95 " ======================================"
96 END IF
97
98 step = 0
99
100 ! Loop until the ML predictor indicates that there is no more data to process
101 ! (i.e., the simulation has reached its end time, which is defined in
102 ! the YAML configuration file)
103 DO WHILE (ml_predictor_has_data(coupler))
104
105 step = step + 1
106
107 ! Generate synthetic temperature field from cell-center coordinates
108 ! T(x,y,t) = 15 + 6*sin(x)*cos(y) + 4*cos(2*y) + 0.5*t
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)
112 t_val = 15.0_wp &
113 + 6.0_wp * sin(x_cell) * cos(y_cell) &
114 + 4.0_wp * cos(2.0_wp * y_cell) &
115 + 0.5_wp * step
116 temperature(i, 1) = t_val
117 END DO
118
119 ! Exchange data with the ML predictor component via coupler.
120 ! heat_flux is only actually received if it is coupled back (two-way
121 ! coupling); for a one-way (training) configuration, heat_flux_received
122 ! is .FALSE. and heat_flux is left unmodified (see coupler_mod.F90).
124 coupler, temperature, heat_flux, heat_flux_received)
125
126 IF (heat_flux_received) THEN
127
128 ! Lazy initialization of the coverage mask for the ML predictor domain
129 ! (only done once, after the first exchange)
130 IF (.NOT. ALLOCATED(ml_predictor_coverage)) THEN
131 ! Get the coverage mask from the ML predictor
132 ! (taking local core mask into account)
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)
137 ! Determine global number of predicted cells across all ranks
138 CALL mpi_allreduce( &
139 mpi_in_place, num_predicted_cells, 1, mpi_integer, mpi_sum, &
140 comp_comm, ierror)
141 IF (is_root) THEN
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)"
145 END IF
146 IF (num_predicted_cells == 0) THEN
147 IF (is_root) THEN
148 WRITE(6, '(A)') &
149 " ["//trim(comp_name)//"]: No cells covered by ML predictor. Exiting."
150 END IF
151 EXIT
152 END IF
153 END IF
154
155 ! Compute ref_heat_flux heat flux: H = 0.5*T + 0.01*T^2
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
159 END DO
160
161 ! Compute min and max of the heat flux prediction for diagnostic output
162 WHERE (.NOT. ml_predictor_coverage) heat_flux(:, 1) = 999._wp
163 heat_flux_min_local = minval(heat_flux(:, 1))
164 CALL mpi_reduce( &
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))
169 CALL mpi_reduce( &
170 heat_flux_max_local, heat_flux_max_global, 1, mpi_real_wp, &
171 mpi_max, 0, comp_comm, ierror)
172
173 ! Compute global deviation between predicted and reference heat flux for diagnostic output
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(:)))
176 CALL mpi_reduce( &
177 heat_flux_sum_diff_local, heat_flux_sum_diff_global, 1, mpi_real_wp, mpi_sum, 0, &
178 comp_comm, ierror)
179 heat_flux_mean_diff_global = &
180 heat_flux_sum_diff_global / real(num_predicted_cells, wp)
181
182 IF (is_root) THEN
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
188 END IF
189
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)"
194 END IF
195
196 END DO
197
198 DEALLOCATE(temperature, heat_flux, ref_heat_flux)
199
200 IF (is_root) THEN
201 WRITE(6, '(A)') &
202 " ======================================"
203 WRITE(6, '(A)') " ["//trim(comp_name)//"]: Finished"
204 END IF
205
206 ! ---- Finalise ----------------------------------------------------------
207 CALL icon_grid_cleanup(simulation_grid)
208 CALL coupler_finalise(coupler)
209
210CONTAINS
211
218 SUBROUTINE parse_arguments(configFilename, gridFilename)
219
220 CHARACTER(LEN=arg_char_length) :: configFilename
221 CHARACTER(LEN=arg_char_length) :: gridFilename
222
223 CHARACTER(LEN=arg_char_length) :: arg
224 INTEGER :: i
225 LOGICAL :: skip_arg = .false.
226
227 DO i = 1, command_argument_count()
228 IF (.NOT. skip_arg) THEN
229 CALL get_command_argument(i, arg)
230 SELECT CASE (arg)
231 CASE ('-c')
232 IF (i == command_argument_count()) THEN
233 print '(2A, /)', &
234 'missing parameter for option: ', arg
235 print '(A)', ' -c configFilename'
236 print '(A)', ' -g gridFilename'
237 stop
238 ELSE
239 CALL get_command_argument(i + 1, &
240 configfilename)
241 skip_arg = .true.
242 END IF
243 CASE ('-g')
244 IF (i == command_argument_count()) THEN
245 print '(2A, /)', &
246 'missing parameter for option: ', arg
247 print '(A)', ' -c configFilename'
248 print '(A)', ' -g gridFilename'
249 stop
250 ELSE
251 CALL get_command_argument(i + 1, &
252 gridfilename)
253 skip_arg = .true.
254 END IF
255 CASE DEFAULT
256 print '(2A, /)', 'unrecognised option: ', arg
257 print '(A)', ' -c configFilename'
258 print '(A)', ' -g gridFilename'
259 stop
260 END SELECT
261 ELSE
262 skip_arg = .false.
263 END IF
264 END DO
265
266 END SUBROUTINE parse_arguments
267
268END PROGRAM simulation_component
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)
Definition toy_scrip.c:490