C SUBROUTINE FILE 'VAR.FOR' TO STORE IN THE MEMORY VARIATIONS OF THE
C FUNCTIONS DESCRIBING THE MODEL, WITH RESPECT TO THEIR COEFFICIENTS.
C
C BY LUDEK KLIMES
C
C THIS FILE CONSISTS OF THE FOLLOWING SUBROUTINE AND ITS ENTRIES:
C     VAR1... SUBROUTINE DESIGNED TO INITIALIZE (I.E. TO CLEAR) THE
C             MEMORY STORAGE LOCATIONS.  AFTER INVOCATION OF THIS
C             SUBROUTINE NO VARIATIONS ARE IN THE MEMORY, THUS THE
C             VARIATIONS AT A NEW POINT IN THE MODEL MAY BE STARTED TO
C             BE STORED (SEE ENTRY VAR2).
C             THIS SUBROUTINE CONTAINES ENTRIES VAR2, VAR3, VAR4, VAR5
C             AND VAR6 LISTED BELOW.
C     VAR2... ENTRY OF THE SUBROUTINE VAR1, DESIGNED TO STORE VARIATIONS
C             OF THE FUNCTIONS DESCRIBING THE MODEL IN THE MEMORY.
C             ONE NEW VARIATION IS STORED BY ONE INVOCATION, BEING ADDED
C             INTO THE REGISTER NO. 0.  NOTE THAT ONE VARIATION CONSISTS
C             OF THE VARIATION OF THE FUNCTIONAL VALUE AND ITS THREE
C             FIRST DERIVATIVES.
C     VAR3... ENTRY OF THE SUBROUTINE VAR1, DESIGNED TO REPLACE THE
C             RELATIVE INDICES OF THE FUNCTION COEFFICIENTS BY THE
C             ABSOLUTE ONES IN THE REGISTER 0.  IT SHOULD BE CALLED
C             AFTER THE REGISTER 0 IS FILLED BY THE PROPER NUMBER OF
C             INVOCATIONS OF THE SUBROUTINE VAR2.
C     VAR4... ENTRY OF THE SUBROUTINE VAR1, DESIGNED TO DEFINE AND/OR
C             REBUILD THE 4*4 TRANSFORMATION MATRIX WHICH MAY BE APPLIED
C             TO THE STORED VARIATIONS IN ORDER TO MODIFY THEM.
C     VAR5... ENTRY OF THE SUBROUTINE VAR1, DESIGNED TO MODIFY THE
C             STORED VARIATIONS BY MEANS OF A LINEAR TRANSFORMATION,
C             AND/OR TO COPY/MOVE THEM TO THE REGISTERS CORRESPONDING TO
C             THE INDIVIDUAL FUNCTIONS DESCRIBING THE MODEL.
C             THE LINEAR TRANSFORMATION IS DEFINED BY INVOCATON(S) OF
C             THE ABOVE ENTRY VAR4.
C     VAR6... ENTRY OF THE SUBROUTINE VAR1, DESIGNED TO RECALL THE
C             STORED VARIATIONS CORRESPONDING TO A GIVEN FUNCTION
C             DESCRIBING THE MODEL.
C
C ATTENTION:
C (A) WHEN LINKING THIS SUBROUTINE FILE WITH THE FILE 'VAL.FOR',
C     SUBROUTINES CURVB1 AND CURVBD OF THE FILE 'FIT.FOR', INSTEAD OF
C     CURVN1 AND CURV2D, MUST BE CALLED FROM THE 'VAL.FOR' FILE.  THIS
C     IS THE DEFAULT IN THE DISTRIBUTED SOURCE CODE.  SEE ALSO THE
C     COMMENT LINES WITH '*' IN THE FIRST COLUMN IN THE FILE 'VAL.FOR'.
C (B) IN THE BASIC VERSION OF C.R.T. ROUTINES, SUBROUTINES VAR* ARE
C     CALLED FROM THE FOLLOWING SUBROUTINE FILES:
C             'MODEL.FOR' 7 TIMES (IN SUBROUTINES VELOC AND POWER),
C             'PARM.FOR'  7 TIMES (IN SUBROUTINE PARM2),
C             'VAL.FOR'  21 TIMES (IN SUBROUTINE VAL2),
C             'FIT.FOR'   3 TIMES (IN SUBRS. CURVBD, SURFBD AND VAL3BD).
C     NOTE THAT THE CORRESPONDING CALL STATEMENTS CONTAIN THE SUBSTRING
C             '     CALL VAR', AND ARE DENOTED BY 'V' IN THE FIRST
C             COLUMN OF THE DISTRIBUTED SOURCE CODE.
C     EACH 'V' IN THE FIRST COLUMN OF THE ABOVE MENTIONED FILES HAS TO
C     BE REPLACED BY ' ' (A BLANK) IF LINKING WITH 'VAR.FOR'.
C
C RELATIVE CPU-TIME USAGE FOR THE DEMO DATA:
C     CURVN1, CURV2D, NO CALL VAR*:   1.00
C     CURVN1, CURV2D, 'VARNUL':       1.16
C     CURVB1, CURVBD, NO CALL VAR*:   1.04
C     CURVB1, CURVBD, 'VARNUL':       1.22
C     CURVB1, CURVBD, 'VAR':          1.88
C
C DATE: 1992, DECEMBER 3
C CODED BY LUDEK KLIMES
C
C-----------------------------------------------------------------------
C
      SUBROUTINE VAR1()
C     DUMMY ARGUMENTS OF ALL ENTRIES:
      INTEGER IBI,IBB,IVAL,IVAL0,II,NBI
      REAL B0I,B1I,B2I,B3I,BBI
C
C THIS SUBROUTINE IS DESIGNED TO INITIALIZE (I.E.  TO CLEAR) THE MEMORY
C STORAGE LOCATIONS.  AFTER INVOCATION OF THIS SUBROUTINE NO VARIATIONS
C ARE IN THE MEMORY, THUS THE VARIATIONS AT A NEW POINT IN THE MODEL MAY
C BE STARTED TO BE STORED (SEE ENTRY VAR2).
C
C NO INPUT.
C
C NO OUTPUT.
C
C NO SUBROUTINES AND EXTERNAL FUNCTIONS REQUIRED.
C
C.......................................................................
C
C     STORAGE LOCATIONS (COMMON TO ALL ENTRIES):
C
      INTEGER MFUNCT,MB
      PARAMETER (MFUNCT=6,MB=320)
      INTEGER NB(0:MFUNCT),IB(MB), IAUX,I,J,JB,JB0,JVAL,JVAL0
      REAL B0(MB),B1(MB),B2(MB),B3(MB),BB(16), AUX0,AUX1,AUX2,AUX3
      SAVE NB,IB,B0,B1,B2,B3,BB
C
C.......................................................................
C
      DO 11 I=0,MFUNCT
        NB(I)=0
   11 CONTINUE
      RETURN
C
C-----------------------------------------------------------------------
C
      ENTRY VAR2(IBI,B0I,B1I,B2I,B3I)
C     INTEGER IBI
C     REAL B0I,B1I,B2I,B3I
C
C THIS ENTRY IS DESIGNED TO STORE VARIATIONS OF THE FUNCTIONS DESCRIBING
C THE MODEL IN THE MEMORY.  ONE NEW VARIATION IS STORED BY ONE
C INVOCATION, BEING ADDED INTO THE REGISTER NO.  0.  NOTE THAT ONE
C VARIATION CONSISTS OF THE VARIATION OF THE FUNCTIONAL VALUE AND ITS
C THREE FIRST DERIVATIVES.
C
C INPUT:
C     IBI...  INDEX OF THE FUNCTION COEFFICIENT, RELATIVE TO THE
C             BEGINNING OF THE FUNCTION.
C     B0I,B1I,B2I,B3I... VARIATION OF THE FUNCTIONAL VALUE AND THE THREE
C             FIRST DERIVATIVES, WITH RESPECT TO THE IBI-TH COEFFICIENT
C             OF THE FUNCTION.
C THE INPUT PARAMETERS ARE NOT ALTERED.
C
C NO OUTPUT.
C
C
C ERROR MESSAGES:
C     362...  ERROR IN VAR2: ARRAY INDEX OUT OF RANGE:
C             DIMENSION MB OF ARRAYS IB, B0, B1, B2 AND B3 SHOULD BE
C             INCREASED.
C
C.......................................................................
C
      I=NB(MFUNCT)+1
      IF(I.GT.MB) THEN
        PAUSE 'ERROR 362 IN VAR2: ARRAY INDEX OUT OF RANGE.'
        STOP
      END IF
      NB(MFUNCT)=I
      IB(I)=IBI
      B0(I)=B0I
      B1(I)=B1I
      B2(I)=B2I
      B3(I)=B3I
      RETURN
C
C-----------------------------------------------------------------------
C
      ENTRY VAR3(IBI)
C     INTEGER IBI
C
C THIS ENTRY IS DESIGNED TO REPLACE THE RELATIVE INDICES OF THE FUNCTION
C COEFFICIENTS BY THE ABSOLUTE ONES IN THE REGISTER 0.  IT SHOULD BE
C CALLED AFTER THE REGISTER 0 IS FILLED BY THE PROPER NUMBER OF
C INVOCATIONS OF THE SUBROUTINE VAR2.
C
C INPUT:
C     IBI...  SHIFT ADDED TO THE INDEX OF THE FUNCTION COEFFICIENT.
C             IT SHOULD EQUAL THE DIFFERENCE BETWEEN THE ABSOLUTE (SEE
C             ENTRY VAR6) AND RELATIVE (SEE ENTRY VAR2) INDICES OF THE
C             CORRESPONDING FUNCTION.
C THE INPUT PARAMETER IS NOT ALTERED.
C
C NO OUTPUT.
C
C.......................................................................
C
      DO 31 I=NB(MFUNCT-1)+1,NB(MFUNCT)
        IB(I)=IB(I)+IBI
   31 CONTINUE
      RETURN
C
C-----------------------------------------------------------------------
C
      ENTRY VAR4(IBB,BBI)
C     INTEGER IBB
C     REAL BBI
C
C THIS ENTRY IS DESIGNED TO DEFINE AND/OR REBUILD THE 4*4 TRANSFORMATION
C MATRIX WHICH MAY BE APPLIED TO THE STORED VARIATIONS IN ORDER TO
C MODIFY THEM.
C
C INPUT:
C     IBB...  IBB=0: 4*4 TRANSFORMATION MATRIX IS SET TO THE IDENTITY
C               MATRIX MULTIPLIED BY BBI.
C             IBB=1,2,...,16: BBI IS ADDED TO THE IBB-TH ELEMENT OF THE
C               TRANSFORMATION MATRIX.
C     BBI...  GIVEN REAL VALUE.
C THE INPUT PARAMETERS ARE NOT ALTERED.
C
C NO OUTPUT.
C
C.......................................................................
C
      IF(IBB.LE.0) THEN
        DO 41 I=2,15
          BB(I)=0.
   41   CONTINUE
        DO 42 I=1,16,5
          BB(I)=BBI
   42   CONTINUE
      ELSE
        BB(IBB)=BB(IBB)+BBI
      END IF
      RETURN

C-----------------------------------------------------------------------
C
      ENTRY VAR5(IVAL,IVAL0)
C     INTEGER IVAL,IVAL0
C
C THIS ENTRY IS DESIGNED TO MODIFY THE STORED VARIATIONS BY MEANS OF A
C LINEAR TRANSFORMATION, AND/OR TO COPY/MOVE THEM TO THE REGISTERS
C CORRESPONDING TO THE INDIVIDUAL FUNCTIONS DESCRIBING THE MODEL.  THE
C LINEAR TRANSFORMATION IS DEFINED BY INVOCATON(S) OF THE ENTRY VAR4.
C
C INPUT:
C     IVAL,IVAL0... THE VARIATIONS FROM THE REGISTER IVAL0 ARE
C             TRANSFORMED BY MEANS OF THE MATRIX DEFINED THROUGH THE
C             ENTRY VAR4, AND THEN COPIED TO THE REGISTER IVAL.
C             THE TRANSFORMED VARIATIONS ARE APPENDED TO ONES ALREADY
C             STORED IN THE IVAL-TH REGISTER.
C             IF IVAL=IVAL0 OR IVAL0=0, THE ORIGINAL VARIATIONS ARE
C             DELETED FROM THE IVAL0-TH REGISTER, OTHERWISE THE ORIGINAL
C             VARIATIONS ARE RETAINED.
C THE INPUT PARAMETERS ARE NOT ALTERED.
C
C NO OUTPUT.
C
C ERROR MESSAGES:
C     365...  ERROR IN VAR5: ARRAY INDEX OUT OF RANGE:
C             DIMENSION MB OF ARRAYS IB, B0, B1, B2 AND B3 SHOULD BE
C             INCREASED.
C
C.......................................................................
C
      IF(IVAL.LE.0) THEN
        JVAL=MFUNCT
        JB=NB(JVAL-1)
      ELSE
        JVAL=IVAL
        IF(IVAL.EQ.IVAL0) THEN
          JB=NB(JVAL-1)
        ELSE
          JB=NB(JVAL)
        END IF
      END IF
      IF(IVAL0.LE.0) THEN
        JVAL0=MFUNCT
      ELSE
        JVAL0=IVAL0
      END IF
C
      DO 58 J=1,NB(JVAL0)-NB(JVAL0-1)
        JB=JB+1
        IF(JVAL.EQ.MFUNCT.OR.JVAL0.LT.MFUNCT) THEN
          JB0=NB(JVAL0-1)+J
        ELSE
          JB0=NB(JVAL0-1)+1
        END IF
        IAUX=IB(JB0)
        AUX0=B0(JB0)
        AUX1=B1(JB0)
        AUX2=B2(JB0)
        AUX3=B3(JB0)
        IF(JVAL.NE.JVAL0) THEN
          DO 51 I=JVAL,MFUNCT-1
            NB(I)=NB(I)+1
   51     CONTINUE
          IF(JVAL0.LT.MFUNCT) THEN
C           ORIGINAL VARIATIONS ARE NOT DELETED
            JB0=NB(MFUNCT)+1
            NB(MFUNCT)=JB0
          END IF
        END IF
        IF(JB0.GT.MB) THEN
          PAUSE 'ERROR 365 IN VAR5: ARRAY INDEX OUT OF RANGE.'
          STOP
        END IF
        DO 52 I=JB0-1,JB,-1
          IB(I+1)=IB(I)
          B0(I+1)=B0(I)
          B1(I+1)=B1(I)
          B2(I+1)=B2(I)
          B3(I+1)=B3(I)
   52   CONTINUE
        IB(JB)=IAUX
        B0(JB)=BB(1)*AUX0+BB(5)*AUX1+BB( 9)*AUX2+BB(13)*AUX3
        B1(JB)=BB(2)*AUX0+BB(6)*AUX1+BB(10)*AUX2+BB(14)*AUX3
        B2(JB)=BB(3)*AUX0+BB(7)*AUX1+BB(11)*AUX2+BB(15)*AUX3
        B3(JB)=BB(4)*AUX0+BB(8)*AUX1+BB(12)*AUX2+BB(16)*AUX3
   58 CONTINUE
      RETURN
C
C-----------------------------------------------------------------------
C
      ENTRY VAR6(IVAL,II,NBI,IBI,B0I,B1I,B2I,B3I)
C     INTEGER IVAL,II,NBI,IBI
C     REAL B0I,B1I,B2I,B3I
C
C THIS ENTRY IS DESIGNED TO RECALL THE STORED VARIATIONS CORRESPONDING
C TO A GIVEN FUNCTION DESCRIBING THE MODEL.
C
C INPUT:
C     IVAL... INDEX OF THE FUNCTION DESCRIBING THE MODEL.  THE OUTPUT
C             VARIATIONS ARE THUS RECALLED FROM THE IVAL-TH REGISTER.
C     II...   SEQUENTIAL NUMBER WITHIN THE REGISTER OF THE REQUIRED
C             VARIATION OF THE IVAL-TH FUNCTION.
C THE INPUT PARAMETERS ARE NOT ALTERED.
C
C OUTPUT:
C     NBI...  NUMBER OF THE VARIATIONS OF THE IVAL-TH FUNCTION STORED IN
C             THE IVAL-TH REGISTER.
C     IBI...  ABSOLUTE INDEX OF THE FUNCTION COEFFICIENT.  FOR II.GT.NBI
C             UNDEFINED.
C     B0I,B1I,B2I,B3I... VARIATION OF THE FUNCTIONAL VALUE AND THE THREE
C             FIRST DERIVATIVES, WITH RESPECT TO THE IBI-TH COEFFICIENT
C             OF THE MODEL.  FOR II.GT.NBI UNDEFINED.
C
C.......................................................................
C
      NBI=NB(IVAL)-NB(IVAL-1)
      IF(II.LE.NBI) THEN
        I=NB(IVAL-1)+II
        IBI=IB(I)
        B0I=B0(I)
        B1I=B1(I)
        B2I=B2(I)
        B3I=B3(I)
      END IF
      RETURN
      END
C
C=======================================================================
C
