-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantum3body_simulation_window.cc
315 lines (268 loc) · 9.31 KB
/
quantum3body_simulation_window.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* vim: set sw=4 sts=4 ft=cpp et foldmethod=syntax : */
/*
* Copyright (c) 2011 Tiziano Müller <[email protected]>
* Christian Reinhardt
*
*
*
*/
#include "quantum3body_simulation_window.hh"
#include "two_dim_spo.hh"
#include "time_evolutions.hh"
#include "color_maps.hh"
#include "ui_quantum3body_simulation_window.h"
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtGui/QFileDialog>
#include <QtGui/QImageWriter>
#include <QtCore/QFile>
#include <QtCore/QDateTime>
#include <algorithm>
#include <cassert>
/**
* This should match the entries in the initialPotential QComboBox
*/
enum PotentialSelection
{
HARMONIC_POTENTIAL = 0,
ZERO_POTENTIAL = 1
};
/**
* This should match the entries in the initialTimeEvolution QComboBox
*/
enum TimeEvolutionSelection
{
DEFAULT_EVOLUTION = 0,
QUANTUM3BODY_EVOLUTION = 1
};
/**
* This should match the entries in the resolution QComboBox
*/
enum Resolution
{
RESOLUTION_64x64 = 0,
RESOLUTION_128x128 = 1,
RESOLUTION_256x256 = 2,
RESOLUTION_512x512 = 3,
RESOLUTION_1024x1024 = 4,
RESOLUTION_2048x2048 = 5,
RESOLUTION_4096x4096 = 6,
RESOLUTION_8192x8192 = 7
};
Quantum3BodySimulationWindow::Quantum3BodySimulationWindow(QWidget* p) :
QMainWindow(p),
_simulation(NULL),
_timer(new QTimer(this)),
_ui(new Ui::Quantum3BodyWindow),
_currentIteration(0),
_lastResetTimestamp(0),
_image(NULL)
{
_ui->setupUi(this);
_imageLabel = new QLabel;
_imageLabel->setBackgroundRole(QPalette::Base);
_imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
_imageLabel->setScaledContents(true);
_ui->plotScrollArea->setBackgroundRole(QPalette::Dark);
_ui->plotScrollArea->setWidget(_imageLabel);
_ui->fitToWindowAction->setCheckable(true);
_ui->fitToWindowAction->setChecked(true);
updateViewActions();
connect(_ui->fitToWindowAction, SIGNAL(triggered()), this, SLOT(viewFitToWindow()));
connect(_ui->normalSizeAction, SIGNAL(triggered()), this, SLOT(viewNormalSize()));
_defaultTimeEvolution = new DefaultTimeEvolution;
_quantum3bodyTimeEvolution = new Quantum3BodyTimeEvolution;
connect(_timer, SIGNAL(timeout()), SLOT(evolve()));
connect(_ui->run, SIGNAL(toggled(bool)), SLOT(runSimulation(bool)));
connect(_ui->reset, SIGNAL(pressed()), SLOT(resetSimulation()));
connect(_ui->browsePictureFolder, SIGNAL(pressed()), SLOT(browsePictureFolder()));
connect(_ui->initialTimeEvolution, SIGNAL(currentIndexChanged(int)), SLOT(setTimeEvolution(int)));
QByteArray format;
foreach (format, QImageWriter::supportedImageFormats())
_ui->pictureFormat->insertItem(0, format);
_ui->pictureFormat->setCurrentIndex(_ui->pictureFormat->findText("png")); // set the selected item to png or nothing
resetSimulation();
}
Quantum3BodySimulationWindow::~Quantum3BodySimulationWindow()
{
_timer->stop();
delete _simulation;
delete _defaultTimeEvolution;
delete _quantum3bodyTimeEvolution;
delete _image;
}
void Quantum3BodySimulationWindow::resetSimulation()
{
statusBar()->showMessage("resetting the simulation, this may take a while...");
size_t gridSizeX(0), gridSizeY(0);
switch (_ui->resolution->currentIndex())
{
case RESOLUTION_64x64:
gridSizeX = gridSizeY = 64;
break;
case RESOLUTION_128x128:
gridSizeX = gridSizeY = 128;
break;
case RESOLUTION_256x256:
gridSizeX = gridSizeY = 256;
break;
case RESOLUTION_512x512:
gridSizeX = gridSizeY = 512;
break;
case RESOLUTION_1024x1024:
gridSizeX = gridSizeY = 1024;
break;
case RESOLUTION_2048x2048:
gridSizeX = gridSizeY = 2048;
break;
case RESOLUTION_4096x4096:
gridSizeX = gridSizeY = 4096;
break;
case RESOLUTION_8192x8192:
gridSizeX = gridSizeY = 8192;
break;
default:
assert(!"the selected grid size is not implemented");
}
// only reinitialize the simulation if the resolution changed
if (_simulation == NULL || gridSizeX != _simulation->sizeX() || gridSizeY != _simulation->sizeY())
{
delete _simulation;
delete _image;
_simulation = new TwoDimSPO(gridSizeX, gridSizeY);
_image = new QImage(static_cast<int>(gridSizeY), static_cast<int>(gridSizeX), QImage::Format_RGB32);
}
switch (_ui->initialPotential->currentIndex())
{
case HARMONIC_POTENTIAL:
_defaultTimeEvolution->useHarmonicPotential = true;
break;
case ZERO_POTENTIAL:
_defaultTimeEvolution->useHarmonicPotential = false;
break;
default:
assert(!"Selected potential not defined.");
}
_quantum3bodyTimeEvolution->e = _ui->initialEpsilon->value();
switch (_ui->initialTimeEvolution->currentIndex())
{
case DEFAULT_EVOLUTION:
_simulation->setTimeEvolution(_defaultTimeEvolution);
break;
case QUANTUM3BODY_EVOLUTION:
_simulation->setTimeEvolution(_quantum3bodyTimeEvolution);
break;
default:
assert(!"Selected time evolution not defined.");
}
std::function<complex (const double&, const double&)> phi0 = [&](const double& x, const double& y)->complex {
double kx(_ui->initialPropagationX->value());
double ky(_ui->initialPropagationY->value());
double dx(_ui->initialPositionX->value());
double dy(_ui->initialPositionY->value());
return exp(-0.5*((x-dx)*(x-dx)+(y-dy)*(y-dy)) + complex(0,1)*ky*y + complex(0,1)*kx*x);
};
_simulation->initialize(phi0);
_currentIteration = 0;
_ui->currentIteration->setValue(static_cast<int>(_currentIteration));
_lastResetTimestamp = QDateTime::currentDateTime().toTime_t();
plot();
statusBar()->showMessage("ready...");
}
void Quantum3BodySimulationWindow::browsePictureFolder()
{
_ui->pictureFolder->setText(QFileDialog::getExistingDirectory(this,
tr("Open base directory for saving pictures"),
QString()));
}
void Quantum3BodySimulationWindow::runSimulation(bool run)
{
if (run)
_timer->start();
else
_timer->stop();
}
void Quantum3BodySimulationWindow::evolve()
{
++_currentIteration;
_ui->currentIteration->setValue(static_cast<int>(_currentIteration));
_simulation->evolveStep(_ui->dtParameter->value());
if (_currentIteration % _ui->updatePlotSteps->value() == 0)
plot();
if (_currentIteration == static_cast<size_t>(_ui->numberOfIterations->value()))
_ui->run->setChecked(false);
}
void Quantum3BodySimulationWindow::plot()
{
const size_t gridSizeX(_simulation->sizeX()), gridSizeY(_simulation->sizeY());
double totalProbability(0.0);
const double binSizeX(_simulation->binSizeX()), binSizeY(_simulation->binSizeY());
const complex* f(_simulation->phi());
for (size_t i(0); i < gridSizeX; ++i)
{
for (size_t j(0); j < gridSizeY; ++j)
{
totalProbability += binSizeX*binSizeY*fabs(f[j + gridSizeY*i])*fabs(f[j + gridSizeY*i]);
}
}
_ui->totalProbability->setValue(totalProbability);
for(size_t i(0); i < gridSizeX; ++i)
{
for(size_t j(0); j < gridSizeY; ++j)
{
_image->setPixel(static_cast<int>(i), static_cast<int>(gridSizeY-1-j), rainbowColorMap(fabs(f[j + gridSizeY*i])));
}
}
_imageLabel->setPixmap(QPixmap::fromImage(*_image));
if (!_ui->fitToWindowAction->isChecked())
_imageLabel->adjustSize();
if (_ui->savePictures->isChecked())
{
QDir pictureFolder(_ui->pictureFolder->text());
if (pictureFolder.exists())
{
QString filepath(
pictureFolder.filePath(
QString("%1.%2_%3.%4")
.arg(_ui->pictureBasename->text())
.arg(_lastResetTimestamp)
.arg(_currentIteration, 4, 10, QLatin1Char('0'))
.arg(_ui->pictureFormat->currentText().toLower())));
qDebug() << "dumping picture to " << filepath;
_image->save(filepath, _ui->pictureFormat->currentText().toAscii().constData());
}
}
}
void Quantum3BodySimulationWindow::setTimeEvolution(int selectedEvolutionAlgorithm)
{
switch (selectedEvolutionAlgorithm)
{
case DEFAULT_EVOLUTION:
_ui->initialPotential->setEnabled(true);
_ui->initialEpsilon->setEnabled(false);
break;
case QUANTUM3BODY_EVOLUTION:
_ui->initialPotential->setEnabled(false);
_ui->initialEpsilon->setEnabled(true);
break;
default:
assert(!"Selected time evolution not defined.");
}
}
void Quantum3BodySimulationWindow::viewNormalSize()
{
_imageLabel->adjustSize();
}
void Quantum3BodySimulationWindow::viewFitToWindow()
{
bool fitToWindow = _ui->fitToWindowAction->isChecked();
_ui->plotScrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow) {
viewNormalSize();
}
updateViewActions();
}
void Quantum3BodySimulationWindow::updateViewActions()
{
_ui->normalSizeAction->setEnabled(!_ui->fitToWindowAction->isChecked());
}