--- trunk/src/brains/SimCreator.cpp 2005/03/10 15:10:24 417 +++ trunk/src/brains/SimCreator.cpp 2005/12/02 15:38:03 770 @@ -47,71 +47,117 @@ * @version 1.0 */ +#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" + + #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(const std::string mdFileName){ + 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(mdFileName, 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(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(mdFileName); + lexer.initDeferredLineCount(); -#ifdef IS_MPI + // Create a parser that reads from the scanner + MDParser parser(lexer); + parser.setFilename(mdFileName); + + // 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 - - simParams->initalize(); - set_interface_stamps(stamps, simParams); - -#ifdef IS_MPI - - mpiEventInit(); - -#endif - - yacc_BASS(mdFileName.c_str()); - -#ifdef IS_MPI - - throwMPIEvent(NULL); - } else { - set_interface_stamps(stamps, simParams); - mpiEventInit(); - MPIcheckPoint(); - mpiEventLoop(); - } - -#endif - + 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()); + + } + catch (exception& e) { + cerr << "parser exception: " << e.what() << endl; + } + + 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) { + //parse meta-data file - parseFile(mdFileName, stamps, simParams); + Globals* simParams = parseFile(mdFileName); //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,14 +186,10 @@ namespace oopse { } ff->parse(forcefieldFileName); - - //extract the molecule stamps - std::vector < std::pair > moleculeStampPairs; - compList(stamps, simParams, moleculeStampPairs); - + //create SimInfo - SimInfo * info = new SimInfo(moleculeStampPairs, ff, simParams); - + SimInfo * info = new SimInfo(ff, simParams); + //gather parameters (SimCreator only retrieves part of the parameters) gatherParameters(info, mdFileName); @@ -178,9 +220,6 @@ namespace oopse { info->addExcludePairs(mol); } - - //load initial coordinates, some extra information are pushed into SimInfo's property map ( such as - //eta, chi for NPT integrator) if (loadInitCoords) loadCoordinates(info); @@ -189,7 +228,7 @@ namespace oopse { 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 @@ -395,69 +434,7 @@ 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; @@ -609,7 +586,7 @@ namespace oopse { //invalid initial coordinate file sprintf(painCave.errMsg, "Initial configuration file %s should at least contain one frame\n", - simParams->getInitialConfig()); + simParams->getInitialConfig().c_str()); painCave.isFatal = 1; simError(); }