1 |
/* |
2 |
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
* |
4 |
* Contact: oopse@oopse.org |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU Lesser General Public License |
8 |
* as published by the Free Software Foundation; either version 2.1 |
9 |
* of the License, or (at your option) any later version. |
10 |
* All we ask is that proper credit is given for our work, which includes |
11 |
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
* of your source code files, and to any copyright notice that you may distribute |
13 |
* with programs based on this work. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU Lesser General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU Lesser General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
* |
24 |
*/ |
25 |
|
26 |
#ifndef MINIMIZERS_OOPSEMINIMIZER_HPP |
27 |
#define MINIMIZERS_OOPSEMINIMIZER_HPP |
28 |
|
29 |
#include <iostream> |
30 |
|
31 |
#include "integrators/Integrator.hpp" |
32 |
#include "minimizers/MinimizerParameterSet.hpp" |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
// base class of minimizer |
39 |
|
40 |
namespace oopse { |
41 |
|
42 |
/** @todo need refactorying */ |
43 |
const int MIN_LSERROR = -1; |
44 |
const int MIN_MAXITER = 0; |
45 |
const int MIN_CONVERGE = 1; |
46 |
|
47 |
const int CONVG_UNCONVG = 0; |
48 |
const int CONVG_FTOL = 1; |
49 |
const int CONVG_GTOL = 2; |
50 |
const int CONVG_ABSGTOL = 3; |
51 |
const int CONVG_STEPTOL = 4; |
52 |
|
53 |
const int LS_SUCCEED =1; |
54 |
const int LS_ERROR = -1; |
55 |
|
56 |
|
57 |
class OOPSEMinimizer { |
58 |
public: |
59 |
|
60 |
OOPSEMinimizer(SimInfo *theInfo, MinimizerParameterSet* param); |
61 |
|
62 |
virtual ~OOPSEMinimizer(); |
63 |
|
64 |
// |
65 |
virtual void init() {} |
66 |
|
67 |
//driver function of minimization method |
68 |
virtual void minimize(); |
69 |
|
70 |
// |
71 |
virtual int step() = 0; |
72 |
|
73 |
// |
74 |
virtual void prepareStep() {}; |
75 |
|
76 |
//line search algorithm, for the time being, we use back track algorithm |
77 |
virtual int doLineSearch(std::vector<double>& direction, double stepSize); |
78 |
|
79 |
virtual int checkConvg() = 0; |
80 |
|
81 |
//print detail information of the minimization method |
82 |
virtual void printMinimizerInfo(); |
83 |
|
84 |
//save the result when minimization method is done |
85 |
virtual void saveResult(){} |
86 |
|
87 |
//write out the trajectory |
88 |
virtual void writeOut(std::vector<double>&x, double curIter); |
89 |
|
90 |
|
91 |
//get the status of minimization |
92 |
int getMinStatus() {return minStatus;} |
93 |
|
94 |
// get the dimension of the model |
95 |
int getDim() { return ndim; } |
96 |
|
97 |
//get the name of minimizer method |
98 |
std::string getMinimizerName() { return minimizerName; } |
99 |
|
100 |
//return number of current Iteration |
101 |
int getCurIter() { return curIter; } |
102 |
|
103 |
// set the verbose mode of minimizer |
104 |
void setVerbose(bool verbose) { bVerbose = verbose;} |
105 |
|
106 |
//get and set the coordinate |
107 |
std::vector<double> getX() { return curX; } |
108 |
void setX(std::vector<double>& x); |
109 |
|
110 |
//get and set the value of object function |
111 |
double getF() { return curF; } |
112 |
void setF(double f) { curF = f; } |
113 |
|
114 |
std::vector<double> getG() { return curG; } |
115 |
void setG(std::vector<double>& g); |
116 |
|
117 |
//get and set the gradient |
118 |
std::vector<double> getGrad() { return curG; } |
119 |
void setGrad(std::vector<double>& g) { curG = g; } |
120 |
|
121 |
//interal function to evaluate the energy and gradient in OOPSE |
122 |
void calcEnergyGradient(vector<double>& x, std::vector<double>& grad, double& |
123 |
energy, int& status); |
124 |
|
125 |
//calculate the value of object function |
126 |
virtual void calcF(); |
127 |
virtual void calcF(std::vector<double>& x, double&f, int& status); |
128 |
|
129 |
//calculate the gradient |
130 |
virtual void calcG(); |
131 |
virtual void calcG(std::vector<double>& x, std::vector<double>& g, double& f, int& status); |
132 |
|
133 |
//calculate the hessian |
134 |
//virtual void calcH(int& status); |
135 |
//virtual void calcH(vector<double>& x, std::vector<dobule>& g, SymMatrix& h, int& status); |
136 |
|
137 |
|
138 |
protected: |
139 |
|
140 |
// transfrom cartesian and rotational coordinates into minimization coordinates |
141 |
std::vector<double> getCoor(); |
142 |
|
143 |
// transfrom minimization coordinates into cartesian and rotational coordinates |
144 |
void setCoor(std::vector<double>& x); |
145 |
|
146 |
//flag of turning on shake algorithm |
147 |
bool bShake; |
148 |
|
149 |
//constraint the bonds; |
150 |
int shakeR(); |
151 |
|
152 |
//remove the force component along the bond direction |
153 |
int shakeF(); |
154 |
|
155 |
|
156 |
SimInfo* info; |
157 |
|
158 |
//parameter set of minimization method |
159 |
MinimizerParameterSet* paramSet; |
160 |
|
161 |
// dimension of the model |
162 |
int ndim; |
163 |
|
164 |
//name of the minimizer |
165 |
std::string minimizerName; |
166 |
|
167 |
// current iteration number |
168 |
int curIter; |
169 |
//status of minimization |
170 |
int minStatus; |
171 |
|
172 |
//flag of verbose mode |
173 |
bool bVerbose; |
174 |
|
175 |
|
176 |
//status of energy and gradient evaluation |
177 |
int egEvalStatus; |
178 |
|
179 |
//initial coordinates |
180 |
//vector<double> initX; |
181 |
|
182 |
//current value of the function |
183 |
double curF; |
184 |
// current coordinates |
185 |
std::vector<double> curX; |
186 |
//gradient at curent coordinates |
187 |
std::vector<double> curG; |
188 |
|
189 |
//hessian at current coordinates |
190 |
//SymMatrix curH; |
191 |
|
192 |
private: |
193 |
|
194 |
//calculate the dimension od the model for minimization |
195 |
void calcDim(); |
196 |
|
197 |
}; |
198 |
|
199 |
} |
200 |
#endif |
201 |
|