ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/InitializeFromFile.cpp
Revision: 590
Committed: Thu Jul 10 22:15:53 2003 UTC (21 years ago) by mmeineke
File size: 16258 byte(s)
Log Message:
fixed some bugs

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