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

      SUBROUTINE SPACE_GEDIIS(MRESET,MSET,Geom,Grad,HEAT,NCoord,GeomSet,GradSet,ESET,FRST)

!----------------------------------------------------------------------
! This routine was adapted from the public domain mopac6 diis.f 
!  source file (c) 2009, Stewart Computational Chemistry.
!  <http://www.openmopac.net/Downloads/Downloads.html>
!
!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------

      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 
      ! Geom=geometry with NCoord coordinates. GRAD=gradients.
    REAL(KREAL), INTENT(IN) :: Geom(NCoord)
      REAL(KREAL) :: Grad(NCoord)
    REAL(KREAL) :: Heat
      LOGICAL :: FRST, Print=.TRUE.
      ! GeomSet, GradSet : a long array to store
      REAL(KREAL) :: GeomSet(MRESET*NCoord), GradSet(MRESET*NCoord), ESET(MRESET)
      ! Geom and GRAD for all iterations.

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

      ! UPDATE PARAMETER AND GRADIENT SUBSPACE:
      IF (PRINT)  WRITE(*,'(/,''       BEGIN SPACE_GEDIIS  '')')
      IF(FRST)THEN
         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=.FALSE.
         MSET=0
      ENDIF

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

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

      END SUBROUTINE SPACE_GEDIIS
