melgym.envs.melcor.MelcorEnv

class melgym.envs.melcor.MelcorEnv(melcor_model: str, control_cfs: list[str], min_action_value: float, max_action_value: float, control_horizon: int = 10, output_dir: str | None = None, melgen_path: str | None = None, melcor_path: str | None = None)

MELCOR control environment.

This environment redefines CF scale factors to control the variables recorded in the EDF. Between restarts, each CFnnn00 entry (automatically included in the MELCOR input) is rewritten after a specified number of timesteps, as defined by the control horizon.

__init__(melcor_model: str, control_cfs: list[str], min_action_value: float, max_action_value: float, control_horizon: int = 10, output_dir: str | None = None, melgen_path: str | None = None, melcor_path: str | None = None)

Initializes the MELCOR environment.

Parameters:
  • melcor_model (str) – Path to the MELCOR model file.

  • control_cfs (list[str]) – List of controlled CFs.

  • min_action_value (float) – Minimum action value.

  • max_action_value (float) – Maximum action value.

  • control_horizon (int) – Control horizon (timesteps between actions).

  • output_dir (Optional[str]) – Directory name for output files. If None, a default directory is used.

  • render_mode (Optional[str]) – Mode for rendering the environment.

  • melgen_path (Optional[str]) – Path to the MELGEN executable. If None, the default path in exec directory is used.

  • melcor_path (Optional[str]) – Path to the MELCOR executable. If None, the default path in exec directory is used.

Methods

__init__(melcor_model, control_cfs, ...[, ...])

Initializes the MELCOR environment.

close()

Closes the environment and cleans up resources.

get_wrapper_attr(name)

Gets the attribute name from the environment.

has_wrapper_attr(name)

Checks if the attribute name exists in the environment.

render()

Renders the environment.

reset([seed, options])

Resets the environment to an initial state and returns the first observation.

set_wrapper_attr(name, value, *[, force])

Sets the attribute name on the environment with value, see Wrapper.set_wrapper_attr for more info.

step(action)

Executes a step in the MELCOR environment by applying the given action, running a MELCOR simulation during a given control horizon, and retrieving the latest state.

_add_cfs_redefinition()

Includes the definitions of the CFs to be overwritten in the MELCOR input, inserting them after “EOR MELCOR”. If the marker is not found, the block is inserted before the last line.

Raises:

FileNotFoundError – If the input file is not found.

_check_termination(obs, info)

Checks if the episode has terminated. This method should be overridden in subclasses to define the termination condition.

Parameters:
  • obs (np.array) – Current observation.

  • info (dict) – Additional information about the current state.

_check_truncation(obs, info)

Checks if the episode should be truncated. This method should be overridden in subclasses to define the truncation condition.

Parameters:
  • obs (np.array) – Current observation.

  • info (dict) – Additional information about the current state.

_clean_out_files()

Cleans the output directory where past simulation files are stored.

_compute_reward(obs, info)

Computes the reward based on the current state. This method should be overridden in subclasses to define the reward function.

Parameters:
  • obs (np.array) – Current observation.

  • info (dict) – Additional information about the current state.

_get_last_edf_data()

Reads the last recorded values from the EDF file.

Returns:

An array containing the last recorded values as np.float64.

Return type:

np.array

Raises:
  • FileNotFoundError – If the EDF file is not found.

  • ValueError – If the file cannot be parsed correctly.

_update_cfs(action)

Updates the scale factor of every controlled CF in the MELCOR input according to a given action.

Parameters:

action (np.array) – New scale factors to assign to the CFs.

Raises:
  • Exception – If the marker “EOR MELCOR” is not found.

  • FileNotFoundError – If the input file is not found.

_update_time()

Updates the TEND value in the input file based on the specified control horizon.

Raises:
  • ValueError – If no TEND register is found in the input file.

  • FileNotFoundError – If the input file is not found.

close()

Closes the environment and cleans up resources.

Raises:

Exception – If the MELCOR or MELGEN processes cannot be terminated.

render()

Renders the environment. This method should be overridden in subclasses to define the render method.

reset(seed: int | None = None, options: dict | None = None)

Resets the environment to an initial state and returns the first observation.

Parameters:
  • seed (Optional[int]) – Seed for random number generation.

  • options (Optional[dict]) – Additional options for resetting. Not used by default.

Returns:

A tuple containing the initial observation and info.

Return type:

tuple

Raises:

Exception – If the MELGEN execution fails.

step(action)

Executes a step in the MELCOR environment by applying the given action, running a MELCOR simulation during a given control horizon, and retrieving the latest state.

Parameters:

action (np.array) – An array of control values (CFs scale factors) to be applied.

Returns:

  • np.array: Observations, representing the latest EDF values after simulation.

  • float: Reward calculated based on the latest state.

  • bool: Whether the episode has terminated.

  • bool: Whether the episode was truncated.

  • dict: Additional metadata, including:
    • ”TIME” (float): The current simulation time.

    • Controlled variable names as keys with their respective values.

Return type:

tuple

Raises:
  • Exception – If reset() has not been called before step().

  • Exception – If the MELCOR execution fails.