Skip to content

Add optimization test with FD derivatives - #487

Open
ewu63 wants to merge 12 commits into
mainfrom
add-opt-FD-test
Open

Add optimization test with FD derivatives#487
ewu63 wants to merge 12 commits into
mainfrom
add-opt-FD-test

Conversation

@ewu63

@ewu63 ewu63 commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Purpose

Addresses part of #256.

Expected time until merged

A few days.

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (non-backwards-compatible fix or feature)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Documentation update
  • Maintenance update
  • Other (please describe)

Testing

Checklist

  • I have run ruff check and ruff format to make sure the Python code adheres to PEP-8 and is consistently formatted
  • I have formatted the Fortran code with fprettify or C/C++ code with clang-format as applicable
  • I have run unit and regression tests which pass locally with my changes
  • I have added new tests that prove my fix is effective or that my feature works
  • I have added necessary documentation

@ewu63
ewu63 requested a review from marcomangano as a code owner June 16, 2026 05:51
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.22%. Comparing base (f0393e2) to head (2de841d).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #487   +/-   ##
=======================================
  Coverage   54.22%   54.22%           
=======================================
  Files           1        1           
  Lines         225      225           
=======================================
  Hits          122      122           
  Misses        103      103           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@marcomangano

Copy link
Copy Markdown
Collaborator

Which one should be merged first between this PR and #486 ? Both add the same two new files, does it make sense attach them to one PR over the other?

@ewu63

ewu63 commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator Author

This is on top of #486, let's wait for that one first.

@marcomangano
marcomangano requested a review from A-CGray July 2, 2026 16:51

@A-CGray A-CGray left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same overall comment as #486, can you add docstrings for new tests explaining what they are testing?

Also, is there a preferred order in which we should merge this and #486 ?

Comment thread pyoptsparse/pyOpt_gradient.py Outdated
Comment on lines +48 to +51
# Complex step divides by the imaginary part of the step, so a purely
# real step would silently yield NaN gradients.
if self.sensType == "cs" and np.imag(self.sensStep) == 0:
raise ValueError(f"The complex step size must have a nonzero imaginary part, got {self.sensStep}.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says that sensStep is the step size, and should be a float. So we need to either

  1. Update the docstring
  2. Change the implementation so that we take in a float and do self.sensStep = 1j * sensStep when using complex-step

Option 2 would be my slight preference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if we keep the implementation as is and update the docstring, shouldn't we also check that sensStep is purely imaginary (i.e np.real(self.sensStep) == 0?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer not to change any code behaviour (other than additional error checking as done here). The "correct" path can be updated later on if we want to, but definitely require a separate PR and more documentation. I will update the docstring to be more clear, but yes as coded the user needs to pass the actual complex perturbation. In practice, the default of 1e-40j should work for almost all scenarios.

@ewu63

ewu63 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator Author

This PR should be merged after #486, I will rebase it after that one is merged.

Comment thread tests/test_sphere.py
product(ALL_OPTIMIZERS, ["fd", "fdr", "cd", "cdr", "cs"]),
name_func=lambda f, n, p: f"{f.__name__}_{p.args[0]}_{p.args[1]}",
)
def test_optimization_approx_deriv(self, optName, sens):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only new test added here, the rest are from the other branch (the diff will look better once the other PR is merged).

A-CGray
A-CGray previously approved these changes Aug 22, 2026
@ewu63

ewu63 commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator Author

Sorry had to rebase --onto since we squash PRs. Need another approval @A-CGray. I plan to merge #485 after this one and make a new release.

A-CGray and others added 2 commits August 21, 2026 23:35
Updated sensitivity type descriptions to use lowercase and clarified the sensStep parameter details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants