module base_lapc2d_mod
  use  base_adjnul_mod
  use  base_grad2d_mod
  implicit none

  integer                           , private :: n1, n2
  real   , dimension(:), allocatable, private :: temp
  logical, parameter                , private :: T = .true.
  logical, parameter                , private :: F = .false.
contains

  integer function base_lapc2d_init(n1_in, n2_in) result(stat)
    integer :: n1_in, n2_in
    n1 = n1_in
    n2 = n2_in
    if (.not.allocated(temp)) allocate(temp(n1*n2*2))
    stat = base_grad2d_init(n1, n2)
    stat = 0
  end function

  integer function base_lapc2d_oper(adj, add, modl, data) result(stat)
    logical            :: adj, add
    real, dimension(:) :: modl, data

    call base_adjnul_real(adj, add, modl, data)
    call base_lapc2d(adj, add, modl, data)
    stat = 0
  end function

  integer function base_lapc2d_clos() result(stat)
    n1 = 0
    n2 = 0
    deallocate(temp)
    stat = base_grad2d_clos()
    stat = 0
  end function

  subroutine base_lapc2d(adj, add, modl, data)
    logical            :: adj, add
    real, dimension(:) :: modl, data

    integer            :: stat

    if (adj) then
       stat = base_grad2d_oper(F, F  , data, temp)
       stat = base_grad2d_oper(T, add, modl, temp)
    else
       stat = base_grad2d_oper(F, F  , modl, temp)
       stat = base_grad2d_oper(T, add, data, temp)
    end if

  end subroutine


end module
