--- trunk/src/brains/SimCreator.cpp 2005/12/30 15:32:55 832 +++ trunk/src/brains/SimCreator.cpp 2006/09/25 22:08:33 1051 @@ -46,7 +46,7 @@ * @time 13:51am * @version 1.0 */ - +#include #include #include #include @@ -81,112 +81,253 @@ namespace oopse { namespace oopse { -Globals* SimCreator::parseFile(const std::string mdFileName){ - Globals* simParams = NULL; - try { + Globals* SimCreator::parseFile(std::istream& rawMetaDataStream, const std::string& filename, int startOfMetaDataBlock ){ + Globals* simParams = NULL; + try { - // Create a preprocessor that preprocesses md file into an ostringstream - std::stringstream ppStream; + // Create a preprocessor that preprocesses md file into an ostringstream + std::stringstream ppStream; #ifdef IS_MPI - int streamSize; - const int masterNode = 0; - int commStatus; - if (worldRank == masterNode) { + int streamSize; + const int masterNode = 0; + int commStatus; + if (worldRank == masterNode) { #endif - SimplePreprocessor preprocessor; - preprocessor.preprocess(mdFileName, ppStream); + SimplePreprocessor preprocessor; + preprocessor.preprocess(rawMetaDataStream, filename, startOfMetaDataBlock, ppStream); #ifdef IS_MPI - //brocasting the stream size - streamSize = ppStream.str().size() +1; - commStatus = MPI_Bcast(&streamSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); + //brocasting the stream size + streamSize = ppStream.str().size() +1; + commStatus = MPI_Bcast(&streamSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); - commStatus = MPI_Bcast(static_cast(const_cast(ppStream.str().c_str())), streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + commStatus = MPI_Bcast(static_cast(const_cast(ppStream.str().c_str())), streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); - } else { - //get stream size - commStatus = MPI_Bcast(&streamSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); + } else { + //get stream size + commStatus = MPI_Bcast(&streamSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); - char* buf = new char[streamSize]; - assert(buf); + char* buf = new char[streamSize]; + assert(buf); - //receive file content - commStatus = MPI_Bcast(buf, streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + //receive file content + commStatus = MPI_Bcast(buf, streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); - ppStream.str(buf); - delete buf; + ppStream.str(buf); + delete buf; - } + } #endif - // Create a scanner that reads from the input stream - MDLexer lexer(ppStream); - lexer.setFilename(mdFileName); - lexer.initDeferredLineCount(); + // Create a scanner that reads from the input stream + MDLexer lexer(ppStream); + lexer.setFilename(filename); + lexer.initDeferredLineCount(); - // Create a parser that reads from the scanner - MDParser parser(lexer); - parser.setFilename(mdFileName); + // Create a parser that reads from the scanner + MDParser parser(lexer); + parser.setFilename(filename); - // Create an observer that synchorizes file name change - FilenameObserver observer; - observer.setLexer(&lexer); - observer.setParser(&parser); - lexer.setObserver(&observer); + // Create an observer that synchorizes file name change + FilenameObserver observer; + observer.setLexer(&lexer); + observer.setParser(&parser); + lexer.setObserver(&observer); - antlr::ASTFactory factory; - parser.initializeASTFactory(factory); - parser.setASTFactory(&factory); - parser.mdfile(); + antlr::ASTFactory factory; + parser.initializeASTFactory(factory); + parser.setASTFactory(&factory); + parser.mdfile(); - // Create a tree parser that reads information into Globals - MDTreeParser treeParser; - treeParser.initializeASTFactory(factory); - treeParser.setASTFactory(&factory); - simParams = treeParser.walkTree(parser.getAST()); + // Create a tree parser that reads information into Globals + MDTreeParser treeParser; + treeParser.initializeASTFactory(factory); + treeParser.setASTFactory(&factory); + simParams = treeParser.walkTree(parser.getAST()); - } + } + - catch(antlr::MismatchedCharException& e) { - cerr<< "parser exception: " << e.getMessage() << " " << e.getFilename() << ":" << e.getLine() << " " << e.getColumn() << endl; - } - catch(antlr::MismatchedTokenException &e) { - cerr<< "parser exception: " << e.getMessage() << " " << e.getFilename() << ":" << e.getLine() << " " << e.getColumn() << endl; - } - catch(antlr::NoViableAltForCharException &e) { - cerr<< "parser exception: " << e.getMessage() << " " << e.getFilename() << ":" << e.getLine() << " " << e.getColumn() << endl; - } - catch(antlr::NoViableAltException &e) { - cerr<< "parser exception: " << e.getMessage() << " " << e.getFilename() << ":" << e.getLine() << " " << e.getColumn() << endl; - } - catch(antlr::TokenStreamRecognitionException& e) { - cerr<< "parser exception: " << e.getMessage() << " " << e.getFilename() << ":" << e.getLine() << " " << e.getColumn() << endl; - } - catch(antlr::TokenStreamIOException& e) { - cerr<< "parser exception: " << e.getMessage() << endl; - } - catch(antlr::TokenStreamException& e) { - cerr<< "parser exception: " << e.getMessage() << endl; - } - catch (antlr::RecognitionException& e) { - cerr<< "parser exception: " << e.getMessage() << " " << e.getFilename() << ":" << e.getLine() << " " << e.getColumn() << endl; - } - catch (antlr::CharStreamException& e) { - cerr << "parser exception: " << e.getMessage() << endl; - } - catch (exception& e) { - cerr << "parser exception: " << e.what() << endl; - } + catch(antlr::MismatchedCharException& e) { + sprintf(painCave.errMsg, + "parser exception: %s %s:%d:%d\n", + e.getMessage().c_str(),e.getFilename().c_str(), e.getLine(), e.getColumn()); + painCave.isFatal = 1; + simError(); + } + catch(antlr::MismatchedTokenException &e) { + sprintf(painCave.errMsg, + "parser exception: %s %s:%d:%d\n", + e.getMessage().c_str(),e.getFilename().c_str(), e.getLine(), e.getColumn()); + painCave.isFatal = 1; + simError(); + } + catch(antlr::NoViableAltForCharException &e) { + sprintf(painCave.errMsg, + "parser exception: %s %s:%d:%d\n", + e.getMessage().c_str(),e.getFilename().c_str(), e.getLine(), e.getColumn()); + painCave.isFatal = 1; + simError(); + } + catch(antlr::NoViableAltException &e) { + sprintf(painCave.errMsg, + "parser exception: %s %s:%d:%d\n", + e.getMessage().c_str(),e.getFilename().c_str(), e.getLine(), e.getColumn()); + painCave.isFatal = 1; + simError(); + } + + catch(antlr::TokenStreamRecognitionException& e) { + sprintf(painCave.errMsg, + "parser exception: %s %s:%d:%d\n", + e.getMessage().c_str(),e.getFilename().c_str(), e.getLine(), e.getColumn()); + painCave.isFatal = 1; + simError(); + } + + catch(antlr::TokenStreamIOException& e) { + sprintf(painCave.errMsg, + "parser exception: %s\n", + e.getMessage().c_str()); + painCave.isFatal = 1; + simError(); + } + + catch(antlr::TokenStreamException& e) { + sprintf(painCave.errMsg, + "parser exception: %s\n", + e.getMessage().c_str()); + painCave.isFatal = 1; + simError(); + } + catch (antlr::RecognitionException& e) { + sprintf(painCave.errMsg, + "parser exception: %s %s:%d:%d\n", + e.getMessage().c_str(),e.getFilename().c_str(), e.getLine(), e.getColumn()); + painCave.isFatal = 1; + simError(); + } + catch (antlr::CharStreamException& e) { + sprintf(painCave.errMsg, + "parser exception: %s\n", + e.getMessage().c_str()); + painCave.isFatal = 1; + simError(); + } + catch (OOPSEException& e) { + sprintf(painCave.errMsg, + "%s\n", + e.getMessage().c_str()); + painCave.isFatal = 1; + simError(); + } + catch (std::exception& e) { + sprintf(painCave.errMsg, + "parser exception: %s\n", + e.what()); + painCave.isFatal = 1; + simError(); + } - return simParams; + return simParams; } SimInfo* SimCreator::createSim(const std::string & mdFileName, bool loadInitCoords) { + const int bufferSize = 65535; + char buffer[bufferSize]; + int lineNo = 0; + std::string mdRawData; + int metaDataBlockStart = -1; + int metaDataBlockEnd = -1; + int i; + int mdOffset; + +#ifdef IS_MPI + const int masterNode = 0; + if (worldRank == masterNode) { +#endif + + std::ifstream mdFile_(mdFileName.c_str()); + + if (mdFile_.fail()) { + sprintf(painCave.errMsg, + "SimCreator: Cannot open file: %s\n", + mdFileName.c_str()); + painCave.isFatal = 1; + simError(); + } + + mdFile_.getline(buffer, bufferSize); + ++lineNo; + std::string line = trimLeftCopy(buffer); + i = CaseInsensitiveFind(line, ""); + if (i != string::npos) { + metaDataBlockStart = lineNo; + mdOffset = mdFile_.tellg(); + } + } else { + i = CaseInsensitiveFind(line, ""); + if (i != string::npos) { + metaDataBlockEnd = lineNo; + } + } + } + + if (metaDataBlockStart == -1) { + sprintf(painCave.errMsg, + "SimCreator: File: %s did not contain a tag!\n", + mdFileName.c_str()); + painCave.isFatal = 1; + simError(); + } + if (metaDataBlockEnd == -1) { + sprintf(painCave.errMsg, + "SimCreator: File: %s did not contain a closed MetaData block!\n", + mdFileName.c_str()); + painCave.isFatal = 1; + simError(); + } + + mdFile_.clear(); + mdFile_.seekg(0); + mdFile_.seekg(mdOffset); + + mdRawData.clear(); + + for (int i = 0; i < metaDataBlockEnd - metaDataBlockStart - 1; ++i) { + mdFile_.getline(buffer, bufferSize); + mdRawData += buffer; + mdRawData += "\n"; + } + + mdFile_.close(); + +#ifdef IS_MPI + } +#endif + + std::stringstream rawMetaDataStream(mdRawData); + //parse meta-data file - Globals* simParams = parseFile(mdFileName); + Globals* simParams = parseFile(rawMetaDataStream, mdFileName, metaDataBlockStart+1); //create the force field ForceField * ff = ForceFieldFactory::getInstance() @@ -201,6 +342,7 @@ Globals* SimCreator::parseFile(const std::string mdFil } if (simParams->haveForceFieldFileName()) { + std::cout<< simParams->getForceFieldFileName() << "\n"; ff->setForceFieldFileName(simParams->getForceFieldFileName()); } @@ -227,8 +369,11 @@ Globals* SimCreator::parseFile(const std::string mdFil ff->setFortranForceOptions(); //create SimInfo SimInfo * info = new SimInfo(ff, simParams); + + info->setRawMetaData(mdRawData); - //gather parameters (SimCreator only retrieves part of the parameters) + //gather parameters (SimCreator only retrieves part of the + //parameters) gatherParameters(info, mdFileName); //divide the molecules and determine the global index of molecules @@ -240,18 +385,23 @@ Globals* SimCreator::parseFile(const std::string mdFil createMolecules(info); - //allocate memory for DataStorage(circular reference, need to break it) + //allocate memory for DataStorage(circular reference, need to + //break it) info->setSnapshotManager(new SimSnapshotManager(info)); - //set the global index of atoms, rigidbodies and cutoffgroups (only need to be set once, the - //global index will never change again). Local indices of atoms and rigidbodies are already set by - //MoleculeCreator class which actually delegates the responsibility to LocalIndexManager. + //set the global index of atoms, rigidbodies and cutoffgroups + //(only need to be set once, the global index will never change + //again). Local indices of atoms and rigidbodies are already set + //by MoleculeCreator class which actually delegates the + //responsibility to LocalIndexManager. setGlobalIndex(info); - //Alought addExculdePairs is called inside SimInfo's addMolecule method, at that point - //atoms don't have the global index yet (their global index are all initialized to -1). - //Therefore we have to call addExcludePairs explicitly here. A way to work around is that - //we can determine the beginning global indices of atoms before they get created. + //Although addExcludePairs is called inside SimInfo's addMolecule + //method, at that point atoms don't have the global index yet + //(their global index are all initialized to -1). Therefore we + //have to call addExcludePairs explicitly here. A way to work + //around is that we can determine the beginning global indices of + //atoms before they get created. SimInfo::MoleculeIterator mi; Molecule* mol; for (mol= info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { @@ -259,7 +409,7 @@ Globals* SimCreator::parseFile(const std::string mdFil } if (loadInitCoords) - loadCoordinates(info); + loadCoordinates(info, mdFileName); return info; } @@ -295,12 +445,12 @@ Globals* SimCreator::parseFile(const std::string mdFil #ifdef IS_MPI void SimCreator::divideMolecules(SimInfo *info) { - double numerator; - double denominator; - double precast; - double x; - double y; - double a; + RealType numerator; + RealType denominator; + RealType precast; + RealType x; + RealType y; + RealType a; int old_atoms; int add_atoms; int new_atoms; @@ -412,7 +562,7 @@ Globals* SimCreator::parseFile(const std::string mdFil // Pacc(x) = exp(- a * x) // where a = penalty / (average atoms per molecule) - x = (double)(new_atoms - nTarget); + x = (RealType)(new_atoms - nTarget); y = myRandom->rand(); if (y < exp(- a * x)) { @@ -478,6 +628,7 @@ Globals* SimCreator::parseFile(const std::string mdFil Molecule::AtomIterator ai; Molecule::RigidBodyIterator ri; Molecule::CutoffGroupIterator ci; + Molecule::IntegrableObjectIterator ioi; Molecule * mol; Atom * atom; RigidBody * rb; @@ -486,7 +637,8 @@ Globals* SimCreator::parseFile(const std::string mdFil int beginRigidBodyIndex; int beginCutoffGroupIndex; int nGlobalAtoms = info->getNGlobalAtoms(); - + + /**@todo fixme */ #ifndef IS_MPI beginAtomIndex = 0; @@ -601,21 +753,53 @@ Globals* SimCreator::parseFile(const std::string mdFil #else info->setGlobalMolMembership(globalMolMembership); #endif + + // nIOPerMol holds the number of integrable objects per molecule + // here the molecules are listed by their global indices. + + std::vector nIOPerMol(info->getNGlobalMolecules(), 0); + for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { + nIOPerMol[mol->getGlobalIndex()] = mol->getNIntegrableObjects(); + } +#ifdef IS_MPI + std::vector numIntegrableObjectsPerMol(info->getNGlobalMolecules(), 0); + MPI_Allreduce(&nIOPerMol[0], &numIntegrableObjectsPerMol[0], + info->getNGlobalMolecules(), MPI_INT, MPI_SUM, MPI_COMM_WORLD); +#else + std::vector numIntegrableObjectsPerMol = nIOPerMol; +#endif + + std::vector startingIOIndexForMol(info->getNGlobalMolecules()); + +int startingIndex = 0; + for (int i = 0; i < info->getNGlobalMolecules(); i++) { + startingIOIndexForMol[i] = startingIndex; + startingIndex += numIntegrableObjectsPerMol[i]; + } + + std::vector IOIndexToIntegrableObject(info->getNGlobalIntegrableObjects(), (StuntDouble*)NULL); + for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) { + int myGlobalIndex = mol->getGlobalIndex(); + int globalIO = startingIOIndexForMol[myGlobalIndex]; + for (StuntDouble* integrableObject = mol->beginIntegrableObject(ioi); integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(ioi)) { + integrableObject->setGlobalIntegrableObjectIndex(globalIO); + IOIndexToIntegrableObject[globalIO] = integrableObject; + globalIO++; + } + } + + info->setIOIndexToIntegrableObject(IOIndexToIntegrableObject); + } - void SimCreator::loadCoordinates(SimInfo* info) { + void SimCreator::loadCoordinates(SimInfo* info, const std::string& mdFileName) { Globals* simParams; simParams = info->getSimParams(); - if (!simParams->haveInitialConfig()) { - sprintf(painCave.errMsg, - "Cannot intialize a simulation without an initial configuration file.\n"); - painCave.isFatal = 1;; - simError(); - } - DumpReader reader(info, simParams->getInitialConfig()); + DumpReader reader(info, mdFileName); int nframes = reader.getNFrames(); if (nframes > 0) { @@ -624,7 +808,7 @@ Globals* SimCreator::parseFile(const std::string mdFil //invalid initial coordinate file sprintf(painCave.errMsg, "Initial configuration file %s should at least contain one frame\n", - simParams->getInitialConfig().c_str()); + mdFileName.c_str()); painCave.isFatal = 1; simError(); }