Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing initial state values in MPhys #321

Open
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

lamkina
Copy link
Contributor

@lamkina lamkina commented Sep 22, 2023

Purpose

This PR fixes a bug where the initial ADflow states were being set to a vector of all ones when running with MPhys. This occurred because OpenMDAO defaults the initial value of output vectors to all ones.

To fix this issue, I overwrote the _set_vectors() method of the ADflowSolver component to update the initial value of the output vector.

This PR should be merged after PRs #307 and #308 because it includes changes from both to control the print outs in the _set_vectors() method and to ensure the adjoint works during mesh warping.

Expected time until merged

A week or two, depending on PRs #307 and #308

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

We don't have testing for MPhys, but I tested this change on a handful of local test cases including BC and geometric design variables. Reviewers should test this with their own MPhys cases if possible.

Checklist

  • I have run flake8 and black 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

lamkina and others added 30 commits August 19, 2023 11:52
@lamkina lamkina requested a review from a team as a code owner September 22, 2023 22:10
@lamkina lamkina requested review from marcomangano and removed request for marcomangano September 22, 2023 22:10
@codecov
Copy link

codecov bot commented Sep 22, 2023

Codecov Report

Attention: Patch coverage is 0% with 12 lines in your changes are missing coverage. Please review.

Project coverage is 41.34%. Comparing base (63d5649) to head (78b8cc0).

Files Patch % Lines
adflow/mphys/mphys_adflow.py 0.00% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #321      +/-   ##
==========================================
- Coverage   41.45%   41.34%   -0.12%     
==========================================
  Files          13       13              
  Lines        4069     4080      +11     
==========================================
  Hits         1687     1687              
- Misses       2382     2393      +11     

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

@lamkina lamkina mentioned this pull request Sep 26, 2023
@A-CGray A-CGray self-requested a review September 27, 2023 15:00
@lamkina
Copy link
Contributor Author

lamkina commented Nov 8, 2023

bump @A-CGray @anilyil

Copy link
Contributor

@eirikurj eirikurj left a comment

Choose a reason for hiding this comment

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

@lamkina after merging #307, there seem to be still some items here that we resolved in #307. Please check.

Comment on lines 453 to 454
self.solver.setOption("printIterations", True)
self.solver.adflow.inputiteration.printbcwarnings = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems that this was retained after merging (same for above). I think you probably want to restore the previous state as done in PR #307. See here for original comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I'm not sure how I did this, but nonetheless I'll get it fixed.

Comment on lines 96 to 101
# This is a flag not exposed to the user as an option that controls
# printing the boundary condition warnings. This should only
# be used in the MPhys layer when we make multiple calls to
# updateBCAllLevels.
self.adflow.inputiteration.printbcwarnings = True

Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, this should not be here, like in PR #307. See here for original comment.

@A-CGray
Copy link
Member

A-CGray commented Nov 10, 2023

@lamkina do you have any suggestions on how I should test this?

@lamkina
Copy link
Contributor Author

lamkina commented Nov 13, 2023

I would check out the main branch and run the tutorial wing case. Print the initial state vector before ADflow runs the first analysis and you'll see it's an array of all ones. Then switch to this branch and run the same case and you should see the initial state vector is the correct one from ADflow.

You can confirm the state vector is correct by running the case with MACH only and printing the initial state vector to compare.

@A-CGray
Copy link
Member

A-CGray commented Nov 13, 2023

I'm confused, if I add code printing out the state on the line in solve_nonlinear right before the ADflow solver is called then this change seems to be working, but the initial residual printed out by ADflow is the same in both cases:

Andrew's branch

self.solver.getStates()=array([1.00000000e+00, 9.46248398e-01, 0.00000000e+00, ...,
       2.47783863e-02, 2.94800000e+00, 1.86845485e-07])
-> Alpha... 1.500000 
#
# Grid 1: Performing 1000 iterations, unless converged earlier. Minimum required iteration before NK switch:      5. Switch to NK at totalR of:   8.73E+01
#
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#  Grid  | Iter | Iter |  Iter  |   CFL   | Step | Lin  |        Res rho         |         C_lift         |        C_drag          |        totalRes        |
#  level |      | Tot  |  Type  |         |      | Res  |                        |                        |                        |                        |
#------------------------------------------------------------------------------------------------------------------------------------------------------------
      1       0      0     None     ----    ----   ----   4.7365425919241388E+03   9.5383731214195250E-04   1.2235096525765710E-01   8.7316352022811938E+06 
      1       1      3     *ANK   5.00E+00  0.15  0.001   4.1598265494843654E+03   1.7769816557941003E-02   1.4384704885726513E-01   7.7007228271569945E+06 
      1       2      8      ANK   5.00E+00  0.26  0.006   3.3403259619915884E+03   4.3599101742336291E-02   1.7417888366812920E-01   6.1704190701800054E+06 
      1       3     13      ANK   5.00E+00  0.32  0.027   2.5844088319778084E+03   6.7937514673665350E-02   2.0130232788327942E-01   4.7463852232853156E+06 
      1       4     19      ANK   5.00E+00  0.34  0.028   1.9900973496159793E+03   8.6145282454101885E-02   2.2036009068597789E-01   3.6286528693136014E+06

Main branch

self.solver.getStates()=array([1., 1., 1., ..., 1., 1., 1.])
-> Alpha... 1.500000 
#
# Grid 1: Performing 1000 iterations, unless converged earlier. Minimum required iteration before NK switch:      5. Switch to NK at totalR of:   8.73E+01
#
#------------------------------------------------------------------------------------------------------------------------------------------------------------
#  Grid  | Iter | Iter |  Iter  |   CFL   | Step | Lin  |        Res rho         |         C_lift         |        C_drag          |        totalRes        |
#  level |      | Tot  |  Type  |         |      | Res  |                        |                        |                        |                        |
#------------------------------------------------------------------------------------------------------------------------------------------------------------
      1       0      0     None     ----    ----   ----   4.7365425919241388E+03   9.5383731214195250E-04   1.2235096525765710E-01   8.7316352022811938E+06 
      1       1      3     *ANK   5.00E+00  0.15  0.001   4.1598265494843654E+03   1.7769816557941003E-02   1.4384704885726513E-01   7.7007228271569945E+06 
      1       2      8      ANK   5.00E+00  0.26  0.006   3.3403259619915884E+03   4.3599101742336291E-02   1.7417888366812920E-01   6.1704190701800054E+06 
      1       3     13      ANK   5.00E+00  0.32  0.027   2.5844088319778084E+03   6.7937514673665350E-02   2.0130232788327942E-01   4.7463852232853156E+06

~8e6 seems like a pretty reasonable initial residual, so my best guess is that there is something in ADflow's __call__ method that is overwriting the [1, 1, 1, ...] initial state anyway.

@lamkina
Copy link
Contributor Author

lamkina commented Nov 16, 2023

Huh, I was not seeing this behavior in my initial testing. It's concerning that ADflow is overwriting a state vector internally that should be coming from OpenMDAO. This isn't happening other than the initialization, but still weird.

@lamkina
Copy link
Contributor Author

lamkina commented Nov 16, 2023

@anilyil @eirikurj do either of you have any idea what could be happening here?

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.

None yet

3 participants