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

Introduce matrix slicer objects #1346

Merged
merged 56 commits into from
Mar 6, 2025
Merged

Conversation

keileg
Copy link
Contributor

@keileg keileg commented Feb 21, 2025

Proposed changes

This PR contains two main changes:

  1. A MatrixSlicer class is introduced as an alternative to creating projection matrices. Benchmarking indicates that, depending on the context, projection by the slicer is newer worse that the equivalent matrix, and it can be orders of magnitudes faster. For the subdomain and mortar projections (not yet introduced, see below), it is reasonable to expect decent performance gains of 10s of percentages.
  2. A class pp.ad.Projection is introduced as a wrapper of the MatrixSlicer in the ad framework. This Projection class has been taken into use in the geometry.e_i() method, with significant performance improvements (cut estimated to >90% of the computation time in some cases). Since the MatrixSlicer only supports a limited set of arithmetic operations, it was necessary to introduce changes to the way basis functions are summed (see new method pp.ad.sum_projection_list) and parsed (see changes to AdParser).

In addition, I have done some maintenance, mainly to matrix_operations.py and the associated tests. These files would benefit from further improvements in that regard, but I chose not to do so here.

What has not been done

I have not introduced the new projections in SubdomainProjection and MortarProjection. During development and benchmarking, and partly triggered by #1343, it became clear that we need to do a more thorough rethinking of how projections are designed (see #1345), and I therefore chose to go for a PR with limited scope.

Questions to reviewers

  • Should the MatrixSlicer be available for direct import, pp.MatrixSlicer? I'm not sure how much we will use it directly. Typing would be more elegant, though.
  • Is MatrixSlicer an okay name? It can slice more than matrices.

EDIT: In light of the centralized projection storage discussed in #1346, pp.ad.Projection may not be the best name for the class mentioned in point 2 in the above list. Naming suggestions are much appreciated.

Types of changes

What types of changes does this PR introduce to PorePy?
Put an x in the boxes that apply.

  • Minor change (e.g., dependency bumps, broken links).
  • Bugfix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).
  • Testing (contribution related to testing of existing or new functionality).
  • Documentation (contribution related to adding, improving, or fixing documentation).
  • Maintenance (e.g., improve logic and performance, remove obsolete code).
  • Other:

Checklist

Put an x in the boxes that apply or explain briefly why the box is not relevant.

  • The documentation is up-to-date.
  • Static typing is included in the update.
  • This PR does not duplicate existing functionality.
  • The update is covered by the test suite (including tests added in the PR).
  • If new skipped tests have been introduced in this PR, pytest was run with the --run-skipped flag.

Also renamed slice_mat -> slice_sparse_matrix
…ws/columns

Changed the test parameters somewhat, but the effective test coverage should be the same
The method for slicing matrices was changed some commits ago,
but this line was not updated
When the projection is onto the range, native scipy slicing is much
more efficient than the tailored approach
The new definition is consistent with the representation of the slicer as a projection matrix

Also updated the test
The main improvement is to let the right multiplication and division method return a copy of the object
For some reason, one of the lists of tested and testable methods has been shuffled. This is potentially because the order in which the constitutive laws are accessed has changed
(due to changes done during introduction of the MatrixSlicer, though EK does not understand exactly how).

There is no reason why the two lists should be arranged identically, so sorting should be okay.
This should be significantly faster than the old sparse matrix constructed through a Kronecker product
@keileg
Copy link
Contributor Author

keileg commented Mar 4, 2025

Thanks for the thorough review. I believe I have addressed most of the comments now, most of those not yet resolved can hopefully be put to rest quite soon.

In terms of functionality, the main change since the initial PR is that the ArraySlicer now also can be used to broadcast ad scalars. This was needed for a unified treatment of the variable c_num_as_scalar, which can be a scalar or a DenseAdArray (I can confirm that the latter works, at least on a parsing-technical level). Tests have been updated and documentation improved.

You suggest that we somehow force the test of ArraySlicer to be updated if new functionality is introduced to this class. I agree this is a good idea, but I am blank on how to do that. If you have concrete ideas, I'll have a go, if not, I am afraid we will have to rely on self discipline here. The good news is, there should not be a need to update this class frequently (fingers crossed).

@keileg keileg requested a review from IvarStefansson March 4, 2025 10:19
Copy link
Contributor

@IvarStefansson IvarStefansson left a comment

Choose a reason for hiding this comment

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

Some answers and some resolved.

@keileg
Copy link
Contributor Author

keileg commented Mar 5, 2025

Ready for re-review. Main changes:

  1. I made the ArraySlicer usable as a right operand for the full set of arithmetic operations. In this way, we can take the class more broadly into use in the code (I have the feeling this can be good for Mpfa, for instance).
  2. I significantly upgraded the test, and improved documentation, of the test of delayed evaluation. As explained in a conversation, I do not think that coverage of further cases is relevant now.

@keileg keileg requested a review from IvarStefansson March 5, 2025 10:53
@keileg keileg merged commit 8220bef into pmgbergen:develop Mar 6, 2025
6 checks passed
@keileg keileg deleted the matrix_slicer branch March 6, 2025 11:38
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