alfonsogonzalez / AWP

Astrodynamics with Python book, software, and videos. Spacecraft trajectory and attitude modeling and simulation
275 stars 67 forks source link

Switch ODE solving method in Spacecraft and Rocket classes from scipy.integrate.ode to scipy.integrate.solve_ivp #23

Closed alfonsogonzalez closed 2 years ago

alfonsogonzalez commented 2 years ago

Thank you to a YouTube commenter on this video ( https://youtu.be/TzX6bg3Kc0E ) for pointing out that SciPy's ODE package has a function solve_ivp that in my opinion is a much better solution for the Spacecraft and Rocket classes. Here are the reasons:

CONS: The implementation of this function is much less intuitive: Stop conditions zero-crossings are not trivial for someone in the processing of learning orbital mechanics, and there is a specific nuance to the solve_ivp events that requires defining attributes to the Spacecraft stop_condition methods that is very non-intuitive to a Python beginner. For example:

`

    self.check_min_alt.__func__.direction   = -1
    self.check_max_alt.__func__.direction   =  1
    self.check_enter_SOI.__func__.direction = -1

    self.check_min_alt.__func__.terminal = True
    self.stop_condition_functions        = [ self.check_min_alt ]

    if 'min_alt' not in self.config[ 'stop_conditions' ].keys():
        self.config[ 'stop_conditions' ][ 'min_alt' ] =\
            self.cb[ 'deorbit_altitude' ]

    self.stop_conditions_map = {
        'min_alt'  : self.check_min_alt,
        'max_alt'  : self.check_max_alt,
        'enter_SOI': self.check_enter_SOI
        }

    for key in self.config[ 'stop_conditions' ].keys():
        method                   = self.stop_conditions_map[ key ]
        method.__func__.terminal = True
        self.stop_condition_functions.append( method )

` The reason for func being used can be found in this StackOverflow answer: https://stackoverflow.com/questions/53450624/hasattr-telling-lies-attributeerror-method-object-has-no-attribute-annot

For these CONS reasons, the Spacecraft class will be updated with this function, but the Fundamentals of Orbital Mechanics series will continue using a constant step-size RK4 solver because it is much more intuitive, and complex ODE solver schemes are outside of the scope of learning the fundamentals of orbital mechanics.