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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
#include "cp_head.h"
FILE *fp;
int jason_howard_code(char *datastr)
{
int count=0,nodecount,num,i;
int j,n;
int lpcnt=0;
int f1=3; /* number of flips */
int k;
int v1; /* first random vertice */
int v2; /* neighbor of v1 */
complex W[7]; /* matrix for side pairing maps */
complex C[1000]; /* max number of flips is 1000 */
complex w,wcheck; /* period ratio */
complex tau;
complex r1,r12,r2,r22,r3,r32;
double bnd;
struct p_data *p=&(packdata[current_p]);
complex m_one;
Mobius mob;
// sprintf(msgbuf,"Hello\n");
//msg();
if (sscanf(datastr,"%d",&f1)!= 1 || f1<0
|| p->num_bdry_comp>0 || p->genus!=1)
return 0;
sprintf(msgbuf,"Hello %d \n", f1);
msg();
/* read data in. Want w1,w2 ... = cdiv(mob.a, mob.b) for each
edge-pair[i].mob consider putting in array W and do for loop on
i.(this will change r1 etc)*/
nodecount=p->nodecount;
m_one.re=-1.0;m_one.im=0.0;
k=0;
do
{
for(n=0;n<1;n++){
/* since arrays start at 0 in C right? */
j=1;
while(p->edge_pair[j].edge)
{
mob=p->edge_pair[j].mob;
W[j]=cdiv(mob.b,mob.a);
j++;
}
if (mob.a.re!=0 && mob.a.im!=0) {
/*----------Calculate ratios and select candidate in upper half plane ------*/
r1=cdiv(W[1],W[2]);
r12=cdiv(W[2],W[1]);
r2=cdiv(W[3],W[2]);
r22=cdiv(W[2],W[3]);
r3=cdiv(W[3],W[4]);
r32=cdiv(W[4],W[3]);
/*get initial tau in upper half plane*/
if (r1.im>=0)
w=r1;
else if (r12.im>=0)
w=r12;
else if (r2.im>=0)
w=r2;
else if (r22.im>=0)
w=r22;
else if (r3.im>=0)
w=r3;
else if (r32.im>=0)
w=r32;
else
return 0;
/* lower bnd for imag part in fundemental cell*/
if (w.re>=1)
{
w.re -= floor(w.re);
}
else if (w.re<-1){
w.re = w.re - floor(w.re);
}
bnd=sqrt(1-(w.re)*(w.re));
lpcnt=0;
while (fabs(w.re)>.5 || w.im<bnd)
{
wcheck.re=w.re;
wcheck.im=w.im;
if (w.re>=1)
{
w.re -= floor(w.re);
}
else if (w.re<-1){
w.re = w.re - floor(w.re);
}
else if (w.re<=1 && w.re>.5){
w.re -= 1.0;
}
else if (w.re>=-1 && w.re<-.5){
w.re += 1.0;
}
if (w.im<bnd) {
w=cdiv(m_one,w);
}
if (w.re>=1)
{
w.re -= floor(w.re);
}
else if (w.re<-1){
w.re = w.re - floor(w.re);
}
bnd=sqrt(1-(w.re)*(w.re));
lpcnt=lpcnt+1;
if (lpcnt>5000){
sprintf(msgbuf,"**ERROR**Caught in infinte loop ");
msg();
sprintf(msgbuf,"Flip # %d of %d",count,f1);
msg();
return 0;
}
// need to add more conditions HERE to wcheck
//if (w.re == wcheck.re && w.im == wcheck.im){
// sprintf(msgbuf,"error:caught in infinte loop ");
// msg();
// return 0;
// }
}
tau=w;
sprintf(msgbuf,"tau=%lf + %lf i",tau.re,tau.im);
msg();
// C[count]=tau;
//count++;
if (count<f1)
{
v1=rand()%nodecount;
v1=v1+1;
// sprintf(msgbuf,"v1=%d",v1);
//msg();
num=p->packK_ptr[v1].num;
// v2=rand()%(num-1);
v2=p->packK_ptr[v1].flower[rand()%(num)];
//sprintf(msgbuf,"v2=%d",v2);
// msg();
sprintf(buf,"flip %d %d",v1,v2);
handle_cmd(buf,¤t_p);
sprintf(buf,"repack 2000");
handle_cmd(buf,¤t_p);
sprintf(buf,"fix");
handle_cmd(buf,¤t_p);
//sprintf(buf,"disp -w -C -Rn a");
//handle_cmd(buf,¤t_p);
//sprintf(msgbuf,"Flip # %d of %d",count,f1);
//msg();
}
}
else if (mob.a.re==0 && mob.a.im==0){
sprintf(buf,"unflip %d %d",v1,v2);
handle_cmd(buf,¤t_p);
sprintf(buf,"repack 2000");
handle_cmd(buf,¤t_p);
sprintf(buf,"fix");
handle_cmd(buf,¤t_p);
sprintf(buf,"write /tmp/unflip.p");
handle_cmd(buf,¤t_p);
printf("unflip %d %d.\n",v1,v2);
return 0;
count=count-1;
k=k+1;
}
}
C[count]=tau;
count++;
} while (count<f1);
fp=fopen("tau.dat","w");
for (i=0;i<count;i++)
// fprintf(fp,"%d: tau = (%.12f,%.12f)\n",i,C[i].re,C[i].im);
fprintf(fp," %.12f %.12f\n",C[i].re,C[i].im);
fclose(fp);
sprintf(msgbuf,"Accesed unflip command %d times",k);
msg();
} /* jason_howard_code */
|