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> |
45 |
|
#include "types/MoleculeStamp.hpp" |
46 |
|
#include "utils/Tuple.hpp" |
47 |
< |
|
47 |
> |
#include "utils/MemoryUtils.hpp" |
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() { |
68 |
|
} |
69 |
|
|
70 |
|
MoleculeStamp::~MoleculeStamp() { |
71 |
< |
|
71 |
> |
MemoryUtils::deletePointers(atomStamps_); |
72 |
> |
MemoryUtils::deletePointers(bondStamps_); |
73 |
> |
MemoryUtils::deletePointers(bendStamps_); |
74 |
> |
MemoryUtils::deletePointers(torsionStamps_); |
75 |
> |
MemoryUtils::deletePointers(rigidBodyStamps_); |
76 |
> |
MemoryUtils::deletePointers(cutoffGroupStamps_); |
77 |
> |
MemoryUtils::deletePointers(fragmentStamps_); |
78 |
|
} |
79 |
|
|
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 |
|
|
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 |
|
} |
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 |
|
|
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 |
|
} |
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()) { |
211 |
< |
std::cout << "Error in Molecule " << getName() << " : atoms of bend" << containerToString(bendAtoms) << "have invalid indices\n"; |
212 |
< |
} |
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 |
|
|
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 |
|
} |
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 |
|
} |
328 |
|
|
329 |
|
//find bend c--a--b |
330 |
|
AtomStamp::AtomIter ai; |
331 |
< |
for(int c= atomA->getFirstBonedAtom(ai);c != -1;c = atomA->getNextBonedAtom(ai)) |
331 |
> |
for(int c= atomA->getFirstBondedAtom(ai);c != -1;c = atomA->getNextBondedAtom(ai)) |
332 |
|
{ |
333 |
|
if(b == c) |
334 |
|
continue; |
347 |
|
} |
348 |
|
|
349 |
|
//find bend a--b--c |
350 |
< |
for(int c= atomB->getFirstBonedAtom(ai);c != -1;c = atomB->getNextBonedAtom(ai)) |
350 |
> |
for(int c= atomB->getFirstBondedAtom(ai);c != -1;c = atomB->getNextBondedAtom(ai)) |
351 |
|
{ |
352 |
|
if(a == c) |
353 |
|
continue; |
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) { |
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 |
|
} |
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 |
|
|
441 |
|
AtomStamp::AtomIter ai2; |
442 |
|
AtomStamp::AtomIter ai3; |
443 |
|
|
444 |
< |
for(int a = atomB->getFirstBonedAtom(ai2);a != -1;a = atomB->getNextBonedAtom(ai2)) |
444 |
> |
for(int a = atomB->getFirstBondedAtom(ai2);a != -1;a = atomB->getNextBondedAtom(ai2)) |
445 |
|
{ |
446 |
|
if(a == c) |
447 |
|
continue; |
448 |
|
|
449 |
< |
for(int d = atomC->getFirstBonedAtom(ai3);d != -1;d = atomC->getNextBonedAtom(ai3)) |
449 |
> |
for(int d = atomC->getFirstBondedAtom(ai3);d != -1;d = atomC->getNextBondedAtom(ai3)) |
450 |
|
{ |
451 |
|
if(d == b) |
452 |
|
continue; |
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) { |
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() { |
450 |
– |
|
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 |
|
} |
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 |
|
} |