Skip to content

Coffee (Newton's Law of Cooling)

dissmodel.models.sysdyn.cofee.Coffee

Bases: Model

Newton's Law of Cooling model.

Simulates the cooling of a hot beverage towards room temperature. At each step, the temperature decreases proportionally to the difference between the current temperature and room temperature.

Parameters:

Name Type Description Default
temperature float

Initial temperature of the beverage in degrees, by default 80.

required
room_temperature float

Ambient room temperature in degrees, by default 20.

required
cooling_rate float

Proportionality constant controlling cooling speed, by default 0.1.

required
Notes

The @track_plot decorator registers temperature for automatic live plotting. Any :class:~dissmodel.visualization.Chart connected to the same environment will plot it at every step without any extra configuration.

The temperature update at each step follows Newton's Law of Cooling:

.. math::

T_{t+1} = T_t - k \times (T_t - T_{room})

Examples:

>>> from dissmodel.core import Environment
>>> env = Environment(end_time=30)
>>> coffee = Coffee()
>>> env.run()
Source code in dissmodel/models/sysdyn/cofee.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@track_plot("Temperature", "blue")
class Coffee(Model):
    """
    Newton's Law of Cooling model.

    Simulates the cooling of a hot beverage towards room temperature.
    At each step, the temperature decreases proportionally to the
    difference between the current temperature and room temperature.

    Parameters
    ----------
    temperature : float, optional
        Initial temperature of the beverage in degrees, by default 80.
    room_temperature : float, optional
        Ambient room temperature in degrees, by default 20.
    cooling_rate : float, optional
        Proportionality constant controlling cooling speed,
        by default 0.1.

    Notes
    -----
    The ``@track_plot`` decorator registers ``temperature`` for automatic
    live plotting. Any :class:`~dissmodel.visualization.Chart` connected
    to the same environment will plot it at every step without any extra
    configuration.

    The temperature update at each step follows Newton's Law of Cooling:

    .. math::

        T_{t+1} = T_t - k \\times (T_t - T_{room})

    Examples
    --------
    >>> from dissmodel.core import Environment
    >>> env = Environment(end_time=30)
    >>> coffee = Coffee()
    >>> env.run()
    """

    #: Current temperature of the beverage.
    temperature: float

    #: Ambient room temperature.
    room_temperature: float

    #: Proportionality constant controlling cooling speed.
    cooling_rate: float

    def setup(
        self,
        temperature: float = 80.0,
        room_temperature: float = 20.0,
        cooling_rate: float = 0.1,
    ) -> None:
        """
        Configure the model parameters.

        Parameters
        ----------
        temperature : float, optional
            Initial temperature of the beverage, by default 80.0.
        room_temperature : float, optional
            Ambient room temperature, by default 20.0.
        cooling_rate : float, optional
            Proportionality constant controlling cooling speed,
            by default 0.1.
        """
        self.temperature = temperature
        self.room_temperature = room_temperature
        self.cooling_rate = cooling_rate

    def execute(self) -> None:
        """
        Advance the model by one time step.

        Updates the temperature according to Newton's Law of Cooling.
        """
        self.temperature -= self.cooling_rate * (self.temperature - self.room_temperature)

execute()

Advance the model by one time step.

Updates the temperature according to Newton's Law of Cooling.

Source code in dissmodel/models/sysdyn/cofee.py
79
80
81
82
83
84
85
def execute(self) -> None:
    """
    Advance the model by one time step.

    Updates the temperature according to Newton's Law of Cooling.
    """
    self.temperature -= self.cooling_rate * (self.temperature - self.room_temperature)

setup(temperature=80.0, room_temperature=20.0, cooling_rate=0.1)

Configure the model parameters.

Parameters:

Name Type Description Default
temperature float

Initial temperature of the beverage, by default 80.0.

80.0
room_temperature float

Ambient room temperature, by default 20.0.

20.0
cooling_rate float

Proportionality constant controlling cooling speed, by default 0.1.

0.1
Source code in dissmodel/models/sysdyn/cofee.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def setup(
    self,
    temperature: float = 80.0,
    room_temperature: float = 20.0,
    cooling_rate: float = 0.1,
) -> None:
    """
    Configure the model parameters.

    Parameters
    ----------
    temperature : float, optional
        Initial temperature of the beverage, by default 80.0.
    room_temperature : float, optional
        Ambient room temperature, by default 20.0.
    cooling_rate : float, optional
        Proportionality constant controlling cooling speed,
        by default 0.1.
    """
    self.temperature = temperature
    self.room_temperature = room_temperature
    self.cooling_rate = cooling_rate