Skip to content

Commit

Permalink
Added example
Browse files Browse the repository at this point in the history
  • Loading branch information
teubert committed Nov 16, 2023
1 parent 83cfdcc commit 931d15e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
72 changes: 72 additions & 0 deletions examples/06_Combining_Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,78 @@
"fig = simulated_results.states.plot(keys=['DCMotor.i_b', 'DCMotor.i_c', 'DCMotor.i_a'], ylabel='ESC Currents')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: A function can be used to perform simple transitions between models. For example, if you wanted to multiply the torque by 1.1 to represent some gearing or additional load, that could be done by defining a function, as follows"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def torque_multiplier(t_l):\n",
" return t_l * 1.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The function is referred to as 'function' by the composite model. So we can add the function into the connections as follows. Note that the argument name is used for the input of the function and 'return' is used to signify the function's return value. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"connections = [\n",
" ('PropellerLoad.t_l', 'function.t_l'),\n",
" ('function.return', 'DCMotor.t_l')\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's add back in the other connections and build the composite model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"connections.extend([\n",
" ('ESC.v_a', 'DCMotor.v_a'),\n",
" ('ESC.v_b', 'DCMotor.v_b'),\n",
" ('ESC.v_c', 'DCMotor.v_c'),\n",
" ('DCMotor.theta', 'ESC.theta'),\n",
" ('DCMotor.v_rot', 'PropellerLoad.v_rot')\n",
"])\n",
"m_powertrain = CompositeModel(\n",
" (m_esc, m_load, m_motor, torque_multiplier), \n",
" connections=connections,\n",
" outputs={'DCMotor.v_rot', 'DCMotor.theta'})\n",
"simulated_results = m_powertrain.simulate_to(2, future_loading, dt=2.5e-5, save_freq=1e-2)\n",
"fig = simulated_results.outputs.plot(compact=False, keys=['DCMotor.v_rot'], ylabel='Velocity')\n",
"fig = simulated_results.states.plot(keys=['DCMotor.i_b', 'DCMotor.i_c', 'DCMotor.i_a'], ylabel='ESC Currents')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that you can also have functions with more than one argument. If you dont connect the arguments of the function to some model, it will show up in the inputs of the composite model."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
8 changes: 4 additions & 4 deletions src/progpy/composite_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class CompositeModel(PrognosticsModel):
A CompositeModel is a PrognosticsModel that is composed of multiple PrognosticsModels. This is a tool for modeling system-of-systems. I.e., interconnected systems, where the behavior and state of one system effects the state of another system. The composite prognostics models are connected using defined connections between the output or state of one model, and the input of another model. The resulting CompositeModel behaves as a single model.
Args:
models (list[PrognosticsModel] or list[tuple[str, PrognosticsModel]]):
models ({list[{PrognosticsModel, function}], list[tuple[str, {PrognosticsModel, function}]]):
A list of PrognosticsModels to be combined into a single model.
Provided in one of two forms:
1. A list of PrognosticsModels. The name of each model will be the class name. A number will be added for duplicates
1. A list of PrognosticsModels or functions. The name of each model will be the class name for models or 'function' for functions. A number will be added for duplicates
2. A list of tuples where the first element is the model name and the second element is the model
2. A list of tuples where the first element is the model/function name and the second element is the model/function
Note: Order provided will be the order that models are executed
connections (list[tuple[str, str]], optional):
A list of tuples where the first element is the name of the output, state, or performance metrics of one model and the second element is the name of the input of another model.
A list of tuples where the first element is the name of the output, state, or performance metrics of one model or function return and the second element is the name of the input of another model or argument of a function.
The first element of the tuple must be of the form "model_name.output_name", "model_name.state_name", or "model_name.performance_metric_key".
The second element of the tuple must be of the form "model_name.input_name".
For example, if you have two models, "Batt1" and "Batt2", and you want to connect the output of "Batt1" to the input of "Batt2", you would use the following connection: ("Batt1.output", "Batt2.input")
Expand Down

0 comments on commit 931d15e

Please sign in to comment.