NFFT  3.3.2alpha
fastsum_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002, 2016 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
25 #include "config.h"
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <math.h>
31 #ifdef HAVE_COMPLEX_H
32  #include <complex.h>
33 #endif
34 
35 #ifdef _OPENMP
36  #include <omp.h>
37 #endif
38 
39 #include "fastsum.h"
40 #include "kernels.h"
41 #include "infft.h"
42 
49 int main(int argc, char **argv)
50 {
51  int j, k;
52  int d;
53  int N;
54  int M;
55  int n;
56  int m;
57  int p;
58  const char *s;
59  C (*kernel)(R, int, const R *);
60  R c;
61  fastsum_plan my_fastsum_plan;
62  C *direct;
63  ticks t0, t1;
64  R time;
65  R error = K(0.0);
66  R eps_I;
67  R eps_B;
69  if (argc != 11)
70  {
71  printf("\nfastsum_test d N M n m p kernel c eps_I eps_B\n\n");
72  printf(" d dimension \n");
73  printf(" N number of source nodes \n");
74  printf(" M number of target nodes \n");
75  printf(" n expansion degree \n");
76  printf(" m cut-off parameter \n");
77  printf(" p degree of smoothness \n");
78  printf(" kernel kernel function (e.g., gaussian)\n");
79  printf(" c kernel parameter \n");
80  printf(" eps_I inner boundary \n");
81  printf(" eps_B outer boundary \n\n");
82  exit(EXIT_FAILURE);
83  }
84  else
85  {
86  d = atoi(argv[1]);
87  N = atoi(argv[2]);
88  c = K(1.0) / POW((R)(N), K(1.0) / ((R)(d)));
89  M = atoi(argv[3]);
90  n = atoi(argv[4]);
91  m = atoi(argv[5]);
92  p = atoi(argv[6]);
93  s = argv[7];
94  c = (R)(atof(argv[8]));
95  eps_I = (R)(atof(argv[9]));
96  eps_B = (R)(atof(argv[10]));
97  if (strcmp(s, "gaussian") == 0)
98  kernel = gaussian;
99  else if (strcmp(s, "multiquadric") == 0)
100  kernel = multiquadric;
101  else if (strcmp(s, "inverse_multiquadric") == 0)
102  kernel = inverse_multiquadric;
103  else if (strcmp(s, "logarithm") == 0)
104  kernel = logarithm;
105  else if (strcmp(s, "thinplate_spline") == 0)
106  kernel = thinplate_spline;
107  else if (strcmp(s, "one_over_square") == 0)
108  kernel = one_over_square;
109  else if (strcmp(s, "one_over_modulus") == 0)
110  kernel = one_over_modulus;
111  else if (strcmp(s, "one_over_x") == 0)
112  kernel = one_over_x;
113  else if (strcmp(s, "inverse_multiquadric3") == 0)
114  kernel = inverse_multiquadric3;
115  else if (strcmp(s, "sinc_kernel") == 0)
116  kernel = sinc_kernel;
117  else if (strcmp(s, "cosc") == 0)
118  kernel = cosc;
119  else if (strcmp(s, "cot") == 0)
120  kernel = kcot;
121  else
122  {
123  s = "multiquadric";
124  kernel = multiquadric;
125  }
126  }
127  printf(
128  "d=%d, N=%d, M=%d, n=%d, m=%d, p=%d, kernel=%s, c=%" __FGS__ ", eps_I=%" __FGS__ ", eps_B=%" __FGS__ " \n",
129  d, N, M, n, m, p, s, c, eps_I, eps_B);
130 #ifdef NF_KUB
131  printf("nearfield correction using piecewise cubic Lagrange interpolation\n");
132 #elif defined(NF_QUADR)
133  printf("nearfield correction using piecewise quadratic Lagrange interpolation\n");
134 #elif defined(NF_LIN)
135  printf("nearfield correction using piecewise linear Lagrange interpolation\n");
136 #endif
137 
138 #ifdef _OPENMP
139 #pragma omp parallel
140  {
141 #pragma omp single
142  {
143  printf("nthreads=%d\n", omp_get_max_threads());
144  }
145  }
146 
147  FFTW(init_threads)();
148 #endif
149 
151  fastsum_init_guru(&my_fastsum_plan, d, N, M, kernel, &c, 0, n, m, p, eps_I,
152  eps_B);
153  //fastsum_init_guru(&my_fastsum_plan, d, N, M, kernel, &c, NEARFIELD_BOXES, n, m, p, eps_I, eps_B);
154 
155  if (my_fastsum_plan.flags & NEARFIELD_BOXES)
156  printf(
157  "determination of nearfield candidates based on partitioning into boxes\n");
158  else
159  printf("determination of nearfield candidates based on search tree\n");
160 
162  k = 0;
163  while (k < N)
164  {
165  R r_max = K(0.25) - my_fastsum_plan.eps_B / K(2.0);
166  R r2 = K(0.0);
167 
168  for (j = 0; j < d; j++)
169  my_fastsum_plan.x[k * d + j] = K(2.0) * r_max * NFFT(drand48)() - r_max;
170 
171  for (j = 0; j < d; j++)
172  r2 += my_fastsum_plan.x[k * d + j] * my_fastsum_plan.x[k * d + j];
173 
174  if (r2 >= r_max * r_max)
175  continue;
176 
177  k++;
178  }
179 
180  for (k = 0; k < N; k++)
181  {
182  /* R r=(0.25-my_fastsum_plan.eps_B/2.0)*pow((R)rand()/(R)RAND_MAX,1.0/d);
183  my_fastsum_plan.x[k*d+0] = r;
184  for (j=1; j<d; j++)
185  {
186  R phi=2.0*KPI*(R)rand()/(R)RAND_MAX;
187  my_fastsum_plan.x[k*d+j] = r;
188  for (t=0; t<j; t++)
189  {
190  my_fastsum_plan.x[k*d+t] *= cos(phi);
191  }
192  my_fastsum_plan.x[k*d+j] *= sin(phi);
193  }
194  */
195  my_fastsum_plan.alpha[k] = NFFT(drand48)() + II * NFFT(drand48)();
196  }
197 
199  k = 0;
200  while (k < M)
201  {
202  R r_max = K(0.25) - my_fastsum_plan.eps_B / K(2.0);
203  R r2 = K(0.0);
204 
205  for (j = 0; j < d; j++)
206  my_fastsum_plan.y[k * d + j] = K(2.0) * r_max * NFFT(drand48)() - r_max;
207 
208  for (j = 0; j < d; j++)
209  r2 += my_fastsum_plan.y[k * d + j] * my_fastsum_plan.y[k * d + j];
210 
211  if (r2 >= r_max * r_max)
212  continue;
213 
214  k++;
215  }
216  /* for (k=0; k<M; k++)
217  {
218  R r=(0.25-my_fastsum_plan.eps_B/2.0)*pow((R)rand()/(R)RAND_MAX,1.0/d);
219  my_fastsum_plan.y[k*d+0] = r;
220  for (j=1; j<d; j++)
221  {
222  R phi=2.0*KPI*(R)rand()/(R)RAND_MAX;
223  my_fastsum_plan.y[k*d+j] = r;
224  for (t=0; t<j; t++)
225  {
226  my_fastsum_plan.y[k*d+t] *= cos(phi);
227  }
228  my_fastsum_plan.y[k*d+j] *= sin(phi);
229  }
230  } */
231 
233  printf("direct computation: ");
234  fflush(NULL);
235  t0 = getticks();
236  fastsum_exact(&my_fastsum_plan);
237  t1 = getticks();
238  time = NFFT(elapsed_seconds)(t1, t0);
239  printf(__FI__ "sec\n", time);
240 
242  direct = (C *) NFFT(malloc)((size_t)(my_fastsum_plan.M_total) * (sizeof(C)));
243  for (j = 0; j < my_fastsum_plan.M_total; j++)
244  direct[j] = my_fastsum_plan.f[j];
245 
247  printf("pre-computation: ");
248  fflush(NULL);
249  t0 = getticks();
250  fastsum_precompute(&my_fastsum_plan);
251  t1 = getticks();
252  time = NFFT(elapsed_seconds)(t1, t0);
253  printf(__FI__ "sec\n", time);
254 
256  printf("fast computation: ");
257  fflush(NULL);
258  t0 = getticks();
259  fastsum_trafo(&my_fastsum_plan);
260  t1 = getticks();
261  time = NFFT(elapsed_seconds)(t1, t0);
262  printf(__FI__ "sec\n", time);
263 
265  error = K(0.0);
266  for (j = 0; j < my_fastsum_plan.M_total; j++)
267  {
268  if (CABS(direct[j] - my_fastsum_plan.f[j]) / CABS(direct[j]) > error)
269  error = CABS(direct[j] - my_fastsum_plan.f[j]) / CABS(direct[j]);
270  }
271  printf("max relative error: %" __FES__ "\n", error);
272 
274  fastsum_finalize(&my_fastsum_plan);
275 
276  return EXIT_SUCCESS;
277 }
278 /* \} */
fastsum_plan_::f
C * f
target evaluations
Definition: fastsum.h:82
fastsum_plan_::y
R * y
target knots in d-ball with radius 1/4-eps_b/2
Definition: fastsum.h:85
fastsum_plan_::x
R * x
source knots in d-ball with radius 1/4-eps_b/2
Definition: fastsum.h:84
fastsum_plan_::alpha
C * alpha
source coefficients
Definition: fastsum.h:81
fastsum_plan_::flags
unsigned flags
flags precomp.
Definition: fastsum.h:90
kernels.h
Header file with predefined kernels for the fast summation algorithm.
fastsum_plan_
plan for fast summation algorithm
Definition: fastsum.h:72
fastsum_init_guru
void fastsum_init_guru(fastsum_plan *ths, int d, int N_total, int M_total, kernel k, R *param, unsigned flags, int nn, int m, int p, R eps_I, R eps_B)
initialization of fastsum plan
Definition: fastsum.c:691
fastsum.h
Header file for the fast NFFT-based summation algorithm.
fastsum_finalize
void fastsum_finalize(fastsum_plan *ths)
finalization of fastsum plan
Definition: fastsum.c:844
fastsum_trafo
void fastsum_trafo(fastsum_plan *ths)
fast NFFT-based summation
Definition: fastsum.c:1055
fastsum_plan_::eps_B
R eps_B
outer boundary
Definition: fastsum.h:103
fastsum_exact
void fastsum_exact(fastsum_plan *ths)
direct computation of sums
Definition: fastsum.c:877
fastsum_precompute
void fastsum_precompute(fastsum_plan *ths)
precomputation for fastsum
Definition: fastsum.c:907
fastsum_plan_::M_total
int M_total
number of target knots
Definition: fastsum.h:79