subroutine svd_tunes(tbt, i, directory) use precision_def use tbt_mod use nrtype use nr implicit none type (tbt_all_data_struct) tbt integer i, nturns_max, k,j,n,ii,konst integer nt real(rp), allocatable:: A(:,:), AA(:,:), W(:), V(:,:), WV(:,:), WVI(:,:), & C(:,:), fft_x(:), fft_y(:), L(:,:), U(:,:), b(:), d(:), x(:) character*60 directory character*6 ci real(rp) coeff interface ! SUBROUTINE inverse(aq,cq) ! USE nrtype ! IMPLICIT NONE ! REAL(dp), DIMENSION(:,:), INTENT(INOUT) :: aq ! REAL(dp), DIMENSION(:), INTENT(OUT) :: cq ! end subroutine SUBROUTINE MY_svdcmp(a,w,v) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror,outerprod USE nr, ONLY : pythag IMPLICIT NONE REAL(dp), DIMENSION(:,:), INTENT(INOUT) :: a REAL(dp), DIMENSION(:), INTENT(OUT) :: w REAL(dp), DIMENSION(:,:), INTENT(OUT) :: v end subroutine end interface k=0 !nturns_max = 2048 nturns_max = 8192 ! count detectors do j= 0,size(tbt%bunch(i)%bpm)-1 if(tbt%bunch(i)%bpm(j)%data_ok)then k=k+1 !detectors nturns_max = min(size(tbt%bunch(i)%bpm(j)%turn),nturns_max) endif end do nt = nturns_max konst=k allocate(A(1:nt,1:k)) !matrix to decompose allocate(AA(1:nt,1:k)) !same as A, do not destroy allocate(W(1:k)) !eigenvalues allocate(V(1:k,1:k)) !matrix of eigenvectors allocate(WV(1:k,1:k)) !product of eigenvalues and eigenvector matrices allocate(WVI(1:k,1:k)) !inverse of above allocate(C(1:nt,1:k)) !useful eigenvectors allocate(fft_x(1:nt)) !maybe should change? allocate(fft_y(1:nt)) allocate(L(1:k,1:k)) allocate(U(1:k,1:k)) allocate(b(1:k)) allocate(d(1:k)) allocate(x(1:k)) write(ci,'(i3.3)')i k=0 do j= 0,size(tbt%bunch(i)%bpm)-1 if(tbt%bunch(i)%bpm(j)%data_ok)then k=k+1 !detectors do n= 1, nt A(n,k) = tbt%bunch(i)%bpm(j)%turn(n)%x AA(n,k) = tbt%bunch(i)%bpm(j)%turn(n)%x end do endif end do call my_svdcmp(A(1:nt,1:konst),W,V) ! call dgesvd('N','A',k,nt,A,k,S,U,k,V,nt,WORK, LWORK,INFO) do j=1,konst do k=1,konst WV(j,k) = W(j)*V(k,j) WVI(j,k) = W(j)*V(k,j) end do end do n=konst ! inversion procedure found online ! step 0: initialization for matrices L and U and b ! Fortran 90/95 aloows such operations on matrices do j=1,n b(j)=0 do k=1,n L(j,k)=0.0 U(j,k)=0.0 end do end do ! step 1: forward elimination do k=1, n-1 do ii=k+1,n coeff=WV(ii,k)/WV(k,k) L(ii,k) = coeff do j=k+1,n WV(ii,j) = WV(ii,j)-coeff*WV(k,j) end do end do end do ! Step 2: prepare L and U matrices ! L matrix is a matrix of the elimination coefficient ! + the diagonal elements are 1.0 do ii=1,n L(ii,ii) = 1.0 end do ! U matrix is the upper triangular part of A do j=1,n do ii=1,j U(ii,j) = WV(ii,j) end do end do ! Step 3: compute columns of the inverse matrix C do k=1,n b(k)=1.0 d(1) = b(1) ! Step 3a: Solve Ld=b using the forward substitution do ii=2,n d(ii)=b(ii) do j=1,ii-1 d(ii) = d(ii) - L(ii,j)*d(j) end do end do ! Step 3b: Solve Ux=d using the back substitution x(n)=d(n)/U(n,n) do ii = n-1,1,-1 x(ii) = d(ii) do j=n,ii+1,-1 x(ii)=x(ii)-U(ii,j)*x(j) end do x(ii) = x(ii)/u(ii,ii) end do ! Step 3c: fill the solutions x(n) into column k of C do ii=1,n WVI(ii,k) = x(ii) end do b(k)=0.0 end do ! end of inversion procedure do j=1,nt do k=1,konst C(j,k)=0 do n=1,konst C(j,k)=C(j,k)+AA(j,n)*WVI(n,k) end do end do end do fft_x(1:nt) = C(1:nt,1) !fft_x(1:nt) = fft_x(1:nt)+C(1:nt,2) !fft_x(1:nt) = fft_x(1:nt)+C(1:nt,3) !fft_x(1:nt) = fft_x(1:nt)+C(1:nt,4) !fft_x(1:nt) = fft_x(1:nt)+C(1:nt,5) call realft(fft_x(1:nt),1) !fft_x(1:10) = W(1:10); k=0 do j= 0,size(tbt%bunch(i)%bpm)-1 if(tbt%bunch(i)%bpm(j)%data_ok)then k=k+1 !detectors do n= 1, nt A(n,k) = tbt%bunch(i)%bpm(j)%turn(n)%y AA(n,k) = tbt%bunch(i)%bpm(j)%turn(n)%y end do endif end do call my_svdcmp(A(1:nt,1:konst),W,V) ! call dgesvd('N','A',k,nt,A,k,S,U,k,V,nt,WORK, LWORK,INFO) do j=1,konst do k=1,konst WV(j,k) = W(j)*V(k,j) WVI(j,k) = W(j)*V(k,j) end do end do n=konst ! inversion procedure found online ! step 0: initialization for matrices L and U and b ! Fortran 90/95 aloows such operations on matrices do j=1,n b(j)=0 do k=1,n L(j,k)=0.0 U(j,k)=0.0 end do end do ! step 1: forward elimination do k=1, n-1 do ii=k+1,n coeff=WV(ii,k)/WV(k,k) L(ii,k) = coeff do j=k+1,n WV(ii,j) = WV(ii,j)-coeff*WV(k,j) end do end do end do ! Step 2: prepare L and U matrices ! L matrix is a matrix of the elimination coefficient ! + the diagonal elements are 1.0 do ii=1,n L(ii,ii) = 1.0 end do ! U matrix is the upper triangular part of A do j=1,n do ii=1,j U(ii,j) = WV(ii,j) end do end do ! Step 3: compute columns of the inverse matrix C do k=1,n b(k)=1.0 d(1) = b(1) ! Step 3a: Solve Ld=b using the forward substitution do ii=2,n d(ii)=b(ii) do j=1,ii-1 d(ii) = d(ii) - L(ii,j)*d(j) end do end do ! Step 3b: Solve Ux=d using the back substitution x(n)=d(n)/U(n,n) do ii = n-1,1,-1 x(ii) = d(ii) do j=n,ii+1,-1 x(ii)=x(ii)-U(ii,j)*x(j) end do x(ii) = x(ii)/u(ii,ii) end do ! Step 3c: fill the solutions x(n) into column k of C do ii=1,n WVI(ii,k) = x(ii) end do b(k)=0.0 end do ! end of inversion procedure do j=1,nt do k=1,konst C(j,k)=0 do n=1,konst C(j,k)=C(j,k)+AA(j,n)*WVI(n,k) end do end do end do fft_y(1:nt) = C(1:nt,1) !fft_y(1:nt) = fft_y(1:nt)+C(1:nt,2) !fft_y(1:nt) = fft_y(1:nt)+C(1:nt,3) !fft_y(1:nt) = fft_y(1:nt)+C(1:nt,4) !fft_y(1:nt) = fft_y(1:nt)+C(1:nt,5) call realft(fft_y(1:nt),1) open(unit=97,file = trim(directory)//'/svd_Vectors_'//trim(ci)//'.dat') write(97,'(a)')' V(1:10) ' do j=1,nt write(97,'(i10,1x, es12.4,3x,es12.4)')j,fft_x(j),fft_y(j) !write(97,'(es12.4)')fft(j) !simplified readout end do close(unit=98) close(unit=97) deallocate(A,AA,W,V,WV,WVI,C,L,U,b,d,x) return end