Skip to content

Commit

Permalink
same for sympy Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
RemDelaporteMathurin committed Jun 4, 2024
1 parent f259df8 commit ede311e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
18 changes: 13 additions & 5 deletions docs/source/userguide/temperature.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Temperature
===========

Definition of a temperature field or problem is essential for hydrogen transport
and FESTIM as a whole.
and FESTIM as a whole.
Regardless of how you define the temperature of the problem, it is passed to the :code:`T` attribute of the :class:`festim.Simulation` object.

----------------------
Analytical expressions
Expand All @@ -16,7 +17,7 @@ The temperature can be defined as a constant value in Kelvin (K):
my_temperature = 300
Temperature can also be defined as an expression of time and/or space using the :class:`festim.Temperature`.
Temperature can also be defined as an expression of time and/or space.
For example:

.. math::
Expand All @@ -29,7 +30,7 @@ would be passed to FESTIM as:
from festim import x, t
my_temp = Temperature(300+2*x+3*t)
my_temp = 300 + 2*x + 3*t
More complex expressions can be expressed with sympy:

Expand All @@ -44,9 +45,16 @@ would be passed to FESTIM as:
from festim import x, t
import sympy as sp
my_temp = Temperature(sp.exp(x)*sp.sin(t))
my_temp = sp.exp(x) * sp.sin(t)
For more details, see :class:`festim.Temperature`.
Conditional expressions are also possible:

.. code-block:: python
from festim import x, t
import sympy as sp
my_temp = sp.Piecewise((400, F.t < 10), (300, True))
---------------------------
From a heat transfer solver
Expand Down
5 changes: 3 additions & 2 deletions festim/generic_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from festim.h_transport_problem import HTransportProblem
from fenics import *
import numpy as np
import sympy as sp
import warnings


Expand All @@ -25,7 +26,7 @@ class Simulation:
None.
settings (festim.Settings, optional): The model's settings.
Defaults to None.
temperature (int, float, festim.Temperature, optional): The model's
temperature (int, float, sympy.Expr, festim.Temperature, optional): The model's
temperature. Can be an expression or a heat transfer model.
Defaults to None.
initial_conditions (list of festim.InitialCondition, optional):
Expand Down Expand Up @@ -174,7 +175,7 @@ def T(self, value):
self._T = value
elif value is None:
self._T = value
elif isinstance(value, (int, float)):
elif isinstance(value, (int, float, sp.Expr)):
self._T = festim.Temperature(value)

def attribute_source_terms(self):
Expand Down
20 changes: 17 additions & 3 deletions test/simulation/test_initialise.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import festim as F
from pathlib import Path
import pytest
import sympy as sp


def test_initialise_changes_nb_of_sources():
Expand Down Expand Up @@ -149,11 +150,24 @@ def test_cartesian_and_surface_flux_warning(quantity, sys):
my_model.initialise()


@pytest.mark.parametrize("value", [100, 0, 100.0])
def test_initialise_temp_as_number(value):
@pytest.mark.parametrize(
"value",
[
100,
0,
100.0,
0.0,
100 + 1 * F.x,
0 + 1 * F.x,
F.x,
F.t,
sp.Piecewise((400, F.t < 10), (300, True)),
],
)
def test_initialise_temp_as_number_or_sympy(value):
"""
Creates a Simulation object and checks that the T attribute
can be given as an int or float
can be given as an int, a float or a sympy Expr
"""
# build
my_model = F.Simulation()
Expand Down

0 comments on commit ede311e

Please sign in to comment.