Skip to content

truss_bridge

TrussBridgeGenerator

Bases: BaseGenerator

Randomly samples and analyzes a Pratt, Howe, or Parker truss bridge (deck or through, triangulated or not).

Unlike the CEM-based bridge generators, the truss's force densities are solved directly by least squares from the equilibrium matrix; if analysis is enabled, the resulting geometry and equilibrium are verified and rejected via InvalidSampleError if inconsistent.

Source code in src/torch_structure/generators/truss_bridge.py
 10
 11
 12
 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
class TrussBridgeGenerator(BaseGenerator):
    """Randomly samples and analyzes a Pratt, Howe, or Parker truss bridge (deck or through, triangulated or not).

    Unlike the CEM-based bridge generators, the truss's force densities are
    solved directly by least squares from the equilibrium matrix; if
    ``analysis`` is enabled, the resulting geometry and equilibrium are
    verified and rejected via
    [InvalidSampleError][torch_structure.generators.base_generator.InvalidSampleError]
    if inconsistent.
    """

    def __init__(self, analysis=True, **overrides):
        super().__init__(**overrides)
        self.max_attempts = 100
        self.analysis = analysis

        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.bool),
            "sequence": torch.empty((0, 1), dtype=torch.long),
            "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.default_attrs = {
            "coords": torch.full((3,), torch.nan),
            "load": torch.zeros(3, dtype=torch.float),
            "support_condition": torch.zeros(3, 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),
        }
        self.cat_dict = {
            0: {"generator": {
                0: "truss",
                1: "arch_susp"
            }},
            # if truss
            1: {"truss_type": {
                0: "pratt",
                1: "howe",
                2: "parker"
            }},
            2: {"triangle": {
                0: False,
                1: True
            }},
            # if not triangle
            3: {"deck_truss": {
                0: False,
                1: True
            }},
            4: {"inclined": {
                0: False,
                1: True
            }},
            # endif not triangle
            5: {"n_bays_truss": {
                i: i for i in range(30)
            }},
            # endif truss
            # if arch_susp
            6: {"n_cables": {
                0: 1,
                1: 2,
            }},
            7: {"n_bays_arch_susp": {
                i: i for i in range(30)
            }},
            # if 2 cables
            8: {"connected_cables": {
                0: False,
                1: True
            }}
        }

    def validate_input(
        self,
        span,
        deck_truss,
        triangle,
        n_bays,
        truss_height,
        deck_width,
        inclined_truss,
        truss_inclination,
        truss_type,
    ):
        """Validate that ``truss_type`` is one of 'pratt', 'howe', or 'parker'; raises ``ValueError`` otherwise."""
        if truss_type not in ["pratt", "howe", "parker"]:
            raise ValueError("Truss type must be either 'pratt', 'howe', or 'parker'.")

    def sample_input(
        self,
        span=None,
        deck_truss=None,
        triangle=None,
        n_bays=None,
        truss_height=None,
        deck_width=None,
        inclined_truss=None,
        truss_inclination=None,
        truss_type=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.truss_bridge.TrussBridgeGenerator.generate].
        """
        if span is None:
            span = np.random.uniform(40.0, 80.0)
        if deck_truss is None:
            deck_truss = np.random.choice([True, False], p=[0.5, 0.5])
        if triangle is None:
            if deck_truss:
                triangle = np.random.choice([True, False], p=[0.3, 0.7])
            else:
                triangle = False
        if deck_width is None:
            deck_width = np.random.uniform(2.0, 5.0)
        if truss_height is None:
            truss_height = np.random.uniform(0.1, 0.2) * span * (deck_width/5)
        if n_bays is None:
            n_bays = np.random.randint(int(span / truss_height + 0.5) - 1, int(span / truss_height + 0.5) + 5) // 2
        if inclined_truss is None:
            if triangle:
                inclined_truss = False
            else:
                inclined_truss = np.random.choice([True, False], p=[0.7, 0.3])
        if truss_inclination is None:
            truss_inclination = np.random.uniform(-5.0, 30.0) if inclined_truss else 0.0
        if truss_type is None:
            truss_type = np.random.choice(["pratt", "howe", "parker"], p=[1/3, 1/3, 1/3])

        return {
            "span": span,
            "deck_truss": deck_truss,
            "triangle": triangle,
            "n_bays": n_bays,
            "truss_height": truss_height,
            "deck_width": deck_width,
            "inclined_truss": inclined_truss,
            "truss_inclination": truss_inclination,
            "truss_type": truss_type,
        }

    def generate(
        self,
        span,
        deck_truss,
        triangle,
        n_bays,
        truss_height,
        deck_width,
        inclined_truss,
        truss_inclination,
        truss_type,
    ):
        """Build the truss geometry and, if ``self.analysis`` is enabled, solve and verify its equilibrium.

        Returns:
            tuple[StructData, dict]: the (optionally analyzed) truss graph,
            and a dict of descriptive text labels (typology, truss type,
            dimensions, etc.).

        Raises:
            InvalidSampleError: if analysis is enabled and the solved
                geometry is not in equilibrium or is inconsistent with the
                sampled coordinates.
        """
        # Initialize data object
        data = StructData(
            node_attrs=self.node_attrs,
            default_attrs=self.default_attrs,
        )

        # Global parameters
        bay_size = 0.5 * span / n_bays
        line_load = 0.5
        load_mag = line_load * span / (2 * (2 * n_bays + 1))
        load = torch.tensor([0.0, 0.0, -load_mag], dtype=torch.float)
        if deck_truss:
            chord_z = -truss_height
        else:
            chord_z = truss_height
        if inclined_truss:
            chord_y_offset = truss_height * math.tan(math.radians(truss_inclination))
        else:
            chord_y_offset = 0.0
        # center nodes
        deck_center_coords = torch.tensor([
            [0.0, 0.5*deck_width, 0.0],
            [0.0, -0.5*deck_width, 0.0],
        ], dtype=torch.float)
        profile_side = ["right", "left"]

        if triangle:
            chord_center_coords = torch.tensor([
                [0.0, 0.0, chord_z],
            ], dtype=torch.float)
            profile_side.append("center")
        else:
            chord_center_coords = torch.tensor([
                [0.0, 0.5*deck_width + chord_y_offset, chord_z],
                [0.0, -0.5*deck_width - chord_y_offset, chord_z],
            ], dtype=torch.float)
            profile_side.extend(["right", "left"])

        center_coords = torch.cat([deck_center_coords, chord_center_coords], dim=0)

        # chords
        for i, center_coord in enumerate(center_coords):
            is_deck = i < 2
            data.add_node(
                f"trail_{i}_node_0",
                coords=center_coord,
                load=load if is_deck else torch.tensor([0.0, 0.0, 0.0]),
                sequence=torch.tensor([0], dtype=torch.long),
                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 side in [-1, 1]:
                for j in range(n_bays):
                    if not is_deck and j == n_bays - 1:
                        if truss_type == "parker":
                            continue
                        elif truss_type == "pratt" and deck_truss:
                            continue
                        elif truss_type == "howe" and not deck_truss:
                            continue
                    coord = center_coord + torch.tensor([side*(j + 1)*bay_size, 0.0, 0.0])
                    if truss_type == "parker" and not is_deck:
                        z_shift = math.copysign(3 * truss_height / span**2 * coord[0]**2, chord_z)
                        coord[2] -= z_shift
                        if inclined_truss and not triangle:
                            factor = 1 if profile_side[i] == "right" else -1
                            coord[1] -= factor * z_shift * math.tan(math.radians(truss_inclination))

                    if j == n_bays - 1 and is_deck:
                        y_fixed = (profile_side[i] == "left" and side == 1)
                        y_fixed = True
                        support_condition = torch.tensor([True, y_fixed, True]) if side == -1 else torch.tensor([False, y_fixed, True])
                    else:
                        support_condition = torch.tensor([False, False, False])
                    data.add_node(
                        f"trail_{i}_node_{j+1}_side_{side}",
                        load=load if is_deck else torch.tensor([0.0, 0.0, 0.0]),
                        support_condition=support_condition,
                        coords=coord,
                        sequence=torch.tensor([j + 1], dtype=torch.long),
                        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}_side_{side}" if j > 0 else f"trail_{i}_node_0",
                        f"trail_{i}_node_{j+1}_side_{side}",
                    )

        # diagonals and verticals
        if triangle:
            chord_pairs = [(0, 2), (1, 2)]
        else:
            chord_pairs = [(0, 2), (1, 3)]

        for pair in chord_pairs:
            for side in [-1, 1]:
                for j in range(n_bays):
                    index_0 = j
                    index_1 = j + 1
                    if deck_truss:
                        index_0, index_1 = index_1, index_0
                    if truss_type in ["pratt", "parker"]:
                        if truss_type == "parker" and not deck_truss and j == n_bays - 1:
                            data.add_edge(
                                f"trail_{pair[0]}_node_{index_1}_side_{side}" if index_1 > 0 else f"trail_{pair[0]}_node_0",
                                f"trail_{pair[1]}_node_{index_0}_side_{side}" if index_0 > 0 else f"trail_{pair[1]}_node_0",
                            )
                        else:
                            data.add_edge(
                                f"trail_{pair[0]}_node_{index_0}_side_{side}" if index_0 > 0 else f"trail_{pair[0]}_node_0",
                                f"trail_{pair[1]}_node_{index_1}_side_{side}" if index_1 > 0 else f"trail_{pair[1]}_node_0",
                            )
                    if truss_type == "howe":
                        data.add_edge(
                            f"trail_{pair[0]}_node_{index_1}_side_{side}" if index_1 > 0 else f"trail_{pair[0]}_node_0",
                            f"trail_{pair[1]}_node_{index_0}_side_{side}" if index_0 > 0 else f"trail_{pair[1]}_node_0",
                        )
                    if j == n_bays - 1:
                        if truss_type == "parker":
                            continue
                        elif truss_type == "pratt" and deck_truss:
                            continue
                        elif truss_type == "howe" and not deck_truss:
                            continue
                    data.add_edge(
                        f"trail_{pair[0]}_node_{j+1}_side_{side}",
                        f"trail_{pair[1]}_node_{j+1}_side_{side}",
                    )
            data.add_edge(
                f"trail_{pair[0]}_node_0",
                f"trail_{pair[1]}_node_0",
            )

        if inclined_truss or triangle:
            for side in [-1, 1]:
                for i in range(n_bays - 1):
                    # Inter-deck edges
                    data.add_edge(
                        f"trail_0_node_{i+1}_side_{side}",
                        f"trail_1_node_{i+1}_side_{side}",
                    )
                data.add_edge(
                    "trail_0_node_0",
                    "trail_1_node_0",
                )

        # Analysis
        if self.analysis:
            support = data.support_condition
            load = data.load

            coords = torch.clone(data.coords)  # check if this is necessary
            C = create_branch_node_matrix(data.directed_edge_index)

            CxT = C[:, ~support[:, 0]].T   # (n_free_x, E)
            CyT = C[:, ~support[:, 1]].T   # (n_free_y, E)
            CzT = C[:, ~support[:, 2]].T   # (n_free_z, E)

            u = torch.mv(C, coords[:, 0])
            v = torch.mv(C, coords[:, 1])
            w = torch.mv(C, coords[:, 2])
            U = torch.diag(u)
            V = torch.diag(v)
            W = torch.diag(w)

            A = torch.vstack((
                CxT @ U,
                CyT @ V,
                CzT @ W
            ))

            b = torch.hstack((load[~support[:, 0], 0], load[~support[:, 1], 1], load[~support[:, 2], 2]))

            force_density = torch.linalg.lstsq(A, b).solution

            data.force_density = data.edge_attr_to_undirected(force_density.view(-1, 1), mask=data.directed_mask)
            data.force = data.force_density * data.length_from_coords
            if not data.verify_equilibrium():
                raise InvalidSampleError("Equilibrium not found.")

            prev_coords = torch.clone(data.coords)
            data = data.fdm()
            if (data.coords - prev_coords).abs().max() > 1e-4:
                # print("Inconsistent geometry detected.")
                # data.plot()
                # data.plot(show=True, force=None, coords=prev_coords)
                raise InvalidSampleError("Inconsistent geometry.")


        # Text labels
        typology = "deck_truss" if deck_truss else "through_truss"
        text_label_dict = {
            "typology": typology,
            "truss_type": str(truss_type),
            "triangle": bool(triangle),
            "n_bays": 2*n_bays,
            "inclined": bool(inclined_truss),
            "inclination": truss_inclination,
            "span": span,
            "deck_width": deck_width,
        }

        data.topology_params = torch.tensor(
            [0,
             {"pratt": 0, "howe": 1, "parker": 2}[truss_type],
             int(triangle),
             int(deck_truss) if not triangle else -100,
             int(inclined_truss) if not triangle else -100,
             n_bays,
             -100,
             -100,
             -100], dtype=torch.long).view(1, -1)

        return data, text_label_dict

generate(span, deck_truss, triangle, n_bays, truss_height, deck_width, inclined_truss, truss_inclination, truss_type)

Build the truss geometry and, if self.analysis is enabled, solve and verify its equilibrium.

Returns:

Type Description

tuple[StructData, dict]: the (optionally analyzed) truss graph,

and a dict of descriptive text labels (typology, truss type,

dimensions, etc.).

Raises:

Type Description
InvalidSampleError

if analysis is enabled and the solved geometry is not in equilibrium or is inconsistent with the sampled coordinates.

Source code in src/torch_structure/generators/truss_bridge.py
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
def generate(
    self,
    span,
    deck_truss,
    triangle,
    n_bays,
    truss_height,
    deck_width,
    inclined_truss,
    truss_inclination,
    truss_type,
):
    """Build the truss geometry and, if ``self.analysis`` is enabled, solve and verify its equilibrium.

    Returns:
        tuple[StructData, dict]: the (optionally analyzed) truss graph,
        and a dict of descriptive text labels (typology, truss type,
        dimensions, etc.).

    Raises:
        InvalidSampleError: if analysis is enabled and the solved
            geometry is not in equilibrium or is inconsistent with the
            sampled coordinates.
    """
    # Initialize data object
    data = StructData(
        node_attrs=self.node_attrs,
        default_attrs=self.default_attrs,
    )

    # Global parameters
    bay_size = 0.5 * span / n_bays
    line_load = 0.5
    load_mag = line_load * span / (2 * (2 * n_bays + 1))
    load = torch.tensor([0.0, 0.0, -load_mag], dtype=torch.float)
    if deck_truss:
        chord_z = -truss_height
    else:
        chord_z = truss_height
    if inclined_truss:
        chord_y_offset = truss_height * math.tan(math.radians(truss_inclination))
    else:
        chord_y_offset = 0.0
    # center nodes
    deck_center_coords = torch.tensor([
        [0.0, 0.5*deck_width, 0.0],
        [0.0, -0.5*deck_width, 0.0],
    ], dtype=torch.float)
    profile_side = ["right", "left"]

    if triangle:
        chord_center_coords = torch.tensor([
            [0.0, 0.0, chord_z],
        ], dtype=torch.float)
        profile_side.append("center")
    else:
        chord_center_coords = torch.tensor([
            [0.0, 0.5*deck_width + chord_y_offset, chord_z],
            [0.0, -0.5*deck_width - chord_y_offset, chord_z],
        ], dtype=torch.float)
        profile_side.extend(["right", "left"])

    center_coords = torch.cat([deck_center_coords, chord_center_coords], dim=0)

    # chords
    for i, center_coord in enumerate(center_coords):
        is_deck = i < 2
        data.add_node(
            f"trail_{i}_node_0",
            coords=center_coord,
            load=load if is_deck else torch.tensor([0.0, 0.0, 0.0]),
            sequence=torch.tensor([0], dtype=torch.long),
            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 side in [-1, 1]:
            for j in range(n_bays):
                if not is_deck and j == n_bays - 1:
                    if truss_type == "parker":
                        continue
                    elif truss_type == "pratt" and deck_truss:
                        continue
                    elif truss_type == "howe" and not deck_truss:
                        continue
                coord = center_coord + torch.tensor([side*(j + 1)*bay_size, 0.0, 0.0])
                if truss_type == "parker" and not is_deck:
                    z_shift = math.copysign(3 * truss_height / span**2 * coord[0]**2, chord_z)
                    coord[2] -= z_shift
                    if inclined_truss and not triangle:
                        factor = 1 if profile_side[i] == "right" else -1
                        coord[1] -= factor * z_shift * math.tan(math.radians(truss_inclination))

                if j == n_bays - 1 and is_deck:
                    y_fixed = (profile_side[i] == "left" and side == 1)
                    y_fixed = True
                    support_condition = torch.tensor([True, y_fixed, True]) if side == -1 else torch.tensor([False, y_fixed, True])
                else:
                    support_condition = torch.tensor([False, False, False])
                data.add_node(
                    f"trail_{i}_node_{j+1}_side_{side}",
                    load=load if is_deck else torch.tensor([0.0, 0.0, 0.0]),
                    support_condition=support_condition,
                    coords=coord,
                    sequence=torch.tensor([j + 1], dtype=torch.long),
                    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}_side_{side}" if j > 0 else f"trail_{i}_node_0",
                    f"trail_{i}_node_{j+1}_side_{side}",
                )

    # diagonals and verticals
    if triangle:
        chord_pairs = [(0, 2), (1, 2)]
    else:
        chord_pairs = [(0, 2), (1, 3)]

    for pair in chord_pairs:
        for side in [-1, 1]:
            for j in range(n_bays):
                index_0 = j
                index_1 = j + 1
                if deck_truss:
                    index_0, index_1 = index_1, index_0
                if truss_type in ["pratt", "parker"]:
                    if truss_type == "parker" and not deck_truss and j == n_bays - 1:
                        data.add_edge(
                            f"trail_{pair[0]}_node_{index_1}_side_{side}" if index_1 > 0 else f"trail_{pair[0]}_node_0",
                            f"trail_{pair[1]}_node_{index_0}_side_{side}" if index_0 > 0 else f"trail_{pair[1]}_node_0",
                        )
                    else:
                        data.add_edge(
                            f"trail_{pair[0]}_node_{index_0}_side_{side}" if index_0 > 0 else f"trail_{pair[0]}_node_0",
                            f"trail_{pair[1]}_node_{index_1}_side_{side}" if index_1 > 0 else f"trail_{pair[1]}_node_0",
                        )
                if truss_type == "howe":
                    data.add_edge(
                        f"trail_{pair[0]}_node_{index_1}_side_{side}" if index_1 > 0 else f"trail_{pair[0]}_node_0",
                        f"trail_{pair[1]}_node_{index_0}_side_{side}" if index_0 > 0 else f"trail_{pair[1]}_node_0",
                    )
                if j == n_bays - 1:
                    if truss_type == "parker":
                        continue
                    elif truss_type == "pratt" and deck_truss:
                        continue
                    elif truss_type == "howe" and not deck_truss:
                        continue
                data.add_edge(
                    f"trail_{pair[0]}_node_{j+1}_side_{side}",
                    f"trail_{pair[1]}_node_{j+1}_side_{side}",
                )
        data.add_edge(
            f"trail_{pair[0]}_node_0",
            f"trail_{pair[1]}_node_0",
        )

    if inclined_truss or triangle:
        for side in [-1, 1]:
            for i in range(n_bays - 1):
                # Inter-deck edges
                data.add_edge(
                    f"trail_0_node_{i+1}_side_{side}",
                    f"trail_1_node_{i+1}_side_{side}",
                )
            data.add_edge(
                "trail_0_node_0",
                "trail_1_node_0",
            )

    # Analysis
    if self.analysis:
        support = data.support_condition
        load = data.load

        coords = torch.clone(data.coords)  # check if this is necessary
        C = create_branch_node_matrix(data.directed_edge_index)

        CxT = C[:, ~support[:, 0]].T   # (n_free_x, E)
        CyT = C[:, ~support[:, 1]].T   # (n_free_y, E)
        CzT = C[:, ~support[:, 2]].T   # (n_free_z, E)

        u = torch.mv(C, coords[:, 0])
        v = torch.mv(C, coords[:, 1])
        w = torch.mv(C, coords[:, 2])
        U = torch.diag(u)
        V = torch.diag(v)
        W = torch.diag(w)

        A = torch.vstack((
            CxT @ U,
            CyT @ V,
            CzT @ W
        ))

        b = torch.hstack((load[~support[:, 0], 0], load[~support[:, 1], 1], load[~support[:, 2], 2]))

        force_density = torch.linalg.lstsq(A, b).solution

        data.force_density = data.edge_attr_to_undirected(force_density.view(-1, 1), mask=data.directed_mask)
        data.force = data.force_density * data.length_from_coords
        if not data.verify_equilibrium():
            raise InvalidSampleError("Equilibrium not found.")

        prev_coords = torch.clone(data.coords)
        data = data.fdm()
        if (data.coords - prev_coords).abs().max() > 1e-4:
            # print("Inconsistent geometry detected.")
            # data.plot()
            # data.plot(show=True, force=None, coords=prev_coords)
            raise InvalidSampleError("Inconsistent geometry.")


    # Text labels
    typology = "deck_truss" if deck_truss else "through_truss"
    text_label_dict = {
        "typology": typology,
        "truss_type": str(truss_type),
        "triangle": bool(triangle),
        "n_bays": 2*n_bays,
        "inclined": bool(inclined_truss),
        "inclination": truss_inclination,
        "span": span,
        "deck_width": deck_width,
    }

    data.topology_params = torch.tensor(
        [0,
         {"pratt": 0, "howe": 1, "parker": 2}[truss_type],
         int(triangle),
         int(deck_truss) if not triangle else -100,
         int(inclined_truss) if not triangle else -100,
         n_bays,
         -100,
         -100,
         -100], dtype=torch.long).view(1, -1)

    return data, text_label_dict

sample_input(span=None, deck_truss=None, triangle=None, n_bays=None, truss_height=None, deck_width=None, inclined_truss=None, truss_inclination=None, truss_type=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) is left unchanged. Returns a dict of all resolved parameters, to be passed to generate.

Source code in src/torch_structure/generators/truss_bridge.py
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
def sample_input(
    self,
    span=None,
    deck_truss=None,
    triangle=None,
    n_bays=None,
    truss_height=None,
    deck_width=None,
    inclined_truss=None,
    truss_inclination=None,
    truss_type=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.truss_bridge.TrussBridgeGenerator.generate].
    """
    if span is None:
        span = np.random.uniform(40.0, 80.0)
    if deck_truss is None:
        deck_truss = np.random.choice([True, False], p=[0.5, 0.5])
    if triangle is None:
        if deck_truss:
            triangle = np.random.choice([True, False], p=[0.3, 0.7])
        else:
            triangle = False
    if deck_width is None:
        deck_width = np.random.uniform(2.0, 5.0)
    if truss_height is None:
        truss_height = np.random.uniform(0.1, 0.2) * span * (deck_width/5)
    if n_bays is None:
        n_bays = np.random.randint(int(span / truss_height + 0.5) - 1, int(span / truss_height + 0.5) + 5) // 2
    if inclined_truss is None:
        if triangle:
            inclined_truss = False
        else:
            inclined_truss = np.random.choice([True, False], p=[0.7, 0.3])
    if truss_inclination is None:
        truss_inclination = np.random.uniform(-5.0, 30.0) if inclined_truss else 0.0
    if truss_type is None:
        truss_type = np.random.choice(["pratt", "howe", "parker"], p=[1/3, 1/3, 1/3])

    return {
        "span": span,
        "deck_truss": deck_truss,
        "triangle": triangle,
        "n_bays": n_bays,
        "truss_height": truss_height,
        "deck_width": deck_width,
        "inclined_truss": inclined_truss,
        "truss_inclination": truss_inclination,
        "truss_type": truss_type,
    }

validate_input(span, deck_truss, triangle, n_bays, truss_height, deck_width, inclined_truss, truss_inclination, truss_type)

Validate that truss_type is one of 'pratt', 'howe', or 'parker'; raises ValueError otherwise.

Source code in src/torch_structure/generators/truss_bridge.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def validate_input(
    self,
    span,
    deck_truss,
    triangle,
    n_bays,
    truss_height,
    deck_width,
    inclined_truss,
    truss_inclination,
    truss_type,
):
    """Validate that ``truss_type`` is one of 'pratt', 'howe', or 'parker'; raises ``ValueError`` otherwise."""
    if truss_type not in ["pratt", "howe", "parker"]:
        raise ValueError("Truss type must be either 'pratt', 'howe', or 'parker'.")