ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/brains/SimCreator.cpp
(Generate patch)

Comparing trunk/src/brains/SimCreator.cpp (file contents):
Revision 1241 by gezelter, Fri Apr 25 15:14:47 2008 UTC vs.
Revision 1880 by gezelter, Mon Jun 17 18:28:30 2013 UTC

# Line 6 | Line 6
6   * redistribute this software in source and binary code form, provided
7   * that the following conditions are met:
8   *
9 < * 1. Acknowledgement of the program authors must be made in any
10 < *    publication of scientific results based in part on use of the
11 < *    program.  An acceptable form of acknowledgement is citation of
12 < *    the article in which the program was described (Matthew
13 < *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 < *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 < *    Parallel Simulation Engine for Molecular Dynamics,"
16 < *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 < *
18 < * 2. Redistributions of source code must retain the above copyright
9 > * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   *
12 < * 3. Redistributions in binary form must reproduce the above copyright
12 > * 2. Redistributions in binary form must reproduce the above copyright
13   *    notice, this list of conditions and the following disclaimer in the
14   *    documentation and/or other materials provided with the
15   *    distribution.
# Line 37 | Line 28
28   * arising out of the use of or inability to use software, even if the
29   * University of Notre Dame has been advised of the possibility of
30   * such damages.
31 + *
32 + * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your
33 + * research, please cite the appropriate papers when you publish your
34 + * work.  Good starting points are:
35 + *                                                                      
36 + * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37 + * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38 + * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).          
39 + * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 + * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43   /**
44   * @file SimCreator.cpp
45   * @author tlin
46   * @date 11/03/2004
46 * @time 13:51am
47   * @version 1.0
48   */
49   #include <exception>
# Line 55 | Line 55
55   #include "brains/SimCreator.hpp"
56   #include "brains/SimSnapshotManager.hpp"
57   #include "io/DumpReader.hpp"
58 < #include "UseTheForce/ForceFieldFactory.hpp"
58 > #include "brains/ForceField.hpp"
59   #include "utils/simError.h"
60   #include "utils/StringUtils.hpp"
61   #include "math/SeqRandNumGen.hpp"
# Line 75 | Line 75
75   #include "antlr/NoViableAltForCharException.hpp"
76   #include "antlr/NoViableAltException.hpp"
77  
78 + #include "types/DirectionalAdapter.hpp"
79 + #include "types/MultipoleAdapter.hpp"
80 + #include "types/EAMAdapter.hpp"
81 + #include "types/SuttonChenAdapter.hpp"
82 + #include "types/PolarizableAdapter.hpp"
83 + #include "types/FixedChargeAdapter.hpp"
84 + #include "types/FluctuatingChargeAdapter.hpp"
85 +
86   #ifdef IS_MPI
87 + #include "mpi.h"
88   #include "math/ParallelRandNumGen.hpp"
89   #endif
90  
91 < namespace oopse {
91 > namespace OpenMD {
92    
93 <  Globals* SimCreator::parseFile(std::istream& rawMetaDataStream, const std::string& filename, int startOfMetaDataBlock ){
93 >  Globals* SimCreator::parseFile(std::istream& rawMetaDataStream, const std::string& filename, int mdFileVersion, int startOfMetaDataBlock ){
94      Globals* simParams = NULL;
95      try {
96  
# Line 90 | Line 99 | namespace oopse {
99   #ifdef IS_MPI            
100        int streamSize;
101        const int masterNode = 0;
102 <      int commStatus;
102 >
103        if (worldRank == masterNode) {
104 < #endif
105 <                
104 >        MPI::COMM_WORLD.Bcast(&mdFileVersion, 1, MPI::INT, masterNode);
105 > #endif                
106          SimplePreprocessor preprocessor;
107          preprocessor.preprocess(rawMetaDataStream, filename, startOfMetaDataBlock, ppStream);
108                  
109   #ifdef IS_MPI            
110          //brocasting the stream size
111          streamSize = ppStream.str().size() +1;
112 <        commStatus = MPI_Bcast(&streamSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD);                  
113 <
114 <        commStatus = MPI_Bcast(static_cast<void*>(const_cast<char*>(ppStream.str().c_str())), streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD);
106 <            
107 <                
112 >        MPI::COMM_WORLD.Bcast(&streamSize, 1, MPI::LONG, masterNode);
113 >        MPI::COMM_WORLD.Bcast(static_cast<void*>(const_cast<char*>(ppStream.str().c_str())), streamSize, MPI::CHAR, masterNode);
114 >                          
115        } else {
116 +        MPI::COMM_WORLD.Bcast(&mdFileVersion, 1, MPI::INT, masterNode);
117 +
118          //get stream size
119 <        commStatus = MPI_Bcast(&streamSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD);  
120 <                
119 >        MPI::COMM_WORLD.Bcast(&streamSize, 1, MPI::LONG, masterNode);
120 >
121          char* buf = new char[streamSize];
122          assert(buf);
123                  
124          //receive file content
125 <        commStatus = MPI_Bcast(buf, streamSize, MPI_CHAR, masterNode, MPI_COMM_WORLD);
125 >        MPI::COMM_WORLD.Bcast(buf, streamSize, MPI::CHAR, masterNode);
126                  
127          ppStream.str(buf);
128 <        delete buf;
120 <
128 >        delete [] buf;
129        }
130   #endif            
131        // Create a scanner that reads from the input stream
# Line 145 | Line 153 | namespace oopse {
153        treeParser.initializeASTFactory(factory);
154        treeParser.setASTFactory(&factory);
155        simParams = treeParser.walkTree(parser.getAST());
148
156      }
157  
158        
# Line 215 | Line 222 | namespace oopse {
222        painCave.isFatal = 1;
223        simError();        
224      }
225 <    catch (OOPSEException& e) {
225 >    catch (OpenMDException& e) {
226        sprintf(painCave.errMsg,
227                "%s\n",
228                e.getMessage().c_str());
# Line 230 | Line 237 | namespace oopse {
237        simError();
238      }
239  
240 +    simParams->setMDfileVersion(mdFileVersion);
241      return simParams;
242    }
243    
244    SimInfo*  SimCreator::createSim(const std::string & mdFileName,
245                                    bool loadInitCoords) {
246 <
246 >    
247      const int bufferSize = 65535;
248      char buffer[bufferSize];
249      int lineNo = 0;
250      std::string mdRawData;
251      int metaDataBlockStart = -1;
252      int metaDataBlockEnd = -1;
253 <    int i;
254 <    int mdOffset;
253 >    int i, j;
254 >    streamoff mdOffset;
255 >    int mdFileVersion;
256  
257 +    // Create a string for embedding the version information in the MetaData
258 +    std::string version;
259 +    version.assign("## Last run using OpenMD Version: ");
260 +    version.append(OPENMD_VERSION_MAJOR);
261 +    version.append(".");
262 +    version.append(OPENMD_VERSION_MINOR);
263 +
264 +    std::string svnrev;
265 +    //convert a macro from compiler to a string in c++
266 +    STR_DEFINE(svnrev, SVN_REV );
267 +    version.append(" Revision: ");
268 +    // If there's no SVN revision, just call this the RELEASE revision.
269 +    if (!svnrev.empty()) {
270 +      version.append(svnrev);
271 +    } else {
272 +      version.append("RELEASE");
273 +    }
274 +  
275   #ifdef IS_MPI            
276      const int masterNode = 0;
277      if (worldRank == masterNode) {
278   #endif
279  
280 <      std::ifstream mdFile_(mdFileName.c_str());
280 >      std::ifstream mdFile_;
281 >      mdFile_.open(mdFileName.c_str(), ifstream::in | ifstream::binary);
282        
283        if (mdFile_.fail()) {
284          sprintf(painCave.errMsg,
# Line 263 | Line 291 | namespace oopse {
291        mdFile_.getline(buffer, bufferSize);
292        ++lineNo;
293        std::string line = trimLeftCopy(buffer);
294 <      i = CaseInsensitiveFind(line, "<OOPSE");
295 <      if (i == string::npos) {
294 >      i = CaseInsensitiveFind(line, "<OpenMD");
295 >      if (static_cast<size_t>(i) == string::npos) {
296 >        // try the older file strings to see if that works:
297 >        i = CaseInsensitiveFind(line, "<OOPSE");
298 >      }
299 >      
300 >      if (static_cast<size_t>(i) == string::npos) {
301 >        // still no luck!
302          sprintf(painCave.errMsg,
303 <                "SimCreator: File: %s is not an OOPSE file!\n",
303 >                "SimCreator: File: %s is not a valid OpenMD file!\n",
304                  mdFileName.c_str());
305          painCave.isFatal = 1;
306          simError();
307        }
308 +      
309 +      // found the correct opening string, now try to get the file
310 +      // format version number.
311  
312 +      StringTokenizer tokenizer(line, "=<> \t\n\r");
313 +      std::string fileType = tokenizer.nextToken();
314 +      toUpper(fileType);
315 +
316 +      mdFileVersion = 0;
317 +
318 +      if (fileType == "OPENMD") {
319 +        while (tokenizer.hasMoreTokens()) {
320 +          std::string token(tokenizer.nextToken());
321 +          toUpper(token);
322 +          if (token == "VERSION") {
323 +            mdFileVersion = tokenizer.nextTokenAsInt();
324 +            break;
325 +          }
326 +        }
327 +      }
328 +            
329        //scan through the input stream and find MetaData tag        
330        while(mdFile_.getline(buffer, bufferSize)) {
331          ++lineNo;
# Line 312 | Line 366 | namespace oopse {
366  
367        mdRawData.clear();
368  
369 +      bool foundVersion = false;
370 +
371        for (int i = 0; i < metaDataBlockEnd - metaDataBlockStart - 1; ++i) {
372          mdFile_.getline(buffer, bufferSize);
373 <        mdRawData += buffer;
373 >        std::string line = trimLeftCopy(buffer);
374 >        j = CaseInsensitiveFind(line, "## Last run using OpenMD Version");
375 >        if (static_cast<size_t>(j) != string::npos) {
376 >          foundVersion = true;
377 >          mdRawData += version;
378 >        } else {
379 >          mdRawData += buffer;
380 >        }
381          mdRawData += "\n";
382        }
383 <
383 >      
384 >      if (!foundVersion) mdRawData += version + "\n";
385 >      
386        mdFile_.close();
387  
388   #ifdef IS_MPI
# Line 327 | Line 392 | namespace oopse {
392      std::stringstream rawMetaDataStream(mdRawData);
393  
394      //parse meta-data file
395 <    Globals* simParams = parseFile(rawMetaDataStream, mdFileName, metaDataBlockStart+1);
395 >    Globals* simParams = parseFile(rawMetaDataStream, mdFileName, mdFileVersion,
396 >                                   metaDataBlockStart + 1);
397      
398      //create the force field
399 <    ForceField * ff = ForceFieldFactory::getInstance()
400 <      ->createForceField(simParams->getForceField());
335 <    
399 >    ForceField * ff = new ForceField(simParams->getForceField());
400 >
401      if (ff == NULL) {
402        sprintf(painCave.errMsg,
403                "ForceField Factory can not create %s force field\n",
# Line 365 | Line 430 | namespace oopse {
430      }
431      
432      ff->parse(forcefieldFileName);
368    ff->setFortranForceOptions();
433      //create SimInfo
434      SimInfo * info = new SimInfo(ff, simParams);
435  
# Line 383 | Line 447 | namespace oopse {
447      //create the molecules
448      createMolecules(info);
449      
450 <    
450 >    //find the storage layout
451 >
452 >    int storageLayout = computeStorageLayout(info);
453 >
454      //allocate memory for DataStorage(circular reference, need to
455      //break it)
456 <    info->setSnapshotManager(new SimSnapshotManager(info));
456 >    info->setSnapshotManager(new SimSnapshotManager(info, storageLayout));
457      
458      //set the global index of atoms, rigidbodies and cutoffgroups
459      //(only need to be set once, the global index will never change
# Line 395 | Line 462 | namespace oopse {
462      //responsibility to LocalIndexManager.
463      setGlobalIndex(info);
464      
465 <    //Although addExcludePairs is called inside SimInfo's addMolecule
465 >    //Although addInteractionPairs is called inside SimInfo's addMolecule
466      //method, at that point atoms don't have the global index yet
467      //(their global index are all initialized to -1).  Therefore we
468 <    //have to call addExcludePairs explicitly here. A way to work
468 >    //have to call addInteractionPairs explicitly here. A way to work
469      //around is that we can determine the beginning global indices of
470      //atoms before they get created.
471      SimInfo::MoleculeIterator mi;
472      Molecule* mol;
473      for (mol= info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {
474 <      info->addExcludePairs(mol);
474 >      info->addInteractionPairs(mol);
475      }
476      
477      if (loadInitCoords)
478        loadCoordinates(info, mdFileName);    
412    
479      return info;
480    }
481    
# Line 444 | Line 510 | namespace oopse {
510    
511   #ifdef IS_MPI
512    void SimCreator::divideMolecules(SimInfo *info) {
447    RealType numerator;
448    RealType denominator;
449    RealType precast;
450    RealType x;
451    RealType y;
513      RealType a;
453    int old_atoms;
454    int add_atoms;
455    int new_atoms;
456    int nTarget;
457    int done;
458    int i;
459    int j;
460    int loops;
461    int which_proc;
514      int nProcessors;
515      std::vector<int> atomsPerProc;
516      int nGlobalMols = info->getNGlobalMolecules();
517 <    std::vector<int> molToProcMap(nGlobalMols, -1); // default to an error condition:
517 >    std::vector<int> molToProcMap(nGlobalMols, -1); // default to an
518 >                                                    // error
519 >                                                    // condition:
520      
521 <    MPI_Comm_size(MPI_COMM_WORLD, &nProcessors);
521 >    nProcessors = MPI::COMM_WORLD.Get_size();
522      
523      if (nProcessors > nGlobalMols) {
524        sprintf(painCave.errMsg,
# Line 473 | Line 527 | namespace oopse {
527                "\tthe number of molecules.  This will not result in a \n"
528                "\tusable division of atoms for force decomposition.\n"
529                "\tEither try a smaller number of processors, or run the\n"
530 <              "\tsingle-processor version of OOPSE.\n", nProcessors, nGlobalMols);
530 >              "\tsingle-processor version of OpenMD.\n", nProcessors,
531 >              nGlobalMols);
532        
533        painCave.isFatal = 1;
534        simError();
535      }
536      
482    int seedValue;
537      Globals * simParams = info->getSimParams();
538 <    SeqRandNumGen* myRandom; //divide labor does not need Parallel random number generator
538 >    SeqRandNumGen* myRandom; //divide labor does not need Parallel
539 >                             //random number generator
540      if (simParams->haveSeed()) {
541 <      seedValue = simParams->getSeed();
541 >      int seedValue = simParams->getSeed();
542        myRandom = new SeqRandNumGen(seedValue);
543      }else {
544        myRandom = new SeqRandNumGen();
# Line 496 | Line 551 | namespace oopse {
551      atomsPerProc.insert(atomsPerProc.end(), nProcessors, 0);
552      
553      if (worldRank == 0) {
554 <      numerator = info->getNGlobalAtoms();
555 <      denominator = nProcessors;
556 <      precast = numerator / denominator;
557 <      nTarget = (int)(precast + 0.5);
554 >      RealType numerator = info->getNGlobalAtoms();
555 >      RealType denominator = nProcessors;
556 >      RealType precast = numerator / denominator;
557 >      int nTarget = (int)(precast + 0.5);
558        
559 <      for(i = 0; i < nGlobalMols; i++) {
560 <        done = 0;
561 <        loops = 0;
559 >      for(int i = 0; i < nGlobalMols; i++) {
560 >
561 >        int done = 0;
562 >        int loops = 0;
563          
564          while (!done) {
565            loops++;
566            
567            // Pick a processor at random
568            
569 <          which_proc = (int) (myRandom->rand() * nProcessors);
569 >          int which_proc = (int) (myRandom->rand() * nProcessors);
570            
571            //get the molecule stamp first
572            int stampId = info->getMoleculeStampId(i);
573            MoleculeStamp * moleculeStamp = info->getMoleculeStamp(stampId);
574            
575            // How many atoms does this processor have so far?
576 <          old_atoms = atomsPerProc[which_proc];
577 <          add_atoms = moleculeStamp->getNAtoms();
578 <          new_atoms = old_atoms + add_atoms;
576 >          int old_atoms = atomsPerProc[which_proc];
577 >          int add_atoms = moleculeStamp->getNAtoms();
578 >          int new_atoms = old_atoms + add_atoms;
579            
580            // If we've been through this loop too many times, we need
581            // to just give up and assign the molecule to this processor
582            // and be done with it.
583            
584            if (loops > 100) {
585 +
586              sprintf(painCave.errMsg,
587 <                    "I've tried 100 times to assign molecule %d to a "
588 <                    " processor, but can't find a good spot.\n"
589 <                    "I'm assigning it at random to processor %d.\n",
587 >                    "There have been 100 attempts to assign molecule %d to an\n"
588 >                    "\tunderworked processor, but there's no good place to\n"
589 >                    "\tleave it.  OpenMD is assigning it at random to processor %d.\n",
590                      i, which_proc);
591 <            
591 >          
592              painCave.isFatal = 0;
593 +            painCave.severity = OPENMD_INFO;
594              simError();
595              
596              molToProcMap[i] = which_proc;
# Line 561 | Line 619 | namespace oopse {
619            //           Pacc(x) = exp(- a * x)
620            // where a = penalty / (average atoms per molecule)
621            
622 <          x = (RealType)(new_atoms - nTarget);
623 <          y = myRandom->rand();
622 >          RealType x = (RealType)(new_atoms - nTarget);
623 >          RealType y = myRandom->rand();
624            
625            if (y < exp(- a * x)) {
626              molToProcMap[i] = which_proc;
# Line 577 | Line 635 | namespace oopse {
635        }
636        
637        delete myRandom;
638 <      
638 >
639        // Spray out this nonsense to all other processors:
640 <      
583 <      MPI_Bcast(&molToProcMap[0], nGlobalMols, MPI_INT, 0, MPI_COMM_WORLD);
640 >      MPI::COMM_WORLD.Bcast(&molToProcMap[0], nGlobalMols, MPI::INT, 0);
641      } else {
642        
643        // Listen to your marching orders from processor 0:
644 <      
645 <      MPI_Bcast(&molToProcMap[0], nGlobalMols, MPI_INT, 0, MPI_COMM_WORLD);
644 >      MPI::COMM_WORLD.Bcast(&molToProcMap[0], nGlobalMols, MPI::INT, 0);
645 >
646      }
647      
648      info->setMolToProcMap(molToProcMap);
# Line 608 | Line 665 | namespace oopse {
665   #endif
666          
667          stampId = info->getMoleculeStampId(i);
668 <        Molecule * mol = molCreator.createMolecule(info->getForceField(), info->getMoleculeStamp(stampId),
669 <                                                   stampId, i, info->getLocalIndexManager());
668 >        Molecule * mol = molCreator.createMolecule(info->getForceField(),
669 >                                                   info->getMoleculeStamp(stampId),
670 >                                                   stampId, i,
671 >                                                   info->getLocalIndexManager());
672          
673          info->addMolecule(mol);
674          
# Line 622 | Line 681 | namespace oopse {
681      } //end for(int i=0)  
682    }
683      
684 +  int SimCreator::computeStorageLayout(SimInfo* info) {
685 +
686 +    Globals* simParams = info->getSimParams();
687 +    int nRigidBodies = info->getNGlobalRigidBodies();
688 +    set<AtomType*> atomTypes = info->getSimulatedAtomTypes();
689 +    set<AtomType*>::iterator i;
690 +    bool hasDirectionalAtoms = false;
691 +    bool hasFixedCharge = false;
692 +    bool hasDipoles = false;    
693 +    bool hasQuadrupoles = false;    
694 +    bool hasPolarizable = false;    
695 +    bool hasFluctuatingCharge = false;    
696 +    bool hasMetallic = false;
697 +    int storageLayout = 0;
698 +    storageLayout |= DataStorage::dslPosition;
699 +    storageLayout |= DataStorage::dslVelocity;
700 +    storageLayout |= DataStorage::dslForce;
701 +
702 +    for (i = atomTypes.begin(); i != atomTypes.end(); ++i) {
703 +
704 +      DirectionalAdapter da = DirectionalAdapter( (*i) );
705 +      MultipoleAdapter ma = MultipoleAdapter( (*i) );
706 +      EAMAdapter ea = EAMAdapter( (*i) );
707 +      SuttonChenAdapter sca = SuttonChenAdapter( (*i) );
708 +      PolarizableAdapter pa = PolarizableAdapter( (*i) );
709 +      FixedChargeAdapter fca = FixedChargeAdapter( (*i) );
710 +      FluctuatingChargeAdapter fqa = FluctuatingChargeAdapter( (*i) );
711 +
712 +      if (da.isDirectional()){
713 +        hasDirectionalAtoms = true;
714 +      }
715 +      if (ma.isDipole()){
716 +        hasDipoles = true;
717 +      }
718 +      if (ma.isQuadrupole()){
719 +        hasQuadrupoles = true;
720 +      }
721 +      if (ea.isEAM() || sca.isSuttonChen()){
722 +        hasMetallic = true;
723 +      }
724 +      if ( fca.isFixedCharge() ){
725 +        hasFixedCharge = true;
726 +      }
727 +      if ( fqa.isFluctuatingCharge() ){
728 +        hasFluctuatingCharge = true;
729 +      }
730 +      if ( pa.isPolarizable() ){
731 +        hasPolarizable = true;
732 +      }
733 +    }
734 +    
735 +    if (nRigidBodies > 0 || hasDirectionalAtoms) {
736 +      storageLayout |= DataStorage::dslAmat;
737 +      if(storageLayout & DataStorage::dslVelocity) {
738 +        storageLayout |= DataStorage::dslAngularMomentum;
739 +      }
740 +      if (storageLayout & DataStorage::dslForce) {
741 +        storageLayout |= DataStorage::dslTorque;
742 +      }
743 +    }
744 +    if (hasDipoles) {
745 +      storageLayout |= DataStorage::dslDipole;
746 +    }
747 +    if (hasQuadrupoles) {
748 +      storageLayout |= DataStorage::dslQuadrupole;
749 +    }
750 +    if (hasFixedCharge || hasFluctuatingCharge) {
751 +      storageLayout |= DataStorage::dslSkippedCharge;
752 +    }
753 +    if (hasMetallic) {
754 +      storageLayout |= DataStorage::dslDensity;
755 +      storageLayout |= DataStorage::dslFunctional;
756 +      storageLayout |= DataStorage::dslFunctionalDerivative;
757 +    }
758 +    if (hasPolarizable) {
759 +      storageLayout |= DataStorage::dslElectricField;
760 +    }
761 +    if (hasFluctuatingCharge){
762 +      storageLayout |= DataStorage::dslFlucQPosition;
763 +      if(storageLayout & DataStorage::dslVelocity) {
764 +        storageLayout |= DataStorage::dslFlucQVelocity;
765 +      }
766 +      if (storageLayout & DataStorage::dslForce) {
767 +        storageLayout |= DataStorage::dslFlucQForce;
768 +      }
769 +    }
770 +    
771 +    // if the user has asked for them, make sure we've got the memory for the
772 +    // objects defined.
773 +
774 +    if (simParams->getOutputParticlePotential()) {
775 +      storageLayout |= DataStorage::dslParticlePot;
776 +    }
777 +
778 +    if (simParams->havePrintHeatFlux()) {
779 +      if (simParams->getPrintHeatFlux()) {
780 +        storageLayout |= DataStorage::dslParticlePot;
781 +      }
782 +    }
783 +
784 +    if (simParams->getOutputElectricField() | simParams->haveElectricField()) {
785 +      storageLayout |= DataStorage::dslElectricField;
786 +    }
787 +
788 +    if (simParams->getOutputFluctuatingCharges()) {
789 +      storageLayout |= DataStorage::dslFlucQPosition;
790 +      storageLayout |= DataStorage::dslFlucQVelocity;
791 +      storageLayout |= DataStorage::dslFlucQForce;
792 +    }
793 +
794 +    info->setStorageLayout(storageLayout);
795 +
796 +    return storageLayout;
797 +  }
798 +
799    void SimCreator::setGlobalIndex(SimInfo *info) {
800      SimInfo::MoleculeIterator mi;
801      Molecule::AtomIterator ai;
# Line 636 | Line 810 | namespace oopse {
810      int beginRigidBodyIndex;
811      int beginCutoffGroupIndex;
812      int nGlobalAtoms = info->getNGlobalAtoms();
813 <
640 <    /**@todo fixme */
641 < #ifndef IS_MPI
813 >    int nGlobalRigidBodies = info->getNGlobalRigidBodies();
814      
815      beginAtomIndex = 0;
644    beginRigidBodyIndex = 0;
645    beginCutoffGroupIndex = 0;
646    
647 #else
648    
649    int nproc;
650    int myNode;
651    
652    myNode = worldRank;
653    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
654    
655    std::vector < int > tmpAtomsInProc(nproc, 0);
656    std::vector < int > tmpRigidBodiesInProc(nproc, 0);
657    std::vector < int > tmpCutoffGroupsInProc(nproc, 0);
658    std::vector < int > NumAtomsInProc(nproc, 0);
659    std::vector < int > NumRigidBodiesInProc(nproc, 0);
660    std::vector < int > NumCutoffGroupsInProc(nproc, 0);
661    
662    tmpAtomsInProc[myNode] = info->getNAtoms();
663    tmpRigidBodiesInProc[myNode] = info->getNRigidBodies();
664    tmpCutoffGroupsInProc[myNode] = info->getNCutoffGroups();
665    
666    //do MPI_ALLREDUCE to exchange the total number of atoms, rigidbodies and cutoff groups
667    MPI_Allreduce(&tmpAtomsInProc[0], &NumAtomsInProc[0], nproc, MPI_INT,
668                  MPI_SUM, MPI_COMM_WORLD);
669    MPI_Allreduce(&tmpRigidBodiesInProc[0], &NumRigidBodiesInProc[0], nproc,
670                  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
671    MPI_Allreduce(&tmpCutoffGroupsInProc[0], &NumCutoffGroupsInProc[0], nproc,
672                  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
673    
674    beginAtomIndex = 0;
675    beginRigidBodyIndex = 0;
676    beginCutoffGroupIndex = 0;
677    
678    for(int i = 0; i < myNode; i++) {
679      beginAtomIndex += NumAtomsInProc[i];
680      beginRigidBodyIndex += NumRigidBodiesInProc[i];
681      beginCutoffGroupIndex += NumCutoffGroupsInProc[i];
682    }
683    
684 #endif
685    
816      //rigidbody's index begins right after atom's
817 <    beginRigidBodyIndex += info->getNGlobalAtoms();
818 <    
819 <    for(mol = info->beginMolecule(mi); mol != NULL;
820 <        mol = info->nextMolecule(mi)) {
817 >    beginRigidBodyIndex = info->getNGlobalAtoms();
818 >    beginCutoffGroupIndex = 0;
819 >
820 >    for(int i = 0; i < info->getNGlobalMolecules(); i++) {
821        
822 <      //local index(index in DataStorge) of atom is important
823 <      for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
824 <        atom->setGlobalIndex(beginAtomIndex++);
822 > #ifdef IS_MPI      
823 >      if (info->getMolToProc(i) == worldRank) {
824 > #endif        
825 >        // stuff to do if I own this molecule
826 >        mol = info->getMoleculeByGlobalIndex(i);
827 >
828 >        //local index(index in DataStorge) of atom is important
829 >        for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
830 >          atom->setGlobalIndex(beginAtomIndex++);
831 >        }
832 >        
833 >        for(rb = mol->beginRigidBody(ri); rb != NULL;
834 >            rb = mol->nextRigidBody(ri)) {
835 >          rb->setGlobalIndex(beginRigidBodyIndex++);
836 >        }
837 >        
838 >        //local index of cutoff group is trivial, it only depends on
839 >        //the order of travesing
840 >        for(cg = mol->beginCutoffGroup(ci); cg != NULL;
841 >            cg = mol->nextCutoffGroup(ci)) {
842 >          cg->setGlobalIndex(beginCutoffGroupIndex++);
843 >        }        
844 >        
845 > #ifdef IS_MPI        
846 >      }  else {
847 >
848 >        // stuff to do if I don't own this molecule
849 >        
850 >        int stampId = info->getMoleculeStampId(i);
851 >        MoleculeStamp* stamp = info->getMoleculeStamp(stampId);
852 >
853 >        beginAtomIndex += stamp->getNAtoms();
854 >        beginRigidBodyIndex += stamp->getNRigidBodies();
855 >        beginCutoffGroupIndex += stamp->getNCutoffGroups() + stamp->getNFreeAtoms();
856        }
857 <      
858 <      for(rb = mol->beginRigidBody(ri); rb != NULL;
859 <          rb = mol->nextRigidBody(ri)) {
860 <        rb->setGlobalIndex(beginRigidBodyIndex++);
700 <      }
701 <      
702 <      //local index of cutoff group is trivial, it only depends on the order of travesing
703 <      for(cg = mol->beginCutoffGroup(ci); cg != NULL;
704 <          cg = mol->nextCutoffGroup(ci)) {
705 <        cg->setGlobalIndex(beginCutoffGroupIndex++);
706 <      }
707 <    }
708 <    
857 > #endif          
858 >
859 >    } //end for(int i=0)  
860 >
861      //fill globalGroupMembership
862      std::vector<int> globalGroupMembership(info->getNGlobalAtoms(), 0);
863      for(mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {        
# Line 717 | Line 869 | namespace oopse {
869          
870        }      
871      }
872 <    
872 >  
873   #ifdef IS_MPI    
874      // Since the globalGroupMembership has been zero filled and we've only
875      // poked values into the atoms we know, we can do an Allreduce
876      // to get the full globalGroupMembership array (We think).
877      // This would be prettier if we could use MPI_IN_PLACE like the MPI-2
878      // docs said we could.
879 <    std::vector<int> tmpGroupMembership(nGlobalAtoms, 0);
880 <    MPI_Allreduce(&globalGroupMembership[0], &tmpGroupMembership[0], nGlobalAtoms,
881 <                  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
879 >    std::vector<int> tmpGroupMembership(info->getNGlobalAtoms(), 0);
880 >    MPI::COMM_WORLD.Allreduce(&globalGroupMembership[0],
881 >                              &tmpGroupMembership[0], nGlobalAtoms,
882 >                              MPI::INT, MPI::SUM);
883      info->setGlobalGroupMembership(tmpGroupMembership);
884   #else
885      info->setGlobalGroupMembership(globalGroupMembership);
886   #endif
887      
888      //fill molMembership
889 <    std::vector<int> globalMolMembership(info->getNGlobalAtoms(), 0);
889 >    std::vector<int> globalMolMembership(info->getNGlobalAtoms() +
890 >                                         info->getNGlobalRigidBodies(), 0);
891      
892 <    for(mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {
893 <      
892 >    for(mol = info->beginMolecule(mi); mol != NULL;
893 >        mol = info->nextMolecule(mi)) {
894        for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
895          globalMolMembership[atom->getGlobalIndex()] = mol->getGlobalIndex();
896        }
897 +      for (rb = mol->beginRigidBody(ri); rb != NULL;
898 +           rb = mol->nextRigidBody(ri)) {
899 +        globalMolMembership[rb->getGlobalIndex()] = mol->getGlobalIndex();
900 +      }
901      }
902      
903   #ifdef IS_MPI
904 <    std::vector<int> tmpMolMembership(nGlobalAtoms, 0);
904 >    std::vector<int> tmpMolMembership(info->getNGlobalAtoms() +
905 >                                      info->getNGlobalRigidBodies(), 0);
906 >    MPI::COMM_WORLD.Allreduce(&globalMolMembership[0], &tmpMolMembership[0],
907 >                              nGlobalAtoms + nGlobalRigidBodies,
908 >                              MPI::INT, MPI::SUM);
909      
748    MPI_Allreduce(&globalMolMembership[0], &tmpMolMembership[0], nGlobalAtoms,
749                  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
750    
910      info->setGlobalMolMembership(tmpMolMembership);
911   #else
912      info->setGlobalMolMembership(globalMolMembership);
# Line 757 | Line 916 | namespace oopse {
916      // here the molecules are listed by their global indices.
917  
918      std::vector<int> nIOPerMol(info->getNGlobalMolecules(), 0);
919 <    for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {
919 >    for (mol = info->beginMolecule(mi); mol != NULL;
920 >         mol = info->nextMolecule(mi)) {
921        nIOPerMol[mol->getGlobalIndex()] = mol->getNIntegrableObjects();      
922      }
923      
924   #ifdef IS_MPI
925      std::vector<int> numIntegrableObjectsPerMol(info->getNGlobalMolecules(), 0);
926 <    MPI_Allreduce(&nIOPerMol[0], &numIntegrableObjectsPerMol[0],
927 <                  info->getNGlobalMolecules(), MPI_INT, MPI_SUM, MPI_COMM_WORLD);
926 >    MPI::COMM_WORLD.Allreduce(&nIOPerMol[0], &numIntegrableObjectsPerMol[0],
927 >                              info->getNGlobalMolecules(), MPI::INT, MPI::SUM);
928   #else
929      std::vector<int> numIntegrableObjectsPerMol = nIOPerMol;
930   #endif    
931  
932 < std::vector<int> startingIOIndexForMol(info->getNGlobalMolecules());
933 <
934 < int startingIndex = 0;
935 < for (int i = 0; i < info->getNGlobalMolecules(); i++) {
936 <  startingIOIndexForMol[i] = startingIndex;
937 <  startingIndex += numIntegrableObjectsPerMol[i];
938 < }
939 <
940 < std::vector<StuntDouble*> IOIndexToIntegrableObject(info->getNGlobalIntegrableObjects(), (StuntDouble*)NULL);
941 < for (mol = info->beginMolecule(mi); mol != NULL; mol = info->nextMolecule(mi)) {
932 >    std::vector<int> startingIOIndexForMol(info->getNGlobalMolecules());
933 >    
934 >    int startingIndex = 0;
935 >    for (int i = 0; i < info->getNGlobalMolecules(); i++) {
936 >      startingIOIndexForMol[i] = startingIndex;
937 >      startingIndex += numIntegrableObjectsPerMol[i];
938 >    }
939 >    
940 >    std::vector<StuntDouble*> IOIndexToIntegrableObject(info->getNGlobalIntegrableObjects(), (StuntDouble*)NULL);
941 >    for (mol = info->beginMolecule(mi); mol != NULL;
942 >         mol = info->nextMolecule(mi)) {
943        int myGlobalIndex = mol->getGlobalIndex();
944        int globalIO = startingIOIndexForMol[myGlobalIndex];
945 <      for (StuntDouble* integrableObject = mol->beginIntegrableObject(ioi); integrableObject != NULL;
946 <           integrableObject = mol->nextIntegrableObject(ioi)) {
947 <            integrableObject->setGlobalIntegrableObjectIndex(globalIO);
948 <            IOIndexToIntegrableObject[globalIO] = integrableObject;
949 <            globalIO++;
945 >      for (StuntDouble* sd = mol->beginIntegrableObject(ioi); sd != NULL;
946 >           sd = mol->nextIntegrableObject(ioi)) {
947 >        sd->setGlobalIntegrableObjectIndex(globalIO);
948 >        IOIndexToIntegrableObject[globalIO] = sd;
949 >        globalIO++;
950        }
951      }
952 <
953 <  info->setIOIndexToIntegrableObject(IOIndexToIntegrableObject);
954 <  
952 >      
953 >    info->setIOIndexToIntegrableObject(IOIndexToIntegrableObject);
954 >    
955    }
956    
957    void SimCreator::loadCoordinates(SimInfo* info, const std::string& mdFileName) {
797    Globals* simParams;
798    simParams = info->getSimParams();
958      
800    
959      DumpReader reader(info, mdFileName);
960      int nframes = reader.getNFrames();
961      
# Line 811 | Line 969 | int startingIndex = 0;
969        painCave.isFatal = 1;
970        simError();
971      }
814    
972      //copy the current snapshot to previous snapshot
973      info->getSnapshotManager()->advance();
974    }
975    
976 < } //end namespace oopse
976 > } //end namespace OpenMD
977  
978  

Comparing trunk/src/brains/SimCreator.cpp (property svn:keywords):
Revision 1241 by gezelter, Fri Apr 25 15:14:47 2008 UTC vs.
Revision 1880 by gezelter, Mon Jun 17 18:28:30 2013 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines