mc.h 1.91 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
15
16
private:
	int sweep_ = 0;
	int therm_ = 0;
17

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

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

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

42
43
public:
	double random01();
44
	int sweep() const;
45

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

48
49
50
	// 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
51

52
53
	void _write(const std::string &dir);
	bool _read(const std::string &dir);
54

55
	void _write_output(const std::string &filename);
56

57
	void _do_update();
58
	void _do_measurement();
59
	void _pt_update_param(double new_param, const std::string &new_dir);
Lukas Weber's avatar
Lukas Weber committed
60
	double _pt_weight_ratio(double new_param);
61

62
	double safe_exit_interval();
Lukas Weber's avatar
Lukas Weber committed
63
64
65
	
	// write only measurement data (useful for parallel tempering)
	void measurement_write(const std::string &dir);
66

67
	bool is_thermalized();
68
69
	measurements measure;

Lukas Weber's avatar
wrong &    
Lukas Weber committed
70
	mc(const parser &p);
71
	virtual ~mc() = default;
72
};
Lukas Weber's avatar
Lukas Weber committed
73

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