.. 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 14-22 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 22-33 .. 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 34-36 Specify working directory ~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. 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 45-47 Launch Motor-CAD ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. 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 52-54 Disable popup messages ~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. 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 60-67 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 67-69 .. code-block:: Python mcad.show_magnetic_context() .. GENERATED FROM PYTHON SOURCE LINES 70-71 Display the **Scripting** tab. .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python mcad.display_screen("Scripting") .. GENERATED FROM PYTHON SOURCE LINES 74-75 Set the geometry. .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. 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 80-102 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 104-105 Set the stator/rotor lamination materials. .. GENERATED FROM PYTHON SOURCE LINES 105-108 .. 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 109-110 Set the torque calculation options. .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. 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 116-117 Disable all performance tests except the ones for transient torque. .. GENERATED FROM PYTHON SOURCE LINES 117-126 .. 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 127-128 Enable transient torque. .. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: Python mcad.set_variable("TorqueCalculation", True) .. GENERATED FROM PYTHON SOURCE LINES 131-132 Set the operating point. .. GENERATED FROM PYTHON SOURCE LINES 132-138 .. 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 139-140 Save the file. .. GENERATED FROM PYTHON SOURCE LINES 140-143 .. code-block:: Python filename = os.path.join(working_folder, "../ActiveX_Scripting_EMagnetic.mot") mcad.save_to_file(filename) .. GENERATED FROM PYTHON SOURCE LINES 144-147 Run simulation -------------- Run the simulation. .. GENERATED FROM PYTHON SOURCE LINES 147-149 .. code-block:: Python mcad.do_magnetic_calculation() .. GENERATED FROM PYTHON SOURCE LINES 150-153 Export results to CSV file -------------------------- Export results to a CSV file. .. GENERATED FROM PYTHON SOURCE LINES 153-160 .. 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 161-164 Get and analyze results ----------------------- Get torque and voltage data. .. GENERATED FROM PYTHON SOURCE LINES 164-167 .. code-block:: Python shaft_torque = mcad.get_variable("ShaftTorque") line_voltage = mcad.get_variable("PeakLineLineVoltage") .. GENERATED FROM PYTHON SOURCE LINES 168-169 Graph the torque data. .. GENERATED FROM PYTHON SOURCE LINES 169-178 .. 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 179-180 Graph the airgap flux density data. .. GENERATED FROM PYTHON SOURCE LINES 180-185 .. code-block:: Python loop = 0 success = 0 mech_angle = [] airgap_flux_density = [] .. GENERATED FROM PYTHON SOURCE LINES 186-187 Keep looking until you cannot find the point. .. GENERATED FROM PYTHON SOURCE LINES 187-196 .. 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 197-198 Graph the harmonic data. .. GENERATED FROM PYTHON SOURCE LINES 198-216 .. 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 217-220 Plot results ------------ Plot results from the simulation. .. GENERATED FROM PYTHON SOURCE LINES 220-234 .. 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 235-238 Exit Motor-CAD -------------- Exit Motor-CAD. .. GENERATED FROM PYTHON SOURCE LINES 238-240 .. code-block:: Python mcad.quit() .. GENERATED FROM PYTHON SOURCE LINES 241-244 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:** (0 minutes 51.235 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_