ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/InitializeFromFile.cpp
Revision: 670
Committed: Thu Aug 7 21:47:18 2003 UTC (20 years, 11 months ago) by mmeineke
File size: 16533 byte(s)
Log Message:
switched SimInfo to use a system configuration from SimState rather than arrays from Atom

File Contents

# Content
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 :: readInit( SimInfo* the_simnfo ){
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 double currTime;
87 double boxMat[9];
88 double theBoxMat3[3][3];
89
90 simnfo = the_simnfo;
91
92
93 #ifndef IS_MPI
94 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
95 if( eof_test == NULL ){
96 sprintf( painCave.errMsg,
97 "InitializeFromFile error: error reading 1st line of \"%s\"\n",
98 c_in_name );
99 painCave.isFatal = 1;
100 simError();
101 }
102
103 n_atoms = atoi( read_buffer );
104
105 Atom **atoms = simnfo->atoms;
106 DirectionalAtom* dAtom;
107
108 if( n_atoms != simnfo->n_atoms ){
109 sprintf( painCave.errMsg,
110 "Initialize from File error. %s n_atoms, %d, "
111 "does not match the BASS file's n_atoms, %d.\n",
112 c_in_name, n_atoms, simnfo->n_atoms );
113 painCave.isFatal = 1;
114 simError();
115 }
116
117 //read the box mat from the comment line
118
119 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
120 if(eof_test == NULL){
121 sprintf( painCave.errMsg,
122 "error in reading commment in %s\n", c_in_name);
123 painCave.isFatal = 1;
124 simError();
125 }
126
127 parseErr = parseBoxLine( read_buffer, boxMat, currTime );
128 if( parseErr != NULL ){
129 strcpy( painCave.errMsg, parseErr );
130 painCave.isFatal = 1;
131 simError();
132 }
133
134 for(i=0;i<3;i++)
135 for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i];
136
137 simnfo->setBoxM( theBoxMat3 );
138 simnfo->setTime( currTime );
139
140
141 for( i=0; i < n_atoms; i++){
142
143 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
144 if(eof_test == NULL){
145 sprintf(painCave.errMsg,
146 "error in reading file %s\n"
147 "natoms = %d; index = %d\n"
148 "error reading the line from the file.\n",
149 c_in_name, n_atoms, i );
150 painCave.isFatal = 1;
151 simError();
152 }
153
154
155 parseErr = parseDumpLine( read_buffer, i );
156 if( parseErr != NULL ){
157 strcpy( painCave.errMsg, parseErr );
158 painCave.isFatal = 1;
159 simError();
160 }
161 }
162
163
164 // MPI Section of code..........
165 #else //IS_MPI
166
167 // first thing first, suspend fatalities.
168 painCave.isEventLoop = 1;
169
170 int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
171 int haveError;
172
173 MPI_Status istatus;
174 int *AtomToProcMap = mpiSim->getAtomToProcMap();
175
176
177 haveError = 0;
178 if (worldRank == 0) {
179
180 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
181 if( eof_test == NULL ){
182 sprintf( painCave.errMsg,
183 "Error reading 1st line of %d \n ",c_in_name);
184 haveError = 1;
185 simError();
186 }
187
188 n_atoms = atoi( read_buffer );
189
190 Atom **atoms = simnfo->atoms;
191 DirectionalAtom* dAtom;
192
193 // Check to see that the number of atoms in the intial configuration file is the
194 // same as declared in simBass.
195
196 if( n_atoms != mpiSim->getTotAtoms() ){
197 sprintf( painCave.errMsg,
198 "Initialize from File error. %s n_atoms, %d, "
199 "does not match the BASS file's n_atoms, %d.\n",
200 c_in_name, n_atoms, simnfo->n_atoms );
201 haveError= 1;
202 simError();
203 }
204
205 //read the boxMat from the comment line
206
207 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
208 if(eof_test == NULL){
209 sprintf( painCave.errMsg,
210 "error in reading commment in %s\n", c_in_name);
211 haveError = 1;
212 simError();
213 }
214
215 parseErr = parseBoxLine( read_buffer, boxMat, currTime );
216 if( parseErr != NULL ){
217 strcpy( painCave.errMsg, parseErr );
218 haveError = 1;
219 simError();
220 }
221
222 MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD );
223 MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD );
224
225 if(haveError) nodeZeroError();
226
227 for (i=0 ; i < mpiSim->getTotAtoms(); i++) {
228
229 eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
230 if(eof_test == NULL){
231 sprintf(painCave.errMsg,
232 "error in reading file %s\n"
233 "natoms = %d; index = %d\n"
234 "error reading the line from the file.\n",
235 c_in_name, n_atoms, i );
236 haveError= 1;
237 simError();
238 }
239
240 if(haveError) nodeZeroError();
241
242 // Get the Node number which wants this atom:
243 which_node = AtomToProcMap[i];
244 if (which_node == 0) {
245 parseErr = parseDumpLine( read_buffer, i );
246 if( parseErr != NULL ){
247 strcpy( painCave.errMsg, parseErr );
248 haveError = 1;
249 simError();
250 }
251 if(haveError) nodeZeroError();
252 }
253
254 else {
255
256 myStatus = 1;
257 MPI_Send(&myStatus, 1, MPI_INT, which_node,
258 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
259 MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
260 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
261 MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
262 MPI_COMM_WORLD);
263 MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
264 MPI_COMM_WORLD, &istatus);
265
266 if(!myStatus) nodeZeroError();
267 }
268 }
269 myStatus = -1;
270 for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
271 MPI_Send( &myStatus, 1, MPI_INT, j,
272 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
273 }
274
275 } else {
276
277 MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD);
278 MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
279
280 done = 0;
281 while (!done) {
282
283 MPI_Recv(&myStatus, 1, MPI_INT, 0,
284 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
285
286 if(!myStatus) anonymousNodeDie();
287
288 if(myStatus < 0) break;
289
290 MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0,
291 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
292 MPI_Recv(&which_atom, 1, MPI_INT, 0,
293 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
294
295 myStatus = 1;
296 parseErr = parseDumpLine( read_buffer, which_atom );
297 if( parseErr != NULL ){
298 strcpy( painCave.errMsg, parseErr );
299 myStatus = 0;;
300 simError();
301 }
302
303 MPI_Send( &myStatus, 1, MPI_INT, 0,
304 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
305
306 }
307 }
308
309 // last thing last, enable fatalities.
310 painCave.isEventLoop = 0;
311
312 for(i=0;i<3;i++)
313 for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i];
314
315 simnfo->setBoxM( theBoxMat3 );
316 simnfo->setTime( currTime );
317
318
319 #endif
320 }
321
322 char* InitializeFromFile::parseDumpLine(char* readLine, int globalIndex){
323
324 char *foo; // the pointer to the current string token
325
326 double pos[3]; // position place holders
327 double vel[3]; // velocity placeholders
328 double q[4]; // the quaternions
329 double jx, jy, jz; // angular velocity placeholders;
330 double qSqr, qLength; // needed to normalize the quaternion vector.
331
332 Atom **atoms = simnfo->atoms;
333 DirectionalAtom* dAtom;
334
335 int j, n_atoms, atomIndex;
336
337 #ifdef IS_MPI
338 n_atoms = mpiSim->getTotAtoms();
339 atomIndex=-1;
340 for (j=0; j < mpiSim->getMyNlocal(); j++) {
341 if (atoms[j]->getGlobalIndex() == globalIndex) atomIndex = j;
342 }
343 if (atomIndex == -1) {
344 sprintf( painCave.errMsg,
345 "Initialize from file error. Atom at index %d "
346 "in file %s does not exist on processor %d .\n",
347 globalIndex, c_in_name, mpiSim->getMyNode() );
348 return strdup( painCave.errMsg );
349 }
350 #else
351 n_atoms = simnfo->n_atoms;
352 atomIndex = globalIndex;
353 #endif // is_mpi
354
355 // set the string tokenizer
356
357 foo = strtok(readLine, " ,;\t");
358
359 // check the atom name to the current atom
360
361 if( strcmp( foo, atoms[atomIndex]->getType() ) ){
362 sprintf( painCave.errMsg,
363 "Initialize from file error. Atom %s at index %d "
364 "in file %s does not"
365 " match the BASS atom %s.\n",
366 foo, atomIndex, c_in_name, atoms[atomIndex]->getType() );
367 return strdup( painCave.errMsg );
368 }
369
370 // get the positions
371
372 foo = strtok(NULL, " ,;\t");
373 if(foo == NULL){
374 sprintf( painCave.errMsg,
375 "error in reading postition x from %s\n"
376 "natoms = %d, index = %d\n",
377 c_in_name, n_atoms, atomIndex );
378 return strdup( painCave.errMsg );
379 }
380 pos[0] = atof( foo );
381
382 foo = strtok(NULL, " ,;\t");
383 if(foo == NULL){
384 sprintf( painCave.errMsg,
385 "error in reading postition y from %s\n"
386 "natoms = %d, index = %d\n",
387 c_in_name, n_atoms, atomIndex );
388 return strdup( painCave.errMsg );
389 }
390 pos[1] = atof( foo );
391
392 foo = strtok(NULL, " ,;\t");
393 if(foo == NULL){
394 sprintf( painCave.errMsg,
395 "error in reading postition z from %s\n"
396 "natoms = %d, index = %d\n",
397 c_in_name, n_atoms, atomIndex );
398 return strdup( painCave.errMsg );
399 }
400 pos[2] = atof( foo );
401
402
403 // get the velocities
404
405 foo = strtok(NULL, " ,;\t");
406 if(foo == NULL){
407 sprintf( painCave.errMsg,
408 "error in reading velocity x from %s\n"
409 "natoms = %d, index = %d\n",
410 c_in_name, n_atoms, atomIndex );
411 return strdup( painCave.errMsg );
412 }
413 vel[0] = atof( foo );
414
415 foo = strtok(NULL, " ,;\t");
416 if(foo == NULL){
417 sprintf( painCave.errMsg,
418 "error in reading velocity y from %s\n"
419 "natoms = %d, index = %d\n",
420 c_in_name, n_atoms, atomIndex );
421 return strdup( painCave.errMsg );
422 }
423 vel[1] = atof( foo );
424
425 foo = strtok(NULL, " ,;\t");
426 if(foo == NULL){
427 sprintf( painCave.errMsg,
428 "error in reading velocity z from %s\n"
429 "natoms = %d, index = %d\n",
430 c_in_name, n_atoms, atomIndex );
431 return strdup( painCave.errMsg );
432 }
433 vel[2] = atof( foo );
434
435
436 // get the quaternions
437
438 if( atoms[atomIndex]->isDirectional() ){
439
440 foo = strtok(NULL, " ,;\t");
441 if(foo == NULL){
442 sprintf(painCave.errMsg,
443 "error in reading quaternion 0 from %s\n"
444 "natoms = %d, index = %d\n",
445 c_in_name, n_atoms, atomIndex );
446 return strdup( painCave.errMsg );
447 }
448 q[0] = atof( foo );
449
450 foo = strtok(NULL, " ,;\t");
451 if(foo == NULL){
452 sprintf( painCave.errMsg,
453 "error in reading quaternion 1 from %s\n"
454 "natoms = %d, index = %d\n",
455 c_in_name, n_atoms, atomIndex );
456 return strdup( painCave.errMsg );
457 }
458 q[1] = atof( foo );
459
460 foo = strtok(NULL, " ,;\t");
461 if(foo == NULL){
462 sprintf( painCave.errMsg,
463 "error in reading quaternion 2 from %s\n"
464 "natoms = %d, index = %d\n",
465 c_in_name, n_atoms, atomIndex );
466 return strdup( painCave.errMsg );
467 }
468 q[2] = atof( foo );
469
470 foo = strtok(NULL, " ,;\t");
471 if(foo == NULL){
472 sprintf( painCave.errMsg,
473 "error in reading quaternion 3 from %s\n"
474 "natoms = %d, index = %d\n",
475 c_in_name, n_atoms, atomIndex );
476 return strdup( painCave.errMsg );
477 }
478 q[3] = atof( foo );
479
480 // get the angular velocities
481
482 foo = strtok(NULL, " ,;\t");
483 if(foo == NULL){
484 sprintf( painCave.errMsg,
485 "error in reading angular momentum jx from %s\n"
486 "natoms = %d, index = %d\n",
487 c_in_name, n_atoms, atomIndex );
488 return strdup( painCave.errMsg );
489 }
490 jx = atof( foo );
491
492 foo = strtok(NULL, " ,;\t");
493 if(foo == NULL){
494 sprintf( painCave.errMsg,
495 "error in reading angular momentum jy from %s\n"
496 "natoms = %d, index = %d\n",
497 c_in_name, n_atoms, atomIndex );
498 return strdup( painCave.errMsg );
499 }
500 jy = atof(foo );
501
502 foo = strtok(NULL, " ,;\t");
503 if(foo == NULL){
504 sprintf( painCave.errMsg,
505 "error in reading angular momentum jz from %s\n"
506 "natoms = %d, index = %d\n",
507 c_in_name, n_atoms, atomIndex );
508 return strdup( painCave.errMsg );
509 }
510 jz = atof( foo );
511
512 dAtom = ( DirectionalAtom* )atoms[atomIndex];
513
514 // check that the quaternion vector is normalized
515
516 qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
517
518 qLength = sqrt( qSqr );
519 q[0] = q[0] / qLength;
520 q[1] = q[1] / qLength;
521 q[2] = q[2] / qLength;
522 q[3] = q[3] / qLength;
523
524 dAtom->setQ( q );
525
526 // add the angular velocities
527
528 dAtom->setJx( jx );
529 dAtom->setJy( jy );
530 dAtom->setJz( jz );
531 }
532
533 // add the positions and velocities to the atom
534
535 atoms[atomIndex]->setPos( pos );
536 atoms[atomIndex]->setVel( vel );
537
538 return NULL;
539 }
540
541
542 char* InitializeFromFile::parseBoxLine(char* readLine, double boxMat[9],
543 double &time ){
544
545 char *foo; // the pointer to the current string token
546 int j;
547
548 // set the string tokenizer
549
550 foo = strtok(readLine, " ,;\t");
551 // set the timeToken.
552
553 if(foo == NULL){
554 sprintf( painCave.errMsg,
555 "error in reading Time from %s\n",
556 c_in_name );
557 return strdup( painCave.errMsg );
558 }
559 time = atof( foo );
560
561 // get the Hx vector
562
563 foo = strtok(NULL, " ,;\t");
564 if(foo == NULL){
565 sprintf( painCave.errMsg,
566 "error in reading Hx[0] from %s\n",
567 c_in_name );
568 return strdup( painCave.errMsg );
569 }
570 boxMat[0] = atof( foo );
571
572 foo = strtok(NULL, " ,;\t");
573 if(foo == NULL){
574 sprintf( painCave.errMsg,
575 "error in reading Hx[1] from %s\n",
576 c_in_name );
577 return strdup( painCave.errMsg );
578 }
579 boxMat[1] = atof( foo );
580
581 foo = strtok(NULL, " ,;\t");
582 if(foo == NULL){
583 sprintf( painCave.errMsg,
584 "error in reading Hx[2] from %s\n",
585 c_in_name );
586 return strdup( painCave.errMsg );
587 }
588 boxMat[2] = atof( foo );
589
590 // get the Hy vector
591
592 foo = strtok(NULL, " ,;\t");
593 if(foo == NULL){
594 sprintf( painCave.errMsg,
595 "error in reading Hy[0] from %s\n",
596 c_in_name );
597 return strdup( painCave.errMsg );
598 }
599 boxMat[3] = atof( foo );
600
601 foo = strtok(NULL, " ,;\t");
602 if(foo == NULL){
603 sprintf( painCave.errMsg,
604 "error in reading Hy[1] from %s\n",
605 c_in_name );
606 return strdup( painCave.errMsg );
607 }
608 boxMat[4] = atof( foo );
609
610 foo = strtok(NULL, " ,;\t");
611 if(foo == NULL){
612 sprintf( painCave.errMsg,
613 "error in reading Hy[2] from %s\n",
614 c_in_name );
615 return strdup( painCave.errMsg );
616 }
617 boxMat[5] = atof( foo );
618
619 // get the Hz vector
620
621 foo = strtok(NULL, " ,;\t");
622 if(foo == NULL){
623 sprintf( painCave.errMsg,
624 "error in reading Hz[0] from %s\n",
625 c_in_name );
626 return strdup( painCave.errMsg );
627 }
628 boxMat[6] = atof( foo );
629
630 foo = strtok(NULL, " ,;\t");
631 if(foo == NULL){
632 sprintf( painCave.errMsg,
633 "error in reading Hz[1] from %s\n",
634 c_in_name );
635 return strdup( painCave.errMsg );
636 }
637 boxMat[7] = atof( foo );
638
639 foo = strtok(NULL, " ,;\t");
640 if(foo == NULL){
641 sprintf( painCave.errMsg,
642 "error in reading Hz[2] from %s\n",
643 c_in_name );
644 return strdup( painCave.errMsg );
645 }
646 boxMat[8] = atof( foo );
647
648 return NULL;
649 }
650
651
652 #ifdef IS_MPI
653
654 // a couple of functions to let us escape the read loop
655
656 void initFile::nodeZeroError( void ){
657 int j, myStatus;
658
659 myStatus = 0;
660 for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
661 MPI_Send( &myStatus, 1, MPI_INT, j,
662 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
663 }
664
665
666 MPI_Finalize();
667 exit (0);
668
669 }
670
671 void initFile::anonymousNodeDie( void ){
672
673 MPI_Finalize();
674 exit (0);
675 }
676
677 #endif //is_mpi