Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
made it start a graph, need to work on integrator
  • Loading branch information
jic13003 committed Dec 5, 2015
1 parent 9fd3337 commit f276491
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
26 changes: 24 additions & 2 deletions src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java
@@ -1,12 +1,19 @@
package jat.examples.ThreeBodyExample;

import java.util.ArrayList;

import org.apache.commons.math3.ode.FirstOrderDifferentialEquations;
import org.apache.commons.math3.ode.sampling.StepHandler;
import org.apache.commons.math3.ode.sampling.StepInterpolator;

import jat.coreNOSA.cm.ThreeBody;
import jat.coreNOSA.math.MatrixVector.data.VectorN;

public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEquations{

public ArrayList<Double> time = new ArrayList<Double>();
public ArrayList<Double> xsol = new ArrayList<Double>();
public ArrayList<Double> ysol = new ArrayList<Double>();
public ArrayList<Double> zsol = new ArrayList<Double>();
public ThreeBodyAPL(double G, double m1, double m2, double m3) {
super(G, m1, m2, m3);
}
Expand All @@ -20,7 +27,7 @@ public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEqu
@Override
public int getDimension() {
// returns a dimension of 6
return 6;
return 18;
}
public double[] randv() {
double[] randv = new double[18];
Expand All @@ -44,4 +51,19 @@ public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEqu
randv[17] = 8.0;
return randv;
}

public StepHandler stepHandler = new StepHandler() {
public void init(double t0, double[] y0, double t) {
}

public void handleStep(StepInterpolator interpolator, boolean isLast) {
double t = interpolator.getCurrentTime();
double[] y = interpolator.getInterpolatedState();
// System.out.println(t + " " + y[0] + " " + y[1]+ " " + y[2]);
time.add(t);
xsol.add(y[0]);
ysol.add(y[1]);
zsol.add(y[2]);
}
};
}
71 changes: 57 additions & 14 deletions src/jat/examples/ThreeBodyExample/ThreeBodyExample.java
Expand Up @@ -20,23 +20,24 @@ import org.apache.commons.math3.ode.FirstOrderIntegrator;
import org.apache.commons.math3.ode.nonstiff.DormandPrince853Integrator;

public class ThreeBodyExample {

public ThreeBodyExample() {
}

public static void main(String[] args) {
// Class that solves a three body problem and plots it
ThreeBodyExample x = new ThreeBodyExample();

//initialize variables
double totalEnergy = 0;
// set the final time = one orbit period
double tf = 2.0;
double tf = 7.0;

// create a ThreeBody orbit using three masses and gravitational con
ThreeBodyAPL sat = new ThreeBodyAPL(10, 0.3, 1.0, 2.0);
//initialize VectorN elements
double[] y = sat.randv();

ArrayRealVector v = new ArrayRealVector(y);

DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00");
Expand All @@ -45,27 +46,69 @@ public class ThreeBodyExample {

// find out the derivs of the problem
double[] derivs = sat.derivs(tf, y);

// set the initial time to zero
double t0 = 0.0;
// obtain center of mass
VectorN cm = sat.center_of_mass(y);

// obtain current energy
totalEnergy = sat.Energy(y);

// propagate the orbit
//FirstOrderIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
//dp853.addStepHandler(sat.stepHandler);
// double[] y = new double[] { 7000.0, 0, 0, .0, 8, 0 }; // initial
// state
//test outputs
FirstOrderIntegrator dp853 = new DormandPrince853Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
dp853.addStepHandler(sat.stepHandler);
// double[] y = new double[] { 7000.0, 0, 0, .0, 8, 0 }; // initial
// state

dp853.integrate(sat, 0.0, y, 8000, y); // now y contains final state at
// tf

Double[] objArray = sat.time.toArray(new Double[sat.time.size()]);
double[] timeArray = ArrayUtils.toPrimitive(objArray);
double[] xsolArray = ArrayUtils.toPrimitive(sat.xsol.toArray(new Double[sat.time.size()]));
double[] ysolArray = ArrayUtils.toPrimitive(sat.ysol.toArray(new Double[sat.time.size()]));

double[][] XY = new double[timeArray.length][2];

// int a=0;
// System.arraycopy(timeArray,0,XY[a],0,timeArray.length);
// System.arraycopy(ysolArray,0,XY[1],0,ysolArray.length);

for (int i = 0; i < timeArray.length; i++) {
XY[i][0] = xsolArray[i];
XY[i][1] = ysolArray[i];
}

Plot2DPanel p = new Plot2DPanel();

// Plot2DPanel p = new Plot2DPanel(min, max, axesScales, axesLabels);

ScatterPlot s = new ScatterPlot("orbit", Color.RED, XY);
// LinePlot l = new LinePlot("sin", Color.RED, XY);
// l.closed_curve = false;
// l.draw_dot = true;
p.addPlot(s);
p.setLegendOrientation(PlotPanel.SOUTH);
double plotSize = 10000.;
double[] min = { -plotSize, -plotSize };
double[] max = { plotSize, plotSize };

p.setFixedBounds(min, max);
// test outputs
// output derivatives
for (int i = 0; i< derivs.length; i++)
{
System.out.println(derivs[i]);
}

// output center of mass
System.out.println(cm);

// output total energy of the system
System.out.println(totalEnergy);
}
new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

System.out.println("end");
}
}

0 comments on commit f276491

Please sign in to comment.