Revision | 40e76f60f9a930abd0ca925fc9723ca0da62cdbd (tree) |
---|---|
Zeit | 2014-01-11 21:21:33 |
Autor | Katsuhiko Nishimra <ktns.87@gmai...> |
Commiter | Katsuhiko Nishimra |
Factor out OptimizerState::CastRef(). #32881
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/refactor_opt@1640 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -131,32 +131,27 @@ void ConjugateGradient::UpdateSearchDirection(OptimizerState& stateOrig, | ||
131 | 131 | boost::shared_ptr<ElectronicStructure> electronicStructure, |
132 | 132 | const MolDS_base::Molecule& molecule, |
133 | 133 | int elecState) const{ |
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 | - } | |
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 | - } | |
134 | + ConjugateGradientState& state = stateOrig.CastRef<ConjugateGradientState>(); | |
135 | + for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
136 | + for(int i=0; i<CartesianType_end; i++){ | |
137 | + state.GetOldMatrixForce()[a][i] = state.GetMatrixForce()[a][i]; | |
149 | 138 | } |
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 | - } | |
139 | + } | |
140 | + state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
141 | + double beta=0.0; | |
142 | + double temp=0.0; | |
143 | + for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
144 | + for(int i=0; i<CartesianType_end; i++){ | |
145 | + temp += pow(state.GetOldMatrixForce()[a][i],2.0); | |
146 | + beta += (state.GetMatrixForce()[a][i] - state.GetOldMatrixForce()[a][i])*state.GetMatrixForce()[a][i]; | |
156 | 147 | } |
157 | 148 | } |
158 | - catch(std::bad_cast& ex){ | |
159 | - throw MolDSException(this->errorMessageFailedToDowncastState); | |
149 | + beta /= temp; | |
150 | + for(int a=0;a<molecule.GetAtomVect().size();a++){ | |
151 | + for(int i=0; i<CartesianType_end; i++){ | |
152 | + state.GetMatrixSearchDirection()[a][i] *= beta; | |
153 | + state.GetMatrixSearchDirection()[a][i] += state.GetMatrixForce()[a][i]; | |
154 | + } | |
160 | 155 | } |
161 | 156 | } |
162 | 157 |
@@ -49,6 +49,12 @@ using namespace MolDS_base_atoms; | ||
49 | 49 | using namespace MolDS_base_factories; |
50 | 50 | |
51 | 51 | namespace MolDS_optimization{ |
52 | + | |
53 | +void Optimizer::OptimizerState::SetMessages(){ | |
54 | + this->errorMessageFailedToDowncastState | |
55 | + = "Failed to downcast Optimizer::OptimizerState!"; | |
56 | +} | |
57 | + | |
52 | 58 | Optimizer::Optimizer(){ |
53 | 59 | this->SetEnableTheoryTypes(); |
54 | 60 | //this->OutputLog("Optimizer created\n"); |
@@ -89,8 +95,6 @@ void Optimizer::Optimize(Molecule& molecule){ | ||
89 | 95 | void Optimizer::SetMessages(){ |
90 | 96 | this->errorMessageTheoryType = "\ttheory type = "; |
91 | 97 | this->errorMessageTotalSteps = "\tTotal steps = "; |
92 | - this->errorMessageFailedToDowncastState | |
93 | - = "Failed to downcast Optimizer::OptimizerState!"; | |
94 | 98 | this->messageGeometyrOptimizationMetConvergence |
95 | 99 | = "\t\tGeometry otimization met convergence criterion(^^b\n\n\n"; |
96 | 100 | this->messageStartGeometryOptimization = "********** START: Geometry optimization **********\n"; |
@@ -28,9 +28,11 @@ protected: | ||
28 | 28 | double currentEnergy; |
29 | 29 | double initialEnergy; |
30 | 30 | double const* const* matrixForce; |
31 | + std::string errorMessageFailedToDowncastState; | |
32 | + virtual void SetMessages(); | |
31 | 33 | public: |
32 | 34 | OptimizerState(): |
33 | - currentEnergy(0.0), initialEnergy(0.0), matrixForce(NULL){} | |
35 | + currentEnergy(0.0), initialEnergy(0.0), matrixForce(NULL){this->SetMessages();} | |
34 | 36 | virtual ~OptimizerState(){} |
35 | 37 | double& GetCurrentEnergyRef(){return this->currentEnergy;} |
36 | 38 | double GetCurrentEnergy(){return this->currentEnergy;} |
@@ -40,6 +42,15 @@ protected: | ||
40 | 42 | void SetCurrentEnergy(double currentEnergy){this->currentEnergy = currentEnergy;} |
41 | 43 | void SetInitialEnergy(double initialEnergy){this->initialEnergy = initialEnergy;} |
42 | 44 | void SetMatrixForce(double const* const* matrixForce){this->matrixForce = matrixForce;} |
45 | + template<class State> | |
46 | + State& CastRef(){ | |
47 | + try{ | |
48 | + return dynamic_cast<State&>(*this); | |
49 | + } | |
50 | + catch(std::bad_cast& ex){ | |
51 | + throw MolDS_base::MolDSException(this->errorMessageFailedToDowncastState); | |
52 | + } | |
53 | + } | |
43 | 54 | }; |
44 | 55 | public: |
45 | 56 | Optimizer(); |
@@ -48,7 +59,6 @@ public: | ||
48 | 59 | protected: |
49 | 60 | std::string errorMessageNotEnebleTheoryType; |
50 | 61 | std::string errorMessageGeometyrOptimizationNotConverged; |
51 | - std::string errorMessageFailedToDowncastState; | |
52 | 62 | std::string messageLineSearchSteps; |
53 | 63 | virtual void SetMessages(); |
54 | 64 | void UpdateMolecularCoordinates(MolDS_base::Molecule& molecule, double const* const* matrixForce, double dt) const; |