1 #ifndef directPhaseReconstructor_hpp
2 #define directPhaseReconstructor_hpp
5 #include "../../improc/eigenImage.hpp"
6 #include "../../improc/eigenCube.hpp"
7 #include "../../ioutils/fits/fitsFile.hpp"
8 using namespace mx::improc;
9 using namespace mx::fits;
11 #include "../../math/cuda/cudaPtr.hpp"
12 #include "../../math/cuda/templateCublas.hpp"
14 #include "../../sigproc/signalWindows.hpp"
17 #define BREAD_CRUMB std::cout << "DEBUG: " << __FILE__ << " " << __LINE__ << "\n";
30 struct directPhaseReconstructorSpec
33 std::string basisName;
41 template<
typename realT>
47 typedef Eigen::Array<realT, -1, -1>
imageT;
50 typedef Eigen::Array<realT, -1, -1>
rmatT;
53 typedef directPhaseReconstructorSpec
specT;
60 imageT * m_pupil {
nullptr};
80 cublasHandle_t *m_cublasHandle;
90 Eigen::Array<realT,-1,-1> m_recon;
93 int m_measurementSize {0};
94 std::vector<size_t> * m_idx {
nullptr};
97 realT m_calAmp {1e-6};
108 template<
typename AOSysT>
109 void initialize( AOSysT & AOSys,
122 void calAmp(realT ca);
143 void loadRecon(
const std::string & fname);
146 int measurementSize();
154 template<
typename measurementT,
typename wfsImageT>
155 void calcMeasurement(measurementT & slopes, wfsImageT & wfsImage);
158 template<
typename measurementT,
typename wfsImageT>
159 void reconstruct(measurementT & commandVect, wfsImageT & wfsImage);
168 void initializeRMat(
int nmodes, realT calamp,
int detrows,
int detcols);
175 template<
typename measurementT>
176 void accumulateRMat(
int i, measurementT &measureVec);
179 template<
typename measurementT,
typename wfsImageT>
180 void accumulateRMat(
int i, measurementT &measureVec, wfsImageT & wfsImage);
186 void saveRMat(std::string fname);
188 void saveRImages(std::string fname);
192 template<
typename realT>
197 template<
typename realT>
198 template<
typename AOSysT>
203 static_cast<void>(spec);
206 m_pupil = &AOSys._pupil;
208 m_nPix = m_pupil->sum();
210 m_modes = &AOSys.dm.m_infF;
212 m_nModes = m_modes->planes();
213 m_detRows = m_modes->rows();
214 m_detCols = m_modes->cols();
216 m_idx = &AOSys.dm.m_idx;
218 m_measurementSize = m_idx->size();
222 m_cublasHandle = &AOSys.m_cublasHandle;
230 m_one.upload(&one, 1);
236 recon.resize(m_nModes, m_measurementSize);
237 for(
int pp=0; pp<m_nModes; ++pp)
239 for(
int nn=0; nn < m_idx->size(); ++nn)
241 recon(pp,nn) = *(m_modes->image(pp).data() + (*m_idx)[nn])/m_nPix;
245 m_devRecon.upload(recon.data(), recon.rows()*recon.cols());
247 m_devSlopes.resize(m_measurementSize);
249 m_devAmps.resize(m_nModes);
253 m_recon.resize(m_measurementSize, m_nModes);
255 for(
int pp=0; pp<m_nModes; ++pp)
257 for(
int nn=0; nn < m_idx->size(); ++nn)
259 m_recon(nn,pp) = *(m_modes->image(pp).data() + (*m_idx)[nn])/m_nPix;
270 template<
typename realT>
273 return 0.5*800.0e-9/math::two_pi<realT>();
276 template<
typename realT>
282 template<
typename realT>
288 template<
typename realT>
294 template<
typename realT>
300 template<
typename realT>
307 ff.
read(m_recon, head, fname);
312 template<
typename realT>
315 return m_measurementSize;
318 template<
typename realT>
319 template<
typename measurementT,
typename wfsImageT>
322 slopes.measurement.resize(m_measurementSize);
324 realT * imp = wfsImage.image.data();
325 for(
int nn=0; nn < m_idx->size(); ++nn)
327 slopes.measurement[nn] = *(imp + (*m_idx)[nn]);
332 template<
typename realT>
333 template<
typename measurementT,
typename wfsImageT>
339 static measurementT slopes;
341 calcMeasurement(slopes, wfsImage);
343 m_devSlopes.upload(slopes.measurement.data(), slopes.measurement.size());
348 cublasStatus_t stat = cuda::cublasTgemv<realT>(*m_cublasHandle, CUBLAS_OP_N, m_nModes, m_measurementSize, m_one(), m_devRecon(), m_devSlopes(), m_zero(), m_devAmps());
349 if(stat != CUBLAS_STATUS_SUCCESS)
351 std::cerr <<
"cublas error\n";
354 commandVect.measurement.resize(m_nModes);
355 m_devAmps.download(commandVect.measurement.data());
357 commandVect.iterNo = wfsImage.iterNo;
368 #pragma omp parallel for
369 for(
int j=0; j< m_nModes; ++j)
376 for(
size_t k=0;
k < m_idx->size(); ++
k)
378 amp += *(wfsImage.image.data() + (*m_idx)[
k]) *m_recon(
k,j);
380 commandVect.measurement[j] = amp;
394 commandVect.iterNo = wfsImage.iterNo;
399 template<
typename realT>
407 m_rMat.resize(measurementSize(), nModes);
410 m_rImages.resize(m_detRows, m_detCols, m_nModes);
413 template<
typename realT>
414 template<
typename measurementT>
418 for(
int j=0; j<measureVec.measurement.rows(); ++j)
420 for(
int k=0;
k<measureVec.measurement.cols(); ++
k)
422 m_rMat(l, i) = measureVec.measurement(j,
k);
430 template<
typename realT>
431 template<
typename measurementT,
typename wfsImageT>
434 accumulateRMat(i, measureVec);
435 m_rImages.image(i) = wfsImage.image;
438 template<
typename realT>
444 head.
append(
"DETROWS", m_detRows,
"WFS detector rows");
445 head.
append(
"DETCOLS", m_detCols,
"WFS detector cols");
446 head.
append(
"CALAMP", m_calAmp,
"DM Calibration amplitude");
447 head.
append(
"NMODES", m_nModes,
"Number of modes included in the response matrix.");
449 ff.
write(fname, m_rMat, head);
452 template<
typename realT>
459 head.
append(
"DETROWS", m_detRows,
"WFS detector rows");
460 head.
append(
"DETCOLS", m_detCols,
"WFS detector cols");
461 head.
append(
"CALAMP", m_calAmp,
"DM Calibration amplitude");
462 head.
append(
"NMODES", m_nModes,
"Number of modes included in the response matrix.");
465 ff.
write(fname, m_rImages, head);
Direct Phase Reconstructor.
void detCols(int dc)
Set the number of detector columns (m_detCols)
Eigen::Array< realT, -1, -1 > imageT
The type of the WFS image.
imageT m_rMat
The response matrix.
directPhaseReconstructorSpec specT
The specificaion type.
Eigen::Array< realT, -1, -1 > rmatT
The type of the response matrix.
void detRows(int dr)
Set the number of detector rows (m_detRows)
Class to manage interactions with a FITS file.
int write(const dataT *im, int d1, int d2, int d3, fitsHeader *head)
Write the contents of a raw array to the FITS file.
int read(dataT *data)
Read the contents of the FITS file into an array.
constexpr units::realT k()
Boltzmann Constant.