Bayesian Analysis of Neutron Star Mass and Radius Observations
bamr.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2012-2015, Andrew W. Steiner
5 
6  This file is part of Bamr.
7 
8  Bamr is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  Bamr is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Bamr. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file bamr.h
24  \brief Definition of main bamr class
25 */
26 #ifndef BAMR_H
27 #define BAMR_H
28 
29 #include <iostream>
30 
31 #include <o2scl/rng_gsl.h>
32 #include <o2scl/shared_ptr.h>
33 #include <o2scl/uniform_grid.h>
34 #include <o2scl/table3d.h>
35 #include <o2scl/hdf_file.h>
36 #include <o2scl/exception.h>
37 
38 #ifdef BAMR_READLINE
39 #include <o2scl/cli_readline.h>
40 #else
41 #include <o2scl/cli.h>
42 #endif
43 
44 #include "nstar_cold2.h"
45 #include "entry.h"
46 #include "models.h"
47 
48 /** \brief Main namespace
49 
50  The bamr namespace which holds all classes and functions.
51 
52  This file is documented in bamr.h .
53 */
54 namespace bamr {
55 
56  /** \brief Statistical analysis of EOS from M and R constraints
57 
58  \todo Right now the EOS is rejected if the pressure decreases
59  at any density, when in reality, it should only check if
60  the pressure decreases at a density below that of the central
61  density of the maximum mass star. This isn't a problem for
62  current EOSs, but could be a problem in the future.
63 
64  \todo It's not clear if successive calls of the mcmc command
65  really work. For now, one may have ensure the program exits
66  after each mcmc() run.
67 
68  \todo Fix issue of block_counter giving confusing output if
69  there are too few MCMC points between each block.
70 
71  \todo More testing
72 
73  \todo Better documentation
74 
75  \todo Help with plots
76 
77  \future Allow non-tabulated data specified as a function?
78  */
79  class bamr_class {
80 
81  protected:
82 
83  /** \brief Error handler for each thread
84  */
86 
87  /// \name Parameter objects for the 'set' command
88  //@{
90  o2scl::cli::parameter_double p_min_max_mass;
92  o2scl::cli::parameter_double p_exit_mass;
93  o2scl::cli::parameter_double p_input_dist_thresh;
95  o2scl::cli::parameter_int p_n_warm_up;
96  o2scl::cli::parameter_int p_grid_size;
97  o2scl::cli::parameter_int p_user_seed;
98  o2scl::cli::parameter_int p_max_iters;
99  o2scl::cli::parameter_int p_file_update_iters;
100  o2scl::cli::parameter_bool p_norm_max;
101  o2scl::cli::parameter_bool p_debug_star;
102  o2scl::cli::parameter_bool p_debug_load;
103  o2scl::cli::parameter_bool p_debug_eos;
104  o2scl::cli::parameter_bool p_output_next;
105  o2scl::cli::parameter_bool p_baryon_density;
106  o2scl::cli::parameter_bool p_use_crust;
107  o2scl::cli::parameter_bool p_inc_baryon_mass;
114  //@}
115 
116  /** \name Histogram limits
117  */
118  //@{
119  double nb_low;
120  double nb_high;
121  double e_low;
122  double e_high;
123  double m_low;
124  double m_high;
125  //@}
126 
127  /// \name Grids
128  //@{
132  //@}
133 
134  /// \name Other parameters accessed by 'set' and 'get'
135  //@{
136  /** \brief The number of MCMC successes between file updates
137  */
139 
140  /** \brief If true, output debug information about the input data
141  files (default false)
142  */
144 
145  /** \brief If true, normalize the data distributions so that the
146  max is one, otherwise, normalize so that the integral is one
147  */
148  bool norm_max;
149 
150  /// Maximum number of iterations (default 0)
152 
153  /// If true, use the default crust (default true)
154  bool use_crust;
155 
156  /// If true, output stellar properties for debugging
158 
159  /// If true, output equation of state for debugging
160  bool debug_eos;
161 
162  /// If true, output next point
164 
165  /// If true, compute the baryon density
167 
168  /// MCMC stepsize factor (default 15)
169  double step_fac;
170 
171  /// The lower threshold for the input distributions
173 
174  /// An upper mass threshold
175  double exit_mass;
176 
177  /// Minimum mass allowed for any of the individual neutron stars
178  double min_mass;
179 
180  /// Minimum allowed maximum mass
181  double min_max_mass;
182 
183  /** \brief Number of warm up steps (successful steps not iterations)
184 
185  \note Not to be confused with <tt>warm_up</tt>, which is
186  a boolean local variable in some functions not an int.
187  */
189 
190  /** \brief Time in seconds (3600 seconds is one hour, default is
191  86400 seconds or 1 day)
192  */
193  double max_time;
194 
195  /// If non-zero, use as the seed for the random number generator
197 
198  /** \brief If true, output more detailed information about the
199  best point
200  */
202 
203  /** \brief If true, output information about the baryon mass
204  as well as the gravitational mass
205  */
207  //@}
208 
209  /** \name Limits on mass and radius from source data files
210 
211  These are automatically computed in load_mc() as the
212  smallest rectangle in the \f$ (M,R) \f$ plane which
213  encloses all of the user-specified source data
214  */
215  //@{
216  double in_m_min;
217  double in_m_max;
218  double in_r_min;
219  double in_r_max;
220  //@}
221 
222  /// \name MPI properties
223  //@{
224  /// The MPI processor rank
225  int mpi_rank;
226 
227  /// The MPI number of processors
229 
230  /// The MPI starting time
232  //@}
233 
234  /// \name Input neutron star data
235  //@{
236  /// Input probability distributions
237  std::vector<o2scl::table3d> source_tables;
238 
239  /// The names for each source
240  std::vector<std::string> source_names;
241 
242  /// The names of the table in the data file
243  std::vector<std::string> table_names;
244 
245  /// File names for each source
246  std::vector<std::string> source_fnames;
247 
248  /// Slice names for each source
249  std::vector<std::string> slice_names;
250 
251  /** \brief The number of sources
252  */
253  size_t nsources;
254 
255  /// The initial set of neutron star masses
256  std::vector<double> first_mass;
257  //@}
258 
259  /// \name Parameter limits
260  //@{
261  entry low, high;
262  //@}
263 
264  /// \name Other variables
265  //@{
266  /// The first point in the parameter space
268 
269  /// The file containing the initial point
270  std::string first_point_file;
271 
272  /// \name Integer designating how to set the initial point
273  //@{
274  int first_point_type;
275  static const int fp_unspecified=-1;
276  static const int fp_last=-2;
277  static const int fp_best=-3;
278  //@}
279 
280  /// Main data table for Markov chain
282 
283  /// The number of Metropolis steps which succeeded
284  size_t mh_success;
285 
286  /// The number of Metropolis steps which failed
287  size_t mh_failure;
288 
289  /// Total number of mcmc iterations
291 
292  /// Store the full Markov chain
293  std::vector<double> markov_chain;
294 
295 #ifdef BAMR_READLINE
296  /// Command-line interface
298 #else
299  /// Command-line interface
301 #endif
302 
303  /// If true, then \ref first_update() has been called
305 
306  /// Number of bins for all histograms (default 100)
308 
309  /// Number of Markov chain segments
310  size_t n_chains;
311 
312  /// Number of chains
313  size_t chain_size;
314 
315  /// A string indicating which model is used, set in \ref set_model().
316  std::string model_type;
317 
318  /// True if the model provides S and L
319  bool has_esym;
320 
321  /// True if the model has an EOS
322  bool has_eos;
323 
324  /// Number of parameters, set in \ref set_model()
325  size_t nparams;
326 
327  /// Schwarzchild radius (set in constructor)
328  double schwarz_km;
329 
330  /// The model for the EOS
332 
333  /// The second copy of the model for the EOS
335 
336  /// Random number generator
338 
339  /// The screen output file
340  std::ofstream scr_out;
341  //@}
342 
343  /// \name Main functions called from the command-line interface
344  //@{
345  /** \brief Set the model for the EOS to use
346  */
347  virtual int set_model(std::vector<std::string> &sv, bool itive_com);
348 
349  /** \brief Set the first point in the parameter space
350  */
351  virtual int set_first_point(std::vector<std::string> &sv, bool itive_com);
352 
353  /** \brief Add a data distribution to the list
354  */
355  virtual int add_data(std::vector<std::string> &sv, bool itive_com);
356 
357  /** \brief Run a MCMC simulation
358 
359  In order to save the code from copying results over if a step
360  is rejected, the Metropolis steps alternate between two kinds.
361  In the first kind of step, \ref modp is the previous point and
362  \ref modp2 is the new point to be considered, and in the
363  second kind of step, \ref modp2 is the old point and \ref modp
364  is the new point to be considered. This two-step procedure is
365  also why there are two TOV solvers, i.e. \ref ts and \ref ts2.
366  */
367  virtual int mcmc(std::vector<std::string> &sv, bool itive_com);
368  //@}
369 
370  /// \name Internal functions
371  //@{
372  /// The function which selects the next neutron star masses
373  virtual void select_mass(entry &e_current, entry &e_next, double mmax);
374 
375  /// Setup column names and units for data table
376  virtual void table_names_units(std::string &s, std::string &u);
377 
378  /** \brief Make any necessary preparations for the mcmc() function
379 
380  This is called by \ref mcmc(). If the return value is non-zero
381  then it is assumed that the calculation fails and mcmc()
382  returns.
383 
384  This function does nothing and returns zero by default.
385  */
386  virtual int mcmc_init();
387 
388  /** \brief Decide to accept or reject the step
389  */
390  virtual bool make_step(double w_current, double w_next, bool debug,
391  bool warm_up, int iteration);
392 
393  /** \brief Initialize the expectation value objects
394 
395  This function is called by mcmc().
396  */
397  virtual void init_grids_table(entry &low, entry &high);
398 
399  /** \brief Further preparations of the EOS before calling the TOV
400  solver
401 
402  This function is empty by default.
403  */
404  virtual void prepare_eos(entry &e, model &modref, o2scl::tov_solve *tsr,
405  int &success);
406 
407  /** \brief Add a measurement
408  */
409  virtual void add_measurement
410  (entry &e, o2scl::o2_shared_ptr<o2scl::table_units<> >::type tab_eos,
412  double weight, bool new_meas, size_t n_meas, ubvector &weights);
413 
414  /** \brief Fill vector in <tt>line</tt> with data from the
415  current Monte Carlo point
416  */
417  virtual void fill_line
418  (entry &e, o2scl::o2_shared_ptr<o2scl::table_units<> >::type tab_eos,
420  double weight, bool new_meas, size_t n_meas, ubvector &weights,
421  std::vector<double> &line);
422 
423  /** \brief Write initial data to HDF file
424  */
425  virtual void first_update(o2scl_hdf::hdf_file &hf, model &modp);
426 
427  /** \brief Write histogram data to files with prefix \c fname
428  */
429  virtual void update_files(std::string fname_prefix,
430  model &modp, entry &e_current);
431 
432  /** \brief Set up the 'cli' object
433 
434  This function just adds the four commands and the 'set' parameters
435  */
436  virtual void setup_cli();
437 
438  /** \brief Load input probability distributions (called by mcmc())
439  */
440  virtual void load_mc();
441 
442  /** \brief Compute \f$ P(M|D) \f$ from parameters in entry \c e
443 
444  Called by mcmc().
445  */
446  virtual double compute_weight(entry &e, model &modref,
447  o2scl::tov_solve *ts, int &success,
448  ubvector &wgts, bool warm_up);
449 
450  public:
451 
452  /** \brief Tabulate EOS and then use in cold_nstar
453 
454  Called by compute_weight().
455 
456  \todo Temporarily made public for drdp project hack
457  */
458  virtual void compute_star(entry &e, model &modref, o2scl::tov_solve *ts,
459  int &success);
460 
461  protected:
462 
463  /// Output the "best" EOS obtained so far (called by mcmc())
464  virtual void output_best
465  (std::string fname_prefix, entry &e_best, double w_best,
468  ubvector &wgts);
469  //@}
470 
471  /// \name TOV solver objects
472  //@{
473  /// EOS interpolation object for TOV solver
475 
476  /// Pointer to a TOV solver
478 
479  /// Second pointer to a TOV solver
481 
482  /// Default TOV solver
484 
485  /// Second default TOV solver
487  //@}
488 
489  /** \brief The arguments sent to the command-line
490  */
491  std::vector<std::string> run_args;
492 
493  public:
494 
495  bamr_class();
496 
497  virtual ~bamr_class();
498 
499  /// Main wrapper for parsing command-line arguments
500  virtual void run(int argc, char *argv[]);
501 
502  /// \name Return codes for each point
503  //@{
504  std::vector<int> ret_codes;
505  static const int ix_success=0;
506  static const int ix_mr_outside=1;
507  static const int ix_r_outside=2;
508  static const int ix_zero_wgt=3;
509  static const int ix_press_dec=4;
510  static const int ix_nb_problem=5;
511  static const int ix_nb_problem2=6;
512  static const int ix_crust_unstable=7;
513  static const int ix_mvsr_failed=8;
514  static const int ix_tov_failure=9;
515  static const int ix_small_max=10;
516  static const int ix_tov_conv=11;
517  static const int ix_mvsr_table=12;
518  static const int ix_acausal=13;
519  static const int ix_acausal_mr=14;
520  static const int ix_param_mismatch=15;
521  static const int ix_neg_pressure=16;
522  static const int ix_no_eos_table=17;
523  static const int ix_eos_solve_failed=18;
524  //@}
525 
526  };
527 
528 }
529 
530 #endif
model * modp2
The second copy of the model for the EOS.
Definition: bamr.h:334
bool output_next
If true, output next point.
Definition: bamr.h:163
bool inc_baryon_mass
If true, output information about the baryon mass as well as the gravitational mass.
Definition: bamr.h:206
bool first_file_update
If true, then first_update() has been called.
Definition: bamr.h:304
Definition of EOS models.
virtual double compute_weight(entry &e, model &modref, o2scl::tov_solve *ts, int &success, ubvector &wgts, bool warm_up)
Compute from parameters in entry e.
bool debug_star
If true, output stellar properties for debugging.
Definition: bamr.h:157
virtual void run(int argc, char *argv[])
Main wrapper for parsing command-line arguments.
A data class which holds the EOS parameters, the masses and the radii.
Definition: entry.h:38
bool debug_eos
If true, output equation of state for debugging.
Definition: bamr.h:160
Main namespace.
Definition: bamr.h:54
virtual void select_mass(entry &e_current, entry &e_next, double mmax)
The function which selects the next neutron star masses.
std::vector< o2scl::table3d > source_tables
Input probability distributions.
Definition: bamr.h:237
int mpi_nprocs
The MPI number of processors.
Definition: bamr.h:228
bool baryon_density
If true, compute the baryon density.
Definition: bamr.h:166
o2scl::tov_solve def_ts2
Second default TOV solver.
Definition: bamr.h:486
o2scl::tov_solve def_ts
Default TOV solver.
Definition: bamr.h:483
model * modp
The model for the EOS.
Definition: bamr.h:331
int file_update_iters
The number of MCMC successes between file updates.
Definition: bamr.h:138
Base class for an EOS parameterization.
Definition: models.h:44
int grid_size
Number of bins for all histograms (default 100)
Definition: bamr.h:307
bool use_crust
If true, use the default crust (default true)
Definition: bamr.h:154
double schwarz_km
Schwarzchild radius (set in constructor)
Definition: bamr.h:328
size_t nparams
Number of parameters, set in set_model()
Definition: bamr.h:325
std::string first_point_file
The file containing the initial point.
Definition: bamr.h:270
virtual void load_mc()
Load input probability distributions (called by mcmc())
std::vector< std::string > run_args
The arguments sent to the command-line.
Definition: bamr.h:491
virtual void add_measurement(entry &e, o2scl::o2_shared_ptr< o2scl::table_units<> >::type tab_eos, o2scl::o2_shared_ptr< o2scl::table_units<> >::type tab_mvsr, double weight, bool new_meas, size_t n_meas, ubvector &weights)
Add a measurement.
virtual int mcmc_init()
Make any necessary preparations for the mcmc() function.
o2scl::table_units tc
Main data table for Markov chain.
Definition: bamr.h:281
virtual int set_first_point(std::vector< std::string > &sv, bool itive_com)
Set the first point in the parameter space.
bool has_eos
True if the model has an EOS.
Definition: bamr.h:322
int user_seed
If non-zero, use as the seed for the random number generator.
Definition: bamr.h:196
Definition of entry class.
virtual void fill_line(entry &e, o2scl::o2_shared_ptr< o2scl::table_units<> >::type tab_eos, o2scl::o2_shared_ptr< o2scl::table_units<> >::type tab_mvsr, double weight, bool new_meas, size_t n_meas, ubvector &weights, std::vector< double > &line)
Fill vector in line with data from the current Monte Carlo point.
double input_dist_thresh
The lower threshold for the input distributions.
Definition: bamr.h:172
double min_max_mass
Minimum allowed maximum mass.
Definition: bamr.h:181
virtual void setup_cli()
Set up the 'cli' object.
virtual void update_files(std::string fname_prefix, model &modp, entry &e_current)
Write histogram data to files with prefix fname.
std::vector< std::string > table_names
The names of the table in the data file.
Definition: bamr.h:243
double mpi_start_time
The MPI starting time.
Definition: bamr.h:231
o2scl::err_hnd_cpp ee
Error handler for each thread.
Definition: bamr.h:85
double min_mass
Minimum mass allowed for any of the individual neutron stars.
Definition: bamr.h:178
virtual void compute_star(entry &e, model &modref, o2scl::tov_solve *ts, int &success)
Tabulate EOS and then use in cold_nstar.
size_t nsources
The number of sources.
Definition: bamr.h:253
bool best_detail
If true, output more detailed information about the best point.
Definition: bamr.h:201
Definition of nstar_cold2.
o2scl::rng_gsl gr
Random number generator.
Definition: bamr.h:337
virtual int add_data(std::vector< std::string > &sv, bool itive_com)
Add a data distribution to the list.
virtual bool make_step(double w_current, double w_next, bool debug, bool warm_up, int iteration)
Decide to accept or reject the step.
size_t n_chains
Number of Markov chain segments.
Definition: bamr.h:310
virtual void prepare_eos(entry &e, model &modref, o2scl::tov_solve *tsr, int &success)
Further preparations of the EOS before calling the TOV solver.
int n_warm_up
Number of warm up steps (successful steps not iterations)
Definition: bamr.h:188
std::vector< std::string > slice_names
Slice names for each source.
Definition: bamr.h:249
std::ofstream scr_out
The screen output file.
Definition: bamr.h:340
double exit_mass
An upper mass threshold.
Definition: bamr.h:175
virtual void init_grids_table(entry &low, entry &high)
Initialize the expectation value objects.
o2scl::tov_solve * ts2
Second pointer to a TOV solver.
Definition: bamr.h:480
double step_fac
MCMC stepsize factor (default 15)
Definition: bamr.h:169
virtual int set_model(std::vector< std::string > &sv, bool itive_com)
Set the model for the EOS to use.
bool has_esym
True if the model provides S and L.
Definition: bamr.h:319
size_t mh_success
The number of Metropolis steps which succeeded.
Definition: bamr.h:284
virtual int mcmc(std::vector< std::string > &sv, bool itive_com)
Run a MCMC simulation.
ubvector first_point
The first point in the parameter space.
Definition: bamr.h:267
std::vector< double > markov_chain
Store the full Markov chain.
Definition: bamr.h:293
double max_time
Time in seconds (3600 seconds is one hour, default is 86400 seconds or 1 day)
Definition: bamr.h:193
bool norm_max
If true, normalize the data distributions so that the max is one, otherwise, normalize so that the in...
Definition: bamr.h:148
o2scl::tov_solve * ts
Pointer to a TOV solver.
Definition: bamr.h:477
virtual void output_best(std::string fname_prefix, entry &e_best, double w_best, o2scl::o2_shared_ptr< o2scl::table_units<> >::type tab_eos, o2scl::o2_shared_ptr< o2scl::table_units<> >::type tab_mvsr, ubvector &wgts)
Output the "best" EOS obtained so far (called by mcmc())
size_t mcmc_iterations
Total number of mcmc iterations.
Definition: bamr.h:290
o2scl::eos_tov_interp teos
EOS interpolation object for TOV solver.
Definition: bamr.h:474
virtual void first_update(o2scl_hdf::hdf_file &hf, model &modp)
Write initial data to HDF file.
bool debug_load
If true, output debug information about the input data files (default false)
Definition: bamr.h:143
virtual void table_names_units(std::string &s, std::string &u)
Setup column names and units for data table.
std::vector< std::string > source_names
The names for each source.
Definition: bamr.h:240
std::vector< double > first_mass
The initial set of neutron star masses.
Definition: bamr.h:256
Statistical analysis of EOS from M and R constraints.
Definition: bamr.h:79
size_t mh_failure
The number of Metropolis steps which failed.
Definition: bamr.h:287
o2scl::cli cl
Command-line interface.
Definition: bamr.h:300
std::string model_type
A string indicating which model is used, set in set_model().
Definition: bamr.h:316
int mpi_rank
The MPI processor rank.
Definition: bamr.h:225
std::vector< std::string > source_fnames
File names for each source.
Definition: bamr.h:246
size_t chain_size
Number of chains.
Definition: bamr.h:313
int max_iters
Maximum number of iterations (default 0)
Definition: bamr.h:151

Documentation generated with Doxygen. Bamr documentation is under the GNU Free Documentation License.