From 2c12d444563d92eeec70df01c712a3cd5fad724f Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Mon, 23 Oct 2023 13:36:26 -0400 Subject: [PATCH 1/2] get_diffusion_coeff doesn't require list of species --- festim/hydrogen_transport_problem.py | 4 +- festim/material.py | 32 +++++------ test/test_material.py | 81 ++++------------------------ test/test_permeation_problem.py | 6 +-- 4 files changed, 29 insertions(+), 94 deletions(-) diff --git a/festim/hydrogen_transport_problem.py b/festim/hydrogen_transport_problem.py index 3fac2caf1..cfbffd189 100644 --- a/festim/hydrogen_transport_problem.py +++ b/festim/hydrogen_transport_problem.py @@ -265,7 +265,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) @@ -307,7 +307,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( diff --git a/festim/material.py b/festim/material.py index 59d8deba1..811766f6d 100644 --- a/festim/material.py +++ b/festim/material.py @@ -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) @@ -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") - 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") - 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") return D_0 * ufl.exp(-E_D / F.k_B / temperature) diff --git a/test/test_material.py b/test/test_material.py index 23a780d9d..df8f70c08 100644 --- a/test/test_material.py +++ b/test/test_material.py @@ -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) @@ -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)] @@ -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) @@ -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) @@ -106,54 +89,10 @@ 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(): @@ -164,6 +103,4 @@ def test_D_0_type_rasies_error(): 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) diff --git a/test/test_permeation_problem.py b/test/test_permeation_problem.py index 587235e99..d69b3dfa6 100644 --- a/test/test_permeation_problem.py +++ b/test/test_permeation_problem.py @@ -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) @@ -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) From 337a35862ace8accccb5bc6248a14b695b5baf55 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Mon, 23 Oct 2023 14:43:07 -0400 Subject: [PATCH 2/2] removed unused variable --- test/test_material.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_material.py b/test/test_material.py index df8f70c08..53cc3f645 100644 --- a/test/test_material.py +++ b/test/test_material.py @@ -99,7 +99,6 @@ 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"):