NFFT Logo 3.2.3
glacier.c
1 /*
2  * Copyright (c) 2002, 2012 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 
19 /* $Id: glacier.c 3896 2012-10-10 12:19:26Z tovo $ */
20 #include "config.h"
21 
22 #include <stdio.h>
23 #include <math.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #ifdef HAVE_COMPLEX_H
27 #include <complex.h>
28 #endif
29 
30 #include "nfft3util.h"
31 #include "nfft3.h"
32 #include "infft.h"
33 
42 static double my_weight(double z,double a,double b,double c)
43 {
44  return pow(0.25-z*z,b)/(c+pow(fabs(z),2*a));
45 }
46 
48 static void glacier(int N,int M)
49 {
50  int j,k,k0,k1,l,my_N[2],my_n[2];
51  double tmp_y;
52  nfft_plan p;
54  FILE* fp;
55 
56  /* initialise p */
57  my_N[0]=N; my_n[0]=X(next_power_of_2)(N);
58  my_N[1]=N; my_n[1]=X(next_power_of_2)(N);
59  nfft_init_guru(&p, 2, my_N, M, my_n, 6,
60  PRE_PHI_HUT| PRE_FULL_PSI|
61  MALLOC_X| MALLOC_F_HAT| MALLOC_F|
62  FFTW_INIT| FFT_OUT_OF_PLACE,
63  FFTW_MEASURE| FFTW_DESTROY_INPUT);
64 
65  /* initialise ip, specific */
66  solver_init_advanced_complex(&ip,(nfft_mv_plan_complex*)(&p), CGNE| PRECOMPUTE_DAMP);
67  fprintf(stderr,"Using the generic solver!");
68 
69  /* init nodes */
70  fp=fopen("input_data.dat","r");
71  for(j=0;j<p.M_total;j++)
72  {
73  fscanf(fp,"%le %le %le",&p.x[2*j+0],&p.x[2*j+1],&tmp_y);
74  ip.y[j]=tmp_y;
75  }
76  fclose(fp);
77 
78  /* precompute psi */
79  if(p.nfft_flags & PRE_ONE_PSI)
80  nfft_precompute_one_psi(&p);
81 
82  /* initialise damping factors */
83  if(ip.flags & PRECOMPUTE_DAMP)
84  for(k0=0;k0<p.N[0];k0++)
85  for(k1=0;k1<p.N[1];k1++)
86  ip.w_hat[k0*p.N[1]+k1]=
87  my_weight(((double)(k0-p.N[0]/2))/p.N[0],0.5,3,0.001)*
88  my_weight(((double)(k1-p.N[1]/2))/p.N[1],0.5,3,0.001);
89 
90  /* init some guess */
91  for(k=0;k<p.N_total;k++)
92  ip.f_hat_iter[k]=0;
93 
94  /* inverse trafo */
95  solver_before_loop_complex(&ip);
96  for(l=0;l<40;l++)
97  {
98  fprintf(stderr,"Residual ||r||=%e,\n",sqrt(ip.dot_r_iter));
100  }
101 
102  for(k=0;k<p.N_total;k++)
103  printf("%le %le\n",creal(ip.f_hat_iter[k]),cimag(ip.f_hat_iter[k]));
104 
106  nfft_finalize(&p);
107 }
108 
110 static void glacier_cv(int N,int M,int M_cv,unsigned solver_flags)
111 {
112  int j,k,k0,k1,l,my_N[2],my_n[2];
113  double tmp_y,r;
114  nfft_plan p,cp;
116  double _Complex* cp_y;
117  FILE* fp;
118  int M_re=M-M_cv;
119 
120  /* initialise p for reconstruction */
121  my_N[0]=N; my_n[0]=X(next_power_of_2)(N);
122  my_N[1]=N; my_n[1]=X(next_power_of_2)(N);
123  nfft_init_guru(&p, 2, my_N, M_re, my_n, 6,
124  PRE_PHI_HUT| PRE_FULL_PSI|
125  MALLOC_X| MALLOC_F_HAT| MALLOC_F|
126  FFTW_INIT| FFT_OUT_OF_PLACE,
127  FFTW_MEASURE| FFTW_DESTROY_INPUT);
128 
129 
130  /* initialise ip, specific */
131  solver_init_advanced_complex(&ip,(nfft_mv_plan_complex*)(&p), solver_flags);
132 
133  /* initialise cp for validation */
134  cp_y = (double _Complex*) nfft_malloc(M*sizeof(double _Complex));
135  nfft_init_guru(&cp, 2, my_N, M, my_n, 6,
136  PRE_PHI_HUT| PRE_FULL_PSI|
137  MALLOC_X| MALLOC_F|
138  FFTW_INIT| FFT_OUT_OF_PLACE,
139  FFTW_MEASURE| FFTW_DESTROY_INPUT);
140 
141  cp.f_hat=ip.f_hat_iter;
142 
143  /* set up data in cp and cp_y */
144  fp=fopen("input_data.dat","r");
145  for(j=0;j<cp.M_total;j++)
146  {
147  fscanf(fp,"%le %le %le",&cp.x[2*j+0],&cp.x[2*j+1],&tmp_y);
148  cp_y[j]=tmp_y;
149  }
150  fclose(fp);
151 
152  /* copy part of the data to p and ip */
153  for(j=0;j<p.M_total;j++)
154  {
155  p.x[2*j+0]=cp.x[2*j+0];
156  p.x[2*j+1]=cp.x[2*j+1];
157  ip.y[j]=tmp_y;
158  }
159 
160  /* precompute psi */
161  if(p.nfft_flags & PRE_ONE_PSI)
162  nfft_precompute_one_psi(&p);
163 
164  /* precompute psi */
165  if(cp.nfft_flags & PRE_ONE_PSI)
166  nfft_precompute_one_psi(&cp);
167 
168  /* initialise damping factors */
169  if(ip.flags & PRECOMPUTE_DAMP)
170  for(k0=0;k0<p.N[0];k0++)
171  for(k1=0;k1<p.N[1];k1++)
172  ip.w_hat[k0*p.N[1]+k1]=
173  my_weight(((double)(k0-p.N[0]/2))/p.N[0],0.5,3,0.001)*
174  my_weight(((double)(k1-p.N[1]/2))/p.N[1],0.5,3,0.001);
175 
176  /* init some guess */
177  for(k=0;k<p.N_total;k++)
178  ip.f_hat_iter[k]=0;
179 
180  /* inverse trafo */
181  solver_before_loop_complex(&ip);
182  // fprintf(stderr,"iteration starts,\t");
183  for(l=0;l<40;l++)
185 
186  //fprintf(stderr,"r=%1.2e, ",sqrt(ip.dot_r_iter)/M_re);
187 
189  nfft_trafo(&p);
191  nfft_upd_axpy_complex(p.f,-1,ip.y,M_re);
192  r=sqrt(nfft_dot_complex(p.f,M_re)/nfft_dot_complex(cp_y,M));
193  fprintf(stderr,"r=%1.2e, ",r);
194  printf("$%1.1e$ & ",r);
195 
196  nfft_trafo(&cp);
197  nfft_upd_axpy_complex(&cp.f[M_re],-1,&cp_y[M_re],M_cv);
198  r=sqrt(nfft_dot_complex(&cp.f[M_re],M_cv)/nfft_dot_complex(cp_y,M));
199  fprintf(stderr,"r_1=%1.2e\t",r);
200  printf("$%1.1e$ & ",r);
201 
202  nfft_finalize(&cp);
204  nfft_finalize(&p);
205 }
206 
207 
209 int main(int argc, char **argv)
210 {
211  int M_cv;
212 
213  if(argc<3)
214  {
215  fprintf(stderr,"Call this program from the Matlab script glacier.m!");
216  exit(-1);
217  }
218 
219  if(argc==3)
220  glacier(atoi(argv[1]),atoi(argv[2]));
221  else
222  for(M_cv=atoi(argv[3]);M_cv<=atoi(argv[5]);M_cv+=atoi(argv[4]))
223  {
224  fprintf(stderr,"\nM_cv=%d,\t",M_cv);
225  printf("$%d$ & ",M_cv);
226  fprintf(stderr,"cgne+damp: ");
227  glacier_cv(atoi(argv[1]),atoi(argv[2]),M_cv,CGNE| PRECOMPUTE_DAMP);
228  //fprintf(stderr,"cgne: ");
229  //glacier_cv(atoi(argv[1]),atoi(argv[2]),M_cv,CGNE);
230  fprintf(stderr,"cgnr: ");
231  glacier_cv(atoi(argv[1]),atoi(argv[2]),M_cv,CGNR);
232  fprintf(stderr,"cgnr: ");
233  glacier_cv(atoi(argv[1])/4,atoi(argv[2]),M_cv,CGNR);
234  printf("XXX \\\\\n");
235  }
236 
237  fprintf(stderr,"\n");
238 
239  return 1;
240 }
241 /* \} */

Generated on Tue Apr 30 2013 by Doxygen 1.8.1