-
Notifications
You must be signed in to change notification settings - Fork 40
Weakly enforced Dirichlet BCs #1158
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
base: main
Are you sure you want to change the base?
Changes from all commits
6424040
dec3499
2fbb912
6f5b24f
b8570e0
e9add2b
8265cd2
bdd9637
90c8bff
a137992
48d775f
fc716a9
39ebee7
3c4b7c1
41d10dc
5dd570d
cf30494
ac946f0
b80166d
0294cb1
ad8d19e
d2bedae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ | |
| import numpy as np | ||
| import numpy.typing as npt | ||
| import ufl | ||
| import ufl.argument | ||
| import ufl.core | ||
| import ufl.core.expr | ||
| import ufl.indexed | ||
| from dolfinx import fem | ||
| from dolfinx import mesh as _mesh | ||
|
|
||
|
|
@@ -19,13 +21,20 @@ class DirichletBCBase: | |
| Args: | ||
| subdomain: The surface subdomain where the boundary condition is applied | ||
| value: The value of the boundary condition | ||
| enforce_weakly: Whether to enforce the boundary condition weakly using Nitsche's | ||
| method. Defaults to False. | ||
| penalty: The penalty parameter to use if ``enforce_weakly`` is True. | ||
| Defaults to None | ||
|
|
||
| Attributes: | ||
| subdomain: The surface subdomain where the boundary condition is applied | ||
| value: The value of the boundary condition | ||
| value_fenics: The value of the boundary condition in fenics format | ||
| bc_expr: The expression of the boundary condition that is used to | ||
| update the `value_fenics` | ||
| enforce_weakly: Whether to enforce the boundary condition weakly using Nitsche's | ||
| method. | ||
| penalty: The penalty parameter to use if ``enforce_weakly`` is True. | ||
| """ | ||
|
|
||
| subdomain: _subdomain.SurfaceSubdomain | ||
|
|
@@ -45,9 +54,13 @@ def __init__( | |
| self, | ||
| subdomain: _subdomain.SurfaceSubdomain, | ||
| value: np.ndarray | fem.Constant | int | float | Callable, | ||
| enforce_weakly: bool = False, | ||
| penalty: float | None = None, | ||
| ): | ||
| self.subdomain = subdomain | ||
| self.value = value | ||
| self.enforce_weakly = enforce_weakly | ||
| self.penalty = penalty | ||
|
|
||
| self.value_fenics = None | ||
| self.bc_expr = None | ||
|
|
@@ -138,6 +151,47 @@ def update(self, t: float): | |
| elif self.bc_expr is not None: | ||
| self.value_fenics.interpolate(self.bc_expr) | ||
|
|
||
| def weak_formulation( | ||
| self, | ||
| u: fem.Function | ufl.indexed.Indexed, | ||
| v: ufl.argument.Argument | ufl.indexed.Indexed, | ||
| ds: ufl.Measure, | ||
| ) -> ufl.core.expr.Expr: | ||
| """ | ||
| Returns the Nitsche weak formulation for the BC | ||
| This follows the dolfinx tutorial | ||
| https://jsdokken.com/dolfinx-tutorial/chapter1/nitsche.html | ||
|
|
||
| Args: | ||
| u: the solution function associated to the species for which the BC | ||
| is applied | ||
| v: the test function | ||
| ds: the surface measure | ||
|
|
||
| Returns: | ||
| the weak formulation | ||
| """ | ||
| mesh = ds.ufl_domain() | ||
| n = ufl.FacetNormal(mesh) | ||
| h = ufl.Circumradius(mesh) # FIXME this doesn't work for rectangles | ||
| alpha = self.penalty | ||
| assert alpha is not None, ( | ||
| "Penalty parameter must be given for weakly enforced Dirichlet BCs" | ||
| ) | ||
| assert self.value_fenics is not None, ( | ||
| "value_fenics must be defined for weakly enforced Dirichlet BCs" | ||
| ) | ||
|
Comment on lines
+178
to
+183
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. Should this not be done in a setter instead? Only if the
Collaborator
Author
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. No not in a setter because then you always have to set say This method is only called if |
||
|
|
||
| form = -ufl.inner(n, ufl.grad(u)) * v * ds(self.subdomain.id) | ||
|
|
||
| form += ( | ||
| +ufl.inner(n, ufl.grad(v)) * (u - self.value_fenics) * ds(self.subdomain.id) | ||
| ) | ||
| form += ( | ||
| -alpha / h * ufl.inner((u - self.value_fenics), v) * ds(self.subdomain.id) | ||
| ) | ||
| return form | ||
|
|
||
|
|
||
| class FixedConcentrationBC(DirichletBCBase): | ||
| """ | ||
|
|
@@ -147,6 +201,10 @@ class FixedConcentrationBC(DirichletBCBase): | |
| value: The value of the boundary condition. It can be a function of | ||
| space and/or time | ||
| species: The name of the species | ||
| enforce_weakly: Whether to enforce the boundary condition weakly using Nitsche's | ||
| method. Defaults to False. | ||
| penalty: The penalty parameter to use if ``enforce_weakly`` is True. | ||
| Defaults to None | ||
|
|
||
| Attributes: | ||
| temperature_dependent (bool): True if the value of the bc is | ||
|
|
@@ -180,9 +238,13 @@ def __init__( | |
| subdomain: _subdomain.SurfaceSubdomain, | ||
| value: np.ndarray | fem.Constant | int | float | Callable, | ||
| species: Species, | ||
| enforce_weakly: bool = False, | ||
| penalty: float | None = None, | ||
| ): | ||
| self.species = species | ||
| super().__init__(subdomain, value) | ||
| super().__init__( | ||
| subdomain, value, enforce_weakly=enforce_weakly, penalty=penalty | ||
| ) | ||
|
|
||
| @property | ||
| def temperature_dependent(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this break for hex meshes or only give the wrong value? Because if it breaks, maybe we could have a mini test in here and give a more descriptive error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will break. But because there is an open issue for this elswhere I'm hoping we can fix this pretty soon anyway