.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "howto/configuration.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_howto_configuration.py: ================== Configuring Models ================== At times, model templates need to be parametrized in a more of a programming sense than a mathematical one. An example of this is a linear time invariant (LTI) ODE system, where the size of the state vector and whether there is feedback control are dependent on what the user passes in for the state and input matrices. .. GENERATED FROM PYTHON SOURCE LINES 13-25 Module Configuration -------------------- One option for generating models is through a ``settings`` object in the top-level ``condor`` namespace, where you register the module's default configuration with ``get_settings``. Then the module is imported via ``get_module``. Here is the configured model source with the name ``_lti.py``: .. literalinclude:: _lti.py :caption: File: _lti.py :linenos: .. GENERATED FROM PYTHON SOURCE LINES 27-29 To use this module, we use :func:`~condor.settings.get_module`, passing its declared settings as concrete keyword arguments. .. GENERATED FROM PYTHON SOURCE LINES 29-39 .. code-block:: Python import numpy as np import condor A = np.array([[0.0, 1.0], [0.0, 0.0]]) B = np.array([[0.0], [1.0]]) dblint_mod = condor.settings.get_module("_lti", A=A, B=B) .. GENERATED FROM PYTHON SOURCE LINES 40-42 The returned object is a module, so we can access the model with its declared class name: .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: Python LTI_dblint = dblint_mod.LTI .. GENERATED FROM PYTHON SOURCE LINES 46-47 And finally we can use this configured ODE system to simulate a trajectory. .. GENERATED FROM PYTHON SOURCE LINES 47-61 .. code-block:: Python import matplotlib.pyplot as plt class Sim(LTI_dblint.TrajectoryAnalysis): tf = 20 initial[x] = [1.0, 0.1] sim = Sim(K=[1.0, 0.1]) plt.figure() plt.plot(sim.t, sim.x[0].squeeze()) .. image-sg:: /howto/images/sphx_glr_configuration_001.png :alt: configuration :srcset: /howto/images/sphx_glr_configuration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 62-63 We can also re-use the module with a different configuration: .. GENERATED FROM PYTHON SOURCE LINES 63-78 .. code-block:: Python LTI_exp = condor.settings.get_module("_lti", A=np.array([[0, 1], [-2, -3]])).LTI class Sim(LTI_exp.TrajectoryAnalysis): tf = 10 initial[x] = [1.0, 0.5] sim = Sim() plt.figure() plt.plot(sim.t, sim.x[0].squeeze()) .. image-sg:: /howto/images/sphx_glr_configuration_002.png :alt: configuration :srcset: /howto/images/sphx_glr_configuration_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 79-85 Programmatic Construction ------------------------- An alternative approach is to programmatically generate the model using the metaprogramming machinery Condor uses internally. See :ref:`metaprogramming-walkthrough` for a more thorough overview. .. GENERATED FROM PYTHON SOURCE LINES 85-118 .. code-block:: Python from condor.contrib import ModelTemplateType, ODESystem def make_LTI(A, B=None, name="LTISystem"): attrs = ModelTemplateType.__prepare__(name, (ODESystem,)) attrs["A"] = A state = attrs["state"] x = state(shape=A.shape[0]) attrs["x"] = x xdot = A @ x if B is not None: attrs["B"] = B K = attrs["parameter"](shape=B.T.shape) attrs["K"] = K u = -K @ x attrs["dynamic_output"].u = u xdot += B @ u attrs["dot"][x] = xdot plant = ModelTemplateType(name, (ODESystem,), attrs) return plant .. GENERATED FROM PYTHON SOURCE LINES 119-120 Use of the model factory function looks similar to using ``get_module``: .. GENERATED FROM PYTHON SOURCE LINES 120-134 .. code-block:: Python LTI_dblint = make_LTI(A, B=B) class Sim(LTI_dblint.TrajectoryAnalysis): tf = 20 initial[x] = [1.0, 0.1] sim = Sim(K=[1.0, 0.1]) plt.figure() plt.plot(sim.t, sim.x[0].squeeze()) .. image-sg:: /howto/images/sphx_glr_configuration_003.png :alt: configuration :srcset: /howto/images/sphx_glr_configuration_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 135-150 .. code-block:: Python LTI_exp = make_LTI(A=np.array([[0, 1], [-2, -3]])) class Sim(LTI_exp.TrajectoryAnalysis): tf = 20 initial[x] = [1.0, 0.5] sim = Sim() plt.figure() plt.plot(sim.t, sim.x[0].squeeze()) .. image-sg:: /howto/images/sphx_glr_configuration_004.png :alt: configuration :srcset: /howto/images/sphx_glr_configuration_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 151-153 .. code-block:: Python plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.390 seconds) .. _sphx_glr_download_howto_configuration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: configuration.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: configuration.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: configuration.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_