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
690 stars 111 forks source link

Avoid comparing float to integer #23

Closed KikeM closed 4 years ago

KikeM commented 4 years ago

In the forces calculation you have this line of code:

https://github.com/peterdsharpe/AeroSandbox/blob/0bcc01ccaa3b38dd943cac52b17df777c08faa6a/aerosandbox/aerodynamics/casvlm1.py#L451

I think it is better to use np.isclose, as it is more robust to finding zeroes and to NaN values.

self.CL_over_CDi = cas.if_else(np.islclose(self.CDi, tol_rel, tol_abs), 0, self.CL / self.CDi)

Reference: https://docs.scipy.org/doc/numpy/reference/generated/numpy.isclose.html

peterdsharpe commented 4 years ago

Ah - thanks for bringing this up!

This is a great suggestion and I totally see where you're coming from! Unfortunately, using np.isclose or any other numpy feature breaks automatic differentiability within CasADi ("cas", the AD engine that I've used throughout AeroSandbox v1.0.0+), which is important for coupled analysis and design optimization! In theory, it would be possible to recreate this effect with a series of nested cas.if_else() statements, though - perhaps that's something I'll look into!

Another consideration is that realistic (optimized) designs (with some tacked-on profile drag) will almost never have CD close to zero, as the point of CL/CD_max is often fairly high up on the CD vs. CL polar. This line is really just there to keep the optimizer from NaNing with a divide-by-zero error in the middle of an optimization run.

Because of this, I think it's probably best to keep the line as written. However, feel free to change it in your local copy if you prefer (especially if you're not using AeroSandbox's built-in optimization tools) - there's the beauty of open-source code! :)