!C
!C  SPACE SIMPLY LOADS THE CURRENT VALUES OF Geom AND GRAD INTO
!C  THE ARRAYS GeomSet AND GradSet
!C
      SUBROUTINE SPACE(MRESET,MSET,Geom,GRAD,HEAT,NVAR,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)

!      INCLUDE 'SIZES'

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

      INTEGER(KINT) :: I, K, NMK, MI, NI
      INTEGER(KINT), SAVE :: NRESET
!C
!C     UPDATE PARAMETER AND GRADIENT SUBSPACE
!C
      IF (PRINT)  WRITE(*,'(/,''       BEGIN SPACE  '')')
      IF(FRST)THEN
         NRESET=MIN(NVAR/2,MRESET)
             IF (NVAR .LT. 5 ) THEN ! This condition is imposed for small NVAR,
             ! particularly if NVAR=NCOORD is equal to 2 or 3.
                NRESET = NVAR-1
           END IF
         FRST=.FALSE.
         MSET=0
      ENDIF
!C
      !IF (PRINT)  WRITE(*,'(/,''       SPACE 1  '')')
        ! purging the very first Geom and GRAD.
        if (print) WRITE(*,*) "DBG Space MSET, NRESET",MSET,NRESET
       IF (MSET .EQ. NRESET) THEN
         DO 10 I=1,MSET-1
            MI = NVAR*(I-1)
            NI = NVAR*I
            ESET(I)=ESET(I+1)
            DO 10 K=1,NVAR
               GeomSet(MI+K) = GeomSet(NI+K)
   10    GradSet(MI+K) = GradSet(NI+K)
            MSET=NRESET-1
       ENDIF
!C
!C     STORE THE CURRENT POINT
!C
      !IF (PRINT)  WRITE(*,'(/,''       SPACE 2  '')')
        ! NVAR =  NCOORD
      DO  K=1,NVAR
         NMK = NVAR*MSET+K ! MSET corresponds mth iteration.
         IF (PRINT)  WRITE(*,*) 'K,NMK,MSET,NVAR',K,NMK,MSET,NVAR, &
              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   '')')
!C
      RETURN
      END
