]> hackdaworld.org Git - physik/posic.git/blob - vasp_tools/visualize
auto bond feature added
[physik/posic.git] / vasp_tools / visualize
1 #!/bin/sh
2
3 #
4 # visualization script
5 # author: frank.zirkelbach@physik.uni-augsburg.de
6 #
7
8 # help function
9 draw_cyl() {
10         cat >> temp.pov <<-EOF
11 cylinder {
12 <$1, $3, $2>, <$4, $6, $5>, 0.05
13 pigment { color White }
14 }
15 EOF
16 }
17 draw_bond() {
18         cat >> temp.pov <<-EOF
19 cylinder {
20 <$1, $3, $2>, <$4, $6, $5>, $7
21 pigment { color Blue }
22 }
23 EOF
24 }
25
26 # defaults
27
28 lc=5.429
29 directory="doesnt_exist____for_sure"
30 width="640"
31 height="480"
32 radius="0.6"
33 x0="-0.6"; y0="-0.6"; z0="-0.6";
34 x1="0.6"; y1="0.6"; z1="0.6";
35 cx=""; cy=""; cz="";
36 lx="0"; ly="-100"; lz="100";
37 ortographic=""
38 bx0=""; by0=""; bz0="";
39 bx1=""; by1=""; bz1="";
40 bcr="0.1";
41 clx="0"; cly="0"; clz="0";
42 extra=0
43 displace=""
44 mirror=0
45 mx1=0; mx2=0; mx3=0;
46 my1=0; my2=0; my3=0;
47 mz1=0; mz2=0; mz3=0;
48 ab=0
49
50 # parse argv
51
52 while [ "$1" ]; do
53         case "$1" in
54                 -d)             directory=$2;           shift 2;;
55                 -w)             width=$2;               shift 2;;
56                 -h)             height=$2;              shift 2;;
57                 -r)             radius=$2;              shift 2;;
58                 -nll)           x0=$2; y0=$3; z0=$4;    shift 4;;
59                 -fur)           x1=$2; y1=$3; z1=$4;    shift 4;;
60                 -c)             cx=$2; cy=$3; cz=$4;    shift 4;;
61                 -L)             clx=$2; cly=$3; clz=$4; shift 4;;
62                 -l)             lx=$2; ly=$3; lz=$4;    shift 4;;
63                 -o)             ortographic=1;          shift 1;;
64                 -b)             bx0=$2; by0=$3; bz0=$4;
65                                 bx1=$5; by1=$6; bz1=$7; shift 7;;
66                 -B)             bcr=$2;                 shift 2;;
67                 -C)             lc=$2;                  shift 2;;
68                 -e)             extra=1;                shift 1;;
69                 -D)             displace=$2;            shift 2;;
70                 -m)             mx1=$2; mx2=$3; mx3=$4;
71                                 my1=$5; my2=$6; my3=$7;
72                                 mz1=$8; mz2=$9; mz3=${10};
73                                 mirror=1;               shift 10;;
74                 -A)             ab=1;
75                                 anr=$2; cutoff=$3;      shift 3;;
76                 *)
77                                 echo "options:"
78                                 echo "########"
79                                 echo "directory to progress:"
80                                 echo "  -d <directory> (mandatory)"
81                                 echo "png dim:"
82                                 echo "  -w <width>"
83                                 echo "  -h <height>"
84                                 echo "atom size:"
85                                 echo "  -r <radius>"
86                                 echo "  -B <bond cylinder radius>"
87                                 echo "unit cell:"
88                                 echo "  -C <lattice constant>"
89                                 echo "  -m <dimX> <dimY> <dimZ> (mirror atoms)"
90                                 echo "visualization volume:"
91                                 echo "  -nll <x> <y> <z> (near lower left)"
92                                 echo "  -fur <x> <y> <z> (far upper right)"
93                                 echo "  -o (ortographic)"
94                                 echo "bounding box:"
95                                 echo "  -b <x0> <y0> <z0> <x1> <y1> <z1>"
96                                 echo "povray:"
97                                 echo "  -c <x> <y> <z> (camera position)"
98                                 echo "  -L <x> <y> <z> (camera look)"
99                                 echo "  -l <x> <y> <z> (light source)"
100                                 echo "bonds:"
101                                 echo "  -ab <atom number> <cutoff> (auto bonds)"
102                                 exit 1;;
103         esac
104 done
105
106 # calculation from lattic eunits to angstroms
107
108 [ "$lc" = "sic" ] && lc=4.359
109 [ "$lc" = "si" ] && lc=5.480
110 [ "$lc" = "c" ] && lc=3.566
111
112 #offset=`echo 0.125 \* $lc | bc`
113 offset=0.0
114
115 x0=`echo $x0 \* $lc + $offset | bc`
116 y0=`echo $y0 \* $lc + $offset | bc`
117 z0=`echo $z0 \* $lc + $offset | bc`
118 x1=`echo $x1 \* $lc + $offset | bc`
119 y1=`echo $y1 \* $lc + $offset | bc`
120 z1=`echo $z1 \* $lc + $offset | bc`
121
122 mx1=`echo $mx1 \* $lc + $offset | bc`
123 my1=`echo $my1 \* $lc + $offset | bc`
124 mz1=`echo $mz1 \* $lc + $offset | bc`
125 mx2=`echo $mx2 \* $lc + $offset | bc`
126 my2=`echo $my2 \* $lc + $offset | bc`
127 mz2=`echo $mz2 \* $lc + $offset | bc`
128 mx3=`echo $mx3 \* $lc + $offset | bc`
129 my3=`echo $my3 \* $lc + $offset | bc`
130 mz3=`echo $mz3 \* $lc + $offset | bc`
131
132 clx=`echo $clx \* $lc + $offset | bc`
133 cly=`echo $cly \* $lc + $offset | bc`
134 clz=`echo $clz \* $lc + $offset | bc`
135
136 if [ -n "$cx" -a -n "$cy" -a -n "$cz" ]; then
137         cx=`echo $cx \* $lc + $offset | bc`
138         cy=`echo $cy \* $lc + $offset | bc`
139         cz=`echo $cz \* $lc + $offset | bc`
140 fi
141
142 if [ -n "$bx0" ]; then
143         bx0=`echo $bx0 \* $lc + $offset | bc`
144         by0=`echo $by0 \* $lc + $offset | bc`
145         bz0=`echo $bz0 \* $lc + $offset | bc`
146         bx1=`echo $bx1 \* $lc + $offset | bc`
147         by1=`echo $by1 \* $lc + $offset | bc`
148         bz1=`echo $bz1 \* $lc + $offset | bc`
149 fi
150
151 # povray command
152
153 POVRAY="povray -W${width} -H${height} -d" 
154
155 # convert options
156
157 COPTS="-font /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"
158 COPTS="$COPTS -depth 8 -fill white -stroke blue -pointsize 24"
159
160 # do it ...
161
162 if [ -d $directory ]; then
163         filesource=$directory/atomic_conf_*.xyz
164 fi
165
166 if [ -f $directory ]; then
167         filesource=$directory
168 fi
169
170 for file in $filesource; do
171
172         cat > temp.pov <<-EOF
173 #include "colors.inc"
174 #include "textures.inc"
175 #include "shapes.inc"
176 #include "glass.inc"
177 #include "metals.inc"
178 #include "woods.inc"
179 #include "stones.inc"
180 EOF
181
182         # meta info
183         count=`grep '# \[P\]' $file | awk '{ print $3 }'`
184         time=`grep '# \[P\]' $file | awk '{ print $4 }'`
185         camloc=`grep '# \[P\]' $file | awk '{ print $5 }'`
186         [ -n "$cx" -a -n "$cy" -a -n "$cz" ] && camloc="<$cx,$cz,$cy>"
187
188         # atoms
189 #echo "-----> $x0 $y0 $z0 $x1 $y1 $z1 <-----"
190         if [ -n "$x0" ]; then
191                 export x0 y0 z0 x1 y1 z1 radius extra
192                 export mx1 mx2 mx3
193                 export my1 my2 my3
194                 export mz1 mz2 mz3
195                 export mirror
196                 cat $file | grep -v '#' | awk '\
197                 BEGIN {
198                         x0=ENVIRON["x0"]; y0=ENVIRON["y0"]; z0=ENVIRON["z0"];
199                         x1=ENVIRON["x1"]; y1=ENVIRON["y1"]; z1=ENVIRON["z1"];
200                         mx1=ENVIRON["mx1"]; my1=ENVIRON["my1"];
201                         mz1=ENVIRON["mz1"]; mx2=ENVIRON["mx2"];
202                         my2=ENVIRON["my2"]; mz2=ENVIRON["mz2"];
203                         mx3=ENVIRON["mx3"]; my3=ENVIRON["my3"];
204                         mz3=ENVIRON["mz3"]; mirror=ENVIRON["mirror"];
205                         radius=ENVIRON["radius"]; extra=ENVIRON["extra"];
206                         nx=0; ny=0; nz=0;
207                 }
208                 {
209                         if(($2>=x0)&&($3>=y0)&&($4>=z0)&&\
210                            ($2<=x1)&&($3<=y1)&&($4<=z1)) {
211                                 print "sphere { <"$2","$4","$3">, "radius" ";
212                                 if(extra)
213                 print "texture { pigment { color rgb<"$6/4.4",0,"1-$6/4.4"> } ";
214                                 else
215                 print "texture { pigment { color "$5" } ";
216                                 print "finish { phong 1 metallic } } }";
217                         }
218
219                         if(mirror) {
220
221                         nx=$2-mx1; ny=$3-mx2; nz=$4-mx3;
222                         if((nx>=x0)&&(ny>=y0)&&(nz>=z0)&&\
223                            (nx<=x1)&&(ny<=y1)&&(nz<=z1)) {
224 print "// translation: "$2/5.480" "$3/5.480" "$4/5.480" -> "nx/5.480" "ny/5.480" "nz/5.480" ...";
225                                 print "sphere { <"nx","nz","ny">, "radius" ";
226                                 if(extra)
227                 print "texture { pigment { color rgb<"$6/4.4",0,"1-$6/4.4"> } ";
228                                 else
229                 print "texture { pigment { color "$5" } ";
230                                 print "finish { phong 1 metallic } } }";
231                         }
232                         nx=$2-my1; ny=$3-my2; nz=$4-my3;
233                         if((nx>=x0)&&(ny>=y0)&&(nz>=z0)&&\
234                            (nx<=x1)&&(ny<=y1)&&(nz<=z1)) {
235                                 print "sphere { <"nx","nz","ny">, "radius" ";
236                                 if(extra)
237                 print "texture { pigment { color rgb<"$6/4.4",0,"1-$6/4.4"> } ";
238                                 else
239                 print "texture { pigment { color "$5" } ";
240                                 print "finish { phong 1 metallic } } }";
241                         }
242                         nx=$2-mz1; ny=$3-mz2; nz=$4-mz3;
243                         if((nx>=x0)&&(ny>=y0)&&(nz>=z0)&&\
244                            (nx<=x1)&&(ny<=y1)&&(nz<=z1)) {
245                                 print "sphere { <"nx","nz","ny">, "radius" ";
246                                 if(extra)
247                 print "texture { pigment { color rgb<"$6/4.4",0,"1-$6/4.4"> } ";
248                                 else
249                 print "texture { pigment { color "$5" } ";
250                                 print "finish { phong 1 metallic } } }";
251                         }
252
253                         }
254
255                 }' >> temp.pov
256         else
257                 cat $file | grep -v '#' | while read name x y z color temp; do
258                         cat >> temp.pov <<-EOF
259 sphere {
260 <$x, $z, $y>, $radius
261 texture {
262 pigment { color $color }
263 finish {
264 phong 1
265 metallic
266 }
267 }
268 }
269 EOF
270                 done
271         fi
272
273         # boundaries
274         if [ -z "$bx0" ]; then
275
276         #if [ -z "$x0" ]; then
277
278         cat $file | grep '# \[D\]' | while read foo bar x1 y1 z1 x2 y2 z2 ; do
279                 draw_cyl $x1 $y1 $z1 $x2 $y2 $z2 0.05
280         done
281
282         #else
283
284                 # manually drawing the 3x4 boundaries ...
285 #               draw_cyl $x0 $y0 $z0 $x1 $y0 $z0
286 #               draw_cyl $x0 $y0 $z0 $x0 $y1 $z0
287 #               draw_cyl $x1 $y1 $z0 $x1 $y0 $z0
288 #               draw_cyl $x0 $y1 $z0 $x1 $y1 $z0
289
290 #               draw_cyl $x0 $y0 $z1 $x1 $y0 $z1
291 #               draw_cyl $x0 $y0 $z1 $x0 $y1 $z1
292 #               draw_cyl $x1 $y1 $z1 $x1 $y0 $z1
293 #               draw_cyl $x0 $y1 $z1 $x1 $y1 $z1
294
295 #               draw_cyl $x0 $y0 $z1 $x0 $y0 $z0
296 #               draw_cyl $x0 $y1 $z1 $x0 $y1 $z0
297 #               draw_cyl $x1 $y0 $z1 $x1 $y0 $z0
298 #               draw_cyl $x1 $y1 $z1 $x1 $y1 $z0
299 #       fi
300
301         else
302
303                 # manually drawing the 3x4 boundaries specified by argv ...
304                 draw_cyl $bx0 $by0 $bz0 $bx1 $by0 $bz0
305                 draw_cyl $bx0 $by0 $bz0 $bx0 $by1 $bz0
306                 draw_cyl $bx1 $by1 $bz0 $bx1 $by0 $bz0
307                 draw_cyl $bx0 $by1 $bz0 $bx1 $by1 $bz0
308
309                 draw_cyl $bx0 $by0 $bz1 $bx1 $by0 $bz1
310                 draw_cyl $bx0 $by0 $bz1 $bx0 $by1 $bz1
311                 draw_cyl $bx1 $by1 $bz1 $bx1 $by0 $bz1
312                 draw_cyl $bx0 $by1 $bz1 $bx1 $by1 $bz1
313
314                 draw_cyl $bx0 $by0 $bz1 $bx0 $by0 $bz0
315                 draw_cyl $bx0 $by1 $bz1 $bx0 $by1 $bz0
316                 draw_cyl $bx1 $by0 $bz1 $bx1 $by0 $bz0
317                 draw_cyl $bx1 $by1 $bz1 $bx1 $by1 $bz0
318
319         fi      
320
321         # bonds
322         if [ -n "$bcr" ]; then
323
324                 if [ -z "$x0" ]; then
325
326         cat $file | grep '# \[B\]' | while read foo bar x1 y1 z1 x2 y2 z2 ; do
327                 draw_bond $x1 $z1 $y1 $x2 $z2 $y2 $bcr
328         done
329
330                 else
331
332                 export x0 y0 z0 x1 y1 z1 bcr
333                 cat $file | grep '# \[B\]' | awk '\
334                 BEGIN {
335                         x0=ENVIRON["x0"]; y0=ENVIRON["y0"]; z0=ENVIRON["z0"];
336                         x1=ENVIRON["x1"]; y1=ENVIRON["y1"]; z1=ENVIRON["z1"];
337                         bcr=ENVIRON["bcr"];
338                 }
339                 {
340                         if(($3>=x0)&&($4>=y0)&&($5>=z0)&&\
341                            ($3<=x1)&&($4<=y1)&&($5<=z1)) {
342                                 print "cylinder {";
343                                 print "<"$3","$5","$4">,";
344                                 print "<"$6","$8","$7">, "bcr;
345                                 print "pigment { color Blue }";
346                                 print "}";
347                         }
348                 }' >> temp.pov
349
350                 fi
351         fi
352
353         # auto bonds
354         if [ "$ab" = "1" ]; then
355                 ((tmp=anr+1))
356                 line="`sed -n ${tmp}p $file`"
357                 aX=`echo $line | awk '{ print $2 }'`
358                 aY=`echo $line | awk '{ print $3 }'`
359                 aZ=`echo $line | awk '{ print $4 }'`
360                 export aX aY aZ cutoff mirror
361                 cat $file | grep -v '#' | awk '\
362                 BEGIN {
363                         x=ENVIRON["aX"]; y=ENVIRON["aY"]; z=ENVIRON["aZ"];
364                         co=ENVIRON["cutoff"]; dist=0; bcr=ENVIRON["bcr"];
365                         mx1=ENVIRON["mx1"]; my1=ENVIRON["my1"];
366                         mz1=ENVIRON["mz1"]; mx2=ENVIRON["mx2"];
367                         my2=ENVIRON["my2"]; mz2=ENVIRON["mz2"];
368                         mx3=ENVIRON["mx3"]; my3=ENVIRON["my3"];
369                         mz3=ENVIRON["mz3"]; mirror=ENVIRON["mirror"];
370                 }
371                 {
372                         dist=sqrt((x-$2)^2+(y-$3)^2+(z-$4)^2);
373                         if((dist<=co)&&(dist>0.01)) {
374                                 print "cylinder {";
375                                 print "<"x","z","y">,";
376                                 print "<"$2","$4","$3">, "bcr;
377                                 print "pigment { color Blue }";
378                                 print "}";
379                         }
380
381                         if(mirror) {
382                                 nx=$2-mx1; ny=$3-mx2; nz=$4-mx3;
383                                 dist=sqrt((x-nx)^2+(y-ny)^2+(z-nz)^2)
384                                 if((dist<=co)&&(dist>0.01)) {
385                                         print "cylinder {";
386                                         print "<"x","z","y">,";
387                                         print "<"nx","nz","ny">, "bcr;
388                                         print "pigment { color Blue }";
389                                         print "}";
390                                 }
391                                 nx=$2-my1; ny=$3-my2; nz=$4-my3;
392                                 dist=sqrt((x-nx)^2+(y-ny)^2+(z-nz)^2)
393                                 if((dist<=co)&&(dist>0.01)) {
394                                         print "cylinder {";
395                                         print "<"x","z","y">,";
396                                         print "<"nx","nz","ny">, "bcr;
397                                         print "pigment { color Blue }";
398                                         print "}";
399                                 }
400                                 nx=$2-mz1; ny=$3-mz2; nz=$4-mz3;
401                                 dist=sqrt((x-nx)^2+(y-ny)^2+(z-nz)^2)
402                                 if((dist<=co)&&(dist>0.01)) {
403                                         print "cylinder {";
404                                         print "<"x","z","y">,";
405                                         print "<"nx","nz","ny">, "bcr;
406                                         print "pigment { color Blue }";
407                                         print "}";
408                                 }
409                         }
410
411                 }' >> temp.pov
412         fi
413
414         # add camera and light source
415         cat >> temp.pov <<-EOF
416 camera {
417 EOF
418         if [ -n "$ortographic" ]; then  cat >> temp.pov <<-EOF
419 orthographic
420 EOF
421         fi
422         cat >> temp.pov <<-EOF
423 location $camloc
424 look_at <$clx,$clz,$clz>
425 }
426 light_source { <0,100,-100> color White shadowless }
427 EOF
428
429         # mv png
430         $POVRAY temp.pov > /dev/null 2>&1
431         time=`basename $file | awk -F. '{ print $1 }' | awk -F_ '{ print $3 }'`
432         if [ ! -z $displace ]; then
433         convert $COPTS -draw "text 5,20 'd = $displace %'" temp.png temp.png
434         else
435         convert $COPTS -draw "text 5,20 't = $time fs'" temp.png temp.png
436         fi
437         mv temp.png `echo $file | sed 's/\.xyz/\.png/'`
438
439 done
440
441 echo "done"