RocketPy-Team / RocketPy

Next generation High-Power Rocketry 6-DOF Trajectory Simulation
https://docs.rocketpy.org/
MIT License
570 stars 137 forks source link

ENH: Complex step differentiation #594

Closed Gui-FernandesBR closed 1 month ago

Gui-FernandesBR commented 1 month ago

Pull request type

Checklist

Current behavior

The Function.differentiate() method is the only method that can be used to derivate Function objects.

New behavior

The new, powerful, and fast Function.differentiate_complex_step() method is here! This method will be used to speed up simulations in future PRs.

Breaking change

Additional information

This PR will close the #131 issue, a really old feature request of our repository.

codecov[bot] commented 1 month ago

Codecov Report

Attention: Patch coverage is 83.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 73.31%. Comparing base (92e93a7) to head (fc6804c). Report is 2 commits behind head on develop.

Files Patch % Lines
rocketpy/mathutils/function.py 83.33% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #594 +/- ## =========================================== - Coverage 73.35% 73.31% -0.05% =========================================== Files 57 57 Lines 9429 9436 +7 =========================================== + Hits 6917 6918 +1 - Misses 2512 2518 +6 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Gui-FernandesBR commented 1 month ago

Really good!

Questions:

  1. Are there any benefits to using the old differentiation method? Or is this one just better?

Yes, this new method only works if the function accepts complex numbers that presents an imaginary part. Also, it is required that the function should be "differentiable in complex dimension", but this is not practical insight. Therefore, the old method should still be used sometimes.

  1. Have you tried plugging this in the flight class? Does it make a difference in terms of performance? If so, why not do it in this PR?

Yes, it helps the flight class to go faster. The goal of this PR was to implement a complex step differentiation method, nothing more. As I mentioned in the PR description: "This method will be used to speed up simulations in future PRs."

  1. Could you add second-order differentiation to this?

Let me try to implement it the next weekend.

Gui-FernandesBR commented 1 month ago

@MateusStano let's forget about the second order differentiation, it was giving some errors that I really think are not worth it to debug right now.

The tests are already covering basic scenarios and I know for a fact that the flight simulations are running correctly when using the complex step method (1st order).

I would leave the second order for a future PR.

Could you approve this one please?

MateusStano commented 1 month ago

The tests are already covering basic scenarios and I know for a fact that the flight simulations are running correctly when using the complex step method (1st order).

I would leave the second order for a future PR.

Could you approve this one please?

Okay. One more thing that would be good here is using complex diff for derivative_function. That might make a big difference for liquids/hybrids simulations