• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

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

Commit MetaInfo

Revisiond61b1cc2054ed35082654d07b148b0a583c561fb (tree)
Zeit2014-01-17 11:24:19
AutorKatsuhiko Nishimra <ktns.87@gmai...>
CommiterKatsuhiko Nishimra

Log Message

Unify Optimizer::SearchMinimum. #32881

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

Ändern Zusammenfassung

Diff

--- a/src/optimization/BFGS.cpp
+++ b/src/optimization/BFGS.cpp
@@ -54,7 +54,7 @@ using namespace MolDS_base_atoms;
5454 namespace MolDS_optimization{
5555
5656 BFGS::BFGSState::BFGSState(Molecule& molecule,
57- boost::shared_ptr<ElectronicStructure>& electronicStructure):
57+ const boost::shared_ptr<ElectronicStructure>& electronicStructure):
5858 OptimizerState(molecule, electronicStructure),
5959 matrixHessian(NULL),
6060 matrixOldForce(NULL),
@@ -129,46 +129,6 @@ void BFGS::SetMessages(){
129129 = "Scaling factor is increased to %e.\n";
130130 }
131131
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-
172132 void BFGS::InitializeState(OptimizerState &stateOrig, const Molecule& molecule) const{
173133 const MolDS_wrappers::molds_blas_int dimension = molecule.GetAtomVect().size()*CartesianType_end;
174134 const double one = 1;
--- a/src/optimization/BFGS.h
+++ b/src/optimization/BFGS.h
@@ -41,7 +41,7 @@ private:
4141 BFGSState(const BFGSState&); // delete default copy constructor
4242 public:
4343 BFGSState(MolDS_base::Molecule& molecule,
44- boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
44+ const boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
4545 virtual ~BFGSState();
4646 double const* GetVectorForce (){return this->Matrix2Vector(this->matrixForce);}
4747 double* GetVectorOldForce (){return this->Matrix2Vector(this->matrixOldForce);}
@@ -87,12 +87,11 @@ private:
8787 const std::string& OptimizationStepMessage() const{
8888 return this->messageStartBFGSStep;
8989 }
90- virtual void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
91- MolDS_base::Molecule& molecule,
92- double* lineSearchedEnergy,
93- bool* obainesOptimizedStructure) const;
94-
9590 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+ }
9695 void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const;
9796 void PrepareState(OptimizerState& state,
9897 const MolDS_base::Molecule& molecule,
--- a/src/optimization/ConjugateGradient.cpp
+++ b/src/optimization/ConjugateGradient.cpp
@@ -26,6 +26,7 @@
2626 #include<vector>
2727 #include<stdexcept>
2828 #include<boost/shared_ptr.hpp>
29+#include<boost/scoped_ptr.hpp>
2930 #include<boost/format.hpp>
3031 #include"../base/Enums.h"
3132 #include"../base/Uncopyable.h"
@@ -49,7 +50,7 @@ using namespace MolDS_base_atoms;
4950 namespace MolDS_optimization{
5051
5152 ConjugateGradient::ConjugateGradientState::ConjugateGradientState(Molecule& molecule,
52- boost::shared_ptr<ElectronicStructure>& electronicStructure):
53+ const boost::shared_ptr<ElectronicStructure>& electronicStructure):
5354 OptimizerState(molecule, electronicStructure),
5455 oldMatrixForce(NULL),
5556 matrixSearchDirection(NULL),
@@ -81,46 +82,6 @@ void ConjugateGradient::SetMessages(){
8182 this->messageStartConjugateGradientStep = "\n========== START: Conjugate gradient step ";
8283 }
8384
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-
12485 void ConjugateGradient::InitializeState(OptimizerState &stateOrig, const Molecule& molecule) const{
12586 ConjugateGradientState& state = stateOrig.CastRef<ConjugateGradientState>();
12687 for(int a=0;a<molecule.GetAtomVect().size();a++){
--- a/src/optimization/ConjugateGradient.h
+++ b/src/optimization/ConjugateGradient.h
@@ -31,7 +31,7 @@ private:
3131 ConjugateGradientState(const ConjugateGradientState&); // delete default copy constructor
3232 public:
3333 ConjugateGradientState(MolDS_base::Molecule& molecule,
34- boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
34+ const boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
3535 virtual ~ConjugateGradientState();
3636 double** GetOldMatrixForce(){return this->oldMatrixForce;}
3737 double** GetMatrixSearchDirection(){return this->matrixSearchDirection;}
@@ -46,10 +46,10 @@ private:
4646 const std::string& OptimizationStepMessage() const{
4747 return this->messageStartConjugateGradientStep;
4848 }
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+ }
5353 void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const;
5454 virtual void PrepareState(OptimizerState& state,
5555 const MolDS_base::Molecule& molecule,
--- a/src/optimization/Optimizer.cpp
+++ b/src/optimization/Optimizer.cpp
@@ -24,8 +24,10 @@
2424 #include<math.h>
2525 #include<string>
2626 #include<vector>
27+#include<memory>
2728 #include<stdexcept>
2829 #include<boost/shared_ptr.hpp>
30+#include<boost/scoped_ptr.hpp>
2931 #include<boost/format.hpp>
3032 #include"../config.h"
3133 #include"../base/Enums.h"
@@ -51,7 +53,7 @@ using namespace MolDS_base_factories;
5153 namespace MolDS_optimization{
5254
5355 Optimizer::OptimizerState::OptimizerState(Molecule& molecule,
54- boost::shared_ptr<ElectronicStructure>& electronicStructure):
56+ const boost::shared_ptr<ElectronicStructure>& electronicStructure):
5557 molecule(molecule),
5658 electronicStructure(electronicStructure),
5759 elecState(Parameters::GetInstance()->GetElectronicStateIndexOptimization()),
@@ -107,6 +109,48 @@ void Optimizer::Optimize(Molecule& molecule){
107109 this->OutputLog(this->messageEndGeometryOptimization);
108110 }
109111
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+
110154 void Optimizer::SetMessages(){
111155 this->errorMessageTheoryType = "\ttheory type = ";
112156 this->errorMessageTotalSteps = "\tTotal steps = ";
--- a/src/optimization/Optimizer.h
+++ b/src/optimization/Optimizer.h
@@ -41,7 +41,7 @@ protected:
4141 OptimizerState(const OptimizerState&); // delete default copy constructor
4242 public:
4343 OptimizerState(MolDS_base::Molecule& molecule,
44- boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
44+ const boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
4545 virtual ~OptimizerState(){}
4646 double& GetCurrentEnergyRef(){return this->currentEnergy;}
4747 double GetCurrentEnergy(){return this->currentEnergy;}
@@ -117,10 +117,14 @@ private:
117117 void SetEnableTheoryTypes();
118118 void CheckEnableTheoryType(MolDS_base::TheoryType theoryType) const;
119119 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+ }
124128 virtual void InitializeState(OptimizerState &state, const MolDS_base::Molecule& molecule) const = 0;
125129 virtual void PrepareState(OptimizerState& state,
126130 const MolDS_base::Molecule& molecule,
--- a/src/optimization/SteepestDescent.cpp
+++ b/src/optimization/SteepestDescent.cpp
@@ -65,45 +65,6 @@ void SteepestDescent::SetMessages(){
6565 this->messageStartSteepestDescentStep = "\n========== START: Steepest Descent step ";
6666 }
6767
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-
10768 void SteepestDescent::CalcNextStepGeometry(Molecule &molecule,
10869 OptimizerState& state,
10970 boost::shared_ptr<ElectronicStructure> electronicStructure,
--- a/src/optimization/SteepestDescent.h
+++ b/src/optimization/SteepestDescent.h
@@ -32,10 +32,6 @@ private:
3232 const std::string& OptimizationStepMessage() const{
3333 return this->messageStartSteepestDescentStep;
3434 }
35- void SearchMinimum(boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure,
36- MolDS_base::Molecule& molecule,
37- double* lineSearchedEnergy,
38- bool* obainesOptimizedStructure) const;
3935 void InitializeState(OptimizerState&, const MolDS_base::Molecule&) const{}
4036 virtual void PrepareState(OptimizerState& state,
4137 const MolDS_base::Molecule& molecule,