Skip to main content

polyzonotope_reach/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![forbid(unsafe_code)]
4
5extern crate alloc;
6
7/// The set types this crate propagates, re-exported so callers need one dependency.
8pub use polyzonotope;
9
10mod dynamics;
11mod linear;
12mod nonlinear;
13mod taylor;
14
15pub use dynamics::{Dynamics, IntervalTensor, Polynomial};
16pub use linear::{LinearReach, LinearSystem, Step};
17pub use nonlinear::{Adaptive, Method, NonlinearReach, NonlinearSettings, Restructure, Splitting};
18
19/// Why a reachability computation could not produce a sound result.
20///
21/// Non-exhaustive: new ways to refuse may be added without a breaking
22/// release, so match with a wildcard arm.
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub enum ReachError {
26    /// The time step is negative, infinite or NaN; the remainder bound needs
27    /// a finite `r >= 0`.
28    InvalidStep,
29    /// The time step is too large for the number of Taylor terms: the
30    /// remainder bound of the matrix exponential needs `‖A‖∞ r < η + 2`.
31    StepTooLarge,
32    /// The abstraction error of a nonlinear step was not confirmed: it kept
33    /// outgrowing the assumed bound, or stopped being finite. The set or
34    /// the step is too large for the abstraction (linearization or
35    /// polynomialization); split the set or use a smaller step.
36    AbstractionDiverged,
37    /// [`Method::Polynomialization`] needs third derivatives for its
38    /// Lagrange remainder, and [`Dynamics::third_derivatives`] supplies none.
39    /// Implement it, or use [`Method::Linearization`].
40    MissingDerivatives,
41}
42
43impl core::fmt::Display for ReachError {
44    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45        f.write_str(match self {
46            Self::InvalidStep => "time step must be finite and non-negative",
47            Self::StepTooLarge => "time step too large for the Taylor remainder bound",
48            Self::AbstractionDiverged => "abstraction error of the step did not converge",
49            Self::MissingDerivatives => "polynomialization needs third derivatives of the dynamics",
50        })
51    }
52}
53
54impl core::error::Error for ReachError {}
55
56/// A multi-step computation that stopped early, with everything it proved
57/// before the failing step.
58///
59/// Returned by [`NonlinearReach::reach`]: the sets in `proven` are as sound
60/// as those of a complete run, they just end before the requested horizon.
61#[derive(Clone, Debug, PartialEq)]
62pub struct ReachFailure {
63    /// The completed steps, in order: entry `k` holds the pieces of step `k`,
64    /// exactly as a successful run would have returned them. A step that
65    /// failed for any of its pieces is left out entirely, so this is shorter
66    /// than the requested horizon and may be empty.
67    pub proven: alloc::vec::Vec<alloc::vec::Vec<Step>>,
68    /// Why the next step could not be proven.
69    pub error: ReachError,
70}
71
72impl core::fmt::Display for ReachFailure {
73    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
74        write!(f, "{} after {} proven steps", self.error, self.proven.len())
75    }
76}
77
78impl core::error::Error for ReachFailure {
79    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
80        Some(&self.error)
81    }
82}