1# Copyright (C) 2016 Atsushi Togo
2# All rights reserved.
3#
4# This file is part of phonopy.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# * Redistributions of source code must retain the above copyright
11#   notice, this list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright
14#   notice, this list of conditions and the following disclaimer in
15#   the documentation and/or other materials provided with the
16#   distribution.
17#
18# * Neither the name of the phonopy project nor the names of its
19#   contributors may be used to endorse or promote products derived
20#   from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33# POSSIBILITY OF SUCH DAMAGE.
34
35from phonopy.structure.cells import get_angles, get_cell_parameters
36
37def write_cif_P1(cell, U_cif=None, filename=None):
38    if filename:
39        with open(filename, 'w') as w:
40            w.write(get_cif_P1(cell, U_cif=U_cif))
41
42def get_cif_P1(cell, U_cif=None):
43    a, b, c = get_cell_parameters(cell.get_cell())
44    alpha, beta, gamma = get_angles(cell.get_cell())
45
46    cif = """data_crystal_structure_P1
47
48_symmetry_space_group_name_H-M     'P 1'
49_symmetry_Int_Tables_number        1
50
51_cell_length_a                     %.5f
52_cell_length_b                     %.5f
53_cell_length_c                     %.5f
54_cell_angle_alpha                  %.5f
55_cell_angle_beta                   %.5f
56_cell_angle_gamma                  %.5f
57_cell_volume                       %.5f
58_cell_formula_units_Z              1
59
60loop_
61_space_group_symop_operation_xyz
62x,y,z
63
64loop_
65_atom_site_label
66_atom_site_type_symbol
67_atom_site_fract_x
68_atom_site_fract_y
69_atom_site_fract_z
70_atom_site_occupancy\n""" % (a, b, c, alpha, beta, gamma, cell.get_volume())
71
72    symbols = []
73    for s, p in zip(cell.get_chemical_symbols(), cell.get_scaled_positions()):
74        symbols.append(s)
75        cif += ("%-7s%2s %10.5f%10.5f%10.5f   1.00000\n" %
76                (s + "%d" % symbols.count(s), s, p[0], p[1], p[2]))
77
78    if U_cif is not None:
79
80        aniso_U = """loop_
81_atom_site_aniso_label
82_atom_site_aniso_U_11
83_atom_site_aniso_U_22
84_atom_site_aniso_U_33
85_atom_site_aniso_U_23
86_atom_site_aniso_U_13
87_atom_site_aniso_U_12\n"""
88
89        cif += aniso_U
90
91        symbols = []
92        for i, s in enumerate(cell.get_chemical_symbols()):
93            symbols.append(s)
94            m = U_cif[i]
95            vals = (m[0, 0], m[1, 1], m[2, 2], m[1, 2], m[0, 2], m[0, 1])
96            cif += ("%6s %10.5f %10.5f %10.5f %10.5f %10.5f %10.5f\n" %
97                    ((s + "%d" % symbols.count(s),) + vals))
98
99    return cif
100