-
Notifications
You must be signed in to change notification settings - Fork 1
/
bodies.rkt
368 lines (299 loc) · 11.9 KB
/
bodies.rkt
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname bodies) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
; This file is part of the orbital simulation challenge
; of the lecture "Informatik 1" at Tübingen University
; in winter term 2015/16.
;
; By Jonathan Brachthäuser and Tillmann Rendel.
;
;
; This file contains definitions to model four celestial bodies:
; the earth, the moon, a space ship, and a satellite.
; The definitions in this file use definitions from the following file:
(require "vector.rkt")
; Force is a Number
; interp. physical force in newton (N).
; Mass is a Number
; interp. weight in kilograms (kg).
; Distance is a Number
; interp. the distance between two points in meters (m).
; Speed is a Number
; interp. the speed of an object in meters per second (m/s)
; Position is a structure: (make-posn Distance Distance)
; interp. a position in a 2D-space, relative to the origin.
; Direction is a structure: (make-posn Distance Distance)
; interp. a direction in a 2D-space, magnitude of the vector should be ignored.
; DirectedForce is a structure: (make-posn Force Force)
; interp. directed force with a horizontal and vertical force component.
; Velocity is a structure: (make-posn Speed Speed)
; interp. directed speed of an object.
; Flow is a Number
; interp. mass flow in kg/s.
(define-struct earth (position velocity))
(define-struct moon (position velocity))
(define-struct sat (position velocity))
(define-struct ship (position velocity fuel orientation thrust))
(define-struct earth-centered ())
(define-struct moon-centered ())
(define-struct sat-centered ())
(define-struct ship-centered ())
; Earth is a structure: (make-earth Position Velocity)
; interp. simulation data about the earth.
; Moon is a structure: (make-moon Position Velocity)
; interp. simulation data about the moon.
; Sat is a structure: (make-sat Position Velocity)
; interp. simulation data about the gps satellite.
; Ship is a structure: (make-ship Position Velocity Mass Direction Direction)
; interp. simulation data about the spaceship.
; Body is one of:
; - Earth
; - Moon
; - Sat
; - Ship
; interp. simulation data about celestial body.
; A CenteredBody is one of:
; - (make-earth-centered)
; - (make-moon-centered)
; - (make-sat-centered)
; - (make-ship-centered)
; interp. a flag, which celestial body is currently centered.
; a Force that represents the gravitational constant
(define G 6.667408e-11)
; EARTH
; -----
; initial Position of the earth in the simulated world
(define EARTH-POS (make-posn 0 0))
; initial Velocity of the earth in the simulated world
(define EARTH-VEL (make-posn 0 0))
; Mass of the earth
(define EARTH-MASS 5.974e24)
; radius of the earth, as a Distance
(define EARTH-RADIUS 6371e3)
; gravity of a 1kg object on the surface of the earth, as a Force
(define EARTH-GRAVITY 9.80665)
; initial simulation state of the earth
(define EARTH (make-earth EARTH-POS EARTH-VEL))
; MOON
; ----
; orbital distance of the moon to the earth, as a Distance
(define MOON-ORBIT 363.3e6)
; initial orbital speed of the moon, as a Speed
(define MOON-INIT-V 1.023e3)
; Mass of the moon
(define MOON-MASS 7.349e22)
; radius of the moon, as a Distance
(define MOON-RADIUS 1.738e6)
; initial Position of the moon in the simulated world
(define MOON-POS (make-posn (* -1 MOON-ORBIT) 0))
; initial Velocity of the moon in the simulated world
(define MOON-VEL (make-posn 0 MOON-INIT-V))
; initial simulation state of the moon
(define MOON (make-moon MOON-POS MOON-VEL))
; GPS-SATELLITE AROUND THE EARTH
; ------------------------------
; orbital Distance of the gps-satellite to the earth
(define SAT-ORBIT (+ 20.2e6 EARTH-RADIUS))
; initial orbital Speed of the satellite
(define SAT-INIT-V (sqrt (/ (* G EARTH-MASS) SAT-ORBIT)))
; Mass of the gps satellite
(define SAT-MASS 1080)
; initial Position of the satellite in the simulated world
(define SAT-POS (v* SAT-ORBIT (v-norm (make-posn 1 1))))
; initial Velocity of the satellite in the simulated in
(define SAT-VEL (v* SAT-INIT-V (orthogonal-unit-vector SAT-POS)))
; initial simulation state of the satellite
(define SAT (make-sat SAT-POS SAT-VEL))
; SPACE SHIP: Apollo 10
; ---------------------
; orbital Distance of the space ship in parking orbit around earth
(define SHIP-ORBIT (+ 185e3 EARTH-RADIUS))
; initial orbital Speed of the ship
(define SHIP-INIT-V (sqrt (/ (* G EARTH-MASS) SHIP-ORBIT)))
; engine propellants as a Mass
(define SHIP-FUEL 18410)
; structural Mass of the space-ship without fuel
(define SHIP-MASS-EMPTY 8590)
; initial Mass of the space-ship including fuel
(define SHIP-MASS (+ SHIP-FUEL SHIP-MASS-EMPTY))
; initial Position of the space-ship in the simulated world
(define SHIP-POS (make-posn SHIP-ORBIT 0))
; initial Velocity of the space-ship in the simulated world
(define SHIP-VEL (make-posn 0 (* -1 SHIP-INIT-V)))
; initial simulation state of the space-ship
(define SHIP (make-ship SHIP-POS SHIP-VEL SHIP-FUEL SHIP-VEL ZERO-VECTOR))
; PROPULSION SYSTEM OF THE CSM (AJ10)
; -----------------------------------
; specific impulse of the engine
(define AJ10-ISP-PER-SEC 314)
; thrust of the AJ10 engine in vacuum, as a Force
(define AJ10-THRUST-VAC 43.7e3)
; Mass flow rate of exhausted fuel in kg/sec
(define AJ10-FUEL-FLOW (/ AJ10-THRUST-VAC EARTH-GRAVITY AJ10-ISP-PER-SEC))
; Exhaust Speed of the engine
(define AJ10-EXHAUST-SPEED (/ AJ10-THRUST-VAC AJ10-FUEL-FLOW))
; =====================================
; Library to work with celestial bodies
; =====================================
; Body -> Mass
; return a celestial body's mass
(check-expect (mass EARTH) EARTH-MASS)
(check-expect (mass MOON) MOON-MASS)
(check-expect (mass SAT) SAT-MASS)
(check-expect (mass SHIP) (+ SHIP-MASS-EMPTY SHIP-FUEL))
(define (mass body)
(cond
[(earth? body) EARTH-MASS]
[(moon? body) MOON-MASS]
[(sat? body) SAT-MASS]
[(ship? body) (+ (ship-fuel body) SHIP-MASS-EMPTY)]))
; Body -> Velocity
; return a celestial body's velocity
(check-expect (velocity EARTH) EARTH-VEL)
(check-expect (velocity MOON) MOON-VEL)
(check-expect (velocity SAT) SAT-VEL)
(check-expect (velocity SHIP) SHIP-VEL)
(define (velocity body)
(cond
[(earth? body) (earth-velocity body)]
[(moon? body) (moon-velocity body)]
[(sat? body) (sat-velocity body)]
[(ship? body) (ship-velocity body)]))
; Body -> Position
; return a celestial body's position
(check-expect (position EARTH) EARTH-POS)
(check-expect (position MOON) MOON-POS)
(check-expect (position SAT) SAT-POS)
(check-expect (position SHIP) SHIP-POS)
(define (position body)
(cond
[(earth? body) (earth-position body)]
[(moon? body) (moon-position body)]
[(sat? body) (sat-position body)]
[(ship? body) (ship-position body)]))
; Body -> DirectedForce
; returns a celestial body's engine's thrust
(check-expect (thrust EARTH) ZERO-VECTOR)
(check-expect (thrust MOON) ZERO-VECTOR)
(check-expect (thrust SAT) ZERO-VECTOR)
(check-expect (thrust SHIP) ZERO-VECTOR)
(check-expect (thrust (set-thrust v1 SHIP)) v1)
(define (thrust body)
(cond
[(ship? body) (ship-thrust body)]
[else ZERO-VECTOR]))
; Position Body -> Body
; returns a new body with the updated position
(check-within (position (set-position ZERO-VECTOR EARTH)) ZERO-VECTOR 1e-5)
(check-within (position (set-position ZERO-VECTOR MOON)) ZERO-VECTOR 1e-5)
(check-within (position (set-position ZERO-VECTOR SAT)) ZERO-VECTOR 1e-5)
(check-within (position (set-position ZERO-VECTOR SHIP)) ZERO-VECTOR 1e-5)
(check-within (set-position (position EARTH) EARTH) EARTH 1e-5)
(check-within (set-position (position MOON) MOON) MOON 1e-5)
(check-within (set-position (position SAT) SAT) SAT 1e-5)
(check-within (set-position (position SHIP) SHIP) SHIP 1e-5)
(define (set-position new-position body)
(cond
[(earth? body) (make-earth new-position (velocity body))]
[(moon? body) (make-moon new-position (velocity body))]
[(sat? body) (make-sat new-position (velocity body))]
[(ship? body) (make-ship new-position
(velocity body)
(fuel body)
(ship-orientation body)
(thrust body))]))
; Velocity Body -> Body
; returns a new body with the updated velocity
(check-within (velocity (set-velocity ZERO-VECTOR EARTH)) ZERO-VECTOR 1e-5)
(check-within (velocity (set-velocity ZERO-VECTOR MOON)) ZERO-VECTOR 1e-5)
(check-within (velocity (set-velocity ZERO-VECTOR SAT)) ZERO-VECTOR 1e-5)
(check-within (velocity (set-velocity ZERO-VECTOR SHIP)) ZERO-VECTOR 1e-5)
(check-within (set-velocity (velocity EARTH) EARTH) EARTH 1e-5)
(check-within (set-velocity (velocity MOON) MOON) MOON 1e-5)
(check-within (set-velocity (velocity SAT) SAT) SAT 1e-5)
(check-within (set-velocity (velocity SHIP) SHIP) SHIP 1e-5)
(define (set-velocity new-velocity body)
(cond
[(earth? body) (make-earth (position body) new-velocity)]
[(moon? body) (make-moon (position body) new-velocity)]
[(sat? body) (make-sat (position body) new-velocity)]
[(ship? body) (make-ship (position body)
new-velocity
(fuel body)
(ship-orientation body)
(thrust body))]))
; Body -> Mass
; returns the fuel of an celestial body
(check-expect (fuel SHIP) SHIP-FUEL)
(check-expect (fuel EARTH) 0)
(check-expect (fuel MOON) 0)
(check-expect (fuel SAT) 0)
(define (fuel body)
(if (ship? body)
(ship-fuel body)
0))
; Body -> Flow
; returns the maximum fuel use per second of an celestial body
(check-expect (fuel-flow SHIP) AJ10-FUEL-FLOW)
(check-expect (fuel-flow EARTH) 0)
(check-expect (fuel-flow MOON) 0)
(check-expect (fuel-flow SAT) 0)
(define (fuel-flow body)
(if (ship? body)
AJ10-FUEL-FLOW
0))
; Body -> Speed
; returns the exhaust speed of a celestial body's rocket engine
(check-expect (exhaust-speed SHIP) AJ10-EXHAUST-SPEED)
(check-expect (exhaust-speed EARTH) 0)
(check-expect (exhaust-speed MOON) 0)
(check-expect (exhaust-speed SAT) 0)
(define (exhaust-speed body)
(if (ship? body)
AJ10-EXHAUST-SPEED
0))
; Mass Body -> Body
; returns a new ship with the updated fuel, or other celestial bodies unchanged
(check-within (fuel (set-fuel 0 SHIP)) 0 1e-5)
(check-within (set-fuel (fuel SHIP) SHIP) SHIP 1e-5)
(check-within (set-fuel 42 EARTH) EARTH 1e-5)
(check-within (set-fuel 42 MOON) MOON 1e-5)
(check-within (set-fuel 42 SAT) SAT 1e-5)
(define (set-fuel new-fuel body)
(if (ship? body)
(make-ship (position body)
(velocity body)
new-fuel
(ship-orientation body)
(thrust body))
body))
; Direction Ship -> Ship
; returns a new space-ship with the updated orientation
(check-within (ship-orientation (set-orientation ZERO-VECTOR SHIP)) ZERO-VECTOR 1e-5)
(check-within (set-orientation (ship-orientation SHIP) SHIP) SHIP 1e-5)
(define (set-orientation new-orientation ship)
(make-ship (position ship)
(velocity ship)
(ship-fuel ship)
new-orientation
(thrust ship)))
; Direction Ship -> Ship
; returns a new space-ship with the updated thrust vector
(check-within (thrust (set-thrust ZERO-VECTOR SHIP)) ZERO-VECTOR 1e-5)
(check-within (set-thrust (thrust SHIP) SHIP) SHIP 1e-5)
(define (set-thrust thrust ship)
(make-ship (position ship)
(velocity ship)
(ship-fuel ship)
(ship-orientation ship)
thrust))
; The following two lines make all definitions in this file
; available to other files that contain:
;
; (require "bodies.rkt")
;
; Attention: These two lines have to come at the end of this file
; to avoid confusing the (define-struct ...) from the teaching
; languages with the (define-struct ...) from racket/base.
(require racket/base)
(provide (all-defined-out))