initial setup of thesis repository
[cluster_expansion_thesis.git] / little_helpers / tikz / contcar2dmol.sh
blobcd588b3c177dd3bfec8a34c67458521b40c38494
1 #!/bin/bash -eu
2 #------------------------------------------------
3 # contcar2dmol.sh
4 # Hackish script to convert VASP structure file with periodic
5 # boundary conditions and orthogonal lattice into dmol file
6 # Copyright (C) Max J. Hoffmann 2009 mjhoffmann@gmail.com
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # Based on documentation found at
22 # http://cms.mpi.univie.ac.at/vasp/vasp.pdf.gz , subsection 5.7 POSCAR file
23 #------------------------------------------------
25 #Initialize variables
26 contcar=""
28 for i in $(seq 7)
30 species_name[${i}]=""
31 done
33 #Parse options
34 while getopts "hi:a:b:c:d:e:f:g:" optionName
35 #Sorry, about the clumsy option names, but getopts does
36 #not handle long options
38 case "${optionName}" in
39 a) species_name[1]=${OPTARG} ;;
40 b) species_name[2]=${OPTARG} ;;
41 c) species_name[3]=${OPTARG} ;;
42 d) species_name[4]=${OPTARG} ;;
43 e) species_name[5]=${OPTARG} ;;
44 f) species_name[6]=${OPTARG} ;;
45 g) species_name[7]=${OPTARG} ;;
46 h) echo "Usage: ${0} -i CONTCAR_FILE [-a Species1 -b Species2 ...] > DMOL_FILE " ;
47 echo "Only 7 species can be specified due to limitations of getopts"
48 exit ;;
49 i) contcar=${OPTARG} ;;
50 esac
51 done
54 #Exit with usage message if no input file is provided
55 if [ "${contcar}" = "" ]
56 then
57 ${0} -h
58 exit
61 #INPUT
62 #fetch maximum number of species
63 max_spec=$(awk '{if(NR==6) print $0}' ${contcar} | wc -w)
66 #global scaling factor
67 a=$(awk '{if(NR==2) print $1}' ${contcar})
68 #first lattice vector
69 x1=$(awk '{if(NR==3) print $1}' ${contcar})
70 x2=$(awk '{if(NR==3) print $2}' ${contcar})
71 x3=$(awk '{if(NR==3) print $3}' ${contcar})
73 #second lattice vector
74 y1=$(awk '{if(NR==4) print $1}' ${contcar})
75 y2=$(awk '{if(NR==4) print $2}' ${contcar})
76 y3=$(awk '{if(NR==4) print $3}' ${contcar})
78 #third lattice vector
79 y1=$(awk '{if(NR==4) print $1}' ${contcar})
80 z1=$(awk '{if(NR==5) print $1}' ${contcar})
81 z2=$(awk '{if(NR==5) print $2}' ${contcar})
82 z3=$(awk '{if(NR==5) print $3}' ${contcar})
84 #fetch numbers of species
85 for i in $(seq ${max_spec})
87 sp[${i}]=$(awk "{if(NR==6) print \$${i}}" ${contcar})
88 done
91 #OUTPUT
92 #lattice constants
93 echo "\$cell vectors"
94 head -n 5 ${contcar} | tail -n 3
95 echo "\$coordinates"
97 #atomic coordinates
98 accum=0
99 species_nr=1
100 for atoms in ${sp[@]}
103 #The seventh line may contain [sS]elective Dynamics and is optional, so check ...
104 #I did not understand the meaning of this line. So the results might be wrong if there is
105 #no 'selective dynamics' printed.
106 if [ -n "$(sed -n 7p ${contcar} | grep -i "^\s*s")" ]; then offset=8; else offset=7; fi
107 for i in $(seq ${atoms})
109 line=$(expr "${offset}+${accum}+${i}" | bc -l)
111 #Switch between direct (fractional) coordinates and cartesian(absolute) coordinates
112 if [ $(sed -n ${offset}p ${contcar} | grep -oi "^\s*d" ) ]
113 then
114 abs_x=$(expr "${a}*($(awk "{if(NR==${line}) print \$1 }" ${contcar})*${x1}+\
115 $(awk "{if(NR==${line}) print \$2 }" ${contcar})*${y1}+\
116 $(awk "{if(NR==${line}) print \$3 }" ${contcar})*${z1})" | bc -l )
117 abs_y=$(expr "${a}*($(awk "{if(NR==${line}) print \$1 }" ${contcar})*${x2}+\
118 $(awk "{if(NR==${line}) print \$2 }" ${contcar})*${y2}+\
119 $(awk "{if(NR==${line}) print \$3 }" ${contcar})*${z2})" | bc -l )
120 abs_z=$(expr "${a}*($(awk "{if(NR==${line}) print \$1 }" ${contcar})*${x3}+\
121 $(awk "{if(NR==${line}) print \$2 }" ${contcar})*${y3}+\
122 $(awk "{if(NR==${line}) print \$3 }" ${contcar})*${z3})" | bc -l )
123 else
124 abs_x=$(awk "{if(NR==${line}) print \$1 }" ${contcar})
125 abs_y=$(awk "{if(NR==${line}) print \$2 }" ${contcar})
126 abs_z=$(awk "{if(NR==${line}) print \$3 }" ${contcar})
128 if [ "${species_name[${species_nr}]}" = "" ]
129 then
130 echo "Species${species_nr} ${abs_x} ${abs_y} ${abs_z}"
131 else
132 echo "${species_name[${species_nr}]} ${abs_x} ${abs_y} ${abs_z}"
135 done
136 accum=$(expr "${accum}+${atoms}" | bc -l)
137 species_nr=$(expr "${species_nr} + 1" | bc -l)
139 done
141 #And we are done!
142 echo "\$end"