Skip to content

Commit

Permalink
Merge https://github.com/ansys/pymotorcad into tutorial/turbocharger_…
Browse files Browse the repository at this point in the history
…mesh
  • Loading branch information
ravi-ansys committed Jan 9, 2025
2 parents fdd8644 + 20363d3 commit 3e0c698
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 171 deletions.
22 changes: 5 additions & 17 deletions examples/adaptive_library/BPMTriangularRotorNotches.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,11 @@
# If the Adaptive Parameters have already been set in the current Motor-CAD file, their current
# values will be used. Otherwise, the Adaptive Parameters will be defined and set to default values.
#
# The function ``set_default_parameter`` is defined to check if a parameter exists, and if not,
# create it with a default value.


def set_default_parameter(parameter_name, default_value):
try:
mc.get_adaptive_parameter_value(parameter_name)
except pymotorcad.MotorCADError:
mc.set_adaptive_parameter_value(parameter_name, default_value)


# %%
# Use the ``set_default_parameter`` to set the required parameters if undefined
set_default_parameter("Notch Angle", -4)
set_default_parameter("Notch Sweep", 5)
set_default_parameter("Notch Depth", 1)
set_default_parameter("Notches per Pole", 2)
# Use the ``set_adaptive_parameter_default`` method to set the required parameters if undefined.
mc.set_adaptive_parameter_default("Notch Angle", -4)
mc.set_adaptive_parameter_default("Notch Sweep", 5)
mc.set_adaptive_parameter_default("Notch Depth", 1)
mc.set_adaptive_parameter_default("Notches per Pole", 2)


# %%
Expand Down
20 changes: 6 additions & 14 deletions examples/adaptive_library/BezierCurveRotorPockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,13 @@
# %%
# Set adaptive parameter if required
# ----------------------------------
# The ``set_default_parameter`` function is defined to check if a parameter exists. If not,
# The ``set_adaptive_parameter_default`` function checks if a parameter exists. If not,
# it creates the parameter with a default value.
def set_default_parameter(parameter_name, default_value):
try:
mc.get_adaptive_parameter_value(parameter_name)
except pymotorcad.MotorCADError:
mc.set_adaptive_parameter_value(parameter_name, default_value)


# %%
# Use the ``set_default_parameter()`` function to set the required ``L1 Bezier Curve Projection``,
# ``L1 Upper Convex`` and ``L1 Lower Concave`` parameters if undefined.
set_default_parameter("L1 Bezier Curve Projection", 6)
set_default_parameter("L1 Upper Convex", 0.5)
set_default_parameter("L1 Lower Concave", -0.3)
# Set the required ``L1 Bezier Curve Projection``, ``L1 Upper Convex`` and ``L1 Lower Concave``
# parameters if undefined.
mc.set_adaptive_parameter_default("L1 Bezier Curve Projection", 6)
mc.set_adaptive_parameter_default("L1 Upper Convex", 0.5)
mc.set_adaptive_parameter_default("L1 Lower Concave", -0.3)

# %%
# The adaptive parameters are used to define the curved rotor pocket geometry with a Bezier
Expand Down
18 changes: 3 additions & 15 deletions examples/adaptive_library/OblongStatorDuct.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@
# %%
# Define necessary functions
# --------------------------
# Set adaptive parameter if required
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The ``set_default_parameter`` function is defined to check if a parameter exists. If not,
# it creates the parameter with a default value.
def set_default_parameter(parameter_name, default_value):
try:
mc.get_adaptive_parameter_value(parameter_name)
except pymotorcad.MotorCADError:
mc.set_adaptive_parameter_value(parameter_name, default_value)


# %%
# Check line distance from origin
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The rectangle consists of two lines of length equal to the rectangle width.
Expand Down Expand Up @@ -192,9 +180,9 @@ def get_arc_radius_halfduct(entity_start, entity_end, height, Line_origin, Symm_
# -----------------------------------
# From Motor-CAD, get the adaptive parameters and their values.
#
# Use the ``set_default_parameter()`` method to set the required ``Duct Arc Height`` parameter
# if undefined.
set_default_parameter("Duct Arc Height", 0.7)
# Use the ``set_adaptive_parameter_default()`` method to set the required ``Duct Arc Height``
# parameter if undefined.
mc.set_adaptive_parameter_default("Duct Arc Height", 0.7)


# %%
Expand Down
18 changes: 2 additions & 16 deletions examples/adaptive_library/RoundParallelSlotBttm.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,14 @@
mc.reset_adaptive_geometry()


# %%
# Define necessary functions
# --------------------------
# Set adaptive parameter if required
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The ``set_default_parameter`` function is defined to check if a parameter exists. If not,
# it creates the parameter with a default value.
def set_default_parameter(parameter_name, default_value):
try:
mc.get_adaptive_parameter_value(parameter_name)
except pymotorcad.MotorCADError:
mc.set_adaptive_parameter_value(parameter_name, default_value)


# %%
# Get required parameters and objects
# -----------------------------------
# From Motor-CAD, get the adaptive parameters and their values.
#
# Use the ``set_default_parameter`` method to set the required ``Slot Bttm Corner Radius``
# Use the ``set_adaptive_parameter_default`` method to set the required ``Slot Bttm Corner Radius``
# parameter if undefined.
set_default_parameter("Slot Bttm Corner Radius", 0.5)
mc.set_adaptive_parameter_default("Slot Bttm Corner Radius", 0.5)

# %%
# Get the slot bottom corner radius adaptive parameter value.
Expand Down
76 changes: 31 additions & 45 deletions examples/adaptive_library/TrapezoidalDuct.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import tempfile

import ansys.motorcad.core as pymotorcad
from ansys.motorcad.core.geometry import Coordinate, rt_to_xy, xy_to_rt
from ansys.motorcad.core.geometry import xy_to_rt

# %%
# Connect to Motor-CAD
Expand All @@ -66,7 +66,7 @@
mc.set_variable("MessageDisplayState", 2)
mc.set_visible(True)
mc.load_template("e10")
mc.set_variable("RotorDuctType", 4) # selected rectangular ducts
mc.set_variable("RotorDuctType", 4) # select rectangular ducts
mc.set_array_variable("RotorCircularDuctLayer_ChannelWidth", 0, 4) # set duct width

# Open relevant file
Expand All @@ -86,18 +86,6 @@
# %%
# Define necessary functions
# --------------------------
# Set adaptive parameter if required
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The ``set_default_parameter`` function is defined to check if a parameter exists. If not,
# it creates the parameter with a default value.
def set_default_parameter(parameter_name, default_value):
try:
mc.get_adaptive_parameter_value(parameter_name)
except pymotorcad.MotorCADError:
mc.set_adaptive_parameter_value(parameter_name, default_value)


# %%
# Check line distance from origin
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The rectangle consists of two lines of length equal to the rectangle width.
Expand Down Expand Up @@ -129,9 +117,9 @@ def check_line_origin_distance(i, duct_region):
# -----------------------------------
# From Motor-CAD, get the adaptive parameters and their values.
#
# Use the ``set_default_parameter()`` method to set the required ``Trapezoid_base_ratio`` parameter
# if undefined.
set_default_parameter("Trapezoid_base_ratio", 0.7)
# Use the ``set_adaptive_parameter_default()`` method to set the required ``Trapezoid_base_ratio``
# parameter if undefined.
mc.set_adaptive_parameter_default("Trapezoid_base_ratio", 0.7)

# %%
# Set required parameters for the trapezoid: ratio of top width / base width
Expand Down Expand Up @@ -159,7 +147,7 @@ def check_line_origin_distance(i, duct_region):
#
# * Find the top line that makes up the duct.
#
# * Modify the start and end points of the line.
# * Modify the start and end points of the line using the get_coordinate_from_distance() method.
#
# * Set the region in Motor-CAD.
#
Expand All @@ -169,63 +157,61 @@ def check_line_origin_distance(i, duct_region):
for child_name in rt_region.child_names:
if "RotorDuctFluidRegion" in child_name:
duct_region = mc.get_region(child_name)
if round(duct_region.area / duct_area, 2) == 1: # check if full duct is drawn
if round(duct_region.area / duct_area, 2) == 1: # check if a full duct is drawn
for i, entity in enumerate(duct_region.entities):
if round(entity.length / Trap_W, 2) == 1: # check if the line is width
# additional check in case width = height
if (
round(entity.length / Trap_W, 2) == 1
): # check if the line length is the same as the duct width
# additional check in case the duct width = height
r_start_point, angle_start_point = xy_to_rt(entity.start.x, entity.start.y)
r_end_point, angle_end_point = xy_to_rt(entity.end.x, entity.end.y)
if abs(angle_end_point - angle_start_point) > 0.05: # 0.05 degree is tolerance
# check if the line located at top or bottom
# check if the line is located at top or bottom
Line_origin = check_line_origin_distance(i, duct_region)
if not Line_origin:
del_trap_angle = (
(angle_end_point - angle_start_point) * (1 - Trap_ratio) / 2
distance = entity.length * (1 - Trap_ratio)
new_start_point = entity.get_coordinate_from_distance(
entity.start, fraction=(1 - Trap_ratio) / 2
)
new_angle_start_point = angle_start_point + del_trap_angle
new_angle_end_point = angle_end_point - del_trap_angle
new_start_x, new_start_y = rt_to_xy(
r_start_point, new_angle_start_point
new_end_point = entity.get_coordinate_from_distance(
entity.end, fraction=(1 - Trap_ratio) / 2
)
new_end_x, new_end_y = rt_to_xy(r_end_point, new_angle_end_point)
new_start_point = Coordinate(new_start_x, new_start_y)
new_end_point = Coordinate(new_end_x, new_end_y)
duct_region.edit_point(entity.start, new_start_point)
duct_region.edit_point(entity.end, new_end_point)
mc.set_region(duct_region)

elif round(duct_region.area / duct_area, 2) == 0.5: # half duct
elif (
round(duct_region.area / duct_area, 2) == 0.5
): # account for the case where we have half ducts
Symm_angle = 360 / duct_region.duplications # angle of symmetry

for i, entity in enumerate(duct_region.entities):
if round(entity.length / Trap_W, 2) == 0.5: # check if the line is width
# additional check in case width = height
if (
round(entity.length / Trap_W, 2) == 0.5
): # check if the line length is the same as the duct width
# additional check in case the duct width = height
r_start_point, angle_start_point = xy_to_rt(entity.start.x, entity.start.y)
r_end_point, angle_end_point = xy_to_rt(entity.end.x, entity.end.y)
if abs(angle_end_point - angle_start_point) > 0.05: # 0.05 degree is tolerance
Line_origin = check_line_origin_distance(i, duct_region)
if not Line_origin:
del_trap_angle = (angle_end_point - angle_start_point) * (
1 - Trap_ratio
)
distance = entity.length * (1 - Trap_ratio)
if (
angle_start_point - 0 < 1e-10
or angle_start_point == 0
or round(angle_start_point / Symm_angle, 2) == 1
): # on symmetry plane
new_angle_end_point = angle_end_point - del_trap_angle
new_end_x, new_end_y = rt_to_xy(r_end_point, new_angle_end_point)
new_end_point = Coordinate(new_end_x, new_end_y)
new_end_point = entity.get_coordinate_from_distance(
entity.end, fraction=(1 - Trap_ratio) / 2
)
duct_region.edit_point(entity.end, new_end_point)
elif (
angle_end_point - 0 < 1e-10
or round(angle_end_point / Symm_angle, 2) == 1
): # symmetry plan
new_angle_start_point = angle_start_point + del_trap_angle
new_start_x, new_start_y = rt_to_xy(
r_start_point, new_angle_start_point
): # symmetry plane
new_start_point = entity.get_coordinate_from_distance(
entity.start, fraction=(1 - Trap_ratio) / 2
)
new_start_point = Coordinate(new_start_x, new_start_y)
duct_region.edit_point(entity.start, new_start_point)

mc.set_region(duct_region)
Expand Down
Loading

0 comments on commit 3e0c698

Please sign in to comment.