ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/types/MoleculeStamp.cpp
(Generate patch)

Comparing trunk/OOPSE-3.0/src/types/MoleculeStamp.cpp (file contents):
Revision 2543 by gezelter, Wed Jan 11 18:41:01 2006 UTC vs.
Revision 2544 by tim, Wed Jan 11 19:01:20 2006 UTC

# Line 38 | Line 38
38   * University of Notre Dame has been advised of the possibility of
39   * such damages.
40   */
41 <
41 > #include <algorithm>
42   #include <functional>
43   #include <iostream>
44   #include <sstream>
# Line 48 | Line 48 | std::string containerToString(ContainerType& cont) {
48   namespace oopse {
49  
50   template<class ContainerType>
51 < std::string containerToString(ContainerType& cont) {
52 <    std::ostringstream oss;
53 <    oss << "(";
54 <    typename ContainerType::iterator i = cont.begin();
55 <    if (i != cont.end()) {
56 <        oss << *i;
57 <        ++i;
58 <    }
59 <    for (; i != cont.end();++i) {
60 <        oss << ", ";
61 <        oss << *i;
62 <    }
63 <    oss << ")";
64 <    return oss.str();
51 > bool hasDuplicateElement(const ContainerType& cont) {
52 >    ContainerType tmp = cont;
53 >    std::sort(tmp.begin(), tmp.end());
54 >    tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());
55 >    return tmp.size() != cont.size();
56   }
57  
58   MoleculeStamp::MoleculeStamp() {
# Line 89 | Line 80 | bool MoleculeStamp::addAtomStamp( AtomStamp* atom) {
80   bool MoleculeStamp::addAtomStamp( AtomStamp* atom) {
81      bool ret = addIndexSensitiveStamp(atomStamps_, atom);
82      if (!ret) {
83 <         std::cout<< "Error in Molecule " << getName()  << ": multiple atoms have the same indices"<< atom->getIndex() <<"\n";
83 >         std::ostringstream oss;
84 >         oss<< "Error in Molecule " << getName()  << ": multiple atoms have the same indices"<< atom->getIndex() <<"\n";
85 >         throw OOPSEException(oss.str());
86      }
87      return ret;
88      
# Line 113 | Line 106 | bool MoleculeStamp::addRigidBodyStamp( RigidBodyStamp*
106   bool MoleculeStamp::addRigidBodyStamp( RigidBodyStamp* rigidbody) {
107      bool ret = addIndexSensitiveStamp(rigidBodyStamps_, rigidbody);
108      if (!ret) {
109 <        std::cout<< "Error in Molecule " << getName()  << ": multiple rigidbodies have the same indices: " << rigidbody->getIndex() <<"\n";
109 >        std::ostringstream oss;
110 >        oss<< "Error in Molecule " << getName()  << ": multiple rigidbodies have the same indices: " << rigidbody->getIndex() <<"\n";
111 >        throw OOPSEException(oss.str());
112      }
113      return ret;
114   }
# Line 165 | Line 160 | void MoleculeStamp::checkAtoms() {
160   void MoleculeStamp::checkAtoms() {
161      std::vector<AtomStamp*>::iterator ai = std::find(atomStamps_.begin(), atomStamps_.end(), static_cast<AtomStamp*>(NULL));
162      if (ai != atomStamps_.end()) {
163 <        std::cout << "Error in Molecule " << getName() << ": atom[" << ai - atomStamps_.begin()<< "] is missing\n";
163 >        std::ostringstream oss;
164 >        oss << "Error in Molecule " << getName() << ": atom[" << ai - atomStamps_.begin()<< "] is missing\n";
165 >        throw OOPSEException(oss.str());
166      }
167  
168   }
169  
170   void MoleculeStamp::checkBonds() {
171 +    std::ostringstream oss;
172      //make sure index is not out of range
173      int natoms = getNAtoms();
174      for(int i = 0; i < getNBonds(); ++i) {
175          BondStamp* bondStamp = getBondStamp(i);
176 <        if (bondStamp->getA() >=  natoms && bondStamp->getB() >= natoms) {
177 <            std::cout << "Error in Molecule " << getName() <<  ": bond(" << bondStamp->getA() << ", " << bondStamp->getB() << ") is invalid\n";
176 >        if (bondStamp->getA() > natoms-1 ||  bondStamp->getA() < 0 || bondStamp->getB() > natoms-1 || bondStamp->getB() < 0 || bondStamp->getA() == bondStamp->getB()) {
177 >            
178 >            oss << "Error in Molecule " << getName() <<  ": bond(" << bondStamp->getA() << ", " << bondStamp->getB() << ") is invalid\n";
179 >            throw OOPSEException(oss.str());
180          }
181      }
182      
# Line 192 | Line 192 | void MoleculeStamp::checkBonds() {
192          
193          std::set<std::pair<int, int> >::iterator iter = allBonds.find(bondPair);
194          if ( iter != allBonds.end()) {
195 <            std::cout << "Error in Molecule " << getName() << ": " << "bond(" <<iter->first << ", "<< iter->second << ")appears multiple times\n";
195 >            
196 >            oss << "Error in Molecule " << getName() << ": " << "bond(" <<iter->first << ", "<< iter->second << ") appears multiple times\n";
197 >            throw OOPSEException(oss.str());
198          } else {
199              allBonds.insert(bondPair);
200          }
# Line 202 | Line 204 | void MoleculeStamp::checkBonds() {
204      for(int i = 0; i < getNBonds(); ++i) {
205          BondStamp* bondStamp = getBondStamp(i);
206          if (atom2Rigidbody[bondStamp->getA()] == atom2Rigidbody[bondStamp->getB()]) {
207 <            std::cout << "Error in Molecule " << getName() << ": "<<"bond(" << bondStamp->getA() << ", " << bondStamp->getB() << ") belong to same rigidbody " << atom2Rigidbody[bondStamp->getA()] << "\n";
207 >            
208 >            oss << "Error in Molecule " << getName() << ": "<<"bond(" << bondStamp->getA() << ", " << bondStamp->getB() << ") belong to same rigidbody " << atom2Rigidbody[bondStamp->getA()] << "\n";
209 >            throw OOPSEException(oss.str());
210          }
211      }
212      
213   }
214  
215   void MoleculeStamp::checkBends() {
216 +    std::ostringstream oss;
217      for(int i = 0; i < getNBends(); ++i) {
218          BendStamp* bendStamp = getBendStamp(i);
219          std::vector<int> bendAtoms =  bendStamp->getMembers();
220          std::vector<int>::iterator j =std::find_if(bendAtoms.begin(), bendAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
221 <        if (j != bendAtoms.end()) {
217 <            std::cout << "Error in Molecule " << getName() << " : atoms of bend" << containerToString(bendAtoms) << "have invalid indices\n";
218 <        }
221 >        std::vector<int>::iterator k =std::find_if(bendAtoms.begin(), bendAtoms.end(), std::bind2nd(std::less<int>(), 0));
222  
223 +        if (j != bendAtoms.end() || k != bendAtoms.end()) {
224 +            
225 +            oss << "Error in Molecule " << getName() << " : atoms of bend" << containerToString(bendAtoms) << " have invalid indices\n";
226 +            throw OOPSEException(oss.str());
227 +        }
228 +        
229 +        if (hasDuplicateElement(bendAtoms)) {
230 +            oss << "Error in Molecule " << getName() << " : atoms of bend" << containerToString(bendAtoms) << " have duplicated indices\n";    
231 +            throw OOPSEException(oss.str());            
232 +        }
233 +            
234          if (bendAtoms.size() == 2 ) {
235              if (!bendStamp->haveGhostVectorSource()) {
236 <                std::cout << "Error in Molecule " << getName() << ": ghostVectorSouce is missing\n";
236 >                
237 >                oss << "Error in Molecule " << getName() << ": ghostVectorSouce is missing\n";
238 >                throw OOPSEException(oss.str());
239              }else{
240                  int ghostIndex = bendStamp->getGhostVectorSource();
241                  if (ghostIndex < getNAtoms()) {
242                      if (std::find(bendAtoms.begin(), bendAtoms.end(), ghostIndex) == bendAtoms.end()) {
243 <                      std::cout <<  "Error in Molecule " << getName() << ": ghostVectorSouce "<< ghostIndex<<"is invalid\n";
243 >                      
244 >                      oss <<  "Error in Molecule " << getName() << ": ghostVectorSouce "<< ghostIndex<<"is invalid\n";
245 >                      throw OOPSEException(oss.str());
246                      }
247                      if (!getAtomStamp(ghostIndex)->haveOrientation()) {
248 <                        std::cout <<  "Error in Molecule " << getName() << ": ghost atom must be a directioanl atom\n";
248 >                        
249 >                        oss <<  "Error in Molecule " << getName() << ": ghost atom must be a directioanl atom\n";
250 >                        throw OOPSEException(oss.str());
251                      }
252                  }else {
253 <                    std::cout << "Error in Molecule " << getName() <<  ": ghostVectorsource " << ghostIndex<< "  is invalid\n";
253 >                    oss << "Error in Molecule " << getName() <<  ": ghostVectorsource " << ghostIndex<< "  is invalid\n";
254 >                    throw OOPSEException(oss.str());
255                  }
256              }
257          } else if (bendAtoms.size() == 3 && bendStamp->haveGhostVectorSource()) {
258 <            std::cout <<  "Error in Molecule " << getName() << ": normal bend should not have ghostVectorSouce\n";
258 >            oss <<  "Error in Molecule " << getName() << ": normal bend should not have ghostVectorSouce\n";
259 >            throw OOPSEException(oss.str());
260          }
261      }
262  
# Line 248 | Line 270 | void MoleculeStamp::checkBends() {
270              if (rigidbodyIndex >= 0) {
271                  ++rigidSet[rigidbodyIndex];
272                  if (rigidSet[rigidbodyIndex] > 1) {
273 <                    std::cout << "Error in Molecule " << getName() << ": bend" << containerToString(bendAtoms) << " belong to same rigidbody " << rigidbodyIndex << "\n";                    
273 >                    oss << "Error in Molecule " << getName() << ": bend" << containerToString(bendAtoms) << " belong to same rigidbody " << rigidbodyIndex << "\n";  
274 >                    throw OOPSEException(oss.str());
275                  }
276              }
277          }
# Line 288 | Line 311 | void MoleculeStamp::checkBends() {
311          
312          iter = allBends.find(bendTuple);
313          if ( iter != allBends.end()) {
314 <            std::cout << "Error in Molecule " << getName() << ": " << "Bend appears multiple times\n";
314 >            oss << "Error in Molecule " << getName() << ": " << "Bend" << containerToString(bend)<< " appears multiple times\n";
315 >            throw OOPSEException(oss.str());
316          } else {
317              allBends.insert(bendTuple);
318          }
# Line 344 | Line 368 | void MoleculeStamp::checkTorsions() {
368   }
369  
370   void MoleculeStamp::checkTorsions() {
371 +    std::ostringstream oss;
372      for(int i = 0; i < getNTorsions(); ++i) {
373          TorsionStamp* torsionStamp = getTorsionStamp(i);
374          std::vector<int> torsionAtoms =  torsionStamp ->getMembers();
375          std::vector<int>::iterator j =std::find_if(torsionAtoms.begin(), torsionAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
376 <        if (j != torsionAtoms.end()) {
377 <            std::cout << "Error in Molecule " << getName() << ": atoms of torsion" << containerToString(torsionAtoms) << " have invalid indices\n";
376 >        std::vector<int>::iterator k =std::find_if(torsionAtoms.begin(), torsionAtoms.end(), std::bind2nd(std::less<int>(), 0));
377 >
378 >        if (j != torsionAtoms.end() || k != torsionAtoms.end()) {
379 >            oss << "Error in Molecule " << getName() << ": atoms of torsion" << containerToString(torsionAtoms) << " have invalid indices\n";
380 >            throw OOPSEException(oss.str());
381          }
382 +        if (hasDuplicateElement(torsionAtoms)) {
383 +            oss << "Error in Molecule " << getName() << " : atoms of torsion" << containerToString(torsionAtoms) << " have duplicated indices\n";    
384 +            throw OOPSEException(oss.str());            
385 +        }        
386      }
387      
388      for(int i = 0; i < getNTorsions(); ++i) {
# Line 363 | Line 395 | void MoleculeStamp::checkTorsions() {
395              if (rigidbodyIndex >= 0) {
396                  ++rigidSet[rigidbodyIndex];
397                  if (rigidSet[rigidbodyIndex] > 1) {
398 <                    std::cout << "Error in Molecule " << getName() << ": torsion" << containerToString(torsionAtoms) << "is invalid\n";                  
398 >                    oss << "Error in Molecule " << getName() << ": torsion" << containerToString(torsionAtoms) << "is invalid\n";          
399 >                    throw OOPSEException(oss.str());
400                  }
401              }
402          }
# Line 392 | Line 425 | void MoleculeStamp::checkTorsions() {
425           if ( iter == allTorsions.end()) {
426              allTorsions.insert(torsionTuple);
427           } else {
428 <            std::cout << "Error in Molecule " << getName() << ": " << "Torsion appears multiple times\n";
428 >            oss << "Error in Molecule " << getName() << ": " << "Torsion" << containerToString(torsion)<< " appears multiple times\n";
429 >            throw OOPSEException(oss.str());
430           }
431       }
432  
# Line 436 | Line 470 | void MoleculeStamp::checkRigidBodies() {
470   }
471  
472   void MoleculeStamp::checkRigidBodies() {
473 +     std::ostringstream oss;
474       std::vector<RigidBodyStamp*>::iterator ri = std::find(rigidBodyStamps_.begin(), rigidBodyStamps_.end(), static_cast<RigidBodyStamp*>(NULL));
475       if (ri != rigidBodyStamps_.end()) {
476 <         std::cout << "Error in Molecule " << getName() << ":rigidBody[" <<  ri - rigidBodyStamps_.begin()<< "] is missing\n";
476 >         oss << "Error in Molecule " << getName() << ":rigidBody[" <<  ri - rigidBodyStamps_.begin()<< "] is missing\n";
477 >         throw OOPSEException(oss.str());
478       }
479  
480      for (int i = 0; i < getNRigidBodies(); ++i) {
# Line 446 | Line 482 | void MoleculeStamp::checkRigidBodies() {
482          std::vector<int> rigidAtoms =  rbStamp ->getMembers();
483          std::vector<int>::iterator j =std::find_if(rigidAtoms.begin(), rigidAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
484          if (j != rigidAtoms.end()) {
485 <            std::cout << "Error in Molecule " << getName();
485 >            oss << "Error in Molecule " << getName();
486 >            throw OOPSEException(oss.str());
487          }
488          
489      }    
490   }
491  
492   void MoleculeStamp::checkCutoffGroups() {
456
493      for(int i = 0; i < getNCutoffGroups(); ++i) {
494          CutoffGroupStamp* cutoffGroupStamp = getCutoffGroupStamp(i);
495          std::vector<int> cutoffGroupAtoms =  cutoffGroupStamp ->getMembers();
496          std::vector<int>::iterator j =std::find_if(cutoffGroupAtoms.begin(), cutoffGroupAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
497          if (j != cutoffGroupAtoms.end()) {
498 <            std::cout << "Error in Molecule " << getName() << ": cutoffGroup" << " is out of range\n";
498 >            std::ostringstream oss;
499 >            oss << "Error in Molecule " << getName() << ": cutoffGroup" << " is out of range\n";
500 >            throw OOPSEException(oss.str());
501          }
502      }    
503   }
# Line 468 | Line 506 | void MoleculeStamp::checkFragments() {
506  
507      std::vector<FragmentStamp*>::iterator fi = std::find(fragmentStamps_.begin(), fragmentStamps_.end(), static_cast<FragmentStamp*>(NULL));
508      if (fi != fragmentStamps_.end()) {
509 <        std::cout << "Error in Molecule " << getName() << ":fragment[" <<  fi - fragmentStamps_.begin()<< "] is missing\n";
509 >        std::ostringstream oss;
510 >        oss << "Error in Molecule " << getName() << ":fragment[" <<  fi - fragmentStamps_.begin()<< "] is missing\n";
511 >        throw OOPSEException(oss.str());
512      }
513      
514   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines