ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/NLModel1.cpp
Revision: 1031
Committed: Fri Feb 6 18:58:06 2004 UTC (20 years, 5 months ago) by tim
File size: 2360 byte(s)
Log Message:
Add some lines into global.cpp to make it work with energy minimization

File Contents

# Content
1 #include "NLModel.hpp"
2 #include "Utility.hpp"
3
4
5
6 //calculate hessian using finite difference
7 SymMatrix NLModel1::FiniteHessian(vector<double>& x, vector<double>& h){
8
9 SymMatrix hessian(ndim);
10 vector<double> gplus;
11 vector<double> tempX;
12 double hi;
13
14 #ifdef IS_MPI
15 vector<double> gplusAll;
16 vector<double> tempGradAll;
17
18 //tempGradAll = getAllGrad();
19 #endif
20
21 tempX = x;
22
23 for(int i = 0; i < ndim; i++){
24
25 #ifndef IS_MPI
26 hi = copysign(h[i], tempX[i]);
27
28 tempX[i] += hi;
29
30 gplus = calcGrad(tempX);
31
32 //hessian.Colume(i) = (gplus - currentGrad) / hi;
33
34 //restore tempX to its original value
35 tempX[i] -= hi;
36 #else
37 if(procMappingArray[i] == myRank){
38
39 hi = copysign(h[i], tempX[i]);
40 tempX[i] += hi;
41
42 }
43
44 gplus = calcGrad(tempX);
45
46 gplusAll.clear();
47
48 //very inefficient, need to change
49 for(int j = 0; j , numOfProc; j++){
50 //brocastVector(gplus, j);
51 //gplusAll.insert(gplus);
52 }
53
54 if(procMappingArray[i] == myRank){
55
56 hi = copysign(h[i], tempX[i]);
57 tempX[i] += hi;
58
59 }
60
61 //hessian.Cloume(i) = (gplusAll - tempGradAll) / hi;
62 #endif
63
64 }
65
66 return hessian;
67
68 }
69
70 //----------------------------------------------------------------------------//
71 ConcreteNLModel1::ConcreteNLModel1(int dim, ObjFunctor1* func , ConstraintList* cons)
72 : NLModel1(dim, cons){
73 objfunc = func;
74 }
75 double ConcreteNLModel1::calcF(){
76
77 currentF = (*objfunc)(currentX, currentGrad);
78 numOfFunEval ++;
79
80 return currentF;
81 }
82
83 double ConcreteNLModel1::calcF(vector<double>& x){
84
85 vector<double> tempGrad(x.size());
86
87 double tempF;
88
89 tempF = (*objfunc)(x, tempGrad);
90 numOfFunEval ++;
91
92 return tempF;
93 }
94
95 vector<double> ConcreteNLModel1::calcGrad(){
96
97 currentF = (*objfunc)(currentX, currentGrad);
98
99 return currentGrad;
100
101 }
102
103 vector<double> ConcreteNLModel1::calcGrad(vector<double>& x){
104 vector<double> tempGrad(x.size());
105
106 double tempF;
107
108 tempF = (*objfunc)(x, tempGrad);
109
110 return tempGrad;
111 }
112
113
114 SymMatrix ConcreteNLModel1::calcHessian(){
115 calcGrad(currentX);
116
117 return FiniteHessian(currentX, currentX);
118 }
119
120 SymMatrix ConcreteNLModel1::calcHessian(vector<double>& x){
121 return FiniteHessian(x, x);
122 }
123

Properties

Name Value
svn:executable *