Skip to content

Commit a2fe47b

Browse files
committed
initial commit
0 parents  commit a2fe47b

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

IsingMC/IsingMC.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "IsingMC.hpp"
2+
3+
#include <iostream>
4+
5+
#include <cmath>
6+
#include <cstdlib>
7+
8+
IsingMC::IsingMC(unsigned M, unsigned N, double J, double beta)
9+
: M(M), N(N), J(J), beta(beta) {
10+
11+
Lattice = new int[M];
12+
for (unsigned i = 0; i < M; i++) {
13+
Lattice[i] = new int[N];
14+
}
15+
16+
for (unsigned i = 0; i < M; i++) {
17+
for (unsigned j = 0; j < N; j++) {
18+
if (random() % 2 == 0) Lattice[i][j] = 1;
19+
else Lattice[i][j] = -1;
20+
}
21+
}
22+
}
23+
24+
IsingMC::~IsingMC() {
25+
26+
for (unsigned i = 0; i < M; i++) {
27+
delete[] Lattice[i];
28+
}
29+
delete[] Lattice;
30+
Lattice = NULL;
31+
}
32+
33+
void IsingMC::RandomPoint(unsigned& m, unsigned& n) {
34+
35+
unsigned pos = static_cast<unsigned>
36+
(floor (static_cast<double> (random()) / RAND_MAX * M * N));
37+
38+
m = static_cast<unsigned> (floor (pos / N));
39+
n = pos - m * N;
40+
}
41+
42+
double IsingMC::SpinEnergy(unsigned m, unsigned n) {
43+
44+
double E = 0;
45+
for (unsigned i = m - 1 ; i < m + 1; i += 2) {
46+
for (unsigned j = n - 1; j < n + 1; j += 2) {
47+
48+
if (i < 0) ii = M - 1;
49+
else if (i >= M - 1) ii = 0;
50+
else ii = i;
51+
52+
if (j < 0) jj = N - 1;
53+
else if (j >= N - 1) jj = 0;
54+
else jj = j;
55+
56+
E -= Lattice[ii][jj] * Lattice[m][n] * J;
57+
}
58+
}
59+
return E;
60+
}
61+
62+
double IsingMC::Energy() {
63+
64+
double E = 0;
65+
for (unsigned i = 0; i < M; i++) {
66+
for (unsigned j = 0; j < N; j++) {
67+
E += SpinEnergy(i, j);
68+
}
69+
}
70+
return E;
71+
}
72+
73+
void IsingMC::Step() {
74+
75+
unsigned i, j;
76+
RandomPoint(i, j);
77+
78+
double E_1 = Energy();
79+
Lattice[i][j] = -Lattice[i][j];
80+
81+
double E_2 = Energy();
82+
83+
if (E_2 < E_1) return;
84+
85+
if ()
86+
}

IsingMC/IsingMC.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _ISINGMC_
2+
#define _ISINGMC_
3+
4+
class IsingMC {
5+
6+
public:
7+
IsingMC(unsigned, unsigned, double, double);
8+
~IsingMC();
9+
double Energy();
10+
void Step();
11+
12+
private:
13+
unsigned N, M;
14+
int **Lattice;
15+
double J, beta;
16+
17+
18+
void RandomPoint(unsigned&, unsigned&);
19+
double SpinEnergy(unsigned, unsigned);
20+
21+
}
22+
23+
#endif _ISINGMC_ // #ifndef _ISINGMC_

0 commit comments

Comments
 (0)