!jInvert_Demo.f95 
!2026.04.19.1605cdt-    Demonstrates calling jInvert()

!    Author- Jeffrey M. Setterholm, Lakeville,MN 55044 USA
! IP Status- Free source code (e.g.: post copyright)
!
!  Computer- "T4"/HP-800-G4-Mini/i7-8700T/IntelUHD630/Win10Pro-22H2
!             ^name  ^Mfgr.Id    ^chipset ^graphics   ^OS
!                /AbsoftProFortran 21.0.2/OpenGL+Glut3.6
!                 ^compiler               ^Fortran graphics
!Disclaimer:
! ***************************************************************
! **********   Individual cognition is always flawed,  **********
! **********         including yours and mine.         **********
! **********                 - So: -                   **********
! ********** Use this code & program at your own risk. **********
! ***************************************************************

!Table of Contents: ...use to search...
!Program jInvert_Demo !Demonstrates calling jInvert                        @ 25

!       "jInvert_Demo-f95.pdf" has the color-coded Fortran syntax.
!------------------------------------------------------------------------------

Program jInvert_Demo
!Exercises Subroutine jInvert(Nin,C,iRW,iP) ...after defining Nin,C, iRW, & iP.
!--Globals
 use jInvert_FoR & !The "Frame-of-Reference" of jInvert(), i.e.: the variables
                ,only:  DataFileIn, DataFileOut, YourDataHeader  &
                      , iP, iRW, Nin, C, iRankin, iRankOut
 !--End Globals
 implicit none
 !--Arguments
 !--Internals                <- locally defined variables:
  logical(4):: LExists       !File existence flag
  integer(4):: iAlloc        !Array allocation flag =0:success
  integer(4):: i,j,i2,j2

  !--EndDefs----------------------------------------------
   write( 6, "(/'!{Program jInvert_Demo:',45('-')&
                     &,t58,' jInvert_Demo.f95 @ 40')")
   pause"Use this program at your own risk. Press enter to continue."        
   iP  = 13
   iRW = 14
  !--Read "jInvert-Filenames.ini":
   inquire(file="jInvert-Filenames.ini",exist=LExists)
   if(LExists)then
     open( unit=iRW, file="jInvert-Filenames.ini"  , action='read'    &
                  , access='sequential', status='old' ,err=25)

       read(iRW,*,err= 5) DataFileIn ; write( 0,*) DataFileIn          ;goto 10
 5     write( 6,*) "Error reading line 1 of jInvert-Filenames.ini"
10     read(iRW,*,err=15) DataFileOut; write( 0,*) DataFileOut         ;goto 20
15     write( 6,*) "Error reading line 2 of jInvert-Filenames.ini"
20   close(iRW)                                                        ;goto 30
   endif!(LExists)
25 write( 6,*) "jInvert-Filenames.ini not found. Using defaults."

  !--Open DataFileOut:              
30 write(6,"('jInvert: DataFileOut     = ',:,79a1)") &
                     (DataFileOut(i:i),i=1,len_trim(DataFileOut))

   open(unit=iP,file=DataFileOut,action='write',err=35); goto 40
35 iP= 6;write(iP,"( 'The file could not be opened for write. Using screen.')")

  !--Read DataFileIn:
40 write(iP,"(/'jInvert_Demo: reading ',:,79a1)") &
                            (DataFileIn(i:i),i=1,len_trim(DataFileIn))
   inquire(file=DataFileIn,exist=LExists)
   if(LExists)then
     open( unit=iRW, file=DataFileIn    , action='read'    &
                   , access='sequential', status='old' ,err=75)
       read(iRW,*,err=55) YourDataHeader ; write(iP,*) YourDataHeader  ;goto 60
55     write(iP,*) "Error reading line 1 of DataFileIn"
60     read(iRW,*,err=65) Nin ; write(iP,"(i7,', : Nin')") Nin         ;goto 70
65     write(iP,*) "Error reading line 2 of DataFileIn"
70     read(iRW,*,err=75) iRankIn
                      write(iP,"(i7,', : iRankIn   ignored.')")iRankIn ;goto 80
75     write(iP,*) "Error reading line 3 of DataFileIn"

80     allocate(C(Nin,Nin),stat=iAlloc)
       if(iAlloc.ne.0) then; Pause &
         'jInvert_Demo: C(Nin,Nin) allocation error. Will halt. Press enter'
         Stop '@ 105'
       endif!(iAlloc.ne.0)
       C = ( 0._16, 0._16 )  !clears the allocated complex matrix.
     do i = 1,Nin; do j = 1,Nin      
       read(iRW,*,err=85) i2,j2, C(i2,j2)
       write(iP,"(2(i3,','),sp,' (',e38.30,',',e38.30,'),')") &
                      i2,j2,               C(i2,j2)
     enddo        ;enddo                                               ;goto 90
85   write(iP,*) "Error reading the complex matrix coefficients"

90   close(iRW)                                                       ;goto 100
   endif!(LExists)
   write(iP,*) "DataFileIn not found. Using defaults."
   
  !Call jInvert():
100 call jInvert(Nin,C,iRankOut,iRW,iP)

   write(iP,"( 'jCinverse:')")  
   write(iP,*) YourDataHeader
   write(iP,"(i7,', : Nin')") Nin  
   write(iP,"(i7,', : iRankOut')")iRankOut

   do i = 1,Nin; do j = 1,Nin      
     write(iP,"(2(i3,','),sp,' (',e38.30,',',e38.30,'),')") &
                  i,j,                  C(i,j)
   enddo        ;enddo

   if(allocated(C)) deallocate(C)
   write(iP,"('  Program jInvert_Demo}',16x,' [deallocated]'&
                                                        &,t70,' Done @113',/)")
   if(iP>6) close(iP)
End Program jInvert_Demo
!------------------------------------------------------------------------------
