te1d/simul.rs
1//! Provides common features for simulation of thermoelectric effects.
2
3use std::fmt;
4
5use crate::opt::{OptimizeErrorKind};
6
7
8/// A list specifying general categories of errors in simulation
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub enum SimulationErrorKind {
11 /// boundary condition matching failed
12 BcNotMatchError,
13 /// reach maximum iterations without finding a solution
14 OptimizerMaxIterError,
15}
16
17impl SimulationErrorKind {
18 pub(crate) fn as_str(&self) -> &'static str {
19 use SimulationErrorKind::*;
20 // Strictly alphabetical, please. (Sadly rustfmt cannot do this yet.)
21 match *self {
22 BcNotMatchError => "boundary condition not satisfied",
23 OptimizerMaxIterError => "optimum point not found until maximum iterations reached",
24 }
25 }
26}
27
28impl fmt::Display for SimulationErrorKind {
29 /// Shows a human-readable description of the [`SimulationErrorKind`].
30 ///
31 /// This is similar to `impl Display for Error`, but doesn't require first converting to Error.
32 ///
33 /// # Examples
34 /// ```
35 /// use te1d::simul::SimulationErrorKind;
36 /// assert_eq!("optimum point not found until maximum iterations reached", SimulationErrorKind::OptimizerMaxIterError.to_string());
37 /// ```
38 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
39 fmt.write_str(self.as_str())
40 }
41}
42
43
44/// Convert a [`OptimizeErrorKind`](crate::opt::OptimizeErrorKind) into a [`SimulationErrorKind`].
45pub fn convert_opt_error_to_simul_error(optimize_error: OptimizeErrorKind) -> SimulationErrorKind {
46 let result: SimulationErrorKind = match optimize_error {
47 OptimizeErrorKind::MaxIterError => SimulationErrorKind::OptimizerMaxIterError,
48 };
49
50 result
51}