functions to create half-domain and neel state
[qmc.git] / main.cpp
blobdc35b253869ef1c242e75292a6e00b08a2a6e8bb
1 #include <iostream>
2 #include <vector>
3 #include <cstdlib>
4 #include <cmath>
5 #include <ctime>
7 #define PI 3.141592653589792
8 #define DEFAULT_SIZE 10
10 using namespace std;
12 class State;
13 class Zero;
14 class Probability;
15 class Energy;
17 // End of forward decl
19 class State {
20 private:
21 public:
22 struct Change {
23 int i;
24 int j;
27 State () : m_spins(DEFAULT_SIZE) {}
28 State (int size) : m_spins(size) {}
29 State (const State& other) : m_spins(other.m_spins) {}
31 Change trialChange () {
32 Change ret;
33 ret.i = (rand()/(RAND_MAX+1.0))*size();
34 ret.j = (rand()/(RAND_MAX+1.0))*(size()-1);
35 if (ret.j>=ret.i) ret.j++;
36 return ret;
39 State applyChange (const Change& change) {
40 State ret(*this);
41 ret.m_spins[change.i] = !ret.m_spins[change.i];
42 ret.m_spins[change.j] = !ret.m_spins[change.j];
43 return ret;
46 int size () const { return m_spins.size(); }
47 int at (int i) const { return m_spins[i]?1:-1; }
48 bool parallel (int i, int j) const { return m_spins[i] == m_spins[j]; }
50 static State up (int size = DEFAULT_SIZE) {
51 State ret(size);
52 for (int i=0;i<size;i++) {
53 ret.m_spins[i] = true;
55 return ret;
58 static State down (int size = DEFAULT_SIZE) {
59 State ret(size);
60 for (int i=0;i<size;i++) {
61 ret.m_spins[i] = false;
63 return ret;
66 static State neel (int size = DEFAULT_SIZE) {
67 State ret(size);
68 for (int i=0;i<size;i++) {
69 ret.m_spins[i] = i%2;
71 return ret;
74 static State half (int size = DEFAULT_SIZE) {
75 State ret(size);
76 for (int i=0;i<size;i++) {
77 ret.m_spins[i] = 2*i < size;
79 return ret;
82 static State random (int size = DEFAULT_SIZE) {
83 State ret(size);
84 for (int i=0;i<size;i++) {
85 ret.m_spins[i] = rand()/(RAND_MAX+1.0) < 0.5;
87 return ret;
90 friend ostream& operator << (ostream&, const State&);
91 const State& operator = (const State& other) { m_spins = other.m_spins; return *this; }
92 State& swap (State& other) { m_spins.swap(other.m_spins); return *this; }
94 private:
95 std::vector<bool> m_spins;
98 ostream& operator << (ostream& out, const State& s) {
99 for (std::vector<bool>::const_iterator iter = s.m_spins.begin();iter != s.m_spins.end();iter++) {
100 if (iter!=s.m_spins.begin() && iter!=s.m_spins.end()) out << ", ";
101 out << *iter;
103 return out;
106 class VarProb {
107 private:
108 double distance (double i, double j) { return 2*sin(PI*abs(i-j)); }
109 public:
110 VarProb() : alpha(1.0) {}
111 VarProb(double a) : alpha(a) {}
113 double compute (const State& state) {
114 double x = 0.0;
115 int L = state.size();
116 int ups = 0;
117 for (int i=0;i<L;i++) {
118 ups += state.at(i);
119 for (int j=i+1;j<L;j++) {
120 double d = distance(double(i)/L, double(j)/L);
121 x += log(d*d)*(state.parallel(i, j)?1.0:-1.0);
124 if (ups!=0) return 0.0;
125 return exp(alpha*x);
127 private:
128 double alpha;
131 class Observable {
132 private:
133 public:
136 template <typename Probability>
137 class QMC {
138 private:
139 public:
141 QMC (const Probability& p, const Observable &o, const State& s) : m_probability(p), m_obs(o), m_state(s) {
142 current_probability = m_probability.compute(m_state);
145 State state () const { return m_state; }
146 void setState (const State& s) { m_state = s; }
147 Probability probability () const { return m_probability; }
148 void setProbability (const Probability& p) { m_probability = p; }
150 bool step() {
151 bool ret = false;
152 State::Change trial = m_state.trialChange();
153 cout << "original state: " << m_state << endl;
154 State t_state = m_state.applyChange(trial);
155 cout << " --> proposed: " << t_state << endl;
156 double prob = m_probability.compute(t_state);
157 cout << " --> with probability: " << prob << endl;
158 double a = prob/current_probability;
159 cout << " --> and acceptance ratio: " << a << endl;
160 if (a>(rand()/(RAND_MAX+1.0))) {
161 // accept
162 m_state = t_state;
163 current_probability = prob;
164 cout << " --> accepted" << endl;
165 ret = true;
166 } else {
167 // reject
168 cout << " --> rejected" << endl;
169 ret = false;
171 return ret;
173 private:
174 State m_state;
175 Probability m_probability;
176 double current_probability;
177 Observable m_obs;
180 int main (int argc, char **argv) {
181 srand(time(NULL));
182 QMC<VarProb> qmc(VarProb(+0.26), Observable(), State::half(100));
183 for (int i=0;i<1e3;i++) qmc.step();
184 return 0;