• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Révision5675d3b6b291c6af7e507734baa7babba1fa8c2a (tree)
l'heure2014-01-17 11:24:16
AuteurKatsuhiko Nishimra <ktns.87@gmai...>
CommiterKatsuhiko Nishimra

Message de Log

Factor out Optimizer::UpdateState. #32881

git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/refactor_opt@1649 1136aad2-a195-0410-b898-f5ea1d11b9d8

Change Summary

Modification

--- a/src/optimization/BFGS.cpp
+++ b/src/optimization/BFGS.cpp
@@ -133,14 +133,12 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct
133133 Molecule& molecule,
134134 double* lineSearchedEnergy,
135135 bool* obtainesOptimizedStructure) const {
136- const int dimension = molecule.GetAtomVect().size()*CartesianType_end;
137136 BFGSState state(molecule, electronicStructure);
138137
139138 // initial calculation
140139 bool requireGuess = true;
141140 this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs());
142141 state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
143-
144142 state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
145143
146144 this->InitializeState(state, molecule);
@@ -152,10 +150,11 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct
152150
153151 this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT());
154152
155- this->UpdateTrustRadius(state.GetTrustRadiusRef(), state.GetApproximateChange(), state.GetInitialEnergy(), state.GetCurrentEnergy());
156-
153+ state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
157154 state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
158155
156+ this->UpdateState(state);
157+
159158 // check convergence
160159 if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(),
161160 molecule,
@@ -166,25 +165,6 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct
166165 *obtainesOptimizedStructure = true;
167166 break;
168167 }
169-
170- //Calculate displacement (K_k at Eq. (15) in [SJTO_1983])
171- this->CalcDisplacement(state.GetMatrixDisplacement(), state.GetMatrixOldCoordinates(), molecule);
172-
173- //Rollback geometry and energy if energy increases
174- bool isHillClimbing = state.GetCurrentEnergy() > state.GetInitialEnergy();
175- if(isHillClimbing){
176- this->OutputLog(this->messageHillClimbing);
177- this->RollbackMolecularGeometry(molecule, state.GetMatrixOldCoordinates());
178- state.SetCurrentEnergy(state.GetInitialEnergy());
179- }
180-
181- // Update Hessian
182- this->UpdateHessian(state.GetMatrixHessian(), dimension, state.GetVectorForce(), state.GetVectorOldForce(), &state.GetMatrixDisplacement()[0][0]);
183-
184- //Rollback gradient if energy increases
185- if(isHillClimbing){
186- state.SetMatrixForce(state.GetMatrixOldForce());
187- }
188168 }
189169 *lineSearchedEnergy = state.GetCurrentEnergy();
190170 }
@@ -255,6 +235,33 @@ void BFGS::CalcNextStepGeometry(Molecule &molecule,
255235 this->OutputMoleculeElectronicStructure(electronicStructure, molecule, this->CanOutputLogs());
256236 }
257237
238+void BFGS::UpdateState(OptimizerState& stateOrig) const{
239+ BFGSState& state = stateOrig.CastRef<BFGSState>();
240+ const MolDS_wrappers::molds_blas_int dimension = state.GetMolecule().GetAtomVect().size() * CartesianType_end;
241+
242+ this->UpdateTrustRadius(state.GetTrustRadiusRef(), state.GetApproximateChange(), state.GetInitialEnergy(), state.GetCurrentEnergy());
243+
244+ //Calculate displacement (K_k at Eq. (15) in [SJTO_1983])
245+ this->CalcDisplacement(state.GetMatrixDisplacement(), state.GetMatrixOldCoordinates(), state.GetMolecule());
246+
247+ //Rollback geometry and energy if energy increases
248+ bool isHillClimbing = state.GetCurrentEnergy() > state.GetInitialEnergy();
249+ if(isHillClimbing){
250+ this->OutputLog(this->messageHillClimbing);
251+ this->RollbackMolecularGeometry(state.GetMolecule(), state.GetMatrixOldCoordinates());
252+ state.SetCurrentEnergy(state.GetInitialEnergy());
253+ }
254+
255+ state.SetMatrixForce(state.GetElectronicStructure()->GetForce(state.GetElecState()));
256+
257+ // Update Hessian
258+ this->UpdateHessian(state.GetMatrixHessian(), dimension, state.GetVectorForce(), state.GetVectorOldForce(), &state.GetMatrixDisplacement()[0][0]);
259+
260+ //Rollback gradient if energy increases
261+ if(isHillClimbing){
262+ state.SetMatrixForce(state.GetMatrixOldForce());
263+ }
264+}
258265 void BFGS::CalcRFOStep(double* vectorStep,
259266 double const* const* matrixHessian,
260267 double const* vectorForce,
--- a/src/optimization/BFGS.h
+++ b/src/optimization/BFGS.h
@@ -102,6 +102,7 @@ protected:
102102 boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
103103 const int elecState,
104104 const double dt) const;
105+ void UpdateState(OptimizerState& state) const;
105106 void CalcRFOStep(double* vectorStep,
106107 double const* const* matrixHessian,
107108 double const* vectorForce,
--- a/src/optimization/ConjugateGradient.cpp
+++ b/src/optimization/ConjugateGradient.cpp
@@ -91,13 +91,10 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele
9191 bool requireGuess = true;
9292 this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs());
9393 state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
94-
95- requireGuess = false;
9694 state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
9795
9896 this->InitializeState(state, molecule);
9997
100- // conugate gradient loop
10198 for(int s=0; s<state.GetTotalSteps(); s++){
10299 this->OutputOptimizationStepMessage(s);
103100
@@ -105,7 +102,10 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele
105102
106103 this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT());
107104
108- this->UpdateSearchDirection(state, electronicStructure, molecule, state.GetElecState());
105+ state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
106+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
107+
108+ this->UpdateState(state);
109109
110110 // check convergence
111111 if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(),
@@ -118,7 +118,6 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele
118118 break;
119119 }
120120 }
121-
122121 *lineSearchedEnergy = state.GetCurrentEnergy();
123122 }
124123
@@ -131,6 +130,19 @@ void ConjugateGradient::InitializeState(OptimizerState &stateOrig, const Molecul
131130 }
132131 }
133132
133+void ConjugateGradient::PrepareState(OptimizerState& stateOrig,
134+ const MolDS_base::Molecule& molecule,
135+ const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
136+ const int elecState) const{
137+ ConjugateGradientState& state = stateOrig.CastRef<ConjugateGradientState>();
138+
139+ for(int a=0;a<molecule.GetAtomVect().size();a++){
140+ for(int i=0; i<CartesianType_end; i++){
141+ state.GetOldMatrixForce()[a][i] = state.GetMatrixForce()[a][i];
142+ }
143+ }
144+}
145+
134146 void ConjugateGradient::CalcNextStepGeometry(Molecule &molecule,
135147 OptimizerState& stateOrig,
136148 boost::shared_ptr<ElectronicStructure> electronicStructure,
@@ -143,17 +155,16 @@ void ConjugateGradient::CalcNextStepGeometry(Molecule &molecule,
143155 this->LineSearch(electronicStructure, molecule, state.GetCurrentEnergyRef(), state.GetMatrixSearchDirection(), elecState, dt);
144156 }
145157
158+void ConjugateGradient::UpdateState(OptimizerState& state) const{
159+ this->UpdateSearchDirection(state, state.GetElectronicStructure(), state.GetMolecule(), state.GetElecState());
160+}
161+
162+
146163 void ConjugateGradient::UpdateSearchDirection(OptimizerState& stateOrig,
147164 boost::shared_ptr<ElectronicStructure> electronicStructure,
148165 const MolDS_base::Molecule& molecule,
149166 int elecState) const{
150167 ConjugateGradientState& state = stateOrig.CastRef<ConjugateGradientState>();
151- for(int a=0;a<molecule.GetAtomVect().size();a++){
152- for(int i=0; i<CartesianType_end; i++){
153- state.GetOldMatrixForce()[a][i] = state.GetMatrixForce()[a][i];
154- }
155- }
156- state.SetMatrixForce(electronicStructure->GetForce(elecState));
157168 double beta=0.0;
158169 double temp=0.0;
159170 for(int a=0;a<molecule.GetAtomVect().size();a++){
--- a/src/optimization/ConjugateGradient.h
+++ b/src/optimization/ConjugateGradient.h
@@ -53,12 +53,13 @@ private:
5353 virtual void PrepareState(OptimizerState& state,
5454 const MolDS_base::Molecule& molecule,
5555 const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
56- const int elecState) const{};
56+ const int elecState) const;
5757 void CalcNextStepGeometry(MolDS_base::Molecule &molecule,
5858 OptimizerState& state,
5959 boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
6060 const int elecState,
6161 const double dt) const;
62+ void UpdateState(OptimizerState& state) const;
6263 void UpdateSearchDirection(OptimizerState& state,
6364 boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
6465 const MolDS_base::Molecule& molecule,
--- a/src/optimization/Optimizer.h
+++ b/src/optimization/Optimizer.h
@@ -129,6 +129,7 @@ private:
129129 boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
130130 const int elecState,
131131 const double dt) const= 0;
132+ virtual void UpdateState(OptimizerState& state) const = 0;
132133 virtual const std::string& OptimizationStepMessage() const = 0;
133134 };
134135
--- a/src/optimization/SteepestDescent.cpp
+++ b/src/optimization/SteepestDescent.cpp
@@ -74,17 +74,21 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect
7474 // initial calculation
7575 bool requireGuess = true;
7676 this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs());
77-
78- requireGuess = false;
79- state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
8077 state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
78+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
79+
80+ this->InitializeState(state, molecule);
81+
8182 for(int s=0; s<state.GetTotalSteps(); s++){
8283 this->OutputLog(boost::format("%s%d\n\n") % this->messageStartSteepestDescentStep.c_str() % (s+1));
8384 state.SetInitialEnergy(state.GetCurrentEnergy());
8485
8586 this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT());
8687
87- this->UpdateSearchDirection(state, electronicStructure, molecule, state.GetElecState());
88+ state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
89+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
90+
91+ this->UpdateState(state);
8892
8993 // check convergence
9094 if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(),
@@ -97,7 +101,6 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect
97101 break;
98102 }
99103 }
100-
101104 *lineSearchedEnergy = state.GetCurrentEnergy();
102105 }
103106
@@ -111,6 +114,10 @@ void SteepestDescent::CalcNextStepGeometry(Molecule &molecule,
111114 this->LineSearch(electronicStructure, molecule, state.GetCurrentEnergyRef(), state.GetMatrixForce(), elecState, dt);
112115 }
113116
117+void SteepestDescent::UpdateState(OptimizerState& state) const{
118+ this->UpdateSearchDirection(state, state.GetElectronicStructure(), state.GetMolecule(), state.GetElecState());
119+}
120+
114121 void SteepestDescent::UpdateSearchDirection(OptimizerState& state,
115122 boost::shared_ptr<ElectronicStructure> electronicStructure,
116123 const MolDS_base::Molecule& molecule,
--- a/src/optimization/SteepestDescent.h
+++ b/src/optimization/SteepestDescent.h
@@ -46,6 +46,7 @@ private:
4646 boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
4747 const int elecState,
4848 const double dt) const;
49+ void UpdateState(OptimizerState& state) const;
4950 void UpdateSearchDirection(OptimizerState& state,
5051 boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
5152 const MolDS_base::Molecule& molecule,