File size: 9,464 Bytes
fc7d689
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
"""Form-finding helpers: FDM equilibrium computations.

Implements the Force Density Method (FDM) equilibrium equations for
pin-jointed bar systems.

References
----------
[1] Schek, H.J. (1974). The Force Density Method for form finding and
    computation of general networks. CMAME, 3(1):115-134.
[2] Pastrana, R. et al. (2025). Neural FDM. ICLR 2025. Eq. 1, 8.
"""

import jax.numpy as jnp
from jax_fdm.equilibrium import EquilibriumParametersState as FDParametersState
from jax_fdm.equilibrium import EquilibriumState, LoadState, nodes_load_from_faces

# ===============================================================================
# Load helpers
# ===============================================================================

def calculate_area_loads(x, structure, load):
    """
    Convert area loads into vertex loads.

    Parameters
    ----------
    x: `jax.Array`
        The 3D coordinates of the vertices.
    structure: `jax_fdm.EquilibriumStructure`
        A structure with the discretization of the shape.
    load: `float`
        The vertical load per unit area in the `z` direction.

    Returns
    -------
    vertices_load: `jax.Array`
        The 3D vertex loads.
    """
    x = jnp.reshape(x, (-1, 3))

    # need to convert loads into face loads
    num_faces = structure.num_faces
    faces_load_xy = jnp.zeros(shape=(num_faces, 2))  # (num_faces, xy)
    faces_load_z = jnp.ones(shape=(num_faces, 1)) * load  # (num_faces, xy)
    faces_load = jnp.hstack((faces_load_xy, faces_load_z))

    vertices_load = nodes_load_from_faces(
        x,
        faces_load,
        structure,
        is_local=False
    )

    return vertices_load


def calculate_constant_loads(x, structure, load):
    """
    Create constant vertical vertex loads.

    Parameters
    ----------
    x: `jax.Array`
        The 3D coordinates of the vertices.
    structure: `jax_fdm.EquilibriumStructure`
        A structure with the discretization of the shape.
    load: `float`
        The vertical load per vertex in the `z` direction.

    Returns
    -------
    vertices_load: `jax.Array`
        The 3D vertex loads.
    """
    num_vertices = structure.num_vertices
    # (num_vertices, xy)
    vertices_load_xy = jnp.zeros(shape=(num_vertices, 2))
    # (num_vertices, xy)
    vertices_load_z = jnp.ones(shape=(num_vertices, 1)) * load

    return jnp.hstack((vertices_load_xy, vertices_load_z))


# ===============================================================================
# Form-finding helpers
# ===============================================================================


def edges_vectors(xyz, connectivity):
    """
    Calculate the unnormalized edge directions (nodal coordinate differences).

    Parameters
    ----------
    xyz: `jax.Array`
        The 3D coordinates of the vertices.
    connectivity: `jax.Array`
        The connectivity matrix of the structure.

    Returns
    -------
    vectors: `jax.Array`
        The edge vectors.
    """
    return connectivity @ xyz


def edges_lengths(vectors):
    """
    Compute the length of the edge vectors.

    Parameters
    ----------
    vectors: `jax.Array`
        The edge vectors.

    Returns
    -------
    lengths: `jax.Array`
        The lengths.
    """
    return jnp.linalg.norm(vectors, axis=1, keepdims=True)


def edges_forces(q, lengths):
    """
    Calculate the force in the edges.

    Parameters
    ----------
    q: `jax.Array`
        The force densities.
    lengths: `jax.Array`
        The edge lengths.

    Returns
    -------
    forces: `jax.Array`
        The forces in the edges.
    """
    return jnp.reshape(q, (-1, 1)) * lengths


def vertices_residuals(q, loads, vectors, connectivity):
    """
    Compute the residual forces on the vertices of the structure.

    Parameters
    ----------
    q: `jax.Array`
        The force densities.
    loads: `jax.Array`
        The loads on the vertices.
    vectors: `jax.Array`
        The edge vectors.
    connectivity: `jax.Array`
        The connectivity matrix of the structure.

    Returns
    -------
    residuals: `jax.Array`
        The residual forces on the vertices.
    """
    return loads - connectivity.T @ (q[:, None] * vectors)


def vertices_residuals_from_xyz(q, loads, xyz, structure):
    """
    Compute the residual forces on the vertices of the structure.

    Parameters
    ----------
    q: `jax.Array`
        The force densities.
    loads: `jax.Array`
        The loads on the vertices.
    xyz: `jax.Array`
        The 3D coordinates of the vertices.
    structure: `jax_fdm.EquilibriumStructure`
        A structure with the discretization of the shape.

    Returns
    -------
    residuals: `jax.Array`
        The residual forces on the vertices.
    """
    connectivity = structure.connectivity

    xyz = jnp.reshape(xyz, (-1, 3))
    vectors = edges_vectors(xyz, connectivity)

    return vertices_residuals(q, loads, vectors, connectivity)


def calculate_equilibrium_state(q, xyz, loads_nodes, structure):
    """
    Assembles an equilibrium state object.

    Parameters
    ----------
    q: `jax.Array`
        The force densities.
    xyz: `jax.Array`
        The 3D coordinates of the vertices.
    loads_nodes: `jax.Array`
        The loads on the vertices.
    structure: `jax_fdm.EquilibriumStructure`
        A structure with the discretization of the shape.

    Returns
    -------
    state: `jax_fdm.EquilibriumState`
        The equilibrium state.
    """
    connectivity = structure.connectivity

    vectors = edges_vectors(xyz, connectivity)
    lengths = edges_lengths(vectors)
    residuals = vertices_residuals(q, loads_nodes, vectors, connectivity)
    forces = edges_forces(q, lengths)

    return EquilibriumState(
        xyz=xyz,
        residuals=residuals,
        lengths=lengths,
        forces=forces,
        loads=loads_nodes,
        vectors=vectors
    )


def calculate_fd_params_state(q, xyz_fixed, loads_nodes):
    """
    Assembles an simulation parameters state.

    Parameters
    ----------
    q: `jax.Array`
        The force densities.
    xyz_fixed: `jax.Array`
        The 3D coordinates of the fixed vertices.
    loads_nodes: `jax.Array`
        The loads on the vertices.

    Returns
    -------
    state: `jax_fdm.EquilibriumParametersState`
        The current state of the simulation parameters.
    """
    return FDParametersState(q, xyz_fixed, LoadState(loads_nodes, 0.0, 0.0))


# =============================================================================
# Reaction forces at supports
# =============================================================================


def compute_reactions(q, loads, xyz, structure):
    """
    Compute reaction forces at support (fixed) nodes.

    In equilibrium, the residual at free nodes is zero. At fixed nodes,
    the residual represents the reaction force the support must provide:

        R_i = sum_j K_ij (x_j - x_i) * q_j - P_i

    This is the negative of the residual at fixed nodes.

    Parameters
    ----------
    q : jax.Array (E,)
        Force densities per edge.
    loads : jax.Array (N, 3)
        Applied loads at all vertices.
    xyz : jax.Array (N, 3)
        Vertex positions.
    structure : EquilibriumStructure
        The mesh structure with connectivity.

    Returns
    -------
    reactions : jax.Array (N_fixed, 3)
        Reaction force vectors at each support node.
    indices_fixed : jax.Array (N_fixed,)
        Indices of the fixed (support) nodes.
    """
    # Compute residuals at ALL nodes (not just free)
    residuals_all = vertices_residuals_from_xyz(q, loads, xyz, structure)

    # Reactions are the negative residuals at fixed nodes
    # At equilibrium, free node residuals are ~0
    # At fixed nodes, residuals represent the imbalance that supports carry
    indices_fixed = structure.indices_fixed
    reactions = -residuals_all[indices_fixed]

    return reactions, indices_fixed


def compute_total_reactions(reactions):
    """
    Compute total reaction forces (sum over all supports).

    Parameters
    ----------
    reactions : jax.Array (N_fixed, 3)
        Reaction force vectors per support.

    Returns
    -------
    total : jax.Array (3,)
        Sum of all reactions [Rx, Ry, Rz].
    """
    return jnp.sum(reactions, axis=0)


def compute_l_physics(x_hat, q, loads, structure, free_indices=None):
    """Compute L_physics: Euclidean norm (L2) of residual forces at free nodes.

    Matches the paper's physics loss definition (losses.py compute_error_residual):
    sqrt(sum(residual_vectors_free^2)). This is the standard metric used
    throughout all benchmarks for consistency.

    Parameters
    ----------
    x_hat : jax.Array
        Predicted vertex positions (flat or (N,3)).
    q : jax.Array
        Force densities per edge.
    loads : jax.Array
        Applied loads per vertex (N,3).
    structure : EquilibriumStructure
        Mesh connectivity structure.
    free_indices : array-like, optional
        Indices of free (non-support) nodes. If None, uses all nodes.

    Returns
    -------
    l_physics : float
        Euclidean norm of residual forces at free nodes.
    """
    xyz = jnp.reshape(x_hat, (-1, 3))
    residuals = vertices_residuals_from_xyz(q, loads, xyz, structure)
    if free_indices is not None:
        residuals = residuals[jnp.array(free_indices), :]
    return float(jnp.sqrt(jnp.sum(jnp.square(residuals))))