.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "samples\general\MagnetPocketPeakStress.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_samples_general_MagnetPocketPeakStress.py: Find peak stress on boundary of region(s) ========================================= This example shows how to sample the stresses around a magnet pocket It finds the overall region that makes up the magnet pocket (the pocket region itself and the magnet), and then samples the stresses along the perimeter of this region. This script should be run from the scripting tab after the stress calculation has been run in Motor-CAD. .. GENERATED FROM PYTHON SOURCE LINES 35-96 .. rst-class:: sphx-glr-script-out .. code-block:: none Max von Mises stress: 56.52 MPa | .. code-block:: Python import math import os # Need to import pymotorcad to access Motor-CAD import ansys.motorcad.core as pymotorcad # Connect to Motor-CAD mc = pymotorcad.MotorCAD() # Users should run this script from the scripting tab after the stress calculation # Trigger this automatically for the automated documentation build if "PYMOTORCAD_DOCS_BUILD" in os.environ: mc.set_variable("MessageDisplayState", 2) mc.load_template("e10") mc.do_mechanical_calculation() ############ # Settings # ############ # Define the region name or names that make up the region of interest (e.g. magnet and pocket) # Peak stress will be found on the boundary of this region region_names = ["L1_1Magnet1", "Rotor Pocket_1"] sample_distance = ( 0.1 # Sampling distance in mm along lines/arcs (end points will always be included) ) ############### # Main script # ############### # Get the region(s) we are interested in, combined into a single region object regions = [] for region_name in region_names: regions.append(mc.get_region(region_name)) if len(regions) > 1: region = mc.unite_regions(regions[0], regions[1:]) else: region = regions[0] # Get the points that make up the region points = [] for entity in region.entities: length = entity.length samples = math.ceil(length / sample_distance) for sample in range(samples): # Sample points along line. Don't get end point, as this will be start of the next entity points.append(entity.get_coordinate_from_distance(entity.start, fraction=sample / samples)) # Get stress result at these points stresses = [] stress_unit = None for point in points: stress, stress_unit = mc.get_point_value("SVM", point.x, point.y) stresses.append(stress) # Find the max stress, and print the output max_stress = max(stresses) print(f"Max von Mises stress: {max_stress} {stress_unit}") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 43.545 seconds) .. _sphx_glr_download_samples_general_MagnetPocketPeakStress.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: MagnetPocketPeakStress.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: MagnetPocketPeakStress.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: MagnetPocketPeakStress.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_