def get_diffusion_analyzer(self, specie, temperature, time_step, step_skip, smoothed=None, min_obs=30, avg_nsteps=1000): """ Args: specie (Element/Specie): Specie to calculate diffusivity for as a String. E.g., "Li". temperature (float): Temperature of the diffusion run in Kelvin. time_step (int): Time step between measurements. step_skip (int): Sampling frequency of the displacements ( time_step is multiplied by this number to get the real time between measurements) For the other parameters please see the pymatgen.analysis.diffusion_analyzer.DiffusionAnalyzer documentation. Returns: DiffusionAnalyzer """ # structures = self.get_structures_from_trajectory() structure, disp = self.get_displacements() return DiffusionAnalyzer(structure, disp, specie, temperature, time_step, step_skip=step_skip, smoothed=smoothed, min_obs=min_obs, avg_nsteps=avg_nsteps)
def test_from_structure_NPT(self): from pymatgen import Lattice, Structure coords1 = np.array([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]]) coords2 = np.array([[0.0, 0.0, 0.0], [0.6, 0.6, 0.6]]) coords3 = np.array([[0.0, 0.0, 0.0], [0.7, 0.7, 0.7]]) lattice1 = Lattice.from_parameters( a=2.0, b=2.0, c=2.0, alpha=90, beta=90, gamma=90 ) lattice2 = Lattice.from_parameters( a=2.1, b=2.1, c=2.1, alpha=90, beta=90, gamma=90 ) lattice3 = Lattice.from_parameters( a=2.0, b=2.0, c=2.0, alpha=90, beta=90, gamma=90 ) s1 = Structure(coords=coords1, lattice=lattice1, species=["F", "Li"]) s2 = Structure(coords=coords2, lattice=lattice2, species=["F", "Li"]) s3 = Structure(coords=coords3, lattice=lattice3, species=["F", "Li"]) structures = [s1, s2, s3] d = DiffusionAnalyzer.from_structures( structures, specie="Li", temperature=500.0, time_step=2.0, step_skip=1, smoothed=None, ) self.assertArrayAlmostEqual( d.disp[1], np.array([[0.0, 0.0, 0.0], [0.21, 0.21, 0.21], [0.40, 0.40, 0.40]]), )
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer.json")) as f: d = DiffusionAnalyzer.from_dict(json.load(f)) self.assertAlmostEqual(d.conductivity, 74.1362195972, 7) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertTrue(np.allclose( d.conductivity_components, [47.8728896, 31.3098319, 143.47106767])) self.assertTrue(np.allclose( d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06])) self.assertAlmostEqual(d.max_framework_displacement, 1.1865683960) d = DiffusionAnalyzer.from_dict(d.to_dict) self.assertIsInstance(d, DiffusionAnalyzer)
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer.json")) as f: d = DiffusionAnalyzer.from_dict(json.load(f)) self.assertAlmostEqual(d.conductivity, 74.1362195972, 7) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertTrue( np.allclose(d.conductivity_components, [47.8728896, 31.3098319, 143.47106767])) self.assertTrue( np.allclose(d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06])) self.assertAlmostEqual(d.max_framework_displacement, 1.1865683960) d = DiffusionAnalyzer.from_dict(d.to_dict) self.assertIsInstance(d, DiffusionAnalyzer)
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer.json")) as f: d = DiffusionAnalyzer.from_dict(json.load(f)) self.assertAlmostEqual(d.conductivity, 74.1362195972, 7) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertAlmostEqual(d.conductivity_std_dev, 8.301909069566328, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 1.29992598086e-07, 7) self.assertArrayAlmostEqual( d.conductivity_components, [47.8728896, 31.3098319, 143.47106767]) self.assertArrayAlmostEqual( d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06]) self.assertArrayAlmostEqual( d.conductivity_components_std_dev, [8.16076457, 22.74144339, 20.64816641] ) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [1.27782535e-07, 3.56089098e-07, 3.23312238e-07] ) self.assertAlmostEqual(d.max_framework_displacement, 1.1865683960) d = DiffusionAnalyzer.from_dict(d.to_dict) self.assertIsInstance(d, DiffusionAnalyzer) #Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=True, weighted=True) self.assertAlmostEqual(d.conductivity, 74.16537220815061, 7) self.assertAlmostEqual(d.diffusivity, 1.14606446822e-06, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False) self.assertAlmostEqual(d.conductivity, 27.2047915553, 7) self.assertAlmostEqual(d.diffusivity, 4.25976905436e-07, 7)
def test_van_hove(self): data_file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(data_file, "r")) obj = DiffusionAnalyzer.from_dict(data) vh = VanHoveAnalysis(diffusion_analyzer=obj, avg_nsteps=5, ngrid=101, rmax=10.0, step_skip=5, sigma=0.1, species=["Li", "Na"]) check = np.shape(vh.gsrt) == (20, 101) and np.shape(vh.gdrt) == (20, 101) self.assertTrue(check) self.assertAlmostEqual(vh.gsrt[0, 0], 3.98942280401, 10) self.assertAlmostEqual(vh.gdrt[10, 0], 9.68574868168, 10)
def test_probability_classmethod(self): file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(file, "r")) diff_analyzer = DiffusionAnalyzer.from_dict(data) #ProbabilityDensityAnalysis object pda = ProbabilityDensityAnalysis.from_diffusion_analyzer(diffusion_analyzer=diff_analyzer, interval=0.5) dV = pda.structure.lattice.volume / pda.lens[0] / pda.lens[1] / pda.lens[2] Pr_tot = np.sum(pda.Pr) * dV self.assertAlmostEqual(pda.Pr.max(), 0.0361594977596, 8) self.assertAlmostEqual(pda.Pr.min(), 0.0, 12) self.assertAlmostEqual(Pr_tot, 1.0, 12)
def test_get_df(self): data_file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(data_file, "r")) obj = DiffusionAnalyzer.from_dict(data) structure_list = [] for i, s in enumerate(obj.get_drift_corrected_structures()): structure_list.append(s) if i == 9: break eva = EvolutionAnalyzer(structure_list, rmax=10, step=1, time_step=2) rdf = eva.get_df(EvolutionAnalyzer.rdf, pair=("Na", "Na")) atom_dist = eva.get_df(EvolutionAnalyzer.atom_dist, specie="Na", direction="c") check = np.shape(rdf) == (10, 101) and np.shape(atom_dist) == (10, 101) and eva.pairs[0] == ("Na", "Na") self.assertTrue(check) self.assertAlmostEqual(max(np.array(rdf)[0]), 1.82363640047, 4)
def test_rdf(self): data_file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(data_file, "r")) obj = DiffusionAnalyzer.from_dict(data) structure_list = [] for i, s in enumerate(obj.get_drift_corrected_structures()): structure_list.append(s) if i == 9: break obj = RadialDistributionFunction(structures=structure_list, ngrid=101, rmax=10.0, cellrange=1, sigma=0.1, species = ["Na","P","S"]) check = np.shape(obj.rdf)[0] == 101 and np.argmax(obj.rdf) == 34 self.assertTrue(check) self.assertAlmostEqual(obj.rdf.max(), 1.6831, 4)
def test_from_structure_NPT( self ): from pymatgen import Structure, Lattice coords1 = np.array([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]] ) coords2 = np.array([[0.0, 0.0, 0.0], [0.6, 0.6, 0.6]] ) coords3 = np.array([[0.0, 0.0, 0.0], [0.7, 0.7, 0.7]] ) lattice1 = Lattice.from_parameters(a=2.0, b=2.0, c=2.0, alpha=90, beta=90, gamma=90) lattice2 = Lattice.from_parameters(a=2.1, b=2.1, c=2.1, alpha=90, beta=90, gamma=90) lattice3 = Lattice.from_parameters(a=2.0, b=2.0, c=2.0, alpha=90, beta=90, gamma=90) s1 = Structure(coords=coords1, lattice=lattice1, species=['F', 'Li']) s2 = Structure(coords=coords2, lattice=lattice2, species=['F', 'Li']) s3 = Structure(coords=coords3, lattice=lattice3, species=['F', 'Li']) structures = [s1, s2, s3] d = DiffusionAnalyzer.from_structures( structures, specie='Li', temperature=500.0, time_step=2.0, step_skip=1, smoothed=None ) self.assertArrayAlmostEqual(d.disp[1], np.array([[0., 0., 0. ], [0.21, 0.21, 0.21], [0.40, 0.40, 0.40]]))
def test_site_occupancy_classmethod(self): file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(file, "r")) diff_analyzer = DiffusionAnalyzer.from_dict(data) structure = diff_analyzer.structure coords_ref = [ss.frac_coords for ss in structure if ss.specie.symbol == "Na"] #SiteOccupancyAnalyzer object socc = SiteOccupancyAnalyzer.from_diffusion_analyzer(coords_ref, diffusion_analyzer=diff_analyzer) site_occ = socc.site_occ self.assertAlmostEqual(np.sum(site_occ), len(coords_ref), 12) self.assertAlmostEqual(site_occ[1], 0.98, 12) self.assertAlmostEqual(site_occ[26], 0.97, 12) self.assertEqual(len(coords_ref), 48)
def test_rdf(self): data_file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(data_file, "r")) obj = DiffusionAnalyzer.from_dict(data) structure_list = [] for i, s in enumerate(obj.get_drift_corrected_structures()): structure_list.append(s) if i == 9: break obj = RadialDistributionFunction(structures=structure_list, ngrid=101, rmax=10.0, cellrange=1, sigma=0.1, species=["Na", "P", "S"]) check = np.shape(obj.rdf)[0] == 101 and np.argmax(obj.rdf) == 34 self.assertTrue(check) self.assertAlmostEqual(obj.rdf.max(), 1.6831, 4)
def write_data(crt): # -- Save diffusivity and conductivity of current step # -- Skip initial heating process and stablizing process : set as start_num start_num = 1 if crt >= start_num: vaspruns = [] for i in range(start_num, crt + 1): dirname = "run%03d" % i vasprun = dirname + "/vasprun.xml.gz" vaspruns.append(vasprun) # -- collect all smoothing modes of analyzer analyzers = {} for mode in [False, 'constant', 'max']: try: analyzers[mode] = DiffusionAnalyzer.from_files(vaspruns, specie=specie, smoothed=mode, min_obs=60) except: analyzers[mode] = None # -- save DiffusionAnalzyer as pickle to plot msd quickly if crt % 10 == 0: with open("analyzer%03d.pkl" % crt, 'wb') as save_data: pickle.dump(analyzers, save_data) os.system("gzip analyzer%03d.pkl" % crt) # -- write data f = open("data_%sK.csv" % temp, "a") timestep = (crt - start_num + 1) * nsw * 2 / 1000 step_info = "%d,%d," % (crt, timestep) f.write(step_info) for mode in [False, 'constant', 'max']: if analyzers[mode] == None: diffusivity, conductivity = 0.0, 0.0 else: sd = analyzers[mode].get_summary_dict() diffusivity = sd['D'] conductivity = sd['S'] f = open("data_%sK.csv" % temp, "a") line = "%.10f,%.4f," % (diffusivity, conductivity) f.write(line) f.write("\n") f.close()
def test_generate_stable_sites(self): file = os.path.join(tests_dir, "cNa3PS4_pda.json") data = json.load(open(file, "r")) diff_analyzer = DiffusionAnalyzer.from_dict(data) # ProbabilityDensityAnalysis object pda = ProbabilityDensityAnalysis.from_diffusion_analyzer( diffusion_analyzer=diff_analyzer, interval=0.1) pda.generate_stable_sites(p_ratio=0.25, d_cutoff=1.5) self.assertEqual(len(pda.stable_sites), 50) self.assertAlmostEqual(pda.stable_sites[1][2], 0.24113475177304966, 8) self.assertAlmostEqual(pda.stable_sites[7][1], 0.5193661971830985, 8) s = pda.get_full_structure() self.assertEqual(s.num_sites, 178) self.assertEqual(s.composition["Na"], 48) self.assertEqual(s.composition["X"], 50) self.assertAlmostEqual(s[177].frac_coords[2], 0.57446809)
def test_rdf(self): data_file = os.path.join(tests_dir, "cNa3PS4_pda.json") with open(data_file, "r") as j: data = json.load(j) obj = DiffusionAnalyzer.from_dict(data) structure_list = [] for i, s in enumerate(obj.get_drift_corrected_structures()): structure_list.append(s) if i == 9: break species = ["Na", "P", "S"] # Test from_species obj = RadialDistributionFunction.from_species( structures=structure_list, ngrid=101, rmax=10.0, cell_range=1, sigma=0.1, species=species, reference_species=species) check = np.shape(obj.rdf)[0] == 101 and np.argmax(obj.rdf) == 34 self.assertTrue(check) self.assertAlmostEqual(obj.rdf.max(), 1.634448, 4) # Test init s = structure_list[0] indices = [ i for (i, site) in enumerate(s) if site.species_string in species ] obj = RadialDistributionFunction(structures=structure_list, ngrid=101, rmax=10.0, cell_range=1, sigma=0.1, indices=indices, reference_indices=indices) check = np.shape(obj.rdf)[0] == 101 and np.argmax(obj.rdf) == 34 self.assertTrue(check) self.assertAlmostEqual(obj.rdf.max(), 1.634448, 4)
def Diffusion(self, skip=10, spaces='S', temp=[300, 310, 350, 390]): from pymatgen.analysis.diffusion_analyzer import DiffusionAnalyzer, fit_arrhenius, get_arrhenius_plot import shutil import os os.chdir(self.dire) filePath = self.dire file = [] for dirpath, dirnames, filenames in os.walk(filePath): # print(dirpath) for name in dirnames: path = os.path.join(filePath, name) path1 = os.path.join(path, 'vasprun.xml') print(path1) file.append(str(path1)) diff = DiffusionAnalyzer.from_files(file, specie=spaces, step_skip=skip) diff.get_msd_plot() diff.get_summary_dict() fit = fit_arrhenius(temps=temp, diffusivities=diff.diffusivity) plot = get_arrhenius_plot(temps=temp, diffusivities=diff.diffusivity)
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open( os.path.join(PymatgenTest.TEST_FILES_DIR, "DiffusionAnalyzer.json")) as f: dd = json.load(f) d = DiffusionAnalyzer.from_dict(dd) # large tolerance because scipy constants changed between 0.16.1 and 0.17 self.assertAlmostEqual(d.conductivity, 74.165372613735684, 4) self.assertAlmostEqual(d.chg_conductivity, 232.8278799754324, 4) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 3.64565578208e-06, 7) self.assertAlmostEqual(d.conductivity_std_dev, 0.0097244677795984488, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 9.1013023085561779e-09, 7) self.assertAlmostEqual(d.chg_diffusivity_std_dev, 7.20911399729e-10, 5) self.assertAlmostEqual(d.haven_ratio, 0.31854161048867402, 7) self.assertArrayAlmostEqual(d.conductivity_components, [45.7903694, 26.1651956, 150.5406140], 3) self.assertArrayAlmostEqual( d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06], ) self.assertArrayAlmostEqual(d.conductivity_components_std_dev, [0.0063566, 0.0180854, 0.0217918]) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [8.9465670e-09, 2.4931224e-08, 2.2636384e-08], ) self.assertArrayAlmostEqual( d.mscd[0:4], [0.69131064, 0.71794072, 0.74315283, 0.76703961]) self.assertArrayAlmostEqual( d.max_ion_displacements, [ 1.4620659693989553, 1.2787303484445025, 3.419618540097756, 2.340104469126246, 2.6080973517594233, 1.3928579365672844, 1.3561505956708932, 1.6699242923686253, 1.0352389639563648, 1.1662520093955808, 1.2322019205885841, 0.8094210554832534, 1.9917808504954169, 1.2684148391206396, 2.392633794162402, 2.566313049232671, 1.3175030435622759, 1.4628945430952793, 1.0984921286753002, 1.2864482076554093, 0.655567027815413, 0.5986961164605746, 0.5639091444309045, 0.6166004192954059, 0.5997911580422605, 0.4374606277579815, 1.1865683960470783, 0.9017064371676591, 0.6644840367853767, 1.0346375380664645, 0.6177630142863979, 0.7952002051914302, 0.7342686123054011, 0.7858047956905577, 0.5570732369065661, 1.0942937746885417, 0.6509372395308788, 1.0876687380413455, 0.7058162184725, 0.8298306317598585, 0.7813913747621343, 0.7337655232056153, 0.9057161616236746, 0.5979093093186919, 0.6830333586985015, 0.7926500894084628, 0.6765180009988608, 0.8555866032968998, 0.713087091642237, 0.7621007695790749, ], ) self.assertEqual(d.sq_disp_ions.shape, (50, 206)) self.assertEqual(d.lattices.shape, (1, 3, 3)) self.assertEqual(d.mscd.shape, (206, )) self.assertEqual(d.mscd.shape, d.msd.shape) self.assertAlmostEqual(d.max_framework_displacement, 1.18656839605) ss = list(d.get_drift_corrected_structures(10, 1000, 20)) self.assertEqual(len(ss), 50) n = random.randint(0, 49) n_orig = n * 20 + 10 self.assertArrayAlmostEqual( ss[n].cart_coords - d.structure.cart_coords + d.drift[:, n_orig, :], d.disp[:, n_orig, :], ) d = DiffusionAnalyzer.from_dict(d.as_dict()) self.assertIsInstance(d, DiffusionAnalyzer) # Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer( d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="max", ) self.assertAlmostEqual(d.conductivity, 74.165372613735684, 4) self.assertAlmostEqual(d.diffusivity, 1.14606446822e-06, 7) self.assertAlmostEqual(d.haven_ratio, 0.318541610489, 6) self.assertAlmostEqual(d.chg_conductivity, 232.8278799754324, 4) self.assertAlmostEqual(d.chg_diffusivity, 3.64565578208e-06, 7) d = DiffusionAnalyzer( d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False, ) self.assertAlmostEqual(d.conductivity, 27.20479170406027, 4) self.assertAlmostEqual(d.diffusivity, 4.25976905436e-07, 7) self.assertAlmostEqual(d.chg_diffusivity, 1.6666666666666667e-17, 3) d = DiffusionAnalyzer( d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=100, ) self.assertAlmostEqual(d.conductivity, 47.404056230438741, 4) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7) self.assertAlmostEqual(d.chg_conductivity, 1.06440821953e-09, 4) # Can't average over 2000 steps because this is a 1000-step run. self.assertRaises( ValueError, DiffusionAnalyzer, d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=2000, ) d = DiffusionAnalyzer.from_structures( list(d.get_drift_corrected_structures()), d.specie, d.temperature, d.time_step, d.step_skip, smoothed=d.smoothed, avg_nsteps=100, ) self.assertAlmostEqual(d.conductivity, 47.404056230438741, 4) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7) d.export_msdt("test.csv") with open("test.csv") as f: data = [] for row in csv.reader(f): if row: data.append(row) data.pop(0) data = np.array(data, dtype=np.float64) self.assertArrayAlmostEqual(data[:, 1], d.msd) self.assertArrayAlmostEqual(data[:, -1], d.mscd) os.remove("test.csv")
def test_init_npt(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open( os.path.join(PymatgenTest.TEST_FILES_DIR, "DiffusionAnalyzer_NPT.json"), "r") as f: dd = json.load(f) d = DiffusionAnalyzer.from_dict(dd) # large tolerance because scipy constants changed between 0.16.1 and 0.17 self.assertAlmostEqual(d.conductivity, 499.1504129387108, 4) self.assertAlmostEqual(d.chg_conductivity, 1219.5959181678043, 4) self.assertAlmostEqual(d.diffusivity, 8.40265434771e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 2.05305709033e-05, 6) self.assertAlmostEqual(d.conductivity_std_dev, 0.10368477696021029, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 9.1013023085561779e-09, 7) self.assertAlmostEqual(d.chg_diffusivity_std_dev, 1.20834853646e-08, 6) self.assertAlmostEqual(d.haven_ratio, 0.409275240679, 7) self.assertArrayAlmostEqual(d.conductivity_components, [455.178101, 602.252644, 440.0210014], 3) self.assertArrayAlmostEqual( d.diffusivity_components, [7.66242570e-06, 1.01382648e-05, 7.40727250e-06], ) self.assertArrayAlmostEqual(d.conductivity_components_std_dev, [0.1196577, 0.0973347, 0.1525400]) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [2.0143072e-09, 1.6385239e-09, 2.5678445e-09], ) self.assertArrayAlmostEqual( d.max_ion_displacements, [ 1.13147881, 0.79899554, 1.04153733, 0.96061850, 0.83039864, 0.70246715, 0.61365911, 0.67965179, 1.91973907, 1.69127386, 1.60568746, 1.35587641, 1.03280378, 0.99202692, 2.03359655, 1.03760269, 1.40228350, 1.36315080, 1.27414979, 1.26742035, 0.88199589, 0.97700804, 1.11323184, 1.00139511, 2.94164403, 0.89438909, 1.41508334, 1.23660358, 0.39322939, 0.54264064, 1.25291806, 0.62869809, 0.40846708, 1.43415505, 0.88891241, 0.56259128, 0.81712740, 0.52700441, 0.51011733, 0.55557882, 0.49131002, 0.66740277, 0.57798671, 0.63521025, 0.50277142, 0.52878021, 0.67803443, 0.81161269, 0.46486345, 0.47132761, 0.74301293, 0.79285519, 0.48789600, 0.61776836, 0.60695847, 0.67767756, 0.70972268, 1.08232442, 0.87871177, 0.84674206, 0.45694693, 0.60417985, 0.61652272, 0.66444583, 0.52211986, 0.56544134, 0.43311443, 0.43027547, 1.10730439, 0.59829728, 0.52270635, 0.72327608, 1.02919775, 0.84423208, 0.61694764, 0.72795752, 0.72957755, 0.55491631, 0.68507454, 0.76745343, 0.96346584, 0.66672645, 1.06810107, 0.65705843, ], ) self.assertEqual(d.sq_disp_ions.shape, (84, 217)) self.assertEqual(d.lattices.shape, (1001, 3, 3)) self.assertEqual(d.mscd.shape, (217, )) self.assertEqual(d.mscd.shape, d.msd.shape) self.assertAlmostEqual(d.max_framework_displacement, 1.43415505156) ss = list(d.get_drift_corrected_structures(10, 1000, 20)) self.assertEqual(len(ss), 50) n = random.randint(0, 49) n_orig = n * 20 + 10 self.assertArrayAlmostEqual( ss[n].cart_coords - d.structure.cart_coords + d.drift[:, n_orig, :], d.disp[:, n_orig, :], ) d = DiffusionAnalyzer.from_dict(d.as_dict()) self.assertIsInstance(d, DiffusionAnalyzer) # Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer( d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="max", ) self.assertAlmostEqual(d.conductivity, 499.1504129387108, 4) self.assertAlmostEqual(d.diffusivity, 8.40265434771e-06, 7) self.assertAlmostEqual(d.haven_ratio, 0.409275240679, 7) self.assertAlmostEqual(d.chg_diffusivity, 2.05305709033e-05, 7) d = DiffusionAnalyzer( d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False, ) self.assertAlmostEqual(d.conductivity, 406.5964019770787, 4) self.assertAlmostEqual(d.diffusivity, 6.8446082e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 1.03585877962e-05, 6) self.assertAlmostEqual(d.haven_ratio, 0.6607665413, 6) d = DiffusionAnalyzer( d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=100, ) self.assertAlmostEqual(d.conductivity, 425.77884571149525, 4) self.assertAlmostEqual(d.diffusivity, 7.167523809142514e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 9.33480892187e-06, 6) self.assertAlmostEqual(d.haven_ratio, 0.767827586952, 6) self.assertAlmostEqual(d.chg_conductivity, 554.5240271992852, 6) # Can't average over 2000 steps because this is a 1000-step run. self.assertRaises( ValueError, DiffusionAnalyzer, d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=2000, ) d = DiffusionAnalyzer.from_structures( list(d.get_drift_corrected_structures()), d.specie, d.temperature, d.time_step, d.step_skip, smoothed=d.smoothed, avg_nsteps=100, ) self.assertAlmostEqual(d.conductivity, 425.7788457114952, 4) self.assertAlmostEqual(d.diffusivity, 7.1675238091425148e-06, 7) self.assertAlmostEqual(d.haven_ratio, 0.767827586952, 7) self.assertAlmostEqual(d.chg_conductivity, 554.5240271992852, 6) d.export_msdt("test.csv") with open("test.csv") as f: data = [] for row in csv.reader(f): if row: data.append(row) data.pop(0) data = np.array(data, dtype=np.float64) self.assertArrayAlmostEqual(data[:, 1], d.msd) self.assertArrayAlmostEqual(data[:, -1], d.mscd) os.remove("test.csv")
#%% from pymatgen import Molecule, Structure, Lattice from pymatgen.analysis.diffusion_analyzer import DiffusionAnalyzer import os os.chdir('/home/jinho93/oxides/perobskite/lanthanum-aluminate/slab/gulp/stoichiometric/more') # os.chdir('/home/jinho93/new/oxides/perobskite/lanthanum-aluminate/slab/gulp/nostochio/La-vac') with open('lao.xyz') as f: lines = f.readlines() ss = [] atoms_number = 802 for i in range(len(lines) // atoms_number // 5): mole = Molecule.from_str(''.join(lines[atoms_number * i:atoms_number * (i + 1)]), fmt='xyz') l = Lattice([[15.2882,0,0], [0,15.2882, 0], [0,0,50]]) s = Structure(l, mole.species, mole.cart_coords, coords_are_cartesian=True) ss.append(s) from pymatgen.core import Element, Specie d = DiffusionAnalyzer.from_structures(ss, 'La', 300, len(ss) // 2, step_skip=2) d.get_msd_plot() #%% import numpy as np np.savetxt('msd.dat', d.dt)
def __init__(self, diffusion_analyzer: DiffusionAnalyzer, avg_nsteps: int = 50, ngrid: int = 101, rmax: float = 10.0, step_skip: int = 50, sigma: float = 0.1, cell_range: int = 1, species: Union[Tuple, List] = ("Li", "Na"), reference_species: Union[Tuple, List] = None, indices: List = None): """ Initiation. Args: diffusion_analyzer (DiffusionAnalyzer): A pymatgen.analysis.diffusion_analyzer.DiffusionAnalyzer object avg_nsteps (int): Number of t0 used for statistical average ngrid (int): Number of radial grid points rmax (float): Maximum of radial grid (the minimum is always set zero) step_skip (int): # of time steps skipped during analysis. It defines the resolution of the reduced time grid sigma (float): Smearing of a Gaussian function cell_range (int): Range of translational vector elements associated with supercell. Default is 1, i.e. including the adjacent image cells along all three directions. species ([string]): a list of specie symbols of interest. reference_species ([string]): Set this option along with 'species' parameter to calculate the distinct-part of van Hove function. Note that the self-part of van Hove function is always computed only for those in "species" parameter. indices (list of int): If not None, only a subset of atomic indices will be selected for the analysis. If this is given, "species" parameter will be ignored. """ # initial check if step_skip <= 0: raise ValueError("skip_step should be >=1!") n_ions, nsteps, ndim = diffusion_analyzer.disp.shape if nsteps <= avg_nsteps: raise ValueError("Number of timesteps is too small!") ntsteps = nsteps - avg_nsteps if ngrid - 1 <= 0: raise ValueError("Ntot should be greater than 1!") if sigma <= 0.0: raise ValueError("sigma should be > 0!") dr = rmax / (ngrid - 1) interval = np.linspace(0.0, rmax, ngrid) reduced_nt = int(ntsteps / float(step_skip)) + 1 lattice = diffusion_analyzer.structure.lattice structure = diffusion_analyzer.structure if indices is None: indices = [ j for j, site in enumerate(structure) if site.specie.symbol in species ] ref_indices = indices if reference_species: ref_indices = [ j for j, site in enumerate(structure) if site.specie.symbol in reference_species ] rho = float(len(indices)) / lattice.volume # reduced time grid rtgrid = np.arange(0.0, reduced_nt) # van Hove functions gsrt = np.zeros((reduced_nt, ngrid), dtype=np.double) gdrt = np.zeros((reduced_nt, ngrid), dtype=np.double) tracking_ions = [] ref_ions = [] # auxiliary factor for 4*\pi*r^2 aux_factor = 4.0 * np.pi * interval**2 aux_factor[0] = np.pi * dr**2 for i, ss in enumerate( diffusion_analyzer.get_drift_corrected_structures()): all_fcoords = np.array(ss.frac_coords) tracking_ions.append(all_fcoords[indices, :]) ref_ions.append(all_fcoords[ref_indices, :]) tracking_ions = np.array(tracking_ions) ref_ions = np.array(ref_ions) gaussians = norm.pdf(interval[:, None], interval[None, :], sigma) / float(avg_nsteps) / float( len(ref_indices)) # calculate self part of van Hove function image = np.array([0, 0, 0]) for it in range(reduced_nt): dns = Counter() it0 = min(it * step_skip, ntsteps) for it1 in range(avg_nsteps): dists = [ lattice.get_distance_and_image(tracking_ions[it1][u], tracking_ions[it0 + it1][u], jimage=image)[0] for u in range(len(indices)) ] dists = filter(lambda e: e < rmax, dists) r_indices = [int(dist / dr) for dist in dists] dns.update(r_indices) for indx, dn in dns.most_common(ngrid): gsrt[it, :] += gaussians[indx, :] * dn # calculate distinct part of van Hove function of species r = np.arange(-cell_range, cell_range + 1) arange = r[:, None] * np.array([1, 0, 0])[None, :] brange = r[:, None] * np.array([0, 1, 0])[None, :] crange = r[:, None] * np.array([0, 0, 1])[None, :] images = arange[:, None, None] + brange[None, :, None] + crange[None, None, :] images = images.reshape((len(r)**3, 3)) # find the zero image vector zd = np.sum(images**2, axis=1) indx0 = np.argmin(zd) for it in range(reduced_nt): dns = Counter() it0 = min(it * step_skip, ntsteps) for it1 in range(avg_nsteps): dcf = (tracking_ions[it0 + it1, :, None, None, :] + images[None, None, :, :] - ref_ions[it1, None, :, None, :]) dcc = lattice.get_cartesian_coords(dcf) d2 = np.sum(dcc**2, axis=3) dists = [ d2[u, v, j]**0.5 for u in range(len(indices)) for v in range(len(ref_indices)) for j in range(len(r)**3) if u != v or j != indx0 ] dists = filter(lambda e: e < rmax, dists) r_indices = [int(dist / dr) for dist in dists] dns.update(r_indices) for indx, dn in dns.most_common(ngrid): gdrt[it, :] += gaussians[indx, :] * dn / aux_factor[indx] / rho self.obj = diffusion_analyzer self.avg_nsteps = avg_nsteps self.step_skip = step_skip self.rtgrid = rtgrid self.interval = interval self.gsrt = gsrt self.gdrt = gdrt # time interval (in ps) in gsrt and gdrt. self.timeskip = self.obj.time_step * self.obj.step_skip * step_skip / 1000.0
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer.json")) as f: dd = json.load(f) d = DiffusionAnalyzer.from_dict(dd) self.assertAlmostEqual(d.conductivity, 74.165372208150615, 7) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertAlmostEqual(d.conductivity_std_dev, 0.0097244677795984488, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 9.1013023085561779e-09, 7) self.assertArrayAlmostEqual( d.conductivity_components, [45.9109701, 26.28563 , 150.5405718]) self.assertArrayAlmostEqual( d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06]) self.assertArrayAlmostEqual( d.conductivity_components_std_dev, [0.0063579, 0.0180862, 0.0217917] ) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [8.9465670e-09, 2.4931224e-08, 2.2636384e-08] ) self.assertArrayAlmostEqual( d.max_ion_displacements, [1.4620659693989553, 1.2787303484445025, 3.419618540097756, 2.340104469126246, 2.6080973517594233, 1.3928579365672844, 1.3561505956708932, 1.6699242923686253, 1.0352389639563648, 1.1662520093955808, 1.2322019205885841, 0.8094210554832534, 1.9917808504954169, 1.2684148391206396, 2.392633794162402, 2.566313049232671, 1.3175030435622759, 1.4628945430952793, 1.0984921286753002, 1.2864482076554093, 0.655567027815413, 0.5986961164605746, 0.5639091444309045, 0.6166004192954059, 0.5997911580422605, 0.4374606277579815, 1.1865683960470783, 0.9017064371676591, 0.6644840367853767, 1.0346375380664645, 0.6177630142863979, 0.7952002051914302, 0.7342686123054011, 0.7858047956905577, 0.5570732369065661, 1.0942937746885417, 0.6509372395308788, 1.0876687380413455, 0.7058162184725, 0.8298306317598585, 0.7813913747621343, 0.7337655232056153, 0.9057161616236746, 0.5979093093186919, 0.6830333586985015, 0.7926500894084628, 0.6765180009988608, 0.8555866032968998, 0.713087091642237, 0.7621007695790749]) self.assertEqual(d.sq_disp_ions.shape, (50, 206)) self.assertAlmostEqual(d.max_framework_displacement, 1.18656839605) ss = list(d.get_drift_corrected_structures()) self.assertEqual(len(ss), 1000) n = random.randint(0, 999) self.assertArrayAlmostEqual( ss[n].cart_coords - d.structure.cart_coords + d.drift[:, n, :], d.disp[:, n, :]) d = DiffusionAnalyzer.from_dict(d.as_dict()) self.assertIsInstance(d, DiffusionAnalyzer) #Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="max") self.assertAlmostEqual(d.conductivity, 74.16537220815061, 7) self.assertAlmostEqual(d.diffusivity, 1.14606446822e-06, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False) self.assertAlmostEqual(d.conductivity, 27.2047915553, 7) self.assertAlmostEqual(d.diffusivity, 4.25976905436e-07, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 47.404055971202155, 7) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7) # Can't average over 2000 steps because this is a 1000-step run. self.assertRaises(ValueError, DiffusionAnalyzer, d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=2000) d = DiffusionAnalyzer.from_structures( list(d.get_drift_corrected_structures()), d.specie, d.temperature, d.time_step, d.step_skip, d.smoothed, avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 47.404055971202155, 7) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7)
#%% from pymatgen.core.lattice import Lattice from pymatgen.io.xyz import XYZ from pymatgen import Structure from pymatgen.analysis.diffusion_analyzer import DiffusionAnalyzer import os import numpy as np os.chdir('/home/jinho93/oxides/wurtzite/zno/cp2k/1.aimd/3.16A/30/3.fix') x = XYZ.from_file('526.xyz') lat = Lattice(np.diag([16.02, 16.67, 40.67])) s = [ Structure(lat, r.species, r.cart_coords, coords_are_cartesian=True) for r in x.all_molecules ] #%% from pymatgen.analysis.diffusion_analyzer import DiffusionAnalyzer d = DiffusionAnalyzer.from_structures(s, 'Zn', 600, 500, 2) d.get_msd_plot()
分析AIMD结果,计算MSD 和 conductivity ''' import os from pymatgen.core.trajectory import Trajectory from pymatgen.io.vasp.outputs import Xdatcar from pymatgen import Structure from pymatgen.analysis.diffusion_analyzer import DiffusionAnalyzer import numpy as np import pickle # 这一步是读取 XDATCAR,得到一系列结构信息 traj = Trajectory.from_file('XDATCAR') # 这一步是实例化 DiffusionAnalyzer 的类 # 并用 from_structures 方法初始化这个类; 900 是温度,2 是POTIM 的值,1是间隔步数 # 间隔步数(step_skip)不太容易理解,但是根据官方教程: # dt = timesteps * self.time_step * self.step_skip diff = DiffusionAnalyzer.from_structures(traj,'Li',900,2,1) # 可以用内置的 plot_msd 方法画出 MSD 图像 # 有些终端不能显示图像,这时候可以调用 export_msdt() 方法,得到数据后再自己作图 diff.plot_msd() # 接下来直接得到 离子迁移率, 单位是 mS/cm C = diff.conductivity with open('result.dat','w') as f: f.write('# AIMD result for Li-ion\n') f.write('temp\tconductivity\n') f.write('%d\t%.2f\n' %(900,C))
def test_init_npt(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer_NPT.json"), 'r') as f: dd = json.load(f) d = DiffusionAnalyzer.from_dict(dd) # large tolerance because scipy constants changed between 0.16.1 and 0.17 self.assertAlmostEqual(d.conductivity, 499.15058192970508, 4) self.assertAlmostEqual(d.chg_conductivity, 1219.59633107, 4) self.assertAlmostEqual(d.diffusivity, 8.40265434771e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 2.05305709033e-05, 6) self.assertAlmostEqual(d.conductivity_std_dev, 0.10368477696021029, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 9.1013023085561779e-09, 7) self.assertAlmostEqual(d.chg_diffusivity_std_dev, 1.20834853646e-08, 6) self.assertAlmostEqual(d.haven_ratio, 0.409275240679, 7) self.assertArrayAlmostEqual( d.conductivity_components, [455.178101, 602.252644, 440.0210014], 3) self.assertArrayAlmostEqual( d.diffusivity_components, [7.66242570e-06, 1.01382648e-05, 7.40727250e-06]) self.assertArrayAlmostEqual( d.conductivity_components_std_dev, [0.1196577, 0.0973347, 0.1525400] ) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [2.0143072e-09, 1.6385239e-09, 2.5678445e-09] ) self.assertArrayAlmostEqual( d.max_ion_displacements, [1.13147881, 0.79899554, 1.04153733, 0.96061850, 0.83039864, 0.70246715, 0.61365911, 0.67965179, 1.91973907, 1.69127386, 1.60568746, 1.35587641, 1.03280378, 0.99202692, 2.03359655, 1.03760269, 1.40228350, 1.36315080, 1.27414979, 1.26742035, 0.88199589, 0.97700804, 1.11323184, 1.00139511, 2.94164403, 0.89438909, 1.41508334, 1.23660358, 0.39322939, 0.54264064, 1.25291806, 0.62869809, 0.40846708, 1.43415505, 0.88891241, 0.56259128, 0.81712740, 0.52700441, 0.51011733, 0.55557882, 0.49131002, 0.66740277, 0.57798671, 0.63521025, 0.50277142, 0.52878021, 0.67803443, 0.81161269, 0.46486345, 0.47132761, 0.74301293, 0.79285519, 0.48789600, 0.61776836, 0.60695847, 0.67767756, 0.70972268, 1.08232442, 0.87871177, 0.84674206, 0.45694693, 0.60417985, 0.61652272, 0.66444583, 0.52211986, 0.56544134, 0.43311443, 0.43027547, 1.10730439, 0.59829728, 0.52270635, 0.72327608, 1.02919775, 0.84423208, 0.61694764, 0.72795752, 0.72957755, 0.55491631, 0.68507454, 0.76745343, 0.96346584, 0.66672645, 1.06810107, 0.65705843]) self.assertEqual(d.sq_disp_ions.shape, (84, 217)) self.assertEqual(d.lattices.shape, (1001, 3, 3)) self.assertEqual(d.mscd.shape, (217,)) self.assertEqual(d.mscd.shape, d.msd.shape) self.assertAlmostEqual(d.max_framework_displacement, 1.43415505156) ss = list(d.get_drift_corrected_structures(10, 1000, 20)) self.assertEqual(len(ss), 50) n = random.randint(0, 49) n_orig = n * 20 + 10 self.assertArrayAlmostEqual( ss[n].cart_coords - d.structure.cart_coords + d.drift[:, n_orig, :], d.disp[:, n_orig, :]) d = DiffusionAnalyzer.from_dict(d.as_dict()) self.assertIsInstance(d, DiffusionAnalyzer) # Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="max") self.assertAlmostEqual(d.conductivity, 499.15058192970508, 4) self.assertAlmostEqual(d.diffusivity, 8.40265434771e-06, 7) self.assertAlmostEqual(d.haven_ratio, 0.409275240679, 7) self.assertAlmostEqual(d.chg_diffusivity, 2.05305709033e-05, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False) self.assertAlmostEqual(d.conductivity, 406.5965396, 4) self.assertAlmostEqual(d.diffusivity, 6.8446082e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 1.03585877962e-05, 6) self.assertAlmostEqual(d.haven_ratio, 0.6607665413, 6) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 425.7789898, 4) self.assertAlmostEqual(d.diffusivity, 7.167523809142514e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 9.33480892187e-06, 6) self.assertAlmostEqual(d.haven_ratio, 0.767827586952, 6) self.assertAlmostEqual(d.chg_conductivity, 554.524214937, 6) # Can't average over 2000 steps because this is a 1000-step run. self.assertRaises(ValueError, DiffusionAnalyzer, d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=2000) d = DiffusionAnalyzer.from_structures( list(d.get_drift_corrected_structures()), d.specie, d.temperature, d.time_step, d.step_skip, smoothed=d.smoothed, avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 425.77898986201302, 4) self.assertAlmostEqual(d.diffusivity, 7.1675238091425148e-06, 7) self.assertAlmostEqual(d.haven_ratio, 0.767827586952, 7) self.assertAlmostEqual(d.chg_conductivity, 554.524214937, 6) d.export_msdt("test.csv") with open("test.csv") as f: data = [] for row in csv.reader(f): if row: data.append(row) data.pop(0) data = np.array(data, dtype=np.float64) self.assertArrayAlmostEqual(data[:, 1], d.msd) self.assertArrayAlmostEqual(data[:, -1], d.mscd) os.remove("test.csv")
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer.json")) as f: dd = json.load(f) d = DiffusionAnalyzer.from_dict(dd) self.assertAlmostEqual(d.conductivity, 74.165372208150615, 7) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertAlmostEqual(d.conductivity_std_dev, 0.0097244677795984488, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 9.1013023085561779e-09, 7) self.assertArrayAlmostEqual(d.conductivity_components, [45.9109701, 26.28563, 150.5405718]) self.assertArrayAlmostEqual( d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06]) self.assertArrayAlmostEqual(d.conductivity_components_std_dev, [0.0063579, 0.0180862, 0.0217917]) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [8.9465670e-09, 2.4931224e-08, 2.2636384e-08]) self.assertArrayAlmostEqual(d.max_ion_displacements, [ 1.4620659693989553, 1.2787303484445025, 3.419618540097756, 2.340104469126246, 2.6080973517594233, 1.3928579365672844, 1.3561505956708932, 1.6699242923686253, 1.0352389639563648, 1.1662520093955808, 1.2322019205885841, 0.8094210554832534, 1.9917808504954169, 1.2684148391206396, 2.392633794162402, 2.566313049232671, 1.3175030435622759, 1.4628945430952793, 1.0984921286753002, 1.2864482076554093, 0.655567027815413, 0.5986961164605746, 0.5639091444309045, 0.6166004192954059, 0.5997911580422605, 0.4374606277579815, 1.1865683960470783, 0.9017064371676591, 0.6644840367853767, 1.0346375380664645, 0.6177630142863979, 0.7952002051914302, 0.7342686123054011, 0.7858047956905577, 0.5570732369065661, 1.0942937746885417, 0.6509372395308788, 1.0876687380413455, 0.7058162184725, 0.8298306317598585, 0.7813913747621343, 0.7337655232056153, 0.9057161616236746, 0.5979093093186919, 0.6830333586985015, 0.7926500894084628, 0.6765180009988608, 0.8555866032968998, 0.713087091642237, 0.7621007695790749 ]) self.assertEqual(d.sq_disp_ions.shape, (50, 206)) self.assertAlmostEqual(d.max_framework_displacement, 1.18656839605) ss = list(d.get_drift_corrected_structures()) self.assertEqual(len(ss), 1000) n = random.randint(0, 999) self.assertArrayAlmostEqual( ss[n].cart_coords - d.structure.cart_coords + d.drift[:, n, :], d.disp[:, n, :]) d = DiffusionAnalyzer.from_dict(d.as_dict()) self.assertIsInstance(d, DiffusionAnalyzer) #Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="max") self.assertAlmostEqual(d.conductivity, 74.16537220815061, 7) self.assertAlmostEqual(d.diffusivity, 1.14606446822e-06, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False) self.assertAlmostEqual(d.conductivity, 27.2047915553, 7) self.assertAlmostEqual(d.diffusivity, 4.25976905436e-07, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 47.404055971202155, 7) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7) # Can't average over 2000 steps because this is a 1000-step run. self.assertRaises(ValueError, DiffusionAnalyzer, d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=2000) d = DiffusionAnalyzer.from_structures(list( d.get_drift_corrected_structures()), d.specie, d.temperature, d.time_step, d.step_skip, d.smoothed, avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 47.404055971202155, 7) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7)
def test_init(self): # Diffusion vasprun.xmls are rather large. We are only going to use a # very small preprocessed run for testing. Note that the results are # unreliable for short runs. with open(os.path.join(test_dir, "DiffusionAnalyzer.json")) as f: dd = json.load(f) d = DiffusionAnalyzer.from_dict(dd) # large tolerance because scipy constants changed between 0.16.1 and 0.17 self.assertAlmostEqual(d.conductivity, 74.165372613735684, 4) self.assertAlmostEqual(d.chg_conductivity, 232.827958801, 4) self.assertAlmostEqual(d.diffusivity, 1.16083658794e-06, 7) self.assertAlmostEqual(d.chg_diffusivity, 3.64565578208e-06, 7) self.assertAlmostEqual(d.conductivity_std_dev, 0.0097244677795984488, 7) self.assertAlmostEqual(d.diffusivity_std_dev, 9.1013023085561779e-09, 7) self.assertAlmostEqual(d.chg_diffusivity_std_dev, 7.20911399729e-10, 5) self.assertAlmostEqual(d.haven_ratio, 0.31854161048867402, 7) self.assertArrayAlmostEqual( d.conductivity_components, [45.7903694, 26.1651956, 150.5406140], 3) self.assertArrayAlmostEqual( d.diffusivity_components, [7.49601236e-07, 4.90254273e-07, 2.24649255e-06]) self.assertArrayAlmostEqual( d.conductivity_components_std_dev, [0.0063566, 0.0180854, 0.0217918] ) self.assertArrayAlmostEqual( d.diffusivity_components_std_dev, [8.9465670e-09, 2.4931224e-08, 2.2636384e-08] ) self.assertArrayAlmostEqual( d.mscd[0:4], [0.69131064, 0.71794072, 0.74315283, 0.76703961] ) self.assertArrayAlmostEqual( d.max_ion_displacements, [1.4620659693989553, 1.2787303484445025, 3.419618540097756, 2.340104469126246, 2.6080973517594233, 1.3928579365672844, 1.3561505956708932, 1.6699242923686253, 1.0352389639563648, 1.1662520093955808, 1.2322019205885841, 0.8094210554832534, 1.9917808504954169, 1.2684148391206396, 2.392633794162402, 2.566313049232671, 1.3175030435622759, 1.4628945430952793, 1.0984921286753002, 1.2864482076554093, 0.655567027815413, 0.5986961164605746, 0.5639091444309045, 0.6166004192954059, 0.5997911580422605, 0.4374606277579815, 1.1865683960470783, 0.9017064371676591, 0.6644840367853767, 1.0346375380664645, 0.6177630142863979, 0.7952002051914302, 0.7342686123054011, 0.7858047956905577, 0.5570732369065661, 1.0942937746885417, 0.6509372395308788, 1.0876687380413455, 0.7058162184725, 0.8298306317598585, 0.7813913747621343, 0.7337655232056153, 0.9057161616236746, 0.5979093093186919, 0.6830333586985015, 0.7926500894084628, 0.6765180009988608, 0.8555866032968998, 0.713087091642237, 0.7621007695790749]) self.assertEqual(d.sq_disp_ions.shape, (50, 206)) self.assertEqual(d.lattices.shape, (1, 3, 3)) self.assertEqual(d.mscd.shape, (206,)) self.assertEqual(d.mscd.shape, d.msd.shape) self.assertAlmostEqual(d.max_framework_displacement, 1.18656839605) ss = list(d.get_drift_corrected_structures(10, 1000, 20)) self.assertEqual(len(ss), 50) n = random.randint(0, 49) n_orig = n * 20 + 10 self.assertArrayAlmostEqual( ss[n].cart_coords - d.structure.cart_coords + d.drift[:, n_orig, :], d.disp[:, n_orig, :]) d = DiffusionAnalyzer.from_dict(d.as_dict()) self.assertIsInstance(d, DiffusionAnalyzer) #Ensure summary dict is json serializable. json.dumps(d.get_summary_dict(include_msd_t=True)) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="max") self.assertAlmostEqual(d.conductivity, 74.165372613735684, 4) self.assertAlmostEqual(d.diffusivity, 1.14606446822e-06, 7) self.assertAlmostEqual(d.haven_ratio, 0.318541610489, 6) self.assertAlmostEqual(d.chg_conductivity, 232.827958801, 4) self.assertAlmostEqual(d.chg_diffusivity, 3.64565578208e-06, 7) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed=False) self.assertAlmostEqual(d.conductivity, 27.20479170406027, 4) self.assertAlmostEqual(d.diffusivity, 4.25976905436e-07, 7) self.assertAlmostEqual(d.chg_diffusivity, 1.6666666666666667e-17, 3) d = DiffusionAnalyzer(d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 47.404056230438741, 4) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7) self.assertAlmostEqual(d.chg_conductivity, 1.06440821953e-09, 4) # Can't average over 2000 steps because this is a 1000-step run. self.assertRaises(ValueError, DiffusionAnalyzer, d.structure, d.disp, d.specie, d.temperature, d.time_step, d.step_skip, smoothed="constant", avg_nsteps=2000) d = DiffusionAnalyzer.from_structures( list(d.get_drift_corrected_structures()), d.specie, d.temperature, d.time_step, d.step_skip, smoothed=d.smoothed, avg_nsteps=100) self.assertAlmostEqual(d.conductivity, 47.404056230438741, 4) self.assertAlmostEqual(d.diffusivity, 7.4226016496716148e-07, 7) d.export_msdt("test.csv") with open("test.csv") as f: data = [] for row in csv.reader(f): if row: data.append(row) data.pop(0) data = np.array(data, dtype=np.float64) self.assertArrayAlmostEqual(data[:, 1], d.msd) self.assertArrayAlmostEqual(data[:, -1], d.mscd) os.remove("test.csv")
temperatures = [500, 600, 700, 800, 900, 1000] tetrels = ["Ge", "Sn"] for i in temperatures: for j in tetrels: x1 = Xdatcar(f'{i}K/{j}/1/XDATCAR') x2 = Xdatcar(f'{i}K/{j}/2/XDATCAR') x3 = Xdatcar(f'{i}K/{j}/3/XDATCAR') x4 = Xdatcar(f'{i}K/{j}/4/XDATCAR') x5 = Xdatcar(f'{i}K/{j}/5/XDATCAR') structures = x1.structures + x2.structures + x3.structures + x4.structures + x5.structures s500K = [ "500/Sn/1/vasprun.xml", "500/Sn/2/vasprun.xml", "500/Sn/3/vasprun.xml" ] d500K = DiffusionAnalyzer.from_files(s500K, specie="Na", smoothed=max) s600K = [ "600/Sn/1/vasprun.xml", "600/Sn/2/vasprun.xml", "600/Sn/3/vasprun.xml" ] d600K = DiffusionAnalyzer.from_files(s600K, specie="Na", smoothed=max) s700K = [ "700/Sn/1/vasprun.xml", "700/Sn/2/vasprun.xml", "700/Sn/3/vasprun.xml" ] d700K = DiffusionAnalyzer.from_files(s700K, specie="Na", smoothed=max) s800K = [ "800/Sn/1/vasprun.xml", "800/Sn/2/vasprun.xml", "800/Sn/3/vasprun.xml" ] d800K = DiffusionAnalyzer.from_files(s800K, specie="Na", smoothed=max) s900K = [ "900/Sn/1/vasprun.xml", "900/Sn/2/vasprun.xml", "900/Sn/3/vasprun.xml" ]