Skip to content

Commit ba7bd43

Browse files
Ensure the geometry tolerance and the planar tolerance are compatible with each other (#1389)
* Ensure the geometry tolerance and the planar tolerance are compatible with each other * Fix msg
1 parent 3f796a1 commit ba7bd43

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

flow360/component/simulation/meshing_param/params.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,42 @@ def invalid_geometry_accuracy(cls, value):
178178
raise ValueError("Geometry accuracy is required when geometry AI is used.")
179179
return value
180180

181+
@pd.field_validator("geometry_accuracy", mode="after")
182+
@classmethod
183+
def compatible_geometry_accuracy_and_planar_face_tolerance(cls, value):
184+
"""Ensure geometry accuracy and planar face tolerance are compatible"""
185+
if value is None:
186+
return value
187+
188+
validation_info = get_validation_info()
189+
190+
if (
191+
validation_info is None
192+
or validation_info.planar_face_tolerance is None
193+
or validation_info.global_bounding_box is None
194+
or validation_info.project_length_unit is None
195+
):
196+
return value
197+
198+
absolute_tolerance_dimensioned = (
199+
validation_info.global_bounding_box.largest_dimension
200+
* validation_info.planar_face_tolerance
201+
* validation_info.project_length_unit
202+
)
203+
if value > absolute_tolerance_dimensioned:
204+
minimum_planar_face_tolerance = (
205+
value
206+
/ validation_info.project_length_unit
207+
/ validation_info.global_bounding_box.largest_dimension
208+
).value
209+
raise ValueError(
210+
f"geometry_accuracy is too large for the planar_face_tolerance to take effect."
211+
f" Reduce geometry_accuracy to at most {absolute_tolerance_dimensioned} "
212+
f"or increase the planar_face_tolerance to at least {minimum_planar_face_tolerance}."
213+
)
214+
215+
return value
216+
181217
@pd.field_validator(
182218
"surface_max_aspect_ratio", "surface_max_adaptation_iterations", mode="after"
183219
)

tests/simulation/params/test_validators_params.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import flow360.component.simulation.units as u
99
from flow360.component.simulation.entity_info import (
10+
GeometryEntityInfo,
1011
SurfaceMeshEntityInfo,
1112
VolumeMeshEntityInfo,
1213
)
@@ -1843,6 +1844,7 @@ def test_beta_mesher_only_features():
18431844

18441845

18451846
def test_geometry_AI_only_features():
1847+
# * Test GAI guardrails
18461848
with SI_unit_system:
18471849
params = SimulationParams(
18481850
meshing=MeshingParams(
@@ -1903,6 +1905,61 @@ def test_geometry_AI_only_features():
19031905
errors[0]["msg"] == "Value error, Geometry accuracy is required when geometry AI is used."
19041906
)
19051907

1908+
# * Test geometry_accuracy and planar_face_tolerance compatibility
1909+
with SI_unit_system:
1910+
params_original = SimulationParams(
1911+
meshing=MeshingParams(
1912+
defaults=MeshingDefaults(
1913+
geometry_accuracy=1e-5 * u.m,
1914+
planar_face_tolerance=1e-10,
1915+
boundary_layer_first_layer_thickness=10,
1916+
surface_max_edge_length=1e-2,
1917+
),
1918+
),
1919+
private_attribute_asset_cache=AssetCache(
1920+
project_length_unit=1 * u.cm,
1921+
use_inhouse_mesher=True,
1922+
use_geometry_AI=True,
1923+
project_entity_info=GeometryEntityInfo(
1924+
global_bounding_box=[[-100, -100, -100], [100, 1e-12, 100]],
1925+
),
1926+
),
1927+
)
1928+
params, errors, _ = validate_model(
1929+
params_as_dict=params_original.model_dump(mode="json"),
1930+
validated_by=ValidationCalledBy.LOCAL,
1931+
root_item_type="Geometry",
1932+
validation_level="VolumeMesh",
1933+
)
1934+
assert len(errors) == 1
1935+
# Largest dim = 200 cm
1936+
# with planar face tolerance = 1e-10
1937+
# largest geometry accuracy = 1e-10 * 200 cm = 2e-8 cm
1938+
# with geometry accuracy = 1e-5m
1939+
# minimum planar face tolerance = 1e-5m / 200 cm = 5e-06
1940+
assert errors[0]["msg"] == (
1941+
"Value error, geometry_accuracy is too large for the planar_face_tolerance to take effect. "
1942+
"Reduce geometry_accuracy to at most 2e-08 cm or increase the planar_face_tolerance to at least 5e-06."
1943+
)
1944+
params_original.meshing.defaults.geometry_accuracy = 2e-08 * u.cm
1945+
params, _, _ = validate_model(
1946+
params_as_dict=params_original.model_dump(mode="json"),
1947+
validated_by=ValidationCalledBy.LOCAL,
1948+
root_item_type="Geometry",
1949+
validation_level="VolumeMesh",
1950+
)
1951+
assert params
1952+
1953+
params_original.meshing.defaults.geometry_accuracy = 1e-5 * u.m
1954+
params_original.meshing.defaults.planar_face_tolerance = 5e-06
1955+
params, _, _ = validate_model(
1956+
params_as_dict=params_original.model_dump(mode="json"),
1957+
validated_by=ValidationCalledBy.LOCAL,
1958+
root_item_type="Geometry",
1959+
validation_level="VolumeMesh",
1960+
)
1961+
assert params
1962+
19061963

19071964
def test_redefined_user_defined_fields():
19081965

0 commit comments

Comments
 (0)