Skip to content

Change interface of fem.petsc.create_vector to allow for general list of function spaces - #3694

Merged
garth-wells merged 60 commits into
FEniCS:mainfrom
schnellerhase:create_vector_from_spaces
Sep 11, 2025
Merged

Change interface of fem.petsc.create_vector to allow for general list of function spaces#3694
garth-wells merged 60 commits into
FEniCS:mainfrom
schnellerhase:create_vector_from_spaces

Conversation

@schnellerhase

@schnellerhase schnellerhase commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

The creation of PETSc vectors relies internally on a list of tuples (IndexMap, bs). Given a list of such and a vector kind we can create the corresponding coefficient vector. Previously this functionality was not exposed to the user directly and only supported indirectly by passing a Form from which the spaces would then be extracted.

  • Introduces la.petsc.create_vector which now takes a list of (IndexMap, bs) tuples and returns a PETSc.Vec object. The interface of fem.petsc.create_vector is extended to allow for function spaces as input as well. Vectors can therefore now be create by a list of function spaces: b = dolfinx.fem.create_vector([V, W]) and no longer require a form to be present for such construction. And alternatively from a general sequence of index maps and blocks sizes, without the presence of any function spaces with la.petsc.create_vector.
  • Introduces _assign_block_data which factors out the attachment of block information to PETSc vectors.
  • To keep the interface tight it is no longer possible to call create_vector on a form. Enforces explicitness, that the space is the critical argument, not the form.

@schnellerhase
schnellerhase force-pushed the create_vector_from_spaces branch from 9591f98 to 7e9c3e2 Compare April 8, 2025 21:07
Comment thread python/dolfinx/la/petsc.py Outdated
@schnellerhase schnellerhase changed the title Change interface of la.petsc.create_vector to allow for general list of function spaces Change interface of fem.petsc.create_vector to allow for general list of function spaces Apr 12, 2025
@schnellerhase
schnellerhase marked this pull request as ready for review April 12, 2025 15:19
Comment thread python/dolfinx/la/petsc.py Outdated
@francesco-ballarin francesco-ballarin added this to the 0.10.0 milestone Jul 9, 2025
Comment thread python/dolfinx/fem/petsc.py Outdated
Comment thread python/test/unit/fem/test_assembler.py Outdated
@jorgensd

jorgensd commented Sep 3, 2025

Copy link
Copy Markdown
Member

Shouldn't then this also be done for matrices to stay consistent, i.e. have an extract_function_spaces(bilinear_form), and create_matrix(nested_list_of_spaces)?

@schnellerhase

Copy link
Copy Markdown
Contributor Author

Shouldn't then this also be done for matrices to stay consistent, i.e. have an extract_function_spaces(bilinear_form), and create_matrix(nested_list_of_spaces)?

Sounds good - lets do that in another PR.

Comment thread python/test/unit/fem/test_assembler.py Outdated
@schnellerhase

schnellerhase commented Sep 10, 2025

Copy link
Copy Markdown
Contributor Author

92d1a1b adapts to very fresh ruff release https://pypi.org/project/ruff/0.13.0/

@garth-wells
garth-wells added this pull request to the merge queue Sep 11, 2025
Merged via the queue into FEniCS:main with commit b4988a9 Sep 11, 2025
16 of 17 checks passed
@michalhabera

Copy link
Copy Markdown
Contributor

Shouldn't then this also be done for matrices to stay consistent, i.e. have an extract_function_spaces(bilinear_form), and create_matrix(nested_list_of_spaces)?

Sounds good - lets do that in another PR.

Is that possible? We inspect type of integrals present in the form to prepare the sparsity pattern of the matrix, see fem::build_sparsity_pattern.

@schnellerhase
schnellerhase deleted the create_vector_from_spaces branch September 11, 2025 20:49
@schnellerhase

Copy link
Copy Markdown
Contributor Author

True matrix is integral, and thus form, dependant. If one was to switch basing the construction of a matrix on spaces, creation and sparsity pattern setting would need to be separated. That could be desirable for customisable sparsity patterns, ref #3779 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants