def test_density_ncl(self): print("TEST DENSITY NCL") sys.stdout.flush() print("LOAD WAVEFUNCTION") sys.stdout.flush() wf = NCLWavefunction.from_directory('noncollinear') print("FINISHED LOAD WAVEFUNCTION") sys.stdout.flush() #wf = wf.desymmetrized_copy() wf.write_density_realspace(scale = wf.structure.lattice.volume) wf.write_density_realspace(dim=np.array([40,40,40]), scale = wf.structure.lattice.volume) tstchg = Chgcar.from_file("AECCAR2").data['total']# / wf.structure.volume chg = Chgcar.from_file("PYAECCAR").data['total'] reldiff = np.sqrt(np.mean(((chg-tstchg)/tstchg)**2)) newchg = chg-tstchg Chgcar(Poscar(wf.structure), {'total': newchg}).write_file('DIFFCHGCAR.vasp') print(np.sum(chg)/40**3, np.sum(tstchg)/40**3) assert_almost_equal(reldiff, 0, decimal=3)
def test_density(self): print("TEST DENSITY") sys.stdout.flush() wf = Wavefunction.from_directory('nosym') #wf = wf.desymmetrized_copy() wf.write_density_realspace(dim=np.array([40,40,40]), scale = wf.structure.lattice.volume) tstchg = Chgcar.from_file("AECCAR2").data['total']# / wf.structure.volume chg = Chgcar.from_file("PYAECCAR").data['total'] reldiff = np.sqrt(np.mean(((chg-tstchg)/tstchg)**2)) newchg = chg-tstchg Chgcar(Poscar(wf.structure), {'total': newchg}).write_file('DIFFCHGCAR.vasp') print(np.sum(chg)/40**3, np.sum(tstchg)/40**3) assert_almost_equal(reldiff, 0, decimal=2) wf = Wavefunction.from_directory('nosym') res = wf.write_density_realspace(filename="BAND4DENS", bands=4) #os.remove('PYAECCAR') print("DENS shape", res.shape) assert_almost_equal(np.sum(res)*wf.structure.lattice.volume/np.cumprod(res.shape)[-1], 1, 4)
def to_chgcar(self, filename=None): from pymatgen.io.vasp.inputs import Poscar from pymatgen.io.vasp.outputs import Chgcar # From: http://cms.mpi.univie.ac.at/vasp/vasp/CHGCAR_file.html # This file contains the total charge density multiplied by the volume # For spinpolarized calculations, two sets of data can be found in the CHGCAR file. # The first set contains the total charge density (spin up plus spin down), # the second one the magnetization density (spin up minus spin down). # For non collinear calculations the CHGCAR file contains the total charge density # and the magnetisation density in the x, y and z direction in this order. myrhor = self.datar * self.structure.volume data_dict = {"total": myrhor[0]} if self.nsppol == 2: {"diff": myrhor[0] - myrhor[1]} if self.nspinor == 2: raise NotImplementedError("pymatgen Chgcar does not implement nspinor == 2") chgcar = Chgcar(Poscar(self.structure), data_dict) if filename is not None: chgcar.write_file(filename) return chgcar
#%% from pymatgen.io.vasp.outputs import Chgcar from pymatgen.io.cube import Cube import os os.chdir('/home/jinho93/molecule/ddt/cp2k/bulk/asym/t1') cub = Cube('s1-SPIN_DENSITY-1_0.cube') cub.NZ for i in range(cub.NX): for j in range(cub.NY): for k in range(cub.NZ): if i + k > cub.NZ + 2 or i + k < cub.NZ / 2 + 5: cub.data[i, j, k] = 0 chg = Chgcar(cub.structure, {'total': cub.data}) chg.write_file('NEW_CHGCAR') # chg = Chgcar(cub.structure, cub.data) # %%