-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheigen.f90
More file actions
750 lines (621 loc) · 20.2 KB
/
eigen.f90
File metadata and controls
750 lines (621 loc) · 20.2 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
MODULE eigen
IMPLICIT NONE
CONTAINS
SUBROUTINE eigenDec(nvars, dfx, V, U, eigv)
!% input/output variables
integer, intent(in) :: nvars
real*8, intent(in) :: dfx(nvars,nvars)
real*8, intent(out) :: V(nvars,nvars)
real*8, intent(out) :: U(nvars,nvars)
real*8, intent(out) :: eigv(nvars)
!% local variables
! lapack
integer :: astat
integer :: info
integer :: lwork
character :: jobu = 'N'
character :: jobv = 'V'
real*8 :: evr(nvars)
real*8 :: evi(nvars)
real*8 :: evv(nvars,2)
real*8 :: rlam(nvars)
real*8, allocatable :: work(:)
! ordering
integer :: i,j,k
integer :: imv
real*8 :: x,y,z
real*8 :: temp
real*8 :: norm
! inverse
integer :: liwork
integer :: ipiv(nvars)
real*8, allocatable :: iwork(:)
!=======================================================!
! allocation
if(allocated(work)) deallocate(work)
lwork = 4*nvars
liwork = nvars
allocate(&
work(lwork), &
iwork(liwork), &
stat=astat)
if (astat .ne. 0) stop
!=======================================================!
! eigendecomposition of dfx
call dgeev(jobu, jobv, nvars, dfx, nvars, evr, evi, &
U, nvars, V, nvars, work, lwork, info)
evv(:,1) = evr
evv(:,2) = evi
rlam = abs(evr)
!=======================================================!
! order eigenvalues and eigenvectors
do i = 1,nvars-1
x = rlam(i)
imv = i
do j = i,nvars
y = rlam(j)
if(y .lt. x) then
x = y
imv = j
end if
end do
if(i .ne. imv) then
do k = 1,2
temp = evv(i,k)
evv(i,k) = evv(imv,k)
evv(imv,k) = temp
end do
temp = rlam(i)
rlam(i) = rlam(imv)
rlam(imv) = temp
do k = 1,nvars
temp = V(k,i)
V(k,i) = V(k,imv)
V(k,imv) = temp
end do
end if
end do
!=======================================================!
! real canonical form
do i = 1,nvars-1
x = evv(i,2)
y = evv(i+1,2)
if((x .eq. 0.0d0) .or. (y .eq. 0.0d0)) then
cycle
end if
z = abs(abs(x)-abs(y)/abs(x))
if(z .gt. 1.0d-07) then
cycle
end if
do j = 1,nvars
x = V(j,i)
y = V(j,i+1)
V(j,i) = x-y
V(j,i+1) = x+y
end do
end do
!=======================================================!
! normalize and get left eigenvectors
do i = 1,nvars
norm = sqrt( sum( V(:,i) * V(:,i) ) )
norm = 1.0d0 / norm
V(:,i) = norm * V(:,i)
U(:,i) = V(:,i)
end do
call dgetrf(nvars, nvars, U, nvars, ipiv, info)
call dgetri(nvars, U, nvars, ipiv, iwork, liwork, info)
eigv = evv(:,1)
END SUBROUTINE eigenDec
SUBROUTINE schur(mode, nslow, nfast, nvars, dfx, qr, ql, qrs, qrf, qls, qlf, eig, eigv, eigs, eigf)
! input/output variables
integer, intent(in) :: mode
integer, intent(in) :: nslow
integer, intent(in) :: nfast
integer, intent(in) :: nvars
real*8, intent(inout) :: dfx(nvars,nvars)
real*8, intent(out), optional :: qr(nvars,nvars)
real*8, intent(out), optional :: ql(nvars,nvars)
real*8, intent(out), optional :: qrs(nvars,nslow)
real*8, intent(out), optional :: qrf(nvars,nfast)
real*8, intent(out), optional :: qls(nslow,nvars)
real*8, intent(out), optional :: qlf(nfast,nvars)
real*8, intent(out), optional :: eig(nvars,nvars)
real*8, intent(out), optional :: eigv(nvars)
real*8, intent(out), optional :: eigs(nslow,nslow)
real*8, intent(out), optional :: eigf(nfast,nfast)
! local variables
character :: jobvs = 'v'
character :: sort = 'n'
integer :: i, astat
integer :: info
integer :: ifst
integer :: ilst
integer :: sdim = 0
integer :: lwork1
real*8 :: jac(nvars,nvars)
real*8 :: qz(nvars,nvars)
real*8 :: qzt(nvars,nvars)
real*8 :: zrs(nvars,nslow)
real*8 :: zrf(nvars,nfast)
real*8 :: zls(nslow,nvars)
real*8 :: zlf(nfast,nvars)
real*8 :: wr(nvars)
real*8 :: wi(nvars)
real*8 :: work2(nvars)
real*8, allocatable :: work1(:)
logical :: sel, bwork(nvars)
real*8 :: qdq(nvars,nvars)
800 format(4e16.8)
!=======================================================!
! allocation
if(allocated(work1)) deallocate(work1)
lwork1 = 4*nvars
allocate(work1(lwork1), stat=astat)
if (astat .ne. 0) stop
!=======================================================!
! schur decomposition of dfx
jac = dfx
call dgees(jobvs, sort, sel, nvars, jac, nvars, &
sdim, wr, wi, qz, nvars, work1, lwork1, bwork, info)
!=======================================================!
! order schur decomposition
ilst = 1
100 ifst = ilst
do i = ilst+1,nvars
if (abs(jac(i,i)) .lt. abs(jac(ifst,ifst))) then
ifst = i
end if
end do
if(ifst .ne. ilst) then
call dtrexc(jobvs, nvars, jac, nvars, qz, nvars, &
ifst, ilst, work2, info)
end if
ilst = ilst + 1
if (ilst < nvars) then
goto 100
end if
!=======================================================!
! output
select case (mode)
case (0)
if(present(qr)) then
qr = qz
end if
if(present(ql)) then
ql = qzt
end if
if(present(eig)) then
eig = jac
end if
if(present(eigv)) then
eigv = 0.0d0
do i = 1,nvars
eigv(i) = jac(i,i)
end do
end if
case (1)
! invariant decomposition
call split_schur(nslow=nslow, nfast=nfast, nvars=nvars, &
jac=jac, qz=qz, zrs=zrs, zrf=zrf, zls=zls, zlf=zlf, &
info=info)
if(present(qrs)) then
qrs = zrs
end if
if(present(qrf)) then
qrf = zrf
end if
if(present(qls)) then
qls = zls
end if
if(present(qlf)) then
qlf = zlf
end if
if(present(eigs)) then
eigs = jac(1:nslow,1:nslow)
end if
if(present(eigf)) then
eigf = jac(nslow+1:nvars,nslow+1:nvars)
end if
if(present(eigv)) then
eigv = 0.0d0
do i = 1,nvars
eigv(i) = jac(i,i)
end do
end if
end select
700 continue
!=======================================================!
! deallocation
if(allocated(work1)) deallocate(work1)
END SUBROUTINE schur
SUBROUTINE split_schur(nslow, nfast, nvars, jac, qz, &
myzr, myzl, zrs, zrf, zls, zlf, info)
! input/output variables
integer, intent(in) :: nslow
integer, intent(in) :: nfast
integer, intent(in) :: nvars
real*8, intent(inout) :: jac(nvars,nvars)
real*8, intent(in) :: qz(nvars,nvars)
real*8, intent(out), optional :: myzr(nvars,nvars)
real*8, intent(out), optional :: myzl(nvars,nvars)
real*8, intent(out), optional :: zrs(nvars,nslow)
real*8, intent(out), optional :: zrf(nvars,nfast)
real*8, intent(out), optional :: zls(nslow,nvars)
real*8, intent(out), optional :: zlf(nfast,nvars)
integer, intent(out) :: info
! local variables
character :: trans = 'n'
integer :: i, j, astat
integer :: flag
integer :: isgn = -1
real*8 :: scale = 1.0d0
real*8 :: mat(nvars,nvars)
real*8 :: jss(nslow,nslow)
real*8 :: jff(nfast,nfast)
real*8 :: jsf(nslow,nfast)
real*8 :: zr(nvars,nvars)
real*8 :: zl(nvars,nvars)
real*8 :: szr(nvars,nslow)
real*8 :: fzr(nvars,nfast)
real*8 :: szl(nslow,nvars)
real*8 :: fzl(nfast,nvars)
600 format(2f10.4)
!=======================================================!
! solve sylvester equation
mat = 0.0d0
forall (i = 1:nvars) mat(i,i) = 1.0d0
jss = jac(1:nslow,1:nslow)
jff = jac(nslow+1:nvars,nslow+1:nvars)
jsf = jac(1:nslow,nslow+1:nvars)
jsf = -jsf
call dtrsyl(trans, trans, isgn, nslow, nfast, jss, &
nslow, jff, nfast, jsf, nslow, scale, info)
!=======================================================!
! find invariant decomposition
! info = 0: dtrsyl successful
! info = 1: dtrsyl failed, no zr/zl decomposition
select case (info)
case (0)
flag = 0
100 select case (flag)
case (0)
mat(1:nslow,1+nslow:nvars) = jsf
zr = matmul(qz, mat)
flag = 1
goto 100
case (1)
mat(1:nslow,1+nslow:nvars) = -jsf
zl = matmul(mat, transpose(qz))
end select
szr = zr(1:nvars,1:nslow)
fzr = zr(1:nvars,nslow+1:nvars)
szl = zl(1:nslow,1:nvars)
fzl = zl(nslow+1:nvars,1:nvars)
case (1)
zr = qz
zl = transpose(qz)
szr = zr(1:nvars,1:nslow)
fzr = zr(1:nvars,nslow+1:nvars)
szl = zl(1:nslow,1:nvars)
fzl = zl(nslow+1:nvars,1:nvars)
end select
continue
jac(1:nslow,nslow+1:nvars) = 0.0d0
if(present(myzr)) then
myzr = zr
end if
if(present(myzl)) then
myzl = zl
end if
if(present(zrs)) then
zrs = szr
end if
if(present(zrf)) then
zrf = fzr
end if
if(present(zls)) then
zls = szl
end if
if(present(zlf)) then
zlf = fzl
end if
END SUBROUTINE split_schur
!!!
!!!
SUBROUTINE fronorm_schur(nslow, nfast, nvars, dfx, nsf, zr, zl, fronorm)
! input/output variables
integer, intent(in) :: nslow
integer, intent(in) :: nfast
integer, intent(in) :: nvars
real*8, intent(in) :: dfx(nvars,nvars)
real*8, intent(in) :: nsf(nvars,nvars)
real*8, intent(in) :: zr(nvars,nvars)
real*8, intent(in) :: zl(nvars,nvars)
real*8, intent(out) :: fronorm
! local variables
integer :: astat
real*8, allocatable :: &
zreig(:,:), &
zreigzl(:,:), &
work(:)
real*8, external :: dlange
!=======================================================!
! allocation
if(allocated(work)) deallocate(work)
if(allocated(zreig)) deallocate(zreig)
if(allocated(zreigzl)) deallocate(zreigzl)
allocate(work(nvars), &
zreig(nvars,nvars), &
zreigzl(nvars,nvars), &
stat = astat)
if (astat .ne. 0) stop
!=======================================================!
! jacobian reconstruction
zreig = matmul(zr, nsf)
zreigzl = matmul(zreig, zl)
zreigzl = zreigzl - dfx
!=======================================================!
! frobenius norm
fronorm = dlange('F', nvars, nvars, zreigzl, nvars, work)
!=======================================================!
! deallocation
deallocate(zreig, zreigzl, work)
END SUBROUTINE fronorm_schur
!!!
!!!
SUBROUTINE colspace(m, n, r, mat, col)
IMPLICIT NONE
! input/output variables
integer, intent(in) :: m
integer, intent(in) :: n
integer, intent(in) :: r
integer, intent(in) :: mat(m,n)
real*8, intent(out) :: col(m,r)
! local variables
integer :: astat
integer :: pivot
integer :: i, j, t
integer :: npiv
integer, allocatable :: ipiv(:)
real*8, allocatable :: tmat(:,:)
real*8, allocatable :: trow(:)
real*8, allocatable :: temp(:,:)
npiv = min(m,n)
!=======================================================!
! allocation
if(allocated(ipiv)) deallocate(ipiv)
if(allocated(tmat)) deallocate(tmat)
if(allocated(trow)) deallocate(trow)
if(allocated(temp)) deallocate(temp)
allocate(&
ipiv(npiv), &
trow(m), &
tmat(n,m), &
temp(m,n), &
stat=astat)
if(astat .ne. 0) stop
!=======================================================!
! lu decomposition of mat-transpose
tmat = real(transpose(mat),8)
call dgetrf(n, m, tmat, n, ipiv, astat)
forall(i = 1:n, j = 1:m, i .gt. j) tmat(i,j) = 0
!=======================================================!
! row reduction of u
call rref(n, m, tmat)
!=======================================================!
! transpose and remove zero-cols
temp = transpose(tmat)
col = temp(1:m,1:r)
!=======================================================!
! deallocation
if(allocated(ipiv)) deallocate(ipiv)
if(allocated(tmat)) deallocate(tmat)
if(allocated(trow)) deallocate(trow)
if(allocated(temp)) deallocate(temp)
END SUBROUTINE colspace
!!!
!!!
SUBROUTINE pinv(m, n, r, mat, inv)
IMPLICIT NONE
! input/ output variables
integer, intent(in) :: m
integer, intent(in) :: n
integer, intent(in) :: r
real*8, intent(in) :: mat(m,n)
real*8, intent(out) :: inv(n,m)
! local variables
character :: jobu = 'a', jobvt = 'a'
integer :: astat, i
integer :: nrhs
integer :: lsval
integer :: lwork
real*8, allocatable :: a(:,:)
real*8, allocatable :: u(:,:)
real*8, allocatable :: sval(:)
real*8, allocatable :: vt(:,:)
real*8, allocatable :: s(:,:)
real*8, allocatable :: work(:)
!=======================================================!
! allocation
lsval = min(m,n)
lwork = max(1,3*min(m,n)+max(m,n),5*min(m,n))
if(allocated(a)) deallocate(a)
if(allocated(u)) deallocate(u)
if(allocated(sval)) deallocate(sval)
if(allocated(vt)) deallocate(vt)
if(allocated(s)) deallocate(s)
if(allocated(work)) deallocate(work)
allocate(&
a(m,n), &
u(m,m), &
sval(lsval), &
vt(n,n), &
s(n,m), &
work(lwork), &
stat=astat)
if(astat .ne. 0) stop
!=======================================================!
! solve the linear least squares problem
a = mat
call dgesvd(jobu, jobvt, m, n, a, m, sval, &
u, m, vt, n, work, lwork, astat)
sval = 1/sval
s = 0
forall(i = 1:r) s(i,i) = sval(i)
s = matmul(s,transpose(u))
inv = matmul(transpose(vt),s)
!=======================================================!
! deallocation
if(allocated(u)) deallocate(u)
if(allocated(sval)) deallocate(sval)
if(allocated(vt)) deallocate(vt)
if(allocated(work)) deallocate(work)
END SUBROUTINE pinv
!!!
!!!
SUBROUTINE rref(m, n, mat)
IMPLICIT NONE
! input/output variables
integer, intent(in) :: m
integer, intent(in) :: n
real*8, intent(inout) :: mat(m,n)
! local variables
integer :: pivot
integer :: i, j
real*8, allocatable :: trow(:)
!=======================================================!
! allocation
allocate(trow(n))
!=======================================================!
! elimination
pivot = 1
do j = 1, m
if(n .le. pivot) exit
i = j
do while(mat(i,pivot) .eq. 0)
i = i + 1
if(m .eq. i) then
i = j
pivot = pivot + 1
if(n .eq. pivot) return
end if
end do
trow = mat(i,:)
mat(i,:) = mat(j,:)
mat(j,:) = trow
mat(j,:) = mat(j,:)/mat(j,pivot)
do i = 1, m
if (i .ne. j) then
mat(i,:) = mat(i,:) - mat(j,:) * mat(i,pivot)
end if
end do
pivot = pivot + 1
end do
!=======================================================!
! deallocation
deallocate(trow)
END SUBROUTINE rref
SUBROUTINE nullspace(m, n, r, k, mat, ker)
! input/output variables
integer, intent(in) :: m
integer, intent(in) :: n
integer, intent(in) :: r
integer, intent(in) :: k
real*8, intent(in) :: mat(m,n)
real*8, intent(out) :: ker(n,k)
! local variables
character :: jobu = 'a', jobvt = 'a'
integer :: astat, i, info
integer :: lsval
integer :: lwork
real*8 :: a(m,n)
real*8 :: u(m,m)
real*8 :: vt(n,n)
real*8, allocatable :: sval(:)
real*8, allocatable :: work(:)
!=======================================================!
! allocation
lsval = min(m,n)
lwork = max(1,3*min(m,n)+max(m,n),5*min(m,n))
if(allocated(sval)) deallocate(sval)
if(allocated(work)) deallocate(work)
allocate(&
sval(lsval), &
work(lwork), &
stat=astat)
if(astat .ne. 0) stop
!=======================================================!
! solve the linear least squares problem
a = mat
info = 0
call dgesvd(jobu, jobvt, m, n, a, m, sval, &
u, m, vt, n, work, lwork, info)
if(info .ne. 0) stop
vt = transpose(vt)
ker = vt(:,r+1:n)
!=======================================================!
! deallocation
if(allocated(sval)) deallocate(sval)
if(allocated(work)) deallocate(work)
END SUBROUTINE nullspace
SUBROUTINE conjugategradient(n, mat, b, x, ifail)
! input/output variables
integer, intent(in) :: n
real*8, intent(in) :: mat(n,n)
real*8, intent(in) :: b(n)
real*8, intent(out) :: x(n)
integer, intent(out) :: ifail
! local variables
integer :: i, info
integer :: k, kmax
integer :: ipiv(n)
real*8 :: alpha
real*8 :: beta
real*8 :: r(n)
real*8 :: rp(n)
real*8 :: y(n)
real*8 :: yp(n)
real*8 :: p(n)
real*8 :: pcn(n,n)
real*8 :: err
real*8 :: tol
!=======================================================!
! initialize
kmax = 50
tol = 1
tol = epsilon(tol)
pcn = 0.0
forall(i = 1:n) pcn(i,i) = 1/mat(i,i)
x = 0.0
r = matmul(mat,x) - b
y = matmul(pcn,r)
rp = r
yp = y
p = -y
err = sqrt(sum(r*r))
k = 0
!=======================================================!
! iterate
do while((err > tol) .and. (k .le. kmax))
alpha = sum(r*y)/sum(p*matmul(mat,p))
x = x + alpha*p
r = r + alpha*matmul(mat,p)
y = matmul(pcn,r)
beta = sum(r*y)/sum(rp*yp)
p = beta*p - y
rp = r
yp = y
err = sqrt(sum(r*r))
k = k + 1
end do
if(err > tol) then
ifail = 1
write(*,'(A23,i3,A11)') "PCG failed after ", k," iterations"
write(*,'(A6,e14.4)') "err = ", err
else
ifail = 0
write(*,'(A23,i3,A11)') "PCG converged within ", k," iterations"
write(*,'(A6,e14.4)') "err = ", err
end if
END SUBROUTINE conjugategradient
END MODULE eigen