Skip to content

MPhys improvements#404

Merged
timryanb merged 48 commits intomasterfrom
mphys-fwd
Feb 3, 2026
Merged

MPhys improvements#404
timryanb merged 48 commits intomasterfrom
mphys-fwd

Conversation

@A-CGray
Copy link
Contributor

@A-CGray A-CGray commented Jan 23, 2026

Summary of changes:

Changes to MPhys solver class

Adds forward mode implementations for solve_linear and apply_linear (for state and rhs perturbations) in the TacsSolver class. This will not enable forward mode derivative calculation (because TACS doesn't have forward mode partials with respect to DVs and node coordinates) but should allow you to use TACS within coupled Newton solvers.

I also added a few lines to apply_linear to explicitly zero the output vectors before TACS adds to them. This didn't seem to be causing any issues before, but in my experience you can't rely on OpenMDAO having zeroed those vectors.

I changed the setup of one of the MPhys integration tests to use OpenMDAO Newton and PETScKrylov nonlinear and linear solvers, with the TACS solver class's solve_linear as the linear preconditioner. This will test both forward and reverse modes of solve_linear and apply_linear.

New static problem methods

Adds static problem methods required for MPhys forward mode operations:

  • Replaces addTransposeJacVecProduct with addJacVecProduct, which can compute normal and transpose Jac Vec products
  • Adds a solveForward method, non-transposed equivalent to solveAdjoint. Solves linear system with stiffness matrix and arbitrary RHS

Code quality updates:

  • Added copyFromTACSVec and copyToTACSVec helper functions to remove a bunch of duplicate code
  • Preallocate a TACS Vec in the pyTACS class so that applyBCsToVec and applyBCsInVec don't allocate a TACS Vec every time they're called

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds limited forward-mode support for the TACS MPhys wrapper (for state and RHS perturbations) and consolidates several vector copy patterns through shared utilities, along with a minor performance improvement in pyTACS BC application.

Changes:

  • Introduces copyToTACSVec / copyFromTACSVec utilities and refactors multiple problem/system methods to use them for consistent handling of TACS Vecs, NumPy arrays, and scalars.
  • Extends StaticProblem with addJacVecProduct (unifying Jacobian/Jᵀ products) and a new solveForward method, and wires these into TacsSolver.solve_linear / apply_linear to support forward-mode linear solves in MPhys.
  • Preallocates a temporary TACS Vec in pyTACS for applying boundary conditions to NumPy arrays and updates an integration test to exercise the new Newton + user-defined linear solver path.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/integration_tests/test_mphys_struct_plate.py Tightens FD settings, switches to central differencing, and configures a Newton solver with LinearUserDefined to exercise the new forward-mode linear solve in the MPhys TACS wrapper.
tacs/utilities.py Adds numpy import and defines copyToTACSVec / copyFromTACSVec static utilities on BaseUI to centralize copying between TACS Vecs and NumPy arrays/scalars.
tacs/system.py Refactors setDesignVars and setNodes to use copyToTACSVec with unified error handling; note that the dict path in setNodes currently uses self.varName instead of self.coordName, so coordinate updates passed via dicts are ignored (commented as a bug).
tacs/pytacs.py Allocates a reusable tempVec once and reuses it in applyBCsToVec / setBCsInVec when called with NumPy arrays, avoiding repeated TACS Vec allocations.
tacs/problems/transient.py Uses copyToTACSVec in setInitConditions and copyFromTACSVec in getVariables, simplifying type handling and making the optional output arguments consistently respected.
tacs/problems/static.py Replaces addTransposeJacVecProduct with a unified addJacVecProduct (with transpose flag), adds solveForward to solve J ψ = rhs, and refactors variable getters/setters and adjoint solve to use the new copy helpers; contains minor doc typos (commented) but otherwise aligns with the new forward-mode usage.
tacs/problems/modal.py Updates getVariables to use copyFromTACSVec, simplifying returning eigenvectors into either TACS Vecs or NumPy arrays.
tacs/problems/buckling.py Similarly updates getVariables to rely on copyFromTACSVec for buckling eigenvector extraction.
tacs/mphys/solver.py Implements forward-mode solve_linear using StaticProblem.solveForward and forward-mode apply_linear using addJacVecProduct, including RHS and coordinate/DV handling; the forward RHS term currently uses d_residuals[self.states_name] instead of the RHS perturbation from d_inputs[self.rhs_name], so the forward Jacobian-vector product wrt RHS is incorrect (commented as a critical bug).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

A-CGray and others added 4 commits January 23, 2026 15:58
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@A-CGray A-CGray marked this pull request as ready for review January 23, 2026 21:37
@A-CGray A-CGray requested a review from timryanb January 23, 2026 21:37
A-CGray and others added 2 commits January 24, 2026 23:43
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@A-CGray A-CGray marked this pull request as draft January 25, 2026 18:58
@A-CGray
Copy link
Contributor Author

A-CGray commented Jan 25, 2026

@timryanb I'm marking this as a draft for now as I'd like to integrate the changes from #405 into this PR once it's merged.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@A-CGray A-CGray changed the title Add limited forward mode support in MPhys wrapper MPhys improvements Jan 30, 2026
@A-CGray A-CGray marked this pull request as ready for review January 30, 2026 17:17
@A-CGray
Copy link
Contributor Author

A-CGray commented Jan 30, 2026

@timryanb I don't see any output from the test that's failing on the real Mac-OS build and I don't have a way to recreate that environment locally. Are you able to run the test locally and see what the issue is? My best guess is something PETSc related since the same test is passing in the Complex Mac-OS build which doesn't use the PETSc linear solver.

@timryanb timryanb merged commit d50a22c into master Feb 3, 2026
13 of 22 checks passed
@timryanb timryanb deleted the mphys-fwd branch February 3, 2026 11: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.

2 participants