ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/Integrator.hpp
Revision: 1064
Committed: Tue Feb 24 15:44:45 2004 UTC (20 years, 4 months ago) by tim
File size: 13363 byte(s)
Log Message:
Using inherit instead of compose to implement Minimizer
both versions are working

File Contents

# User Rev Content
1 mmeineke 377 #ifndef _INTEGRATOR_H_
2     #define _INTEGRATOR_H_
3    
4 tim 658 #include <string>
5     #include <vector>
6 mmeineke 377 #include "Atom.hpp"
7 gezelter 604 #include "Molecule.hpp"
8 mmeineke 377 #include "SRI.hpp"
9     #include "AbstractClasses.hpp"
10     #include "SimInfo.hpp"
11     #include "ForceFields.hpp"
12 mmeineke 540 #include "Thermo.hpp"
13     #include "ReadWrite.hpp"
14 tim 658 #include "ZConsWriter.hpp"
15 mmeineke 377
16 tim 658 using namespace std;
17 mmeineke 561 const double kB = 8.31451e-7;// boltzmann constant amu*Ang^2*fs^-2/K
18     const double eConvert = 4.184e-4; // converts kcal/mol -> amu*A^2/fs^2
19 mmeineke 586 const double p_convert = 1.63882576e8; //converts amu*fs^-2*Ang^-1 -> atm
20 mmeineke 561 const int maxIteration = 300;
21     const double tol = 1.0e-6;
22    
23 mmeineke 377
24 tim 645 template<typename T = BaseIntegrator> class Integrator : public T {
25    
26 mmeineke 377 public:
27 mmeineke 561 Integrator( SimInfo *theInfo, ForceFields* the_ff );
28 mmeineke 548 virtual ~Integrator();
29 mmeineke 377 void integrate( void );
30 tim 763 virtual double getConservedQuantity(void);
31 tim 837 virtual string getAdditionalParameters(void);
32 mmeineke 377
33 mmeineke 540 protected:
34 mmeineke 778
35 mmeineke 540 virtual void integrateStep( int calcPot, int calcStress );
36 mmeineke 548 virtual void preMove( void );
37 mmeineke 540 virtual void moveA( void );
38     virtual void moveB( void );
39     virtual void constrainA( void );
40     virtual void constrainB( void );
41 mmeineke 559 virtual int readyCheck( void ) { return 1; }
42 tim 677
43 mmeineke 746 virtual void resetIntegrator( void ) { }
44 tim 837
45     virtual void calcForce( int calcPot, int calcStress );
46 tim 677 virtual void thermalize();
47 tim 837
48 mmeineke 778 virtual void rotationPropagation( DirectionalAtom* dAtom, double ji[3] );
49    
50 mmeineke 540 void checkConstraints( void );
51 tim 837 void rotate( int axes1, int axes2, double angle, double j[3],
52 tim 701 double A[3][3] );
53 tim 837
54 mmeineke 377 ForceFields* myFF;
55    
56 mmeineke 540 SimInfo *info; // all the info we'll ever need
57     int nAtoms; /* the number of atoms */
58 mmeineke 548 int oldAtoms;
59 mmeineke 540 Atom **atoms; /* array of atom pointers */
60 mmeineke 423 Molecule* molecules;
61     int nMols;
62    
63 mmeineke 548 int isConstrained; // boolean to know whether the systems contains
64 tim 701 // constraints.
65 tim 837 int nConstrained; // counter for number of constraints
66     int *constrainedA; // the i of a constraint pair
67     int *constrainedB; // the j of a constraint pair
68     double *constrainedDsqr; // the square of the constraint distance
69    
70 mmeineke 548 int* moving; // tells whether we are moving atom i
71     int* moved; // tells whether we have moved atom i
72 tim 837 double* oldPos; // pre constrained positions
73 mmeineke 548
74 mmeineke 540 short isFirst; /*boolean for the first time integrate is called */
75 tim 837
76 mmeineke 540 double dt;
77 mmeineke 541 double dt2;
78 gezelter 560
79 mmeineke 540 Thermo *tStats;
80     StatWriter* statOut;
81     DumpWriter* dumpOut;
82 tim 837
83 mmeineke 377 };
84    
85 tim 645 typedef Integrator<BaseIntegrator> RealIntegrator;
86 mmeineke 540
87 tim 645 template<typename T> class NVE : public T {
88    
89 mmeineke 561 public:
90     NVE ( SimInfo *theInfo, ForceFields* the_ff ):
91 tim 645 T( theInfo, the_ff ){}
92 tim 837 virtual ~NVE(){}
93 tim 645 };
94 mmeineke 555
95    
96 tim 645 template<typename T> class NVT : public T {
97 mmeineke 555
98 gezelter 560 public:
99    
100 mmeineke 561 NVT ( SimInfo *theInfo, ForceFields* the_ff);
101 tim 763 virtual ~NVT();
102 mmeineke 540
103 gezelter 560 void setTauThermostat(double tt) {tauThermostat = tt; have_tau_thermostat=1;}
104     void setTargetTemp(double tt) {targetTemp = tt; have_target_temp = 1;}
105 tim 763 void setChiTolerance(double tol) {chiTolerance = tol;}
106     virtual double getConservedQuantity(void);
107 tim 837 virtual string getAdditionalParameters(void);
108 gezelter 560
109 mmeineke 540 protected:
110 gezelter 560
111 mmeineke 561 virtual void moveA( void );
112     virtual void moveB( void );
113 mmeineke 540
114 mmeineke 561 virtual int readyCheck();
115 gezelter 560
116 mmeineke 746 virtual void resetIntegrator( void );
117    
118 gezelter 565 // chi is a propagated degree of freedom.
119 gezelter 560
120 gezelter 565 double chi;
121 gezelter 560
122 tim 763 //integral of chi(t)dt
123     double integralOfChidt;
124    
125 gezelter 565 // targetTemp must be set. tauThermostat must also be set;
126 gezelter 560
127     double targetTemp;
128     double tauThermostat;
129 tim 837
130 gezelter 565 short int have_tau_thermostat, have_target_temp;
131 gezelter 560
132 tim 763 double *oldVel;
133     double *oldJi;
134    
135     double chiTolerance;
136     short int have_chi_tolerance;
137    
138 mmeineke 540 };
139    
140    
141    
142 mmeineke 778 template<typename T> class NPT : public T{
143 tim 645
144 gezelter 560 public:
145    
146 mmeineke 778 NPT ( SimInfo *theInfo, ForceFields* the_ff);
147     virtual ~NPT();
148 tim 837
149 mmeineke 594 virtual void integrateStep( int calcPot, int calcStress ){
150     calcStress = 1;
151 tim 645 T::integrateStep( calcPot, calcStress );
152 mmeineke 594 }
153    
154 mmeineke 778 virtual double getConservedQuantity(void) = 0;
155 tim 837 virtual string getAdditionalParameters(void) = 0;
156 mmeineke 843
157     double myTauThermo( void ) { return tauThermostat; }
158     double myTauBaro( void ) { return tauBarostat; }
159    
160 gezelter 560 void setTauThermostat(double tt) {tauThermostat = tt; have_tau_thermostat=1;}
161     void setTauBarostat(double tb) {tauBarostat = tb; have_tau_barostat=1;}
162     void setTargetTemp(double tt) {targetTemp = tt; have_target_temp = 1;}
163     void setTargetPressure(double tp) {targetPressure = tp; have_target_pressure = 1;}
164 tim 763 void setChiTolerance(double tol) {chiTolerance = tol; have_chi_tolerance = 1;}
165     void setPosIterTolerance(double tol) {posIterTolerance = tol; have_pos_iter_tolerance = 1;}
166     void setEtaTolerance(double tol) {etaTolerance = tol; have_eta_tolerance = 1;}
167 gezelter 560
168     protected:
169    
170 mmeineke 561 virtual void moveA( void );
171     virtual void moveB( void );
172 gezelter 560
173 mmeineke 561 virtual int readyCheck();
174 gezelter 560
175 mmeineke 746 virtual void resetIntegrator( void );
176    
177 mmeineke 778 virtual void getVelScaleA( double sc[3], double vel[3] ) = 0;
178     virtual void getVelScaleB( double sc[3], int index ) = 0;
179 tim 837 virtual void getPosScale(double pos[3], double COM[3],
180 mmeineke 778 int index, double sc[3]) = 0;
181    
182 mmeineke 857 virtual void calcVelScale( void ) = 0;
183    
184 mmeineke 778 virtual bool chiConverged( void );
185     virtual bool etaConverged( void ) = 0;
186 tim 837
187 mmeineke 778 virtual void evolveChiA( void );
188     virtual void evolveEtaA( void ) = 0;
189     virtual void evolveChiB( void );
190     virtual void evolveEtaB( void ) = 0;
191    
192     virtual void scaleSimBox( void ) = 0;
193    
194 tim 763 void accIntegralOfChidt(void) { integralOfChidt += dt * chi;}
195    
196 gezelter 565 // chi and eta are the propagated degrees of freedom
197 gezelter 560
198 mmeineke 778 double oldChi;
199     double prevChi;
200 gezelter 565 double chi;
201 gezelter 574 double NkBT;
202 tim 763 double fkBT;
203 gezelter 560
204 mmeineke 778 double tt2, tb2;
205     double instaTemp, instaPress, instaVol;
206 mmeineke 780 double press[3][3];
207 mmeineke 778
208 tim 763 int Nparticles;
209    
210     double integralOfChidt;
211    
212 tim 837 // targetTemp, targetPressure, and tauBarostat must be set.
213 gezelter 560 // One of qmass or tauThermostat must be set;
214    
215     double targetTemp;
216     double targetPressure;
217     double tauThermostat;
218     double tauBarostat;
219    
220     short int have_tau_thermostat, have_tau_barostat, have_target_temp;
221 gezelter 565 short int have_target_pressure;
222 gezelter 560
223 tim 763 double *oldPos;
224     double *oldVel;
225     double *oldJi;
226    
227     double chiTolerance;
228     short int have_chi_tolerance;
229     double posIterTolerance;
230     short int have_pos_iter_tolerance;
231     double etaTolerance;
232     short int have_eta_tolerance;
233    
234 gezelter 560 };
235    
236 mmeineke 778 template<typename T> class NPTi : public T{
237 tim 837
238 mmeineke 778 public:
239     NPTi( SimInfo *theInfo, ForceFields* the_ff);
240     ~NPTi();
241    
242     virtual double getConservedQuantity(void);
243     virtual void resetIntegrator(void);
244 tim 837 virtual string getAdditionalParameters(void);
245 mmeineke 778 protected:
246    
247    
248 tim 837
249 mmeineke 778 virtual void evolveEtaA(void);
250     virtual void evolveEtaB(void);
251    
252     virtual bool etaConverged( void );
253    
254     virtual void scaleSimBox( void );
255    
256     virtual void getVelScaleA( double sc[3], double vel[3] );
257     virtual void getVelScaleB( double sc[3], int index );
258 tim 837 virtual void getPosScale(double pos[3], double COM[3],
259 mmeineke 778 int index, double sc[3]);
260    
261 mmeineke 857 virtual void calcVelScale( void );
262    
263 mmeineke 778 double eta, oldEta, prevEta;
264 mmeineke 857 double vScale;
265 mmeineke 778 };
266    
267 tim 645 template<typename T> class NPTf : public T{
268 gezelter 576
269     public:
270    
271     NPTf ( SimInfo *theInfo, ForceFields* the_ff);
272 tim 767 virtual ~NPTf();
273 gezelter 576
274 tim 763 virtual double getConservedQuantity(void);
275 tim 837 virtual string getAdditionalParameters(void);
276 mmeineke 780 virtual void resetIntegrator(void);
277 mmeineke 594
278 gezelter 576 protected:
279    
280 mmeineke 780 virtual void evolveEtaA(void);
281     virtual void evolveEtaB(void);
282 gezelter 576
283 mmeineke 780 virtual bool etaConverged( void );
284 mmeineke 746
285 mmeineke 780 virtual void scaleSimBox( void );
286 gezelter 576
287 mmeineke 780 virtual void getVelScaleA( double sc[3], double vel[3] );
288     virtual void getVelScaleB( double sc[3], int index );
289 tim 837 virtual void getPosScale(double pos[3], double COM[3],
290 mmeineke 780 int index, double sc[3]);
291 tim 763
292 mmeineke 857 virtual void calcVelScale( void );
293    
294 gezelter 588 double eta[3][3];
295 mmeineke 780 double oldEta[3][3];
296     double prevEta[3][3];
297 mmeineke 857 double vScale[3][3];
298 gezelter 576 };
299    
300 mmeineke 812 template<typename T> class NPTxyz : public T{
301 mmeineke 755
302     public:
303    
304 mmeineke 812 NPTxyz ( SimInfo *theInfo, ForceFields* the_ff);
305     virtual ~NPTxyz();
306 mmeineke 755
307 mmeineke 812 virtual double getConservedQuantity(void);
308 tim 837 virtual string getAdditionalParameters(void);
309 mmeineke 812 virtual void resetIntegrator(void);
310 mmeineke 755
311     protected:
312    
313 mmeineke 812 virtual void evolveEtaA(void);
314     virtual void evolveEtaB(void);
315 mmeineke 755
316 mmeineke 812 virtual bool etaConverged( void );
317 mmeineke 755
318 mmeineke 812 virtual void scaleSimBox( void );
319 mmeineke 755
320 mmeineke 812 virtual void getVelScaleA( double sc[3], double vel[3] );
321     virtual void getVelScaleB( double sc[3], int index );
322 tim 837 virtual void getPosScale(double pos[3], double COM[3],
323 mmeineke 812 int index, double sc[3]);
324 mmeineke 755
325 mmeineke 857 virtual void calcVelScale( void );
326    
327 mmeineke 812 double eta[3][3];
328     double oldEta[3][3];
329     double prevEta[3][3];
330 mmeineke 857 double vScale[3][3];
331 mmeineke 812 };
332 mmeineke 755
333    
334 tim 658 template<typename T> class ZConstraint : public T {
335 tim 837
336     public:
337 tim 738 class ForceSubtractionPolicy{
338 tim 699 public:
339 tim 738 ForceSubtractionPolicy(ZConstraint<T>* integrator) {zconsIntegrator = integrator;}
340 tim 658
341 tim 837 virtual void update() = 0;
342 tim 699 virtual double getZFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce) = 0;
343     virtual double getZFOfMovingMols(Atom* atom, double totalForce) = 0;
344 tim 701 virtual double getHFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce) = 0;
345     virtual double getHFOfUnconsMols(Atom* atom, double totalForce) = 0;
346 tim 837
347 tim 701 protected:
348 mmeineke 788 ZConstraint<T>* zconsIntegrator;
349 tim 699 };
350    
351 tim 738 class PolicyByNumber : public ForceSubtractionPolicy{
352 gezelter 747
353 tim 699 public:
354 tim 837 PolicyByNumber(ZConstraint<T>* integrator) :ForceSubtractionPolicy(integrator) {}
355     virtual void update();
356 tim 699 virtual double getZFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce) ;
357     virtual double getZFOfMovingMols(Atom* atom, double totalForce) ;
358 tim 701 virtual double getHFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce);
359     virtual double getHFOfUnconsMols(Atom* atom, double totalForce);
360 tim 837
361 tim 699 private:
362 tim 763 int totNumOfMovingAtoms;
363 tim 699 };
364    
365 tim 738 class PolicyByMass : public ForceSubtractionPolicy{
366 gezelter 747
367 tim 699 public:
368 tim 837 PolicyByMass(ZConstraint<T>* integrator) :ForceSubtractionPolicy(integrator) {}
369    
370     virtual void update();
371 tim 699 virtual double getZFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce) ;
372     virtual double getZFOfMovingMols(Atom* atom, double totalForce) ;
373 tim 701 virtual double getHFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce);
374     virtual double getHFOfUnconsMols(Atom* atom, double totalForce);
375 tim 699
376 tim 701 private:
377     double totMassOfMovingAtoms;
378 tim 699 };
379    
380 tim 658 public:
381    
382     ZConstraint( SimInfo *theInfo, ForceFields* the_ff);
383     ~ZConstraint();
384 tim 837
385 tim 658 void setZConsTime(double time) {this->zconsTime = time;}
386     void getZConsTime() {return zconsTime;}
387 tim 837
388 tim 701 void setIndexOfAllZConsMols(vector<int> index) {indexOfAllZConsMols = index;}
389     void getIndexOfAllZConsMols() {return indexOfAllZConsMols;}
390 tim 837
391 tim 701 void setZConsOutput(const char * fileName) {zconsOutput = fileName;}
392 tim 658 string getZConsOutput() {return zconsOutput;}
393 tim 837
394 tim 677 virtual void integrate();
395 tim 658
396 tim 837
397 tim 658 #ifdef IS_MPI
398 tim 701 virtual void update(); //which is called to indicate the molecules' migration
399 mmeineke 377 #endif
400 tim 658
401 tim 837 enum ZConsState {zcsMoving, zcsFixed};
402 mmeineke 790
403     vector<Molecule*> zconsMols; //z-constraint molecules array
404     vector<ZConsState> states; //state of z-constraint molecules
405    
406    
407 tim 837
408 mmeineke 790 int totNumOfUnconsAtoms; //total number of uncontraint atoms
409     double totalMassOfUncons; //total mas of unconstraint molecules
410    
411    
412 tim 658 protected:
413    
414 mmeineke 790
415 tim 837
416     virtual void calcForce( int calcPot, int calcStress );
417 tim 677 virtual void thermalize(void);
418 tim 837
419 tim 677 void zeroOutVel();
420     void doZconstraintForce();
421 tim 682 void doHarmonic();
422 tim 677 bool checkZConsState();
423    
424     bool haveFixedZMols();
425     bool haveMovingZMols();
426    
427     double calcZSys();
428    
429     int isZConstraintMol(Molecule* mol);
430    
431    
432 tim 701 double zconsTime; //sample time
433     double zconsTol; //tolerance of z-contratint
434     double zForceConst; //base force constant term
435     //which is estimate by OOPSE
436 mmeineke 790
437 tim 837
438     vector<double> massOfZConsMols; //mass of z-constraint molecule
439 tim 701 vector<double> kz; //force constant array
440 mmeineke 790
441 tim 701 vector<double> zPos; //
442 tim 837
443    
444 tim 701 vector<Molecule*> unconsMols; //unconstraint molecules array
445     vector<double> massOfUnconsMols; //mass array of unconstraint molecules
446 tim 682
447 mmeineke 790
448 tim 701 vector<ZConsParaItem>* parameters; //
449 tim 837
450 tim 658 vector<int> indexOfAllZConsMols; //index of All Z-Constraint Molecuels
451 tim 677
452 tim 837 int* indexOfZConsMols; //index of local Z-Constraint Molecules
453 tim 658 double* fz;
454 tim 699 double* curZPos;
455 tim 677
456 mmeineke 790
457 tim 837
458     int whichDirection; //constraint direction
459    
460 tim 658 private:
461 tim 837
462 tim 701 string zconsOutput; //filename of zconstraint output
463     ZConsWriter* fzOut; //z-constraint writer
464 tim 677
465 tim 837 double curZconsTime;
466 tim 699
467 tim 696 double calcMovingMolsCOMVel();
468     double calcSysCOMVel();
469     double calcTotalForce();
470 tim 837
471 gezelter 747 ForceSubtractionPolicy* forcePolicy; //force subtraction policy
472 tim 738 friend class ForceSubtractionPolicy;
473 tim 677
474 tim 658 };
475    
476 tim 1064 /*
477 tim 969 class OOPSEMinimizerBase : public RealIntegrator {
478     public:
479    
480     OOPSEMinimizerBase ( SimInfo *theInfo, ForceFields* the_ff );
481     virtual ~OOPSEMinimizerBase();
482    
483 tim 1010 double calcGradient(vector<double>& x, vector<double>& grad);
484 tim 1035 void setCoor(vector<double>& x);
485     vector<double> getCoor();
486 tim 1031 void output(vector<double>& x, int iteration);
487     int getDim() {return dim;}
488 tim 1057 void constraintMove();
489     void shakeF();
490     void shakeR();
491 tim 1031 protected:
492 tim 969
493 tim 1031 int dim;
494     void calcDim();
495 tim 969 };
496    
497 tim 1031 //template<typename TMinimizer> class OOPSEMinimizer : public OOPSEMinimizerBase, TMinimizer{
498     // public:
499     // void writeOutput();
500     //};
501 tim 1064 */
502 tim 658 #endif