8from yac
import Action, ExchangeType, Field, Location, Reg2dGrid, TimeUnit, YAC
13 Encapsulates YAC setup and exchange for the ml_predictor component.
16 def __init__(self, component_name: str =
"ml_predictor"):
18 Initialize the YAC coupler for the ml_predictor component.
20 @param[in] component_name Name of the component for registration in YAC
27 if self.
comp.comp_comm.size > 1:
28 raise RuntimeError(
"ml_predictor must run in a single process")
30 def setup(self, grid_name: str, x_vertices: np.ndarray, y_vertices: np.ndarray):
32 Define the 2D regular grid and fields for the ml_predictor component.
34 @param[in] grid_name Name of the grid for registration in YAC
35 @param[in] x_vertices X coordinates of the grid vertices
36 @param[in] y_vertices Y coordinates of the grid vertices
38 grid =
Reg2dGrid(grid_name, x_vertices, y_vertices,
39 cyclic=[
False,
False])
42 x_centers = 0.5 * (x_vertices[:-1] + x_vertices[1:])
43 y_centers = 0.5 * (y_vertices[:-1] + y_vertices[1:])
46 points = grid.def_points(Location.CELL, x_centers, y_centers)
50 "temperature", self.
comp, points,
51 collection_size=1, timestep=
"1", timeunit=TimeUnit.SECOND)
54 "heat_flux", self.
comp, points,
55 collection_size=1, timestep=
"1", timeunit=TimeUnit.SECOND)
64 return self.
temp_field.action != Action.OUT_OF_BOUND
69 Whether heat_flux is actually coupled back to the simulation
70 component. A one-way (training) configuration only couples
71 temperature, in which case heat_flux's role is
72 ExchangeType.NONE instead of ExchangeType.SOURCE.
74 @return True if heat_flux should be sent via `send_heat_flux`
81 Return the current datetime of the next exchange
82 (temperature is used as a reference).
84 @return The current datetime of the next exchange as an ISO 8601
85 string (e.g., "2024-01-01T00:00:00Z")
91 Receive the temperature field from the Fortran component.
93 @return The temperature values, or None if no data was received.
96 assert info
in (Action.COUPLING, Action.GET_FOR_RESTART, Action.NONE), (
97 f
"Unexpected action: {info}"
99 if temperature
is None:
101 return temperature.ravel()
105 Send the predicted heat flux back to the Fortran component.
107 Only call this if `heat_flux_is_coupled` is True; otherwise there is
108 no coupling configured for heat_flux to send data on (e.g. during a
109 one-way training run).
111 @param[in] heat_flux The predicted heat flux values to send back.
114 "heat_flux is not coupled in the current configuration; "
115 "check heat_flux_is_coupled before calling send_heat_flux"
121 """Finalize the YAC coupler and clean up resources."""
122 if self.
yac is not None:
Encapsulates YAC setup and exchange for the ml_predictor component.
bool heat_flux_is_coupled(self)
Whether heat_flux is actually coupled back to the simulation component.
send_heat_flux(self, np.ndarray heat_flux)
Send the predicted heat flux back to the Fortran component.
str datetime(self)
Return the current datetime of the next exchange (temperature is used as a reference).
setup(self, str grid_name, np.ndarray x_vertices, np.ndarray y_vertices)
Define the 2D regular grid and fields for the ml_predictor component.
__init__(self, str component_name="ml_predictor")
Initialize the YAC coupler for the ml_predictor component.
finalize(self)
Finalize the YAC coupler and clean up resources.
np.ndarray|None recv_temperature(self)
Receive the temperature field from the Fortran component.
Initializies a YAC instance and provides further functionality.