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
//! This module provides functionality for working with the `Dae` structure,
//! which is part of the Abstract Syntax Tree (AST) representation in the
//! Differential-Algebraic Equation (DAE) domain. It is used to model and
//! manipulate DAE-related constructs within the application.
use crate::dae::ast::Dae;
use crate::ir::analysis::condition_finder::ConditionFinder;
use crate::ir::analysis::state_finder::StateFinder;
use crate::ir::ast::{
    Causality, ClassDefinition, Component, Equation, Expression, Name, Statement, Token,
    Variability,
};
use crate::ir::error::IrError;
use crate::ir::transform::constants::BUILTIN_REINIT;
use crate::ir::transform::eval::eval_boolean;
use crate::ir::visitor::{MutVisitable, Visitable, Visitor};
use git_version::git_version;
use indexmap::IndexMap;
use std::collections::HashSet;

use anyhow::Result;

// =============================================================================
// Defined Variable Collector Visitor
// =============================================================================

/// Visitor that collects variable names from LHS of simple equations.
struct DefinedVariableCollector {
    defined: HashSet<String>,
}

impl DefinedVariableCollector {
    fn new() -> Self {
        Self {
            defined: HashSet::new(),
        }
    }

    fn into_defined(self) -> HashSet<String> {
        self.defined
    }
}

impl Visitor for DefinedVariableCollector {
    fn enter_equation(&mut self, node: &Equation) {
        if let Equation::Simple {
            lhs: Expression::ComponentReference(cref),
            ..
        } = node
        {
            self.defined.insert(cref.to_string());
        }
    }
}

const GIT_VERSION: &str = git_version!(
    args = ["--tags", "--always", "--dirty=-dirty"],
    fallback = "unknown"
);

/// Check if an equation assigns to a filtered component (on the LHS).
/// Used to filter out equations that define variables that were removed due to false conditions.
fn equation_assigns_to_filtered(eq: &Equation, filtered: &HashSet<String>) -> bool {
    match eq {
        Equation::Simple { lhs, .. } => {
            if let Expression::ComponentReference(cref) = lhs {
                // Get the base component name (first part of the reference)
                if let Some(first_part) = cref.parts.first() {
                    return filtered.contains(&first_part.ident.text);
                }
            }
            false
        }
        // For other equation types, don't filter (they may have multiple LHS or complex structure)
        _ => false,
    }
}

/// Recursively filter equations, removing those that assign to filtered components.
fn filter_equations(equations: Vec<Equation>, filtered: &HashSet<String>) -> Vec<Equation> {
    equations
        .into_iter()
        .filter(|eq| !equation_assigns_to_filtered(eq, filtered))
        .map(|eq| match eq {
            Equation::If {
                cond_blocks,
                else_block,
            } => Equation::If {
                cond_blocks: cond_blocks
                    .into_iter()
                    .map(|mut block| {
                        block.eqs = filter_equations(block.eqs, filtered);
                        block
                    })
                    .collect(),
                else_block: else_block.map(|eqs| filter_equations(eqs, filtered)),
            },
            Equation::For {
                indices,
                equations: inner,
            } => Equation::For {
                indices,
                equations: filter_equations(inner, filtered),
            },
            Equation::When(blocks) => Equation::When(
                blocks
                    .into_iter()
                    .map(|mut block| {
                        block.eqs = filter_equations(block.eqs, filtered);
                        block
                    })
                    .collect(),
            ),
            other => other,
        })
        .collect()
}

/// Collect variable names that appear on the left-hand side of simple equations.
/// These variables have defining equations and should not be treated as external inputs
/// even if they have Input causality (e.g., signal connector inputs with connect equations).
///
/// Uses the visitor pattern for clean, maintainable traversal.
fn collect_defined_variables(equations: &[Equation]) -> HashSet<String> {
    let mut collector = DefinedVariableCollector::new();
    for eq in equations {
        eq.accept(&mut collector);
    }
    collector.into_defined()
}

/// Expand an array component into individual scalar components.
/// For example, `x[3]` becomes `x[1]`, `x[2]`, `x[3]`.
fn expand_array_component(comp: &Component) -> Vec<(String, Component)> {
    if comp.shape.is_empty() {
        // Scalar component - return as-is
        return vec![(comp.name.clone(), comp.clone())];
    }

    // Calculate total number of elements
    let total_elements: usize = comp.shape.iter().product();
    if total_elements == 0 {
        // Empty array (e.g., x[0]) - return nothing
        return vec![];
    }

    let mut result = Vec::with_capacity(total_elements);

    // Generate all index combinations
    let indices = generate_indices(&comp.shape);

    for idx in indices {
        // Create subscripted name like "x[1]" or "x[1,2]"
        let subscript_str = idx
            .iter()
            .map(|i| i.to_string())
            .collect::<Vec<_>>()
            .join(",");
        let scalar_name = format!("{}[{}]", comp.name, subscript_str);

        // Create a scalar component (empty shape)
        let mut scalar_comp = comp.clone();
        scalar_comp.name = scalar_name.clone();
        scalar_comp.shape = vec![]; // Now it's a scalar

        // If the start value is an array, extract the corresponding element
        if !matches!(comp.start, Expression::Empty) {
            scalar_comp.start = extract_array_element(&comp.start, &idx);
        }

        result.push((scalar_name, scalar_comp));
    }

    result
}

/// Generate all index combinations for a given shape.
/// For shape [2, 3], generates [[1,1], [1,2], [1,3], [2,1], [2,2], [2,3]]
fn generate_indices(shape: &[usize]) -> Vec<Vec<usize>> {
    if shape.is_empty() {
        return vec![vec![]];
    }

    let mut result = Vec::new();
    generate_indices_recursive(shape, 0, &mut vec![], &mut result);
    result
}

fn generate_indices_recursive(
    shape: &[usize],
    dim: usize,
    current: &mut Vec<usize>,
    result: &mut Vec<Vec<usize>>,
) {
    if dim >= shape.len() {
        result.push(current.clone());
        return;
    }

    for i in 1..=shape[dim] {
        current.push(i);
        generate_indices_recursive(shape, dim + 1, current, result);
        current.pop();
    }
}

/// Extract an element from an array expression given indices.
fn extract_array_element(expr: &Expression, indices: &[usize]) -> Expression {
    if indices.is_empty() {
        return expr.clone();
    }

    match expr {
        Expression::Array { elements, .. } => {
            let idx = indices[0];
            if idx > 0 && idx <= elements.len() {
                if indices.len() == 1 {
                    elements[idx - 1].clone()
                } else {
                    extract_array_element(&elements[idx - 1], &indices[1..])
                }
            } else {
                expr.clone()
            }
        }
        _ => {
            // For non-array expressions, create a subscripted reference
            // This handles cases like `x_start` where start is a parameter
            expr.clone()
        }
    }
}

/// Creates a DAE (Differential-Algebraic Equation) representation from a flattened class definition.
///
/// This function transforms a flattened Modelica class into a structured DAE representation suitable
/// for numerical solving. It performs the following transformations:
///
/// - Identifies state variables (those appearing in `der()` calls) and their derivatives
/// - Classifies components by variability (parameters, constants, discrete, continuous)
/// - Classifies components by causality (inputs, outputs, algebraic variables)
/// - Finds and extracts conditions from when/if clauses
/// - Processes `reinit` statements in when clauses
/// - Creates previous value variables for discrete and state variables
/// - Collects all equations into the appropriate DAE categories
///
/// # Arguments
///
/// * `fclass` - A mutable reference to a flattened class definition (output from the `flatten` function)
///
/// # Returns
///
/// * `Result<Dae>` - The DAE representation on success, or an error if:
///   - Connection equations are not yet expanded (not implemented)
///   - Invalid reinit function calls are encountered
///
/// # Errors
///
/// Returns an error if connection equations are encountered (they should be expanded during
/// flattening but this feature is not yet implemented).
pub fn create_dae(fclass: &mut ClassDefinition) -> Result<Dae> {
    // create default Dae struct
    let mut dae = Dae {
        model_name: fclass.name.text.clone(),
        rumoca_version: env!("CARGO_PKG_VERSION").to_string(),
        git_version: GIT_VERSION.to_string(),
        t: Component {
            name: "t".to_string(),
            type_name: Name {
                name: vec![Token {
                    text: "Real".to_string(),
                    ..Default::default()
                }],
            },
            ..Default::default()
        },
        ..Default::default()
    };

    // run statefinder to find states and replace
    // derivative references
    let mut state_finder = StateFinder::default();
    fclass.accept_mut(&mut state_finder);

    // find conditions
    let mut condition_finder = ConditionFinder::default();
    fclass.accept_mut(&mut condition_finder);

    // Find variables that have defining equations (appear on LHS of simple equations)
    // These should be treated as algebraic variables, not inputs, even if they have Input causality
    let defined_variables = collect_defined_variables(&fclass.equations);

    // First pass: Collect all parameters for condition evaluation
    // Parameters are needed to evaluate conditional component expressions like `if use_reset`
    let mut all_params: IndexMap<String, Component> = IndexMap::new();
    for (_, comp) in &fclass.components {
        if matches!(
            comp.variability,
            Variability::Parameter(..) | Variability::Constant(..)
        ) {
            let expanded = expand_array_component(comp);
            for (name, c) in expanded {
                all_params.insert(name, c);
            }
        }
    }

    // Track components that are filtered out due to false conditions
    // These will be used to filter equations that assign to them
    let mut filtered_components: HashSet<String> = HashSet::new();

    // handle components - expand arrays to scalar components
    for (_, comp) in &fclass.components {
        // Check conditional component: if condition evaluates to false, skip this component
        if let Some(ref cond_expr) = comp.condition {
            match eval_boolean(cond_expr, &all_params) {
                Some(false) => {
                    // Condition is false - skip this component entirely
                    // Track the component name so we can filter equations that assign to it
                    filtered_components.insert(comp.name.clone());
                    continue;
                }
                Some(true) => {
                    // Condition is true - include the component
                }
                None => {
                    // Condition can't be evaluated at compile time - include the component
                    // This is the conservative approach (same as before)
                }
            }
        }

        // Expand array components to individual scalars
        let expanded = expand_array_component(comp);

        for (scalar_name, scalar_comp) in expanded {
            match scalar_comp.variability {
                Variability::Parameter(..) => {
                    dae.p.insert(scalar_name, scalar_comp);
                }
                Variability::Constant(..) => {
                    dae.cp.insert(scalar_name, scalar_comp);
                }
                Variability::Discrete(..) => {
                    dae.m.insert(scalar_name, scalar_comp);
                }
                Variability::Empty => {
                    // Check causality, but also check if the variable has a defining equation
                    match scalar_comp.causality {
                        Causality::Input(..) => {
                            // Determine if this is a top-level input or a sub-component input.
                            // Top-level inputs are declared directly in the model (no dot in base name).
                            // Sub-component inputs are from instantiated components (dot in base name).
                            //
                            // Per Modelica spec: "the input prefix defines that values for such a
                            // variable have to be provided from the simulation environment."
                            // This means top-level inputs are ALWAYS external inputs, even if they
                            // appear on the LHS of equations (like in DeMultiplex: [u] = [y1; y2]).
                            //
                            // Sub-component inputs that have connect equations become internal signals
                            // (algebraic variables) because they are connected within the model.
                            let base_name = &comp.name; // Original name before array expansion
                            let is_top_level = !base_name.contains('.');

                            if is_top_level {
                                // Top-level input - always external input per Modelica spec
                                dae.u.insert(scalar_name, scalar_comp);
                            } else if defined_variables.contains(&scalar_name) {
                                // Sub-component input with defining equation - algebraic variable
                                dae.y.insert(scalar_name, scalar_comp);
                            } else {
                                // Sub-component input without defining equation - external input
                                dae.u.insert(scalar_name, scalar_comp);
                            }
                        }
                        Causality::Output(..) | Causality::Empty => {
                            // For outputs and regular variables, check if it's a state
                            // Check both the original array name and the scalar name for states
                            let base_name = comp.name.clone();
                            if state_finder.states.contains(&base_name)
                                || state_finder.states.contains(&scalar_name)
                            {
                                // Add state variable only - derivatives remain as der() calls in equations
                                dae.x.insert(scalar_name, scalar_comp);
                            } else {
                                dae.y.insert(scalar_name, scalar_comp);
                            }
                        }
                    }
                }
            }
        }
    }

    // handle conditions and relations
    dae.c = condition_finder.conditions.clone();
    dae.fc = condition_finder.expressions.clone();

    // Build set of variables to exclude from BLT matching
    // (parameters, constants, inputs, states, and "time" should not be solved for)
    // States are excluded because their values come from integration, not algebraic equations
    let mut exclude_from_matching: HashSet<String> = HashSet::new();
    for name in dae.p.keys() {
        exclude_from_matching.insert(name.clone());
    }
    for name in dae.cp.keys() {
        exclude_from_matching.insert(name.clone());
    }
    for name in dae.u.keys() {
        exclude_from_matching.insert(name.clone());
    }
    for name in dae.x.keys() {
        exclude_from_matching.insert(name.clone());
    }
    exclude_from_matching.insert("time".to_string());

    // Apply structural transformation to reorder and normalize equations
    let transformed_equations =
        crate::ir::structural::blt_transform(fclass.equations.clone(), &exclude_from_matching);

    // Filter out equations that assign to conditional components that were removed
    let filtered_equations = if filtered_components.is_empty() {
        transformed_equations
    } else {
        filter_equations(transformed_equations, &filtered_components)
    };

    // handle equations
    for eq in &filtered_equations {
        match &eq {
            Equation::Simple { .. } => {
                dae.fx.push(eq.clone());
            }
            Equation::If { .. } => {
                dae.fx.push(eq.clone());
            }
            Equation::For { .. } => {
                // For equations are passed through directly - they will be
                // either expanded by the backend or serialized as-is
                dae.fx.push(eq.clone());
            }
            Equation::Connect { .. } => {
                return Err(IrError::UnexpandedConnectionEquation.into());
            }
            Equation::When(blocks) => {
                for block in blocks {
                    for eq in &block.eqs {
                        match eq {
                            Equation::FunctionCall { comp, args } => {
                                let name = comp.to_string();
                                if name == BUILTIN_REINIT {
                                    let cond_name = match &block.cond {
                                        Expression::ComponentReference(cref) => cref.to_string(),
                                        other => {
                                            let loc = other
                                                .get_location()
                                                .map(|l| {
                                                    format!(
                                                        " at {}:{}:{}",
                                                        l.file_name, l.start_line, l.start_column
                                                    )
                                                })
                                                .unwrap_or_default();
                                            anyhow::bail!(
                                                "Unsupported condition type in 'when' block{}. \
                                                 Expected a component reference.",
                                                loc
                                            )
                                        }
                                    };
                                    if args.len() != 2 {
                                        return Err(
                                            IrError::InvalidReinitArgCount(args.len()).into()
                                        );
                                    }
                                    match &args[0] {
                                        Expression::ComponentReference(cref) => {
                                            dae.fr.insert(
                                                cond_name,
                                                Statement::Assignment {
                                                    comp: cref.clone(),
                                                    value: args[1].clone(),
                                                },
                                            );
                                        }
                                        _ => {
                                            return Err(IrError::InvalidReinitFirstArg(format!(
                                                "{:?}",
                                                args[0]
                                            ))
                                            .into());
                                        }
                                    }
                                }
                            }
                            Equation::Simple { lhs, rhs } => {
                                // Handle direct variable assignments in when blocks
                                // e.g., when trigger then y = expr; end when;
                                let cond_name = match &block.cond {
                                    Expression::ComponentReference(cref) => cref.to_string(),
                                    other => {
                                        let loc = other
                                            .get_location()
                                            .map(|l| {
                                                format!(
                                                    " at {}:{}:{}",
                                                    l.file_name, l.start_line, l.start_column
                                                )
                                            })
                                            .unwrap_or_default();
                                        anyhow::bail!(
                                            "Unsupported condition type in 'when' block{}. \
                                             Expected a component reference.",
                                            loc
                                        )
                                    }
                                };
                                // Convert lhs to ComponentReference for assignment
                                match lhs {
                                    Expression::ComponentReference(cref) => {
                                        dae.fr.insert(
                                            format!("{}_{}", cond_name, cref),
                                            Statement::Assignment {
                                                comp: cref.clone(),
                                                value: rhs.clone(),
                                            },
                                        );
                                    }
                                    Expression::Tuple { elements } => {
                                        // Handle tuple assignments like (a, b) = func()
                                        // Each tuple element gets its own assignment in fr.
                                        // Do NOT add to fz as that would double-count equations.
                                        for (i, elem) in elements.iter().enumerate() {
                                            if let Expression::ComponentReference(cref) = elem {
                                                // Create indexed access to RHS if it's a tuple result
                                                dae.fr.insert(
                                                    format!("{}_tuple_{}", cond_name, i),
                                                    Statement::Assignment {
                                                        comp: cref.clone(),
                                                        value: rhs.clone(), // Will be handled by backend
                                                    },
                                                );
                                            }
                                        }
                                    }
                                    _ => {
                                        // For other complex LHS patterns, add as event equation
                                        dae.fz.push(eq.clone());
                                    }
                                }
                            }
                            Equation::If { .. } | Equation::For { .. } => {
                                // Pass through if/for equations inside when blocks as event equations
                                dae.fz.push(eq.clone());
                            }
                            other => {
                                let loc = other
                                    .get_location()
                                    .map(|l| {
                                        format!(
                                            " at {}:{}:{}",
                                            l.file_name, l.start_line, l.start_column
                                        )
                                    })
                                    .unwrap_or_default();
                                anyhow::bail!(
                                    "Unsupported equation type in 'when' block{}. \
                                     Only assignments, 'reinit', 'if' and 'for' are currently supported.",
                                    loc
                                )
                            }
                        }
                    }
                }
            }
            _ => {}
        }
    }

    // Handle initial equations
    for eq in &fclass.initial_equations {
        match eq {
            Equation::Simple { .. } | Equation::For { .. } | Equation::If { .. } => {
                dae.fx_init.push(eq.clone());
            }
            _ => {
                // Other equation types in initial section are less common
                // but we'll pass them through
                dae.fx_init.push(eq.clone());
            }
        }
    }

    Ok(dae)
}