How-To and Basic Usage Guide

Compile and Run a Model

You’ve been given a zip file with a generated model (or you’ve generated it yourself. Way to go!). Now you want to run it in Python. It’s as simple as importing PySimlink and calling print_all_params

from pysimlink import Model, print_all_params

model = Model("my_awesome_model", "./my_awesome_model.zip")
model.reset()
print_all_params(model)

Once you’ve figured out what signals you need to read, you can call model.step() to iterate over the model!

Change the Value of Signals

This feature is not directly supported by PySimlink. The reason why is kind of complex. In short, it would work for the fixed-step discrete solver. But for a solver with minor timesteps, changing the value of a signal could cause a singularity in an integrator, or might not even affect anything at all (if the signal is changed on the major time step from PySimlink, then updated by the originating block at the next minor timestep (which PySimlink does not control), then the change from PySimlink would have no affect).

How do we get around this? You’ll have to tweak the model and generate it again… While this is not ideal, this keeps us from violating solver during simulation.

Take this model, for example. And say you want to change the value of the highlighted signal during simulation.

../_images/signal_highlight.png

A garbage model with a signal whose value we want to change during simulation.

To be able to change the value, we need to terminate the signal and replace it with a constant. The resulting change looks like this.

../_images/signal_highlight2.png

The same model adjusted so we can change the value of this signal during simulation.

Now, during simulation, we change the “Value” parameter of the block labeled “Constant”. While this may not simulate properly in Simulink, you can change this value at every timestep to get your desired behavior.

Tip

If you’re making changes to your model like this just for code generation or for PySimlink and don’t want to change its normal behavior, check out Variant Subsystems. You can have one subsystem for code generation and one for Simulink simulation.

Read The Value of Bus Signals

Note

New in pysimlink 1.2.0

Bus signals are a bit more complicated than other signals. They are a collection of other signals, and can even be nested in other busses. PySimlink allows you to read bus signal types and their contents. To see the available signals, you can use the dir function.

model = Model("my_awesome_model", "./my_awesome_model.zip")
model.reset()
struct_signal = model.get_signal("my_awesome_model/sig1")
print(dir(struct_signal))

Note

There are a few python-internal properties prepended and appended by two underscores. These are not part of your bus and can be ignored.

You can access the signals in the bus as follows:

print(struct_signal.a)
print(struct_signal.b)