C:\> Rostislav Persion's Projects

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


Computer Simulated Model Rocket





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