! Program principal pentru General_R
! Problema genaralizata:
! MU''+KU =0
!
! M si K =simetrice + pozitiv definite
! Stocare si procesare: matricea comnpleta
! Introducere: triunghiul superior - pe linii
!
! Dec. 2006
! - cu kod_gen pt. M si pt. K: kod de citire a matricii.
! - kod_gen >0: Matricile = citite din fisierul de intrare sau specificat
!	M - triunghiul sup., pe linii (incepand cu elem. diagonal)
!	K - triunghiul sup, pe linii
!   La fisier specificat: Precedate de: 1 linie text
!
! Fisierul de intrare - continut:
!================================
! Daca matricea este citita: se introduce si "factor";
! Daca este generata: nu
!
! 1) Titlul problemei
! 2) ngl, kod_genK, kod_genM	
!	2-1) matricea M - daca kod_genM =1
!	2-2) m_factor
!
!	2-3) matricea K - daca kod_genK =1
!	2-4) k_factor
! 3) kod_print
!
! kod_gen  <=0 / >0: M si K =citite/generate
! 	kod_gen <=0:  matrice generata (subrutina)
!	kod_gen =1:  matricea citita din fisierul de intrare
!	kod_gen >=2: matricea citita din fisier specificat (selectat)

!

Program General_R
use dfwin
use work

use dflib

implicit real(8) (a-h,o-z)

real(8) m_factor, k_factor

character(80) titlu
character(max_path) fname1, fname2, fname3, fname4

real(8) norm1
character(80) dummy

 CHARACTER(3)        drive
 CHARACTER(256)      dir
! CHARACTER(256)      name		! in work
 CHARACTER(256)      ext
 CHARACTER(256)      file



call GetFile(fname1, status, 'Select Input File'C)

open(1,file =fname1, status ='old', form ='formatted')

! Read data; title.
read(1,'(a)') title

read(1,*) ngl, kod_genM, kod_genK

! Alocari:
!nn =ngl*(ngl+1)/2
allocate(M(ngl,ngl), K(ngl,ngl), R(ngl,ngl))
allocate(S_1(ngl,ngl))
M =0.; K =0.; R =0.;
S_1 =0

! Genereaza matricile M si K
IF(kod_genM <=0) THEN
call gen_M()

ELSE	!read M

	if(kod_genM ==1) then
	do i =1,ngl
	read(1,*) (M(i,j), j =i,ngl)
	enddo

	else
	call GetFile(fname2, status, 'Select MASS File'C)
	open(2, file =fname2, status ='old', form ='formatted')

	read(2,*) dummy
	do i =1,ngl
	read(2,*) ( M(i,j), j=i,ngl )
	enddo

	close(2)	
	endif


! Triunghiul inf.:
do i =1,ngl
M(i, 1:i-1) =M(1:i-1, i) 
enddo

read(1,*) m_factor

if(m_factor /=1._8) then
M =M*m_factor
endif

ENDIF


IF(kod_genK <=0) THEN
call gen_K()

ELSE	!read K

	if(kod_genK ==1) then
	do i =1,ngl
	read(1,*) ( K(i,j), j=i,ngl )
	enddo

	else
	call GetFile(fname3, status, 'Select STIFFNESS File'C)
	open(3, file =fname3, status ='old', form ='formatted')

	read(3,*) dummy
	do i =1,ngl
	read(3,*) ( K(i,j), j=i,ngl )
	enddo

	close(3)	
	endif

! Triunghiul inf.:
do i =1,ngl
K(i, 1:i-1) =K(1:i-1, i) 
enddo

read(1,*) k_factor

if(k_factor /=1._8) then
K =K*k_factor
endif

ENDIF

read(1,*) kod_Print

if(kod_Print /=0) then
write(1, '(/, a)') ' M'
do i=1,ngl
write(1,*) M(i,:)
enddo

write(1, '(/, a)') ' K'
do i=1,ngl
write(1,*) K(i,:)
enddo

endif

! Genereaza R:
call gen_R(kod_Print)

deallocate(M, K)

! Scale R-matrix:
norm1 =maxval(abs(R))

scal =2**(exponent(norm1)-1)

if(scal /=1) then
write(*,*) 'Scale Factor :', scal

R =R/scal
endif

! Write R matrix:
call Write_R(scal)

! Pentru vectorii proprii:
write(1, '(/, a)') ' Matricea S^(-1):'
do i =1,ngl
write(1,*) S_1(i,i:)
enddo


close(1)
deallocate(R, S_1)

END Program General_R
