title: cannot use SUNDIALS-KINSOL in FORTRAN subroutine?
body: <p>Thank you for having a look at this problem.</p>
  
  <p><strong>Problem:</strong>
  seg. fault when returning from f90 subroutine that contains KINSOL solving process, <em>after</em> the <em>correct</em> computation result has been generated. No problem when the same solving process is in the main program.</p>
  
  <p><strong>Environment:</strong>
  linux,
  gcc,
  sundials static libs</p>
  
  <p><strong>How to initiate the problem:</strong></p>
  
  <p>get the attached REDUCED test code </p>
  
  <pre><code>module moduleNonlinearSolve
    integer,save::nEq
  contains
    subroutine solveNonlinear(u)
      double precision::u(*)
      integer iout(15),ier
      double precision rout(2),koefScal(nEq)
      koefScal(:)=1d0
      call fnvinits(3,nEq,ier)
      call fkinmalloc(iout,rout,ier)
      call fkinspgmr(50,10,ier)
      call fkinsol(u,1,koefScal,koefScal,ier)
      call fkinfree()
      do i=1,nEq
        write(*,*),i,u(i)
      end do
    end subroutine
  end module
  
  subroutine fkfun(u,fval,ier)
    use moduleNonlinearSolve
    double precision::u(*)
    double precision::fval(*)
    integer::ier
    forall(i=2:nEq-1)
      fval(i)=-u(i-1)+2d0*u(i)-u(i+1)-1d0
    end forall
    fval(1)=u(1)+2d0*u(1)-u(2)-1d0
    fval(nEq)=-u(nEq-1)+2d0*u(nEq)+u(nEq)-1d0
    ier=0
  end subroutine
  
  program test
    use moduleNonLinearSolve
    double precision u(10)
    nEq=size(u)
    u(:)=10d0
    call solveNonlinear(u)
  end program``
  </code></pre>
  
  <p>compile</p>
  
  <pre><code>$ gfortran -c -Wall -g test.f90
  $ gfortran -Wall -g -o test test.o -lsundials_fkinsol -lsundials_fnvecserial -lsundials_kinsol -lsundials_nvecserial -llapack -lblas
  </code></pre>
  
  <p>run</p>
  
  <pre><code>$ ./test
  </code></pre>
  
  <p><strong>Note: It would work flawlessly if put all the SUNDIALS procedures in the main program.</strong></p>
  
  <p>Thank you very much for any input.</p>
  
  <p>Mianzhi</p>