DP3
FastPredict.h
Go to the documentation of this file.
1 // FastPredict.h: DP3 step class to predict visibilities from a source model
2 // Copyright (C) 2023 ASTRON (Netherlands Institute for Radio Astronomy)
3 // SPDX-License-Identifier: GPL-3.0-or-later
4 
7 
8 #ifndef DP3_STEPS_FASTPREDICT_H_
9 #define DP3_STEPS_FASTPREDICT_H_
10 
11 #ifdef USE_FAST_PREDICT
12 
13 #include <atomic>
14 
15 #include <xtensor/containers/xtensor.hpp>
16 
17 #include "ApplyCal.h"
18 #include "ResultStep.h"
19 #include "base/DP3.h"
20 #include "base/DPBuffer.h"
21 
22 #include "base/ModelComponent.h"
23 #include "base/PredictBuffer.h"
24 
25 #include "model/Patch.h"
26 
27 #include <predict/PredictPlan.h>
28 #include <predict/PredictPlanExecCPU.h>
29 #include <predict/Predict.h>
30 
31 namespace dp3 {
32 namespace common {
33 class ParameterSet;
34 }
35 
36 namespace steps {
37 
41 class FastPredict : public ModelDataStep {
42  public:
46  FastPredict(const common::ParameterSet&, const std::string& prefix,
47  const std::vector<std::string>& source_patterns);
48 
49  ~FastPredict() override;
50 
51  common::Fields getRequiredFields() const override {
52  common::Fields fields = kUvwField;
53  if (operation_ == Operation::kAdd || operation_ == Operation::kSubtract) {
54  fields |= kDataField;
55  }
56  if (apply_cal_step_) {
57  fields |= base::GetChainRequiredFields(apply_cal_step_);
58  }
59  return fields;
60  }
61 
62  common::Fields getProvidedFields() const override {
63  // When operation_ == "replace", the output of apply_cal_step_ is passed to
64  // the next step. For all other operations, the predicted visibility data
65  // is the only provided output.
66  common::Fields fields;
67 
68  if (output_data_name_.empty()) {
69  // The predicted visibilities go to the main data buffer
70  fields |= kDataField;
71  } else {
72  // TODO(AST-1241): Handle these dependencies using Fields.
73  }
74 
75  if (operation_ == Operation::kReplace && apply_cal_step_) {
76  std::shared_ptr<Step> step = apply_cal_step_;
77  do {
78  fields |= step->getProvidedFields();
79  step = step->getNextStep();
80  } while (step);
81  }
82  return fields;
83  }
84 
86  void SetApplyCal(const common::ParameterSet&, const std::string& prefix);
87 
89  void SetOperation(const std::string& type);
90 
91  void SetThreadData(std::mutex*) {
92  // FastPredict does not require synchronisation, this is done in the
93  // RD/Predict module itself. This method is need to satisfy the
94  // steps/Predict.h interface.
95  }
96 
100  bool process(std::unique_ptr<base::DPBuffer>) override;
101 
103  void finish() override;
104 
106  void updateInfo(const base::DPInfo&) override;
107 
109  void show(std::ostream&) const override;
110 
112  void showTimings(std::ostream&, double duration) const override;
113 
115  void setSources(const std::vector<std::string>& sourcePatterns);
116 
118  base::Direction GetFirstDirection() const override;
119 
120  private:
121  enum class Operation { kReplace, kAdd, kSubtract };
122 
124  void Init(const common::ParameterSet&, const std::string& prefix,
125  const std::vector<std::string>& sourcePatterns);
126 
127  void InitializePlan();
128 
129  void RunPlan(base::DPBuffer::DataType& destination, double time);
130 
131  void CopyPredictBufferToData(
132  base::DPBuffer::DataType& destination,
133  const xt::xtensor<double, 4, xt::layout_type::row_major>& buffer);
134 
135  std::string name_;
139  xt::xtensor<std::complex<float>, 3> input_data_;
140  std::string source_db_name_;
141  bool correct_time_smearing_ = false;
142  bool correct_freq_smearing_ = false;
143  Operation operation_;
144  std::string output_data_name_;
145  bool apply_beam_ = false;
146  std::string coefficients_path_;
147  bool use_channel_freq_ = false;
148  bool one_beam_per_patch_ = false;
149  bool thread_over_baselines_ = false;
153  double beam_proximity_limit_ = 0.0;
154  double beam_evaluation_interval_ = 0.0;
155  double previous_beam_time_ = 0.0;
156  bool stokes_i_only_ = false;
157  bool any_orientation_is_absolute_ = false;
159  base::Direction phase_ref_;
160  bool moving_phase_ref_ = false;
161 
162  std::shared_ptr<ApplyCal> apply_cal_step_;
163  std::shared_ptr<ResultStep> result_step_;
164 
165  unsigned int debug_level_ = 0;
166 
167  std::vector<double> scaled_ncp_uvw_;
168 
169  std::vector<std::pair<size_t, size_t>> baselines_;
170 
172  std::vector<int> uvw_split_index_;
173 
175  xt::xtensor<double, 2> station_uvw_;
176 
178  everybeam::BeamMode beam_mode_ = everybeam::BeamMode::kNone;
179  everybeam::ElementResponseModel element_response_model_ =
180  everybeam::ElementResponseModel::kDefault;
181  casacore::MeasFrame meas_frame_;
182  casacore::MDirection::Convert meas_converter_;
183  std::shared_ptr<everybeam::telescope::Telescope> telescope_;
184  predict::PredictPlan predict_plan_;
185  predict::Predict predict_;
186  std::unique_ptr<predict::PredictPlanExecCPU> predict_plan_exec_;
187 
188  std::string direction_str_;
189  std::vector<std::shared_ptr<model::Patch>> patch_list_;
190 
191  std::vector<std::pair<std::shared_ptr<base::ModelComponent>,
192  std::shared_ptr<model::Patch>>>
193  source_list_;
194 
195  common::NSTimer timer_;
196 
208  std::atomic<int64_t> predict_time_ = 0;
209 
210  std::mutex* measures_mutex_;
211  std::mutex mutex_;
212 };
213 
214 } // namespace steps
215 } // namespace dp3
216 
217 #endif // USE_FAST_PREDICT
218 #endif // DP3_BASE_FASTPREDICT_H
DP3 step class to apply multiple calibration solutions.
Buffer holding the data of a timeslot/band.
aocommon::xt::UTensor< std::complex< float >, 3 > DataType
Definition: DPBuffer.h:95
dp3::common::Fields GetChainRequiredFields(std::shared_ptr< steps::Step > first_step)
BaseTimer< std::chrono::steady_clock > NSTimer
Definition: Timer.h:129
This file has generic helper routines for testing steps.
Definition: AntennaConfig.h:53