|
| 1 | +======================== |
| 2 | +Introduction to RocketPy |
| 3 | +======================== |
| 4 | + |
| 5 | +This tutorial part shows how to open rocketpy files and run a simple simulation. |
| 6 | + |
| 7 | +Opening rocketpy folder |
| 8 | +======================= |
| 9 | + |
| 10 | +Go into the cloned repository folder by typing on a terminal |
| 11 | + |
| 12 | +.. code-block:: console |
| 13 | +
|
| 14 | + cd <rocketpy directory> |
| 15 | +
|
| 16 | +Open your preference editor by typing on a terminal |
| 17 | + |
| 18 | +.. code-block:: console |
| 19 | + |
| 20 | + <editor name> . |
| 21 | +
|
| 22 | +For example, to open VS Code type on terminal |
| 23 | + |
| 24 | +.. code-block:: console |
| 25 | + |
| 26 | + code . |
| 27 | +
|
| 28 | +Alternatively, you can open the folder directly through your editor's interface. |
| 29 | + |
| 30 | +Preparing directory for code editing |
| 31 | +==================================== |
| 32 | + |
| 33 | +You may create a testing file in any directory, but you must remember that they should not be included in the commits and pull requests unless they are part of the proposed solution. |
| 34 | +With that in mind, it is suggested the creation of a folder with all the testing files, so they can be added in the .gitignore file, which contains the name of all the files and folders that will not be added to the commits. To create the folder, type on the terminal: |
| 35 | + |
| 36 | +.. code-block:: console |
| 37 | +
|
| 38 | + mkdir <folder name> |
| 39 | +
|
| 40 | +And, to add it on .gitignore, type: |
| 41 | + |
| 42 | +.. code-block:: console |
| 43 | + |
| 44 | + echo <folder name>/ >> .gitignore |
| 45 | +
|
| 46 | +It is important to remember that all the files inside this folder will not be included in any commit so, if it is important to the solution, do not add them inside it. |
| 47 | + |
| 48 | +Running a simulation with RocketPy |
| 49 | +================================== |
| 50 | + |
| 51 | +Importing the RocketPy files |
| 52 | +---------------------------- |
| 53 | + |
| 54 | +First, create a python (or .ipynb) file to make the simulation. |
| 55 | +To ensure you are using the local files and not the files as a python package (if you installed the library via pip for example), add |
| 56 | + |
| 57 | +.. code-block:: python |
| 58 | +
|
| 59 | + pip install -e . |
| 60 | +
|
| 61 | +Alternatively you can use the following command to pip install the local library: |
| 62 | + |
| 63 | +.. code-block:: console |
| 64 | + |
| 65 | + import sys |
| 66 | + sys.path.append('../') # if you are using a notebook |
| 67 | + sys.path.append('../rocketpy') # if you are using a script |
| 68 | +
|
| 69 | +Import the classes that will be used, in case: |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | + |
| 73 | + from rocketpy import Environment, SolidMotor, Rocket, Flight, Function |
| 74 | +
|
| 75 | +If it is the first time you are using rocketpy and you do not have all required libraries installed, you could use the command: |
| 76 | + |
| 77 | +.. code-block:: python |
| 78 | +
|
| 79 | + pip install -r </path/to/requirements.txt> |
| 80 | +
|
| 81 | +Alternatively, if you are in rocketpy folder, just type |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + pip install -r requirements.txt |
| 86 | +
|
| 87 | +Creating an Environment |
| 88 | +----------------------- |
| 89 | + |
| 90 | +Here we create the environment object that will be used in the simulation. |
| 91 | +It contains information about the local pressure profile, temperature, speed of sound, wind direction and intensity, etc. |
| 92 | + |
| 93 | +.. code-block:: python |
| 94 | +
|
| 95 | + Env = Environment(railLength=5.2, latitude=32.990254, longitude=-106.974998, elevation=1400) |
| 96 | +
|
| 97 | +RocketPy can use local files via the Ensemble method or meteorological forecasts through OpenDAP protocol. |
| 98 | +To work with environment files, it will be very important ensuring tha that you have the netCDF4 library installed. |
| 99 | +Assuming we are using forecast, first we set the simulated data with: |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + import datetime |
| 104 | + tomorrow = datetime.date.today() + datetime.timedelta(days=1) |
| 105 | + Env.setDate((tomorrow.year, tomorrow.month, tomorrow.day, 12)) # Hour given in UTC time |
| 106 | +
|
| 107 | +Then we set the atmospheric model, in this case, GFS forecast: |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + Env.setAtmosphericModel(type="Forecast", file="GFS") |
| 112 | +
|
| 113 | +Weather forecast data can be visualized through two info methods. |
| 114 | + |
| 115 | +``Env.info()`` or ``Env.allInfo()`` |
| 116 | + |
| 117 | +Creating the motor that boosts the rocket |
| 118 | +----------------------------------------- |
| 119 | + |
| 120 | +Now we need to create the motor. |
| 121 | +For example, we will use a solid motor called Pro75M1670, but other configurations are also possible. |
| 122 | +The motor class contains information about the thrust curve and uses some geometric parameters to calculate the mass variation over time, as well as the total thrust and other important outputs. |
| 123 | + |
| 124 | +.. code-block:: python |
| 125 | +
|
| 126 | + Pro75M1670 = SolidMotor( |
| 127 | + thrustSource="../data/motors/Cesaroni_M1670.eng", |
| 128 | + burnOut=3.9, |
| 129 | + grainNumber=5, |
| 130 | + grainSeparation=5 / 1000, |
| 131 | + grainDensity=1815, |
| 132 | + grainOuterRadius=33 / 1000, |
| 133 | + grainInitialInnerRadius=15 / 1000, |
| 134 | + grainInitialHeight=120 / 1000, |
| 135 | + nozzleRadius=33 / 1000, |
| 136 | + throatRadius=11 / 1000, |
| 137 | + interpolationMethod="linear", |
| 138 | + ) |
| 139 | +
|
| 140 | +Motor data can be visualized through the following methods: |
| 141 | + |
| 142 | +``Pro75M1670.info()`` or ``Pro75M1670.allInfo()`` |
| 143 | + |
| 144 | + |
| 145 | +Creating the rocket |
| 146 | +------------------- |
| 147 | + |
| 148 | +The Rocket class contains all information about the rocket that are necessary to the simulation, including the motor, rocket mass and inertia, aerodynamic surfaces, parachutes, etc. |
| 149 | +The first step is to initialize the class with the vital data: |
| 150 | + |
| 151 | +.. code-block:: python |
| 152 | +
|
| 153 | + Calisto = Rocket( |
| 154 | + motor=Pro75M1670, |
| 155 | + radius=127 / 2000, |
| 156 | + mass=19.197 - 2.956, |
| 157 | + inertiaI=6.60, |
| 158 | + inertiaZ=0.0351, |
| 159 | + distanceRocketNozzle=-1.255, |
| 160 | + distanceRocketPropellant=-0.85704, |
| 161 | + powerOffDrag="../../data/calisto/powerOffDragCurve.csv", |
| 162 | + powerOnDrag="../../data/calisto/powerOnDragCurve.csv", |
| 163 | + ) |
| 164 | +
|
| 165 | +Then the rail buttons must be set: |
| 166 | + |
| 167 | +.. code-block:: python |
| 168 | + |
| 169 | + Calisto.setRailButtons([0.2, -0.5]) |
| 170 | +
|
| 171 | +In sequence, the aerodynamic surfaces must be set. |
| 172 | +If a lift curve for the fin set is not specified, it is assumed that they behave according to a linearized model with a coefficient calculated with Barrowman's theory. |
| 173 | +In the example, a nosecone, one fin set and one tail were added, but each case can be designed differently. |
| 174 | + |
| 175 | +.. code-block:: python |
| 176 | +
|
| 177 | + NoseCone = Calisto.addNose(length=0.55829, kind="vonKarman", distanceToCM=0.71971) |
| 178 | +
|
| 179 | + FinSet = Calisto.addFins(4, span=0.100, rootChord=0.120, tipChord=0.040, distanceToCM=-1.04956) |
| 180 | +
|
| 181 | + Tail = Calisto.addTail(topRadius=0.0635, bottomRadius=0.0435, length=0.060, distanceToCM=-1.194656) |
| 182 | +
|
| 183 | +If you are considering the parachutes in the simulation, they also have to be added to the rocket object. |
| 184 | +A trigger function must be supplied to trigger the parachutes. |
| 185 | +Currently, the pressure `(p)` and the state-space variables `(y)` are necessary inputs for the function. |
| 186 | +The state-space contains information about the rocket's position and velocities (translation and rotation). |
| 187 | +For example: |
| 188 | + |
| 189 | +.. code-block:: python |
| 190 | +
|
| 191 | + def drogueTrigger(p, y): |
| 192 | + # p = pressure |
| 193 | + # y = [x, y, z, vx, vy, vz, e0, e1, e2, e3, w1, w2, w3] |
| 194 | + # activate drogue when vz < 0 m/s. |
| 195 | + return True if y[5] < 0 else False |
| 196 | +
|
| 197 | +
|
| 198 | + def mainTrigger(p, y): |
| 199 | + # p = pressure |
| 200 | + # y = [x, y, z, vx, vy, vz, e0, e1, e2, e3, w1, w2, w3] |
| 201 | + # activate main when vz < 0 m/s and z < 800 + 1400 m (+1400 due to surface elevation). |
| 202 | + return True if y[5] < 0 and y[2] < 800 + 1400 else False |
| 203 | +
|
| 204 | +After having the trigger functions defined, the parachute must be added to the rocket: |
| 205 | + |
| 206 | +.. code-block:: python |
| 207 | +
|
| 208 | + Main = Calisto.addParachute( |
| 209 | + "Main", |
| 210 | + CdS=10.0, |
| 211 | + trigger=mainTrigger, |
| 212 | + samplingRate=105, |
| 213 | + lag=1.5, |
| 214 | + noise=(0, 8.3, 0.5), |
| 215 | + ) |
| 216 | +
|
| 217 | + Drogue = Calisto.addParachute( |
| 218 | + "Drogue", |
| 219 | + CdS=1.0, |
| 220 | + trigger=drogueTrigger, |
| 221 | + samplingRate=105, |
| 222 | + lag=1.5, |
| 223 | + noise=(0, 8.3, 0.5), |
| 224 | + ) |
| 225 | +
|
| 226 | +Simulating the flight |
| 227 | +-------------------- |
| 228 | + |
| 229 | +Finally, the flight can be simulated with the provided data. |
| 230 | +The rocket and environment classes are supplied as inputs, as well as the rail inclination and heading angle. |
| 231 | + |
| 232 | +.. code-block:: python |
| 233 | +
|
| 234 | + TestFlight = Flight(rocket=Calisto, environment=Env, inclination=85, heading=0) |
| 235 | +
|
| 236 | +Flight data can be retrieved through: |
| 237 | + |
| 238 | +``TestFlight.info()`` or ``TestFlight.allInfo()`` |
| 239 | + |
| 240 | +This function plots a comprehensive amount of flight data and graphs but, if you want to access one specific variable, for example Z position, this may be achieved by `TestFlight.z`. |
| 241 | +If you insert `TestFlight.z()` the graph of the function will be plotted. |
| 242 | +This and other features can be found in the documentation of the `Function` class, which allows data to be treated in an easier way. |
| 243 | +The documentation of each variable used in the class can be found on `Flight.py` file. |
| 244 | + |
| 245 | +Further considerations |
| 246 | +====================== |
| 247 | + |
| 248 | +RocketPy's classes documentation can be accessed in code via `help(<name of the class>)` command. |
| 249 | +For example, to access Flight class parameters, you can use: |
| 250 | + |
| 251 | +.. code-block:: python |
| 252 | +
|
| 253 | + help(Flight) |
| 254 | +
|
| 255 | +More documentation materials can be found at [read the docs](https://docs.rocketpy.org/en/latest/?badge=latest). |
| 256 | +It can also be found on RocketPy's GitHub page on the badge "docs". |
0 commit comments