
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/plot_2kw_ipmsm_fvc_sweep.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_plot_2kw_ipmsm_fvc_sweep.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_plot_2kw_ipmsm_fvc_sweep.py:


2.2-kW IPMSM, FVC, parameter sweep
==================================

This example studies the sensitivity of sensorless flux-vector control (FVC) of a
2.2-kW interior permanent-magnet synchronous machine (IPMSM) drive to an error in the
stator resistance estimate at low speeds. The drive of
:doc:`plot_2kw_ipmsm_fvc` is simulated natively for several estimates, each simulation
taking a fraction of a second.

.. GENERATED FROM PYTHON SOURCE LINES 14-24

.. code-block:: Python

    import time
    from math import pi

    import matplotlib.pyplot as plt
    import motulator.drive.control.sm as control
    from motulator.common.utils import set_style
    from motulator.drive import model, utils

    from motulator_export import native








.. GENERATED FROM PYTHON SOURCE LINES 25-26

Compute base values based on the nominal values.

.. GENERATED FROM PYTHON SOURCE LINES 26-31

.. code-block:: Python


    nom = utils.NominalValues(U=370, I=4.3, f=75, P=2.2e3, tau=14)
    base = utils.BaseValues.from_nominal(nom, n_p=3)









.. GENERATED FROM PYTHON SOURCE LINES 32-35

Configure the system. The control system uses the stator resistance estimate
`k_R*R_s`. The speed reference is 0.1 p.u., and half the rated load torque is
applied at 0.6 s.

.. GENERATED FROM PYTHON SOURCE LINES 35-59

.. code-block:: Python



    def build_system(k_R: float) -> tuple[model.Drive, control.VectorControlSystem]:
        """Build the system with the stator resistance estimate `k_R*R_s`."""
        par = model.SynchronousMachinePars(
            n_p=3, R_s=3.6, L_d=0.036, L_q=0.051, psi_f=0.545
        )
        mdl = model.Drive(
            model.SynchronousMachine(par),
            model.MechanicalSystem(J=0.015),
            model.VoltageSourceConverter(u_dc=540),
        )
        est_par = model.SynchronousMachinePars(
            n_p=3, R_s=k_R * 3.6, L_d=0.036, L_q=0.051, psi_f=0.545
        )
        cfg = control.FluxVectorControllerCfg(i_s_max=1.5 * base.i, sensorless=True)
        vector_ctrl = control.FluxVectorController(est_par, cfg)
        speed_ctrl = control.SpeedController(J=0.015, alpha_s=2 * pi * 4)
        ctrl = control.VectorControlSystem(vector_ctrl, speed_ctrl)
        ctrl.set_speed_ref(lambda t: (t > 0.1) * 0.1 * base.w_M)
        mdl.mechanics.set_external_load_torque(lambda t: (t > 0.6) * 0.5 * nom.tau)
        return mdl, ctrl









.. GENERATED FROM PYTHON SOURCE LINES 60-63

Simulate natively with the different estimates and plot the rotor speeds. With the
overestimated resistance (`k_R = 1.2`), the drive loses its stability under the
load.

.. GENERATED FROM PYTHON SOURCE LINES 63-80

.. code-block:: Python


    set_style()
    fig, ax = plt.subplots()
    k_Rs = [0.8, 0.9, 1.0, 1.1, 1.2]
    start = time.perf_counter()
    for i, k_R in enumerate(k_Rs):
        sim = native.Simulation(*build_system(k_R), show_progress=False)
        res = sim.simulate(t_stop=1.2)
        color = plt.get_cmap("viridis")(i / (len(k_Rs) - 1))
        ax.plot(res.mdl.t, res.mdl.mechanics.w_M / base.w_M, color=color, label=k_R)
    print(f"Run time of the {len(k_Rs)} simulations: {time.perf_counter() - start:.2f} s")
    ax.set_xlim(0, 1.2)
    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Rotor speed (p.u.)")
    ax.legend(title=r"$k_R$", loc="center left", bbox_to_anchor=(1, 0.5))
    fig.tight_layout()
    plt.show()



.. image-sg:: /examples/images/sphx_glr_plot_2kw_ipmsm_fvc_sweep_001.png
   :alt: plot 2kw ipmsm fvc sweep
   :srcset: /examples/images/sphx_glr_plot_2kw_ipmsm_fvc_sweep_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Run time of the 5 simulations: 0.67 s
    /home/runner/work/motulator-export/motulator-export/examples/native/plot_2kw_ipmsm_fvc_sweep.py:78: UserWarning: The figure layout has changed to tight
      fig.tight_layout()





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.796 seconds)


.. _sphx_glr_download_examples_plot_2kw_ipmsm_fvc_sweep.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_2kw_ipmsm_fvc_sweep.ipynb <plot_2kw_ipmsm_fvc_sweep.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_2kw_ipmsm_fvc_sweep.py <plot_2kw_ipmsm_fvc_sweep.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_2kw_ipmsm_fvc_sweep.zip <plot_2kw_ipmsm_fvc_sweep.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
