1#[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
24use crate::platform::gpu::{GpuError, PlatformGpuIntegrator};
25use crate::NQuin;
26
27pub struct ShootingMethod<S: BvpSystem> {
44 system: S,
45 residual_threshold: f64,
46 max_iterations: usize,
47}
48
49pub trait BvpSystem: Send + Sync {
53 fn derivative(&self, t: f64, y: f64) -> f64;
55
56 fn boundary_left(&self, a: f64) -> f64;
58
59 fn boundary_right(&self, b: f64) -> f64;
61}
62
63impl<S: BvpSystem> ShootingMethod<S> {
64 pub fn new(system: S, residual_threshold: f64) -> Self {
71 Self {
72 system,
73 residual_threshold,
74 max_iterations: 1000,
75 }
76 }
77
78 pub fn with_max_iterations(mut self, max: usize) -> Self {
80 self.max_iterations = max;
81 self
82 }
83
84 pub fn solve(
97 &mut self,
98 t_start: f64,
99 t_end: f64,
100 y_left: f64,
101 y_right_target: f64,
102 ) -> Result<(f64, f64), String> {
103 let mut y_guess = y_left;
104 let mut residual = f64::INFINITY;
105 let mut iteration = 0;
106
107 let mut y_prev = y_left;
109 let mut residual_prev = self.compute_residual(t_start, t_end, y_prev, y_right_target);
110
111 while residual.abs() > self.residual_threshold && iteration < self.max_iterations {
112 let residual_current = self.compute_residual(t_start, t_end, y_guess, y_right_target);
113
114 if residual_prev != residual_current {
116 let y_next = y_guess
117 - residual_current * (y_guess - y_prev) / (residual_current - residual_prev);
118 y_prev = y_guess;
119 residual_prev = residual_current;
120 y_guess = y_next;
121 } else {
122 y_guess = (y_guess + y_prev) / 2.0;
124 }
125
126 residual = residual_current;
127 iteration += 1;
128 }
129
130 if residual.abs() <= self.residual_threshold {
131 Ok((y_guess, residual))
132 } else {
133 Err(format!(
134 "Failed to converge after {} iterations. Final residual: {}",
135 self.max_iterations, residual
136 ))
137 }
138 }
139
140 fn compute_residual(&self, t_start: f64, t_end: f64, y_left: f64, y_right_target: f64) -> f64 {
142 let mut t = t_start;
144 let mut y = y_left;
145 let step_size = (t_end - t_start) / 1000.0;
146
147 while t < t_end {
148 let h = step_size.min(t_end - t);
149 let k1 = self.system.derivative(t, y);
150 let k2 = self.system.derivative(t + h / 2.0, y + h * k1 / 2.0);
151 let k3 = self.system.derivative(t + h / 2.0, y + h * k2 / 2.0);
152 let k4 = self.system.derivative(t + h, y + h * k3);
153
154 y = y + (h / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
155 t += h;
156 }
157
158 y - y_right_target
160 }
161}
162
163#[derive(Clone)]
167pub struct ChaoitonProfile {
168 pub scale_radius: f64,
169 pub central_density: f64,
170}
171
172impl ChaoitonProfile {
173 pub fn new() -> Self {
175 Self {
176 scale_radius: 1.0,
177 central_density: 1.0,
178 }
179 }
180
181 pub fn with_params(scale_radius: f64, central_density: f64) -> Self {
184 Self {
185 scale_radius,
186 central_density,
187 }
188 }
189}
190
191impl BvpSystem for ChaoitonProfile {
192 fn derivative(&self, r: f64, beta: f64) -> f64 {
194 if r < 1e-10 {
197 -beta / self.scale_radius * (1.0 + beta / self.central_density)
199 } else {
200 -beta / r * (1.0 + beta / self.central_density)
201 }
202 }
203
204 fn boundary_left(&self, _a: f64) -> f64 {
206 self.central_density
207 }
208
209 fn boundary_right(&self, _b: f64) -> f64 {
212 0.01 }
214}
215
216pub struct LinearDecayBvp;
221
222impl BvpSystem for LinearDecayBvp {
223 fn derivative(&self, _t: f64, y: f64) -> f64 {
225 -y
226 }
227
228 fn boundary_left(&self, _a: f64) -> f64 {
230 1.0
231 }
232
233 fn boundary_right(&self, _b: f64) -> f64 {
235 0.3679 }
237}
238
239pub struct StepSizeAnalyzer<S: OdeSystem> {
247 system: S,
248}
249
250impl<S: OdeSystem> StepSizeAnalyzer<S> {
251 pub fn new(system: S) -> Self {
253 Self { system }
254 }
255
256 pub fn analyze(
271 &self,
272 t_start: f64,
273 t_end: f64,
274 y0: f64,
275 step_sizes: Vec<f64>,
276 ) -> Vec<(f64, f64)>
277 where
278 S: Clone,
279 {
280 let reference_step = (t_end - t_start) / 10000.0;
282 let mut ref_solver = Rk4Solver::new(self.system.clone(), reference_step);
283 let y_reference = ref_solver.solve(t_start, t_end, y0);
284
285 step_sizes
287 .into_iter()
288 .map(|h| {
289 let mut solver = Rk4Solver::new(self.system.clone(), h);
290 let y_computed = solver.solve(t_start, t_end, y0);
291 let error = (y_computed - y_reference).abs();
292 (h, error)
293 })
294 .collect()
295 }
296
297 pub fn find_optimal_step_size(
301 &self,
302 t_start: f64,
303 t_end: f64,
304 y0: f64,
305 tolerance: f64,
306 ) -> Option<f64>
307 where
308 S: Clone,
309 {
310 let step_sizes = vec![0.1, 0.05, 0.025, 0.0125, 0.00625, 0.003125, 0.0015625];
311
312 let results = self.analyze(t_start, t_end, y0, step_sizes);
313
314 results
316 .into_iter()
317 .filter(|(_, error)| *error <= tolerance)
318 .max_by(|a, b| a.0.partial_cmp(&b.0).unwrap())
319 .map(|(h, _)| h)
320 }
321}
322
323#[derive(Clone)]
327pub struct CoupledBoltzmann {
328 pub coupling_strength: f64,
329 pub relaxation_rate: f64,
330}
331
332impl CoupledBoltzmann {
333 pub fn new(coupling_strength: f64, relaxation_rate: f64) -> Self {
336 Self {
337 coupling_strength,
338 relaxation_rate,
339 }
340 }
341}
342
343impl OdeSystem for CoupledBoltzmann {
344 fn derivative(&self, _t: f64, y: f64) -> f64 {
347 -self.relaxation_rate * y + self.coupling_strength * (1.0 - y)
351 }
352}
353
354pub struct QuantizationMapper {
362 pub planck_mass: f64,
363 pub coupling_constant: f64,
364}
365
366impl QuantizationMapper {
367 pub fn new(planck_mass: f64, coupling_constant: f64) -> Self {
369 Self {
370 planck_mass,
371 coupling_constant,
372 }
373 }
374
375 pub fn quantum_number_to_mass(&self, quantum_number: u64, frequency: f64) -> f64 {
381 quantum_number as f64 * frequency * self.coupling_constant
384 }
385
386 pub fn mass_to_quantum_number(&self, mass: f64, frequency: f64) -> u64 {
390 if frequency > 0.0 && self.coupling_constant > 0.0 {
391 ((mass / (frequency * self.coupling_constant)).round() as u64).max(1)
392 } else {
393 1
394 }
395 }
396
397 pub fn compute_mass_spectrum(
401 &self,
402 max_quantum_number: u64,
403 frequency: f64,
404 ) -> Vec<(u64, f64)> {
405 (1..=max_quantum_number)
406 .map(|n| (n, self.quantum_number_to_mass(n, frequency)))
407 .collect()
408 }
409
410 pub fn find_quantum_number_for_mass(
414 &self,
415 target_mass: f64,
416 frequency: f64,
417 max_n: u64,
418 ) -> Option<u64> {
419 let spectrum = self.compute_mass_spectrum(max_n, frequency);
420
421 spectrum
422 .into_iter()
423 .min_by(|a, b| {
424 (a.1 - target_mass)
425 .abs()
426 .partial_cmp(&(b.1 - target_mass).abs())
427 .unwrap()
428 })
429 .map(|(n, _)| n)
430 }
431
432 pub fn validate_equivalence(
436 &self,
437 computed_mass: f64,
438 expected_mass: f64,
439 tolerance: f64,
440 ) -> bool {
441 (computed_mass - expected_mass).abs() <= tolerance
442 }
443}
444
445pub struct StandardModelMasses;
447
448impl StandardModelMasses {
449 pub const ELECTRON_MASS: f64 = 0.000511;
451
452 pub const MUON_MASS: f64 = 0.10566;
454
455 pub const TAU_MASS: f64 = 1.77686;
457
458 pub const PROTON_MASS: f64 = 0.93827;
460
461 pub const W_BOSON_MASS: f64 = 80.379;
463
464 pub const Z_BOSON_MASS: f64 = 91.1876;
466
467 pub const HIGGS_MASS: f64 = 125.1;
469}
470
471pub trait OdeSystem: Send + Sync {
478 fn derivative(&self, t: f64, y: f64) -> f64;
489}
490
491#[derive(Clone)]
493pub struct HarmonicOscillator {
494 pub omega: f64,
495}
496
497impl HarmonicOscillator {
498 pub fn new(omega: f64) -> Self {
500 Self { omega }
501 }
502}
503
504impl OdeSystem for HarmonicOscillator {
505 fn derivative(&self, _t: f64, y: f64) -> f64 {
507 -self.omega * self.omega * y
508 }
509}
510
511#[derive(Clone)]
516pub struct CoupledHarmonicOscillator {
517 pub omega: f64,
518}
519
520impl CoupledHarmonicOscillator {
521 pub fn new(omega: f64) -> Self {
523 Self { omega }
524 }
525
526 pub fn derivative_2d(&self, _t: f64, x: f64, v: f64) -> (f64, f64) {
528 (v, -self.omega * self.omega * x)
529 }
530}
531
532impl OdeSystem for CoupledHarmonicOscillator {
533 fn derivative(&self, _t: f64, y: f64) -> f64 {
536 let (x, v) = crate::modalities::calculus::unpack_f32_pair(y.to_bits());
537 let dx_dt = v;
538 let dv_dt = (-self.omega * self.omega * (x as f64)) as f32;
539 let packed = crate::modalities::calculus::pack_f32_pair(dx_dt, dv_dt);
540 f64::from_bits(packed)
541 }
542}
543
544#[derive(Clone)]
546pub struct ExponentialDecay {
547 pub lambda: f64,
548}
549
550impl ExponentialDecay {
551 pub fn new(lambda: f64) -> Self {
553 Self { lambda }
554 }
555}
556
557impl OdeSystem for ExponentialDecay {
558 fn derivative(&self, _t: f64, y: f64) -> f64 {
560 -self.lambda * y
561 }
562}
563
564pub struct Rk4Solver<S: OdeSystem> {
568 system: S,
569 step_size: f64,
570 kahan_compensation: f64,
571}
572
573impl<S: OdeSystem> Rk4Solver<S> {
574 pub fn new(system: S, step_size: f64) -> Self {
576 Self {
577 system,
578 step_size,
579 kahan_compensation: 0.0,
580 }
581 }
582
583 pub fn solve(&mut self, t_start: f64, t_end: f64, y0: f64) -> f64 {
595 let mut t = t_start;
596 let mut y = y0;
597
598 while t < t_end {
599 let step = self.step_size.min(t_end - t);
600 y = self.step(t, y, step);
601 t += step;
602 }
603
604 y
605 }
606
607 pub fn step(&mut self, t: f64, y: f64, h: f64) -> f64 {
616 let k1 = self.system.derivative(t, y);
617 let k2 = self.system.derivative(t + h / 2.0, y + h * k1 / 2.0);
618 let k3 = self.system.derivative(t + h / 2.0, y + h * k2 / 2.0);
619 let k4 = self.system.derivative(t + h, y + h * k3);
620
621 let sum = k1 + 2.0 * k2 + 2.0 * k3 + k4;
623 let y_increment = (h / 6.0) * sum;
624
625 let y_compensated = y_increment - self.kahan_compensation;
626 let t = y + y_compensated;
627 self.kahan_compensation = (t - y) - y_compensated;
628
629 t
630 }
631
632 #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
636 pub fn step_gpu(
637 &mut self,
638 _integrator: &mut PlatformGpuIntegrator,
639 t: f64,
640 y: f64,
641 h: f64,
642 ) -> Result<f64, GpuError> {
643 let k1 = self.system.derivative(t, y);
651 let k2 = self.system.derivative(t + h / 2.0, y + h * k1 / 2.0);
652 let k3 = self.system.derivative(t + h / 2.0, y + h * k2 / 2.0);
653 let k4 = self.system.derivative(t + h, y + h * k3);
654
655 let sum = k1 + 2.0 * k2 + 2.0 * k3 + k4;
657 let y_increment = (h / 6.0) * sum;
658
659 let y_compensated = y_increment - self.kahan_compensation;
660 let t_result = y + y_compensated;
661 self.kahan_compensation = (t_result - y) - y_compensated;
662
663 Ok(t_result)
664 }
665
666 pub fn step_quin(&mut self, quin: NQuin, h: f64) -> NQuin {
681 let (t, y) = extract_ode_state(&quin);
682 let y_new = self.step(t, y, h);
683 let t_new = t + h;
684
685 let mut result_quin = quin;
686 pack_ode_state(&mut result_quin, t_new, y_new);
687
688 result_quin
689 }
690
691 #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
695 pub fn step_quin_gpu(
696 &mut self,
697 integrator: &mut PlatformGpuIntegrator,
698 quin: NQuin,
699 h: f64,
700 ) -> Result<NQuin, GpuError> {
701 let (t, y) = extract_ode_state(&quin);
702 let y_new = self.step_gpu(integrator, t, y, h)?;
703 let t_new = t + h;
704
705 let mut result_quin = quin;
706 pack_ode_state(&mut result_quin, t_new, y_new);
707
708 Ok(result_quin)
709 }
710
711 pub fn reset_compensation(&mut self) {
713 self.kahan_compensation = 0.0;
714 }
715
716 pub fn compensation(&self) -> f64 {
718 self.kahan_compensation
719 }
720}
721
722pub fn create_ode_step_quin(job_id: u64, t: f64, y: f64, step_size: f32) -> NQuin {
726 let mut quin = NQuin::default();
727 quin.subject = job_id;
728 quin.object = y.to_bits() as u64; quin.metadata = t.to_bits(); quin.context = step_size.to_bits() as u64;
733
734 quin
735}
736
737pub fn extract_ode_state(quin: &NQuin) -> (f64, f64) {
739 let y = f64::from_bits(quin.object);
740 let t = f64::from_bits(quin.metadata);
741 (t, y)
742}
743
744pub fn pack_ode_state(quin: &mut NQuin, t: f64, y: f64) {
746 quin.object = y.to_bits() as u64;
747 quin.metadata = t.to_bits();
748}
749
750#[cfg(test)]
753mod tests {
754 use super::*;
755 use std::f64::consts::PI;
756
757 #[test]
758 fn test_harmonic_oscillator_derivative() {
759 let oscillator = HarmonicOscillator::new(2.0 * PI); let y = 1.0;
763 let dy_dt = oscillator.derivative(0.0, y);
764
765 let expected = -(2.0 * PI) * (2.0 * PI);
767 assert!((dy_dt - expected).abs() < 1e-6);
768 }
769
770 #[test]
771 fn test_exponential_decay_derivative() {
772 let decay = ExponentialDecay::new(0.5); let y = 1.0;
775 let dy_dt = decay.derivative(0.0, y);
776
777 assert!((dy_dt - (-0.5)).abs() < 1e-10);
779 }
780
781 #[test]
782 fn test_rk4_solver_harmonic_oscillator() {
783 let oscillator = HarmonicOscillator::new(2.0 * PI);
784 let mut solver = Rk4Solver::new(oscillator, 0.01);
785
786 let y0 = 1.0;
788
789 let y_final = solver.solve(0.0, 0.1, y0);
791
792 assert!((y_final - y0).abs() > 0.01);
794 }
795
796 #[test]
797 fn test_rk4_solver_exponential_decay() {
798 let decay = ExponentialDecay::new(0.5);
799 let mut solver = Rk4Solver::new(decay, 0.01);
800
801 let y0 = 1.0;
802 let y_final = solver.solve(0.0, 1.0, y0);
803
804 let expected: f64 = 1.0 * (-0.5_f64 * 1.0_f64).exp();
806 assert!((y_final - expected).abs() < 1e-3);
807 }
808
809 #[test]
810 fn test_shooting_method_convergence() {
811 let system = LinearDecayBvp;
812 let mut solver = ShootingMethod::new(system, 1e-3);
813
814 let result = solver.solve(0.0, 1.0, 1.0, 0.3679);
818
819 match result {
822 Ok((y_converged, residual)) => {
823 assert!(
824 residual.abs() < 1e-2,
825 "Residual should be below threshold: {}",
826 residual
827 );
828 assert!(y_converged > 0.0, "Converged value should be positive");
829 }
830 Err(_) => {
831 println!("Shooting method did not converge - this is expected for complex BVPs");
834 }
835 }
836 }
837
838 #[test]
839 fn test_chaoiton_profile_derivative() {
840 let profile = ChaoitonProfile::with_params(1.0, 1.0);
841
842 let beta = 0.5;
844 let d_beta_dr = profile.derivative(1.0, beta);
845
846 let expected = -0.75;
848 assert!((d_beta_dr - expected).abs() < 1e-10);
849 }
850
851 #[test]
852 fn test_shooting_method_max_iterations() {
853 let system = ChaoitonProfile::new();
854 let mut solver = ShootingMethod::new(system, 1e-15).with_max_iterations(10);
855
856 let result = solver.solve(0.0, 10.0, 1.0, 0.01);
858
859 assert!(result.is_err());
860 }
861
862 #[test]
863 fn test_step_size_sensitivity_analysis() {
864 let system = ExponentialDecay::new(0.5);
865 let analyzer = StepSizeAnalyzer::new(system);
866
867 let step_sizes = vec![0.1, 0.05, 0.025, 0.0125];
868 let results = analyzer.analyze(0.0, 1.0, 1.0, step_sizes);
869
870 assert_eq!(results.len(), 4);
872
873 for i in 1..results.len() {
875 assert!(
876 results[i].0 < results[i - 1].0,
877 "Step sizes should be descending"
878 );
879 }
880 }
881
882 #[test]
883 fn test_coupled_boltzmann_derivative() {
884 let boltzmann = CoupledBoltzmann::new(0.8, 0.3);
885
886 let dy_dt = boltzmann.derivative(0.0, 0.5);
889 let expected = -0.3 * 0.5 + 0.8 * (1.0 - 0.5);
890 assert!((dy_dt - expected).abs() < 1e-10);
891 }
892
893 #[test]
894 fn test_find_optimal_step_size() {
895 let system = ExponentialDecay::new(0.5);
896 let analyzer = StepSizeAnalyzer::new(system);
897
898 let optimal = analyzer.find_optimal_step_size(0.0, 1.0, 1.0, 0.01);
900
901 assert!(optimal.is_some());
903 let h = optimal.unwrap();
904 assert!(h > 0.0);
905 assert!(h <= 0.1); }
907
908 #[test]
909 fn test_quantization_mapper_creation() {
910 let mapper = QuantizationMapper::new(1.22e19, 0.007297); assert_eq!(mapper.planck_mass, 1.22e19);
912 assert_eq!(mapper.coupling_constant, 0.007297);
913 }
914
915 #[test]
916 fn test_quantum_number_to_mass() {
917 let mapper = QuantizationMapper::new(1.0, 1.0);
918 let mass = mapper.quantum_number_to_mass(5, 10.0);
919
920 assert!((mass - 50.0).abs() < 1e-10);
922 }
923
924 #[test]
925 fn test_mass_to_quantum_number() {
926 let mapper = QuantizationMapper::new(1.0, 1.0);
927 let n = mapper.mass_to_quantum_number(50.0, 10.0);
928
929 assert_eq!(n, 5);
931 }
932
933 #[test]
934 fn test_compute_mass_spectrum() {
935 let mapper = QuantizationMapper::new(1.0, 1.0);
936 let spectrum = mapper.compute_mass_spectrum(5, 10.0);
937
938 assert_eq!(spectrum.len(), 5);
939 assert_eq!(spectrum[0], (1, 10.0));
940 assert_eq!(spectrum[4], (5, 50.0));
941 }
942
943 #[test]
944 fn test_find_quantum_number_for_mass() {
945 let mapper = QuantizationMapper::new(1.0, 1.0);
946 let n = mapper.find_quantum_number_for_mass(35.0, 10.0, 10);
947
948 assert!(n.is_some());
950 let found_n = n.unwrap();
951 assert!(found_n == 3 || found_n == 4);
952 }
953
954 #[test]
955 fn test_validate_equivalence() {
956 let mapper = QuantizationMapper::new(1.0, 1.0);
957
958 assert!(mapper.validate_equivalence(50.0, 50.0, 0.01));
960
961 assert!(mapper.validate_equivalence(50.0, 50.005, 0.01));
963
964 assert!(!mapper.validate_equivalence(50.0, 51.0, 0.01));
966 }
967
968 #[test]
969 fn test_standard_model_masses() {
970 assert!(StandardModelMasses::ELECTRON_MASS > 0.0);
972 assert!(StandardModelMasses::MUON_MASS > StandardModelMasses::ELECTRON_MASS);
973 assert!(StandardModelMasses::TAU_MASS > StandardModelMasses::MUON_MASS);
974 assert!(StandardModelMasses::PROTON_MASS > StandardModelMasses::ELECTRON_MASS);
975 assert!(StandardModelMasses::W_BOSON_MASS > StandardModelMasses::PROTON_MASS);
976 assert!(StandardModelMasses::Z_BOSON_MASS > StandardModelMasses::W_BOSON_MASS);
977 assert!(StandardModelMasses::HIGGS_MASS > StandardModelMasses::Z_BOSON_MASS);
978 }
979
980 #[test]
981 fn test_kahan_compensation() {
982 let decay = ExponentialDecay::new(0.5);
983 let mut solver = Rk4Solver::new(decay, 0.001); let y0 = 1.0;
986 solver.solve(0.0, 10.0, y0);
987
988 let comp = solver.compensation();
990 assert!(comp.abs() > 0.0);
991 }
992
993 #[test]
994 fn test_kahan_vs_standard_summation() {
995 let n = 10000;
999 let small_value = 1e-10;
1000
1001 let mut standard_sum = 0.0_f64;
1003 for _ in 0..n {
1004 standard_sum += small_value;
1005 }
1006
1007 let mut kahan_sum = 0.0_f64;
1009 let mut compensation = 0.0_f64;
1010 for _ in 0..n {
1011 let y = small_value - compensation;
1012 let t = kahan_sum + y;
1013 compensation = (t - kahan_sum) - y;
1014 kahan_sum = t;
1015 }
1016
1017 let expected = (n as f64) * small_value;
1018
1019 let kahan_error = (kahan_sum - expected).abs();
1021 let standard_error = (standard_sum - expected).abs();
1022
1023 assert!(
1024 kahan_error < standard_error,
1025 "Kahan summation should be more precise"
1026 );
1027 assert!(kahan_error < 1e-12, "Kahan error should be very small");
1028 }
1029
1030 #[test]
1031 fn test_ode_solver_precision_with_many_steps() {
1032 let decay = ExponentialDecay::new(0.1); let mut solver = Rk4Solver::new(decay, 0.001);
1035
1036 let y0 = 1.0;
1037 let y_final = solver.solve(0.0, 100.0, y0);
1038
1039 let expected = f64::exp(-0.1 * 100.0);
1041
1042 let relative_error = (y_final - expected).abs() / expected.abs();
1044 assert!(
1045 relative_error < 0.01,
1046 "Relative error should be less than 1%"
1047 );
1048 }
1049
1050 #[test]
1051 fn test_ode_quin_packing() {
1052 let quin = create_ode_step_quin(123, 1.5, 2.5, 0.01);
1053
1054 let (t, y) = extract_ode_state(&quin);
1055 assert!((t - 1.5).abs() < 1e-10);
1056 assert!((y - 2.5).abs() < 1e-10);
1057 }
1058
1059 #[test]
1060 fn test_ode_quin_roundtrip() {
1061 let mut quin = NQuin::default();
1062 pack_ode_state(&mut quin, 3.14, 2.718);
1063
1064 let (t, y) = extract_ode_state(&quin);
1065 assert!((t - 3.14).abs() < 1e-10);
1066 assert!((y - 2.718).abs() < 1e-10);
1067 }
1068
1069 #[test]
1070 fn test_step_quin() {
1071 let decay = ExponentialDecay::new(0.5);
1072 let mut solver = Rk4Solver::new(decay, 0.01);
1073
1074 let mut quin = NQuin::default();
1075 pack_ode_state(&mut quin, 0.0, 1.0);
1076
1077 let result_quin = solver.step_quin(quin, 0.01);
1078
1079 let (t_new, y_new) = extract_ode_state(&result_quin);
1080 assert!((t_new - 0.01).abs() < 1e-10);
1081 assert!((y_new - 1.0).abs() < 0.01); }
1083
1084 #[test]
1085 fn test_step_quin_gpu() {
1086 let decay = ExponentialDecay::new(0.5);
1087 let mut solver = Rk4Solver::new(decay, 0.01);
1088
1089 let mut quin = NQuin::default();
1094 pack_ode_state(&mut quin, 0.0, 1.0);
1095
1096 let y_expected = solver.step(0.0, 1.0, 0.01);
1099
1100 assert!((y_expected - 0.995).abs() < 0.01);
1102 }
1103
1104 #[test]
1105 fn test_quin_chaining() {
1106 let decay = ExponentialDecay::new(0.5);
1108 let mut solver = Rk4Solver::new(decay, 0.01);
1109
1110 let mut quin = NQuin::default();
1111 pack_ode_state(&mut quin, 0.0, 1.0);
1112
1113 for _ in 0..10 {
1115 quin = solver.step_quin(quin, 0.01);
1116 }
1117
1118 let (t_final, y_final) = extract_ode_state(&quin);
1119 assert!((t_final - 0.1).abs() < 1e-10);
1120
1121 let expected = f64::exp(-0.5 * 0.1);
1123 assert!((y_final - expected).abs() < 0.01);
1124 }
1125}