NFFT  3.3.0
reconstruct_data_3d.c
1 /*
2  * Copyright (c) 2002, 2015 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$ */
20 #include "config.h"
21 
22 #include <math.h>
23 #include <stdlib.h>
24 #ifdef HAVE_COMPLEX_H
25 #include <complex.h>
26 #endif
27 
28 #include "nfft3.h"
29 
39 static void reconstruct(char* filename,int N,int M,int Z,int iteration, int weight)
40 {
41  int j,k,z,l; /* some variables */
42  double real,imag; /* to read the real and imag part of a complex number */
43  nfft_plan my_plan; /* plan for the two dimensional nfft */
44  solver_plan_complex my_iplan; /* plan for the two dimensional infft */
45  FILE* fin; /* input file */
46  FILE* fout_real; /* output file (real part) */
47  FILE* fout_imag; /* output file (imag part) */
48  int my_N[3],my_n[3]; /* to init the nfft */
49  double epsilon=0.0000003; /* tmp to read the obsolent z from 700.acs
50  epsilon is a the break criterion for
51  the iteration */
52  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP; /* flags for the infft */
53 
54  /* initialise my_plan, specific.
55  we don't precompute psi */
56  my_N[0]=Z; my_n[0]=ceil(Z*1.2);
57  my_N[1]=N; my_n[1]=ceil(N*1.2);
58  my_N[2]=N; my_n[2]=ceil(N*1.2);
59  nfft_init_guru(&my_plan, 3, my_N, M, my_n, 6,
60  PRE_PHI_HUT| PRE_PSI |MALLOC_X| MALLOC_F_HAT|
61  MALLOC_F| FFTW_INIT| FFT_OUT_OF_PLACE,
62  FFTW_MEASURE| FFTW_DESTROY_INPUT);
63 
64  /* precompute lin psi */
65  if(my_plan.flags & PRE_LIN_PSI)
66  nfft_precompute_lin_psi(&my_plan);
67 
68  if (weight)
69  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
70 
71  /* initialise my_iplan, advanced */
72  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
73 
74  /* get the weights */
75  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
76  {
77  fin=fopen("weights.dat","r");
78  for(j=0;j<M;j++)
79  {
80  fscanf(fin,"%le ",&my_iplan.w[j]);
81  }
82  fclose(fin);
83  }
84 
85  /* get the damping factors */
86  if(my_iplan.flags & PRECOMPUTE_DAMP)
87  {
88  for(j=0;j<N;j++){
89  for(k=0;k<N;k++) {
90  for(z=0;z<N;z++) {
91  int j2= j-N/2;
92  int k2= k-N/2;
93  int z2= z-N/2;
94  double r=sqrt(j2*j2+k2*k2+z2*z2);
95  if(r>(double) N/2)
96  my_iplan.w_hat[z*N*N+j*N+k]=0.0;
97  else
98  my_iplan.w_hat[z*N*N+j*N+k]=1.0;
99  }
100  }
101  }
102  }
103 
104  /* open the input file */
105  fin=fopen(filename,"r");
106 
107  /* open the output files */
108  fout_real=fopen("output_real.dat","w");
109  fout_imag=fopen("output_imag.dat","w");
110 
111  /* read x,y,freal and fimag from the knots */
112  for(j=0;j<M;j++)
113  {
114  fscanf(fin,"%le %le %le %le %le ",&my_plan.x[3*j+1],&my_plan.x[3*j+2], &my_plan.x[3*j+0],
115  &real,&imag);
116  my_iplan.y[j] = real + _Complex_I*imag;
117  }
118 
119  /* precompute psi */
120  if(my_plan.flags & PRE_PSI)
121  nfft_precompute_psi(&my_plan);
122 
123  /* precompute full psi */
124  if(my_plan.flags & PRE_FULL_PSI)
125  nfft_precompute_full_psi(&my_plan);
126 
127  /* init some guess */
128  for(k=0;k<my_plan.N_total;k++)
129  my_iplan.f_hat_iter[k]=0.0;
130 
131  /* inverse trafo */
132  solver_before_loop_complex(&my_iplan);
133  for(l=0;l<iteration;l++)
134  {
135  /* break if dot_r_iter is smaller than epsilon*/
136  if(my_iplan.dot_r_iter<epsilon)
137  break;
138  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
139  l+1,iteration);
140  solver_loop_one_step_complex(&my_iplan);
141  }
142 
143  for(l=0;l<Z;l++)
144  {
145  for(k=0;k<N*N;k++)
146  {
147  /* write every Layer in the files */
148  fprintf(fout_real,"%le ",creal(my_iplan.f_hat_iter[ k+N*N*l ]));
149  fprintf(fout_imag,"%le ",cimag(my_iplan.f_hat_iter[ k+N*N*l ]));
150  }
151  fprintf(fout_real,"\n");
152  fprintf(fout_imag,"\n");
153  }
154 
155  fclose(fout_real);
156  fclose(fout_imag);
157 
158  solver_finalize_complex(&my_iplan);
159  nfft_finalize(&my_plan);
160 }
161 
162 int main(int argc, char **argv)
163 {
164  if (argc <= 6) {
165  printf("usage: ./reconstruct3D FILENAME N M Z ITER WEIGHTS\n");
166  return 1;
167  }
168 
169  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),atoi(argv[6]));
170  return 1;
171 }
172 /* \} */
double * w
weighting factors
Definition: nfft3.h:786
unsigned flags
iteration type
Definition: nfft3.h:786
double dot_r_iter
weighted dotproduct of r_iter
Definition: nfft3.h:786
data structure for an NFFT (nonequispaced fast Fourier transform) plan with double precision ...
Definition: nfft3.h:194
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:194
static void reconstruct(char *filename, int N, int M, int Z, int iteration, int weight)
reconstruct makes an inverse 3d-nfft
fftw_complex * y
right hand side, samples
Definition: nfft3.h:786
double * x
Nodes in time/spatial domain, size is doubles.
Definition: nfft3.h:194
unsigned flags
Flags for precomputation, (de)allocation, and FFTW usage, default setting is PRE_PHI_HUT | PRE_PSI | ...
Definition: nfft3.h:194
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:786
double * w_hat
damping factors
Definition: nfft3.h:786
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:786