リビジョン | da53aa73a47676fece7c7c572fb11894a1a3c7b1 (tree) |
---|---|
日時 | 2014-01-10 17:04:35 |
作者 | Katsuhiko Nishimra <ktns.87@gmai...> |
コミッター | Katsuhiko Nishimra |
Refactor UpdateSearchDirection using OptimizerState. #32881
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/refactor_opt@1635 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -110,8 +110,7 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
110 | 110 | // do line search |
111 | 111 | this->LineSearch(electronicStructure, molecule, state.GetCurrentEnergyRef(), state.GetMatrixSearchDirection(), elecState, dt); |
112 | 112 | |
113 | - // update matrixSearchDirection | |
114 | - this->UpdateSearchDirection(state.GetMatrixForcePtr(), state.GetOldMatrixForce(), state.GetMatrixSearchDirection(), electronicStructure, molecule, elecState); | |
113 | + this->UpdateSearchDirection(state, electronicStructure, molecule, elecState); | |
115 | 114 | |
116 | 115 | // check convergence |
117 | 116 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
@@ -128,33 +127,37 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
128 | 127 | *lineSearchedEnergy = state.GetCurrentEnergy(); |
129 | 128 | } |
130 | 129 | |
131 | -void ConjugateGradient::UpdateSearchDirection(double const* const** matrixForce, | |
132 | - double** oldMatrixForce, | |
133 | - double** matrixSearchDirection, | |
130 | +void ConjugateGradient::UpdateSearchDirection(OptimizerState& stateOrig, | |
134 | 131 | boost::shared_ptr<ElectronicStructure> electronicStructure, |
135 | 132 | const MolDS_base::Molecule& molecule, |
136 | 133 | int elecState) const{ |
137 | - for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
138 | - for(int i=0; i<CartesianType_end; i++){ | |
139 | - oldMatrixForce[a][i] = (*matrixForce)[a][i]; | |
134 | + try{ | |
135 | + ConjugateGradientState& state = dynamic_cast<ConjugateGradientState&>(stateOrig); | |
136 | + for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
137 | + for(int i=0; i<CartesianType_end; i++){ | |
138 | + state.GetOldMatrixForce()[a][i] = state.GetMatrixForce()[a][i]; | |
139 | + } | |
140 | 140 | } |
141 | - } | |
142 | - *matrixForce = electronicStructure->GetForce(elecState); | |
143 | - double beta=0.0; | |
144 | - double temp=0.0; | |
145 | - for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
146 | - for(int i=0; i<CartesianType_end; i++){ | |
147 | - temp += pow(oldMatrixForce[a][i],2.0); | |
148 | - beta += ((*matrixForce)[a][i] - oldMatrixForce[a][i])*(*matrixForce)[a][i]; | |
141 | + state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
142 | + double beta=0.0; | |
143 | + double temp=0.0; | |
144 | + for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
145 | + for(int i=0; i<CartesianType_end; i++){ | |
146 | + temp += pow(state.GetOldMatrixForce()[a][i],2.0); | |
147 | + beta += (state.GetMatrixForce()[a][i] - state.GetOldMatrixForce()[a][i])*state.GetMatrixForce()[a][i]; | |
148 | + } | |
149 | 149 | } |
150 | - } | |
151 | - beta /= temp; | |
152 | - for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
153 | - for(int i=0; i<CartesianType_end; i++){ | |
154 | - matrixSearchDirection[a][i] *= beta; | |
155 | - matrixSearchDirection[a][i] += (*matrixForce)[a][i]; | |
150 | + beta /= temp; | |
151 | + for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
152 | + for(int i=0; i<CartesianType_end; i++){ | |
153 | + state.GetMatrixSearchDirection()[a][i] *= beta; | |
154 | + state.GetMatrixSearchDirection()[a][i] += state.GetMatrixForce()[a][i]; | |
155 | + } | |
156 | 156 | } |
157 | 157 | } |
158 | + catch(std::bad_cast& ex){ | |
159 | + throw MolDSException(this->errorMessageFailedToDowncastState); | |
160 | + } | |
158 | 161 | } |
159 | 162 | |
160 | 163 |
@@ -45,9 +45,7 @@ private: | ||
45 | 45 | MolDS_base::Molecule& molecule, |
46 | 46 | double* lineSearchedEnergy, |
47 | 47 | bool* obainesOptimizedStructure) const; |
48 | - void UpdateSearchDirection(double const* const** matrixForce, | |
49 | - double** oldMatrixForce, | |
50 | - double** matrixSearchDirection, | |
48 | + void UpdateSearchDirection(OptimizerState& state, | |
51 | 49 | boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, |
52 | 50 | const MolDS_base::Molecule& molecule, |
53 | 51 | int elecState) const; |
@@ -89,6 +89,8 @@ void Optimizer::Optimize(Molecule& molecule){ | ||
89 | 89 | void Optimizer::SetMessages(){ |
90 | 90 | this->errorMessageTheoryType = "\ttheory type = "; |
91 | 91 | this->errorMessageTotalSteps = "\tTotal steps = "; |
92 | + this->errorMessageFailedToDowncastState | |
93 | + = "Failed to downcast Optimizer::OptimizerState!"; | |
92 | 94 | this->messageGeometyrOptimizationMetConvergence |
93 | 95 | = "\t\tGeometry otimization met convergence criterion(^^b\n\n\n"; |
94 | 96 | this->messageStartGeometryOptimization = "********** START: Geometry optimization **********\n"; |
@@ -48,6 +48,7 @@ public: | ||
48 | 48 | protected: |
49 | 49 | std::string errorMessageNotEnebleTheoryType; |
50 | 50 | std::string errorMessageGeometyrOptimizationNotConverged; |
51 | + std::string errorMessageFailedToDowncastState; | |
51 | 52 | std::string messageLineSearchSteps; |
52 | 53 | virtual void SetMessages(); |
53 | 54 | void UpdateMolecularCoordinates(MolDS_base::Molecule& molecule, double const* const* matrixForce, double dt) const; |
@@ -90,8 +90,7 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect | ||
90 | 90 | // do line search |
91 | 91 | this->LineSearch(electronicStructure, molecule, state.GetCurrentEnergyRef(), state.GetMatrixForce(), elecState, dt); |
92 | 92 | |
93 | - // update force | |
94 | - state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
93 | + this->UpdateSearchDirection(state, electronicStructure, molecule, elecState); | |
95 | 94 | |
96 | 95 | // check convergence |
97 | 96 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
@@ -103,11 +102,16 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect | ||
103 | 102 | *obtainesOptimizedStructure = true; |
104 | 103 | break; |
105 | 104 | } |
106 | - | |
107 | 105 | } |
106 | + | |
108 | 107 | *lineSearchedEnergy = state.GetCurrentEnergy(); |
109 | 108 | } |
110 | -} | |
111 | - | |
112 | - | |
113 | 109 | |
110 | +void SteepestDescent::UpdateSearchDirection(OptimizerState& state, | |
111 | + boost::shared_ptr<ElectronicStructure> electronicStructure, | |
112 | + const MolDS_base::Molecule& molecule, | |
113 | + int elecState) const{ | |
114 | + // update force | |
115 | + state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
116 | +} | |
117 | +} |
@@ -33,6 +33,10 @@ private: | ||
33 | 33 | MolDS_base::Molecule& molecule, |
34 | 34 | double* lineSearchedEnergy, |
35 | 35 | bool* obainesOptimizedStructure) const; |
36 | + void UpdateSearchDirection(OptimizerState& state, | |
37 | + boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
38 | + const MolDS_base::Molecule& molecule, | |
39 | + int elecState) const; | |
36 | 40 | }; |
37 | 41 | |
38 | 42 | } |