Canoe
Comprehensive Atmosphere N' Ocean Engine
inversion.hpp
Go to the documentation of this file.
1 #ifndef SRC_INVERSION_INVERSION_HPP_
2 #define SRC_INVERSION_INVERSION_HPP_
3 
4 // C/C++
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 // Eigen
10 #include <Eigen/Core>
11 
12 // athena
13 #include <athena/athena.hpp>
14 
15 // canoe
16 #include <configure.hpp>
17 #include <virtual_groups.hpp>
18 
19 // inversion
20 #include "mcmc.hpp"
21 
22 class MeshBlock;
23 class ParameterInput;
24 class Coordinates;
25 class Hydro;
26 class Thermodynamics;
27 class Radiation;
28 
29 class Inversion : public NamedGroup,
30  // public RestartGroup,
31  public FITSOutputGroup {
32  public:
34  Inversion(MeshBlock *pmb, ParameterInput *pin, std::string name);
35  virtual ~Inversion();
36 
37  virtual Real LogPosteriorProbability(Radiation *prad, Hydro *phydro,
38  Real const *par, Real *val,
39  int k) const {
40  return 0.;
41  }
42 
43  virtual void CalculateFitTarget(Radiation const *prad, Real *val, int nvalue,
44  int k, int j) const {}
45 
46  virtual void InitializePositions() {}
47 
48  virtual void UpdateHydro(Hydro *phydro, ParameterInput *pin) const {}
49 
50  virtual int getX2Span() const { return 0.; }
51 
52  // MCMC functions
53  void InitializeChain(int nstep, int nwalker, int ndim, int nvalue);
54  void MakeMCMCOutputs(std::string fname);
55  void MCMCInit(Radiation *prad, Hydro *phydro);
56  void MCMCMove(Radiation *prad, Hydro *phydro);
57  void MCMCSave(Hydro *phydro);
58  void ResetChain();
59 
60  // access functions
61  int GetDims() const { return recs_.ndim; }
62  int GetValues() const { return recs_.nvalue; }
63  int GetWalkers() const { return recs_.nwalker; }
64  int GetSteps() const { return recs_.nstep; }
65  void SetLogProbability(int k, Real lnp) { recs_.lnp[recs_.cur][k] = lnp; }
66  Real GetLogProbability(int k) const { return recs_.lnp[recs_.cur][k]; }
67 
68  void setX2Indices(int j) {
69  jl_ = j;
70  ju_ = jl_ + getX2Span() - 1;
71  }
72 
74  bool ShouldFITSOutput(std::string variable_name) const override {
75  return true;
76  }
77  void LoadFITSOutputData(OutputType *pod, int *num_vars) const override {}
78 
79  protected:
80  // name of the inversion
81  std::string name_;
82 
83  // fit data
84  Eigen::VectorXd target_;
85  Eigen::MatrixXd icov_;
86 
87  // mcmc initial positions
88  Real **init_pos_;
89 
90  // whether to fit differential observation
92 
93  // j-index range
94  int jl_, ju_;
95 
97  MeshBlock const *pmy_block_;
98 
99  private:
100  // mcmc variables
104 
105  // scratch arrays
106  Real *zz_, *par_;
107 };
108 
109 using InversionPtr = std::shared_ptr<Inversion>;
110 using AllInversions = std::vector<std::shared_ptr<Inversion>>;
111 
113  public:
114  static AllInversions Create(MeshBlock *pmb, ParameterInput *pin);
115 };
116 
117 namespace InversionHelper {
118 
119 void read_observation_file(Eigen::VectorXd *target, Eigen::MatrixXd *icov,
120  std::string fname);
121 void gather_probability(std::vector<Inversion *> const &fitq);
122 
123 } // namespace InversionHelper
124 
125 #endif // SRC_INVERSION_INVERSION_HPP_
Real * zz_
Definition: inversion.hpp:106
void InitializeChain(int nstep, int nwalker, int ndim, int nvalue)
Definition: inversion.cpp:71
Eigen::VectorXd target_
Definition: inversion.hpp:84
int GetDims() const
Definition: inversion.hpp:61
int GetSteps() const
Definition: inversion.hpp:64
bool fit_differential_
Definition: inversion.hpp:91
virtual ~Inversion()
Definition: inversion.cpp:61
void LoadFITSOutputData(OutputType *pod, int *num_vars) const override
Definition: inversion.hpp:77
mcmc_opts opts_
Definition: inversion.hpp:101
void MakeMCMCOutputs(std::string fname)
Definition: inversion.cpp:79
Real GetLogProbability(int k) const
Definition: inversion.hpp:66
int GetValues() const
Definition: inversion.hpp:62
virtual void CalculateFitTarget(Radiation const *prad, Real *val, int nvalue, int k, int j) const
Definition: inversion.hpp:43
Eigen::MatrixXd icov_
Definition: inversion.hpp:85
Real ** init_pos_
Definition: inversion.hpp:88
bool ShouldFITSOutput(std::string variable_name) const override
MeshOutputGroup functions.
Definition: inversion.hpp:74
int GetWalkers() const
Definition: inversion.hpp:63
bool mcmc_initialized_
Definition: inversion.hpp:103
void ResetChain()
Definition: inversion.cpp:89
std::string name_
Definition: inversion.hpp:81
Inversion(MeshBlock *pmb, ParameterInput *pin, std::string name)
Constructor and destructor.
Definition: inversion.cpp:25
mcmc_recs recs_
Definition: inversion.hpp:102
virtual void UpdateHydro(Hydro *phydro, ParameterInput *pin) const
Definition: inversion.hpp:48
void setX2Indices(int j)
Definition: inversion.hpp:68
Real * par_
Definition: inversion.hpp:106
virtual void InitializePositions()
Definition: inversion.hpp:46
void MCMCSave(Hydro *phydro)
Definition: mcmc_step.cpp:53
void MCMCInit(Radiation *prad, Hydro *phydro)
Definition: mcmc_init.cpp:25
void SetLogProbability(int k, Real lnp)
Definition: inversion.hpp:65
virtual Real LogPosteriorProbability(Radiation *prad, Hydro *phydro, Real const *par, Real *val, int k) const
Definition: inversion.hpp:37
void MCMCMove(Radiation *prad, Hydro *phydro)
Definition: mcmc_step.cpp:25
virtual int getX2Span() const
Definition: inversion.hpp:50
MeshBlock const * pmy_block_
pointer to parent MeshBlock
Definition: inversion.hpp:97
static AllInversions Create(MeshBlock *pmb, ParameterInput *pin)
Definition: inversion.cpp:124
std::shared_ptr< Inversion > InversionPtr
Definition: inversion.hpp:109
std::vector< std::shared_ptr< Inversion > > AllInversions
Definition: inversion.hpp:110
This file contains data structure and subroutines implementing Markov Chain Monte Carlo sampler.
void read_observation_file(Eigen::VectorXd *target, Eigen::MatrixXd *icov, std::string fname)
Definition: inversion.cpp:162
void gather_probability(std::vector< Inversion * > const &fitq)
Definition: inversion.cpp:203
int nstep
Definition: mcmc.hpp:53
int ndim
Definition: mcmc.hpp:50
int nvalue
Definition: mcmc.hpp:51
double ** lnp
Definition: mcmc.hpp:57
int cur
Definition: mcmc.hpp:64
int nwalker
Definition: mcmc.hpp:52