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
//! Port-Hamiltonian Systems and Structure-Preserving Integration
//!
//! This module implements the port-Hamiltonian (pH) framework for modeling and
//! simulating physical systems that obey energy balance laws, combined with
//! structure-preserving numerical integrators that maintain these laws at the
//! discrete level.
//!
//! # Port-Hamiltonian Systems
//!
//! A port-Hamiltonian system is described by:
//!
//! ```text
//! dx/dt = (J(x) - R(x)) ∇H(x) + B(x) u
//! y = B(x)^T ∇H(x)
//! ```
//!
//! where:
//! - `x ∈ ℝⁿ` is the state (energy variables)
//! - `H: ℝⁿ → ℝ` is the Hamiltonian (stored energy)
//! - `J(x) = -J(x)^T` is the skew-symmetric interconnection matrix
//! - `R(x) = R(x)^T ≥ 0` is the positive semi-definite dissipation matrix
//! - `B(x)` is the input/output (port) matrix
//! - `u` is the port input (external forces, voltages, etc.)
//! - `y` is the power-conjugate output
//!
//! ## Energy Balance
//!
//! The fundamental property of pH systems is the **power balance**:
//! ```text
//! dH/dt = -∇H^T R ∇H + y^T u
//! ≤ y^T u (passivity)
//! ```
//! The system is passive: it can only dissipate or store, not generate, energy.
//!
//! # Structure-Preserving Integrators
//!
//! To maintain the energy balance at the discrete level, we provide:
//!
//! | Method | Energy preservation | Order | Implicit |
//! |--------|--------------------|---------| --------|
//! | [`DiscreteGradientGonzalez`] | Exact (conservative) | 2 | Yes |
//! | [`DiscreteGradientItohAbe`] | Exact (conservative) | 2 | Yes |
//! | [`AverageVectorField`] | Exact (conservative) | 2 | Yes |
//! | [`ImplicitMidpoint`] | Quadratic H exactly | 2 | Yes |
//! | [`StormerVerletPH`] | Symplectic | 2 | Partially |
//! | [`Rattle`] | Symplectic + constraints | 2 | Yes |
//!
//! # Example: Simple Pendulum
//!
//! ```rust
//! use scirs2_integrate::port_hamiltonian::{
//! PortHamiltonianBuilder, DiscreteGradientGonzalez,
//! };
//! use scirs2_core::ndarray::array;
//!
//! // Build a conservative pendulum (m=1, l=1, g=9.81, no damping)
//! let system = PortHamiltonianBuilder::new(2, 1)
//! .with_j(|_x| Ok(array![[0.0, 1.0], [-1.0, 0.0]]))
//! .with_r(|_x| Ok(array![[0.0, 0.0], [0.0, 0.0]]))
//! .with_hamiltonian(|x| {
//! let q = x[0]; let p = x[1];
//! Ok(p * p / 2.0 + 9.81 * (1.0 - q.cos()))
//! })
//! .with_grad_hamiltonian(|x| {
//! Ok(array![9.81 * x[0].sin(), x[1]])
//! })
//! .with_b(array![[0.0], [1.0]])
//! .build()
//! .expect("Failed to build system");
//!
//! let integrator = DiscreteGradientGonzalez::new();
//! let result = integrator
//! .integrate(&system, &[0.5, 0.0], 0.0, 10.0, 0.05, None)
//! .expect("Integration failed");
//!
//! // Check energy conservation
//! let h0 = result.energy[0];
//! let h_final = result.energy.last().copied().unwrap_or(h0);
//! assert!((h_final - h0).abs() < 1e-10, "Energy drift: {}", (h_final - h0).abs());
//! ```
//!
//! # Example: RLC Circuit
//!
//! ```rust
//! use scirs2_integrate::port_hamiltonian::{rlc_circuit_ph, AverageVectorField};
//!
//! // Series RLC: L=1mH, C=1μF, R=100Ω
//! let circuit = rlc_circuit_ph(1e-3, 1e-6, 100.0).expect("Failed to create RLC");
//!
//! // Initial state: capacitor charged to 1V (q_c = C * V = 1μC), no current
//! let x0 = vec![1e-6, 0.0];
//!
//! let avf = AverageVectorField::new();
//! let result = avf
//! .integrate(&circuit, &x0, 0.0, 1e-4, 1e-7, None)
//! .expect("Integration failed");
//!
//! // Energy must decay (resistor dissipates)
//! let h_final = result.energy.last().copied().unwrap_or(0.0);
//! assert!(h_final < result.energy[0]);
//! ```
// ─── Re-exports from system ───────────────────────────────────────────────────
pub use ;
// ─── Re-exports from integrators ─────────────────────────────────────────────
pub use ;
// ─── Re-exports from dissipation ─────────────────────────────────────────────
pub use ;
// ─── Re-exports from examples ────────────────────────────────────────────────
pub use ;