Repository navigation
Expand file tree
/
Copy pathdgpadm.F
More file actions
220 lines (215 loc) · 6.43 KB
/
Copy pathdgpadm.F
File metadata and controls
220 lines (215 loc) · 6.43 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
c
c
c
c from the expokit package, see comments below for copyright etc.
c
c $Id: dgpadm.F,v 1.3 2002/12/27 19:56:02 becker Exp $
c
*----------------------------------------------------------------------|
subroutine DGPADM( ideg,m,t,H,ldh,wsp,lwsp,ipiv,
& iexph,ns,iflag )
#include "fortran_precision.h"
c implicit none
integer ideg, m, ldh, lwsp, iexph, ns, iflag, ipiv(m)
COMP_PRECISION t, H(ldh,m), wsp(lwsp)
*-----Purpose----------------------------------------------------------|
*
* Computes exp(t*H), the matrix exponential of a general matrix in
* full, using the irreducible rational Pade approximation to the
* exponential function exp(x) = r(x) = (+/-)( I + 2*(q(x)/p(x)) ),
* combined with scaling-and-squaring.
*
*-----Arguments--------------------------------------------------------|
*
* ideg : (input) the degre of the diagonal Pade to be used.
* a value of 6 is generally satisfactory.
*
* m : (input) order of H.
*
* H(ldh,m) : (input) argument matrix.
*
* t : (input) time-scale (can be < 0).
*
* wsp(lwsp) : (workspace/output) lwsp .ge. 4*m*m+ideg+1.
*
* ipiv(m) : (workspace)
*
*>>>> iexph : (output) number such that wsp(iexph) points to exp(tH)
* i.e., exp(tH) is located at wsp(iexph ... iexph+m*m-1)
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* NOTE: if the routine was called with wsp(iptr),
* then exp(tH) will start at wsp(iptr+iexph-1).
*
* ns : (output) number of scaling-squaring used.
*
* iflag : (output) exit flag.
* 0 - no problem
* <0 - problem
*
*----------------------------------------------------------------------|
* Roger B. Sidje (rbs@maths.uq.edu.au)
* EXPOKIT: Software Package for Computing Matrix Exponentials.
* ACM - Transactions On Mathematical Software, 24(1):130-156, 1998
*----------------------------------------------------------------------|
*
integer mm,i,j,k,ih2,ip,iq,iused,ifree,iodd,icoef,iput,iget
COMP_PRECISION hnorm,scale,scale2,cp,cq
intrinsic INT,ABS,DBLE,LOG,MAX
*--- check restrictions on input parameters ...
mm = m*m
iflag = 0
if ( ldh.lt.m ) iflag = -1
if ( lwsp.lt.4*mm+ideg+1 ) iflag = -2
if ( iflag.ne.0 ) stop 'bad sizes (in input of DGPADM)'
*
*--- initialise pointers ...
*
icoef = 1
ih2 = icoef + (ideg+1)
ip = ih2 + mm
iq = ip + mm
ifree = iq + mm
*
*--- scaling: seek ns such that ||t*H/2^ns|| < 1/2;
* and set scale = t/2^ns ...
*
do i = 1,m
wsp(i) = 0.0d0
enddo
do j = 1,m
do i = 1,m
wsp(i) = wsp(i) + ABS( H(i,j) )
enddo
enddo
hnorm = 0.0d0
do i = 1,m
hnorm = MAX( hnorm,wsp(i) )
enddo
hnorm = ABS( t*hnorm )
if ( hnorm.eq.0.0d0 ) then
print *,'Error - null H in input of DGPADM.'
print *,'h:',h
print *,'t:',t,' hnorm:',hnorm
stop
endif
ns = MAX( 0,INT(LOG(hnorm)/LOG(2.0d0))+2 )
scale = t / DBLE(2**ns)
scale2 = scale*scale
*
*--- compute Pade coefficients ...
*
i = ideg+1
j = 2*ideg+1
wsp(icoef) = 1.0d0
do k = 1,ideg
wsp(icoef+k) = (wsp(icoef+k-1)*DBLE( i-k ))/DBLE( k*(j-k) )
enddo
*
*--- H2 = scale2*H*H ...
*
#ifndef SINGLE_PRECISION
call DGEMM( 'n','n',m,m,m,scale2,H,ldh,H,ldh,0.0d0,wsp(ih2),m )
#else
call SGEMM( 'n','n',m,m,m,scale2,H,ldh,H,ldh,0.0,wsp(ih2),m )
#endif
*
*--- initialize p (numerator) and q (denominator) ...
*
cp = wsp(icoef+ideg-1)
cq = wsp(icoef+ideg)
do j = 1,m
do i = 1,m
wsp(ip + (j-1)*m + i-1) = 0.0d0
wsp(iq + (j-1)*m + i-1) = 0.0d0
enddo
wsp(ip + (j-1)*(m+1)) = cp
wsp(iq + (j-1)*(m+1)) = cq
enddo
*
*--- Apply Horner rule ...
*
iodd = 1
k = ideg - 1
100 continue
iused = iodd*iq + (1-iodd)*ip
#ifndef SINGLE_PRECISION
call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iused),m,
. wsp(ih2),m, 0.0d0,wsp(ifree),m )
#else
call SGEMM( 'n','n',m,m,m, 1.0d0,wsp(iused),m,
. wsp(ih2),m, 0.0,wsp(ifree),m )
#endif
do j = 1,m
wsp(ifree+(j-1)*(m+1)) = wsp(ifree+(j-1)*(m+1))+wsp(icoef+k-1)
enddo
ip = (1-iodd)*ifree + iodd*ip
iq = iodd*ifree + (1-iodd)*iq
ifree = iused
iodd = 1-iodd
k = k-1
if ( k.gt.0 ) goto 100
*
*--- Obtain (+/-)(I + 2*(p\q)) ...
*
if ( iodd .eq. 1 ) then
#ifndef SINGLE_PRECISION
call DGEMM( 'n','n',m,m,m, scale,wsp(iq),m,
. H,ldh, 0.0d0,wsp(ifree),m )
#else
call SGEMM( 'n','n',m,m,m, scale,wsp(iq),m,
. H,ldh, 0.0,wsp(ifree),m )
#endif
iq = ifree
else
#ifndef SINGLE_PRECISION
call DGEMM( 'n','n',m,m,m, scale,wsp(ip),m,
. H,ldh, 0.0d0,wsp(ifree),m )
#else
call SGEMM( 'n','n',m,m,m, scale,wsp(ip),m,
. H,ldh, 0.0,wsp(ifree),m )
#endif
ip = ifree
endif
#ifndef SINGLE_PRECISION
call DAXPY( mm, -1.0d0,wsp(ip),1, wsp(iq),1 )
call DGESV( m,m, wsp(iq),m, ipiv, wsp(ip),m, iflag )
if ( iflag.ne.0 ) stop 'Problem in DGESV (within DGPADM)'
call DSCAL( mm, 2.0d0, wsp(ip), 1 )
#else
call SAXPY( mm, -1.0,wsp(ip),1, wsp(iq),1 )
call SGESV( m,m, wsp(iq),m, ipiv, wsp(ip),m, iflag )
if ( iflag.ne.0 ) stop 'Problem in SGESV (within SGPADM)'
call SSCAL( mm, 2.0, wsp(ip), 1 )
#endif
do j = 1,m
wsp(ip+(j-1)*(m+1)) = wsp(ip+(j-1)*(m+1)) + 1.0d0
enddo
iput = ip
if ( ns.eq.0 .and. iodd.eq.1 ) then
#ifndef SINGLE_PRECISION
call DSCAL( mm, -1.0d0, wsp(ip), 1 )
#else
call SSCAL( mm, -1.0, wsp(ip), 1 )
#endif
goto 200
endif
*
*-- squaring : exp(t*H) = (exp(t*H))^(2^ns) ...
*
iodd = 1
do k = 1,ns
iget = iodd*ip + (1-iodd)*iq
iput = (1-iodd)*ip + iodd*iq
#ifndef SINGLE_PRECISION
call DGEMM( 'n','n',m,m,m, 1.0d0,wsp(iget),m, wsp(iget),m,
. 0.0d0,wsp(iput),m )
#else
call SGEMM( 'n','n',m,m,m, 1.0,wsp(iget),m, wsp(iget),m,
. 0.0,wsp(iput),m )
#endif
iodd = 1-iodd
enddo
200 continue
iexph = iput
END
*----------------------------------------------------------------------|