Skip to content

Commit 633137c

Browse files
committed
Add tests for coverage and fix bugs
1 parent 3f3e72f commit 633137c

File tree

13 files changed

+264
-35
lines changed

13 files changed

+264
-35
lines changed

examples/shape/cylinder.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
import os
22

33
import classy_blocks as cb
4-
from classy_blocks.construct.flat.sketches.disk import DiskBase
5-
6-
DiskBase.core_ratio = 0.4 # Default is 0.8
74

85
mesh = cb.Mesh()
96

107
axis_point_1 = [0.0, 0.0, 0.0]
118
axis_point_2 = [5.0, 5.0, 0.0]
129
radius_point_1 = [0.0, 0.0, 2.0]
1310

14-
cylinder = cb.QuarterCylinder(axis_point_1, axis_point_2, radius_point_1)
11+
cylinder = cb.Cylinder(axis_point_1, axis_point_2, radius_point_1)
1512

1613
cylinder.set_start_patch("inlet")
1714
cylinder.set_end_patch("outlet")
1815
cylinder.set_outer_patch("walls")
19-
cylinder.set_symmetry_patch("sym")
2016

21-
# if curved core edges get in the way (when moving vertices, optimization, ...),
17+
# If curved core edges get in the way (when moving vertices, optimization, ...),
2218
# remove them with this method:
2319
cylinder.remove_inner_edges(start=False, end=True)
2420

21+
# Chop and grade
2522
bl_thickness = 1e-3
2623
core_size = 0.1
2724

28-
# manual grading
29-
# cylinder.chop_axial(count=30)
30-
# cylinder.chop_radial(start_size=core_size, end_size=bl_thickness)
31-
# cylinder.chop_tangential(start_size=core_size)
25+
cylinder.chop_axial(count=30)
26+
cylinder.chop_radial(start_size=core_size, end_size=bl_thickness)
27+
cylinder.chop_tangential(start_size=core_size)
3228

3329
mesh.add(cylinder)
3430
mesh.modify_patch("walls", "wall")
3531

36-
# automatic grading
37-
grader = cb.SmoothGrader(mesh, 0.1)
38-
grader.grade()
39-
4032
mesh.write(os.path.join("..", "case", "system", "blockMeshDict"), debug_path="debug.vtk")

examples/shape/one_core_cylinder.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
3+
import classy_blocks as cb
4+
from classy_blocks.construct.flat.sketches.disk import OneCoreDisk
5+
from classy_blocks.util import functions as f
6+
7+
# A simpler version of a Cylinder with only a single block in the middle;
8+
# easier to block but slightly worse cell quality;
9+
# currently there is no OneCoreCylinder shape so it has to be extruded;
10+
# two lines are needed instead of one.
11+
12+
mesh = cb.Mesh()
13+
14+
axis_point_1 = f.vector(0.0, 0.0, 0.0)
15+
axis_point_2 = f.vector(5.0, 5.0, 0.0)
16+
radius_point_1 = f.vector(0.0, 0.0, 2.0)
17+
18+
19+
one_core_disk = OneCoreDisk(axis_point_1, radius_point_1, axis_point_1 - axis_point_2)
20+
quarter_cylinder = cb.ExtrudedShape(one_core_disk, f.norm(axis_point_2 - axis_point_1))
21+
quarter_cylinder.chop(0, count=5)
22+
quarter_cylinder.chop(1, count=10)
23+
quarter_cylinder.chop(2, count=20)
24+
mesh.add(quarter_cylinder)
25+
26+
mesh.set_default_patch("walls", "wall")
27+
mesh.write(os.path.join("..", "case", "system", "blockMeshDict"), debug_path="debug.vtk")

examples/shape/quarter_cylinder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
mesh.add(quarter_cylinder)
2020

21-
mesh.assemble()
22-
21+
# Use an automatic grader that will try to make cells in neighbouring blocks
22+
# as similar in size as possible
2323
grader = cb.SmoothGrader(mesh, 0.05)
2424
grader.grade()
2525

src/classy_blocks/construct/flat/sketches/disk.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,8 @@ def parts(self):
191191

192192
class OneCoreDisk(DiskBase):
193193
"""A disk with a single block in the center and four blocks around;
194-
see docs/blocking for point numbers and faces/grid indexing."""
194+
see docs/sketches for point numbers and faces/grid indexing."""
195195

196-
# TODO: test
197196
chops: ClassVar = [
198197
[1], # axis 0
199198
[1, 2], # axis 1
@@ -220,6 +219,10 @@ def __init__(self, center_point: PointType, radius_point: PointType, normal: Vec
220219
def center(self):
221220
return self.faces[0].center
222221

222+
@property
223+
def origo(self):
224+
return self.faces[0].center
225+
223226
@property
224227
def grid(self):
225228
return [self.faces[:1], self.faces[1:]]
@@ -229,7 +232,7 @@ def add_core_spline_edges(self):
229232

230233

231234
class QuarterDisk(DiskBase):
232-
"""A quarter of a four-core disk; see docs/blocking for point numbers and faces/grid indexing"""
235+
"""A quarter of a four-core disk; see docs/sketches for point numbers and faces/grid indexing"""
233236

234237
chops: ClassVar = [
235238
[1], # axis 0

src/classy_blocks/construct/shapes/cylinder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ def fill(cls, source: "ExtrudedRing") -> "Cylinder":
116116
)
117117

118118
return cls(source.sketch_1.center, source.sketch_2.center, source.sketch_1.inner_radius_point)
119+
120+
def set_symmetry_patch(self, _name: str) -> None:
121+
raise RuntimeError("Cylinder does not have symmetry patches")

src/classy_blocks/grading/autograding/fixed/rules.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ def get_count(self, _length, _start_at_wall, _end_at_wall):
1515
def is_squeezed(self, _count, _info) -> bool:
1616
return True # grade everything in first pass
1717

18-
def get_chops(self, count, _info) -> List[Chop]:
19-
return [Chop(count=count)]
18+
def get_chops(self, _count, _info) -> List[Chop]:
19+
# In FixedCountGrader this is never called
20+
# (everything is graded as 'squeezed')
21+
raise RuntimeError

src/classy_blocks/grading/autograding/grader.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import abc
21
from typing import List, Tuple, get_args
32

43
from classy_blocks.cbtyping import ChopTakeType, DirectionType
@@ -10,9 +9,7 @@
109
from classy_blocks.mesh import Mesh
1110

1211

13-
class GraderBase(abc.ABC):
14-
stages: int
15-
12+
class GraderBase:
1613
def __init__(self, mesh: Mesh, rules: ChopRules):
1714
self.mesh = mesh
1815
self.rules = rules
@@ -57,11 +54,6 @@ def set_counts(self, row: Row, take: ChopTakeType) -> None:
5754

5855
def grade_squeezed(self, row: Row) -> None:
5956
for entry in row.entries:
60-
# TODO! don't touch wires, defined by USER
61-
# if wire.is_defined:
62-
# # TODO: test
63-
# continue
64-
6557
for wire in entry.wires:
6658
if wire.is_defined:
6759
continue
@@ -74,10 +66,6 @@ def grade_squeezed(self, row: Row) -> None:
7466

7567
def finalize(self, row: Row) -> None:
7668
for entry in row.entries:
77-
# TODO! don't touch wires, defined by USER
78-
# if wire.is_defined:
79-
# # TODO: test
80-
# continue
8169
for wire in entry.wires:
8270
if wire.is_defined:
8371
continue

tests/test_construct/test_flat/test_disk.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from parameterized import parameterized
66

77
from classy_blocks.construct.flat.sketches.disk import HalfDisk, OneCoreDisk, Oval, QuarterDisk, WrappedDisk
8+
from classy_blocks.construct.shape import ExtrudedShape
89
from classy_blocks.construct.shapes.sphere import get_named_points
10+
from classy_blocks.mesh import Mesh
911
from classy_blocks.util import functions as f
1012
from classy_blocks.util.constants import TOL
1113

@@ -107,6 +109,11 @@ def test_one_core_disk(self):
107109
self.assertEqual(len(disk.grid[0]), 1)
108110
self.assertEqual(len(disk.grid[1]), 4)
109111

112+
def test_one_core_disk_origo(self):
113+
disk = OneCoreDisk([0, 0, 0], [1, 0, 0], [0, 0, 1])
114+
115+
np.testing.assert_array_almost_equal(disk.origo, [0, 0, 0])
116+
110117
def test_half_disk(self):
111118
disk = HalfDisk([0, 0, 0], [1, 0, 0], [0, 0, 1])
112119

@@ -148,3 +155,19 @@ def test_grid(self):
148155

149156
self.assertEqual(len(oval.grid[0]), 6)
150157
self.assertEqual(len(oval.grid[1]), 10)
158+
159+
160+
class ChopTests(unittest.TestCase):
161+
def setUp(self):
162+
self.mesh = Mesh()
163+
164+
def test_one_core_disk(self):
165+
sketch = OneCoreDisk([0, 0, 0], [1, 0, 0], [0, 0, 1])
166+
extrude = ExtrudedShape(sketch, 1)
167+
168+
extrude.chop(0, count=10)
169+
extrude.chop(1, count=5)
170+
extrude.chop(2, count=1)
171+
172+
self.mesh.add(extrude)
173+
self.mesh.assemble()

tests/test_construct/test_shape.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ def test_remove_inner_edges(self):
322322
for edge in operation.top_face.edges:
323323
self.assertIsInstance(edge, Line)
324324

325+
def test_symmetry_patches(self):
326+
with self.assertRaises(RuntimeError):
327+
self.cylinder.set_symmetry_patch("test")
328+
325329

326330
class LoftedShapeTests(unittest.TestCase):
327331
@property

tests/test_grading/test_autograde.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44

5+
import classy_blocks as cb
56
from classy_blocks.construct.flat.sketches.grid import Grid
67
from classy_blocks.construct.operations.box import Box
78
from classy_blocks.construct.shapes.cylinder import Cylinder
@@ -65,7 +66,7 @@ def test_simple_grader_stack(self):
6566
for axis in block.axes:
6667
self.assertEqual(axis.count, 5)
6768

68-
def test_highre_cylinder(self):
69+
def test_smooth_cylinder(self):
6970
self.mesh.add(self.get_cylinder())
7071
self.mesh.assemble()
7172

@@ -75,3 +76,37 @@ def test_highre_cylinder(self):
7576
# make sure all blocks are defined
7677
for block in self.mesh.blocks:
7778
self.assertTrue(block.is_defined)
79+
80+
81+
class AdvancedAutogradeTests(unittest.TestCase):
82+
def setUp(self):
83+
self.mesh = Mesh()
84+
85+
def get_grid(self, badness: float = 0.8):
86+
# 6 blocks of weird shape, taken from the autograder examples
87+
# but with adjustable 'badness' (amount of displacement)
88+
# to test border cases
89+
base = cb.Grid([0, 0, 0], [3, 2, 0], 3, 2)
90+
shape = cb.ExtrudedShape(base, 1)
91+
92+
# turn one block around
93+
shape.grid[1][0].rotate(np.pi, [0, 0, 1])
94+
self.mesh.add(shape)
95+
96+
# move some points to get a mesh with uneven blocks
97+
self.mesh.assemble()
98+
finder = cb.GeometricFinder(self.mesh)
99+
100+
move_points = [[0, 1, 1], [2, 1, 1], [3, 1, 1]]
101+
102+
for point in move_points:
103+
vertex = next(iter(finder.find_in_sphere(point)))
104+
vertex.translate([0, badness, 0])
105+
106+
def test_squeezed_chop(self):
107+
self.get_grid()
108+
109+
grader = SmoothGrader(self.mesh, 0.05)
110+
grader.grade()
111+
112+
self.assertIsNone(self.mesh.blocks[5].axes[1].wires[1].grading.chops[0].total_expansion)

0 commit comments

Comments
 (0)