1 |
#include <iostream> |
2 |
#include <cmath> |
3 |
|
4 |
#include <stdio.h> |
5 |
#include <stdlib.h> |
6 |
#include <string.h> |
7 |
#include <unistd.h> |
8 |
#include <sys/types.h> |
9 |
#include <sys/stat.h> |
10 |
|
11 |
#include "ReadWrite.hpp" |
12 |
#include "simError.h" |
13 |
|
14 |
#ifdef IS_MPI |
15 |
#include <mpi.h> |
16 |
#include <mpi++.h> |
17 |
#include "mpiSimulation.hpp" |
18 |
#define TAKE_THIS_TAG 0 |
19 |
#endif // is_mpi |
20 |
|
21 |
InitializeFromFile :: InitializeFromFile( char *in_name ){ |
22 |
#ifdef IS_MPI |
23 |
if (worldRank == 0) { |
24 |
#endif |
25 |
|
26 |
c_in_file = fopen(in_name, "r"); |
27 |
if(c_in_file == NULL){ |
28 |
sprintf(painCave.errMsg, |
29 |
"Cannot open file: %s\n", in_name); |
30 |
painCave.isFatal = 1; |
31 |
simError(); |
32 |
} |
33 |
|
34 |
strcpy( c_in_name, in_name); |
35 |
#ifdef IS_MPI |
36 |
} |
37 |
strcpy( checkPointMsg, "Infile opened for reading successfully." ); |
38 |
MPIcheckPoint(); |
39 |
#endif |
40 |
return; |
41 |
} |
42 |
|
43 |
InitializeFromFile :: ~InitializeFromFile( ){ |
44 |
#ifdef IS_MPI |
45 |
if (worldRank == 0) { |
46 |
#endif |
47 |
int error; |
48 |
error = fclose( c_in_file ); |
49 |
if( error ){ |
50 |
sprintf( painCave.errMsg, |
51 |
"Error closing %s\n", c_in_name ); |
52 |
simError(); |
53 |
} |
54 |
#ifdef IS_MPI |
55 |
} |
56 |
strcpy( checkPointMsg, "Infile closed successfully." ); |
57 |
MPIcheckPoint(); |
58 |
#endif |
59 |
|
60 |
return; |
61 |
} |
62 |
|
63 |
|
64 |
void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){ |
65 |
|
66 |
int i, j, done, which_node, which_atom; // loop counter |
67 |
|
68 |
const int BUFFERSIZE = 2000; // size of the read buffer |
69 |
int n_atoms; // the number of atoms |
70 |
char read_buffer[BUFFERSIZE]; //the line buffer for reading |
71 |
#ifdef IS_MPI |
72 |
char send_buffer[BUFFERSIZE]; |
73 |
#endif |
74 |
|
75 |
char *eof_test; // ptr to see when we reach the end of the file |
76 |
char *parseErr; |
77 |
int procIndex; |
78 |
|
79 |
entry_plug = the_entry_plug; |
80 |
|
81 |
|
82 |
#ifndef IS_MPI |
83 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
84 |
if( eof_test == NULL ){ |
85 |
sprintf( painCave.errMsg, |
86 |
"InitializeFromFile error: error reading 1st line of \"%s\"\n", |
87 |
c_in_name ); |
88 |
painCave.isFatal = 1; |
89 |
simError(); |
90 |
} |
91 |
|
92 |
n_atoms = atoi( read_buffer ); |
93 |
|
94 |
Atom **atoms = entry_plug->atoms; |
95 |
DirectionalAtom* dAtom; |
96 |
|
97 |
if( n_atoms != entry_plug->n_atoms ){ |
98 |
sprintf( painCave.errMsg, |
99 |
"Initialize from File error. %s n_atoms, %d, " |
100 |
"does not match the BASS file's n_atoms, %d.\n", |
101 |
c_in_name, n_atoms, entry_plug->n_atoms ); |
102 |
painCave.isFatal = 1; |
103 |
simError(); |
104 |
} |
105 |
|
106 |
//read and toss the comment line |
107 |
|
108 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
109 |
if(eof_test == NULL){ |
110 |
sprintf( painCave.errMsg, |
111 |
"error in reading commment in %s\n", c_in_name); |
112 |
painCave.isFatal = 1; |
113 |
simError(); |
114 |
} |
115 |
|
116 |
for( i=0; i < n_atoms; i++){ |
117 |
|
118 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
119 |
if(eof_test == NULL){ |
120 |
sprintf(painCave.errMsg, |
121 |
"error in reading file %s\n" |
122 |
"natoms = %d; index = %d\n" |
123 |
"error reading the line from the file.\n", |
124 |
c_in_name, n_atoms, i ); |
125 |
painCave.isFatal = 1; |
126 |
simError(); |
127 |
} |
128 |
|
129 |
|
130 |
parseErr = parseDumpLine( read_buffer, i ); |
131 |
if( parseErr != NULL ){ |
132 |
strcpy( painCave.errMsg, parseErr ); |
133 |
painCave.isFatal = 1; |
134 |
simError(); |
135 |
} |
136 |
} |
137 |
|
138 |
|
139 |
// MPI Section of code.......... |
140 |
#else //IS_MPI |
141 |
|
142 |
MPI::Status istatus; |
143 |
int *AtomToProcMap = mpiSim->getAtomToProcMap(); |
144 |
|
145 |
if (worldRank == 0) { |
146 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
147 |
if( eof_test == NULL ){ |
148 |
sprintf( painCave.errMsg, |
149 |
"Error reading 1st line of %d \n ",c_in_name); |
150 |
painCave.isFatal = 1; |
151 |
simError(); |
152 |
} |
153 |
|
154 |
n_atoms = atoi( read_buffer ); |
155 |
|
156 |
Atom **atoms = entry_plug->atoms; |
157 |
DirectionalAtom* dAtom; |
158 |
|
159 |
// Check to see that the number of atoms in the intial configuration file is the |
160 |
// same as declared in simBass. |
161 |
|
162 |
if( n_atoms != mpiSim->getTotAtoms() ){ |
163 |
sprintf( painCave.errMsg, |
164 |
"Initialize from File error. %s n_atoms, %d, " |
165 |
"does not match the BASS file's n_atoms, %d.\n", |
166 |
c_in_name, n_atoms, entry_plug->n_atoms ); |
167 |
painCave.isFatal = 1; |
168 |
simError(); |
169 |
} |
170 |
|
171 |
//read and toss the comment line |
172 |
|
173 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
174 |
if(eof_test == NULL){ |
175 |
sprintf( painCave.errMsg, |
176 |
"error in reading commment in %s\n", c_in_name); |
177 |
painCave.isFatal = 1; |
178 |
simError(); |
179 |
} |
180 |
|
181 |
|
182 |
for (i=0 ; i < mpiSim->getTotAtoms(); i++) { |
183 |
|
184 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
185 |
if(eof_test == NULL){ |
186 |
sprintf(painCave.errMsg, |
187 |
"error in reading file %s\n" |
188 |
"natoms = %d; index = %d\n" |
189 |
"error reading the line from the file.\n", |
190 |
c_in_name, n_atoms, i ); |
191 |
painCave.isFatal = 1; |
192 |
simError(); |
193 |
} |
194 |
|
195 |
// Get the Node number which wants this atom: |
196 |
which_node = AtomToProcMap[i]; |
197 |
if (which_node == mpiSim->getMyNode()) { |
198 |
parseErr = parseDumpLine( read_buffer, i ); |
199 |
if( parseErr != NULL ){ |
200 |
strcpy( painCave.errMsg, parseErr ); |
201 |
painCave.isFatal = 1; |
202 |
simError(); |
203 |
} |
204 |
} else { |
205 |
MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, |
206 |
TAKE_THIS_TAG); |
207 |
MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG); |
208 |
} |
209 |
} |
210 |
sprintf(read_buffer, "GAMEOVER"); |
211 |
for (j = 0; j < mpiSim->getNumberProcessors(); j++) { |
212 |
MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, j, |
213 |
TAKE_THIS_TAG); |
214 |
} |
215 |
|
216 |
} else { |
217 |
|
218 |
done = 0; |
219 |
while (!done) { |
220 |
MPI::COMM_WORLD.Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, |
221 |
TAKE_THIS_TAG, istatus); |
222 |
if (strcmp(read_buffer, "GAMEOVER")) { |
223 |
done = 1; |
224 |
continue; |
225 |
} else { |
226 |
MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, |
227 |
TAKE_THIS_TAG, istatus); |
228 |
parseErr = parseDumpLine( read_buffer, which_atom ); |
229 |
if( parseErr != NULL ){ |
230 |
strcpy( painCave.errMsg, parseErr ); |
231 |
painCave.isFatal = 1; |
232 |
simError(); |
233 |
} |
234 |
} |
235 |
} |
236 |
} |
237 |
|
238 |
#endif |
239 |
} |
240 |
|
241 |
char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ |
242 |
|
243 |
char *foo; // the pointer to the current string token |
244 |
|
245 |
double rx, ry, rz; // position place holders |
246 |
double vx, vy, vz; // velocity placeholders |
247 |
double q[4]; // the quaternions |
248 |
double jx, jy, jz; // angular velocity placeholders; |
249 |
double qSqr, qLength; // needed to normalize the quaternion vector. |
250 |
|
251 |
Atom **atoms = entry_plug->atoms; |
252 |
DirectionalAtom* dAtom; |
253 |
|
254 |
int n_atoms; |
255 |
|
256 |
#ifdef IS_MPI |
257 |
n_atoms = mpiSim->getTotAtoms(); |
258 |
#else |
259 |
n_atoms = entry_plug->n_atoms; |
260 |
#endif // is_mpi |
261 |
|
262 |
|
263 |
// set the string tokenizer |
264 |
|
265 |
foo = strtok(readLine, " ,;\t"); |
266 |
|
267 |
// check the atom name to the current atom |
268 |
|
269 |
if( strcmp( foo, atoms[atomIndex]->getType() ) ){ |
270 |
sprintf( painCave.errMsg, |
271 |
"Initialize from file error. Atom %s at index %d " |
272 |
"in file %s does not" |
273 |
" match the BASS atom %s.\n", |
274 |
foo, atomIndex, c_in_name, atoms[atomIndex]->getType() ); |
275 |
return strdup( painCave.errMsg ); |
276 |
} |
277 |
|
278 |
// get the positions |
279 |
|
280 |
foo = strtok(NULL, " ,;\t"); |
281 |
if(foo == NULL){ |
282 |
sprintf( painCave.errMsg, |
283 |
"error in reading postition x from %s\n" |
284 |
"natoms = %d, index = %d\n", |
285 |
c_in_name, n_atoms, atomIndex ); |
286 |
return strdup( painCave.errMsg ); |
287 |
} |
288 |
rx = atof( foo ); |
289 |
|
290 |
foo = strtok(NULL, " ,;\t"); |
291 |
if(foo == NULL){ |
292 |
sprintf( painCave.errMsg, |
293 |
"error in reading postition y from %s\n" |
294 |
"natoms = %d, index = %d\n", |
295 |
c_in_name, n_atoms, atomIndex ); |
296 |
return strdup( painCave.errMsg ); |
297 |
} |
298 |
ry = atof( foo ); |
299 |
|
300 |
foo = strtok(NULL, " ,;\t"); |
301 |
if(foo == NULL){ |
302 |
sprintf( painCave.errMsg, |
303 |
"error in reading postition z from %s\n" |
304 |
"natoms = %d, index = %d\n", |
305 |
c_in_name, n_atoms, atomIndex ); |
306 |
return strdup( painCave.errMsg ); |
307 |
} |
308 |
rz = atof( foo ); |
309 |
|
310 |
|
311 |
// get the velocities |
312 |
|
313 |
foo = strtok(NULL, " ,;\t"); |
314 |
if(foo == NULL){ |
315 |
sprintf( painCave.errMsg, |
316 |
"error in reading velocity x from %s\n" |
317 |
"natoms = %d, index = %d\n", |
318 |
c_in_name, n_atoms, atomIndex ); |
319 |
return strdup( painCave.errMsg ); |
320 |
} |
321 |
vx = atof( foo ); |
322 |
|
323 |
foo = strtok(NULL, " ,;\t"); |
324 |
if(foo == NULL){ |
325 |
sprintf( painCave.errMsg, |
326 |
"error in reading velocity y from %s\n" |
327 |
"natoms = %d, index = %d\n", |
328 |
c_in_name, n_atoms, atomIndex ); |
329 |
return strdup( painCave.errMsg ); |
330 |
} |
331 |
vy = atof( foo ); |
332 |
|
333 |
foo = strtok(NULL, " ,;\t"); |
334 |
if(foo == NULL){ |
335 |
sprintf( painCave.errMsg, |
336 |
"error in reading velocity z from %s\n" |
337 |
"natoms = %d, index = %d\n", |
338 |
c_in_name, n_atoms, atomIndex ); |
339 |
return strdup( painCave.errMsg ); |
340 |
} |
341 |
vz = atof( foo ); |
342 |
|
343 |
|
344 |
// get the quaternions |
345 |
|
346 |
if( atoms[atomIndex]->isDirectional() ){ |
347 |
|
348 |
foo = strtok(NULL, " ,;\t"); |
349 |
if(foo == NULL){ |
350 |
sprintf(painCave.errMsg, |
351 |
"error in reading quaternion 0 from %s\n" |
352 |
"natoms = %d, index = %d\n", |
353 |
c_in_name, n_atoms, atomIndex ); |
354 |
return strdup( painCave.errMsg ); |
355 |
} |
356 |
q[0] = atof( foo ); |
357 |
|
358 |
foo = strtok(NULL, " ,;\t"); |
359 |
if(foo == NULL){ |
360 |
sprintf( painCave.errMsg, |
361 |
"error in reading quaternion 1 from %s\n" |
362 |
"natoms = %d, index = %d\n", |
363 |
c_in_name, n_atoms, atomIndex ); |
364 |
return strdup( painCave.errMsg ); |
365 |
} |
366 |
q[1] = atof( foo ); |
367 |
|
368 |
foo = strtok(NULL, " ,;\t"); |
369 |
if(foo == NULL){ |
370 |
sprintf( painCave.errMsg, |
371 |
"error in reading quaternion 2 from %s\n" |
372 |
"natoms = %d, index = %d\n", |
373 |
c_in_name, n_atoms, atomIndex ); |
374 |
return strdup( painCave.errMsg ); |
375 |
} |
376 |
q[2] = atof( foo ); |
377 |
|
378 |
foo = strtok(NULL, " ,;\t"); |
379 |
if(foo == NULL){ |
380 |
sprintf( painCave.errMsg, |
381 |
"error in reading quaternion 3 from %s\n" |
382 |
"natoms = %d, index = %d\n", |
383 |
c_in_name, n_atoms, atomIndex ); |
384 |
return strdup( painCave.errMsg ); |
385 |
} |
386 |
q[3] = atof( foo ); |
387 |
|
388 |
// get the angular velocities |
389 |
|
390 |
foo = strtok(NULL, " ,;\t"); |
391 |
if(foo == NULL){ |
392 |
sprintf( painCave.errMsg, |
393 |
"error in reading angular momentum jx from %s\n" |
394 |
"natoms = %d, index = %d\n", |
395 |
c_in_name, n_atoms, atomIndex ); |
396 |
return strdup( painCave.errMsg ); |
397 |
} |
398 |
jx = atof( foo ); |
399 |
|
400 |
foo = strtok(NULL, " ,;\t"); |
401 |
if(foo == NULL){ |
402 |
sprintf( painCave.errMsg, |
403 |
"error in reading angular momentum jy from %s\n" |
404 |
"natoms = %d, index = %d\n", |
405 |
c_in_name, n_atoms, atomIndex ); |
406 |
return strdup( painCave.errMsg ); |
407 |
} |
408 |
jy = atof(foo ); |
409 |
|
410 |
foo = strtok(NULL, " ,;\t"); |
411 |
if(foo == NULL){ |
412 |
sprintf( painCave.errMsg, |
413 |
"error in reading angular momentum jz from %s\n" |
414 |
"natoms = %d, index = %d\n", |
415 |
c_in_name, n_atoms, atomIndex ); |
416 |
return strdup( painCave.errMsg ); |
417 |
} |
418 |
jz = atof( foo ); |
419 |
|
420 |
dAtom = ( DirectionalAtom* )atoms[atomIndex]; |
421 |
|
422 |
// check that the quaternion vector is normalized |
423 |
|
424 |
qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); |
425 |
|
426 |
qLength = sqrt( qSqr ); |
427 |
q[0] = q[0] / qLength; |
428 |
q[1] = q[1] / qLength; |
429 |
q[2] = q[2] / qLength; |
430 |
q[3] = q[3] / qLength; |
431 |
|
432 |
dAtom->setQ( q ); |
433 |
|
434 |
// add the angular velocities |
435 |
|
436 |
dAtom->setJx( jx ); |
437 |
dAtom->setJy( jy ); |
438 |
dAtom->setJz( jz ); |
439 |
} |
440 |
|
441 |
// add the positions and velocities to the atom |
442 |
|
443 |
atoms[atomIndex]->setX( rx ); |
444 |
atoms[atomIndex]->setY( ry ); |
445 |
atoms[atomIndex]->setZ( rz ); |
446 |
|
447 |
atoms[atomIndex]->set_vx( vx ); |
448 |
atoms[atomIndex]->set_vy( vy ); |
449 |
atoms[atomIndex]->set_vz( vz ); |
450 |
|
451 |
return NULL; |
452 |
} |