from BPTK_Py import Model, Module
Module
Module
Module Constructor
Module(model, name, parent=None)
The Module class is used to structure SD DSL models into individual modules.
Modules can be nested.
If you create model elements such as stocks, flows and converters via the module, the elments are added to the model, but the element names are turned into fully qualified names of the form parent_module_name.module_name.name.
The fully qualfied name is used as the equation name in the Model class and is needed when making calls to bptk.run_scenario or bptk.plot_scenario.
Check the Beergame or Enterprise Digital Twin models to see the module class in action.
Module.biflow
biflow(name)
Add a Biflow to the underlying model. The name of the biflow will be a fully qualified name consisting of all nested module names plus the actual element name using dot notation, i.e. namespace.name
Module.constant
constant(name)
Add a Constant to the model. The name of the constant will be a fully qualified name consisting of all nested module names plus the actual element name using dot notation, i.e. namespace.name
Module.converter
converter(name)
Add a Converter to the model. The name of the converter will be a fully qualified name consisting of all nested module names plus the actual element name using dot notation, i.e. namespace.name
Module.flow
flow(name)
Add a Flow to the model. The name of the flow will be a fully qualified name consisting of all nested module names plus the actual element name using dot notation, i.e. namespace.name
Module.fqn
fqn(name)
Given a name this returns the fully qualified name, i.e. name prefixed by the module namespace.
The namespace is defined by the names of all parent modules, e.g. parent_module_name.module_name
Parameters
name – String The name that is to be converted into a fully qualified name.
Return
Return the fully qualified name, i.e. namespace.name
Module.initialize
initialize(module,…)
Override this method in concrete Module subclasses and use it to define the model using Stocks, Flows, Converters and Constants.
Pass in any module dependencies as parameters.
Parameters
** module ** Module subclass External module that contains model elements that are needed within the current module
Module.stock
stock(name)
Add a Stock to the model. The name of the stock will be a fully qualified name consisting of all nested module names plus the actual element name using dot notation, i.e. namespace.name
Usage
Let’s create a simple example model containing to modules A and B that depend on each other.
Set up a model that runs over 10 timesteps:
= Model(starttime=1,stoptime=10,dt=1,name='model') model
Now create to module classes that define the actual model:
class A(Module):
def initialize(self,b):
## stocks
= self.stock("stock")
stock
## flows.
= self.flow("flow")
flow
## equations
= 0.0
stock.initial_value = flow
stock.equation
= b.stock("stock") flow.equation
class B(Module):
def initialize(self,a):
## stocks
= self.stock("stock")
stock
## flows.
= self.flow("flow")
flow
## equations
= 1.0
stock.initial_value = flow
stock.equation
= a.stock("stock") flow.equation
Initialize the modules:
= A(model,"a")
a = B(model,"b")
b
a.initialize(b) b.initialize(a)
Plot the graph for the a_module.a_stock element:
"a.stock").plot() model.stock(
Of course you can also access the model element directly via their respective modules:
"stock").plot() a.stock(
In practice you will probably set up and scenario managers and then access the model elements via the bptk class.
from BPTK_Py import bptk
= bptk() bptk
bptk.register_scenario_manager(
{"sm":
{"model":model,
"scenarios":
{"base":{}
}
}
} )
bptk.plot_scenarios(=["sm"],
scenario_managers=["base"],
scenarios=["a.stock","b.stock"]
equations )
During model development, it can be useful to list all model equations:
bptk.list_equations()
Available Equations:
Scenario Manager: sm
Scenario: base
--
stock: a.stock
stock: b.stock
flow: a.flow
flow: b.flow