RustedSciThe 0.4.20

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
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
use core::fmt::Display;

use crate::Utils::plots::plots;
/// Backward Euler method for solving systems of ordinary differential equation
/// Newton-Raphson calculation on each step of the method is made by using the analytic jacobian
pub mod NR_for_Euler;

/// Common Backward Euler types for applications using the direct solver API.
pub mod prelude {
    pub use super::NR_for_Euler::{NreError, NreSolverOptions, NreStepMode, NRE};
    pub use super::{
        BeContinuationStatistics, BeDetailedStatistics, BeError, BeSolverOptions, BeStatus,
        BeSymbolicAssemblyBackend, BeTelemetryMode, BE, DEFAULT_BE_MAX_STEPS,
    };
    pub use crate::symbolic::symbolic_engine::Expr;
    pub use crate::symbolic::symbolic_ivp_generated::{
        DenseIvpGeneratedBackendMode, SymbolicIvpGeneratedBackendConfig,
    };
    pub use nalgebra::{DMatrix, DVector};
}

use self::NR_for_Euler::{NreError, NreStepMode, NRE};
use crate::symbolic::ivp_telemetry::IvpTelemetrySnapshot;
use crate::symbolic::symbolic_engine::Expr;
use crate::symbolic::symbolic_ivp::IvpBackendError;
use crate::symbolic::symbolic_ivp::IvpSymbolicAssemblyBackend;
use crate::symbolic::symbolic_ivp_generated::{
    prepare_generated_symbolic_ivp_problem, DenseIvpGeneratedBackendMode, IvpBackendStatistics,
    SymbolicIvpGeneratedBackendConfig, SymbolicIvpGeneratedError,
};
use log::info;
use nalgebra::{DMatrix, DVector};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;

/// Default upper bound for BE step attempts in one integration segment.
pub const DEFAULT_BE_MAX_STEPS: usize = 1_000_000;

/// Symbolic frontend used to prepare Backward Euler residual/Jacobian callbacks.
///
/// `ExprLegacy` remains the compatibility default. `AtomViewNative` is
/// recommended for medium/large symbolic systems based on the BE frontend
/// measurements; tiny systems may favor the legacy preparation path.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BeSymbolicAssemblyBackend {
    /// Legacy expression differentiation and callback preparation.
    #[default]
    ExprLegacy,
    /// Native AtomView differentiation and evaluator preparation.
    AtomViewNative,
}

impl BeSymbolicAssemblyBackend {
    const fn as_ivp_backend(self) -> IvpSymbolicAssemblyBackend {
        match self {
            Self::ExprLegacy => IvpSymbolicAssemblyBackend::ExprLegacy,
            Self::AtomViewNative => IvpSymbolicAssemblyBackend::AtomView,
        }
    }
}

/// Controls collection of BE/NRE lifecycle and callback telemetry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BeTelemetryMode {
    /// Do not install instrumentation or collect counters.
    Off,
    /// Collect work counters without reading the clock.
    Counters,
    /// Collect counters and wall-clock stage timings.
    #[default]
    Timings,
}

impl BeTelemetryMode {
    /// Compatibility alias for the former fully disabled mode.
    #[allow(non_upper_case_globals)]
    pub const Disabled: Self = Self::Off;

    /// Compatibility alias for the former counters-and-timings mode.
    #[allow(non_upper_case_globals)]
    pub const Enabled: Self = Self::Timings;

    pub(crate) const fn collects_counters(self) -> bool {
        !matches!(self, Self::Off)
    }

    pub(crate) const fn collects_timings(self) -> bool {
        matches!(self, Self::Timings)
    }
}

/// Typed lifecycle state for Backward Euler.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BeStatus {
    /// Configured and ready, or currently advancing.
    #[default]
    Running,
    /// Reached the configured final time.
    Finished,
    /// Stopped at a configured sampled stop condition.
    StoppedByCondition,
    /// Integration failed.
    Failed,
}

impl BeStatus {
    /// Stable text representation retained for compatibility APIs.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Running => "running",
            Self::Finished => "finished",
            Self::StoppedByCondition => "stopped_by_condition",
            Self::Failed => "failed",
        }
    }
}

#[derive(Debug)]
pub enum BeError {
    InvalidConfiguration(&'static str),
    Backend(IvpBackendError),
    GeneratedBackend(SymbolicIvpGeneratedError),
    Newton(NreError),
    StepUnderflow { time: f64, step: f64 },
    StepLimit { max_steps: usize },
}

/// Telemetry classification for failures returned by BE operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BeFailureKind {
    Configuration,
    Backend,
    GeneratedBackend,
    Newton,
    StepUnderflow,
    StepLimit,
}

impl BeError {
    fn failure_kind(&self) -> BeFailureKind {
        match self {
            Self::InvalidConfiguration(_) => BeFailureKind::Configuration,
            Self::Backend(_) => BeFailureKind::Backend,
            Self::GeneratedBackend(_) => BeFailureKind::GeneratedBackend,
            Self::Newton(_) => BeFailureKind::Newton,
            Self::StepUnderflow { .. } => BeFailureKind::StepUnderflow,
            Self::StepLimit { .. } => BeFailureKind::StepLimit,
        }
    }
}

/// Counters for explicit accepted-state continuation requests.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct BeContinuationStatistics {
    /// Number of continuation calls, including requests rejected at validation.
    pub attempts: usize,
    /// Number of continuation calls ending in a successful terminal status.
    pub completed: usize,
    /// Number of continuation calls returning an error.
    pub failures: usize,
    /// Cumulative wall-clock time spent in continuation calls, in milliseconds.
    pub elapsed_ms_total: f64,
}

impl BeContinuationStatistics {
    fn record(&mut self, elapsed: Option<std::time::Duration>, succeeded: bool) {
        self.attempts = self.attempts.saturating_add(1);
        if succeeded {
            self.completed = self.completed.saturating_add(1);
        } else {
            self.failures = self.failures.saturating_add(1);
        }
        if let Some(elapsed) = elapsed {
            self.elapsed_ms_total += elapsed.as_secs_f64() * 1_000.0;
        }
    }
}

/// BE/NRE work that is not represented by the shared IVP callback counters.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct BeDetailedStatistics {
    /// Parameter-value binding attempts, including rejected values.
    pub parameter_bind_attempts: usize,
    /// Successful parameter-value bindings.
    pub parameter_bind_successes: usize,
    /// Rejected parameter-value bindings.
    pub parameter_bind_failures: usize,
    /// Cumulative time validating and applying parameter values, in milliseconds.
    pub parameter_bind_ms_total: f64,
    /// Failed BE operations classified by their returned error type.
    pub configuration_failures: usize,
    pub backend_failures: usize,
    pub generated_backend_failures: usize,
    pub newton_failures: usize,
    pub step_underflow_failures: usize,
    pub step_limit_failures: usize,
    /// RHS evaluations performed only to construct a finite-difference Jacobian.
    pub fd_rhs_evaluations: usize,
    /// Newton matrix factorizations attempted.
    pub factorization_calls: usize,
    /// Cumulative Newton matrix factorization time in milliseconds.
    pub factorization_ms_total: f64,
    /// Linear-system solve attempts after factorization.
    pub linear_solve_calls: usize,
    /// Cumulative triangular/linear solve time in milliseconds.
    pub linear_solve_ms_total: f64,
    /// Steps whose Newton solve completed and whose state was committed.
    pub accepted_steps: usize,
    /// Step attempts that failed before committing a state.
    pub failed_steps: usize,
    /// Number of final/continuation trajectory assembly operations.
    pub output_assembly_calls: usize,
    /// Cumulative trajectory assembly time in milliseconds.
    pub output_assembly_ms_total: f64,
}

impl BeDetailedStatistics {
    pub(crate) fn record_parameter_bind(
        &mut self,
        elapsed: Option<std::time::Duration>,
        succeeded: bool,
    ) {
        self.parameter_bind_attempts = self.parameter_bind_attempts.saturating_add(1);
        if succeeded {
            self.parameter_bind_successes = self.parameter_bind_successes.saturating_add(1);
        } else {
            self.parameter_bind_failures = self.parameter_bind_failures.saturating_add(1);
        }
        if let Some(elapsed) = elapsed {
            self.parameter_bind_ms_total += elapsed.as_secs_f64() * 1_000.0;
        }
    }

    pub(crate) fn record_failure(&mut self, kind: BeFailureKind) {
        let count = match kind {
            BeFailureKind::Configuration => &mut self.configuration_failures,
            BeFailureKind::Backend => &mut self.backend_failures,
            BeFailureKind::GeneratedBackend => &mut self.generated_backend_failures,
            BeFailureKind::Newton => &mut self.newton_failures,
            BeFailureKind::StepUnderflow => &mut self.step_underflow_failures,
            BeFailureKind::StepLimit => &mut self.step_limit_failures,
        };
        *count = count.saturating_add(1);
    }

    pub(crate) fn record_fd_rhs_evaluation(&mut self) {
        self.fd_rhs_evaluations = self.fd_rhs_evaluations.saturating_add(1);
    }

    pub(crate) fn record_factorization(&mut self, elapsed: Option<std::time::Duration>) {
        self.factorization_calls = self.factorization_calls.saturating_add(1);
        if let Some(elapsed) = elapsed {
            self.factorization_ms_total += elapsed.as_secs_f64() * 1_000.0;
        }
    }

    pub(crate) fn record_linear_solve(&mut self, elapsed: Option<std::time::Duration>) {
        self.linear_solve_calls = self.linear_solve_calls.saturating_add(1);
        if let Some(elapsed) = elapsed {
            self.linear_solve_ms_total += elapsed.as_secs_f64() * 1_000.0;
        }
    }

    pub(crate) fn record_accepted_step(&mut self) {
        self.accepted_steps = self.accepted_steps.saturating_add(1);
    }

    pub(crate) fn record_failed_step(&mut self) {
        self.failed_steps = self.failed_steps.saturating_add(1);
    }

    pub(crate) fn record_output_assembly(&mut self, elapsed: Option<std::time::Duration>) {
        self.output_assembly_calls = self.output_assembly_calls.saturating_add(1);
        if let Some(elapsed) = elapsed {
            self.output_assembly_ms_total += elapsed.as_secs_f64() * 1_000.0;
        }
    }
}

impl std::fmt::Display for BeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidConfiguration(message) => {
                write!(f, "invalid Backward Euler configuration: {message}")
            }
            Self::Backend(error) => write!(f, "Backward Euler backend failed: {error}"),
            Self::GeneratedBackend(error) => {
                write!(f, "Backward Euler generated backend failed: {error}")
            }
            Self::Newton(error) => write!(f, "Backward Euler Newton solve failed: {error}"),
            Self::StepUnderflow { time, step } => {
                write!(f, "Backward Euler step {step} cannot advance time {time}")
            }
            Self::StepLimit { max_steps } => {
                write!(
                    f,
                    "Backward Euler exceeded the maximum of {max_steps} steps"
                )
            }
        }
    }
}

impl std::error::Error for BeError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Backend(error) => Some(error),
            Self::GeneratedBackend(error) => Some(error),
            Self::Newton(error) => Some(error),
            _ => None,
        }
    }
}

impl From<IvpBackendError> for BeError {
    fn from(value: IvpBackendError) -> Self {
        Self::Backend(value)
    }
}

impl From<NreError> for BeError {
    fn from(value: NreError) -> Self {
        Self::Newton(value)
    }
}

type BeNativeRhs = Arc<dyn Fn(f64, &DVector<f64>) -> DVector<f64> + Send + Sync>;
type BeNativeJac = Arc<dyn Fn(f64, &DVector<f64>) -> DMatrix<f64> + Send + Sync>;
pub enum Equation {
    LHS(Vec<Expr>),
    RHS(Vec<Expr>),
}

/// Grouped setup for one Backward Euler solve.
#[derive(Clone)]
pub struct BeSolverOptions {
    pub eq_system: Vec<Expr>,
    pub values: Vec<String>,
    pub arg: String,
    pub tolerance: f64,
    pub max_iterations: usize,
    /// Fixed integration step. `None` recomputes the legacy local heuristic
    /// before each step from current `(t, y)` and the remaining interval. The
    /// selected step is fixed during that step's Newton solve; this is not an
    /// error estimator and does not guarantee integration accuracy.
    pub h: Option<f64>,
    pub t0: f64,
    pub t_bound: f64,
    pub y0: DVector<f64>,
    pub generated_backend_config: SymbolicIvpGeneratedBackendConfig,
    /// Symbolic expression frontend used when callbacks are prepared.
    pub symbolic_assembly_backend: BeSymbolicAssemblyBackend,
    /// Maximum integration step attempts in one solve/continuation segment.
    pub max_steps: usize,
    /// Controls solver and callback instrumentation.
    pub telemetry_mode: BeTelemetryMode,
}

impl BeSolverOptions {
    /// Creates grouped Backward Euler options.
    pub fn new(
        eq_system: Vec<Expr>,
        values: Vec<String>,
        arg: String,
        tolerance: f64,
        max_iterations: usize,
        h: Option<f64>,
        t0: f64,
        t_bound: f64,
        y0: DVector<f64>,
    ) -> Self {
        Self {
            eq_system,
            values,
            arg,
            tolerance,
            max_iterations,
            h,
            t0,
            t_bound,
            y0,
            generated_backend_config: SymbolicIvpGeneratedBackendConfig::defaults(),
            symbolic_assembly_backend: BeSymbolicAssemblyBackend::default(),
            max_steps: DEFAULT_BE_MAX_STEPS,
            telemetry_mode: BeTelemetryMode::Timings,
        }
    }

    /// Applies one explicit generated-backend config.
    pub fn with_generated_backend_config(
        mut self,
        config: SymbolicIvpGeneratedBackendConfig,
    ) -> Self {
        self.generated_backend_config = config;
        self
    }

    /// Selects the symbolic frontend used to prepare residual/Jacobian callbacks.
    pub fn with_symbolic_assembly_backend(mut self, backend: BeSymbolicAssemblyBackend) -> Self {
        self.symbolic_assembly_backend = backend;
        self
    }

    /// Sets the maximum attempted steps per integration segment.
    pub fn with_max_steps(mut self, max_steps: usize) -> Self {
        self.max_steps = max_steps;
        self
    }

    /// Enables or disables lifecycle and callback telemetry.
    pub fn with_telemetry_mode(mut self, mode: BeTelemetryMode) -> Self {
        self.telemetry_mode = mode;
        self
    }

    /// Applies one high-level dense generated backend mode.
    pub fn with_dense_generated_backend_mode(mut self, mode: DenseIvpGeneratedBackendMode) -> Self {
        let mut config = SymbolicIvpGeneratedBackendConfig::from_mode(mode);
        config.resolver = self.generated_backend_config.resolver.clone();
        config.aot_options = self.generated_backend_config.aot_options;
        config.aot_codegen_backend = self.generated_backend_config.aot_codegen_backend;
        config.aot_c_compiler = self.generated_backend_config.aot_c_compiler.clone();
        config.output_parent_dir = self.generated_backend_config.output_parent_dir.clone();
        config.crate_name_override = self.generated_backend_config.crate_name_override.clone();
        config.module_name_override = self.generated_backend_config.module_name_override.clone();
        self.generated_backend_config = config;
        self
    }

    /// Uses compiled dense IVP path via `C + tcc` when startup latency matters most.
    ///
    /// Practical note:
    /// for Backward Euler this is usually the first compiled backend worth
    /// trying once the nonlinear system stops being toy-sized. Current IVP
    /// comparisons show that `C + tcc` is often the best practical compromise
    /// for medium and larger stiff systems because setup stays cheap while the
    /// generated Jacobian/residual path becomes faster than `Lambdify`.
    pub fn with_dense_generated_backend_c_tcc(self, output_parent_dir: impl Into<PathBuf>) -> Self {
        self.with_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .with_c_tcc(),
        )
    }

    /// Uses compiled dense IVP path via `C + gcc` when runtime throughput matters more.
    ///
    /// Practical note:
    /// this is the "pay a larger setup cost for stronger native code" option.
    /// It is worth trying for long repeated Backward Euler runs when startup
    /// latency is secondary.
    pub fn with_dense_generated_backend_c_gcc(self, output_parent_dir: impl Into<PathBuf>) -> Self {
        self.with_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .with_c_gcc(),
        )
    }

    /// Uses compiled dense IVP path via Zig.
    pub fn with_dense_generated_backend_zig(self, output_parent_dir: impl Into<PathBuf>) -> Self {
        self.with_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .with_zig(),
        )
    }

    /// Recommended generated-backend preset for dense IVP repeated solves.
    ///
    /// For `BE` the practical recommendation is:
    /// - small systems: keep `Lambdify`;
    /// - larger stiff systems: try this preset first;
    /// - if startup latency becomes a problem, drop to explicit `C + tcc`.
    pub fn with_dense_generated_backend_for_repeated_solves(
        self,
        output_parent_dir: impl Into<PathBuf>,
    ) -> Self {
        self.with_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .for_repeated_solves(),
        )
    }
}
//#[derive(Debug)]
pub struct BE {
    pub newton: NRE,
    y0: DVector<f64>,
    t0: f64,
    t_bound: f64,
    t: f64,
    y: DVector<f64>,
    t_old: Option<f64>,
    t_result: DVector<f64>,
    y_result: DMatrix<f64>,
    status: BeStatus,
    message: Option<String>,
    h: Option<f64>,
    stop_conditions: Vec<(usize, f64)>,
    neighborhood_check: f64,
    generated_backend_config: SymbolicIvpGeneratedBackendConfig,
    statistics: IvpBackendStatistics,
    native_rhs: Option<BeNativeRhs>,
    native_jac: Option<BeNativeJac>,
    last_error: Option<BeError>,
    continuation_statistics: BeContinuationStatistics,
    max_steps: usize,
    telemetry_mode: BeTelemetryMode,
    telemetry_locked: bool,
}
impl Display for BE {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "BE {{ t0: {}, t_bound: {}, t: {}, y: {:?} }}",
            self.t0, self.t_bound, self.t, self.y
        )
    }
}

impl BE {
    pub fn new() -> BE {
        let nr_new = NRE::new(
            Vec::new(),
            DVector::zeros(0),
            Vec::new(),
            String::new(),
            0.0,
            0,
            0.0,
            true,
            None,
        );
        BE {
            newton: nr_new,
            y0: DVector::zeros(0),
            t0: 0.0,
            t_bound: 0.0,
            t: 0.0,
            y: DVector::zeros(0),
            t_old: None,
            t_result: DVector::zeros(0),
            y_result: DMatrix::zeros(0, 0),
            status: BeStatus::Running,
            message: None,
            h: None,
            stop_conditions: Vec::new(),
            neighborhood_check: 1e-6,
            generated_backend_config: SymbolicIvpGeneratedBackendConfig::defaults(),
            statistics: IvpBackendStatistics::default(),
            native_rhs: None,
            native_jac: None,
            last_error: None,
            continuation_statistics: BeContinuationStatistics::default(),
            max_steps: DEFAULT_BE_MAX_STEPS,
            telemetry_mode: BeTelemetryMode::Timings,
            telemetry_locked: false,
        }
    }

    /// Preferred grouped setup path for Backward Euler.
    pub fn new_with_options(options: BeSolverOptions) -> Self {
        Self::try_new_with_options(options)
            .expect("Backward Euler options should describe a valid problem")
    }

    pub fn try_new_with_options(options: BeSolverOptions) -> Result<Self, BeError> {
        let symbolic_assembly_backend = options.symbolic_assembly_backend;
        let mut solver =
            Self::new().with_generated_backend_config(options.generated_backend_config);
        solver.try_set_initial(
            options.eq_system,
            options.values,
            options.arg,
            options.tolerance,
            options.max_iterations,
            options.h,
            options.t0,
            options.t_bound,
            options.y0,
        )?;
        solver.try_set_max_steps(options.max_steps)?;
        solver.try_set_telemetry_mode(options.telemetry_mode)?;
        solver.set_symbolic_assembly_backend(symbolic_assembly_backend);
        Ok(solver)
    }

    /// Installs one high-level generated-backend orchestration config.
    pub fn set_generated_backend_config(&mut self, config: SymbolicIvpGeneratedBackendConfig) {
        self.generated_backend_config = config;
        self.newton
            .set_generated_backend_config(self.generated_backend_config.clone());
    }

    /// Returns the current generated-backend orchestration config.
    pub fn generated_backend_config(&self) -> &SymbolicIvpGeneratedBackendConfig {
        &self.generated_backend_config
    }

    /// Selects the symbolic frontend used for future callback preparation.
    ///
    /// This must be changed before callbacks are prepared. Re-selecting a
    /// frontend invalidates any currently prepared symbolic callback set.
    pub fn set_symbolic_assembly_backend(&mut self, backend: BeSymbolicAssemblyBackend) {
        self.newton.set_symbolic_assembly_backend(backend);
    }

    /// Returns the selected symbolic frontend.
    pub fn symbolic_assembly_backend(&self) -> BeSymbolicAssemblyBackend {
        self.newton.symbolic_assembly_backend()
    }

    pub fn get_statistics(&self) -> IvpBackendStatistics {
        if !self.telemetry_mode.collects_counters() {
            return IvpBackendStatistics::default();
        }
        let mut stats = self.statistics.clone();
        let newton_stats = self.newton.statistics();
        stats.nonlinear_solve_calls = newton_stats.nonlinear_solve_calls;
        stats.nonlinear_iterations_total = newton_stats.nonlinear_iterations_total;
        stats.residual_calls = newton_stats.residual_calls;
        stats.residual_ms_total = newton_stats.residual_ms_total;
        stats.jacobian_calls = newton_stats.jacobian_calls;
        stats.jacobian_ms_total = newton_stats.jacobian_ms_total;
        stats
    }

    pub fn statistics_report(&self) -> String {
        let mut report = self.get_statistics().table_report();
        let detailed = self.detailed_statistics();
        report.push_str(&format!(
            "\ntelemetry={}; continuation: attempts={} completed={} failures={} elapsed_ms_total={:.6}\nBE work: parameter_bind_attempts={} parameter_bind_successes={} parameter_bind_failures={} parameter_bind_ms={:.6} failures=[configuration:{},backend:{},generated_backend:{},newton:{},step_underflow:{},step_limit:{}] fd_rhs={} factorization_calls={} factorization_ms={:.6} linear_solve_calls={} linear_solve_ms={:.6} accepted_steps={} failed_steps={} output_assembly_calls={} output_assembly_ms={:.6}",
            match self.telemetry_mode {
                BeTelemetryMode::Off => "off",
                BeTelemetryMode::Counters => "counters",
                BeTelemetryMode::Timings => "timings",
            },
            self.continuation_statistics.attempts,
            self.continuation_statistics.completed,
            self.continuation_statistics.failures,
            self.continuation_statistics.elapsed_ms_total,
            detailed.parameter_bind_attempts,
            detailed.parameter_bind_successes,
            detailed.parameter_bind_failures,
            detailed.parameter_bind_ms_total,
            detailed.configuration_failures,
            detailed.backend_failures,
            detailed.generated_backend_failures,
            detailed.newton_failures,
            detailed.step_underflow_failures,
            detailed.step_limit_failures,
            detailed.fd_rhs_evaluations,
            detailed.factorization_calls,
            detailed.factorization_ms_total,
            detailed.linear_solve_calls,
            detailed.linear_solve_ms_total,
            detailed.accepted_steps,
            detailed.failed_steps,
            detailed.output_assembly_calls,
            detailed.output_assembly_ms_total,
        ));
        if let Some(snapshot) = self.symbolic_ivp_telemetry_snapshot() {
            report.push_str("\n\n## Symbolic IVP lifecycle telemetry\n\n");
            report.push_str(&snapshot.pretty_report());
        }
        report
    }

    /// Returns the shared typed symbolic-IVP telemetry captured during
    /// preparation and subsequent callback evaluation, if telemetry is enabled.
    pub fn symbolic_ivp_telemetry_snapshot(&self) -> Option<IvpTelemetrySnapshot> {
        self.newton.symbolic_ivp_telemetry_snapshot()
    }

    /// Returns BE/NRE-specific work counters and stage timings.
    pub fn detailed_statistics(&self) -> BeDetailedStatistics {
        self.newton.detailed_statistics()
    }

    /// Returns the configured maximum attempted steps per integration segment.
    pub fn max_steps(&self) -> usize {
        self.max_steps
    }

    /// Configures a nonzero maximum number of attempted steps per segment.
    pub fn try_set_max_steps(&mut self, max_steps: usize) -> Result<(), BeError> {
        if max_steps == 0 {
            return Err(BeError::InvalidConfiguration(
                "maximum step count must be nonzero",
            ));
        }
        self.max_steps = max_steps;
        Ok(())
    }

    /// Returns the telemetry mode selected for this solver.
    pub fn telemetry_mode(&self) -> BeTelemetryMode {
        self.telemetry_mode
    }

    /// Changes telemetry mode before integration starts.
    pub fn try_set_telemetry_mode(&mut self, mode: BeTelemetryMode) -> Result<(), BeError> {
        if (self.telemetry_locked || self.newton.jac.is_some()) && mode != self.telemetry_mode {
            return Err(BeError::InvalidConfiguration(
                "telemetry mode cannot change after integration has started; reinitialize the problem first",
            ));
        }
        self.telemetry_mode = mode;
        self.newton.set_telemetry_mode(mode);
        Ok(())
    }

    /// Returns counters for accepted-state continuation requests.
    pub fn continuation_statistics(&self) -> &BeContinuationStatistics {
        &self.continuation_statistics
    }

    /// Builder-style generated backend setup.
    pub fn with_generated_backend_config(
        mut self,
        config: SymbolicIvpGeneratedBackendConfig,
    ) -> Self {
        self.set_generated_backend_config(config);
        self
    }

    /// Applies one high-level dense generated backend mode.
    pub fn set_dense_generated_backend_mode(&mut self, mode: DenseIvpGeneratedBackendMode) {
        let mut config = SymbolicIvpGeneratedBackendConfig::from_mode(mode);
        config.resolver = self.generated_backend_config.resolver.clone();
        config.aot_options = self.generated_backend_config.aot_options;
        config.aot_codegen_backend = self.generated_backend_config.aot_codegen_backend;
        config.aot_c_compiler = self.generated_backend_config.aot_c_compiler.clone();
        config.output_parent_dir = self.generated_backend_config.output_parent_dir.clone();
        config.crate_name_override = self.generated_backend_config.crate_name_override.clone();
        config.module_name_override = self.generated_backend_config.module_name_override.clone();
        self.set_generated_backend_config(config);
    }

    /// Builder-style preset for the dense generated backend mode.
    pub fn with_dense_generated_backend_mode(mut self, mode: DenseIvpGeneratedBackendMode) -> Self {
        self.set_dense_generated_backend_mode(mode);
        self
    }

    /// Uses compiled dense IVP path via `C + tcc` when startup latency matters most.
    ///
    /// This is often the most practical compiled `BE` choice once the problem
    /// is large enough for Jacobian throughput to matter.
    pub fn set_dense_generated_backend_c_tcc(&mut self, output_parent_dir: impl Into<PathBuf>) {
        self.set_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .with_c_tcc(),
        );
    }

    /// Uses compiled dense IVP path via `C + gcc` for runtime-oriented repeated solves.
    ///
    /// Prefer this when you expect many repeated Backward Euler solves on the
    /// same symbolic problem and startup latency is acceptable.
    pub fn set_dense_generated_backend_c_gcc(&mut self, output_parent_dir: impl Into<PathBuf>) {
        self.set_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .with_c_gcc(),
        );
    }

    /// Uses compiled dense IVP path via Zig.
    pub fn set_dense_generated_backend_zig(&mut self, output_parent_dir: impl Into<PathBuf>) {
        self.set_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .with_zig(),
        );
    }

    /// Recommended generated-backend preset for dense IVP repeated solves.
    ///
    /// For `BE` this points to the runtime-oriented generated path. Benchmark
    /// against `Lambdify` on small systems before enabling it by default in
    /// application code.
    pub fn set_dense_generated_backend_for_repeated_solves(
        &mut self,
        output_parent_dir: impl Into<PathBuf>,
    ) {
        self.set_generated_backend_config(
            SymbolicIvpGeneratedBackendConfig::build_if_missing_release(output_parent_dir)
                .for_repeated_solves(),
        );
    }

    /// Builder-style alias for `C + tcc` dense generated backend setup.
    pub fn with_dense_generated_backend_c_tcc(
        mut self,
        output_parent_dir: impl Into<PathBuf>,
    ) -> Self {
        self.set_dense_generated_backend_c_tcc(output_parent_dir);
        self
    }

    /// Builder-style alias for `C + gcc` dense generated backend setup.
    pub fn with_dense_generated_backend_c_gcc(
        mut self,
        output_parent_dir: impl Into<PathBuf>,
    ) -> Self {
        self.set_dense_generated_backend_c_gcc(output_parent_dir);
        self
    }

    /// Builder-style alias for Zig dense generated backend setup.
    pub fn with_dense_generated_backend_zig(
        mut self,
        output_parent_dir: impl Into<PathBuf>,
    ) -> Self {
        self.set_dense_generated_backend_zig(output_parent_dir);
        self
    }

    /// Builder-style alias for the recommended repeated-solve IVP preset.
    pub fn with_dense_generated_backend_for_repeated_solves(
        mut self,
        output_parent_dir: impl Into<PathBuf>,
    ) -> Self {
        self.set_dense_generated_backend_for_repeated_solves(output_parent_dir);
        self
    }
    pub fn set_initial(
        &mut self,
        eq_system: Vec<Expr>, //
        values: Vec<String>,
        arg: String,
        tolerance: f64,        // tolerance
        max_iterations: usize, // max number of iterations
        h: Option<f64>,
        t0: f64,
        t_bound: f64,
        y0: DVector<f64>,
    ) -> () {
        self.try_set_initial(
            eq_system,
            values,
            arg,
            tolerance,
            max_iterations,
            h,
            t0,
            t_bound,
            y0,
        )
        .expect("Backward Euler initial configuration should be valid");
    }

    pub fn try_set_initial(
        &mut self,
        eq_system: Vec<Expr>,
        values: Vec<String>,
        arg: String,
        tolerance: f64,
        max_iterations: usize,
        h: Option<f64>,
        t0: f64,
        t_bound: f64,
        y0: DVector<f64>,
    ) -> Result<(), BeError> {
        validate_be_configuration(
            Some(eq_system.len()),
            &values,
            &arg,
            tolerance,
            max_iterations,
            h,
            t0,
            t_bound,
            &y0,
        )?;
        let nr = make_be_newton(
            eq_system,
            y0.clone(),
            values,
            arg,
            tolerance,
            max_iterations,
            h,
            t_bound,
        );
        self.reset_initial_state(nr, h, t0, t_bound, y0);
        Ok(())
    }

    /// Configures a purely numerical Backward Euler problem without symbolic
    /// equations or symbolic-backend preparation.
    pub fn try_set_native_initial<F, J>(
        &mut self,
        values: Vec<String>,
        arg: String,
        tolerance: f64,
        max_iterations: usize,
        h: Option<f64>,
        t0: f64,
        t_bound: f64,
        y0: DVector<f64>,
        rhs: F,
        jac: Option<J>,
    ) -> Result<(), BeError>
    where
        F: Fn(f64, &DVector<f64>) -> DVector<f64> + Send + Sync + 'static,
        J: Fn(f64, &DVector<f64>) -> DMatrix<f64> + Send + Sync + 'static,
    {
        validate_be_configuration(
            None,
            &values,
            &arg,
            tolerance,
            max_iterations,
            h,
            t0,
            t_bound,
            &y0,
        )?;
        let nr = make_be_newton(
            Vec::new(),
            y0.clone(),
            values,
            arg,
            tolerance,
            max_iterations,
            h,
            t_bound,
        );
        self.reset_initial_state(nr, h, t0, t_bound, y0);
        self.set_native_ode_callbacks(rhs, jac);
        Ok(())
    }

    fn reset_initial_state(
        &mut self,
        newton: NRE,
        h: Option<f64>,
        t0: f64,
        t_bound: f64,
        y0: DVector<f64>,
    ) {
        let symbolic_assembly_backend = self.newton.symbolic_assembly_backend();
        self.newton = newton;
        self.newton
            .set_symbolic_assembly_backend(symbolic_assembly_backend);
        self.t0 = t0;
        self.t_bound = t_bound;
        self.y0 = y0.clone();
        self.t = t0;
        self.y = y0.clone();
        self.h = h;
        self.t_old = None;
        self.t_result = DVector::from_vec(vec![t0]);
        self.y_result = DMatrix::from_row_slice(1, y0.len(), y0.as_slice());
        self.status = BeStatus::Running;
        self.message = None;
        self.native_rhs = None;
        self.native_jac = None;
        self.stop_conditions.clear();
        self.neighborhood_check = 1e-6;
        self.statistics = IvpBackendStatistics::default();
        self.last_error = None;
        self.continuation_statistics = BeContinuationStatistics::default();
        self.telemetry_locked = false;
        self.newton.set_telemetry_mode(self.telemetry_mode);
    }

    pub fn set_stop_condition(&mut self, stop_condition: HashMap<String, f64>) {
        self.try_set_stop_condition(stop_condition)
            .expect("Backward Euler stop condition should be valid");
    }

    pub fn set_equation_parameters(&mut self, params: Option<&[&str]>) {
        self.try_set_equation_parameters(params)
            .expect("Backward Euler parameter schema should be valid");
    }

    /// Stops after an accepted sample enters the configured neighborhood.
    /// This is a sampled threshold check, not event localization; the solver
    /// does not interpolate a crossing between time steps.
    pub fn try_set_stop_condition(
        &mut self,
        stop_condition: HashMap<String, f64>,
    ) -> Result<(), BeError> {
        let mut compiled = Vec::with_capacity(stop_condition.len());
        for (name, target) in stop_condition {
            if !target.is_finite() {
                return Err(BeError::InvalidConfiguration(
                    "stop conditions must reference known states and finite targets",
                ));
            }
            let Some(index) = self
                .newton
                .values
                .iter()
                .position(|state_name| state_name == &name)
            else {
                return Err(BeError::InvalidConfiguration(
                    "stop conditions must reference known states and finite targets",
                ));
            };
            compiled.push((index, target));
        }
        self.stop_conditions = compiled;
        Ok(())
    }

    pub fn try_set_equation_parameters(&mut self, params: Option<&[&str]>) -> Result<(), BeError> {
        if self.newton.eq_system.is_empty() {
            return Err(BeError::InvalidConfiguration(
                "equation parameter schemas require a symbolic equation system",
            ));
        }
        self.newton.try_set_equation_parameters(params)?;
        Ok(())
    }

    pub fn set_parameter_values(&mut self, values: DVector<f64>) -> Result<(), BeError> {
        if self.newton.eq_system.is_empty() {
            let start = self.telemetry_mode.collects_timings().then(Instant::now);
            if self.telemetry_mode.collects_counters() {
                self.newton
                    .record_parameter_bind_result(start.map(|start| start.elapsed()), false);
            }
            return Err(BeError::InvalidConfiguration(
                "symbolic parameter values cannot be rebound on a numerical-callback problem",
            ));
        }
        self.newton.set_parameter_values(values)?;
        Ok(())
    }

    /// Fallible alias emphasizing that parameter validation can fail.
    pub fn try_set_parameter_values(&mut self, values: DVector<f64>) -> Result<(), BeError> {
        self.set_parameter_values(values)
    }

    pub fn set_neighborhood_check(&mut self, tolerance: f64) {
        self.try_set_neighborhood_check(tolerance)
            .expect("Backward Euler stop-condition tolerance should be valid");
    }

    pub fn try_set_neighborhood_check(&mut self, tolerance: f64) -> Result<(), BeError> {
        if tolerance.is_finite() && tolerance > 0.0 {
            self.neighborhood_check = tolerance;
            Ok(())
        } else {
            Err(BeError::InvalidConfiguration(
                "stop-condition tolerance must be positive and finite",
            ))
        }
    }

    /// Installs pure numerical ODE callbacks for Backward Euler.
    ///
    /// If `jac` is `None`, Newton iterations use a finite-difference Jacobian.
    /// Symbolic equation parameter setters do not apply to numerical callbacks;
    /// manage values captured by such callbacks through their own shared state.
    pub fn set_native_ode_callbacks<F, J>(&mut self, rhs: F, jac: Option<J>)
    where
        F: Fn(f64, &DVector<f64>) -> DVector<f64> + Send + Sync + 'static,
        J: Fn(f64, &DVector<f64>) -> DMatrix<f64> + Send + Sync + 'static,
    {
        self.native_rhs = Some(Arc::new(rhs));
        self.native_jac = jac.map(|j| Arc::new(j) as BeNativeJac);
        self.newton.jac = None;
        self.newton.clear_symbolic_ivp_telemetry();
    }

    fn check_stop_condition(&self, y: &DVector<f64>) -> bool {
        self.stop_conditions
            .iter()
            .any(|(index, target)| (y[*index] - target).abs() <= self.neighborhood_check)
    }
    /// Validates the current problem configuration without panicking.
    pub fn try_check(&self) -> Result<(), BeError> {
        validate_be_configuration(
            if self.native_rhs.is_some() {
                None
            } else {
                Some(self.newton.eq_system.len())
            },
            &self.newton.values,
            &self.newton.arg,
            self.newton.tolerance,
            self.newton.max_iterations,
            self.h,
            self.t0,
            self.t_bound,
            &self.y0,
        )?;
        Ok(())
    }

    /// Compatibility wrapper; prefer [`BE::try_check`] in fallible code.
    pub fn check(&self) {
        self.try_check()
            .expect("Backward Euler configuration should be valid");
    }

    pub fn _step_impl(&mut self) -> (bool, Option<String>) {
        match self.try_step_impl() {
            Ok(()) => (true, None),
            Err(error) => (false, Some(error.to_string())),
        }
    }

    fn try_step_impl(&mut self) -> Result<(), BeError> {
        let remaining = self.t_bound - self.t;
        if remaining <= 0.0 {
            return Ok(());
        }
        let dt = match self.h {
            Some(h) => h.min(remaining),
            None => self.newton.suggest_step_size(self.t, &self.y, remaining)?,
        };
        let t_next = self.t + dt;
        if !t_next.is_finite() || t_next <= self.t {
            return Err(BeError::StepUnderflow {
                time: self.t,
                step: dt,
            });
        }

        self.newton.dt = dt;
        self.newton.set_step_mode(NreStepMode::Fixed);
        self.newton.t_bound = None;
        self.newton.set_t(t_next);
        self.newton.set_initial_guess(self.y.clone());
        self.y = self.newton.try_solve()?;
        self.t = if dt >= remaining {
            self.t_bound
        } else {
            t_next
        };
        Ok(())
    }

    fn try_step_with_telemetry(&mut self) -> Result<(), BeError> {
        self.install_native_callbacks();
        match self.telemetry_mode {
            BeTelemetryMode::Off => self.try_step_inner::<false>(),
            BeTelemetryMode::Counters | BeTelemetryMode::Timings => self.try_step_inner::<true>(),
        }
    }

    /// Advances by one step using already-prepared callbacks.
    ///
    /// Native callbacks configured through BE are installed automatically. For
    /// symbolic equations, prepare the NRE callbacks first or use [`BE::try_solve`].
    pub fn try_step(&mut self) -> Result<(), BeError> {
        let result = self.try_step_with_telemetry();
        if self.telemetry_mode.collects_counters() {
            self.statistics.step_calls = self.statistics.step_calls.saturating_add(1);
        }
        if let Err(error) = &result {
            self.message = Some(error.to_string());
            self.status = BeStatus::Failed;
            if self.telemetry_mode.collects_counters() {
                self.newton.record_failure(error.failure_kind());
            }
        }
        result
    }

    /// Compatibility wrapper; use [`BE::try_step`] to handle failures explicitly.
    pub fn step(&mut self) {
        if let Err(error) = self.try_step() {
            self.last_error = Some(error);
        }
    }

    fn try_step_inner<const COUNTERS: bool>(&mut self) -> Result<(), BeError> {
        if self.t >= self.t_bound {
            self.t = self.t_bound;
            self.status = BeStatus::Finished;
            return Ok(());
        }
        let old_t = self.t;
        if let Err(error) = self.try_step_impl() {
            if COUNTERS {
                self.newton.record_failed_step();
            }
            return Err(error);
        }
        if COUNTERS {
            self.newton.record_accepted_step();
        }
        self.t_old = Some(old_t);
        if self.check_stop_condition(&self.y) {
            self.status = BeStatus::StoppedByCondition;
        } else if self.t >= self.t_bound {
            self.status = BeStatus::Finished;
        } else {
            self.status = BeStatus::Running;
        }
        Ok(())
    }

    fn run_main_loop(&mut self) {
        self.telemetry_locked = true;
        match self.telemetry_mode {
            BeTelemetryMode::Off => self.main_loop_impl::<false, false>(),
            BeTelemetryMode::Counters => self.main_loop_impl::<true, false>(),
            BeTelemetryMode::Timings => self.main_loop_impl::<true, true>(),
        }
    }

    /// Runs the integration loop using callbacks/backend already installed on BE.
    /// Prefer [`BE::try_solve`] when preparation and integration should be combined.
    pub fn try_main_loop(&mut self) -> Result<(), BeError> {
        self.last_error = None;
        self.install_native_callbacks();
        self.run_main_loop();
        let result = if let Some(error) = self.last_error.take() {
            Err(error)
        } else if matches!(
            self.status,
            BeStatus::Finished | BeStatus::StoppedByCondition
        ) {
            Ok(())
        } else {
            Err(BeError::InvalidConfiguration(
                "solver stopped without a successful terminal status",
            ))
        };
        if let Err(error) = &result {
            if self.telemetry_mode.collects_counters() {
                self.newton.record_failure(error.failure_kind());
            }
        }
        result
    }

    /// Compatibility wrapper; use [`BE::try_main_loop`] to handle failures explicitly.
    pub fn main_loop(&mut self) {
        if let Err(error) = self.try_main_loop() {
            self.message = Some(error.to_string());
            self.status = BeStatus::Failed;
            self.last_error = Some(error);
        }
    }

    fn main_loop_impl<const COUNTERS: bool, const TIMINGS: bool>(&mut self) {
        let start = if TIMINGS { Some(Instant::now()) } else { None };
        // Analogue of https://github.com/scipy/scipy/blob/main/scipy/integrate/_ivp/ivp.py

        let mut integr_status: Option<i8> = None;
        let state_dimension = self.y.len();
        let mut state_samples = Vec::with_capacity(state_dimension);
        state_samples.extend(self.y.iter().copied());
        let mut t: Vec<f64> = vec![self.t];
        let mut step_count = 0usize;
        while integr_status.is_none() {
            if step_count >= self.max_steps {
                self.status = BeStatus::Failed;
                let error = BeError::StepLimit {
                    max_steps: self.max_steps,
                };
                self.message = Some(error.to_string());
                self.last_error = Some(error);
                integr_status = Some(-1);
                break;
            }
            let result = self.try_step_inner::<COUNTERS>();
            if let Err(error) = result {
                self.message = Some(error.to_string());
                self.status = BeStatus::Failed;
                self.last_error = Some(error);
            }
            if COUNTERS {
                self.statistics.step_calls = self.statistics.step_calls.saturating_add(1);
            }
            let _status: i8 = 0;
            //   info("\n iteration: {}", i);
            //if i == 100 {panic!()}
            step_count += 1;
            if self.status == BeStatus::Finished {
                integr_status = Some(0)
            } else if self.status == BeStatus::Failed {
                integr_status = Some(-1);
                break;
            }
            // Check stop condition before storing solution
            if self.check_stop_condition(&self.y) {
                self.status = BeStatus::StoppedByCondition;
                integr_status = Some(0);
            }

            //  info("i: {}, t: {}, y: {:?}, _status: {}", i, self.Solver_instance.t, self.Solver_instance.y, _status);
            if t.last().is_some_and(|last| self.t > *last) {
                t.push(self.t);
                state_samples.extend(self.y.iter().copied());
            }
            // info("time  {:?}, len {}", t, t.len())
        }

        let output_assembly_start = TIMINGS.then(Instant::now);
        let rows = t.len();
        let y_res = DMatrix::from_vec(state_dimension, rows, state_samples).transpose();
        let t_res = DVector::from_vec(t);

        // info("time  {:?}, len {}", &t_res, t_res.len());
        //info("y  {:?}, len {:?}", &y_res, y_res.shape());
        if COUNTERS {
            if let Some(start) = start {
                let duration = start.elapsed();
                info!("Program took {} milliseconds to run", duration.as_millis());
                self.statistics.record_solve_duration(duration);
            } else {
                self.statistics.solve_calls = self.statistics.solve_calls.saturating_add(1);
            }
        }
        self.t_result = t_res;
        self.y_result = y_res;
        if COUNTERS {
            self.newton
                .record_output_assembly(output_assembly_start.map(|start| start.elapsed()));
        }
    } //

    pub fn try_solve(&mut self) -> Result<(), BeError> {
        self.t = self.t0;
        self.y = self.y0.clone();
        self.t_old = None;
        self.t_result = DVector::from_vec(vec![self.t0]);
        self.y_result = DMatrix::from_row_slice(1, self.y0.len(), self.y0.as_slice());
        self.status = BeStatus::Running;
        self.message = None;
        self.last_error = None;
        self.newton.set_initial_guess(self.y0.clone());

        let result = self.run_current_interval();
        if let Err(error) = &result {
            if self.telemetry_mode.collects_counters() {
                self.newton.record_failure(error.failure_kind());
            }
        }
        result
    }

    /// Continues integration from the latest accepted state to `new_t_bound`.
    ///
    /// The existing trajectory and prepared callbacks are retained. The bound
    /// must be finite and strictly later than the current accepted time.
    /// Calling [`Self::try_solve`] afterwards restarts from `(t0, y0)` and uses
    /// the currently configured bound, including this extended bound.
    pub fn try_continue_to(&mut self, new_t_bound: f64) -> Result<(), BeError> {
        if self.telemetry_mode == BeTelemetryMode::Off {
            return self.try_continue_to_inner(new_t_bound);
        }
        let start = self.telemetry_mode.collects_timings().then(Instant::now);
        let result = self.try_continue_to_inner(new_t_bound);
        self.continuation_statistics
            .record(start.map(|start| start.elapsed()), result.is_ok());
        if let Err(error) = &result {
            self.newton.record_failure(error.failure_kind());
        }
        result
    }

    /// Restarts a prepared BE runtime from a new state and interval.
    ///
    /// Symbolic/native callbacks and their generated backend remain installed;
    /// only integration state and the Newton initial guess are replaced. This
    /// is the explicit-state counterpart of [`Self::try_continue_to`].
    pub fn try_restart_with_initial_state(
        &mut self,
        t0: f64,
        y0: DVector<f64>,
        t_bound: f64,
    ) -> Result<(), BeError> {
        let equation_dimension =
            (!self.newton.eq_system.is_empty()).then_some(self.newton.eq_system.len());
        validate_be_configuration(
            equation_dimension,
            &self.newton.values,
            &self.newton.arg,
            self.newton.tolerance,
            self.newton.max_iterations,
            self.h,
            t0,
            t_bound,
            &y0,
        )?;
        if y0.len() != self.y0.len() {
            return Err(BeError::InvalidConfiguration(
                "restart initial state dimension differs from the prepared problem",
            ));
        }

        self.t0 = t0;
        self.t_bound = t_bound;
        self.y0 = y0.clone();
        self.t = t0;
        self.y = y0.clone();
        self.t_old = None;
        self.t_result = DVector::from_vec(vec![t0]);
        self.y_result = DMatrix::from_row_slice(1, y0.len(), y0.as_slice());
        self.status = BeStatus::Running;
        self.message = None;
        self.last_error = None;
        self.telemetry_locked = false;
        self.newton.set_t(t0);
        self.newton.t_bound = Some(t_bound);
        self.newton.set_initial_guess(y0);
        Ok(())
    }

    fn try_continue_to_inner(&mut self, new_t_bound: f64) -> Result<(), BeError> {
        if self.y.is_empty() || self.t_result.is_empty() {
            return Err(BeError::InvalidConfiguration(
                "configure an initial value problem before continuing",
            ));
        }
        if !new_t_bound.is_finite() || new_t_bound <= self.t {
            return Err(BeError::InvalidConfiguration(
                "continuation bound must be finite and greater than the current accepted time",
            ));
        }

        let prefix_times = std::mem::replace(&mut self.t_result, DVector::from_vec(vec![self.t]));
        let prefix_states = std::mem::replace(
            &mut self.y_result,
            DMatrix::from_row_slice(1, self.y.len(), self.y.as_slice()),
        );
        self.t_bound = new_t_bound;
        self.t_old = None;
        self.status = BeStatus::Running;
        self.message = None;
        self.last_error = None;
        self.newton.set_initial_guess(self.y.clone());

        let result = self.run_current_interval();
        let output_assembly_start = self.telemetry_mode.collects_timings().then(Instant::now);
        self.append_trajectory(prefix_times, prefix_states);
        if self.telemetry_mode.collects_counters() {
            self.newton
                .record_output_assembly(output_assembly_start.map(|start| start.elapsed()));
        }
        result
    }

    fn append_trajectory(&mut self, prefix_times: DVector<f64>, prefix_states: DMatrix<f64>) {
        let prefix_len = prefix_times.len();
        let segment_len = self.t_result.len();
        debug_assert_eq!(prefix_states.nrows(), prefix_len);
        debug_assert_eq!(prefix_states.ncols(), self.y_result.ncols());
        debug_assert!(segment_len > 0);

        let mut times = Vec::with_capacity(prefix_len + segment_len.saturating_sub(1));
        times.extend(prefix_times.iter().copied());
        times.extend(self.t_result.iter().skip(1).copied());

        let rows = times.len();
        let cols = self.y_result.ncols();
        let states = DMatrix::from_fn(rows, cols, |row, col| {
            if row < prefix_len {
                prefix_states[(row, col)]
            } else {
                self.y_result[(row - prefix_len + 1, col)]
            }
        });
        self.t_result = DVector::from_vec(times);
        self.y_result = states;
    }

    fn run_current_interval(&mut self) -> Result<(), BeError> {
        self.telemetry_locked = true;
        if self.t_bound == self.t {
            self.status = BeStatus::Finished;
            return Ok(());
        }
        if self.check_stop_condition(&self.y) {
            self.status = BeStatus::StoppedByCondition;
            return Ok(());
        }

        self.install_native_callbacks();
        if self.newton.jac.is_none() {
            let start = self.telemetry_mode.collects_timings().then(Instant::now);
            let mut options = crate::symbolic::symbolic_ivp::SymbolicIvpProblemOptions::new();
            if let Some(parameters) = self.newton.equation_parameters.clone() {
                options = options.with_equation_parameters(parameters);
            }
            if let Some(values) = self.newton.equation_parameter_values.clone() {
                options = options.with_equation_parameter_values(values);
            }
            let symbolic_telemetry = self.newton.symbolic_ivp_telemetry_for_preparation();
            options = options.with_telemetry(symbolic_telemetry);
            options = options.with_symbolic_assembly_backend(
                self.newton.symbolic_assembly_backend().as_ivp_backend(),
            );
            let prepared = match prepare_generated_symbolic_ivp_problem(
                self.newton.eq_system.clone(),
                self.newton.values.clone(),
                self.newton.arg.clone(),
                options.with_aot_options(self.generated_backend_config.aot_options),
                self.generated_backend_config.clone(),
            ) {
                Ok(prepared) => prepared,
                Err(error) => {
                    self.status = BeStatus::Failed;
                    self.message = Some(error.to_string());
                    return Err(BeError::GeneratedBackend(error));
                }
            };
            self.generated_backend_config.resolver = prepared.updated_resolver.clone();
            self.newton
                .install_prepared_backend(prepared.into_problem());
            if self.telemetry_mode.collects_counters() {
                if let Some(start) = start {
                    self.statistics
                        .record_backend_prepare_duration(start.elapsed());
                } else {
                    self.statistics.backend_prepare_calls =
                        self.statistics.backend_prepare_calls.saturating_add(1);
                }
            }
        }
        self.run_main_loop();
        if let Some(error) = self.last_error.take() {
            return Err(error);
        }
        if self.status != BeStatus::Finished && self.status != BeStatus::StoppedByCondition {
            return Err(BeError::InvalidConfiguration(
                "solver stopped without a successful terminal status",
            ));
        }
        Ok(())
    }

    fn install_native_callbacks(&mut self) {
        if let Some(rhs) = self.native_rhs.clone() {
            let jac = self.native_jac.clone();
            self.newton.set_native_callbacks(
                Box::new(move |t: f64, y: &DVector<f64>| rhs(t, y)),
                jac.map(|jac_fun| {
                    Box::new(move |t: f64, y: &DVector<f64>| jac_fun(t, y))
                        as Box<dyn Fn(f64, &DVector<f64>) -> DMatrix<f64>>
                }),
            );
        }
    }

    pub fn solve(&mut self) -> () {
        self.try_solve()
            .expect("Backward Euler symbolic IVP backend generation should succeed");
    }
    pub fn plot_result(&self) -> () {
        plots(
            self.newton.arg.clone(),
            self.newton.values.clone(),
            self.t_result.clone(),
            self.y_result.clone(),
        );
        info!("result plotted");
    }

    /// Returns sampled times and states as `(times, states)`.
    ///
    /// `states` has one row per time sample and one column per state variable.
    /// The initial state is included, and after failure the matrices contain
    /// only the initial sample plus states from successfully accepted steps.
    pub fn get_result(&self) -> (Option<DVector<f64>>, Option<DMatrix<f64>>) {
        (Some(self.t_result.clone()), Some(self.y_result.clone()))
    }

    /// Borrows the accepted trajectory without cloning its potentially large
    /// time vector or state matrix. `states` is shaped `(samples, variables)`.
    pub fn trajectory(&self) -> (&DVector<f64>, &DMatrix<f64>) {
        (&self.t_result, &self.y_result)
    }

    /// Returns the typed lifecycle state.
    pub fn status(&self) -> BeStatus {
        self.status
    }

    /// Returns the stable legacy text form of the lifecycle state.
    ///
    /// BE uses [`BeStatus`] internally; this allocating conversion is kept only
    /// for callers that still consume textual lifecycle flags.
    pub fn get_status(&self) -> String {
        self.status.as_str().to_owned()
    }
}

fn validate_be_configuration(
    equation_count: Option<usize>,
    values: &[String],
    arg: &str,
    tolerance: f64,
    max_iterations: usize,
    h: Option<f64>,
    t0: f64,
    t_bound: f64,
    y0: &DVector<f64>,
) -> Result<(), BeError> {
    if y0.is_empty() || !y0.iter().all(|value| value.is_finite()) {
        return Err(BeError::InvalidConfiguration(
            "initial state must be nonempty and finite",
        ));
    }
    if values.len() != y0.len() || equation_count.is_some_and(|count| count != y0.len()) {
        let dimension_message = if equation_count.is_some() {
            "state, variable names, and equations must have equal dimensions"
        } else {
            "state and variable names must have equal dimensions"
        };
        return Err(BeError::InvalidConfiguration(dimension_message));
    }
    if arg.trim().is_empty()
        || values.iter().any(|name| name.trim().is_empty())
        || values
            .iter()
            .collect::<std::collections::HashSet<_>>()
            .len()
            != values.len()
    {
        return Err(BeError::InvalidConfiguration(
            "time argument and state variable names must be nonempty and unique",
        ));
    }
    if !tolerance.is_finite() || tolerance <= 0.0 || max_iterations == 0 {
        return Err(BeError::InvalidConfiguration(
            "tolerance must be positive and finite, and max_iterations must be nonzero",
        ));
    }
    if !t0.is_finite() || !t_bound.is_finite() || t_bound < t0 {
        return Err(BeError::InvalidConfiguration(
            "time bounds must be finite and backward integration is not supported",
        ));
    }
    if h.is_some_and(|step| !step.is_finite() || step <= 0.0) {
        return Err(BeError::InvalidConfiguration(
            "fixed step must be positive and finite",
        ));
    }
    Ok(())
}

fn make_be_newton(
    eq_system: Vec<Expr>,
    initial_guess: DVector<f64>,
    values: Vec<String>,
    arg: String,
    tolerance: f64,
    max_iterations: usize,
    h: Option<f64>,
    t_bound: f64,
) -> NRE {
    if let Some(dt) = h {
        NRE::new(
            eq_system,
            initial_guess,
            values,
            arg,
            tolerance,
            max_iterations,
            dt,
            true,
            None,
        )
    } else {
        info!("Backward Euler uses the legacy local step-size heuristic");
        NRE::new(
            eq_system,
            initial_guess,
            values,
            arg,
            tolerance,
            max_iterations,
            1e-4,
            false,
            Some(t_bound),
        )
    }
}

#[cfg(test)]
#[path = "tests/be_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "tests/performance_story_tests.rs"]
mod performance_story_tests;

#[cfg(test)]
#[path = "tests/cold_process_story_tests.rs"]
mod cold_process_story_tests;