physics/source/app.d
2021-03-03 17:17:39 +13:00

63 lines
2.2 KiB
D

import std.conv;
import std.getopt;
import std.stdio;
import std.string;
import quantities.runtime;
import quantities.si;
// Aaceleration limits (in g):
const double lossOfConsciousness = 15.0;
const double internalInjuries = 25.0;
const double fatal = 40.0;
const double puree = 100.0;
const Acceleration g = 9.8 * metre / second / second; // about 35.3 km/h/s
const Mass typicalHuman = 80 * kilogram;
/++
+ Prompt the user for a plausible number from stdin.
+/
double getVelocity() {
string v = "";
while (!isNumeric(v)) {
write("How fast was The Flash going? Enter a speed, as km/h: ");
v = readln().strip();
}
return to!double(v);
}
void main(string[] argv) {
// Fetch a number from a cli argument, or prompt for one from stdin.
double velocity = 0;
auto args = getopt(argv,
"velocity|v", "How fast The Flash was going", &velocity
);
if (args.helpWanted) {
defaultGetoptPrinter("A stupid superhero physics calculator.", args.options);
return;
}
writeln("Hello, welcome to the stupid superhero physics calculator.");
if (!velocity) velocity = getVelocity();
// v = vᵢ + at; vᵢ = 0, ∴ v = at, a = v/t
Speed v = parseSI!Speed(format!"%s km/h"(velocity));
Time t = 0.1 * second;
Acceleration a = v / t;
// F = ma
Force f = typicalHuman * a;
double accel_g = a / g;
// Output some opinionated results.
writefln("The Flash goes from stand-still to %s in a tenth of a second, thus accelerating at %.1f g.", "%.0f km/h".siFormat(v), accel_g);
writefln("This would exert a force of %s on a typical human body.", "%.1f kN".siFormat(f));
if (accel_g > puree) {
writefln("Accelerating humans at over %.0f g requires hosing down afterwards.", puree);
} else if (accel_g > fatal) {
writefln("Accelerating humans at over %.0f g is usually fatal.", fatal);
} else if (accel_g > internalInjuries) {
writefln("Accelerating humans at over %.0f g results in injuries including internal bleeding.", internalInjuries);
} else if (accel_g > lossOfConsciousness) {
writefln("Accelerating humans at over %.0f g results in loss of consciousness.", lossOfConsciousness);
}
}