rlx-fdm 0.2.1

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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// 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/>.

//! Constrained form-finding (jax_fdm `constrained_fdm` + GD / L-BFGS).

use crate::constraints::{
    Constraint, constraints_grad_xyz_free, constraints_have_nonlinear, constraints_penalty,
};
use crate::equilibrium::{EquilibriumModel, FdmError};
use crate::implicit::AdjointSolveConfig;
use crate::lbfgs::Lbfgs;
use crate::loads::LoadState;
use crate::losses::{Loss, losses_total};
use crate::mesh::MeshStructure;
use crate::network::Network;
use crate::objective::{Goal, GoalReport, goals_report};
use crate::parameters::{DesignParam, DesignVector, loss_grad_xyz_free};
use crate::reference::{FdmOptions, apply_equilibrium, fdm_with_structure};
use crate::slsqp::Slsqp;
use crate::state::EquilibriumState;
use crate::structure::Structure;

/// Optimizer for [`constrained_fdm`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OptimizerKind {
    /// Projected gradient descent with fixed learning rate.
    Gd,
    /// Limited-memory BFGS with backtracking line search (box projection on `q`).
    Lbfgs,
    /// Penalty + L-BFGS for nonlinear inequalities (jax_fdm `SLSQP` subset).
    Slsqp,
    /// Alias for [`Self::Slsqp`] (external IPOPT not linked; use penalty SLSQP).
    Ipopt,
}

/// Gradient-descent / L-BFGS settings for [`constrained_fdm`].
#[derive(Clone, Debug)]
pub struct OptimizeConfig {
    pub optimizer: OptimizerKind,
    pub learning_rate: f64,
    pub max_iter: usize,
    pub loss_tol: f64,
    pub grad_tol: f64,
    pub fd_eps: f64,
    /// L2 penalty `½ · weight · ‖q‖²` on force densities (jax_fdm regularizer subset).
    pub q_l2_weight: f64,
    pub lbfgs_history: usize,
    pub fdm: FdmOptions,
    pub verbose: bool,
    /// Reuse a compiled MIR equilibrium graph each iteration (`feature ir`).
    pub fuse_mir: bool,
    /// Design variables (default: all edge `q`).
    pub parameters: Vec<DesignParam>,
    /// jax_fdm-style loss collections (default: goals as squared error).
    pub losses: Vec<Loss>,
    /// Penalty weight for nonlinear constraints in [`OptimizerKind::Slsqp`].
    pub slsqp_penalty_weight: f64,
    /// Match jax_fdm: use dense solver when constraints are present.
    pub force_dense_with_constraints: bool,
}

impl Default for OptimizeConfig {
    fn default() -> Self {
        Self {
            optimizer: OptimizerKind::Gd,
            learning_rate: 0.05,
            max_iter: 200,
            loss_tol: 1e-6,
            grad_tol: 1e-5,
            fd_eps: 1e-7,
            q_l2_weight: 0.0,
            lbfgs_history: 10,
            fdm: FdmOptions::default(),
            verbose: false,
            fuse_mir: false,
            parameters: Vec::new(),
            losses: Vec::new(),
            slsqp_penalty_weight: 50.0,
            force_dense_with_constraints: true,
        }
    }
}

impl OptimizeConfig {
    /// Arch / loadpath form-finding: L-BFGS, linear equilibrium, CSR+PCG when large enough.
    pub fn arch_form_finding() -> Self {
        Self {
            optimizer: OptimizerKind::Lbfgs,
            learning_rate: 1.0,
            max_iter: 120,
            loss_tol: 1e-6,
            grad_tol: 1e-5,
            lbfgs_history: 12,
            fdm: FdmOptions {
                sparse: true,
                iterative: crate::iterative::IterativeConfig::linear(),
            },
            verbose: false,
            fuse_mir: true,
            ..Default::default()
        }
    }
}

/// Result of [`constrained_fdm`].
#[derive(Clone, Debug)]
pub struct OptimizeResult {
    pub network: Network,
    pub equilibrium: EquilibriumState,
    pub loss: f64,
    pub goal_loss: f64,
    pub penalty: f64,
    pub iterations: usize,
    pub loss_history: Vec<f64>,
    pub goal_reports: Vec<GoalReport>,
}

/// Cached topology and load state for repeated equilibrium + adjoint solves.
pub struct OptWorkspace {
    pub structure: Structure,
    pub load_state: LoadState,
    pub mesh: Option<MeshStructure>,
    pub xyz_fixed: Vec<f64>,
    pub iter_config: crate::iterative::IterativeConfig,
}

impl OptWorkspace {
    pub fn from_network(network: &Network, fdm: &FdmOptions) -> Self {
        let structure = Structure::from_network(network);
        let load_state = network.load_state();
        let mesh = network.mesh_structure();
        let na = structure.num_fixed();
        let mut xyz_fixed = vec![0.0; na * 3];
        for (j, &node) in structure.indices_fixed.iter().enumerate() {
            for c in 0..3 {
                xyz_fixed[j * 3 + c] = network.xyz[node * 3 + c];
            }
        }
        let mut iter_config = fdm.iterative.clone();
        iter_config.use_sparse = fdm.sparse;
        Self {
            structure,
            load_state,
            mesh,
            xyz_fixed,
            iter_config,
        }
    }

    pub fn solve(&self, network: &Network, fdm: &FdmOptions) -> Result<EquilibriumState, FdmError> {
        fdm_with_structure(network, &self.structure, fdm)
    }

    /// Equilibrium at trial `q` with fixed supports / anchor geometry from `network`.
    pub fn solve_q(
        &self,
        q: &[f64],
        network: &Network,
        fdm: &FdmOptions,
    ) -> Result<EquilibriumState, FdmError> {
        let mut iterative = fdm.iterative.clone();
        iterative.use_sparse = fdm.sparse;
        EquilibriumModel::equilibrium_with_config(
            q,
            &network.xyz,
            &self.load_state,
            &self.structure,
            &network.edges,
            &iterative,
            self.mesh.as_ref(),
        )
    }

    pub fn pack_xyz_free(&self, eq: &EquilibriumState) -> Vec<f64> {
        EquilibriumModel::pack_xyz_free(&eq.xyz, &self.structure)
    }
}

/// Constrained inverse form-finding: minimize goals subject to soft/hard constraints on `q`.
///
/// Design variables are edge force densities `q` (jax_fdm `EdgeForceDensityParameter`).
/// With `feature ir` and [`OptimizeConfig::fuse_mir`], reuses a compiled equilibrium graph
/// (see [`crate::fuse::FusedEquilibriumRunner`]).
pub fn constrained_fdm(
    network: &Network,
    goals: &[Goal],
    constraints: &[Constraint],
    config: &OptimizeConfig,
) -> Result<OptimizeResult, FdmError> {
    #[cfg(feature = "ir")]
    if config.fuse_mir {
        if let Some(result) =
            crate::fuse::try_constrained_fdm_fused(network, goals, constraints, config)?
        {
            return Ok(result);
        }
    }
    #[cfg(feature = "ir")]
    {
        constrained_fdm_host(network, goals, constraints, config, None)
    }
    #[cfg(not(feature = "ir"))]
    {
        constrained_fdm_host(network, goals, constraints, config, None)
    }
}

/// Host optimization loop; optional fused MIR equilibrium runner (`feature ir`).
pub(crate) fn constrained_fdm_host(
    network: &Network,
    goals: &[Goal],
    constraints: &[Constraint],
    config: &OptimizeConfig,
    #[cfg(feature = "ir")] mut fused_runner: Option<crate::fuse::FusedEquilibriumRunner>,
    #[cfg(not(feature = "ir"))] mut fused_runner: Option<()>,
) -> Result<OptimizeResult, FdmError> {
    network.validate().map_err(FdmError::Validation)?;
    let mut net = network.clone();
    let fdm = effective_fdm_options(config, constraints);
    let ws = OptWorkspace::from_network(&net, &fdm);
    let mesh = ws.mesh.as_ref();
    let loss_terms = effective_losses(config, goals);
    let params = effective_parameters(config);
    let mut design = DesignVector::from_network(&net, &params);
    let adjoint = AdjointSolveConfig::from(&fdm.iterative).with_sparse(fdm.sparse);

    let mut loss_history = Vec::new();
    if config.verbose {
        eprintln!(
            "constrained_fdm: {} loss terms, {} constraints, opt={:?} n_x={} sparse={}",
            loss_terms.len(),
            constraints.len(),
            config.optimizer,
            design.x.len(),
            fdm.sparse
        );
        if config.force_dense_with_constraints
            && constraints_have_nonlinear(constraints)
            && config.fdm.sparse
        {
            eprintln!("  note: jax_fdm uses dense FDM when constraints are active");
        }
    }

    let mut eq = solve_equilibrium(&ws, &net, config, &fdm, &mut fused_runner)?;
    let mut total = eval_total(
        &loss_terms,
        constraints,
        &eq,
        &ws.structure,
        &net,
        mesh,
        config,
    );
    loss_history.push(total);

    let mut lbfgs = Lbfgs::default();
    lbfgs.history = config.lbfgs_history;
    let mut slsqp = Slsqp {
        penalty_weight: config.slsqp_penalty_weight,
        fd_eps: config.fd_eps,
        lbfgs: lbfgs.clone(),
    };

    for iter in 0..config.max_iter {
        let goal_loss = losses_total(&loss_terms, &eq, &ws.structure, &net.is_support, mesh);
        let penalty = constraints_penalty(constraints, &eq, &net.q);
        total = goal_loss + penalty + q_l2_loss(&net.q, config.q_l2_weight);

        let mut loss_grad =
            loss_grad_xyz_free(&[], &eq, &ws.structure, &net.edges, &net.is_support, mesh);
        for loss in &loss_terms {
            let lg = loss_grad_xyz_free(
                &loss.goals,
                &eq,
                &ws.structure,
                &net.edges,
                &net.is_support,
                mesh,
            );
            for (a, b) in loss_grad.iter_mut().zip(lg.iter()) {
                *a += *b;
            }
        }
        let cg = constraints_grad_xyz_free(constraints, &eq, &ws.structure, &net.edges);
        for (a, b) in loss_grad.iter_mut().zip(cg.iter()) {
            *a += *b;
        }

        let mut gx = design.gradient(
            &net,
            &eq,
            &loss_grad,
            &ws.structure,
            &ws.load_state,
            &ws.xyz_fixed,
            &ws.iter_config,
            mesh,
            config.fd_eps,
            &adjoint,
        )?;
        design.add_q_l2_grad(&net, &mut gx, config.q_l2_weight);
        let grad_norm: f64 = gx.iter().map(|x| x * x).sum::<f64>().sqrt();
        if config.verbose && (iter % 10 == 0 || iter + 1 == config.max_iter) {
            eprintln!(
                "  iter {iter:4}  loss={total:.6}  goals={goal_loss:.6}  pen={penalty:.6}  |grad|={grad_norm:.4}"
            );
        }
        if total < config.loss_tol || grad_norm < config.grad_tol {
            return Ok(finish(
                net,
                eq,
                total,
                goal_loss,
                penalty,
                iter + 1,
                loss_history,
                &loss_terms,
                &ws.structure,
                mesh,
            ));
        }

        let x_prev = design.x.clone();
        let g_prev = gx.clone();
        let opt = config.optimizer;
        match opt {
            OptimizerKind::Gd => {
                for (xi, gi) in design.x.iter_mut().zip(gx.iter()) {
                    *xi -= config.learning_rate * gi;
                }
            }
            OptimizerKind::Lbfgs => {
                let dir = lbfgs.direction(&gx);
                let (x_new, _) = lbfgs.line_search(&design.x, &dir, total, &gx, |x| {
                    eval_objective_at(
                        x,
                        &design,
                        &ws,
                        &net,
                        &loss_terms,
                        constraints,
                        config,
                        &fdm,
                        mesh,
                    )
                });
                design.x.copy_from_slice(&x_new);
            }
            OptimizerKind::Slsqp | OptimizerKind::Ipopt => {
                let low = design.low.clone();
                let up = design.up.clone();
                let mut x_step = design.x.clone();
                let nonlinear = |x: &[f64]| {
                    let mut trial = net.clone();
                    apply_design(&design, x, &mut trial);
                    let teq = ws.solve(&trial, &fdm).expect("trial eq");
                    crate::constraints::nonlinear_ineq_values(constraints, &teq, mesh, &trial.edges)
                };
                slsqp.step(
                    &mut x_step,
                    &low,
                    &up,
                    |x| {
                        eval_objective_at(
                            x,
                            &design,
                            &ws,
                            &net,
                            &loss_terms,
                            constraints,
                            config,
                            &fdm,
                            mesh,
                        )
                    },
                    |x, g| {
                        let mut trial = net.clone();
                        apply_design(&design, x, &mut trial);
                        let teq = ws.solve(&trial, &fdm).expect("eq grad");
                        let lg =
                            combined_loss_grad(&loss_terms, constraints, &teq, &ws, &trial, mesh);
                        let sub = design
                            .gradient(
                                &trial,
                                &teq,
                                &lg,
                                &ws.structure,
                                &ws.load_state,
                                &ws.xyz_fixed,
                                &ws.iter_config,
                                mesh,
                                config.fd_eps,
                                &adjoint,
                            )
                            .expect("grad");
                        g.copy_from_slice(&sub);
                        design.add_q_l2_grad(&trial, g, config.q_l2_weight);
                    },
                    nonlinear,
                );
                design.x.copy_from_slice(&x_step);
            }
        }
        design.project();
        project_q_constraints(constraints, &mut design, &mut net);

        eq = solve_equilibrium(&ws, &net, config, &fdm, &mut fused_runner)?;
        total = eval_total(
            &loss_terms,
            constraints,
            &eq,
            &ws.structure,
            &net,
            mesh,
            config,
        );
        loss_history.push(total);

        if matches!(opt, OptimizerKind::Lbfgs) {
            let lg = combined_loss_grad(&loss_terms, constraints, &eq, &ws, &net, mesh);
            if let Ok(mut g_new) = design.gradient(
                &net,
                &eq,
                &lg,
                &ws.structure,
                &ws.load_state,
                &ws.xyz_fixed,
                &ws.iter_config,
                mesh,
                config.fd_eps,
                &adjoint,
            ) {
                design.add_q_l2_grad(&net, &mut g_new, config.q_l2_weight);
                lbfgs.update(&x_prev, &g_prev, &design.x, &g_new);
            }
        }
    }

    let goal_loss = losses_total(&loss_terms, &eq, &ws.structure, &net.is_support, mesh);
    let penalty = constraints_penalty(constraints, &eq, &net.q);
    let l2 = q_l2_loss(&net.q, config.q_l2_weight);
    Ok(finish(
        net,
        eq,
        goal_loss + penalty + l2,
        goal_loss,
        penalty,
        config.max_iter,
        loss_history,
        &loss_terms,
        &ws.structure,
        mesh,
    ))
}

fn q_l2_loss(q: &[f64], weight: f64) -> f64 {
    if weight <= 0.0 {
        return 0.0;
    }
    0.5 * weight * q.iter().map(|&x| x * x).sum::<f64>()
}

fn project_q(q: &mut [f64], constraints: &[Constraint]) {
    for c in constraints {
        c.project_q(q);
    }
    for qi in q.iter_mut() {
        if qi.abs() < 1e-4 {
            *qi = qi.signum().clamp(-1.0, 1.0) * 1e-4;
        }
    }
}

fn solve_equilibrium(
    ws: &OptWorkspace,
    net: &Network,
    _config: &OptimizeConfig,
    fdm: &FdmOptions,
    #[cfg(feature = "ir")] fused_runner: &mut Option<crate::fuse::FusedEquilibriumRunner>,
    #[cfg(not(feature = "ir"))] _fused_runner: &mut Option<()>,
) -> Result<EquilibriumState, FdmError> {
    #[cfg(feature = "ir")]
    if let Some(runner) = fused_runner.as_mut() {
        return runner.solve(net);
    }
    ws.solve(net, fdm)
}

fn effective_fdm_options(config: &OptimizeConfig, constraints: &[Constraint]) -> FdmOptions {
    let mut fdm = config.fdm.clone();
    if config.force_dense_with_constraints && !constraints.is_empty() {
        fdm.sparse = false;
        fdm.iterative.use_sparse = false;
    }
    fdm
}

fn effective_parameters(config: &OptimizeConfig) -> Vec<DesignParam> {
    if config.parameters.is_empty() {
        vec![DesignParam::all_edge_q(f64::NEG_INFINITY, f64::INFINITY)]
    } else {
        config.parameters.clone()
    }
}

fn effective_losses(config: &OptimizeConfig, goals: &[Goal]) -> Vec<Loss> {
    if !config.losses.is_empty() {
        config.losses.clone()
    } else if goals.is_empty() {
        Vec::new()
    } else {
        vec![Loss::new(goals.to_vec())]
    }
}

fn eval_total(
    losses: &[Loss],
    constraints: &[Constraint],
    eq: &EquilibriumState,
    structure: &Structure,
    net: &Network,
    mesh: Option<&MeshStructure>,
    config: &OptimizeConfig,
) -> f64 {
    losses_total(losses, eq, structure, &net.is_support, mesh)
        + constraints_penalty(constraints, eq, &net.q)
        + q_l2_loss(&net.q, config.q_l2_weight)
}

fn eval_objective_at(
    x: &[f64],
    design: &DesignVector,
    ws: &OptWorkspace,
    net: &Network,
    losses: &[Loss],
    constraints: &[Constraint],
    config: &OptimizeConfig,
    fdm: &FdmOptions,
    mesh: Option<&MeshStructure>,
) -> f64 {
    let mut trial = net.clone();
    design.apply_x_to_network(x, &mut trial);
    let teq = ws.solve(&trial, fdm).expect("trial equilibrium");
    eval_total(
        losses,
        constraints,
        &teq,
        &ws.structure,
        &trial,
        mesh,
        config,
    )
}

fn apply_design(design: &DesignVector, x: &[f64], net: &mut Network) {
    design.apply_x_to_network(x, net);
}

fn combined_loss_grad(
    losses: &[Loss],
    constraints: &[Constraint],
    eq: &EquilibriumState,
    ws: &OptWorkspace,
    net: &Network,
    mesh: Option<&MeshStructure>,
) -> Vec<f64> {
    let mut loss_grad =
        loss_grad_xyz_free(&[], eq, &ws.structure, &net.edges, &net.is_support, mesh);
    for loss in losses {
        let lg = loss_grad_xyz_free(
            &loss.goals,
            eq,
            &ws.structure,
            &net.edges,
            &net.is_support,
            mesh,
        );
        for (a, b) in loss_grad.iter_mut().zip(lg.iter()) {
            *a += *b;
        }
    }
    let cg = constraints_grad_xyz_free(constraints, eq, &ws.structure, &net.edges);
    for (a, b) in loss_grad.iter_mut().zip(cg.iter()) {
        *a += *b;
    }
    loss_grad
}

fn project_q_constraints(constraints: &[Constraint], design: &mut DesignVector, net: &mut Network) {
    design.apply_to_network(net);
    project_q(&mut net.q, constraints);
    design.sync_from_network(net);
}

fn finish(
    mut network: Network,
    eq: EquilibriumState,
    loss: f64,
    goal_loss: f64,
    penalty: f64,
    iterations: usize,
    loss_history: Vec<f64>,
    losses: &[Loss],
    structure: &Structure,
    mesh: Option<&MeshStructure>,
) -> OptimizeResult {
    apply_equilibrium(&mut network, &eq);
    let goals: Vec<Goal> = losses.iter().flat_map(|l| l.goals.clone()).collect();
    OptimizeResult {
        goal_reports: goals_report(&goals, &eq, structure, &network.is_support, mesh),
        network,
        equilibrium: eq,
        loss,
        goal_loss,
        penalty,
        iterations,
        loss_history,
    }
}