Skip to content

Simulation

To simulate a model (or models), we will need to first perform a few steps. In the most simple forms, these are the steps:

  1. Install the model
  2. Import the model class
  3. Create model objects
  4. Create a simulation object
  5. Simulate the simulation object.

Note

Naturally, the sund package needs to be imported first. We assume it has been imported as sund.

Note

For practical reasons, we assume that you have two models named model1 and model2, written in the model1.txt and model2.txt files respectively.

Installing models

To install a model, point the installer to a text file containing the model file.

sund.install_model('model1.txt') # Installs the model in model1.txt
sund.install_model('model2.txt') # Installs the model in model2.txt

Note that the model will get the name and standard values given in the text file. Also note that it is possible to install multiple models by giving a regular expression instead of a specific file name as input (e.g., 'model[12].txt' in this case).

Importing the model class

Then, we need to import the model classes of the installed models before we can construct the model objects:

m1_class = sund.import_model('model1') # Imports the model named 'model1'
m2_class = sund.import_model('model2') # Imports the model named 'model2'

Constructing the model objects

The next step is to construct the model objects:

m1 = m1_class() # Creates a model1 object, based on the model1 class. 
m2 = m2_class() # Creates a model2 object, based on the model2 class. 

Creating a simulation object

Next, we use the model objects to construct a simulation object:

sim = sund.Simulation(models = [m1, m2]) # Creates a simulation object, using our two model objects to define which models to simulate. 

Simulating the model

Finally, we simulate our simulation object. Note that we can access the simulated values of the model features using the feature_data attribute of the simulation object.

sim.simulate(time_vector = times) # Simulates the constructed simulation object, for some time vector 'times'. 
print(sim.feature_data)

Note that different alternative shortcuts are available for this Simulate() function. For instance, instead of writing time_vector, you could also write t :

sim.Simulate(t = times) # Simulates the constructed simulation object, for some time vector 'times'. 
print(sim.feature_data)

To have the featuredata as dictionary format, you can use the feature_data_as_dict() function.

dict=sim.feature_data_as_dict() 
print(dict)

Finally, from the dictionary format, you can also have the dataframe format using the pandas library.

import pandas as pd
df=pd.DataFrame(dict)
print(df)

Summary

This has been the most basic steps to simulate one or more models. In practice, you would typically also want to include parameter_values or state_values.

Warning

The simulation object does not reset between consecutive simulations if not explicitly asked for. Supply the sim.simulate(...) function with the key value - pair reset = True, or run sim.reset_simulation() between the simulations.