class SimulinkExporter:
"""Analyse an assembly into a `SimulinkModel` (step 1 of `docs/local/done/simulink_export.md`).
Args:
assembly (Assembly): The assembly; its FMUs are read from `assembly.fmu_directory`.
Raises:
AssemblyError: If the assembly cannot be exported (see `analyse`).
"""
def __init__(self, assembly: Assembly):
if assembly.root is None:
raise AssemblyError("The assembly is empty: nothing to export to Simulink.")
self.assembly = assembly
self.fmu_directory = Path(assembly.fmu_directory)
self._analysed: dict[Path, EmbeddedFMU] = {} # one analysis per FMU file, shared by its instances
self.fmu_files: dict[str, Path] = {}
self.sample_times: list[float] = []
# --------------------------------------------------------------------------------------------- public API
def analyse(self, name: str | None = None) -> SimulinkModel:
"""Build the intermediate model of the assembly.
Args:
name (str | None): Name of the model, by default made from the name of the container.
Raises:
AssemblyError: If a node uses `ts_multiplier` (decision 7), if a step cannot be found (decision 13), if a
port does not exist or is used against its causality, or if an FMU cannot be read.
"""
root = self.assembly.root
assert root is not None # checked by the constructor
system = self._system(root)
return SimulinkModel(name=name or matlab_name(root.name), root=system,
fixed_step=gcd_of_steps(self.sample_times),
start_time=root.start_time, stop_time=root.stop_time, fmu_files=self.fmu_files)
def export(self, filename: str | Path) -> Path:
"""Write the MATLAB script that builds and saves the Simulink model (decisions 28, 33).
Args:
filename (str | Path): The script, e.g. `toto.m`: its function `toto()` builds and saves the model
`toto_model.slx` (decision 33). `.m` is appended when missing. The FMUs of a folder that MATLAB
refuses on its path (`resources`, `private`, `@…`, `+…`) are copied into `toto_fmus/` (decision 36).
Returns:
Path: The script.
Raises:
AssemblyError: If the name of the script is not a valid MATLAB identifier, if an FMU cannot be designated
relatively to the script (another drive), or if the assembly cannot be exported (see `analyse`).
"""
logger.warning("The Simulink export is at the alpha stage: check the generated model.")
filename = Path(filename)
if filename.suffix.lower() != ".m":
filename = filename.with_name(filename.name + ".m")
if not _identifier(filename.stem) or len(filename.stem + MODEL_SUFFIX) > MATLAB_NAME_LENGTH:
raise AssemblyError(f"'{filename.name}': the name of a MATLAB script must be a valid MATLAB identifier "
f"(a letter, then letters, digits or '_', at most "
f"{MATLAB_NAME_LENGTH - len(MODEL_SUFFIX)} characters).")
model = self.analyse(filename.stem + MODEL_SUFFIX)
directory = filename.resolve().parent
if reserved_folder(directory):
raise AssemblyError(f"'{directory.name}' cannot be added to the MATLAB path: write '{filename.name}' in "
f"another folder.")
folders = []
for name, path in model.fmu_files.items():
if reserved_folder(path.parent): # e.g. the FMUs extracted from an SSP file: <ssp>.dir/resources
copies = directory / f"{filename.stem}_fmus"
copies.mkdir(exist_ok=True)
shutil.copyfile(path, copies / name)
logger.info(f"'{path.parent.name}' cannot be added to the MATLAB path: '{name}' is copied into "
f"'{copies.name}'.")
path = copies / name
try:
folder = Path(os.path.relpath(path.parent, directory)).as_posix()
except ValueError: # another drive (Windows): no relative path
raise AssemblyError(f"'{path}' cannot be designated relatively to '{filename.name}' (another "
f"drive): write the script on the drive of the FMUs.") from None
if folder not in folders:
folders.append(folder)
assert self.assembly.root is not None
writer = MatlabWriter(model, folders, self.assembly.root.name, function_name=filename.stem)
filename.write_text(writer.render(), encoding="utf-8")
logger.info(f"Simulink model '{model.name}': MATLAB script '{filename}' written.")
return filename
# ------------------------------------------------------------------------------------------------- nodes
def _check_options(self, node: AssemblyNode):
"""Refuse `ts_multiplier`, warn about the options that the export ignores (decisions 5, 7, 10, 26)."""
if node.ts_multiplier:
raise AssemblyError(f"'{node.name}': the option ts_multiplier cannot be exported to Simulink.")
for option in ("mt", "profiling", "sequential"):
if getattr(node, option):
logger.warning(f"'{node.name}': the option {option} is ignored by the Simulink export.")
implicit = [option for option in ("auto_link", "auto_input", "auto_output", "auto_parameter", "auto_local")
if getattr(node, option)]
if implicit and not node.implicit_resolved:
logger.warning(f"'{node.name}': the implicit links and ports ({', '.join(implicit)}) are not exported to "
f"Simulink: only the explicit ones are.")
def _embedded(self, node: AssemblyNode, fmu_name: str) -> EmbeddedFMU:
"""The analysis of an embedded FMU, shared by the instances of its file; its file is added to the archive."""
path = (self.fmu_directory / node.fmu_path(fmu_name)).resolve()
if path not in self._analysed:
try:
self._analysed[path] = EmbeddedFMU(path)
except (FMUContainerError, OSError) as error:
raise AssemblyError(f"Cannot read '{node.fmu_path(fmu_name)}': {error}") from error
if path.name in self.fmu_files:
raise AssemblyError(f"'{self.fmu_files[path.name]}' and '{path}' have the same file name: the FMU "
f"block of Simulink designates an FMU by its file name only (decision 28).")
self.fmu_files[path.name] = path
return self._analysed[path]
@staticmethod
def _file_name(fmu: EmbeddedFMU) -> str:
return Path(fmu.fmu.fmu_filename).name
def _step(self, node: AssemblyNode, fmus: dict[str, EmbeddedFMU], children: dict[str, SimulinkSystem]) -> float:
"""Step of the node (decision 17): its `step_size`, otherwise deduced as `make_fmu` does (decision 13)."""
if node.step_size:
return node.step_size
steps = [(fmu.step_size, not fmu.imposes_step_size) for fmu in fmus.values() if fmu.step_size]
steps += [(child.sample_time, True) for child in children.values()]
step = deduce_step_size(steps)
if step is None:
raise AssemblyError(f"'{node.name}': no step size, neither in the assembly nor in its FMUs.")
return step
def _system(self, node: AssemblyNode) -> SimulinkSystem:
"""The system of a node, its sub-containers first."""
self._check_options(node)
children = {name: self._system(child) for name, child in node.children.items()}
fmus = {name: self._embedded(node, name) for name in node.fmu_names_list}
system = SimulinkSystem(block_name(node.name), self._step(node, fmus, children))
self.sample_times.append(system.sample_time)
# Blocks of the embedded FMUs and of the sub-containers
names: dict[str, str] = {} # name in the assembly -> name of the block
for name, fmu in fmus.items():
ports = [port for port in fmu.ports.values() if not port.is_fmi2_aggregate]
names[name] = system.unique_name(block_name(name))
system.add(FmuBlock(names[name], self._file_name(fmu), fmu.fmi_version or 0, fmu.is_me, system.sample_time,
inputs=[port.name for port in ports if port.causality == "input"],
outputs=[port.name for port in ports if port.causality == "output"]))
for name, child in children.items():
names[name] = system.unique_name(block_name(name))
system.add(SubsystemBlock(names[name], child))
connected_inputs: set[tuple[str, int | str]] = set()
connected_outputs: set[tuple[str, int | str]] = set()
def endpoint(port: Port, direction: str) -> _Endpoint:
return self._endpoint(node, fmus, children, names, port, direction)
def connect(source: _Endpoint, target: _Endpoint):
system.lines.append(SimulinkLine(source.block, source.port, target.block, target.port))
connected_outputs.add((source.block, source.port))
connected_inputs.add((target.block, target.port))
# Exposed inputs: one Inport per name, a line to each of its targets (decision 4)
inports: dict[str, str] = {}
for port, exposed in node.input_ports.items():
try:
target = endpoint(port, "input")
except _NotAPort as error:
logger.warning(str(error))
continue
exposed = exposed or (reset_input_name(port.fmu_name) if target.port == "Reset" else port.port_name)
if exposed not in inports:
inports[exposed] = system.unique_name(exposed)
system.add(InportBlock(inports[exposed], len(system.inports) + 1))
system.inports.append(exposed)
system.inport_starts[exposed] = target.start
system.lines.append(SimulinkLine(inports[exposed], 1, target.block, target.port))
connected_inputs.add((target.block, target.port))
# Links, the terminals expanded as the builder does (decision 24)
for link in node.links:
for from_port, to_port in self._expand(node, fmus, link.from_port, link.to_port):
self._check_input_clocks(node, fmus, from_port, to_port)
source, target = endpoint(from_port, "output"), endpoint(to_port, "input")
connect(source, target)
# Exposed outputs
for port, exposed in node.output_ports.items():
try:
source = endpoint(port, "output")
except _NotAPort as error:
logger.warning(str(error))
continue
exposed = exposed or port.port_name
name = system.unique_name(exposed)
system.add(OutportBlock(name, len(system.outports) + 1))
system.outports.append(exposed)
system.lines.append(SimulinkLine(source.block, source.port, name, 1))
connected_outputs.add((source.block, source.port))
# Dropped ports: a Terminator (decision 22)
for port in node.drop_ports:
source = endpoint(port, "output")
if (source.block, source.port) not in connected_outputs:
self._terminate(system, source, connected_outputs)
self._start_values(node, fmus, children, names, system, endpoint, connected_inputs)
self._resets(node, fmus, names, system, connected_inputs)
self._unconnected(node, fmus, children, names, system, connected_inputs, connected_outputs)
self._break_loops(node, fmus, names, system)
return system
# ------------------------------------------------------------------------------------------------ loops
def _break_loops(self, node: AssemblyNode, fmus: dict[str, EmbeddedFMU], names: dict[str, str],
system: SimulinkSystem):
"""A *Unit Delay* on each line that closes a loop of FMU blocks or subsystems (decision 34).
The FMU blocks and the atomic subsystems have a direct feedthrough for Simulink: a loop of them is an
algebraic loop. A depth-first search in the order of the blocks finds the lines going back to a block of the
current path; each one gets a delay, whose initial value is the start value of its source output, otherwise
of its target input, otherwise 0.
"""
nodes = [name for name, block in system.blocks.items() if isinstance(block, FmuBlock | SubsystemBlock)]
successors: dict[str, list[str]] = {name: [] for name in nodes}
for line in system.lines:
if line.source in successors and line.target in successors and line.target not in successors[line.source]:
successors[line.source].append(line.target)
back_edges: set[tuple[str, str]] = set()
state: dict[str, int] = {} # 1: on the current path, 2: done
def visit(name: str):
state[name] = 1
for successor in successors[name]:
if state.get(successor) == 1:
back_edges.add((name, successor))
elif successor not in state:
visit(successor)
state[name] = 2
for name in nodes:
if name not in state:
visit(name)
if not back_edges:
return
fmu_of_block = {names[name]: fmus[name] for name in fmus}
lines = []
for line in system.lines:
if (line.source, line.target) not in back_edges:
lines.append(line)
continue
initial = self._delay_initial(fmu_of_block, system, line)
delay = system.unique_name(f"{line.source}.{line.source_port} delay")
system.add(DelayBlock(delay, initial, system.sample_time))
lines += [SimulinkLine(line.source, line.source_port, delay, 1),
SimulinkLine(delay, 1, line.target, line.target_port)]
logger.info(f"'{node.name}': loop of '{line.source}' → '{line.target}' broken by a Unit Delay.")
system.lines = lines
@staticmethod
def _delay_initial(fmu_of_block: dict[str, EmbeddedFMU], system: SimulinkSystem, line: SimulinkLine) -> StartValue:
"""Initial value of a delay: start value of the source output, otherwise of the target input, otherwise 0."""
source, target = system.blocks[line.source], system.blocks[line.target]
if isinstance(source, FmuBlock) and isinstance(line.source_port, int):
port = fmu_of_block[source.name].ports[source.outputs[line.source_port - 1]]
if port.start_value is not None:
return StartValue(str(port.start_value), port.type_name)
if isinstance(target, FmuBlock) and isinstance(line.target_port, int):
port = fmu_of_block[target.name].ports[target.inputs[line.target_port - 1]]
if port.start_value is not None:
return StartValue(str(port.start_value), port.type_name)
return StartValue("0", port.type_name)
return StartValue("0", "real64")
# ------------------------------------------------------------------------------------------------- ports
def _endpoint(self, node: AssemblyNode, fmus: dict[str, EmbeddedFMU], children: dict[str, SimulinkSystem],
names: dict[str, str], port: Port, direction: str) -> _Endpoint:
"""The block and the port index of `port`, used as an `input` or an `output` of a connection."""
if port.fmu_name in fmus:
fmu = fmus[port.fmu_name]
node_block = names[port.fmu_name]
if direction == "input" and node.reset_ports and port.port_name == FMUContainer.RESET_PORT:
return _Endpoint(node_block, "Reset", port.port_name, "boolean", None)
fmu_port = fmu.ports.get(port.port_name)
if fmu_port is None or fmu_port.is_fmi2_aggregate:
raise AssemblyError(f"'{node.name}': port '{port}' does not exist.")
if fmu_port.causality != direction and \
fmu_port.causality in ("parameter", "structuralParameter", "local", "calculatedParameter"):
raise _NotAPort(f"'{node.name}': '{port}' is a {fmu_port.causality}: the FMU block of Simulink has no "
f"port for it, it is not exposed.")
if fmu_port.causality != direction:
raise AssemblyError(f"'{node.name}': port '{port}' has the causality '{fmu_port.causality}': it "
f"cannot be used as an {direction}.")
ports = [p for p in fmu.ports.values() if not p.is_fmi2_aggregate and p.causality == direction]
index = next(i for i, p in enumerate(ports, start=1) if p.name == port.port_name)
start = self._fmu_start(node, port, fmu_port) if direction == "input" else None
return _Endpoint(node_block, index, str(port), fmu_port.type_name, start)
if port.fmu_name in children:
child = children[port.fmu_name]
exposed = child.inports if direction == "input" else child.outports
if port.port_name not in exposed:
raise AssemblyError(f"'{node.name}': port '{port}' is not an {direction} of the sub-container.")
start = child.inport_starts.get(port.port_name) if direction == "input" else None
return _Endpoint(names[port.fmu_name], exposed.index(port.port_name) + 1, str(port), None, start)
raise AssemblyError(f"'{node.name}': '{port.fmu_name}' is neither an FMU nor a sub-container of the node.")
@staticmethod
def _fmu_start(node: AssemblyNode, port: Port, fmu_port: EmbeddedFMUPort) -> StartValue | None:
"""Start value of an FMU input: the one of the assembly, otherwise the one of the FMU."""
value = node.start_values.get(port, fmu_port.start_value)
return None if value is None else StartValue(str(value), fmu_port.type_name)
def _expand(self, node: AssemblyNode, fmus: dict[str, EmbeddedFMU], from_port: Port, to_port: Port):
"""The port-to-port links of a link: itself, or the links of two connected terminals (decision 24)."""
fmu_from, fmu_to = fmus.get(from_port.fmu_name), fmus.get(to_port.fmu_name)
if fmu_from is None or fmu_to is None or from_port.port_name not in fmu_from.terminals \
or to_port.port_name not in fmu_to.terminals:
return [self._oriented(fmus, from_port, to_port)]
terminal1, terminal2 = fmu_from.terminals[from_port.port_name], fmu_to.terminals[to_port.port_name]
if not terminal1 == terminal2:
raise AssemblyError(f"'{node.name}': the terminals '{from_port}' and '{to_port}' are not compatible.")
try:
pairs = terminal1.connect(terminal2)
except TerminalError as error:
raise AssemblyError(f"'{node.name}': cannot plug the terminals '{from_port}' and '{to_port}': "
f"{error}") from error
self._terminal_parameters(node, fmus, from_port.fmu_name, to_port.fmu_name,
terminal1.connected_parameters(terminal2))
return [self._oriented(fmus, Port(from_port.fmu_name, a), Port(to_port.fmu_name, b)) for a, b in pairs]
@staticmethod
def _oriented(fmus: dict[str, EmbeddedFMU], from_port: Port, to_port: Port) -> tuple[Port, Port]:
"""A link from an output to an input: inverted when it goes from an input to an output, as the builder
accepts."""
def causality(port: Port) -> str | None:
fmu = fmus.get(port.fmu_name)
fmu_port = fmu.ports.get(port.port_name) if fmu else None
return fmu_port.causality if fmu_port else None
if causality(from_port) == "input" and causality(to_port) == "output":
return to_port, from_port
return from_port, to_port
@staticmethod
def _check_input_clocks(node: AssemblyNode, fmus: dict[str, EmbeddedFMU], from_port: Port, to_port: Port):
"""Refuse a link between two input clocks (FMI-LS-BUS): the container schedules them without source, a line
of Simulink goes from an output to an input (decision 27)."""
ports = [fmus[port.fmu_name].ports.get(port.port_name) if port.fmu_name in fmus else None
for port in (from_port, to_port)]
if all(port is not None and port.causality == "input" and port.type_name == "clock" for port in ports):
raise AssemblyError(f"'{node.name}': the input clocks '{from_port}' and '{to_port}' are linked together: "
f"such a link cannot be exported to Simulink.")
@staticmethod
def _terminal_parameters(node: AssemblyNode, fmus: dict[str, EmbeddedFMU], fmu1: str, fmu2: str, pairs):
"""The parameters of a Configuration terminal take the value of the connected Bus Simulation, as the
builder does (`apply_terminal_parameters`), unless the assembly gives them a start value."""
for name1, name2 in pairs:
port1, port2 = fmus[fmu1].ports[name1], fmus[fmu2].ports[name2]
if port1.causality == "parameter" and port2.causality != "parameter":
target, source = Port(fmu1, name1), port2
elif port2.causality == "parameter" and port1.causality != "parameter":
target, source = Port(fmu2, name2), port1
else:
continue
if target not in node.start_values and source.start_value is not None:
node.start_values[target] = source.start_value
# ---------------------------------------------------------------------------------------- extra blocks
@staticmethod
def _terminate(system: SimulinkSystem, source: _Endpoint, connected_outputs: set):
name = system.unique_name(f"{source.block}.{source.name.split('/', 1)[-1]} end")
system.add(TerminatorBlock(name))
system.lines.append(SimulinkLine(source.block, source.port, name, 1))
connected_outputs.add((source.block, source.port))
def _start_values(self, node: AssemblyNode, fmus: dict[str, EmbeddedFMU], children: dict[str, SimulinkSystem],
names: dict[str, str], system: SimulinkSystem, endpoint, connected_inputs: set):
"""Start values of the assembly: values of the block parameters, start values of the connected inputs; those
of the unconnected inputs go to their `Constant` block (decision 21)."""
for port, value in node.start_values.items():
if port.fmu_name in children:
continue # an input of a sub-container: its Constant block, when it is not connected
fmu = fmus.get(port.fmu_name)
fmu_port = fmu.ports.get(port.port_name) if fmu else None
if fmu_port is None:
raise AssemblyError(f"'{node.name}': the start value of '{port}' is given to a port that does not "
f"exist.")
block = system.blocks[names[port.fmu_name]]
assert isinstance(block, FmuBlock)
if fmu_port.causality in ("parameter", "structuralParameter"):
block.parameters[port.port_name] = StartValue(str(value), fmu_port.type_name)
elif fmu_port.causality == "input":
target = endpoint(port, "input")
if (target.block, target.port) in connected_inputs:
block.input_starts[port.port_name] = StartValue(str(value), fmu_port.type_name)
else:
logger.warning(f"'{node.name}': the start value of '{port}' ({fmu_port.causality}) is not exported "
f"to Simulink.")
@staticmethod
def _resets(node: AssemblyNode, fmus: dict[str, EmbeddedFMU], names: dict[str, str], system: SimulinkSystem,
connected_inputs: set):
"""`reset_ports`: an FMU whose reset is driven is placed in a resettable subsystem (decision 23)."""
if not node.reset_ports:
return
for name in fmus:
block = system.blocks[names[name]]
assert isinstance(block, FmuBlock)
if (block.name, "Reset") in connected_inputs:
block.resettable = True
else:
logger.warning(f"Reset of FMU '{name}' is neither linked nor exposed: the FMU will never be reset.")
def _unconnected(self, node: AssemblyNode, fmus: dict[str, EmbeddedFMU], children: dict[str, SimulinkSystem],
names: dict[str, str], system: SimulinkSystem, connected_inputs: set, connected_outputs: set):
"""A `Constant` block on each unconnected input, a `Terminator` on each unconnected output (decisions 21,
22)."""
for name in [*fmus, *children]:
block = system.blocks[names[name]]
if isinstance(block, FmuBlock):
fmu = fmus[name]
inputs = [(index, fmu.ports[port]) for index, port in enumerate(block.inputs, start=1)]
for index, fmu_port in inputs:
if (block.name, index) in connected_inputs or fmu_port.type_name in NO_CONSTANT_TYPES:
continue
start = self._fmu_start(node, Port(name, fmu_port.name), fmu_port)
self._constant(system, block.name, index, fmu_port.name,
start or StartValue("0", fmu_port.type_name))
outputs = block.outputs
else:
assert isinstance(block, SubsystemBlock)
child = block.system
for index, exposed in enumerate(child.inports, start=1):
if (block.name, index) in connected_inputs:
continue
inner = child.inport_starts.get(exposed)
given = node.start_values.get(Port(name, exposed))
value = inner if given is None else StartValue(str(given), inner.type_name if inner else "real64")
if value is not None and value.type_name not in NO_CONSTANT_TYPES:
self._constant(system, block.name, index, exposed, value)
outputs = child.outports
for index, port_name in enumerate(outputs, start=1):
if (block.name, index) not in connected_outputs:
self._terminate(system, _Endpoint(block.name, index, port_name, None, None), connected_outputs)
@staticmethod
def _constant(system: SimulinkSystem, block: str, index: int, port_name: str, value: StartValue):
name = system.unique_name(f"{block}.{port_name} start")
system.add(ConstantBlock(name, value))
system.lines.append(SimulinkLine(name, 1, block, index))