Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/autonoma.ca.git

Make working and non-working code closer

Author djarvis <email>
Date 2024-12-03 19:49:53 GMT-0800
Commit 7e3fb30b9b13a83157eff19a2c337a1a19ae70f4
Parent 819f478
calculators/rocket/payload/Earth.js
}
- soundSpeed(altitude) {
- const temperature = this.atmosphere.temperature(altitude);
- return Earth.SPEED_SOUND_0C * Math.sqrt((1 + temperature) / Earth.KELVIN);
- }
+ rotationalSpeed(latitude, altitude) {
+ const radialLatitude = this.radius(latitude);
+ const distance = radialLatitude + altitude;
- rotationalSpeed(latitude) {
- return this.radius(latitude) * (2 * Math.PI) / Earth.SIDEREAL_TIME;
+ return (2 * Math.PI * distance) / Earth.SIDEREAL_TIME;
}
orbitalSpeed(latitude, altitude) {
const distance = this.radius(latitude) + altitude;
+
return Math.sqrt(Earth.STANDARD_GRAVITY / distance);
+ }
+
+ soundSpeed(altitude) {
+ const temperature = this.atmosphere.temperature(altitude);
+
+ return Earth.SPEED_SOUND_0C * Math.sqrt((1 + temperature) / Earth.KELVIN);
}
calculators/rocket/payload/Rocket.js
-function LaunchResult(orbital, horizontal, target, deltaV, deltaVRocket, rocketDrag, mfr, hVelocity, vVelocity) {
- this.orbital = orbital;
- this.horizontal = horizontal;
- this.target = target;
- this.deltaV = deltaV;
- this.deltaVRocket = deltaVRocket;
- this.rocketDrag = rocketDrag;
- this.mfr = mfr;
- this.hVelocity = hVelocity;
- this.vVelocity = vVelocity;
-}
-
-function DragLog(rho, hVelocityNet, vVelocity, totalSpeed, ballisticCoefficient, magnitude, forceRatio) {
- this.rho = rho;
- this.hVelocityNet = hVelocityNet;
- this.vVelocity = vVelocity;
- this.totalSpeed = totalSpeed;
- this.ballisticCoefficient = ballisticCoefficient;
- this.magnitude = magnitude;
- this.forceRatio = forceRatio;
-}
-
-function FlightLog(time, thrust, altitude, hVelocity, vVelocity, dragForce, hAcceleration, vAcceleration, mass, massFlowRate) {
- this.time = time;
- this.thrust = thrust;
- this.altitude = altitude;
- this.hVelocity = hVelocity;
- this.vVelocity = vVelocity;
- this.dragForce = dragForce;
- this.hAcceleration = hAcceleration;
- this.vAcceleration = vAcceleration;
- this.mass = mass;
- this.massFlowRate = massFlowRate;
-}
-
-function RecordLog(time, altitude, hVelocity, percentSpeed, mass, magnitude) {
- this.time = time;
- this.altitude = altitude;
- this.hVelocity = hVelocity;
- this.percentSpeed = percentSpeed;
- this.mass = mass;
- this.magnitude = magnitude;
-}
-
class Rocket {
constructor(wet, payload, diameter, dragCoefficient, specificImpulse) {
console.assert(wet >= 1);
console.assert(payload >= 1);
console.assert(diameter > 0);
console.assert(dragCoefficient > 0);
console.assert(specificImpulse > 1);
- this.wet = wet;
- this.dry = wet * 0.1;
- this.targetMass = this.dry + payload;
+ this.mass = wet;
+ this.targetMass = wet + payload;
this.radius = diameter / 2.0;
this.specificImpulse = specificImpulse;
this.totalThrust = 0.0;
this.targetAltitude = 0.0;
this.massFlowRate = 0.0;
+ this.vAcceleration = 0.0;
+ this.hAcceleration = 0.0;
const crossSectionArea = Math.PI * this.radius ** 2;
this.ballisticCoefficient = dragCoefficient * crossSectionArea;
this.flightRecorder = false;
this.launched = false;
this.relocated = false;
this.world = false;
-
- this.launchResults = [];
- this.flightLogs = [];
- this.recordLogs = [];
- this.dragLogs = [];
}
const LOCAL_GRAVITY = this.planet.gravity(latitude, altitude);
+ this.vExhaust = this.specificImpulse * LOCAL_GRAVITY;
this.maxAcceleration = 5.0 * LOCAL_GRAVITY;
this.rotationalSpeed = this.planet.rotationalSpeed(latitude, altitude);
- this.vExhaust = this.specificImpulse * LOCAL_GRAVITY;
this.relocated = true;
}
launch(hVelocity, vVelocity) {
+ console.assert(this.world);
+ console.assert(this.relocated);
console.assert(!this.launched);
this.launched = true;
this.hVelocity = hVelocity + this.rotationalSpeed;
- console.log("ROTATIONAL SPEED: " + this.rotationalSpeed);
this.vVelocity = vVelocity;
const dragForce = this.drag();
- const absDragForce = Math.abs(dragForce[1]);
-
- this.massFlowRate = absDragForce / this.vExhaust * 1.001;
-
- const vOrb = this.planet.orbitalSpeed(this.latitude, this.altitude);
- const vReq = vOrb - this.rotationalSpeed;
- const vRocket = this.vExhaust * Math.log(this.wet / this.targetMass);
- const result = new LaunchResult(vOrb, this.hVelocity, this.targetMass, vReq, vRocket, dragForce, this.massFlowRate, this.hVelocity, this.vVelocity);
- this.launchResults.push(result);
+ this.massFlowRate = Math.abs(dragForce[1]) / this.vExhaust * 1.001;
+ this.totalThrust = this.vExhaust * this.massFlowRate;
}
apogee(altitude) {
- this.targetVelocity = this.planet.orbitalSpeed(this.latitude, altitude);
+ this.hTargetVelocity = this.planet.orbitalSpeed(this.latitude, altitude);
this.targetAltitude = altitude;
}
const magnitude = 0.5 * rho * this.ballisticCoefficient * totalSpeed ** 2;
const forceRatio = [-hVelocityNet / totalSpeed, -this.vVelocity / totalSpeed];
-
- const log = new DragLog(rho, hVelocityNet, this.vVelocity, totalSpeed, this.ballisticCoefficient, magnitude, forceRatio);
- this.dragLogs.push(log);
return [forceRatio[0] * magnitude, forceRatio[1] * magnitude];
}
flying() {
- console.assert(this.wet > 0);
+ console.assert(this.mass > 0);
console.assert(this.targetMass > 0);
console.assert(this.targetAltitude > 0);
return (
- this.wet > this.targetMass &&
- this.wet > this.dry &&
+ this.mass > this.targetMass &&
this.altitude < this.targetAltitude
);
}
- fly(time) {
+ fly(step) {
console.assert(this.launched);
- console.assert(time > 0);
+ console.assert(step > 0);
const dragForce = this.drag();
- const absDragForce = Math.abs(dragForce[1]);
-
- this.totalThrust = this.vExhaust * this.massFlowRate;
-
- const thrusting = this.totalThrust > 0;
- const vThrust = thrusting ? -dragForce[1] : 0.0;
- const hThrust = thrusting ? Math.sqrt(this.totalThrust ** 2 - vThrust ** 2) : 0.0;
-
- console.assert(this.wet > 0);
-
- this.hAcceleration = dragForce[0] / this.wet + hThrust / this.wet;
- this.vAcceleration = dragForce[1] / this.wet - this.planet.gravity(this.latitude, this.altitude) + vThrust / this.wet;
-
- // Records the initial values and all subsequent deltas.
- this.record(time);
-
- this.hVelocity += this.hAcceleration * time;
- this.vVelocity += this.vAcceleration * time;
-
- this.azimuth += this.hVelocity * time + 0.5 * this.hAcceleration * time ** 2;
- this.altitude += this.vVelocity * time + 0.5 * this.vAcceleration * time ** 2;
-
- this.wet -= this.massFlowRate * time;
- if (this.hVelocity < this.targetVelocity) {
+ if (this.hVelocity < this.hTargetVelocity) {
const magnitude = Math.sqrt(
this.hAcceleration ** 2 + this.vAcceleration ** 2
- ) / 9.8;
-
- console.log(magnitude);
+ );
+ const absDragForce = Math.abs(dragForce[1]);
if (magnitude > this.maxAcceleration &&
this.vExhaust * this.massFlowRate * 0.98 > absDragForce) {
this.massFlowRate *= 0.99;
- console.log('Limiting Mfr :', this.massFlowRate);
}
if (this.vExhaust * this.massFlowRate < absDragForce) {
this.massFlowRate = absDragForce / this.vExhaust * 1.1;
}
} else {
this.massFlowRate = 0.0;
}
- const log = new FlightLog(time, this.totalThrust, this.altitude, this.hVelocity, this.vVelocity, dragForce, this.hAcceleration, this.vAcceleration, this.wet, this.massFlowRate);
- this.flightLogs.push(log);
- }
+ this.totalThrust = this.vExhaust * this.massFlowRate;
- stop() {
- console.table(this.launchResults);
- console.table(this.dragLogs);
- console.table(this.flightLogs);
- console.table(this.recordLogs);
+ const thrusting = this.totalThrust > 0;
+ const vThrust = thrusting ? -dragForce[1] : 0.0;
+ const hThrust = thrusting ? Math.sqrt(this.totalThrust ** 2 - vThrust ** 2) : 0.0;
+
+ this.hAcceleration = dragForce[0] / this.mass + hThrust / this.mass;
+ this.vAcceleration = dragForce[1] / this.mass - this.planet.gravity(this.latitude, this.altitude) + vThrust / this.mass;
+
+ this.hVelocity += this.hAcceleration * step;
+ this.vVelocity += this.vAcceleration * step;
+
+ this.azimuth += this.hVelocity * step + 0.5 * this.hAcceleration * step ** 2;
+ this.altitude += this.vVelocity * step + 0.5 * this.vAcceleration * step ** 2;
+
+ this.mass -= this.massFlowRate * step;
+
}
+
+ stop() {}
record(time) {
this.blackBox.verticalVelocity(time, this.vVelocity);
this.blackBox.verticalAcceleration(time, this.vAcceleration);
- this.blackBox.thrustAcceleration(time, this.totalThrust / this.wet);
- this.blackBox.mass(time, this.wet);
-
- const magnitude = Math.sqrt(this.hAcceleration ** 2 + this.vAcceleration ** 2) / 9.8;
- const percentSpeed = this.hVelocity / this.targetVelocity * 100.0;
-
- const recordLog = new RecordLog(time, this.altitude, this.hVelocity, percentSpeed, this.wet, magnitude);
- this.recordLogs.push(recordLog);
+ this.blackBox.thrustAcceleration(time, this.totalThrust / this.mass);
+ this.blackBox.mass(time, this.mass);
}
}
calculators/rocket/payload/Telemetry.js
const maxYValue = d3.max(data, d => d.value);
+
const numDigits = maxYValue.toFixed(0).length;
- const margin = { top: 30, right: 30, bottom: 50, left: 40 + (numDigits * 10) },
+ const margin = {
+ top: 30,
+ right: 30,
+ bottom: 50,
+ left: 40 + (numDigits * 10)
+ },
width = 800 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
.y(d => y(d.value))
);
-
- const fontStyle = {
- 'font-family': 'sans-serif',
- 'font-size': '16px',
- 'fill': '#333'
- };
svg.append('text')
calculators/rocket/payload/calculator.js
rocket.launch(INITIAL_HORIZONTAL_VELOCITY, INITIAL_VERTICAL_VELOCITY);
+ const DELTA_TIME = 0.01;
+
let time = 0.0;
- while (rocket.flying()) {
- time = parseFloat((time + 0.01).toFixed(2));
- rocket.fly(time);
+ do {
+ rocket.record(time);
+ rocket.fly(DELTA_TIME);
+ time = parseFloat((time + DELTA_TIME).toFixed(2));
}
+ while (rocket.flying());
rocket.stop();
Delta 62 lines added, 132 lines removed, 70-line decrease