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

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

.. _sphx_glr_examples_03-Maxwell_Maxwell_Control_Program.py:


Enabling Control Program in a Maxwell 2D Project
------------------------------------------------
This example shows how you can use PyAEDT to enable control program in a Maxwell 2D project.
It shows how to create the geometry, load material properties from an Excel file and
set up the mesh settings. Moreover, it focuses on post-processing operations, in particular how to
plot field line traces, relevant for an electrostatic analysis.

.. GENERATED FROM PYTHON SOURCE LINES 11-14

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

.. GENERATED FROM PYTHON SOURCE LINES 14-19

.. code-block:: Python


    from pyaedt import downloads
    from pyaedt import generate_unique_folder_name
    from pyaedt import Maxwell2d








.. GENERATED FROM PYTHON SOURCE LINES 20-23

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

.. GENERATED FROM PYTHON SOURCE LINES 23-26

.. code-block:: Python


    aedt_version = "2024.1"








.. GENERATED FROM PYTHON SOURCE LINES 27-31

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

.. GENERATED FROM PYTHON SOURCE LINES 31-34

.. code-block:: Python


    non_graphical = False








.. GENERATED FROM PYTHON SOURCE LINES 35-38

Download .aedt file example
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set local temporary folder to export the .aedt file to.

.. GENERATED FROM PYTHON SOURCE LINES 38-42

.. code-block:: Python

    temp_folder = generate_unique_folder_name()
    aedt_file = downloads.download_file("maxwell_ctrl_prg", "ControlProgramDemo.aedt", temp_folder)
    ctrl_prg_file = downloads.download_file("maxwell_ctrl_prg", "timestep_only.py", temp_folder)








.. GENERATED FROM PYTHON SOURCE LINES 43-46

Launch Maxwell 2D
~~~~~~~~~~~~~~~~~
Launch Maxwell 2D.

.. GENERATED FROM PYTHON SOURCE LINES 46-52

.. code-block:: Python


    m2d = Maxwell2d(projectname=aedt_file,
                    specified_version=aedt_version,
                    new_desktop_session=True,
                    non_graphical=non_graphical)





.. 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 4828 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 53-56

Set active design
~~~~~~~~~~~~~~~~~
Set active design.

.. GENERATED FROM PYTHON SOURCE LINES 56-59

.. code-block:: Python


    m2d.set_active_design("1 time step control")





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

 .. code-block:: none


    True



.. GENERATED FROM PYTHON SOURCE LINES 60-63

Get design setup
~~~~~~~~~~~~~~~~
Get design setup to enable the control program to.

.. GENERATED FROM PYTHON SOURCE LINES 63-66

.. code-block:: Python


    setup = m2d.setups[0]








.. GENERATED FROM PYTHON SOURCE LINES 67-70

Enable control program
~~~~~~~~~~~~~~~~~~~~~~
Enable control program by giving the path to the file.

.. GENERATED FROM PYTHON SOURCE LINES 70-73

.. code-block:: Python


    setup.enable_control_program(control_program_path=ctrl_prg_file)





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

 .. code-block:: none


    True



.. GENERATED FROM PYTHON SOURCE LINES 74-77

Analyze setup
~~~~~~~~~~~~~
Analyze setup.

.. GENERATED FROM PYTHON SOURCE LINES 77-81

.. code-block:: Python


    setup.analyze()









.. GENERATED FROM PYTHON SOURCE LINES 82-85

Plot results
~~~~~~~~~~~~
Plot Solved Results.

.. GENERATED FROM PYTHON SOURCE LINES 85-89

.. code-block:: Python


    sols = m2d.post.get_solution_data("FluxLinkage(Winding1)", variations={"Time":["All"]}, primary_sweep_variable="Time")
    sols.plot()




.. image-sg:: /examples/03-Maxwell/images/sphx_glr_Maxwell_Control_Program_001.png
   :alt: Simulation Results Plot
   :srcset: /examples/03-Maxwell/images/sphx_glr_Maxwell_Control_Program_001.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 90-93

Save project and close AEDT
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Save the project and close AEDT.

.. GENERATED FROM PYTHON SOURCE LINES 93-96

.. code-block:: Python


    m2d.save_project()
    m2d.release_desktop()




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

 .. code-block:: none


    True




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

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


.. _sphx_glr_download_examples_03-Maxwell_Maxwell_Control_Program.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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