From cdf88d4683ad12d02cfccf06d6688fd935c2b32d Mon Sep 17 00:00:00 2001 From: jic13003 Date: Sun, 6 Dec 2015 20:05:49 -0500 Subject: [PATCH 1/3] latest working version --- src/jat/coreNOSA/cm/ThreeBody.java | 6 +++--- src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/jat/coreNOSA/cm/ThreeBody.java b/src/jat/coreNOSA/cm/ThreeBody.java index a9eaabd..ef46b20 100644 --- a/src/jat/coreNOSA/cm/ThreeBody.java +++ b/src/jat/coreNOSA/cm/ThreeBody.java @@ -34,9 +34,9 @@ import jat.coreNOSA.math.MatrixVector.data.VectorN; public class ThreeBody implements Derivatives //, Printable { - - private double G; // Gravitational constant - private double m1, m2, m3; // masses + protected double steps = 500.; + protected double G; // Gravitational constant + protected double m1, m2, m3; // masses /** * Method ThreeBody. diff --git a/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java b/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java index f22a529..f1d5d07 100644 --- a/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java +++ b/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java @@ -17,9 +17,10 @@ public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEqu public ArrayList xsol = new ArrayList(); public ArrayList ysol = new ArrayList(); public ArrayList zsol = new ArrayList(); - + protected double[] randv[]; public ThreeBodyAPL(double G, double m1, double m2, double m3) { super(G, m1, m2, m3); + this.randv(); } @Override @@ -77,5 +78,4 @@ public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEqu zsol.add(y[2]); } }; - } \ No newline at end of file From e889a08a35544c9f88d7ae45db8141c9a38a63de Mon Sep 17 00:00:00 2001 From: jic13003 Date: Sun, 6 Dec 2015 20:46:10 -0500 Subject: [PATCH 2/3] test commit --- src/jat/coreNOSA/cm/ThreeBody.java | 6 +- .../ThreeBodyExample/ThreeBodyAPL.java | 81 +++++++++++++ .../ThreeBodyExample/ThreeBodyExample.java | 114 ++++++++++++++++++ 3 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java create mode 100644 src/jat/examples/ThreeBodyExample/ThreeBodyExample.java diff --git a/src/jat/coreNOSA/cm/ThreeBody.java b/src/jat/coreNOSA/cm/ThreeBody.java index a9eaabd..ef46b20 100644 --- a/src/jat/coreNOSA/cm/ThreeBody.java +++ b/src/jat/coreNOSA/cm/ThreeBody.java @@ -34,9 +34,9 @@ import jat.coreNOSA.math.MatrixVector.data.VectorN; public class ThreeBody implements Derivatives //, Printable { - - private double G; // Gravitational constant - private double m1, m2, m3; // masses + protected double steps = 500.; + protected double G; // Gravitational constant + protected double m1, m2, m3; // masses /** * Method ThreeBody. diff --git a/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java b/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java new file mode 100644 index 0000000..f1d5d07 --- /dev/null +++ b/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java @@ -0,0 +1,81 @@ +package jat.examples.ThreeBodyExample; + +import java.util.ArrayList; + +import org.apache.commons.math3.geometry.euclidean.threed.Vector3D; +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.algorithm.integrators.Printable; +import jat.coreNOSA.cm.Constants; +import jat.coreNOSA.cm.ThreeBody; +import jat.coreNOSA.math.MatrixVector.data.VectorN; + +public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEquations{ + public ArrayList time = new ArrayList(); + public ArrayList xsol = new ArrayList(); + public ArrayList ysol = new ArrayList(); + public ArrayList zsol = new ArrayList(); + protected double[] randv[]; + public ThreeBodyAPL(double G, double m1, double m2, double m3) { + super(G, m1, m2, m3); + this.randv(); + } + + @Override + public void computeDerivatives(double t, double[] y, double[] yDot) { + // returns the derivatives of the ThreeBody problem + yDot = this.derivs(t, y); + } + + @Override + public int getDimension() { + // returns a dimension of 6 + return 18; + } + public double[] randv() { + // returns the position and velocity of the three objects + // vector r1 is x[0], x[1], x[2] + // vector v1 is x[3], x[4], x[5] + // vector r2 is x[6], x[7], x[8] + // vector v2 is x[9], x[10], x[11] + // vector r3 is x[12], x[13], x[14] + // vector v3 is x[15], x[16], x[17] + double[] randv = new double[18]; + randv[0] = 2.0; + randv[1] = 6.0; + randv[2] = 7.0; + randv[3] = 4.0; + randv[4] = 8.0; + randv[5] = 7.0; + randv[6] = 5.0; + randv[7] = 8.0; + randv[8] = 3.0; + randv[9] = 6.0; + randv[10] = 8.0; + randv[11] = 9.0; + randv[12] = 0.0; + randv[13] = 7.0; + randv[14] = 4.0; + randv[15] = 5.0; + randv[16] = 7.0; + 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]); + } + }; +} \ No newline at end of file diff --git a/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java b/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java new file mode 100644 index 0000000..e9243c8 --- /dev/null +++ b/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java @@ -0,0 +1,114 @@ +package jat.examples.ThreeBodyExample; + +import jat.core.cm.TwoBodyAPL; +import jat.core.plot.plot.FrameView; +import jat.core.plot.plot.Plot2DPanel; +import jat.core.plot.plot.PlotPanel; +import jat.core.plot.plot.plots.ScatterPlot; +import jat.coreNOSA.math.MatrixVector.data.VectorN; +import jat.examples.TwoBodyExample.TwoBodyExample; + +import java.awt.Color; +import java.text.DecimalFormat; + +import javax.swing.JFrame; + +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.math3.linear.ArrayRealVector; +import org.apache.commons.math3.linear.RealVectorFormat; +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 = 1.0; + + // create a ThreeBody orbit using three masses and gravitational con + ThreeBodyAPL sat = new ThreeBodyAPL(9.87, 0.3, 1.0, 2.0); + //initialize VectorN elements + double[] y = sat.randv(); + + ArrayRealVector v = new ArrayRealVector(y); + + DecimalFormat df2 = new DecimalFormat("#,###,###,##0.00"); + RealVectorFormat format = new RealVectorFormat(df2); + System.out.println(format.format(v)); + + // 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 + + 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"); + } +} From 8b46a142acf867c53db176d569333186023e39da Mon Sep 17 00:00:00 2001 From: jic13003 Date: Sun, 6 Dec 2015 20:40:09 -0500 Subject: [PATCH 3/3] added comments --- src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java | 5 ++++- src/jat/examples/ThreeBodyExample/ThreeBodyExample.java | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java b/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java index f1d5d07..0cabaaa 100644 --- a/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java +++ b/src/jat/examples/ThreeBodyExample/ThreeBodyAPL.java @@ -18,7 +18,9 @@ public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEqu public ArrayList ysol = new ArrayList(); public ArrayList zsol = new ArrayList(); protected double[] randv[]; + public ThreeBodyAPL(double G, double m1, double m2, double m3) { + //constructor super(G, m1, m2, m3); this.randv(); } @@ -65,13 +67,14 @@ public class ThreeBodyAPL extends ThreeBody implements FirstOrderDifferentialEqu } public StepHandler stepHandler = new StepHandler() { + //step handler for dp853 integrator 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]); + System.out.println(t + " " + y[0] + " " + y[1]+ " " + y[2]); //prints time and xyz positions time.add(t); xsol.add(y[0]); ysol.add(y[1]); diff --git a/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java b/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java index e9243c8..b55c7e9 100644 --- a/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java +++ b/src/jat/examples/ThreeBodyExample/ThreeBodyExample.java @@ -95,6 +95,7 @@ public class ThreeBodyExample { double[] max = { plotSize, plotSize }; p.setFixedBounds(min, max); + // test outputs // output derivatives for (int i = 0; i< derivs.length; i++)