-
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
Draft
jorgensd
wants to merge
7
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.
+792
−238
Draft
Changes from 2 commits
Commits
Show all changes
7 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 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
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,32 @@ | ||||||
| """Firedrake backend for Irksome""" | ||||||
|
|
||||||
|
|
||||||
| from operator import mul | ||||||
| from functools import reduce | ||||||
|
|
||||||
|
Comment on lines
+3
to
+5
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
|
||||||
| import firedrake | ||||||
| import ufl | ||||||
| from ..tools import get_stage_space | ||||||
| import typing | ||||||
|
|
||||||
| TestFunction = firedrake.TestFunction | ||||||
|
|
||||||
|
|
||||||
| def get_stage_space(V: ufl.FunctionSpace, num_stages:int)->ufl.FunctionSpace: | ||||||
| return reduce(mul, (V for _ in range(num_stages))) | ||||||
|
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 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 create_nonlinearvariational_problem(F: ufl.Form, u: ufl.Coefficient, solver_parameters: dict): | ||||||
|
jorgensd marked this conversation as resolved.
Outdated
|
||||||
| """Create a non-linear variational solver that uses PETSc SNES.""" | ||||||
| problem = firedrake.NonlinearVariationalProblem(F, u) | ||||||
|
pbrubeck marked this conversation as resolved.
Outdated
|
||||||
| return firedrake.NonlinearVariationalSolver( | ||||||
| problem, solver_parameters=solver_parameters | ||||||
|
pbrubeck marked this conversation as resolved.
Outdated
|
||||||
| ) | ||||||
|
|
||||||
| def get_function_space(u: ufl.Coefficient) -> firedrake.FunctionSpace: | ||||||
| return u.function_space() | ||||||
|
|
||||||
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 |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
|
|
||
|
jorgensd marked this conversation as resolved.
Outdated
|
||
| import numpy | ||
| from ufl import as_ufl, as_tensor, Form, Coefficient | ||
| from .tableaux import ButcherTableaux | ||
| from .constant import vecconst | ||
| from .tools import AI, dot, replace, reshape, fields_to_components | ||
| from .ufl.deriv import Dt, TimeDerivative, expand_time_derivatives | ||
| from .backend import get_backend | ||
|
|
||
| __all__ = ["getForm"] | ||
|
|
||
| def getForm(F: Form, butch:ButcherTableaux, t: Coefficient, dt:Coefficient, u0:Coefficient, stages, bcs=None, bc_type=None, splitting=AI, aux_indices=None, backend:str="firedrake"): | ||
| """Given a time-dependent variational form and a | ||
| :class:`ButcherTableau`, produce UFL for the s-stage RK method. | ||
|
|
||
| :arg F: UFL form for the semidiscrete ODE/DAE | ||
| :arg butch: the :class:`ButcherTableau` for the RK method being used to | ||
| advance in time. | ||
| :arg t: a :class:`Function` on the Real space over the same mesh as | ||
| `u0`. This serves as a variable referring to the current time. | ||
| :arg dt: a :class:`Function` on the Real space over the same mesh as | ||
| `u0`. This serves as a variable referring to the current time step. | ||
| The user may adjust this value between time steps. | ||
| :arg u0: a :class:`Function` referring to the state of | ||
| the PDE system at time `t` | ||
| :arg stages: a :class:`Function` representing the stages to be solved for. | ||
| :kwarg bcs: optionally, a :class:`DirichletBC` or :class:`EquationBC` | ||
| object (or iterable thereof) containing (possibly time-dependent) | ||
| boundary conditions imposed on the system. | ||
| :kwarg bc_type: How to manipulate the strongly-enforced boundary | ||
| conditions to derive the stage boundary conditions. Should | ||
| be a string, either "DAE", which implements BCs as | ||
| constraints in the style of a differential-algebraic | ||
| equation, or "ODE", which takes the time derivative of the | ||
| boundary data and evaluates this for the stage values. | ||
| Support for `firedrake.EquationBC` in `bcs` is limited | ||
| to DAE style BCs. | ||
| :kwarg splitting: a callable that maps the (floating point) Butcher matrix | ||
| a to a pair of matrices `A1, A2` such that `butch.A = A1 A2`. This is used | ||
| to vary between the classical RK formulation and Butcher's reformulation | ||
| that leads to a denser mass matrix with block-diagonal stiffness. | ||
| Some choices of function will assume that `butch.A` is invertible. | ||
| :kwarg aux_indices: a list of field indices to be discretized as :class:`TimeDerivative`, | ||
| analogouos to :class:`ContinouosPetrovGalerkinTimeStepper`. | ||
|
|
||
| :returns: a 2-tuple of | ||
| - `Fnew`, the :class:`Form` | ||
| - `bcnew`, a list of :class:`firedrake.DirichletBC` or :class:`EquationBC` | ||
| objects to be posed on the stages | ||
| """ | ||
| backend_cls = get_backend(backend) | ||
| if bc_type is None: | ||
| bc_type = "DAE" | ||
|
|
||
| # preprocess time derivatives | ||
| F = expand_time_derivatives(F, t=t, timedep_coeffs=(u0,)) | ||
| v, = F.arguments() | ||
| V = backend_cls.get_function_space(v) | ||
| assert V == backend_cls.get_function_space(u0) | ||
|
|
||
| c = vecconst(butch.c, backend=backend) | ||
| bA1, bA2 = splitting(butch.A) | ||
| try: | ||
| bA2inv = numpy.linalg.inv(bA2) | ||
| except numpy.linalg.LinAlgError: | ||
| raise NotImplementedError("We require A = A1 A2 with A2 invertible") | ||
| A1 = vecconst(bA1, backend=backend) | ||
| A2inv = vecconst(bA2inv, backend=backend) | ||
|
|
||
| # s-way product space for the stage variables | ||
| num_stages = butch.num_stages | ||
| Vbig = backend_cls.get_function_space(stages) | ||
| test = backend_cls.TestFunction(Vbig) | ||
|
|
||
| # set up the pieces we need to work with to do our substitutions | ||
| v_np = reshape(test, (num_stages, *v.ufl_shape)) | ||
| w_np = reshape(stages, (num_stages, *u0.ufl_shape)) | ||
| A1w = dot(A1, w_np) | ||
| A2invw = dot(A2inv, w_np) | ||
| dtu = TimeDerivative(u0) | ||
|
|
||
| aux_components = fields_to_components(V, aux_indices or []) | ||
|
|
||
| repl = {} | ||
| for i in range(num_stages): | ||
| usub = u0 + as_tensor(A1w[i]) * dt | ||
| dtusub = A2invw[i] | ||
| if aux_components: | ||
| # Apply TimeDerivative substitution to auxiliary fields | ||
| usub = reshape(usub, u0.ufl_shape) | ||
| usub[aux_components] = dtusub[aux_components] * dt | ||
|
|
||
| repl[i] = {t: t + c[i] * dt, | ||
| v: v_np[i], | ||
| u0: usub, | ||
| dtu: dtusub} | ||
|
|
||
| Fnew = sum(replace(F, repl[i]) for i in range(num_stages)) | ||
|
|
||
| if bcs is None: | ||
| bcs = [] | ||
| if bc_type == "ODE": | ||
| assert splitting == AI, "ODE-type BC aren't implemented for this splitting strategy" | ||
|
|
||
| def bc2stagebc(bc, i): | ||
| from irksome.bcs import BCStageData | ||
| from firedrake.bcs import EquationBCSplit | ||
|
|
||
| if isinstance(bc, EquationBCSplit): | ||
| raise NotImplementedError("EquationBC not implemented for ODE formulation") | ||
| gorig = as_ufl(bc._original_arg) | ||
| gfoo = expand_time_derivatives(Dt(gorig), t=t, timedep_coeffs=(u0,)) | ||
| gcur = replace(gfoo, {t: t + c[i] * dt}) | ||
| return BCStageData(bc, gcur, u0, stages, i) | ||
|
|
||
| elif bc_type == "DAE": | ||
| try: | ||
| bA1inv = numpy.linalg.inv(bA1) | ||
| A1inv = vecconst(bA1inv, backend=backend) | ||
| except numpy.linalg.LinAlgError: | ||
| raise NotImplementedError("Cannot have DAE BCs for this Butcher Tableau/splitting") | ||
|
|
||
| def bc2stagebc(bc, i): | ||
| from irksome.bcs import BCStageData, stage2spaces4bc, bc2space | ||
| from firedrake.bcs import EquationBCSplit, EquationBC | ||
| if isinstance(bc, EquationBCSplit): | ||
| F_bc_orig = expand_time_derivatives(bc.f, t=t, timedep_coeffs=(u0,)) | ||
| F_bc_new = replace(F_bc_orig, repl[i]) | ||
| Vbigi = stage2spaces4bc(bc, V, Vbig, i) | ||
| return EquationBC(F_bc_new == 0, stages, bc.sub_domain, V=Vbigi, | ||
| bcs=[bc2stagebc(innerbc, i) for innerbc in backend_cls.extract_bcs(bc.bcs)]) | ||
| else: | ||
| gcur = bc._original_arg | ||
| if gcur != 0: | ||
| gorig = as_ufl(gcur) | ||
| ucur = bc2space(bc, u0) | ||
| gcur = (1/dt) * sum((replace(gorig, {t: t + c[j]*dt}) - ucur) * A1inv[i, j] | ||
| for j in range(num_stages)) | ||
| return BCStageData(bc, gcur, u0, stages, i) | ||
| else: | ||
| raise ValueError(f"Unrecognised bc_type: {bc_type}") | ||
|
|
||
| # This logic uses information set up in the previous section to | ||
| # set up the new BCs for either method | ||
| bcs = backend_cls.extract_bcs(bcs) | ||
| bcnew = [bc2stagebc(bc, i) for i in range(num_stages) for bc in bcs] | ||
|
|
||
| return Fnew, bcnew | ||
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
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.