Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Double

I need to avert permitting a type double variable from exceeding the range limits for that data type during a Gaussian, numerical integration.

I need some means of predicting that the variable is going to infinity due to a time step that is too large, and reducing the time step after some test to determine if the time step needs to be smaller, while expanding it again so the integration doesn't take forever in more stable regions. (Right now the variable simply goes to infinity. Simply reducing the time step would probably leave me in need of a supercomputer to run the entire numerical integration.)

I will ask here, and consult my numerical methods text as well, hoping for some useful insights relative to C++.

Thank you.

Re: Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Do

Quote:

Originally Posted by

**Protocol**
I need some means of predicting that the variable is going to infinity due to a time step that is too large, and reducing the time step after some test to determine if the time step needs to be smaller, while expanding it again so the integration doesn't take forever in more stable regions. (Right now the variable simply goes to infinity. Simply reducing the time step would probably leave me in need of a supercomputer to run the entire numerical integration.)

Can't you reduce the time step after the result goes to infinity and roll back the last computation, or redo the whole computation?

Re: Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Do

The "explosion" somehow forces the logic of the software to drop out of the function performing the computation. The variable "blows up" in a function loop, and I need to identify how to detect a variable that has just gone to infinity based upon some characteristic of the variable.

Yes, I can test for an empty position in the output vector (a zero value), and if I find one, restart the loop with a smaller time step. My question here is a little more subtle than that. I'm seeking some deeper mathematical insight into how to avoid merely adjusting the time step down to something on the order of 10^ -10 seconds in a simulation over billions of years and waiting for the world to end before the computation is done.

I could detect the "blow-up" via the output vector test, then adjust the time-step down by a factor of ten until the "blow-up" doesn't occur, after resetting variables appropriately, then re-set the time-step to the "normal" value if the loop completes without a problem.

Testing the products, numerators, and denominators might be a useful alternative in terms of the exponential terms, to see if they can add up to a problematic value. I could reduce the time-step if the exponents seem to present a problem.

I'm going to attempt a 4th order Runge-Kutta method first, since my initial Euler technique is known to produce errors, which could generate instability if the errors are large enough. (I was hoping for a quick and dirty solution that would work.)

Thanks.

Re: Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Do

I'm attempting to use a "try-catch" construct to try to compute a value and assign it to a variable, intending to "catch" any result that exceeds the defined magnitude limits of the variable. If I'm using a type "double" for the variable, could someone please provide some insight relative to how to detect a computation that exceeds the limits of a "double"? Thank you.

Re: Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Do

Quote:

Originally Posted by

**Protocol**
I'm attempting to use a "try-catch" construct to try to compute a value and assign it to a variable, intending to "catch" any result that exceeds the defined magnitude limits of the variable. If I'm using a type "double" for the variable, could someone please provide some insight relative to how to detect a computation that exceeds the limits of a "double"? Thank you.

Not with an exception, this is C++. Compare the value with std::numeric_limits<double>::infinity().

Re: Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Do

Thanks!

I'm now thinking of attempting to use a Boolean test of all three equations that may blow up. If any one fails using the comparison you suggest, I'll call a function that halves the time step. On the next line of the "if" construct (within the brackets) I insert a "goto" statement that drops to the bottom of the "while" loop to initiate another test of the time step on the next loop through. Only when the "if" statement is false do I proceed through the loop normally. (I hate to use a "goto" statement in C++, but it seems the most straightforward solution.)

Re: Need Variable Time Step to Avert Unstable Numerical Integ. Exceeding Limits of Do

Quote:

Originally Posted by

**Protocol**
On the next line of the "if" construct (within the brackets) I insert a "goto" statement that drops to the bottom of the "while" loop to initiate another test of the time step on the next loop through. Only when the "if" statement is false do I proceed through the loop normally.

In C++, there is no situation in which using 'goto' makes code more readable. There's always a better alternative.

What you describe is exactly what the 'continue' keyword does. However, I find that makes code almost as unreadable as goto does and would avoid it. Why not just write an else clause?