Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

# coding: utf-8 

# Copyright (c) Pymatgen Development Team. 

# Distributed under the terms of the MIT License. 

 

from __future__ import division, unicode_literals 

 

""" 

This module provides input and output from the CSSR file format. 

""" 

 

 

__author__ = "Shyue Ping Ong" 

__copyright__ = "Copyright 2012, The Materials Project" 

__version__ = "0.1" 

__maintainer__ = "Shyue Ping Ong" 

__email__ = "shyuep@gmail.com" 

__date__ = "Jan 24, 2012" 

 

import re 

 

from six.moves import map 

 

from monty.io import zopen 

from pymatgen.core.lattice import Lattice 

from pymatgen.core.structure import Structure 

 

 

class Cssr(object): 

""" 

Basic object for working with Cssr file. Right now, only conversion from 

a Structure to a Cssr file is supported. 

 

Args: 

structure (Structure/IStructure): A structure to create the Cssr object. 

""" 

 

def __init__(self, structure): 

if not structure.is_ordered: 

raise ValueError("Cssr file can only be constructed from ordered " 

"structure") 

self.structure = structure 

 

def __str__(self): 

output = ["{:.4f} {:.4f} {:.4f}" 

.format(*self.structure.lattice.abc), 

"{:.2f} {:.2f} {:.2f} SPGR = 1 P 1 OPT = 1" 

.format(*self.structure.lattice.angles), 

"{} 0".format(len(self.structure)), 

"0 {}".format(self.structure.formula)] 

for i, site in enumerate(self.structure.sites): 

output.append("{} {} {:.4f} {:.4f} {:.4f}" 

.format(i + 1, site.specie, site.a, site.b, site.c)) 

return "\n".join(output) 

 

def write_file(self, filename): 

""" 

Write out a CSSR file. 

 

Args: 

filename (str): Filename to write to. 

""" 

with zopen(filename, 'wt') as f: 

f.write(str(self) + "\n") 

 

@staticmethod 

def from_string(string): 

""" 

Reads a string representation to a Cssr object. 

 

Args: 

string (str): A string representation of a CSSR. 

 

Returns: 

Cssr object. 

""" 

lines = string.split("\n") 

toks = lines[0].split() 

lengths = [float(i) for i in toks] 

toks = lines[1].split() 

angles = [float(i) for i in toks[0:3]] 

latt = Lattice.from_lengths_and_angles(lengths, angles) 

sp = [] 

coords = [] 

for l in lines[4:]: 

m = re.match("\d+\s+(\w+)\s+([0-9\-\.]+)\s+([0-9\-\.]+)\s+" + 

"([0-9\-\.]+)", l.strip()) 

if m: 

sp.append(m.group(1)) 

coords.append([float(m.group(i)) for i in range(2, 5)]) 

return Cssr(Structure(latt, sp, coords)) 

 

@staticmethod 

def from_file(filename): 

""" 

Reads a CSSR file to a Cssr object. 

 

Args: 

filename (str): Filename to read from. 

 

Returns: 

Cssr object. 

""" 

with zopen(filename, "rt") as f: 

return Cssr.from_string(f.read())