use castep_cell_fmt::{Cell, CellValue, ToCell, ToCellValue, FromBlock, FromCellValue, CResult, query::value_as_f64};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OpticsKpointsListEntry {
pub coord: [f64; 3],
pub weight: f64,
}
impl FromCellValue for OpticsKpointsListEntry {
fn from_cell_value(value: &CellValue<'_>) -> CResult<Self> {
match value {
CellValue::Array(arr) if arr.len() == 4 => {
let coord = [
value_as_f64(&arr[0])?,
value_as_f64(&arr[1])?,
value_as_f64(&arr[2])?,
];
let weight = value_as_f64(&arr[3])?;
Ok(OpticsKpointsListEntry { coord, weight })
}
_ => Err(castep_cell_fmt::Error::Message(
"OpticsKpointsListEntry must be an array of 4 floats".into(),
)),
}
}
}
impl ToCellValue for OpticsKpointsListEntry {
fn to_cell_value(&self) -> CellValue<'_> {
CellValue::Array(
self.coord
.into_iter()
.map(CellValue::Float)
.chain([CellValue::Float(self.weight)])
.collect(),
)
}
}
#[derive(Debug, Clone, PartialEq, bon::Builder)]
pub struct OpticsKpointsList {
#[builder(default)]
pub kpoints: Vec<OpticsKpointsListEntry>,
}
impl FromBlock for OpticsKpointsList {
const BLOCK_NAME: &'static str = "OPTICS_KPOINT_LIST";
const BLOCK_ALIASES: &'static [&'static str] = &["OPTICS_KPOINTS_LIST"];
fn from_block_rows(rows: &[CellValue<'_>]) -> CResult<Self> {
let kpoints = rows
.iter()
.map(OpticsKpointsListEntry::from_cell_value)
.collect::<CResult<Vec<_>>>()?;
Ok(OpticsKpointsList { kpoints })
}
}
impl ToCell for OpticsKpointsList {
fn to_cell(&self) -> Cell<'_> {
Cell::Block(
"OPTICS_KPOINT_LIST",
self.kpoints
.iter()
.map(|entry| entry.to_cell_value())
.collect::<Vec<CellValue>>(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_optics_kpoints_list_entry_from_cell_value() {
let val = CellValue::Array(vec![
CellValue::Float(0.5),
CellValue::Float(0.25),
CellValue::Float(0.0),
CellValue::Float(1.0),
]);
let entry = OpticsKpointsListEntry::from_cell_value(&val).unwrap();
assert_eq!(entry.coord, [0.5, 0.25, 0.0]);
assert_eq!(entry.weight, 1.0);
}
#[test]
fn test_optics_kpoints_list_entry_insufficient_elements() {
let val = CellValue::Array(vec![
CellValue::Float(0.5),
CellValue::Float(0.25),
CellValue::Float(0.0),
]);
assert!(OpticsKpointsListEntry::from_cell_value(&val).is_err());
}
#[test]
fn test_optics_kpoints_list_entry_to_cell_value() {
let entry = OpticsKpointsListEntry {
coord: [0.5, 0.25, 0.0],
weight: 1.0,
};
let val = entry.to_cell_value();
match val {
CellValue::Array(arr) => {
assert_eq!(arr.len(), 4);
assert_eq!(arr[3], CellValue::Float(1.0));
}
_ => panic!("Expected Array"),
}
}
#[test]
fn test_optics_kpoints_list_multiple_entries() {
let rows = vec![
CellValue::Array(vec![
CellValue::Float(0.0),
CellValue::Float(0.0),
CellValue::Float(0.0),
CellValue::Float(0.5),
]),
CellValue::Array(vec![
CellValue::Float(0.5),
CellValue::Float(0.5),
CellValue::Float(0.5),
CellValue::Float(0.5),
]),
];
let result = OpticsKpointsList::from_block_rows(&rows).unwrap();
assert_eq!(result.kpoints.len(), 2);
assert_eq!(result.kpoints[0].weight, 0.5);
assert_eq!(result.kpoints[1].weight, 0.5);
}
#[test]
fn test_optics_kpoints_list_empty() {
let result = OpticsKpointsList::from_block_rows(&[]).unwrap();
assert_eq!(result.kpoints.len(), 0);
}
#[test]
fn test_optics_kpoints_list_block_name() {
assert_eq!(OpticsKpointsList::BLOCK_NAME, "OPTICS_KPOINT_LIST");
}
#[test]
fn test_optics_kpoints_list_to_cell() {
let kpts = OpticsKpointsList {
kpoints: vec![OpticsKpointsListEntry {
coord: [0.0, 0.0, 0.0],
weight: 1.0,
}],
};
let cell = kpts.to_cell();
match cell {
Cell::Block(name, values) => {
assert_eq!(name, "OPTICS_KPOINT_LIST");
assert_eq!(values.len(), 1);
}
_ => panic!("Expected Block"),
}
}
}