peterdsharpe / AeroSandbox

Aircraft design optimization made fast through modern automatic differentiation. Composable analysis tools for aerodynamics, propulsion, structures, trajectory design, and much more.
https://peterdsharpe.github.io/AeroSandbox/
MIT License
687 stars 111 forks source link

_DynamicsPointMassBaseClass.__getitem__(self, index: int) doesn't work for slices like in tutorial "06 - 3D Aircraft Dynamics for Engine-Out Analysis.ipynb" #130

Closed carsonwmoon closed 3 months ago

carsonwmoon commented 3 months ago

Bug Description / Observed Behavior

First off, this AeroSandbox library is amazing!! Secondly, a small issue. I'm just looking at tutorials now but in the 06 - 3D Aircraft Dynamics for Engine-Out Analysis.ipynb file, running it cell by cell on my computer, and in the code section quoted below it gives the error listed below.

It seems the error is due to _DynamicsPointMassBaseClass.__getitem__(self, index: int) not working/implemented for index parameters of type slice like was intended to be used in the notebook code. Perhaps this is something on my end, but since this tutorial file had worked before, perhaps there's something under the hood that has changed. And again, amazing work on this library! Very impressive stuff.

# NBVAL_SKIP

index_of_end_of_first_turn = np.nonzero(dyn.track > np.pi)[0][0]
plotter = dyn[:index_of_end_of_first_turn].draw(
    vehicle_model=airplane,
    scale_vehicle_model=1.5,  # Draw the vehicle at 1.5x scale
    show=False
)
plotter.camera.elevation = 15  # Increase the elevation angle by 15 degrees
plotter.camera.azimuth = 100
plotter.camera.zoom(1.2)
plotter.show(jupyter_backend="static")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[18], [line 4](vscode-notebook-cell:?execution_count=18&line=4)
      [1](vscode-notebook-cell:?execution_count=18&line=1) # NBVAL_SKIP
      [3](vscode-notebook-cell:?execution_count=18&line=3) index_of_end_of_first_turn = np.nonzero(dyn.track > np.pi)[0][0]
----> [4](vscode-notebook-cell:?execution_count=18&line=4) plotter = dyn[:index_of_end_of_first_turn].draw(
      [5](vscode-notebook-cell:?execution_count=18&line=5)     vehicle_model=airplane,
      [6](vscode-notebook-cell:?execution_count=18&line=6)     scale_vehicle_model=1.5,  # Draw the vehicle at 1.5x scale
      [7](vscode-notebook-cell:?execution_count=18&line=7)     show=False
      [8](vscode-notebook-cell:?execution_count=18&line=8) )
      [9](vscode-notebook-cell:?execution_count=18&line=9) plotter.camera.elevation = 15  # Increase the elevation angle by 15 degrees
     [10](vscode-notebook-cell:?execution_count=18&line=10) plotter.camera.azimuth = 100

File [c:\Users\CWM\CWM\Education\2024W\optimized_glider_project\glider-optimization\.conda\Lib\site-packages\aerosandbox\dynamics\point_mass\common_point_mass.py:201](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:201), in _DynamicsPointMassBaseClass.__getitem__(self, index)
    [189](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:189) """
    [190](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:190) Indexes one item from each attribute of a Dynamics instance.
    [191](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:191) Returns a new Dynamics instance of the same type.
   (...)
    [198](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:198) 
    [199](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:199) """
    [200](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:200) l = len(self)
--> [201](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:201) if index >= l or index < -l:
    [202](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:202)     raise IndexError("Index is out of range!")
    [204](file:///C:/Users/CWM/CWM/Education/2024W/optimized_glider_project/glider-optimization/.conda/Lib/site-packages/aerosandbox/dynamics/point_mass/common_point_mass.py:204) def get_item_of_attribute(a):

TypeError: '>=' not supported between instances of 'slice' and 'int'

Steps to Reproduce

I got a fresh download of the library, downloaded the 06 - 3D Aircraft Dynamics for Engine-Out Analysis.ipynb and cessna152.py files, and started running it cell by cell until I through the one listed, at which point it gives the error listed.

Expected Behavior

I expected it to work and result in what the online copy has here

System Information

Other Information

Nope

peterdsharpe commented 3 months ago

Thank you @carsonwmoon! This is indeed a bug, and caused by some recent changes in indexing that neglected to consider slice inputs to __getitem__() methods.

Fixed by 99b3d67c77bd5ba6da16ac453b7a1b99e54e6d60, and adds unit test to catch this failure mode in the future. Will be released along with the next ASB update.

Thanks!