Citlali
Loading...
Searching...
No Matches
ml_mm.h
Go to the documentation of this file.
1// pragma once
2
3#include <Eigen/Sparse>
4#include <fftw3.h>
5
7
10
12
13// selects the type of TCData
14using timestream::TCDataKind;
15
16namespace mapmaking {
17
19
20};
21
23public:
24 // get logger
25 std::shared_ptr<spdlog::logger> logger = spdlog::get("citlali_logger");
26
27 double tolerance;
29
30 // populate maps with a time chunk (signal, kernel, coverage, and noise)
31 template<class map_buffer_t, typename Derived, typename calib_t>
32 void populate_maps_ml(TCData<TCDataKind::PTC, Eigen::MatrixXd> &, map_buffer_t &, map_buffer_t &,
33 Eigen::DenseBase<Derived> &, std::string &, calib_t &, double, bool, bool);
34};
35
36template<class map_buffer_t, typename Derived, typename calib_t>
37void MLMapmaker::populate_maps_ml(TCData<TCDataKind::PTC, Eigen::MatrixXd> &in, map_buffer_t &omb, map_buffer_t &cmb,
38 Eigen::DenseBase<Derived> &map_indices, std::string &pixel_axes, calib_t &calib, double d_fsmp,
39 bool run_omb, bool run_noise) {
40
41 const bool use_cmb = !cmb.noise.empty();
42 const bool use_omb = !omb.noise.empty();
43 const bool run_kernel = !omb.kernel.empty();
44 const bool run_coverage = !omb.coverage.empty();
45
46 Eigen::Index n_pixels = omb.n_rows * omb.n_cols;
47
48 for (Eigen::Index arr=0; arr<calib.n_arrays; ++arr) {
49 logger->info("making map for array {}/{}",arr + 1,calib.n_arrays);
50 auto array = calib.arrays[arr];
51 Eigen::Index map_index = arr;
52
53 // start indices for current array
54 Eigen::Index start = std::get<0>(calib.array_limits[array]);
55 // end indices for current array
56 Eigen::Index end = std::get<1>(calib.array_limits[array]);
57
58 // number of good detectors
59 Eigen::Index n_good_det = (calib.apt["flag"](Eigen::seq(std::get<0>(calib.array_limits[array]),
60 std::get<1>(calib.array_limits[array])-1)).array()==0).count();
61
62 //for (Eigen::Index i=start; i<end; ++i) {
63 // if ((in.flags.data.col(i).array() ==1).all()) {
64 // n_good_det--;
65 // }
66 //}
67
68 // total number of data points
69 Eigen::Index n_pts = n_good_det*in.scans.data.rows();
70
71 logger->info("start {} end {} n_pts {} n_pixels {} n_good_det {} n_rows {}", start, end, n_pts, n_pixels, n_good_det, in.scans.data.rows());
72
73 // signal and kernel timestreams
74 Eigen::VectorXd b(n_pts), b2(n_pts);
75 // pointing matrix
76 Eigen::SparseMatrix<double> A(n_pts,n_pixels);
77
78 // hold the values for the pointing matrix
79 std::vector<Eigen::Triplet<double>> triplet_list;
80 triplet_list.reserve(n_pts);
81
82 // keep track of what index next detector starts at
83 Eigen::Index k = 0;
84
85 for (Eigen::Index i=start; i<end; ++i) {
86 auto det_index = i;
87 if (calib.apt["flag"](det_index)==0) {// && !((in.flags.data.col(i).array()==1).all())) {
88 // get detector pointing
89 auto [lat,lon] = engine_utils::calc_det_pointing(in.tel_data.data, calib.apt["x_t"](det_index), calib.apt["y_t"](det_index),
90 pixel_axes, in.pointing_offsets_arcsec.data, omb.map_grouping);
91
92 // get map buffer row and col indices for lat and lon vectors
93 Eigen::VectorXd omb_irow = lat.array()/omb.pixel_size_rad + (omb.n_rows)/2.;
94 Eigen::VectorXd omb_icol = lon.array()/omb.pixel_size_rad + (omb.n_cols)/2.;
95
96 // loop through current detector chunk
97 for (Eigen::Index j=0; j<in.scans.data.rows(); ++j) {
98 Eigen::Index omb_ir = omb_irow(j);
99 Eigen::Index omb_ic = omb_icol(j);
100 Eigen::Index index = omb.n_rows * omb_ic + omb_ir;
101 // get pointing matrix value
102 triplet_list.push_back(Eigen::Triplet<double>(j + k,index,in.weights.data(i)));
103 }
104 // get signal values
105 b.segment(k,in.scans.data.rows()) = in.scans.data.col(i)*in.weights.data(i);
106
107 if (run_kernel) {
108 // get kernel values
109 b2.segment(k,in.scans.data.rows()) = in.kernel.data.col(i)*in.weights.data(i);
110 }
111 // move onto the next detector
112 k += in.scans.data.rows();
113 }
114 }
115
116 // initialize sparse matrix
117 A.setFromTriplets(triplet_list.begin(), triplet_list.end());
118
119 logger->info("running conjugate gradient for array {}/{}", arr + 1, calib.n_arrays);
120
121 //Eigen::ConjugateGradient<Eigen::SparseMatrix<double>, Eigen::Lower|Eigen::Upper> cg;
122 Eigen::LeastSquaresConjugateGradient<Eigen::SparseMatrix<double> > cg;
123 cg.setMaxIterations(max_iterations);
124 cg.setTolerance(tolerance);
125
126 // compute pointing matrix
127 cg.compute(A);
128
129 // solve for signal map
130 auto x = cg.solve(b).eval();
131 // populate signal map
132 omb.signal[map_index] += Eigen::Map<Eigen::MatrixXd>(x.data(),omb.n_rows, omb.n_cols);
133 logger->info("signal iterations {}",cg.iterations());
134 logger->info("signal error {}",cg.error());
135 logger->info("signal[{}] {}",map_index,omb.signal[map_index]);
136
137 if (run_kernel) {
138 // solve for kernel map
139 x = cg.solve(b2).eval();
140 // populate kernel map
141 omb.kernel[map_index] += Eigen::Map<Eigen::MatrixXd>(x.data(),omb.n_rows, omb.n_cols);
142 logger->info("kernel iterations {}",cg.iterations());
143 logger->info("kernel error {}",cg.error());
144 logger->info("kernel[{}] {}",map_index,omb.signal[map_index]);
145 }
146
147 b2.setOnes();
148 // solve for weight map
149 x = cg.solve(b2).eval();
150 // populate weight map
151 omb.weight[map_index] += Eigen::Map<Eigen::MatrixXd>(x.data(),omb.n_rows, omb.n_cols);
152 logger->info("weight iterations {}",cg.iterations());
153 logger->info("weight error {}",cg.error());
154 logger->info("weight[{}] {}",map_index,omb.signal[map_index]);
155 }
156
157 // free fftw vectors
158 /*fftw_free(fftw_a);
159 fftw_free(fftw_b);
160 // destroy fftw plan
161 fftw_destroy_plan(pf);*/
162}
163} // namespace mapmaking
Definition ml_mm.h:18
Definition ml_mm.h:22
std::shared_ptr< spdlog::logger > logger
Definition ml_mm.h:25
double tolerance
Definition ml_mm.h:27
void populate_maps_ml(TCData< TCDataKind::PTC, Eigen::MatrixXd > &, map_buffer_t &, map_buffer_t &, Eigen::DenseBase< Derived > &, std::string &, calib_t &, double, bool, bool)
Definition ml_mm.h:37
int max_iterations
Definition ml_mm.h:28
auto calc_det_pointing(tel_data_t &tel_data, double az_off, double el_off, const std::string pixel_axes, pointing_offset_t &pointing_offsets, const std::string map_grouping)
Definition pointing.h:11
Definition jinc_mm.h:24
TC data class.
Definition timestream.h:55