예제 #1
0
    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")
예제 #2
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)
            # 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")
예제 #3
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(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")
예제 #4
0
    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")
예제 #5
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)
예제 #6
0
    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
예제 #7
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)