Skip to content

Commit 7980792

Browse files
committed
Fix WireInfo.starts_at_wall (and ends)
1 parent 4a9b280 commit 7980792

File tree

8 files changed

+112
-22
lines changed

8 files changed

+112
-22
lines changed

examples/advanced/inflation_grader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# TODO: Hack! mesh.assemble() won't work here but wires et. al. must be updated
3636
mesh.block_list.update()
3737

38-
grader = InflationGrader(mesh, 1e-3, 0.1)
38+
grader = InflationGrader(mesh, 1e-2, 0.1)
3939
grader.grade()
4040

4141
mesh.write(os.path.join("..", "case", "system", "blockMeshDict"), debug_path="debug.vtk")

src/classy_blocks/grading/autograding/params/inflation.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ def end_size(self) -> float:
3535
"""Size of the last cell in this layer"""
3636
return self.start_size * self.c2c_expansion**self.count
3737

38-
# @abc.abstractmethod
39-
# def get_chop(self, invert: bool) -> Chop:
40-
# """Prepare a Chop for grader params"""
41-
4238
@property
4339
def is_final(self) -> bool:
4440
"""Returns True if this layer is the last (no more space for additional ones)"""
@@ -53,14 +49,14 @@ def get_chop(self, total_count: int, invert: bool) -> Chop:
5349
length_ratio=self.length,
5450
end_size=self.end_size,
5551
c2c_expansion=1 / self.c2c_expansion,
56-
count=max(self.count, total_count),
52+
count=min(self.count, total_count),
5753
)
5854

5955
return Chop(
6056
length_ratio=self.length,
6157
start_size=self.start_size,
6258
c2c_expansion=self.c2c_expansion,
63-
count=max(self.count, total_count),
59+
count=min(self.count, total_count),
6460
)
6561

6662
def __repr__(self):
@@ -89,16 +85,17 @@ def __init__(self, start_size: float, c2c_expansion: float, bulk_size: float, ma
8985
self.total_expansion = self.bulk_size / self.start_size
9086

9187
# manually sum up those few cells that lead from start to bulk size
92-
count = 1
88+
count = 0
9389
size = self.start_size
9490
length = 0.0
9591

9692
while size <= self.bulk_size:
93+
length += size
94+
count += 1
95+
9796
if length > max_length:
9897
break
9998

100-
length += size
101-
count += 1
10299
size *= self.c2c_expansion
103100

104101
self._count = count
@@ -236,8 +233,6 @@ def get_stack(self, length: float) -> LayerStack:
236233
return stack
237234

238235
def get_count(self, length: float, starts_at_wall: bool, ends_at_wall: bool):
239-
print(starts_at_wall, ends_at_wall)
240-
241236
if not (starts_at_wall or ends_at_wall):
242237
return super().get_count(length, False, False)
243238

src/classy_blocks/grading/autograding/probe.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22
import functools
3-
from typing import List, Optional, Set
3+
from typing import List, Optional, Set, Tuple
44

55
from classy_blocks.base.exceptions import PatchNotFoundError
66
from classy_blocks.grading.autograding.catalogue import Catalogue
@@ -11,7 +11,7 @@
1111
from classy_blocks.mesh import Mesh
1212
from classy_blocks.optimize.grid import HexGrid
1313
from classy_blocks.types import DirectionType, OrientType
14-
from classy_blocks.util.constants import FACE_MAP
14+
from classy_blocks.util.constants import DIRECTION_MAP
1515

1616

1717
@functools.lru_cache(maxsize=2)
@@ -130,7 +130,7 @@ def get_default_wall_vertices(self, block: Block) -> Set[Vertex]:
130130
]
131131

132132
for orient in boundaries:
133-
side_vertices = {block.vertices[i] for i in FACE_MAP[orient]}
133+
side_vertices = set(block.get_side_vertices(orient))
134134
# check if they are defined elsewhere
135135
try:
136136
self.mesh.patch_list.find(side_vertices)
@@ -143,10 +143,28 @@ def get_default_wall_vertices(self, block: Block) -> Set[Vertex]:
143143

144144
def get_wall_vertices(self, block: Block) -> Set[Vertex]:
145145
"""Returns vertices that are on the 'wall' patches"""
146+
# TODO: delete if not needed
146147
return self.get_explicit_wall_vertices(block).union(self.get_default_wall_vertices(block))
147148

148-
def get_wire_info(self, wire: Wire, block: Block) -> WireInfo:
149-
# TODO: test
150-
wall_vertices = self.get_wall_vertices(block)
149+
def get_wire_boundaries(self, wire: Wire, block: Block) -> Tuple[bool, bool]:
150+
"""Finds out whether a Wire starts or ends on a wall patch"""
151+
start_orient = DIRECTION_MAP[wire.direction][0]
152+
end_orient = DIRECTION_MAP[wire.direction][1]
153+
154+
def find_patch(orient: OrientType) -> bool:
155+
vertices = set(block.get_side_vertices(orient))
156+
157+
try:
158+
patch = self.mesh.patch_list.find(vertices)
159+
if patch.kind == "wall":
160+
return True
161+
except PatchNotFoundError:
162+
if self.mesh.patch_list.default.get("kind") == "wall":
163+
return True
151164

152-
return WireInfo(wire, wire.vertices[0] in wall_vertices, wire.vertices[1] in wall_vertices)
165+
return False
166+
167+
return (find_patch(start_orient), find_patch(end_orient))
168+
169+
def get_wire_info(self, wire: Wire, block: Block) -> WireInfo:
170+
return WireInfo(wire, *self.get_wire_boundaries(wire, block))

src/classy_blocks/items/block.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from classy_blocks.items.vertex import Vertex
66
from classy_blocks.items.wires.axis import Axis
77
from classy_blocks.items.wires.wire import Wire
8-
from classy_blocks.types import DirectionType, IndexType
8+
from classy_blocks.types import DirectionType, IndexType, OrientType
99
from classy_blocks.util import constants
1010
from classy_blocks.util.frame import Frame
1111

@@ -117,6 +117,9 @@ def check_consistency(self) -> None:
117117
def indexes(self) -> IndexType:
118118
return [vertex.index for vertex in self.vertices]
119119

120+
def get_side_vertices(self, orient: OrientType) -> List[Vertex]:
121+
return [self.vertices[i] for i in constants.FACE_MAP[orient]]
122+
120123
def format_grading(self) -> str:
121124
"""Returns the simple/edgeGrading string"""
122125
if all(axis.is_simple for axis in self.axes): # is_simple

src/classy_blocks/items/wires/wire.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, vertices: List[Vertex], direction: DirectionType, corner_1: i
3030
self.corners = [corner_1, corner_2]
3131
self.vertices = [vertices[corner_1], vertices[corner_2]]
3232

33-
self.direction = direction
33+
self.direction: DirectionType = direction
3434

3535
# the default edge is 'line' but will be replaced if the user wishes so
3636
# (that is, not included in edge.factory.registry)

src/classy_blocks/util/constants.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict, List, Tuple
22

3-
from classy_blocks.types import OrientType
3+
from classy_blocks.types import DirectionType, OrientType
44

55
# data type
66
DTYPE = "float" # dtype as taken by np.array()
@@ -32,6 +32,14 @@
3232
"left",
3333
]
3434

35+
# Connects block axis (direction) and orients
36+
# (read: Direction 0 goes from right to left, etc.
37+
DIRECTION_MAP: Dict[DirectionType, Tuple[OrientType, OrientType]] = {
38+
0: ("left", "right"),
39+
1: ("front", "back"),
40+
2: ("bottom", "top"),
41+
}
42+
3543
# pairs of corner indexes along axes
3644
AXIS_PAIRS = (
3745
((0, 1), (3, 2), (7, 6), (4, 5)), # x

tests/test_grading/test_autograde.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44

55
from classy_blocks.construct.flat.sketches.grid import Grid
6+
from classy_blocks.construct.operations.box import Box
67
from classy_blocks.construct.shapes.cylinder import Cylinder
78
from classy_blocks.construct.shapes.frustum import Frustum
89
from classy_blocks.construct.stack import ExtrudedStack
@@ -30,6 +31,9 @@ def get_cylinder(self) -> Cylinder:
3031
def get_frustum(self) -> Frustum:
3132
return Frustum([0, 0, 0], [1, 0, 0], [0, 1, 0], 0.3)
3233

34+
def get_box(self) -> Box:
35+
return Box([0, 0, 0], [1, 1, 1])
36+
3337
def setUp(self):
3438
self.mesh = Mesh()
3539

tests/test_grading/test_probe.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,65 @@ def test_flipped_shape(self, flip_indexes, check_row, check_index):
205205
probe = Probe(self.mesh)
206206

207207
self.assertTrue(probe.get_rows(1)[check_row].entries[check_index].flipped)
208+
209+
@parameterized.expand(
210+
(
211+
(0, 4, True),
212+
(1, 5, True),
213+
(2, 6, True),
214+
(3, 7, True),
215+
(0, 1, True),
216+
(3, 2, True),
217+
(4, 5, True),
218+
(7, 6, True),
219+
(0, 3, False),
220+
(1, 2, False),
221+
(4, 7, False),
222+
(5, 6, False),
223+
)
224+
)
225+
def test_wire_boundaries_explicit(self, wire_start, wire_end, starts_at_wall):
226+
box = self.get_box()
227+
box.set_patch(["bottom", "left", "right"], "wallPatch")
228+
self.mesh.add(box)
229+
230+
self.mesh.modify_patch("wallPatch", "wall")
231+
self.mesh.assemble()
232+
233+
probe = Probe(self.mesh)
234+
block = self.mesh.blocks[0]
235+
info = probe.get_wire_info(block.wires[wire_start][wire_end], block)
236+
237+
self.assertEqual(info.starts_at_wall, starts_at_wall)
238+
239+
@parameterized.expand(
240+
(
241+
(0, 4, True),
242+
(1, 5, True),
243+
(2, 6, True),
244+
(3, 7, True),
245+
(0, 1, True),
246+
(3, 2, True),
247+
(4, 5, True),
248+
(7, 6, True),
249+
(0, 3, False),
250+
(1, 2, False),
251+
(4, 7, False),
252+
(5, 6, False),
253+
)
254+
)
255+
def test_wire_boundaries_default(self, wire_start, wire_end, starts_at_wall):
256+
# same situation as the 'explicit' test but 'patch' patch types are defined
257+
# and walls are the default
258+
box = self.get_box()
259+
box.set_patch(["top", "front", "back"], "patchPatch")
260+
self.mesh.add(box)
261+
262+
self.mesh.set_default_patch("defaultFaces", "wall")
263+
self.mesh.assemble()
264+
265+
probe = Probe(self.mesh)
266+
block = self.mesh.blocks[0]
267+
info = probe.get_wire_info(block.wires[wire_start][wire_end], block)
268+
269+
self.assertEqual(info.starts_at_wall, starts_at_wall)

0 commit comments

Comments
 (0)