From 3782d4822d728a9010df6b97d70fa48fa58f40f1 Mon Sep 17 00:00:00 2001 From: JDUFOUR Date: Wed, 10 Jul 2024 11:45:21 +0200 Subject: [PATCH] added test in init and corresponding tests --- festim/boundary_conditions/dirichlets/dirichlet_bc.py | 9 +++++++-- festim/sources/source.py | 5 ++++- test/unit/test_boundary_conditions.py | 9 +++++++++ test/unit/test_sources.py | 7 +++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/festim/boundary_conditions/dirichlets/dirichlet_bc.py b/festim/boundary_conditions/dirichlets/dirichlet_bc.py index 3bc72c561..6f5e20cd4 100644 --- a/festim/boundary_conditions/dirichlets/dirichlet_bc.py +++ b/festim/boundary_conditions/dirichlets/dirichlet_bc.py @@ -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 @@ -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): diff --git a/festim/sources/source.py b/festim/sources/source.py index 839e2f93c..e49548fd5 100644 --- a/festim/sources/source.py +++ b/festim/sources/source.py @@ -1,4 +1,5 @@ from fenics import Constant, Expression, Function, UserExpression +from festim import Temperature import sympy as sp @@ -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) diff --git a/test/unit/test_boundary_conditions.py b/test/unit/test_boundary_conditions.py index f005da8d4..504dc119a 100644 --- a/test/unit/test_boundary_conditions.py +++ b/test/unit/test_boundary_conditions.py @@ -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) diff --git a/test/unit/test_sources.py b/test/unit/test_sources.py index 2bc237214..143b35b11 100644 --- a/test/unit/test_sources.py +++ b/test/unit/test_sources.py @@ -1,4 +1,5 @@ import festim +import pytest import fenics as f import sympy as sp import numpy as np @@ -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")