+#ifdef LBFGS_SC
+ double precision function funcgrad_restr1(x,g)
+ implicit real*8 (a-h,o-z)
+ include 'DIMENSIONS'
+ include 'COMMON.DERIV'
+ include 'COMMON.IOUNITS'
+ include 'COMMON.GEO'
+ include 'COMMON.FFIELD'
+ include 'COMMON.INTERACT'
+ include 'COMMON.TIME1'
+ include 'COMMON.CHAIN'
+ include 'COMMON.VAR'
+ integer nvar_restr
+ common /zmienne/ nvar_restr
+ double precision energia(0:n_ene),evdw,escloc
+ double precision ufparm,e1,e2
+ dimension x(maxvar),g(maxvar),gg(maxvar)
+#ifdef OSF
+c Intercept NaNs in the coordinates, before calling etotal
+ x_sum=0.D0
+ do i=1,nvar_restr
+ x_sum=x_sum+x(i)
+ enddo
+ FOUND_NAN=.false.
+ if (x_sum.ne.x_sum) then
+ write(iout,*)" *** func_restr1 : Found NaN in coordinates"
+ f=1.0D+73
+ FOUND_NAN=.true.
+ return
+ endif
+#else
+ FOUND_NAN=.false.
+ do i=1,nvar_restr
+ if (isnan(x(i))) then
+ FOUND_NAN=.true.
+ f=1.0D+73
+ funcgrad_restr1=f
+ write (iout,*) "NaN in coordinates"
+ return
+ endif
+ enddo
+#endif
+
+c write (iout,*) "nvar_restr",nvar_restr
+c write (iout,*) "x",(x(i),i=1,nvar_restr)
+ call var_to_geom_restr(nvar_restr,x)
+ call zerograd
+ call chainbuild_extconf
+cd write (iout,*) 'ETOTAL called from FUNC'
+ call egb1(evdw)
+ call esc(escloc)
+ f=wsc*evdw+wscloc*escloc
+c write (iout,*) "evdw",evdw," escloc",escloc
+ if (isnan(f)) then
+ f=1.0d20
+ funcgrad_restr1=f
+ return
+ endif
+ funcgrad_restr1=f
+c write (iout,*) "f",f
+cd call etotal(energia(0))
+cd f=wsc*energia(1)+wscloc*energia(12)
+cd print *,f,evdw,escloc,energia(0)
+C
+C Sum up the components of the Cartesian gradient.
+C
+ do i=1,nct
+ do j=1,3
+ gradx(j,i,icg)=wsc*gvdwx(j,i)+wscloc*gsclocx(j,i)
+ enddo
+ enddo
+
+C
+C Evaluate the derivatives of virtual bond lengths and SC vectors in variables.
+C
+ call cart2intgrad(nvar,gg)
+C
+C Convert the Cartesian gradient into internal-coordinate gradient.
+C
+
+ ig=0
+ do i=2,nres-1
+ if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
+ IF (mask_side(i).eq.1) THEN
+ ig=ig+1
+ g(ig)=gg(ialph(i,1))
+c write (iout,*) "i",i," ig",ig," ialph",ialph(i,1)
+c write (iout,*) "g",g(ig)," gg",gg(ialph(i,1))
+ ENDIF
+ endif
+ enddo
+ do i=2,nres-1
+ if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
+ IF (mask_side(i).eq.1) THEN
+ ig=ig+1
+ g(ig)=gg(ialph(i,1)+nside)
+c write (iout,*) "i",i," ig",ig," ialph",ialph(i,1)+nside
+c write (iout,*) "g",g(ig)," gg",gg(ialph(i,1)+nside)
+ ENDIF
+ endif
+ enddo
+
+C
+C Add the components corresponding to local energy terms.
+C
+
+ ig=0
+ igall=0
+ do i=4,nres
+ igall=igall+1
+ if (mask_phi(i).eq.1) then
+ ig=ig+1
+ g(ig)=g(ig)+gloc(igall,icg)
+ endif
+ enddo
+
+ do i=3,nres
+ igall=igall+1
+ if (mask_theta(i).eq.1) then
+ ig=ig+1
+ g(ig)=g(ig)+gloc(igall,icg)
+ endif
+ enddo
+
+ do ij=1,2
+ do i=2,nres-1
+ if (itype(i).ne.10 .and. itype(i).ne.ntyp1) then
+ igall=igall+1
+ if (mask_side(i).eq.1) then
+ ig=ig+1
+ g(ig)=g(ig)+gloc(igall,icg)
+c write (iout,*) "ij",ij," i",i," ig",ig," igall",igall
+c write (iout,*) "gloc",gloc(igall,icg)," g",g(ig)
+ endif
+ endif
+ enddo
+ enddo
+
+cd do i=1,ig
+cd write (iout,'(a2,i5,a3,f25.8)') 'i=',i,' g=',g(i)
+cd enddo
+ return
+ end
+#else