Nek5000
SEM for Incompressible NS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
findpts_test.c
Go to the documentation of this file.
1 #include <stddef.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <float.h>
6 #include <math.h>
7 #include "c99.h"
8 #include "name.h"
9 #include "fail.h"
10 #include "types.h"
11 #include "mem.h"
12 #include "poly.h"
13 #include "gs_defs.h"
14 #include "comm.h"
15 #include "rand_elt_test.h"
16 #include "findpts.h"
17 #include "crystal.h"
18 #include "sarray_transfer.h"
19 
20 #define D 3
21 
22 #if D==3
23 #define INITD(a,b,c) {a,b,c}
24 #define MULD(a,b,c) ((a)*(b)*(c))
25 #define INDEXD(a,na, b,nb, c) (((c)*(nb)+(b))*(na)+(a))
26 #define findpts_data findpts_data_3
27 #define findpts_setup findpts_setup_3
28 #define findpts_free findpts_free_3
29 #define findpts findpts_3
30 #define findpts_eval findpts_eval_3
31 #elif D==2
32 #define INITD(a,b,c) {a,b}
33 #define MULD(a,b,c) ((a)*(b))
34 #define INDEXD(a,na, b,nb, c) ((b)*(na)+(a))
35 #define findpts_data findpts_data_2
36 #define findpts_setup findpts_setup_2
37 #define findpts_free findpts_free_2
38 #define findpts findpts_2
39 #define findpts_eval findpts_eval_2
40 #endif
41 
42 #define NR 5
43 #define NS 7
44 #define NT 6
45 #define K 4
46 #define NEL MULD(K,K,K)
47 #define TN 4
48 
49 #define NPT_MAX 256
50 #define BBOX_TOL 0.01
51 #define NEWT_TOL 1024*DBL_EPSILON
52 #define LOC_HASH_SIZE NEL*MULD(NR,NS,NT)
53 #define GBL_HASH_SIZE NEL*MULD(NR,NS,NT)
54 
55 /*
56 #define NPT_MAX 256
57 #define BBOX_TOL 1.00
58 #define NEWT_TOL 1024*DBL_EPSILON
59 #define LOC_HASH_SIZE NEL*3
60 #define GBL_HASH_SIZE NEL*3
61 */
62 
63 static uint np, id;
64 
65 static const unsigned nr[D] = INITD(NR,NS,NT);
66 static const unsigned mr[D] = INITD(2*NR,2*NS,2*NT);
67 static double zr[NR], zs[NS], zt[NT];
68 static double x3[D][MULD(3,3,3)];
69 static double mesh[D][NEL*MULD(NR,NS,NT)];
70 static const double *const elx[D] = INITD(mesh[0],mesh[1],mesh[2]);
71 
72 struct pt_data { double x[D], r[D], dist2, ex[D]; uint code, proc, el; };
73 static struct array testp;
74 
75 static struct crystal cr;
76 
77 static double quad_eval(const double coef[MULD(3,3,3)], const double r[D])
78 {
79  double lr0[D], lr1[D], lr2[D];
80  unsigned d;
81  for(d=0;d<D;++d) lr0[d]=r[d]*(r[d]-1)/2,
82  lr1[d]=(1+r[d])*(1-r[d]),
83  lr2[d]=r[d]*(r[d]+1)/2;
84  #define EVALR(base) ( coef [base ]*lr0[0] \
85  +coef [base+ 1]*lr1[0] \
86  +coef [base+ 2]*lr2[0] )
87  #define EVALS(base) ( EVALR(base )*lr0[1] \
88  +EVALR(base+ 3)*lr1[1] \
89  +EVALR(base+ 6)*lr2[1] )
90  #define EVALT(base) ( EVALS(base )*lr0[2] \
91  +EVALS(base+ 9)*lr1[2] \
92  +EVALS(base+18)*lr2[2] )
93  #if D==2
94  # define EVAL() EVALS(0)
95  #elif D==3
96  # define EVAL() EVALT(0)
97  #endif
98 
99  return EVAL();
100 
101  #undef EVAL
102  #undef EVALT
103  #undef EVALS
104  #undef EVALR
105 }
106 
107 static void rand_mesh(void)
108 {
109  const uint pn = ceil(pow(np,1.0/D));
110  const uint pi=id%pn, pj=(id/pn)%pn;
111  #if D==3
112  const uint pk=(id/pn)/pn;
113  #endif
114  const double pfac = 1.0/pn;
115  const double pbase[D] = INITD(-1+2*pfac*pi, -1+2*pfac*pj, -1+2*pfac*pk);
116  const double fac = 1.0/K;
117  const double z3[3] = {-1,0,1};
118  unsigned ki,kj;
119  #if D==3
120  unsigned kk;
121  rand_elt_3(x3[0],x3[1],x3[2], z3,3,z3,3,z3,3);
122  #elif D==2
123  rand_elt_2(x3[0],x3[1], z3,3,z3,3);
124  #endif
125  if(id==0) printf("Global division: %u^%d\n",(unsigned)pn,D);
126  #if D==3
127  for(kk=0;kk<K;++kk)
128  #endif
129  for(kj=0;kj<K;++kj) for(ki=0;ki<K;++ki) {
130  unsigned off = INDEXD(ki,K, kj,K, kk)*MULD(NR,NS,NT);
131  unsigned i,j;
132  double r[D], base[D] = INITD(-1+2*fac*ki,-1+2*fac*kj,-1+2*fac*kk);
133  #if D==3
134  unsigned k;
135  for(k=0;k<NT;++k) { r[2]=pbase[2]+pfac*(1+base[2]+fac*(1+zt[k]));
136  #endif
137  for(j=0;j<NS;++j) { r[1]=pbase[1]+pfac*(1+base[1]+fac*(1+zs[j]));
138  for(i=0;i<NR;++i) { r[0]=pbase[0]+pfac*(1+base[0]+fac*(1+zr[i]));
139  mesh[0][off+INDEXD(i,NR, j,NS, k)] = quad_eval(x3[0],r);
140  mesh[1][off+INDEXD(i,NR, j,NS, k)] = quad_eval(x3[1],r);
141  #if D==3
142  mesh[2][off+INDEXD(i,NR, j,NS, k)] = quad_eval(x3[2],r);
143  }
144  #endif
145  }}
146  }
147 }
148 
149 static void test_mesh(void)
150 {
151  const uint pn = ceil(pow(np,1.0/D));
152  const uint pi=id%pn, pj=(id/pn)%pn;
153  #if D==3
154  const uint pk=(id/pn)/pn;
155  #endif
156  const double pfac = 1.0/pn;
157  const double pbase[D] = INITD(-1+2*pfac*pi, -1+2*pfac*pj, -1+2*pfac*pk);
158  const double fac = 1.0/K, step = 2.0/(K*(TN-1));
159  unsigned ki,kj;
160  #if D==3
161  unsigned kk;
162  #endif
163  struct pt_data *out = testp.ptr;
164  testp.n = NEL*MULD(TN,TN,TN);
165  memset(testp.ptr,0,testp.n*sizeof(struct pt_data));
166  #if D==3
167  for(kk=0;kk<K;++kk)
168  #endif
169  for(kj=0;kj<K;++kj) for(ki=0;ki<K;++ki) {
170  unsigned i,j;
171  double r[D], base[D] = INITD(-1+2*fac*ki,-1+2*fac*kj,-1+2*fac*kk);
172  #if D==3
173  unsigned k;
174  for(k=0;k<TN;++k) { r[2] = pbase[2]+pfac*(1+base[2]+step*k);
175  #endif
176  for(j=0;j<TN;++j) { r[1] = pbase[1]+pfac*(1+base[1]+step*j);
177  for(i=0;i<TN;++i) { r[0] = pbase[0]+pfac*(1+base[0]+step*i);
178  out->proc = rand()%np;
179  out->x[0] = quad_eval(x3[0],r);
180  out->x[1] = quad_eval(x3[1],r);
181  #if D==3
182  out->x[2] = quad_eval(x3[2],r);
183  #endif
184  ++out;
185  }}
186  #if D==3
187  }
188  #endif
189  }
190  sarray_transfer(struct pt_data,&testp,proc,1,&cr);
191  if(0)
192  printf("%u: %u shuffled points\n",id,(unsigned)testp.n);
193 }
194 
195 static void print_ptdata(const struct comm *const comm)
196 {
197  uint notfound=0;
198  double dist2max=0, ed2max=0;
199  const struct pt_data *pt = testp.ptr, *const end = pt+testp.n;
200  for(;pt!=end;++pt) {
201  if(0&&id==0)
202  printf("code=%u, proc=%u, el=%u, dist2=%g, r=(%.17g,%.17g"
203  #if D==3
204  ",%.17g"
205  #endif
206  "), "
207  "x=(%.17g,%.17g"
208  #if D==3
209  ",%.17g"
210  #endif
211  "), ex=(%.17g,%.17g"
212  #if D==3
213  ",%.17g"
214  #endif
215  ")\n",
216  pt->code,pt->proc,pt->el,pt->dist2,
217  pt->r[0],pt->r[1],
218  #if D==3
219  pt->r[2],
220  #endif
221  pt->x[0],pt->x[1],
222  #if D==3
223  pt->x[2],
224  #endif
225  pt->ex[0],pt->ex[1]
226  #if D==3
227  ,pt->ex[2]
228  #endif
229  );
230  if(pt->code==2) ++notfound;
231  else {
232  double ed2=0, dx;
233  unsigned d; for(d=0;d<D;++d) dx=pt->x[d]-pt->ex[d], ed2+=dx*dx;
234  dist2max=pt->dist2>dist2max?pt->dist2:dist2max;
235  ed2max=ed2>ed2max?ed2:ed2max;
236  }
237  }
238  {
239  double distmax=sqrt(dist2max), edmax=sqrt(ed2max);
240  slong total=testp.n;
241  if(0)
242  printf("%u: maximum distance = %g (adv), %g (eval);"
243  " %u/%u points not found\n",
244  (unsigned)id, distmax, edmax,
245  (unsigned)notfound, (unsigned)testp.n);
246  distmax = comm_reduce_double(comm,gs_max,&distmax,1);
247  edmax = comm_reduce_double(comm,gs_max,&edmax ,1);
248  notfound = comm_reduce_sint(comm,gs_add,(sint*)&notfound,1);
249  total = comm_reduce_slong(comm,gs_add,&total,1);
250  if(id==0)
251  printf("maximum distance = %g (adv), %g (eval);"
252  " %u/%lu points not found\n",
253  distmax, edmax,
254  (unsigned)notfound, (unsigned long)total);
255  }
256 }
257 
258 static void test(const struct comm *const comm)
259 {
260  const double *x_base[D];
261  const unsigned x_stride[D] = INITD(sizeof(struct pt_data),
262  sizeof(struct pt_data),
263  sizeof(struct pt_data));
264  struct findpts_data *fd;
265  struct pt_data *pt;
266  unsigned d;
267  if(id==0) printf("Initializing mesh\n");
268  rand_mesh();
269  test_mesh();
270  pt = testp.ptr;
271  if(id==0) printf("calling findpts_setup\n");
272  fd=findpts_setup(comm,elx,nr,NEL,mr,BBOX_TOL,
274  NPT_MAX,NEWT_TOL);
275  if(id==0) printf("calling findpts\n");
276  x_base[0]=pt->x, x_base[1]=pt->x+1;
277  #if D==3
278  x_base[2]=pt->x+2;
279  #endif
280  findpts(&pt->code , sizeof(struct pt_data),
281  &pt->proc , sizeof(struct pt_data),
282  &pt->el , sizeof(struct pt_data),
283  pt->r , sizeof(struct pt_data),
284  &pt->dist2, sizeof(struct pt_data),
285  x_base , x_stride, testp.n, fd);
286  for(d=0;d<D;++d) {
287  if(id==0) printf("calling findpts_eval (%u)\n",d);
288  findpts_eval(&pt->ex[d], sizeof(struct pt_data),
289  &pt->code , sizeof(struct pt_data),
290  &pt->proc , sizeof(struct pt_data),
291  &pt->el , sizeof(struct pt_data),
292  pt->r , sizeof(struct pt_data),
293  testp.n, mesh[d], fd);
294  }
295  findpts_free(fd);
296  print_ptdata(comm);
297 }
298 
299 int main(int narg, char *arg[])
300 {
301  comm_ext world;
302  struct comm comm;
303 
304 #ifdef MPI
305  MPI_Init(&narg,&arg);
306  world = MPI_COMM_WORLD;
307 #else
308  world=0;
309 #endif
310 
311  comm_init(&comm,world);
312  id=comm.id, np=comm.np;
313 
315  array_init(struct pt_data,&testp,NEL*MULD(TN,TN,TN));
316  crystal_init(&cr,&comm);
317  test(&comm);
318  crystal_free(&cr);
319  array_free(&testp);
320 
321  comm_free(&comm);
322 
323 #ifdef MPI
324  MPI_Finalize();
325 #endif
326 
327  return 0;
328 }
static const double *const elx[D]
Definition: findpts_test.c:70
#define slong
Definition: types.h:74
#define findpts
Definition: findpts_test.c:29
#define uint
Definition: types.h:70
size_t n
Definition: mem.h:111
int main(int narg, char *arg[])
Definition: findpts_test.c:299
#define sint
Definition: types.h:69
double r[D]
#define findpts_setup
Definition: findpts_test.c:27
static double quad_eval(const double coef[MULD(3, 3, 3)], const double r[D])
Definition: findpts_test.c:77
static double zt[NT]
Definition: findpts_test.c:67
#define NR
Definition: findpts_test.c:42
static double zr[NR]
Definition: findpts_test.c:67
#define GBL_HASH_SIZE
Definition: findpts_test.c:53
void rand_elt_2(double *x, double *y, const double *zr, unsigned nr, const double *zs, unsigned ns)
Definition: rand_elt_test.c:70
#define NEWT_TOL
Definition: findpts_test.c:51
#define LOC_HASH_SIZE
Definition: findpts_test.c:52
#define crystal_free
Definition: crystal.c:47
#define BBOX_TOL
Definition: findpts_test.c:50
#define INITD(a, b, c)
Definition: findpts_test.c:23
#define crystal_init
Definition: crystal.c:46
static void test_mesh(void)
Definition: findpts_test.c:149
Definition: comm.h:85
static void comm_free(struct comm *c)
Definition: comm.h:176
#define NPT_MAX
Definition: findpts_test.c:49
static void rand_mesh(void)
Definition: findpts_test.c:107
static double x3[D][MULD(3, 3, 3)]
Definition: findpts_test.c:68
static const unsigned nr[D]
Definition: findpts_test.c:65
static struct array testp
Definition: findpts_test.c:73
cleaned
Definition: mesh_mod.F90:2
static void test(const struct comm *const comm)
Definition: findpts_test.c:258
#define findpts_free
Definition: findpts_test.c:28
static struct crystal cr
Definition: findpts_test.c:75
void rand_elt_3(double *x, double *y, double *z, const double *zr, unsigned nr, const double *zs, unsigned ns, const double *zt, unsigned nt)
int comm_ext
Definition: comm.h:69
uint np
Definition: comm.h:86
Definition: mem.h:111
uint proc
Definition: findpts_test.c:72
#define EVAL()
#define INDEXD(a, na, b, nb, c)
Definition: findpts_test.c:25
#define array_init(T, a, max)
Definition: mem.h:136
#define K
Definition: findpts_test.c:45
#define NS
Definition: findpts_test.c:43
#define NEL
Definition: findpts_test.c:46
double x[D]
Definition: findpts_test.c:72
for i
Definition: xxt_test.m:74
#define D
Definition: findpts_test.c:20
static double zs[NS]
Definition: findpts_test.c:67
#define MULD(a, b, c)
Definition: findpts_test.c:24
void * ptr
Definition: mem.h:111
static uint id
Definition: findpts_test.c:63
#define array_free(a)
Definition: mem.h:135
uint id
Definition: comm.h:86
ulong out[N]
Definition: sort_test2.c:20
#define findpts_eval
Definition: findpts_test.c:30
#define NT
Definition: findpts_test.c:44
#define sarray_transfer(T, A, proc_field, set_src, cr)
#define lobatto_nodes
Definition: poly.c:15
static void print_ptdata(const struct comm *const comm)
Definition: findpts_test.c:195
establishes some macros to establish naming conventions
static const unsigned mr[D]
Definition: findpts_test.c:66
static uint np
Definition: findpts_test.c:63
static void comm_init(struct comm *c, comm_ext ce)
Definition: comm.h:133
#define TN
Definition: findpts_test.c:47
double ex[D]
Definition: findpts_test.c:72