ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/ConstraintAlgorithm.cpp
Revision: 1284
Committed: Mon Jun 21 18:52:21 2004 UTC (20 years, 10 months ago) by tim
File size: 5425 byte(s)
Log Message:
roll in progress

File Contents

# Content
1 #include "ConstraintAlgorithm.hpp"
2 #include "ConstraintPair.hpp"
3 #include "SimInfo.hpp"
4 #include "ConstraintManager.hpp"
5 #include "simError.h"
6
7 ////////////////////////////////////////////////////////////////////////////////
8 //Implementation of ConstraintAlgorithm
9 ////////////////////////////////////////////////////////////////////////////////
10 ConstraintAlgorithm::ConstraintAlgorithm(SimInfo* rhs){
11 info = rhs;
12 cpIter = info->consMan->creatPairIterator();
13 ceIter = info->consMan->creatElementIterator();
14 }
15
16 ConstraintAlgorithm::~ConstraintAlgorithm(){
17 map<TypeInfo, CallbackFunctor*>::iterator callbackIter;
18
19 //destroy callbackMap
20 for(callbackIter = callbackMap.begin(); callbackIter != callbackMap.end(); ++callbackIter){
21 delete callbackIter->second;
22 }
23 callbackMap.erase(callbackMap.begin(), callbackMap.end());
24
25 //destroy iterators of constraint element and constraint pair
26 delete cpIter;
27 delete ceIter;
28 }
29
30 void ConstraintAlgorithm::doConstrain(){
31 const int maxConsIteration = 1;
32 bool done;
33 int iteration;
34 int maxIteration;
35 double tolerance;
36 ConstraintElement* consElem;
37 ConstraintPair* consPair;
38 int exeStatus;
39
40
41 error = false;
42
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 case consExceedMaxIter:
67 //cerr << "ConstraintAlgorithm::doConstrain() Error: can not constrain the bond within maximum iteration" << endl;
68 error = true;
69
70 case consFail:
71 //cerr << "ConstraintAlgorithm::doConstrain() Error: Constraint Fail" << endl;
72 error = true;
73 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 error = true;
87 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 error = true;
92 break;
93 default:
94 cerr << "ConstraintAlgorithm::doConstrain() Error: unrecognized status" << endl;
95 error = true;
96 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
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 }
118
119
120 int ConstraintAlgorithm::doConstrainPair(ConstraintPair* consPair){
121 map<TypeInfo, CallbackFunctor*>::iterator foundResult;
122 CallbackFunctor* functor;
123
124 //typeid must operate on deferenced, otherwise it will return the type_info of base class
125 foundResult = callbackMap.find(TypeInfo(typeid(*consPair)));
126 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 ceIter = rhs->consMan->creatElementIterator();
164 }
165
166 ConsAlgoFramework::~ConsAlgoFramework(){
167 delete ceIter;
168 }
169
170 void ConsAlgoFramework::doPreConstraint(){
171 ConstraintElement* consElem;
172
173 for(ceIter->first(); !ceIter->isEnd(); ceIter->next()){
174 consElem = ceIter->currentItem();
175 consElem->saveOldState();
176 }
177 }

Properties

Name Value
svn:executable *