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

WIP Change of var method for interface discontinuities #904

Draft
wants to merge 39 commits into
base: fenicsx
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3507d15
initial commit
RemDelaporteMathurin Oct 29, 2024
672fd39
Merge branch 'discontinuity-generic' into change-of-var-method
RemDelaporteMathurin Oct 29, 2024
50c3798
example file
RemDelaporteMathurin Oct 29, 2024
ec7ba6f
working example!
RemDelaporteMathurin Oct 29, 2024
d23fa1c
DG elements for traps + only remove vol from list if it's in list
RemDelaporteMathurin Oct 29, 2024
009c833
num-cells-per-segment
RemDelaporteMathurin Oct 29, 2024
76b1e19
Merge branch 'discontinuity-generic' into change-of-var-method
RemDelaporteMathurin Oct 29, 2024
ace007c
don't print form
RemDelaporteMathurin Oct 29, 2024
b2b3bfa
added petsc options
RemDelaporteMathurin Oct 29, 2024
9eb98f5
moved convergence rates
RemDelaporteMathurin Oct 30, 2024
56d3fd0
moved to test
RemDelaporteMathurin Oct 30, 2024
a52f06b
Merge remote-tracking branch 'upstream/fenicsx' into change-of-var-me…
RemDelaporteMathurin Oct 30, 2024
ac8b8db
added check for only one mobile species
RemDelaporteMathurin Oct 30, 2024
e1f4fde
DirichletBC are now rescaled inside festim
RemDelaporteMathurin Oct 30, 2024
14c7455
BCs were not updated when only T was changing
RemDelaporteMathurin Oct 30, 2024
ce8cc87
get_K_S_0 and get_E_K_S
RemDelaporteMathurin Oct 30, 2024
309464b
works in multispecies
RemDelaporteMathurin Oct 30, 2024
976a0bd
removed comment
RemDelaporteMathurin Oct 30, 2024
b131d5e
added MMS test
RemDelaporteMathurin Oct 31, 2024
d823337
add a temporary method (correct) for entities
RemDelaporteMathurin Oct 31, 2024
b51b857
correct L2 error
RemDelaporteMathurin Oct 31, 2024
5593aae
fixed imports
RemDelaporteMathurin Oct 31, 2024
9515840
DG element
RemDelaporteMathurin Oct 31, 2024
2b3cbac
added note
RemDelaporteMathurin Oct 31, 2024
016d8c0
Merge branch 'fix-solver-options' into change-of-var-method
RemDelaporteMathurin Oct 31, 2024
ed809f0
added fix for c = 0 trick
RemDelaporteMathurin Oct 31, 2024
dcf5c5e
removed failing test (referenced in issue)
RemDelaporteMathurin Nov 1, 2024
efd9ab2
ruff
RemDelaporteMathurin Nov 1, 2024
92be8a9
Merge remote-tracking branch 'upstream/fenicsx' into change-of-var-me…
RemDelaporteMathurin Nov 12, 2024
beef3b4
Use newest newton solver
jorgensd Nov 19, 2024
5bf396e
Bump scifem release
jorgensd Nov 19, 2024
e7c7876
Remove rtol from steady state
jorgensd Nov 19, 2024
472eaa5
ruff formatting
jorgensd Nov 19, 2024
54063ba
fixed atol=None
RemDelaporteMathurin Nov 19, 2024
582e7e0
Merge remote-tracking branch 'upstream/fenicsx' into dokken/change-of…
RemDelaporteMathurin Nov 19, 2024
f5f4753
Merge remote-tracking branch 'upstream/fenicsx' into change-of-var-me…
RemDelaporteMathurin Nov 19, 2024
99185ab
Merge branch 'change-of-var-method' into dokken/change-of-var
RemDelaporteMathurin Nov 19, 2024
482d920
Apply suggestions from code review
jorgensd Nov 19, 2024
e6ce4bc
Merge pull request #3 from RemDelaporteMathurin/dokken/change-of-var
RemDelaporteMathurin Nov 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
3 changes: 2 additions & 1 deletion src/festim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
HTransportProblemDiscontinuous,
HydrogenTransportProblem,
)
from .problem_change_of_var import HydrogenTransportProblemDiscontinuousChangeVar
from .initial_condition import InitialCondition, InitialTemperature
from .material import Material
from .mesh.mesh import Mesh
Expand All @@ -47,7 +48,7 @@
from .reaction import Reaction
from .settings import Settings
from .source import HeatSource, ParticleSource, SourceBase
from .species import ImplicitSpecies, Species, find_species_from_name
from .species import ImplicitSpecies, Species, find_species_from_name, SpeciesChangeVar
from .stepsize import Stepsize
from .subdomain.interface import Interface
from .subdomain.surface_subdomain import SurfaceSubdomain, find_surface_from_id
Expand Down
20 changes: 18 additions & 2 deletions src/festim/hydrogen_transport_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
settings=None,
exports=None,
traps=None,
petsc_options=None,
):
super().__init__(
mesh=mesh,
Expand All @@ -167,6 +168,9 @@
self.temperature_fenics = None
self._vtxfiles: list[dolfinx.io.VTXWriter] = []

self._element_for_traps = "DG"
self.petcs_options = petsc_options

@property
def temperature(self):
return self._temperature
Expand Down Expand Up @@ -446,14 +450,25 @@
degree,
basix.LagrangeVariant.equispaced,
)
element_DG = basix.ufl.element(
"DG",
self.mesh.mesh.basix_cell(),
degree,
basix.LagrangeVariant.equispaced,
)

if not self.multispecies:
element = element_CG
else:
elements = []
for spe in self.species:
if isinstance(spe, _species.Species):
elements.append(element_CG)
if spe.mobile:
elements.append(element_CG)
elif self._element_for_traps == "DG":
elements.append(element_DG)
else:
elements.append(element_CG)

Check warning on line 471 in src/festim/hydrogen_transport_problem.py

View check run for this annotation

Codecov / codecov/patch

src/festim/hydrogen_transport_problem.py#L471

Added line #L471 was not covered by tests
element = basix.ufl.mixed_element(elements)

self.function_space = fem.functionspace(self.mesh.mesh, element)
Expand Down Expand Up @@ -690,7 +705,8 @@
# check reactions
for reaction in self.reactions:
if vol == reaction.volume:
not_defined_in_volume.remove(vol)
if vol in not_defined_in_volume:
not_defined_in_volume.remove(vol)

# add c = 0 to formulation where needed
for vol in not_defined_in_volume:
Expand Down
175 changes: 175 additions & 0 deletions src/festim/problem_change_of_var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import festim.boundary_conditions
from festim.hydrogen_transport_problem import HydrogenTransportProblem
from festim import boundary_conditions, as_fenics_constant
import festim
import festim.species as _species
import festim.exports as exports
import ufl
from dolfinx import fem

from typing import List


class HydrogenTransportProblemDiscontinuousChangeVar(HydrogenTransportProblem):
species: List[_species.Species]

def initialise(self):
self.create_species_from_traps()
self.define_function_spaces()
self.define_meshtags_and_measures()
self.assign_functions_to_species()

Check warning on line 20 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L17-L20

Added lines #L17 - L20 were not covered by tests

self.t = fem.Constant(self.mesh.mesh, 0.0)
if self.settings.transient:

Check warning on line 23 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L22-L23

Added lines #L22 - L23 were not covered by tests
# TODO should raise error if no stepsize is provided
# TODO Should this be an attribute of festim.Stepsize?
self.dt = as_fenics_constant(

Check warning on line 26 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L26

Added line #L26 was not covered by tests
self.settings.stepsize.initial_value, self.mesh.mesh
)

self.define_temperature()
self.define_boundary_conditions()
self.create_source_values_fenics()
self.create_flux_values_fenics()
self.create_initial_conditions()
self.create_formulation()
self.create_solver()
self.override_post_processing_solution()
self.initialise_exports()

Check warning on line 38 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L30-L38

Added lines #L30 - L38 were not covered by tests

def create_formulation(self):
"""Creates the formulation of the model"""

self.formulation = 0

Check warning on line 43 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L43

Added line #L43 was not covered by tests

# add diffusion and time derivative for each species
for spe in self.species:
u = spe.solution
u_n = spe.prev_solution
v = spe.test_function

Check warning on line 49 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L46-L49

Added lines #L46 - L49 were not covered by tests

for vol in self.volume_subdomains:
D = vol.material.get_diffusion_coefficient(

Check warning on line 52 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L51-L52

Added lines #L51 - L52 were not covered by tests
self.mesh.mesh, self.temperature_fenics, spe
)
K_S = vol.material.get_solubility_coefficient(

Check warning on line 55 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L55

Added line #L55 was not covered by tests
self.mesh.mesh, self.temperature_fenics, spe
)
if spe.mobile:
c = u * K_S
c_n = u_n * K_S

Check warning on line 60 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L58-L60

Added lines #L58 - L60 were not covered by tests
else:
c = u
c_n = u_n
if spe.mobile:
self.formulation += ufl.dot(D * ufl.grad(c), ufl.grad(v)) * self.dx(

Check warning on line 65 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L62-L65

Added lines #L62 - L65 were not covered by tests
vol.id
)

if self.settings.transient:
self.formulation += ((c - c_n) / self.dt) * v * self.dx(vol.id)

Check warning on line 70 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L69-L70

Added lines #L69 - L70 were not covered by tests

for reaction in self.reactions:
if isinstance(reaction.product, list):
products = reaction.product

Check warning on line 74 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L72-L74

Added lines #L72 - L74 were not covered by tests
else:
products = [reaction.product]

Check warning on line 76 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L76

Added line #L76 was not covered by tests

# hack enforce the concentration attribute of the species for all species
# to be used in reaction.reaction_term
for spe in self.species:
K_S = reaction.volume.material.get_solubility_coefficient(

Check warning on line 81 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L80-L81

Added lines #L80 - L81 were not covered by tests
self.mesh.mesh, self.temperature_fenics, spe
)
if spe.mobile:
spe.concentration = spe.solution * K_S

Check warning on line 85 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L84-L85

Added lines #L84 - L85 were not covered by tests

# reactant
for reactant in reaction.reactant:
if isinstance(reactant, festim.species.Species):
self.formulation += (

Check warning on line 90 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L88-L90

Added lines #L88 - L90 were not covered by tests
reaction.reaction_term(self.temperature_fenics)
* reactant.test_function
* self.dx(reaction.volume.id)
)

# product
for product in products:
self.formulation += (

Check warning on line 98 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L97-L98

Added lines #L97 - L98 were not covered by tests
-reaction.reaction_term(self.temperature_fenics)
* product.test_function
* self.dx(reaction.volume.id)
)
# add sources
for source in self.sources:
self.formulation -= (

Check warning on line 105 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L104-L105

Added lines #L104 - L105 were not covered by tests
source.value_fenics
* source.species.test_function
* self.dx(source.volume.id)
)

# add fluxes
for bc in self.boundary_conditions:
if isinstance(bc, boundary_conditions.ParticleFluxBC):
self.formulation -= (

Check warning on line 114 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L112-L114

Added lines #L112 - L114 were not covered by tests
bc.value_fenics
* bc.species.test_function
* self.ds(bc.subdomain.id)
)

# check if each species is defined in all volumes
if not self.settings.transient:
for spe in self.species:

Check warning on line 122 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L121-L122

Added lines #L121 - L122 were not covered by tests
# if species mobile, already defined in diffusion term
if not spe.mobile:
not_defined_in_volume = self.volume_subdomains.copy()
for vol in self.volume_subdomains:

Check warning on line 126 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L124-L126

Added lines #L124 - L126 were not covered by tests
# check reactions
for reaction in self.reactions:
if vol == reaction.volume:
if vol in not_defined_in_volume:
not_defined_in_volume.remove(vol)

Check warning on line 131 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L128-L131

Added lines #L128 - L131 were not covered by tests

# add c = 0 to formulation where needed
for vol in not_defined_in_volume:
self.formulation += (

Check warning on line 135 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L134-L135

Added lines #L134 - L135 were not covered by tests
spe.solution * spe.test_function * self.dx(vol.id)
)

def override_post_processing_solution(self):
# override the post-processing solution c = theta * K_S
Q0 = fem.functionspace(self.mesh.mesh, ("DG", 0))
Q1 = fem.functionspace(self.mesh.mesh, ("DG", 1))
K_S0 = fem.Function(Q0)
E_KS = fem.Function(Q0)
for subdomain in self.volume_subdomains:
entities = subdomain.locate_subdomain_entities(self.mesh.mesh)
K_S0.x.array[entities] = subdomain.material.K_S_0
E_KS.x.array[entities] = subdomain.material.E_K_S

Check warning on line 148 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L141-L148

Added lines #L141 - L148 were not covered by tests

K_S = K_S0 * ufl.exp(-E_KS / (festim.k_B * self.temperature_fenics))

Check warning on line 150 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L150

Added line #L150 was not covered by tests

for spe in self.species:
theta = spe.solution

Check warning on line 153 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L152-L153

Added lines #L152 - L153 were not covered by tests

spe.dg_expr = fem.Expression(theta * K_S, Q1.element.interpolation_points())
spe.post_processing_solution = fem.Function(Q1)
spe.post_processing_solution.interpolate(spe.dg_expr)

Check warning on line 157 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L155-L157

Added lines #L155 - L157 were not covered by tests

def post_processing(self):

# need to compute c = theta * K_S
# this expression is stored in species.dg_expr
for spe in self.species:
spe.post_processing_solution.interpolate(spe.dg_expr)

Check warning on line 164 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L163-L164

Added lines #L163 - L164 were not covered by tests

super().post_processing()

Check warning on line 166 in src/festim/problem_change_of_var.py

View check run for this annotation

Codecov / codecov/patch

src/festim/problem_change_of_var.py#L166

Added line #L166 was not covered by tests

# def define_boundary_conditions(self):
# for bc in self.boundary_conditions:
# if isinstance(bc.species, str):
# # if name of species is given then replace with species object
# bc.species = _species.find_species_from_name(bc.species, self.species)

# for bc in self.boundary_conditions:
# pass
10 changes: 10 additions & 0 deletions src/festim/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@
if spe.name == name:
return spe
raise ValueError(f"Species {name} not found in list of species")


class SpeciesChangeVar(Species):
@property
def concentration(self):
return self._concentration

Check warning on line 174 in src/festim/species.py

View check run for this annotation

Codecov / codecov/patch

src/festim/species.py#L174

Added line #L174 was not covered by tests

@concentration.setter
def concentration(self, value):
self._concentration = value

Check warning on line 178 in src/festim/species.py

View check run for this annotation

Codecov / codecov/patch

src/festim/species.py#L178

Added line #L178 was not covered by tests
2 changes: 2 additions & 0 deletions src/festim/subdomain/volume_subdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np

from festim.helpers_discontinuity import transfer_meshtags_to_submesh
from festim.material import Material


class VolumeSubdomain:
Expand All @@ -24,6 +25,7 @@ class VolumeSubdomain:
padded: bool
u: dolfinx.fem.Function
u_n: dolfinx.fem.Function
material: Material

def __init__(self, id, material):
self.id = id
Expand Down
Loading
Loading