--- trunk/src/brains/SimCreator.cpp 2005/03/11 17:50:11 436 +++ trunk/src/brains/SimCreator.cpp 2008/07/14 12:35:58 1277 @@ -46,72 +46,295 @@ * @time 13:51am * @version 1.0 */ +#include +#include +#include +#include #include "brains/MoleculeCreator.hpp" #include "brains/SimCreator.hpp" #include "brains/SimSnapshotManager.hpp" #include "io/DumpReader.hpp" -#include "io/parse_me.h" #include "UseTheForce/ForceFieldFactory.hpp" #include "utils/simError.h" #include "utils/StringUtils.hpp" #include "math/SeqRandNumGen.hpp" +#include "mdParser/MDLexer.hpp" +#include "mdParser/MDParser.hpp" +#include "mdParser/MDTreeParser.hpp" +#include "mdParser/SimplePreprocessor.hpp" +#include "antlr/ANTLRException.hpp" +#include "antlr/TokenStreamRecognitionException.hpp" +#include "antlr/TokenStreamIOException.hpp" +#include "antlr/TokenStreamException.hpp" +#include "antlr/RecognitionException.hpp" +#include "antlr/CharStreamException.hpp" + +#include "antlr/MismatchedCharException.hpp" +#include "antlr/MismatchedTokenException.hpp" +#include "antlr/NoViableAltForCharException.hpp" +#include "antlr/NoViableAltException.hpp" + #ifdef IS_MPI -#include "io/mpiBASS.h" #include "math/ParallelRandNumGen.hpp" #endif namespace oopse { - void SimCreator::parseFile(const std::string mdFileName, MakeStamps* stamps, - Globals* simParams){ + 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; +#ifdef IS_MPI + int streamSize; + const int masterNode = 0; + int commStatus; + if (worldRank == masterNode) { +#endif + + 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); + + 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); + + char* buf = new char[streamSize]; + assert(buf); + + //receive file content + commStatus = MPI_Bcast(buf, streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + + ppStream.str(buf); + delete buf; + + } +#endif + // Create a scanner that reads from the input stream + MDLexer lexer(ppStream); + lexer.setFilename(filename); + lexer.initDeferredLineCount(); -#ifdef IS_MPI + // 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); - if (worldRank == 0) { -#endif // is_mpi + 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()); + } + - simParams->initalize(); - set_interface_stamps(stamps, simParams); + 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(); + } -#ifdef IS_MPI - - mpiEventInit(); - -#endif - - yacc_BASS(mdFileName.c_str()); - -#ifdef IS_MPI - - throwMPIEvent(NULL); - } else { - set_interface_stamps(stamps, simParams); - mpiEventInit(); - MPIcheckPoint(); - mpiEventLoop(); + 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(); } - -#endif - + + 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; } - SimInfo* SimCreator::createSim(const std::string & mdFileName, bool loadInitCoords) { - - MakeStamps * stamps = new MakeStamps(); - - Globals * simParams = new Globals(); - + 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 - parseFile(mdFileName, stamps, simParams); + Globals* simParams = parseFile(rawMetaDataStream, mdFileName, metaDataBlockStart+1); //create the force field - ForceField * ff = ForceFieldFactory::getInstance()->createForceField( - simParams->getForceField()); - + ForceField * ff = ForceFieldFactory::getInstance()->createForceField(simParams->getForceField()); + if (ff == NULL) { - sprintf(painCave.errMsg, "ForceField Factory can not create %s force field\n", - simParams->getForceField()); + sprintf(painCave.errMsg, + "ForceField Factory can not create %s force field\n", + simParams->getForceField().c_str()); painCave.isFatal = 1; simError(); } @@ -140,15 +363,14 @@ namespace oopse { } ff->parse(forcefieldFileName); - - //extract the molecule stamps - std::vector < std::pair > moleculeStampPairs; - compList(stamps, simParams, moleculeStampPairs); - + ff->setFortranForceOptions(); //create SimInfo - SimInfo * info = new SimInfo(moleculeStampPairs, ff, simParams); - - //gather parameters (SimCreator only retrieves part of the parameters) + SimInfo * info = new SimInfo(ff, simParams); + + info->setRawMetaData(mdRawData); + + //gather parameters (SimCreator only retrieves part of the + //parameters) gatherParameters(info, mdFileName); //divide the molecules and determine the global index of molecules @@ -160,18 +382,23 @@ namespace oopse { 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)) { @@ -179,14 +406,14 @@ namespace oopse { } if (loadInitCoords) - loadCoordinates(info); + loadCoordinates(info, mdFileName); return info; } void SimCreator::gatherParameters(SimInfo *info, const std::string& mdfile) { - //figure out the ouput file names + //figure out the output file names std::string prefix; #ifdef IS_MPI @@ -215,12 +442,12 @@ namespace oopse { #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; @@ -332,7 +559,7 @@ namespace oopse { // 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)) { @@ -362,7 +589,7 @@ namespace oopse { info->setMolToProcMap(molToProcMap); sprintf(checkPointMsg, "Successfully divided the molecules among the processors.\n"); - MPIcheckPoint(); + errorCheckPoint(); } #endif @@ -392,74 +619,13 @@ namespace oopse { } //end for(int i=0) } - - void SimCreator::compList(MakeStamps *stamps, Globals* simParams, - std::vector < std::pair > &moleculeStampPairs) { - int i; - char * id; - LinkedMolStamp* extractedStamp = NULL; - MoleculeStamp * currentStamp; - Component** the_components = simParams->getComponents(); - int n_components = simParams->getNComponents(); - if (!simParams->haveNMol()) { - // we don't have the total number of molecules, so we assume it is - // given in each component - - for(i = 0; i < n_components; i++) { - if (!the_components[i]->haveNMol()) { - // we have a problem - sprintf(painCave.errMsg, - "SimCreator Error. No global NMol or component NMol given.\n" - "\tCannot calculate the number of atoms.\n"); - - painCave.isFatal = 1; - simError(); - } - - id = the_components[i]->getType(); - - extractedStamp = stamps->extractMolStamp(id); - if (extractedStamp == NULL) { - sprintf(painCave.errMsg, - "SimCreator error: Component \"%s\" was not found in the " - "list of declared molecules\n", id); - - painCave.isFatal = 1; - simError(); - } - - currentStamp = extractedStamp->getStamp(); - - - moleculeStampPairs.push_back( - std::make_pair(currentStamp, the_components[i]->getNMol())); - } //end for (i = 0; i < n_components; i++) - } else { - sprintf(painCave.errMsg, "SimSetup error.\n" - "\tSorry, the ability to specify total" - " nMols and then give molfractions in the components\n" - "\tis not currently supported." - " Please give nMol in the components.\n"); - - painCave.isFatal = 1; - simError(); - } - -#ifdef IS_MPI - - strcpy(checkPointMsg, "Component stamps successfully extracted\n"); - MPIcheckPoint(); - -#endif // is_mpi - - } - void SimCreator::setGlobalIndex(SimInfo *info) { SimInfo::MoleculeIterator mi; Molecule::AtomIterator ai; Molecule::RigidBodyIterator ri; Molecule::CutoffGroupIterator ci; + Molecule::IntegrableObjectIterator ioi; Molecule * mol; Atom * atom; RigidBody * rb; @@ -468,7 +634,8 @@ namespace oopse { int beginRigidBodyIndex; int beginCutoffGroupIndex; int nGlobalAtoms = info->getNGlobalAtoms(); - + + /**@todo fixme */ #ifndef IS_MPI beginAtomIndex = 0; @@ -583,21 +750,53 @@ namespace oopse { #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) { @@ -606,7 +805,7 @@ namespace oopse { //invalid initial coordinate file sprintf(painCave.errMsg, "Initial configuration file %s should at least contain one frame\n", - simParams->getInitialConfig()); + mdFileName.c_str()); painCave.isFatal = 1; simError(); }