Repository navigation
Expand file tree
/
Copy pathlangevin.cpp
More file actions
358 lines (314 loc) · 10.6 KB
/
Copy pathlangevin.cpp
File metadata and controls
358 lines (314 loc) · 10.6 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* @file langevin.c
* @ingroup Langevin
* @brief Routines for numerically solving the langevin equation
* @version $spbd_version$
* @author Kherim Willems
*
* @attention
* @verbatim
*
* SPBD -- Single Protein Brownian Dynamics
*
* Kherim Willems (kherim@kher.im)
*
* @endverbatim
*/
#include "langevin.h"
#include "fileio.h"
int computeLangevinTrajectory(
langevin_simulation &simu
)
{
return computeLangevinTrajectory(
simu.conf.steps,
simu.conf.saveFreq,
simu.conf.timestep,
simu.conf.temperature,
simu.conf.damping,
simu.conf.positionStart,
simu.conf.positionSpacing,
simu.conf.forceVector,
simu.conf.dampingVector,
simu.out.positionVector,
simu.out.timeVector,
simu.conf.method);
}
int computeLangevinTrajectory(
const unsigned long long steps,
const unsigned long long saveFreq,
const float timestep,
const float temperature,
const float damping,
const float positionStart,
const float positionSpacing,
std::vector<float> const &forceVector,
std::vector<float> const &dampingVector,
std::vector<float> &positionVector,
std::vector<unsigned long long> &timeVector,
const int method
)
{
std::cout << "Initializing simulation.\n";
/* Check sanity of arguments */
std::cout << " Checking arguments... ";
// size of positionVector, timeVector == int_round_down(steps/saveFreq)+1
// size of forceVector == dampingVector
// size of positionVector == timeVector
// spacingVector[0] <= positionStart <= spaceVector[end]
std::cout << "done.\n";
/* Allocate memory for force vectors */
std::vector<float> externalVector(forceVector.size());
std::vector<float> thermalVector(forceVector.size());
/* Populate local force vectors */
// F/gamma
std::cout << " Pre-computing external force step size... ";
calcExternalStepVector( timestep,
damping,
forceVector,
dampingVector,
externalVector);
std::cout << "done.\n";
// (kB*T*Dt/gamma)^0.5
std::cout << " Pre-computing thermal force step size... ";
calcThermalStepVector( timestep,
temperature,
damping,
dampingVector,
thermalVector);
std::cout << "done.\n";
/* Initialize random gaussian distribution */
std::cout << " Pre-computing random guassian distribution... ";
std::default_random_engine generator;
std::normal_distribution<float> distribution(0.0,1.0);
std::cout << "done.\n";
/* Set the initial position */
std::cout << " Setting up initial particle position... ";
positionVector.push_back(positionStart);
timeVector.push_back(0);
std::cout << "done.\n";
/*
std::cout << "Printing step vectors:\n";
std::cout << "Damped external force stepsize per position:\n";
printVector(externalVector,',');
std::cout << "Damped thermal force stepsize per position:\n";
printVector(thermalVector,',');
*/
// Allocate loop variables
float newPos = positionStart;
float oldPos = positionStart;
unsigned long long percentFreq = steps/20;
float minPos = 0.0;
float maxPos = (forceVector.size()-1)*positionSpacing;
unsigned long index = long(oldPos/positionSpacing);
float eForceStep = 0.0;
float tForceStep = 0.0;
int percent = 0;
/* Perform steps */
std::cout << "Running simulation for " << steps << " steps.\n";
std::cout << " 0%.." << std::flush;
for (unsigned long long s = 1; s != steps+1; s++) {
// Calculate the index for this position and check for out-of-bounds
if (oldPos < minPos) {
index = long(minPos/positionSpacing);
} else if (oldPos > maxPos) {
index = long(maxPos/positionSpacing);
} else {
index = long(oldPos/positionSpacing);
}
// Grab external force at this position
eForceStep = externalVector[index];
// Grab thermal force at this position
tForceStep = thermalVector[index]*distribution(generator);
// Calculate new position
newPos = oldPos + eForceStep + tForceStep;
// Save position and timestamp if needed
if (s % saveFreq == 0) {
positionVector.push_back(newPos);
timeVector.push_back(s);
}
if (s % percentFreq == 0) {
percent = (float)(s)/(float)(steps)*100;
std::cout << percent << "%.." << std::flush;
}
// Overwrite oldPos for next iteration
oldPos = newPos;
}
std::cout << ". done.\n" << std::endl;
/* Cleanup */
return 0;
}
int calcExternalStepVector(
float timestep,
float damping,
std::vector<float> const &forceVector,
std::vector<float> const &dampingVector,
std::vector<float> &externalVector)
{
float forceV = 0.0;
float dampingV = 0.0;
float externalV = 0.0;
for (unsigned int i = 0; i < externalVector.size(); i++) {
forceV = forceVector[i];
dampingV = dampingVector[i];
externalV = (forceV*timestep)/(damping*dampingV);
externalVector[i] = externalV;
}
return 0;
}
int calcThermalStepVector(
float timestep,
float temperature,
float damping,
std::vector<float> const &dampingVector,
std::vector<float> &thermalVector)
{
float dampingV = 0.0;
float thermalV = 0.0;
for (unsigned int i = 0; i < thermalVector.size(); i++) {
dampingV = dampingVector[i];
thermalV = std::sqrt((2*temperature*timestep)/(damping*dampingV));
thermalVector[i] = thermalV;
}
return 0;
}
template <typename T>
void printVector(std::vector<T> const &vec, char delimiter) {
for (auto val : vec)
{
std::cout << val << delimiter;
}
std::cout << std::endl;
}
void buildForceVector(
float spacing,
float min,
float max,
std::vector<float> &forceVector)
{
unsigned int numElements = (max-min)/spacing + 1;
float position = 0.0;
for (unsigned int i = 0; i<numElements; i++)
{
position = spacing*i + min;
forceVector[i] = -std::pow( (position-3) ,3) + (position-3);
}
}
int loadConfiguration(
std::string const &filename,
langevin_configuration &conf
)
{
// Allocate variables
std::vector<std::string> param_value;
std::vector<std::string> lines;
// Save filename to conf
conf.name = filename;
// Load all lines from configuration file
readFileToStrings(conf.name, lines);
// Convert all lines to configuration parameters
for (unsigned int i=0; i<lines.size(); i++)
{
std::string line = lines[i];
// Extract parameter and value from line
param_value = split(line, ' ');
const char* param = param_value[0].c_str();
std::string value = param_value[1];
// Go over each of the possible parameters
if(std::strcmp(param, "steps") == 0)
{
conf.steps = std::stoull(param_value[1]);
continue;
}
if(std::strcmp(param, "saveFreq") == 0)
{
conf.saveFreq = std::stoull(param_value[1]);
continue;
}
if(std::strcmp(param, "timestep") == 0)
{
conf.timestep = std::stof(param_value[1]);
continue;
}
if(std::strcmp(param, "temperature") == 0)
{
conf.temperature = std::stof(param_value[1]);
continue;
}
if(std::strcmp(param, "damping") == 0)
{
conf.damping = std::stof(param_value[1]);
continue;
}
if(std::strcmp(param, "positionStart") == 0)
{
conf.positionStart = std::stof(param_value[1]);
continue;
}
if(std::strcmp(param, "positionSpacing") == 0)
{
conf.positionSpacing = std::stof(param_value[1]);
continue;
}
if(std::strcmp(param, "forceVector") == 0)
{
// Convert the value part to float vector
std::vector<float> forceVector =
convertStringToFloatVector(split(value,','));
// Save vector in configuration
conf.forceVector = forceVector;
continue;
}
if(std::strcmp(param, "dampingVector") == 0)
{
// Convert the value part to float vector
std::vector<float> dampingVector =
convertStringToFloatVector(split(value,','));
// Save vector in configuration
conf.dampingVector = dampingVector;
continue;
}
if(std::strcmp(param, "method") == 0)
{
int method = 0;
const char* val = param_value[1].c_str();
if (std::strcmp(val, "first") == 0)
{
method = 1;
}
if (std::strcmp(val, "second") == 0)
{
method = 2;
}
conf.method = method;
continue;
}
if(std::strcmp(param, "trajectoryOutputFile") == 0)
{
conf.trajectoryOutputFile = param_value[1];
continue;
}
}
return 0;
}
void printConfiguration(
langevin_configuration conf
)
{
std::cout << "Configuration file parameters:\n";
std::cout << " name: " << conf.name << "\n";
std::cout << " steps: " << conf.steps << "\n";
std::cout << " saveFreq: " << conf.saveFreq << "\n";
std::cout << " timestep: " << conf.timestep << "\n";
std::cout << " temperature: " << conf.temperature << "\n";
std::cout << " damping: " << conf.damping << "\n";
std::cout << " positionStart: " << conf.positionStart << "\n";
std::cout << " positionSpacing: " << conf.positionSpacing << "\n";
std::cout << " method: " << conf.method << "\n";
std::cout << " trajectoryOutputFile: " << conf.trajectoryOutputFile << "\n";
std::cout << " forceVector:\n";
printVector(conf.forceVector, ',');
std::cout << " dampingVector:\n";
printVector(conf.dampingVector, ',');
std::cout << std::endl;
}