-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function and class (OOP) examples to Python lesson (#108)
* Minor text edits * Add module with an example funtion * Add example class to model diffusion * Remove lint * Update example to pass doctest * Rename advanced Python section * Swap the order of the last two topics
- Loading branch information
Showing
5 changed files
with
182 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""Modeling the one-dimensional diffusion equation.""" | ||
import numpy as np | ||
|
||
|
||
class Diffusion: | ||
|
||
"""Model one-dimensional diffusion with fixed boundary conditions. | ||
Examples | ||
-------- | ||
>>> import numpy as np | ||
>>> from diffusion import Diffusion | ||
>>> m = Diffusion(diffusivity=0.25) | ||
>>> m.concentration = np.zeros(m.shape) | ||
>>> m.concentration[int(m.shape/2)] = 5 | ||
>>> m.concentration | ||
array([ 0.0, 0.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, | ||
0.0]) | ||
>>> m.time | ||
0.0 | ||
>>> m.update() | ||
>>> m.time | ||
1.0 | ||
>>> m.concentration | ||
array([ 0.0, 0.0, 0.0, 0.0, 1.2, 2.5, 1.2, 0.0, 0.0, | ||
0.0]) | ||
""" | ||
|
||
def __init__(self, shape=10, spacing=1.0, diffusivity=1.0): | ||
"""Create a new diffusion model. | ||
Parameters | ||
--------- | ||
shape : int, optional | ||
The number of nodes in the solution grid. | ||
spacing : float, optional | ||
Grid spacing. | ||
diffusivity : float, optional | ||
Diffusivity. | ||
""" | ||
self.shape = shape | ||
self.spacing = spacing | ||
self.diffusivity = diffusivity | ||
self.time = 0.0 | ||
self.time_step = self.spacing**2 / (4.0 * self.diffusivity) | ||
|
||
self.concentration = np.random.random(self.shape) | ||
|
||
def solve(self): | ||
"""Solve the 1D diffusion equation.""" | ||
flux = -self.diffusivity * np.diff(self.concentration) / self.spacing | ||
self.concentration[1:-1] -= self.time_step * np.diff(flux) / self.spacing | ||
|
||
def update(self): | ||
"""Calculate concentration at the next time step.""" | ||
self.solve() | ||
self.time += self.time_step |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""A solver for the one-dimensional diffusion equation.""" | ||
import numpy as np | ||
|
||
np.set_printoptions(formatter={"float": "{: 5.1f}".format}) | ||
|
||
|
||
def solve1d(concentration, spacing=1.0, time_step=1.0, diffusivity=1.0): | ||
"""Solve the one-dimensional diffusion equation with fixed boundary conditions. | ||
Parameters | ||
---------- | ||
concentration : ndarray | ||
The quantity being diffused. | ||
spacing : float (optional) | ||
Grid spacing. | ||
time_step : float (optional) | ||
Time step. | ||
alpha : float (optional) | ||
Diffusivity. | ||
Returns | ||
------- | ||
result : ndarray | ||
The temperatures after time *time_step*. | ||
Examples | ||
-------- | ||
>>> import numpy as np | ||
>>> from solver import solve1d | ||
>>> z = np.zeros(5) | ||
>>> z[2] = 5 | ||
>>> solve1d(z, diffusivity=0.25) | ||
array([ 0.0, 1.2, 2.5, 1.2, 0.0]) | ||
""" | ||
flux = -diffusivity * np.diff(concentration) / spacing | ||
concentration[1:-1] -= time_step * np.diff(flux) / spacing | ||
|
||
return concentration | ||
|
||
|
||
def _run_example(): | ||
"""An example of running solve1d.""" | ||
print(_run_example.__doc__) | ||
D = 100 | ||
Lx = 10 | ||
dx = 0.5 | ||
C1 = 500 | ||
C2 = 0 | ||
C = np.empty(Lx) | ||
C[: int(Lx / 2)] = C1 | ||
C[int(Lx / 2) :] = C2 | ||
dt = dx * dx / D / 2.1 | ||
print("Time = 0\n", C) | ||
|
||
for t in range(1, 3): | ||
solve1d(C, dx, dt, D) | ||
print(f"Time = {t}\n", C) | ||
|
||
|
||
if __name__ == "__main__": | ||
_run_example() |