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