      ! SPACE_GEDIIS SIMPLY LOADS THE CURRENT VALUES OF Geom AND Grad INTO THE ARRAYS GeomSet AND GradSet

      SUBROUTINE SPACE_GEDIIS_All(NGeomF,IGeom,MRESET,MSET,Geom,Grad,HEAT,NCoord,GeomSet,GradSet,ESET,FRST)

      IMPLICIT NONE
      integer, parameter :: KINT = kind(1)
      integer, parameter :: KREAL = kind(1.0d0)

      ! MSET=mth iteration, MRESET=Maximum no. of iterations. 
      INTEGER(KINT) :: NCoord, MRESET, MSET(NGeomF), NGeomF, IGeom 
      ! Geom=geometry with NCoord coordinates. GRAD=gradients.
      REAL(KREAL) :: Geom(NCoord), Grad(NCoord)
	  REAL(KREAL) :: Heat
      LOGICAL :: FRST(NGeomF), Print=.FALSE.
      ! GeomSet, GradSet : a long array to store.
      REAL(KREAL) :: GeomSet(NGeomF,MRESET*NCoord), GradSet(NGeomF,MRESET*NCoord), ESET(NGeomF,MRESET)
      ! Geom and GRAD for all iterations.

      INTEGER(KINT) :: I,J,K,NMK,MI,NI
      INTEGER(KINT), SAVE :: NRESET

      ! UPDATE PARAMETER AND GRADIENT SUBSPACE:
      IF (PRINT)  WRITE(*,'(/,''       BEGIN SPACE_GEDIIS_ALL   '')')
	  IF(FRST(IGeom))THEN
	    ! Print *, 'Inside FRST(',IGeom,')=', FRST(IGeom)
         NRESET=MIN(NCoord/2,MRESET)
             IF (NCoord .LT. 5 ) THEN ! This condition is imposed for small NCoord,
               ! particularly if NCoord is equal to 2 or 3.
                NRESET = NCoord-1
             END IF
         FRST(IGeom)=.FALSE.
         MSET(IGeom)=0
      ENDIF

      IF (PRINT)  WRITE(*,'(/,''       SPACE_GEDIIS_ALL 1  '')')
      ! purging the very first Geom and GRAD.
      if (print) WRITE(*,*) "DBG SPACE_GEDIIS MSET, NRESET", MSET,NRESET
      IF (MSET(IGeom) .EQ. NRESET) THEN
         DO I=1,MSET(IGeom)-1
            MI = NCoord*(I-1)
            NI = NCoord*I
            ESET(IGeom,I)=ESET(IGeom,I+1)
            DO K=1,NCoord
               GeomSet(IGeom,MI+K) = GeomSet(IGeom,NI+K)
               GradSet(IGeom,MI+K) = GradSet(IGeom,NI+K)
            END DO
		 END DO	
            MSET(IGeom)=NRESET-1
      ENDIF

      ! STORE THE CURRENT POINT:
	  IF (PRINT)  WRITE(*,'(/,''       SPACE_GEDIIS_ALL  2  '')')
      DO  K=1,NCoord
         NMK = NCoord*MSET(IGeom)+K ! MSET(IGeom) corresponds the mth iteration.
         IF (PRINT)  WRITE(*,*) 'K,NMK,MSET(',IGeom,'),NCoord',K,NMK,MSET(IGeom),NCoord !, &
          !   SIZE(GeomSet(IGeom,:)), SIZE(GradSet(IGeom,:)),SIZE(Geom),SIZE(GRAD)
         GeomSet(IGeom,NMK) = Geom(K)
         GradSet(IGeom,NMK) = Grad(K)
      END DO
	  
      MSET(IGeom)=MSET(IGeom)+1
      ESET(IGeom,MSET(IGeom))=HEAT
	   
      IF (PRINT) WRITE (*,*) "MSET(",IGeom,")=", MSET(IGeom)
      IF (PRINT)  WRITE(*,'(/,''       END SPACE_GEDIIS_ALL    '')')

      END SUBROUTINE SPACE_GEDIIS_All
