Skip to content

Commit

Permalink
Merge pull request #812 from JonathanGSDUFOUR/Error_if_T_and_BC_with_T
Browse files Browse the repository at this point in the history
Error if temperature BC or source with `F.Temperature`
  • Loading branch information
RemDelaporteMathurin authored Jul 24, 2024
2 parents a9d6147 + aba4540 commit 47d7dc4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
12 changes: 12 additions & 0 deletions festim/generic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def attribute_source_terms(self):

# set sources
for source in self.sources:
if source.field == "T" and not isinstance(
self.T, festim.HeatTransferProblem
): # check that there is not a source defined in T as the same time as a festim.Temperature
raise TypeError(
"Heat transfer sources can only be used with HeatTransferProblem"
)
if isinstance(source, festim.RadioactiveDecay) and source.field == "all":
# assign source to each of the unique festim.Concentration
# objects in field_to_object
Expand Down Expand Up @@ -242,6 +248,12 @@ def check_boundary_conditions(self):
):
raise ValueError("SurfaceKinetics can only be used in 1D simulations")

# check that there is not a Temperature defined at the same time as a boundary condition in T
if bc.field == "T" and not isinstance(self.T, festim.HeatTransferProblem):
raise TypeError(
"Heat transfer boundary conditions can only be used with HeatTransferProblem"
)

# checks that DirichletBC or SurfaceKinetics is not set with another bc on the same surface
# iterate through all BCs
for dc_sk_bc in dc_sk_bcs:
Expand Down
63 changes: 61 additions & 2 deletions test/system/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,68 @@ def test_error_DirichletBC_on_same_surface(field, surfaces):
F.DirichletBC(value=1, field=field, surfaces=surfaces),
]

if field == "solute":
with pytest.raises(
ValueError,
match="DirichletBC is simultaneously set with another boundary condition",
):
sim.initialise()
elif (
field == "T"
): # with festim.Temperature already defined there is another error given
with pytest.raises(
TypeError,
match="Heat transfer boundary conditions can only be used with HeatTransferProblem",
):
sim.initialise()


@pytest.mark.parametrize(
"bc",
[
F.FluxBC(value=1, field="T", surfaces=1),
F.ConvectiveFlux(h_coeff=1, T_ext=1, surfaces=1),
F.DirichletBC(value=1, surfaces=1, field="T"),
],
)
def test_BC_on_T_with_Temp(bc):
"""
Test that the code returns an error when a boundary condition is set
on the Temperature field when there is also a Festim.Temperature already set.
"""

sim = F.Simulation()
sim.mesh = F.MeshFromVertices([0, 1, 2, 3])
sim.materials = F.Material(id=1, D_0=1, E_D=0)
sim.T = F.Temperature(value=500)
sim.boundary_conditions = [bc]
sim.settings = F.Settings(
transient=False, absolute_tolerance=1e8, relative_tolerance=1e-8
)
with pytest.raises(
ValueError,
match="DirichletBC is simultaneously set with another boundary condition",
TypeError,
match="Heat transfer boundary conditions can only be used with HeatTransferProblem",
):
sim.initialise()


def test_source_on_T_with_Temp():
"""
Test that the code returns an error when a source is set
on the Temperature field when there is also a Festim.Temperature already set.
"""

sim = F.Simulation()
sim.mesh = F.MeshFromVertices([0, 1, 2, 3])
sim.materials = F.Material(id=1, D_0=1, E_D=0)
sim.T = F.Temperature(value=500)
sim.settings = F.Settings(
transient=False, absolute_tolerance=1e8, relative_tolerance=1e-8
)
sim.sources = [F.Source(value=1, volume=1, field="T")]
with pytest.raises(
TypeError,
match="Heat transfer sources can only be used with HeatTransferProblem",
):
sim.initialise()

Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_boundary_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def custom_fun(T, solute, param1):
"bc",
[
(festim.DissociationFlux(surfaces=[1], Kd_0=1, E_Kd=0, P=1e4)),
(festim.ConvectiveFlux(h_coeff=1, T_ext=1, surfaces=1)),
# (festim.ConvectiveFlux(h_coeff=1, T_ext=1, surfaces=1)),
(festim.FluxBC(surfaces=1, value=1, field=0)),
(festim.MassFlux(h_coeff=1, c_ext=1, surfaces=1)),
(festim.RecombinationFlux(Kr_0=1e-20, E_Kr=0, order=2, surfaces=1)),
Expand Down

0 comments on commit 47d7dc4

Please sign in to comment.