
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/plot_6kw_pmsyrm_gn_fvc.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_6kw_pmsyrm_gn_fvc.py>`
        to download the full example code.

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

.. _sphx_glr_examples_plot_6kw_pmsyrm_gn_fvc.py:


5.6-kW PM-SyRM, GradNet models, FVC
===================================

This example simulates flux-vector control (FVC) of a 5.6-kW PM synchronous reluctance
machine drive natively. The system is that of the *motulator* example
:doc:`motulator:drive_examples/flux_vector/plot_6kw_pmsyrm_gn_fvc_fem_harm`: the machine
model uses a GradNet current map with spatial harmonics, and the control system uses a
GradNet flux map, both trained on FEM data. The GradNets are evaluated in C, which
makes the native simulation about 30 times faster than *motulator*.

.. GENERATED FROM PYTHON SOURCE LINES 15-25

.. code-block:: Python

    import time
    from math import pi
    from pathlib import Path

    import motulator.drive.control.sm as control
    import motulator.drive.gradnet as gn
    from motulator.drive import model, utils

    from motulator_export import native








.. GENERATED FROM PYTHON SOURCE LINES 26-27

Compute base values based on the nominal values.

.. GENERATED FROM PYTHON SOURCE LINES 27-31

.. code-block:: Python


    nom = utils.NominalValues(U=460, I=8.8, f=60, P=5.6e3, tau=29.7)
    base = utils.BaseValues.from_nominal(nom, n_p=2)








.. GENERATED FROM PYTHON SOURCE LINES 32-33

Configure the system model with the GradNet current map with spatial harmonics.

.. GENERATED FROM PYTHON SOURCE LINES 33-48

.. code-block:: Python


    try:
        MODEL_DIR = Path(__file__).parents[1] / "trained_models"
    except NameError:  # The gallery of the documentation runs the examples in their folder
        MODEL_DIR = Path("..") / "trained_models"
    path = MODEL_DIR / "baldor_fem_curr_map_harm_softmax_d48_sub10.npz"
    magnetic_map = gn.CurrentMapWithHarmonics(gn.load_gradnet(path))
    par = model.SpatialSaturatedSynchronousMachinePars(
        n_p=2, R_s=0.63, magnetic_map_fcn=magnetic_map
    )
    machine = model.SynchronousMachine(par)
    mechanics = model.MechanicalSystem(J=0.05)
    converter = model.VoltageSourceConverter(u_dc=540)
    mdl = model.Drive(machine, mechanics, converter)








.. GENERATED FROM PYTHON SOURCE LINES 49-50

Configure the control system with the GradNet flux map.

.. GENERATED FROM PYTHON SOURCE LINES 50-63

.. code-block:: Python


    path = MODEL_DIR / "baldor_fem_flux_map_pnorm_d12_sub20.npz"
    est_flux_map = gn.FluxMap(gn.load_gradnet(path))
    est_par = control.SaturatedSynchronousMachinePars(
        n_p=2, R_s=0.63, psi_s_dq_fcn=est_flux_map
    )
    cfg = control.FluxVectorControllerCfg(
        i_s_max=2 * base.i, alpha_i=0, alpha_o=2 * pi * 8, J=0.05, sensorless=False
    )
    vector_ctrl = control.FluxVectorController(est_par, cfg)
    speed_ctrl = control.SpeedController(J=0.05, alpha_s=2 * pi * 4)
    ctrl = control.VectorControlSystem(vector_ctrl, speed_ctrl)








.. GENERATED FROM PYTHON SOURCE LINES 64-65

Set the speed reference and the external load torque.

.. GENERATED FROM PYTHON SOURCE LINES 65-69

.. code-block:: Python


    ctrl.set_speed_ref(lambda t: (t > 0.25) * 2 * base.w_M)
    mdl.mechanics.set_external_load_torque(lambda t: (t > 1.25) * 0.5 * base.tau)








.. GENERATED FROM PYTHON SOURCE LINES 70-71

Simulate natively and plot the results in per-unit values.

.. GENERATED FROM PYTHON SOURCE LINES 71-77

.. code-block:: Python


    sim = native.Simulation(mdl, ctrl)
    start = time.perf_counter()
    res = sim.simulate(t_stop=1.75)
    print(f"Run time: {time.perf_counter() - start:.2f} s")
    utils.plot(res, base)



.. image-sg:: /examples/images/sphx_glr_plot_6kw_pmsyrm_gn_fvc_001.png
   :alt: plot 6kw pmsyrm gn fvc
   :srcset: /examples/images/sphx_glr_plot_6kw_pmsyrm_gn_fvc_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    Run time: 0.54 s





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

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


.. _sphx_glr_download_examples_plot_6kw_pmsyrm_gn_fvc.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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