[go: up one dir, main page]

File: random_walk_stuff.c

package info (click to toggle)
circlepack 5.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 2,828 kB
  • ctags: 1,683
  • sloc: ansic: 43,152; makefile: 46
file content (227 lines) | stat: -rw-r--r-- 6,414 bytes parent folder | download | duplicates (3)
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
#include "cp_types.h"
#include "cp_proto.h"

/* routines for random walk computations, tangency packings only. */

/* ========= euclidean functions =========== */

double G(double x,double y,double z)
     /* angle at x */
{
  double a;

  a=x*(x+y+z);
  return ( acos((a-y*z)/(a+y*z)) );
} /* G */

double G1(double x,double y,double z)
     /* angle ftn partial. First, convert arguments to s-radii. */
{
  double s;

  s=x+y+z;
  return ((-1.0)*(sqrt(x*y*z)*sqrt(s))*(1.0/s+1.0/x)/((x+y)*(x+z)) );
} /* G1 */

double G2(double x,double y,double z)
     /* angle ftn partial. First, convert arguments to s-radii. */
{
  return ( (sqrt(x*z)/(sqrt(y)*((x+y)*sqrt(x+y+z)))) );
} /* G2 */

double G3(double x,double y,double z)
     /* angle ftn partial. First, convert arguments to s-radii. */
{
  return ( (sqrt(x*y)/(sqrt(z)*((x+z)*sqrt(x+y+z)))) );
} /* G3 */

/* ========= hyperbolic functions ========== */

double g(double x,double y,double z)
     /* angle function. First, convert arguments to s-radii. */
{
  x=1.0/(x*x); y=1.0/(y*y); z=1.0/(z*z);
  return (acos( ((x*y+1.0)*(x*z+1.0)-2.0*x*(y*z+1.0)) / 	
		((x*y-1.0)*(x*z-1.0)) ));
} /* g */

double g1(double x,double y,double z)
     /* angle ftn partial. First, convert arguments to s-radii. */
{
  x=1.0/(x*x); y=1.0/(y*y); z=1.0/(z*z);
  return ( (-1.0)*(x*x*y*z-1.0)*sqrt((y-1.0)*(z-1.0)) / 
	   ((x*y-1.0)*(x*z-1.0)*sqrt(x*(x-1.0)*(x*y*z-1.0))) );
} /* g1 */


double g2(double x,double y,double z)
     /* angle ftn partial. First, convert arguments to s-radii. */
{
  x=1.0/(x*x); y=1.0/(y*y); z=1.0/(z*z);
  return (sqrt(x*(x-1.0)*(z-1.0)) / 
	  ((x*y-1.0)*sqrt((y-1.0)*(x*y*z-1.0))) );
} /* g2 */

double g3(double x,double y,double z)
     /* angle ftn partial. First, convert arguments to s-radii. */
{
  x=1.0/(x*x); y=1.0/(y*y); z=1.0/(z*z);
  return ( sqrt(x*(x-1.0)*(y-1.0)) / 
	   ((x*z-1.0)*sqrt((z-1.0)*(x*y*z-1.0))) );
} /* g3 */

double area_div(struct p_data *p,int v)
     /* deriv of hyp area. */
{
  int k;
  double accum=0.0;
  struct R_data *pR_ptr;
  struct K_data *pK_ptr;

  pR_ptr=p->packR_ptr;pK_ptr=p->packK_ptr;
  if (p->hes>=0 || v<1 || v>p->nodecount) return 0.0; 
  for (k=0;k<pK_ptr[v].num;k++)
    accum += Av(pR_ptr[v].rad,pR_ptr[pK_ptr[v].flower[k]].rad,
		pR_ptr[pK_ptr[v].flower[k+1]].rad);
  return accum;
} /* area_div */

double Av(double v,double u,double w)
     /* rate of change of area of triangle.*/
{
  return ((-1.0)*(g1(v,u,w)+g2(u,v,w)+g3(w,u,v)));
} /* Av */

double dtheta_dlog(struct p_data *p,int v)
     /* compute the derivative of the angle_sum theta at vertex v with 
respect to the log of the actual hyperbolic radius of v. */
{
  double vv,dtdv,h;

  vv=p->packR_ptr[v].rad;
  if (vv<=0) return 0.0;
  vv=1.0/(vv*vv);
  h=radius(p,v);
  dtdv=node_conductance(p,v)/((1.0-vv)*sqrt(vv));
  return (dtdv*2.0*h*exp(2.0*h));
} /* dtheta_dlog */

/* ============= computations ========== */

double twin_conductance(struct p_data *p,int v)
     /* conductance to ground in hyp case */
{
  double coef,vv;

  if (p->hes == 0 || p->hes >0) return 0; 
  if ((vv=p->packR_ptr[v].rad)<=okerr) return 0.0;
  vv=1.0/(vv*vv);
  coef=(vv-1.0)*sqrt(vv);
  return (area_div(p,v)*coef);
} /* twin_conductance */

double Cvw(double v,double u,double w,double a,int flag,int hes)
     /* conductance of edge from v to w. flag: 0=2 nbh, 1=only left 
nbh (a), 2=only right nbh (u). */
{
  double vv,coef;

  if (hes>0) return 0; /* spherical not yet done */
  if (hes<0)
    {
      if (v<=0 || w<=0) return 0;
      vv=1.0/(v*v); /* convert to s-radius */
      coef=(vv-1.0)*sqrt(vv);
      if (flag==1) return (g3(w,a,v)*coef);
      if (flag==2) return (g3(w,u,v)*coef);
      return ( (g3(w,u,v)+g3(w,a,v))*coef);
    }
  else 
    {
      coef=v;
      if (flag==1) return (G3(w,a,v)*coef);
      if (flag==2) return (G3(w,u,v)*coef);
      return ( (G3(w,u,v)+G3(w,a,v))*coef );
    }
} /* Cvw */

double node_conductance(struct p_data *p,int v)
     /* add up edge (and in hyp, twin) conductances */
{
  int k;
  double accum,coef,vv;
  struct R_data *pR_ptr;
  struct K_data *pK_ptr;

  pR_ptr=p->packR_ptr;pK_ptr=p->packK_ptr;
  if (p->hes>0) return 0.0;
  if (p->hes==0)
    {
      accum=0.0;
      for (k=1;k<pK_ptr[v].num;k++) /* middle faces */
	accum += Cvw(pR_ptr[v].rad,pR_ptr[pK_ptr[v].flower[k-1]].rad,
		     pR_ptr[pK_ptr[v].flower[k]].rad,
		     pR_ptr[pK_ptr[v].flower[k+1]].rad,0,p->hes);
      accum += Cvw(pR_ptr[v].rad,0.0,pR_ptr[pK_ptr[v].flower[0]].rad,
		   pR_ptr[pK_ptr[v].flower[1]].rad,1,p->hes); /* first face */
      accum += Cvw(pR_ptr[v].rad,
		   pR_ptr[pK_ptr[v].flower[pK_ptr[v].num-1]].rad,
		   pR_ptr[pK_ptr[v].flower[pK_ptr[v].num]].rad,
		   0.0,2,p->hes); /* last face */
      return accum;
    }
  if (p->hes<0)
    {
      vv=pR_ptr[v].rad;
      if (vv<okerr) return 0.0;
      vv=1.0/(vv*vv);
      coef=(vv-1.0)*sqrt(vv);
      accum=area_div(p,v)*coef;
      for (k=1;k<pK_ptr[v].num;k++) /* middle faces */
	accum += Cvw(pR_ptr[v].rad,pR_ptr[pK_ptr[v].flower[k-1]].rad,
		     pR_ptr[pK_ptr[v].flower[k]].rad,
		     pR_ptr[pK_ptr[v].flower[k+1]].rad,0,p->hes);
      accum += Cvw(pR_ptr[v].rad,0.0,pR_ptr[pK_ptr[v].flower[0]].rad,
		   pR_ptr[pK_ptr[v].flower[1]].rad,1,p->hes); /* first face */
      accum += Cvw(pR_ptr[v].rad,
		   pR_ptr[pK_ptr[v].flower[pK_ptr[v].num-1]].rad,
		   pR_ptr[pK_ptr[v].flower[pK_ptr[v].num]].rad,
		   0.0,2,p->hes); /* last face */
      return accum;
    }
  return 0.0;
} /* node_conductance */

double edge_conduct(struct p_data *p,int v,int indx)
     /* from v to indx neighbor */
{
  int w,left_index;
  double dum;
  struct R_data *pR_ptr;
  struct K_data *pK_ptr;

  pR_ptr=p->packR_ptr;pK_ptr=p->packK_ptr;
  w=pK_ptr[v].flower[indx];
  if (pK_ptr[v].bdry_flag && indx==0) /* bdry and upstream ngb */
    {
      dum=Cvw(pR_ptr[v].rad,0.0,
	      pR_ptr[w].rad,
	      pR_ptr[pK_ptr[v].flower[1]].rad,1,p->hes);
      return (dum);
    }
  if (pK_ptr[v].bdry_flag && indx==pK_ptr[v].num) /* bdry, down ngb */
    {
      dum=Cvw(pR_ptr[v].rad,
	      pR_ptr[pK_ptr[v].flower[pK_ptr[v].num-1]].rad,
	      pR_ptr[w].rad,0.0,2,p->hes);
      return (dum);
    }
  if (indx==0) left_index=pK_ptr[v].num-1;
  else left_index=indx-1;
  dum=Cvw(pR_ptr[v].rad,
	  pR_ptr[pK_ptr[v].flower[left_index]].rad,
	  pR_ptr[w].rad,
	  pR_ptr[pK_ptr[v].flower[indx+1]].rad,0,p->hes);
  return (dum);
} /* edge_conduct */