Sindbad~EG File Manager

Current Path : /usr/home/beeson/ThreeBody/ThreeBodyProblem/
Upload File :
Current File : /usr/home/beeson/ThreeBody/ThreeBodyProblem/leapfrog.c

#include <math.h>
#include <stdlib.h>
#include "leapfrog.h"  // declares leapfrog_step
#include "ode3.h"

// Do one Leapfrog step, similar in interface to rkqc
int leapfrog_step(double *y, double *dydx, int n, double *x, double htry, double eps,
                  double *yscal, double *hdid, double *hnext,
                  double *xptr, double xtol, double ytol, double *mass) {
    int i, err = 0;
    double dt = htry;  // use htry as fixed step size

    double *ytemp = (double *)malloc((n + 1) * sizeof(double));
    double *dytmp = (double *)malloc((n + 1) * sizeof(double));
    if (!ytemp || !dytmp) return 1;

    // Step 1: position update using current velocity and acceleration
    for (i = 1; i <= n / 2; i++) {
        ytemp[i] = y[i] + dt * y[i + n / 2] + 0.5 * dt * dt * dydx[i + n / 2];
    }

    // Step 2: compute new accelerations at t + dt
    double tnew = *x + dt;
    err = derivs(n / 4, tnew, ytemp, dytmp, xptr, mass);
    if (err) {
        free(ytemp); free(dytmp);
        return 1;
    }

    // Step 3: velocity update using average of old and new accelerations
    for (i = 1; i <= n / 2; i++) {
        ytemp[i + n / 2] = y[i + n / 2] + 0.5 * dt * (dydx[i + n / 2] + dytmp[i + n / 2]);
    }

    // Copy results back to y
    for (i = 1; i <= n; i++) {
        y[i] = ytemp[i];
    }

    *x += dt;
    *hdid = dt;
    *hnext = dt;  // No adaptivity yet

    free(ytemp); free(dytmp);
    return 0;
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists