修訂 | 73d05e5186cf12b747bcb2a765b789335744a1f0 (tree) |
---|---|
時間 | 2014-01-17 11:24:12 |
作者 | Katsuhiko Nishimra <ktns.87@gmai...> |
Commiter | Katsuhiko Nishimra |
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
@@ -53,13 +53,17 @@ using namespace MolDS_base_atoms; | ||
53 | 53 | |
54 | 54 | namespace MolDS_optimization{ |
55 | 55 | |
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()) | |
63 | 67 | { |
64 | 68 | const int dimension = numAtoms * CartesianType_end; |
65 | 69 | MallocerFreer::GetInstance()->Malloc(&this->matrixHessian, dimension, dimension); |
@@ -129,43 +133,36 @@ void BFGS::SearchMinimum(boost::shared_ptr<ElectronicStructure> electronicStruct | ||
129 | 133 | Molecule& molecule, |
130 | 134 | double* lineSearchedEnergy, |
131 | 135 | 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(); | |
137 | 136 | 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); | |
141 | 138 | |
142 | 139 | // initial calculation |
143 | 140 | bool requireGuess = true; |
144 | 141 | this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); |
145 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(elecState)); | |
142 | + state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
146 | 143 | |
147 | - state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
144 | + state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
148 | 145 | |
149 | 146 | this->InitializeState(state, molecule); |
150 | 147 | |
151 | - for(int s=0; s<totalSteps; s++){ | |
148 | + for(int s=0; s<state.GetTotalSteps(); s++){ | |
152 | 149 | this->OutputOptimizationStepMessage(s); |
153 | 150 | |
154 | - this->PrepareState(state, molecule, electronicStructure, elecState); | |
151 | + this->PrepareState(state, molecule, electronicStructure, state.GetElecState()); | |
155 | 152 | |
156 | - this->CalcNextStepGeometry(molecule, state, electronicStructure, elecState, dt); | |
153 | + this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
157 | 154 | |
158 | 155 | this->UpdateTrustRadius(state.GetTrustRadiusRef(), state.GetApproximateChange(), state.GetInitialEnergy(), state.GetCurrentEnergy()); |
159 | 156 | |
160 | - state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
157 | + state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
161 | 158 | |
162 | 159 | // check convergence |
163 | 160 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
164 | 161 | molecule, |
165 | 162 | state.GetInitialEnergy(), |
166 | 163 | state.GetCurrentEnergy(), |
167 | - maxGradientThreshold, | |
168 | - rmsGradientThreshold)){ | |
164 | + state.GetMaxGradientThreshold(), | |
165 | + state.GetRmsGradientThreshold())){ | |
169 | 166 | *obtainesOptimizedStructure = true; |
170 | 167 | break; |
171 | 168 | } |
@@ -39,7 +39,8 @@ private: | ||
39 | 39 | template<class vector> |
40 | 40 | vector Matrix2Vector(vector const* matrix){return matrix == NULL ? NULL : &matrix[0][0];} |
41 | 41 | public: |
42 | - BFGSState(MolDS_base::Molecule& molecule); | |
42 | + BFGSState(MolDS_base::Molecule& molecule, | |
43 | + boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
43 | 44 | virtual ~BFGSState(); |
44 | 45 | double const* GetVectorForce (){return this->Matrix2Vector(this->matrixForce);} |
45 | 46 | double* GetVectorOldForce (){return this->Matrix2Vector(this->matrixOldForce);} |
@@ -48,8 +48,12 @@ using namespace MolDS_base_atoms; | ||
48 | 48 | |
49 | 49 | namespace MolDS_optimization{ |
50 | 50 | |
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()){ | |
53 | 57 | MallocerFreer::GetInstance()->Malloc<double>(&this->oldMatrixForce , this->numAtoms, CartesianType_end); |
54 | 58 | MallocerFreer::GetInstance()->Malloc<double>(&this->matrixSearchDirection, this->numAtoms, CartesianType_end); |
55 | 59 | } |
@@ -81,40 +85,35 @@ void ConjugateGradient::SearchMinimum(boost::shared_ptr<ElectronicStructure> ele | ||
81 | 85 | Molecule& molecule, |
82 | 86 | double* lineSearchedEnergy, |
83 | 87 | 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); | |
90 | 89 | |
91 | 90 | // initial calculation |
92 | 91 | bool requireGuess = true; |
93 | 92 | this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); |
94 | - state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(elecState)); | |
93 | + state.SetCurrentEnergy(electronicStructure->GetElectronicEnergy(state.GetElecState())); | |
95 | 94 | |
96 | 95 | requireGuess = false; |
97 | - state.SetMatrixForce(electronicStructure->GetForce(elecState)); | |
96 | + state.SetMatrixForce(electronicStructure->GetForce(state.GetElecState())); | |
98 | 97 | |
99 | 98 | this->InitializeState(state, molecule); |
100 | 99 | |
101 | 100 | // conugate gradient loop |
102 | - for(int s=0; s<totalSteps; s++){ | |
101 | + for(int s=0; s<state.GetTotalSteps(); s++){ | |
103 | 102 | this->OutputOptimizationStepMessage(s); |
104 | 103 | |
105 | - this->PrepareState(state, molecule, electronicStructure, elecState); | |
104 | + this->PrepareState(state, molecule, electronicStructure, state.GetElecState()); | |
106 | 105 | |
107 | - this->CalcNextStepGeometry(molecule, state, electronicStructure, elecState, dt); | |
106 | + this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
108 | 107 | |
109 | - this->UpdateSearchDirection(state, electronicStructure, molecule, elecState); | |
108 | + this->UpdateSearchDirection(state, electronicStructure, molecule, state.GetElecState()); | |
110 | 109 | |
111 | 110 | // check convergence |
112 | 111 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
113 | 112 | molecule, |
114 | 113 | state.GetInitialEnergy(), |
115 | 114 | state.GetCurrentEnergy(), |
116 | - maxGradientThreshold, | |
117 | - rmsGradientThreshold)){ | |
115 | + state.GetMaxGradientThreshold(), | |
116 | + state.GetRmsGradientThreshold())){ | |
118 | 117 | *obtainesOptimizedStructure = true; |
119 | 118 | break; |
120 | 119 | } |
@@ -29,7 +29,8 @@ private: | ||
29 | 29 | double** matrixSearchDirection; |
30 | 30 | size_t numAtoms; |
31 | 31 | public: |
32 | - ConjugateGradientState(MolDS_base::Molecule& molecule); | |
32 | + ConjugateGradientState(MolDS_base::Molecule& molecule, | |
33 | + boost::shared_ptr<MolDS_base::ElectronicStructure>& electronicStructure); | |
33 | 34 | virtual ~ConjugateGradientState(); |
34 | 35 | double** GetOldMatrixForce(){return this->oldMatrixForce;} |
35 | 36 | double** GetMatrixSearchDirection(){return this->matrixSearchDirection;} |
@@ -50,6 +50,21 @@ using namespace MolDS_base_factories; | ||
50 | 50 | |
51 | 51 | namespace MolDS_optimization{ |
52 | 52 | |
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 | + | |
53 | 68 | void Optimizer::OptimizerState::SetMessages(){ |
54 | 69 | this->errorMessageFailedToDowncastState |
55 | 70 | = "Failed to downcast Optimizer::OptimizerState!"; |
@@ -25,17 +25,33 @@ class Optimizer : public MolDS_base::PrintController{ | ||
25 | 25 | protected: |
26 | 26 | class OptimizerState{ |
27 | 27 | 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; | |
28 | 35 | double currentEnergy; |
29 | 36 | double initialEnergy; |
30 | 37 | double const* const* matrixForce; |
31 | 38 | std::string errorMessageFailedToDowncastState; |
32 | 39 | virtual void SetMessages(); |
33 | 40 | 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); | |
36 | 43 | virtual ~OptimizerState(){} |
37 | 44 | double& GetCurrentEnergyRef(){return this->currentEnergy;} |
38 | 45 | 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;} | |
39 | 55 | double GetInitialEnergy(){return this->initialEnergy;} |
40 | 56 | double const* const* GetMatrixForce(){return this->matrixForce;} |
41 | 57 | double const* const** GetMatrixForcePtr(){return &this->matrixForce;} |
@@ -69,35 +69,30 @@ void SteepestDescent::SearchMinimum(boost::shared_ptr<ElectronicStructure> elect | ||
69 | 69 | Molecule& molecule, |
70 | 70 | double* lineSearchedEnergy, |
71 | 71 | 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); | |
78 | 73 | |
79 | 74 | // initial calculation |
80 | 75 | bool requireGuess = true; |
81 | 76 | this->UpdateElectronicStructure(electronicStructure, molecule, requireGuess, this->CanOutputLogs()); |
82 | 77 | |
83 | 78 | 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++){ | |
87 | 82 | this->OutputLog(boost::format("%s%d\n\n") % this->messageStartSteepestDescentStep.c_str() % (s+1)); |
88 | 83 | state.SetInitialEnergy(state.GetCurrentEnergy()); |
89 | 84 | |
90 | - this->CalcNextStepGeometry(molecule, state, electronicStructure, elecState, dt); | |
85 | + this->CalcNextStepGeometry(molecule, state, electronicStructure, state.GetElecState(), state.GetDeltaT()); | |
91 | 86 | |
92 | - this->UpdateSearchDirection(state, electronicStructure, molecule, elecState); | |
87 | + this->UpdateSearchDirection(state, electronicStructure, molecule, state.GetElecState()); | |
93 | 88 | |
94 | 89 | // check convergence |
95 | 90 | if(this->SatisfiesConvergenceCriterion(state.GetMatrixForce(), |
96 | 91 | molecule, |
97 | 92 | state.GetInitialEnergy(), |
98 | 93 | state.GetCurrentEnergy(), |
99 | - maxGradientThreshold, | |
100 | - rmsGradientThreshold)){ | |
94 | + state.GetMaxGradientThreshold(), | |
95 | + state.GetRmsGradientThreshold())){ | |
101 | 96 | *obtainesOptimizedStructure = true; |
102 | 97 | break; |
103 | 98 | } |