-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation.hpp
258 lines (211 loc) · 7.13 KB
/
Simulation.hpp
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
/* Simulation base classes
* used by Tracking/TrackingTask and ResStrengths/ParticleResStrengths
*
* Copyright (C) 2017 Jan Felix Schmidt <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __POLEMATRIX__SIMULATION_HPP_
#define __POLEMATRIX__SIMULATION_HPP_
#include <libpalattice/AccLattice.hpp>
#include <libpalattice/FunctionOfPos.hpp>
#include <thread>
#include <mutex>
#include <memory>
#include <list>
#include <vector>
#include "Configuration.hpp"
#include "Trajectory.hpp"
// abstract base class for a simulation task for a single particle
// can be used by a Simulation object, which passes its config/lattice/orbit
// to many SingleParticleSimulation
class SingleParticleSimulation {
protected:
std::unique_ptr<Trajectory> trajectory; // particle trajectory, implementation depends TrajectoryMode
public:
const unsigned int particleId;
const std::shared_ptr<Configuration> config; //not const Object, because SimToolInstance status can be changed
std::shared_ptr<const pal::AccLattice> lattice;
std::shared_ptr<const pal::FunctionOfPos<pal::AccPair>> orbit;
SingleParticleSimulation(unsigned int id, const std::shared_ptr<Configuration> c);
void setModel(std::shared_ptr<const pal::AccLattice> l, std::shared_ptr<const pal::FunctionOfPos<pal::AccPair>> o);
virtual void run() =0;
virtual double getProgress() const =0; // progress [0,1] for status output
virtual std::string getProgressBar(unsigned int barWidth) const;
};
// abstract base class for a simulation
// including a config as well as lattice and orbit, which can be set using config
template <typename T=SingleParticleSimulation>
class Simulation {
protected:
std::shared_ptr<pal::AccLattice> lattice;
std::shared_ptr<pal::FunctionOfPos<pal::AccPair>> orbit;
// queue
typedef typename std::vector<T>::iterator taskIterator;
typedef typename std::vector<T>::const_iterator const_taskIterator;
std::vector<T> queue;
taskIterator queueIt;
std::list<const_taskIterator> runningTasks; // to display progress
// thread management
std::vector<std::thread> threadPool;
std::thread progress;
std::mutex mutex;
void initThreadPool(unsigned int nThreads);
void startThreads();
void waitForThreads();
void processQueue();
void printProgress() const;
std::map<unsigned int,std::string> errors;
public:
const std::shared_ptr<Configuration> config;
bool showProgressBar;
Simulation(unsigned int nThreads=std::thread::hardware_concurrency())
: queueIt(queue.begin()), config(new Configuration), showProgressBar(true) {initThreadPool(nThreads);}
Simulation(const std::shared_ptr<Configuration> c, unsigned int nThreads=std::thread::hardware_concurrency())
: queueIt(queue.begin()), config(c), showProgressBar(true) {initThreadPool(nThreads);}
Simulation(const Simulation& o) = delete;
void setModel();
bool modelReady() {if (lattice->size()==0 || orbit->size()==0) return false; else return true;}
unsigned int numParticles() const {return config->nParticles();}
unsigned int numSuccessful() const {return numParticles() - errors.size();}
virtual void start() =0;
void saveLattice() const {lattice->print( (config->outpath()/"lattice.dat").string() );}
void saveOrbit() const {orbit->print( (config->outpath()/"closedorbit.dat").string() );}
std::string printErrors() const;
};
// ---- template class implementation ----
template <typename T>
void Simulation<T>::initThreadPool(unsigned int nThreads)
{
// use at least one thread
if (nThreads == 0)
nThreads = 1;
// create threads
for (auto i=0u; i<nThreads; i++) {
threadPool.emplace_back(std::thread());
}
}
template <typename T>
void Simulation<T>::startThreads()
{
for (std::thread& t : threadPool) {
t = std::thread(&Simulation::processQueue,this);
}
//start extra thread for progress bars
if (showProgressBar) {
sleep(1);
progress = std::thread(&Simulation::printProgress,this);
}
}
template <typename T>
void Simulation<T>::waitForThreads()
{
for (std::thread& t : threadPool) {
t.join();
}
if (showProgressBar) {
progress.join();
}
}
template <typename T>
void Simulation<T>::setModel()
{
auto& palattice = config->getSimToolInstance();
lattice.reset( new pal::AccLattice(palattice) );
orbit.reset( new pal::FunctionOfPos<pal::AccPair>(palattice) );
config->updateSimToolSettings(*lattice);
orbit->simToolClosedOrbit( palattice );
config->writeRfMagnetsToLattice(*lattice);
if (config->gammaMode() == GammaMode::simtool
|| config->gammaMode() == GammaMode::simtool_plus_linear
|| config->gammaMode() == GammaMode::simtool_no_interpolation
|| config->gammaMode() == GammaMode::linear) {
// no model setup needed
}
else {
config->autocomplete(*lattice);
}
}
template <typename T>
void Simulation<T>::processQueue()
{
while (true) {
mutex.lock();
if (queueIt == queue.end()) {
mutex.unlock();
return; // finish this thread
}
else {
taskIterator myTask = queueIt;
queueIt++;
runningTasks.push_back(myTask); // to display progress
mutex.unlock();
try {
myTask->setModel(lattice, orbit);
myTask->run(); // run next queued task
}
//cancel thread in error case
catch (std::exception &e) {
std::cout << "ERROR @ particle " << myTask->particleId
// << " (thread_id "<< std::this_thread::get_id() << ")"
<<":"<< std::endl
<< e.what() << std::endl;
mutex.lock();
errors.emplace(myTask->particleId, e.what());
mutex.unlock();
}
mutex.lock();
runningTasks.remove(myTask); // to display progress
mutex.unlock();
}//else
}//while
}
template <typename T>
std::string Simulation<T>::printErrors() const
{
std::stringstream s;
for (auto& it : errors)
s << "ERROR @ particle " << it.first << ": " << it.second << std::endl;
return s.str();
}
template <typename T>
void Simulation<T>::printProgress() const
{
unsigned int barWidth;
auto numTasks = runningTasks.size();
if ( numTasks < 5)
barWidth = 20;
else
barWidth = 15;
//shorter looks ugly
while (runningTasks.size() > 0) {
auto tmp = runningTasks;
unsigned int n=0;
for (auto& task : tmp) {
if (n<2) // first 2 with progress bar
std::cout << task->getProgressBar(barWidth) << " ";
else // others percentage only
std::cout << task->getProgressBar(0) << " ";
n++;
}
while (n<numTasks) { // clear finished tasks
std::cout << " ";
n++;
}
std::cout <<"\r"<< std::flush;
sleep(1);
}
}
#endif
// __POLEMATRIX__SIMULATION_HPP_