Creating User-defined Functions in SD Models¶
One of the benefits of creating System Dynamics models in Python is that we can use the full power of Python to create our own functions, which we can then use in our models.
This how to illustrates how to do this.
First of all, lets set up our model:
from BPTK_Py import Model from BPTK_Py import sd_functions as sd
model = Model(starttime=1,stoptime=10,dt=0.25,name='Test Model')
Now let’s define a function we would like to use in our model. A user defined function can have as many arguments as you like, but it must accept at least a model and time parameter (you don’t need to use the parameters if you don’t want to).
How you define your function is up to you - you can use any of the methods available in Python, such as class methods, using def, or lambda functions.
The example below uses a lambda function which simply multiplies the
t with 5.
my_model_function = model.function("my_model_function", lambda model, t: 5*t)
As you can see, much like with stocks and converters, we associate our
function with the model by calling the models
Next we set up a converter:
converter = model.converter("converter")
The converters equation calls the model function.
converter.equation = my_model_function()
We can test the function as follows:
Let’s plot the function over time:
We can also create a stock that has the converter as an inflow:
stock = model.stock("stock")
stock.equation = converter
We can do all the usual arithmethic:
The function we created above was just dependent on time and not on other model variables. Let’s create a function that takes more argmuments, e.g. one that multiplies a model variable with time.
You can add as many arguments as you like, but they must come after the
another_model_function = model.function("another_model_function", lambda model, t, element: 5*t*element)
Define a new converter which will be the input for the function:
input_converter = model.converter("input_converter")
Now add a converter which will apply the
another_converter = model.converter("another_converter")