Last active
May 31, 2023 02:30
-
-
Save Ionizing/20b59aff99b22b07483ddecc3100585c to your computer and use it in GitHub Desktop.
Calculate spin density and save it to SPIN_DENSITY.vasp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from ase.calculators.vasp import VaspChargeDensity | |
print("READING CHGCAR ...") | |
chgcar = VaspChargeDensity("CHGCAR") | |
assert chgcar.is_spin_polarized() | |
if len(chgcar.chgdiff) == 1: | |
spin_density = chgcar.chgdiff[0].copy() | |
pass | |
elif len(chgcar.chgdiff == 3): | |
spin_density = chgcar.chgdiff[-1].copy() | |
pass | |
else: | |
raise ValueError("Invalid CHGCAR") | |
pass | |
chgcar.chgdiff = [] | |
chgcar.chg = [spin_density] | |
print("WRITING SPIN_DENSITY.vasp") | |
chgcar.write("SPIN_DENSITY.vasp", format='chg') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from ase.calculators.vasp import VaspChargeDensity | |
from argparse import ArgumentParser | |
def calc_spin_density(fname_up, fname_dn, prefix): | |
up = VaspChargeDensity(fname_up) | |
dn = VaspChargeDensity(fname_dn) | |
up.chg[0][...] -= dn.chg[0] | |
up.write(prefix + "_spinden.vasp") | |
def parse_args(): | |
parser = ArgumentParser( | |
description="A tool to calculate spin density with given spin up and spin down densities.", | |
add_help=True) | |
parser.add_argument("spin_up", type=str, action="store", | |
help="Spin up density file") | |
parser.add_argument("spin_dn", type=str, action="store", | |
help="Spin down density file") | |
parser.add_argument("prefix", type=str, nargs="?", | |
help="Prefix of output filename", default="rsgrad") | |
return parser.parse_args() | |
if '__main__' == __name__: | |
args = parse_args() | |
calc_spin_density(args.spin_up, args.spin_dn, args.prefix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment