IDENTIFICATION DIVISION.
PROGRAM-ID. ORBITS.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
    SOURCE-COMPUTER. MACBOOKPRO.
    OBJECT-COMPUTER. OPENCOBOL.

INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT ENTRADA ASSIGN TO 'entrada.dat'
    ORGANIZATION IS LINE SEQUENTIAL.

    SELECT SORTIDA ASSIGN TO 'sortida.lst'
    ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD  ENTRADA DATA RECORDS ARE REG-E-COORD REG-E-MASS REG-E-TIME.
01  REG-E-COORD.
    03 I-X-COORD        PIC +9999999999 USAGE IS DISPLAY.
    03 I-Z-COORD        PIC +9999999999 USAGE IS DISPLAY.
    03 I-Y-COORD        PIC +9999999999 USAGE IS DISPLAY.
01  REG-E-MASS.
    03 I-MASS           PIC +9999999999 USAGE IS DISPLAY.
01  REG-E-TIME.
    03 I-INTERVAL       PIC +9999999999 USAGE IS DISPLAY.
    03 I-INCREMENT      PIC +9999999999 USAGE IS DISPLAY.

FD  SORTIDA DATA RECORD IS LIN-SORTIDA.
01  LIN-SORTIDA            PIC X(80) VALUE SPACES.

LOCAL-STORAGE SECTION.
01  R1.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
01  R2.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
01  V1.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
01  V2.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
01  A1.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
01  A2.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
01  W.
    03 X                COMP-2.
    03 Y                COMP-2.
    03 Z                COMP-2.
77  G                   COMP-2.
77  I                   PIC S9(9) COMP.
77  M1                  COMP-2.
77  M2                  COMP-2.
77  R                   COMP-2.
77  COEF                COMP-2.
77  DT                  COMP-2.
77  NINCR               PIC S9(9) COMP.
01  SW-EOF              PIC X VALUE SPACE.
    88 EOF VALUE 'S'.

01  LIN-CAP1.
    05 FILLER              PIC X(18) VALUE 'X'.
    05 FILLER              PIC X     VALUE SPACE.
    05 FILLER              PIC X(18) VALUE 'Y'.
    05 FILLER              PIC X     VALUE SPACE.
    05 FILLER              PIC X(18) VALUE 'Z'.

01  LIN-CAP2.
    05 FILLER              PIC X(18) VALUE ALL '='.
    05 FILLER              PIC X     VALUE SPACE.
    05 FILLER              PIC X(18) VALUE ALL '='.
    05 FILLER              PIC X     VALUE SPACE.
    05 FILLER              PIC X(18) VALUE ALL '='.

01  LIN-DET.
    05 X-D                 PIC Z(13).9999 VALUE ZERO.
    05 FILLER              PIC X     VALUE SPACE.
    05 Y-D                 PIC Z(13).9999 VALUE ZERO.
    05 FILLER              PIC X     VALUE SPACE.
    05 Z-D                 PIC Z(13).9999 VALUE ZERO.

PROCEDURE DIVISION.
MAIN SECTION.
BEGIN.
    DISPLAY '*** INICI ***'
    MOVE '6.67E-11' TO G
    OPEN INPUT ENTRADA
    OPEN OUTPUT SORTIDA
    PERFORM LLISTAR-CAPCALERA
    PERFORM CARREGAR-DADES
    PERFORM FER-CALCULS VARYING I FROM 1 BY 1 UNTIL I EQUAL NINCR
    CLOSE ENTRADA
    CLOSE SORTIDA
    DISPLAY '*** FINAL ***'
    STOP RUN
    .

FER-CALCULS.
    PERFORM CALCULA-ACCELERACIO
    PERFORM APLICA-VELOCITAT
    PERFORM APLICA-POSICIO
    MOVE X IN R1 TO X-D
    MOVE Y IN R1 TO Y-D
    MOVE Z IN R1 TO Z-D
    MOVE LIN-DET TO LIN-SORTIDA
    WRITE LIN-SORTIDA
    .

CALCULA-ACCELERACIO.
    MOVE R1 TO W
    SUBTRACT X IN R2 FROM X IN W
    SUBTRACT Y IN R2 FROM Y IN W
    SUBTRACT Z IN R2 FROM Z IN W
    PERFORM CALCULA-R
    COMPUTE COEF = -G * M1 * M2 / R**3
    COMPUTE X IN A1 = COEF * X IN W / M1
    COMPUTE Y IN A1 = COEF * Y IN W / M1
    COMPUTE Z IN A1 = COEF * Z IN W / M1
    COMPUTE X IN A2 = -1 * COEF * X IN W / M2
    COMPUTE Y IN A2 = -1 * COEF * Y IN W / M2
    COMPUTE Z IN A2 = -1 * COEF * Z IN W / M2
    .

CALCULA-R.
    COMPUTE R = FUNCTION SQRT ( X IN W * X IN W +
                                Y IN W * Y IN W +
                                Z IN W * Z IN W)
    .


APLICA-VELOCITAT.
    COMPUTE X IN V1 = X IN V1 + DT * X IN A1
    COMPUTE Y IN V1 = Y IN V1 + DT * Y IN A1
    COMPUTE Z IN V1 = Z IN V1 + DT * Z IN A1
    COMPUTE X IN V2 = X IN V2 + DT * X IN A2
    COMPUTE Y IN V2 = Y IN V2 + DT * Y IN A2
    COMPUTE Z IN V2 = Z IN V2 + DT * Z IN A2
    .

APLICA-POSICIO.
    COMPUTE X IN R1 = X IN R1 + DT * X IN V1
    COMPUTE Y IN R1 = Y IN R1 + DT * Y IN V1
    COMPUTE Z IN R1 = Z IN R1 + DT * Z IN V1
    COMPUTE X IN R2 = X IN R2 + DT * X IN V2
    COMPUTE Y IN R2 = Y IN R2 + DT * Y IN V2
    COMPUTE Z IN R2 = Z IN R2 + DT * Z IN V2
    .

CARREGAR-DADES.
    PERFORM LLEGIR-ENTRADA
    MOVE I-X-COORD TO X IN R1
    MOVE I-Y-COORD TO Y IN R1
    MOVE I-Z-COORD TO Z IN R1
    PERFORM LLEGIR-ENTRADA
    MOVE I-X-COORD TO X IN V1
    MOVE I-Y-COORD TO Y IN V1
    MOVE I-Z-COORD TO Z IN V1
    PERFORM LLEGIR-ENTRADA
    MOVE I-MASS TO M1
    PERFORM LLEGIR-ENTRADA
    MOVE I-X-COORD TO X IN R2
    MOVE I-Y-COORD TO Y IN R2
    MOVE I-Z-COORD TO Z IN R2
    PERFORM LLEGIR-ENTRADA
    MOVE I-X-COORD TO X IN V2
    MOVE I-Y-COORD TO Y IN V2
    MOVE I-Z-COORD TO Z IN V2
    PERFORM LLEGIR-ENTRADA
    MOVE I-MASS TO M2
    PERFORM LLEGIR-ENTRADA
    MOVE I-INTERVAL TO DT
    MOVE I-INCREMENT TO NINCR
    .

LLEGIR-ENTRADA.
    READ ENTRADA
      AT END
        SET EOF TO TRUE
        GO TO ABORT-ARXIU-ENTRADA
    END-READ
    .

LLISTAR-CAPCALERA.
    MOVE LIN-CAP1 TO LIN-SORTIDA
    WRITE LIN-SORTIDA
    MOVE LIN-CAP2 TO LIN-SORTIDA
    WRITE LIN-SORTIDA
    .


ABORT-ARXIU-ENTRADA.
    DISPLAY 'ERROR DE FORMAT A ARXIU D''ENTRADA' UPON CONSOLE
    CLOSE ENTRADA
    CLOSE SORTIDA
    STOP RUN
    .
