The VisAO Camera
recmat.cpp
Go to the documentation of this file.
1 /************************************************************
2  * recmat.cpp
3  *
4  * Author: Jared R. Males (jrmales@email.arizona.edu)
5  *
6  * Definitions for a class to manage a reconstructor matrix.
7  *
8  * Developed as part of the Magellan Adaptive Optics system.
9  ************************************************************/
10 
11 /** \file recmat.cpp
12  * \author Jared R. Males
13  * \brief Definitions for a class to manage a reconstructor matrix.
14  *
15  *
16  */
17 
18 #include "recmat.h"
19 
20 recmat::recmat()
21 {
22  n_modes = 0;
23  n_slopes = 0;
24  R = 0;
25  amp = 0;
26 
27  reflection_gain = 2.; //double pass in the tower = 4
28  unit_conversion = 1e9; //meters to nm
29 
30  rec_tech = REC_ATLAS;
31  gpu_inited = 0;
32 }
33 
34 recmat::recmat(std::string fname)
35 {
36  n_modes = 0;
37  n_slopes = 0;
38  R = 0;
39  amp = 0;
40 
41  reflection_gain = 2.; //double pass in the tower
42  unit_conversion = 1e9; //meters to nm
43 
44  rec_tech = REC_ATLAS;
45  gpu_inited = 0;
46 
47  load_recmat_LBT(fname);
48 }
49 
50 recmat::~recmat()
51 {
52  if(R) gsl_matrix_float_free(R);
53 
54  if(amp) delete amp;
55 
56  #ifdef REC_USE_GPU
57  if(gpu_inited) free_gpurecon();
58  #endif
59 }
60 
61 
62 int recmat::load_recmat_LBT(std::string fname)
63 {
64  fitsfile *fptr = 0;
65  int fstatus;
66  int naxis;
67  long naxes[2], fpix[2]={1,1}, lpix[2];
68  int f_nmodes, f_nslopes;
69 
70  std::cout << fname << std::endl;
71 
72  fstatus = 0;
73  fits_open_file(&fptr, fname.c_str(), READONLY, &fstatus);
74 
75  if (fstatus)
76  {
77  fprintf(stderr, "Error in get_open_file.\n");
78  fits_report_error(stderr, fstatus); /* print any error message */
79  return -1;
80  }
81 
82  //If successful, then store the path name.
83  filePath = fname;
84 
85  fits_get_img_dim(fptr, &naxis, &fstatus);
86 
87  if (fstatus)
88  {
89  fprintf(stderr, "Error in get_img_dim.\n");
90  fits_report_error(stderr, fstatus); /* print any error message */
91  return -1;
92  }
93  //Check naxis == 2 here.
94  fits_get_img_size(fptr, naxis, naxes, &fstatus);
95  if (fstatus)
96  {
97  fprintf(stderr, "Error in get_img_size.\n");
98  fits_report_error(stderr, fstatus); /* print any error message */
99 
100  return -1;
101  }
102 
103  f_nmodes = naxes[1];
104  f_nslopes = naxes[0];
105 
106  std::cerr << "Fits image size: " << f_nmodes << " X " << f_nslopes << std::endl;
107 
108  float *fR = new float[f_nmodes*f_nslopes*2];
109 
110  double nm;
111  fits_read_key(fptr, TDOUBLE, "IM_MODES", &nm, 0, &fstatus);
112 
113  std::cerr << "Actual number of modes: " << nm << "\n";
114 
115  n_modes = (int) nm;
116 
117  //n_deforms = n_modes;
118 
119  if(amp) delete amp;
120  amp = new float[n_modes];
121 
122  lpix[0] = f_nslopes;
123  lpix[1] = f_nmodes;
124 
125 
126  get_fits_im(fR, 1, fpix, lpix, &fptr, 0);
127 
128  n_slopes = f_nslopes;
129  for(int k = f_nslopes - 1; k > -1; k--)
130  {
131  if(fR[k] !=0 || fR[1*f_nslopes + k] !=0 || fR[2*f_nslopes + k] !=0)
132  {
133  n_slopes = k+1;
134  break;
135  }
136  }
137 
138  std::cerr << "Actual # slopes " << n_slopes << std::endl;
139 
140  if(R)
141  {
142  gsl_matrix_float_free(R);
143  R = 0;
144  }
145  R = gsl_matrix_float_alloc(n_modes, n_slopes);
146 
147  for(int i=0;i<n_modes;i++)
148  {
149  for(int j=0;j<n_slopes;j++)
150  {
151  gsl_matrix_float_set(R, i, j, fR[i*f_nslopes + j]);
152  }
153  }
154 
155  #ifdef REC_USE_GPU
156  //fR is column major, but is the full size of the fits image, and R->data is the correct size.
157  //So we pass R->data, which is in row-major format.
158 
159  if(gpu_inited) free_gpurecon();
160 
161  init_gpurecon(n_modes, n_slopes, R->data);
162  gpu_inited = 1;
163  #endif
164 
165 
166  fitting_error_sq = fitting_A * pow(n_modes, fitting_B)*pow(tel_diam, 5./3.)/(4.*3.14159*3.14159)*pow(median_r0_lam*1e3/median_r0, 2.);
167 
168  std::cerr << "fitting error squared = " << fitting_error_sq << std::endl;
169 
170  delete fR;
171 
172  fits_close_file(fptr, &fstatus);
173 
174  return 0;
175 }
176 
177 int recmat::reconstruct(float * slopes)
178 {
179  if(rec_tech == REC_ATLAS)
180  {
181  //gsl_blas_sgemv(CblasNoTrans, 1.0, rmat.R, &gslSlopes.vector, 0.0, &gslAmp.vector);
182  cblas_sgemv(CblasRowMajor, CblasNoTrans, n_modes, n_slopes, 1., R->data, n_slopes, slopes, 1, 0., amp, 1);
183  }
184  else if(rec_tech == REC_GPU)
185  {
186  #ifdef REC_USE_GPU
187  gpurecon(slopes, amp);
188  #else
189  std::cerr << "No GPU available.\n";
190  #endif
191  }
192 
193  return 0;
194 }
195 
196 int recmat::calc_sumvar(float *sumvar, int m0, int m1, bool fiterr)
197 {
198  *sumvar = 0.0;
200 
201  if(m1 < 0) m1 = n_modes;
202 
203  for(int j=m0;j<m1; ++j)
204  {
205  *sumvar += fact*amp[j]*amp[j];
206  }
207 
208  if(fiterr) *sumvar += fitting_error_sq;
209 
210  return 0;
211 }
212 
213 /*
214 int main()
215 {
216  recmat rm("bdata/SRTS_wdisturb/Rec_20111125_165625.fits");
217 }*/
218 
int gpu_inited
Flag for whether the gpu globals are initialized.
Definition: recmat.h:53
int n_slopes
The number of slopes, and the columns in the reconstructor matrix.
Definition: recmat.h:57
int rec_tech
Which reconstructor technique to use, either REC_ATLAS or REC_CPU.
Definition: recmat.h:64
std::string filePath
The full path to the reconstructor matrix.
Definition: recmat.h:50
Declarations for a class to manage a reconstructor matrix.
float unit_conversion
Factor to convert variances to nanometers.
Definition: recmat.h:62
float * amp
The reconstructed amplitudes, a vector of length n_modes.
Definition: recmat.h:68
int calc_sumvar(float *sumvar, int m0=2, int m1=-1, bool fiterr=true)
Calculate the sum of the variances, e.g. the dot product of the amplitude vector. ...
Definition: recmat.cpp:196
int n_modes
The number of modes, and the rows in the reconstructor matrix.
Definition: recmat.h:55
int reconstruct(float *slopes)
Given a new slopes vector, reconstruct the amplitudes.
Definition: recmat.cpp:177
float reflection_gain
Factor to apply to reconstructed amplitudes to account for mirror reflection.
Definition: recmat.h:61