Skip to content

Commit d63bfe2

Browse files
committed
first version working, needs a couple of fixes
1 parent 910bf11 commit d63bfe2

File tree

4 files changed

+60
-29
lines changed

4 files changed

+60
-29
lines changed

IsingMC.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ void IsingMC::Step() {
132132
while (true) {
133133
RandomPoint(i, j);
134134

135+
// std::cout << i << '\t' << j << std::endl;
136+
135137
double de = EnergyDiff(i, j);
136138

137139
// {

IsingWindow.cpp

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "IsingWindow.hpp"
44

55
IsingWindow::IsingWindow(unsigned M, unsigned N, double J,
6-
double beta, QWidget *parent) : QMainWindow(parent) {
6+
double beta, QWidget *parent)
7+
: QMainWindow(parent) {
78

89
simwid = new IsingWidget(M, N, J, beta, parent);
910
grid = new QGridLayout;
@@ -14,22 +15,29 @@ IsingWindow::IsingWindow(unsigned M, unsigned N, double J,
1415
lne_b = new QLineEdit( QString(std::to_string(beta).c_str()) );
1516

1617
form = new QFormLayout;
17-
form->addRow("Jparam:", lne_J);
18-
form->addRow("bparam:", lne_b);
18+
form->addRow("J :", lne_J);
19+
form->addRow("1/T :", lne_b);
1920

20-
connect(lne_J, SIGNAL (returnPressed()), this, SLOT (set_J()));
21-
connect(lne_b, SIGNAL (returnPressed()), this, SLOT (set_b()));
21+
connect(lne_J, SIGNAL (returnPressed()), this, SLOT (set_J()) );
22+
connect(lne_b, SIGNAL (returnPressed()), this, SLOT (set_b()) );
2223

23-
grid->addLayout(form, 0, 1);
24-
grid->addWidget(simwid, 0, 0);
24+
btn_start = new QPushButton("Start", this);
25+
btn_start->setCheckable(true);
26+
connect(btn_start, SIGNAL (toggled(bool)), this, SLOT (toggle_simulation(bool)) );
27+
28+
grid->addLayout(form, 0, 2);
29+
grid->addWidget(simwid, 0, 0, 2, 2);
30+
grid->addWidget(btn_start, 1, 2);
2531

2632
centralWidget()->setLayout(grid);
2733

2834
}
2935

3036
IsingWindow::~IsingWindow() {
37+
toggle_simulation(false); // XXX FIXME (data races)
3138
delete lne_J;
3239
delete lne_b;
40+
delete btn_start;
3341
delete form;
3442
delete simwid;
3543
delete grid;
@@ -40,16 +48,19 @@ void IsingWindow::set_J() {
4048
double val = lne_J->text().toDouble(&status);
4149
if (!status) return;
4250
simwid->SetJ(val);
43-
std::cout << "J is now set to: " << val << std::endl;
44-
4551
}
4652

4753
void IsingWindow::set_b() {
4854
bool status;
4955
double val = lne_b->text().toDouble(&status);
5056
if (!status) return;
5157
simwid->SetBeta(val);
52-
std::cout << "beta is now set to: " << val << std::endl;
58+
}
59+
60+
void IsingWindow::toggle_simulation(bool sstate) {
61+
if (sstate) { btn_start->setText("Stop"); }
62+
else { btn_start->setText("Start"); }
63+
simwid->ToggleSimulation(sstate);
5364
}
5465

5566

@@ -62,6 +73,8 @@ IsingWidget::IsingWidget (unsigned M, unsigned N, double J,
6273
: running(false) {
6374

6475
Q_UNUSED(parent);
76+
77+
iterations = 100;
6578
imc = new IsingMC(M, N, J, beta);
6679
setFixedWidth(M);
6780
setFixedHeight(N);
@@ -75,14 +88,11 @@ IsingWidget::~IsingWidget () {
7588
void IsingWidget::paintEvent (QPaintEvent *e) {
7689

7790
Q_UNUSED(e);
78-
return;
7991

8092
QPainter qp(this);
81-
unsigned PenSize = 2;
93+
unsigned PenSize = 1;
8294
unsigned offset = PenSize ? PenSize : 1;
8395

84-
for (unsigned i = 0; i < 100 // imc->XSize() * imc->YSize()
85-
;i++) imc->Step();
8696
// std::cout << imc->Energy() << std::endl;
8797

8898
// std::cout << "Energy: " << imc->Energy()
@@ -105,18 +115,31 @@ void IsingWidget::paintEvent (QPaintEvent *e) {
105115

106116
void IsingWidget::SetJ (double val) { imc->SetJ(val); }
107117
void IsingWidget::SetBeta (double val) { imc->SetBeta(val); }
108-
double IsingWidget::Energy () { return imc->Energy(val); }
109-
double IsingWidget::Magnetization () { return imc->Magnetization(val); }
118+
double IsingWidget::Energy () { return imc->Energy(); }
119+
double IsingWidget::Magnetization () { return imc->Magnetization(); }
110120

111121
void IsingWidget::simulationThread(unsigned num) {
112-
while (true) {
122+
while (running) {
113123
for (unsigned i = 0; i < num; i++) {
114-
mtx.lock();
115-
if (running) {
116-
imc->Step();
117-
}
118-
mtx.unlock();
124+
imc->Step();
119125
}
120-
emit paintEvent(0);
126+
// emit paintEvent(0);
127+
update();
121128
}
122129
}
130+
131+
void IsingWidget::ToggleSimulation(bool sstate) {
132+
std::cout << "state of simulation before changing: " << running << std::endl;
133+
if (sstate) { startSimulation(); }
134+
else { stopSimulation(); }
135+
}
136+
137+
void IsingWidget::startSimulation() {
138+
if (running) return;
139+
std::thread(&IsingWidget::simulationThread, this, iterations).detach();
140+
running = true;
141+
}
142+
143+
void IsingWidget::stopSimulation() {
144+
running = false;
145+
}

IsingWindow.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <QHBoxLayout>
1010
#include <QString>
1111
#include <QGridLayout>
12+
#include <QPushButton>
1213
#include <thread>
13-
#include <mutex>
1414

1515
#include "IsingMC.hpp"
1616

@@ -29,15 +29,19 @@ class IsingWindow : public QMainWindow {
2929
QLineEdit *lne_J, *lne_b;
3030
QGridLayout *grid;
3131
IsingWidget *simwid;
32+
QPushButton *btn_start;
3233

3334
public slots:
3435
void set_J();
3536
void set_b();
37+
void toggle_simulation(bool);
3638

3739
};
3840

3941
// need new widget for ising window
4042
// with seperate computation thread
43+
//
44+
// add start, stop and randomize functionality
4145

4246
class IsingWidget : public QWidget {
4347

@@ -50,15 +54,18 @@ class IsingWidget : public QWidget {
5054
void SetBeta(double);
5155
double Energy();
5256
double Magnetization();
57+
void ToggleSimulation(bool);
5358

5459
protected:
5560
void paintEvent(QPaintEvent *e);
5661
void simulationThread(unsigned);
62+
void startSimulation();
63+
void stopSimulation();
5764

5865
private:
5966
IsingMC *imc;
60-
std::mutex mtx;
6167
bool running;
68+
unsigned iterations;
6269

6370
};
6471

main.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
int main(int argc, char *argv[]) {
66

7-
unsigned NX = 200;
8-
unsigned NY = 200;
7+
unsigned NX = 500;
8+
unsigned NY = 500;
99

1010
QApplication app(argc, argv);
11-
IsingWindow iw(NX, NY, 1.14, 0.1);
11+
IsingWindow iw(NX, NY, 1.14, 0.5);
1212

1313
// iw.resize(NX, NY);
1414
iw.setWindowTitle("2D Ising Monte-Carlo");
@@ -19,7 +19,6 @@ int main(int argc, char *argv[]) {
1919
return app.exec();
2020

2121
while (true) {
22-
// iw.repaint();
2322
app.processEvents();
2423
}
2524

0 commit comments

Comments
 (0)