-
Notifications
You must be signed in to change notification settings - Fork 9
/
BandedNumericalJacobian.h
86 lines (68 loc) · 1.92 KB
/
BandedNumericalJacobian.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef BANDEDNUMERICALJACOBIAN_H
#define BANDEDNUMERICALJACOBIAN_H
#include "Definitions.h"
#include "Utilities.h"
#include "NumericalJacobian.h"
namespace Daetk
{
template <class BandedMatrix>
class BandedNumericalJacobian : public NumericalJacobian
{
public:
BandedNumericalJacobian(BandedMatrix& M,VectorFunction& F);
virtual ~BandedNumericalJacobian();
bool evaluate(const Vec& x,const Vec& F);
//mwf dummy for same interface
void setStride(int str) {}
private:
BandedMatrix& matrix;
};
template <class BandedMatrix>
BandedNumericalJacobian<BandedMatrix>::BandedNumericalJacobian(BandedMatrix& M,VectorFunction& F):
NumericalJacobian(F),
matrix(M)
{};
template <class BandedMatrix>
BandedNumericalJacobian<BandedMatrix>::~BandedNumericalJacobian(){}
template <class BandedMatrix>
bool BandedNumericalJacobian<BandedMatrix>::evaluate(const Vec& x,const Vec& Fatx)
{
if (USE_ANALYTICAL_JACOBIAN)
return ajac.evaluate(x,Fatx);
matrix.zeroAll();
int i,j,k,neq=matrix.getNeq(),
kl=matrix.getLowerBandWidth(),
ku=matrix.getUpperBandWidth(),
bandWidth=ku + kl + 1,begin,end;
attachToSubSystem(*Fp,Fatx);
for (j=0;j<bandWidth;j++)
{
tempDelta=0.0;
for (k=j;k<neq;k+=bandWidth)
{
tempDeltaAttache(k) = deltaAttache(k);
}
Fp->correctArgument(tempDelta);
bool evalError=false;
FatxPdelta = Fp->value(evalError);
if (evalError)
{
Fp->unCorrect();
std::cerr<<"Delta in numerical jacobian is causing S or P to be out of range"<<std::endl;
return true;
}
for (k=j;k<neq;k+=bandWidth)
{
real tempDelInverse;
tempDelInverse = -1.0/ deltaAttache(k);
begin=std::max(0,k-ku);
end=std::min(neq,k+kl+1);
for (i=begin;i<end;i++)
matrix(i,k)=tempDelInverse*(FatxPdeltaAttache(i)-FatxAttache(i));
}
Fp->unCorrect();
}
return false;
}
}//Daetk
#endif