Revision | 79a98bb1a167945549973f6a2124708cac6e45ec (tree) |
---|---|
Time | 2014-01-11 21:21:41 |
Author | Katsuhiko Nishimra <ktns.87@gmai...> |
Commiter | Katsuhiko Nishimra |
Factor Out Optimizer::PrepareState. #32881
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/refactor_opt@1643 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -135,9 +135,9 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
135 | 135 | double maxGradientThreshold = Parameters::GetInstance()->GetMaxGradientOptimization(); |
136 | 136 | double rmsGradientThreshold = Parameters::GetInstance()->GetRmsGradientOptimization(); |
137 | 137 | const int dimension = molecule.GetAtomVect().size()*CartesianType_end; |
138 | - double trustRadius = Parameters::GetInstance()->GetInitialTrustRadiusOptimization(); | |
139 | - const double maxNormStep = Parameters::GetInstance()->GetMaxNormStepOptimization(); | |
140 | 138 | BFGSState state(molecule); |
139 | + state.SetTrustRadius(Parameters::GetInstance()->GetInitialTrustRadiusOptimization()); | |
140 | + state.SetMaxNormStep(Parameters::GetInstance()->GetMaxNormStepOptimization()); | |
141 | 141 | |
142 | 142 | // initial calculation |
143 | 143 | bool requireGuess = true; |
@@ -152,23 +152,7 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
152 | 152 | for(int s=0; s<totalSteps; s++){ |
153 | 153 | this->OutputOptimizationStepMessage(s); |
154 | 154 | |
155 | - // Store old Force data | |
156 | - MolDS_wrappers::Blas::GetInstance()->Dcopy(dimension, | |
157 | - static_cast<double const*>(state.GetVectorForce()), | |
158 | - state.GetVectorOldForce()); | |
159 | - | |
160 | - this->StoreMolecularGeometry(state.GetMatrixOldCoordinatesRef(), molecule); | |
161 | - | |
162 | - // Level shift Hessian redundant modes | |
163 | - this->ShiftHessianRedundantMode(state.GetMatrixHessian(), molecule); | |
164 | - | |
165 | - // Limit the trustRadius to maxNormStep | |
166 | - trustRadius=min(trustRadius,maxNormStep); | |
167 | - | |
168 | - //Calculate RFO step | |
169 | - this->CalcRFOStep(state.GetVectorStep(), state.GetMatrixHessian(), state.GetVectorForce(), trustRadius, dimension); | |
170 | - | |
171 | - double approximateChange = this->ApproximateEnergyChange(dimension, state.GetMatrixHessian(), state.GetVectorForce(), state.GetVectorStep()); | |
155 | + this->PrepareState(state, molecule, electronicStructure, elecState); | |
172 | 156 | |
173 | 157 | // Take a RFO step |
174 | 158 | bool doLineSearch = false; |
@@ -189,7 +173,7 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
189 | 173 | } |
190 | 174 | this->OutputMoleculeElectronicStructure(electronicStructure, molecule, this->CanOutputLogs()); |
191 | 175 | |
192 | - this->UpdateTrustRadius(trustRadius, approximateChange, state.GetInitialEnergy(), state.GetCurrentEnergy()); | |
176 | + this->UpdateTrustRadius(state.GetTrustRadiusRef(), state.GetApproximateChange(), state.GetInitialEnergy(), state.GetCurrentEnergy()); | |
193 | 177 | |
194 | 178 | // check convergence |
195 | 179 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
@@ -234,6 +218,35 @@ void BFGS::InitializeState(OptimizerState &stateOrig, const Molecule& molecule) | ||
234 | 218 | MolDS_wrappers::Blas::GetInstance()->Dcopy(dimension, &one, 0, &state.GetMatrixHessian()[0][0], dimension+1); |
235 | 219 | } |
236 | 220 | |
221 | +void BFGS::PrepareState(OptimizerState& stateOrig, | |
222 | + const MolDS_base::Molecule& molecule, | |
223 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
224 | + const int elecState) const{ | |
225 | + BFGSState& state = stateOrig.CastRef<BFGSState>(); | |
226 | + const MolDS_wrappers::molds_blas_int dimension = molecule.GetAtomVect().size()*CartesianType_end; | |
227 | + // Store old Force data | |
228 | + MolDS_wrappers::Blas::GetInstance()->Dcopy(dimension, | |
229 | + static_cast<double const*>(state.GetVectorForce()), | |
230 | + state.GetVectorOldForce()); | |
231 | + | |
232 | + this->StoreMolecularGeometry(state.GetMatrixOldCoordinatesRef(), molecule); | |
233 | + | |
234 | + // Level shift Hessian redundant modes | |
235 | + this->ShiftHessianRedundantMode(state.GetMatrixHessian(), molecule); | |
236 | + | |
237 | + // Limit the trustRadius to maxNormStep | |
238 | + state.SetTrustRadius(min(state.GetTrustRadius(),state.GetMaxNormStep())); | |
239 | + | |
240 | + //Calculate RFO step | |
241 | + this->CalcRFOStep(state.GetVectorStep(), state.GetMatrixHessian(), state.GetVectorForce(), state.GetTrustRadius(), dimension); | |
242 | + | |
243 | + state.SetApproximateChange(this->ApproximateEnergyChange(dimension, | |
244 | + state.GetMatrixHessian(), | |
245 | + state.GetVectorForce(), | |
246 | + state.GetVectorStep()) | |
247 | + ); | |
248 | +} | |
249 | + | |
237 | 250 | void BFGS::CalcRFOStep(double* vectorStep, |
238 | 251 | double const* const* matrixHessian, |
239 | 252 | double const* vectorForce, |
@@ -31,6 +31,9 @@ private: | ||
31 | 31 | double** matrixOldCoordinates; |
32 | 32 | double* vectorOldCoordinates; |
33 | 33 | double** matrixDisplacement; |
34 | + double approximateChange; | |
35 | + double trustRadius; | |
36 | + double maxNormStep; | |
34 | 37 | size_t numAtoms; |
35 | 38 | private: |
36 | 39 | template<class vector> |
@@ -48,6 +51,13 @@ private: | ||
48 | 51 | double** GetMatrixOldCoordinates() {return this->matrixOldCoordinates;} |
49 | 52 | double**& GetMatrixOldCoordinatesRef(){return this->matrixOldCoordinates;} |
50 | 53 | double** GetMatrixDisplacement() {return this->matrixDisplacement;} |
54 | + double GetApproximateChange() {return this->approximateChange;} | |
55 | + double GetTrustRadius() {return this->trustRadius;} | |
56 | + double& GetTrustRadiusRef() {return this->trustRadius;} | |
57 | + double GetMaxNormStep() {return this->maxNormStep;} | |
58 | + void SetApproximateChange(double approximateChange){this->approximateChange = approximateChange;} | |
59 | + void SetTrustRadius(double trustRadius) {this->trustRadius = trustRadius;} | |
60 | + void SetMaxNormStep(double maxNormStep) {this->maxNormStep = maxNormStep;} | |
51 | 61 | }; |
52 | 62 | public: |
53 | 63 | BFGS(); |
@@ -82,6 +92,10 @@ private: | ||
82 | 92 | |
83 | 93 | protected: |
84 | 94 | void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const; |
95 | + void PrepareState(OptimizerState& state, | |
96 | + const MolDS_base::Molecule& molecule, | |
97 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
98 | + const int elecState) const; | |
85 | 99 | void CalcRFOStep(double* vectorStep, |
86 | 100 | double const* const* matrixHessian, |
87 | 101 | double const* vectorForce, |
@@ -101,6 +101,9 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
101 | 101 | // conugate gradient loop |
102 | 102 | for(int s=0; s<totalSteps; s++){ |
103 | 103 | this->OutputOptimizationStepMessage(s); |
104 | + | |
105 | + this->PrepareState(state, molecule, electronicStructure, elecState); | |
106 | + | |
104 | 107 | state.SetInitialEnergy(state.GetCurrentEnergy()); |
105 | 108 | |
106 | 109 | // do line search |
@@ -49,6 +49,10 @@ private: | ||
49 | 49 | double* lineSearchedEnergy, |
50 | 50 | bool* obainesOptimizedStructure) const; |
51 | 51 | void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const; |
52 | + virtual void PrepareState(OptimizerState& state, | |
53 | + const MolDS_base::Molecule& molecule, | |
54 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
55 | + const int elecState) const{}; | |
52 | 56 | void UpdateSearchDirection(OptimizerState& state, |
53 | 57 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
54 | 58 | const MolDS_base::Molecule& molecule, |
@@ -104,6 +104,10 @@ private: | ||
104 | 104 | double* lineSearchedEnergy, |
105 | 105 | bool* obainesOptimizedStructure) const = 0; |
106 | 106 | virtual void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const = 0; |
107 | + virtual void PrepareState(OptimizerState& state, | |
108 | + const MolDS_base::Molecule& molecule, | |
109 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
110 | + const int elecState) const = 0; | |
107 | 111 | virtual const std::string& OptimizationStepMessage() const = 0; |
108 | 112 | }; |
109 | 113 |
@@ -37,6 +37,10 @@ private: | ||
37 | 37 | double* lineSearchedEnergy, |
38 | 38 | bool* obainesOptimizedStructure) const; |
39 | 39 | void InitializeState(OptimizerState&, const MolDS_base::Molecule&) const{} |
40 | + virtual void PrepareState(OptimizerState& state, | |
41 | + const MolDS_base::Molecule& molecule, | |
42 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
43 | + const int elecState) const{}; | |
40 | 44 | void UpdateSearchDirection(OptimizerState& state, |
41 | 45 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
42 | 46 | const MolDS_base::Molecule& molecule, |