Initial commit
[debian/openrocket] / src / net / sf / openrocket / aerodynamics / AtmosphericModel.java
1 package net.sf.openrocket.aerodynamics;
2
3 public abstract class AtmosphericModel {
4         /** Layer thickness of interpolated altitude. */
5         private static final double DELTA = 500;
6         
7         private AtmosphericConditions[] levels = null;
8         
9         
10         public AtmosphericConditions getConditions(double altitude) {
11                 if (levels == null)
12                         computeLayers();
13                 
14                 if (altitude <= 0)
15                         return levels[0];
16                 if (altitude >= DELTA*(levels.length-1))
17                         return levels[levels.length-1];
18                 
19                 int n = (int)(altitude/DELTA);
20                 double d = (altitude - n*DELTA)/DELTA;
21                 AtmosphericConditions c = new AtmosphericConditions();
22                 c.temperature = levels[n].temperature * (1-d) + levels[n+1].temperature * d;
23                 c.pressure = levels[n].pressure * (1-d) + levels[n+1].pressure * d;
24                         
25                 return c;
26         }
27         
28         
29         private void computeLayers() {
30                 double max = getMaxAltitude();
31                 int n = (int)(max/DELTA) + 1;
32                 levels = new AtmosphericConditions[n];
33                 for (int i=0; i < n; i++) {
34                         levels[i] = getExactConditions(i*DELTA);
35                 }
36         }
37
38         
39         public abstract double getMaxAltitude();
40         public abstract AtmosphericConditions getExactConditions(double altitude);
41 }