-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjsolv.f90
201 lines (155 loc) · 5.68 KB
/
adjsolv.f90
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
#include "port.h"
!=============================================================================!
subroutine adjsolv
!=============================================================================!
use global
implicit none
integer :: ic, i, j
complex :: Ui(neq,neq), Uf(neq,neq), BC(neq), norm
complex, allocatable :: A(:,:)
integer :: icount, mcount=1, ind
real :: tol=0.2, y, dy
!.... eigenvalue iteration variables
complex :: ctemp, cm1, cm2, err, errm1, errm2
complex :: At, Bt, Ct, qt, fd
real :: fdxr, fdxi, fdyr, fdyi
complex :: dp=0
complex, external :: inprod
external adjder
!.... For Lapack eigensolver
integer :: info
integer :: lwork
complex, allocatable :: work(:), eval(:), evec(:,:)
real, allocatable :: rwork(:)
!=============================================================================!
BC = zero
!.... compute the initial condition
call adjini( Ui, ic )
lwork = 2 * ic
allocate( A(ic,ic), work(lwork), rwork(lwork), eval(ic), &
evec(ic,ic) )
!.... Begin the eigenvalue iteration loop
ievec = 0 ! don't compute efun
icount = 0 ! number of iteration
err = one
100 continue
icount = icount + 1
!.... integrate the equations using orthonomalization
call conte( ny-1, tol, neq, ic, Ui, Uf, ymax, zero, ievec, &
adjder, BC, adj )
!.... form the boundary condition matrix for the adjoint
A(1,:) = Uf(1,1:ic)
A(2,:) = Uf(6,1:ic)
A(3,:) = Uf(7,1:ic)
A(4,:) = Uf(8,1:ic)
!.... compute the eigenvalues (only) of A and select the minimum eval
call CGEEV('N', 'N', ic, A, ic, eval, evec, &
ic, evec, ic, work, lwork, rwork, info)
err = eval(1)
ind = 1
do i = 2, ic
if ( abs(eval(i)) .le. abs(err) ) then
err = eval(i)
ind = i
endif
end do
write (*,30) icount, ind, real(alpha), aimag(alpha), &
real(err), aimag(err), abs(err)
30 format (1x,i2,1x,i2,2(2x,1pe13.6,1x,1pe13.6),2x,1pe13.6)
if ( (abs(err) .ge. eps8) .and. (icount .lt. mcount) ) then
!.... compute a new guess for the eigenvalue
!.... 1. Just make a perturbation on the first iteration
!.... 2. A Newton correction
!.... 3. A quadratic correction
ctemp = alpha
if (icount .eq. 1) then
cm2 = alpha
errm2 = err
alpha = alpha * (one + eps8)
else if (icount .eq. 2) then
cm1 = alpha
errm1 = err
fd = (err-errm2)/(alpha-cm2)
alpha = alpha - err/fd
else
qt = (alpha-cm1)/(cm1-cm2)
At = qt*err-qt*(one+qt)*errm1+qt**2*errm2
Bt = (two*qt+one)*err-(one+qt)**2*errm1+qt**2*errm2
Ct = (one+qt)*err
if ( ABS(Bt+SQRT(Bt**2-four*At*Ct)) .gt. &
ABS(Bt-SQRT(Bt**2-four*At*Ct)) ) then
alpha = ctemp-(ctemp-cm1)*two*Ct/(Bt+SQRT(Bt**2-four*At*Ct))
else
alpha = ctemp-(ctemp-cm1)*two*Ct/(Bt-SQRT(Bt**2-four*At*Ct))
end if
cm2 = cm1
cm1 = ctemp
errm2 = errm1
errm1 = err
end if
goto 100
end if
!.... if converged, determine the combination of the independent solutions
!.... that satisfies the boundary conditions by solving an eigensystem
!.... to determine the eigenvector cooresponding to the zero eigenvalue.
A(1,:) = Uf(1,1:ic)
A(2,:) = Uf(6,1:ic)
A(3,:) = Uf(7,1:ic)
A(4,:) = Uf(8,1:ic)
call CGEEV('N', 'V', ic, A, ic, eval, evec, &
ic, evec, ic, work, lwork, rwork, info)
i=1
err = eval(1)
BC(1:ic) = evec(:,i)
do i = 2, ic
if ( abs(eval(i)) .le. abs(err) ) then
err = eval(i)
BC(1:ic) = evec(:,i)
end if
end do
deallocate( A, work, rwork, eval, evec )
ievec = 1
call conte( ny-1, tol, neq, ic, Ui, Uf, ymax, zero, ievec, &
adjder, BC, adj)
!.... make the phase reference consistent
j = ny/2
adj = adj / exp(im*atan2(aimag(adj(1,j)),real(adj(1,j))))
!.... normalize and output the eigenfunction if desired
if (efun_out) then
!.... determine the normalization factor
norm = zero
do i = 1, ndof
do j = 1, ny
if ( ABS(adj(i,j)) .gt. ABS(norm) ) norm = adj(i,j)
end do
end do
!.... normalize and output the eigenfunction
if (norm_efun) adj = adj / norm
open(15,file='adj.out')
write(15,"('# alpha = ',1pe20.13,1x,1pe20.13)") alpha
dy = ymax / real(ny-1)
do j = 1, ny
y = ymax - dy * real(j-1)
write(15,"(17(1pe13.6,1x))") y, &
(real(adj(i,j)), aimag(adj(i,j)), i = 1, neq)
end do
!.... undo the normalization
if(norm_efun) adj = adj * norm
close(15)
!.... un-normalized for use in constructing nonparallel corrections
!.... SSC: was doing this twice?
! adj = adj * norm
end if
!... Test orthogonality (this uses Trapezoidal but the real integrator is
!... RK4 so that there is considerable error here)
dp = zero
j = 1
dp = dp + pt5 * dy * inprod(ndof,adj(:,j),efun(:,j))
do j = 2, ny-1
dp = dp + dy * inprod(ndof,adj(:,j),efun(:,j))
enddo
j = ny
dp = dp + pt5 * dy * inprod(ndof,adj(:,j),efun(:,j))
write(*,'(/,"inprod(adj,efun) [trapezoidal]= ",2(1e13.6,1x))') dp
return
end subroutine adjsolv