Automate Iterative Altair Twin Activate Simulations

RSGarciarivas
RSGarciarivas
Altair Employee
edited November 12 in Altair HyperWorks

When running system simulations in Altair Twin Activate, it’s often useful to run multiple simulations, whether it’s for validating a system’s response to different phenomena, to perform statistical analysis or just to get a better grasp of how a parameter affects the overall response of a system.

This small demo shows the basics for setting up execution of sequential simulations using Twin Activate (and no additional tools). Keep in mind that there may be other more flexible and robust approaches leveraging tools like Altair Compose or Altair HyperStudy but, if you’re looking for something simple and quick to set up, this is it.

Example: Automated signal generation

This simple model consists only of a sine wave generator block and a scope:

The aim of automating sequential simulations in this case is to change two of the sine wave’s parameters, magnitude and frequency, and observe the resulting curves in the scope all at once.

For the automation, the initialization and finalization scripts leverage functions from Twin Activate’s OML Simulation API, which has many commands that come in handy when customizing models and simulations. Let’s have a look at both scripts from the model context window:

  • Initialization script:

First, the number of simulation runs is defined and added to the Base OML Environment, which is a workspace into which the simulator reads and writes variables. This is done with the AddToBase command

The Base OML Environment is an environment which persists between simulations, so any variables that will be used either after a simulation is completed or in subsequent simulations must be added to it.

% Set # of simulations to run

nSims = 3;

AddToBase('nSims', nSims);

Likewise, a counter variable needs to be defined and updated in the Base Environment to run the required number of simulations. In this case, command GetFromBase is needed to get the value of i if it already exists or set it to 1 otherwise (AddToBase is needed to define i in the Base during the first simulation). Also, if it’s the first iteration (i == 1), close allis used to delete any existing scopes from previous runs, so only the results from the most recent run are visible.

% Initialize counter

i = GetFromBase('i', 1);

AddToBase('i', i);

% Reset scope if needed

if i == 1

close all

end

To create sine waves with different magnitudes and frequencies, both have to be declared in the initialization script and sent to the Base, as they will be used later.

% Random magnitude & frequency for signal

mag = 10*rand;

AddToBase('mag', mag);

f = 2*rand + 2;

AddToBase('f', f);

The SineWaveGenerator block must be parameterized with these variables from its parameters window:

  • Finalization script:

After each simulation is completed, all relevant variables must be retrieved from the Base.

% Get relevant variables from base environment

nSims = GetFromBase('nSims', 0);

i = GetFromBase('i', 1);

mag = GetFromBase('mag', 0);

f = GetFromBase('f', 0);

Variables mag and f are needed in the finalization script for labeling each curve accordingly in the output scope. This is done setting the active figure handle to the scope, getting the handle of the axis contained in it and, on each iteration, getting the handle of the most recently added curve (the first child of an axis handle is the most recently added curve).

All this is done to appropriately label each curve on the scope, which is done using command sprintf and setting each curve’s “displayname” property accordingly.

% Add info label to signal in scope

figure(1);

ax = gca;

curve = get(ax, 'children')(1);

curveName = sprintf('mag = %1.2f, f = %1.2f', mag, f);

set(curve, 'displayname', curveName);

Finally, some logic is used to decide whether the counter should be updated and sent to the Base Environment again before running the simulation another time (using vssRunSimulation), or if the counter should be reset and the legend for the scope turned on to show all the signals’ labels.

% Run simulation again or reset

if i < nSims

i = i + 1;

AddToBase('i', i);

vssRunSimulation;

else

AddToBase('i', 1);

legend on

end

With these scripts, running the workflow for three iterations will display each curve and its label like this:

Although this is an extremely simple example, my goal is to show you how you can leverage the Base OML Environment and Twin Activate’s OML API to automate sequential simulations. Go ahead and implement this in your own use cases. You no longer need to run each simulation manually each time!

Reach out in the community product forums if you are having issues leveraging Twin Activate in this way. Alternatively, email hwsupport@altair.com or reach out for support from your Altair One account if you need assistance from experts.