|
5 | 5 | from trussme import Truss, Goals, read_json, Pipe, Box, Square, Bar |
6 | 6 |
|
7 | 7 |
|
| 8 | +def _safe_ratio(numerator: float, denominator: float) -> float: |
| 9 | + if denominator == 0.0: |
| 10 | + return 0.0 |
| 11 | + return numerator / denominator |
| 12 | + |
| 13 | + |
8 | 14 | def make_x0( |
9 | 15 | truss: Truss, |
10 | 16 | joint_optimization: Optional[Literal["full"]] = "full", |
@@ -223,21 +229,27 @@ def truss_generator(x: list[float]) -> Truss: |
223 | 229 | shape_name: str = configured_truss.members[i].shape.name() |
224 | 230 | p = configured_truss.members[i].shape._params |
225 | 231 | if shape_name == "pipe": |
| 232 | + thickness_ratio = _safe_ratio(p["t"], p["r"]) |
226 | 233 | configured_truss.members[i].shape = Pipe( |
227 | | - r=x[idx], t=x[idx] * p["t"] / p["r"] |
| 234 | + r=x[idx], t=x[idx] * thickness_ratio |
228 | 235 | ) |
229 | 236 | idx += 1 |
230 | 237 | elif shape_name == "box": |
| 238 | + height_ratio = _safe_ratio(p["h"], p["w"]) |
| 239 | + thickness_ratio = _safe_ratio(p["t"], p["w"]) |
231 | 240 | configured_truss.members[i].shape = Box( |
232 | | - w=x[idx], h=x[idx] * p["h"] / p["w"], t=x[idx] * p["t"] / p["w"] |
| 241 | + w=x[idx], |
| 242 | + h=x[idx] * height_ratio, |
| 243 | + t=x[idx] * thickness_ratio, |
233 | 244 | ) |
234 | 245 | idx += 1 |
235 | 246 | elif shape_name == "bar": |
236 | 247 | configured_truss.members[i].shape = Bar(r=x[idx]) |
237 | 248 | idx += 1 |
238 | 249 | elif shape_name == "square": |
| 250 | + height_ratio = _safe_ratio(p["h"], p["w"]) |
239 | 251 | configured_truss.members[i].shape = Square( |
240 | | - w=x[idx], h=x[idx] * p["h"] / p["w"] |
| 252 | + w=x[idx], h=x[idx] * height_ratio |
241 | 253 | ) |
242 | 254 | idx += 1 |
243 | 255 |
|
|
0 commit comments