Skip to content

Commit

Permalink
Merge pull request #61 from group22DKE/solver_adapted_function
Browse files Browse the repository at this point in the history
Solver exponential testing
  • Loading branch information
trav-d13 authored Jun 8, 2021
2 parents 18aa2d7 + f254a8b commit 67c17b1
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
19 changes: 19 additions & 0 deletions app/src/main/java/src/peng/ExponentialFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package src.peng;

import java.lang.Math;
import java.util.ArrayList;

public class ExponentialFunction implements ODEFunctionInterface
{
public RateInterface call(double t, StateInterface y)
{
ArrayList<Vector3d> cv = new ArrayList<Vector3d>();
Vector3d derivative = new Vector3d(0, Math.exp(t),0);
cv.add(derivative);

ArrayList<Vector3d> cp = new ArrayList<Vector3d>();
cp.add(new Vector3d());

return new Rate(cv, cp);
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/src/solv/Verlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public State step(ODEFunctionInterface function, double t, State currentState, d

ArrayList<Vector3d> nextVel = nextVelocity(nextChange, currentState, change, step);

return new State(nextVel, nextPos);
return new State(nextVel, nextPos, t+step);
}

public StateInterface step(ODEFunctionInterface function, double t, StateInterface cState, double step)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/src/traj/OrbitController.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public double orbitalHillClimbing(Universe universe, int target, SimulationSetti
{
currentError = routeEvaluation(currentVelocity, settings, universe, target, orbitalHeight);

System.out.println("\nCurrent velocity: "+ currentVelocity);
System.out.println("Current velocity: "+ currentVelocity);
System.out.println("Lower limit error: "+ lowerLimitError);
System.out.println("Current Error: "+ currentError);

Expand Down
100 changes: 100 additions & 0 deletions app/src/test/java/src/test/TestSolverExponential.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package src.test;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import src.peng.ExponentialFunction;
import src.peng.ODEFunctionInterface;
import src.peng.State;
import src.peng.Vector3d;
import src.solv.ODESolver;
import src.solv.Verlet;

import java.util.ArrayList;



public class TestSolverExponential
{
double epsilon = 0.4;
double step = 0.1;
private static final boolean DEBUG = false;

/*Test utilizes the y-value within the velocity field of a state. To represent the exponential function.
*The position field can be ignored as within the solver this field is manipulated with by the velocity field
*in order to determine the next position.
*/
@Test
void testSolverExponentialSingleStep()
{
/*Solver and function setup*/
ODEFunctionInterface exponentialFunction = new ExponentialFunction();
ODESolver solver = new Verlet();

State currentState = createStartState();

/*Solver test with 0.2 sec time step*/
double time = 0;
State nextState = solver.step(exponentialFunction, time, currentState, step);
if(DEBUG)
{
System.out.println("Start State" + currentState.toString());
System.out.println("Next State" + nextState.toString());
System.out.println("Actual value: "+ Math.exp(time + step));
}

/*Test: Exponential function at time 0.1*/
assertTrue(withinRange(nextState.velocity.get(0).getY(), Math.exp(time + step)));
}

/*Testing multiple steps*/
@Test
void testMultipleSteps()
{
/*Solver and function setup*/
ODEFunctionInterface exponentialFunction = new ExponentialFunction();
ODESolver solver = new Verlet();

State currentState = createStartState();

double time = 0;
while(time < 2)
{
State nextState = solver.step(exponentialFunction, time, currentState, step);
if(DEBUG)
{
System.out.println("Start State" + currentState.toString());
System.out.println("Next State" + nextState.toString());
System.out.println("Actual value: "+ Math.exp(time + step));
}

assertTrue(withinRange(nextState.velocity.get(0).getY(), Math.exp(time + step)));

currentState = nextState;
time += step;
}
}

private boolean withinRange(double estimatedValue, double actualValue)
{
if(Math.abs(estimatedValue - actualValue) < epsilon)
{
return true;
}
return false;
}

//Generate an initial state at time 0.
//This included velocity field y-value holding value of e^(0)
private State createStartState()
{
ArrayList<Vector3d> velocity = new ArrayList<Vector3d>(); //Current state velocity
Vector3d startValue = new Vector3d(0, Math.exp(0),0);
velocity.add(startValue);

ArrayList<Vector3d> position = new ArrayList<Vector3d>(); //Current state position
position.add(new Vector3d());

return new State(velocity, position, 0); //Generate state at time 0.
}

}
7 changes: 2 additions & 5 deletions app/src/test/java/src/test/TestSolvers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
import log.TestLogger;
import src.conf.SettingsFileManager;
import src.conf.SimulationSettings;
import src.solv.*;
import src.univ.CelestialBody;
import src.univ.Universe;
import src.peng.NewtonGravityFunction;
import src.peng.ODEFunctionInterface;
import src.peng.State;
import src.peng.StateInterface;
import src.peng.ExponentialFunction;
import src.peng.Vector3d;
import src.solv.EulerSolver;
import src.solv.RungeKutta2nd;
import src.solv.RungeKutta3rd;
import src.solv.RungeKutta4th;
import src.solv.Verlet;

/* --------------------------------------------------------------------------------------------------*/
/** Testing Strategy: Each solver will be run for a year at the following step sizes:
Expand Down

0 comments on commit 67c17b1

Please sign in to comment.