• 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

Revision40e76f60f9a930abd0ca925fc9723ca0da62cdbd (tree)
Zeit2014-01-11 21:21:33
AutorKatsuhiko Nishimra <ktns.87@gmai...>
CommiterKatsuhiko Nishimra

Log Message

Factor out OptimizerState::CastRef(). #32881

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

Ändern Zusammenfassung

Diff

--- a/src/optimization/ConjugateGradient.cpp
+++ b/src/optimization/ConjugateGradient.cpp
@@ -131,32 +131,27 @@ void ConjugateGradient::UpdateSearchDirection(OptimizerState& stateOrig,
131131 boost::shared_ptr<ElectronicStructure> electronicStructure,
132132 const MolDS_base::Molecule& molecule,
133133 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];
149138 }
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];
156147 }
157148 }
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+ }
160155 }
161156 }
162157
--- a/src/optimization/Optimizer.cpp
+++ b/src/optimization/Optimizer.cpp
@@ -49,6 +49,12 @@ using namespace MolDS_base_atoms;
4949 using namespace MolDS_base_factories;
5050
5151 namespace MolDS_optimization{
52+
53+void Optimizer::OptimizerState::SetMessages(){
54+ this->errorMessageFailedToDowncastState
55+ = "Failed to downcast Optimizer::OptimizerState!";
56+}
57+
5258 Optimizer::Optimizer(){
5359 this->SetEnableTheoryTypes();
5460 //this->OutputLog("Optimizer created\n");
@@ -89,8 +95,6 @@ void Optimizer::Optimize(Molecule& molecule){
8995 void Optimizer::SetMessages(){
9096 this->errorMessageTheoryType = "\ttheory type = ";
9197 this->errorMessageTotalSteps = "\tTotal steps = ";
92- this->errorMessageFailedToDowncastState
93- = "Failed to downcast Optimizer::OptimizerState!";
9498 this->messageGeometyrOptimizationMetConvergence
9599 = "\t\tGeometry otimization met convergence criterion(^^b\n\n\n";
96100 this->messageStartGeometryOptimization = "********** START: Geometry optimization **********\n";
--- a/src/optimization/Optimizer.h
+++ b/src/optimization/Optimizer.h
@@ -28,9 +28,11 @@ protected:
2828 double currentEnergy;
2929 double initialEnergy;
3030 double const* const* matrixForce;
31+ std::string errorMessageFailedToDowncastState;
32+ virtual void SetMessages();
3133 public:
3234 OptimizerState():
33- currentEnergy(0.0), initialEnergy(0.0), matrixForce(NULL){}
35+ currentEnergy(0.0), initialEnergy(0.0), matrixForce(NULL){this->SetMessages();}
3436 virtual ~OptimizerState(){}
3537 double& GetCurrentEnergyRef(){return this->currentEnergy;}
3638 double GetCurrentEnergy(){return this->currentEnergy;}
@@ -40,6 +42,15 @@ protected:
4042 void SetCurrentEnergy(double currentEnergy){this->currentEnergy = currentEnergy;}
4143 void SetInitialEnergy(double initialEnergy){this->initialEnergy = initialEnergy;}
4244 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+ }
4354 };
4455 public:
4556 Optimizer();
@@ -48,7 +59,6 @@ public:
4859 protected:
4960 std::string errorMessageNotEnebleTheoryType;
5061 std::string errorMessageGeometyrOptimizationNotConverged;
51- std::string errorMessageFailedToDowncastState;
5262 std::string messageLineSearchSteps;
5363 virtual void SetMessages();
5464 void UpdateMolecularCoordinates(MolDS_base::Molecule& molecule, double const* const* matrixForce, double dt) const;