Search code examples
openmdao

What requirements must be met to use complex steps on a component?


What requirements must be met by a component to use complex steps to approximate partial derivatives on the component?


Solution

  • First, the function must be defined in a way such that it can accept complex input and provide a corresponding output. Many of the functions in numpy support this. One notable exception is arctan2.

    You can demonstrate this for yourself, lets use np.sin and compute its derivative with complex-step:

    >>> import numpy as np
    >>> x = 3.231662
    >>> np.sin(x)
    -0.08994761472861613
    >>> dx = 0+1E-16j
    >>> # The complex step solution
    >>> (np.sin(x+dx) - np.sin(x))/1.0E-16j
    (-0.995946497862527-0j)
    >>> # The analytic solution
    >>> np.cos(x)
    -0.995946497862527
    

    Basically, the math in your component needs to contain functions that handle complex numbers cleanly without truncating the imaginary portion. If you ever do a table lookup using only a real number, for instance, that would mean the response doesn't change if the input's imaginary part changes.

    Secondly, the functions must be complex analytic. This basically means they are continuously differentiable in both directions of the complex plane. Absolute value is one example of a function that is not complex analytic. You can read more about complex analytic functions here: https://en.wikiversity.org/wiki/Complex_analytic_function