-
Notifications
You must be signed in to change notification settings - Fork 17
make getform work (without DirichletBCs) for DOLFINx #200
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
Open
jorgensd
wants to merge
74
commits into
firedrakeproject:master
Choose a base branch
from
jorgensd:dokken/getForm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
74 commits
Select commit
Hold shift + click to select a range
9031a1d
make getform work (without bcs).
jorgensd f5cbeea
Some minor logic fixes in BoundsConstraindDirichletBC
jorgensd e292ed6
Merge remote-tracking branch 'upstream' into dokken/getForm
jorgensd 547c0d2
Start reverting to Pablo's suggestions.
jorgensd ad84b2b
Fix bcs for firedrake (revert to proper functionality as well as star…
jorgensd 60ed4ee
Add try accept for fml
jorgensd 301d79f
Further work on the irksome DOLFINx interface
jorgensd d5fd814
Apply suggestions from code review
jorgensd 2d3c3b7
Apply suggestions from code review
jorgensd 8b5a652
Apply suggestions from code review
jorgensd 9905eab
Rename create_linear and nonlinear var solver to create_*_solver.
jorgensd b804f65
Add backend to most time steppers after moving replace to backend
jorgensd 5ecb387
Some more fixes to ensure that that firedrake tests run.
jorgensd 134da90
Further fixes to replace from backend
jorgensd ef38d2a
Merge branch 'master' into dokken/getForm
pbrubeck dcd835c
Apply suggestions from code review
pbrubeck a2da34b
Apply suggestion from @pbrubeck
pbrubeck 752351e
Apply suggestion from @pbrubeck
pbrubeck a06b382
Apply suggestions from code review
pbrubeck 3361f0b
Remove backend replace and some other minor fixes to get firedrake te…
jorgensd 0d039d2
Apply suggestions from code review
pbrubeck 9a0080f
Apply suggestion from @pbrubeck
pbrubeck a673fc5
Apply suggestion from @pbrubeck
pbrubeck f7530e0
Apply suggestion from @pbrubeck
pbrubeck 70680b6
Update DOLFINx solver to new API and make a try except on firedrake fml
jorgensd 31c50ff
Revert formatting
jorgensd b4e56fe
Revert formatting
jorgensd c249980
Apply suggestions from code review
jorgensd 291fb2b
Revert formatting
jorgensd 6162838
Revert init file
jorgensd ac2fa25
Apply suggestions from code review
jorgensd 33a27cd
Apply suggestions from code review
jorgensd 0244bc4
Revert labelling logic
jorgensd 705f9e8
Apply suggestions from code review
jorgensd 4e4b15c
Apply suggestions from code review
jorgensd a4cc88f
Revert formatting file to main state
jorgensd 9bcaf3c
Remove breakpoint
jorgensd 0a99dbd
Apply suggestions from code review
pbrubeck c5960c3
Apply suggestion from @pbrubeck
pbrubeck 380f15c
Simplify import and propagate backend
jorgensd ebbd629
Add note regarding storage convention
jorgensd b9c3872
Apply suggestions from code review
jorgensd 9fbc12f
Fix naming
jorgensd ea00e3c
Introduce backend to nystrok dirk time-stepper
jorgensd 72fde72
Apply suggestions from code review
pbrubeck 97b82d1
Apply suggestion from @pbrubeck
pbrubeck 0ebf19d
Apply suggestions from code review
pbrubeck 957a39b
Apply suggestions from code review
pbrubeck 21c1a9a
Minor fixes
jorgensd 7f3572d
Revert ruff formatting
jorgensd b18358c
Apply suggestions from code review
jorgensd bbbf40b
Apply suggestions from code review
pbrubeck e206352
Some more fixes (galerkin stepper constant)
jorgensd 55637cf
Apply suggestions from code review
jorgensd e2272ab
Apply suggestions from code review
jorgensd 7dc9fa8
Update irksome/tools.py
pbrubeck 6364076
Update irksome/stage_value.py
pbrubeck 0d8be42
Update irksome/nystrom_dirk_stepper.py
pbrubeck b444c6c
Update irksome/imex.py
pbrubeck 32fd197
Update irksome/nystrom_dirk_stepper.py
pbrubeck 86a598d
Apply suggestion from @pbrubeck
pbrubeck e498394
Apply suggestions from code review
pbrubeck c26f468
Apply suggestions from code review
pbrubeck 221c55d
Revert meshconstant in steppers
jorgensd ce53883
Apply suggestions from code review
pbrubeck df05ee1
Linting and remove demo
jorgensd 780266d
Apply suggestions from code review
pbrubeck ae33c01
Apply suggestions from code review
pbrubeck 2e3aa3d
Apply suggestion from @pbrubeck
pbrubeck 05334a8
Apply suggestions from code review
pbrubeck 4997a00
Revert constant to meshless in galerkin timestepper
jorgensd 3b8a62c
Apply suggestion from @pbrubeck
pbrubeck fc7caa5
Simplify fetching backend
jorgensd 88999f3
Revert to reduce mul from MixedFunctionSpace + some more backend call…
jorgensd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from mpi4py import MPI | ||
| import dolfinx | ||
| import ufl | ||
| from irksome import GaussLegendre, Dt, MeshConstant | ||
| from irksome.stage_derivative import getForm | ||
| from irksome.tools import get_stage_space | ||
| from ufl import pi, atan, div, grad, inner, dx | ||
|
|
||
| butcher_tableau = GaussLegendre(2) | ||
| N = 64 | ||
|
|
||
| x0 = 0.0 | ||
| x1 = 10.0 | ||
| y0 = 0.0 | ||
| y1 = 10.0 | ||
|
|
||
| msh = dolfinx.mesh.create_rectangle(MPI.COMM_WORLD, [[x0, y0], [x1, y1]], [N, N]) | ||
| V = dolfinx.fem.functionspace(msh, ("Lagrange", 1)) | ||
| x, y = ufl.SpatialCoordinate(msh) | ||
|
|
||
| MC = MeshConstant(msh, backend="dolfinx") | ||
| dt = MC.Constant(10 / N) | ||
| t = MC.Constant(0.0) | ||
|
|
||
| Constant = lambda val: dolfinx.fem.Constant(msh, val) | ||
| S = Constant(2.0) | ||
| C = Constant(1000.0) | ||
|
|
||
|
|
||
| B = ( | ||
| (x - Constant(x0)) | ||
| * (x - Constant(x1)) | ||
| * (y - Constant(y0)) | ||
| * (y - Constant(y1)) | ||
| / C | ||
| ) | ||
| R = (x * x + y * y) ** 0.5 | ||
| uexact = B * atan(t) * (pi / 2.0 - atan(S * (R - t))) | ||
|
|
||
| rhs = Dt(uexact) - div(grad(uexact)) | ||
|
|
||
| u = dolfinx.fem.Function(V) | ||
| u.interpolate(dolfinx.fem.Expression(uexact, V.element.interpolation_points)) | ||
|
|
||
| v = ufl.TestFunction(V) | ||
| F = inner(Dt(u), v) * dx + inner(grad(u), grad(v)) * dx - inner(rhs, v) * dx | ||
|
|
||
| bc = [] | ||
| # bc = DirichletBC(V, 0, "on_boundary") | ||
|
|
||
| # Get the function space for the stage-coupled problem and a function to hold the stages we're computing:: | ||
|
|
||
| Vbig = get_stage_space(V, butcher_tableau.num_stages, backend="dolfinx") | ||
| k = dolfinx.fem.Function(Vbig) | ||
|
|
||
| # Get the variational form and bcs for the stage-coupled variational problem:: | ||
|
|
||
| Fnew, bcnew = getForm(F, butcher_tableau, t, dt, u, k, bcs=bc, backend="dolfinx") |
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
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,76 @@ | ||||||||||||||||
| """DOLFINx backend for Irksome""" | ||||||||||||||||
|
|
||||||||||||||||
| try: | ||||||||||||||||
| from mpi4py import MPI | ||||||||||||||||
| import basix.ufl | ||||||||||||||||
| import dolfinx | ||||||||||||||||
| import dolfinx.fem.petsc | ||||||||||||||||
| import ufl | ||||||||||||||||
| import typing | ||||||||||||||||
| import numpy as np | ||||||||||||||||
|
|
||||||||||||||||
| def get_stage_space(V: ufl.FunctionSpace, num_stages: int) -> ufl.FunctionSpace: | ||||||||||||||||
| if num_stages == 1: | ||||||||||||||||
| me = V.ufl_elemet() | ||||||||||||||||
| else: | ||||||||||||||||
| el = V.ufl_element() | ||||||||||||||||
| if el.num_sub_elements > 0: | ||||||||||||||||
| me = basix.ufl.mixed_element( | ||||||||||||||||
| np.tile(el.sub_elements, num_stages).tolist() | ||||||||||||||||
| ) | ||||||||||||||||
| else: | ||||||||||||||||
| me = basix.ufl.blocked_element(el, shape=(num_stages,)) | ||||||||||||||||
| return dolfinx.fem.functionspace(V.mesh, me) | ||||||||||||||||
|
|
||||||||||||||||
| def extract_bcs(bcs: typing.Any) -> tuple[typing.Any]: | ||||||||||||||||
| """Extract boundary conditions""" | ||||||||||||||||
| return bcs | ||||||||||||||||
|
|
||||||||||||||||
| def create_linearvariational_problem( | ||||||||||||||||
| a: ufl.Form, | ||||||||||||||||
| L: ufl.Form, | ||||||||||||||||
| u: ufl.Coefficient | typing.Sequence[ufl.Coefficient], | ||||||||||||||||
| bcs: typing.Sequence | None = None, | ||||||||||||||||
| aP: ufl.Form | None = None, | ||||||||||||||||
| **kwargs, | ||||||||||||||||
| ) -> dolfinx.fem.petsc.LinearProblem: | ||||||||||||||||
| return dolfinx.fem.petsc.LinearProblem( | ||||||||||||||||
| a, | ||||||||||||||||
| L, | ||||||||||||||||
| u, | ||||||||||||||||
| bcs=bcs, | ||||||||||||||||
| petsc_options_prefix="IrkSomeLinearSolver", | ||||||||||||||||
| P=aP, | ||||||||||||||||
| **kwargs, | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
|
jorgensd marked this conversation as resolved.
Outdated
|
||||||||||||||||
| def create_linear_solver( | ||||||||||||||||
| problem: dolfinx.fem.petsc.LinearProblem, | ||||||||||||||||
| solver_parameters: dict | None = None, | ||||||||||||||||
| **kwargs, | ||||||||||||||||
| ): | ||||||||||||||||
| """Create a linear variational solver that uses PETSc KSP.""" | ||||||||||||||||
|
jorgensd marked this conversation as resolved.
Outdated
|
||||||||||||||||
| return problem | ||||||||||||||||
|
|
||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| def create_nonlinearvariational_problem( | ||||||||||||||||
|
jorgensd marked this conversation as resolved.
Outdated
|
||||||||||||||||
| F: ufl.Form, | ||||||||||||||||
| g: ufl.Coefficient, | ||||||||||||||||
| bcs: typing.Sequence | None = None, | ||||||||||||||||
| solver_parameters: dict | None = None, | ||||||||||||||||
| ) -> dolfinx.fem.petsc.NonlinearProblem: | ||||||||||||||||
|
jorgensd marked this conversation as resolved.
Outdated
|
||||||||||||||||
| return dolfinx.fem.petsc.NonlinearProblem( | ||||||||||||||||
| F, | ||||||||||||||||
| g, | ||||||||||||||||
| petsc_options_prefix="IrkSomeNonlinearSolver", | ||||||||||||||||
| bcs=bcs, | ||||||||||||||||
| petsc_options=solver_parameters, | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| def create_nonlinear_solver( | ||||||||||||||||
| problem: dolfinx.fem.petsc.NonlinearProblem, | ||||||||||||||||
| solver_parameters: dict | None = None, | ||||||||||||||||
| ): | ||||||||||||||||
| """Create a non-linear variational solver that uses PETSc SNES.""" | ||||||||||||||||
| return problem | ||||||||||||||||
|
jorgensd marked this conversation as resolved.
Outdated
|
||||||||||||||||
|
|
||||||||||||||||
| def get_function_space(u: ufl.Coefficient) -> ufl.FunctionSpace: | ||||||||||||||||
| return u.ufl_function_space() | ||||||||||||||||
|
|
@@ -31,11 +98,20 @@ class MeshConstant(object): | |||||||||||||||
| def __init__(self, msh): | ||||||||||||||||
| self.msh = msh | ||||||||||||||||
| try: | ||||||||||||||||
| import scifem | ||||||||||||||||
| except ModuleNotFoundError: | ||||||||||||||||
| raise RuntimeError("Scifem is required to make mesh-constants") | ||||||||||||||||
| import basix.ufl | ||||||||||||||||
|
|
||||||||||||||||
| self.V = scifem.create_real_functionspace(msh, ()) | ||||||||||||||||
| r_el = basix.ufl.real_element( | ||||||||||||||||
| msh.basix_cell(), value_shape=(), dtype=dolfinx.default_scalar_type | ||||||||||||||||
| ) | ||||||||||||||||
| self.V = dolfinx.fem.functionspace(msh, r_el) | ||||||||||||||||
| except TypeError: | ||||||||||||||||
| try: | ||||||||||||||||
| import scifem | ||||||||||||||||
| except ModuleNotFoundError: | ||||||||||||||||
| raise RuntimeError( | ||||||||||||||||
| "DOLFINx with real element support or Scifem is required to make mesh-constants" | ||||||||||||||||
| ) | ||||||||||||||||
| self.V = scifem.create_real_functionspace(msh, ()) | ||||||||||||||||
|
|
||||||||||||||||
| def Constant(self, val=0.0) -> ufl.Coefficient: | ||||||||||||||||
| v = dolfinx.fem.Function(self.V) | ||||||||||||||||
|
|
@@ -45,5 +121,51 @@ def Constant(self, val=0.0) -> ufl.Coefficient: | |||||||||||||||
| def get_mesh_constant(MC: MeshConstant | None) -> ufl.core.expr.Expr: | ||||||||||||||||
| return MC.Constant if MC is not None else ufl.constantvalue.ComplexValue | ||||||||||||||||
|
|
||||||||||||||||
| class DirichletBC(dolfinx.fem.DirichletBC): | ||||||||||||||||
| pass | ||||||||||||||||
|
|
||||||||||||||||
| def norm( | ||||||||||||||||
| v: ufl.core.Expr, norm_type: str = "L2", mesh: ufl.Mesh | None = None | ||||||||||||||||
| ) -> float: | ||||||||||||||||
| """Compute the norm of a function in the backend language.""" | ||||||||||||||||
| if mesh is not None: | ||||||||||||||||
| dx = ufl.Mesure("dx", domain=mesh) | ||||||||||||||||
| else: | ||||||||||||||||
| dx = ufl.dx | ||||||||||||||||
| p = 2 | ||||||||||||||||
| if norm_type.startswith("L"): | ||||||||||||||||
| p = int(norm_type[1:]) | ||||||||||||||||
| if p < 1: | ||||||||||||||||
| raise ValueError(f"Invalid norm type {norm_type}") | ||||||||||||||||
| expr = ufl.inner(v, v) ** (p / 2) | ||||||||||||||||
| form = dolfinx.fem.form(expr * dx) | ||||||||||||||||
| else: | ||||||||||||||||
| raise NotImplementedError(f"Norm type {norm_type} not implemented") | ||||||||||||||||
| norm_loc = dolfinx.fem.assemble_scalar(form) | ||||||||||||||||
| return form.mesh.comm.Allreduce(MPI.IN_PLACE, norm_loc, op=MPI.SUM) ** (1 / p) | ||||||||||||||||
|
|
||||||||||||||||
| def assemble(expr: ufl.core.Expr | float): | ||||||||||||||||
| """Assemble a UFL expression in the backend language.""" | ||||||||||||||||
| if isinstance(expr, float): | ||||||||||||||||
| return float | ||||||||||||||||
| else: | ||||||||||||||||
| form = dolfinx.fem.form(expr) | ||||||||||||||||
| if form.rank == 0: | ||||||||||||||||
| return dolfinx.fem.assemble_scalar(form) | ||||||||||||||||
| elif form.rank == 1: | ||||||||||||||||
| return dolfinx.fem.assemble_vector(form) | ||||||||||||||||
| elif form.rank == 2: | ||||||||||||||||
| return dolfinx.fem.assemble_matrix(form) | ||||||||||||||||
| else: | ||||||||||||||||
| raise ValueError(f"Cannot assemble form of rank {form.rank}") | ||||||||||||||||
|
|
||||||||||||||||
| derivative = ufl.derivative | ||||||||||||||||
| TrialFunction = ufl.TrialFunction | ||||||||||||||||
| Function = dolfinx.fem.Function | ||||||||||||||||
| TestFunction = ufl.TestFunction | ||||||||||||||||
|
|
||||||||||||||||
| def invalidate_jacobian(solver: dolfinx.fem.petsc.LinearProblem): | ||||||||||||||||
| """Invalidate the Jacobian matrix in the backend language.""" | ||||||||||||||||
| raise RuntimeError("DOLFINx does not support Jacobian invalidation") | ||||||||||||||||
| except ModuleNotFoundError: | ||||||||||||||||
| pass | ||||||||||||||||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,16 @@ | |
| import firedrake | ||
| import ufl | ||
| from ..tools import get_stage_space | ||
|
jorgensd marked this conversation as resolved.
Outdated
|
||
| import typing | ||
|
|
||
|
|
||
| def get_stage_space(V: ufl.FunctionSpace, num_stages: int) -> ufl.FunctionSpace: | ||
| return firedrake.MixedFunctionSpace(tuple(V) * num_stages) | ||
|
|
||
|
|
||
| def extract_bcs(bcs: typing.Any) -> tuple[typing.Any]: | ||
| """Return an iterable of boundary conditions on the residual form""" | ||
| return tuple(bc.extract_form("F") for bc in firedrake.solving._extract_bcs(bcs)) | ||
|
|
||
|
|
||
| def get_function_space(u: ufl.Coefficient) -> firedrake.FunctionSpace: | ||
|
|
@@ -63,8 +73,10 @@ def invalidate_jacobian(solver): | |
| return firedrake.LinearVariationalSolver.invalidate_jacobian(solver) | ||
|
|
||
|
|
||
| assemble = firedrake.assemble | ||
| derivative = firedrake.derivative | ||
| norm = firedrake.norm | ||
| Function = firedrake.Function | ||
| TestFunction = firedrake.TestFunction | ||
| TrialFunction = firedrake.TrialFunction | ||
| DirichletBC = firedrake.DirichletBC | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file
jorgensd marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.