module invint_mod
  use lint1
  use tcai1
  use cgstep_mod
  use random_mod
  use solver_reg_mod

  implicit none

  logical, save                          :: multreal = .false.
  integer, save                          :: seed = 2941
  real, save                             :: scale = 1.0

contains

  ! --------------------------------------------------------------------------------
  subroutine init_random( mu, se, sc)
    logical                         :: mu
    integer                         :: se
    real                            :: sc

    seed = se;  scale = sc;  multreal = mu
  end subroutine init_random
  ! --------------------------------------------------------------------------------


  ! --------------------------------------------------------------------------------
  subroutine invint( niter, pair, nd, o1, d1, aa, mm, eps)
    integer,               intent (in)   :: niter, nd
    real,                  intent (in)   :: o1, d1, eps
    real, dimension (:),   pointer       :: aa
    real, dimension (:,:), pointer       :: pair
    real, dimension (:),   intent (out)  :: mm
    optional    :: eps

    real, dimension (:),   pointer       :: ord, dd
    real                                 :: ep, rh
    integer                              :: nreg, nm, stat

    real, dimension (:),   pointer       :: xnull, ones, fold

    ord => pair (1 : nd,1)
    dd  => pair (1 : nd,2)

    nm = size (mm)
    call lint1_init (o1, d1, ord)

    allocate( ones(nd), fold(nm) ); ones = 1; fold = 0
    stat = lint1_lop( .false., .false., ones, fold)

    ep = 1.; if (present (eps)) ep = eps

    allocate( xnull(nm) );  xnull = 0
    if( multreal) then
       call random_init(seed)
       call rand_real_array_1d( xnull)
       xnull=scale*(xnull-0.5)
    end if

    call tcai1_init(aa)
    nreg = size (aa) + size (mm)

!    call solver_reg(m=mm, d=dd, &
!    Fop=lint1_lop, stepper=cgstep, niter=niter, &
!    Aop=tcai1_lop, nAop=nreg, eps=ep, &
!    verb=.true.)

    call cgstep_close ()
  end subroutine invint
  ! --------------------------------------------------------------------------------

end module invint_mod

