Project 2 - Rocket Simulation
0. Getting Started
1. Optional Introduction to C++ and Programming Concepts
2. So… How Do I Use and Code in C++
3. Inputs, Outputs and Comments
4. Initialization of Variables
5. Other Data Types
6. Number Operators
7. Control Flow
8. Vectors
9. Functions
Project 1 - Statistics Program
Project 2 - Rocket Simulation
Project 3 - Airline Management
(To start this project, make a new file on the compiler - you don’t need any additional libraries or starter code)
Now that you’ve implemented your first project, you can try a tougher activity that shows how you can implement the skills you’ve learned so far to code up a program to simulate a vertical rocket launch off of the surface of the Earth. We’ll first introduce some of the necessary equations that you’ll need to solve this problem as you’re not expected to know all that background information beforehand. If it seems like it’s something you’re not familiar with, that’s completely fine - this exercise is adapted from a project in an actual class here at Michigan! First, we’ll explain the physics of the problem: you’ll need to calculate acceleration from values we know and approximate the velocity and altitude as time progresses.
We begin with acceleration, a very important value in many engineering applications. An equation that may or may not be new to you (but one you’ll certainly become familiar with if you pursue physics or engineering further) is Newton’s Second Law:
F = ma
where F is the net force (sum of all the forces) applied on an object, m is that object’s mass, and a is the resulting acceleration of that object. We assume this rocket will be taking off from some celestial body (like Earth, for example, but we’ll see what happens if we don’t limit ourselves to just our planet), so we know that gravity will be present. No matter where we are on a planet, gravity will pull the rocket back down towards the planet so it shows up as a negative term when summing forces.
Accounting for gravity, how do we actually get into space? The rocket produces a force called thrust as it fires, which is directly dependent on two quantities, the mass flow rate of the rocket propellant (how much propellent the rocket is using per second) and the effective exhaust velocity of the rocket (how fast that propellant is being pushed out of the rocket). The mass flow rate, ṁ (pronounced “m dot”), if we assume it’s constant, can be simply calculated by dividing the mass of the propellant on board by the time of the burn. The effective exhaust velocity, cE, is determined by a whole range of factors such as propellant type, maximum combustion temperature, and nozzle shape, to name a few. There’s an entire class (AERO 335 here at Michigan) built around calculating this value so we’ll provide it as a given value, but we’ll see what happens when we change that value to model different engines. Using these values, we arrive at the critical equation you need to find acceleration:
T = ṁ × cE
From here, we assume that the only forces acting on the rocket are thrust and gravity; in reality, there’s quite a few more, especially atmospheric drag, but we’ll assume these forces are 0 for now. Finally, you can solve for acceleration a with the following equation, where mg is the force of gravity:
T - mg = ma
Under the assumption that the mass flow rate and effective exhaust velocity are constant values, thrust is constant. Gravity will decrease slightly as our altitude increases but we can also assume that it’s a constant. Mass, however, does change as time passes, which means we have to constantly recalculate our thrust values using the above equation. There’s no perfect way to continuously recalculate this value, so we use a method called numerical approximation to get a reasonably close answer. The method we’ll use here is to calculate the acceleration at the start time, then assume it will be approximately constant for a short interval of time, called a timestep. We choose how long this time step is; shorter timesteps are more accurate as we’re able to better approximate the acceleration, but will also make our program have to do more calculations which can make it take a very long time or even crash if the timestep is just too small. With this approximate acceleration, we can approximate velocity and position too, described in the next section. After we find these three values, we can update the propellant mass by subtracting how much fuel was spent during that time step and then move forward in time by one time step and repeat the process until we’ve reached the end of our burn time and are out of propellant.
We have to approximate velocity and position using our calculated acceleration value and chosen timestep. The change in velocity can be approximated as acceleration multiplied by the timestep since acceleration is assumed to be constant for the entire timestep. We know we start at rest until the rocket takes off, so the initial velocity is zero, but we must add the change in velocity caused by the acceleration to the current velocity value for each and every timestep. Similarly, altitude can be approximated as velocity multiplied by a timestep since velocity is assumed to be constant for each timestep. We can assume we take off from sea level where altitude is zero and have to add the change in position caused by the velocity for each timestep.
ΔVelocity ≈ Acceleration × Timestep
ΔAltitude ≈ Velocity × Timestep
These two equations summarize the point above, where the delta symbol, Δ, indicates “rate of change in” a quantity - you’ll see that symbol again in future math and physics classes.
With what you’ve learned in this activity so far and the equations and concepts explained above, you’ll be able to complete this problem given some starting values. But first, some hints:
The propellant mass is the initial mass minus the dry mass, which is the weight of everything on the spacecraft, except the propellant. You’ll be given both values for the purposes of doing this activity.
When it comes to the timestep, the smaller it is, the more accurate your answer - you want to choose a small value that doesn’t make the program take a long time to run. Definitely experiment with this value and see what happens as you change it but timesteps of 0.1 sec allow my computer to run the solution code in a couple seconds, so aim for values no larger than that.
Using a loop for this problem is required. You can create it using mass or time as the value the loop automatically updates; both methods work fine but I recommend using time.
You should not need vectors in order to do this activity. You can still use them, but they’ll probably make for a slower solution than if you didn’t use them at all.
The solution code has the calculation for final altitude written as a function called multiple times. This function is only about 30-40 lines long. If you find yourself writing a lot of code, especially if code repeats itself, then step back and think if there’s a simpler way to do it.
You’ll likely find the += operator in C++ very helpful. An example using it is included below. We have a variable, x, and a value we want to add, aptly named value_to_add. The += operator in this case takes the value_to_add and adds it to x. After we run this snippet of code, x will have a value of 3. This is useful in loops to update values. It works the same as if we had written: x = x + value_to_add, but more clearly shows that we’re simply adding a value to x. You can use either method of performing this step in your code.
With those hints in mind, you should be ready to write your code. Remember, the final answer we’re looking for is the altitude at the end of the burn, which should be your final altitude value. Use cout to print this value to the output terminal. Use the following values for your code, unless otherwise specified:
The initial mass is 100000 kg.
The dry mass is 10000 kg.
The burn time is 100 seconds.
The effective exhaust velocity is 4000 m/sec.
The rocket is taking off from Earth so gravity is 9.81 m/sec².
Using the given values, answer these questions:
Calculate the final altitude value using the given values. Use cout to print this value to the output terminal. Using the units we’ve given you, this altitude will be in meters. Convert this to kilometers by dividing by 1000. Did we reach space? (Space is often considered to be anything 100 km and further off the surface of the Earth)
What happens if we make the burn time a little longer? Repeat the calculation using a burn time of 140 seconds and see what happens to your final altitude value. Did it increase or decrease?
Repeat the calculation using the given values, except use an upgraded engine with an effective exhaust velocity of 4500 m/sec. Does this increase or decrease the final altitude compared to the original calculation using the given values?
Repeat the calculation using the given values only now we’re launching a bigger spacecraft on the same rocket, so use a dry mass of 15000 kg. Does this increase or decrease the final altitude?
Using the same dry mass as Part 4, we’re going to use a bigger rocket now. Use an initial mass of 125000 kg to recalculate the final altitude. Does it reach a similar altitude (± 5%) as the original rocket with the given values?
Once again using the given values, let’s assume we’re not taking off of Earth anymore. Calculate the final altitude if this rocket took off from:
The Moon (gravity is 1.62 m/sec² here)
Mars (gravity is 3.711 m/sec² here)
Jupiter (gravity is 24.79 m/sec² here)
Jupiter’s moon, Titan (gravity is 1.352 m/sec² here)
This code is pretty flexible since we’ve made some simplifying assumptions and we have quite a few input parameters to play with - feel free to continue trying out different values if you want! Hopefully this activity has given you an understanding of some of the factors that affect rocket launches and their altitudes when the rocket stops firing. Note that this isn’t its final position! The rocket will still have a positive velocity when the engine runs out of fuel, but gravity will still be trying to pull it back to earth, so acceleration is now negative. The rocket will actually end up further away than the value you’ve calculated. If you’re interested in finding this value, you can use a new while loop that checks to make sure velocity is still positive and then update your new velocity, position, and time within the loop (acceleration will be constant since we assume constant gravity and mass will be constant since the rocket isn’t firing anymore and thrust is zero). Feel free to try it out!
Provided below is the solution code for the project, as well as what the final output should look like. Feel free to check against the solution output to see if your solution is right, but try to resist looking at the solution code before putting a full effort to try it on your own (I know, it’s tempting).