Halo orbit solver with modern Fortran

Find us on…

GitHub

halo

halo

Description

Halo orbit solver with modern Fortran

This application can be used to generate long-duration Earth-Moon halo orbits in the ephemeris model. A work in progress.

Build

The application is built using the Fortran Package Manager (FPM). FPM will download all the required dependencies and compile the program, and can also be used to run the tests.

fpm build --profile release

To run an example:

fpm run halo_solver --profile release -- examples/example.json

OpenMP can also be employed to speed up the process. Another example, using the Intel Fortran Compiler with OpenMP:

fpm run halo_solver --profile release --compiler ifort --flag "-qopenmp" -- examples/example.json

Or with Gfortran:

fpm run halo_solver --profile release --flag "-fopenmp" -- examples/example.json

Jacobian options

The solver's Jacobian is computed by finite differences by default ("jacobian_mode": 1). With "jacobian_mode": 2, it is computed analytically instead: each segment is propagated once with its state transition matrix (using Pines' second partials for the lunar gravity field, in src/astro_partials_module.f90), and the epoch partials are computed in closed form. This is faster and more accurate. It requires "use_splined_ephemeris": true. Optional "stm_rtol" and "stm_atol" set the integrator tolerances for the state transition matrix (default: rtol and atol). See the technical note in docs/halo-paper/ for details and a comparison.

The tests partials_test (the partial derivatives) and jacobian_test (both Jacobian methods, compared) are run from the root directory:

fpm test partials_test --profile release
fpm test jacobian_test --profile release --flag "-fopenmp" -- examples/example_sparse.json

moon_frame_test checks the splined MOON_PA frame (src/splined_moon_frame_module.f90, used with "grav_frame": 2) against the moonframe library, which is only a test dependency: the rotation matrices must be identical (fpm test moon_frame_test --profile release --flag "-fopenmp").

text_buffer_test checks the number formatting used to write the trajectory JSON file (every number must read back exactly):

fpm test text_buffer_test --profile release

ephemeris_spline_test times the setup of the splined ephemeris and of the MOON_PA frame (and of the whole problem) for the given numbers of revs (default 100 and 1120), with a config file's ephemeris options:

fpm test ephemeris_spline_test --profile release --flag "-fopenmp" -- examples/example_sparse.json 100 1120

Solution plots

With "generate_solution_plots": true, after the solve the program writes the epochs and the defects at the constraint points for the initial guess and the solution (solution_plot_data_*.json), and runs python/plot_solution.py on it (it needs Python with numpy and matplotlib, as in the pixi environment: pixi run fpm run halo_solver ...). It makes two plots (PDF and PNG): epoch_drift_* (how far the solution's epochs moved from the initial guess's) and defects_* (the position and velocity defects, for the guess and the solution). The script can also be run by itself on a data file: python python/plot_solution.py solution_plot_data_<case>.json [--outdir DIR]. To compare the epoch drift of several solutions (e.g., with different options) in one plot: python python/plot_solution.py --compare a.json b.json --labels "default" "offsets" [--name NAME] [--outdir DIR].

Epoch variables

By default ("epoch_variable_mode": 1), each node's epoch variable is its epoch (days from the reference epoch), scaled by its own initial magnitude. The scale grows along the trajectory, so the solver's minimum-norm steps change the later epochs most, and the orbit's phase (its periapsis passage times) can drift by many hours from the initial guess. With "epoch_variable_mode": 2, the epoch variables are the offsets from the initial guess, all scaled by the same "epoch_scale" (days, default 10). This keeps the phase closer to the initial guess (for the 100-rev baseline: periapsis times within 4.4 h of the guess, instead of 18 h), and makes the solution nearly independent of the trajectory's length (the 100-rev solution is within 0.3 h rms of the first 100 revs of the 1120-rev one). The default of 10 days works well for both 100 and 1120 revs. Too small a scale doesn't converge (0.01 days diverges, and 1 day stalls for 1120 revs), and too large a scale brings the drift back (100 days for 100 revs).

With "xscale_mode": 2, all the variables' scales are instead set from their sensitivities: each scale is divided by the norm of its column of the Jacobian at the initial guess, so a unit change of any scaled variable changes the constraints by the same amount ("epoch_scale_factor" multiplies the epoch scales: >1 makes the epochs cheaper to change). This is experimental: for the 100-rev baseline it converges as well as the default (periapsis times within 5.6 h of the guess), but it doesn't converge for 1120 revs (with any epoch_scale_factor from 1 to 100), where the default scaling does.

GUI

halo_gui is a GUI for the solver (qt-fortran for the window, vkframes for the 3D view). Choose a config file (default examples/example_sparse.json) and click Run solver. The solver runs inside the GUI (in a worker thread), its output is shown in the window, and the resulting trajectory is plotted in the Earth-Moon rotating or inertial frame (Moon-centered, colored by the distance from the Moon). With Plot each iteration checked, each solver iteration's trajectory is plotted as it solves (sent straight to the 3D view, not through files). Center puts the camera's center (what it rotates about) on the central body or on the spacecraft (the camera then moves with it). Stop stops the solver at its next iteration. File > Open Trajectory plots an existing traj_*.json file.

The GUI is a separate fpm package (gui/), so the solver above still builds and runs with plain fpm, without Qt or Vulkan. Qt, VulkanSceneGraph and the C++ compiler come from the pixi environment, and tools/fpm_gui.py gives fpm the C++ settings both libraries need (macOS needs version 26 or later, for the Vulkan driver):

pixi run gui-build                                     # build the GUI (release, OpenMP)
pixi run gui                                           # run it
pixi run gui -- examples/example.json --iterations --run   # check "Plot each iteration", and solve at startup

pixi run gui-build-quad and pixi run gui-quad do the same with the solver in quad precision (-DREAL128, much slower). The 3D view stays in double precision.

Using qr_mumps

The qr_mumps solver can also be used, which will run much faster than the default LAPACK solver for very large problems. For this, the code must be compiled with the WITH_QRMUMPS preprocessor directive. See the build.sh and run.sh scripts for details.

Building On Windows

Work in progress....

Example:

"<intel install dir>\windows\bin\ifortvars.bat" intel64
"<intel install dir>\windows\mkl\bin\mklvars.bat" intel64

set OMP_NUM_THREADS=12

fpm run halo_solver --profile release --compiler ifort --flag "/fpp /Qmkl:parallel /Qopenmp" -- examples/example.json

Result is:

LINK : fatal error LNK1181: cannot open input file 'lapack.lib'

It's because fmin and nlsolver-fortran both have explicit lapack and blas dependencies specified in their fpm.toml files (link = ["lapack", "blas"]). If i delete those it compiles.

Other notes

Another source of CR3BP halos that can be used as initial guesses for this tool can be found here: https://ssd.jpl.nasa.gov/tools/periodic_orbits.html

Documentation

The latest API documentation can be found here. This was generated from the source code using FORD.

See also

Reference

Developer Info

Jacob Williams