| 1 |
tim |
1232 |
#include "ConstraintAlgorithm.hpp" |
| 2 |
|
|
#include "ConstraintPair.hpp" |
| 3 |
|
|
#include "SimInfo.hpp" |
| 4 |
|
|
#include "ConstraintManager.hpp" |
| 5 |
tim |
1248 |
#include "simError.h" |
| 6 |
tim |
1452 |
#include "Vector3d.hpp" |
| 7 |
tim |
1232 |
|
| 8 |
|
|
//////////////////////////////////////////////////////////////////////////////// |
| 9 |
|
|
//Implementation of ConstraintAlgorithm |
| 10 |
|
|
//////////////////////////////////////////////////////////////////////////////// |
| 11 |
|
|
ConstraintAlgorithm::ConstraintAlgorithm(SimInfo* rhs){ |
| 12 |
|
|
info = rhs; |
| 13 |
tim |
1452 |
cpIter = info->consMan->createPairIterator(); |
| 14 |
|
|
ceIter = info->consMan->createElementIterator(); |
| 15 |
tim |
1232 |
} |
| 16 |
|
|
|
| 17 |
|
|
ConstraintAlgorithm::~ConstraintAlgorithm(){ |
| 18 |
|
|
map<TypeInfo, CallbackFunctor*>::iterator callbackIter; |
| 19 |
|
|
|
| 20 |
|
|
//destroy callbackMap |
| 21 |
|
|
for(callbackIter = callbackMap.begin(); callbackIter != callbackMap.end(); ++callbackIter){ |
| 22 |
|
|
delete callbackIter->second; |
| 23 |
|
|
} |
| 24 |
|
|
callbackMap.erase(callbackMap.begin(), callbackMap.end()); |
| 25 |
|
|
|
| 26 |
|
|
//destroy iterators of constraint element and constraint pair |
| 27 |
|
|
delete cpIter; |
| 28 |
|
|
delete ceIter; |
| 29 |
|
|
} |
| 30 |
|
|
|
| 31 |
|
|
void ConstraintAlgorithm::doConstrain(){ |
| 32 |
tim |
1452 |
const int maxConsIteration = 20; |
| 33 |
tim |
1232 |
bool done; |
| 34 |
|
|
int iteration; |
| 35 |
|
|
int maxIteration; |
| 36 |
|
|
double tolerance; |
| 37 |
|
|
ConstraintElement* consElem; |
| 38 |
|
|
ConstraintPair* consPair; |
| 39 |
|
|
int exeStatus; |
| 40 |
|
|
|
| 41 |
tim |
1248 |
error = false; |
| 42 |
tim |
1232 |
|
| 43 |
|
|
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
| 44 |
|
|
consElem = ceIter->currentItem(); |
| 45 |
|
|
consElem->setMoved(true); |
| 46 |
|
|
consElem->setMoving(false); |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
|
done = false; |
| 50 |
|
|
iteration = 0; |
| 51 |
|
|
|
| 52 |
|
|
//main loop of constraint algorithm |
| 53 |
|
|
while(!done && iteration < maxConsIteration){ |
| 54 |
|
|
done = true; |
| 55 |
|
|
|
| 56 |
|
|
//loop over every constraint pair |
| 57 |
|
|
for(cpIter->first(); !cpIter->isEnd(); cpIter->next()){ |
| 58 |
|
|
|
| 59 |
|
|
consPair = cpIter->currentItem(); |
| 60 |
|
|
|
| 61 |
|
|
//dispatch constraint algorithm |
| 62 |
|
|
if(consPair->isMoved()) |
| 63 |
|
|
exeStatus = doConstrainPair(consPair); |
| 64 |
|
|
|
| 65 |
|
|
switch(exeStatus){ |
| 66 |
tim |
1268 |
case consExceedMaxIter: |
| 67 |
tim |
1284 |
//cerr << "ConstraintAlgorithm::doConstrain() Error: can not constrain the bond within maximum iteration" << endl; |
| 68 |
tim |
1268 |
error = true; |
| 69 |
|
|
|
| 70 |
tim |
1232 |
case consFail: |
| 71 |
tim |
1284 |
//cerr << "ConstraintAlgorithm::doConstrain() Error: Constraint Fail" << endl; |
| 72 |
tim |
1248 |
error = true; |
| 73 |
tim |
1232 |
break; |
| 74 |
|
|
case consSuccess: |
| 75 |
|
|
//constrain the pair by moving two elements |
| 76 |
|
|
done = false; |
| 77 |
|
|
consPair->firstElem->setMoving(true); |
| 78 |
|
|
consPair->secondElem->setMoving(true); |
| 79 |
|
|
break; |
| 80 |
|
|
case consAlready: |
| 81 |
|
|
//current pair is already constrained, do not need to move the elements |
| 82 |
|
|
break; |
| 83 |
|
|
case consPairHandlerFail: |
| 84 |
|
|
//can not found call back functor for constraint pair |
| 85 |
|
|
cerr << "ConstraintAlgorithm::doConstrain() Error: can not found callback functor for constraint pair " << endl; |
| 86 |
tim |
1248 |
error = true; |
| 87 |
tim |
1232 |
break; |
| 88 |
|
|
case consElemHandlerFail: |
| 89 |
|
|
//can not found callback functor for constraint element |
| 90 |
|
|
cerr << "ConstraintAlgorithm::doConstrain() Error: can not found callback functor for constraint element " << endl; |
| 91 |
tim |
1248 |
error = true; |
| 92 |
tim |
1232 |
break; |
| 93 |
|
|
default: |
| 94 |
|
|
cerr << "ConstraintAlgorithm::doConstrain() Error: unrecognized status" << endl; |
| 95 |
tim |
1248 |
error = true; |
| 96 |
tim |
1232 |
break; |
| 97 |
|
|
} |
| 98 |
|
|
}//end for(iter->first()) |
| 99 |
|
|
|
| 100 |
|
|
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
| 101 |
|
|
consElem = ceIter->currentItem(); |
| 102 |
|
|
consElem->setMoved(consElem->getMoving()); |
| 103 |
|
|
consElem->setMoving(false); |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
iteration++; |
| 107 |
|
|
}//end while |
| 108 |
tim |
1248 |
|
| 109 |
|
|
//if (!done){ |
| 110 |
|
|
// error = true; |
| 111 |
|
|
// sprintf(painCave.errMsg, |
| 112 |
|
|
// "Constraint failure in constrainB, too many iterations: %d\n", |
| 113 |
|
|
// iteration); |
| 114 |
|
|
// painCave.isFatal = 1; |
| 115 |
|
|
// simError(); |
| 116 |
|
|
//} |
| 117 |
tim |
1232 |
} |
| 118 |
|
|
|
| 119 |
|
|
|
| 120 |
|
|
int ConstraintAlgorithm::doConstrainPair(ConstraintPair* consPair){ |
| 121 |
|
|
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
| 122 |
|
|
CallbackFunctor* functor; |
| 123 |
|
|
|
| 124 |
tim |
1234 |
//typeid must operate on deferenced, otherwise it will return the type_info of base class |
| 125 |
|
|
foundResult = callbackMap.find(TypeInfo(typeid(*consPair))); |
| 126 |
tim |
1232 |
if (foundResult != callbackMap.end()){ |
| 127 |
|
|
functor = foundResult->second; |
| 128 |
|
|
return (*functor)(consPair); |
| 129 |
|
|
} |
| 130 |
|
|
else{ |
| 131 |
|
|
//can not found appropriate functor to handle constraint pair in callbackMap |
| 132 |
|
|
return consPairHandlerFail; |
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
} |
| 136 |
|
|
|
| 137 |
|
|
void ConstraintAlgorithm::registerCallback(const TypeInfo& ti, CallbackFunctor* functor){ |
| 138 |
|
|
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
| 139 |
|
|
|
| 140 |
|
|
foundResult = callbackMap.find(ti); |
| 141 |
|
|
if (foundResult == callbackMap.end()) |
| 142 |
|
|
callbackMap[ti] = functor; |
| 143 |
|
|
else{ |
| 144 |
|
|
delete foundResult->second; |
| 145 |
|
|
foundResult->second = functor; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
|
} |
| 149 |
|
|
void ConstraintAlgorithm::unRegister(TypeInfo& ti){ |
| 150 |
|
|
map<TypeInfo, CallbackFunctor*>::iterator foundResult; |
| 151 |
|
|
|
| 152 |
|
|
foundResult = callbackMap.find(ti); |
| 153 |
|
|
if (foundResult != callbackMap.end()){ |
| 154 |
|
|
delete foundResult->second; |
| 155 |
|
|
callbackMap.erase(foundResult); |
| 156 |
|
|
} |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
|
|
//////////////////////////////////////////////////////////////////////////////// |
| 160 |
|
|
//Implementation of ConsAlgoFramework |
| 161 |
|
|
//////////////////////////////////////////////////////////////////////////////// |
| 162 |
|
|
ConsAlgoFramework::ConsAlgoFramework(SimInfo* rhs){ |
| 163 |
tim |
1452 |
ceIter = rhs->consMan->createElementIterator(); |
| 164 |
tim |
1232 |
} |
| 165 |
|
|
|
| 166 |
|
|
ConsAlgoFramework::~ConsAlgoFramework(){ |
| 167 |
|
|
delete ceIter; |
| 168 |
|
|
} |
| 169 |
tim |
1234 |
|
| 170 |
tim |
1232 |
void ConsAlgoFramework::doPreConstraint(){ |
| 171 |
|
|
ConstraintElement* consElem; |
| 172 |
tim |
1452 |
Vector3d zeroVector(0.0, 0.0, 0.0); |
| 173 |
tim |
1232 |
|
| 174 |
|
|
for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){ |
| 175 |
|
|
consElem = ceIter->currentItem(); |
| 176 |
|
|
consElem->saveOldState(); |
| 177 |
tim |
1452 |
consElem->setConsForce(zeroVector); |
| 178 |
|
|
consElem->setConsTorque(zeroVector); |
| 179 |
tim |
1234 |
} |
| 180 |
|
|
} |