• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

csdms / bmi-example-python / 11409200998

18 Oct 2024 06:40PM UTC coverage: 93.048%. Remained the same
11409200998

Pull #33

github

mdpiper
Merge branch 'master' into mdpiper/update-examples
Pull Request #33: Update examples + check repository health

174 of 187 relevant lines covered (93.05%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/heat/heat.py
1
"""The 2D heat model."""
2

3
import numpy as np
1✔
4
import yaml
1✔
5
from scipy import ndimage
1✔
6

7

8
def solve_2d(temp, spacing, out=None, alpha=1.0, time_step=1.0):
1✔
9
    """Solve the 2D Heat Equation on a uniform mesh.
10

11
    Parameters
12
    ----------
13
    temp : ndarray
14
        Temperature.
15
    spacing : array_like
16
        Grid spacing in the row and column directions.
17
    out : ndarray (optional)
18
        Output array.
19
    alpha : float (optional)
20
        Thermal diffusivity.
21
    time_step : float (optional)
22
        Time step.
23

24
    Returns
25
    -------
26
    result : ndarray
27
        The temperatures after time *time_step*.
28

29
    Examples
30
    --------
31
    >>> from heat import solve_2d
32
    >>> z0 = np.zeros((3, 3))
33
    >>> z0[1:-1, 1:-1] = 1.
34
    >>> solve_2d(z0, (1., 1.), alpha=.25)
35
    array([[0. , 0. , 0. ],
36
           [0. , 0.5, 0. ],
37
           [0. , 0. , 0. ]])
38
    """
39
    dy2, dx2 = spacing[0] ** 2, spacing[1] ** 2
1✔
40
    stencil = (
1✔
41
        np.array([[0.0, dy2, 0.0], [dx2, -2.0 * (dx2 + dy2), dx2], [0.0, dy2, 0.0]])
42
        * alpha
43
        * time_step
44
        / (2.0 * (dx2 * dy2))
45
    )
46

47
    if out is None:
1✔
48
        out = np.empty_like(temp)
1✔
49

50
    ndimage.convolve(temp, stencil, output=out)
1✔
51
    out[(0, -1), :] = 0.0
1✔
52
    out[:, (0, -1)] = 0.0
1✔
53
    return np.add(temp, out, out=out)
1✔
54

55

56
class Heat:
1✔
57

58
    """Solve the Heat equation on a grid.
59

60
    Examples
61
    --------
62
    >>> heat = Heat()
63
    >>> heat.time
64
    0.0
65
    >>> heat.time_step
66
    0.25
67
    >>> heat.advance_in_time()
68
    >>> heat.time
69
    0.25
70

71
    >>> heat = Heat(shape=(5, 5))
72
    >>> heat.temperature = np.zeros_like(heat.temperature)
73
    >>> heat.temperature[2, 2] = 1.
74
    >>> heat.advance_in_time()
75

76
    >>> heat = Heat(alpha=.5)
77
    >>> heat.time_step
78
    0.5
79
    >>> heat = Heat(alpha=.5, spacing=(2., 3.))
80
    >>> heat.time_step
81
    2.0
82
    """
83

84
    def __init__(
1✔
85
        self, shape=(10, 20), spacing=(1.0, 1.0), origin=(0.0, 0.0), alpha=1.0
86
    ):
87
        """Create a new heat model.
88

89
        Parameters
90
        ---------
91
        shape : array_like, optional
92
            The shape of the solution grid as (*rows*, *columns*).
93
        spacing : array_like, optional
94
            Spacing of grid rows and columns.
95
        origin : array_like, optional
96
            Coordinates of lower left corner of grid.
97
        alpha : float
98
            Alpha parameter in the heat equation.
99
        """
100
        self._shape = shape
1✔
101
        self._spacing = spacing
1✔
102
        self._origin = origin
1✔
103
        self._time = 0.0
1✔
104
        self._alpha = alpha
1✔
105
        self._time_step = min(spacing) ** 2 / (4.0 * self._alpha)
1✔
106

107
        self._temperature = np.random.random(self._shape)
1✔
108
        self._next_temperature = np.empty_like(self._temperature)
1✔
109

110
    @property
1✔
111
    def time(self):
1✔
112
        """Current model time."""
113
        return self._time
1✔
114

115
    @property
1✔
116
    def temperature(self):
1✔
117
        """Temperature of the plate."""
118
        return self._temperature
1✔
119

120
    @temperature.setter
1✔
121
    def temperature(self, new_temp):
1✔
122
        """Set the temperature of the plate.
123

124
        Parameters
125
        ----------
126
        new_temp : array_like
127
            The new temperatures.
128
        """
129
        self._temperature[:] = new_temp
1✔
130

131
    @property
1✔
132
    def time_step(self):
1✔
133
        """Model time step."""
134
        return self._time_step
1✔
135

136
    @time_step.setter
1✔
137
    def time_step(self, time_step):
1✔
138
        """Set model time step."""
139
        self._time_step = time_step
1✔
140

141
    @property
1✔
142
    def shape(self):
1✔
143
        """Shape of the model grid."""
144
        return self._shape
1✔
145

146
    @property
1✔
147
    def spacing(self):
1✔
148
        """Spacing between nodes of the model grid."""
149
        return self._spacing
1✔
150

151
    @property
1✔
152
    def origin(self):
1✔
153
        """Origin coordinates of the model grid."""
154
        return self._origin
1✔
155

156
    @classmethod
1✔
157
    def from_file_like(cls, file_like):
1✔
158
        """Create a Heat object from a file-like object.
159

160
        Parameters
161
        ----------
162
        file_like : file_like
163
            Input parameter file.
164

165
        Returns
166
        -------
167
        Heat
168
            A new instance of a Heat object.
169
        """
170
        config = yaml.safe_load(file_like)
1✔
171
        return cls(**config)
1✔
172

173
    def advance_in_time(self):
1✔
174
        """Calculate new temperatures for the next time step."""
175
        solve_2d(
1✔
176
            self._temperature,
177
            self._spacing,
178
            out=self._next_temperature,
179
            alpha=self._alpha,
180
            time_step=self._time_step,
181
        )
182
        np.copyto(self._temperature, self._next_temperature)
1✔
183

184
        self._time += self._time_step
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc