brahe 1.6.0

Brahe is a modern satellite dynamics library for research and engineering applications designed to be easy-to-learn, high-performance, and quick-to-deploy. The north-star of the development is enabling users to solve meaningful problems and answer questions quickly, easily, and correctly.
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"""
Python (Brahe) propagation benchmarks.
"""

import numpy as np

import brahe

from benchmarks.comparative.implementations.python.base import (
    ensure_eop,
    ensure_sw,
    time_iterations,
)
from benchmarks.comparative.results import TaskResult


def keplerian_single(params: dict, iterations: int) -> TaskResult:
    """Benchmark Keplerian propagation of multiple orbits to single future epochs."""
    ensure_eop()
    cases = params["cases"]

    def run():
        results = []
        for case in cases:
            epc = brahe.Epoch.from_jd(case["jd"], brahe.TimeSystem.UTC)
            oe = np.array(case["elements"])
            dt = case["dt"]
            target = epc + dt

            prop = brahe.KeplerianPropagator.from_keplerian(
                epc, oe, brahe.AngleFormat.DEGREES, 60.0
            )
            state = prop.state_eci(target)
            results.append(state.tolist())
        return results

    times, results = time_iterations(run, iterations)

    return TaskResult(
        task_name="propagation.keplerian_single",
        language="python",
        library="brahe",
        iterations=iterations,
        times_seconds=times,
        results=results,
        metadata={
            "library": "brahe",
            "language": "python",
            "version": getattr(brahe, "__version__", "unknown"),
        },
    )


def keplerian_trajectory(params: dict, iterations: int) -> TaskResult:
    """Benchmark Keplerian propagation over trajectory steps.

    Two input shapes:

    - Single-IC (perf): ``{jd, elements, step_size, n_steps}`` — propagate
      one orbit and return every step's state, like before.
    - Multi-IC (accuracy): ``{cases: [{jd, elements}], step_size, n_steps}``
      — propagate each case over the shared horizon and return the
      final state per case, so accuracy stats reflect IC variance.
    """
    ensure_eop()
    step_size = params["step_size"]
    n_steps = params["n_steps"]
    cases = params.get("cases")

    if cases is None:
        # Perf path: single IC, full trajectory output.
        jd = params["jd"]
        oe = np.array(params["elements"])
        epc = brahe.Epoch.from_jd(jd, brahe.TimeSystem.UTC)

        def run():
            prop = brahe.KeplerianPropagator.from_keplerian(
                epc, oe, brahe.AngleFormat.DEGREES, step_size
            )
            results = []
            for step_idx in range(n_steps):
                target = epc + (step_idx + 1) * step_size
                state = prop.state_eci(target)
                results.append(state.tolist())
            return results
    else:
        # Accuracy path: IC sweep, return one final state per case.
        def run():
            results = []
            for case in cases:
                epc = brahe.Epoch.from_jd(case["jd"], brahe.TimeSystem.UTC)
                oe = np.array(case["elements"])
                prop = brahe.KeplerianPropagator.from_keplerian(
                    epc, oe, brahe.AngleFormat.DEGREES, step_size
                )
                target = epc + n_steps * step_size
                state = prop.state_eci(target)
                results.append(state.tolist())
            return results

    times, results = time_iterations(run, iterations)

    return TaskResult(
        task_name="propagation.keplerian_trajectory",
        language="python",
        library="brahe",
        iterations=iterations,
        times_seconds=times,
        results=results,
        metadata={
            "library": "brahe",
            "language": "python",
            "version": getattr(brahe, "__version__", "unknown"),
        },
    )


def sgp4_single(params: dict, iterations: int) -> TaskResult:
    """Benchmark SGP4 propagation to 50 future epochs."""
    ensure_eop()
    line1 = params["line1"]
    line2 = params["line2"]
    offsets = params["time_offsets_seconds"]

    prop = brahe.SGPPropagator.from_tle(line1, line2, 60.0)
    base_epoch = prop.epoch

    def run():
        results = []
        for dt in offsets:
            target = base_epoch + dt
            # Use .state() to get TEME output directly (matches Java's TEME frame)
            state = prop.state(target)
            results.append(state.tolist())
        return results

    times, results = time_iterations(run, iterations)

    return TaskResult(
        task_name="propagation.sgp4_single",
        language="python",
        library="brahe",
        iterations=iterations,
        times_seconds=times,
        results=results,
        metadata={
            "library": "brahe",
            "language": "python",
            "version": getattr(brahe, "__version__", "unknown"),
        },
    )


def sgp4_trajectory(params: dict, iterations: int) -> TaskResult:
    """Benchmark SGP4 propagation over 1 day at 60s steps."""
    ensure_eop()
    line1 = params["line1"]
    line2 = params["line2"]
    step_size = params["step_size"]
    n_steps = params["n_steps"]

    prop = brahe.SGPPropagator.from_tle(line1, line2, step_size)
    base_epoch = prop.epoch

    def run():
        results = []
        for step_idx in range(n_steps):
            target = base_epoch + (step_idx + 1) * step_size
            # Use .state() to get TEME output directly (matches Java's TEME frame)
            state = prop.state(target)
            results.append(state.tolist())
        return results

    times, results = time_iterations(run, iterations)

    return TaskResult(
        task_name="propagation.sgp4_trajectory",
        language="python",
        library="brahe",
        iterations=iterations,
        times_seconds=times,
        results=results,
        metadata={
            "library": "brahe",
            "language": "python",
            "version": getattr(brahe, "__version__", "unknown"),
        },
    )


def numerical_twobody(params: dict, iterations: int) -> TaskResult:
    """Benchmark numerical two-body propagation.

    See keplerian_trajectory for the single-IC vs multi-IC ``cases``
    parameter shape contract. Single-IC returns every trajectory step;
    multi-IC returns one final state per IC.
    """
    ensure_eop()
    step_size = params["step_size"]
    n_steps = params["n_steps"]
    cases = params.get("cases")

    # Fixed-step RK4 to match the Orekit adapter; both sides pinned to the
    # same integrator + step so the residual is a pure-numerics comparison.
    prop_config = brahe.NumericalPropagationConfig(
        brahe.IntegrationMethod.RK4,
        brahe.IntegratorConfig.fixed_step(step_size),
        brahe.VariationalConfig(),
    )
    force_config = brahe.ForceModelConfig.two_body()

    if cases is None:
        # Perf path: single IC, full trajectory.
        jd = params["jd"]
        oe = np.array(params["elements"])
        epc = brahe.Epoch.from_jd(jd, brahe.TimeSystem.UTC)
        cart = brahe.state_koe_to_eci(oe, brahe.AngleFormat.DEGREES)

        def run():
            prop = brahe.NumericalOrbitPropagator(
                epc, cart, prop_config, force_config
            )
            prop.set_trajectory_mode(brahe.TrajectoryMode.DISABLED)
            results = []
            for _ in range(n_steps):
                prop.step_by(step_size)
                results.append(prop.current_state().tolist())
            return results
    else:
        # Accuracy path: IC sweep, final state per case.
        def run():
            results = []
            for case in cases:
                epc = brahe.Epoch.from_jd(case["jd"], brahe.TimeSystem.UTC)
                oe = np.array(case["elements"])
                cart = brahe.state_koe_to_eci(oe, brahe.AngleFormat.DEGREES)
                prop = brahe.NumericalOrbitPropagator(
                    epc, cart, prop_config, force_config
                )
                prop.set_trajectory_mode(brahe.TrajectoryMode.DISABLED)
                for _ in range(n_steps):
                    prop.step_by(step_size)
                results.append(prop.current_state().tolist())
            return results

    times, results = time_iterations(run, iterations)

    return TaskResult(
        task_name="propagation.numerical_twobody",
        language="python",
        library="brahe",
        iterations=iterations,
        times_seconds=times,
        results=results,
        metadata={
            "library": "brahe",
            "language": "python",
            "version": getattr(brahe, "__version__", "unknown"),
        },
    )


def _force_model_from_params(params: dict) -> "brahe.ForceModelConfig":
    """Build a brahe ForceModelConfig matching the task parameter dict.

    The parameter vector layout is brahe's standard
    `[mass, drag_area, Cd, srp_area, Cr]`, so drag/SRP use parameter indices
    rather than fixed values — that way the same vector drives every step.
    """
    gravity = brahe.GravityConfiguration.spherical_harmonic(
        degree=params["gravity_degree"],
        order=params["gravity_order"],
    )

    third_body = None
    bodies = []
    if params.get("third_body_sun"):
        bodies.append(brahe.ThirdBody.SUN)
    if params.get("third_body_moon"):
        bodies.append(brahe.ThirdBody.MOON)
    if bodies:
        third_body = brahe.ThirdBodyConfiguration(
            ephemeris_source=brahe.EphemerisSource.DE440s,
            bodies=bodies,
        )

    drag = None
    if params.get("drag"):
        drag = brahe.DragConfiguration(
            model=brahe.AtmosphericModel.NRLMSISE00,
            area=brahe.ParameterSource.parameter_index(1),
            cd=brahe.ParameterSource.parameter_index(2),
        )

    srp = None
    if params.get("srp"):
        srp = brahe.SolarRadiationPressureConfiguration(
            area=brahe.ParameterSource.parameter_index(3),
            cr=brahe.ParameterSource.parameter_index(4),
            eclipse_model=brahe.EclipseModel.CONICAL,
        )

    needs_mass = drag is not None or srp is not None
    mass = brahe.ParameterSource.parameter_index(0) if needs_mass else None

    return brahe.ForceModelConfig(
        gravity=gravity,
        drag=drag,
        srp=srp,
        third_body=third_body,
        relativity=False,
        mass=mass,
    )


def _numerical_rk4_run(task_name: str, params: dict, iterations: int) -> TaskResult:
    """Shared driver for the RK4 force-model propagation tasks.

    Accepts the same single-IC/multi-IC parameter contract as the
    other propagation adapters (see ``keplerian_trajectory``).
    """
    ensure_eop()
    if params.get("drag"):
        ensure_sw()

    step_size = params["step_size"]
    n_steps = params["n_steps"]
    param_vec = np.array(params["params"])
    cases = params.get("cases")

    force_config = _force_model_from_params(params)
    prop_config = brahe.NumericalPropagationConfig(
        brahe.IntegrationMethod.RK4,
        brahe.IntegratorConfig.fixed_step(step_size),
        brahe.VariationalConfig(),
    )

    if cases is None:
        # Perf path: single IC, full trajectory output.
        jd = params["jd"]
        oe = np.array(params["elements_deg"])
        epc = brahe.Epoch.from_jd(jd, brahe.TimeSystem.UTC)
        cart = brahe.state_koe_to_eci(oe, brahe.AngleFormat.DEGREES)

        def run():
            prop = brahe.NumericalOrbitPropagator(
                epc, cart, prop_config, force_config, param_vec
            )
            prop.set_trajectory_mode(brahe.TrajectoryMode.DISABLED)
            results = []
            for _ in range(n_steps):
                prop.step_by(step_size)
                results.append(prop.current_state().tolist())
            return results
    else:
        # Accuracy path: IC sweep, final state per case.
        def run():
            results = []
            for case in cases:
                epc = brahe.Epoch.from_jd(case["jd"], brahe.TimeSystem.UTC)
                oe = np.array(case["elements"])
                cart = brahe.state_koe_to_eci(oe, brahe.AngleFormat.DEGREES)
                prop = brahe.NumericalOrbitPropagator(
                    epc, cart, prop_config, force_config, param_vec
                )
                prop.set_trajectory_mode(brahe.TrajectoryMode.DISABLED)
                for _ in range(n_steps):
                    prop.step_by(step_size)
                results.append(prop.current_state().tolist())
            return results

    times, results = time_iterations(run, iterations)

    return TaskResult(
        task_name=task_name,
        language="python",
        library="brahe",
        iterations=iterations,
        times_seconds=times,
        results=results,
        metadata={
            "library": "brahe",
            "language": "python",
            "version": getattr(brahe, "__version__", "unknown"),
            "frame": "GCRF",
            "integrator": "RK4",
            "step_size": step_size,
            "gravity": f"{params['gravity_degree']}x{params['gravity_order']}",
        },
    )


def numerical_rk4_grav5x5(params: dict, iterations: int) -> TaskResult:
    return _numerical_rk4_run("propagation.numerical_rk4_grav5x5", params, iterations)


def numerical_rk4_grav20x20_sun_moon(params: dict, iterations: int) -> TaskResult:
    return _numerical_rk4_run(
        "propagation.numerical_rk4_grav20x20_sun_moon", params, iterations
    )


def numerical_rk4_grav80x80_full(params: dict, iterations: int) -> TaskResult:
    return _numerical_rk4_run(
        "propagation.numerical_rk4_grav80x80_full", params, iterations
    )