deimos_numerics 0.16.1

Numerical methods and control systems analysis
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
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
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
//! PID tuning helpers based on low-order process models.
//!
//! The implementation focuses on the pragmatic workflow that is easiest
//! to validate and explain:
//!
//! - fit `FOPDT` / `SOPDT` process models from sampled step-response data
//! - tune `PI` / `PIDF` controllers from those fitted models with SIMC-style
//!   formulas
//!
//! The module also includes:
//!
//! - direct frequency-domain tuning from SISO transfer-function / state-space
//!   plant models
//! - discrete-time step-response optimization from arbitrary identified or
//!   modeled plants
//! - a pragmatic `OKID -> ERA -> PID` bridge for tuning from sampled I/O data
//!
//! # Two Intuitions
//!
//! 1. **Process-model view.** PID tuning is easiest when the plant is reduced
//!    to a small interpretable surrogate like FOPDT or SOPDT.
//! 2. **Closed-loop-objective view.** The richer design paths in this module
//!    tune gains by directly evaluating the modeled closed-loop behavior rather
//!    than by relying only on hand-derived formulas.
//!
//! # Glossary
//!
//! - **SIMC:** Simple internal model control tuning rules.
//! - **`lambda`:** Desired closed-loop time scale in the SIMC tuning rules.
//! - **FOPDT / SOPDT:** Low-order process models with explicit delay.
//! - **OKID/ERA path:** Identification-driven path from sampled I/O data to a
//!   discrete model and then to a tuned PID controller.
//!
//! # Mathematical Formulation
//!
//! The module combines:
//!
//! - low-order process-model fitting from step data
//! - SIMC-style analytical tuning rules
//! - sampled frequency-domain and step-response objective functions
//! - identification-driven tuning on an ERA-realized discrete model
//!
//! # Implementation Notes
//!
//! - FOPDT and SOPDT fitting use structured seeds followed by
//!   Levenberg-Marquardt refinement.
//! - The optimization-backed paths remain SISO and `f64`-oriented in this
//!   implementation.
//! - Public FOPDT / SOPDT model types are shared with the LTI layer to avoid
//!   parallel process-model type hierarchies.

use super::pid::{AntiWindup, Pid, PidError};
use crate::control::identification::{
    EraError, EraParams, OkidError, OkidParams, era_from_markov, okid,
};
use crate::control::lti::state_space::{ContinuousStateSpace, DiscreteStateSpace, StateSpaceError};
use crate::control::lti::{ContinuousTransferFunction, DiscreteTransferFunction, LtiError};
use crate::control::realization::max_square_era_block_dim;
use alloc::vec::Vec;
use core::fmt;
use faer::Mat;
use faer::complex::Complex;
use faer_traits::RealField;
use faer_traits::ext::ComplexFieldExt;
use levenberg_marquardt::{LeastSquaresProblem, LevenbergMarquardt, differentiate_numerically};
use nalgebra::storage::Owned;
use nalgebra::{Dyn, OMatrix, OVector, U1, U2, U3, U4, VecStorage, Vector2, Vector3, Vector4};
use num_traits::Float;

/// Errors produced by PID process-model fitting and SIMC-style tuning.
#[derive(Debug)]
pub enum PidDesignError {
    /// The sampled data is structurally invalid.
    InvalidData {
        /// Identifies the structural property that failed validation.
        which: &'static str,
    },
    /// The detected input step is too small to identify a process gain.
    InvalidStepAmplitude,
    /// The observed output change is too small to identify a process gain.
    InvalidResponseAmplitude,
    /// A delay estimate could not be formed from the sampled response.
    InvalidDelayEstimate,
    /// The fitted process parameters are not physically meaningful.
    InvalidProcessFit,
    /// The requested tuning parameter is invalid.
    InvalidTuningParameter {
        /// Identifies the invalid tuning parameter.
        which: &'static str,
    },
    /// The requested frequency-domain design target is invalid.
    InvalidFrequencyTarget {
        /// Identifies the invalid crossover or margin target.
        which: &'static str,
    },
    /// A model-based or identification-based tuning solve did not converge to
    /// a usable controller.
    OptimizationFailed {
        /// Identifies the optimization stage that failed.
        which: &'static str,
    },
    /// LTI analysis or conversion failed.
    Lti(LtiError),
    /// State-space conversion or validation failed.
    StateSpace(StateSpaceError),
    /// OKID identification failed.
    Okid(OkidError),
    /// ERA realization failed.
    Era(EraError),
    /// PID construction failed after the tuning rule produced gains.
    Pid(PidError),
}

impl fmt::Display for PidDesignError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl core::error::Error for PidDesignError {}

impl From<PidError> for PidDesignError {
    fn from(value: PidError) -> Self {
        Self::Pid(value)
    }
}

impl From<LtiError> for PidDesignError {
    fn from(value: LtiError) -> Self {
        Self::Lti(value)
    }
}

impl From<StateSpaceError> for PidDesignError {
    fn from(value: StateSpaceError) -> Self {
        Self::StateSpace(value)
    }
}

impl From<OkidError> for PidDesignError {
    fn from(value: OkidError) -> Self {
        Self::Okid(value)
    }
}

impl From<EraError> for PidDesignError {
    fn from(value: EraError) -> Self {
        Self::Era(value)
    }
}

/// Sampled SISO step-response experiment data.
///
/// The implementation assumes one dominant input step over the record.
#[derive(Clone, Debug, PartialEq)]
pub struct StepResponseData<R> {
    time: Vec<R>,
    input: Vec<R>,
    output: Vec<R>,
}

impl<R> StepResponseData<R>
where
    R: Float,
{
    /// Creates validated step-response data.
    pub fn new(time: Vec<R>, input: Vec<R>, output: Vec<R>) -> Result<Self, PidDesignError> {
        if time.len() < 4 {
            return Err(PidDesignError::InvalidData { which: "time.len" });
        }
        if time.len() != input.len() || time.len() != output.len() {
            return Err(PidDesignError::InvalidData {
                which: "length_mismatch",
            });
        }
        if time
            .iter()
            .chain(input.iter())
            .chain(output.iter())
            .any(|value| !value.is_finite())
        {
            return Err(PidDesignError::InvalidData {
                which: "nonfinite_sample",
            });
        }
        if time
            .windows(2)
            .any(|window| window[1].partial_cmp(&window[0]) != Some(core::cmp::Ordering::Greater))
        {
            return Err(PidDesignError::InvalidData {
                which: "nonmonotone_time",
            });
        }
        Ok(Self {
            time,
            input,
            output,
        })
    }

    /// Sample times.
    #[must_use]
    pub fn time(&self) -> &[R] {
        &self.time
    }

    /// Input samples.
    #[must_use]
    pub fn input(&self) -> &[R] {
        &self.input
    }

    /// Output samples.
    #[must_use]
    pub fn output(&self) -> &[R] {
        &self.output
    }
}

pub use crate::control::lti::{FopdtModel, SopdtModel};

/// Result of fitting a low-order process model to sampled data.
#[derive(Clone, Debug, PartialEq)]
pub struct ProcessFitResult<M, R> {
    /// Structured initial estimate derived directly from response landmarks.
    pub initial: M,
    /// Refined model after local nonlinear least-squares improvement.
    pub model: M,
    /// Sum of squared residuals of the final fit.
    pub objective: R,
}

/// Solver options for the nonlinear least-squares process-model fitters.
///
/// The fitting path uses Levenberg-Marquardt under the hood. This options
/// struct exposes only the two knobs that matter most in practice for the
/// crate's small FOPDT/SOPDT problems:
///
/// - relative termination tolerance
/// - evaluation patience
///
/// Leaving a field as `None` preserves the solver crate's built-in default.
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct ProcessModelFitOptions {
    /// Optional LM tolerance applied through `with_tol(...)`.
    ///
    /// Smaller values drive a more rigorous fit but can cost more iterations.
    pub tolerance: Option<f64>,
    /// Optional LM patience factor applied through `with_patience(...)`.
    ///
    /// The solver interprets this as a multiplier on the parameter count when
    /// bounding function evaluations.
    pub patience: Option<usize>,
}

/// PID controller family produced by a tuning rule.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PidControllerKind {
    /// `PI` controller. The derivative path is disabled.
    Pi,
    /// `PIDF` controller with a first-order filtered derivative term.
    Pid,
}

/// SIMC-style tuning parameters shared by the low-order process-model rules.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SimcPidParams<R> {
    /// Desired closed-loop time constant / aggressiveness parameter.
    pub lambda: R,
    /// Whether to return a `PI` or `PIDF` controller.
    pub controller: PidControllerKind,
    /// Derivative filter ratio `N_f`, interpreted as `derivative_filter = N_f / Td`.
    pub derivative_filter_ratio: R,
    /// Runtime anti-windup policy to attach to the returned controller.
    pub anti_windup: AntiWindup<R>,
    /// Optional direct limits on the stored integral contribution.
    pub integrator_limits: Option<(R, R)>,
}

impl<R> SimcPidParams<R>
where
    R: Float,
{
    /// Creates validated tuning parameters.
    pub fn new(
        lambda: R,
        controller: PidControllerKind,
        derivative_filter_ratio: R,
        anti_windup: AntiWindup<R>,
    ) -> Result<Self, PidDesignError> {
        if !lambda.is_finite() || lambda <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter { which: "lambda" });
        }
        if !derivative_filter_ratio.is_finite() || derivative_filter_ratio <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "derivative_filter_ratio",
            });
        }
        Ok(Self {
            lambda,
            controller,
            derivative_filter_ratio,
            anti_windup,
            integrator_limits: None,
        })
    }

    /// Adds explicit integrator limits to the tuned controller.
    pub fn with_integrator_limits(mut self, low: R, high: R) -> Result<Self, PidDesignError> {
        if !low.is_finite() || !high.is_finite() || low > high {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "integrator_limits",
            });
        }
        self.integrator_limits = Some((low, high));
        Ok(self)
    }
}

/// Result of tuning a PID controller from a low-order process model.
#[derive(Clone, Debug, PartialEq)]
pub struct ProcessPidDesign<R, M> {
    /// Tuned runtime controller.
    pub pid: Pid<R>,
    /// Process model the tuning rule used.
    pub model: M,
    /// Tuning parameters applied by the rule.
    pub params: SimcPidParams<R>,
}

/// Result of tuning from sampled step-response data.
#[derive(Clone, Debug, PartialEq)]
pub struct StepFitPidDesign<R, M> {
    /// Fitted process model.
    pub fit: ProcessFitResult<M, R>,
    /// Tuned controller using the fitted model.
    pub design: ProcessPidDesign<R, M>,
}

/// Frequency-domain PID tuning parameters for SISO plant models.
///
/// The implementation solves for:
///
/// - `Kp`
/// - `Ti`
///
/// and, for `Pid`, derives `Td = Ti / ti_over_td_ratio`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FrequencyPidParams<R> {
    /// Target crossover frequency in rad/s.
    pub crossover_frequency: R,
    /// Desired phase margin in degrees.
    pub phase_margin_deg: R,
    /// Whether to return a `PI` or `PIDF` controller.
    pub controller: PidControllerKind,
    /// Derivative filter ratio `N_f`, interpreted as `derivative_filter = N_f / Td`.
    pub derivative_filter_ratio: R,
    /// Fixed ratio `Ti / Td` used by the first PID frequency-design pass.
    pub ti_over_td_ratio: R,
    /// Runtime anti-windup policy attached to the returned controller.
    pub anti_windup: AntiWindup<R>,
    /// Optional direct limits on the stored integral contribution.
    pub integrator_limits: Option<(R, R)>,
}

impl<R> FrequencyPidParams<R>
where
    R: Float,
{
    /// Creates validated frequency-domain tuning parameters.
    pub fn new(
        crossover_frequency: R,
        phase_margin_deg: R,
        controller: PidControllerKind,
        derivative_filter_ratio: R,
        ti_over_td_ratio: R,
        anti_windup: AntiWindup<R>,
    ) -> Result<Self, PidDesignError> {
        if !crossover_frequency.is_finite() || crossover_frequency <= R::zero() {
            return Err(PidDesignError::InvalidFrequencyTarget {
                which: "crossover_frequency",
            });
        }
        if !phase_margin_deg.is_finite()
            || phase_margin_deg <= R::zero()
            || phase_margin_deg >= R::from(180.0).unwrap()
        {
            return Err(PidDesignError::InvalidFrequencyTarget {
                which: "phase_margin_deg",
            });
        }
        if !derivative_filter_ratio.is_finite() || derivative_filter_ratio <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "derivative_filter_ratio",
            });
        }
        if !ti_over_td_ratio.is_finite() || ti_over_td_ratio <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "ti_over_td_ratio",
            });
        }
        Ok(Self {
            crossover_frequency,
            phase_margin_deg,
            controller,
            derivative_filter_ratio,
            ti_over_td_ratio,
            anti_windup,
            integrator_limits: None,
        })
    }

    /// Adds explicit integrator limits to the tuned controller.
    pub fn with_integrator_limits(mut self, low: R, high: R) -> Result<Self, PidDesignError> {
        if !low.is_finite() || !high.is_finite() || low > high {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "integrator_limits",
            });
        }
        self.integrator_limits = Some((low, high));
        Ok(self)
    }
}

/// Result of frequency-domain PID tuning.
#[derive(Clone, Debug, PartialEq)]
pub struct FrequencyPidDesign<R> {
    /// Tuned runtime controller.
    pub pid: Pid<R>,
    /// Applied frequency-domain tuning parameters.
    pub params: FrequencyPidParams<R>,
    /// Final open-loop value `L(jω_c)` or `L(e^{j ω_c dt})` at the target crossover.
    pub loop_value: Complex<R>,
    /// Achieved phase margin inferred from the final loop phase at the target.
    pub achieved_phase_margin_deg: R,
}

/// Step-response optimization tuning parameters for discrete-time plant models.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StepOptimizationPidParams<R> {
    /// Desired closed-loop time constant used to build the reference step response.
    pub lambda: R,
    /// Number of discrete samples used in the optimization horizon.
    pub horizon_steps: usize,
    /// Whether to return a `PI` or `PIDF` controller.
    pub controller: PidControllerKind,
    /// Derivative filter ratio `N_f`, interpreted as `derivative_filter = N_f / Td`.
    pub derivative_filter_ratio: R,
    /// Fixed ratio `Ti / Td` used by the first PID optimization pass.
    pub ti_over_td_ratio: R,
    /// Runtime anti-windup policy attached to the returned controller.
    pub anti_windup: AntiWindup<R>,
    /// Optional direct limits on the stored integral contribution.
    pub integrator_limits: Option<(R, R)>,
    /// Weight on the control-effort residual channel.
    pub control_effort_weight: R,
}

impl<R> StepOptimizationPidParams<R>
where
    R: Float,
{
    /// Creates validated discrete-time step-optimization tuning parameters.
    pub fn new(
        lambda: R,
        horizon_steps: usize,
        controller: PidControllerKind,
        derivative_filter_ratio: R,
        ti_over_td_ratio: R,
        anti_windup: AntiWindup<R>,
    ) -> Result<Self, PidDesignError> {
        if !lambda.is_finite() || lambda <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter { which: "lambda" });
        }
        if horizon_steps == 0 {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "horizon_steps",
            });
        }
        if !derivative_filter_ratio.is_finite() || derivative_filter_ratio <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "derivative_filter_ratio",
            });
        }
        if !ti_over_td_ratio.is_finite() || ti_over_td_ratio <= R::zero() {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "ti_over_td_ratio",
            });
        }
        Ok(Self {
            lambda,
            horizon_steps,
            controller,
            derivative_filter_ratio,
            ti_over_td_ratio,
            anti_windup,
            integrator_limits: None,
            control_effort_weight: R::zero(),
        })
    }

    /// Adds explicit integrator limits to the tuned controller.
    pub fn with_integrator_limits(mut self, low: R, high: R) -> Result<Self, PidDesignError> {
        if !low.is_finite() || !high.is_finite() || low > high {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "integrator_limits",
            });
        }
        self.integrator_limits = Some((low, high));
        Ok(self)
    }

    /// Sets the control-effort penalty weight.
    pub fn with_control_effort_weight(mut self, weight: R) -> Result<Self, PidDesignError> {
        if !weight.is_finite() || weight < R::zero() {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "control_effort_weight",
            });
        }
        self.control_effort_weight = weight;
        Ok(self)
    }
}

/// Result of discrete-time step-response PID optimization.
#[derive(Clone, Debug, PartialEq)]
pub struct StepOptimizationPidDesign<R> {
    /// Tuned runtime controller.
    pub pid: Pid<R>,
    /// Applied optimization parameters.
    pub params: StepOptimizationPidParams<R>,
    /// Sum of squared tracking residuals over the optimization horizon.
    pub tracking_cost: R,
    /// Sum of squared control-effort residuals over the optimization horizon.
    pub control_cost: R,
}

/// SISO sampled input/output data used by the `OKID -> ERA` PID path.
#[derive(Clone, Debug, PartialEq)]
pub struct SampledIoData<R> {
    sample_time: R,
    input: Vec<R>,
    output: Vec<R>,
}

impl<R> SampledIoData<R>
where
    R: Float,
{
    /// Creates validated sampled SISO input/output data.
    pub fn new(sample_time: R, input: Vec<R>, output: Vec<R>) -> Result<Self, PidDesignError> {
        if !sample_time.is_finite() || sample_time <= R::zero() {
            return Err(PidDesignError::InvalidData {
                which: "sample_time",
            });
        }
        if input.len() < 2 || input.len() != output.len() {
            return Err(PidDesignError::InvalidData {
                which: "length_mismatch",
            });
        }
        if input
            .iter()
            .chain(output.iter())
            .any(|value| !value.is_finite())
        {
            return Err(PidDesignError::InvalidData {
                which: "nonfinite_sample",
            });
        }
        Ok(Self {
            sample_time,
            input,
            output,
        })
    }

    /// Sample interval.
    #[must_use]
    pub fn sample_time(&self) -> R {
        self.sample_time
    }

    /// Input samples.
    #[must_use]
    pub fn input(&self) -> &[R] {
        &self.input
    }

    /// Output samples.
    #[must_use]
    pub fn output(&self) -> &[R] {
        &self.output
    }
}

/// Result of the `OKID -> ERA -> PID` tuning path.
#[derive(Clone, Debug, PartialEq)]
pub struct OkidEraPidDesign<R: faer_traits::ComplexField> {
    /// Identified discrete-time plant realization.
    pub identified_plant: DiscreteStateSpace<R>,
    /// Final tuned controller on that identified model.
    pub design: StepOptimizationPidDesign<R>,
    /// ERA retained order of the identified model.
    pub retained_order: usize,
}

/// Fits an `FOPDT` model to sampled step-response data.
pub fn fit_fopdt_from_step_response(
    data: &StepResponseData<f64>,
) -> Result<ProcessFitResult<FopdtModel<f64>, f64>, PidDesignError> {
    fit_fopdt_from_step_response_with_options(data, ProcessModelFitOptions::default())
}

/// Fits an `FOPDT` model to sampled step-response data with explicit solver
/// options.
pub fn fit_fopdt_from_step_response_with_options(
    data: &StepResponseData<f64>,
    options: ProcessModelFitOptions,
) -> Result<ProcessFitResult<FopdtModel<f64>, f64>, PidDesignError> {
    let prepared = PreparedStepResponse::from_data(data)?;
    let initial = initial_fopdt_estimate(&prepared)?;
    let initial_objective = fopdt_objective(&prepared, initial);
    let model = match refine_fopdt(&prepared, initial, options) {
        Ok(refined) => {
            let refined_objective = fopdt_objective(&prepared, refined);
            if refined_objective <= initial_objective {
                refined
            } else {
                initial
            }
        }
        Err(_) => initial,
    };
    let objective = fopdt_objective(&prepared, model);
    Ok(ProcessFitResult {
        initial,
        model,
        objective,
    })
}

/// Fits an `SOPDT` model to sampled step-response data.
pub fn fit_sopdt_from_step_response(
    data: &StepResponseData<f64>,
) -> Result<ProcessFitResult<SopdtModel<f64>, f64>, PidDesignError> {
    fit_sopdt_from_step_response_with_options(data, ProcessModelFitOptions::default())
}

/// Fits an `SOPDT` model to sampled step-response data with explicit solver
/// options.
pub fn fit_sopdt_from_step_response_with_options(
    data: &StepResponseData<f64>,
    options: ProcessModelFitOptions,
) -> Result<ProcessFitResult<SopdtModel<f64>, f64>, PidDesignError> {
    let prepared = PreparedStepResponse::from_data(data)?;
    let fopdt = initial_fopdt_estimate(&prepared)?;
    let initial = initial_sopdt_estimate(&prepared, fopdt);
    let initial_objective = sopdt_objective(&prepared, initial);
    let model = match refine_sopdt(&prepared, initial, options) {
        Ok(refined) => {
            let refined_objective = sopdt_objective(&prepared, refined);
            if refined_objective <= initial_objective {
                refined
            } else {
                initial
            }
        }
        Err(_) => initial,
    };
    let objective = sopdt_objective(&prepared, model);
    Ok(ProcessFitResult {
        initial,
        model,
        objective,
    })
}

/// Tunes a `PI` or `PIDF` controller from an `FOPDT` process model.
pub fn design_pid_from_fopdt<R>(
    model: FopdtModel<R>,
    params: SimcPidParams<R>,
) -> Result<ProcessPidDesign<R, FopdtModel<R>>, PidDesignError>
where
    R: Float + RealField,
{
    validate_process_gain(model.gain)?;
    validate_positive(model.time_constant, "time_constant")?;
    validate_nonnegative(model.delay, "delay")?;

    let two = R::from(2.0).unwrap();
    let four = R::from(4.0).unwrap();
    let kp = model.time_constant / (model.gain * (params.lambda + model.delay));
    let (ki, kd, derivative_filter) = match params.controller {
        PidControllerKind::Pi => {
            let ti = model
                .time_constant
                .min(four * (params.lambda + model.delay));
            (kp / ti, R::zero(), None)
        }
        PidControllerKind::Pid => {
            let ti = model.time_constant + model.delay / two;
            let td =
                (model.time_constant * model.delay) / (two * model.time_constant + model.delay);
            let derivative_filter = if td > R::zero() {
                Some(params.derivative_filter_ratio / td)
            } else {
                None
            };
            (kp / ti, kp * td, derivative_filter)
        }
    };

    let mut pid = Pid::new(kp, ki, kd, derivative_filter, params.anti_windup)?;
    if let Some((low, high)) = params.integrator_limits {
        pid = pid.with_integrator_limits(low, high)?;
    }

    Ok(ProcessPidDesign { pid, model, params })
}

/// Tunes a `PI` or `PIDF` controller from an `SOPDT` process model.
pub fn design_pid_from_sopdt<R>(
    model: SopdtModel<R>,
    params: SimcPidParams<R>,
) -> Result<ProcessPidDesign<R, SopdtModel<R>>, PidDesignError>
where
    R: Float + RealField,
{
    validate_process_gain(model.gain)?;
    validate_positive(model.time_constant_1, "time_constant_1")?;
    validate_positive(model.time_constant_2, "time_constant_2")?;
    validate_nonnegative(model.delay, "delay")?;

    let lag_sum = model.time_constant_1 + model.time_constant_2;
    let four = R::from(4.0).unwrap();
    let kp = lag_sum / (model.gain * (params.lambda + model.delay));
    let (ki, kd, derivative_filter) = match params.controller {
        PidControllerKind::Pi => {
            let ti = lag_sum.min(four * (params.lambda + model.delay));
            (kp / ti, R::zero(), None)
        }
        PidControllerKind::Pid => {
            let ti = lag_sum;
            let td = (model.time_constant_1 * model.time_constant_2) / lag_sum;
            let derivative_filter = if td > R::zero() {
                Some(params.derivative_filter_ratio / td)
            } else {
                None
            };
            (kp / ti, kp * td, derivative_filter)
        }
    };

    let mut pid = Pid::new(kp, ki, kd, derivative_filter, params.anti_windup)?;
    if let Some((low, high)) = params.integrator_limits {
        pid = pid.with_integrator_limits(low, high)?;
    }

    Ok(ProcessPidDesign { pid, model, params })
}

/// Fits an `FOPDT` model from sampled step data and tunes from the result.
pub fn design_pid_from_step_response_fopdt(
    data: &StepResponseData<f64>,
    params: SimcPidParams<f64>,
) -> Result<StepFitPidDesign<f64, FopdtModel<f64>>, PidDesignError> {
    let fit = fit_fopdt_from_step_response(data)?;
    let design = design_pid_from_fopdt(fit.model, params)?;
    Ok(StepFitPidDesign { fit, design })
}

/// Fits an `SOPDT` model from sampled step data and tunes from the result.
pub fn design_pid_from_step_response_sopdt(
    data: &StepResponseData<f64>,
    params: SimcPidParams<f64>,
) -> Result<StepFitPidDesign<f64, SopdtModel<f64>>, PidDesignError> {
    let fit = fit_sopdt_from_step_response(data)?;
    let design = design_pid_from_sopdt(fit.model, params)?;
    Ok(StepFitPidDesign { fit, design })
}

/// Tunes a `PI` or `PIDF` controller against a continuous-time SISO transfer
/// function at one target crossover frequency and phase margin.
pub fn design_pid_from_continuous_tf_frequency(
    plant: &ContinuousTransferFunction<f64>,
    params: FrequencyPidParams<f64>,
) -> Result<FrequencyPidDesign<f64>, PidDesignError> {
    design_frequency_pid(FrequencyPlant::Continuous(plant), params)
}

/// Tunes a `PI` or `PIDF` controller against a discrete-time SISO transfer
/// function at one target crossover frequency and phase margin.
pub fn design_pid_from_discrete_tf_frequency(
    plant: &DiscreteTransferFunction<f64>,
    params: FrequencyPidParams<f64>,
) -> Result<FrequencyPidDesign<f64>, PidDesignError> {
    design_frequency_pid(FrequencyPlant::Discrete(plant), params)
}

/// Frequency-domain PID tuning convenience wrapper for a continuous-time SISO
/// state-space plant.
pub fn design_pid_from_continuous_state_space_frequency(
    plant: &ContinuousStateSpace<f64>,
    params: FrequencyPidParams<f64>,
) -> Result<FrequencyPidDesign<f64>, PidDesignError> {
    let tf = plant.to_transfer_function()?;
    design_pid_from_continuous_tf_frequency(&tf, params)
}

/// Frequency-domain PID tuning convenience wrapper for a discrete-time SISO
/// state-space plant.
pub fn design_pid_from_discrete_state_space_frequency(
    plant: &DiscreteStateSpace<f64>,
    params: FrequencyPidParams<f64>,
) -> Result<FrequencyPidDesign<f64>, PidDesignError> {
    let tf = plant.to_transfer_function()?;
    design_pid_from_discrete_tf_frequency(&tf, params)
}

/// Optimizes a `PI` or `PIDF` controller against the step response of a
/// discrete-time SISO transfer-function plant.
pub fn design_pid_from_discrete_tf_step_optimization(
    plant: &DiscreteTransferFunction<f64>,
    params: StepOptimizationPidParams<f64>,
) -> Result<StepOptimizationPidDesign<f64>, PidDesignError> {
    design_step_optimized_pid(plant, params)
}

/// Step-response optimization convenience wrapper for a discrete-time SISO
/// state-space plant.
pub fn design_pid_from_discrete_state_space_step_optimization(
    plant: &DiscreteStateSpace<f64>,
    params: StepOptimizationPidParams<f64>,
) -> Result<StepOptimizationPidDesign<f64>, PidDesignError> {
    let tf = plant.to_transfer_function()?;
    design_pid_from_discrete_tf_step_optimization(&tf, params)
}

/// Identifies a discrete-time plant with `OKID -> ERA` and tunes a `PI` or
/// `PIDF` controller against the identified model with step-response
/// optimization.
pub fn design_pid_from_okid_era(
    data: &SampledIoData<f64>,
    okid_params: &OkidParams,
    mut era_params: EraParams<f64>,
    pid_params: StepOptimizationPidParams<f64>,
) -> Result<OkidEraPidDesign<f64>, PidDesignError> {
    let nsamples = data.input().len();
    let inputs = Mat::from_fn(1, nsamples, |_, col| data.input()[col]);
    let outputs = Mat::from_fn(1, nsamples, |_, col| data.output()[col]);
    let okid_result = okid(outputs.as_ref(), inputs.as_ref(), okid_params)?;
    let q = max_square_era_block_dim(okid_result.markov.len());
    if q == 0 {
        return Err(PidDesignError::InvalidData {
            which: "okid_era.block_dim",
        });
    }
    era_params.sample_time = data.sample_time();
    let era_result = era_from_markov(&okid_result.markov, q, q, &era_params)?;
    let design =
        design_pid_from_discrete_state_space_step_optimization(&era_result.realized, pid_params)?;
    Ok(OkidEraPidDesign {
        identified_plant: era_result.realized,
        design,
        retained_order: era_result.retained_order,
    })
}

#[derive(Clone, Copy)]
enum FrequencyPlant<'a> {
    Continuous(&'a ContinuousTransferFunction<f64>),
    Discrete(&'a DiscreteTransferFunction<f64>),
}

impl FrequencyPlant<'_> {
    /// Evaluates the plant on the appropriate frequency-domain contour.
    fn evaluate_at_omega(self, omega: f64) -> Complex<f64> {
        match self {
            Self::Continuous(plant) => plant.evaluate(Complex::new(0.0, omega)),
            Self::Discrete(plant) => {
                let phase = omega * plant.sample_time();
                plant.evaluate(Complex::new(phase.cos(), phase.sin()))
            }
        }
    }
}

#[derive(Clone, Copy)]
struct LinearPidSpec {
    kp: f64,
    ki: f64,
    kd: f64,
    derivative_filter: Option<f64>,
}

fn design_frequency_pid(
    plant: FrequencyPlant<'_>,
    params: FrequencyPidParams<f64>,
) -> Result<FrequencyPidDesign<f64>, PidDesignError> {
    let floor = positive_floor(1.0 / params.crossover_frequency.max(1.0));
    let plant_value = plant.evaluate_at_omega(params.crossover_frequency);
    let mag_seed = (1.0 / plant_value.abs().max(1.0e-6)).max(floor);
    let ti_seed = (1.0 / params.crossover_frequency).max(floor);

    let kp_scales = [0.25, 1.0, 4.0];
    let ti_scales = [0.25, 1.0, 4.0];
    let signs = [1.0, -1.0];
    let mut best: Option<(FrequencyPidLmProblem<'_>, f64)> = None;

    for &sign in &signs {
        for &kp_scale in &kp_scales {
            for &ti_scale in &ti_scales {
                let seed = Vector2::new((mag_seed * kp_scale).ln(), (ti_seed * ti_scale).ln());
                let problem = FrequencyPidLmProblem {
                    plant,
                    params,
                    sign,
                    floor,
                    variables: seed,
                };
                let (problem, _report) = LevenbergMarquardt::new()
                    .with_patience(20)
                    .minimize(problem);
                let objective = frequency_objective(&problem.residual_vector());
                if objective.is_finite()
                    && best
                        .as_ref()
                        .is_none_or(|(_, best_objective)| objective < *best_objective)
                {
                    best = Some((problem, objective));
                }
            }
        }
    }

    let (best_problem, best_objective) = best.ok_or(PidDesignError::OptimizationFailed {
        which: "frequency_design",
    })?;
    if !best_objective.is_finite() {
        return Err(PidDesignError::OptimizationFailed {
            which: "frequency_design",
        });
    }

    let spec = best_problem.linear_pid_spec();
    let pid = build_runtime_pid_from_spec(spec, params.anti_windup, params.integrator_limits)?;
    let loop_value = open_loop_at_frequency(plant, &spec, params.crossover_frequency)?;
    let achieved_phase_margin_deg = phase_margin_deg(loop_value);
    Ok(FrequencyPidDesign {
        pid,
        params,
        loop_value,
        achieved_phase_margin_deg,
    })
}

fn design_step_optimized_pid(
    plant: &DiscreteTransferFunction<f64>,
    params: StepOptimizationPidParams<f64>,
) -> Result<StepOptimizationPidDesign<f64>, PidDesignError> {
    let floor = positive_floor(params.lambda);
    let dc_gain = plant.evaluate(Complex::new(1.0, 0.0));
    let kp_seed_mag = (1.0 / dc_gain.abs().max(1.0e-6)).max(floor);
    let ti_seed = params.lambda.max(floor);
    let predicted_sign = if dc_gain.re.abs() > 1.0e-9 {
        dc_gain.re.signum()
    } else {
        1.0
    };

    let kp_scales = [0.5, 1.0, 2.0];
    let ti_scales = [0.5, 1.0, 2.0];
    let signs = [predicted_sign, -predicted_sign];
    let mut best_seed: Option<(DiscreteStepPidLmProblem<'_>, f64)> = None;

    for &sign in &signs {
        for &kp_scale in &kp_scales {
            for &ti_scale in &ti_scales {
                let seed = Vector2::new((kp_seed_mag * kp_scale).ln(), (ti_seed * ti_scale).ln());
                let problem = DiscreteStepPidLmProblem {
                    plant,
                    params,
                    sign,
                    floor,
                    variables: seed,
                };
                let objective = step_objective(&problem.residual_vector());
                if objective.is_finite()
                    && best_seed
                        .as_ref()
                        .is_none_or(|(_, best_objective)| objective < *best_objective)
                {
                    best_seed = Some((problem, objective));
                }
            }
        }
    }

    let (seed_problem, seed_objective) = best_seed.ok_or(PidDesignError::OptimizationFailed {
        which: "step_optimization",
    })?;
    if !seed_objective.is_finite() {
        return Err(PidDesignError::OptimizationFailed {
            which: "step_optimization",
        });
    }

    let (refined_problem, _report) = LevenbergMarquardt::new()
        .with_patience(8)
        .minimize(seed_problem);
    let refined_objective = step_objective(&refined_problem.residual_vector());
    let best_problem = if refined_objective.is_finite() && refined_objective <= seed_objective {
        refined_problem
    } else {
        seed_problem
    };

    let spec = best_problem.linear_pid_spec();
    let pid = build_runtime_pid_from_spec(spec, params.anti_windup, params.integrator_limits)?;
    let (tracking_cost, control_cost) = discrete_closed_loop_costs(plant, &spec, params)?;
    Ok(StepOptimizationPidDesign {
        pid,
        params,
        tracking_cost,
        control_cost,
    })
}

fn open_loop_at_frequency(
    plant: FrequencyPlant<'_>,
    controller: &LinearPidSpec,
    omega: f64,
) -> Result<Complex<f64>, PidDesignError> {
    let controller = build_linear_pid_core(*controller)?;
    let controller_value = match plant {
        FrequencyPlant::Continuous(_) => controller
            .to_transfer_function_continuous()?
            .evaluate(Complex::new(0.0, omega)),
        FrequencyPlant::Discrete(plant) => controller
            .to_transfer_function_discrete(plant.sample_time())?
            .evaluate(Complex::new(
                (omega * plant.sample_time()).cos(),
                (omega * plant.sample_time()).sin(),
            )),
    };
    Ok(controller_value * plant.evaluate_at_omega(omega))
}

fn build_linear_pid_spec_from_variables(
    kp_sign: f64,
    variables: Vector2<f64>,
    controller: PidControllerKind,
    derivative_filter_ratio: f64,
    ti_over_td_ratio: f64,
    floor: f64,
) -> LinearPidSpec {
    let kp = kp_sign * variables[0].exp().max(floor);
    let ti = variables[1].exp().max(floor);
    let ki = kp / ti;
    match controller {
        PidControllerKind::Pi => LinearPidSpec {
            kp,
            ki,
            kd: 0.0,
            derivative_filter: None,
        },
        PidControllerKind::Pid => {
            let td = (ti / ti_over_td_ratio).max(floor);
            LinearPidSpec {
                kp,
                ki,
                kd: kp * td,
                derivative_filter: Some(derivative_filter_ratio / td),
            }
        }
    }
}

fn build_linear_pid_core(spec: LinearPidSpec) -> Result<Pid<f64>, PidDesignError> {
    Ok(Pid::new(
        spec.kp,
        spec.ki,
        spec.kd,
        spec.derivative_filter,
        AntiWindup::None,
    )?)
}

fn build_runtime_pid_from_spec(
    spec: LinearPidSpec,
    anti_windup: AntiWindup<f64>,
    integrator_limits: Option<(f64, f64)>,
) -> Result<Pid<f64>, PidDesignError> {
    let mut pid = Pid::new(
        spec.kp,
        spec.ki,
        spec.kd,
        spec.derivative_filter,
        anti_windup,
    )?;
    if let Some((low, high)) = integrator_limits {
        pid = pid.with_integrator_limits(low, high)?;
    }
    Ok(pid)
}

fn frequency_objective(residuals: &OVector<f64, Dyn>) -> f64 {
    residuals.iter().map(|value| value * value).sum()
}

fn step_objective(residuals: &OVector<f64, Dyn>) -> f64 {
    residuals.iter().map(|value| value * value).sum()
}

fn wrap_to_pi(angle: f64) -> f64 {
    let two_pi = core::f64::consts::TAU;
    let shifted = (angle + core::f64::consts::PI) / two_pi;
    let wrapped = angle + core::f64::consts::PI - two_pi * shifted.floor() - core::f64::consts::PI;
    if wrapped == -core::f64::consts::PI {
        core::f64::consts::PI
    } else {
        wrapped
    }
}

fn phase_margin_deg(loop_value: Complex<f64>) -> f64 {
    let mut margin = (core::f64::consts::PI + loop_value.im.atan2(loop_value.re)).to_degrees();
    if margin < 0.0 {
        margin += 360.0;
    }
    margin
}

fn reference_step_sample(k: usize, sample_time: f64, lambda: f64) -> f64 {
    1.0 - (-(k as f64 * sample_time) / lambda).exp()
}

fn discrete_closed_loop_costs(
    plant: &DiscreteTransferFunction<f64>,
    spec: &LinearPidSpec,
    params: StepOptimizationPidParams<f64>,
) -> Result<(f64, f64), PidDesignError> {
    let controller = build_linear_pid_core(*spec)?;
    let controller_tf = controller.to_transfer_function_discrete(plant.sample_time())?;
    let open_loop = controller_tf.mul(plant)?;
    let closed_output = open_loop.unity_feedback()?;
    let closed_control = controller_tf.feedback(plant)?;
    let closed_output_ss = closed_output.to_state_space()?;
    if !closed_output_ss.is_asymptotically_stable()? {
        return Err(PidDesignError::OptimizationFailed {
            which: "unstable_closed_loop",
        });
    }
    let closed_control_ss = closed_control.to_state_space()?;
    let output_response = closed_output_ss.step_response(params.horizon_steps);
    let control_response = closed_control_ss.step_response(params.horizon_steps);

    let mut tracking_cost = 0.0;
    let mut control_cost = 0.0;
    let control_weight = params.control_effort_weight.sqrt();
    for k in 0..params.horizon_steps {
        let y = output_response.values[k][(0, 0)];
        let u = control_response.values[k][(0, 0)];
        let y_ref = reference_step_sample(k, plant.sample_time(), params.lambda);
        let track_residual = y - y_ref;
        tracking_cost += track_residual * track_residual;
        let control_residual = control_weight * u;
        control_cost += control_residual * control_residual;
    }
    Ok((tracking_cost, control_cost))
}

#[derive(Clone, Copy)]
struct FrequencyPidLmProblem<'a> {
    plant: FrequencyPlant<'a>,
    params: FrequencyPidParams<f64>,
    sign: f64,
    floor: f64,
    variables: Vector2<f64>,
}

impl FrequencyPidLmProblem<'_> {
    fn linear_pid_spec(&self) -> LinearPidSpec {
        build_linear_pid_spec_from_variables(
            self.sign,
            self.variables,
            self.params.controller,
            self.params.derivative_filter_ratio,
            self.params.ti_over_td_ratio,
            self.floor,
        )
    }

    fn residual_vector(&self) -> OVector<f64, Dyn> {
        let spec = self.linear_pid_spec();
        let residuals =
            match open_loop_at_frequency(self.plant, &spec, self.params.crossover_frequency) {
                Ok(loop_value) => {
                    let desired_phase =
                        -core::f64::consts::PI + self.params.phase_margin_deg.to_radians();
                    vec![
                        loop_value.abs().ln(),
                        wrap_to_pi(loop_value.im.atan2(loop_value.re) - desired_phase),
                    ]
                }
                Err(_) => vec![1.0e6, 1.0e6],
            };
        OVector::<f64, Dyn>::from_vec(residuals)
    }
}

impl LeastSquaresProblem<f64, Dyn, U2> for FrequencyPidLmProblem<'_> {
    type ParameterStorage = Owned<f64, U2>;
    type ResidualStorage = VecStorage<f64, Dyn, U1>;
    type JacobianStorage = Owned<f64, Dyn, U2>;

    fn set_params(&mut self, x: &Vector2<f64>) {
        self.variables = *x;
    }

    fn params(&self) -> Vector2<f64> {
        self.variables
    }

    fn residuals(&self) -> Option<OVector<f64, Dyn>> {
        Some(self.residual_vector())
    }

    fn jacobian(&self) -> Option<OMatrix<f64, Dyn, U2>> {
        let mut clone = *self;
        differentiate_numerically(&mut clone)
    }
}

#[derive(Clone, Copy)]
struct DiscreteStepPidLmProblem<'a> {
    plant: &'a DiscreteTransferFunction<f64>,
    params: StepOptimizationPidParams<f64>,
    sign: f64,
    floor: f64,
    variables: Vector2<f64>,
}

impl DiscreteStepPidLmProblem<'_> {
    fn linear_pid_spec(&self) -> LinearPidSpec {
        build_linear_pid_spec_from_variables(
            self.sign,
            self.variables,
            self.params.controller,
            self.params.derivative_filter_ratio,
            self.params.ti_over_td_ratio,
            self.floor,
        )
    }

    fn residual_vector(&self) -> OVector<f64, Dyn> {
        let spec = self.linear_pid_spec();
        let controller = match build_linear_pid_core(spec) {
            Ok(controller) => controller,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let controller_tf = match controller.to_transfer_function_discrete(self.plant.sample_time())
        {
            Ok(tf) => tf,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let open_loop = match controller_tf.mul(self.plant) {
            Ok(tf) => tf,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let closed_output = match open_loop.unity_feedback() {
            Ok(tf) => tf,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let closed_control = match controller_tf.feedback(self.plant) {
            Ok(tf) => tf,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let closed_output_ss = match closed_output.to_state_space() {
            Ok(ss) => ss,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let stable = closed_output_ss.is_asymptotically_stable().unwrap_or(false);
        if !stable {
            return step_optimization_penalty_vector(self.params.horizon_steps);
        }
        let closed_control_ss = match closed_control.to_state_space() {
            Ok(ss) => ss,
            Err(_) => return step_optimization_penalty_vector(self.params.horizon_steps),
        };
        let output_response = closed_output_ss.step_response(self.params.horizon_steps);
        let control_response = closed_control_ss.step_response(self.params.horizon_steps);
        let mut residuals = Vec::with_capacity(
            self.params.horizon_steps
                + usize::from(self.params.control_effort_weight > 0.0) * self.params.horizon_steps,
        );
        for k in 0..self.params.horizon_steps {
            let y_ref = reference_step_sample(k, self.plant.sample_time(), self.params.lambda);
            residuals.push(output_response.values[k][(0, 0)] - y_ref);
        }
        if self.params.control_effort_weight > 0.0 {
            let control_weight = self.params.control_effort_weight.sqrt();
            for k in 0..self.params.horizon_steps {
                residuals.push(control_weight * control_response.values[k][(0, 0)]);
            }
        }
        OVector::<f64, Dyn>::from_vec(residuals)
    }
}

impl LeastSquaresProblem<f64, Dyn, U2> for DiscreteStepPidLmProblem<'_> {
    type ParameterStorage = Owned<f64, U2>;
    type ResidualStorage = VecStorage<f64, Dyn, U1>;
    type JacobianStorage = Owned<f64, Dyn, U2>;

    fn set_params(&mut self, x: &Vector2<f64>) {
        self.variables = *x;
    }

    fn params(&self) -> Vector2<f64> {
        self.variables
    }

    fn residuals(&self) -> Option<OVector<f64, Dyn>> {
        Some(self.residual_vector())
    }

    fn jacobian(&self) -> Option<OMatrix<f64, Dyn, U2>> {
        let mut clone = *self;
        differentiate_numerically(&mut clone)
    }
}

#[derive(Clone, Debug)]
struct PreparedStepResponse {
    /// Time grid shifted so the dominant input step occurs near `t = 0`.
    time_relative: Vec<f64>,
    /// Measured output samples on that shifted grid.
    output: Vec<f64>,
    /// Estimated pre-step output baseline.
    initial_output: f64,
    /// Estimated input step amplitude.
    step_amplitude: f64,
    /// Coarse steady-state process gain estimate from baseline/tail averages.
    gain_guess: f64,
    /// Characteristic experiment time span used to scale positivity floors.
    time_scale: f64,
}

impl PreparedStepResponse {
    /// Normalizes raw sampled step data into the quantities needed by the
    /// landmark estimators and LM refinement.
    ///
    /// The preprocessing intentionally looks for one dominant input jump and
    /// then computes pre-step and post-step averages around that detected
    /// transition. That keeps the first fitting pass simple and robust for the
    /// intended "single experiment, single step" workflow.
    fn from_data(data: &StepResponseData<f64>) -> Result<Self, PidDesignError> {
        let n = data.time.len();
        let step_index =
            dominant_step_index(&data.input).ok_or(PidDesignError::InvalidStepAmplitude)?;
        let pre_end = (step_index + 1).max(1);
        let post_start = (step_index + 1).min(n - 1);
        let tail = tail_count(n - post_start);

        let initial_input = mean(&data.input[..pre_end]);
        let final_input = mean(&data.input[post_start..]);
        let initial_output = mean(&data.output[..pre_end]);
        let final_output = mean(&data.output[n - tail..]);

        let step_amplitude = final_input - initial_input;
        if !step_amplitude.is_finite() || step_amplitude.abs() <= 1.0e-12 {
            return Err(PidDesignError::InvalidStepAmplitude);
        }
        let output_change = final_output - initial_output;
        if !output_change.is_finite() || output_change.abs() <= 1.0e-12 {
            return Err(PidDesignError::InvalidResponseAmplitude);
        }

        let step_time = interpolate_input_step_time(&data.time, &data.input, step_index)?;

        let time_relative = data.time.iter().map(|&t| t - step_time).collect::<Vec<_>>();
        let time_scale = (data.time[n - 1] - data.time[0]).abs().max(1.0e-6);
        Ok(Self {
            time_relative,
            output: data.output.clone(),
            initial_output,
            step_amplitude,
            gain_guess: output_change / step_amplitude,
            time_scale,
        })
    }
}

/// Forms the first FOPDT estimate from standard normalized step landmarks.
///
/// The `28.3%` and `63.2%` crossings give a coarse delay/lag split that is
/// easy to compute and usually good enough to seed local nonlinear refinement.
fn initial_fopdt_estimate(data: &PreparedStepResponse) -> Result<FopdtModel<f64>, PidDesignError> {
    let final_output = *data.output.last().ok_or(PidDesignError::InvalidData {
        which: "output.last",
    })?;
    let total_change = final_output - data.initial_output;
    let gain = data.gain_guess;
    let sign = total_change.signum();
    let level_283 = data.initial_output + 0.283 * total_change;
    let level_632 = data.initial_output + 0.632 * total_change;

    let start_index = first_nonnegative_index(&data.time_relative);
    let t_283 = interpolate_crossing(
        &data.time_relative,
        &data.output,
        level_283,
        sign,
        start_index,
    )
    .ok_or(PidDesignError::InvalidDelayEstimate)?;
    let t_632 = interpolate_crossing(
        &data.time_relative,
        &data.output,
        level_632,
        sign,
        start_index,
    )
    .ok_or(PidDesignError::InvalidDelayEstimate)?;

    let tau = 1.5 * (t_632 - t_283);
    let delay = (t_632 - tau).max(0.0);
    if !tau.is_finite() || tau <= 0.0 || !delay.is_finite() {
        return Err(PidDesignError::InvalidProcessFit);
    }

    Ok(FopdtModel {
        gain,
        time_constant: tau,
        delay,
    })
}

/// Builds a physically plausible SOPDT seed from the coarser FOPDT estimate.
///
/// This does not try to solve the second-order fit in closed form. Instead it
/// searches a small deterministic grid of lag splits and delay scalings and
/// keeps the candidate with the smallest direct step-response residual.
fn initial_sopdt_estimate(data: &PreparedStepResponse, fopdt: FopdtModel<f64>) -> SopdtModel<f64> {
    let floor = positive_floor(data.time_scale);
    let total_scales = [0.5, 0.75, 1.0, 1.25, 1.5];
    let split_fractions = [0.55, 0.65, 0.75, 0.85, 0.95];
    let delay_scales = [0.5, 1.0, 1.5];

    let mut best = SopdtModel {
        gain: fopdt.gain,
        time_constant_1: (0.75 * fopdt.time_constant).max(floor),
        time_constant_2: (0.25 * fopdt.time_constant).max(floor),
        delay: fopdt.delay.max(0.0),
    };
    let mut best_objective = sopdt_objective(data, best);

    for &total_scale in &total_scales {
        let total = (fopdt.time_constant * total_scale).max(2.0 * floor);
        for &split in &split_fractions {
            let tau1 = (total * split).max(floor);
            let tau2 = (total * (1.0 - split)).max(floor);
            for &delay_scale in &delay_scales {
                let candidate = SopdtModel {
                    gain: fopdt.gain,
                    time_constant_1: tau1.max(tau2),
                    time_constant_2: tau1.min(tau2),
                    delay: (fopdt.delay * delay_scale).max(0.0),
                };
                let objective = sopdt_objective(data, candidate);
                if objective < best_objective {
                    best = candidate;
                    best_objective = objective;
                }
            }
        }
    }

    best
}

/// Runs LM refinement for the FOPDT parameters.
///
/// The time constant and delay are optimized in log coordinates so the
/// recovered model stays positive without adding explicit inequality
/// constraints to the least-squares problem.
fn refine_fopdt(
    data: &PreparedStepResponse,
    initial: FopdtModel<f64>,
    options: ProcessModelFitOptions,
) -> Result<FopdtModel<f64>, PidDesignError> {
    let floor = positive_floor(data.time_scale);
    let problem = FopdtLmProblem {
        data,
        params: Vector3::new(
            initial.gain,
            initial.time_constant.max(floor).ln(),
            initial.delay.max(floor).ln(),
        ),
        floor,
    };
    let (problem, report) = process_fit_solver(options)?.minimize(problem);
    let model = problem.model();
    if !report.termination.was_successful()
        || !model.gain.is_finite()
        || !model.time_constant.is_finite()
        || !model.delay.is_finite()
        || model.time_constant <= 0.0
        || model.delay < 0.0
    {
        return Err(PidDesignError::InvalidProcessFit);
    }
    Ok(model)
}

/// Runs LM refinement for the SOPDT parameters.
///
/// The parameterization uses:
///
/// - one base lag
/// - one positive lag separation
/// - one positive delay
///
/// so both time constants remain positive and ordered automatically.
fn refine_sopdt(
    data: &PreparedStepResponse,
    initial: SopdtModel<f64>,
    options: ProcessModelFitOptions,
) -> Result<SopdtModel<f64>, PidDesignError> {
    let floor = positive_floor(data.time_scale);
    let base = initial.time_constant_2.max(floor);
    let extra = (initial.time_constant_1 - initial.time_constant_2)
        .abs()
        .max(floor);
    let problem = SopdtLmProblem {
        data,
        params: Vector4::new(
            initial.gain,
            base.ln(),
            extra.ln(),
            initial.delay.max(floor).ln(),
        ),
        floor,
    };
    let (problem, report) = process_fit_solver(options)?.minimize(problem);
    let model = problem.model();
    if !report.termination.was_successful()
        || !model.gain.is_finite()
        || !model.time_constant_1.is_finite()
        || !model.time_constant_2.is_finite()
        || !model.delay.is_finite()
        || model.time_constant_1 <= 0.0
        || model.time_constant_2 <= 0.0
        || model.delay < 0.0
    {
        return Err(PidDesignError::InvalidProcessFit);
    }
    Ok(model)
}

fn process_fit_solver(
    options: ProcessModelFitOptions,
) -> Result<LevenbergMarquardt<f64>, PidDesignError> {
    let mut solver = LevenbergMarquardt::new();
    if let Some(tolerance) = options.tolerance {
        if !tolerance.is_finite() || tolerance <= 0.0 {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "process_fit_tolerance",
            });
        }
        solver = solver.with_tol(tolerance);
    }
    if let Some(patience) = options.patience {
        if patience == 0 {
            return Err(PidDesignError::InvalidTuningParameter {
                which: "process_fit_patience",
            });
        }
        solver = solver.with_patience(patience);
    }
    Ok(solver)
}

/// Sum-of-squares objective used to compare FOPDT candidates.
fn fopdt_objective(data: &PreparedStepResponse, model: FopdtModel<f64>) -> f64 {
    data.time_relative
        .iter()
        .zip(data.output.iter())
        .map(|(&t, &y)| {
            let residual =
                model.step_response_value(t, data.step_amplitude, data.initial_output) - y;
            residual * residual
        })
        .sum()
}

/// Sum-of-squares objective used to compare SOPDT candidates.
fn sopdt_objective(data: &PreparedStepResponse, model: SopdtModel<f64>) -> f64 {
    data.time_relative
        .iter()
        .zip(data.output.iter())
        .map(|(&t, &y)| {
            let residual =
                model.step_response_value(t, data.step_amplitude, data.initial_output) - y;
            residual * residual
        })
        .sum()
}

#[derive(Clone)]
struct FopdtLmProblem<'a> {
    data: &'a PreparedStepResponse,
    /// Parameters stored as `[gain, ln(tau), ln(delay)]`.
    params: Vector3<f64>,
    floor: f64,
}

impl<'a> FopdtLmProblem<'a> {
    /// Maps the LM parameter vector back to a physical FOPDT model.
    fn model(&self) -> FopdtModel<f64> {
        FopdtModel {
            gain: self.params[0],
            time_constant: self.params[1].exp().max(self.floor),
            delay: self.params[2].exp().max(self.floor),
        }
    }

    /// Residual vector for the sampled delayed first-order step model.
    fn residual_vector(&self) -> OVector<f64, Dyn> {
        let model = self.model();
        let residuals = self
            .data
            .time_relative
            .iter()
            .zip(self.data.output.iter())
            .map(|(&t, &y)| {
                model.step_response_value(t, self.data.step_amplitude, self.data.initial_output) - y
            })
            .collect::<Vec<_>>();
        OVector::<f64, Dyn>::from_vec(residuals)
    }
}

impl<'a> LeastSquaresProblem<f64, Dyn, U3> for FopdtLmProblem<'a> {
    type ParameterStorage = Owned<f64, U3>;
    type ResidualStorage = VecStorage<f64, Dyn, U1>;
    type JacobianStorage = Owned<f64, Dyn, U3>;

    fn set_params(&mut self, x: &Vector3<f64>) {
        self.params = *x;
    }

    fn params(&self) -> Vector3<f64> {
        self.params
    }

    fn residuals(&self) -> Option<OVector<f64, Dyn>> {
        Some(self.residual_vector())
    }

    fn jacobian(&self) -> Option<OMatrix<f64, Dyn, U3>> {
        let raw_tau = self.params[1].exp();
        let raw_delay = self.params[2].exp();
        let d_tau_d_log_tau = if raw_tau > self.floor { raw_tau } else { 0.0 };
        let d_delay_d_log_delay = if raw_delay > self.floor {
            raw_delay
        } else {
            0.0
        };
        let model = self.model();
        let mut jacobian =
            OMatrix::<f64, Dyn, U3>::zeros_generic(Dyn(self.data.time_relative.len()), U3);
        for (row, &time_since_step) in self.data.time_relative.iter().enumerate() {
            let partials =
                model.step_response_jacobian_value(time_since_step, self.data.step_amplitude);
            jacobian[(row, 0)] = partials.gain;
            jacobian[(row, 1)] = partials.time_constant * d_tau_d_log_tau;
            jacobian[(row, 2)] = partials.delay * d_delay_d_log_delay;
        }
        Some(jacobian)
    }
}

#[derive(Clone)]
struct SopdtLmProblem<'a> {
    data: &'a PreparedStepResponse,
    /// Parameters stored as `[gain, ln(base_tau), ln(extra_tau), ln(delay)]`.
    params: Vector4<f64>,
    floor: f64,
}

impl<'a> SopdtLmProblem<'a> {
    /// Maps the LM parameter vector back to a physical SOPDT model.
    fn model(&self) -> SopdtModel<f64> {
        let base = self.params[1].exp().max(self.floor);
        let extra = self.params[2].exp().max(self.floor);
        SopdtModel {
            gain: self.params[0],
            time_constant_1: base + extra,
            time_constant_2: base,
            delay: self.params[3].exp().max(self.floor),
        }
    }

    /// Residual vector for the sampled delayed second-order step model.
    fn residual_vector(&self) -> OVector<f64, Dyn> {
        let model = self.model();
        let residuals = self
            .data
            .time_relative
            .iter()
            .zip(self.data.output.iter())
            .map(|(&t, &y)| {
                model.step_response_value(t, self.data.step_amplitude, self.data.initial_output) - y
            })
            .collect::<Vec<_>>();
        OVector::<f64, Dyn>::from_vec(residuals)
    }
}

impl<'a> LeastSquaresProblem<f64, Dyn, U4> for SopdtLmProblem<'a> {
    type ParameterStorage = Owned<f64, U4>;
    type ResidualStorage = VecStorage<f64, Dyn, U1>;
    type JacobianStorage = Owned<f64, Dyn, U4>;

    fn set_params(&mut self, x: &Vector4<f64>) {
        self.params = *x;
    }

    fn params(&self) -> Vector4<f64> {
        self.params
    }

    fn residuals(&self) -> Option<OVector<f64, Dyn>> {
        Some(self.residual_vector())
    }

    fn jacobian(&self) -> Option<OMatrix<f64, Dyn, U4>> {
        let raw_base = self.params[1].exp();
        let raw_extra = self.params[2].exp();
        let raw_delay = self.params[3].exp();
        let d_base_d_log_base = if raw_base > self.floor { raw_base } else { 0.0 };
        let d_extra_d_log_extra = if raw_extra > self.floor {
            raw_extra
        } else {
            0.0
        };
        let d_delay_d_log_delay = if raw_delay > self.floor {
            raw_delay
        } else {
            0.0
        };
        let model = self.model();
        let mut jacobian =
            OMatrix::<f64, Dyn, U4>::zeros_generic(Dyn(self.data.time_relative.len()), U4);
        for (row, &time_since_step) in self.data.time_relative.iter().enumerate() {
            let partials =
                model.step_response_jacobian_value(time_since_step, self.data.step_amplitude);
            jacobian[(row, 0)] = partials.gain;
            jacobian[(row, 1)] =
                (partials.time_constant_1 + partials.time_constant_2) * d_base_d_log_base;
            jacobian[(row, 2)] = partials.time_constant_1 * d_extra_d_log_extra;
            jacobian[(row, 3)] = partials.delay * d_delay_d_log_delay;
        }
        Some(jacobian)
    }
}

/// Finds the first crossing of `target` after `start_index` with the expected
/// response direction.
fn interpolate_crossing<R>(
    time: &[R],
    signal: &[R],
    target: R,
    direction: R,
    start_index: usize,
) -> Option<R>
where
    R: Float,
{
    if direction == R::zero() {
        return None;
    }
    for idx in start_index..signal.len().saturating_sub(1) {
        let lhs = signal[idx];
        let rhs = signal[idx + 1];
        let crossed = if direction > R::zero() {
            lhs <= target && rhs >= target
        } else {
            lhs >= target && rhs <= target
        };
        if crossed {
            let dy = rhs - lhs;
            if dy == R::zero() {
                return Some(time[idx]);
            }
            let alpha = (target - lhs) / dy;
            return Some(time[idx] + alpha * (time[idx + 1] - time[idx]));
        }
    }
    None
}

/// Returns the first index whose shifted time is nonnegative.
fn first_nonnegative_index(values: &[f64]) -> usize {
    values.iter().position(|&value| value >= 0.0).unwrap_or(0)
}

/// Finds the dominant single input jump in the sampled command record.
///
/// The current fitting path assumes one main step experiment, so the largest
/// adjacent jump is treated as the step used for identification.
fn dominant_step_index(signal: &[f64]) -> Option<usize> {
    let mut best_index = None;
    let mut best_jump = 0.0f64;
    for idx in 0..signal.len().saturating_sub(1) {
        let jump = (signal[idx + 1] - signal[idx]).abs();
        if jump > best_jump {
            best_jump = jump;
            best_index = Some(idx);
        }
    }
    if best_jump > 1.0e-12 {
        best_index
    } else {
        None
    }
}

/// Interpolates the detected input step to a sub-sample transition time.
fn interpolate_input_step_time(
    time: &[f64],
    input: &[f64],
    step_index: usize,
) -> Result<f64, PidDesignError> {
    let lhs_u = input[step_index];
    let rhs_u = input[step_index + 1];
    let lhs_t = time[step_index];
    let rhs_t = time[step_index + 1];
    let du = rhs_u - lhs_u;
    if du == 0.0 {
        return Err(PidDesignError::InvalidData {
            which: "input_step_time",
        });
    }
    let target = lhs_u + 0.5 * du;
    let alpha = (target - lhs_u) / du;
    Ok(lhs_t + alpha * (rhs_t - lhs_t))
}

/// Large residual vector returned when step-optimization tuning hits an invalid design.
fn step_optimization_penalty_vector(horizon_steps: usize) -> OVector<f64, Dyn> {
    OVector::<f64, Dyn>::from_vec(vec![1.0e6; horizon_steps])
}

/// Heuristic tail length used for steady-state averaging.
fn tail_count(n: usize) -> usize {
    (n / 10).max(3).min(n.max(1))
}

/// Arithmetic mean helper for baseline and tail estimates.
fn mean(values: &[f64]) -> f64 {
    values.iter().sum::<f64>() / values.len() as f64
}

/// Positive floor derived from the experiment time scale.
///
/// This prevents zero or negative lag/delay values in the transformed LM
/// parameters while staying small relative to the sampled experiment.
fn positive_floor(scale: f64) -> f64 {
    (1.0e-9 * scale).max(1.0e-9)
}

/// Rejects zero or nonfinite process gains before applying a tuning rule.
fn validate_process_gain<R>(gain: R) -> Result<(), PidDesignError>
where
    R: Float,
{
    if gain.is_finite() && gain != R::zero() {
        Ok(())
    } else {
        Err(PidDesignError::InvalidProcessFit)
    }
}

/// Validates a strictly positive tuning or process parameter.
fn validate_positive<R>(value: R, which: &'static str) -> Result<(), PidDesignError>
where
    R: Float,
{
    if value.is_finite() && value > R::zero() {
        Ok(())
    } else {
        Err(PidDesignError::InvalidTuningParameter { which })
    }
}

/// Validates a nonnegative tuning or process parameter.
fn validate_nonnegative<R>(value: R, which: &'static str) -> Result<(), PidDesignError>
where
    R: Float,
{
    if value.is_finite() && value >= R::zero() {
        Ok(())
    } else {
        Err(PidDesignError::InvalidTuningParameter { which })
    }
}

#[cfg(test)]
mod tests {
    use super::{
        AntiWindup, FopdtModel, FrequencyPidParams, PidControllerKind, PidDesignError,
        SampledIoData, SimcPidParams, SopdtModel, StepOptimizationPidParams, StepResponseData,
        design_pid_from_continuous_state_space_frequency, design_pid_from_continuous_tf_frequency,
        design_pid_from_discrete_tf_step_optimization, design_pid_from_fopdt,
        design_pid_from_okid_era, design_pid_from_sopdt, design_pid_from_step_response_fopdt,
        fit_fopdt_from_step_response, fit_sopdt_from_step_response,
    };
    use crate::control::identification::{EraParams, OkidParams};
    use crate::control::lti::{ContinuousTransferFunction, DiscreteTransferFunction};
    use alloc::vec::Vec;
    use faer::Mat;
    use nalgebra::ComplexField;

    fn assert_close(lhs: f64, rhs: f64, tol: f64) {
        let err = (lhs - rhs).abs();
        assert!(err <= tol, "lhs={lhs}, rhs={rhs}, err={err}, tol={tol}");
    }

    fn fopdt_step_data(model: FopdtModel<f64>, dt: f64, duration: f64) -> StepResponseData<f64> {
        let n = (duration / dt).round() as usize + 1;
        let step_time = 5.0 * dt;
        let mut time = Vec::with_capacity(n);
        let mut input = Vec::with_capacity(n);
        let mut output = Vec::with_capacity(n);
        for i in 0..n {
            let t = i as f64 * dt;
            time.push(t);
            let u = if t >= step_time { 1.0 } else { 0.0 };
            input.push(u);
            output.push(model.step_response_value((t - step_time).max(0.0), 1.0, 0.0));
        }
        StepResponseData::new(time, input, output).unwrap()
    }

    fn sopdt_step_data(model: SopdtModel<f64>, dt: f64, duration: f64) -> StepResponseData<f64> {
        let n = (duration / dt).round() as usize + 1;
        let step_time = 5.0 * dt;
        let mut time = Vec::with_capacity(n);
        let mut input = Vec::with_capacity(n);
        let mut output = Vec::with_capacity(n);
        for i in 0..n {
            let t = i as f64 * dt;
            time.push(t);
            let u = if t >= step_time { 1.0 } else { 0.0 };
            input.push(u);
            output.push(model.step_response_value((t - step_time).max(0.0), 1.0, 0.0));
        }
        StepResponseData::new(time, input, output).unwrap()
    }

    #[test]
    fn step_response_data_rejects_invalid_inputs() {
        let err =
            StepResponseData::new(vec![0.0, 0.0], vec![0.0, 1.0], vec![0.0, 1.0]).unwrap_err();
        assert!(matches!(err, PidDesignError::InvalidData { .. }));
    }

    #[test]
    fn fit_fopdt_recovers_synthetic_step_response() {
        let model = FopdtModel {
            gain: 2.0,
            time_constant: 3.0,
            delay: 0.7,
        };
        let data = fopdt_step_data(model, 0.05, 20.0);
        let fit = fit_fopdt_from_step_response(&data).unwrap();

        assert_close(fit.model.gain, model.gain, 1.0e-2);
        assert_close(fit.model.time_constant, model.time_constant, 5.0e-2);
        assert_close(fit.model.delay, model.delay, 5.0e-2);
        assert!(fit.objective <= 1.0e-6);
    }

    #[test]
    fn fit_sopdt_recovers_synthetic_step_response() {
        let model = SopdtModel {
            gain: 1.5,
            time_constant_1: 2.5,
            time_constant_2: 0.8,
            delay: 0.4,
        };
        let data = sopdt_step_data(model, 0.05, 20.0);
        let fit = fit_sopdt_from_step_response(&data).unwrap();

        assert_close(fit.model.gain, model.gain, 1.0e-2);
        assert_close(fit.model.time_constant_1, model.time_constant_1, 7.5e-2);
        assert_close(fit.model.time_constant_2, model.time_constant_2, 7.5e-2);
        assert_close(fit.model.delay, model.delay, 5.0e-2);
        assert!(fit.objective <= 1.0e-6);
    }

    #[test]
    fn simc_fopdt_pi_matches_expected_formula() {
        let model = FopdtModel {
            gain: 2.0,
            time_constant: 4.0,
            delay: 1.0,
        };
        let params =
            SimcPidParams::new(2.0, PidControllerKind::Pi, 10.0, AntiWindup::None).unwrap();
        let design = design_pid_from_fopdt(model, params).unwrap();
        assert_close(design.pid.kp(), 4.0 / (2.0 * (2.0 + 1.0)), 1.0e-12);
        assert_close(design.pid.ki(), design.pid.kp() / 4.0, 1.0e-12);
        assert_close(design.pid.kd(), 0.0, 1.0e-12);
    }

    #[test]
    fn simc_sopdt_pid_populates_derivative_filter() {
        let model = SopdtModel {
            gain: 1.0,
            time_constant_1: 3.0,
            time_constant_2: 1.0,
            delay: 0.5,
        };
        let params =
            SimcPidParams::new(1.5, PidControllerKind::Pid, 8.0, AntiWindup::None).unwrap();
        let design = design_pid_from_sopdt(model, params).unwrap();
        assert!(design.pid.kd() > 0.0);
        assert!(design.pid.derivative_filter().unwrap() > 0.0);
    }

    #[test]
    fn step_fit_design_builds_pid_from_fopdt_fit() {
        let model = FopdtModel {
            gain: 1.2,
            time_constant: 2.0,
            delay: 0.3,
        };
        let data = fopdt_step_data(model, 0.05, 12.0);
        let params =
            SimcPidParams::new(1.0, PidControllerKind::Pid, 10.0, AntiWindup::None).unwrap();
        let design = design_pid_from_step_response_fopdt(&data, params).unwrap();
        assert_close(design.fit.model.gain, model.gain, 1.0e-3);
        assert!(design.design.pid.kp().is_finite());
    }

    #[test]
    fn fit_rejects_zero_step_amplitude() {
        let data = StepResponseData::new(
            vec![0.0, 1.0, 2.0, 3.0],
            vec![1.0, 1.0, 1.0, 1.0],
            vec![0.0, 0.0, 0.0, 0.0],
        )
        .unwrap();
        let err = fit_fopdt_from_step_response(&data).unwrap_err();
        assert!(matches!(err, PidDesignError::InvalidStepAmplitude));
    }

    #[test]
    fn continuous_frequency_design_hits_target() {
        let plant = ContinuousTransferFunction::continuous(vec![1.0], vec![1.0, 1.0]).unwrap();
        let params = FrequencyPidParams::new(
            1.0,
            60.0,
            PidControllerKind::Pi,
            10.0,
            8.0,
            AntiWindup::None,
        )
        .unwrap();
        let design = design_pid_from_continuous_tf_frequency(&plant, params).unwrap();
        assert_close(design.loop_value.abs(), 1.0, 1.0e-8);
        assert_close(design.achieved_phase_margin_deg, 60.0, 1.0e-6);

        let plant_ss = plant.to_state_space().unwrap();
        let from_ss = design_pid_from_continuous_state_space_frequency(&plant_ss, params).unwrap();
        assert_close(from_ss.pid.kp(), design.pid.kp(), 1.0e-8);
        assert_close(from_ss.pid.ki(), design.pid.ki(), 1.0e-8);
    }

    #[test]
    fn discrete_step_optimization_returns_stabilizing_controller() {
        let plant = DiscreteTransferFunction::discrete(vec![0.2], vec![1.0, -0.8], 0.1).unwrap();
        let params = StepOptimizationPidParams::new(
            0.5,
            20,
            PidControllerKind::Pi,
            10.0,
            8.0,
            AntiWindup::None,
        )
        .unwrap();

        let design = design_pid_from_discrete_tf_step_optimization(&plant, params).unwrap();
        assert!(design.tracking_cost.is_finite());
        assert!(design.control_cost.is_finite());

        let controller_tf = design
            .pid
            .to_transfer_function_discrete(plant.sample_time())
            .unwrap();
        let closed_loop = controller_tf
            .mul(&plant)
            .unwrap()
            .unity_feedback()
            .unwrap()
            .to_state_space()
            .unwrap();
        assert!(closed_loop.is_asymptotically_stable().unwrap());
    }

    #[test]
    fn okid_era_pid_design_builds_controller_from_sampled_data() {
        let plant = DiscreteTransferFunction::discrete(vec![0.15], vec![1.0, -0.85], 0.1).unwrap();
        let plant_ss = plant.to_state_space().unwrap();
        let nsamples = 80usize;
        let input = (0..nsamples)
            .map(|k| if (k / 4) % 2 == 0 { 1.0 } else { -0.5 })
            .collect::<Vec<_>>();
        let inputs = Mat::from_fn(1, nsamples, |_, col| input[col]);
        let sim = plant_ss.simulate(&[0.0], inputs.as_ref()).unwrap();
        let output = (0..nsamples)
            .map(|col| sim.outputs[(0, col)])
            .collect::<Vec<_>>();

        let data = SampledIoData::new(0.1, input, output).unwrap();
        let okid_params = OkidParams::new(24, 8);
        let era_params = EraParams::new(0.1).with_order(1);
        let pid_params = StepOptimizationPidParams::new(
            0.6,
            25,
            PidControllerKind::Pi,
            10.0,
            8.0,
            AntiWindup::None,
        )
        .unwrap();

        let design = design_pid_from_okid_era(&data, &okid_params, era_params, pid_params).unwrap();
        assert_eq!(design.identified_plant.sample_time(), 0.1);
        assert_eq!(design.retained_order, 1);
        assert!(design.design.pid.kp().is_finite());
        assert!(design.design.pid.ki().is_finite());

        let original_dc = plant_ss.dc_gain().unwrap()[(0, 0)].re;
        let identified_dc = design.identified_plant.dc_gain().unwrap()[(0, 0)].re;
        assert!((identified_dc - original_dc).abs() <= 0.1);
    }
}