/
model.cpp
407 lines (323 loc) · 10.5 KB
/
model.cpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <Python.h>
#include "model.h"
#include "writer.h"
#include "intField.h"
#include "doubleField.h"
using namespace std;
Model::Model()
{
this->numParticles = 0;
}
Model::Model(Pack *pack)
{
numParticles = pack->numParticles;
positions = new double[numParticles*3];
std::copy(pack->positions, pack->positions+numParticles*3, positions);
states = new int[numParticles];
std::copy(pack->states, pack->states+numParticles, states);
this->dim = pack->dim;
}
Model::Model(double *positions, int *states, long numParticles, int dim)
{
this->numParticles = numParticles;
this->positions = new double[numParticles*3];
std::copy(positions, positions+numParticles*3, this->positions);
this->states = new int[numParticles];
std::copy(states, states+numParticles, this->states);
this->dim = dim;
}
Model::Model(std::vector<double> positions, std::vector<int> states, long numParticles, int dim)
{
this->numParticles = numParticles;
this->positions = new double[numParticles*3];
std::copy(positions.data(), positions.data()+numParticles*3, this->positions);
this->states = new int[numParticles];
std::copy(states.data(), states.data()+numParticles, this->states);
this->dim = dim;
}
Model::~Model()
{
if(positions) delete [] positions;
if(states) delete [] states;
for (long i=0; i < intFields.size(); i++)
{
IntField *thisField = intFields[i];
delete thisField;
}
for (long i=0; i < doubleFields.size(); i++)
{
DoubleField *thisField = doubleFields[i];
delete thisField;
}
}
void Model::AddPack(Pack *pack)
{
if(this->numParticles!=0){
long numParticlesNew = numParticles + pack->numParticles;
double *positionsTmp = new double[numParticlesNew*3];
int *statesTmp = new int[numParticlesNew];
std::copy(positions, positions+numParticles*3, positionsTmp);
std::copy(pack->positions, pack->positions+pack->numParticles*3, positionsTmp+numParticles*3);
std::copy(states, states+numParticles, statesTmp);
std::copy(pack->states, pack->states+pack->numParticles, statesTmp+numParticles);
delete [] positions;
delete [] states;
positions = positionsTmp;
states = statesTmp;
numParticles = numParticlesNew;
} else {
numParticles = pack->numParticles;
positions = new double[numParticles*3];
std::copy(pack->positions, pack->positions+numParticles*3, positions);
states = new int[numParticles];
std::copy(pack->states, pack->states+numParticles, states);
}
dim = pack->dim;
}
void Model::AddPack(double *positions, int *states, long numParticles, int dim)
{
if(this->numParticles!=0){
long numParticlesNew = this->numParticles + numParticles;
double *positionsTmp = new double[numParticlesNew*3];
int *statesTmp = new int[numParticlesNew];
std::copy(this->positions, this->positions+this->numParticles*3, positionsTmp);
std::copy(positions, positions+numParticles*3, positionsTmp+this->numParticles*3);
std::copy(this->states, this->states+this->numParticles, statesTmp);
std::copy(states, states+numParticles, statesTmp+this->numParticles);
delete [] this->positions;
delete [] this->states;
this->positions = positionsTmp;
this->states = statesTmp;
this->numParticles = numParticlesNew;
} else {
this->numParticles = numParticles;
this->positions = new double[numParticles*3];
std::copy(positions, positions+numParticles*3, this->positions);
this->states = new int[numParticles];
std::copy(states, states+numParticles, this->states);
}
this->dim = dim;
}
void Model::AddPack(std::vector<double> positions, std::vector<int> states, long numParticles, int dim)
{
AddPack(positions.data(), states.data(), numParticles, dim);
}
void Model::Serialize(std::string fname, Writer *writer)
{
writer->Write(fname, parameters, positions, intFields, doubleFields, dim, numParticles);
}
int Model::CreateIntField(std::string name, int dim)
{
intFields.push_back(new IntField(name, numParticles,dim));
return intFields.size()-1;
}
int Model::CreateDoubleField(std::string name, int dim)
{
doubleFields.push_back(new DoubleField(name, numParticles,dim));
return doubleFields.size()-1;
}
void Model::SetIntField(int handle, int state, int val)
{
IntField *thisField = intFields[handle];
int fieldDim = thisField->dim;
if(fieldDim>1)
{
std::cout << "Error: Input value does not have the same dimensionality as IntField" << std::endl;
return;
}
for(long i=0; i<numParticles; i++)
{
if(states[i] == state) thisField->data[i] = val;
}
}
void Model::SetIntField(int handle, int state, std::vector<int> val)
{
IntField *thisField = intFields[handle];
int fieldDim = thisField->dim;
if(val.size()!=thisField->dim)
{
std::cout << "Error: Input value does not have the same dimensionality as IntField" << std::endl;
return;
}
for(long i=0; i<numParticles; i++)
{
if(states[i] == state)
{
for(int d=0; d<fieldDim; d++)
{
long idx = d+i*fieldDim;
thisField->data[idx] = val[d];
}
}
}
}
void Model::SetIntField(int handle, int state, PyObject *PyFunc)
{
IntField *thisField = intFields[handle];
int fieldDim = thisField->dim;
Py_XINCREF(PyFunc);
PyObject *func, *arglist;
PyObject *result;
std::vector<int> cResult;
double pt[3];
for(long i=0; i<numParticles; i++)
{
if(states[i] == state)
{
pt[0] = positions[0+i*3];
pt[1] = positions[1+i*3];
pt[2] = positions[2+i*3];
arglist = Py_BuildValue("(ddd)",pt[0],pt[1],pt[2]);
result = PyEval_CallObject(PyFunc, arglist);
if(result == NULL) { std::cout << "Error in field callback" << std::endl; break; }
int success = Process_Python_Result<int>(result, &cResult);
if(!success) { std::cout << "Error: Non-numeric data returned from field callback" << std::endl; break; }
if(cResult.size() != fieldDim) { std::cout << "Error: Data returned from field callback has different dimensions to field" << std::endl; break; }
for(int d=0; d<fieldDim; d++)
{
long idx = d+i*fieldDim;
thisField->data[idx] = cResult[d];
}
cResult.clear();
Py_DECREF(arglist);
Py_XDECREF(result);
}
}
Py_XDECREF(PyFunc);
}
IntField *Model::GetIntField(int handle)
{
return intFields[handle];
}
void Model::SetDoubleField(int handle, int state, double val)
{
DoubleField *thisField = doubleFields[handle];
int fieldDim = thisField->dim;
if(fieldDim>1)
{
std::cout << "Error: Input value does not have the same dimensionality as DoubleField" << std::endl;
return;
}
for(long i=0; i<numParticles; i++)
{
if(states[i] == state) thisField->data[i] = val;
}
}
void Model::SetDoubleField(int handle, int state, std::vector<double> val)
{
DoubleField *thisField = doubleFields[handle];
int fieldDim = thisField->dim;
if(val.size()!=thisField->dim)
{
std::cout << "Error: Input value does not have the same dimensionality as DoubleField" << std::endl;
return;
}
for(long i=0; i<numParticles; i++)
{
if(states[i] == state)
{
for(int d=0; d<fieldDim; d++)
{
long idx = d+i*fieldDim;
thisField->data[idx] = val[d];
}
}
}
}
void Model::SetDoubleField(int handle, int state, PyObject *PyFunc)
{
DoubleField *thisField = doubleFields[handle];
int fieldDim = thisField->dim;
Py_XINCREF(PyFunc);
PyObject *func, *arglist;
PyObject *result;
std::vector<double> cResult;
double pt[3];
for(long i=0; i<numParticles; i++)
{
if(states[i] == state)
{
pt[0] = positions[0+i*3];
pt[1] = positions[1+i*3];
pt[2] = positions[2+i*3];
arglist = Py_BuildValue("(ddd)",pt[0],pt[1],pt[2]);
result = PyEval_CallObject(PyFunc, arglist);
if(result == NULL) { std::cout << "Error in field callback" << std::endl; break; }
int success = Process_Python_Result<double>(result, &cResult);
if(!success) { std::cout << "Error: Non-numeric data returned from field callback" << std::endl; break; }
if(cResult.size() != fieldDim) { std::cout << "Error: Data returned from field callback has different dimensions to field" << std::endl; break; }
for(int d=0; d<fieldDim; d++)
{
long idx = d+i*fieldDim;
thisField->data[idx] = cResult[d];
}
cResult.clear();
Py_DECREF(arglist);
Py_XDECREF(result);
}
}
Py_XDECREF(PyFunc);
}
DoubleField *Model::GetDoubleField(int handle)
{
return doubleFields[handle];
}
int Model::GetNumberParticles(){
return (int)this->numParticles;
}
double* Model::GetPositions(){
return positions;
}
void Model::SetParameter(std::string key, std::string value)
{
parameters[key] = value;
}
void Model::SetParameters(std::map<std::string, std::string> ¶meters)
{
std::map<std::string, std::string>::iterator it;
for ( it = parameters.begin(); it != parameters.end(); it++ )
{
this->parameters[it->first] = it->second;
}
}
int Model::ReadSingleIntegerParameter(std::string key)
{
return atoi(parameters[key].c_str());
}
std::vector<double> Model::ReadMultipleDoubleParameter(std::string key, int n)
{
std::vector<double> values(n,0.0);
double a;
stringstream stream(parameters[key]);
for (int i=0; i<n; i++) {stream>>values[i];}
return values;
}
template<typename T>
int Model::Process_Python_Result(PyObject* incoming, std::vector<T> *data) {
bool failure = false;
if (PyTuple_Check(incoming)) {
for(Py_ssize_t i = 0; i < PyTuple_Size(incoming); i++) {
PyObject *value = PyTuple_GetItem(incoming, i);
if(PyFloat_Check(value)) data->push_back((T)PyFloat_AsDouble(value));
else failure = true;
}
} else if (PyList_Check(incoming)) {
for(Py_ssize_t i = 0; i < PyList_Size(incoming); i++) {
PyObject *value = PyList_GetItem(incoming, i);
if(PyFloat_Check(value)) data->push_back((T)PyFloat_AsDouble(value));
else failure = true;
}
} else if(PyFloat_Check(incoming))
{
data->push_back( (T)PyFloat_AsDouble(incoming) );
} else failure = true;
if(failure) return 0;
return 1;
}