Skip to content

Commit

Permalink
added test in init and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanGSDUFOUR committed Jul 10, 2024
1 parent 1add5fc commit 3782d48
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
9 changes: 7 additions & 2 deletions festim/boundary_conditions/dirichlets/dirichlet_bc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from festim import BoundaryCondition, k_B
from festim import BoundaryCondition, k_B, Temperature
import fenics as f
import sympy as sp

Expand All @@ -17,7 +17,12 @@ class DirichletBC(BoundaryCondition):

def __init__(self, surfaces, value, field) -> None:
super().__init__(surfaces, field=field)
self.value = value
if isinstance(value, Temperature):
raise ValueError(
f"F.Temperature should not be used with DirichletBC or daughter class"
)
else:
self.value = value
self.dirichlet_bc = []

def create_expression(self, T):
Expand Down
5 changes: 4 additions & 1 deletion festim/sources/source.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fenics import Constant, Expression, Function, UserExpression
from festim import Temperature
import sympy as sp


Expand All @@ -25,7 +26,9 @@ def __init__(self, value, volume, field) -> None:
self.volume = volume
self.field = field

if isinstance(value, (float, int)):
if isinstance(value, Temperature):
raise ValueError(f"F.Temperature shouldn't be used with Source")
elif isinstance(value, (float, int)):
self.value = Constant(value)
elif isinstance(value, sp.Expr):
self.value = Expression(sp.printing.ccode(value), t=0, degree=2)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/test_boundary_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,12 @@ def J_vs(cs, T, prm1, prm2):
expected_form += (J_bs - J_sb) * solute_test_function * ds(1)

assert my_bc.form.equals(expected_form)


def test_error_if_used_Temperature():
"""Check that an error is returned when using F.Temperature with DirichletBC"""
with pytest.raises(
ValueError,
match="F.Temperature should not be used with DirichletBC or daughter class",
):
festim.DirichletBC(surfaces=1, value=festim.Temperature(value=300), field=0)
7 changes: 7 additions & 0 deletions test/unit/test_sources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import festim
import pytest
import fenics as f
import sympy as sp
import numpy as np
Expand Down Expand Up @@ -91,3 +92,9 @@ def eval(self, value, x):

source = festim.Source(CustomExpr(), volume=1, field="solute")
assert isinstance(source.value, f.UserExpression)


def test_error_if_used_Temperature_with_source():
"""Check that an error is returned when using F.Temperature with source"""
with pytest.raises(ValueError, match="F.Temperature shouldn't be used with Source"):
festim.Source(value=festim.Temperature(value=300), volume=1, field="solute")

0 comments on commit 3782d48

Please sign in to comment.