Initial commit

This commit is contained in:
Jonathan Harker 2021-03-02 17:26:31 +13:00
commit 87035de28c
4 changed files with 93 additions and 0 deletions

15
.gitignore vendored Normal file
View file

@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/physics
physics.so
physics.dylib
physics.dll
physics.a
physics.lib
physics-test-*
*.exe
*.o
*.obj
*.lst

13
dub.json Normal file
View file

@ -0,0 +1,13 @@
{
"authors": [
"Jonathan Harker"
],
"copyright": "Copyright © 2021, Jonathan Harker",
"dependencies": {
"quantities": "~>0.11.0",
"simpleconfig": "~>0.3.0"
},
"description": "A tiny physics calculator",
"license": "AGPL3",
"name": "physics"
}

7
dub.selections.json Normal file
View file

@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"quantities": "0.11.0",
"simpleconfig": "0.3.0"
}
}

58
source/app.d Normal file
View file

@ -0,0 +1,58 @@
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;
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) {
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;
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);
}
}