      SUBROUTINE free_mem_ns_par(lreset)
      USE vmec_main
      USE realspace
      USE vforces
      USE xstuff
      USE csplinx
      USE fbal

      IMPLICIT NONE
C-----------------------------------------------
C   D u m m y   A r g u m e n t s
C-----------------------------------------------
      LOGICAL, INTENT(in) :: lreset

C-----------------------------------------------
      IF (ALLOCATED(pshalf))  DEALLOCATE(pshalf)
      IF (ALLOCATED(psqrts))  DEALLOCATE(psqrts)
      IF (ALLOCATED(pwint))   DEALLOCATE(pwint)
C     <MJL 2021-02-12>
      IF (ALLOCATED(pwint_ns))   DEALLOCATE(pwint_ns)
      IF (ALLOCATED(ireflect_par)) DEALLOCATE(ireflect_par)
C     </MJL>
      IF (ALLOCATED(pfaclam)) DEALLOCATE(pfaclam)
      IF (ALLOCATED(pchip))   DEALLOCATE(pchip)
      IF (ALLOCATED(pphip))   DEALLOCATE(pphip)
      IF (ALLOCATED(pfaclam)) DEALLOCATE(pfaclam)

      IF (ALLOCATED(pxc)     .and. lreset) DEALLOCATE(pxc)
      IF (ALLOCATED(pscalxc) .and. lreset) DEALLOCATE (pscalxc)

      IF (ALLOCATED(pxstore))    DEALLOCATE (pxstore)
      IF (ALLOCATED(pxcdot))     DEALLOCATE (pxcdot)
      IF (ALLOCATED(pxsave))     DEALLOCATE (pxsave)
      IF (ALLOCATED(pgc))        DEALLOCATE (pgc)
      IF (ALLOCATED(pcol_scale)) DEALLOCATE(pcol_scale)

      END SUBROUTINE free_mem_ns_par

      SUBROUTINE free_mem_ns(lreset)
      USE vmec_main
      USE realspace
      USE vforces
      USE xstuff
      USE csplinx
      USE fbal

      IMPLICIT NONE
C-----------------------------------------------
C   D u m m y   A r g u m e n t s
C-----------------------------------------------
      LOGICAL, INTENT(in) :: lreset
C-----------------------------------------------
C   L o c a l   V a r i a b l e s
C-----------------------------------------------
      INTEGER :: istat1 = 0, istat2 = 0, istat3 = 0, istat4 = 0,
     1           istat5 = 0, istat6 = 0, istat7 = 0, istat8 = 0,
     2           istat9 = 0, istat10 = 0
C-----------------------------------------------

      IF (ALLOCATED(phip)) THEN
         DEALLOCATE(phip, chip, shalf, sqrts, wint, stat=istat1)
      END IF

      IF (ALLOCATED(ireflect)) THEN
         DEALLOCATE(ireflect, stat=istat2)
      END IF

      IF (ALLOCATED(ard)) THEN
         DEALLOCATE(ard, arm, brd, brm, crd, azd, azm, bzd, bzm, sm,
     &              sp, bmin, bmax, stat=istat5)
      END IF

!      IF (ALLOCATED(rru_fac)) THEN
!        DEALLOCATE(rru_fac,frcc_fac,fzsc_fac,icurv,vpphi,presgrad,
!     &             r01,z01,bdamp, stat=istat6)
      IF (ALLOCATED(iotaf)) THEN
         DEALLOCATE(iotaf, mass, phi, presf, jcuru, jcurv, jdotb, buco)
         DEALLOCATE(bvco, bucof, bvcof, chi)
!#ifdef _ANIMEC
!     &              phot, pmap, pppr, papr, tpotb, pd,
!#endif
         DEALLOCATE(bdotgradv, equif, specw, tcon, psi, yellip, yinden)
         DEALLOCATE(ytrian, yshift, ygeo, overr, faclam, iotas, phips)
         DEALLOCATE(chips, pres, vp, beta_vol, jperp2, jpar2, bdotb)
         DEALLOCATE(clam, blam, dlam, phipf, chipf, rru_fac, rzu_fac)
         DEALLOCATE(frcc_fac, fzsc_fac, icurv, vpphi, presgrad, r01)
         DEALLOCATE(z01, bdamp, stat=istat6)
      END IF

      IF (ALLOCATED(rmidx)) THEN
         DEALLOCATE(rmidx, hmidx, wmidx, qmidx, tenmidx, ymidx, y2midx,
     &              stat=istat7)
      END IF

      IF (ALLOCATED(gc)) THEN
         DEALLOCATE(gc, xsave, xstore, xcdot, col_scale, stat=istat8)
      END IF
      IF (ALLOCATED(xc) .AND. lreset) THEN
         DEALLOCATE(xc, scalxc)
      END IF

      IF ((istat1 + istat2 + istat3 + istat4 + istat5 + istat6 +
     &     istat7 + istat8) .ne. 0) THEN
          PRINT *,' deallocation problem in free_mem_ns'
          PRINT *,' istat1 = ',istat1,' istat2 = ',istat2
          PRINT *,' istat3 = ',istat3,' istat4 = ',istat4
          PRINT *,' istat5 = ',istat5,' istat6 = ',istat6
          PRINT *,' istat7 = ',istat7,' istat8 = ',istat8
      END IF

      END SUBROUTINE free_mem_ns
