13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 | class ArchSuspensionBridgeGenerator(BaseGenerator):
"""Randomly samples and form-finds an arch or suspension bridge with one or two main cables/arches.
The deck and cables/arches are each built as a set of CEM trail edges
(see [mpcem][torch_structure.mixins.cem.CEMMixin.mpcem]), connected by
deviation edges, and equilibrated with MP-CEM. Samples that are too
wide/tall, have overly sharp trail angles, or excessive force density
are rejected via [InvalidSampleError][torch_structure.generators.base_generator.InvalidSampleError].
"""
def __init__(self, **overrides):
super().__init__(**overrides)
self.max_attempts = 1000
self.node_attrs = {
"coords": torch.empty((0, 3), dtype=torch.float),
"load": torch.empty((0, 3), dtype=torch.float),
"support_condition": torch.empty((0, 3), dtype=torch.long),
"is_origin_node": torch.empty((0, 1), dtype=torch.bool),
"constraint_plane": torch.empty((0, 6), dtype=torch.float),
"sequence": torch.empty((0, 1), dtype=torch.long),
"deck_slope": torch.empty((0, 1), dtype=torch.float),
"is_deck_node": torch.empty((0, 1), dtype=torch.bool),
"is_left_side_node": torch.empty((0, 1), dtype=torch.bool),
"is_right_side_node": torch.empty((0, 1), dtype=torch.bool),
"is_center_node": torch.empty((0, 1), dtype=torch.bool),
}
self.edge_attrs = {
"force": torch.empty((0, 1), dtype=torch.float),
"length": torch.empty((0, 1), dtype=torch.float),
"is_trail_edge": torch.empty((0, 1), dtype=torch.bool),
"force_sign": torch.empty((0, 1), dtype=torch.float),
"is_mod_v": torch.empty((0, 1), dtype=torch.bool),
"is_mod_h": torch.empty((0, 1), dtype=torch.bool),
"is_deck_trail_edge": torch.empty((0, 1), dtype=torch.bool),
"is_center_deviation_edge": torch.empty((0, 1), dtype=torch.bool),
}
self.default_attrs = {
"force": torch.tensor([torch.nan]),
"length": torch.tensor([torch.nan]),
"coords": torch.full((3,), torch.nan),
"load": torch.zeros(3, dtype=torch.float),
"support_condition": torch.zeros(3, dtype=torch.bool),
"is_origin_node": torch.tensor(0, dtype=torch.bool),
"constraint_plane": torch.full((6,), torch.nan),
"is_mod_v": torch.tensor(0, dtype=torch.bool),
"is_mod_h": torch.tensor(0, dtype=torch.bool),
"is_deck_trail_edge": torch.tensor(0, dtype=torch.bool),
"is_center_deviation_edge": torch.tensor(0, dtype=torch.bool),
"is_deck_node": torch.tensor(0, dtype=torch.bool),
"is_left_side_node": torch.tensor(0, dtype=torch.bool),
"is_right_side_node": torch.tensor(0, dtype=torch.bool),
"is_center_node": torch.tensor(0, dtype=torch.bool),
}
def validate_input(
self,
span,
n_cables,
n_bays,
midspan_height,
deck_width,
inter_cable_distance,
twist,
connected_cables,
inclined_cables,
deck_force,
cable_force,
inter_cable_force,
deck_rise,
):
"""Validate that ``n_cables`` is either 1 or 2; raises [InvalidSampleError][torch_structure.generators.base_generator.InvalidSampleError] otherwise."""
if n_cables not in [1, 2]:
raise InvalidSampleError("Number of cables must be either 1 or 2.")
def sample_input(
self,
span=None,
n_cables=None,
n_bays=None,
midspan_height=None,
deck_width=None,
inter_cable_distance=None,
twist=None,
connected_cables=None,
inclined_cables=None,
deck_force=None,
cable_force=None,
inter_cable_force=None,
deck_rise=None,
):
"""Sample any unspecified generation parameters, deriving dependent defaults where relevant.
Each parameter defaults to ``None``, meaning "sample randomly"; an
explicit value passed via ``overrides`` (see
[BaseGenerator][torch_structure.generators.base_generator.BaseGenerator])
is left unchanged. Returns a dict of all resolved parameters, to be
passed to [generate][torch_structure.generators.arch_suspension_bridge.ArchSuspensionBridgeGenerator.generate].
"""
if cable_force is None:
cable_force_sign = np.random.choice([-1, 1])
cable_force = cable_force_sign * np.random.uniform(3.0, 15.0)
if span is None:
span = np.random.uniform(40.0, 100.0)
if n_cables is None:
n_cables = np.random.choice([1, 2], p=[0.5, 0.5])
if n_bays is None:
n_bays = np.random.randint(math.ceil(0.5 * span / 7), math.floor(0.5 * span / 3) + 1)
if midspan_height is None:
midspan_height = span * np.sign(cable_force) * np.random.uniform(-0.2, 0.05)
if deck_width is None:
deck_width = np.random.uniform(2.0, 5.0)
if twist is None:
twisted = np.random.choice([False, True], p=[0.6, 0.4])
twist = twisted * np.random.uniform(5.0, 20.0)
if connected_cables is None:
connected_cables = np.random.choice([False, True], p=[0.3, 0.7]) if not twisted else 1
if inclined_cables is None:
if n_cables == 1:
inclined_cables = False
else:
inclined_cables = np.random.choice([False, True], p=[0.3, 0.7]) if not twisted and not connected_cables else 1
if inter_cable_distance is None:
inter_cable_distance = np.random.uniform(3.0, 20.0) if inclined_cables else deck_width
if deck_force is None:
deck_force = np.random.uniform(-3.0, -1.5)
if inter_cable_force is None:
inter_cable_force = np.random.uniform(-1.0, 1.0)
if deck_rise is None:
deck_rise = np.random.choice([0.0, np.random.uniform(2.0, 12.0)])
return {
"span": span,
"n_cables": n_cables,
"n_bays": n_bays,
"midspan_height": midspan_height,
"deck_width": deck_width,
"inter_cable_distance": inter_cable_distance,
"twist": twist,
"connected_cables": connected_cables,
"inclined_cables": inclined_cables,
"deck_force": deck_force,
"cable_force": cable_force,
"inter_cable_force": inter_cable_force,
"deck_rise": deck_rise,
}
def generate(
self,
span,
n_cables,
n_bays,
midspan_height,
deck_width,
inter_cable_distance,
twist,
connected_cables,
inclined_cables,
deck_force,
cable_force,
inter_cable_force,
deck_rise,
):
"""Build and form-find the bridge from the given (fully resolved) parameters.
Constructs the deck and cable/arch trails and their deviation
edges, runs MP-CEM equilibration, and rejects the sample (via
[InvalidSampleError][torch_structure.generators.base_generator.InvalidSampleError]) if the resulting geometry is too wide,
too tall, has overly sharp trail angles, or excessive force
density.
Returns:
tuple[StructData, dict]: the form-found bridge, and a dict of
descriptive text labels (typology, span, dimensions, etc.).
"""
# Initialize data object
data = StructData(
node_attrs=self.node_attrs,
edge_attrs=self.edge_attrs,
default_attrs=self.default_attrs,
)
# Global parameters
bay_size = 0.5 * span / (n_bays + 0.5)
line_load = 0.5
load_mag = line_load * span / (2 * (2 * n_bays + 2))
load = torch.tensor([0.0, 0.0, -load_mag], dtype=torch.float)
twist_rad = math.radians(twist)
twist_offset = 0.5 * bay_size * math.tan(twist_rad)
# Origin nodes
deck_origin_coords = torch.tensor([
[0.5*bay_size, 0.5*deck_width, 0.0],
[0.5*bay_size, -0.5*deck_width, 0.0],
[-0.5*bay_size, 0.5*deck_width, 0.0],
[-0.5*bay_size, -0.5*deck_width, 0.0],
], dtype=torch.float)
profile_side = ["right", "left", "right", "left"]
if n_cables == 1:
cable_origin_coords = torch.tensor([
[0.5*bay_size, twist_offset, midspan_height],
[-0.5*bay_size, -twist_offset, midspan_height],
], dtype=torch.float)
profile_side += ["center", "center"]
elif n_cables == 2:
cable_origin_coords = torch.tensor([
[0.5*bay_size, 0.5*inter_cable_distance + twist_offset, midspan_height],
[0.5*bay_size, -0.5*inter_cable_distance + twist_offset, midspan_height],
[-0.5*bay_size, 0.5*inter_cable_distance - twist_offset, midspan_height],
[-0.5*bay_size, -0.5*inter_cable_distance - twist_offset, midspan_height],
], dtype=torch.float)
profile_side += ["right", "left", "right", "left"]
origin_coords = torch.cat([deck_origin_coords, cable_origin_coords], dim=0)
for i, origin_coord in enumerate(origin_coords):
side = 1 if origin_coord[0] > 0 else -1
is_deck = i < 4
data.add_node(
f"trail_{i}_node_0",
coords=origin_coord,
is_origin_node=torch.tensor(True),
load=load if is_deck else torch.tensor([0.0, 0.0, 0.0]),
sequence=torch.tensor([0], dtype=torch.long),
deck_slope=torch.tensor(side*8*deck_rise/span * 1 / (n_bays + 0.5)),
is_deck_node=torch.tensor(is_deck),
is_left_side_node=torch.tensor(profile_side[i] == "left"),
is_right_side_node=torch.tensor(profile_side[i] == "right"),
is_center_node=torch.tensor(profile_side[i] == "center"),
)
for j in range(n_bays):
data.add_node(
f"trail_{i}_node_{j+1}",
load=load if is_deck else torch.tensor([0.0, 0.0, 0.0]),
support_condition=torch.tensor([True, True, True]) if j == n_bays - 1 else torch.tensor([False, False, False]),
constraint_plane=torch.tensor([side*(j + 1.5)*bay_size, 0.0, 0.0, 1.0, 0.0, 0.0]),
sequence=torch.tensor([j + 1], dtype=torch.long),
deck_slope=torch.tensor(side*8*deck_rise/span * (j + 2) / (n_bays + 0.5)),
is_deck_node=torch.tensor(is_deck),
is_left_side_node=torch.tensor(profile_side[i] == "left"),
is_right_side_node=torch.tensor(profile_side[i] == "right"),
is_center_node=torch.tensor(profile_side[i] == "center"),
)
# Trail edges
data.add_edge(
f"trail_{i}_node_{j}",
f"trail_{i}_node_{j+1}",
is_trail_edge=torch.tensor(True),
is_deck_trail_edge=torch.tensor(is_deck),
)
# Deviation edges
center_deck_pairs = [(0, 2), (1, 3)]
inter_deck_pairs = [(0, 1), (2, 3)]
if n_cables == 1:
center_cable_pairs = [(4, 5)]
inter_cable_pairs = []
deck_cable_pairs = [(0, 4), (1, 4), (2, 5), (3, 5)]
elif n_cables == 2:
center_cable_pairs = [(4, 6), (5, 7)]
inter_cable_pairs = [(4, 5), (6, 7)]
deck_cable_pairs = [(0, 4), (1, 5), (2, 6), (3, 7)]
# Center deck edges
for pair in center_deck_pairs:
data.add_edge(
f"trail_{pair[0]}_node_0",
f"trail_{pair[1]}_node_0",
is_trail_edge=torch.tensor(False),
force=deck_force,
is_center_deviation_edge=torch.tensor(True),
)
# Center cable edges
for pair in center_cable_pairs:
data.add_edge(
f"trail_{pair[0]}_node_0",
f"trail_{pair[1]}_node_0",
is_trail_edge=torch.tensor(False),
force=cable_force,
is_center_deviation_edge=torch.tensor(True),
)
temp_force = 0.0
for i in range(n_bays):
# Inter-deck edges
for pair in inter_deck_pairs:
data.add_edge(
f"trail_{pair[0]}_node_{i}",
f"trail_{pair[1]}_node_{i}",
is_trail_edge=torch.tensor(False),
is_mod_h=torch.tensor(True),
force=temp_force,
)
# Deck-cable edges
for pair in deck_cable_pairs:
data.add_edge(
f"trail_{pair[0]}_node_{i}",
f"trail_{pair[1]}_node_{i}",
is_trail_edge=torch.tensor(False),
is_mod_v=torch.tensor(True),
force=temp_force,
)
# Inter-cable edges
if connected_cables:
for pair in inter_cable_pairs:
data.add_edge(
f"trail_{pair[0]}_node_{i}",
f"trail_{pair[1]}_node_{i}",
is_trail_edge=torch.tensor(False),
force=inter_cable_force,
)
# Formfinding
callback = partial(constrained_deck_cb,
cem_edge_index=data.cem_edge_index,
is_mod_v=data.is_mod_v[data.cem_edge_mask],
is_mod_h=data.is_mod_h[data.cem_edge_mask],
is_deck_trail_edge=data.is_deck_trail_edge[data.cem_edge_mask],
sequence=data.sequence,
deck_slope=data.deck_slope)
data = data.mpcem(callback=callback, verbose=False)
# bbox filter
_, y_extent, z_extent = (data.bbox[1] - data.bbox[0]).unbind(dim=0)
y_factor = 0.8
z_factor = 0.5
if y_extent > span * y_factor:
# print("bbox (wide)")
raise InvalidSampleError(f"Bridge geometry is too wide ({y_extent.item()} > {span * y_factor}).")
if z_extent > span * z_factor:
# print("bbox (tall)")
raise InvalidSampleError(f"Bridge geometry is too tall ({z_extent.item()} > {span * z_factor}).")
# # smoothness filter
max_angle = 35 # degrees
edge_mask = (data.is_trail_edge.view(-1) | data.is_center_deviation_edge.view(-1))
src, dst = data.edge_index[:, edge_mask]
unit_vector = line_direction(data.coords[src], data.coords[dst])
vector_sum = scatter(unit_vector, src, dim=0, reduce="sum", dim_size=data.num_nodes)
degree = scatter(torch.ones_like(src), src, dim=0, reduce="sum", dim_size=data.num_nodes)
degree_2_mask = degree == 2
R2 = vector_sum[degree_2_mask].pow(2).sum(dim=1)
cos_theta = R2 * 0.5 - 1
cos_theta = cos_theta.clamp(-1.0, 1.0)
cos_min = torch.cos(torch.deg2rad(torch.tensor(180 - max_angle, dtype=torch.float)))
is_invalid = cos_theta > cos_min
if is_invalid.any():
# print("Invalid nodes:", is_invalid.sum())
# print("vector sums:", vector_sum[degree_2_mask][is_invalid])
# print("smoothness")
raise InvalidSampleError(f"Trails have too sharp angles (>{max_angle}°).")
# # force density filter
data.force_density = data.force / data.length_from_coords
abs_force_density = data.force_density.abs()
# if abs_force_density[~edge_mask].max() > abs_force_density[edge_mask].min():
# # print("force density")
# raise InvalidSampleError("Some secondary edges have higher force density magnitude than deck/main cable edges.")
# # force density magnitude filter
mag_threshold = 30
if abs_force_density.max() > mag_threshold:
raise InvalidSampleError(f"Some edges have too high force density magnitude (>{mag_threshold}).")
# Text labels
typology = "suspension_bridge" if cable_force > 0 else "arch_bridge"
if typology == "suspension_bridge" and data.coords[:, 2].max().item() < 0.05*span:
typology = "inverted_arch"
if typology == "suspension_bridge":
typology_extended = "underspanned" if midspan_height < 0 else None
elif typology == "arch_bridge":
typology_extended = "deck" if midspan_height < 0 else "through"
else:
typology_extended = None
arch_rise_or_cable_sag = data.coords[~data.is_deck_node.view(-1), 2].max().item() - data.coords[~data.is_deck_node.view(-1), 2].min().item()
text_label_dict = {
"typology": typology,
"typology_extended": typology_extended,
"n_cables_or_arches": int(n_cables),
"inclined_cables_or_arches": bool(inclined_cables),
"cables_or_arches_connected": connected_cables,
"arch_rise_or_cable_sag": arch_rise_or_cable_sag,
"n_columns_or_hangers": 4*n_bays,
"span": span,
"deck_width": deck_width,
"twisted": bool(twist > 1e-3),
"arched_deck": bool(deck_rise > 1e-3),
}
data.topology_params = torch.tensor(
[1,
-100,
-100,
-100,
-100,
-100,
n_cables - 1,
n_bays,
int(connected_cables) if n_cables > 1 else -100], dtype=torch.long).view(1, -1)
return data, text_label_dict
|