mc.h 2.06 KB
Newer Older
1 2
#pragma once

3
#include "evalable.h"
4 5
#include "measurements.h"
#include "parser.h"
6 7 8 9
#include "random.h"
#include <memory>
#include <string>
#include <vector>
Lukas Weber's avatar
Lukas Weber committed
10

11 12 13
namespace loadl {

class mc {
14
private:
15 16 17
	int sweep_{0};
	int therm_{0};
	int pt_sweeps_per_global_update_{-1};
18

19 20
	// The following times in seconds are used to estimate a safe exit interval before walltime is
	// up.
21 22 23
	double max_checkpoint_write_time_{0};
	double max_sweep_time_{0};
	double max_meas_time_{0};
24

25 26
protected:
	parser param;
Lukas Weber's avatar
Lukas Weber committed
27
	std::unique_ptr<random_number_generator> rng;
28

29
	virtual void init() = 0;
30 31
	virtual void checkpoint_write(const iodump::group &out) = 0;
	virtual void checkpoint_read(const iodump::group &in) = 0;
32
	virtual void write_output(const std::string &filename);
33
	virtual void do_update() = 0;
34
	virtual void do_measurement() = 0;
35
	virtual void pt_update_param(const std::string& /*param_name*/, double /*new_param*/) {
36 37
		throw std::runtime_error{"running parallel tempering, but pt_update_param not implemented"};
	}
38
	virtual double pt_weight_ratio(const std::string& /*param_name*/, double /*new_param*/) {
Lukas Weber's avatar
Lukas Weber committed
39 40 41
		throw std::runtime_error{"running parallel tempering, but pt_weight_ratio not implemented"};
		return 1;
	}
42

43
public:
44 45
	bool pt_mode_{};

46
	double random01();
47
	int sweep() const;
48

49
	virtual void register_evalables(std::vector<evalable> &evalables) = 0;
50

51 52 53
	// these functions do a little more, like taking care of the
	// random number generator state, then call the child class versions.
	void _init();
Lukas Weber's avatar
Lukas Weber committed
54

55 56
	void _write(const std::string &dir);
	bool _read(const std::string &dir);
57

58 59
	void measurements_write(const std::string &dir);

60
	void _write_output(const std::string &filename);
61

62
	void _do_update();
63
	void _do_measurement();
64 65
	void _pt_update_param(const std::string& param_name, double new_param, const std::string &new_dir);
	double _pt_weight_ratio(const std::string& param_name, double new_param);
66

67 68
	void pt_measure_statistics();

69 70
	double safe_exit_interval();

71
	bool is_thermalized();
72 73
	measurements measure;

Lukas Weber's avatar
wrong &  
Lukas Weber committed
74
	mc(const parser &p);
75
	virtual ~mc() = default;
76
};
Lukas Weber's avatar
Lukas Weber committed
77

Lukas Weber's avatar
wrong &  
Lukas Weber committed
78
typedef std::function<mc *(const parser &)> mc_factory;
79
}