Canoe
Comprehensive Atmosphere N' Ocean Engine
mcmc_init.cpp
Go to the documentation of this file.
1 // C/C++ headers
2 #include <cassert>
3 #include <cmath>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <iostream>
7 
8 // canoe
9 #include <configure.hpp>
10 
11 // climath
12 #include <climath/core.h>
13 
14 // athena
15 #include <athena/hydro/hydro.hpp>
16 #include <athena/mesh/mesh.hpp>
17 
18 // application
19 #include <application/application.hpp>
20 
21 // inversion
22 #include "inversion.hpp"
23 #include "mcmc.hpp"
24 
25 void Inversion::MCMCInit(Radiation *prad, Hydro *phydro) {
26  int nwalker = recs_.nwalker;
27  int ndim = recs_.ndim;
28  int nval = recs_.nvalue;
29 
30  Application::Logger app("inversion");
31 
32  Real **par = init_pos_;
33 
34  recs_.lnp[0][0] = 1.;
35 
36  int is = pmy_block_->is, ie = pmy_block_->ie;
37  int ks = pmy_block_->ks;
38 
39  // make sure that the start points are valid
40  for (int k = 0; k < nwalker; ++k) {
41  recs_.lnp[0][k] =
42  LogPosteriorProbability(prad, phydro, par[k], recs_.val[0][k], ks + k);
43 
44  int niter = 0;
45  while (std::isnan(recs_.lnp[0][k]) &&
46  (niter++ < 10)) { // if point (k) is invalid
47  mcmc_stretch_move(par[k], par, k, nwalker, ndim, &opts_);
48  recs_.lnp[0][k] = LogPosteriorProbability(prad, phydro, par[k],
49  recs_.val[0][k], ks + k);
50  }
51 
52  if (niter >= 10) {
53  app->Error("Starting point iteration > 10 times");
54  }
55 
56  // transfer input parameters to records
57  for (int d = 0; d < ndim; ++d) recs_.par[0][k][d] = par[k][d];
58 
59  if (recs_.lnp[0][k] > recs_.opt_lnp) {
60  recs_.opt_lnp = recs_.lnp[0][k];
61  for (int d = 0; d < ndim; ++d) recs_.opt_par[d] = recs_.par[0][k][d];
62  for (int d = 0; d < nval; ++d) recs_.opt_val[d] = recs_.val[0][k][d];
63  }
64  recs_.newstate[0][k] = 1;
65 
66  // save hydro to w1
67  for (int n = 0; n < NHYDRO; ++n)
68  for (int j = jl_; j <= ju_; ++j)
69  for (int i = is; i <= ie; ++i) {
70  phydro->w1(n, ks + k, j, i) = phydro->w(n, ks + k, j, i);
71  }
72  }
73 
74  recs_.cur++;
75  recs_.accept += nwalker;
76 
77  // make initial output
78  mcmc_report(&opts_, &recs_, "w");
79 }
mcmc_opts opts_
Definition: inversion.hpp:101
Real ** init_pos_
Definition: inversion.hpp:88
mcmc_recs recs_
Definition: inversion.hpp:102
void MCMCInit(Radiation *prad, Hydro *phydro)
Definition: mcmc_init.cpp:25
virtual Real LogPosteriorProbability(Radiation *prad, Hydro *phydro, Real const *par, Real *val, int k) const
Definition: inversion.hpp:37
MeshBlock const * pmy_block_
pointer to parent MeshBlock
Definition: inversion.hpp:97
void mcmc_report(mcmc_opts *opts, mcmc_recs *recs, char const *mode)
Definition: mcmc.cpp:486
double mcmc_stretch_move(double *newp, double **oldp, int iwalker, int nwalker, int ndim, mcmc_opts *opts)
Definition: mcmc.cpp:784
This file contains data structure and subroutines implementing Markov Chain Monte Carlo sampler.
double *** val
Definition: mcmc.hpp:56
int ** newstate
Definition: mcmc.hpp:62
double * opt_par
Definition: mcmc.hpp:59
int ndim
Definition: mcmc.hpp:50
int accept
Definition: mcmc.hpp:65
double * opt_val
Definition: mcmc.hpp:60
int nvalue
Definition: mcmc.hpp:51
double ** lnp
Definition: mcmc.hpp:57
double opt_lnp
Definition: mcmc.hpp:67
int cur
Definition: mcmc.hpp:64
int nwalker
Definition: mcmc.hpp:52
double *** par
Definition: mcmc.hpp:55