-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data independent form compilation for Python interface (#3263)
* Create mesh independent form compilator for Python interface * Ruff formatting * Mypy * Parameterize test over all dtypes * Ruff format * Fix compile options handling * Fix coeff and constant name mapping * Ruff * Remove extra definition of basix element and check that mesh c_el is consistent * Add support for linear and bilinear forms * Fix typo * Apply suggestions from code review Co-authored-by: Chris Richardson <[email protected]> * Apply suggestions from code review Co-authored-by: Chris Richardson <[email protected]> * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Igor Baratta <[email protected]> * Apply suggestions from code review * Add documentation and default scalar type --------- Co-authored-by: Chris Richardson <[email protected]> Co-authored-by: Igor Baratta <[email protected]>
- Loading branch information
1 parent
f72fcb9
commit 17c71a3
Showing
4 changed files
with
297 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
56 changes: 56 additions & 0 deletions
56
python/test/unit/fem/test_assemble_mesh_independent_form.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from mpi4py import MPI | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import basix.ufl | ||
import dolfinx | ||
import ufl | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dtype", | ||
[ | ||
np.float32, | ||
np.float64, | ||
pytest.param(np.complex64, marks=pytest.mark.xfail_win32_complex), | ||
pytest.param(np.complex128, marks=pytest.mark.xfail_win32_complex), | ||
], | ||
) | ||
def test_compiled_form(dtype): | ||
""" | ||
Compile a form without an associated mesh and assemble a form over a sequence of meshes | ||
""" | ||
real_type = dtype(0).real.dtype | ||
c_el = basix.ufl.element("Lagrange", "triangle", 1, shape=(2,), dtype=real_type) | ||
domain = ufl.Mesh(c_el) | ||
el = basix.ufl.element("Lagrange", "triangle", 2, dtype=real_type) | ||
V = ufl.FunctionSpace(domain, el) | ||
u = ufl.Coefficient(V) | ||
w = ufl.Coefficient(V) | ||
c = ufl.Constant(domain) | ||
e = ufl.Constant(domain) | ||
J = c * e * u * w * ufl.dx(domain=domain) | ||
|
||
# Compile form using dolfinx.jit.ffcx_jit | ||
compiled_form = dolfinx.fem.compile_form( | ||
MPI.COMM_WORLD, J, form_compiler_options={"scalar_type": dtype} | ||
) | ||
|
||
def create_and_integrate(N, compiled_form): | ||
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, N, N, dtype=real_type) | ||
assert mesh.ufl_domain().ufl_coordinate_element() == c_el | ||
Vh = dolfinx.fem.functionspace(mesh, u.ufl_element()) | ||
uh = dolfinx.fem.Function(Vh, dtype=dtype) | ||
uh.interpolate(lambda x: x[0]) | ||
wh = dolfinx.fem.Function(Vh, dtype=dtype) | ||
wh.interpolate(lambda x: x[1]) | ||
eh = dolfinx.fem.Constant(mesh, dtype(3.0)) | ||
ch = dolfinx.fem.Constant(mesh, dtype(2.0)) | ||
form = dolfinx.fem.create_form(compiled_form, [], mesh, {u: uh, w: wh}, {c: ch, e: eh}) | ||
assert np.isclose(mesh.comm.allreduce(dolfinx.fem.assemble_scalar(form), op=MPI.SUM), 1.5) | ||
|
||
# Create various meshes, that all uses this compiled form with a map from ufl | ||
# to dolfinx functions and constants | ||
for i in range(1, 4): | ||
create_and_integrate(i, compiled_form) |