!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Module LinLib
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: linlib.f90,v 1.7 2005/02/03 02:30:23 odakker Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!線形計算ライブラリ SSL II と LAPACK のラッパー
!
!== Error Handling
!
!== Known Bugs
!
!LAPACK を選択する部分をコーディングしていない. 
!
!== Note
!
!== Future Plans
!
                                                                 !=end

module linlib
  implicit none
  
  private
                                                                 !=begin
  public linlib_init       !初期化ルーチン
  public linsolv           !実 3 項行列の連立 1 次方程式(倍精度)

  character(1)  :: LLib    !用いるライブラリのフラグ
  save LLib
                                                                 !=end    
contains
                                                                 !=begin
  !== Procedure Interface 
  !
  !=== Initialize module and acquire NAMELIST
  !
  !利用する線形計算パッケージを NAMELIST から設定し, 
  !配列の大きさを決定する
  !
  subroutine linlib_init(cfgfile, N)

    !=== Dependency
    use dc_trace, only : BeginSub, EndSub
    use dc_message, only: MessageNotify
    use ssl2_linear, only: ssl2_ltx_init

    !=== Input
    integer, intent(in)      :: N       !配列サイズ
    character(*), intent(in) :: cfgfile !設定ファイル (NAMELIST)
    
    !=== NAMELIST
    NAMELIST /linlib/ LLib
                                                                 !=end
    open (10, FILE=cfgfile)
    read(10, NML=linlib)
    close(10)

    call BeginSub("linlib_init", &
&                 fmt="%c",       &
&                 c1="Initialize linear algebra library.")

    !--- 用いるライブラリの初期化
    if (LLib == 's') then 
       call ssl2_ltx_init(N)

    elseif (LLib == 'l') then 
      call MessageNotify("Message", "linlib.f90", &
&                         "Sorry, LAPACK is not supported.")

    else
      call MessageNotify("Error", "linlib.f90", &
&                        "Unknown LLib ", c1=LLib)
    end if

    call EndSub("linlib_init")

  end subroutine linlib_init
  
                                                                 !=begin
  !== Procedure Interface 
  !
  !=== Initialize module
  !
  !利用する線形計算パッケージに配列を渡す
  !
  subroutine LinSolv(A, B, C, D)

    !=== Dependency
    use dc_trace, only : BeginSub, EndSub
    use dc_message, only: MessageNotify
    use ssl2_linear, only: ssl2_ltx

    !=== Input
    real(8), intent(in)    :: A(:)
    real(8), intent(in)    :: B(:)
    real(8), intent(in)    :: C(:) 

    !=== In/Out
    real(8), intent(inout) :: D(:)
                                                                 !=end

    call BeginSub("linsolv", &
&                 fmt="%c",       &
&                 c1="call linear algebra library.")

    !--- 用いるライブラリの初期化
    if (LLib == 's') then 
       call ssl2_ltx(A, B, C, D)

    elseif (LLib == 'l') then 
      call MessageNotify("Message", "linlib", &
&                        "Sorry, LAPACK is not supported." )
       
    else
      call MessageNotify("Error", "linlib", &
&                        "unknown LLib")
    end if

    call EndSub("linsolv")

  end subroutine LinSolv
  
end module linlib
