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

.. only:: html

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

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

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

.. _sphx_glr_drive_examples_gradnet_plot_28kw_pmsyrm_curr_map_meas.py:


28-kW PM-SyRM, train current map, measured data
===============================================

This example trains a GradNet current map for a 10-pole 28-kW PM synchronous reluctance
machine (Brusa HSM1.10.18.04) from a measured dataset.

.. GENERATED FROM PYTHON SOURCE LINES 9-17

.. code-block:: Python


    from pathlib import Path

    import numpy as np

    import motulator.drive.gradnet as gn
    from motulator.drive import utils








.. GENERATED FROM PYTHON SOURCE LINES 18-19

Set nominal and base values, needed for figures only.

.. GENERATED FROM PYTHON SOURCE LINES 19-23

.. code-block:: Python


    nom = utils.NominalValues(U=283, I=78, f=408, P=25e3, tau=49)
    base = utils.BaseValues.from_nominal(nom, n_p=5)








.. GENERATED FROM PYTHON SOURCE LINES 24-25

Set up the paths and parameters.

.. GENERATED FROM PYTHON SOURCE LINES 25-33

.. code-block:: Python


    p = Path(__file__).resolve().parent if "__file__" in globals() else Path.cwd()

    dataset_path = p / "datasets/brusa_meas.npz"
    trained_path = p / "trained_models/brusa_meas_curr_map_squareplus_d12_sub100.pth"
    subsample = 100
    activation = gn.Squareplus








.. GENERATED FROM PYTHON SOURCE LINES 34-35

Train the model.

.. GENERATED FROM PYTHON SOURCE LINES 35-47

.. code-block:: Python


    if not trained_path.exists():
        gn.train_gradnet(
            dataset_path=dataset_path,
            base=base,
            save_model_path=trained_path,
            epochs=20000,
            subsample=subsample,
            embed_dim=12,
            activation=activation,
        )








.. GENERATED FROM PYTHON SOURCE LINES 48-49

Create the GradNet model and its callable.

.. GENERATED FROM PYTHON SOURCE LINES 49-53

.. code-block:: Python


    model = gn.load_gradnet(trained_path, activation=activation)
    current_map_fcn = gn.CurrentMap(model)








.. GENERATED FROM PYTHON SOURCE LINES 54-55

Load the dataset for comparison and split it into training and validation sets.

.. GENERATED FROM PYTHON SOURCE LINES 55-60

.. code-block:: Python


    train_data, val_data = gn.get_training_data(
        str(dataset_path), subsample=subsample, base=base
    )








.. GENERATED FROM PYTHON SOURCE LINES 61-62

Plot the current map.

.. GENERATED FROM PYTHON SOURCE LINES 62-98

.. code-block:: Python


    # Sample the map on a grid for plotting
    current_map = gn.sample_map_on_grid(
        current_map_fcn,
        map_type="current_map",
        d_range=np.linspace(-0.15 * base.psi, 1.05 * base.psi, 50),
        q_range=np.linspace(-1.05 * base.psi, 1.05 * base.psi, 50),
    )

    # Constant current contours corresponding to the measured dataset, for visualization
    i_d_levels = np.arange(-145, 146, 5) / base.i
    i_q_levels = np.arange(-145, 145, 5) / base.i
    current_loci_levels = (i_d_levels, i_q_levels)

    gn.plot_maps(
        current_map,
        "d",
        base,
        lims={"x": (-0.2, 1.1), "y": (-1.2, 1.2), "z": (-2, 4)},
        ticks={"x": [0, 0.5, 1], "y": [-1.0, 0, 1.0], "z": [-2, 0, 2, 4]},
        raw_data=[val_data, train_data],
        current_loci=True,
        current_loci_levels=current_loci_levels,
    )

    gn.plot_maps(
        current_map,
        "q",
        base,
        lims={"x": (-0.2, 1.1), "y": (-1.2, 1.2), "z": (-4, 4)},
        ticks={"x": [0, 0.5, 1], "y": [-1.2, 0, 1.2], "z": [-4, -2, 0, 2, 4]},
        raw_data=[val_data, train_data],
        current_loci=True,
        current_loci_levels=current_loci_levels,
    )




.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /drive_examples/gradnet/images/sphx_glr_plot_28kw_pmsyrm_curr_map_meas_001.png
         :alt: plot 28kw pmsyrm curr map meas
         :srcset: /drive_examples/gradnet/images/sphx_glr_plot_28kw_pmsyrm_curr_map_meas_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /drive_examples/gradnet/images/sphx_glr_plot_28kw_pmsyrm_curr_map_meas_002.png
         :alt: plot 28kw pmsyrm curr map meas
         :srcset: /drive_examples/gradnet/images/sphx_glr_plot_28kw_pmsyrm_curr_map_meas_002.png
         :class: sphx-glr-multi-img





.. GENERATED FROM PYTHON SOURCE LINES 99-100

Print statistical error metrics.

.. GENERATED FROM PYTHON SOURCE LINES 100-102

.. code-block:: Python


    gn.print_current_map_errors_meas(current_map_fcn, val_data, base=base)




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

 .. code-block:: none

    Current-map error metrics: rmse=0.032 p.u., max=0.133 p.u., std=0.019 p.u.





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

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


.. _sphx_glr_download_drive_examples_gradnet_plot_28kw_pmsyrm_curr_map_meas.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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