-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmcmc_bamr.h
351 lines (261 loc) · 9.78 KB
/
mcmc_bamr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
-------------------------------------------------------------------
Copyright (C) 2012-2022, Mohammad Al-Mamun, Mahmudul Hasan Anik,
and Andrew W. Steiner
This file is part of Bamr.
Bamr is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Bamr is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bamr. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------
*/
/** \file mcmc_bamr.h
\brief Definition of main bamr class
*/
#ifndef MCMC_BAMR_H
#define MCMC_BAMR_H
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>
#ifdef BAMR_MPI
#include <mpi.h>
#endif
#include <Python.h>
#include <o2scl/hdf_file.h>
#include <o2scl/mcmc_para.h>
// #include <o2scl/emulator.h>
#include "mcmc_emu.h"
#ifdef BAMR_READLINE
#include <o2scl/cli_readline.h>
#else
#include <o2scl/cli.h>
#endif
#include "bamr_class.h"
/** \brief Main namespace
The bamr namespace which holds all classes and functions.
This file is documented in bamr.h .
*/
namespace bamr {
typedef boost::numeric::ublas::vector<double> ubvector;
typedef std::function<int(size_t,const ubvector &, double &,
model_data &)> point_funct;
typedef std::function<int(const ubvector &,double,
std::vector<double> &,model_data &)> fill_funct;
/** \brief A specialized emulator for this code
Note that this emulator has a data type of array<double,ndat>,
because that is what the MCMC is using, but internally em1 uses a
data type of vector<double>, since not all output fields are
always emulated.
*/
class emulator_bamr : public o2scl::emulator_unc<model_data,
model_data,
ubvector> {
public:
int mpi_rank;
int mpi_size;
/** \brief The internal generic emulator
*/
o2scl::emulator_interpm_idw_table<std::vector<double>,ubvector> em1;
/** \brief The second internal generic emulator
*/
o2scl::emulator_interpm_krige_table<std::vector<double>,ubvector> em2;
o2scl::emulator_python<std::vector<double>,ubvector> em3;
/* \brief Pointer to the data_eval object for function evaluations
*/
bamr_class *bcp;
std::ofstream *sop;
/** \brief List of parameters and output quantities (including log_wgt)
*/
std::vector<std::string> list;
/** \brief Tables from which to train
*/
o2scl::table_units<> table;
/** \brief List of filenames containing training tables
*/
std::vector<std::string> files;
/** \brief Number of parameters
*/
size_t np;
/** \brief Number of output quantities for em1
*/
size_t nout;
/** \brief Mapping of parameters to indices
*/
o2scl::vec_index pvi;
/** \brief Mapping of output data to array indices
*/
o2scl::vec_index dvi;
/** \brief Random number generator
*/
o2scl::rng<> r;
emulator_bamr();
/** \brief Train the emulator with the data in \c tab_train
*/
void train(o2scl::table_units<> &tab_train, o2scl::vec_index &pvii,
o2scl::vec_index &dvii, bamr_class *bcpi,
std::ofstream *sopi);
/** \brief Evaluate the emulator, and or the full function if necessary
*/
virtual int eval(size_t n, const ubvector &p, double &log_wgt,
model_data &dat);
/** \brief Evaluate the emulator, and or the full function if necessary
*/
virtual int eval_unc(size_t n, const ubvector &p, double &log_wgt,
double &lw_unc, model_data &dat,
model_data &dat_unc);
};
/** \brief Statistical analysis of EOS from M and R constraints
\comment
\note Right now the EOS is rejected if the pressure decreases
with increasing density at any density, even if it happens at a
density which is larger than the central density of the maximum
mass star.
1/6/16 - I originally thought this was a problem, but
actually there is no problem here, as EOSs can always be
fixed to ensure that pressures always increase.
\endcomment
\todo There's a problem with testing debug_eos and debug_star
because of the complications of calling exit(0) inside
an OpenMP/MPI parallel region. For this reason test1 and
test2 are temporarily disabled. I think MPI_Barrier() may
help this problem, but this fix hasn't been tested and
may not completely fix it.
\todo It's not clear if successive calls of the mcmc command
really work. For now, one may have ensure the program exits
after each mcmc() run.
\comment
\todo Fix issue of block_counter giving confusing output if
there are too few MCMC points between each block.
2/2/16 - I think this is fixed now
\endcomment
\todo More testing
\todo Better documentation
\todo Help with plots
\future Allow non-tabulated data specified as a function?
\future The code internally stores two copies of the model
objects, which makes things a bit easier to handle MC
rejections, but also requires models to have this funny
copy_params() function to copy model parameters between model
objects. There's probably a better way to do this.
*/
class mcmc_bamr :
public o2scl::mcmc_para_cli<point_funct,fill_funct,
model_data,ubvector> {
protected:
/** \brief If true, use index2 to take derivative of M_max
*/
bool dv_index2;
/** \brief Train file name for python emulator
*/
std::string emu_train;
PyObject *train_modFile;
PyObject *train_tParam_Names;
PyObject *train_trainClass;
PyObject *train_instance;
PyObject *train_trainMthd;
PyObject *train_pArgs;
/// The number of sources
PyObject *addtl_sources;
PyObject *train_res;
PyObject *train_pTemp;
PyObject *train_temp;
bool py_train;
/** \brief Train the emulator based on data from \c file_name
using parameter names listed in \c names.
This is called in mcmc_bamr::mcmc_func().
*/
int train(std::string file_name, std::vector<std::string> &names);
/** \brief Calculate posteriors from the emulated points.
*/
virtual int emu_points(std::vector<std::string> &sv,
bool itive_com);
virtual int emu_train2(std::vector<std::string> &sv,
bool itive_com);
/// A string indicating which model is used, set in \ref set_model().
std::string model_type;
/// Desc
vec_index pvi;
/** \brief Vector of \ref bamr_class objects (one for each OpenMP
thread)
This is currently a pointer because it makes it a lot easier
to replace these pointers with children. A shared_ptr might be
better, but I've had problems implementing vector<shared_ptr>
correctly.
*/
std::vector<bamr_class *> bc_arr;
std::vector<emulator_bamr> eb_arr;
/** \brief The \ref bamr::settings object
(shared by instances of \ref bamr_class)
*/
std::shared_ptr<settings> set;
/** \brief The \ref bamr::ns_data object
(shared by instances of \ref bamr_class)
*/
std::shared_ptr<ns_data> nsd;
/// \name Main functions called from the command-line interface
//@{
/** \brief Set the model for the EOS to use
*/
virtual int set_model(std::vector<std::string> &sv, bool itive_com);
//@}
/** \brief Write initial data to HDF file
*/
virtual void file_header(o2scl_hdf::hdf_file &hf);
/** \brief Make any necessary preparations for the mcmc() function
This is called by \ref mcmc(). If the return value is non-zero
then it is assumed that the calculation fails and mcmc()
returns.
*/
virtual int mcmc_init();
/** \brief Add a data distribution to the list
*/
virtual int add_data(std::vector<std::string> &sv, bool itive_com);
/** \brief Add a data distribution to the list
*/
virtual int add_data_alt(std::vector<std::string> &sv, bool itive_com);
/** \brief Perform the MCMC simulation
*/
virtual int mcmc_func(std::vector<std::string> &sv, bool itive_com);
/** \brief Set the number of OpenMP threads
*/
virtual int threads(std::vector<std::string> &sv, bool itive_com);
/** \brief Use the last point in a specifed file for the
initial point
*/
virtual int initial_point_last(std::vector<std::string> &sv,
bool itive_com);
/** \brief Use the highest likelihood point in the specified file
for the initial point
*/
virtual int initial_point_best(std::vector<std::string> &sv,
bool itive_com);
/** \brief Read previous results from a file
*/
virtual int read_prev_results_mb(std::vector<std::string> &sv,
bool itive_com);
public:
/** \brief The command-line interface object
*/
#ifdef BAMR_READLINE
o2scl::cli_readline cl;
#else
o2scl::cli cl;
#endif
/** \brief Create a \ref mcmc_bamr object
*/
mcmc_bamr();
virtual ~mcmc_bamr() {
}
/** \brief Set up the 'cli' object
This function adds three commands (mcmc, model, add-data) and
the 'set' parameters.
*/
virtual void setup_cli_mb();
};
}
#endif