RustedSciThe 0.4.11

Rust framework for symbolic and numerical computing:BVP ( Newton-Raphson frozen/damped/with collocations ), IVP( BDF, Radau, Backward Euler, LSODE, LSODA, RK45, DoPri), nonlinear equations ( Levenberg, Gavin) and more
Documentation
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//! Small native nonlinear-step orchestration layer for LSODE2.
//!
//! This driver sits one level above [`crate::numerical::LSODE2::step_cycle`]:
//! it owns Newton-iteration bookkeeping (`iteration`, previous correction norm,
//! active prediction) and pushes every meaningful event into
//! [`crate::numerical::LSODE2::statistics::Lsode2NativeStatistics`].
//!
//! The current version still accepts externally computed correction vectors.
//! That is deliberate: we can validate native step orchestration and telemetry
//! before wiring real residual/Jacobian/linear-solve callbacks into the loop.

use super::adams_engine::Lsode2AdamsDcfodeTables;
use super::algorithm::Lsode2SwitchTelemetry;
use super::correction::{
    Lsode2CorrectionController, Lsode2CorrectionError, Lsode2CorrectionStatus,
    Lsode2DstodaCorrectorContext,
};
use super::dcfode::Lsode2BdfDcfodeTables;
use super::dstoda_state::Lsode2IterationMode;
use super::native_executor::{Lsode2NativeCallbackExecutor, Lsode2NativeExecutorError};
use super::state::Lsode2RuntimeStateSnapshot;
use super::statistics::Lsode2NativeStatistics;
use super::step_control::Lsode2RetryDecision;
use super::step_cycle::{
    Lsode2PredictedStep, Lsode2StepCycle, Lsode2StepCycleError, Lsode2StepCycleOutcome,
};
use crate::numerical::BDF::BDF_solver::BdfLinearBackend;
use nalgebra::DVector;

#[derive(Debug, Clone, PartialEq)]
pub enum Lsode2NonlinearDriverError {
    StepCycle(Lsode2StepCycleError),
    Correction(Lsode2CorrectionError),
    Executor(Lsode2NativeExecutorError),
    NoActivePrediction,
}

impl std::fmt::Display for Lsode2NonlinearDriverError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::StepCycle(err) => write!(f, "{err}"),
            Self::Correction(err) => write!(f, "{err}"),
            Self::Executor(err) => write!(f, "{err}"),
            Self::NoActivePrediction => write!(
                f,
                "LSODE2 nonlinear driver requires begin_step() before correction submission"
            ),
        }
    }
}

impl std::error::Error for Lsode2NonlinearDriverError {}

impl From<Lsode2StepCycleError> for Lsode2NonlinearDriverError {
    fn from(value: Lsode2StepCycleError) -> Self {
        Self::StepCycle(value)
    }
}

impl From<Lsode2CorrectionError> for Lsode2NonlinearDriverError {
    fn from(value: Lsode2CorrectionError) -> Self {
        Self::Correction(value)
    }
}

impl From<Lsode2NativeExecutorError> for Lsode2NonlinearDriverError {
    fn from(value: Lsode2NativeExecutorError) -> Self {
        Self::Executor(value)
    }
}

#[derive(Debug, Clone)]
pub struct Lsode2NonlinearStepDriver {
    cycle: Lsode2StepCycle,
    correction: Lsode2CorrectionController,
    statistics: Lsode2NativeStatistics,
    active_prediction: Option<Lsode2PredictedStep>,
    active_accumulated_correction: Option<Vec<f64>>,
    current_iteration: usize,
    previous_weighted_norm: Option<f64>,
    previous_rate_estimate: Option<f64>,
    previous_rate_max_estimate: Option<f64>,
}

impl Lsode2NonlinearStepDriver {
    pub fn new(cycle: Lsode2StepCycle, correction: Lsode2CorrectionController) -> Self {
        Self {
            cycle,
            correction,
            statistics: Lsode2NativeStatistics::default(),
            active_prediction: None,
            active_accumulated_correction: None,
            current_iteration: 0,
            previous_weighted_norm: None,
            previous_rate_estimate: None,
            previous_rate_max_estimate: None,
        }
    }

    pub fn cycle(&self) -> &Lsode2StepCycle {
        &self.cycle
    }

    pub fn cycle_mut(&mut self) -> &mut Lsode2StepCycle {
        &mut self.cycle
    }

    pub fn correction(&self) -> &Lsode2CorrectionController {
        &self.correction
    }

    pub fn statistics(&self) -> &Lsode2NativeStatistics {
        &self.statistics
    }

    pub fn statistics_mut(&mut self) -> &mut Lsode2NativeStatistics {
        &mut self.statistics
    }

    pub fn reset_cycle_after_method_switch(
        &mut self,
        cycle: Lsode2StepCycle,
        correction: Lsode2CorrectionController,
    ) {
        self.cycle = cycle;
        self.correction = correction;
        self.active_prediction = None;
        self.active_accumulated_correction = None;
        self.current_iteration = 0;
        self.previous_weighted_norm = None;
        self.previous_rate_estimate = None;
        self.previous_rate_max_estimate = None;
    }

    pub fn reset_iteration_memory_after_method_switch(&mut self) {
        self.active_prediction = None;
        self.active_accumulated_correction = None;
        self.current_iteration = 0;
        self.previous_weighted_norm = None;
        self.previous_rate_estimate = None;
        self.previous_rate_max_estimate = None;
    }

    pub fn current_iteration(&self) -> usize {
        self.current_iteration
    }

    pub fn has_active_prediction(&self) -> bool {
        self.active_prediction.is_some()
    }

    pub fn begin_step(&mut self) -> Result<Lsode2PredictedStep, Lsode2NonlinearDriverError> {
        self.current_iteration = 0;
        self.previous_weighted_norm = None;
        self.previous_rate_estimate = None;
        self.previous_rate_max_estimate = None;
        let predicted = self.cycle.predict()?;
        self.statistics.record_prediction();
        self.statistics
            .record_predictor_ipup_trigger(self.cycle.ipup_trigger());
        self.active_accumulated_correction = Some(vec![0.0; predicted.y_pred.len()]);
        self.active_prediction = Some(predicted.clone());
        Ok(predicted)
    }

    pub fn submit_correction(
        &mut self,
        y_candidate: &[f64],
        correction: &[f64],
    ) -> Result<Lsode2StepCycleOutcome, Lsode2NonlinearDriverError> {
        let prediction = self
            .active_prediction
            .as_ref()
            .ok_or(Lsode2NonlinearDriverError::NoActivePrediction)?;
        let accumulated_correction = self
            .active_accumulated_correction
            .as_mut()
            .ok_or(Lsode2NonlinearDriverError::NoActivePrediction)?;
        self.current_iteration += 1;
        for (acc, delta) in accumulated_correction.iter_mut().zip(correction.iter()) {
            *acc += *delta;
        }

        let h_el1_abs = step_h_el1_abs(
            self.cycle.method(),
            self.cycle.state().h(),
            self.cycle.state().order(),
        );
        let roundoff_tolerance = self.correction.dstoda_roundoff_tolerance(y_candidate)?;
        let tesco2_override = step_tesco2(self.cycle.method(), self.cycle.state().order());
        let assessment = self.correction.assess_iteration_with_dstoda_context(
            self.cycle.state().order(),
            y_candidate,
            correction,
            accumulated_correction.as_slice(),
            self.previous_weighted_norm,
            self.previous_rate_estimate,
            self.current_iteration,
            tesco2_override,
            Some(Lsode2DstodaCorrectorContext {
                method_is_adams: self.cycle.method()
                    == super::step_cycle::Lsode2StepMethod::AdamsLike,
                previous_rate_max: self.previous_rate_max_estimate,
                h_el1_abs,
                roundoff_tolerance: Some(roundoff_tolerance),
            }),
        )?;
        self.statistics.record_correction_assessment(&assessment);

        let outcome = match assessment.status {
            Lsode2CorrectionStatus::Converged => {
                self.cycle
                    .record_adams_lipschitz_estimate_from_assessment(&assessment);
                self.cycle.finish_after_converged_correction(
                    prediction.t_trial,
                    y_candidate,
                    accumulated_correction.as_slice(),
                    &assessment.local_error,
                )?
            }
            Lsode2CorrectionStatus::Continue => {
                self.previous_weighted_norm = Some(assessment.weighted_norm);
                self.previous_rate_estimate = assessment.convergence_rate_estimate;
                self.previous_rate_max_estimate = assessment.rate_max_estimate;
                Lsode2StepCycleOutcome::NonlinearContinue { assessment }
            }
            Lsode2CorrectionStatus::Diverged | Lsode2CorrectionStatus::IterationLimitReached => {
                let retry = self.cycle.reject_after_nonlinear_failure()?;
                Lsode2StepCycleOutcome::NonlinearRejected {
                    assessment,
                    retry,
                    state: self.cycle.state().snapshot(),
                }
            }
        };

        self.statistics.record_step_cycle_outcome(&outcome);
        if !matches!(outcome, Lsode2StepCycleOutcome::NonlinearContinue { .. }) {
            self.reset_iteration_state();
        }
        Ok(outcome)
    }

    pub fn compute_and_submit_correction<L>(
        &mut self,
        y_candidate: &DVector<f64>,
        c: f64,
        executor: &mut Lsode2NativeCallbackExecutor<L>,
    ) -> Result<Lsode2StepCycleOutcome, Lsode2NonlinearDriverError>
    where
        L: BdfLinearBackend,
    {
        self.compute_and_submit_correction_with_refresh_policy(y_candidate, c, executor, false)
    }

    pub fn compute_and_submit_correction_with_refresh_policy<L>(
        &mut self,
        y_candidate: &DVector<f64>,
        c: f64,
        executor: &mut Lsode2NativeCallbackExecutor<L>,
        force_refresh: bool,
    ) -> Result<Lsode2StepCycleOutcome, Lsode2NonlinearDriverError>
    where
        L: BdfLinearBackend,
    {
        let prediction = self
            .active_prediction
            .as_ref()
            .ok_or(Lsode2NonlinearDriverError::NoActivePrediction)?;
        let predictor_refresh_requested = self.cycle.jacobian_update_request().is_requested();
        let effective_force_refresh = force_refresh || predictor_refresh_requested;
        let correction = if self.cycle.iteration_mode() == Lsode2IterationMode::Functional {
            executor.compute_functional_correction(
                prediction.t_trial,
                y_candidate,
                &mut self.statistics,
            )?
        } else {
            let had_current_linearization =
                executor.has_current_linearization(prediction.t_trial, c);
            let correction = executor.compute_newton_correction_with_refresh_policy(
                prediction.t_trial,
                y_candidate,
                c,
                &mut self.statistics,
                effective_force_refresh,
            )?;
            let has_current_linearization =
                executor.has_current_linearization(prediction.t_trial, c);
            if !had_current_linearization && has_current_linearization {
                self.cycle.mark_jacobian_current();
            }
            correction
        };
        self.submit_correction(y_candidate.as_slice(), correction.as_slice())
    }

    pub fn compute_apply_and_submit_correction<L>(
        &mut self,
        y_candidate: &mut DVector<f64>,
        c: f64,
        executor: &mut Lsode2NativeCallbackExecutor<L>,
    ) -> Result<Lsode2StepCycleOutcome, Lsode2NonlinearDriverError>
    where
        L: BdfLinearBackend,
    {
        self.compute_apply_and_submit_correction_with_refresh_policy(
            y_candidate,
            c,
            executor,
            false,
        )
    }

    pub fn compute_apply_and_submit_correction_with_refresh_policy<L>(
        &mut self,
        y_candidate: &mut DVector<f64>,
        c: f64,
        executor: &mut Lsode2NativeCallbackExecutor<L>,
        force_refresh: bool,
    ) -> Result<Lsode2StepCycleOutcome, Lsode2NonlinearDriverError>
    where
        L: BdfLinearBackend,
    {
        let prediction = self
            .active_prediction
            .as_ref()
            .ok_or(Lsode2NonlinearDriverError::NoActivePrediction)?;
        let predictor_refresh_requested = self.cycle.jacobian_update_request().is_requested();
        let effective_force_refresh = force_refresh || predictor_refresh_requested;
        let correction_y = if self.cycle.iteration_mode() == Lsode2IterationMode::Functional {
            executor.compute_functional_correction(
                prediction.t_trial,
                y_candidate,
                &mut self.statistics,
            )?
        } else {
            let had_current_linearization =
                executor.has_current_linearization(prediction.t_trial, c);
            let correction = executor.compute_newton_correction_with_refresh_policy(
                prediction.t_trial,
                y_candidate,
                c,
                &mut self.statistics,
                effective_force_refresh,
            )?;
            let has_current_linearization =
                executor.has_current_linearization(prediction.t_trial, c);
            if !had_current_linearization && has_current_linearization {
                self.cycle.mark_jacobian_current();
            }
            correction
        };
        *y_candidate += &correction_y;

        // DSTODA mirroring:
        // nonlinear correction convergence (`DEL`, `DSM`, error test ACOR/TESCO)
        // is defined in ACOR-space, while executor returns delta in Y-space.
        // For Adams/BDF predictor-corrector we have:
        //   Y = YH(:,1) + EL(1) * ACOR  =>  dY = EL(1) * dACOR
        // therefore assessment/input correction must be scaled by 1/EL(1).
        let el1 = step_el1(self.cycle.method(), self.cycle.state().order())
            .ok_or(Lsode2NonlinearDriverError::NoActivePrediction)?;
        let correction_acor = correction_y
            .iter()
            .map(|value| *value / el1)
            .collect::<Vec<_>>();
        self.submit_correction(y_candidate.as_slice(), correction_acor.as_slice())
    }

    pub fn reject_after_nonlinear_failure(
        &mut self,
    ) -> Result<(Lsode2RetryDecision, Lsode2RuntimeStateSnapshot), Lsode2NonlinearDriverError> {
        let retry = self.cycle.reject_after_nonlinear_failure()?;
        self.statistics.record_native_step_reject_nonlinear();
        self.statistics.record_native_nonlinear_diverged();
        self.statistics.record_jacobian_refresh_request();
        let snapshot = self.cycle.state().snapshot();
        self.reset_iteration_state();
        Ok((retry, snapshot))
    }

    pub fn retry_after_stale_jacobian_nonlinear_failure(
        &mut self,
    ) -> Result<(Lsode2RetryDecision, Lsode2RuntimeStateSnapshot), Lsode2NonlinearDriverError> {
        let retry = self.cycle.retry_after_stale_jacobian_nonlinear_failure()?;
        self.statistics.record_native_nonlinear_diverged();
        self.statistics.record_native_stale_jacobian_retry();
        self.statistics.record_jacobian_refresh_request();
        let snapshot = self.cycle.state().snapshot();
        self.reset_iteration_state();
        Ok((retry, snapshot))
    }

    pub fn switch_telemetry(&self, stiffness_ratio: Option<f64>) -> Lsode2SwitchTelemetry {
        self.cycle.switch_telemetry(stiffness_ratio)
    }

    fn reset_iteration_state(&mut self) {
        self.active_prediction = None;
        self.active_accumulated_correction = None;
        self.current_iteration = 0;
        self.previous_weighted_norm = None;
        self.previous_rate_estimate = None;
        self.previous_rate_max_estimate = None;
    }
}

fn step_h_el1_abs(
    method: super::step_cycle::Lsode2StepMethod,
    h: f64,
    order: usize,
) -> Option<f64> {
    let h_abs = h.abs();
    if !(h_abs.is_finite() && h_abs > 0.0) {
        return None;
    }
    let el1_abs = match method {
        super::step_cycle::Lsode2StepMethod::BdfLike => Lsode2BdfDcfodeTables::default()
            .order(order)
            .ok()
            .map(|c| c.el[0].abs()),
        super::step_cycle::Lsode2StepMethod::AdamsLike => Lsode2AdamsDcfodeTables::default()
            .order(order)
            .ok()
            .map(|c| c.el[1].abs()),
    }?;
    if el1_abs.is_finite() && el1_abs > 0.0 {
        Some(h_abs * el1_abs)
    } else {
        None
    }
}

fn step_el1(method: super::step_cycle::Lsode2StepMethod, order: usize) -> Option<f64> {
    let el1 = match method {
        super::step_cycle::Lsode2StepMethod::BdfLike => Lsode2BdfDcfodeTables::default()
            .order(order)
            .ok()
            .map(|c| c.el[0]),
        super::step_cycle::Lsode2StepMethod::AdamsLike => Lsode2AdamsDcfodeTables::default()
            .order(order)
            .ok()
            .map(|c| c.el[1]),
    }?;
    if el1.is_finite() && el1 != 0.0 {
        Some(el1)
    } else {
        None
    }
}

fn step_tesco2(method: super::step_cycle::Lsode2StepMethod, order: usize) -> Option<f64> {
    let tesco2 = match method {
        super::step_cycle::Lsode2StepMethod::BdfLike => Lsode2BdfDcfodeTables::default()
            .order(order)
            .ok()
            .map(|c| c.tesco2),
        super::step_cycle::Lsode2StepMethod::AdamsLike => Lsode2AdamsDcfodeTables::default()
            .order(order)
            .ok()
            .map(|c| c.tesco2),
    }?;
    if tesco2.is_finite() && tesco2 > 0.0 {
        Some(tesco2)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::numerical::BDF::BDF_solver::BdfJacobian;
    use crate::numerical::LSODE2::step_cycle::Lsode2StepMethod;
    use crate::numerical::LSODE2::{
        FaerSparseBdfLinearBackend, Lsode2CorrectionControlConfig, Lsode2ErrorControlConfig,
        Lsode2ErrorController, Lsode2Icf, Lsode2JacobianCurrency, Lsode2RedoStage,
        Lsode2RetryAction, Lsode2RuntimeState, Lsode2StepControlConfig, Lsode2Tolerance,
    };
    use faer::sparse::Triplet;

    fn make_driver() -> Lsode2NonlinearStepDriver {
        let cycle = Lsode2StepCycle::new(
            Lsode2RuntimeState::new(0.0, &[1.0], 0.1, 3, Lsode2StepControlConfig::default())
                .unwrap(),
            Lsode2ErrorController::new(
                Lsode2Tolerance::scalar(1.0e-3, 1.0e-6),
                Lsode2ErrorControlConfig::default(),
            )
            .unwrap(),
        );
        let correction = Lsode2CorrectionController::new(
            Lsode2Tolerance::scalar(1.0e-3, 1.0e-6),
            Lsode2CorrectionControlConfig::default(),
        )
        .unwrap();
        Lsode2NonlinearStepDriver::new(cycle, correction)
    }

    #[test]
    fn nonlinear_driver_begin_step_records_prediction() {
        let mut driver = make_driver();

        let predicted = driver.begin_step().unwrap();

        assert_eq!(predicted.t_trial, 0.1);
        assert!(driver.has_active_prediction());
        assert_eq!(driver.statistics().native_step_attempts, 1);
    }

    #[test]
    fn nonlinear_driver_can_continue_then_accept() {
        let mut driver = make_driver();
        driver.begin_step().unwrap();

        let continue_outcome = driver.submit_correction(&[0.905], &[1.0e-3]).unwrap();
        match continue_outcome {
            Lsode2StepCycleOutcome::NonlinearContinue { assessment } => {
                assert_eq!(assessment.status, Lsode2CorrectionStatus::Continue);
            }
            other => panic!("expected nonlinear continue, got {other:?}"),
        }
        assert_eq!(driver.current_iteration(), 1);
        assert!(driver.has_active_prediction());

        let accepted = driver.submit_correction(&[0.905], &[1.0e-5]).unwrap();
        match accepted {
            Lsode2StepCycleOutcome::Accepted { state, .. } => {
                assert_eq!(state.accepted_steps, 1);
            }
            other => panic!("expected accepted step, got {other:?}"),
        }

        assert_eq!(driver.current_iteration(), 0);
        assert!(!driver.has_active_prediction());
        assert_eq!(driver.statistics().native_step_attempts, 1);
        assert_eq!(driver.statistics().native_step_accepts, 1);
        assert_eq!(driver.statistics().native_nonlinear_continue_count, 1);
        assert_eq!(driver.statistics().native_nonlinear_converged_count, 1);
    }

    #[test]
    fn nonlinear_driver_rejects_diverged_correction_and_records_refresh() {
        let mut driver = make_driver();
        driver.begin_step().unwrap();
        let continued = driver.submit_correction(&[0.905], &[1.0e-3]).unwrap();
        assert!(matches!(
            continued,
            Lsode2StepCycleOutcome::NonlinearContinue { .. }
        ));

        let rejected = driver.submit_correction(&[0.905], &[1.0e-2]).unwrap();
        match rejected {
            Lsode2StepCycleOutcome::NonlinearRejected {
                assessment, state, ..
            } => {
                assert_eq!(assessment.status, Lsode2CorrectionStatus::Diverged);
                assert_eq!(state.convergence_failures, 1);
            }
            other => panic!("expected nonlinear rejection, got {other:?}"),
        }

        assert_eq!(driver.statistics().native_step_rejects_nonlinear, 1);
        assert_eq!(driver.statistics().native_nonlinear_diverged_count, 1);
        assert_eq!(driver.statistics().native_jacobian_refresh_requests, 1);
        assert!(!driver.has_active_prediction());
    }

    #[test]
    fn nonlinear_driver_supports_direct_failure_before_correction() {
        let mut driver = make_driver();
        driver.begin_step().unwrap();

        let (_retry, snapshot) = driver.reject_after_nonlinear_failure().unwrap();

        assert_eq!(snapshot.convergence_failures, 1);
        assert_eq!(driver.statistics().native_step_rejects_nonlinear, 1);
        assert_eq!(driver.statistics().native_nonlinear_diverged_count, 1);
        assert_eq!(driver.statistics().native_jacobian_refresh_requests, 1);
        assert!(!driver.has_active_prediction());
    }

    #[test]
    fn nonlinear_driver_supports_stale_jacobian_retry_without_step_reject() {
        let mut driver = make_driver();
        driver.begin_step().unwrap();

        let (retry, snapshot) = driver
            .retry_after_stale_jacobian_nonlinear_failure()
            .unwrap();

        assert_eq!(retry.h_new, 0.1);
        assert_eq!(retry.shrink_factor, 1.0);
        assert_eq!(snapshot.rejected_steps, 0);
        assert_eq!(snapshot.convergence_failures, 0);
        assert_eq!(driver.statistics().native_step_rejects_nonlinear, 0);
        assert_eq!(driver.statistics().native_nonlinear_diverged_count, 1);
        assert_eq!(driver.statistics().native_stale_jacobian_retry_count, 1);
        assert_eq!(driver.statistics().native_jacobian_refresh_requests, 1);
        assert!(!driver.has_active_prediction());
    }

    #[test]
    fn nonlinear_driver_submit_path_tracks_one_shot_stale_jacobian_refresh_retry() {
        let mut driver = make_driver();
        driver.begin_step().unwrap();

        let continued = driver.submit_correction(&[0.905], &[1.0e-3]).unwrap();
        assert!(matches!(
            continued,
            Lsode2StepCycleOutcome::NonlinearContinue { .. }
        ));

        driver.cycle_mut().mark_jacobian_stale();
        let rejected = driver.submit_correction(&[0.905], &[1.0e-2]).unwrap();
        match rejected {
            Lsode2StepCycleOutcome::NonlinearRejected {
                retry,
                state,
                assessment,
            } => {
                assert_eq!(assessment.status, Lsode2CorrectionStatus::Diverged);
                assert_eq!(retry.action, Lsode2RetryAction::RetryWithJacobianRefresh);
                assert_eq!(state.convergence_failures, 0);
                assert_eq!(state.rejected_steps, 0);
            }
            other => panic!(
                "expected nonlinear rejection on stale-J path with same-step refresh, got {other:?}"
            ),
        }

        assert_eq!(driver.cycle().icf(), Lsode2Icf::RefreshRequested);
        assert_eq!(
            driver.cycle().redo_stage(),
            Lsode2RedoStage::CorrectorRefreshSameStep
        );
        assert_eq!(driver.statistics().native_stale_jacobian_retry_count, 1);
        assert_eq!(driver.statistics().native_step_rejects_nonlinear, 0);
        assert_eq!(driver.statistics().native_nonlinear_diverged_count, 1);
        assert_eq!(driver.statistics().native_jacobian_refresh_requests, 1);
        assert!(!driver.has_active_prediction());
    }

    #[test]
    fn nonlinear_driver_submit_path_transitions_from_icf1_refresh_to_icf2_retract() {
        let mut driver = make_driver();

        // Attempt #1: converge-rate failure on stale Jacobian -> ICF=1 same-step refresh path.
        driver.begin_step().unwrap();
        let cont1 = driver.submit_correction(&[0.905], &[1.0e-3]).unwrap();
        assert!(matches!(
            cont1,
            Lsode2StepCycleOutcome::NonlinearContinue { .. }
        ));
        driver.cycle_mut().mark_jacobian_stale();
        let first_reject = driver.submit_correction(&[0.905], &[1.0e-2]).unwrap();
        match first_reject {
            Lsode2StepCycleOutcome::NonlinearRejected { retry, state, .. } => {
                assert_eq!(retry.action, Lsode2RetryAction::RetryWithJacobianRefresh);
                assert_eq!(state.convergence_failures, 0);
                assert_eq!(state.rejected_steps, 0);
            }
            other => panic!("expected first nonlinear rejection (ICF=1 refresh), got {other:?}"),
        }
        assert_eq!(driver.cycle().icf(), Lsode2Icf::RefreshRequested);
        assert_eq!(
            driver.cycle().redo_stage(),
            Lsode2RedoStage::CorrectorRefreshSameStep
        );

        // Attempt #2: after refresh path was used, next convergence failure must retract/shrink.
        // Mark J current to mirror refreshed-linearization path consumed before the next attempt.
        driver.cycle_mut().mark_jacobian_current();
        driver.begin_step().unwrap();
        let cont2 = driver.submit_correction(&[0.905], &[1.0e-3]).unwrap();
        assert!(matches!(
            cont2,
            Lsode2StepCycleOutcome::NonlinearContinue { .. }
        ));
        let second_reject = driver.submit_correction(&[0.905], &[1.0e-2]).unwrap();
        match second_reject {
            Lsode2StepCycleOutcome::NonlinearRejected { retry, state, .. } => {
                // In our controller, nonlinear failure retries keep the refresh intent flag.
                // Distinguish ICF=2 by shrink/reject counters and cycle flags, not action enum.
                assert_eq!(retry.action, Lsode2RetryAction::RetryWithJacobianRefresh);
                assert!(retry.shrink_factor < 1.0);
                assert_eq!(state.convergence_failures, 1);
                assert_eq!(state.rejected_steps, 1);
            }
            other => panic!("expected second nonlinear rejection (ICF=2 retract), got {other:?}"),
        }
        assert_eq!(driver.cycle().icf(), Lsode2Icf::RefreshDidNotRecover);
        assert_eq!(
            driver.cycle().redo_stage(),
            Lsode2RedoStage::CorrectorFailureRetry
        );

        // Telemetry parity: first rejection is stale-refresh retry, second is true nonlinear step reject.
        assert_eq!(driver.statistics().native_stale_jacobian_retry_count, 1);
        assert_eq!(driver.statistics().native_step_rejects_nonlinear, 1);
        assert_eq!(driver.statistics().native_nonlinear_diverged_count, 2);
        assert!(driver.statistics().native_jacobian_refresh_requests >= 2);
        assert!(!driver.has_active_prediction());
    }

    #[test]
    fn nonlinear_driver_requires_prediction_before_correction() {
        let mut driver = make_driver();

        let err = driver.submit_correction(&[0.905], &[1.0e-5]).unwrap_err();

        assert!(matches!(
            err,
            Lsode2NonlinearDriverError::NoActivePrediction
        ));
    }

    #[test]
    fn nonlinear_driver_can_compute_and_submit_correction_via_native_executor() {
        let mut driver = make_driver();
        driver.begin_step().unwrap();
        driver.cycle_mut().mark_jacobian_stale();
        let mut executor = Lsode2NativeCallbackExecutor::new(
            |_, y: &DVector<f64>| DVector::from_vec(vec![y[0] - 0.905]),
            |_, _| BdfJacobian::SparseTriplets {
                n: 1,
                triplets: vec![Triplet::new(0, 0, 0.0)],
            },
            FaerSparseBdfLinearBackend::default(),
        );

        let outcome = driver
            .compute_and_submit_correction(
                &DVector::from_vec(vec![0.905 + 1.0e-5]),
                0.0,
                &mut executor,
            )
            .unwrap();

        match outcome {
            Lsode2StepCycleOutcome::Accepted { state, .. } => {
                assert_eq!(state.accepted_steps, 1);
            }
            other => panic!("expected accepted step, got {other:?}"),
        }
        assert_eq!(driver.statistics().native_residual_calls, 1);
        assert_eq!(driver.statistics().native_jacobian_calls, 1);
        assert_eq!(driver.statistics().native_linear_solve_calls, 1);
        assert_eq!(driver.statistics().native_step_accepts, 1);
        assert_eq!(
            driver.cycle().jacobian_currency(),
            Lsode2JacobianCurrency::Stale
        );
    }

    #[test]
    fn nonlinear_driver_honors_dstoda_predictor_refresh_request_on_first_newton_pass() {
        use std::cell::RefCell;
        use std::rc::Rc;

        let mut driver = make_driver();
        let jacobian_calls = Rc::new(RefCell::new(0usize));
        let jacobian_calls_handle = Rc::clone(&jacobian_calls);
        let mut executor = Lsode2NativeCallbackExecutor::new(
            |_, y: &DVector<f64>| DVector::from_vec(vec![y[0] - 0.905]),
            move |_, _| {
                *jacobian_calls_handle.borrow_mut() += 1;
                BdfJacobian::SparseTriplets {
                    n: 1,
                    triplets: vec![Triplet::new(0, 0, 0.0)],
                }
            },
            FaerSparseBdfLinearBackend::default(),
        );

        // Attempt #1 builds the cached linearization.
        driver.begin_step().unwrap();
        let _ = driver
            .compute_and_submit_correction(
                &DVector::from_vec(vec![0.905 + 1.0e-3]),
                0.0,
                &mut executor,
            )
            .unwrap();
        assert_eq!(*jacobian_calls.borrow(), 1);

        // Mirror DSTODA ICF=1 path: same-step refresh requested after convergence trouble.
        let _ = driver
            .retry_after_stale_jacobian_nonlinear_failure()
            .expect("stale-J nonlinear retry should request predictor refresh");
        assert!(driver.cycle().jacobian_update_request().is_requested());

        // Attempt #2 must force a fresh Jacobian on first Newton pass,
        // even though (t, c) stayed the same and executor has cached factors.
        driver.begin_step().unwrap();
        let _ = driver
            .compute_and_submit_correction(
                &DVector::from_vec(vec![0.905 + 1.0e-3]),
                0.0,
                &mut executor,
            )
            .unwrap();
        assert_eq!(*jacobian_calls.borrow(), 2);
    }

    #[test]
    fn nonlinear_driver_can_apply_correction_until_acceptance() {
        let mut driver = make_driver();
        let predicted = driver.begin_step().unwrap();
        let mut candidate = DVector::from_vec(predicted.y_pred.clone());
        let mut executor = Lsode2NativeCallbackExecutor::new(
            |_, y: &DVector<f64>| DVector::from_vec(vec![y[0] - 0.905]),
            |_, _| BdfJacobian::SparseTriplets {
                n: 1,
                triplets: vec![Triplet::new(0, 0, 0.0)],
            },
            FaerSparseBdfLinearBackend::default(),
        );

        let first = driver
            .compute_apply_and_submit_correction(&mut candidate, 0.0, &mut executor)
            .unwrap();
        assert!(matches!(
            first,
            Lsode2StepCycleOutcome::NonlinearContinue { .. }
                | Lsode2StepCycleOutcome::Accepted { .. }
        ));

        if matches!(first, Lsode2StepCycleOutcome::NonlinearContinue { .. }) {
            let mut terminal_outcome = None;
            for _ in 0..3 {
                let next = driver
                    .compute_apply_and_submit_correction(&mut candidate, 0.0, &mut executor)
                    .unwrap();
                if matches!(
                    next,
                    Lsode2StepCycleOutcome::Accepted { .. }
                        | Lsode2StepCycleOutcome::Rejected { .. }
                ) {
                    terminal_outcome = Some(next);
                    break;
                }
                assert!(
                    matches!(next, Lsode2StepCycleOutcome::NonlinearContinue { .. }),
                    "expected additional Newton work or a terminal LSODE-style step outcome, got {next:?}"
                );
            }
            match terminal_outcome {
                Some(Lsode2StepCycleOutcome::Accepted { .. }) => {
                    assert_eq!(driver.statistics().native_step_accepts, 1);
                }
                Some(Lsode2StepCycleOutcome::Rejected { .. }) => {
                    assert_eq!(driver.statistics().native_step_rejects_error_test, 1);
                }
                Some(other) => panic!("unexpected terminal nonlinear-driver outcome: {other:?}"),
                None => panic!(
                    "native nonlinear driver should reach either accept or error-test reject within a few LSODE-style correction iterations"
                ),
            }
        }

        assert!((candidate[0] - 0.905).abs() < 1.0e-8);
        assert!(driver.statistics().native_residual_calls >= 1);
        assert!(driver.statistics().native_jacobian_calls >= 1);
        assert!(driver.statistics().native_linear_solve_calls >= 1);
    }

    #[test]
    fn nonlinear_driver_functional_iteration_uses_residual_only_path() {
        use std::cell::RefCell;
        use std::rc::Rc;

        let mut driver = make_driver();
        driver
            .cycle_mut()
            .set_iteration_mode(Lsode2IterationMode::Functional);
        let predicted = driver.begin_step().unwrap();
        let mut candidate = DVector::from_vec(predicted.y_pred.clone());

        let jacobian_calls = Rc::new(RefCell::new(0usize));
        let jacobian_calls_handle = Rc::clone(&jacobian_calls);
        let mut executor = Lsode2NativeCallbackExecutor::new(
            |_, y: &DVector<f64>| DVector::from_vec(vec![y[0] - 0.905]),
            move |_, _| {
                *jacobian_calls_handle.borrow_mut() += 1;
                BdfJacobian::SparseTriplets {
                    n: 1,
                    triplets: vec![Triplet::new(0, 0, 0.0)],
                }
            },
            FaerSparseBdfLinearBackend::default(),
        );

        let outcome = driver
            .compute_apply_and_submit_correction(&mut candidate, 0.0, &mut executor)
            .expect("functional iteration should compute correction from residual path");

        assert!(matches!(
            outcome,
            Lsode2StepCycleOutcome::NonlinearContinue { .. }
                | Lsode2StepCycleOutcome::Accepted { .. }
        ));
        assert_eq!(driver.statistics().native_residual_calls, 1);
        assert_eq!(driver.statistics().native_jacobian_calls, 0);
        assert_eq!(driver.statistics().native_linear_solve_calls, 0);
        assert_eq!(*jacobian_calls.borrow(), 0);
    }

    #[test]
    fn nonlinear_driver_adams_forces_second_iteration_when_not_roundoff() {
        let cycle = Lsode2StepCycle::new_with_method(
            Lsode2RuntimeState::new(0.0, &[1.0], 0.1, 3, Lsode2StepControlConfig::default())
                .unwrap(),
            Lsode2ErrorController::new(
                Lsode2Tolerance::scalar(1.0e-3, 1.0e-6),
                Lsode2ErrorControlConfig::default(),
            )
            .unwrap(),
            super::super::step_cycle::Lsode2StepMethod::AdamsLike,
        );
        let correction = Lsode2CorrectionController::new(
            Lsode2Tolerance::scalar(1.0e-3, 1.0e-6),
            Lsode2CorrectionControlConfig::default(),
        )
        .unwrap();
        let mut driver = Lsode2NonlinearStepDriver::new(cycle, correction);
        driver.begin_step().unwrap();

        // This increment is small enough to converge by the raw dcon test,
        // but still above roundoff-level threshold.
        let outcome = driver.submit_correction(&[0.905], &[1.0e-10]).unwrap();
        match outcome {
            Lsode2StepCycleOutcome::NonlinearContinue { assessment } => {
                assert_eq!(assessment.status, Lsode2CorrectionStatus::Continue);
            }
            other => panic!("expected forced Adams continue on first pass, got {other:?}"),
        }
        assert_eq!(driver.current_iteration(), 1);
        assert!(driver.has_active_prediction());
    }

    #[test]
    fn nonlinear_driver_adams_high_order_submit_uses_adams_tesco2_without_bdf_overflow() {
        let mut cycle = Lsode2StepCycle::new_with_method(
            Lsode2RuntimeState::new(0.0, &[1.0], 0.1, 12, Lsode2StepControlConfig::default())
                .unwrap(),
            Lsode2ErrorController::new(
                Lsode2Tolerance::scalar(1.0e-3, 1.0e-6),
                Lsode2ErrorControlConfig::default(),
            )
            .unwrap(),
            Lsode2StepMethod::AdamsLike,
        );
        cycle.state_mut().set_order(6).unwrap();

        let correction = Lsode2CorrectionController::new(
            Lsode2Tolerance::scalar(1.0e-3, 1.0e-6),
            Lsode2CorrectionControlConfig::default(),
        )
        .unwrap();
        let mut driver = Lsode2NonlinearStepDriver::new(cycle, correction);
        driver.begin_step().unwrap();

        let outcome = driver.submit_correction(&[0.905], &[1.0e-6]).unwrap();
        assert!(
            matches!(outcome, Lsode2StepCycleOutcome::NonlinearContinue { .. }),
            "Adams q=6 first pass should stay in DSTODA continue-path and must not fail via BDF tesco2 lookup"
        );
    }
}