• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

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

Commit MetaInfo

修訂73d05e5186cf12b747bcb2a765b789335744a1f0 (tree)
時間2014-01-17 11:24:12
作者Katsuhiko Nishimra <ktns.87@gmai...>
CommiterKatsuhiko Nishimra

Log Message

Factor out common data into OptimzerState. #32881

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

Change Summary

差異

--- a/src/optimization/BFGS.cpp
+++ b/src/optimization/BFGS.cpp
@@ -53,13 +53,17 @@ using namespace MolDS_base_atoms;
5353
5454 namespace MolDS_optimization{
5555
56-BFGS::BFGSState::BFGSState(Molecule& molecule):
57- matrixHessian(NULL),
58- matrixOldForce(NULL),
59- matrixStep(NULL),
60- matrixOldCoordinates(NULL),
61- matrixDisplacement(NULL),
62- numAtoms(molecule.GetAtomVect().size())
56+BFGS::BFGSState::BFGSState(Molecule& molecule,
57+ boost::shared_ptr<ElectronicStructure>& electronicStructure):
58+ OptimizerState(molecule, electronicStructure),
59+ matrixHessian(NULL),
60+ matrixOldForce(NULL),
61+ matrixStep(NULL),
62+ matrixOldCoordinates(NULL),
63+ matrixDisplacement(NULL),
64+ trustRadius(Parameters::GetInstance()->GetInitialTrustRadiusOptimization()),
65+ maxNormStep(Parameters::GetInstance()->GetMaxNormStepOptimization()),
66+ numAtoms(molecule.GetAtomVect().size())
6367 {
6468 const int dimension = numAtoms * CartesianType_end;
6569 MallocerFreer::GetInstance()->Malloc(&this->matrixHessian, dimension, dimension);
@@ -129,43 +133,36 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct
129133 Molecule& molecule,
130134 double* lineSearchedEnergy,
131135 bool* obtainesOptimizedStructure) const {
132- int elecState = Parameters::GetInstance()->GetElectronicStateIndexOptimization();
133- double dt = Parameters::GetInstance()->GetTimeWidthOptimization();
134- int totalSteps = Parameters::GetInstance()->GetTotalStepsOptimization();
135- double maxGradientThreshold = Parameters::GetInstance()->GetMaxGradientOptimization();
136- double rmsGradientThreshold = Parameters::GetInstance()->GetRmsGradientOptimization();
137136 const int dimension = molecule.GetAtomVect().size()*CartesianType_end;
138- BFGSState state(molecule);
139- state.SetTrustRadius(Parameters::GetInstance()->GetInitialTrustRadiusOptimization());
140- state.SetMaxNormStep(Parameters::GetInstance()->GetMaxNormStepOptimization());
137+ BFGSState state(molecule, electronicStructure);
141138
142139 // initial calculation
143140 bool requireGuess = true;
144141 this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs());
145- state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(elecState));
142+ state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
146143
147- state.SetMatrixForce(electronicStructure->GetForce(elecState));
144+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
148145
149146 this->InitializeState(state, molecule);
150147
151- for(int s=0; s<totalSteps; s++){
148+ for(int s=0; s<state.GetTotalSteps(); s++){
152149 this->OutputOptimizationStepMessage(s);
153150
154- this->PrepareState(state, molecule, electronicStructure, elecState);
151+ this->PrepareState(state, molecule, electronicStructure, state.GetElecState());
155152
156- this->CalcNextStepGeometry(molecule, state, electronicStructure, elecState, dt);
153+ this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT());
157154
158155 this->UpdateTrustRadius(state.GetTrustRadiusRef(), state.GetApproximateChange(), state.GetInitialEnergy(), state.GetCurrentEnergy());
159156
160- state.SetMatrixForce(electronicStructure->GetForce(elecState));
157+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
161158
162159 // check convergence
163160 if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(),
164161 molecule,
165162 state.GetInitialEnergy(),
166163 state.GetCurrentEnergy(),
167- maxGradientThreshold,
168- rmsGradientThreshold)){
164+ state.GetMaxGradientThreshold(),
165+ state.GetRmsGradientThreshold())){
169166 *obtainesOptimizedStructure = true;
170167 break;
171168 }
--- a/src/optimization/BFGS.h
+++ b/src/optimization/BFGS.h
@@ -39,7 +39,8 @@ private:
3939 template<class vector>
4040 vector Matrix2Vector(vector const* matrix){return matrix == NULL ? NULL : &matrix[0][0];}
4141 public:
42- BFGSState(MolDS_base::Molecule& molecule);
42+ BFGSState(MolDS_base::Molecule& molecule,
43+ boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
4344 virtual ~BFGSState();
4445 double const* GetVectorForce (){return this->Matrix2Vector(this->matrixForce);}
4546 double* GetVectorOldForce (){return this->Matrix2Vector(this->matrixOldForce);}
--- a/src/optimization/ConjugateGradient.cpp
+++ b/src/optimization/ConjugateGradient.cpp
@@ -48,8 +48,12 @@ using namespace MolDS_base_atoms;
4848
4949 namespace MolDS_optimization{
5050
51-ConjugateGradient::ConjugateGradientState::ConjugateGradientState(Molecule& molecule):
52- OptimizerState(), oldMatrixForce(NULL), matrixSearchDirection(NULL), numAtoms(molecule.GetAtomVect().size()){
51+ConjugateGradient::ConjugateGradientState::ConjugateGradientState(Molecule& molecule,
52+ boost::shared_ptr<ElectronicStructure>& electronicStructure):
53+ OptimizerState(molecule, electronicStructure),
54+ oldMatrixForce(NULL),
55+ matrixSearchDirection(NULL),
56+ numAtoms(molecule.GetAtomVect().size()){
5357 MallocerFreer::GetInstance()->Malloc<double>(&this->oldMatrixForce , this->numAtoms, CartesianType_end);
5458 MallocerFreer::GetInstance()->Malloc<double>(&this->matrixSearchDirection, this->numAtoms, CartesianType_end);
5559 }
@@ -81,40 +85,35 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele
8185 Molecule& molecule,
8286 double* lineSearchedEnergy,
8387 bool* obtainesOptimizedStructure) const{
84- int elecState = Parameters::GetInstance()->GetElectronicStateIndexOptimization();
85- double dt = Parameters::GetInstance()->GetTimeWidthOptimization();
86- int totalSteps = Parameters::GetInstance()->GetTotalStepsOptimization();
87- double maxGradientThreshold = Parameters::GetInstance()->GetMaxGradientOptimization();
88- double rmsGradientThreshold = Parameters::GetInstance()->GetRmsGradientOptimization();
89- ConjugateGradientState state(molecule);
88+ ConjugateGradientState state(molecule,electronicStructure);
9089
9190 // initial calculation
9291 bool requireGuess = true;
9392 this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs());
94- state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(elecState));
93+ state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
9594
9695 requireGuess = false;
97- state.SetMatrixForce(electronicStructure->GetForce(elecState));
96+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
9897
9998 this->InitializeState(state, molecule);
10099
101100 // conugate gradient loop
102- for(int s=0; s<totalSteps; s++){
101+ for(int s=0; s<state.GetTotalSteps(); s++){
103102 this->OutputOptimizationStepMessage(s);
104103
105- this->PrepareState(state, molecule, electronicStructure, elecState);
104+ this->PrepareState(state, molecule, electronicStructure, state.GetElecState());
106105
107- this->CalcNextStepGeometry(molecule, state, electronicStructure, elecState, dt);
106+ this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT());
108107
109- this->UpdateSearchDirection(state, electronicStructure, molecule, elecState);
108+ this->UpdateSearchDirection(state, electronicStructure, molecule, state.GetElecState());
110109
111110 // check convergence
112111 if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(),
113112 molecule,
114113 state.GetInitialEnergy(),
115114 state.GetCurrentEnergy(),
116- maxGradientThreshold,
117- rmsGradientThreshold)){
115+ state.GetMaxGradientThreshold(),
116+ state.GetRmsGradientThreshold())){
118117 *obtainesOptimizedStructure = true;
119118 break;
120119 }
--- a/src/optimization/ConjugateGradient.h
+++ b/src/optimization/ConjugateGradient.h
@@ -29,7 +29,8 @@ private:
2929 double** matrixSearchDirection;
3030 size_t numAtoms;
3131 public:
32- ConjugateGradientState(MolDS_base::Molecule& molecule);
32+ ConjugateGradientState(MolDS_base::Molecule& molecule,
33+ boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
3334 virtual ~ConjugateGradientState();
3435 double** GetOldMatrixForce(){return this->oldMatrixForce;}
3536 double** GetMatrixSearchDirection(){return this->matrixSearchDirection;}
--- a/src/optimization/Optimizer.cpp
+++ b/src/optimization/Optimizer.cpp
@@ -50,6 +50,21 @@ using namespace MolDS_base_factories;
5050
5151 namespace MolDS_optimization{
5252
53+Optimizer::OptimizerState::OptimizerState(Molecule& molecule,
54+ boost::shared_ptr<ElectronicStructure>& electronicStructure):
55+ molecule(molecule),
56+ electronicStructure(electronicStructure),
57+ elecState(Parameters::GetInstance()->GetElectronicStateIndexOptimization()),
58+ dt(Parameters::GetInstance()->GetTimeWidthOptimization()),
59+ totalSteps(Parameters::GetInstance()->GetTotalStepsOptimization()),
60+ maxGradientThreshold(Parameters::GetInstance()->GetMaxGradientOptimization()),
61+ rmsGradientThreshold(Parameters::GetInstance()->GetRmsGradientOptimization()),
62+ currentEnergy(0.0),
63+ initialEnergy(0.0),
64+ matrixForce(NULL){
65+ this->SetMessages();
66+}
67+
5368 void Optimizer::OptimizerState::SetMessages(){
5469 this->errorMessageFailedToDowncastState
5570 = "Failed to downcast Optimizer::OptimizerState!";
--- a/src/optimization/Optimizer.h
+++ b/src/optimization/Optimizer.h
@@ -25,17 +25,33 @@ class Optimizer : public MolDS_base::PrintController{
2525 protected:
2626 class OptimizerState{
2727 protected:
28+ MolDS_base::Molecule& molecule;
29+ const boost::shared_ptr<MolDS_base::ElectronicStructure> electronicStructure;
30+ const int elecState;
31+ const double dt;
32+ const int totalSteps;
33+ const double maxGradientThreshold;
34+ const double rmsGradientThreshold;
2835 double currentEnergy;
2936 double initialEnergy;
3037 double const* const* matrixForce;
3138 std::string errorMessageFailedToDowncastState;
3239 virtual void SetMessages();
3340 public:
34- OptimizerState():
35- currentEnergy(0.0), initialEnergy(0.0), matrixForce(NULL){this->SetMessages();}
41+ OptimizerState(MolDS_base::Molecule& molecule,
42+ boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure);
3643 virtual ~OptimizerState(){}
3744 double& GetCurrentEnergyRef(){return this->currentEnergy;}
3845 double GetCurrentEnergy(){return this->currentEnergy;}
46+ MolDS_base::Molecule& GetMolecule(){return this->molecule;}
47+ const boost::shared_ptr<MolDS_base::ElectronicStructure> GetElectronicStructure(){
48+ return this->electronicStructure;
49+ }
50+ int GetElecState(){return this->elecState;}
51+ double GetDeltaT(){return this->dt;}
52+ int GetTotalSteps(){return this->totalSteps;}
53+ double GetMaxGradientThreshold(){return this->maxGradientThreshold;}
54+ double GetRmsGradientThreshold(){return this->rmsGradientThreshold;}
3955 double GetInitialEnergy(){return this->initialEnergy;}
4056 double const* const* GetMatrixForce(){return this->matrixForce;}
4157 double const* const** GetMatrixForcePtr(){return &this->matrixForce;}
--- a/src/optimization/SteepestDescent.cpp
+++ b/src/optimization/SteepestDescent.cpp
@@ -69,35 +69,30 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect
6969 Molecule& molecule,
7070 double* lineSearchedEnergy,
7171 bool* obtainesOptimizedStructure) const{
72- int elecState = Parameters::GetInstance()->GetElectronicStateIndexOptimization();
73- double dt = Parameters::GetInstance()->GetTimeWidthOptimization();
74- int totalSteps = Parameters::GetInstance()->GetTotalStepsOptimization();
75- double maxGradientThreshold = Parameters::GetInstance()->GetMaxGradientOptimization();
76- double rmsGradientThreshold = Parameters::GetInstance()->GetRmsGradientOptimization();
77- OptimizerState state;
72+ OptimizerState state(molecule, electronicStructure);
7873
7974 // initial calculation
8075 bool requireGuess = true;
8176 this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs());
8277
8378 requireGuess = false;
84- state.SetMatrixForce(electronicStructure->GetForce(elecState));
85- state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(elecState));
86- for(int s=0; s<totalSteps; s++){
79+ state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState()));
80+ state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState()));
81+ for(int s=0; s<state.GetTotalSteps(); s++){
8782 this->OutputLog(boost::format("%s%d\n\n") % this->messageStartSteepestDescentStep.c_str() % (s+1));
8883 state.SetInitialEnergy(state.GetCurrentEnergy());
8984
90- this->CalcNextStepGeometry(molecule, state, electronicStructure, elecState, dt);
85+ this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT());
9186
92- this->UpdateSearchDirection(state, electronicStructure, molecule, elecState);
87+ this->UpdateSearchDirection(state, electronicStructure, molecule, state.GetElecState());
9388
9489 // check convergence
9590 if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(),
9691 molecule,
9792 state.GetInitialEnergy(),
9893 state.GetCurrentEnergy(),
99- maxGradientThreshold,
100- rmsGradientThreshold)){
94+ state.GetMaxGradientThreshold(),
95+ state.GetRmsGradientThreshold())){
10196 *obtainesOptimizedStructure = true;
10297 break;
10398 }