Skip to content

Commit

Permalink
Merge pull request #84 from simbilod/fix_update
Browse files Browse the repository at this point in the history
Fix update
  • Loading branch information
simbilod authored Nov 4, 2024
2 parents 588598d + 6c7bf7b commit e846f42
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 8 deletions.
25 changes: 18 additions & 7 deletions meshwell/labeledentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def filter_by_target_and_tags(target_dimension, tags, min_mass, max_mass):
for tag in tags
if min_mass < self.model.occ.getMass(target_dimension, tag) < max_mass
]
return {tag: self.model.occ.getMass(target_dimension, tag) for tag in tags}
return (
{tag: self.model.occ.getMass(target_dimension, tag) for tag in tags}
if tags is not []
else {}
)

# Filter the tags based on current dimension and target
match self.dim - target_dimension:
Expand All @@ -66,18 +70,25 @@ def filter_by_target_and_tags(target_dimension, tags, min_mass, max_mass):
case 1:
tags = self.boundaries
case 2 | 3:
tags = [self.model.occ.getCurveLoops(b)[1] for b in self.boundaries]
tags = [
c
for b in self.boundaries
for cs in self.model.occ.getCurveLoops(b)[1]
for c in cs
]
case -1:
warnings.warn("Applying volume ResolutionSpec to surface, skipping")
return []
return {}

# If targeting points, need post-filtering filtering
if target_dimension == 0:
filtered_tags = filter_by_target_and_tags(1, tags, min_mass, max_mass)
return (
self.model.getBoundary([(0, tag) for tag in filtered_tags]),
None,
) # no mass in 0D
return {
p[1]: None
for p in self.model.getBoundary(
[(1, tag) for tag in filtered_tags.keys()]
)
} # no mass in 0D
else:
return filter_by_target_and_tags(target_dimension, tags, min_mass, max_mass)

Expand Down
4 changes: 4 additions & 0 deletions meshwell/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def refine(self, resolution_factor: float):
if result.sizemin is not None:
result.sizemin *= resolution_factor

return result


class ExponentialField(SampledField):
"""Exponential growth of the characteristic length away from the entity"""
Expand Down Expand Up @@ -196,3 +198,5 @@ def refine(self, resolution_factor: float):
result = copy.copy(self)
if result.sizemin is not None:
result.sizemin *= resolution_factor

return result
132 changes: 131 additions & 1 deletion tests/test_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from meshwell.resolution import ConstantInField, ThresholdField, ExponentialField
from meshwell.utils import compare_meshes
from pathlib import Path
import pytest
import numpy as np


def test_2D_resolution():
Expand Down Expand Up @@ -150,6 +152,134 @@ def test_exponential_field():
)


@pytest.mark.parametrize(
"field",
[
ConstantInField(apply_to="surfaces", resolution=1),
ExponentialField(
growth_factor=2,
sizemin=0.3,
max_samplings=200,
apply_to="curves",
lengthscale=2,
),
ThresholdField(
sizemin=1,
sizemax=5,
distmin=0,
distmax=5,
apply_to="curves",
),
],
)
def test_refine(field):
large_rect = 10

polygon1 = shapely.Polygon(
[[0, 0], [large_rect, 0], [large_rect, large_rect], [0, large_rect], [0, 0]],
)

points = []
for factor in [0.5, 1, 2]:
model = Model(n_threads=1) # 1 thread for deterministic mesh
poly_obj2 = PolySurface(
polygons=polygon1,
model=model,
mesh_order=1,
physical_name="inner",
resolutions=[field.refine(factor)],
)

entities_list = [poly_obj2]

output = model.mesh(
entities_list=entities_list,
default_characteristic_length=5,
verbosity=0,
filename="mesh_test_2D_exponential.msh",
)

points.append(len(output.points))

assert points[0] > points[1] > points[2]


# FIXME: add regression
@pytest.mark.parametrize(
("apply_to", "min_mass", "max_mass"),
[
("volumes", 5**3, np.inf),
("volumes", 0, 5**3),
("surfaces", 5**2, np.inf),
("surfaces", 0, 5**2),
("curves", 5, np.inf),
("curves", 0, 5),
("points", 5, np.inf),
("points", 0, 5),
],
)
def test_filter(apply_to, min_mass, max_mass):
large_rect = 6
small_rect = 4

buffers1 = {-6: 0, 6: 0}
buffers2 = {-4: 0, 4: 0}

polygon1 = shapely.Polygon(
[
[-large_rect / 2, -large_rect / 2],
[large_rect / 2, -large_rect / 2],
[large_rect / 2, large_rect / 2],
[-large_rect / 2, large_rect / 2],
[-large_rect / 2, -large_rect / 2],
],
)
polygon2 = shapely.Polygon(
[
[-small_rect / 2, -small_rect / 2],
[small_rect / 2, -small_rect / 2],
[small_rect / 2, small_rect / 2],
[-small_rect / 2, small_rect / 2],
[-small_rect / 2, -small_rect / 2],
],
)

model = Model(n_threads=1) # 1 thread for deterministic mesh
prism1 = Prism(
polygons=polygon1,
buffers=buffers1,
model=model,
mesh_order=2,
physical_name="outer",
resolutions=[
ConstantInField(
apply_to=apply_to, resolution=0.5, min_mass=min_mass, max_mass=max_mass
)
],
)
prism2 = Prism(
polygons=polygon2,
buffers=buffers2,
model=model,
mesh_order=1,
physical_name="inner",
resolutions=[
ConstantInField(
apply_to=apply_to, resolution=0.5, min_mass=min_mass, max_mass=max_mass
)
],
)

entities_list = [prism1, prism2]

model.mesh(
entities_list=entities_list,
default_characteristic_length=5,
verbosity=0,
filename="mesh_filter.msh",
)


if __name__ == "__main__":
test_exponential_field()
test_filter("points", 5, np.inf)
# test_3D_resolution()

0 comments on commit e846f42

Please sign in to comment.