scirs2_integrate/visualization/
types.rs

1//! Basic plot types and data structures for visualization
2//!
3//! This module contains the fundamental data structures used across the visualization system.
4
5use crate::analysis::BifurcationPoint;
6use scirs2_core::ndarray::{Array1, Array2, Array3};
7use std::collections::HashMap;
8
9/// Data structure for plotting 2D phase space
10#[derive(Debug, Clone)]
11pub struct PhaseSpacePlot {
12    /// X coordinates
13    pub x: Vec<f64>,
14    /// Y coordinates
15    pub y: Vec<f64>,
16    /// Optional color values for each point
17    pub colors: Option<Vec<f64>>,
18    /// Trajectory metadata
19    pub metadata: PlotMetadata,
20}
21
22/// Data structure for bifurcation diagrams
23#[derive(Debug, Clone)]
24pub struct BifurcationDiagram {
25    /// Parameter values
26    pub parameters: Vec<f64>,
27    /// State values (can be multiple branches)
28    pub states: Vec<Vec<f64>>,
29    /// Stability information for each point
30    pub stability: Vec<bool>,
31    /// Bifurcation points
32    pub bifurcation_points: Vec<BifurcationPoint>,
33}
34
35/// Visualization metadata
36#[derive(Debug, Clone)]
37pub struct PlotMetadata {
38    /// Plot title
39    pub title: String,
40    /// X-axis label
41    pub xlabel: String,
42    /// Y-axis label
43    pub ylabel: String,
44    /// Additional annotations
45    pub annotations: HashMap<String, String>,
46}
47
48impl Default for PlotMetadata {
49    fn default() -> Self {
50        Self {
51            title: "Numerical Integration Result".to_string(),
52            xlabel: "X".to_string(),
53            ylabel: "Y".to_string(),
54            annotations: HashMap::new(),
55        }
56    }
57}
58
59/// Field visualization for 2D vector fields
60#[derive(Debug, Clone)]
61pub struct VectorFieldPlot {
62    /// Grid x coordinates
63    pub x_grid: Array2<f64>,
64    /// Grid y coordinates
65    pub y_grid: Array2<f64>,
66    /// X components of vectors
67    pub u: Array2<f64>,
68    /// Y components of vectors
69    pub v: Array2<f64>,
70    /// Magnitude for color coding
71    pub magnitude: Array2<f64>,
72    /// Plot metadata
73    pub metadata: PlotMetadata,
74}
75
76/// Heat map visualization for scalar fields
77#[derive(Debug, Clone)]
78pub struct HeatMapPlot {
79    /// X coordinates
80    pub x: Array1<f64>,
81    /// Y coordinates
82    pub y: Array1<f64>,
83    /// Z values (scalar field)
84    pub z: Array2<f64>,
85    /// Plot metadata
86    pub metadata: PlotMetadata,
87}
88
89/// 3D surface plot data
90#[derive(Debug, Clone)]
91pub struct SurfacePlot {
92    /// X grid
93    pub x: Array2<f64>,
94    /// Y grid
95    pub y: Array2<f64>,
96    /// Z values
97    pub z: Array2<f64>,
98    /// Plot metadata
99    pub metadata: PlotMetadata,
100}
101
102/// Output format options
103#[derive(Debug, Clone, Copy)]
104pub enum OutputFormat {
105    /// ASCII art for terminal
106    ASCII,
107    /// CSV data
108    CSV,
109    /// JSON data
110    JSON,
111    /// SVG graphics
112    SVG,
113}
114
115/// Color scheme options
116#[derive(Debug, Clone, Copy)]
117pub enum ColorScheme {
118    /// Viridis (default)
119    Viridis,
120    /// Plasma
121    Plasma,
122    /// Inferno
123    Inferno,
124    /// Grayscale
125    Grayscale,
126}
127
128/// Statistical summary of plot data
129#[derive(Debug, Clone)]
130pub struct PlotStatistics {
131    pub count: usize,
132    pub mean: f64,
133    pub std_dev: f64,
134    pub min: f64,
135    pub max: f64,
136    pub median: f64,
137}
138
139/// Parameter exploration plot for 2D parameter spaces
140#[derive(Debug, Clone)]
141pub struct ParameterExplorationPlot {
142    /// X parameter grid
143    pub x_grid: Array2<f64>,
144    /// Y parameter grid  
145    pub y_grid: Array2<f64>,
146    /// Function values at each grid point
147    pub z_values: Array2<f64>,
148    /// Parameter ranges
149    pub param_ranges: Vec<(f64, f64)>,
150    /// Parameter names
151    pub param_names: Vec<String>,
152    /// Plot metadata
153    pub metadata: PlotMetadata,
154}
155
156/// Stability classification for attractors
157#[derive(Debug, Clone, PartialEq)]
158pub enum AttractorStability {
159    /// Fixed point attractor
160    FixedPoint,
161    /// Period-2 cycle
162    PeriodTwo,
163    /// Higher-order periodic cycle
164    Periodic(usize),
165    /// Quasi-periodic attractor
166    QuasiPeriodic,
167    /// Chaotic attractor
168    Chaotic,
169    /// Unknown/undetermined
170    Unknown,
171}
172
173/// Real-time bifurcation diagram
174#[derive(Debug, Clone)]
175pub struct RealTimeBifurcationPlot {
176    /// Parameter values
177    pub parameter_values: Vec<f64>,
178    /// Attractor data for each parameter value and initial condition
179    pub attractor_data: Vec<Vec<Vec<f64>>>,
180    /// Stability classification for each attractor
181    pub stability_data: Vec<Vec<AttractorStability>>,
182    /// Parameter range
183    pub parameter_range: (f64, f64),
184    /// Plot metadata
185    pub metadata: PlotMetadata,
186}
187
188/// 3D phase space trajectory
189#[derive(Debug, Clone)]
190pub struct PhaseSpace3D {
191    /// X coordinates
192    pub x: Vec<f64>,
193    /// Y coordinates
194    pub y: Vec<f64>,
195    /// Z coordinates
196    pub z: Vec<f64>,
197    /// Optional color values for each point
198    pub colors: Option<Vec<f64>>,
199    /// Plot metadata
200    pub metadata: PlotMetadata,
201}
202
203/// Sensitivity analysis plot
204#[derive(Debug, Clone)]
205pub struct SensitivityPlot {
206    /// Parameter names
207    pub parameter_names: Vec<String>,
208    /// Sensitivity values for each parameter
209    pub sensitivities: Vec<f64>,
210    /// Base parameter values
211    pub base_parameters: Vec<f64>,
212    /// Base function value
213    pub base_value: f64,
214    /// Plot metadata
215    pub metadata: PlotMetadata,
216}
217
218/// Interactive plot controls
219#[derive(Debug, Clone)]
220pub struct InteractivePlotControls {
221    /// Zoom level
222    pub zoom: f64,
223    /// Pan offset (x, y)
224    pub pan_offset: (f64, f64),
225    /// Selected parameter ranges
226    pub selected_ranges: Vec<(f64, f64)>,
227    /// Animation frame
228    pub current_frame: usize,
229    /// Animation speed
230    pub animation_speed: f64,
231    /// Show/hide elements
232    pub visibility_flags: std::collections::HashMap<String, bool>,
233}
234
235/// Parameter exploration methods
236#[derive(Debug, Clone, Copy)]
237pub enum ExplorationMethod {
238    /// Grid-based scanning
239    GridScan,
240    /// Random sampling
241    RandomSampling,
242    /// Adaptive sampling with refinement
243    AdaptiveSampling,
244    /// Gradient-guided exploration
245    GradientGuided,
246}
247
248/// Parameter region for refinement
249#[derive(Debug, Clone)]
250pub struct ParameterRegion {
251    /// Center of the region
252    pub center: Array1<f64>,
253    /// Radius of the region (relative to parameter bounds)
254    pub radius: f64,
255}
256
257/// Results of parameter exploration
258#[derive(Debug, Clone)]
259pub struct ParameterExplorationResult {
260    /// Explored parameter points
261    pub exploration_points: Vec<Array1<f64>>,
262    /// System responses at each point
263    pub response_values: Vec<Array1<f64>>,
264    /// Full parameter grid (including failed evaluations)
265    pub parameter_grid: Vec<Array1<f64>>,
266    /// Convergence history for iterative methods
267    pub convergence_history: Vec<Array1<f64>>,
268    /// Exploration method used
269    pub exploration_method: ExplorationMethod,
270    /// Optimization metrics
271    pub optimization_metrics: ExplorationMetrics,
272}
273
274/// Exploration performance metrics
275#[derive(Debug, Clone)]
276pub struct ExplorationMetrics {
277    /// Maximum response norm found
278    pub max_response_norm: f64,
279    /// Minimum response norm found
280    pub min_response_norm: f64,
281    /// Mean response norm
282    pub mean_response_norm: f64,
283    /// Response variance
284    pub response_variance: f64,
285    /// Coverage efficiency (0-1)
286    pub coverage_efficiency: f64,
287}
288
289/// Attractor analysis information
290#[derive(Debug, Clone)]
291pub struct AttractorInfo {
292    /// Representative states of the attractor
293    pub representative_states: Vec<f64>,
294    /// Stability flag
295    pub is_stable: bool,
296    /// Period (0 for aperiodic)
297    pub period: usize,
298}
299
300/// Dimension reduction methods
301#[derive(Debug, Clone, Copy)]
302pub enum DimensionReductionMethod {
303    /// Principal Component Analysis
304    PCA,
305    /// t-Distributed Stochastic Neighbor Embedding
306    TSNE,
307    /// Uniform Manifold Approximation and Projection
308    UMAP,
309    /// Linear Discriminant Analysis
310    LDA,
311    /// Multidimensional Scaling
312    MDS,
313}
314
315/// Clustering methods for visualization
316#[derive(Debug, Clone, Copy)]
317pub enum ClusteringMethod {
318    /// K-means clustering
319    KMeans { k: usize },
320    /// DBSCAN clustering
321    DBSCAN { eps: f64, min_samples: usize },
322    /// Hierarchical clustering
323    Hierarchical { n_clusters: usize },
324    /// No clustering
325    None,
326}
327
328/// High-dimensional data visualization plot
329#[derive(Debug, Clone)]
330pub struct HighDimensionalPlot {
331    /// X coordinates (first component)
332    pub x: Vec<f64>,
333    /// Y coordinates (second component)
334    pub y: Vec<f64>,
335    /// Z coordinates (third component, if 3D)
336    pub z: Option<Vec<f64>>,
337    /// Colors for points
338    pub colors: Vec<f64>,
339    /// Cluster labels
340    pub cluster_labels: Option<Vec<usize>>,
341    /// Original data dimensions
342    pub original_dimensions: usize,
343    /// Reduced dimensions
344    pub reduced_dimensions: usize,
345    /// Reduction method used
346    pub reduction_method: DimensionReductionMethod,
347    /// Plot metadata
348    pub metadata: PlotMetadata,
349}
350
351/// Animation settings
352#[derive(Debug, Clone)]
353pub struct AnimationSettings {
354    /// Frames per second
355    pub fps: f64,
356    /// Loop animation
357    pub loop_animation: bool,
358    /// Frame interpolation
359    pub interpolate_frames: bool,
360    /// Fade trail length
361    pub trail_length: usize,
362}
363
364impl Default for AnimationSettings {
365    fn default() -> Self {
366        Self {
367            fps: 30.0,
368            loop_animation: true,
369            interpolate_frames: false,
370            trail_length: 50,
371        }
372    }
373}
374
375/// Fluid state for 2D visualization
376#[derive(Debug, Clone)]
377pub struct FluidState {
378    /// Velocity components [u, v] as 2D arrays
379    pub velocity: Vec<Array2<f64>>,
380    /// Pressure field
381    pub pressure: Array2<f64>,
382    /// Temperature field (optional)
383    pub temperature: Option<Array2<f64>>,
384    /// Current time
385    pub time: f64,
386    /// Grid spacing in x-direction
387    pub dx: f64,
388    /// Grid spacing in y-direction
389    pub dy: f64,
390}
391
392/// Fluid state for 3D visualization
393#[derive(Debug, Clone)]
394pub struct FluidState3D {
395    /// Velocity components [u, v, w] as 3D arrays
396    pub velocity: Vec<Array3<f64>>,
397    /// Pressure field
398    pub pressure: Array3<f64>,
399    /// Temperature field (optional)
400    pub temperature: Option<Array3<f64>>,
401    /// Current time
402    pub time: f64,
403    /// Grid spacing in x-direction
404    pub dx: f64,
405    /// Grid spacing in y-direction
406    pub dy: f64,
407    /// Grid spacing in z-direction
408    pub dz: f64,
409}
410
411/// Error visualization options
412#[derive(Debug, Clone)]
413pub struct ErrorVisualizationOptions {
414    /// Show absolute errors
415    pub show_absolute: bool,
416    /// Show relative errors
417    pub show_relative: bool,
418    /// Show error distribution
419    pub show_distribution: bool,
420    /// Show convergence history
421    pub show_convergence: bool,
422    /// Error threshold for highlighting
423    pub error_threshold: f64,
424}
425
426impl Default for ErrorVisualizationOptions {
427    fn default() -> Self {
428        Self {
429            show_absolute: true,
430            show_relative: true,
431            show_distribution: true,
432            show_convergence: true,
433            error_threshold: 1e-6,
434        }
435    }
436}
437
438/// Error types for visualization
439#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
440pub enum ErrorType {
441    /// Absolute error
442    Absolute,
443    /// Relative error
444    Relative,
445    /// Truncation error
446    Truncation,
447    /// Roundoff error
448    Roundoff,
449    /// Discretization error
450    Discretization,
451}
452
453/// Error distribution plot data
454#[derive(Debug, Clone)]
455pub struct ErrorDistributionPlot {
456    /// Bin center values
457    pub bin_centers: Array1<f64>,
458    /// Histogram values
459    pub histogram: Array1<f64>,
460    /// Type of error
461    pub error_type: ErrorType,
462    /// Statistical summary
463    pub statistics: ErrorStatistics,
464    /// Color scheme
465    pub color_scheme: ColorScheme,
466}
467
468/// Error statistics
469#[derive(Debug, Clone)]
470pub struct ErrorStatistics {
471    /// Mean error
472    pub mean: f64,
473    /// Standard deviation
474    pub std_dev: f64,
475    /// Minimum error
476    pub min: f64,
477    /// Maximum error
478    pub max: f64,
479    /// Median error
480    pub median: f64,
481    /// 95th percentile
482    pub percentile_95: f64,
483}
484
485/// Convergence plot data structure
486#[derive(Debug, Clone)]
487pub struct ConvergencePlot {
488    pub iterations: Array1<f64>,
489    pub residuals: Array1<f64>,
490    pub convergence_iteration: Option<usize>,
491    pub convergence_rate: f64,
492    pub theoretical_line: Option<Array1<f64>>,
493    pub algorithm_name: String,
494    pub tolerance_line: f64,
495}
496
497/// Multi-metric convergence plot
498#[derive(Debug, Clone)]
499pub struct MultiMetricConvergencePlot {
500    pub iterations: Array1<f64>,
501    pub curves: Vec<ConvergenceCurve>,
502    pub convergence_rates: Vec<(String, f64)>,
503    pub tolerance_line: f64,
504}
505
506/// Individual convergence curve
507#[derive(Debug, Clone)]
508pub struct ConvergenceCurve {
509    pub name: String,
510    pub data: Array1<f64>,
511    pub convergence_rate: f64,
512    pub color: [f64; 3],
513}
514
515/// Step size analysis plot
516#[derive(Debug, Clone)]
517pub struct StepSizeAnalysisPlot {
518    pub log_step_sizes: Array1<f64>,
519    pub log_errors: Array1<f64>,
520    pub theoretical_errors: Option<Array1<f64>>,
521    pub order_of_accuracy: f64,
522    pub method_name: String,
523}
524
525/// Phase space density plot
526#[derive(Debug, Clone)]
527pub struct PhaseDensityPlot {
528    pub x_grid: Array1<f64>,
529    pub y_grid: Array1<f64>,
530    pub density_grid: Array2<f64>,
531    pub x_bounds: (f64, f64),
532    pub y_bounds: (f64, f64),
533    pub n_points: usize,
534}