MooseControl

A helper class used to interact with a simulation running with a WebServerControl, which exposes a REST API for changing the simulation.

Basic usage

First, instantiate a control object with an input file that has a WebServerControl object named control_name:

# Setup the control; runs the application and interacts with it
moose_command = 'mpiexec -n 2 /path/to/app-opt -i input.i'
control = MooseControl(moose_command=moose_command, moose_control_name='control_name')

# Initialize the MOOSE process and wait for the webserver to start
control.initialize()

You should know which execution flags the webserver will be listening on, and how many times it will be listening. An example loop over an expected number of timesteps and action on TIMESTEP_BEGIN and TIMESTEP_END is as follows:

# The number of timesteps
num_steps = 10

for t in range(num_steps):
    # Wait for moose to be on TIMESTEP_BEGIN
    control.wait('TIMESTEP_BEGIN')

    # Get a postprocessor value
    pp_value = control.getPostprocesor('postprocesor_name')

    # Tell moose to continue on TIMESTEP_BEGIN
    control.setContinue()

    # Wait for moose to be done with a solve at TIMESTEP_END
    control.wait('TIMESTEP_END')

    # Set a controllable boundary condition value based on
    # the postprocesor value and the time. This is arbitrary
    # and isn't really physical, but describes usage
    control.setControllableReal('BCs/left/value', pp_value * t)

    # Tell moose to continue on TIMESTEP_END
    control.setContinue()

# Wait for MOOSE to finish up
control.finalize()

Class methods

MooseControl.MooseControl

MooseControl.MooseControl(moose_command: list[str] = None, moose_port: int = None, moose_control_name: str = None, inherit_environment: bool = True, poll_time: float = 0.1)

Helper for interacting with the MOOSE WebServerControl.

Use this class as follows: control = MooseControl(...) control.initialize() <interact with the process> control.finalize()

This object is tested primarily by test/tests/controls/web_server_control in the framework.

ControlException

Basic exception for an error within the MooseControl

finalize()

Waits for the MOOSE webserver to stop listening and for the MOOSE process to exit (if one was setup)

Use this when you think MOOSE should be done. This will throw in the event that the webserver is waiting for input when you think it should be done

getPostprocessor(name: str) -> float

Gets a postprocessor value

Parameters: name (str): The name of the postprocessor Returns: float: The value of the postprocessor

getWaitingFlag() -> str

Gets the current EXECUTE_ON flag that WebServerControl is waiting on

Returns: str or None: The current EXECUTE_ON flag if waiting, otherwise None

initialize()

Starts the MOOSE process (if enabled) and waits for the MOOSE webserver to start listening

Must be called before doing any other operations

isListening() -> bool

Returns whether or not the webserver is listening

isProcessRunning()

Returns whether or not a moose process is running

isWaiting() -> bool

Checks whether or not the webserver is waiting

Returns: bool: Whether or not the webserver is waiting

kill()

Kills the underlying moose process if one is running

possiblyRemoveSocket()

Attempts to remove the file socket if one was created and it exists.

returnCode()

Gets the return code of the moose process

setContinue()

Tells the WebServerControl to continue

setControllableBool(path: str, value: bool)

Sets a controllable bool-valued parameter

The provided value must be a bool

Parameters: path (str): The path of the controllable value value (float): The value to set

setControllableInt(path: str, value: int)

Sets a controllable int-valued parameter

The provided value must be numeric

Parameters: path (str): The path of the controllable value value (int): The value to set

setControllableReal(path: str, value: float)

Sets a controllable Real-valued parameter

The provided value must be numeric

Parameters: path (str): The path of the controllable value value (float): The value to set

setControllableString(path: str, value: str)

Sets a controllable string parameter

Parameters: path (str): The path of the controllable value value (str): The value to set

setControllableVectorInt(path: str, value: list[int])

Sets a controllable vector-of-int parameter

The provided value must be a list of numeric values

Parameters: path (str): The path of the controllable value value (list): The value to set

setControllableVectorReal(path: str, value: list[float])

Sets a controllable vector-of-Real parameter

The provided value must be a list of numeric values

Parameters: path (str): The path of the controllable value value (list): The value to set

setControllableVectorString(path: str, value: list[float])

Sets a controllable vector-of-string parameter

The provided value must be a list

Parameters: path (str): The path of the controllable value value (list): The value to set

spawnMoose(cmd: list[str], inherit_environment: bool = True) -> subprocess.Popen

Helper for spawning a MOOSE process that will be cleanly killed

wait(flag: str = None) -> str

Waits for the MOOSE webserver and returns once the WebServerControl is waiting for input

Parameters: flag (str or None): The expected execute on flag, if any, otherwise None Returns: str: The execute on flag