--- trunk/OOPSE/libmdtools/InitializeFromFile.cpp 2003/10/28 22:25:46 836 +++ trunk/OOPSE/libmdtools/InitializeFromFile.cpp 2003/10/29 00:19:10 837 @@ -11,6 +11,7 @@ #include "ReadWrite.hpp" #include "simError.h" +#include "GenericData.hpp" #ifdef IS_MPI #include @@ -40,14 +41,14 @@ InitializeFromFile::InitializeFromFile( char *in_name painCave.isFatal = 1; simError(); } - + strcpy( c_in_name, in_name); #ifdef IS_MPI } strcpy( checkPointMsg, "Infile opened for reading successfully." ); MPIcheckPoint(); #endif - return; + return; } InitializeFromFile::~InitializeFromFile( ){ @@ -74,22 +75,18 @@ void InitializeFromFile :: readInit( SimInfo* the_simn void InitializeFromFile :: readInit( SimInfo* the_simnfo ){ int i, j; - + #ifdef IS_MPI int done, which_node, which_atom; // loop counter #endif //is_mpi const int BUFFERSIZE = 2000; // size of the read buffer int n_atoms; // the number of atoms - char read_buffer[BUFFERSIZE]; //the line buffer for reading + char read_buffer[BUFFERSIZE]; //the line buffer for reading - char *eof_test; // ptr to see when we reach the end of the file + char *eof_test; // ptr to see when we reach the end of the file char *parseErr; - double currTime; - double boxMat[9]; - double theBoxMat3[3][3]; - simnfo = the_simnfo; @@ -113,9 +110,9 @@ void InitializeFromFile :: readInit( SimInfo* the_simn painCave.isFatal = 1; simError(); } - - //read the box mat from the comment line - + + //read the box mat from the comment line + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if(eof_test == NULL){ sprintf( painCave.errMsg, @@ -124,22 +121,19 @@ void InitializeFromFile :: readInit( SimInfo* the_simn simError(); } - parseErr = parseBoxLine( read_buffer, boxMat, currTime ); + + + parseErr = parseCommentLine( read_buffer, simnfo); if( parseErr != NULL ){ strcpy( painCave.errMsg, parseErr ); painCave.isFatal = 1; simError(); } - for(i=0;i<3;i++) - for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i]; + //parse dump lines - simnfo->setBoxM( theBoxMat3 ); - simnfo->setTime( currTime ); - - for( i=0; i < n_atoms; i++){ - + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if(eof_test == NULL){ sprintf(painCave.errMsg, @@ -151,7 +145,7 @@ void InitializeFromFile :: readInit( SimInfo* the_simn simError(); } - + parseErr = parseDumpLine( read_buffer, i ); if( parseErr != NULL ){ strcpy( painCave.errMsg, parseErr ); @@ -169,11 +163,11 @@ void InitializeFromFile :: readInit( SimInfo* the_simn int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone int haveError; - + MPI_Status istatus; int *AtomToProcMap = mpiSim->getAtomToProcMap(); - + haveError = 0; if (worldRank == 0) { @@ -184,12 +178,12 @@ void InitializeFromFile :: readInit( SimInfo* the_simn haveError = 1; simError(); } - + n_atoms = atoi( read_buffer ); - + // Check to see that the number of atoms in the intial configuration file is the // same as declared in simBass. - + if( n_atoms != mpiSim->getTotAtoms() ){ sprintf( painCave.errMsg, "Initialize from File error. %s n_atoms, %d, " @@ -198,9 +192,9 @@ void InitializeFromFile :: readInit( SimInfo* the_simn haveError= 1; simError(); } - - //read the boxMat from the comment line - + + //read the boxMat from the comment line + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if(eof_test == NULL){ sprintf( painCave.errMsg, @@ -208,21 +202,24 @@ void InitializeFromFile :: readInit( SimInfo* the_simn haveError = 1; simError(); } - - parseErr = parseBoxLine( read_buffer, boxMat, currTime ); + + //Every single processor will parse the comment line by itself + //By using this way, we might lose some efficiency, but if we want to add + //more parameters into comment line, we only need to modify function + //parseCommentLine + + MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); + + parseErr = parseCommentLine( read_buffer, simnfo); + if( parseErr != NULL ){ strcpy( painCave.errMsg, parseErr ); haveError = 1; simError(); } - MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD ); - MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD ); - - if(haveError) nodeZeroError(); - for (i=0 ; i < mpiSim->getTotAtoms(); i++) { - + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if(eof_test == NULL){ sprintf(painCave.errMsg, @@ -233,62 +230,70 @@ void InitializeFromFile :: readInit( SimInfo* the_simn haveError= 1; simError(); } - + if(haveError) nodeZeroError(); // Get the Node number which wants this atom: - which_node = AtomToProcMap[i]; + which_node = AtomToProcMap[i]; if (which_node == 0) { parseErr = parseDumpLine( read_buffer, i ); if( parseErr != NULL ){ strcpy( painCave.errMsg, parseErr ); haveError = 1; simError(); - } + } if(haveError) nodeZeroError(); - } - + } + else { - + myStatus = 1; - MPI_Send(&myStatus, 1, MPI_INT, which_node, + MPI_Send(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, MPI_COMM_WORLD); - MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, + MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, MPI_COMM_WORLD); - MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, + MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); - + if(!myStatus) nodeZeroError(); } } myStatus = -1; - for (j = 0; j < mpiSim->getNumberProcessors(); j++) { - MPI_Send( &myStatus, 1, MPI_INT, j, + for (j = 0; j < mpiSim->getNumberProcessors(); j++) { + MPI_Send( &myStatus, 1, MPI_INT, j, TAKE_THIS_TAG_INT, MPI_COMM_WORLD); } - + } else { - - MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD); - MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); + + MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); + + parseErr = parseCommentLine( read_buffer, simnfo); + + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); + haveError = 1; + simError(); + } + done = 0; while (!done) { - MPI_Recv(&myStatus, 1, MPI_INT, 0, + MPI_Recv(&myStatus, 1, MPI_INT, 0, TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); - + if(!myStatus) anonymousNodeDie(); - + if(myStatus < 0) break; - MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, + MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus); - MPI_Recv(&which_atom, 1, MPI_INT, 0, + MPI_Recv(&which_atom, 1, MPI_INT, 0, TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); - + myStatus = 1; parseErr = parseDumpLine( read_buffer, which_atom ); if( parseErr != NULL ){ @@ -296,46 +301,41 @@ void InitializeFromFile :: readInit( SimInfo* the_simn myStatus = 0;; simError(); } - - MPI_Send( &myStatus, 1, MPI_INT, 0, + + MPI_Send( &myStatus, 1, MPI_INT, 0, TAKE_THIS_TAG_INT, MPI_COMM_WORLD); - + } } - + // last thing last, enable fatalities. painCave.isEventLoop = 0; - for(i=0;i<3;i++) - for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i]; - simnfo->setBoxM( theBoxMat3 ); - simnfo->setTime( currTime ); - #endif } char* InitializeFromFile::parseDumpLine(char* readLine, int globalIndex){ - char *foo; // the pointer to the current string token - + char *foo; // the pointer to the current string token + double pos[3]; // position place holders double vel[3]; // velocity placeholders double q[4]; // the quaternions double jx, jy, jz; // angular velocity placeholders; double qSqr, qLength; // needed to normalize the quaternion vector. - + Atom **atoms = simnfo->atoms; DirectionalAtom* dAtom; - + int n_atoms, atomIndex; #ifdef IS_MPI int j; n_atoms = mpiSim->getTotAtoms(); - atomIndex=-1; + atomIndex=-1; for (j=0; j < mpiSim->getMyNlocal(); j++) { if (atoms[j]->getGlobalIndex() == globalIndex) atomIndex = j; } @@ -345,18 +345,18 @@ char* InitializeFromFile::parseDumpLine(char* readLine "in file %s does not exist on processor %d .\n", globalIndex, c_in_name, mpiSim->getMyNode() ); return strdup( painCave.errMsg ); - } + } #else n_atoms = simnfo->n_atoms; atomIndex = globalIndex; #endif // is_mpi // set the string tokenizer - + foo = strtok(readLine, " ,;\t"); - + // check the atom name to the current atom - + if( strcmp( foo, atoms[atomIndex]->getType() ) ){ sprintf( painCave.errMsg, "Initialize from file error. Atom %s at index %d " @@ -365,7 +365,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine foo, atomIndex, c_in_name, atoms[atomIndex]->getType() ); return strdup( painCave.errMsg ); } - + // get the positions foo = strtok(NULL, " ,;\t"); @@ -377,7 +377,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } pos[0] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -387,7 +387,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } pos[1] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -396,7 +396,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine c_in_name, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - pos[2] = atof( foo ); + pos[2] = atof( foo ); // get the velocities @@ -410,7 +410,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } vel[0] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -420,7 +420,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } vel[1] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -430,12 +430,12 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } vel[2] = atof( foo ); - - + + // get the quaternions - + if( atoms[atomIndex]->isDirectional() ){ - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf(painCave.errMsg, @@ -445,7 +445,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } q[0] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -455,7 +455,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } q[1] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -465,7 +465,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } q[2] = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -475,9 +475,9 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } q[3] = atof( foo ); - + // get the angular velocities - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -487,7 +487,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } jx = atof( foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -497,7 +497,7 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } jy = atof(foo ); - + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, @@ -507,30 +507,30 @@ char* InitializeFromFile::parseDumpLine(char* readLine return strdup( painCave.errMsg ); } jz = atof( foo ); - + dAtom = ( DirectionalAtom* )atoms[atomIndex]; // check that the quaternion vector is normalized qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); - + qLength = sqrt( qSqr ); q[0] = q[0] / qLength; q[1] = q[1] / qLength; q[2] = q[2] / qLength; q[3] = q[3] / qLength; - + dAtom->setQ( q ); - + // add the angular velocities dAtom->setJx( jx ); dAtom->setJy( jy ); dAtom->setJz( jz ); } - + // add the positions and velocities to the atom - + atoms[atomIndex]->setPos( pos ); atoms[atomIndex]->setVel( vel ); @@ -538,13 +538,19 @@ char* InitializeFromFile::parseBoxLine(char* readLine, } -char* InitializeFromFile::parseBoxLine(char* readLine, double boxMat[9], - double &time ){ +char* InitializeFromFile::parseCommentLine(char* readLine, SimInfo* entry_plug){ - char *foo; // the pointer to the current string token + double currTime; + double boxMat[9]; + double theBoxMat3[3][3]; + double chi; + double integralOfChidt; + double eta[9]; + char *foo; // the pointer to the current string token + // set the string tokenizer - + foo = strtok(readLine, " ,;\t"); // set the timeToken. @@ -554,116 +560,97 @@ char* InitializeFromFile::parseBoxLine(char* readLine, c_in_name ); return strdup( painCave.errMsg ); } - time = atof( foo ); - // get the Hx vector + currTime = atof( foo ); + entry_plug->setTime( currTime ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hx[0] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); + //get H-Matrix + + for(int i = 0 ; i < 9; i++){ + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading H[%d] from %s\n", i, c_in_name ); + return strdup( painCave.errMsg ); + } + boxMat[i] = atof( foo ); } - boxMat[0] = atof( foo ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hx[1] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); - } - boxMat[1] = atof( foo ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hx[2] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); - } - boxMat[2] = atof( foo ); - // get the Hy vector + for(int i=0;i<3;i++) + for(int j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i]; - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hy[0] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); - } - boxMat[3] = atof( foo ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hy[1] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); - } - boxMat[4] = atof( foo ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hy[2] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); - } - boxMat[5] = atof( foo ); + //set H-Matrix + entry_plug->setBoxM( theBoxMat3 ); - // get the Hz vector + //get chi and integralOfChidt, they should appear by pair + foo = strtok(NULL, " ,;\t\n"); + if(foo != NULL){ + chi = atof(foo); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hz[0] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); + foo = strtok(NULL, " ,;\t\n"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "chi and integralOfChidt should appear by pair in %s\n", c_in_name ); + return strdup( painCave.errMsg ); + } + integralOfChidt = atof( foo ); + + //push chi and integralOfChidt into SimInfo::properties which can be + //retrieved by integrator later + DoubleData* chiValue = new DoubleData(); + chiValue->setID(CHIVALUE_ID); + chiValue->setData(chi); + entry_plug->addProperty(chiValue); + + DoubleData* integralOfChidtValue = new DoubleData(); + integralOfChidtValue->setID(INTEGRALOFCHIDT_ID); + integralOfChidtValue->setData(integralOfChidt); + entry_plug->addProperty(integralOfChidtValue); + } - boxMat[6] = atof( foo ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hz[1] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); + else + return NULL; + + //get eta + for(int i = 0 ; i < 9; i++){ + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading eta[%d] from %s\n", i, c_in_name ); + return strdup( painCave.errMsg ); + } + eta[i] = atof( foo ); } - boxMat[7] = atof( foo ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Hz[2] from %s\n", - c_in_name ); - return strdup( painCave.errMsg ); - } - boxMat[8] = atof( foo ); + //push eta into SimInfo::properties which can be + //retrieved by integrator later + //entry_plug->setBoxM( theBoxMat3 ); + DoubleArrayData* etaValue = new DoubleArrayData(); + etaValue->setID(ETAVALUE_ID); + etaValue->setData(eta, 9); + entry_plug->addProperty(etaValue); + + return NULL; } - #ifdef IS_MPI // a couple of functions to let us escape the read loop void initFile::nodeZeroError( void ){ int j, myStatus; - + myStatus = 0; - for (j = 0; j < mpiSim->getNumberProcessors(); j++) { - MPI_Send( &myStatus, 1, MPI_INT, j, + for (j = 0; j < mpiSim->getNumberProcessors(); j++) { + MPI_Send( &myStatus, 1, MPI_INT, j, TAKE_THIS_TAG_INT, MPI_COMM_WORLD); - } - + } + MPI_Finalize(); exit (0); - + } void initFile::anonymousNodeDie( void ){