Make fuel editors take fuel in ctor
[sw/motorsim] / src / com / billkuker / rocketry / motorsim / fuel / SaintRobertFuel.java
1 package com.billkuker.rocketry.motorsim.fuel;\r
2 \r
3 import java.net.URI;\r
4 import java.net.URISyntaxException;\r
5 \r
6 import javax.measure.quantity.Pressure;\r
7 import javax.measure.quantity.Velocity;\r
8 import javax.measure.quantity.VolumetricDensity;\r
9 import javax.measure.unit.NonSI;\r
10 import javax.measure.unit.Unit;\r
11 \r
12 import org.jscience.physics.amount.Amount;\r
13 \r
14 import com.billkuker.rocketry.motorsim.Fuel;\r
15 import com.billkuker.rocketry.motorsim.RocketScience;\r
16 \r
17 public abstract class SaintRobertFuel implements Fuel {\r
18         \r
19         public enum Type{\r
20                 SI(\r
21                                 javax.measure.unit.SI.MILLIMETER.divide(javax.measure.unit.SI.SECOND).asType(Velocity.class),\r
22                                 javax.measure.unit.SI.MEGA(javax.measure.unit.SI.PASCAL).asType(Pressure.class)),\r
23                 NONSI(\r
24                                 NonSI.INCH.divide(javax.measure.unit.SI.SECOND).asType(Velocity.class),\r
25                                 RocketScience.PSI)\r
26                 ;\r
27                 \r
28                 private final Unit<Velocity> v;\r
29                 private final Unit<Pressure> p;\r
30                 \r
31                 Type( Unit<Velocity> v, Unit<Pressure> p){\r
32                         this.p = p;\r
33                         this.v = v;\r
34                 }\r
35         }\r
36         \r
37         private Type t = Type.SI;\r
38         \r
39         public SaintRobertFuel(Type t){\r
40                 if ( t == null )\r
41                         throw new IllegalArgumentException("Type must be non-null");\r
42                 this.t = t;\r
43         }\r
44         \r
45         public Type getType(){\r
46                 return t;\r
47         }\r
48         \r
49         protected void setType(final Type t){\r
50                 this.t = t;\r
51         }\r
52 \r
53         public Amount<Velocity> burnRate(Amount<Pressure> pressure) {\r
54 \r
55                 \r
56                 double p = pressure.doubleValue(getType().p);\r
57                 double a = burnrateCoefficient(pressure);\r
58                 double n = burnrateExponent(pressure);\r
59                 \r
60                 return burnrateConstant().plus(Amount.valueOf( a*Math.pow(p,n), getType().v ));\r
61         }\r
62         \r
63         protected abstract double burnrateCoefficient(Amount<Pressure> pressure);\r
64         \r
65         protected abstract double burnrateExponent(Amount<Pressure> pressure);\r
66         \r
67         protected Amount<Velocity> burnrateConstant(){\r
68                 return Amount.valueOf(0, Velocity.UNIT);\r
69         }\r
70 \r
71         public abstract Amount<VolumetricDensity> getIdealDensity();\r
72 \r
73         public URI getURI(){\r
74                 try {\r
75                         return new URI("motorsim:" + this.getClass().getSimpleName());\r
76                 } catch (URISyntaxException e) {\r
77                         throw new Error("Shouldn't happen", e);\r
78                 }\r
79         }\r
80         \r
81         public String getName(){\r
82                 return this.getClass().getSimpleName();\r
83         }\r
84         \r
85         public String toString(){\r
86                 return getName();\r
87         }\r
88 }\r