use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NKConfig {
pub tol: f64,
pub max_terms: usize,
pub asymptotic_terms: usize,
pub asymptotic_threshold: f64,
pub use_hypergeometric: bool,
}
impl Default for NKConfig {
fn default() -> Self {
NKConfig {
tol: 1e-12,
max_terms: 200,
asymptotic_terms: 15,
asymptotic_threshold: 8.0,
use_hypergeometric: true,
}
}
}
#[derive(Debug, Clone)]
pub struct NKResult {
pub value: f64,
pub derivative: Option<f64>,
pub index: usize,
pub x: f64,
pub error_estimate: Option<f64>,
pub method: NKMethod,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NKMethod {
Quadrature,
AsymptoticLarge,
SeriesSmall,
Hypergeometric,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NKFunctionType {
Nk0,
Nk1,
Nk2,
Nk3,
NkGeneral(usize),
}
impl NKFunctionType {
pub fn index(&self) -> usize {
match self {
NKFunctionType::Nk0 => 0,
NKFunctionType::Nk1 => 1,
NKFunctionType::Nk2 => 2,
NKFunctionType::Nk3 => 3,
NKFunctionType::NkGeneral(i) => *i,
}
}
pub fn from_index(i: usize) -> Self {
match i {
0 => NKFunctionType::Nk0,
1 => NKFunctionType::Nk1,
2 => NKFunctionType::Nk2,
3 => NKFunctionType::Nk3,
_ => NKFunctionType::NkGeneral(i),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NKPhysicalModel {
Brinkman,
DarcyLapwood,
AcousticGravity,
AiryIntegral,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nk_config_default() {
let config = NKConfig::default();
assert!((config.tol - 1e-12).abs() < f64::EPSILON);
assert_eq!(config.max_terms, 200);
assert_eq!(config.asymptotic_terms, 15);
assert!((config.asymptotic_threshold - 8.0).abs() < f64::EPSILON);
assert!(config.use_hypergeometric);
}
#[test]
fn test_nk_function_type_index() {
assert_eq!(NKFunctionType::Nk0.index(), 0);
assert_eq!(NKFunctionType::Nk1.index(), 1);
assert_eq!(NKFunctionType::Nk2.index(), 2);
assert_eq!(NKFunctionType::Nk3.index(), 3);
assert_eq!(NKFunctionType::NkGeneral(5).index(), 5);
}
#[test]
fn test_nk_function_type_from_index() {
assert_eq!(NKFunctionType::from_index(0), NKFunctionType::Nk0);
assert_eq!(NKFunctionType::from_index(1), NKFunctionType::Nk1);
assert_eq!(NKFunctionType::from_index(7), NKFunctionType::NkGeneral(7));
}
}