Skip to content
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

Interface concentration discontinuities (fenicsx) #878

Conversation

RemDelaporteMathurin
Copy link
Collaborator

@RemDelaporteMathurin RemDelaporteMathurin commented Aug 20, 2024

Proposed changes

A massive thank you to @jorgensd who was instrumental in getting this functionality in FESTIM 🥇

This PR is a first (big) step towards interface discontinuities in the fenicsx branch (#719).

(requires #764)

This new approach to conservation of chemical potential relies on the mixed domain approach implemented in dolfinx 0.8.0 and based off a demo available here

I created a new problem class HTransportProblemDiscontinuous - which is not yet complete - to avoid breaking existing functionality. We will complete this class by integrating transient, post-processing, ICs, etc. Eventually this whole class will be merged with HydrogenTransportProblem.

In the current interface, users need to provide correspondence between surfaces and volumes as well as listing interfaces and which volumes they are connected to:

list_of_interfaces = [F.Interface(6, (left_domain, middle_domain))]
my_model.subdomains = [left_domain, right_domain, left_surface, right_surface]

I believe this can be streamlined by FESTIM under the hood but I'll keep that for a future PR.

Proposed user interface:

import numpy as np

import festim as F

my_model = F.HTransportProblemDiscontinuous()

interface_1 = 0.2
interface_2 = 0.8

vertices = np.concatenate(
    [
        np.linspace(0, interface_1, num=100),
        np.linspace(interface_1, interface_2, num=100),
        np.linspace(interface_2, 1, num=100),
    ]
)

my_model.mesh = F.Mesh1D(vertices)

material_left = F.Material(D_0=2.0, E_D=0.1, K_S_0=2.0, E_K_S=0)
material_mid = F.Material(D_0=2.0, E_D=0.1, K_S_0=4.0, E_K_S=0)
material_right = F.Material(D_0=2.0, E_D=0.1, K_S_0=6.0, E_K_S=0)

left_domain = F.VolumeSubdomain1D(3, borders=[0, interface_1], material=material_left)
middle_domain = F.VolumeSubdomain1D(
    4, borders=[interface_1, interface_2], material=material_mid
)
right_domain = F.VolumeSubdomain1D(5, borders=[interface_2, 1], material=material_right)

left_surface = F.SurfaceSubdomain1D(id=1, x=vertices[0])
right_surface = F.SurfaceSubdomain1D(id=2, x=vertices[-1])

# the ids here are arbitrary in 1D, you can put anything as long as it's not the same as the surfaces
my_model.interfaces = [
    F.Interface(6, (left_domain, middle_domain)),
    F.Interface(7, (middle_domain, right_domain)),
]
my_model.surface_to_volume = {right_surface: right_domain, left_surface: left_domain}

my_model.subdomains = [
    left_domain,
    middle_domain,
    right_domain,
    left_surface,
    right_surface,
]

H = F.Species("H", mobile=True)
trapped_H = F.Species("H_trapped", mobile=False)
empty_trap = F.ImplicitSpecies(n=0.5, others=[trapped_H])

my_model.species = [H, trapped_H]

for species in [H, trapped_H]:
    species.subdomains = [left_domain, middle_domain, right_domain]


my_model.reactions = [
    F.Reaction(
        reactant=[H, empty_trap],
        product=[trapped_H],
        k_0=2,
        E_k=0,
        p_0=0.1,
        E_p=0,
        volume=domain,
    )
    for domain in [left_domain, middle_domain, right_domain]
]

my_model.boundary_conditions = [
    F.DirichletBC(left_surface, value=0.05, species=H),
    F.DirichletBC(right_surface, value=0.2, species=H),
]


my_model.temperature = lambda x: 300 + 100 * x[0]

my_model.settings = F.Settings(atol=1e-10, rtol=1e-10, transient=False)

my_model.exports = [
    F.VTXSpeciesExport(
        f"u_{field.name}_{subdomain.id}.bp", field=field, subdomain=subdomain
    )
    for subdomain in my_model.volume_subdomains
    for field in [H, trapped_H]
]

my_model.initialise()
my_model.run()

image

Types of changes

What types of changes does your code introduce to FESTIM?

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code refactoring
  • Documentation Update (if none of the other choices apply)
  • New tests

Checklist

  • Black formatted
  • Unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need to keep this

Comment on lines +27 to +31
solution = self.field.solution
indices = self.surface.locate_boundary_facet_indices(
solution.function_space.mesh
)
self.value = np.max(self.field.solution.x.array[indices])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for future: this is not safe in parallel

Comment on lines +830 to +834
default_petsc_options = {
"ksp_type": "preonly",
"pc_type": "lu",
"pc_factor_mat_solver_type": "mumps",
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should live in Settings

Comment on lines +1105 to +1116
v_b = H.subdomain_to_test_function[subdomain_0](res[0])
v_t = H.subdomain_to_test_function[subdomain_1](res[1])

u_b = H.subdomain_to_solution[subdomain_0](res[0])
u_t = H.subdomain_to_solution[subdomain_1](res[1])

K_b = subdomain_0.material.get_solubility_coefficient(
self.mesh.mesh, self.temperature_fenics(res[0]), H
)
K_t = subdomain_1.material.get_solubility_coefficient(
self.mesh.mesh, self.temperature_fenics(res[1]), H
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix bs and ts

Comment on lines +27 to +36
if self._mesh is not None:
# create cell to facet connectivity
self._mesh.topology.create_connectivity(
self._mesh.topology.dim, self._mesh.topology.dim - 1
)

# create facet to cell connectivity
self._mesh.topology.create_connectivity(
self._mesh.topology.dim - 1, self._mesh.topology.dim
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to go in setter

This was referenced Oct 30, 2024
@RemDelaporteMathurin RemDelaporteMathurin merged commit 0c78549 into festim-dev:fenicsx Oct 30, 2024
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fenicsx Issue that is related to the fenicsx support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants