Révision | 5675d3b6b291c6af7e507734baa7babba1fa8c2a (tree) |
---|---|
l'heure | 2014-01-17 11:24:16 |
Auteur | Katsuhiko Nishimra <ktns.87@gmai...> |
Commiter | Katsuhiko Nishimra |
Factor out Optimizer::UpdateState. #32881
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/refactor_opt@1649 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -133,14 +133,12 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
133 | 133 | Molecule& molecule, |
134 | 134 | double* lineSearchedEnergy, |
135 | 135 | bool* obtainesOptimizedStructure) const { |
136 | - const int dimension = molecule.GetAtomVect().size()*CartesianType_end; | |
137 | 136 | BFGSState state(molecule, electronicStructure); |
138 | 137 | |
139 | 138 | // initial calculation |
140 | 139 | bool requireGuess = true; |
141 | 140 | this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); |
142 | 141 | state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); |
143 | - | |
144 | 142 | state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); |
145 | 143 | |
146 | 144 | this->InitializeState(state, molecule); |
@@ -152,10 +150,11 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
152 | 150 | |
153 | 151 | this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); |
154 | 152 | |
155 | - this->UpdateTrustRadius(state.GetTrustRadiusRef(), state.GetApproximateChange(), state.GetInitialEnergy(), state.GetCurrentEnergy()); | |
156 | - | |
153 | + state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
157 | 154 | state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); |
158 | 155 | |
156 | + this->UpdateState(state); | |
157 | + | |
159 | 158 | // check convergence |
160 | 159 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
161 | 160 | molecule, |
@@ -166,25 +165,6 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
166 | 165 | *obtainesOptimizedStructure = true; |
167 | 166 | break; |
168 | 167 | } |
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 | - } | |
188 | 168 | } |
189 | 169 | *lineSearchedEnergy = state.GetCurrentEnergy(); |
190 | 170 | } |
@@ -255,6 +235,33 @@ void BFGS::CalcNextStepGeometry(Molecule &molecule, | ||
255 | 235 | this->OutputMoleculeElectronicStructure(electronicStructure, molecule, this->CanOutputLogs()); |
256 | 236 | } |
257 | 237 | |
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 | +} | |
258 | 265 | void BFGS::CalcRFOStep(double* vectorStep, |
259 | 266 | double const* const* matrixHessian, |
260 | 267 | double const* vectorForce, |
@@ -102,6 +102,7 @@ protected: | ||
102 | 102 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
103 | 103 | const int elecState, |
104 | 104 | const double dt) const; |
105 | + void UpdateState(OptimizerState& state) const; | |
105 | 106 | void CalcRFOStep(double* vectorStep, |
106 | 107 | double const* const* matrixHessian, |
107 | 108 | double const* vectorForce, |
@@ -91,13 +91,10 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
91 | 91 | bool requireGuess = true; |
92 | 92 | this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); |
93 | 93 | state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); |
94 | - | |
95 | - requireGuess = false; | |
96 | 94 | state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); |
97 | 95 | |
98 | 96 | this->InitializeState(state, molecule); |
99 | 97 | |
100 | - // conugate gradient loop | |
101 | 98 | for(int s=0; s<state.GetTotalSteps(); s++){ |
102 | 99 | this->OutputOptimizationStepMessage(s); |
103 | 100 |
@@ -105,7 +102,10 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
105 | 102 | |
106 | 103 | this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); |
107 | 104 | |
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); | |
109 | 109 | |
110 | 110 | // check convergence |
111 | 111 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
@@ -118,7 +118,6 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
118 | 118 | break; |
119 | 119 | } |
120 | 120 | } |
121 | - | |
122 | 121 | *lineSearchedEnergy = state.GetCurrentEnergy(); |
123 | 122 | } |
124 | 123 |
@@ -131,6 +130,19 @@ void ConjugateGradient::InitializeState(OptimizerState &stateOrig, const Molecul | ||
131 | 130 | } |
132 | 131 | } |
133 | 132 | |
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 | + | |
134 | 146 | void ConjugateGradient::CalcNextStepGeometry(Molecule &molecule, |
135 | 147 | OptimizerState& stateOrig, |
136 | 148 | boost::shared_ptr<ElectronicStructure> electronicStructure, |
@@ -143,17 +155,16 @@ void ConjugateGradient::CalcNextStepGeometry(Molecule &molecule, | ||
143 | 155 | this->LineSearch(electronicStructure, molecule, state.GetCurrentEnergyRef(), state.GetMatrixSearchDirection(), elecState, dt); |
144 | 156 | } |
145 | 157 | |
158 | +void ConjugateGradient::UpdateState(OptimizerState& state) const{ | |
159 | + this->UpdateSearchDirection(state, state.GetElectronicStructure(), state.GetMolecule(), state.GetElecState()); | |
160 | +} | |
161 | + | |
162 | + | |
146 | 163 | void ConjugateGradient::UpdateSearchDirection(OptimizerState& stateOrig, |
147 | 164 | boost::shared_ptr<ElectronicStructure> electronicStructure, |
148 | 165 | const MolDS_base::Molecule& molecule, |
149 | 166 | int elecState) const{ |
150 | 167 | 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)); | |
157 | 168 | double beta=0.0; |
158 | 169 | double temp=0.0; |
159 | 170 | for(int a=0;a<molecule.GetAtomVect().size();a++){ |
@@ -53,12 +53,13 @@ private: | ||
53 | 53 | virtual void PrepareState(OptimizerState& state, |
54 | 54 | const MolDS_base::Molecule& molecule, |
55 | 55 | const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
56 | - const int elecState) const{}; | |
56 | + const int elecState) const; | |
57 | 57 | void CalcNextStepGeometry(MolDS_base::Molecule &molecule, |
58 | 58 | OptimizerState& state, |
59 | 59 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
60 | 60 | const int elecState, |
61 | 61 | const double dt) const; |
62 | + void UpdateState(OptimizerState& state) const; | |
62 | 63 | void UpdateSearchDirection(OptimizerState& state, |
63 | 64 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
64 | 65 | const MolDS_base::Molecule& molecule, |
@@ -129,6 +129,7 @@ private: | ||
129 | 129 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
130 | 130 | const int elecState, |
131 | 131 | const double dt) const= 0; |
132 | + virtual void UpdateState(OptimizerState& state) const = 0; | |
132 | 133 | virtual const std::string& OptimizationStepMessage() const = 0; |
133 | 134 | }; |
134 | 135 |
@@ -74,17 +74,21 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect | ||
74 | 74 | // initial calculation |
75 | 75 | bool requireGuess = true; |
76 | 76 | this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); |
77 | - | |
78 | - requireGuess = false; | |
79 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
80 | 77 | state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); |
78 | + state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
79 | + | |
80 | + this->InitializeState(state, molecule); | |
81 | + | |
81 | 82 | for(int s=0; s<state.GetTotalSteps(); s++){ |
82 | 83 | this->OutputLog(boost::format("%s%d\n\n") % this->messageStartSteepestDescentStep.c_str() % (s+1)); |
83 | 84 | state.SetInitialEnergy(state.GetCurrentEnergy()); |
84 | 85 | |
85 | 86 | this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); |
86 | 87 | |
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); | |
88 | 92 | |
89 | 93 | // check convergence |
90 | 94 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
@@ -97,7 +101,6 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect | ||
97 | 101 | break; |
98 | 102 | } |
99 | 103 | } |
100 | - | |
101 | 104 | *lineSearchedEnergy = state.GetCurrentEnergy(); |
102 | 105 | } |
103 | 106 |
@@ -111,6 +114,10 @@ void SteepestDescent::CalcNextStepGeometry(Molecule &molecule, | ||
111 | 114 | this->LineSearch(electronicStructure, molecule, state.GetCurrentEnergyRef(), state.GetMatrixForce(), elecState, dt); |
112 | 115 | } |
113 | 116 | |
117 | +void SteepestDescent::UpdateState(OptimizerState& state) const{ | |
118 | + this->UpdateSearchDirection(state, state.GetElectronicStructure(), state.GetMolecule(), state.GetElecState()); | |
119 | +} | |
120 | + | |
114 | 121 | void SteepestDescent::UpdateSearchDirection(OptimizerState& state, |
115 | 122 | boost::shared_ptr<ElectronicStructure> electronicStructure, |
116 | 123 | const MolDS_base::Molecule& molecule, |
@@ -46,6 +46,7 @@ private: | ||
46 | 46 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
47 | 47 | const int elecState, |
48 | 48 | const double dt) const; |
49 | + void UpdateState(OptimizerState& state) const; | |
49 | 50 | void UpdateSearchDirection(OptimizerState& state, |
50 | 51 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
51 | 52 | const MolDS_base::Molecule& molecule, |