oxiflow 0.3.0

Generic PDE solving engine for transport, reaction and diffusion phenomena (∂u/∂t + ∇·F = S)
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
//! # Module `solver::scenario`
//!
//! Problem declaration — `Scenario` (WHAT pole, DD-020, issue #32).
//!
//! ## Design
//!
//! `Scenario` is unified: it handles both single-domain (J1) and multi-domain
//! (J3) problems. The single-domain case is the degenerate case with one element
//! in `domains` and empty `couplings`. Ergonomic helpers cover the J1 use case
//! without verbosity. At J3, additional domains and coupling operators are added
//! without any API change (DD-020).

use crate::boundary::BoundaryCondition;
use crate::context::error::OxiflowError;
use crate::context::variable::ContextVariable;
use crate::coupling::{CouplingOperator, Interface};
use crate::mesh::Mesh;
use crate::model::traits::PhysicalModel;

// ── DomainId ──────────────────────────────────────────────────────────────────

/// Typed identifier for a domain within a multi-domain scenario.
///
/// At J1, a single-domain scenario uses `DomainId::default()` ("default").
/// At J3, each domain gets a meaningful identifier for coupling resolution.
///
/// # Examples
///
/// ```rust
/// use oxiflow::solver::scenario::DomainId;
///
/// let id = DomainId::new("column");
/// assert_eq!(id.as_str(), "column");
///
/// let default_id = DomainId::default();
/// assert_eq!(default_id.as_str(), "default");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DomainId(String);

impl DomainId {
    /// Creates a new domain identifier.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Returns the identifier as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Default for DomainId {
    fn default() -> Self {
        Self("default".to_string())
    }
}

impl std::fmt::Display for DomainId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for DomainId {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

// ── Domain ────────────────────────────────────────────────────────────────────

/// Single physical domain: model + mesh + boundary conditions.
///
/// `boundary_conditions` is empty at J1. At J2, BCs are added via the
/// `Domain::with_boundary_conditions` builder.
///
/// # Serialisation
///
/// `Domain` does not implement `serde::Serialize` / `serde::Deserialize`.
/// It holds `Box<dyn PhysicalModel>`, `Box<dyn Mesh>`, and
/// `Box<dyn BoundaryCondition>` (trait objects), which cannot be serialised
/// directly. See `SimulationSnapshot` (DD-025 Option B).
#[non_exhaustive]
pub struct Domain {
    /// Unique identifier for this domain.
    pub id: DomainId,
    /// Physical model — declares and computes field equations.
    pub model: Box<dyn PhysicalModel>,
    /// Spatial mesh — INV-1.
    pub mesh: Box<dyn Mesh>,
    /// Boundary conditions applied after context calculation, before physics.
    pub boundary_conditions: Vec<Box<dyn BoundaryCondition>>,
}

impl Domain {
    /// Creates a new domain with the given id, model, and mesh.
    ///
    /// `boundary_conditions` is initialised to an empty list. Use
    /// `with_boundary_conditions` to attach BCs.
    pub fn new(
        id: impl Into<DomainId>,
        model: Box<dyn PhysicalModel>,
        mesh: Box<dyn Mesh>,
    ) -> Self {
        Self {
            id: id.into(),
            model,
            mesh,
            boundary_conditions: vec![],
        }
    }

    /// Attaches boundary conditions to this domain.
    ///
    /// Replaces any previously set boundary conditions.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let domain = Domain::new("column", model, mesh)
    ///     .with_boundary_conditions(vec![Box::new(inlet_bc), Box::new(outlet_bc)]);
    /// ```
    pub fn with_boundary_conditions(mut self, bcs: Vec<Box<dyn BoundaryCondition>>) -> Self {
        self.boundary_conditions = bcs;
        self
    }
}

// ── Scenario ──────────────────────────────────────────────────────────────────

/// Complete problem declaration — WHAT pole.
///
/// Holds one or more `Domain`s, coupling operators between them (J3), and
/// the simulation start time. Single-domain problems use the ergonomic helper
/// `Scenario::single()`. Multi-domain problems add domains and couplings without
/// any breaking change (DD-020).
///
/// `Scenario` is declarative — it contains no solving logic. The `Solver`
/// receives it and validates it via `context_requirements()` before solving.
///
/// # Serialisation
///
/// `Scenario` does not implement `serde::Serialize` / `serde::Deserialize`.
/// It contains `Vec<Domain>`, which holds trait objects (`Box<dyn PhysicalModel>`,
/// `Box<dyn Mesh>`). Restoring a simulation from a snapshot requires user code
/// to reconstruct `Scenario` from its own configuration and inject the physical
/// state from `SimulationSnapshot` (DD-025 Option B, v0.6.0).
///
/// # Examples
///
/// ```rust
/// use oxiflow::solver::scenario::Scenario;
/// use oxiflow::model::traits::{PhysicalModel, RequiresContext};
/// use oxiflow::context::variable::ContextVariable;
/// use oxiflow::context::value::ContextValue;
/// use oxiflow::context::compute::ComputeContext;
/// use oxiflow::context::error::OxiflowError;
/// use oxiflow::mesh::{Mesh, UniformGrid1D};
/// use nalgebra::DVector;
///
/// struct ConstantModel;
/// impl RequiresContext for ConstantModel {
///     fn required_variables(&self) -> Vec<ContextVariable> { vec![] }
/// }
/// impl PhysicalModel for ConstantModel {
///     fn compute_physics(&self, s: &ContextValue, _: &ComputeContext)
///         -> Result<ContextValue, OxiflowError> { Ok(s.clone()) }
///     fn initial_state(&self, mesh: &dyn Mesh) -> ContextValue {
///         ContextValue::ScalarField(DVector::from_element(mesh.n_dof(), 0.0))
///     }
///     fn name(&self) -> &str { "constant" }
/// }
///
/// let mesh = Box::new(UniformGrid1D::new(10, 0.0, 1.0).unwrap());
/// let scenario = Scenario::single(Box::new(ConstantModel), mesh);
/// assert_eq!(scenario.n_domains(), 1);
/// ```
pub struct Scenario {
    /// Physical domains — at least one required.
    domains: Vec<Domain>,
    /// Coupling operators between domains (J3, DD-011, INV-3).
    couplings: Vec<Box<dyn CouplingOperator>>,
    /// Interfaces shared between domains (J3, DD-011).
    interfaces: Vec<Interface>,
    /// Simulation start time.
    pub t_start: f64,
}

impl Scenario {
    // ── Constructors ──────────────────────────────────────────────────────────

    /// Creates a single-domain scenario (J1 default).
    ///
    /// Uses `DomainId::default()` as the domain identifier.
    /// `t_start` defaults to `0.0`.
    pub fn single(model: Box<dyn PhysicalModel>, mesh: Box<dyn Mesh>) -> Self {
        Self {
            domains: vec![Domain::new(DomainId::default(), model, mesh)],
            couplings: vec![],
            interfaces: vec![],
            t_start: 0.0,
        }
    }

    /// Creates a single-domain scenario with a custom start time.
    pub fn single_from(model: Box<dyn PhysicalModel>, mesh: Box<dyn Mesh>, t_start: f64) -> Self {
        Self {
            domains: vec![Domain::new(DomainId::default(), model, mesh)],
            couplings: vec![],
            interfaces: vec![],
            t_start,
        }
    }

    /// Creates a multi-domain scenario from a list of domains (J3 path).
    ///
    /// Domains are provided as pre-built `Domain` instances.
    pub fn multi(domains: Vec<Domain>) -> Result<Self, OxiflowError> {
        if domains.is_empty() {
            return Err(OxiflowError::InvalidDomain(
                "Scenario requires at least one domain".into(),
            ));
        }
        Ok(Self {
            domains,
            couplings: vec![],
            interfaces: vec![],
            t_start: 0.0,
        })
    }

    /// Sets the simulation start time.
    pub fn with_t_start(mut self, t_start: f64) -> Self {
        self.t_start = t_start;
        self
    }

    /// Adds a coupling operator and its associated interface (J3, INV-3).
    ///
    /// The interface is extracted from the operator via `CouplingOperator::interface()`.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let scenario = Scenario::multi(domains)?
    ///     .with_coupling(Box::new(my_coupling_op));
    /// ```
    pub fn with_coupling(mut self, coupling: Box<dyn CouplingOperator>) -> Self {
        self.interfaces.push(coupling.interface().clone());
        self.couplings.push(coupling);
        self
    }

    // ── Accessors ─────────────────────────────────────────────────────────────

    /// Returns the number of domains.
    pub fn n_domains(&self) -> usize {
        self.domains.len()
    }

    /// Returns the number of coupling operators.
    pub fn n_couplings(&self) -> usize {
        self.couplings.len()
    }

    /// Returns a reference to all coupling operators.
    pub fn couplings(&self) -> &[Box<dyn CouplingOperator>] {
        &self.couplings
    }

    /// Returns a reference to all interfaces.
    pub fn interfaces(&self) -> &[Interface] {
        &self.interfaces
    }

    /// Returns a reference to all domains.
    pub fn domains(&self) -> &[Domain] {
        &self.domains
    }

    /// Returns the single domain — convenience for J1 single-domain scenarios.
    ///
    /// # Errors
    ///
    /// Returns `OxiflowError::InvalidDomain` if the scenario has more than one domain.
    pub fn single_domain(&self) -> Result<&Domain, OxiflowError> {
        if self.domains.len() != 1 {
            return Err(OxiflowError::InvalidDomain(format!(
                "expected 1 domain, found {}",
                self.domains.len()
            )));
        }
        Ok(&self.domains[0])
    }

    // ── Context aggregation ───────────────────────────────────────────────────

    /// Aggregates context variable requirements from all domains.
    ///
    /// At J1: model requirements only.
    /// At J2: model + boundary condition requirements (deduplicated).
    /// At J3: + coupling operator requirements.
    pub fn context_requirements(&self) -> Vec<ContextVariable> {
        let mut requirements: Vec<ContextVariable> = self
            .domains
            .iter()
            .flat_map(|d| d.model.required_variables())
            .collect();

        // J2: extend with BC requirements
        self.domains.iter().for_each(|d| {
            requirements.extend(
                d.boundary_conditions
                    .iter()
                    .flat_map(|bc| bc.required_variables()),
            );
        });

        // J3: extend with coupling operator requirements
        self.couplings.iter().for_each(|c| {
            requirements.extend(c.required_variables());
        });

        requirements.sort_by(|a, b| format!("{a:?}").cmp(&format!("{b:?}")));
        requirements.dedup();
        requirements
    }

    // ── Validation ────────────────────────────────────────────────────────────

    /// Validates scenario consistency before solving.
    ///
    /// At J1: verifies at least one domain is present.
    /// At J3: will verify interface consistency and coupling coverage.
    pub fn validate(&self) -> Result<(), OxiflowError> {
        if self.domains.is_empty() {
            return Err(OxiflowError::InvalidDomain(
                "Scenario has no domains".into(),
            ));
        }
        for domain in &self.domains {
            if domain.mesh.n_dof() == 0 {
                return Err(OxiflowError::InvalidDomain(format!(
                    "domain '{}' has empty mesh",
                    domain.id
                )));
            }
        }
        Ok(())
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::compute::ComputeContext;
    use crate::context::error::OxiflowError;
    use crate::context::value::ContextValue;
    use crate::context::variable::ContextVariable;
    use crate::mesh::structured::UniformGrid1D;
    use crate::model::traits::{PhysicalModel, RequiresContext};
    use nalgebra::DVector;

    // ── Fixtures ──────────────────────────────────────────────────────────────

    struct NeedsTime;
    impl RequiresContext for NeedsTime {
        fn required_variables(&self) -> Vec<ContextVariable> {
            vec![ContextVariable::Time]
        }
    }
    impl PhysicalModel for NeedsTime {
        fn compute_physics(
            &self,
            s: &ContextValue,
            _: &ComputeContext,
        ) -> Result<ContextValue, OxiflowError> {
            Ok(s.clone())
        }
        fn initial_state(&self, mesh: &dyn Mesh) -> ContextValue {
            ContextValue::ScalarField(DVector::from_element(mesh.n_dof(), 0.0))
        }
        fn name(&self) -> &str {
            "needs_time"
        }
    }

    struct NeedsGradient;
    impl RequiresContext for NeedsGradient {
        fn required_variables(&self) -> Vec<ContextVariable> {
            vec![ContextVariable::SpatialGradient {
                dimension: 0,
                component: None,
            }]
        }
    }
    impl PhysicalModel for NeedsGradient {
        fn compute_physics(
            &self,
            s: &ContextValue,
            _: &ComputeContext,
        ) -> Result<ContextValue, OxiflowError> {
            Ok(s.clone())
        }
        fn initial_state(&self, mesh: &dyn Mesh) -> ContextValue {
            ContextValue::ScalarField(DVector::from_element(mesh.n_dof(), 0.0))
        }
        fn name(&self) -> &str {
            "needs_gradient"
        }
    }

    fn make_mesh() -> Box<dyn Mesh> {
        Box::new(UniformGrid1D::new(10, 0.0, 1.0).unwrap())
    }

    // ── DomainId ──────────────────────────────────────────────────────────────

    #[test]
    fn domain_id_new() {
        let id = DomainId::new("col_a");
        assert_eq!(id.as_str(), "col_a");
    }

    #[test]
    fn domain_id_default_is_default() {
        assert_eq!(DomainId::default().as_str(), "default");
    }

    #[test]
    fn domain_id_from_str() {
        let id: DomainId = "lake".into();
        assert_eq!(id.as_str(), "lake");
    }

    #[test]
    fn domain_id_display() {
        assert_eq!(format!("{}", DomainId::new("river")), "river");
    }

    #[test]
    fn domain_id_equality() {
        assert_eq!(DomainId::new("a"), DomainId::new("a"));
        assert_ne!(DomainId::new("a"), DomainId::new("b"));
    }

    // ── Scenario::single ──────────────────────────────────────────────────────

    #[test]
    fn single_creates_one_domain() {
        let s = Scenario::single(Box::new(NeedsTime), make_mesh());
        assert_eq!(s.n_domains(), 1);
    }

    #[test]
    fn single_t_start_defaults_to_zero() {
        let s = Scenario::single(Box::new(NeedsTime), make_mesh());
        assert_eq!(s.t_start, 0.0);
    }

    #[test]
    fn single_from_sets_t_start() {
        let s = Scenario::single_from(Box::new(NeedsTime), make_mesh(), 5.0);
        assert_eq!(s.t_start, 5.0);
    }

    #[test]
    fn with_t_start_builder() {
        let s = Scenario::single(Box::new(NeedsTime), make_mesh()).with_t_start(2.5);
        assert_eq!(s.t_start, 2.5);
    }

    // ── Scenario::multi ───────────────────────────────────────────────────────

    #[test]
    fn multi_with_two_domains() {
        let d1 = Domain::new("a", Box::new(NeedsTime), make_mesh());
        let d2 = Domain::new("b", Box::new(NeedsGradient), make_mesh());
        let s = Scenario::multi(vec![d1, d2]).unwrap();
        assert_eq!(s.n_domains(), 2);
    }

    #[test]
    fn multi_empty_returns_error() {
        assert!(Scenario::multi(vec![]).is_err());
    }

    // ── single_domain ─────────────────────────────────────────────────────────

    #[test]
    fn single_domain_ok_for_one_domain() {
        let s = Scenario::single(Box::new(NeedsTime), make_mesh());
        assert!(s.single_domain().is_ok());
        assert_eq!(s.single_domain().unwrap().id, DomainId::default());
    }

    #[test]
    fn single_domain_err_for_multi() {
        let d1 = Domain::new("a", Box::new(NeedsTime), make_mesh());
        let d2 = Domain::new("b", Box::new(NeedsGradient), make_mesh());
        let s = Scenario::multi(vec![d1, d2]).unwrap();
        assert!(s.single_domain().is_err());
    }

    // ── context_requirements ─────────────────────────────────────────────────

    #[test]
    fn requirements_from_single_domain() {
        let s = Scenario::single(Box::new(NeedsTime), make_mesh());
        let reqs = s.context_requirements();
        assert!(reqs.contains(&ContextVariable::Time));
    }

    #[test]
    fn requirements_aggregated_and_deduped_across_domains() {
        let d1 = Domain::new("a", Box::new(NeedsTime), make_mesh());
        let d2 = Domain::new("b", Box::new(NeedsTime), make_mesh());
        let s = Scenario::multi(vec![d1, d2]).unwrap();
        // Time appears in both domains — dedup → only once
        let reqs = s.context_requirements();
        assert_eq!(
            reqs.iter().filter(|v| **v == ContextVariable::Time).count(),
            1
        );
    }

    #[test]
    fn requirements_union_of_all_domains() {
        let d1 = Domain::new("a", Box::new(NeedsTime), make_mesh());
        let d2 = Domain::new("b", Box::new(NeedsGradient), make_mesh());
        let s = Scenario::multi(vec![d1, d2]).unwrap();
        let reqs = s.context_requirements();
        assert!(reqs.contains(&ContextVariable::Time));
        assert!(reqs.contains(&ContextVariable::SpatialGradient {
            dimension: 0,
            component: None
        }));
    }

    // ── validate ──────────────────────────────────────────────────────────────

    #[test]
    fn validate_ok_for_valid_scenario() {
        let s = Scenario::single(Box::new(NeedsTime), make_mesh());
        assert!(s.validate().is_ok());
    }

    // ── BoundaryCondition integration ─────────────────────────────────────────

    use crate::boundary::BoundaryCondition;
    use crate::context::compute::ComputeContext as Ctx;

    #[derive(Debug)]
    struct TimeDependentBC;

    impl RequiresContext for TimeDependentBC {
        fn required_variables(&self) -> Vec<ContextVariable> {
            vec![ContextVariable::Time]
        }
    }

    impl BoundaryCondition for TimeDependentBC {
        fn boundary_type(&self) -> crate::boundary::BoundaryType {
            crate::boundary::BoundaryType::Dirichlet
        }
        fn apply(
            &self,
            _state: &mut DVector<f64>,
            _ctx: &Ctx,
            _mesh: &dyn Mesh,
        ) -> Result<(), OxiflowError> {
            Ok(())
        }
    }

    #[test]
    fn domain_with_boundary_conditions_stores_bcs() {
        let bc: Box<dyn BoundaryCondition> = Box::new(TimeDependentBC);
        let domain =
            Domain::new("col", Box::new(NeedsTime), make_mesh()).with_boundary_conditions(vec![bc]);
        assert_eq!(domain.boundary_conditions.len(), 1);
    }

    #[test]
    fn domain_new_has_empty_bcs() {
        let domain = Domain::new("col", Box::new(NeedsTime), make_mesh());
        assert!(domain.boundary_conditions.is_empty());
    }

    #[test]
    fn context_requirements_includes_bc_variables() {
        let bc: Box<dyn BoundaryCondition> = Box::new(TimeDependentBC);
        let domain = Domain::new("col", Box::new(NeedsGradient), make_mesh())
            .with_boundary_conditions(vec![bc]);
        let scenario = Scenario::multi(vec![domain]).unwrap();
        let reqs = scenario.context_requirements();
        // NeedsGradient requires SpatialGradient; TimeDependentBC requires Time.
        assert!(reqs.contains(&ContextVariable::Time));
        assert!(reqs.contains(&ContextVariable::SpatialGradient {
            dimension: 0,
            component: None
        }));
    }

    #[test]
    fn context_requirements_deduplicates_bc_and_model_variables() {
        // Both model and BC require Time — must appear only once.
        let bc: Box<dyn BoundaryCondition> = Box::new(TimeDependentBC);
        let domain =
            Domain::new("col", Box::new(NeedsTime), make_mesh()).with_boundary_conditions(vec![bc]);
        let scenario = Scenario::multi(vec![domain]).unwrap();
        let reqs = scenario.context_requirements();
        assert_eq!(
            reqs.iter().filter(|v| **v == ContextVariable::Time).count(),
            1
        );
    }
}