ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/types/MoleculeStamp.cpp
Revision: 2483
Committed: Mon Dec 5 18:23:30 2005 UTC (18 years, 6 months ago) by tim
File size: 21544 byte(s)
Log Message:
adding more checking for topology

File Contents

# Content
1 /*
2 * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 *
4 * The University of Notre Dame grants you ("Licensee") a
5 * non-exclusive, royalty free, license to use, modify and
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
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * 3. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * This software is provided "AS IS," without a warranty of any
27 * kind. All express or implied conditions, representations and
28 * warranties, including any implied warranty of merchantability,
29 * fitness for a particular purpose or non-infringement, are hereby
30 * excluded. The University of Notre Dame and its licensors shall not
31 * be liable for any damages suffered by licensee as a result of
32 * using, modifying or distributing the software or its
33 * derivatives. In no event will the University of Notre Dame or its
34 * licensors be liable for any lost revenue, profit or data, or for
35 * direct, indirect, special, consequential, incidental or punitive
36 * damages, however caused and regardless of the theory of liability,
37 * arising out of the use of or inability to use software, even if the
38 * University of Notre Dame has been advised of the possibility of
39 * such damages.
40 */
41
42 #include <iostream>
43 #include <functional>
44 #include "types/MoleculeStamp.hpp"
45 #include "utils/Tuple.hpp"
46
47 namespace oopse {
48 MoleculeStamp::MoleculeStamp() {
49 DefineParameter(Name, "name");
50
51 deprecatedKeywords_.insert("nAtoms");
52 deprecatedKeywords_.insert("nBonds");
53 deprecatedKeywords_.insert("nBends");
54 deprecatedKeywords_.insert("nTorsions");
55 deprecatedKeywords_.insert("nRigidBodies");
56 deprecatedKeywords_.insert("nCutoffGroups");
57
58 }
59
60 MoleculeStamp::~MoleculeStamp() {
61
62 }
63
64 bool MoleculeStamp::addAtomStamp( AtomStamp* atom) {
65 bool ret = addIndexSensitiveStamp(atomStamps_, atom);
66 if (!ret) {
67 std::cout << "multiple atoms have the same index: " << atom->getIndex() <<" in " << getName() << " Molecule\n";
68 }
69 return ret;
70
71 }
72
73 bool MoleculeStamp::addBondStamp( BondStamp* bond) {
74 bondStamps_.push_back(bond);
75 return true;
76 }
77
78 bool MoleculeStamp::addBendStamp( BendStamp* bend) {
79 bendStamps_.push_back(bend);
80 return true;
81 }
82
83 bool MoleculeStamp::addTorsionStamp( TorsionStamp* torsion) {
84 torsionStamps_.push_back(torsion);
85 return true;
86 }
87
88 bool MoleculeStamp::addRigidBodyStamp( RigidBodyStamp* rigidbody) {
89 bool ret = addIndexSensitiveStamp(rigidBodyStamps_, rigidbody);
90 if (!ret) {
91 std::cout << "multiple rigidbodies have the same index: " << rigidbody->getIndex() <<" in " << getName() << " Molecule\n";
92 }
93 return ret;
94 }
95
96 bool MoleculeStamp::addCutoffGroupStamp( CutoffGroupStamp* cutoffgroup) {
97 cutoffGroupStamps_.push_back(cutoffgroup);
98 return true;
99 }
100
101 bool MoleculeStamp::addFragmentStamp( FragmentStamp* fragment) {
102 return addIndexSensitiveStamp(fragmentStamps_, fragment);
103 }
104
105 void MoleculeStamp::validate() {
106 DataHolder::validate();
107
108 atom2Rigidbody.resize(getNAtoms());
109 // negative number means atom is a free atom, does not belong to rigidbody
110 //every element in atom2Rigidbody has unique negative number at the very beginning
111 for(int i = 0; i < atom2Rigidbody.size(); ++i) {
112 atom2Rigidbody[i] = -1 - i;
113 }
114 for (int i = 0; i < getNRigidBodies(); ++i) {
115 RigidBodyStamp* rbStamp = getRigidBodyStamp(i);
116 std::vector<int> members = rbStamp->getMembers();
117 for(std::vector<int>::iterator j = members.begin(); j != members.end(); ++j) {
118 atom2Rigidbody[*j] = i;
119 }
120 }
121
122 checkAtoms();
123 checkBonds();
124 fillBondInfo();
125 checkBends();
126 checkTorsions();
127 checkRigidBodies();
128 checkCutoffGroups();
129 checkFragments();
130
131 int nrigidAtoms = 0;
132 for (int i = 0; i < getNRigidBodies(); ++i) {
133 RigidBodyStamp* rbStamp = getRigidBodyStamp(i);
134 nrigidAtoms += rbStamp->getNMembers();
135 }
136 nintegrable_ = getNAtoms()+ getNRigidBodies() - nrigidAtoms;
137
138 }
139
140 void MoleculeStamp::checkAtoms() {
141 std::vector<AtomStamp*>::iterator ai = std::find(atomStamps_.begin(), atomStamps_.end(), static_cast<AtomStamp*>(NULL));
142 if (ai != atomStamps_.end()) {
143 std::cout << "Error in Molecule " << getName() << ": atom[" << ai - atomStamps_.begin()<< "] is missing\n";
144 }
145
146 //make sure index is not out of range
147 int natoms = getNAtoms();
148 for(int i = 0; i < getNBonds(); ++i) {
149 BondStamp* bondStamp = getBondStamp(i);
150 if (bondStamp->getA() >= natoms && bondStamp->getB() >= natoms) {
151 std::cout << "Error in Molecule " << getName() << ": bond between " << bondStamp->getA() << " and " << bondStamp->getB() << " is invalid\n";
152 }
153 }
154 }
155
156 void MoleculeStamp::checkBonds() {
157 //make sure bonds are unique
158 std::set<std::pair<int, int> > allBonds;
159 for(int i = 0; i < getNBonds(); ++i) {
160 BondStamp* bondStamp= getBondStamp(i);
161 std::pair<int, int> bondPair(bondStamp->getA(), bondStamp->getB());
162 //make sure bondTuple.first is always less than or equal to bondTuple.third
163 if (bondPair.first > bondPair.second) {
164 std::swap(bondPair.first, bondPair.second);
165 }
166
167 std::set<std::pair<int, int> >::iterator iter = allBonds.find(bondPair);
168 if ( iter != allBonds.end()) {
169 std::cout << "Error in Molecule " << getName() << ": " << "Bond appears multiple times\n";
170 } else {
171 allBonds.insert(bondPair);
172 }
173 }
174
175 //make sure atoms belong to same rigidbody do not bond to each other
176 for(int i = 0; i < getNBonds(); ++i) {
177 BondStamp* bondStamp = getBondStamp(i);
178 if (atom2Rigidbody[bondStamp->getA()] == atom2Rigidbody[bondStamp->getB()]) {
179 std::cout << "Error in Molecule " << getName() << ": "<<"bond between " << bondStamp->getA() << " and " << bondStamp->getB() << " belong to same rigidbody " << atom2Rigidbody[bondStamp->getA()] << "\n";
180 }
181 }
182
183 }
184
185 struct BendLessThan : public std::binary_function<IntTuple4, IntTuple4, bool> {
186 bool operator()(IntTuple3 b1, IntTuple3 b2) {
187 return b1.first < b2.first
188 || (!(b2.first < b1.first) && b1.second < b2.second)
189 || (!(b2.first < b1.first) && !(b2.second < b2.second) && b1.third < b2.third);
190 }
191 };
192
193 void MoleculeStamp::checkBends() {
194 for(int i = 0; i < getNBends(); ++i) {
195 BendStamp* bendStamp = getBendStamp(i);
196 std::vector<int> bendAtoms = bendStamp->getMembers();
197 std::vector<int>::iterator j =std::find_if(bendAtoms.begin(), bendAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
198 if (j != bendAtoms.end()) {
199 std::cout << "Error in Molecule " << getName();
200 }
201
202 if (bendAtoms.size() == 2 ) {
203 if (!bendStamp->haveGhostVectorSource()) {
204 std::cout << "Error in Molecule " << getName() << ": ghostVectorSouce is missing\n";
205 }else{
206 int ghostIndex = bendStamp->getGhostVectorSource();
207 if (ghostIndex < getNAtoms()) {
208 if (std::find(bendAtoms.begin(), bendAtoms.end(), ghostIndex) == bendAtoms.end()) {
209 std::cout << "Error in Molecule " << getName() << ": ghostVectorSouce "<< ghostIndex<<"is invalid\n";
210 }
211 if (!getAtomStamp(ghostIndex)->haveOrientation()) {
212 std::cout << "Error in Molecule " << getName() << ": ghost atom must be a directioanl atom\n";
213 }
214 }else {
215 std::cout << "Error in Molecule " << getName() << ": ghostVectorsource " << ghostIndex<< " is invalid\n";
216 }
217 }
218 } else if (bendAtoms.size() == 3 && bendStamp->haveGhostVectorSource()) {
219 std::cout << "Error in Molecule " << getName() << ": normal bend should not have ghostVectorSouce\n";
220 }
221 }
222
223 for(int i = 0; i < getNBends(); ++i) {
224 BendStamp* bendStamp = getBendStamp(i);
225 std::vector<int> bendAtoms = bendStamp->getMembers();
226 std::vector<int> rigidSet(getNRigidBodies(), 0);
227 std::vector<int>::iterator j;
228 for( j = bendAtoms.begin(); j != bendAtoms.end(); ++j) {
229 int rigidbodyIndex = atom2Rigidbody[*j];
230 if (rigidbodyIndex >= 0) {
231 ++rigidSet[rigidbodyIndex];
232 if (rigidSet[rigidbodyIndex] > 1) {
233 std::cout << "Error in Molecule " << getName() << ": ";
234 //std::cout << "atoms of bend " << << "belong to same rigidbody " << rigidbodyIndex << "\n";
235 }
236 }
237 }
238 }
239
240
241 std::set<IntTuple3, BendLessThan> allBends;
242 std::set<IntTuple3, BendLessThan>::iterator iter;
243 for(int i = 0; i < getNBends(); ++i) {
244 BendStamp* bendStamp= getBendStamp(i);
245 std::vector<int> bend = bendStamp->getMembers();
246 if (bend.size() == 2) {
247 // in case we have two ghost bend. For example,
248 // bend {
249 // members (0, 1);
250 // ghostVectorSource = 0;
251 // }
252 // and
253 // bend {
254 // members (0, 1);
255 // ghostVectorSource = 0;
256 // }
257 // In order to distinguish them. we expand them to Tuple3.
258 // the first one is expanded to (0, 0, 1) while the second one is expaned to (0, 1, 1)
259 int ghostIndex = bendStamp->getGhostVectorSource();
260 std::vector<int>::iterator j = std::find(bend.begin(), bend.end(), ghostIndex);
261 if (j != bend.end()) {
262 bend.insert(j, ghostIndex);
263 }
264 }
265
266 IntTuple3 bendTuple(bend[0], bend[1], bend[2]);
267 //make sure bendTuple.first is always less than or equal to bendTuple.third
268 if (bendTuple.first > bendTuple.third) {
269 std::swap(bendTuple.first, bendTuple.third);
270 }
271
272 iter = allBends.find(bendTuple);
273 if ( iter != allBends.end()) {
274 std::cout << "Error in Molecule " << getName() << ": " << "Bend appears multiple times\n";
275 } else {
276 allBends.insert(bendTuple);
277 }
278 }
279
280 for (int i = 0; i < getNBonds(); ++i) {
281 BondStamp* bondStamp = getBondStamp(i);
282 int a = bondStamp->getA();
283 int b = bondStamp->getB();
284
285 AtomStamp* atomA = getAtomStamp(a);
286 AtomStamp* atomB = getAtomStamp(b);
287
288 //find bend c--a--b
289 AtomStamp::AtomIter ai;
290 for(int c= atomA->getFirstBonedAtom(ai);c != -1;c = atomA->getNextBonedAtom(ai))
291 {
292 if(b == c)
293 continue;
294
295 IntTuple3 newBend(c, a, b);
296 if (newBend.first > newBend.third) {
297 std::swap(newBend.first, newBend.third);
298 }
299
300 if (allBends.find(newBend) == allBends.end() ) {
301 allBends.insert(newBend);
302 BendStamp * newBendStamp = new BendStamp();
303 newBendStamp->setMembers(newBend);
304 addBendStamp(newBendStamp);
305 }
306 }
307
308 //find bend a--b--c
309 for(int c= atomB->getFirstBonedAtom(ai);c != -1;c = atomB->getNextBonedAtom(ai))
310 {
311 if(a == c)
312 continue;
313
314 IntTuple3 newBend( a, b, c);
315 if (newBend.first > newBend.third) {
316 std::swap(newBend.first, newBend.third);
317 }
318 if (allBends.find(newBend) == allBends.end() ) {
319 allBends.insert(newBend);
320 BendStamp * newBendStamp = new BendStamp();
321 newBendStamp->setMembers(newBend);
322 addBendStamp(newBendStamp);
323 }
324 }
325 }
326
327 }
328
329 struct TorsionLessThan : public std::binary_function<IntTuple4, IntTuple4, bool> {
330 bool operator()(IntTuple4 t1, IntTuple4 t2) {
331
332 return t1.first < t2.first
333 || (!(t2.first < t1.first) && t1.second < t2.second)
334 || (!(t2.first < t1.first) && !(t2.second < t2.second) && t1.third < t2.third)
335 ||(!(t2.first < t1.first) && !(t2.second < t2.second) && !(t2.third < t1.third) && t1.fourth < t2.fourth);
336 }
337
338
339
340 };
341
342
343 void MoleculeStamp::checkTorsions() {
344 for(int i = 0; i < getNTorsions(); ++i) {
345 TorsionStamp* torsionStamp = getTorsionStamp(i);
346 std::vector<int> torsionAtoms = torsionStamp->getMembers();
347 std::vector<int> rigidSet(getNRigidBodies(), 0);
348 std::vector<int>::iterator j;
349 for( j = torsionAtoms.begin(); j != torsionAtoms.end(); ++j) {
350 int rigidbodyIndex = atom2Rigidbody[*j];
351 if (rigidbodyIndex >= 0) {
352 ++rigidSet[rigidbodyIndex];
353 if (rigidSet[rigidbodyIndex] > 1) {
354 std::cout << "Error in Molecule " << getName() << ": ";
355 //std::cout << "atoms of torsion " << << "belong to same rigidbody " << rigidbodyIndex << "\n";
356 }
357 }
358 }
359 }
360
361 std::set<IntTuple4, TorsionLessThan> allTorsions;
362 std::set<IntTuple4, TorsionLessThan>::iterator iter;
363 for(int i = 0; i < getNTorsions(); ++i) {
364 TorsionStamp* torsionStamp= getTorsionStamp(i);
365 std::vector<int> torsion = torsionStamp->getMembers();
366 if (torsion.size() == 3) {
367 int ghostIndex = torsionStamp->getGhostVectorSource();
368 std::vector<int>::iterator j = std::find(torsion.begin(), torsion.end(), ghostIndex);
369 if (j != torsion.end()) {
370 torsion.insert(j, ghostIndex);
371 }
372 }
373
374 IntTuple4 torsionTuple(torsion[0], torsion[1], torsion[2], torsion[3]);
375 if (torsionTuple.first > torsionTuple.fourth) {
376 std::swap(torsionTuple.first, torsionTuple.fourth);
377 std::swap(torsionTuple.second, torsionTuple.third);
378 }
379
380 iter = allTorsions.find(torsionTuple);
381 if ( iter == allTorsions.end()) {
382 allTorsions.insert(torsionTuple);
383 } else {
384 std::cout << "Error in Molecule " << getName() << ": " << "Torsion appears multiple times\n";
385 }
386 }
387
388 for (int i = 0; i < getNBonds(); ++i) {
389 BondStamp* bondStamp = getBondStamp(i);
390 int b = bondStamp->getA();
391 int c = bondStamp->getB();
392
393 AtomStamp* atomB = getAtomStamp(b);
394 AtomStamp* atomC = getAtomStamp(c);
395
396 AtomStamp::AtomIter ai2;
397 AtomStamp::AtomIter ai3;
398
399 for(int a = atomB->getFirstBonedAtom(ai2);a != -1;a = atomB->getNextBonedAtom(ai2))
400 {
401 if(a == c)
402 continue;
403
404 for(int d = atomC->getFirstBonedAtom(ai3);d != -1;d = atomC->getNextBonedAtom(ai3))
405 {
406 if(d == b)
407 continue;
408
409 IntTuple4 newTorsion(a, b, c, d);
410 //make sure the first element is always less than or equal to the fourth element in IntTuple4
411 if (newTorsion.first > newTorsion.fourth) {
412 std::swap(newTorsion.first, newTorsion.fourth);
413 std::swap(newTorsion.second, newTorsion.third);
414 }
415 if (allTorsions.find(newTorsion) == allTorsions.end() ) {
416 allTorsions.insert(newTorsion);
417 TorsionStamp * newTorsionStamp = new TorsionStamp();
418 newTorsionStamp->setMembers(newTorsion);
419 addTorsionStamp(newTorsionStamp);
420 }
421 }
422 }
423 }
424
425 }
426
427 void MoleculeStamp::checkRigidBodies() {
428 std::vector<RigidBodyStamp*>::iterator ri = std::find(rigidBodyStamps_.begin(), rigidBodyStamps_.end(), static_cast<RigidBodyStamp*>(NULL));
429 if (ri != rigidBodyStamps_.end()) {
430 std::cout << "Error in Molecule " << getName() << ":rigidBody[" << ri - rigidBodyStamps_.begin()<< "] is missing\n";
431 }
432
433 for (int i = 0; i < getNRigidBodies(); ++i) {
434 RigidBodyStamp* rbStamp = getRigidBodyStamp(i);
435 std::vector<int> rigidAtoms = rbStamp ->getMembers();
436 std::vector<int>::iterator j =std::find_if(rigidAtoms.begin(), rigidAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
437 if (j != rigidAtoms.end()) {
438 std::cout << "Error in Molecule " << getName();
439 }
440
441 }
442 }
443
444 void MoleculeStamp::checkCutoffGroups() {
445
446 for(int i = 0; i < getNCutoffGroups(); ++i) {
447 CutoffGroupStamp* cutoffGroupStamp = getCutoffGroupStamp(i);
448 std::vector<int> cutoffGroupAtoms = cutoffGroupStamp ->getMembers();
449 std::vector<int>::iterator j =std::find_if(cutoffGroupAtoms.begin(), cutoffGroupAtoms.end(), std::bind2nd(std::greater<int>(), getNAtoms()-1));
450 if (j != cutoffGroupAtoms.end()) {
451 std::cout << "Error in Molecule " << getName() << ": cutoffGroup" << " is out of range\n";
452 }
453 }
454 }
455
456 void MoleculeStamp::checkFragments() {
457
458 std::vector<FragmentStamp*>::iterator fi = std::find(fragmentStamps_.begin(), fragmentStamps_.end(), static_cast<FragmentStamp*>(NULL));
459 if (fi != fragmentStamps_.end()) {
460 std::cout << "Error in Molecule " << getName() << ":fragment[" << fi - fragmentStamps_.begin()<< "] is missing\n";
461 }
462
463 }
464
465 void MoleculeStamp::fillBondInfo() {
466
467 for (int i = 0; i < getNBonds(); ++i) {
468 BondStamp* bondStamp = getBondStamp(i);
469 int a = bondStamp->getA();
470 int b = bondStamp->getB();
471 AtomStamp* atomA = getAtomStamp(a);
472 AtomStamp* atomB = getAtomStamp(b);
473 atomA->addBond(i);
474 atomA->addBondedAtom(b);
475 atomB->addBond(i);
476 atomB->addBondedAtom(a);
477
478 }
479 }
480
481
482
483 //Function Name: isBondInSameRigidBody
484 //Return true is both atoms of the bond belong to the same rigid body, otherwise return false
485 bool MoleculeStamp::isBondInSameRigidBody(BondStamp* bond){
486 int rbA;
487 int rbB;
488 int consAtomA;
489 int consAtomB;
490
491 if (!isAtomInRigidBody(bond->getA(),rbA, consAtomA))
492 return false;
493
494 if(!isAtomInRigidBody(bond->getB(),rbB, consAtomB) )
495 return false;
496
497 if(rbB == rbA)
498 return true;
499 else
500 return false;
501 }
502
503 // Function Name: isAtomInRigidBody
504 //return false if atom does not belong to a rigid body, otherwise return true
505 bool MoleculeStamp::isAtomInRigidBody(int atomIndex){
506 int whichRigidBody;
507 int consAtomIndex;
508
509 return isAtomInRigidBody(atomIndex, whichRigidBody, consAtomIndex);
510
511 }
512
513 // Function Name: isAtomInRigidBody
514 //return false if atom does not belong to a rigid body otherwise return true and set whichRigidBody
515 //and consAtomIndex
516 //atomIndex : the index of atom in component
517 //whichRigidBody: the index of rigidbody in component
518 //consAtomIndex: the position of joint atom apears in rigidbody's definition
519 bool MoleculeStamp::isAtomInRigidBody(int atomIndex, int& whichRigidBody, int& consAtomIndex){
520 RigidBodyStamp* rbStamp;
521 int numRb;
522 int numAtom;
523
524 whichRigidBody = -1;
525 consAtomIndex = -1;
526
527 numRb = this->getNRigidBodies();
528
529 for(int i = 0 ; i < numRb; i++){
530 rbStamp = this->getRigidBodyStamp(i);
531 numAtom = rbStamp->getNMembers();
532 for(int j = 0; j < numAtom; j++)
533 if (rbStamp->getMemberAt(j) == atomIndex){
534 whichRigidBody = i;
535 consAtomIndex = j;
536 return true;
537 }
538 }
539
540 return false;
541
542 }
543
544 //return the position of joint atom apears in rigidbody's definition
545 //for the time being, we will use the most inefficient algorithm, the complexity is O(N2)
546 //actually we could improve the complexity to O(NlgN) by sorting the atom index in rigid body first
547 std::vector<std::pair<int, int> > MoleculeStamp::getJointAtoms(int rb1, int rb2){
548 RigidBodyStamp* rbStamp1;
549 RigidBodyStamp* rbStamp2;
550 int natomInRb1;
551 int natomInRb2;
552 int atomIndex1;
553 int atomIndex2;
554 std::vector<std::pair<int, int> > jointAtomIndexPair;
555
556 rbStamp1 = this->getRigidBodyStamp(rb1);
557 natomInRb1 =rbStamp1->getNMembers();
558
559 rbStamp2 = this->getRigidBodyStamp(rb2);
560 natomInRb2 =rbStamp2->getNMembers();
561
562 for(int i = 0; i < natomInRb1; i++){
563 atomIndex1 = rbStamp1->getMemberAt(i);
564
565 for(int j= 0; j < natomInRb1; j++){
566 atomIndex2 = rbStamp2->getMemberAt(j);
567
568 if(atomIndex1 == atomIndex2){
569 jointAtomIndexPair.push_back(std::make_pair(i, j));
570 break;
571 }
572
573 }
574
575 }
576
577 return jointAtomIndexPair;
578 }
579
580 }