       subroutine func(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,iacc
       logical func_type
       common/ftype/func_type
       common/accuracy/iacc
       level=1
       xp=x(1)
       yp=x(2)
       zp=x(3)
       if(iacc.eq.0) then
          call elf_eval(rho,drho,d2rho,elfd,delf,d2elf,xp,yp,zp,level)
          if(func_type) then
             f=-elfd
             g(1)=-delf(1)
             g(2)=-delf(2)
             g(3)=-delf(3)
          else
             f=-rho
             g(1)=-drho(1)
             g(2)=-drho(2)
             g(3)=-drho(3)
          endif
       else
          if(func_type) then
             call elf_dif(f,g,xp,yp,zp)
          else
             call rho_dif(f,g,xp,yp,zp)
          endif
          f=-f
          g(1)=-g(1)
          g(2)=-g(2)
          g(3)=-g(3)
       endif
       return
       end
