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
//! # Adaptive Grid Refinement API
//!
//! ## Module Purpose
//! This module provides a unified interface for various adaptive grid refinement algorithms
//! used in boundary value problem (BVP) solvers. It acts as a dispatcher that selects and
//! executes the appropriate grid refinement method based on solution characteristics and
//! user preferences.
//!
//! ## Key Features
//! - **Unified Interface**: Single entry point for all grid refinement algorithms
//! - **Multiple Algorithms**: Supports 6 different refinement strategies from simple to sophisticated
//! - **Automatic Selection**: Chooses optimal refinement based on problem characteristics
//! - **Solution Interpolation**: Provides initial guess on refined mesh through interpolation
//!
//! ## Main Structures
//! - [`GridRefinementMethod`]: Enum defining available refinement algorithms with their parameters
//!
//! ## Core Function
//! - [`new_grid`]: Main dispatcher function that creates refined mesh and interpolated solution
//!
//! ## Available Algorithms
//! 1. **DoublePoints**: Naive approach - doubles mesh density everywhere
//! 2. **Easy**: Simple gradient-based refinement with tolerance parameter
//! 3. **Pearson**: Classical boundary layer algorithm with smoothing
//! 4. **GrcarSmooke**: Advanced method considering both solution and derivative jumps
//! 5. **Sci**: SciPy-inspired residual-based refinement
//! 6. **TwoPoint**: FORTRAN TWOPNT-inspired algorithm with multiple criteria
//!
//! ## Algorithm Selection Guidelines
//! - Use `DoublePoints` for testing or when other methods fail
//! - Use `Easy` for simple problems with moderate gradients
//! - Use `Pearson` for boundary layer problems
//! - Use `GrcarSmooke` for combustion/reaction problems
//! - Use `Sci` when residual information is available
//! - Use `TwoPoint` for complex multi-scale problems
//!
//! ## Integration with BVP Solver
//! This module is called by the damped Newton-Raphson solver when:
//! - Newton iterations fail to converge on current mesh
//! - Solution gradients exceed specified thresholds
//! - Adaptive refinement is enabled in solver configuration
use crate;
use cratetwpnt_refinement;
use ;
/// Enumeration of available grid refinement algorithms
///
/// Each variant represents a different strategy for adaptive mesh refinement,
/// with associated parameters that control the refinement behavior.
///
/// # Algorithm Descriptions
/// - `DoublePoints`: Uniformly refines by adding midpoints to all intervals
/// - `Easy(tolerance)`: Refines based on solution gradient magnitude
/// - `Pearson(delta, safety)`: Classical boundary layer method with buffering
/// - `GrcarSmooke(d, g, C)`: Considers both solution jumps and derivative changes
/// - `Sci()`: Uses residual-based error estimation
/// - `TwoPoint(p1, p2, p3)`: Multi-criteria refinement with three parameters
/// Creates a refined mesh using the specified refinement algorithm
///
/// This is the main entry point for adaptive grid refinement. It analyzes the current
/// solution and mesh, then applies the chosen refinement algorithm to create a finer
/// mesh where needed.
///
/// # Arguments
/// * `method` - The grid refinement algorithm to use
/// * `y_DMatrix` - Current solution matrix (variables × mesh points)
/// * `x_mesh` - Current mesh points (spatial/temporal coordinates)
/// * `abs_tolerance` - Absolute tolerance for convergence checking
/// * `residuals` - Optional residual vector (required for Sci method)
///
/// # Returns
/// A tuple containing:
/// * `Vec<f64>` - New refined mesh points
/// * `DMatrix<f64>` - Interpolated solution on new mesh (initial guess)
/// * `usize` - Number of intervals that were refined
///
/// # Algorithm Selection
/// The function dispatches to the appropriate refinement algorithm based on the
/// `method` parameter. Each algorithm analyzes the solution differently:
///
/// - **Gradient-based** (Easy, Pearson, GrcarSmooke): Look at solution changes
/// - **Residual-based** (Sci): Uses equation residuals to guide refinement
/// - **Uniform** (DoublePoints): Refines everywhere regardless of solution
/// - **Multi-criteria** (TwoPoint): Combines multiple refinement indicators
///
/// # Interpolation Strategy
/// All methods provide linear interpolation of the old solution onto the new mesh
/// to create an initial guess for the next Newton iteration. This ensures the
/// refined solution starts close to the converged coarse solution.
///
/// # Performance Notes
/// - Refinement cost scales with number of mesh points and solution complexity
/// - Methods that add fewer points (Easy, Pearson) are faster but may need more iterations
/// - Uniform refinement (DoublePoints) is expensive but guaranteed to improve resolution