ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/NLModel1.cpp
Revision: 1015
Committed: Tue Feb 3 22:54:52 2004 UTC (20 years, 5 months ago) by tim
File size: 2061 byte(s)
Log Message:
NLModel0, NLModel1 pass uit test

File Contents

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

Properties

Name Value
svn:executable *