Skip to content

Commit f670aa1

Browse files
committed
Drop the 'invert' keyword for chopping
1 parent 92b1d4d commit f670aa1

File tree

9 files changed

+46
-49
lines changed

9 files changed

+46
-49
lines changed

examples/advanced/edge_grading.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
start.chop(0, start_size=0.1)
1212
start.chop(1, length_ratio=0.5, start_size=0.01, c2c_expansion=1.2, preserve="start_size")
13-
# start.chop(1, length_ratio=0.5, end_size=0.01, c2c_expansion=1 / 1.2, preserve="end_size")
13+
start.chop(1, length_ratio=0.5, end_size=0.01, c2c_expansion=1 / 1.2, preserve="end_size")
1414
start.chop(2, count=1)
1515
mesh.add(start)
1616

@@ -24,6 +24,7 @@
2424
contract.chop(2, start_size=0.1)
2525
mesh.add(contract)
2626

27+
# rotate the end block to demonstrate grading propagation on non-aligned blocks
2728
end = cb.Extrude(contract.get_face("top"), 1)
2829
end.rotate(np.pi, [0, 0, 1])
2930
end.chop(2, start_size=0.1)

examples/operation/box.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
box = cb.Box([-1, -2, -4], [4, 2, 1])
66

77
# direction of corners 0-1
8-
box.chop(0, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=False)
9-
box.chop(0, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=True)
8+
box.chop(0, length_ratio=0.5, start_size=0.02, c2c_expansion=1.2)
9+
box.chop(0, length_ratio=0.5, end_size=0.02, c2c_expansion=1 / 1.2)
1010

1111
# direction of corners 1-2
12-
box.chop(1, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=False)
13-
box.chop(1, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=True)
12+
box.chop(1, length_ratio=0.5, start_size=0.02, c2c_expansion=1.2)
13+
box.chop(1, length_ratio=0.5, end_size=0.02, c2c_expansion=1 / 1.2)
1414

1515
# extrude direction
1616
box.chop(2, c2c_expansion=1, count=20)

examples/operation/extrude.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
extrude = cb.Extrude(base, [0.5, 0.5, 3])
88

99
# direction of corners 0-1
10-
extrude.chop(0, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=False)
11-
extrude.chop(0, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=True)
10+
extrude.chop(0, length_ratio=0.5, start_size=0.02, c2c_expansion=1.2)
11+
extrude.chop(0, length_ratio=0.5, end_size=0.02, c2c_expansion=1 / 1.2)
1212

1313
# direction of corners 1-2
14-
extrude.chop(1, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=False)
15-
extrude.chop(1, start_size=0.02, c2c_expansion=1.2, length_ratio=0.5, invert=True)
14+
extrude.chop(1, length_ratio=0.5, start_size=0.02, c2c_expansion=1.2)
15+
extrude.chop(1, length_ratio=0.5, end_size=0.02, c2c_expansion=1 / 1.2)
1616

1717
# extrude direction
1818
extrude.chop(2, c2c_expansion=1, count=20)

examples/operation/wedge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
wedges[-1].set_patch("right", "outlet")
2929

3030
# this will be copied to all next blocks
31-
wedges[0].chop(1, c2c_expansion=1.2, start_size=0.01, invert=True)
31+
wedges[0].chop(1, end_size=0.01, c2c_expansion=1 / 1.2)
3232

3333
# Once an entity is added to the mesh,
3434
# its modifications will not be reflected there;

src/classy_blocks/grading/chop.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class Chop:
5757
count: Optional[int] = None
5858
end_size: Optional[float] = None
5959
total_expansion: Optional[float] = None
60-
invert: bool = False
6160
preserve: ChopPreserveType = "c2c_expansion"
6261

6362
def __post_init__(self) -> None:
@@ -92,9 +91,6 @@ def calculate(self, length: float) -> Tuple[int, float]:
9291
if {"count", "total_expansion", "c2c_expansion", "start_size", "end_size"}.issubset(calculated):
9392
self.results["count"] = int(self.results["count"])
9493

95-
if self.invert:
96-
data["total_expansion"] = 1 / data["total_expansion"]
97-
9894
return data["count"], data["total_expansion"]
9995

10096
for chop_rel in ChopRelation.get_possible_combinations():
@@ -113,6 +109,17 @@ def calculate(self, length: float) -> Tuple[int, float]:
113109

114110
raise ValueError(f"Could not calculate count and grading for given parameters: {data}")
115111

112+
def invert(self) -> None:
113+
"""Modifies this chop so that grading will have an opposite orientation,
114+
a.k.a. start -> end and c2c -> 1/c2c."""
115+
self.end_size, self.start_size = self.start_size, self.end_size
116+
117+
if self.c2c_expansion is not None:
118+
self.c2c_expansion = 1 / self.c2c_expansion
119+
120+
if self.total_expansion is not None:
121+
self.total_expansion = 1 / self.total_expansion
122+
116123
def copy_preserving(self, inverted: bool = False) -> "Chop":
117124
"""Creates a copy of this Chop with equal count but
118125
sets other parameters from current data so that
@@ -123,21 +130,10 @@ def copy_preserving(self, inverted: bool = False) -> "Chop":
123130
for arg in ["total_expansion", "c2c_expansion", "start_size", "end_size"]:
124131
args[arg] = None
125132

126-
# TODO: get rid of if-ing
127-
if self.preserve == "start_size":
128-
if inverted:
129-
args["end_size"] = self.results["start_size"]
130-
else:
131-
args["start_size"] = self.results["start_size"]
132-
elif self.preserve == "end_size":
133-
if inverted:
134-
args["start_size"] = self.results["end_size"]
135-
else:
136-
args["end_size"] = self.results["end_size"]
137-
else:
138-
if inverted:
139-
args["c2c_expansion"] = 1 / self.results["c2c_expansion"]
140-
else:
141-
args["c2c_expansion"] = self.results["c2c_expansion"]
142-
143-
return Chop(**args)
133+
args[self.preserve] = self.results[self.preserve]
134+
chop = Chop(**args)
135+
136+
if inverted:
137+
chop.invert()
138+
139+
return chop

src/classy_blocks/items/block.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def chop(self, axis: AxisType, chop: Chop) -> None:
5757
Optionally, this can be the only provided argument;
5858
in that case c2c_expansion will be set to 1.
5959
* *start_size:
60-
size of the first cell (last if invert==True)
60+
size of the first cell
6161
* *end_size:
6262
size of the last cell
6363
* *c2c_expansion:
@@ -66,12 +66,12 @@ def chop(self, axis: AxisType, chop: Chop) -> None:
6666
ratio between first and last cell size
6767
6868
:Optional keyword arguments:
69-
* *invert:
70-
reverses grading if True
71-
* *take:
72-
must be 'min', 'max', or 'avg'; takes minimum or maximum edge
73-
length for block size calculation, or average of all edges in given direction.
74-
With multigrading only the first 'take' argument is used, others are copied.
69+
* *preserve:
70+
which of the specified values should be preserved. Must be one of
71+
"start_size", "end_size" or "c2c_expansion". The last is default and will produce
72+
regular simpleGrading with 3 values for each axis. When start or end size
73+
is to be kept, grading will switch to edgeGrading so that cells on each edge
74+
will stay consistent start/end size regardless of edge length.
7575
* *length_ratio:
7676
in case the block is graded using multiple gradings, specify
7777
length of current division; see

src/classy_blocks/types.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class ChopArgs(TypedDict, total=False):
3939
count: int
4040
end_size: float
4141
total_expansion: float
42-
invert: bool
4342
preserve: ChopPreserveType
4443

4544

tests/test_grading.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
import numpy as np
34
from parameterized import parameterized
45

56
from classy_blocks.grading import relations as rel
@@ -111,25 +112,27 @@ def test_add_division_1(self):
111112
self.g.length = 2
112113

113114
self.g.add_chop(Chop(length_ratio=0.5, start_size=0.1, c2c_expansion=1.1))
114-
self.g.add_chop(Chop(length_ratio=0.5, start_size=0.1, c2c_expansion=1.1, invert=True))
115+
self.g.add_chop(Chop(length_ratio=0.5, end_size=0.1, c2c_expansion=1 / 1.1))
115116

116-
self.assertListEqual(self.g.specification, [[0.5, 8, 1.9487171000000012], [0.5, 8, 0.5131581182307065]])
117+
np.testing.assert_almost_equal(
118+
self.g.specification, [[0.5, 8, 1.9487171000000012], [0.5, 8, 0.5131581182307065]]
119+
)
117120

118121
def test_add_division_2(self):
119122
"""single grading, set c2c_expansion and count"""
120123
self.g.add_chop(Chop(1, c2c_expansion=1.1, count=10))
121-
self.assertListEqual(self.g.specification, [[1, 10, 2.357947691000002]])
124+
np.testing.assert_almost_equal(self.g.specification, [[1, 10, 2.357947691000002]])
122125

123126
def test_add_division_3(self):
124127
"""single grading, set count and start_size"""
125128
self.g.add_chop(Chop(1, count=10, start_size=0.05))
126129

127-
self.assertListEqual(self.g.specification, [[1, 10, 3.433788027752166]])
130+
np.testing.assert_almost_equal(self.g.specification, [[1, 10, 3.433788027752166]])
128131

129132
def test_add_division_inverted(self):
130133
"""Inverted chop, different result"""
131-
self.g.add_chop(Chop(0.5, count=10, start_size=0.05, invert=False))
132-
self.g.add_chop(Chop(0.5, count=10, start_size=0.05, invert=True))
134+
self.g.add_chop(Chop(0.5, count=10, start_size=0.05))
135+
self.g.add_chop(Chop(0.5, count=10, end_size=0.05))
133136

134137
self.assertAlmostEqual(self.g.specification[0][2], 1 / self.g.specification[1][2])
135138

tests/test_optimize/test_grid.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Set
2-
31
import numpy as np
42
from parameterized import parameterized
53

@@ -132,7 +130,7 @@ def test_fixed_points(self):
132130
]
133131

134132
grid = QuadGrid(positions, indexes)
135-
fixed_points: Set[int] = set()
133+
fixed_points = set()
136134

137135
for cell in grid.cells:
138136
fixed_points.update(cell.boundary)

0 commit comments

Comments
 (0)