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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_03-Maxwell_Maxwell2D_Transient.py>`
        to download the full example code.

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

.. _sphx_glr_examples_03-Maxwell_Maxwell2D_Transient.py:


Maxwell 2D: transient winding analysis
--------------------------------------
This example shows how you can use PyAEDT to create a project in Maxwell 2D
and run a transient simulation. It runs only on Windows using CPython.

The following libraries are required for the advanced postprocessing features
used in this example:

- `Matplotlib <https://pypi.org/project/matplotlib/>`_
- `Numpty <https://pypi.org/project/numpy/>`_
- `PyVista <https://pypi.org/project/pyvista/>`_

Install these libraries with:

.. code::

   pip install numpy pyvista matplotlib

.. GENERATED FROM PYTHON SOURCE LINES 22-25

Perform required imports
~~~~~~~~~~~~~~~~~~~~~~~~
Perform required imports.

.. GENERATED FROM PYTHON SOURCE LINES 25-30

.. code-block:: Python


    import os
    import pyaedt
    import tempfile








.. GENERATED FROM PYTHON SOURCE LINES 31-34

Set AEDT version
~~~~~~~~~~~~~~~~
Set AEDT version.

.. GENERATED FROM PYTHON SOURCE LINES 34-37

.. code-block:: Python


    aedt_version = "2024.1"








.. GENERATED FROM PYTHON SOURCE LINES 38-41

Create temporary directory
~~~~~~~~~~~~~~~~~~~~~~~~~~
Create temporary directory.

.. GENERATED FROM PYTHON SOURCE LINES 41-44

.. code-block:: Python


    temp_dir = tempfile.TemporaryDirectory(suffix=".ansys")








.. GENERATED FROM PYTHON SOURCE LINES 45-49

Set non-graphical mode
~~~~~~~~~~~~~~~~~~~~~~
Set non-graphical mode. 
You can set ``non_graphical`` either to ``True`` or ``False``.

.. GENERATED FROM PYTHON SOURCE LINES 49-52

.. code-block:: Python


    non_graphical = False








.. GENERATED FROM PYTHON SOURCE LINES 53-56

Insert Maxwell 2D design and save project
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Insert a Maxwell 2D design and save the project.

.. GENERATED FROM PYTHON SOURCE LINES 56-60

.. code-block:: Python


    maxwell_2d = pyaedt.Maxwell2d(solution_type="TransientXY", specified_version=aedt_version, non_graphical=non_graphical,
                                  new_desktop_session=True, projectname=pyaedt.generate_unique_project_name())





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

 .. code-block:: none

    C:\actions-runner\_work\_tool\Python\3.10.9\x64\lib\subprocess.py:1072: ResourceWarning: subprocess 5924 is still running
      _warn("subprocess %s is still running" % self.pid,
    C:\actions-runner\_work\pyaedt\pyaedt\.venv\lib\site-packages\pyaedt\generic\settings.py:383: ResourceWarning: unclosed file <_io.TextIOWrapper name='D:\\Temp\\pyaedt_ansys.log' mode='a' encoding='cp1252'>
      self._logger = val




.. GENERATED FROM PYTHON SOURCE LINES 61-64

Create rectangle and duplicate it
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create a rectangle and duplicate it.

.. GENERATED FROM PYTHON SOURCE LINES 64-69

.. code-block:: Python


    rect1 = maxwell_2d.modeler.create_rectangle([0, 0, 0], [10, 20], name="winding", material="copper")
    added = rect1.duplicate_along_line([14, 0, 0])
    rect2 = maxwell_2d.modeler[added[0]]








.. GENERATED FROM PYTHON SOURCE LINES 70-73

Create air region
~~~~~~~~~~~~~~~~~
Create an air region.

.. GENERATED FROM PYTHON SOURCE LINES 73-76

.. code-block:: Python


    region = maxwell_2d.modeler.create_region([100, 100, 100, 100])








.. GENERATED FROM PYTHON SOURCE LINES 77-80

Assign windings and balloon
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Assigns windings to the sheets and a balloon to the air region.

.. GENERATED FROM PYTHON SOURCE LINES 80-84

.. code-block:: Python


    maxwell_2d.assign_winding([rect1.name, rect2.name], name="PHA")
    maxwell_2d.assign_balloon(region.edges)





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

 .. code-block:: none


    <pyaedt.modules.Boundary.BoundaryObject object at 0x0000022602D10670>



.. GENERATED FROM PYTHON SOURCE LINES 85-88

Plot model
~~~~~~~~~~
Plot the model.

.. GENERATED FROM PYTHON SOURCE LINES 88-91

.. code-block:: Python


    maxwell_2d.plot(show=False, export_path=os.path.join(temp_dir.name, "Image.jpg"), plot_air_objects=True)




.. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_Transient_001.png
   :alt: Maxwell2D Transient
   :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_Transient_001.png
   :class: sphx-glr-single-img


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

 .. code-block:: none


    <pyaedt.generic.plot.ModelPlotter object at 0x0000022602D11180>



.. GENERATED FROM PYTHON SOURCE LINES 92-95

Create setup
~~~~~~~~~~~~
Create the transient setup.

.. GENERATED FROM PYTHON SOURCE LINES 95-104

.. code-block:: Python


    setup = maxwell_2d.create_setup()
    setup.props["StopTime"] = "0.02s"
    setup.props["TimeStep"] = "0.0002s"
    setup.props["SaveFieldsType"] = "Every N Steps"
    setup.props["N Steps"] = "1"
    setup.props["Steps From"] = "0s"
    setup.props["Steps To"] = "0.002s"








.. GENERATED FROM PYTHON SOURCE LINES 105-108

Create rectangular plot
~~~~~~~~~~~~~~~~~~~~~~~
Create a rectangular plot.

.. GENERATED FROM PYTHON SOURCE LINES 108-112

.. code-block:: Python


    maxwell_2d.post.create_report("InputCurrent(PHA)", domain="Time", primary_sweep_variable="Time",
                                  plot_name="Winding Plot 1")





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

 .. code-block:: none


    <pyaedt.modules.report_templates.Standard object at 0x0000022601023A00>



.. GENERATED FROM PYTHON SOURCE LINES 113-116

Solve model
~~~~~~~~~~~
Solve the model.

.. GENERATED FROM PYTHON SOURCE LINES 116-119

.. code-block:: Python


    maxwell_2d.analyze(use_auto_settings=False)





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

 .. code-block:: none


    True



.. GENERATED FROM PYTHON SOURCE LINES 120-123

Create output and plot using PyVista
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create the output and plot it using PyVista.

.. GENERATED FROM PYTHON SOURCE LINES 123-143

.. code-block:: Python


    cutlist = ["Global:XY"]
    face_lists = rect1.faces
    face_lists += rect2.faces
    timesteps = [str(i * 2e-4) + "s" for i in range(11)]
    id_list = [f.id for f in face_lists]

    gif = maxwell_2d.post.plot_animated_field(quantity="Mag_B", assignment=id_list, plot_type="Surface",
                                              intrinsics={"Time": "0s"}, variation_variable="Time",
                                              variations=timesteps, show=False, export_gif=False)
    gif.isometric_view = False
    gif.camera_position = [15, 15, 80]
    gif.focal_point = [15, 15, 0]
    gif.roll_angle = 0
    gif.elevation_angle = 0
    gif.azimuth_angle = 0
    # Set off_screen to False to visualize the animation.
    # gif.off_screen = False
    gif.animate()




.. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_Transient_002.png
   :alt: Maxwell2D Transient
   :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_Transient_002.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    C:\actions-runner\_work\pyaedt\pyaedt\.venv\lib\site-packages\pyvista\plotting\plotter.py:4644: PyVistaDeprecationWarning: This method is deprecated and will be removed in a future version of PyVista. Directly modify the scalars of a mesh in-place instead.
      warnings.warn(

    True



.. GENERATED FROM PYTHON SOURCE LINES 144-147

Generate plot outside AEDT
~~~~~~~~~~~~~~~~~~~~~~~~~~
Generate the same plot outside AEDT.

.. GENERATED FROM PYTHON SOURCE LINES 147-151

.. code-block:: Python


    solutions = maxwell_2d.post.get_solution_data("InputCurrent(PHA)", primary_sweep_variable="Time")
    solutions.plot()




.. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_Transient_003.png
   :alt: Simulation Results Plot
   :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell2D_Transient_003.png
   :class: sphx-glr-single-img


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

 .. code-block:: none

    No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

    <Figure size 2000x1000 with 1 Axes>



.. GENERATED FROM PYTHON SOURCE LINES 152-155

Close AEDT
~~~~~~~~~~
Close AEDT.

.. GENERATED FROM PYTHON SOURCE LINES 155-158

.. code-block:: Python


    maxwell_2d.release_desktop()
    temp_dir.cleanup()








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

   **Total running time of the script:** (1 minutes 21.365 seconds)


.. _sphx_glr_download_examples_03-Maxwell_Maxwell2D_Transient.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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