# | Line 13 | Line 13 | |
---|---|---|
13 | ||
14 | #ifdef IS_MPI | |
15 | #include <mpi.h> | |
16 | – | #include <mpi++.h> |
16 | #include "mpiSimulation.hpp" | |
17 | < | #define TAKE_THIS_TAG 0 |
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 ){ |
29 | > | InitializeFromFile::InitializeFromFile( char *in_name ){ |
30 | > | |
31 | > | std::cerr << "Hello from Initialize from file.\n"; |
32 | > | |
33 | #ifdef IS_MPI | |
34 | if (worldRank == 0) { | |
35 | #endif | |
# | Line 40 | Line 51 | InitializeFromFile :: InitializeFromFile( char *in_nam | |
51 | return; | |
52 | } | |
53 | ||
54 | < | InitializeFromFile :: ~InitializeFromFile( ){ |
54 | > | InitializeFromFile::~InitializeFromFile( ){ |
55 | #ifdef IS_MPI | |
56 | if (worldRank == 0) { | |
57 | #endif | |
# | Line 61 | Line 72 | InitializeFromFile :: ~InitializeFromFile( ){ | |
72 | } | |
73 | ||
74 | ||
75 | < | void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){ |
75 | > | void InitializeFromFile :: readInit( SimInfo* the_simnfo ){ |
76 | ||
77 | int i, j, done, which_node, which_atom; // loop counter | |
78 | ||
# | Line 75 | Line 86 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
86 | char *eof_test; // ptr to see when we reach the end of the file | |
87 | char *parseErr; | |
88 | int procIndex; | |
89 | + | double currTime; |
90 | + | double boxMat[9]; |
91 | + | double theBoxMat3[3][3]; |
92 | ||
93 | < | entry_plug = the_entry_plug; |
93 | > | simnfo = the_simnfo; |
94 | ||
95 | ||
96 | #ifndef IS_MPI | |
# | Line 91 | Line 105 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
105 | ||
106 | n_atoms = atoi( read_buffer ); | |
107 | ||
108 | < | Atom **atoms = entry_plug->atoms; |
108 | > | Atom **atoms = simnfo->atoms; |
109 | DirectionalAtom* dAtom; | |
110 | ||
111 | < | if( n_atoms != entry_plug->n_atoms ){ |
111 | > | if( n_atoms != simnfo->n_atoms ){ |
112 | sprintf( painCave.errMsg, | |
113 | "Initialize from File error. %s n_atoms, %d, " | |
114 | "does not match the BASS file's n_atoms, %d.\n", | |
115 | < | c_in_name, n_atoms, entry_plug->n_atoms ); |
115 | > | c_in_name, n_atoms, simnfo->n_atoms ); |
116 | painCave.isFatal = 1; | |
117 | simError(); | |
118 | } | |
119 | ||
120 | < | //read and toss the comment line |
120 | > | //read the box mat from the comment line |
121 | ||
122 | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); | |
123 | if(eof_test == NULL){ | |
# | Line 113 | Line 127 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
127 | simError(); | |
128 | } | |
129 | ||
130 | + | parseErr = parseBoxLine( read_buffer, boxMat, currTime ); |
131 | + | if( parseErr != NULL ){ |
132 | + | strcpy( painCave.errMsg, parseErr ); |
133 | + | painCave.isFatal = 1; |
134 | + | simError(); |
135 | + | } |
136 | + | |
137 | + | for(i=0;i<3;i++) |
138 | + | for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i]; |
139 | + | |
140 | + | simnfo->setBoxM( theBoxMat3 ); |
141 | + | simnfo->setTime( currTime ); |
142 | + | |
143 | + | |
144 | for( i=0; i < n_atoms; i++){ | |
145 | ||
146 | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); | |
# | Line 132 | Line 160 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
160 | strcpy( painCave.errMsg, parseErr ); | |
161 | painCave.isFatal = 1; | |
162 | simError(); | |
163 | < | } |
163 | > | } |
164 | } | |
165 | ||
166 | ||
167 | // MPI Section of code.......... | |
168 | #else //IS_MPI | |
169 | ||
170 | < | MPI::Status istatus; |
170 | > | // first thing first, suspend fatalities. |
171 | > | painCave.isEventLoop = 1; |
172 | > | |
173 | > | int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone |
174 | > | int haveError; |
175 | > | |
176 | > | MPI_Status istatus; |
177 | int *AtomToProcMap = mpiSim->getAtomToProcMap(); | |
178 | ||
179 | + | |
180 | + | haveError = 0; |
181 | if (worldRank == 0) { | |
182 | + | |
183 | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); | |
184 | if( eof_test == NULL ){ | |
185 | sprintf( painCave.errMsg, | |
186 | "Error reading 1st line of %d \n ",c_in_name); | |
187 | < | painCave.isFatal = 1; |
187 | > | haveError = 1; |
188 | simError(); | |
189 | } | |
190 | ||
191 | n_atoms = atoi( read_buffer ); | |
192 | ||
193 | < | Atom **atoms = entry_plug->atoms; |
193 | > | Atom **atoms = simnfo->atoms; |
194 | DirectionalAtom* dAtom; | |
195 | ||
196 | // Check to see that the number of atoms in the intial configuration file is the | |
# | Line 163 | Line 200 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
200 | sprintf( painCave.errMsg, | |
201 | "Initialize from File error. %s n_atoms, %d, " | |
202 | "does not match the BASS file's n_atoms, %d.\n", | |
203 | < | c_in_name, n_atoms, entry_plug->n_atoms ); |
204 | < | painCave.isFatal = 1; |
203 | > | c_in_name, n_atoms, simnfo->n_atoms ); |
204 | > | haveError= 1; |
205 | simError(); | |
206 | } | |
207 | ||
208 | < | //read and toss the comment line |
208 | > | //read the boxMat from the comment line |
209 | ||
210 | eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); | |
211 | if(eof_test == NULL){ | |
212 | sprintf( painCave.errMsg, | |
213 | "error in reading commment in %s\n", c_in_name); | |
214 | < | painCave.isFatal = 1; |
214 | > | haveError = 1; |
215 | simError(); | |
216 | } | |
217 | + | |
218 | + | parseErr = parseBoxLine( read_buffer, boxMat, currTime ); |
219 | + | if( parseErr != NULL ){ |
220 | + | strcpy( painCave.errMsg, parseErr ); |
221 | + | haveError = 1; |
222 | + | simError(); |
223 | + | } |
224 | + | |
225 | + | MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD ); |
226 | + | MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD ); |
227 | ||
228 | + | if(haveError) nodeZeroError(); |
229 | ||
230 | for (i=0 ; i < mpiSim->getTotAtoms(); i++) { | |
231 | ||
# | Line 188 | Line 236 | void InitializeFromFile :: read_xyz( SimInfo* the_entr | |
236 | "natoms = %d; index = %d\n" | |
237 | "error reading the line from the file.\n", | |
238 | c_in_name, n_atoms, i ); | |
239 | < | painCave.isFatal = 1; |
239 | > | haveError= 1; |
240 | simError(); | |
241 | } | |
242 | ||
243 | + | if(haveError) nodeZeroError(); |
244 | + | |
245 | // Get the Node number which wants this atom: | |
246 | which_node = AtomToProcMap[i]; | |
247 | < | if (which_node == mpiSim->getMyNode()) { |
247 | > | if (which_node == 0) { |
248 | parseErr = parseDumpLine( read_buffer, i ); | |
249 | if( parseErr != NULL ){ | |
250 | strcpy( painCave.errMsg, parseErr ); | |
251 | < | painCave.isFatal = 1; |
251 | > | haveError = 1; |
252 | simError(); | |
253 | } | |
254 | < | } else { |
255 | < | MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, |
256 | < | TAKE_THIS_TAG); |
257 | < | MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG); |
254 | > | if(haveError) nodeZeroError(); |
255 | > | } |
256 | > | |
257 | > | else { |
258 | > | |
259 | > | myStatus = 1; |
260 | > | MPI_Send(&myStatus, 1, MPI_INT, which_node, |
261 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
262 | > | MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, |
263 | > | TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); |
264 | > | MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, |
265 | > | MPI_COMM_WORLD); |
266 | > | MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, |
267 | > | MPI_COMM_WORLD, &istatus); |
268 | > | |
269 | > | if(!myStatus) nodeZeroError(); |
270 | } | |
271 | } | |
272 | < | sprintf(read_buffer, "GAMEOVER"); |
272 | > | myStatus = -1; |
273 | for (j = 0; j < mpiSim->getNumberProcessors(); j++) { | |
274 | < | MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, j, |
275 | < | TAKE_THIS_TAG); |
274 | > | MPI_Send( &myStatus, 1, MPI_INT, j, |
275 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
276 | } | |
277 | ||
278 | } else { | |
279 | ||
280 | + | MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
281 | + | MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); |
282 | + | |
283 | done = 0; | |
284 | while (!done) { | |
285 | < | MPI::COMM_WORLD.Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, |
286 | < | TAKE_THIS_TAG, istatus); |
287 | < | if (strcmp(read_buffer, "GAMEOVER")) { |
288 | < | done = 1; |
289 | < | continue; |
290 | < | } else { |
291 | < | MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, |
292 | < | TAKE_THIS_TAG, istatus); |
293 | < | |
294 | < | parseErr = parseDumpLine( read_buffer, which_atom ); |
295 | < | if( parseErr != NULL ){ |
296 | < | strcpy( painCave.errMsg, parseErr ); |
297 | < | painCave.isFatal = 1; |
298 | < | simError(); |
299 | < | } |
285 | > | |
286 | > | MPI_Recv(&myStatus, 1, MPI_INT, 0, |
287 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); |
288 | > | |
289 | > | if(!myStatus) anonymousNodeDie(); |
290 | > | |
291 | > | if(myStatus < 0) break; |
292 | > | |
293 | > | MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, |
294 | > | TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus); |
295 | > | MPI_Recv(&which_atom, 1, MPI_INT, 0, |
296 | > | TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); |
297 | > | |
298 | > | myStatus = 1; |
299 | > | parseErr = parseDumpLine( read_buffer, which_atom ); |
300 | > | if( parseErr != NULL ){ |
301 | > | strcpy( painCave.errMsg, parseErr ); |
302 | > | myStatus = 0;; |
303 | > | simError(); |
304 | } | |
305 | + | |
306 | + | MPI_Send( &myStatus, 1, MPI_INT, 0, |
307 | + | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
308 | + | |
309 | } | |
310 | } | |
311 | < | |
311 | > | |
312 | > | // last thing last, enable fatalities. |
313 | > | painCave.isEventLoop = 0; |
314 | > | |
315 | > | for(i=0;i<3;i++) |
316 | > | for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i]; |
317 | > | |
318 | > | simnfo->setBoxM( theBoxMat3 ); |
319 | > | simnfo->setTime( currTime ); |
320 | > | |
321 | > | |
322 | #endif | |
323 | } | |
324 | ||
# | Line 243 | Line 326 | char* InitializeFromFile::parseDumpLine(char* readLine | |
326 | ||
327 | char *foo; // the pointer to the current string token | |
328 | ||
329 | < | double rx, ry, rz; // position place holders |
330 | < | double vx, vy, vz; // velocity placeholders |
329 | > | double pos[3]; // position place holders |
330 | > | double vel[3]; // velocity placeholders |
331 | double q[4]; // the quaternions | |
332 | double jx, jy, jz; // angular velocity placeholders; | |
333 | double qSqr, qLength; // needed to normalize the quaternion vector. | |
334 | ||
335 | < | Atom **atoms = entry_plug->atoms; |
335 | > | Atom **atoms = simnfo->atoms; |
336 | DirectionalAtom* dAtom; | |
337 | ||
338 | int j, n_atoms, atomIndex; | |
# | Line 268 | Line 351 | char* InitializeFromFile::parseDumpLine(char* readLine | |
351 | return strdup( painCave.errMsg ); | |
352 | } | |
353 | #else | |
354 | < | n_atoms = entry_plug->n_atoms; |
354 | > | n_atoms = simnfo->n_atoms; |
355 | atomIndex = globalIndex; | |
356 | #endif // is_mpi | |
357 | ||
# | Line 297 | Line 380 | char* InitializeFromFile::parseDumpLine(char* readLine | |
380 | c_in_name, n_atoms, atomIndex ); | |
381 | return strdup( painCave.errMsg ); | |
382 | } | |
383 | < | rx = atof( foo ); |
383 | > | pos[0] = atof( foo ); |
384 | ||
385 | foo = strtok(NULL, " ,;\t"); | |
386 | if(foo == NULL){ | |
# | Line 307 | Line 390 | char* InitializeFromFile::parseDumpLine(char* readLine | |
390 | c_in_name, n_atoms, atomIndex ); | |
391 | return strdup( painCave.errMsg ); | |
392 | } | |
393 | < | ry = atof( foo ); |
393 | > | pos[1] = atof( foo ); |
394 | ||
395 | foo = strtok(NULL, " ,;\t"); | |
396 | if(foo == NULL){ | |
# | Line 317 | Line 400 | char* InitializeFromFile::parseDumpLine(char* readLine | |
400 | c_in_name, n_atoms, atomIndex ); | |
401 | return strdup( painCave.errMsg ); | |
402 | } | |
403 | < | rz = atof( foo ); |
403 | > | pos[2] = atof( foo ); |
404 | ||
405 | ||
406 | // get the velocities | |
# | Line 330 | Line 413 | char* InitializeFromFile::parseDumpLine(char* readLine | |
413 | c_in_name, n_atoms, atomIndex ); | |
414 | return strdup( painCave.errMsg ); | |
415 | } | |
416 | < | vx = atof( foo ); |
416 | > | vel[0] = atof( foo ); |
417 | ||
418 | foo = strtok(NULL, " ,;\t"); | |
419 | if(foo == NULL){ | |
# | Line 340 | Line 423 | char* InitializeFromFile::parseDumpLine(char* readLine | |
423 | c_in_name, n_atoms, atomIndex ); | |
424 | return strdup( painCave.errMsg ); | |
425 | } | |
426 | < | vy = atof( foo ); |
426 | > | vel[1] = atof( foo ); |
427 | ||
428 | foo = strtok(NULL, " ,;\t"); | |
429 | if(foo == NULL){ | |
# | Line 350 | Line 433 | char* InitializeFromFile::parseDumpLine(char* readLine | |
433 | c_in_name, n_atoms, atomIndex ); | |
434 | return strdup( painCave.errMsg ); | |
435 | } | |
436 | < | vz = atof( foo ); |
436 | > | vel[2] = atof( foo ); |
437 | ||
438 | ||
439 | // get the quaternions | |
# | Line 452 | Line 535 | char* InitializeFromFile::parseDumpLine(char* readLine | |
535 | ||
536 | // add the positions and velocities to the atom | |
537 | ||
538 | < | atoms[atomIndex]->setX( rx ); |
539 | < | atoms[atomIndex]->setY( ry ); |
540 | < | atoms[atomIndex]->setZ( rz ); |
541 | < | |
542 | < | atoms[atomIndex]->set_vx( vx ); |
543 | < | atoms[atomIndex]->set_vy( vy ); |
544 | < | atoms[atomIndex]->set_vz( vz ); |
538 | > | atoms[atomIndex]->setPos( pos ); |
539 | > | atoms[atomIndex]->setVel( vel ); |
540 | > | |
541 | > | return NULL; |
542 | > | } |
543 | > | |
544 | > | |
545 | > | char* InitializeFromFile::parseBoxLine(char* readLine, double boxMat[9], |
546 | > | double &time ){ |
547 | > | |
548 | > | char *foo; // the pointer to the current string token |
549 | > | int j; |
550 | > | |
551 | > | // set the string tokenizer |
552 | > | |
553 | > | foo = strtok(readLine, " ,;\t"); |
554 | > | // set the timeToken. |
555 | > | |
556 | > | if(foo == NULL){ |
557 | > | sprintf( painCave.errMsg, |
558 | > | "error in reading Time from %s\n", |
559 | > | c_in_name ); |
560 | > | return strdup( painCave.errMsg ); |
561 | > | } |
562 | > | time = atof( foo ); |
563 | > | |
564 | > | // get the Hx vector |
565 | > | |
566 | > | foo = strtok(NULL, " ,;\t"); |
567 | > | if(foo == NULL){ |
568 | > | sprintf( painCave.errMsg, |
569 | > | "error in reading Hx[0] from %s\n", |
570 | > | c_in_name ); |
571 | > | return strdup( painCave.errMsg ); |
572 | > | } |
573 | > | boxMat[0] = atof( foo ); |
574 | > | |
575 | > | foo = strtok(NULL, " ,;\t"); |
576 | > | if(foo == NULL){ |
577 | > | sprintf( painCave.errMsg, |
578 | > | "error in reading Hx[1] from %s\n", |
579 | > | c_in_name ); |
580 | > | return strdup( painCave.errMsg ); |
581 | > | } |
582 | > | boxMat[1] = atof( foo ); |
583 | > | |
584 | > | foo = strtok(NULL, " ,;\t"); |
585 | > | if(foo == NULL){ |
586 | > | sprintf( painCave.errMsg, |
587 | > | "error in reading Hx[2] from %s\n", |
588 | > | c_in_name ); |
589 | > | return strdup( painCave.errMsg ); |
590 | > | } |
591 | > | boxMat[2] = atof( foo ); |
592 | > | |
593 | > | // get the Hy vector |
594 | > | |
595 | > | foo = strtok(NULL, " ,;\t"); |
596 | > | if(foo == NULL){ |
597 | > | sprintf( painCave.errMsg, |
598 | > | "error in reading Hy[0] from %s\n", |
599 | > | c_in_name ); |
600 | > | return strdup( painCave.errMsg ); |
601 | > | } |
602 | > | boxMat[3] = atof( foo ); |
603 | > | |
604 | > | foo = strtok(NULL, " ,;\t"); |
605 | > | if(foo == NULL){ |
606 | > | sprintf( painCave.errMsg, |
607 | > | "error in reading Hy[1] from %s\n", |
608 | > | c_in_name ); |
609 | > | return strdup( painCave.errMsg ); |
610 | > | } |
611 | > | boxMat[4] = atof( foo ); |
612 | > | |
613 | > | foo = strtok(NULL, " ,;\t"); |
614 | > | if(foo == NULL){ |
615 | > | sprintf( painCave.errMsg, |
616 | > | "error in reading Hy[2] from %s\n", |
617 | > | c_in_name ); |
618 | > | return strdup( painCave.errMsg ); |
619 | > | } |
620 | > | boxMat[5] = atof( foo ); |
621 | ||
622 | + | // get the Hz vector |
623 | + | |
624 | + | foo = strtok(NULL, " ,;\t"); |
625 | + | if(foo == NULL){ |
626 | + | sprintf( painCave.errMsg, |
627 | + | "error in reading Hz[0] from %s\n", |
628 | + | c_in_name ); |
629 | + | return strdup( painCave.errMsg ); |
630 | + | } |
631 | + | boxMat[6] = atof( foo ); |
632 | + | |
633 | + | foo = strtok(NULL, " ,;\t"); |
634 | + | if(foo == NULL){ |
635 | + | sprintf( painCave.errMsg, |
636 | + | "error in reading Hz[1] from %s\n", |
637 | + | c_in_name ); |
638 | + | return strdup( painCave.errMsg ); |
639 | + | } |
640 | + | boxMat[7] = atof( foo ); |
641 | + | |
642 | + | foo = strtok(NULL, " ,;\t"); |
643 | + | if(foo == NULL){ |
644 | + | sprintf( painCave.errMsg, |
645 | + | "error in reading Hz[2] from %s\n", |
646 | + | c_in_name ); |
647 | + | return strdup( painCave.errMsg ); |
648 | + | } |
649 | + | boxMat[8] = atof( foo ); |
650 | + | |
651 | return NULL; | |
652 | } | |
653 | + | |
654 | + | |
655 | + | #ifdef IS_MPI |
656 | + | |
657 | + | // a couple of functions to let us escape the read loop |
658 | + | |
659 | + | void initFile::nodeZeroError( void ){ |
660 | + | int j, myStatus; |
661 | + | |
662 | + | myStatus = 0; |
663 | + | for (j = 0; j < mpiSim->getNumberProcessors(); j++) { |
664 | + | MPI_Send( &myStatus, 1, MPI_INT, j, |
665 | + | TAKE_THIS_TAG_INT, MPI_COMM_WORLD); |
666 | + | } |
667 | + | |
668 | + | |
669 | + | MPI_Finalize(); |
670 | + | exit (0); |
671 | + | |
672 | + | } |
673 | + | |
674 | + | void initFile::anonymousNodeDie( void ){ |
675 | + | |
676 | + | MPI_Finalize(); |
677 | + | exit (0); |
678 | + | } |
679 | + | |
680 | + | #endif //is_mpi |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |