Revision | d61b1cc2054ed35082654d07b148b0a583c561fb (tree) |
---|---|
Zeit | 2014-01-17 11:24:19 |
Autor | Katsuhiko Nishimra <ktns.87@gmai...> |
Commiter | Katsuhiko Nishimra |
Unify Optimizer::SearchMinimum. #32881
git-svn-id: https://svn.sourceforge.jp/svnroot/molds/branches/refactor_opt@1651 1136aad2-a195-0410-b898-f5ea1d11b9d8
@@ -54,7 +54,7 @@ using namespace MolDS_base_atoms; | ||
54 | 54 | namespace MolDS_optimization{ |
55 | 55 | |
56 | 56 | BFGS::BFGSState::BFGSState(Molecule& molecule, |
57 | - boost::shared_ptr<ElectronicStructure>& electronicStructure): | |
57 | + const boost::shared_ptr<ElectronicStructure>& electronicStructure): | |
58 | 58 | OptimizerState(molecule, electronicStructure), |
59 | 59 | matrixHessian(NULL), |
60 | 60 | matrixOldForce(NULL), |
@@ -129,46 +129,6 @@ void BFGS::SetMessages(){ | ||
129 | 129 | = "Scaling factor is increased to %e.\n"; |
130 | 130 | } |
131 | 131 | |
132 | -void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStructure, | |
133 | - Molecule& molecule, | |
134 | - double* lineSearchedEnergy, | |
135 | - bool* obtainesOptimizedStructure) const { | |
136 | - BFGSState state(molecule, electronicStructure); | |
137 | - | |
138 | - // initial calculation | |
139 | - bool requireGuess = true; | |
140 | - this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); | |
141 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
142 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
143 | - | |
144 | - this->InitializeState(state, molecule); | |
145 | - | |
146 | - for(int s=0; s<state.GetTotalSteps(); s++){ | |
147 | - this->OutputOptimizationStepMessage(s); | |
148 | - | |
149 | - this->PrepareState(state, molecule, electronicStructure, state.GetElecState()); | |
150 | - | |
151 | - this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
152 | - | |
153 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
154 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
155 | - | |
156 | - this->UpdateState(state); | |
157 | - | |
158 | - // check convergence | |
159 | - if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), | |
160 | - molecule, | |
161 | - state.GetInitialEnergy(), | |
162 | - state.GetCurrentEnergy(), | |
163 | - state.GetMaxGradientThreshold(), | |
164 | - state.GetRmsGradientThreshold())){ | |
165 | - *obtainesOptimizedStructure = true; | |
166 | - break; | |
167 | - } | |
168 | - } | |
169 | - *lineSearchedEnergy = state.GetCurrentEnergy(); | |
170 | -} | |
171 | - | |
172 | 132 | void BFGS::InitializeState(OptimizerState &stateOrig, const Molecule& molecule) const{ |
173 | 133 | const MolDS_wrappers::molds_blas_int dimension = molecule.GetAtomVect().size()*CartesianType_end; |
174 | 134 | const double one = 1; |
@@ -41,7 +41,7 @@ private: | ||
41 | 41 | BFGSState(const BFGSState&); // delete default copy constructor |
42 | 42 | public: |
43 | 43 | BFGSState(MolDS_base::Molecule& molecule, |
44 | - boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
44 | + const boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
45 | 45 | virtual ~BFGSState(); |
46 | 46 | double const* GetVectorForce (){return this->Matrix2Vector(this->matrixForce);} |
47 | 47 | double* GetVectorOldForce (){return this->Matrix2Vector(this->matrixOldForce);} |
@@ -87,12 +87,11 @@ private: | ||
87 | 87 | const std::string& OptimizationStepMessage() const{ |
88 | 88 | return this->messageStartBFGSStep; |
89 | 89 | } |
90 | - virtual void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
91 | - MolDS_base::Molecule& molecule, | |
92 | - double* lineSearchedEnergy, | |
93 | - bool* obainesOptimizedStructure) const; | |
94 | - | |
95 | 90 | protected: |
91 | + virtual OptimizerState* CreateState(MolDS_base::Molecule& molecule, | |
92 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure) const{ | |
93 | + return new BFGSState(molecule, electronicStructure); | |
94 | + } | |
96 | 95 | void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const; |
97 | 96 | void PrepareState(OptimizerState& state, |
98 | 97 | const MolDS_base::Molecule& molecule, |
@@ -26,6 +26,7 @@ | ||
26 | 26 | #include<vector> |
27 | 27 | #include<stdexcept> |
28 | 28 | #include<boost/shared_ptr.hpp> |
29 | +#include<boost/scoped_ptr.hpp> | |
29 | 30 | #include<boost/format.hpp> |
30 | 31 | #include"../base/Enums.h" |
31 | 32 | #include"../base/Uncopyable.h" |
@@ -49,7 +50,7 @@ using namespace MolDS_base_atoms; | ||
49 | 50 | namespace MolDS_optimization{ |
50 | 51 | |
51 | 52 | ConjugateGradient::ConjugateGradientState::ConjugateGradientState(Molecule& molecule, |
52 | - boost::shared_ptr<ElectronicStructure>& electronicStructure): | |
53 | + const boost::shared_ptr<ElectronicStructure>& electronicStructure): | |
53 | 54 | OptimizerState(molecule, electronicStructure), |
54 | 55 | oldMatrixForce(NULL), |
55 | 56 | matrixSearchDirection(NULL), |
@@ -81,46 +82,6 @@ void ConjugateGradient::SetMessages(){ | ||
81 | 82 | this->messageStartConjugateGradientStep = "\n========== START: Conjugate gradient step "; |
82 | 83 | } |
83 | 84 | |
84 | -void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStructure, | |
85 | - Molecule& molecule, | |
86 | - double* lineSearchedEnergy, | |
87 | - bool* obtainesOptimizedStructure) const{ | |
88 | - ConjugateGradientState state(molecule,electronicStructure); | |
89 | - | |
90 | - // initial calculation | |
91 | - bool requireGuess = true; | |
92 | - this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); | |
93 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
94 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
95 | - | |
96 | - this->InitializeState(state, molecule); | |
97 | - | |
98 | - for(int s=0; s<state.GetTotalSteps(); s++){ | |
99 | - this->OutputOptimizationStepMessage(s); | |
100 | - | |
101 | - this->PrepareState(state, molecule, electronicStructure, state.GetElecState()); | |
102 | - | |
103 | - this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
104 | - | |
105 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
106 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
107 | - | |
108 | - this->UpdateState(state); | |
109 | - | |
110 | - // check convergence | |
111 | - if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), | |
112 | - molecule, | |
113 | - state.GetInitialEnergy(), | |
114 | - state.GetCurrentEnergy(), | |
115 | - state.GetMaxGradientThreshold(), | |
116 | - state.GetRmsGradientThreshold())){ | |
117 | - *obtainesOptimizedStructure = true; | |
118 | - break; | |
119 | - } | |
120 | - } | |
121 | - *lineSearchedEnergy = state.GetCurrentEnergy(); | |
122 | -} | |
123 | - | |
124 | 85 | void ConjugateGradient::InitializeState(OptimizerState &stateOrig, const Molecule& molecule) const{ |
125 | 86 | ConjugateGradientState& state = stateOrig.CastRef<ConjugateGradientState>(); |
126 | 87 | for(int a=0;a<molecule.GetAtomVect().size();a++){ |
@@ -31,7 +31,7 @@ private: | ||
31 | 31 | ConjugateGradientState(const ConjugateGradientState&); // delete default copy constructor |
32 | 32 | public: |
33 | 33 | ConjugateGradientState(MolDS_base::Molecule& molecule, |
34 | - boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
34 | + const boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
35 | 35 | virtual ~ConjugateGradientState(); |
36 | 36 | double** GetOldMatrixForce(){return this->oldMatrixForce;} |
37 | 37 | double** GetMatrixSearchDirection(){return this->matrixSearchDirection;} |
@@ -46,10 +46,10 @@ private: | ||
46 | 46 | const std::string& OptimizationStepMessage() const{ |
47 | 47 | return this->messageStartConjugateGradientStep; |
48 | 48 | } |
49 | - void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
50 | - MolDS_base::Molecule& molecule, | |
51 | - double* lineSearchedEnergy, | |
52 | - bool* obainesOptimizedStructure) const; | |
49 | + OptimizerState* CreateState(MolDS_base::Molecule& molecule, | |
50 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure) const{ | |
51 | + return new ConjugateGradientState(molecule, electronicStructure); | |
52 | + } | |
53 | 53 | void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const; |
54 | 54 | virtual void PrepareState(OptimizerState& state, |
55 | 55 | const MolDS_base::Molecule& molecule, |
@@ -24,8 +24,10 @@ | ||
24 | 24 | #include<math.h> |
25 | 25 | #include<string> |
26 | 26 | #include<vector> |
27 | +#include<memory> | |
27 | 28 | #include<stdexcept> |
28 | 29 | #include<boost/shared_ptr.hpp> |
30 | +#include<boost/scoped_ptr.hpp> | |
29 | 31 | #include<boost/format.hpp> |
30 | 32 | #include"../config.h" |
31 | 33 | #include"../base/Enums.h" |
@@ -51,7 +53,7 @@ using namespace MolDS_base_factories; | ||
51 | 53 | namespace MolDS_optimization{ |
52 | 54 | |
53 | 55 | Optimizer::OptimizerState::OptimizerState(Molecule& molecule, |
54 | - boost::shared_ptr<ElectronicStructure>& electronicStructure): | |
56 | + const boost::shared_ptr<ElectronicStructure>& electronicStructure): | |
55 | 57 | molecule(molecule), |
56 | 58 | electronicStructure(electronicStructure), |
57 | 59 | elecState(Parameters::GetInstance()->GetElectronicStateIndexOptimization()), |
@@ -107,6 +109,48 @@ void Optimizer::Optimize(Molecule& molecule){ | ||
107 | 109 | this->OutputLog(this->messageEndGeometryOptimization); |
108 | 110 | } |
109 | 111 | |
112 | +void Optimizer::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStructure, | |
113 | + Molecule& molecule, | |
114 | + double* lineSearchedEnergy, | |
115 | + bool* obtainesOptimizedStructure) const{ | |
116 | + boost::scoped_ptr<OptimizerState> statePtr(this->CreateState(molecule, electronicStructure)); | |
117 | + OptimizerState& state = *statePtr.get(); | |
118 | + | |
119 | + // initial calculation | |
120 | + bool requireGuess = true; | |
121 | + this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); | |
122 | + state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
123 | + state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
124 | + | |
125 | + this->InitializeState(state, molecule); | |
126 | + | |
127 | + for(int s=0; s<state.GetTotalSteps(); s++){ | |
128 | + this->OutputOptimizationStepMessage(s); | |
129 | + state.SetInitialEnergy(state.GetCurrentEnergy()); | |
130 | + | |
131 | + this->PrepareState(state, molecule, electronicStructure, state.GetElecState()); | |
132 | + | |
133 | + this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
134 | + | |
135 | + state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
136 | + state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
137 | + | |
138 | + this->UpdateState(state); | |
139 | + | |
140 | + // check convergence | |
141 | + if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), | |
142 | + molecule, | |
143 | + state.GetInitialEnergy(), | |
144 | + state.GetCurrentEnergy(), | |
145 | + state.GetMaxGradientThreshold(), | |
146 | + state.GetRmsGradientThreshold())){ | |
147 | + *obtainesOptimizedStructure = true; | |
148 | + break; | |
149 | + } | |
150 | + } | |
151 | + *lineSearchedEnergy = state.GetCurrentEnergy(); | |
152 | +} | |
153 | + | |
110 | 154 | void Optimizer::SetMessages(){ |
111 | 155 | this->errorMessageTheoryType = "\ttheory type = "; |
112 | 156 | this->errorMessageTotalSteps = "\tTotal steps = "; |
@@ -41,7 +41,7 @@ protected: | ||
41 | 41 | OptimizerState(const OptimizerState&); // delete default copy constructor |
42 | 42 | public: |
43 | 43 | OptimizerState(MolDS_base::Molecule& molecule, |
44 | - boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
44 | + const boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
45 | 45 | virtual ~OptimizerState(){} |
46 | 46 | double& GetCurrentEnergyRef(){return this->currentEnergy;} |
47 | 47 | double GetCurrentEnergy(){return this->currentEnergy;} |
@@ -117,10 +117,14 @@ private: | ||
117 | 117 | void SetEnableTheoryTypes(); |
118 | 118 | void CheckEnableTheoryType(MolDS_base::TheoryType theoryType) const; |
119 | 119 | void ClearMolecularMomenta(MolDS_base::Molecule& molecule) const; |
120 | - virtual void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
121 | - MolDS_base::Molecule& molecule, | |
122 | - double* lineSearchedEnergy, | |
123 | - bool* obainesOptimizedStructure) const = 0; | |
120 | + void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
121 | + MolDS_base::Molecule& molecule, | |
122 | + double* lineSearchedEnergy, | |
123 | + bool* obainesOptimizedStructure) const; | |
124 | + virtual OptimizerState* CreateState(MolDS_base::Molecule& molecule, | |
125 | + const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure) const{ | |
126 | + return new OptimizerState(molecule, electronicStructure); | |
127 | + } | |
124 | 128 | virtual void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const = 0; |
125 | 129 | virtual void PrepareState(OptimizerState& state, |
126 | 130 | const MolDS_base::Molecule& molecule, |
@@ -65,45 +65,6 @@ void SteepestDescent::SetMessages(){ | ||
65 | 65 | this->messageStartSteepestDescentStep = "\n========== START: Steepest Descent step "; |
66 | 66 | } |
67 | 67 | |
68 | -void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStructure, | |
69 | - Molecule& molecule, | |
70 | - double* lineSearchedEnergy, | |
71 | - bool* obtainesOptimizedStructure) const{ | |
72 | - OptimizerState state(molecule, electronicStructure); | |
73 | - | |
74 | - // initial calculation | |
75 | - bool requireGuess = true; | |
76 | - this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); | |
77 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
78 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
79 | - | |
80 | - this->InitializeState(state, molecule); | |
81 | - | |
82 | - for(int s=0; s<state.GetTotalSteps(); s++){ | |
83 | - this->OutputLog(boost::format("%s%d\n\n") % this->messageStartSteepestDescentStep.c_str() % (s+1)); | |
84 | - state.SetInitialEnergy(state.GetCurrentEnergy()); | |
85 | - | |
86 | - this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
87 | - | |
88 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
89 | - state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
90 | - | |
91 | - this->UpdateState(state); | |
92 | - | |
93 | - // check convergence | |
94 | - if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), | |
95 | - molecule, | |
96 | - state.GetInitialEnergy(), | |
97 | - state.GetCurrentEnergy(), | |
98 | - state.GetMaxGradientThreshold(), | |
99 | - state.GetRmsGradientThreshold())){ | |
100 | - *obtainesOptimizedStructure = true; | |
101 | - break; | |
102 | - } | |
103 | - } | |
104 | - *lineSearchedEnergy = state.GetCurrentEnergy(); | |
105 | -} | |
106 | - | |
107 | 68 | void SteepestDescent::CalcNextStepGeometry(Molecule &molecule, |
108 | 69 | OptimizerState& state, |
109 | 70 | boost::shared_ptr<ElectronicStructure> electronicStructure, |
@@ -32,10 +32,6 @@ private: | ||
32 | 32 | const std::string& OptimizationStepMessage() const{ |
33 | 33 | return this->messageStartSteepestDescentStep; |
34 | 34 | } |
35 | - void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure, | |
36 | - MolDS_base::Molecule& molecule, | |
37 | - double* lineSearchedEnergy, | |
38 | - bool* obainesOptimizedStructure) const; | |
39 | 35 | void InitializeState(OptimizerState&, const MolDS_base::Molecule&) const{} |
40 | 36 | virtual void PrepareState(OptimizerState& state, |
41 | 37 | const MolDS_base::Molecule& molecule, |