File: khyp.c

package info (click to toggle)
evolver 2.30c.dfsg-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,800 kB
  • ctags: 10,247
  • sloc: ansic: 118,999; makefile: 100
file content (326 lines) | stat: -rw-r--r-- 8,870 bytes parent folder | download | duplicates (4)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*************************************************************
*  This file is part of the Surface Evolver source code.     *
*  Programmer:  Ken Brakke, brakke@susqu.edu                 *
*************************************************************/

/*************************************************************
*
*   file:      khyp.c
*
*   Purpose:  Illustration of how to do quotient spaces.
*             This example does a Klein hyperbolic model in R^2.
*             The user needs to define the integer representation
*             used for group elements and three functions to
*             do group operations.  WRAPTYPE is the type of
*             group element representation, currently long int.
*             You must write your own versions of group_wrap(),
*             group_compose(), and group_inverse().
*
*             You don't have the privilege
*             of defining your own datafile syntax, but you can access
*             values of parameters defined in the datafile as
*             *var, where var = get_global(char *name).
*/

#include "include.h"


#define PDIM  4  /* Minkowski space dimension */
/* Minkowski dim is tacked on before other dimensions so this file
    can be used in different dimensions */

/* Description of group element encoding: 
    The Klein  disk is always mapped back to a Minkowski hyperbola
    for transformations.
    There are 8 translation elements that translate the fundamental region
    to one of its neighbors.  Translating around a vertex gives a
    circular string of the 8 elements.  The group elements encoded are
    substrings of the 8, with null string being the identity.  Encoding
    is 4 bits for start element, 4 bits for stop (actually the one
    after stop so 0 0 is identity).
*/

#define GENUS2BITS 4
#define GENUS2MASK 7

#define CHOP(g) ((g)&GENUS2MASK)
#define END(g) CHOP((g)>>GENUS2BITS)
#define DFF(g,h,d) (CHOP((h)-(g))==(d))
#define ENC(g1,gk) (CHOP(g1) | (CHOP(gk)<<GENUS2BITS))
#define WRAP(g1,gk) (DFF(g1,gk,0)? 0:(DFF(g1,gk,7)? ENC(g1+3,g1+4):ENC(g1,gk)))

/* define the two group generators used in generating the 8 elements */
/* Group elements alway act in Minkowski space */
#undef COSH
#undef SINH
#undef SIN
#undef COS
#define COSH 2.414213562373 /* 1+sqrt(2) */
#define SINH 2.19736822693562
#define SIN 0.70710678118655 /* sqrt(2)/2 */
#define COS -0.70710678118655 /* sqrt(2)/2 */
REAL TrMat[PDIM][PDIM] =
     {{COSH,SINH,0.,0.},{SINH,COSH,0.,0.},{0.,0.,1.,0.},{0.,0.,0.,1.}};
REAL RotMat[PDIM][PDIM] = /* rotate by 135 deg clockwise */
     {{1.,0.,0.,0.},{0.,COS,SIN,0.},{0.,-SIN,COS,0.},{0.,0.,0.,1.}};
REAL id_mat[PDIM][PDIM] = 
    {{1.,0.,0.,0.},{0.,1.,0.,0.},{0.,0.,1.,0.},{0.,0.,0.,1.}};

int q_init_flag;     /* set once stuff initialized */
REAL eg[8][PDIM][PDIM];  /* generator matrices */

typedef REAL ptype[PDIM]; 
typedef REAL pptype[PDIM][PDIM]; 


/*****************************************************************
* 
* Some utility functions for matrix operations on fixed size
* matrices.
*/
  
static void copy ARGS((ptype,ptype));
static void matmult ARGS((pptype,ptype,ptype));
static void applygen ARGS((int,ptype,ptype));
static void copymat ARGS((pptype,pptype));
static void matmatmult ARGS((pptype,pptype,pptype));
static void genmat ARGS((int,pptype));
static void q_init ARGS ((void));
void khyp_wrap ARGS((REAL*,REAL*,WRAPTYPE));
WRAPTYPE khyp_compose ARGS((WRAPTYPE,WRAPTYPE));
WRAPTYPE khyp_inverse ARGS((WRAPTYPE));
void khyp_form_pullback ARGS(( REAL *,REAL *,REAL *,WRAPTYPE));

static void copy(z,w)
REAL z[PDIM],w[PDIM];
{
     int j;

     for (j=0; j<PDIM; j++)
          w[j]=z[j];
}

static void matmult(m,z,w)
REAL m[PDIM][PDIM],z[PDIM],w[PDIM];
{
     int i,j;
     REAL t[PDIM];

     for (j=0; j<=SDIM; j++)
          for (i=0,t[j]=0.; i<=SDIM; i++)
                t[j] += m[j][i] * z[i];
     copy(t,w);
}

static void applygen(g,z,w)
int g;
REAL z[PDIM],w[PDIM];
{
     REAL t[PDIM];
     int i;

     g = CHOP(g);
     copy(z,t);
     for (i=0;i<g;i++)
          matmult(RotMat,t,t);
     matmult(TrMat,t,t);
     matmult(TrMat,t,t);
     for (i=g;i<8;i++)
          matmult(RotMat,t,t);
     copy(t,w);
}

static void copymat(a,b)
REAL a[PDIM][PDIM],b[PDIM][PDIM];
{
  memcpy((char*)b,(char*)a,PDIM*PDIM*sizeof(REAL));
}

static void matmatmult(a,b,c)
REAL a[PDIM][PDIM],b[PDIM][PDIM],c[PDIM][PDIM];
{ int i,j,k;
  REAL t[PDIM][PDIM];
  REAL *tp;
  for ( i = 0 ; i <=SDIM ; i++ )
     for ( k = 0, tp = t[i] ; k <= SDIM ; k++,tp++ )
        { *tp = 0.0;
          for ( j = 0 ; j <= SDIM ; j++ )
             *tp += a[i][j]*b[j][k];
        }
  copymat(t,c);
}

static void genmat(g,t)
int g;
REAL t[PDIM][PDIM];
{
     int i;

     for (i=0;i<g;i++)
          matmatmult(RotMat,t,t);
     matmatmult(TrMat,t,t);
     matmatmult(TrMat,t,t);
     for (i=g;i<8;i++)
          matmatmult(RotMat,t,t);
}

static void q_init()
{ int i;
  
  for ( i = 0 ; i < 8 ; i++ )
    { copymat(id_mat,eg[i]);
      genmat(i,eg[i]);
    }

  q_init_flag = 1;
}

/*******************************************************************
*
*  function: khyp_wrap
*
*  purpose:  Provide adjusted coordinates for vertices that get
*                wrapped around torus.  Serves as example for user-written
*                symmetry function.
*
*/

void khyp_wrap(x,y,wrap)
REAL *x;    /* original coordinates */
REAL *y;    /* wrapped coordinates  */
WRAPTYPE wrap;  /* encoded symmetry group element */
{
     REAL z[2][PDIM]; /* two work vectors */
     int j,i;
     int g1,gk, g;

     if ( q_init_flag == 0 ) q_init();

     if (wrap==0)
     {
          for (j=0; j<SDIM; j++)
                y[j]=x[j];
          return;
     }

     g1 = CHOP(wrap);
     gk = CHOP(wrap>>GENUS2BITS);
     if (gk<g1) gk += 8;

     copy(x,z[0]+1);
     z[0][0] = 1.;
     for (g=gk-1,j=0; g>=g1; g--,j=1-j)
     {
          matmult(eg[CHOP(g)],z[j],z[1-j]);
     }
    for ( i = 0 ; i < SDIM ; i++ ) y[i] = z[j][i+1]/z[j][0];
}


/********************************************************************
*
*  function: khyp_compose()
*
*  purpose:  do composition of two group elements
khyp_wrap(khyp_wrap(pt,w),v) = khyp_wrap(pt,khyp_compose(v,w))
If the current wrap is v, and we get to an edge with wrap w,
and its head is at pt, then either of these should give the unwrapped
coordinates of its head (relative to khyp_wrap(tail,v) being its tail).
*
*/

/* generators 1=A,2=b,3=C,4=d,5=a,6=B,7=c,0=8=D */
/* inverses differ by 4 */

#define TRY(g1,gk,h1,hk) if (DFF(gk,h1,0)) return WRAP(g1,hk)

WRAPTYPE khyp_compose(gw,hw)
WRAPTYPE gw,hw;  /* the elements to compose */
{
     int g1,gk, h1,hk;

     if (gw==0) return hw;
     if (hw==0) return gw;
     g1 = CHOP(gw); gk = END(gw);
     h1 = CHOP(hw); hk = END(hw);

     TRY(g1,gk,h1,hk);  /* h nicely follows after g */
     if (DFF(h1,hk,1)) TRY(g1,gk,h1-3,h1-4); /* equiv string of 7 */
     if (DFF(g1,gk,1)) TRY(g1-3,g1-4,h1,hk); /* equiv string of 7 */
     if (DFF(h1,hk,1) && DFF(g1,gk,1)) TRY(g1-3,g1-4,h1-3,h1-4);

     sprintf(msg,"Trying to compose %d-%d and %d-%d\n",g1,gk,h1,hk);
     strcat(msg,"khyp: Wrap outside known range\n");
     kb_error(1305,msg,WARNING);

     return 0;
}


/********************************************************************
*
*  function: khyp_inverse()
*
*  purpose:  return inverse of group element.
*
*/

WRAPTYPE khyp_inverse(wrap)
WRAPTYPE wrap;  /* the element to invert */
{
    return WRAP(END(wrap),CHOP(wrap));
}
  
/*******************************************************************
*
*  function: khyp_form_pullback
*
*  purpose:  Pull back differential forms at vertices that get
*                wrapped.
*
*/

void khyp_form_pullback(x,xform,yform,wrap)
REAL *x;    /* original coordinates */
REAL *xform;  /* result pullback */
REAL *yform;    /* original form input  */
WRAPTYPE wrap;  /* encoded symmetry group element */
{
  int i,j;
  REAL trans[PDIM][PDIM];
  REAL jac[PDIM][PDIM];  /* Jacobian matrix */
  REAL y[PDIM];
  REAL w[PDIM];  /* Minkowski coord of x */
  int g1,gk,g;    /* element numbers */

  if ( wrap == 0 ) /* just copy */
     { memcpy((char *)xform,(char*)yform,SDIM*sizeof(REAL));
        return;
     }

  g1 = CHOP(wrap);
  gk = CHOP(wrap>>GENUS2BITS);
  if (gk<g1) gk += 8;

  /* get total linear transformation */
  copymat(eg[g1],trans);
  for ( g = g1+1; g < gk ; g++ )
    matmatmult(trans,eg[CHOP(g)],trans);

  /* get transformed point in Minkowski */
  memcpy((char *)(w+1),(char*)x,SDIM*sizeof(REAL));
  w[0] = 1.0;
  matmult(trans,w,y);

  /* set up Jacobian */
  for ( i = 0 ; i < SDIM ; i++ )
    for ( j = 0 ; j < SDIM ; j++ )
      jac[i][j] = (trans[i+1][j+1] - y[i+1]*trans[0][j+1]/y[0])/y[0];

  /* pull back form with transpose of jacobian */
  for ( i = 0 ; i < SDIM ; i++ )
     for ( j = 0, xform[i] = 0. ; j < SDIM ; j++ )
        xform[i] += jac[j][i]*yform[j];

}