Skip to content

Commit

Permalink
Merge pull request #14 from RemDelaporteMathurin/no-model-species
Browse files Browse the repository at this point in the history
`Material.get_diffusion_coefficient` doesn't require list of species
  • Loading branch information
jhdark authored Oct 24, 2023
2 parents 4fdcf70 + 337a358 commit fdb5361
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 95 deletions.
4 changes: 2 additions & 2 deletions festim/hydrogen_transport_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def create_formulation(self):

for vol in self.volume_subdomains:
D = vol.material.get_diffusion_coefficient(
self.mesh.mesh, self.temperature, spe, self.species
self.mesh.mesh, self.temperature, spe
)

self.formulation += dot(D * grad(u), grad(v)) * self.dx(vol.id)
Expand Down Expand Up @@ -310,7 +310,7 @@ def run(self):

n = self.mesh.n
D = self.subdomains[0].material.get_diffusion_coefficient(
self.mesh.mesh, self.temperature, self.species[0], self.species
self.mesh.mesh, self.temperature, self.species[0]
)
cm = self.species[0].solution
progress = tqdm.autonotebook.tqdm(
Expand Down
32 changes: 16 additions & 16 deletions festim/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@ def __init__(self, D_0, E_D, name=None) -> None:
self.E_D = E_D
self.name = name

def get_diffusion_coefficient(self, mesh, temperature, species, model_species):
def get_diffusion_coefficient(self, mesh, temperature, species=None):
"""Defines the diffusion coefficient
Args:
mesh (dolfinx.mesh.Mesh): the domain mesh
temperature (dolfinx.fem.Constant): the temperature
species (festim.Species): the species
model_species (list): the list of species in the model
species (festim.Species, optional): the species we want the diffusion
coefficient of. Only needed if D_0 and E_D are dicts.
Returns:
ufl.algebra.Product: the diffusion coefficient
"""
if species not in model_species:
raise ValueError(f"Species {species} not found in model species")

if isinstance(self.D_0, (float, int)) and isinstance(self.E_D, (float, int)):
D_0 = F.as_fenics_constant(self.D_0, mesh)
Expand All @@ -57,24 +55,26 @@ def get_diffusion_coefficient(self, mesh, temperature, species, model_species):

elif isinstance(self.D_0, dict) and isinstance(self.E_D, dict):
# check D_0 and E_D have the same keys
# this check should go in a setter
if list(self.D_0.keys()) != list(self.E_D.keys()):
raise ValueError("D_0 and E_D have different keys")

for key in self.D_0.keys():
if isinstance(key, str):
F.find_species_from_name(key, model_species)
elif key not in model_species:
raise ValueError(f"Species {key} not found in model species")
if species is None:
raise ValueError("species must be provided if D_0 and E_D are dicts")

Check warning on line 63 in festim/material.py

View check run for this annotation

Codecov / codecov/patch

festim/material.py#L63

Added line #L63 was not covered by tests

try:
D_0 = F.as_fenics_constant(self.D_0[species.name], mesh)
except KeyError:
if species in self.D_0:
D_0 = F.as_fenics_constant(self.D_0[species], mesh)
elif species.name in self.D_0:
D_0 = F.as_fenics_constant(self.D_0[species.name], mesh)
else:
raise ValueError(f"{species} is not in D_0 keys")

Check warning on line 70 in festim/material.py

View check run for this annotation

Codecov / codecov/patch

festim/material.py#L70

Added line #L70 was not covered by tests

try:
E_D = F.as_fenics_constant(self.E_D[species.name], mesh)
except KeyError:
if species in self.E_D:
E_D = F.as_fenics_constant(self.E_D[species], mesh)
elif species.name in self.E_D:
E_D = F.as_fenics_constant(self.E_D[species.name], mesh)
else:
raise ValueError(f"{species} is not in E_D keys")

Check warning on line 77 in festim/material.py

View check run for this annotation

Codecov / codecov/patch

festim/material.py#L77

Added line #L77 was not covered by tests

return D_0 * ufl.exp(-E_D / F.k_B / temperature)

Expand Down
82 changes: 9 additions & 73 deletions test/test_material.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def test_define_diffusion_coefficient():
T, D_0, E_D = 10, 1.2, 0.5
dum_spe = F.Species("dummy")
my_mat = F.Material(D_0=D_0, E_D=E_D)
D = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=dum_spe, model_species=[dum_spe]
)
D = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=dum_spe)

D_analytical = D_0 * np.exp(-E_D / F.k_B / T)

Expand All @@ -26,15 +24,10 @@ def test_multispecies_dict_strings():
D_0_A, D_0_B = 1, 2
E_D_A, E_D_B = 0.1, 0.2
A, B = F.Species("A"), F.Species("B")
spe_list = [A, B]

my_mat = F.Material(D_0={"A": D_0_A, "B": D_0_B}, E_D={"A": E_D_A, "B": E_D_B})
D_A = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=A, model_species=spe_list
)
D_B = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=B, model_species=spe_list
)
D_A = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=A)
D_B = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=B)

D = [float(D_A), float(D_B)]

Expand All @@ -55,15 +48,10 @@ def test_multispecies_dict_objects():

A = F.Species("A")
B = F.Species("B")
spe_list = [A, B]

my_mat = F.Material(D_0={A: D_0_A, B: D_0_B}, E_D={A: E_D_A, B: E_D_B})
D_A = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=A, model_species=spe_list
)
D_B = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=B, model_species=spe_list
)
D_A = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=A)
D_B = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=B)
D = [float(D_A), float(D_B)]

D_A_analytical = D_0_A * np.exp(-E_D_A / F.k_B / T)
Expand All @@ -83,15 +71,10 @@ def test_multispecies_dict_objects_and_strings():

A = F.Species("A")
B = F.Species("B")
spe_list = [A, B]

my_mat = F.Material(D_0={A: D_0_A, "B": D_0_B}, E_D={A: E_D_A, "B": E_D_B})
D_A = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=A, model_species=spe_list
)
D_B = my_mat.get_diffusion_coefficient(
test_mesh.mesh, T, species=B, model_species=spe_list
)
D_A = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=A)
D_B = my_mat.get_diffusion_coefficient(test_mesh.mesh, T, species=B)
D = [float(D_A), float(D_B)]

D_A_analytical = D_0_A * np.exp(-E_D_A / F.k_B / T)
Expand All @@ -106,64 +89,17 @@ def test_multispecies_dict_different_keys():
"""Test that a value error is rasied when the keys of the D_0 and E_D
are not the same"""
A = F.Species("A")
spe_list = [A]
my_mat = F.Material(D_0={"A": 1, "B": 2}, E_D={"A": 0.1, "B": 0.2, "C": 0.3})

with pytest.raises(ValueError, match="D_0 and E_D have different keys"):
my_mat.get_diffusion_coefficient(
test_mesh.mesh, 500, species=A, model_species=spe_list
)


def test_multispecies_dict_wrong_name_species_not_found():
"""Test that a value error is rasied when the length of the D_0 and E_D
are not the same"""
J = F.Species("J")
spe_list = [J]
my_mat = F.Material(D_0={"A": 1, "B": 2}, E_D={"A": 0.1, "B": 0.2})

with pytest.raises(ValueError, match="Species A not found in list of species"):
my_mat.get_diffusion_coefficient(
test_mesh.mesh, 500, species=J, model_species=spe_list
)


def test_multispecies_dict_contains_species_not_in_species_list():
"""Test that a value error is rasied in the get_diffusion_coefficient
function"""
J = F.Species("J")
A = F.Species("A")
spe_list = [J]
my_mat = F.Material(D_0={A: 1, "B": 2}, E_D={A: 0.1, "B": 0.2})

with pytest.raises(ValueError, match="Species A not found in model species"):
my_mat.get_diffusion_coefficient(
test_mesh.mesh, 500, species=J, model_species=spe_list
)


def test_contains_species_not_in_species_list():
"""Test that a value error is rasied in the get_diffusion_coefficient
function with one species"""
J = F.Species("J")
A = F.Species("A")
spe_list = [J]
my_mat = F.Material(D_0=1, E_D=0)

with pytest.raises(ValueError, match="Species A not found in model species"):
my_mat.get_diffusion_coefficient(
test_mesh.mesh, 500, species=A, model_species=spe_list
)
my_mat.get_diffusion_coefficient(test_mesh.mesh, 500, species=A)


def test_D_0_type_rasies_error():
"""Test that a value error is rasied in the get_diffusion_coefficient
function"""
A = F.Species("A")
spe_list = [A]
my_mat = F.Material(D_0=[1, 1], E_D=0.1)

with pytest.raises(ValueError, match="D_0 and E_D must be either floats or dicts"):
my_mat.get_diffusion_coefficient(
test_mesh.mesh, 500, species=A, model_species=spe_list
)
my_mat.get_diffusion_coefficient(test_mesh.mesh, 500, species=A)
6 changes: 2 additions & 4 deletions test/test_permeation_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_permeation_problem(mesh_size=1001):
# -------------------------- analytical solution -------------------------------------

D = my_mat.get_diffusion_coefficient(
my_mesh.mesh, my_model.temperature, my_model.species[0], my_model.species
my_mesh.mesh, my_model.temperature, my_model.species[0]
)

S_0 = float(my_model.boundary_conditions[-1].S_0)
Expand Down Expand Up @@ -155,9 +155,7 @@ def test_permeation_problem_multi_volume():
times, flux_values = my_model.run()

# ---------------------- analytical solution -----------------------------
D = my_mat.get_diffusion_coefficient(
my_mesh.mesh, temperature, my_model.species[0], model_species=my_model.species
)
D = my_mat.get_diffusion_coefficient(my_mesh.mesh, temperature, my_model.species[0])

S_0 = float(my_model.boundary_conditions[-1].S_0)
E_S = float(my_model.boundary_conditions[-1].E_S)
Expand Down

0 comments on commit fdb5361

Please sign in to comment.