Skip to content

Commit

Permalink
vasp: add Outcar abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ybyygu committed Mar 4, 2024
1 parent 572c7da commit 5d8883d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ clap = {version="4", features = ["derive"]}
gosh-core = { version="0.2.0" }
gosh-model = { version="0.2.0" }
gosh-repl = "0.1.3"
# NOTE: remove
text_parser = { version = "0.3.0", package = "gchemol-parser" }
gchemol-parser = { version = "0.5.0" }
grep-reader = "0.1.3"
winnow = "0.6"
serde = { version = "1.0", features = ["derive"] }
Expand Down
11 changes: 11 additions & 0 deletions src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ pub fn jump_to<'a>(literal: &str) -> impl FnMut(&mut &str) -> PResult<()> + '_ {
}
}

/// Take until found `literal`. The `literal` will not be consumed.
pub fn jump_until<'a>(literal: &str) -> impl FnMut(&mut &str) -> PResult<()> + '_ {
use winnow::token::take_until;
move |input: &mut &str| {
let _: &str = take_until(1.., literal)
.context(label("jump_until"))
.parse_next(input)?;
Ok(())
}
}

/// A combinator that takes a parser `inner` and produces a parser
/// that also consumes both leading and trailing whitespace, returning
/// the output of `inner`.
Expand Down
71 changes: 70 additions & 1 deletion src/vasp/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ pub struct Frame {
}
// c35d320f ends here

// [[file:../../adaptors.note::6f1bf8e7][6f1bf8e7]]
use gchemol_parser::TextReader;
use std::fs::File;
use std::io::BufReader;

pub struct Outcar {
reader: TextReader<BufReader<File>>,
}

impl Outcar {
pub fn try_from_path(f: &Path) -> Result<Self> {
let reader = TextReader::try_from_path(f)?;
Ok(Self { reader })
}
}
// 6f1bf8e7 ends here

// [[file:../../adaptors.note::d5a293f0][d5a293f0]]
// free energy TOTEN = -20.54559168 eV
fn energy_toten(input: &mut &str) -> PResult<f64> {
Expand Down Expand Up @@ -113,7 +130,7 @@ pub(self) fn num_ions_per_type<'a>(input: &mut &'a str) -> PResult<Vec<usize>> {
repeat(1.., preceded(space0, unsiged_integer)), _: space0,
_: line_ending,
}
.context(label("OUTCAR atom type"))
.context(label("OUTCAR ions per type"))
.parse_next(input)?;

Ok(sym.0)
Expand Down Expand Up @@ -264,6 +281,58 @@ fn outcar_positons_and_forces() -> PResult<()> {
}
// b5eb3fb1 ends here

// [[file:../../adaptors.note::5843bea2][5843bea2]]
impl Outcar {
pub(self) fn parse_atom_types(&mut self) -> Result<Vec<String>> {
use winnow::token::take_until;

let potcar_or_titel = |line: &str| line.contains("TITEL =") || line.contains("POTCAR:");
self.reader.seek_line(potcar_or_titel)?;

let mut buf = String::new();
let ions_per_type = |line: &str| line.contains(" ions per type =");
self.reader.read_until(&mut buf, ions_per_type)?;
self.reader.read_line(&mut buf)?;

let (types, nums) = seq! {
atom_types,
_: jump_until(" ions per type ="),
num_ions_per_type
}
.parse(&buf)
.map_err(|e| parse_error(e, &buf))?;

let symbols: Vec<_> = types
.into_iter()
.zip(nums)
.flat_map(|(s, n)| std::iter::repeat(s.to_owned()).take(n))
.collect();
Ok(symbols)
}
}

#[test]
fn outcar_atom_types_new() -> Result<()> {
let mut outcar = Outcar::try_from_path("./tests/files/vasp/OUTCAR-5.3.5".as_ref())?;
let symbols = outcar.parse_atom_types().unwrap();
assert_eq!(symbols.len(), 289);

let mut outcar = Outcar::try_from_path("tests/files/vasp/OUTCAR_diamond.dat".as_ref())?;
let symbols = outcar.parse_atom_types().unwrap();
assert_eq!(symbols.len(), 2);

let mut outcar = Outcar::try_from_path("tests/files/vasp/OUTCAR-5.2".as_ref())?;
let symbols = outcar.parse_atom_types().unwrap();
assert_eq!(symbols.len(), 8);

let mut outcar = Outcar::try_from_path("tests/files/vasp/AlH3_Vasp5.dat".as_ref())?;
let symbols = outcar.parse_atom_types().unwrap();
assert_eq!(symbols.len(), 8);

Ok(())
}
// 5843bea2 ends here

// [[file:../../adaptors.note::cf96d53e][cf96d53e]]
// For old VASP below 5.2.11
fn parse_frames_old(input: &mut &str) -> PResult<Vec<Frame>> {
Expand Down

0 comments on commit 5d8883d

Please sign in to comment.