State Conversions

The goal of this tutorial is to introduce key methods that allow efficient conversions between different spacecraft state representations.

We start by importing numpy and the astro module:

In [1]:
import numpy as np
import godot.core.astro as astro

Next, we create a state vector. For instance, using classic orbit elements,

In [2]:
d2r = np.pi/180
a = 7000.0
e = 0.01
I = 32*d2r
RAAN = 10*d2r
argP = 170*d2r
ta = 23*d2r
coe = np.array([a, e, I, RAAN, argP, ta])
print(coe)
[7.00000000e+03 1.00000000e-02 5.58505361e-01 1.74532925e-01
 2.96705973e+00 4.01425728e-01]

Built-in godot functions allow for efficient conversions between different state representations. For instance,

In [3]:
GM = 398600.4418
xyz = astro.cartFromKep(coe, GM)
print('xyz: ', xyz)

equi = astro.equiFromCart(xyz, GM)
print('equi: ', equi)

coe_check = astro.kepFromEqui(equi, GM)
print('coe_check: ', coe_check)

err = coe - coe_check
print('Norm of absolute error: ', np.linalg.norm(err))
xyz:  [-6.42528954e+03 -2.47643514e+03 -8.26747585e+02  2.75264645e+00
 -5.91053872e+00 -3.93588733e+00]
equi:  [ 6.99930000e+03 -1.00000000e-02  1.07878116e-17  2.82389079e-01
  4.97928137e-02 -2.74016693e+00]
coe_check:  [7.00000000e+03 1.00000000e-02 5.58505361e-01 1.74532925e-01
 2.96705973e+00 4.01425728e-01]
Norm of absolute error:  9.095054492869776e-13

One advantage available in godot is that seemingly straightforward conversions can be implemented also when calculating partial derivatives. For instance,

In [4]:
import godot.core.autodif as ad
coe_x = ad.Vector(coe, 'coe')
GM_x = ad.Scalar(GM, 'GM')
xyz_x = astro.cartFromKep(coe_x, GM_x)
print(xyz_x)
         Value |        coe[0]        coe[1]        coe[2]        coe[3]        coe[4]        coe[5] |            GM
 -6.425290e+03 | -9.178985e-01  5.989082e+03 -1.435632e+02  2.476435e+03  2.531590e+03  2.506713e+03 |              
 -2.476435e+03 | -3.537764e-01  2.308312e+03  8.141874e+02 -6.425290e+03 -5.372878e+03 -5.382466e+03 |              
 -8.267476e+02 | -1.181068e-01  7.706204e+02 -1.323073e+03  0.000000e+00 -3.581037e+03 -3.584238e+03 |              
  2.752646e+00 | -1.966176e-04 -1.685671e-01 -6.834597e-01  5.910539e+00  7.066437e+00  6.991319e+00 |  3.452889e-06
 -5.910539e+00 |  4.221813e-04 -6.493424e+00  3.876092e+00  2.752646e+00  2.696555e+00  2.694594e+00 | -7.414115e-06
 -3.935887e+00 |  2.811348e-04 -3.977608e+00 -6.298736e+00  0.000000e+00  8.926348e-01  8.995790e-01 | -4.937134e-06

As expected, the position coordinates of a spacecraft do not depend explicitly on the gravitational parameter

We conclude this tutorial by showcasing the most general conversion method available in godot: the astro.convert function.

In [5]:
help(astro.convert)
Help on built-in function convert in module godot.core.astro._astro:

convert(...) method of builtins.PyCapsule instance
    convert(from, to, state, property=None, settings=None)
    
    Converts between any two :class:`godot.core.astro.StateType` state representations.
    
    Parameters
    ----------
    from: :class:`str` or :class:`godot.core.astro.StateType`
        State type to convert from
    to: :class:`str` or :class:`godot.core.astro.StateType`
        State type to convert to
    state: array_type (size 6)
        State to convert
    property: `dict(str,:class:`float` | :class:`godot.core.autodif.xdouble`)`, optional
        Containing values necessary for the tranformations, by default empty
    settings: `dict(str,str)`, optional
        Containing values necessary for the tranformations, by default empty
    Returns
    -------
    array_type (size 6)
        Converted state in the desired state type

As can be seen, the astro.convert function takes as an input two strings (representing the initial and target state representations), the state to be converted (array), and two additional objects: 'property' and 'settings'. Properties can be created using the astro.StateConversionProperty, whereas Settings are additional parameters that may be needed for custom state representations.

Below is an example of how to convert from 'Kep' orbit elements to 'Cart' coordinates via astro.convert. Note how the gravitational parameter of the Earth is passed via the astro.StateConversionProperty(GM) object.

In [6]:
From = 'Kep'
To = 'Cart'
gm = astro.StateConversionProperty(GM)
xyz_new = astro.convert(From, To, coe, gm)
print('xyz: ', xyz_new)
print('error check: ', xyz_new - xyz)
xyz:  [-6.42528954e+03 -2.47643514e+03 -8.26747585e+02  2.75264645e+00
 -5.91053872e+00 -3.93588733e+00]
error check:  [0. 0. 0. 0. 0. 0.]