0af17121516c476316eb3598816bb352558ce079
[debian/openrocket] / src / net / sf / openrocket / simulation / BasicLandingStepper.java
1 package net.sf.openrocket.simulation;
2
3 import net.sf.openrocket.models.atmosphere.AtmosphericConditions;
4 import net.sf.openrocket.rocketcomponent.RecoveryDevice;
5 import net.sf.openrocket.simulation.exception.SimulationException;
6 import net.sf.openrocket.util.Coordinate;
7 import net.sf.openrocket.util.MathUtil;
8
9 public class BasicLandingStepper extends AbstractSimulationStepper {
10         
11         private static final double RECOVERY_TIME_STEP = 0.5;
12         
13         // FIXME:  Add lat/lon code here as well
14         
15         @Override
16         public SimulationStatus initialize(SimulationStatus status) throws SimulationException {
17                 return status;
18         }
19         
20         @Override
21         public void step(SimulationStatus status, double maxTimeStep) throws SimulationException {
22                 double totalCD = 0;
23                 double refArea = status.getConfiguration().getReferenceArea();
24                 
25                 // Get the atmospheric conditions
26                 AtmosphericConditions atmosphere = modelAtmosphericConditions(status);
27                 
28                 //// Local wind speed and direction
29                 Coordinate windSpeed = modelWindVelocity(status);
30                 Coordinate airSpeed = status.getRocketVelocity().add(windSpeed);
31                 
32                 // Get total CD
33                 double mach = airSpeed.length() / atmosphere.getMachSpeed();
34                 for (RecoveryDevice c : status.getDeployedRecoveryDevices()) {
35                         totalCD += c.getCD(mach) * c.getArea() / refArea;
36                 }
37                 
38                 // Compute drag force
39                 double dynP = (0.5 * atmosphere.getDensity() * airSpeed.length2());
40                 double dragForce = totalCD * dynP * refArea;
41                 MassData massData = calculateMassData(status);
42                 double mass = massData.getCG().weight;
43                 
44
45                 // Compute drag acceleration
46                 Coordinate linearAcceleration;
47                 if (airSpeed.length() > 0.001) {
48                         linearAcceleration = airSpeed.normalize().multiply(-dragForce / mass);
49                 } else {
50                         linearAcceleration = Coordinate.NUL;
51                 }
52                 
53                 // Add effect of gravity
54                 double gravity = modelGravity(status);
55                 linearAcceleration = linearAcceleration.sub(0, 0, gravity);
56                 
57
58                 // Select time step
59                 double timeStep = MathUtil.min(0.5 / linearAcceleration.length(), RECOVERY_TIME_STEP);
60                 
61                 // Perform Euler integration
62                 status.setRocketPosition(status.getRocketPosition().add(status.getRocketVelocity().multiply(timeStep)).
63                                 add(linearAcceleration.multiply(MathUtil.pow2(timeStep) / 2)));
64                 status.setRocketVelocity(status.getRocketVelocity().add(linearAcceleration.multiply(timeStep)));
65                 status.setSimulationTime(status.getSimulationTime() + timeStep);
66                 
67
68                 // Store data
69                 FlightDataBranch data = status.getFlightData();
70                 boolean extra = status.getSimulationConditions().isCalculateExtras();
71                 data.addPoint();
72                 
73                 data.setValue(FlightDataType.TYPE_TIME, status.getSimulationTime());
74                 data.setValue(FlightDataType.TYPE_ALTITUDE, status.getRocketPosition().z);
75                 data.setValue(FlightDataType.TYPE_POSITION_X, status.getRocketPosition().x);
76                 data.setValue(FlightDataType.TYPE_POSITION_Y, status.getRocketPosition().y);
77                 if (extra) {
78                         data.setValue(FlightDataType.TYPE_POSITION_XY,
79                                         MathUtil.hypot(status.getRocketPosition().x, status.getRocketPosition().y));
80                         data.setValue(FlightDataType.TYPE_POSITION_DIRECTION,
81                                         Math.atan2(status.getRocketPosition().y, status.getRocketPosition().x));
82                         
83                         data.setValue(FlightDataType.TYPE_VELOCITY_XY,
84                                         MathUtil.hypot(status.getRocketVelocity().x, status.getRocketVelocity().y));
85                         data.setValue(FlightDataType.TYPE_ACCELERATION_XY,
86                                         MathUtil.hypot(linearAcceleration.x, linearAcceleration.y));
87                         
88                         data.setValue(FlightDataType.TYPE_ACCELERATION_TOTAL, linearAcceleration.length());
89                         
90                         double Re = airSpeed.length() *
91                                         status.getConfiguration().getLength() /
92                                         atmosphere.getKinematicViscosity();
93                         data.setValue(FlightDataType.TYPE_REYNOLDS_NUMBER, Re);
94                 }
95                 
96                 data.setValue(FlightDataType.TYPE_VELOCITY_Z, status.getRocketVelocity().z);
97                 data.setValue(FlightDataType.TYPE_ACCELERATION_Z, linearAcceleration.z);
98                 
99                 data.setValue(FlightDataType.TYPE_VELOCITY_TOTAL, airSpeed.length());
100                 data.setValue(FlightDataType.TYPE_MACH_NUMBER, mach);
101                 
102                 data.setValue(FlightDataType.TYPE_MASS, mass);
103                 
104                 data.setValue(FlightDataType.TYPE_THRUST_FORCE, 0);
105                 data.setValue(FlightDataType.TYPE_DRAG_FORCE, dragForce);
106                 
107                 data.setValue(FlightDataType.TYPE_WIND_VELOCITY, windSpeed.length());
108                 data.setValue(FlightDataType.TYPE_AIR_TEMPERATURE, atmosphere.getTemperature());
109                 data.setValue(FlightDataType.TYPE_AIR_PRESSURE, atmosphere.getPressure());
110                 data.setValue(FlightDataType.TYPE_SPEED_OF_SOUND, atmosphere.getMachSpeed());
111                 
112                 data.setValue(FlightDataType.TYPE_TIME_STEP, timeStep);
113                 data.setValue(FlightDataType.TYPE_COMPUTATION_TIME,
114                                 (System.nanoTime() - status.getSimulationStartWallTime()) / 1000000000.0);
115         }
116         
117 }