Generic LinearOperator.toarray() and tosparse() defaults - #89
Merged
Merged
Conversation
Replace the abstract LinearOperator.toarray() by a default implementation that assembles the global matrix column by column from dot(e_j). Works for any operator (also matrix-free) on Stencil/(nested) Block vector spaces, in serial and parallel (full matrix on every rank), dense or scipy.sparse. Ported and improved from struphy's LinOpWithTransp.toarray_struphy(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tosparse(format='csr') is no longer abstract and defaults to the generic LinearOperator.toarray(is_sparse=True). Remove the toarray/tosparse overrides that only raised NotImplementedError (MatrixFreeLinearOperator, InverseLinearOperator, ComposedLinearOperator.toarray, PowerLinearOperator, DistributedFFTBase, KroneckerLinearSolver), so the generic methods apply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
|
Paired struphy PR (runs the struphy test suite against this branch): struphy-hub/struphy#662 |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Serial and parallel tests on Stencil, Block and nested Block domains, all sparse formats, in-place output, composed/power/inverse operators and invalid input, against the explicit StencilMatrix/BlockLinearOperator. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
Author
max-models
approved these changes
Oct 2, 2026
spossann
added a commit
to struphy-hub/struphy
that referenced
this pull request
Oct 3, 2026
…OpWithTransp (#662) **Solves the following issue(s):** Paired with struphy-hub/feectools#89. The feectools submodule points to that PR's branch, so struphy's tests run against it. **Core changes:** - `LinOpWithTransp` is removed. Its only extra was `toarray_struphy()`, since `transpose` is already abstract in feectools. That method is now the default `LinearOperator.toarray()` in feectools (feectools#89), so all struphy operators subclass `LinearOperator` directly. - Removed `toarray`/`tosparse` overrides that only raised `NotImplementedError` (several declared as properties) or just forwarded to `toarray(is_sparse=True)`. These classes now use the feectools defaults: the basis projection operators, `StencilMatrixFreeMassOperator`, `AverageOperator`, the preconditioners, projectors, variational transport operators, polar operators, `BoundaryOperator`, and `GT_MAT_G`. - Tests that used `toarray_struphy()` now call `LinearOperator.toarray(M, ...)`. - Feectools submodule bumped to the feectools#89 branch. **Model-specific changes:** None **Documentation changes:** None **Before merging:** merge feectools#89 into `devel-tiny` first, then move the submodule to the new `devel-tiny` head. Until then the "feectools submodule freshness" check is expected to fail. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Core changes:
LinearOperator.toarray(out=None, is_sparse=False, format='csr')is no longer abstract. The default builds the global matrix one column at a time, asdot(e_j), so it works for any operator, including matrix-free ones. It supports StencilVectorSpace domains and (possibly nested) BlockVectorSpace domains made of them. It runs in serial and in parallel (every rank gets the full matrix) and returns either a dense array or a scipy.sparse matrix. This is ported from struphy'sLinOpWithTransp.toarray_struphy(), with these improvements:Allreduce(IN_PLACE)), so no second full-size array is allocated;allgatherper array, and the format is converted withcoo_matrix.asformat();LinearOperator.tosparse(format='csr')is no longer abstract either. It defaults toLinearOperator.toarray(is_sparse=True).toarray/tosparseoverrides that only raisedNotImplementedError, so the defaults apply:MatrixFreeLinearOperator,InverseLinearOperator,ComposedLinearOperator.toarray,PowerLinearOperator,DistributedFFTBase,KroneckerLinearSolver.The default costs one
dotper degree of freedom, so it's meant for testing and small problems. Classes that store an explicit matrix keep their own faster versions.Testing:
StencilMatrix/BlockLinearOperator(including a nested block), periodic and non-periodic, all 7 sparse formats, plus matrix-free, composed, power and CG-inverse operators. Results matched in serial and with 2 and 4 MPI ranks.devel-tiny(the only failures are the existing petsc ones).See this struphy PR: struphy-hub/struphy#662
🤖 Generated with Claude Code