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

Unit testing tolerance improvements + Von Mises sensitivity fix #216

Merged
merged 29 commits into from
Jun 29, 2023

Conversation

A-CGray
Copy link
Contributor

@A-CGray A-CGray commented Jun 1, 2023

In unit tests we currently use an essentially infinite absolute tolerance and a very large relative tolerance. This is done because it is very hard to specify a single absolute tolerance that is valid for test values that span multiple orders of magnitude, and relative errors can be very high even for accurate derivatives when the test values are very small.

This PR fixes this issue by implementing a new kind of check between test and reference values. In doing so, the constitutive unit tests now pass with tolerances 1000x tighter than before.

The check now performed is identical to that used by numpy's assert_allclose method:

fail = abs( test - ref ) > atol + rtol * abs(ref)

Where ref is the finite-difference or complex-step value and test is the analytic value

When abs(ref) >> atol/rtol this check acts like a relative tolerance check, but for abs(ref) << atol/rtol it acts like an absolute tolerance check, thus avoiding the issues associated with relative tolerances of very small values.

Changes:

  • Add a TacsAssertAllClose method
  • Switch to using TacsAssertAllClose for all constitutive model unit tests
  • Reduce relative tolerance from 1e-1 to 1e-4
  • Reduce absolute tolerance from 1e99 to 1e-5*self.rtol clipped to be within 1e-8 - 1e-14
  • Reduce finite difference step size from 1e-6 to 1e-8
  • Add absolute error to output of TacsPrintErrorComponents
  • Reduce complex-step step size from 1e-50 to 1e-200
  • Reduce magnitude of strains used in constitutive tests from 1 to 1e-3
  • Fix bug in Von Mises strain sensitivity that was previously missed when testing with huge strains

@timryanb timryanb requested review from gjkennedy, markleader and timryanb and removed request for markleader June 1, 2023 20:02
@A-CGray
Copy link
Contributor Author

A-CGray commented Jun 1, 2023

I think this is good to go now

@timryanb
Copy link
Collaborator

timryanb commented Jun 2, 2023

@A-CGray we should probably add these improvements to the element tests too for consistency?

@A-CGray
Copy link
Contributor Author

A-CGray commented Jun 2, 2023

OK, I've switched over the elements tests to use TacsAssertAllClose, I changed the absolute tolerance for all the tests that previously used an absolute tolerance of 1e99 but there are other tests that use lower absolute tolerances that I haven't changed yet.

There is currently one failing test in the pcm_heat_conduction_2d elements, but I think it is a genuine failure. The reason this wasn't caught before is that relative errors used to be computed with the analytic derivatives as the reference value (relError = (analytic - FD)/analytic) and the error is treated as zero if the analytic value as zero. I've now changed the relative error computation so that the FD or CS value is treated as the reference, which results in an error in this case.

I think the error here is correct, but maybe @markleader knows more.

Testing the Jacobian of the weak form for model TACSObject
Max Err: 4.9862e-02 in component 6.
Max REr: 1.0000e+00 in component 5.
Val[   ]        Analytic     Approximate      Rel. Error      Abs. Error
Jac[  0]    1.095718e+00    1.095718e+00      0.0000e+00      0.0000e+00
Jac[  1]    1.829044e-01    1.829044e-01      1.5175e-16      2.7756e-17
Jac[  2]    0.000000e+00    0.000000e+00
Jac[  3]    0.000000e+00    0.000000e+00
Jac[  4]    1.829044e-01    1.829044e-01      1.5175e-16      2.7756e-17
Jac[  5]    0.000000e+00   -3.355847e-04      1.0000e+00      3.3558e-04
Jac[  6]    0.000000e+00   -4.986198e-02      1.0000e+00      4.9862e-02
Jac[  7]    0.000000e+00   -1.625886e-02      1.0000e+00      1.6259e-02

tests/element_tests/test_pcm_heat_conduction_2d.py:ModelTest.test_element_model_jacobian  ... FAIL (00:00:0.00, 187 MB)

@A-CGray A-CGray changed the title Unit testing tolerance improvements Unit testing tolerance improvements + Von Mises sensitivity fix Jun 3, 2023
@A-CGray
Copy link
Contributor Author

A-CGray commented Jun 8, 2023

@markleader do you have any insight on the failing PCM test?

@markleader
Copy link
Contributor

@markleader do you have any insight on the failing PCM test?

@A-CGray Started looking into it today. Sorry for the delay. I haven't figured it out yet but I'll check in again soon

@markleader
Copy link
Contributor

@A-CGray This should be fixed now with the PR I just submitted. Take a look and let me know

@A-CGray
Copy link
Contributor Author

A-CGray commented Jun 13, 2023

Great, thanks @markleader !

@A-CGray
Copy link
Contributor Author

A-CGray commented Jun 26, 2023

@gjkennedy can you merge this if you're happy with the changes

@gjkennedy gjkennedy merged commit 02149fb into master Jun 29, 2023
@gjkennedy gjkennedy deleted the TestImprovements branch June 29, 2023 18:56
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.

4 participants