scirs2_integrate/ode/utils/stiffness/
integration.rs

1//! Integration-related stiffness detection utilities
2
3use crate::IntegrateFloat;
4
5/// The current state of an adaptive method
6#[derive(Debug, Clone)]
7pub struct AdaptiveMethodState<F: IntegrateFloat> {
8    /// Current method type
9    pub method_type: AdaptiveMethodType,
10    /// Steps since last method switch
11    pub steps_since_switch: usize,
12    /// Current order of the method
13    pub order: usize,
14    /// Stiffness detector configuration
15    pub config: crate::ode::utils::stiffness::StiffnessDetectionConfig<F>,
16    /// Stiffness detector
17    pub detector: crate::ode::utils::stiffness::StiffnessDetector<F>,
18}
19
20impl<F: IntegrateFloat> AdaptiveMethodState<F> {
21    /// Create with configuration
22    pub fn with_config(config: crate::ode::utils::stiffness::StiffnessDetectionConfig<F>) -> Self {
23        let detector = crate::ode::utils::stiffness::StiffnessDetector::with_config(config.clone());
24        Self {
25            method_type: AdaptiveMethodType::Adams,
26            steps_since_switch: 0,
27            order: 1, // Start with order 1
28            config,
29            detector,
30        }
31    }
32
33    /// Record a step
34    pub fn record_step(&mut self, _errorestimate: F) {
35        self.steps_since_switch += 1;
36    }
37
38    /// Check if method should switch
39    pub fn check_method_switch(&self) -> Option<AdaptiveMethodType> {
40        if self.steps_since_switch > 10 {
41            // Simple switching logic for now
42            None
43        } else {
44            None
45        }
46    }
47
48    /// Switch to a new method
49    pub fn switch_method(
50        &mut self,
51        new_method: AdaptiveMethodType,
52        _steps: usize,
53    ) -> crate::error::IntegrateResult<()> {
54        self.method_type = new_method;
55        self.steps_since_switch = 0;
56        Ok(())
57    }
58
59    /// Generate a diagnostic message about the current state
60    pub fn generate_diagnostic_message(&self) -> String {
61        format!(
62            "AdaptiveMethodState: method={:?}, steps_since_switch={}",
63            self.method_type, self.steps_since_switch
64        )
65    }
66}
67
68/// Type of adaptive method
69#[derive(Debug, Clone, Copy, PartialEq)]
70pub enum AdaptiveMethodType {
71    /// Adams methods for non-stiff problems
72    Adams,
73    /// BDF methods for stiff problems
74    BDF,
75    /// Runge-Kutta methods
76    RungeKutta,
77    /// Implicit methods (Radau, etc.)
78    Implicit,
79    /// Explicit methods (Adams, etc.)
80    Explicit,
81}