.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\basics\emag_basics.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_basics_emag_basics.py: .. _ref_emag_basics: Motor-CAD E-magnetic example script =================================== This example provides a Motor-CAD E-magnetic script. This script creates a partial custom winding pattern to change parameter values, run the analysis, and plot results. To create a full winding pattern, parameters must be specified for all coils. .. GENERATED FROM PYTHON SOURCE LINES 36-44 Set up example -------------- Setting up this example consists of performing imports, specifying the working directory, launching Motor-CAD, and disabling all popup messages from Motor-CAD. Perform required imports ~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 44-55 .. code-block:: Python import os import matplotlib.pyplot as plt import ansys.motorcad.core as pymotorcad if "QT_API" in os.environ: os.environ["QT_API"] = "pyqt" .. GENERATED FROM PYTHON SOURCE LINES 56-58 Specify working directory ~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: Python working_folder = os.getcwd() if os.path.isdir(working_folder) is False: print("Working folder does not exist. Choose a folder that exists and try again.") print(working_folder) exit() .. GENERATED FROM PYTHON SOURCE LINES 67-69 Launch Motor-CAD ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 69-73 .. code-block:: Python print("Starting initialization.") mcad = pymotorcad.MotorCAD() .. rst-class:: sphx-glr-script-out .. code-block:: none Starting initialization. .. GENERATED FROM PYTHON SOURCE LINES 74-76 Disable popup messages ~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 76-81 .. code-block:: Python mcad.set_variable("MessageDisplayState", 2) print("Initialization completed.") print("Running simulation.") .. rst-class:: sphx-glr-script-out .. code-block:: none Initialization completed. Running simulation. .. GENERATED FROM PYTHON SOURCE LINES 82-89 Create analysis --------------- Creating the analysis consists of showing the magnetic context, displaying the **Scripting** tab, setting the geometry and parameters, and saving the file. Show the magnetic context. .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python mcad.show_magnetic_context() .. GENERATED FROM PYTHON SOURCE LINES 92-93 Display the **Scripting** tab. .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python mcad.display_screen("Scripting") .. GENERATED FROM PYTHON SOURCE LINES 96-97 Set the geometry. .. GENERATED FROM PYTHON SOURCE LINES 97-101 .. code-block:: Python mcad.set_variable("Slot_Number", 24) mcad.set_variable("Tooth_Width", 6) mcad.set_variable("Magnet_Thickness", 4.5) .. GENERATED FROM PYTHON SOURCE LINES 102-124 Set parameters for creating the custom winding pattern. The following code creates only a partial winding pattern. Set the winding type to custom: :code:`mcad.set_variable('MagWindingType', 1)` Set the path type to upper and lower: :code:`mcad.set_variable('MagPathType', 1)` Set the number of phases: :code:`mcad.set_variable('MagPhases', 3)` Set the number of parallel paths: :code:`mcad.set_variable('ParallelPaths', 1)` Set the number of winding layers: :code:`mcad.set_variable('WindingLayers', 2)` Define a coil's parameters: :code:`set_winding_coil(phase, path, coil, go_slot, go_position, return_slot, return_position, turns)` .. GENERATED FROM PYTHON SOURCE LINES 126-127 Set the stator/rotor lamination materials. .. GENERATED FROM PYTHON SOURCE LINES 127-130 .. code-block:: Python mcad.set_component_material("Stator Lam (Back Iron)", "M250-35A") mcad.set_component_material("Rotor Lam (Back Iron)", "M250-35A") .. GENERATED FROM PYTHON SOURCE LINES 131-132 Set the torque calculation options. .. GENERATED FROM PYTHON SOURCE LINES 132-137 .. code-block:: Python points_per_cycle = 30 number_cycles = 1 mcad.set_variable("TorquePointsPerCycle", points_per_cycle) mcad.set_variable("TorqueNumberCycles", number_cycles) .. GENERATED FROM PYTHON SOURCE LINES 138-139 Disable all performance tests except the ones for transient torque. .. GENERATED FROM PYTHON SOURCE LINES 139-148 .. code-block:: Python mcad.set_variable("BackEMFCalculation", False) mcad.set_variable("CoggingTorqueCalculation", False) mcad.set_variable("ElectromagneticForcesCalc_OC", False) mcad.set_variable("TorqueSpeedCalculation", False) mcad.set_variable("DemagnetizationCalc", False) mcad.set_variable("ElectromagneticForcesCalc_Load", False) mcad.set_variable("InductanceCalc", False) mcad.set_variable("BPMShortCircuitCalc", False) .. GENERATED FROM PYTHON SOURCE LINES 149-150 Enable transient torque. .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: Python mcad.set_variable("TorqueCalculation", True) .. GENERATED FROM PYTHON SOURCE LINES 153-154 Set the operating point. .. GENERATED FROM PYTHON SOURCE LINES 154-160 .. code-block:: Python mcad.set_variable("Shaft_Speed_[RPM]", 1000) mcad.set_variable("CurrentDefinition", 0) mcad.set_variable("PeakCurrent", 3) mcad.set_variable("DCBusVoltage", 350) mcad.set_variable("PhaseAdvance", 45) .. GENERATED FROM PYTHON SOURCE LINES 161-162 Save the file. .. GENERATED FROM PYTHON SOURCE LINES 162-165 .. code-block:: Python filename = os.path.join(working_folder, "../ActiveX_Scripting_EMagnetic.mot") mcad.save_to_file(filename) .. GENERATED FROM PYTHON SOURCE LINES 166-169 Run simulation -------------- Run the simulation. .. GENERATED FROM PYTHON SOURCE LINES 169-171 .. code-block:: Python mcad.do_magnetic_calculation() .. GENERATED FROM PYTHON SOURCE LINES 172-175 Export results to CSV file -------------------------- Export results to a CSV file. .. GENERATED FROM PYTHON SOURCE LINES 175-182 .. code-block:: Python exportFile = os.path.join(working_folder, "../Export_EMag_Results.csv") try: mcad.export_results("EMagnetic", exportFile) print("Results successfully exported.") except pymotorcad.MotorCADError: print("Results failed to export.") .. rst-class:: sphx-glr-script-out .. code-block:: none Results successfully exported. .. GENERATED FROM PYTHON SOURCE LINES 183-186 Get and analyze results ----------------------- Get torque and voltage data. .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. code-block:: Python shaft_torque = mcad.get_variable("ShaftTorque") line_voltage = mcad.get_variable("PeakLineLineVoltage") .. GENERATED FROM PYTHON SOURCE LINES 190-191 Graph the torque data. .. GENERATED FROM PYTHON SOURCE LINES 191-200 .. code-block:: Python num_torque_points = points_per_cycle * number_cycles rotor_position = [] torque_vw = [] for n in range(num_torque_points): (x, y) = mcad.get_magnetic_graph_point("TorqueVW", n) rotor_position.append(x) torque_vw.append(y) .. GENERATED FROM PYTHON SOURCE LINES 201-202 Graph the airgap flux density data. .. GENERATED FROM PYTHON SOURCE LINES 202-207 .. code-block:: Python loop = 0 success = 0 mech_angle = [] airgap_flux_density = [] .. GENERATED FROM PYTHON SOURCE LINES 208-209 Keep looking until you cannot find the point. .. GENERATED FROM PYTHON SOURCE LINES 209-218 .. code-block:: Python while success == 0: try: (x, y) = mcad.get_fea_graph_point("B Gap (on load)", 1, loop, 0) mech_angle.append(x) airgap_flux_density.append(y) loop = loop + 1 except pymotorcad.MotorCADError: success = 1 .. GENERATED FROM PYTHON SOURCE LINES 219-220 Graph the harmonic data. .. GENERATED FROM PYTHON SOURCE LINES 220-238 .. code-block:: Python mcad.initialise_tab_names() mcad.display_screen("Graphs;Harmonics;Torque") num_harmonic_points = (points_per_cycle * number_cycles) + 1 data_point = [] torque = [] for n in range(num_harmonic_points): try: (x, y) = mcad.get_magnetic_graph_point("HarmonicDataCycle", n) data_point.append(x) torque.append(y) except pymotorcad.MotorCADError: print("Results failed to export.") print("Simulation completed.") .. rst-class:: sphx-glr-script-out .. code-block:: none Simulation completed. .. GENERATED FROM PYTHON SOURCE LINES 239-242 Plot results ------------ Plot results from the simulation. .. GENERATED FROM PYTHON SOURCE LINES 242-256 .. code-block:: Python plt.subplot(211) plt.plot(mech_angle, airgap_flux_density) plt.xlabel("Mech Angle") plt.ylabel("Airgap Flux Density") plt.subplot(212) plt.plot(rotor_position, torque_vw) plt.xlabel("Rotor Position") plt.ylabel("TorqueVW") plt.figure(2) plt.plot(data_point, torque) plt.xlabel("DataPoint") plt.ylabel("Torque (Nm)") plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/basics/images/sphx_glr_emag_basics_001.png :alt: emag basics :srcset: /examples/basics/images/sphx_glr_emag_basics_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/basics/images/sphx_glr_emag_basics_002.png :alt: emag basics :srcset: /examples/basics/images/sphx_glr_emag_basics_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 257-260 Exit Motor-CAD -------------- Exit Motor-CAD. .. GENERATED FROM PYTHON SOURCE LINES 260-262 .. code-block:: Python mcad.quit() .. GENERATED FROM PYTHON SOURCE LINES 263-266 If you want to continue working with this instance of Motor-CAD, rather than using the preceding command, use this command: :code:`mcad.set_variable('MessageDisplayState', 0)` .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 35.595 seconds) .. _sphx_glr_download_examples_basics_emag_basics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: emag_basics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: emag_basics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: emag_basics.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_