C:\> Rostislav Persion's Projects

.:: Model Rocket Simulator ::.
Model Rocket Simulator


Computer Simulated Model Rocket





   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Threading.Tasks;
   6:  using System.Threading;
   7:   
   8:  namespace RocketSim001
   9:  {
  10:   
  11:      enum RocketEngine {A8, B6, C6}; // ENGINE TYPES
  12:   
  13:      class Program
  14:      {
  15:   
  16:          static string DrawBar(double number, double outof, double max_size)
  17:          {
  18:              string bars = "";
  19:              if (number <= outof)
  20:              {
  21:                  int num = (int)Math.Round((number / outof) * max_size);
  22:                  for (int i = 0; i < num; i++) { bars += "#"; }
  23:              }
  24:              else
  25:              {
  26:                  bars = "# OVERFLOW #";
  27:              }
  28:              return bars;
  29:          }
  30:   
  31:   
  32:          static double Thrust(double t, RocketEngine re) // THRUST OF ENGINE AT TIME T
  33:          {
  34:              double ret_thrust = 0;
  35:              if (re == RocketEngine.C6) // C6 Engine
  36:              {
  37:                  if (t >= 0 && t < .1) { ret_thrust = 3; }
  38:                  if (t >= .1 && t < .2) { ret_thrust = 9; }
  39:                  if (t >= .2 && t < .3) { ret_thrust = 9; }
  40:                  if (t >= .3 && t < 1.8) { ret_thrust = 5; }
  41:                  if (t >= 1.8) { ret_thrust = 0; }
  42:              }
  43:   
  44:              return ret_thrust;
  45:          }
  46:   
  47:          static double MassOfEngine(double t, RocketEngine re) // MASS OF ENGINE AT TIME T
  48:          {
  49:              double ret_mass = 0;
  50:   
  51:              if (re == RocketEngine.C6) // C6 Engine
  52:              {
  53:                  if (t >= 0 && t < 1.8) { ret_mass = (0.0108 + (0.012 - (0.007 * t))); }
  54:                  else { ret_mass = 0.0108; }
  55:              }
  56:              return ret_mass;
  57:          }
  58:   
  59:          static double Drag(double velocity, double diameter) // COMPUTE AIR DRAG
  60:          {
  61:              double area = Math.PI * Math.Pow(diameter / 2, 2);
  62:              //F = .5 * density_of_air * velocity^2 * drag_coef * area
  63:              return .5 * 1.225 * Math.Pow(velocity, 2) * .75 * area * Math.Sign(velocity);
  64:          }
  65:   
  66:          static void Main(string[] args)
  67:          {
  68:              // MAIN PROGRAM LOOP
  69:              while (true)
  70:              {
  71:                  // DECLARE SIMULATION VARIABLES
  72:   
  73:                  double height = 0;
  74:                  double velocity = 0;
  75:                  double acceleration = 0;
  76:                  double time = 0;
  77:   
  78:                  double rocketdiameter = .05;
  79:                  double massofrocket = .05;
  80:                  RocketEngine re = RocketEngine.C6;
  81:   
  82:                  double gravity = 9.8;
  83:                  double dt = .01;
  84:                  int delay = 100;
  85:   
  86:                  // SPLASH SCREEN
  87:                  Console.Clear();
  88:                  Console.ForegroundColor = ConsoleColor.Yellow;
  89:                  Console.WriteLine("ROSTISLAV PERSION - MODEL ROCKET SIMULATOR - C6 ENGINE\n");
  90:                  Console.ForegroundColor = ConsoleColor.Green;
  91:   
  92:                  // USER INPUT
  93:   
  94:                  Console.Write("ROCKET DIAMETER (m) [0.05]: ");
  95:                  rocketdiameter = Convert.ToDouble(Console.ReadLine());
  96:   
  97:                  Console.Write("ROCKET MASS W/O ENGINE (kg) [0.01]: ");
  98:                  massofrocket = Convert.ToDouble(Console.ReadLine());
  99:   
 100:                  Console.Write("DELTA TIME (s) [0.01]: ");
 101:                  dt = Convert.ToDouble(Console.ReadLine());
 102:   
 103:                  Console.Write("DELAY TIME (ms) [1]: ");
 104:                  delay = Convert.ToInt32(Console.ReadLine());
 105:   
 106:                  Console.Write("ROCKET ENGINE [A8, B6, C6]: ");
 107:                  string re_string = Console.ReadLine();
 108:                  re = RocketEngine.C6;
 109:                  if (re_string.Trim().ToUpper() == "A8") { re = RocketEngine.A8; }
 110:                  if (re_string.Trim().ToUpper() == "B6") { re = RocketEngine.B6; }
 111:                  if (re_string.Trim().ToUpper() == "C6") { re = RocketEngine.C6; }
 112:   
 113:                  // RECORD KEEPING
 114:                  double max_height = 0;
 115:                  double max_velocity = 0;
 116:                  double at_height = 0;
 117:   
 118:   
 119:                  // MAIN SIMULATION LOOP
 120:                  while (height >= 0)
 121:                  {
 122:                      time += dt;
 123:   
 124:                      // MOTION STUFF
 125:                      acceleration = (Thrust(time, re) - Drag(velocity, rocketdiameter)) / (massofrocket + MassOfEngine(time, re)) - gravity;
 126:                      velocity += acceleration * (dt);
 127:                      height += velocity * (dt);
 128:   
 129:                      // RECORD KEEPING
 130:                      if (height > max_height) { max_height = height; }
 131:                      if (velocity > max_velocity) { max_velocity = velocity; at_height = height; }
 132:   
 133:   
 134:                      // DISPLAY STATS
 135:                      Console.Clear();
 136:   
 137:                      Console.ForegroundColor = ConsoleColor.Red;
 138:                      Console.WriteLine("SIMULATING: " + re.ToString() + " ROCKET ENGINE");
 139:                      Console.WriteLine();
 140:   
 141:                      Console.ForegroundColor = ConsoleColor.Green;
 142:                      Console.WriteLine("TIME: " + time);
 143:                      Console.WriteLine("HEIGHT: " + Math.Round(height) + " meters " + Math.Round(3.28 * height) + " feet");
 144:                      Console.WriteLine("ACCEL: " + Math.Round(acceleration, 3));
 145:                      Console.WriteLine("VELOCITY: " + Math.Round(velocity, 2) + " m/s " + Math.Round(2.24 * velocity, 2) + " mph");
 146:                      Console.WriteLine("DRAG: " + Drag(velocity, rocketdiameter) + " Newtons");
 147:                      Console.WriteLine("MASS OF ROCKET: " + (massofrocket + MassOfEngine(time, re)) + " kg " + Math.Round(((massofrocket + MassOfEngine(time, re)) * 1000), 1) + " grams");
 148:   
 149:                      Console.ForegroundColor = ConsoleColor.White;
 150:                      Console.WriteLine();
 151:                      Console.WriteLine("MAX HEIGHT: " + Math.Round(max_height) + " meters " + Math.Round(3.28 * max_height) + " feet");
 152:                      Console.WriteLine("MAX VELOCITY: " + Math.Round(max_velocity, 2) + " m/s " + Math.Round(2.24 * max_velocity, 2) + " mph AT: " + Math.Round(at_height,2) + " meters " + Math.Round(3.28 * at_height) + " feet");
 153:   
 154:                      // BAR GRAPH
 155:                      Console.WriteLine();
 156:                      if (height < 200)
 157:                      {
 158:                          double console_width = 70;
 159:                          double max_value = 300;
 160:                          Console.Write("ROCKET HEIGHT: ");
 161:                          Console.WriteLine(DrawBar(height, max_value, console_width));
 162:                      }
 163:   
 164:                      // SLOW DOWN SIMULATION
 165:                      Thread.Sleep(delay);
 166:   
 167:                  }
 168:   
 169:                  Console.WriteLine();
 170:                  Console.WriteLine("### SIMULATION COMPLETE");
 171:                  Console.WriteLine();
 172:                  Console.WriteLine("### PRESS X + ENTER TO EXIT. OR JUST ENTER TO RESTART");
 173:                  string exit = Console.ReadLine();
 174:                  if (exit.Trim().ToUpper() == "X") { Environment.Exit(0); }
 175:                  
 176:   
 177:              }
 178:          }
 179:      }
 180:   
 181:  }