       subroutine func2(x,f,g)
       double precision x(3),g(3),xp,yp,zp,f
       double precision rho,elfd,drho(3),delf(3),d2rho(6),d2elf(6)
       integer level
       logical func_type
       common/ftype/func_type
       level=2
       xp=x(1)
       yp=x(2)
       zp=x(3)
       call elf_eval(rho,drho,d2rho,elfd,delf,d2elf,xp,yp,zp,level)
       if(func_type) then
          f=delf(1)**2+delf(2)**2+delf(3)**2
          g(1)=delf(1)*d2elf(1)+delf(2)*d2elf(2)+delf(3)*d2elf(3)
          g(2)=delf(1)*d2elf(2)+delf(2)*d2elf(4)+delf(3)*d2elf(5)
          g(3)=delf(1)*d2elf(3)+delf(2)*d2elf(5)+delf(3)*d2elf(6)
       else
          f=drho(1)**2+drho(2)**2+drho(3)**2
          g(1)=drho(1)*d2rho(1)+drho(2)*d2rho(2)+drho(3)*d2rho(3)
          g(2)=drho(1)*d2rho(2)+drho(2)*d2rho(4)+drho(3)*d2rho(5)
          g(3)=drho(1)*d2rho(3)+drho(2)*d2rho(5)+drho(3)*d2rho(6)
       endif
       g(1)=2.0d0*g(1)
       g(2)=2.0d0*g(2)
       g(3)=2.0d0*g(3)
       return
       end
