def write_cif( filename: str, struct: Structure, refine_cell=False, resize_volume=False, symprec=1e-2, comment="", ): """ dump structure in CIF format after resizing to feasible volume and refing cell by symmetry. Parameters ---------- filename: str struct: pymatgen.core.Structure refine_cell: bool, optional if true, refine cell setting by spglib resize_volume: bool, optional if true, resize lattice by DLSVolumePredictor in pymatgen symprec: float, optional symprec in spglib """ struct = refine_and_resize_structure(struct, refine_cell, resize_volume) cw = CifWriter(struct, symprec=symprec) comment = f"# generated by {__version__}\n" + comment with open(filename, "w") as f: if comment: f.write(comment + "\n" + cw.__str__()) else: f.write(cw.__str__())
def pre_save_post_validation(cls, sender, document, **kwargs): from mpcontribs.api.structures.views import StructuresResource resource = StructuresResource() d = resource.serialize(document, fields=["lattice", "sites", "charge"]) s = json.dumps(d, sort_keys=True).encode("utf-8") document.md5 = md5(s).hexdigest() structure = Structure.from_dict(d) try: writer = CifWriter(structure, symprec=1e-10) except TypeError: # save CIF string without symmetry information writer = CifWriter(structure) document.cif = writer.__str__()
def test_write(self): cw_ref_string = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd0 1 0.31746000 0.81746000 0.00000000 1.0 Gd Gd1 1 0.18254000 0.31746000 0.00000000 1.0 Gd Gd2 1 0.81746000 0.68254000 0.00000000 1.0 Gd Gd3 1 0.68254000 0.18254000 0.00000000 1.0 B B4 1 0.00000000 0.00000000 0.20290000 1.0 B B5 1 0.50000000 0.50000000 0.79710000 1.0 B B6 1 0.00000000 0.00000000 0.79710000 1.0 B B7 1 0.50000000 0.50000000 0.20290000 1.0 B B8 1 0.17590000 0.03800000 0.50000000 1.0 B B9 1 0.96200000 0.17590000 0.50000000 1.0 B B10 1 0.03800000 0.82410000 0.50000000 1.0 B B11 1 0.67590000 0.46200000 0.50000000 1.0 B B12 1 0.32410000 0.53800000 0.50000000 1.0 B B13 1 0.82410000 0.96200000 0.50000000 1.0 B B14 1 0.53800000 0.67590000 0.50000000 1.0 B B15 1 0.46200000 0.32410000 0.50000000 1.0 B B16 1 0.08670000 0.58670000 0.50000000 1.0 B B17 1 0.41330000 0.08670000 0.50000000 1.0 B B18 1 0.58670000 0.91330000 0.50000000 1.0 B B19 1 0.91330000 0.41330000 0.50000000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd0 5.05000000 5.05000000 0.00000000 Gd1 -5.05000000 5.05000000 0.00000000 Gd2 5.05000000 -5.05000000 0.00000000 Gd3 -5.05000000 -5.05000000 0.00000000 """ s_ncl = self.mcif_ncl.get_structures(primitive=False)[0] cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) # from list-type magmoms list_magmoms = [list(m) for m in s_ncl.site_properties['magmom']] # float magmoms (magnitude only) float_magmoms = [float(m) for m in s_ncl.site_properties['magmom']] s_ncl.add_site_property('magmom', list_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) s_ncl.add_site_property('magmom', float_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) cw_ref_string_magnitudes = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd0 1 0.31746000 0.81746000 0.00000000 1.0 Gd Gd1 1 0.18254000 0.31746000 0.00000000 1.0 Gd Gd2 1 0.81746000 0.68254000 0.00000000 1.0 Gd Gd3 1 0.68254000 0.18254000 0.00000000 1.0 B B4 1 0.00000000 0.00000000 0.20290000 1.0 B B5 1 0.50000000 0.50000000 0.79710000 1.0 B B6 1 0.00000000 0.00000000 0.79710000 1.0 B B7 1 0.50000000 0.50000000 0.20290000 1.0 B B8 1 0.17590000 0.03800000 0.50000000 1.0 B B9 1 0.96200000 0.17590000 0.50000000 1.0 B B10 1 0.03800000 0.82410000 0.50000000 1.0 B B11 1 0.67590000 0.46200000 0.50000000 1.0 B B12 1 0.32410000 0.53800000 0.50000000 1.0 B B13 1 0.82410000 0.96200000 0.50000000 1.0 B B14 1 0.53800000 0.67590000 0.50000000 1.0 B B15 1 0.46200000 0.32410000 0.50000000 1.0 B B16 1 0.08670000 0.58670000 0.50000000 1.0 B B17 1 0.41330000 0.08670000 0.50000000 1.0 B B18 1 0.58670000 0.91330000 0.50000000 1.0 B B19 1 0.91330000 0.41330000 0.50000000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd0 0.00000000 0.00000000 7.14177849 Gd1 0.00000000 0.00000000 7.14177849 Gd2 0.00000000 0.00000000 -7.14177849 Gd3 0.00000000 0.00000000 -7.14177849 """ self.assertEqual(cw.__str__().strip(), cw_ref_string_magnitudes.strip()) # test we're getting correct magmoms in ncl case s_ncl2 = self.mcif_ncl2.get_structures()[0] list_magmoms = [list(m) for m in s_ncl2.site_properties['magmom']] self.assertEqual(list_magmoms[0][0], 0.0) self.assertAlmostEqual(list_magmoms[0][1], 5.9160793408726366) self.assertAlmostEqual(list_magmoms[1][0], -5.1234749999999991) self.assertAlmostEqual(list_magmoms[1][1], 2.9580396704363183) # test creating an structure without oxidation state doesn't raise errors s_manual = Structure(Lattice.cubic(4.2), ["Cs", "Cl"], [[0, 0, 0], [0.5, 0.5, 0.5]]) s_manual.add_spin_by_site([1, -1]) cw = CifWriter(s_manual, write_magmoms=True) # check oxidation state cw_manual_oxi_string = """# generated using pymatgen data_CsCl _symmetry_space_group_name_H-M 'P 1' _cell_length_a 4.20000000 _cell_length_b 4.20000000 _cell_length_c 4.20000000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural CsCl _chemical_formula_sum 'Cs1 Cl1' _cell_volume 74.08800000 _cell_formula_units_Z 1 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_type_symbol _atom_type_oxidation_number Cs+ 1.0 Cl+ 1.0 loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Cs+ Cs0 1 0.00000000 0.00000000 0.00000000 1 Cl+ Cl1 1 0.50000000 0.50000000 0.50000000 1 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z """ s_manual.add_oxidation_state_by_site([1, 1]) cw = CifWriter(s_manual, write_magmoms=True) self.assertEqual(cw.__str__(), cw_manual_oxi_string)
def get_string(self) -> str: cw = CifWriter(self.structure) return cw.__str__()
def test_write(self): cw_ref_string = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd1 1 0.317460 0.817460 0.000000 1.0 Gd Gd2 1 0.182540 0.317460 0.000000 1.0 Gd Gd3 1 0.817460 0.682540 0.000000 1.0 Gd Gd4 1 0.682540 0.182540 0.000000 1.0 B B5 1 0.000000 0.000000 0.202900 1.0 B B6 1 0.500000 0.500000 0.797100 1.0 B B7 1 0.000000 0.000000 0.797100 1.0 B B8 1 0.500000 0.500000 0.202900 1.0 B B9 1 0.175900 0.038000 0.500000 1.0 B B10 1 0.962000 0.175900 0.500000 1.0 B B11 1 0.038000 0.824100 0.500000 1.0 B B12 1 0.675900 0.462000 0.500000 1.0 B B13 1 0.324100 0.538000 0.500000 1.0 B B14 1 0.824100 0.962000 0.500000 1.0 B B15 1 0.538000 0.675900 0.500000 1.0 B B16 1 0.462000 0.324100 0.500000 1.0 B B17 1 0.086700 0.586700 0.500000 1.0 B B18 1 0.413300 0.086700 0.500000 1.0 B B19 1 0.586700 0.913300 0.500000 1.0 B B20 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd1 5.05000 5.05000 0.00000 Gd2 -5.05000 5.05000 0.00000 Gd3 5.05000 -5.05000 0.00000 Gd4 -5.05000 -5.05000 0.00000 """ s_ncl = self.mcif_ncl.get_structures(primitive=False)[0] cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) # from list-type magmoms list_magmoms = [list(m) for m in s_ncl.site_properties['magmom']] # float magmoms (magnitude only) float_magmoms = [float(m) for m in s_ncl.site_properties['magmom']] s_ncl.add_site_property('magmom', list_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) s_ncl.add_site_property('magmom', float_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) cw_ref_string_magnitudes = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd1 1 0.317460 0.817460 0.000000 1.0 Gd Gd2 1 0.182540 0.317460 0.000000 1.0 Gd Gd3 1 0.817460 0.682540 0.000000 1.0 Gd Gd4 1 0.682540 0.182540 0.000000 1.0 B B5 1 0.000000 0.000000 0.202900 1.0 B B6 1 0.500000 0.500000 0.797100 1.0 B B7 1 0.000000 0.000000 0.797100 1.0 B B8 1 0.500000 0.500000 0.202900 1.0 B B9 1 0.175900 0.038000 0.500000 1.0 B B10 1 0.962000 0.175900 0.500000 1.0 B B11 1 0.038000 0.824100 0.500000 1.0 B B12 1 0.675900 0.462000 0.500000 1.0 B B13 1 0.324100 0.538000 0.500000 1.0 B B14 1 0.824100 0.962000 0.500000 1.0 B B15 1 0.538000 0.675900 0.500000 1.0 B B16 1 0.462000 0.324100 0.500000 1.0 B B17 1 0.086700 0.586700 0.500000 1.0 B B18 1 0.413300 0.086700 0.500000 1.0 B B19 1 0.586700 0.913300 0.500000 1.0 B B20 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd1 0.00000 0.00000 7.14178 Gd2 0.00000 0.00000 7.14178 Gd3 0.00000 0.00000 -7.14178 Gd4 0.00000 0.00000 -7.14178 """ self.assertEqual(cw.__str__(), cw_ref_string_magnitudes) # test we're getting correct magmoms in ncl case s_ncl2 = self.mcif_ncl2.get_structures()[0] list_magmoms = [list(m) for m in s_ncl2.site_properties['magmom']] self.assertEqual(list_magmoms[0][0], 0.0) self.assertAlmostEqual(list_magmoms[0][1], 5.9160793408726366) self.assertAlmostEqual(list_magmoms[1][0], -5.1234749999999991) self.assertAlmostEqual(list_magmoms[1][1], 2.9580396704363183)
def test_write(self): cw_ref_string = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd0 1 0.317460 0.817460 0.000000 1.0 Gd Gd1 1 0.182540 0.317460 0.000000 1.0 Gd Gd2 1 0.817460 0.682540 0.000000 1.0 Gd Gd3 1 0.682540 0.182540 0.000000 1.0 B B4 1 0.000000 0.000000 0.202900 1.0 B B5 1 0.500000 0.500000 0.797100 1.0 B B6 1 0.000000 0.000000 0.797100 1.0 B B7 1 0.500000 0.500000 0.202900 1.0 B B8 1 0.175900 0.038000 0.500000 1.0 B B9 1 0.962000 0.175900 0.500000 1.0 B B10 1 0.038000 0.824100 0.500000 1.0 B B11 1 0.675900 0.462000 0.500000 1.0 B B12 1 0.324100 0.538000 0.500000 1.0 B B13 1 0.824100 0.962000 0.500000 1.0 B B14 1 0.538000 0.675900 0.500000 1.0 B B15 1 0.462000 0.324100 0.500000 1.0 B B16 1 0.086700 0.586700 0.500000 1.0 B B17 1 0.413300 0.086700 0.500000 1.0 B B18 1 0.586700 0.913300 0.500000 1.0 B B19 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd0 5.05000 5.05000 0.00000 Gd1 -5.05000 5.05000 0.00000 Gd2 5.05000 -5.05000 0.00000 Gd3 -5.05000 -5.05000 0.00000 """ s_ncl = self.mcif_ncl.get_structures(primitive=False)[0] cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) # from list-type magmoms list_magmoms = [list(m) for m in s_ncl.site_properties['magmom']] # float magmoms (magnitude only) float_magmoms = [float(m) for m in s_ncl.site_properties['magmom']] s_ncl.add_site_property('magmom', list_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) s_ncl.add_site_property('magmom', float_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) cw_ref_string_magnitudes = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd0 1 0.317460 0.817460 0.000000 1.0 Gd Gd1 1 0.182540 0.317460 0.000000 1.0 Gd Gd2 1 0.817460 0.682540 0.000000 1.0 Gd Gd3 1 0.682540 0.182540 0.000000 1.0 B B4 1 0.000000 0.000000 0.202900 1.0 B B5 1 0.500000 0.500000 0.797100 1.0 B B6 1 0.000000 0.000000 0.797100 1.0 B B7 1 0.500000 0.500000 0.202900 1.0 B B8 1 0.175900 0.038000 0.500000 1.0 B B9 1 0.962000 0.175900 0.500000 1.0 B B10 1 0.038000 0.824100 0.500000 1.0 B B11 1 0.675900 0.462000 0.500000 1.0 B B12 1 0.324100 0.538000 0.500000 1.0 B B13 1 0.824100 0.962000 0.500000 1.0 B B14 1 0.538000 0.675900 0.500000 1.0 B B15 1 0.462000 0.324100 0.500000 1.0 B B16 1 0.086700 0.586700 0.500000 1.0 B B17 1 0.413300 0.086700 0.500000 1.0 B B18 1 0.586700 0.913300 0.500000 1.0 B B19 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd0 0.00000 0.00000 7.14178 Gd1 0.00000 0.00000 7.14178 Gd2 0.00000 0.00000 -7.14178 Gd3 0.00000 0.00000 -7.14178 """ self.assertEqual(cw.__str__(), cw_ref_string_magnitudes) # test we're getting correct magmoms in ncl case s_ncl2 = self.mcif_ncl2.get_structures()[0] list_magmoms = [list(m) for m in s_ncl2.site_properties['magmom']] self.assertEqual(list_magmoms[0][0], 0.0) self.assertAlmostEqual(list_magmoms[0][1], 5.9160793408726366) self.assertAlmostEqual(list_magmoms[1][0], -5.1234749999999991) self.assertAlmostEqual(list_magmoms[1][1], 2.9580396704363183) # test creating an structure without oxidation state doesn't raise errors s_manual = Structure(Lattice.cubic(4.2), ["Cs", "Cl"],[[0, 0, 0], [0.5, 0.5, 0.5]]) s_manual.add_spin_by_site([1, -1]) cw = CifWriter(s_manual, write_magmoms=True) # check oxidation state cw_manual_oxi_string = """# generated using pymatgen data_CsCl _symmetry_space_group_name_H-M 'P 1' _cell_length_a 4.20000000 _cell_length_b 4.20000000 _cell_length_c 4.20000000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural CsCl _chemical_formula_sum 'Cs1 Cl1' _cell_volume 74.08800000 _cell_formula_units_Z 1 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_type_symbol _atom_type_oxidation_number Cs+ 1.0 Cl+ 1.0 loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Cs+ Cs0 1 0.000000 0.000000 0.000000 1 Cl+ Cl1 1 0.500000 0.500000 0.500000 1 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z """ s_manual.add_oxidation_state_by_site([1,1]) cw = CifWriter(s_manual, write_magmoms=True) self.assertEqual(cw.__str__(), cw_manual_oxi_string)
def test_symmetrized(self): filepath = os.path.join(test_dir, 'POSCAR') poscar = Poscar.from_file(filepath) writer = CifWriter(poscar.structure, symprec=0.1) ans = """# generated using pymatgen data_FePO4 _symmetry_space_group_name_H-M Pnma _cell_length_a 10.41176687 _cell_length_b 6.06717188 _cell_length_c 4.75948954 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 62 _chemical_formula_structural FePO4 _chemical_formula_sum 'Fe4 P4 O16' _cell_volume 300.65685512 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' 2 '-x, -y, -z' 3 '-x+1/2, -y, z+1/2' 4 'x+1/2, y, -z+1/2' 5 'x+1/2, -y+1/2, -z+1/2' 6 '-x+1/2, y+1/2, z+1/2' 7 '-x, y+1/2, -z' 8 'x, -y+1/2, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Fe Fe1 4 0.218728 0.750000 0.474867 1 P P2 4 0.094613 0.250000 0.418243 1 O O3 8 0.165710 0.046072 0.285384 1 O O4 4 0.043372 0.750000 0.707138 1 O O5 4 0.096642 0.250000 0.741320 1""" for l1, l2 in zip(str(writer).split("\n"), ans.split("\n")): self.assertEqual(l1.strip(), l2.strip()) ans = """# generated using pymatgen data_LiFePO4 _symmetry_space_group_name_H-M Pnma _cell_length_a 4.74480000 _cell_length_b 6.06577000 _cell_length_c 10.41037000 _cell_angle_alpha 90.50179000 _cell_angle_beta 90.00019000 _cell_angle_gamma 90.00362000 _symmetry_Int_Tables_number 62 _chemical_formula_structural LiFePO4 _chemical_formula_sum 'Li4 Fe4 P4 O16' _cell_volume 299.607967711 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' 2 '-x, -y, -z' 3 'x+1/2, -y, -z+1/2' 4 '-x+1/2, y, z+1/2' 5 '-x+1/2, -y+1/2, z+1/2' 6 'x+1/2, y+1/2, -z+1/2' 7 '-x, y+1/2, -z' 8 'x, -y+1/2, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Li Li1 4 0.000010 0.500000 0.999990 1.0 Fe Fe2 4 0.025030 0.746540 0.281160 1.0 P P3 4 0.082060 0.248260 0.405570 1.0 O O4 8 0.213420 0.043980 0.334230 1.0 O O5 4 0.208430 0.251100 0.543180 1.0 O O6 4 0.241480 0.750450 0.596220 1.0 """ s = Structure.from_file(os.path.join(test_dir, 'LiFePO4.cif')) writer = CifWriter(s, symprec=0.1) self.assertEqual(writer.__str__().strip(), ans.strip())
def test_write(self): s_ncl = self.mcif_ncl.get_structures(primitive=False)[0] cw = CifWriter(s_ncl, write_magmoms=True) cw_ref_string = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.007290027 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd1 1 0.317460 0.817460 0.000000 1.0 Gd Gd2 1 0.182540 0.317460 0.000000 1.0 Gd Gd3 1 0.817460 0.682540 0.000000 1.0 Gd Gd4 1 0.682540 0.182540 0.000000 1.0 B B5 1 0.000000 0.000000 0.202900 1.0 B B6 1 0.500000 0.500000 0.797100 1.0 B B7 1 0.000000 0.000000 0.797100 1.0 B B8 1 0.500000 0.500000 0.202900 1.0 B B9 1 0.175900 0.038000 0.500000 1.0 B B10 1 0.962000 0.175900 0.500000 1.0 B B11 1 0.038000 0.824100 0.500000 1.0 B B12 1 0.675900 0.462000 0.500000 1.0 B B13 1 0.324100 0.538000 0.500000 1.0 B B14 1 0.824100 0.962000 0.500000 1.0 B B15 1 0.538000 0.675900 0.500000 1.0 B B16 1 0.462000 0.324100 0.500000 1.0 B B17 1 0.086700 0.586700 0.500000 1.0 B B18 1 0.413300 0.086700 0.500000 1.0 B B19 1 0.586700 0.913300 0.500000 1.0 B B20 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd1 5.05 5.05 0.0 Gd2 -5.05 5.05 0.0 Gd3 5.05 -5.05 0.0 Gd4 -5.05 -5.05 0.0 """ self.assertEqual(cw.__str__(), cw_ref_string)
def test_write(self): cw_ref_string = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd1 1 0.317460 0.817460 0.000000 1.0 Gd Gd2 1 0.182540 0.317460 0.000000 1.0 Gd Gd3 1 0.817460 0.682540 0.000000 1.0 Gd Gd4 1 0.682540 0.182540 0.000000 1.0 B B5 1 0.000000 0.000000 0.202900 1.0 B B6 1 0.500000 0.500000 0.797100 1.0 B B7 1 0.000000 0.000000 0.797100 1.0 B B8 1 0.500000 0.500000 0.202900 1.0 B B9 1 0.175900 0.038000 0.500000 1.0 B B10 1 0.962000 0.175900 0.500000 1.0 B B11 1 0.038000 0.824100 0.500000 1.0 B B12 1 0.675900 0.462000 0.500000 1.0 B B13 1 0.324100 0.538000 0.500000 1.0 B B14 1 0.824100 0.962000 0.500000 1.0 B B15 1 0.538000 0.675900 0.500000 1.0 B B16 1 0.462000 0.324100 0.500000 1.0 B B17 1 0.086700 0.586700 0.500000 1.0 B B18 1 0.413300 0.086700 0.500000 1.0 B B19 1 0.586700 0.913300 0.500000 1.0 B B20 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd1 5.05000 5.05000 0.00000 Gd2 -5.05000 5.05000 0.00000 Gd3 5.05000 -5.05000 0.00000 Gd4 -5.05000 -5.05000 0.00000 """ s_ncl = self.mcif_ncl.get_structures(primitive=False)[0] cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) # from list-type magmoms list_magmoms = [list(m) for m in s_ncl.site_properties['magmom']] # float magmoms (magnitude only) float_magmoms = [float(m) for m in s_ncl.site_properties['magmom']] s_ncl.add_site_property('magmom', list_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) self.assertEqual(cw.__str__(), cw_ref_string) s_ncl.add_site_property('magmom', float_magmoms) cw = CifWriter(s_ncl, write_magmoms=True) cw_ref_string_magnitudes = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.00729003 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd1 1 0.317460 0.817460 0.000000 1.0 Gd Gd2 1 0.182540 0.317460 0.000000 1.0 Gd Gd3 1 0.817460 0.682540 0.000000 1.0 Gd Gd4 1 0.682540 0.182540 0.000000 1.0 B B5 1 0.000000 0.000000 0.202900 1.0 B B6 1 0.500000 0.500000 0.797100 1.0 B B7 1 0.000000 0.000000 0.797100 1.0 B B8 1 0.500000 0.500000 0.202900 1.0 B B9 1 0.175900 0.038000 0.500000 1.0 B B10 1 0.962000 0.175900 0.500000 1.0 B B11 1 0.038000 0.824100 0.500000 1.0 B B12 1 0.675900 0.462000 0.500000 1.0 B B13 1 0.324100 0.538000 0.500000 1.0 B B14 1 0.824100 0.962000 0.500000 1.0 B B15 1 0.538000 0.675900 0.500000 1.0 B B16 1 0.462000 0.324100 0.500000 1.0 B B17 1 0.086700 0.586700 0.500000 1.0 B B18 1 0.413300 0.086700 0.500000 1.0 B B19 1 0.586700 0.913300 0.500000 1.0 B B20 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd1 0.00000 0.00000 7.14178 Gd2 0.00000 0.00000 7.14178 Gd3 0.00000 0.00000 -7.14178 Gd4 0.00000 0.00000 -7.14178 """ self.assertEqual(cw.__str__(), cw_ref_string_magnitudes) # test we're getting correct magmoms in ncl case s_ncl2 = self.mcif_ncl2.get_structures()[0] list_magmoms = [list(m) for m in s_ncl2.site_properties['magmom']] self.assertEqual(list_magmoms[0][0], 0.0) self.assertAlmostEqual(list_magmoms[0][1], 5.9160793408726366) self.assertAlmostEqual(list_magmoms[1][0], -5.1234749999999991) self.assertAlmostEqual(list_magmoms[1][1], 2.9580396704363183)
def test_write(self): s_ncl = self.mcif_ncl.get_structures(primitive=False)[0] cw = CifWriter(s_ncl, write_magmoms=True) cw_ref_string = """# generated using pymatgen data_GdB4 _symmetry_space_group_name_H-M 'P 1' _cell_length_a 7.13160000 _cell_length_b 7.13160000 _cell_length_c 4.05050000 _cell_angle_alpha 90.00000000 _cell_angle_beta 90.00000000 _cell_angle_gamma 90.00000000 _symmetry_Int_Tables_number 1 _chemical_formula_structural GdB4 _chemical_formula_sum 'Gd4 B16' _cell_volume 206.007290027 _cell_formula_units_Z 4 loop_ _symmetry_equiv_pos_site_id _symmetry_equiv_pos_as_xyz 1 'x, y, z' loop_ _atom_site_type_symbol _atom_site_label _atom_site_symmetry_multiplicity _atom_site_fract_x _atom_site_fract_y _atom_site_fract_z _atom_site_occupancy Gd Gd1 1 0.317460 0.817460 0.000000 1.0 Gd Gd2 1 0.182540 0.317460 0.000000 1.0 Gd Gd3 1 0.817460 0.682540 0.000000 1.0 Gd Gd4 1 0.682540 0.182540 0.000000 1.0 B B5 1 0.000000 0.000000 0.202900 1.0 B B6 1 0.500000 0.500000 0.797100 1.0 B B7 1 0.000000 0.000000 0.797100 1.0 B B8 1 0.500000 0.500000 0.202900 1.0 B B9 1 0.175900 0.038000 0.500000 1.0 B B10 1 0.962000 0.175900 0.500000 1.0 B B11 1 0.038000 0.824100 0.500000 1.0 B B12 1 0.675900 0.462000 0.500000 1.0 B B13 1 0.324100 0.538000 0.500000 1.0 B B14 1 0.824100 0.962000 0.500000 1.0 B B15 1 0.538000 0.675900 0.500000 1.0 B B16 1 0.462000 0.324100 0.500000 1.0 B B17 1 0.086700 0.586700 0.500000 1.0 B B18 1 0.413300 0.086700 0.500000 1.0 B B19 1 0.586700 0.913300 0.500000 1.0 B B20 1 0.913300 0.413300 0.500000 1.0 loop_ _atom_site_moment_label _atom_site_moment_crystalaxis_x _atom_site_moment_crystalaxis_y _atom_site_moment_crystalaxis_z Gd1 5.05 5.05 0.0 Gd2 -5.05 5.05 0.0 Gd3 5.05 -5.05 0.0 Gd4 -5.05 -5.05 0.0 """ self.assertEqual(cw.__str__(), cw_ref_string)