How to change simulation parameter via xml-rpc during simulation?

Hi,

I am using python xml-rpc to run simulation and use plecs.simulate() to start simulation.

image

Once simulation start, until it ends, I am unable to send get&set parameter command.

However if in model window, if I change the value of a component, say resistor, it changes during simulation and works well. How can I achieve this via script.

If someone have found a solution like a framework which we can make in model itself so that it applies desired changes to parameters at the right time we need, it would be really helpful.

Hi @vishweshgm

It is not possible to change parameters during runtime using a script.

If you know in advance when a parameter needs to be modified, you should implement the corresponding logic directly in your model. However, in some cases this is not feasible and the decision must be made “on the fly.”

When using scripting, the required workflow is to stop the simulation, save the states, modify the desired parameter, and then restart the simulation using the stored states.

An example of this approach can be found in the following forum post:
Co-Simulation with Scripting for Changing Thermal Parameters During Simulation

Although the example focuses on thermal simulations, the same workflow can be applied to any parameter that needs to be changed during simulation.

Hope this helps.

Thanks @RetoChristen

I tried the method proposed here. What I see is during second simulation run, plecs scope starts again from t=0.0 with new trace. Is this expected behavior? Is there a way we can continue from last tarce file or merge the new trace file with old?

When stored states are loaded for a new simulation, PLECS starts from the final state of the previously stored system, not from time 0. Since the traces are generated as new results, you need to keep the traces from the first simulation if you want to see the complete run.

As this behavior is demonstrated in all three models in the post, could you clarify which one did not work for you? Also, are you running the script in Octave or Python? And which PLECS version are you using?

Hi
just following up on this topic after long time,

I was able to use save the systemState feature and use scripting to get the model variables change during runtime.

I was happy until, my model integrated DLL. Now when I resume after changing ModelVar using script “on the fly”, DLL restarts from plecsStart().

Can we have this fixed?
b.t.w my company uses 4.2.6 version, please tell me if this bug/issue is fixed in any updated versions.

More details:

I am scripting a simulation over XML-RPC and need to change a model variable at a specific point in simulation time. As advised in earlier threads, I am doing this by splitting the run into two segments and handing the system state across the boundary:

seg1 = plecs.simulate(MODEL, {
    "ModelVars":  {"MyVar": 100},
    "SolverOpts": {"StartTime": 0.0, "StopTime": 0.5},
})
state = plecs.get(MODEL, "SystemState")

seg2 = plecs.simulate(MODEL, {
    "ModelVars":  {"MyVar": 120},
    "SolverOpts": {"StartTime": 0.5, "StopTime": 1.0,
                   "InitialSystemState": state},
})

This works: the variable takes the new value in the second segment, and the circuit states continue from where the first segment stopped.

I can see from its log output that plecsStart is called again for the second segment, so the controller re-initialises from scratch while the plant continues from the restored state.

Is the DLL unloaded and reloaded between consecutive simulate() calls in the same PLECS session, or does it stay loaded so that static variables retain their values? This determines whether preserving controller state is possible at all.

One possible solution would be to modify the DLL itself so that the relevant controller states are stored as PLECS states rather than only as internal variables of the DLL.

The DLL interface provides the states array in SimulationState for this purpose. The number of states is specified via numStates, and the DLL can then read and update them through aState->states. You can find the corresponding interface description in the DLL block documentation. There is also some related discussion about storing data in DLLs in this forum thread.

I initially thought of another possibility that would avoid modifying the DLL: calling the DLL from a C-Script and using the C-Script states to preserve the controller state. In current PLECS versions, a C-Script can load external functions and provides several mechanisms for storing and restoring its state.

Unfortunately, this does not help in your particular case because you are using PLECS 4.2. The possibility to load external files from a C-Script was only introduced with PLECS 4.8.

Therefore, with PLECS 4.2, I do not see a practical way of implementing such a wrapper around the existing DLL. Modifying the DLL so that its controller states are exposed through the DLL block’s state interface would probably be the most promising approach.

Wrapper to start DLL from C-Script in PLECS 5.0:
CScriptDLLInterface.zip (200.8 KB)