Skip to content

Commit

Permalink
[wavecar.rs] add custom grid size option for get_wavefunction_realspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Ionizing committed Oct 7, 2023
1 parent 551faaa commit d30ebe1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release-binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ jobs:
- name: Optimize and package binary
run: |
cd target/x86_64-unknown-linux-musl/release
strip rsgrad
chmod +x rsgrad
tar -c rsgrad | gzip > rsgrad.tar.gz
- name: Upload binary
Expand Down Expand Up @@ -60,7 +59,6 @@ jobs:
- name: Optimize and package binary
run: |
cd target/release
strip rsgrad
chmod +x rsgrad
tar -c rsgrad | gzip > rsgrad.tar.gz
Expand Down Expand Up @@ -95,7 +93,6 @@ jobs:
- name: Optimize and package binary
run: |
cd target/release
strip rsgrad
chmod +x rsgrad
tar -c rsgrad | gzip > rsgrad.tar.gz
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rsgrad"
version = "0.4.5"
version = "0.4.6"
authors = ["Ionizing <[email protected]>"]
edition = "2018"

Expand Down Expand Up @@ -38,6 +38,7 @@ tempdir = "0.3"


[profile.release]
debug = true
incremental = true
lto = true
opt-level = 3
2 changes: 1 addition & 1 deletion src/commands/wav1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ I suggest you provide `gamma_half` argument to avoid confusion.");
let eig = eigs[[ispin as usize, ikpoint as usize, iband as usize]] - efermi;
let label = format!("s{}_k{}_b{}_{:06.3}eV", ispin+1, ikpoint+1, iband+1, eig);

let wavr = wav.get_wavefunction_realspace(ispin, ikpoint, iband)
let wavr = wav.get_wavefunction_realspace(ispin, ikpoint, iband, None)
.unwrap_or_else(|_| panic!("Failed to get wavefunction in realspace at s{} k{} b{}", ispin+1, ikpoint+1, iband+1))
.normalize();

Expand Down
12 changes: 11 additions & 1 deletion src/commands/wav3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ pub struct Wav3D {
/// processing WAVECAR produced by `vasp_gam`.
gamma_half: Option<String>,

#[clap(long, number_of_values = 3)]
/// Grid size for realspace wavefunction, 3 numbers are required, i.e. NGXF NGYF and NGZF.
///
/// If this argument is left empty, NG_F will be set as NG_F=2*NG_.
///
/// Be aware that NG_F must be greater than or at least equal to corresponding NG_.
ngrid: Option<Vec<u64>>,

#[clap(long, short = 'o', possible_values = &["normsquared", "ns", "uns", "dns", "real", "re", "imag", "im", "reim"],
multiple_values = true)]
/// Specify output part of the wavefunction.
Expand Down Expand Up @@ -187,6 +195,8 @@ I suggest you provide `gamma_half` argument to avoid confusion.");
.map(|v| v as u64 - 1)
.collect::<Vec<_>>();

let ngrid = self.ngrid.as_ref().map(|g| { [g[0], g[1], g[2]] });

let indices = iproduct!(ispins, ikpoints, ibands)
.collect::<Vec<(u64, u64, u64)>>();

Expand All @@ -203,7 +213,7 @@ I suggest you provide `gamma_half` argument to avoid confusion.");
String::new()
};

let wavr = wav.get_wavefunction_realspace(ispin, ikpoint, iband)?.normalize();
let wavr = wav.get_wavefunction_realspace(ispin, ikpoint, iband, ngrid)?.normalize();
let chgd = match wavr.clone() {
Wavefunction::Complex64Array3(w) => w.mapv(|v| v.norm_sqr() * factor),
Wavefunction::Float64Array3(w) => w.mapv(|v| v * v * factor),
Expand Down
69 changes: 40 additions & 29 deletions src/vasp_parsers/wavecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,28 +637,39 @@ impl Wavecar {


// indices start from 0
pub fn get_wavefunction_realspace(&self, ispin: u64, ikpoint: u64, iband: u64) -> Result<Wavefunction> {
pub fn get_wavefunction_realspace(&self, ispin: u64, ikpoint: u64, iband: u64, ngrid: Option<[u64; 3]>) -> Result<Wavefunction> {
assert!(ispin < self.nspin, "Invalid ispin: {}, nspin = {}", ispin + 1, self.nspin);
assert!(ikpoint < self.nkpoints, "Invalid ikpoint: {}, nkpoints = {}", ikpoint + 1, self.nkpoints);
assert!(iband < self.nbands, "Invalid iband: {}, nbands = {}", iband + 1, self.nbands);

let ngrid = if let Some(g) = ngrid {
if g[0] < self.ngrid[0] || g[1] < self.ngrid[1] || g[2] < self.ngrid[2] {
bail!("NGXF NGYF NGZF smaller than NGX NGY NGZ, pleas check.")
}
g
} else {
[ self.ngrid[0] * 2,
self.ngrid[1] * 2,
self.ngrid[2] * 2, ]
};

let ngxr = ngrid[0] as i64;
let ngyr = ngrid[1] as i64;
let ngzr = ngrid[2] as i64;

match self.wavecar_type {
WavecarType::Standard => self._get_wavefunction_realspace_std(ispin, ikpoint, iband),
WavecarType::NonCollinear => self._get_wavefunction_realspace_ncl(ispin, ikpoint, iband),
WavecarType::GamaHalf(Axis::X) => self._get_wavefunction_realspace_gamx(ispin, ikpoint, iband),
WavecarType::GamaHalf(Axis::Z) => self._get_wavefunction_realspace_gamz(ispin, ikpoint, iband),
WavecarType::Standard => self._get_wavefunction_realspace_std(ispin, ikpoint, iband, ngxr, ngyr, ngzr),
WavecarType::NonCollinear => self._get_wavefunction_realspace_ncl(ispin, ikpoint, iband, ngxr, ngyr, ngzr),
WavecarType::GamaHalf(Axis::X) => self._get_wavefunction_realspace_gamx(ispin, ikpoint, iband, ngxr, ngyr, ngzr),
WavecarType::GamaHalf(Axis::Z) => self._get_wavefunction_realspace_gamz(ispin, ikpoint, iband, ngxr, ngyr, ngzr),
_ => bail!("Unknown or unsupported WAVECAR: {}", self.wavecar_type),
}
}


fn _get_wavefunction_realspace_std(&self, ispin: u64, ikpoint :u64, iband: u64) -> Result<Wavefunction> {
fn _get_wavefunction_realspace_std(&self, ispin: u64, ikpoint :u64, iband: u64, ngxr: i64, ngyr: i64, ngzr: i64) -> Result<Wavefunction> {
assert_eq!(self.wavecar_type, WavecarType::Standard);

let ngxr = self.ngrid[0] as i64 * 2;
let ngyr = self.ngrid[1] as i64 * 2;
let ngzr = self.ngrid[2] as i64 * 2;

let gvecs: MatX3<usize> = self.generate_fft_grid(ikpoint)
.into_iter()
.map(|[x, y, z]| {
Expand Down Expand Up @@ -700,13 +711,9 @@ impl Wavecar {
}


fn _get_wavefunction_realspace_gamx(&self, ispin: u64, ikpoint: u64, iband: u64) -> Result<Wavefunction> {
fn _get_wavefunction_realspace_gamx(&self, ispin: u64, ikpoint: u64, iband: u64, ngxr: i64, ngyr: i64, ngzr: i64) -> Result<Wavefunction> {
assert_eq!(self.wavecar_type, WavecarType::GamaHalf(Axis::X));

let ngxr = self.ngrid[0] as i64 * 2;
let ngyr = self.ngrid[1] as i64 * 2;
let ngzr = self.ngrid[2] as i64 * 2;

let ngxk = ngxr / 2 + 1;
let ngyk = ngyr;
let ngzk = ngzr;
Expand Down Expand Up @@ -767,13 +774,9 @@ impl Wavecar {
}


fn _get_wavefunction_realspace_gamz(&self, ispin: u64, ikpoint: u64, iband: u64) -> Result<Wavefunction> {
fn _get_wavefunction_realspace_gamz(&self, ispin: u64, ikpoint: u64, iband: u64, ngxr: i64, ngyr: i64, ngzr: i64) -> Result<Wavefunction> {
assert_eq!(self.wavecar_type, WavecarType::GamaHalf(Axis::Z));

let ngxr = self.ngrid[0] as i64 * 2;
let ngyr = self.ngrid[1] as i64 * 2;
let ngzr = self.ngrid[2] as i64 * 2;

let ngxk = ngxr;
let ngyk = ngyr;
let ngzk = ngzr / 2 + 1;
Expand Down Expand Up @@ -835,13 +838,9 @@ impl Wavecar {
}


fn _get_wavefunction_realspace_ncl(&self, ispin: u64, ikpoint: u64, iband: u64) -> Result<Wavefunction> {
fn _get_wavefunction_realspace_ncl(&self, ispin: u64, ikpoint: u64, iband: u64, ngxr: i64, ngyr: i64, ngzr: i64) -> Result<Wavefunction> {
assert_eq!(self.wavecar_type, WavecarType::NonCollinear);

let ngxr = self.ngrid[0] as i64 * 2;
let ngyr = self.ngrid[1] as i64 * 2;
let ngzr = self.ngrid[2] as i64 * 2;

let gvecs: MatX3<usize> = self.generate_fft_grid(ikpoint)
.into_iter()
.map(|[x, y, z]| {
Expand Down Expand Up @@ -1071,9 +1070,12 @@ mod tests {
#[ignore]
fn test_wavefunction_realspace_std() {
let wav = Wavecar::from_file("WAVECAR").unwrap();
let ngxr = wav.ngrid[0] as i64 * 2;
let ngyr = wav.ngrid[0] as i64 * 2;
let ngzr = wav.ngrid[0] as i64 * 2;

for i in 0 .. 1 {
let wavr = wav._get_wavefunction_realspace_std(0, i, 0).unwrap(); // 1 (i+1) 1
let wavr = wav._get_wavefunction_realspace_std(0, i, 0, ngxr, ngyr, ngzr).unwrap(); // 1 (i+1) 1
let mut wavr = match wavr {
Wavefunction::Complex64Array3(dump) => dump,
_ => panic!(),
Expand Down Expand Up @@ -1106,9 +1108,12 @@ mod tests {
#[ignore]
fn test_wavefunction_realspace_ncl() {
let wav = Wavecar::from_file("WAVECAR").unwrap();
let ngxr = wav.ngrid[0] as i64 * 2;
let ngyr = wav.ngrid[0] as i64 * 2;
let ngzr = wav.ngrid[0] as i64 * 2;

for i in 0 .. 64 {
let wavr = wav._get_wavefunction_realspace_ncl(0, 0, i).unwrap(); // 1 (i+1) 1
let wavr = wav._get_wavefunction_realspace_ncl(0, 0, i, ngxr, ngyr, ngzr).unwrap(); // 1 (i+1) 1
let mut wavr = match wavr {
Wavefunction::Ncl64Array4(dump) => dump,
_ => panic!(),
Expand Down Expand Up @@ -1141,9 +1146,12 @@ mod tests {
#[ignore]
fn test_wavefunction_realspace_gamx() {
let wav = Wavecar::from_file("WAVECAR").unwrap();
let ngxr = wav.ngrid[0] as i64 * 2;
let ngyr = wav.ngrid[0] as i64 * 2;
let ngzr = wav.ngrid[0] as i64 * 2;

for i in 0 .. 1 {
let wavr = wav._get_wavefunction_realspace_gamx(0, i, 0).unwrap(); // 1 (i+1) 1
let wavr = wav._get_wavefunction_realspace_gamx(0, i, 0, ngxr, ngyr, ngzr).unwrap(); // 1 (i+1) 1
let mut wavr = match wavr {
Wavefunction::Float64Array3(dump) => dump,
_ => panic!(),
Expand Down Expand Up @@ -1177,9 +1185,12 @@ mod tests {
fn test_wavefunction_realspace_gamz() {
let mut wav = Wavecar::from_file("WAVECAR").unwrap();
wav.set_wavecar_type(WavecarType::GamaHalf(Axis::Z)).unwrap();
let ngxr = wav.ngrid[0] as i64 * 2;
let ngyr = wav.ngrid[0] as i64 * 2;
let ngzr = wav.ngrid[0] as i64 * 2;

for i in 0 .. 1 {
let wavr = wav._get_wavefunction_realspace_gamz(0, i, 0).unwrap(); // 1 (i+1) 1
let wavr = wav._get_wavefunction_realspace_gamz(0, i, 0, ngxr, ngyr, ngzr).unwrap(); // 1 (i+1) 1
let mut wavr = match wavr {
Wavefunction::Float64Array3(dump) => dump,
_ => panic!(),
Expand Down

0 comments on commit d30ebe1

Please sign in to comment.