coming from System Dynamics I am wondering how stopping a simulation, changing variables (including state reinitialization), and continueing the simulation can be achieved from the outside (e.g. using Mathematica's integration with the WSM)?
Use cases for this would first of all be interactive games (e.g. "Applying Modelica Tools to System Dynamics Based Learning Games: Project Management Gameo" by Miettinen et al. (2016)), but also it may be needed to control Bayesian identification procedures that require state reinitialization when observed state data are available.
How should one best go about this using WSM and Mathematica? One would need to stop and continue/restart the simulation at arbitrary points in time with the following requirements:
Thanks in advance for some ideas!
PS: A related question can be found on StackOverflow -- unfortunately the answers only relate to Dymola as far as I can see.
Capabilities like this only exists today for real-time simulations, there you can provide inputs, pause the simulation etc. during a simulation. Please check out the Mathematica tutorial WSMLink/tutorial/Simulate#1436150622 to see if that could be suitable for your (it's possible to run in scaled real-time as well).
To get true step-wise progression of time in a simulation today (with the possibility to change inputs at each step) I would export my model as a co-simulation FMU (wsm:///ref/UserGuide/ModelCenterFMUExportAndImport.html) and write a small FMI co-simulator in Mathematica using LibraryLink.
If you use the first approach and I want to send a command to the simulation every millisecond, you say that I can slow down real time and interact with the simulation, is there a way to guaranty that the commands always go into the input at every millisecond of simulation time? (make sure that inputs are not “lost” or doubled up). For example, can I use this approach to "control" a simulation by reading an output, calculate something in MMA and send a new input and guaranty that I do not miss an input?
The second approach seems to solve this problem. Will the FMU approach be controllable as described above? (Have the FMU send an output and then process it and return the new input and run the FMU again) Will there be any speed hit for using an FMU because the simulation must be reinitialized?
Indeed Co-Simulation looks like the way to go for gaming and probably also for state updating in filtering approaches. The flexibility of linking WSM and Mathematica unfortunately is promising and daunting at the same time. ;-)
no there is no way to guarantee that, when the simulation runs in real-time (scaled or not), that's the only thing it cares about. It will pass in any input it gets during a time-step to be used for the next time step, but it won't wait for data.
As you say, the FMI co-simulation approach solves that problem (and removes the overhead of having to pass everything over TCP/IP.
The process would be something like:
1. Initialize simulation
2. Get outputs/set inputs
3. Advance time to tn
4. Repeat from 2.
So no, you wouldn't need to reinitialize the FMU (as long as what you want to change can be exposed as inputs).