From 87035de28cafaa3e278649b2b41e326eb17cb725 Mon Sep 17 00:00:00 2001 From: Jonathan Harker Date: Tue, 2 Mar 2021 17:26:31 +1300 Subject: [PATCH] Initial commit --- .gitignore | 15 ++++++++++++ dub.json | 13 ++++++++++ dub.selections.json | 7 ++++++ source/app.d | 58 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 dub.json create mode 100644 dub.selections.json create mode 100644 source/app.d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..82ceb8e --- /dev/null +++ b/.gitignore @@ -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 diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..6ce066f --- /dev/null +++ b/dub.json @@ -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" +} \ No newline at end of file diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..af3541b --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,7 @@ +{ + "fileVersion": 1, + "versions": { + "quantities": "0.11.0", + "simpleconfig": "0.3.0" + } +} diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..d21ebe1 --- /dev/null +++ b/source/app.d @@ -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); + } +}