Skip to content

Commit

Permalink
Custom mesh lengths for adaptive regions. (#334)
Browse files Browse the repository at this point in the history
* initial commit

* update tests
  • Loading branch information
jgsdavies authored Sep 23, 2024
1 parent bae7688 commit beec069
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/ansys/motorcad/core/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def __init__(self, motorcad_instance=None):
self._child_names = []
self._motorcad_instance = motorcad_instance
self._region_type = RegionType.adaptive

# expect other properties to be implemented here including number duplications, material etc
self.mesh_length = 0

def __eq__(self, other):
"""Override the default equals implementation for Region."""
Expand Down Expand Up @@ -187,6 +186,9 @@ def _from_json(cls, json, motorcad_instance=None):
new_region.parent_name = json["parent_name"]
new_region._child_names = json["child_names"]

if "mesh_length" in json:
new_region.mesh_length = json["mesh_length"]

return new_region

# method to convert python object to send to Motor-CAD
Expand All @@ -209,6 +211,7 @@ def _to_json(self):
"entities": _convert_entities_to_json(self.entities),
"parent_name": self.parent_name,
"region_type": self._region_type.value,
"mesh_length": self.mesh_length,
}

return region_dict
Expand Down
15 changes: 14 additions & 1 deletion src/ansys/motorcad/core/methods/adaptive_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
# SOFTWARE.

"""Methods for adaptive geometry."""
from ansys.motorcad.core.geometry import Region
from warnings import warn

from ansys.motorcad.core.geometry import Region, RegionMagnet
from ansys.motorcad.core.rpc_client_core import is_running_in_internal_scripting


Expand Down Expand Up @@ -118,6 +120,17 @@ def set_region(self, region):
Motor-CAD region object.
"""
self.connection.ensure_version_at_least("2024.0")

if isinstance(region, RegionMagnet):
if (region._br_multiplier != 0.0) or (region._magnet_angle != 0.0):
# User has changed magnet properties that do not exist in older Motor-CAD API
if not self.connection.check_version_at_least("2024.2"):
warn("Setting magnet properties is only available in Motor-CAD 2024R2 or later")

if region.mesh_length != 0:
if not self.connection.check_version_at_least("2025"):
warn("Setting region mesh length is only available in Motor-CAD 2025R1 or later")

raw_region = region._to_json()

method = "SetRegion"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
_orientation_of_three_points,
rt_to_xy,
)
import ansys.motorcad.core.rpc_client_core as rpc_client_core
from ansys.motorcad.core.rpc_client_core import DEFAULT_INSTANCE, set_default_instance


Expand Down Expand Up @@ -351,6 +352,7 @@ def test_region_from_json():
"parent_name": "Insulation",
"child_names": ["Duct", "Duct_1"],
"region type": "Adaptive Region",
"mesh_length": 0.035,
}

test_region = geometry.Region()
Expand All @@ -364,6 +366,7 @@ def test_region_from_json():
test_region.entities = []
test_region.parent_name = "Insulation"
test_region._child_names = ["Duct", "Duct_1"]
test_region.mesh_length = 0.035

region = geometry.Region._from_json(raw_region)

Expand All @@ -382,6 +385,7 @@ def test_region_to_json():
"entities": [],
"parent_name": "Insulation",
"region_type": "Adaptive Region",
"mesh_length": 0.035,
}

test_region = geometry.Region()
Expand All @@ -394,6 +398,7 @@ def test_region_to_json():
test_region.duplications = 10
test_region.entities = []
test_region.parent_name = "Insulation"
test_region.mesh_length = 0.035

assert test_region._to_json() == raw_region

Expand Down Expand Up @@ -1906,3 +1911,18 @@ def test_get_set_region_magnet(mc):
assert magnet.br_value == 1.31
assert magnet.br_used == 1.31 * 2
assert magnet.region_type == RegionType.magnet


def test_get_set_region_compatibility(mc, monkeypatch):
monkeypatch.setattr(mc.connection, "program_version", "2024.1")
monkeypatch.setattr(rpc_client_core, "DONT_CHECK_MOTORCAD_VERSION", False)
test_region = RegionMagnet()
test_region.br_multiplier = 2
with pytest.warns(UserWarning):
mc.set_region(test_region)

test_region = Region()
test_region.mesh_length = 0.1

with pytest.warns(UserWarning):
mc.set_region(test_region)

0 comments on commit beec069

Please sign in to comment.