Canoe
Comprehensive Atmosphere N' Ocean Engine
mcmc_step.cpp
Go to the documentation of this file.
1 // C/C++
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::MCMCMove(Radiation *prad, Hydro *phydro) {
26  Application::Logger app("inversion");
27  app->Log("MCMC move");
28 
29  // initialize model
30  if (recs_.cur == 0) {
31  MCMCInit(prad, phydro);
32  }
33 
34  std::stringstream msg;
35  if (!mcmc_initialized_) {
36  app->Error("mcmc chain uninitialized");
37  }
38 
39  int cur = recs_.cur;
40  int ndim = recs_.ndim;
41  int nwalker = recs_.nwalker;
42 
43  for (int k = 0; k < nwalker; ++k) {
44  zz_[k] =
45  mcmc_stretch_move(par_, recs_.par[cur - 1], k, nwalker, ndim, &opts_);
46  // mcmc_walk_move(par_all + k*np, par, np, k, nwalker, zz + k, opts_);
47 
49  prad, phydro, par_, recs_.val[cur][k], pmy_block_->ks + k);
50  }
51 }
52 
53 void Inversion::MCMCSave(Hydro *phydro) {
54  int cur = recs_.cur;
55  int ndim = recs_.ndim;
56  int nwalker = recs_.nwalker;
57  int nval = recs_.nvalue;
58 
59  int is = pmy_block_->is, ie = pmy_block_->ie;
60  int ks = pmy_block_->ks;
61 
62  unsigned int seed = time(NULL);
63  for (int k = 0; k < nwalker; ++k) {
64  double lnp0 = recs_.lnp[cur - 1][k], lnp1 = recs_.lnp[cur][k],
65  pdiff = (ndim - 1.) * log(zz_[k]) + lnp1 - lnp0;
66 
67  if (pdiff > log(1. * rand_r(&seed) / RAND_MAX)) { // accept this position
68  for (int d = 0; d < ndim; ++d) recs_.par[cur][k][d] = par_[d];
69 
70  if (lnp1 > recs_.opt_lnp) { // new best position
71  recs_.opt_lnp = lnp1;
72  for (int d = 0; d < ndim; ++d) recs_.opt_par[d] = recs_.par[cur][k][d];
73  for (int d = 0; d < nval; ++d) recs_.opt_val[d] = recs_.val[cur][k][d];
74  }
75 
76  recs_.accept++;
77  recs_.newstate[cur][k] = 1;
78 
79  // save hydro to w1
80  for (int n = 0; n < NHYDRO; ++n)
81  for (int j = jl_; j <= ju_; ++j)
82  for (int i = is; i <= ie; ++i) {
83  phydro->w1(n, ks + k, j, i) = phydro->w(n, ks + k, j, i);
84  }
85  } else { // do not accept this position
86  for (int d = 0; d < ndim; ++d)
87  recs_.par[cur][k][d] = recs_.par[cur - 1][k][d];
88  for (int d = 0; d < nval; ++d)
89  recs_.val[cur][k][d] = recs_.val[cur - 1][k][d];
90  recs_.lnp[cur][k] = recs_.lnp[cur - 1][k];
91  recs_.newstate[cur][k] = 0;
92 
93  // load hydro from w1
94  for (int n = 0; n < NHYDRO; ++n)
95  for (int j = jl_; j <= ju_; ++j)
96  for (int i = is; i <= ie; ++i) {
97  phydro->w(n, ks + k, j, i) = phydro->w1(n, ks + k, j, i);
98  }
99  }
100  }
101 
102  recs_.cur++;
103  if (recs_.cur % opts_.print == 0) mcmc_report(&opts_, &recs_, "a");
104 }
Real * zz_
Definition: inversion.hpp:106
mcmc_opts opts_
Definition: inversion.hpp:101
bool mcmc_initialized_
Definition: inversion.hpp:103
mcmc_recs recs_
Definition: inversion.hpp:102
Real * par_
Definition: inversion.hpp:106
void MCMCSave(Hydro *phydro)
Definition: mcmc_step.cpp:53
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
void MCMCMove(Radiation *prad, Hydro *phydro)
Definition: mcmc_step.cpp:25
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.
int print
Definition: mcmc.hpp:40
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