rumoca 0.7.28

Modelica compiler written in RUST
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! Block Lower Triangular (BLT) decomposition for equation ordering.
//!
//! This module implements BLT transformation to reorder equations so that:
//! 1. Each equation can be solved for one variable
//! 2. Variables are computed in dependency order
//! 3. Derivative equations (der(x) = ...) are in proper form
//!
//! The algorithm combines:
//! - **Hopcroft-Karp algorithm** for maximum bipartite matching (equation-variable assignment)
//! - **Tarjan's strongly connected components (SCC)** for topological ordering
//!
//! ## Submodules
//!
//! - `tearing` - Tearing algorithm for optimizing algebraic loop solving
//! - `differentiate` - Symbolic differentiation for index reduction
//! - `pantelides` - Pantelides algorithm for DAE index reduction
//!
//! ## Steps
//!
//! 1. Build bipartite graph: equations on one side, variables on the other
//! 2. Use Hopcroft-Karp to find maximum matching (O(E√V) complexity)
//! 3. Build dependency graph from matching
//! 4. Apply Tarjan's algorithm to find strongly connected components (SCCs)
//!    - SCCs represent blocks of mutually dependent equations (algebraic loops)
//! 5. Process equations in dependency order
//! 6. Normalize derivative equations: swap if der() on RHS but not on LHS
//!
//! ## References
//!
//! - Hopcroft, J. & Karp, R. (1973). "An n^(5/2) Algorithm for Maximum Matchings in Bipartite Graphs"
//! - Tarjan, R. (1972). "Depth-first search and linear graph algorithms"
//! - Pantelides, C. (1988). "The consistent initialization of differential-algebraic systems"
//! - Elmqvist, H. & Otter, M. (1994). "Methods for Tearing Systems of Equations"

mod causalize;
pub mod create_dae;
mod differentiate;
pub mod location;
mod matching;
mod pantelides;
mod scc;
mod tearing;

use crate::ir::ast::{ComponentReference, Equation, Expression};
use crate::ir::visitor::{Visitable, Visitor};
use causalize::{causalize_equation, check_if_needs_swap, normalize_derivative_equation};
use matching::find_maximum_matching;
use scc::tarjan_scc;
use std::collections::{HashMap, HashSet};

// Re-export public APIs
pub use causalize::has_der_call;
pub use differentiate::{differentiate_equation, differentiate_expression};
pub use pantelides::pantelides_index_reduction;
pub use tearing::{analyze_algebraic_loops, tear_algebraic_loop};

/// Visitor to find all variables referenced in an expression.
/// Excludes function names (like "der", "sin", etc.) from the variable list.
struct VariableFinder {
    variables: HashSet<String>,
    /// Track when entering a function call to skip the function name
    skip_next_cref: bool,
}

impl VariableFinder {
    fn new() -> Self {
        Self {
            variables: HashSet::new(),
            skip_next_cref: false,
        }
    }
}

impl Visitor for VariableFinder {
    fn enter_expression(&mut self, node: &Expression) {
        // When entering a function call, mark that we should skip the next component reference
        // (which is the function name, not a variable)
        if matches!(node, Expression::FunctionCall { .. }) {
            self.skip_next_cref = true;
        }
    }

    fn enter_component_reference(&mut self, comp: &ComponentReference) {
        // Skip function names, only collect actual variable references
        if self.skip_next_cref {
            self.skip_next_cref = false;
        } else {
            self.variables.insert(comp.to_string());
        }
    }
}

/// Visitor to find der() calls in an expression
struct DerivativeFinder {
    derivatives: Vec<String>,
}

impl DerivativeFinder {
    fn new() -> Self {
        Self {
            derivatives: Vec::new(),
        }
    }
}

impl Visitor for DerivativeFinder {
    fn enter_expression(&mut self, node: &Expression) {
        if let Expression::FunctionCall { comp, args } = node
            && comp.to_string() == "der"
            && !args.is_empty()
            && let Expression::ComponentReference(cref) = &args[0]
        {
            self.derivatives.push(cref.to_string());
        }
    }
}

/// Information about an equation in the BLT graph
#[derive(Debug, Clone)]
pub(crate) struct EquationInfo {
    pub equation: Equation,
    /// All variables that appear in this equation (both LHS and RHS)
    pub all_variables: HashSet<String>,
    /// Variable on LHS (if in form: var = expr or der(var) = expr)
    pub lhs_variable: Option<String>,
    /// True if this is a derivative equation: der(x) = expr
    pub is_derivative: bool,
    /// Matched variable (assigned by Hopcroft-Karp)
    pub matched_variable: Option<String>,
}

/// Information about an algebraic loop (SCC with size > 1)
///
/// Algebraic loops occur when equations are mutually dependent and must be
/// solved simultaneously. Tearing can reduce the computational cost by
/// selecting a subset of variables to iterate on.
#[derive(Debug, Clone)]
pub struct AlgebraicLoop {
    /// Indices of equations in this loop
    pub equation_indices: Vec<usize>,
    /// All variables involved in this loop
    pub variables: HashSet<String>,
    /// Tearing variables (if tearing was applied)
    /// These are guessed/iterated variables that break the loop
    pub tearing_variables: Vec<String>,
    /// Residual variables (solved sequentially from tearing variables)
    pub residual_variables: Vec<String>,
    /// Size of the loop (number of equations)
    pub size: usize,
}

/// A dummy derivative variable introduced by index reduction
///
/// When the Pantelides algorithm differentiates constraint equations,
/// it introduces new algebraic variables representing higher derivatives.
#[derive(Debug, Clone)]
pub struct DummyDerivative {
    /// Name of the dummy variable (e.g., "der_x" for der(x))
    pub name: String,
    /// The variable being differentiated
    pub base_variable: String,
    /// Order of differentiation (1 = first derivative, 2 = second, etc.)
    pub order: usize,
}

/// Result of structural analysis including index reduction information
///
/// This provides detailed information about DAE structure useful for:
/// - Detecting high-index DAEs that need index reduction
/// - Identifying algebraic loops that may need tearing
/// - Diagnosing structural singularities
#[derive(Debug, Clone, Default)]
pub struct StructuralAnalysis {
    /// The DAE index (0 = ODE, 1 = index-1 DAE, 2+ = high index)
    pub dae_index: usize,

    /// Equations that need to be differentiated for index reduction
    /// Maps equation index to number of times it needs differentiation
    pub equations_to_differentiate: HashMap<usize, usize>,

    /// Variables that are "dummy derivatives" introduced by index reduction
    /// These are new algebraic variables representing higher derivatives
    pub dummy_derivatives: Vec<DummyDerivative>,

    /// Information about each strongly connected component (algebraic loop)
    pub algebraic_loops: Vec<AlgebraicLoop>,

    /// Whether the system is structurally singular
    pub is_singular: bool,

    /// Diagnostic messages
    pub diagnostics: Vec<String>,
}

/// Result of BLT transformation including structural information
#[derive(Debug, Clone, Default)]
pub struct BltResult {
    /// Transformed equations in topological order
    pub equations: Vec<Equation>,
    /// Strongly connected components (algebraic loops have size > 1)
    pub sccs: Vec<Vec<usize>>,
    /// Matching: equation index -> matched variable name
    pub matching: HashMap<usize, String>,
    /// Whether a perfect matching was found
    pub is_complete_matching: bool,
    /// Algebraic loops with tearing information (SCCs with size > 1)
    pub algebraic_loops: Vec<AlgebraicLoop>,
}

/// Perform BLT transformation on a set of equations
///
/// This function:
/// 1. Parses equations to extract variable information
/// 2. Uses Hopcroft-Karp algorithm to find maximum matching between equations and variables
/// 3. Uses Tarjan's SCC algorithm for topological ordering
/// 4. Normalizes derivative equations (der(x) on LHS)
///
/// The `exclude_from_matching` parameter specifies variables that should not
/// be matched to equations (e.g., parameters, constants, time). This ensures that
/// equations like `R * i = v` are solved for the algebraic variable `i` rather
/// than the parameter `R`.
pub fn blt_transform(
    equations: Vec<Equation>,
    exclude_from_matching: &HashSet<String>,
) -> Vec<Equation> {
    blt_transform_with_info(equations, exclude_from_matching).equations
}

/// Perform BLT transformation and return detailed structural information
///
/// This function returns both the transformed equations and additional
/// structural information useful for:
/// - Index reduction (detecting high-index DAEs)
/// - Tearing (optimizing algebraic loop solving)
/// - Diagnostics (debugging structural issues)
pub fn blt_transform_with_info(
    equations: Vec<Equation>,
    exclude_from_matching: &HashSet<String>,
) -> BltResult {
    // Parse equations and extract variable information
    let mut eq_infos: Vec<EquationInfo> = Vec::new();
    let mut all_variables_set: HashSet<String> = HashSet::new();

    for eq in equations.iter() {
        if let Equation::Simple { lhs, rhs, .. } = eq {
            let mut info = EquationInfo {
                equation: eq.clone(),
                all_variables: HashSet::new(),
                lhs_variable: None,
                is_derivative: false,
                matched_variable: None,
            };

            // Find LHS variable and add to all_variables
            match lhs {
                Expression::ComponentReference(cref) => {
                    let var_name = cref.to_string();
                    info.lhs_variable = Some(var_name.clone());
                    info.all_variables.insert(var_name.clone());
                    all_variables_set.insert(var_name);
                }
                Expression::FunctionCall { comp, args } => {
                    if comp.to_string() == "der"
                        && !args.is_empty()
                        && let Expression::ComponentReference(cref) = &args[0]
                    {
                        let var_name = format!("der({})", cref);
                        info.lhs_variable = Some(var_name.clone());
                        info.all_variables.insert(var_name.clone());
                        all_variables_set.insert(var_name);
                        info.is_derivative = true;
                    }
                }
                _ => {
                    // For other LHS types, try to extract variables
                    let mut lhs_finder = VariableFinder::new();
                    lhs.accept(&mut lhs_finder);
                    for var in lhs_finder.variables {
                        info.all_variables.insert(var.clone());
                        all_variables_set.insert(var);
                    }
                }
            }

            // Find all variables in RHS
            let mut var_finder = VariableFinder::new();
            rhs.accept(&mut var_finder);
            for var in var_finder.variables {
                info.all_variables.insert(var.clone());
                all_variables_set.insert(var);
            }

            // Also check for der() calls in both LHS and RHS
            let mut der_finder = DerivativeFinder::new();
            lhs.accept(&mut der_finder);
            rhs.accept(&mut der_finder);
            for der_var in &der_finder.derivatives {
                let var_name = format!("der({})", der_var);
                info.all_variables.insert(var_name.clone());
                all_variables_set.insert(var_name);
            }

            eq_infos.push(info);
        } else {
            // Non-simple equations (If, When, etc.) - keep as-is
            eq_infos.push(EquationInfo {
                equation: eq.clone(),
                all_variables: HashSet::new(),
                lhs_variable: None,
                is_derivative: false,
                matched_variable: None,
            });
        }
    }

    // Convert variable set to sorted vector for consistent ordering
    // Exclude specified variables from the matching (e.g., parameters, constants, time)
    let all_variables: Vec<String> = {
        let mut vars: Vec<_> = all_variables_set
            .into_iter()
            .filter(|v| !exclude_from_matching.contains(v))
            .collect();
        vars.sort();
        vars
    };

    // Use Hopcroft-Karp to find maximum matching
    let matching = find_maximum_matching(&eq_infos, &all_variables, exclude_from_matching);

    // Update eq_infos with matched variables
    for (eq_idx, var_name) in &matching {
        eq_infos[*eq_idx].matched_variable = Some(var_name.clone());
    }

    // Build dependency graph and find ordering using Tarjan's SCC algorithm
    let tarjan_result = tarjan_scc(&eq_infos);

    // Reorder, normalize, and causalize equations
    let mut result_equations = Vec::new();
    for idx in &tarjan_result.ordered_indices {
        let info = &eq_infos[*idx];

        // Normalize derivative equations: if der(x) appears on RHS, swap sides
        if let Equation::Simple { lhs, rhs, .. } = &info.equation {
            let needs_swap = check_if_needs_swap(lhs, rhs);

            if needs_swap {
                // Swap LHS and RHS
                result_equations.push(Equation::Simple {
                    lhs: rhs.clone(),
                    rhs: lhs.clone(),
                });
            } else if let Some(normalized) = normalize_derivative_equation(lhs, rhs) {
                // Normalize derivative equations like C * der(x) = y to der(x) = y / C
                result_equations.push(normalized);
            } else {
                // Check if we need to causalize (solve for matched variable)
                if let Some(matched_var) = &info.matched_variable {
                    if let Some(causalized) = causalize_equation(lhs, rhs, matched_var) {
                        result_equations.push(causalized);
                    } else {
                        result_equations.push(info.equation.clone());
                    }
                } else {
                    result_equations.push(info.equation.clone());
                }
            }
        } else {
            result_equations.push(info.equation.clone());
        }
    }

    // Check if we have a complete matching
    let is_complete_matching = matching.len() == eq_infos.len();

    // Analyze algebraic loops (SCCs with size > 1) and apply tearing
    let algebraic_loops = tearing::analyze_algebraic_loops(&result_equations, &tarjan_result.sccs);

    BltResult {
        equations: result_equations,
        sccs: tarjan_result.sccs,
        matching,
        is_complete_matching,
        algebraic_loops,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::ast::{ComponentRefPart, OpBinary, OpUnary, TerminalType, Token};

    fn make_var(name: &str) -> Expression {
        Expression::ComponentReference(ComponentReference {
            local: false,
            parts: vec![ComponentRefPart {
                ident: Token {
                    text: name.to_string(),
                    ..Default::default()
                },
                subs: None,
            }],
        })
    }

    fn make_der(var: Expression) -> Expression {
        Expression::FunctionCall {
            comp: ComponentReference {
                local: false,
                parts: vec![ComponentRefPart {
                    ident: Token {
                        text: "der".to_string(),
                        ..Default::default()
                    },
                    subs: None,
                }],
            },
            args: vec![var],
        }
    }

    fn make_zero() -> Expression {
        Expression::Terminal {
            terminal_type: TerminalType::UnsignedReal,
            token: Token {
                text: "0".to_string(),
                ..Default::default()
            },
        }
    }

    #[test]
    fn test_swap_derivative_equation() {
        // v = der(h) should become der(h) = v
        let equations = vec![Equation::Simple {
            lhs: make_var("v"),
            rhs: make_der(make_var("h")),
        }];

        let result = blt_transform(equations, &HashSet::new());

        assert_eq!(result.len(), 1);
        if let Equation::Simple { lhs, rhs, .. } = &result[0] {
            assert!(has_der_call(lhs), "LHS should have der()");
            assert!(!has_der_call(rhs), "RHS should not have der()");
        } else {
            panic!("Expected Simple equation");
        }
    }

    #[test]
    fn test_blt_with_chain_dependencies() {
        // Test BLT ordering with chain: z = 1, y = z, x = y
        // Should be ordered as: z = 1, y = z, x = y

        let equations = vec![
            // x = y
            Equation::Simple {
                lhs: make_var("x"),
                rhs: make_var("y"),
            },
            // y = z
            Equation::Simple {
                lhs: make_var("y"),
                rhs: make_var("z"),
            },
            // z = 1
            Equation::Simple {
                lhs: make_var("z"),
                rhs: Expression::Terminal {
                    terminal_type: TerminalType::UnsignedInteger,
                    token: Token {
                        text: "1".to_string(),
                        ..Default::default()
                    },
                },
            },
        ];

        let result = blt_transform(equations, &HashSet::new());

        // After BLT, z=1 should come first, then y=z, then x=y
        assert_eq!(result.len(), 3);

        // Extract LHS variable names for checking order
        let order: Vec<String> = result
            .iter()
            .filter_map(|eq| {
                if let Equation::Simple {
                    lhs: Expression::ComponentReference(cref),
                    ..
                } = eq
                {
                    return Some(cref.to_string());
                }
                None
            })
            .collect();

        // z should come before y, y should come before x
        let z_pos = order.iter().position(|s| s == "z").unwrap();
        let y_pos = order.iter().position(|s| s == "y").unwrap();
        let x_pos = order.iter().position(|s| s == "x").unwrap();

        assert!(
            z_pos < y_pos,
            "z should be computed before y (z at {}, y at {})",
            z_pos,
            y_pos
        );
        assert!(
            y_pos < x_pos,
            "y should be computed before x (y at {}, x at {})",
            y_pos,
            x_pos
        );
    }

    #[test]
    fn test_blt_algebraic_loop_detection() {
        // Test that algebraic loops (SCCs) are kept together
        // x = y + 1
        // y = x + 1
        // These form an algebraic loop

        let one = Expression::Terminal {
            terminal_type: TerminalType::UnsignedInteger,
            token: Token {
                text: "1".to_string(),
                ..Default::default()
            },
        };

        let equations = vec![
            // x = y + 1
            Equation::Simple {
                lhs: make_var("x"),
                rhs: Expression::Binary {
                    lhs: Box::new(make_var("y")),
                    op: OpBinary::Add(Token::default()),
                    rhs: Box::new(one.clone()),
                },
            },
            // y = x + 1
            Equation::Simple {
                lhs: make_var("y"),
                rhs: Expression::Binary {
                    lhs: Box::new(make_var("x")),
                    op: OpBinary::Add(Token::default()),
                    rhs: Box::new(one),
                },
            },
        ];

        let result = blt_transform(equations, &HashSet::new());

        // Both equations should be present (algebraic loop)
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_causalize_already_causal() {
        // Test: a = b should return None (already in causal form for "a")
        let lhs = make_var("a");
        let rhs = make_var("b");

        let result = causalize_equation(&lhs, &rhs, "a");
        assert!(
            result.is_none(),
            "Should return None for already causal equation"
        );
    }

    #[test]
    fn test_causalize_sum_to_zero() {
        // Test: a + b = 0 with matching to "a" should become a = -b
        let lhs = Expression::Binary {
            op: OpBinary::Add(Token::default()),
            lhs: Box::new(make_var("a")),
            rhs: Box::new(make_var("b")),
        };
        let rhs = make_zero();

        let result = causalize_equation(&lhs, &rhs, "a");
        assert!(
            result.is_some(),
            "Should be able to causalize a + b = 0 for a"
        );

        if let Some(Equation::Simple { lhs, rhs }) = result {
            // LHS should be "a"
            if let Expression::ComponentReference(cref) = lhs {
                assert_eq!(cref.to_string(), "a");
            } else {
                panic!("LHS should be ComponentReference");
            }

            // RHS should be -b (Unary minus of b)
            if let Expression::Unary {
                op: OpUnary::Minus(_),
                rhs,
            } = rhs
            {
                if let Expression::ComponentReference(cref) = *rhs {
                    assert_eq!(cref.to_string(), "b");
                } else {
                    panic!("RHS of negation should be ComponentReference");
                }
            } else {
                panic!("RHS should be Unary negation, got: {:?}", rhs);
            }
        }
    }

    #[test]
    fn test_causalize_three_term_sum() {
        // Test: a + b + c = 0 with matching to "a" should become a = -(b + c)
        let inner = Expression::Binary {
            op: OpBinary::Add(Token::default()),
            lhs: Box::new(make_var("a")),
            rhs: Box::new(make_var("b")),
        };
        let lhs = Expression::Binary {
            op: OpBinary::Add(Token::default()),
            lhs: Box::new(inner),
            rhs: Box::new(make_var("c")),
        };
        let rhs = make_zero();

        let result = causalize_equation(&lhs, &rhs, "a");
        assert!(
            result.is_some(),
            "Should be able to causalize a + b + c = 0 for a"
        );

        if let Some(Equation::Simple { lhs, .. }) = result {
            // LHS should be "a"
            if let Expression::ComponentReference(cref) = lhs {
                assert_eq!(cref.to_string(), "a");
            } else {
                panic!("LHS should be ComponentReference");
            }
        }
    }

    #[test]
    fn test_causalize_zero_on_lhs() {
        // Test: 0 = a + b (alternate form) with matching to "a" should become a = -b
        let lhs = make_zero();
        let rhs = Expression::Binary {
            op: OpBinary::Add(Token::default()),
            lhs: Box::new(make_var("a")),
            rhs: Box::new(make_var("b")),
        };

        let result = causalize_equation(&lhs, &rhs, "a");
        assert!(
            result.is_some(),
            "Should be able to causalize 0 = a + b for a"
        );

        if let Some(Equation::Simple { lhs, rhs }) = result {
            // LHS should be "a"
            if let Expression::ComponentReference(cref) = lhs {
                assert_eq!(cref.to_string(), "a");
            } else {
                panic!("LHS should be ComponentReference");
            }

            // RHS should be -b (Unary minus of b)
            if let Expression::Unary {
                op: OpUnary::Minus(_),
                rhs,
            } = rhs
            {
                if let Expression::ComponentReference(cref) = *rhs {
                    assert_eq!(cref.to_string(), "b");
                } else {
                    panic!("RHS of negation should be ComponentReference");
                }
            } else {
                panic!("RHS should be Unary negation, got: {:?}", rhs);
            }
        }
    }

    #[test]
    fn test_kcl_style_equation() {
        // Simulate KCL equation from circuit: R2_n_i + L1_p_i = 0
        // This should be causalized to R2_n_i = -L1_p_i
        let equations = vec![Equation::Simple {
            lhs: Expression::Binary {
                op: OpBinary::Add(Token::default()),
                lhs: Box::new(make_var("R2_n_i")),
                rhs: Box::new(make_var("L1_p_i")),
            },
            rhs: make_zero(),
        }];

        let result = blt_transform(equations, &HashSet::new());
        assert_eq!(result.len(), 1);

        // The equation should now be causalized
        if let Equation::Simple { lhs, .. } = &result[0] {
            // LHS should be a simple variable reference, not a binary expression
            assert!(
                matches!(lhs, Expression::ComponentReference(_)),
                "LHS should be a simple variable after causalization, got: {:?}",
                lhs
            );
        } else {
            panic!("Expected Simple equation");
        }
    }

    #[test]
    fn test_kcl_style_equation_zero_on_lhs() {
        // Simulate KCL equation from circuit in the form: 0 = R2_n_i + L1_p_i
        // This should be causalized to one of the variables
        let equations = vec![Equation::Simple {
            lhs: make_zero(),
            rhs: Expression::Binary {
                op: OpBinary::Add(Token::default()),
                lhs: Box::new(make_var("R2_n_i")),
                rhs: Box::new(make_var("L1_p_i")),
            },
        }];

        let result = blt_transform(equations, &HashSet::new());
        assert_eq!(result.len(), 1);

        // The equation should now be causalized
        if let Equation::Simple { lhs, .. } = &result[0] {
            // LHS should be a simple variable reference, not zero
            assert!(
                matches!(lhs, Expression::ComponentReference(_)),
                "LHS should be a simple variable after causalization, got: {:?}",
                lhs
            );
        } else {
            panic!("Expected Simple equation");
        }
    }
}