rlx-fdm 0.2.13

Force density method (FDM) for RLX — pin-jointed form-finding ported from jax_fdm.
Documentation
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
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
420
421
422
423
424
425
426
427
428
429
430
431
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Form-finding objectives (jax_fdm `goals` + `losses.errors`).

use crate::goals::{
    EdgeIndex, GoalIndex, accumulate_edge_length_grad, edge_force, grad_edge_force_wrt_xyz_free,
    grad_edge_lengths_wrt_xyz_free, grad_mesh_laplacian_wrt_xyz_free,
    grad_mesh_mean_face_rectangular_wrt_xyz_free, grad_mesh_mean_planarity_wrt_xyz_free,
    grad_mesh_total_area_wrt_xyz_free, grad_min_free_z_wrt_xyz_free, grad_node_coord_wrt_xyz_free,
    grad_residual_wrt_xyz_free, mean_edge_force, mean_edge_length, mesh_laplacian_energy,
    mesh_mean_face_rectangular, mesh_mean_planarity, mesh_total_area, min_free_z, network_loadpath,
    node_coord, packed_free_dim, residual_loss,
};
use crate::mesh::MeshStructure;
use crate::state::EquilibriumState;
use crate::structure::Structure;

/// Axis index for [`Goal::NodeCoord`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CoordAxis {
    X = 0,
    Y = 1,
    Z = 2,
}

/// A weighted objective term (jax_fdm `Goal` + squared error).
#[derive(Clone, Debug)]
pub enum Goal {
    /// Minimize `weight · (loadpath − target)²` (jax_fdm `NetworkLoadPathGoal`).
    NetworkLoadpath { target: f64, weight: f64 },
    /// Minimize `weight · (length[edge] − target)²` (jax_fdm `EdgeLengthGoal`).
    EdgeLength {
        edge: EdgeIndex,
        target: f64,
        weight: f64,
    },
    /// Minimize `weight · (mean length − target)²`.
    MeanEdgeLength { target: f64, weight: f64 },
    /// Minimize `weight · (force[edge] − target)²` (jax_fdm `EdgeForceGoal`).
    EdgeForce {
        edge: EdgeIndex,
        target: f64,
        weight: f64,
    },
    /// Minimize `weight · (mean |force| − target)²`.
    MeanEdgeForce { target: f64, weight: f64 },
    /// Minimize `weight · (node coord − target)²`.
    NodeCoord {
        node: usize,
        axis: CoordAxis,
        target: f64,
        weight: f64,
    },
    /// Minimize `weight · (min free-node z − target)²` (sag control).
    MinFreeZ { target: f64, weight: f64 },
    /// Penalize equilibrium residual norm (useful as soft constraint).
    Residual { weight: f64 },
    /// Minimize `weight · (total mesh area − target)²` (requires mesh on network).
    MeshArea { target: f64, weight: f64 },
    /// Minimize `weight · (mean face planarity − target)²`.
    MeshPlanarity { target: f64, weight: f64 },
    /// Minimize `weight · (mean face rectangular deviation − target)²` (jax_fdm `FaceRectangularGoal`).
    MeshFaceRectangular { target: f64, weight: f64 },
    /// Minimize `weight · (mesh edge spring energy − target)²` (smoothing).
    MeshLaplacian { target: f64, weight: f64 },
}

impl Goal {
    pub fn network_loadpath(target: f64, weight: f64) -> Self {
        Self::NetworkLoadpath { target, weight }
    }

    pub fn edge_length(edge: usize, target: f64, weight: f64) -> Self {
        Self::EdgeLength {
            edge: EdgeIndex(edge),
            target,
            weight,
        }
    }

    pub fn mean_edge_length(target: f64, weight: f64) -> Self {
        Self::MeanEdgeLength { target, weight }
    }

    pub fn edge_force(edge: usize, target: f64, weight: f64) -> Self {
        Self::EdgeForce {
            edge: EdgeIndex(edge),
            target,
            weight,
        }
    }

    pub fn mean_edge_force(target: f64, weight: f64) -> Self {
        Self::MeanEdgeForce { target, weight }
    }

    pub fn node_coord(node: usize, axis: CoordAxis, target: f64, weight: f64) -> Self {
        Self::NodeCoord {
            node,
            axis,
            target,
            weight,
        }
    }

    pub fn node_z(node: usize, target: f64, weight: f64) -> Self {
        Self::node_coord(node, CoordAxis::Z, target, weight)
    }

    pub fn min_free_z(target: f64, weight: f64) -> Self {
        Self::MinFreeZ { target, weight }
    }

    pub fn residual(weight: f64) -> Self {
        Self::Residual { weight }
    }

    pub fn mesh_area(target: f64, weight: f64) -> Self {
        Self::MeshArea { target, weight }
    }

    pub fn mesh_planarity(target: f64, weight: f64) -> Self {
        Self::MeshPlanarity { target, weight }
    }

    pub fn mesh_face_rectangular(target: f64, weight: f64) -> Self {
        Self::MeshFaceRectangular { target, weight }
    }

    pub fn mesh_laplacian(target: f64, weight: f64) -> Self {
        Self::MeshLaplacian { target, weight }
    }

    pub fn prediction(&self, state: &EquilibriumState, is_support: &[bool]) -> f64 {
        match self {
            Self::NetworkLoadpath { .. } => network_loadpath(state),
            Self::EdgeLength { edge, .. } => {
                let e = edge.0;
                state.lengths.get(e).copied().unwrap_or(0.0)
            }
            Self::MeanEdgeLength { .. } => mean_edge_length(state),
            Self::EdgeForce { edge, .. } => edge_force(state, edge.0),
            Self::MeanEdgeForce { .. } => mean_edge_force(state),
            Self::NodeCoord { node, axis, .. } => node_coord(state, *node, *axis as usize),
            Self::MinFreeZ { .. } => {
                let mut min_z = f64::INFINITY;
                for (i, &sup) in is_support.iter().enumerate() {
                    if !sup {
                        min_z = min_z.min(state.xyz[i * 3 + 2]);
                    }
                }
                min_z
            }
            Self::Residual { .. } => residual_loss(state, is_support).sqrt(),
            Self::MeshArea { .. }
            | Self::MeshPlanarity { .. }
            | Self::MeshFaceRectangular { .. }
            | Self::MeshLaplacian { .. } => 0.0,
        }
    }

    /// Like [`Self::prediction`] but with structure for mesh-aware goals.
    pub fn prediction_with_structure(
        &self,
        state: &EquilibriumState,
        structure: &Structure,
        is_support: &[bool],
        mesh: Option<&MeshStructure>,
    ) -> f64 {
        match self {
            Self::MinFreeZ { .. } => min_free_z(state, structure),
            Self::MeshArea { .. } => mesh.map(|m| mesh_total_area(m, &state.xyz)).unwrap_or(0.0),
            Self::MeshPlanarity { .. } => mesh
                .map(|m| mesh_mean_planarity(m, &state.xyz))
                .unwrap_or(0.0),
            Self::MeshFaceRectangular { .. } => mesh
                .map(|m| mesh_mean_face_rectangular(m, &state.xyz))
                .unwrap_or(0.0),
            Self::MeshLaplacian { .. } => mesh
                .map(|m| mesh_laplacian_energy(m, &state.xyz))
                .unwrap_or(0.0),
            _ => self.prediction(state, is_support),
        }
    }

    pub fn target(&self) -> f64 {
        match self {
            Self::NetworkLoadpath { target, .. }
            | Self::EdgeLength { target, .. }
            | Self::MeanEdgeLength { target, .. }
            | Self::EdgeForce { target, .. }
            | Self::MeanEdgeForce { target, .. }
            | Self::NodeCoord { target, .. }
            | Self::MinFreeZ { target, .. }
            | Self::MeshArea { target, .. }
            | Self::MeshPlanarity { target, .. }
            | Self::MeshFaceRectangular { target, .. }
            | Self::MeshLaplacian { target, .. } => *target,
            Self::Residual { .. } => 0.0,
        }
    }

    pub fn weight(&self) -> f64 {
        match self {
            Self::NetworkLoadpath { weight, .. }
            | Self::EdgeLength { weight, .. }
            | Self::MeanEdgeLength { weight, .. }
            | Self::EdgeForce { weight, .. }
            | Self::MeanEdgeForce { weight, .. }
            | Self::NodeCoord { weight, .. }
            | Self::MinFreeZ { weight, .. }
            | Self::Residual { weight, .. }
            | Self::MeshArea { weight, .. }
            | Self::MeshPlanarity { weight, .. }
            | Self::MeshFaceRectangular { weight, .. }
            | Self::MeshLaplacian { weight, .. } => *weight,
        }
    }

    /// Squared-error contribution (jax_fdm `SquaredError`).
    pub fn loss(&self, state: &EquilibriumState, is_support: &[bool]) -> f64 {
        let d = self.prediction(state, is_support) - self.target();
        self.weight() * d * d
    }

    pub fn loss_with_structure(
        &self,
        state: &EquilibriumState,
        structure: &Structure,
        is_support: &[bool],
        mesh: Option<&MeshStructure>,
    ) -> f64 {
        let d = self.prediction_with_structure(state, structure, is_support, mesh) - self.target();
        self.weight() * d * d
    }

    /// `∂L/∂x_f` for this goal (positions only; chain to `q` via implicit adjoint).
    pub fn grad_xyz_free(
        &self,
        state: &EquilibriumState,
        structure: &Structure,
        edges: &[(usize, usize)],
        is_support: &[bool],
        mesh: Option<&MeshStructure>,
    ) -> Vec<f64> {
        let nf = structure.num_free();
        let dim = packed_free_dim(nf);
        let pred = self.prediction_with_structure(state, structure, is_support, mesh);
        let scale = 2.0 * self.weight() * (pred - self.target());
        let mut g = vec![0.0; dim];
        match self {
            Self::NetworkLoadpath { .. } => {
                for e in 0..edges.len() {
                    let fe = state.forces[e].abs();
                    accumulate_edge_length_grad(&mut g, state, structure, edges, e, fe * scale);
                }
            }
            Self::EdgeLength { edge, .. } => {
                let e = edge.0;
                if e < edges.len() {
                    accumulate_edge_length_grad(&mut g, state, structure, edges, e, scale);
                }
            }
            Self::MeanEdgeLength { .. } => {
                let gg = grad_edge_lengths_wrt_xyz_free(
                    state,
                    structure,
                    edges,
                    scale / edges.len() as f64,
                );
                for (a, b) in g.iter_mut().zip(gg.iter()) {
                    *a += *b;
                }
            }
            Self::EdgeForce { edge, .. } => {
                let e = edge.0;
                let qeff = state.forces.get(e).copied().unwrap_or(0.0)
                    / state.lengths.get(e).copied().unwrap_or(1.0).max(1e-12);
                let gg = grad_edge_force_wrt_xyz_free(state, structure, edges, e, scale * qeff);
                for (a, b) in g.iter_mut().zip(gg.iter()) {
                    *a += *b;
                }
            }
            Self::MeanEdgeForce { .. } => {
                for e in 0..edges.len() {
                    let qeff = state.forces[e] / state.lengths[e].max(1e-12);
                    let gg = grad_edge_force_wrt_xyz_free(
                        state,
                        structure,
                        edges,
                        e,
                        scale * qeff / edges.len() as f64,
                    );
                    for (a, b) in g.iter_mut().zip(gg.iter()) {
                        *a += *b;
                    }
                }
            }
            Self::NodeCoord { node, axis, .. } => {
                let gg = grad_node_coord_wrt_xyz_free(structure, *node, *axis as usize, scale);
                for (a, b) in g.iter_mut().zip(gg.iter()) {
                    *a += *b;
                }
            }
            Self::MinFreeZ { .. } => {
                let gg = grad_min_free_z_wrt_xyz_free(state, structure);
                for (a, b) in g.iter_mut().zip(gg.iter()) {
                    *a += *b * scale;
                }
            }
            Self::Residual { .. } => {
                let gg = grad_residual_wrt_xyz_free(state, structure);
                for (a, b) in g.iter_mut().zip(gg.iter()) {
                    *a += *b * scale;
                }
            }
            Self::MeshArea { .. } => {
                if let Some(m) = mesh {
                    let gg = grad_mesh_total_area_wrt_xyz_free(m, &state.xyz, structure);
                    for (a, b) in g.iter_mut().zip(gg.iter()) {
                        *a += *b * scale;
                    }
                }
            }
            Self::MeshPlanarity { .. } => {
                if let Some(m) = mesh {
                    let gg = grad_mesh_mean_planarity_wrt_xyz_free(m, &state.xyz, structure);
                    for (a, b) in g.iter_mut().zip(gg.iter()) {
                        *a += *b * scale;
                    }
                }
            }
            Self::MeshFaceRectangular { .. } => {
                if let Some(m) = mesh {
                    let gg = grad_mesh_mean_face_rectangular_wrt_xyz_free(m, &state.xyz, structure);
                    for (a, b) in g.iter_mut().zip(gg.iter()) {
                        *a += *b * scale;
                    }
                }
            }
            Self::MeshLaplacian { .. } => {
                if let Some(m) = mesh {
                    let gg = grad_mesh_laplacian_wrt_xyz_free(m, &state.xyz, structure);
                    for (a, b) in g.iter_mut().zip(gg.iter()) {
                        *a += *b * scale;
                    }
                }
            }
        }
        g
    }
}

/// Sum of goal losses.
pub fn goals_loss(goals: &[Goal], state: &EquilibriumState, is_support: &[bool]) -> f64 {
    goals.iter().map(|g| g.loss(state, is_support)).sum()
}

pub fn goals_loss_with_structure(
    goals: &[Goal],
    state: &EquilibriumState,
    structure: &Structure,
    is_support: &[bool],
    mesh: Option<&MeshStructure>,
) -> f64 {
    goals
        .iter()
        .map(|g| g.loss_with_structure(state, structure, is_support, mesh))
        .sum()
}

/// Sum of goal `∂L/∂x_f` vectors.
pub fn goals_grad_xyz_free(
    goals: &[Goal],
    state: &EquilibriumState,
    structure: &Structure,
    edges: &[(usize, usize)],
    is_support: &[bool],
    mesh: Option<&MeshStructure>,
) -> Vec<f64> {
    let dim = packed_free_dim(structure.num_free());
    let mut g = vec![0.0; dim];
    for goal in goals {
        let gg = goal.grad_xyz_free(state, structure, edges, is_support, mesh);
        for (a, b) in g.iter_mut().zip(gg.iter()) {
            *a += *b;
        }
    }
    g
}

/// Per-goal diagnostics after a solve.
#[derive(Clone, Debug)]
pub struct GoalReport {
    pub index: GoalIndex,
    pub prediction: f64,
    pub target: f64,
    pub loss: f64,
}

pub fn goals_report(
    goals: &[Goal],
    state: &EquilibriumState,
    structure: &Structure,
    is_support: &[bool],
    mesh: Option<&MeshStructure>,
) -> Vec<GoalReport> {
    goals
        .iter()
        .enumerate()
        .map(|(i, g)| GoalReport {
            index: GoalIndex(i),
            prediction: g.prediction_with_structure(state, structure, is_support, mesh),
            target: g.target(),
            loss: g.loss_with_structure(state, structure, is_support, mesh),
        })
        .collect()
}