Hi. I'm having some trouble trying to determine the best gear a car should be in to maximize acceleration.

I'm not looking for the perfect point at which to change gears to maximize acceleration.

I can get the current rpm of the car, the current speed, the current torque, optimal RPM's for gear ratios, engine torque for rpm, rpm from speed, and gear ratios.

I'm just not sure how to figure out what gear I should be in to get the best acceleration possible.

for example:

say I'm driving my car, I manage to shift all the way to 5th gear, going very fast. I suddenly crash, my speed reduces (but not to 0), I'm still in 5th gear. My acceleration now is very small, I should change gears, but to which gear in order to accelerate the fastest?

another example:

say I'm in 5th gear travelling very fast, I accidentally shift into 2nd, the rpm's rise quickly to the redline, my speed drops. What gear should I change into so that my engine doesn't explode and my acceleration will continue to increase?