Reset platonic code.
[voro++.git] / branches / dynamic / src / worklist_gen.pl
blob70837056c91d90b93c51cf22508958a771bceb64
1 #!/usr/bin/perl
2 # Voro++, a 3D cell-based Voronoi library
4 # Author : Chris H. Rycroft (LBL / UC Berkeley)
5 # Email : chr@alum.mit.edu
6 # Date : July 1st 2008
8 # worklist_gen.pl - this perl script is used to automatically generate the worklists
9 # of blocks that are stored in worklist.cc, that are used by the compute_cell()
10 # routine to ensure maximum efficiency
12 # Each region is divided into a grid of subregions, and a worklist is constructed
13 # for each. This parameter sets is set to half the number of subregions that the
14 # block is divided into.
15 $hr=4;
17 # This parameter is automatically set to the the number of subregions that the
18 # block is divided into
19 $r=$hr*2;
21 # This parameter sets the total number of block entries in each worklist
22 $ls=63;
24 # When the worklists are being constructed, a 3D array is made use of.
25 # To prevent the creation of array elements with negative indices, this
26 # parameter sets an arbitrary displacement.
27 $d=8;
29 use Math::Trig;
31 # Construct the worklist header file, based on the parameters above
32 open W,">worklist.hh";
33 print W <<EOF;
34 // Voro++, a 3D cell-based Voronoi library
36 // Author : Chris H. Rycroft (LBL / UC Berkeley)
37 // Email : chr\@alum.mit.edu
38 // Date : July 1st 2008
40 /** \\file worklist.hh
41 * \\brief Header file for setting constants used in the block worklists that are
42 * used during cell computation.
44 * This file is automatically generated by worklist_generate.pl and it is not
45 * intended to be edited by hand. */
47 EOF
48 print W "static const int hgrid=$hr;\n";
49 print W "static const int fgrid=$r;\n";
50 printf W "static const int hgridsq=%d;\n",$hr*$hr*$hr;
51 printf W "static const int seq_length=%d;\n",$ls+1;
52 printf W "static const unsigned int wl[%d];\n",($ls+1)*$hr*$hr*$hr;
53 close W;
55 # Construct the preamble to the worklist.cc file
56 open W,">worklist.cc";
57 print W <<EOF;
58 // Voro++, a 3D cell-based Voronoi library
60 // Author : Chris H. Rycroft (LBL / UC Berkeley)
61 // Email : chr\@alum.mit.edu
62 // Date : July 1st 2008
64 /** \\file worklist.cc
65 * \\brief The table of block worklists that are used during the cell
66 * computation.
68 * This file is automatically generated by worklist_generate.pl and it is not
69 * intended to be edited by hand. */
71 EOF
72 printf W "template<class r_option>\n";
73 printf W "const unsigned int container_base<r_option>::wl[%d]={\n",($ls+1)*$hr*$hr*$hr;
75 # Now create a worklist for each subregion
76 for($kk=0;$kk<$hr;$kk++) {
77 for($jj=0;$jj<$hr;$jj++) {
78 for($ii=0;$ii<$hr;$ii++) {
79 worklist($ii,$jj,$kk);
84 # Finish the file and close it
85 print W "};\n";
86 close W;
88 sub worklist {
89 # print "@_[0] @_[1] @_[2]\n";
90 $ind=@_[0]+$hr*(@_[1]+$hr*@_[2]);
91 $ac=0;$v++;
92 $xp=$yp=$zp=0;
93 $x=(@_[0]+0.5)/$r;
94 $y=(@_[1]+0.5)/$r;
95 $z=(@_[2]+0.5)/$r;
96 $m[$d][$d][$d]=$v;
97 add(1,0,0);add(0,1,0);add(0,0,1);
98 add(-1,0,0);add(0,-1,0);add(0,0,-1);
99 foreach $l (1..$ls) {
100 $minwei=1e9;
101 foreach (0..$ac-1) {
102 $xt=@a[3*$_];$yt=@a[3*$_+1];$zt=@a[3*$_+2];
103 # $wei=dis($x,$y,$z,$xt,$yt,$zt)+1*acos(($xt*$xp+$yt*$yp+$zt*$zp)/($xt*$xt+$yt*$yt+$zt*$zt)*($xp*$xp+$yp*$yp+$zp*$zp));
104 $wei=adis($x,$y,$z,$xt,$yt,$zt)+0.02*sqrt(($xt-$xp)**2+($yt-$yp)**2+($zt-$zp)**2);
105 $nx=$_,$minwei=$wei if $wei<$minwei;
107 $xp=@a[3*$nx];$yp=@a[3*$nx+1];$zp=@a[3*$nx+2];
108 add($xp+1,$yp,$zp);add($xp,$yp+1,$zp);add($xp,$yp,$zp+1);
109 add($xp-1,$yp,$zp);add($xp,$yp-1,$zp);add($xp,$yp,$zp-1);
110 # print "=> $l $xp $yp $zp\n" if $l<4;
111 push @b,(splice @a,3*$nx,3);$ac--;
113 $v++;
114 for($i=0;$i<$#b;$i+=3) {
115 $xt=@b[$i];$yt=@b[$i+1];$zt=@b[$i+2];
116 $m[$d+$xt][$d+$yt][$d+$zt]=$v;
118 $m[$d][$d][$d]=$v;
119 for($i=0;$i<$#b;$i+=3) {
120 $xt=@b[$i];$yt=@b[$i+1];$zt=@b[$i+2];
121 last if $m[$d+$xt+1][$d+$yt][$d+$zt]!=$v;
122 last if $m[$d+$xt][$d+$yt+1][$d+$zt]!=$v;
123 last if $m[$d+$xt][$d+$yt][$d+$zt+1]!=$v;
124 last if $m[$d+$xt-1][$d+$yt][$d+$zt]!=$v;
125 last if $m[$d+$xt][$d+$yt-1][$d+$zt]!=$v;
126 last if $m[$d+$xt][$d+$yt][$d+$zt-1]!=$v;
128 $j=$i/3;
129 print W "\t$j";
130 while ($#b>0) {
131 $i-=3;
132 $xt=shift @b;$yt=shift @b;$zt=shift @b;
133 $o=0;
134 $o|=1 if $m[$d+$xt+1][$d+$yt][$d+$zt]!=$v;
135 $o^=3 if $m[$d+$xt-1][$d+$yt][$d+$zt]!=$v;
136 $o|=8 if $m[$d+$xt][$d+$yt+1][$d+$zt]!=$v;
137 $o^=24 if $m[$d+$xt][$d+$yt-1][$d+$zt]!=$v;
138 $o|=64 if $m[$d+$xt][$d+$yt][$d+$zt+1]!=$v;
139 $o^=192 if $m[$d+$xt][$d+$yt][$d+$zt-1]!=$v;
140 $pack=($xt+64)|($yt+64)<<7|($zt+64)<<14|$o<<21;
141 printf W ",%#x",$pack;
143 print W "," unless $ind==$hr*$hr*$hr-1;
144 print W "\n";
145 undef @a;
146 undef @b;
149 sub add {
150 if ($m[$d+@_[0]][$d+@_[1]][$d+@_[2]]!=$v) {
151 $ac++;
152 push @a,@_[0],@_[1],@_[2];
153 $m[$d+@_[0]][$d+@_[1]][$d+@_[2]]=$v;
157 sub dis {
158 $xl=@_[3]+0.3-@_[0];$xh=@_[3]+0.7-@_[0];
159 $yl=@_[4]+0.3-@_[1];$yh=@_[4]+0.7-@_[1];
160 $zl=@_[5]+0.3-@_[2];$zh=@_[5]+0.7-@_[2];
161 $dis=(abs($xl)<abs($xh)?$xl:$xh)**2
162 +(abs($yl)<abs($yh)?$yl:$yh)**2
163 +(abs($zl)<abs($zh)?$zl:$zh)**2;
164 return sqrt $dis;
167 sub adis {
168 $xco=$yco=$zco=0;
169 $xco=@_[0]-@_[3] if @_[3]>0;
170 $xco=@_[0]-@_[3]-1 if @_[3]<0;
171 $yco=@_[1]-@_[4] if @_[4]>0;
172 $yco=@_[1]-@_[4]-1 if @_[4]<0;
173 $zco=@_[2]-@_[5] if @_[5]>0;
174 $zco=@_[2]-@_[5]-1 if @_[5]<0;
175 return sqrt $xco*$xco+$yco*$yco+$zco*$zco;