Merge pull request #2352 from JoePfeiffer/fix-acceleration-status

Bug fixes in RK4SimulationStepper and RK4SimulationStatus
This commit is contained in:
Joe Pfeiffer 2023-12-20 06:03:34 -07:00 committed by GitHub
commit 1ab0f4344b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 40 deletions

View File

@ -8,7 +8,6 @@ public class RK4SimulationStatus extends SimulationStatus implements Cloneable {
private Coordinate launchRodDirection; private Coordinate launchRodDirection;
private double previousAcceleration = 0; private double previousAcceleration = 0;
private AtmosphericConditions previousAtmosphericConditions;
// Used for determining when to store aerodynamic computation warnings: // Used for determining when to store aerodynamic computation warnings:
private double maxZVelocity = 0; private double maxZVelocity = 0;
@ -26,7 +25,6 @@ public class RK4SimulationStatus extends SimulationStatus implements Cloneable {
this.previousAcceleration = ((RK4SimulationStatus) other).previousAcceleration; this.previousAcceleration = ((RK4SimulationStatus) other).previousAcceleration;
this.maxZVelocity = ((RK4SimulationStatus) other).maxZVelocity; this.maxZVelocity = ((RK4SimulationStatus) other).maxZVelocity;
this.startWarningTime = ((RK4SimulationStatus) other).startWarningTime; this.startWarningTime = ((RK4SimulationStatus) other).startWarningTime;
this.previousAtmosphericConditions = ((RK4SimulationStatus) other).previousAtmosphericConditions;
} }
} }
public void setLaunchRodDirection(Coordinate launchRodDirection) { public void setLaunchRodDirection(Coordinate launchRodDirection) {
@ -49,18 +47,6 @@ public class RK4SimulationStatus extends SimulationStatus implements Cloneable {
this.previousAcceleration = previousAcceleration; this.previousAcceleration = previousAcceleration;
} }
public AtmosphericConditions getPreviousAtmosphericConditions() {
return previousAtmosphericConditions;
}
public void setPreviousAtmosphericConditions(
AtmosphericConditions previousAtmosphericConditions) {
this.previousAtmosphericConditions = previousAtmosphericConditions;
}
public double getMaxZVelocity() { public double getMaxZVelocity() {
return maxZVelocity; return maxZVelocity;
} }

View File

@ -59,9 +59,7 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
private static final double MAX_PITCH_CHANGE = 4 * Math.PI / 180; private static final double MAX_PITCH_CHANGE = 4 * Math.PI / 180;
private Random random; private Random random;
DataStore store = new DataStore();
@Override @Override
public RK4SimulationStatus initialize(SimulationStatus original) { public RK4SimulationStatus initialize(SimulationStatus original) {
@ -90,27 +88,31 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
public void step(SimulationStatus simulationStatus, double maxTimeStep) throws SimulationException { public void step(SimulationStatus simulationStatus, double maxTimeStep) throws SimulationException {
RK4SimulationStatus status = (RK4SimulationStatus) simulationStatus; RK4SimulationStatus status = (RK4SimulationStatus) simulationStatus;
DataStore store = new DataStore();
//////// Perform RK4 integration: //////// //////// Perform RK4 integration: ////////
RK4SimulationStatus status2; RK4SimulationStatus status2;
RK4Parameters k1, k2, k3, k4; RK4Parameters k1, k2, k3, k4;
/* /*
* Start with previous time step which is used to compute the initial thrust estimate. * Start with previous time step which is used to compute the initial thrust estimate.
* Don't make it longer than maxTimeStep, but at least MIN_TIME_STEP. * Don't make it longer than maxTimeStep, but at least MIN_TIME_STEP.
*/ */
store.timestep = status.getPreviousTimeStep();
store.timestep = MathUtil.max(MathUtil.min(store.timestep, maxTimeStep), MIN_TIME_STEP); store.timestep = MathUtil.max(MathUtil.min(store.timestep, maxTimeStep), MIN_TIME_STEP);
checkNaN(store.timestep); checkNaN(store.timestep);
/*
* Get the current atmospheric conditions
*/
calculateFlightConditions(status, store);
store.atmosphericConditions = store.flightConditions.getAtmosphericConditions();
/* /*
* Compute the initial thrust estimate. This is used for the first time step computation. * Compute the initial thrust estimate. This is used for the first time step computation.
*/ */
store.thrustForce = calculateAverageThrust(status, store.timestep, status.getPreviousAcceleration(), store.thrustForce = calculateAverageThrust(status, store.timestep, store.longitudinalAcceleration,
status.getPreviousAtmosphericConditions(), false); store.atmosphericConditions, false);
/* /*
* Perform RK4 integration. Decide the time step length after the first step. * Perform RK4 integration. Decide the time step length after the first step.
@ -150,7 +152,7 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
dt[0] /= 5.0; dt[0] /= 5.0;
dt[6] = status.getSimulationConditions().getLaunchRodLength() / k1.v.length() / 10; dt[6] = status.getSimulationConditions().getLaunchRodLength() / k1.v.length() / 10;
} }
dt[7] = 1.5 * status.getPreviousTimeStep(); dt[7] = 1.5 * store.timestep;
store.timestep = Double.MAX_VALUE; store.timestep = Double.MAX_VALUE;
int limitingValue = -1; int limitingValue = -1;
@ -261,8 +263,6 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
} }
status.setSimulationTime(status.getSimulationTime() + store.timestep); status.setSimulationTime(status.getSimulationTime() + store.timestep);
status.setPreviousTimeStep(store.timestep);
// Store data // Store data
// TODO: MEDIUM: Store acceleration etc of entire RK4 step, store should be cloned or something... // TODO: MEDIUM: Store acceleration etc of entire RK4 step, store should be cloned or something...
storeData(status, store); storeData(status, store);
@ -283,11 +283,17 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
throws SimulationException { throws SimulationException {
RK4Parameters params = new RK4Parameters(); RK4Parameters params = new RK4Parameters();
// if (dataStore == null) { // Call pre-listeners
// dataStore = new DataStore(); store.accelerationData = SimulationListenerHelper.firePreAccelerationCalculation(status);
// }
// Calculate acceleration (if not overridden by pre-listeners)
if (store.accelerationData == null) {
store.accelerationData = calculateAcceleration(status, dataStore);
}
// Call post-listeners
store.accelerationData = SimulationListenerHelper.firePostAccelerationCalculation(status, store.accelerationData);
calculateAcceleration(status, dataStore);
params.a = dataStore.linearAcceleration; params.a = dataStore.linearAcceleration;
params.ra = dataStore.angularAcceleration; params.ra = dataStore.angularAcceleration;
params.v = status.getRocketVelocity(); params.v = status.getRocketVelocity();
@ -312,13 +318,7 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
* @param status the status of the rocket. * @param status the status of the rocket.
* @throws SimulationException * @throws SimulationException
*/ */
private void calculateAcceleration(RK4SimulationStatus status, DataStore store) throws SimulationException { private AccelerationData calculateAcceleration(RK4SimulationStatus status, DataStore store) throws SimulationException {
// Call pre-listeners
store.accelerationData = SimulationListenerHelper.firePreAccelerationCalculation(status);
if (store.accelerationData != null) {
return;
}
// Compute the forces affecting the rocket // Compute the forces affecting the rocket
calculateForces(status, store); calculateForces(status, store);
@ -404,8 +404,7 @@ public class RK4SimulationStepper extends AbstractSimulationStepper {
} }
// Call post-listeners return new AccelerationData(null, null, store.linearAcceleration, store.angularAcceleration, status.getRocketOrientationQuaternion());
store.accelerationData = SimulationListenerHelper.firePostAccelerationCalculation(status, store.accelerationData);
} }