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

Add pybindings for Jacobian diagnostic #4685

Merged
merged 2 commits into from
Feb 3, 2023

Conversation

geoffrey4444
Copy link
Contributor

Proposed changes

Add python bindings to the jacobian diagnostic.

Upgrade instructions

Code review checklist

  • The code is documented and the documentation renders correctly. Run
    make doc to generate the documentation locally into BUILD_DIR/docs/html.
    Then open index.html.
  • The code follows the stylistic and code quality guidelines listed in the
    code review guide.
  • The PR lists upgrade instructions and is labeled bugfix or
    new feature if appropriate.

Further comments

TargetFrame>&,
const tnsr::I<DataVector, Dim, TargetFrame>&, const ::Mesh<Dim>&)>(
&::domain::jacobian_diagnostic<Dim, TargetFrame>),
py::arg("jacobian"), py::arg("mapped_coords"), py::arg("mesh"));
Copy link
Member

Choose a reason for hiding this comment

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

Name the second arg TargetFrame == Frame::Inertial ? "inertial_coords" : "grid_coords". Then the py script will get the inertial coords from the volume data file by default, without having to specify the dataset name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! I didn't know these were automagically available.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't compile @nilsvu ... I get this error:

/Users/geoffrey/Codes/spectre/spectre/src/Domain/Python/JacobianDiagnostic.cpp:31:15: error: 'TargetFrame' does not refer to a value
      py::arg(TargetFrame == Frame::Inertial ? "inertial_coords"
              ^
/Users/geoffrey/Codes/spectre/spectre/src/Domain/Python/JacobianDiagnostic.cpp:21:32: note: declared here
template <size_t Dim, typename TargetFrame>
                               ^
/Users/geoffrey/Codes/spectre/spectre/src/Domain/Python/JacobianDiagnostic.cpp:31:46: error: expected '(' for function-style cast or type construction
      py::arg(TargetFrame == Frame::Inertial ? "inertial_coords"

Is there a way to do this with std::is_same? I tried but didn't get anything to build

Copy link
Member

Choose a reason for hiding this comment

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

Right, frames are structs and no enums. std::is_same_v should work.

jac_diag = jacobian_diagnostic(jac, mapped_coordinates, mesh)
expected_jac_diag = tnsr.I[DataVector, 1,
Frame.ElementLogical](num_points=4, fill=0.)
self.assertAlmostEqual(jac_diag[0], expected_jac_diag[0])
Copy link
Member

Choose a reason for hiding this comment

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

Does this work? You're comparing arrays, so I'd do import numpy.testing as npt and npt.assert_allclose(jac_diag, expected).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Works because it's in 1D, so only one component. I did not know about assert_allclose, but I'm happy to switch to that!

Copy link
Member

Choose a reason for hiding this comment

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

Only one component but 4 points. Not sure what exactly happens without all_close.

@geoffrey4444
Copy link
Contributor Author

@nilsvu is the test failure something I messed up? Here's the error:

Traceback (most recent call last):
[654](https://github.com/sxs-collaboration/spectre/actions/runs/4077683748/jobs/7027002359#step:12:655)
  File "/__w/spectre/spectre/tests/Unit/Domain/Python/Test_JacobianDiagnostic.py", line 4, in <module>
[655](https://github.com/sxs-collaboration/spectre/actions/runs/4077683748/jobs/7027002359#step:12:656)
    from spectre.Domain import jacobian_diagnostic
[656](https://github.com/sxs-collaboration/spectre/actions/runs/4077683748/jobs/7027002359#step:12:657)
  File "/work/build/bin/python/spectre/Domain/__init__.py", line 4, in <module>
[657](https://github.com/sxs-collaboration/spectre/actions/runs/4077683748/jobs/7027002359#step:12:658)
    from ._PyDomain import *
[658](https://github.com/sxs-collaboration/spectre/actions/runs/4077683748/jobs/7027002359#step:12:659)
ModuleNotFoundError: No module named 'spectre.Domain._PyDomain'

I don't see how I could have broken this or why only this one test failed, but maybe I'm missing something?

@nilsvu
Copy link
Member

nilsvu commented Feb 3, 2023

@geoffrey4444 the failing build is the one with pybindings disabled. It fails the C++ test because it uses the same Python file for random-value tests as the Python test, which imports the Domain pybindings. Just keep the random-value test in a separate file, not in the Python/ subdirectory.

@geoffrey4444 geoffrey4444 force-pushed the pybind_jacdiag branch 2 times, most recently from 7c705aa to ec43e1e Compare February 3, 2023 15:39
@@ -31,6 +31,12 @@ spectre_add_python_bindings_test(
"Unit;Domain"
PyDomain)

spectre_add_python_bindings_test(
"Unit.Domain.Python.JacobianDiagnostic"
Test_JacobianDiagnosticPythonBinding.py
Copy link
Member

Choose a reason for hiding this comment

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

Please just name this Test_JacobianDiagnostic.py and move the other file up one directory level. Also, name the other file just JacobianDiagnostic.py. That's what we do for all other random-value tests.

@geoffrey4444
Copy link
Contributor Author

I don't think the macOS CI failure is related to this PR...it complains about some packages like hdf5 not being installed

@nilsvu nilsvu enabled auto-merge February 3, 2023 19:19
@nilsvu nilsvu changed the title Pybind jacdiag Add pybindings for Jacobian diagnostic Feb 3, 2023
@nilsvu nilsvu merged commit 1c06e9d into sxs-collaboration:develop Feb 3, 2023
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.

2 participants