The Diffusion Equation

The Diffusion Equation

from numpy import pi
from pde import PDE, CartesianGrid, ScalarField, DiffusionPDE, MemoryStorage, plot_kymograph, plot_interactive, movie
grid = CartesianGrid([[0, 1]], 101, periodic=False)
state = ScalarField.from_expression(grid, "cos(6*pi*x)")
bc_x_left = {"derivative": 0.0}
bc_x_right = {"derivative": 0.0}
bc_x = [bc_x_left, bc_x_right]
eq_1 = DiffusionPDE(diffusivity=0.01,bc=[bc_x])
storage_1 = MemoryStorage()
result_1 = eq_1.solve(state, t_range=2, method="scipy", tracker=storage_1.tracker(0.1))
result_2 = eq_1.solve(state, t_range=2)
Explicit stepper with a fixed time step did not receive any initial value for `dt`. Using dt=0.001, but specifying a value or enabling adaptive stepping is advisable.
result_1
ScalarField(grid=CartesianGrid(bounds=((0.0, 1.0),), shape=(101,), periodic=[False]), data=[ 8.33505818e-04  8.04546889e-04  7.47681407e-04  6.64791372e-04
  5.58893921e-04  4.33484063e-04  2.93142802e-04  1.42477606e-04
 -1.29738043e-05 -1.68153942e-04 -3.17299097e-04 -4.55633748e-04
 -5.77923151e-04 -6.80373036e-04 -7.58954450e-04 -8.11422899e-04
 -8.35462607e-04 -8.30736299e-04 -7.96911606e-04 -7.35653330e-04
 -6.48610759e-04 -5.39267502e-04 -4.10983564e-04 -2.68622594e-04
 -1.16755163e-04  3.90116023e-05  1.93554303e-04  3.41271773e-04
  4.77209795e-04  5.96535009e-04  6.95146516e-04  7.69650755e-04
  8.17349615e-04  8.36781441e-04  8.26987614e-04  7.88685164e-04
  7.22730332e-04  6.31988376e-04  5.18932112e-04  3.88273299e-04
  2.43655098e-04  9.11057525e-05 -6.51952084e-05 -2.18587313e-04
 -3.65089929e-04 -4.98153994e-04 -6.14733491e-04 -7.09091162e-04
 -7.79750455e-04 -8.22346862e-04 -8.37419374e-04 -8.22321163e-04
 -7.79801741e-04 -7.09014513e-04 -6.14835171e-04 -4.98027722e-04
 -3.65240246e-04 -2.18413601e-04 -6.53915691e-05  9.13239193e-05
  2.43416058e-04  3.88532195e-04  5.18654459e-04  6.32283614e-04
  7.22418749e-04  7.89011790e-04  8.26647302e-04  8.37134033e-04
  8.16986191e-04  7.70023530e-04  6.94765901e-04  5.96921933e-04
  4.76818107e-04  3.41666671e-04  1.93157748e-04  3.94082655e-05
 -1.17150398e-04 -2.68230306e-04 -4.11371409e-04 -5.38885567e-04
 -6.48985351e-04 -7.35287475e-04 -7.97267373e-04 -8.30391924e-04
 -8.35794337e-04 -8.11105010e-04 -7.59257360e-04 -6.80086183e-04
 -5.78192933e-04 -4.55381984e-04 -3.17531965e-04 -1.67940778e-04
 -1.31665292e-05  1.42649230e-04  2.92992866e-04  4.33611801e-04
  5.58788814e-04  6.64873492e-04  7.47622552e-04  8.04582281e-04
  8.33494007e-04])
result_2
ScalarField(grid=CartesianGrid(bounds=((0.0, 1.0),), shape=(101,), periodic=[False]), data=[ 8.23082346e-04  7.94497105e-04  7.38319373e-04  6.56500177e-04
  5.51881055e-04  4.28095378e-04  2.89442161e-04  1.40736762e-04
 -1.28563554e-05 -1.66002978e-04 -3.13384399e-04 -4.49882136e-04
 -5.70755690e-04 -6.71807181e-04 -7.49527143e-04 -8.01216400e-04
 -8.25079808e-04 -8.20288605e-04 -7.87009185e-04 -7.26397327e-04
 -6.40558051e-04 -5.32472511e-04 -4.05894466e-04 -2.65219907e-04
 -1.15334392e-04  3.85566284e-05  1.91108597e-04  3.37023458e-04
  4.71233662e-04  5.89078155e-04  6.86464256e-04  7.60009794e-04
  8.07160570e-04  8.26279059e-04  8.16701287e-04  7.78759885e-04
  7.13772538e-04  6.23996226e-04  5.12548834e-04  3.83300876e-04
  2.40741069e-04  8.98204441e-05 -6.42196004e-05 -2.16029330e-04
 -3.60336468e-04 -4.92129300e-04 -6.06830725e-04 -7.00457221e-04
 -7.69757185e-04 -8.12323864e-04 -8.26678938e-04 -8.12323864e-04
 -7.69757185e-04 -7.00457221e-04 -6.06830725e-04 -4.92129300e-04
 -3.60336468e-04 -2.16029330e-04 -6.42196004e-05  8.98204441e-05
  2.40741069e-04  3.83300876e-04  5.12548834e-04  6.23996226e-04
  7.13772538e-04  7.78759885e-04  8.16701287e-04  8.26279059e-04
  8.07160570e-04  7.60009794e-04  6.86464256e-04  5.89078155e-04
  4.71233662e-04  3.37023458e-04  1.91108597e-04  3.85566284e-05
 -1.15334392e-04 -2.65219907e-04 -4.05894466e-04 -5.32472511e-04
 -6.40558051e-04 -7.26397327e-04 -7.87009185e-04 -8.20288605e-04
 -8.25079808e-04 -8.01216400e-04 -7.49527143e-04 -6.71807181e-04
 -5.70755690e-04 -4.49882136e-04 -3.13384399e-04 -1.66002978e-04
 -1.28563554e-05  1.40736762e-04  2.89442161e-04  4.28095378e-04
  5.51881055e-04  6.56500177e-04  7.38319373e-04  7.94497105e-04
  8.23082346e-04])
result_1.plot()

<pde.tools.plotting.PlotReference at 0x13867d140>
result_2.plot()

<pde.tools.plotting.PlotReference at 0x138749080>
plot_kymograph(storage_1)

<pde.tools.plotting.PlotReference at 0x1388230c0>
eq_2 = PDE({"φ": "0.01 * laplace(φ)"},bc=[bc_x])
storage_2 = MemoryStorage()
result_3 = eq_2.solve(state, t_range=2, method="scipy", tracker=storage_2.tracker(0.1))
result_4 = eq_2.solve(state, t_range=2)
Explicit stepper with a fixed time step did not receive any initial value for `dt`. Using dt=0.001, but specifying a value or enabling adaptive stepping is advisable.
result_3
ScalarField(grid=CartesianGrid(bounds=((0.0, 1.0),), shape=(101,), periodic=[False]), data=[ 8.33505818e-04  8.04546889e-04  7.47681407e-04  6.64791372e-04
  5.58893921e-04  4.33484063e-04  2.93142802e-04  1.42477606e-04
 -1.29738043e-05 -1.68153942e-04 -3.17299097e-04 -4.55633748e-04
 -5.77923151e-04 -6.80373036e-04 -7.58954450e-04 -8.11422899e-04
 -8.35462607e-04 -8.30736299e-04 -7.96911606e-04 -7.35653330e-04
 -6.48610759e-04 -5.39267502e-04 -4.10983564e-04 -2.68622594e-04
 -1.16755163e-04  3.90116023e-05  1.93554303e-04  3.41271773e-04
  4.77209795e-04  5.96535009e-04  6.95146516e-04  7.69650755e-04
  8.17349615e-04  8.36781441e-04  8.26987614e-04  7.88685164e-04
  7.22730332e-04  6.31988376e-04  5.18932112e-04  3.88273299e-04
  2.43655098e-04  9.11057525e-05 -6.51952084e-05 -2.18587313e-04
 -3.65089929e-04 -4.98153994e-04 -6.14733491e-04 -7.09091162e-04
 -7.79750455e-04 -8.22346862e-04 -8.37419374e-04 -8.22321163e-04
 -7.79801741e-04 -7.09014513e-04 -6.14835171e-04 -4.98027722e-04
 -3.65240246e-04 -2.18413601e-04 -6.53915691e-05  9.13239193e-05
  2.43416058e-04  3.88532195e-04  5.18654459e-04  6.32283614e-04
  7.22418749e-04  7.89011790e-04  8.26647302e-04  8.37134033e-04
  8.16986191e-04  7.70023530e-04  6.94765901e-04  5.96921933e-04
  4.76818107e-04  3.41666671e-04  1.93157748e-04  3.94082655e-05
 -1.17150398e-04 -2.68230306e-04 -4.11371409e-04 -5.38885567e-04
 -6.48985351e-04 -7.35287475e-04 -7.97267373e-04 -8.30391924e-04
 -8.35794337e-04 -8.11105010e-04 -7.59257360e-04 -6.80086183e-04
 -5.78192933e-04 -4.55381984e-04 -3.17531965e-04 -1.67940778e-04
 -1.31665292e-05  1.42649230e-04  2.92992866e-04  4.33611801e-04
  5.58788814e-04  6.64873492e-04  7.47622552e-04  8.04582281e-04
  8.33494007e-04])
result_4
ScalarField(grid=CartesianGrid(bounds=((0.0, 1.0),), shape=(101,), periodic=[False]), data=[ 8.23082346e-04  7.94497105e-04  7.38319373e-04  6.56500177e-04
  5.51881055e-04  4.28095378e-04  2.89442161e-04  1.40736762e-04
 -1.28563554e-05 -1.66002978e-04 -3.13384399e-04 -4.49882136e-04
 -5.70755690e-04 -6.71807181e-04 -7.49527143e-04 -8.01216400e-04
 -8.25079808e-04 -8.20288605e-04 -7.87009185e-04 -7.26397327e-04
 -6.40558051e-04 -5.32472511e-04 -4.05894466e-04 -2.65219907e-04
 -1.15334392e-04  3.85566284e-05  1.91108597e-04  3.37023458e-04
  4.71233662e-04  5.89078155e-04  6.86464256e-04  7.60009794e-04
  8.07160570e-04  8.26279059e-04  8.16701287e-04  7.78759885e-04
  7.13772538e-04  6.23996226e-04  5.12548834e-04  3.83300876e-04
  2.40741069e-04  8.98204441e-05 -6.42196004e-05 -2.16029330e-04
 -3.60336468e-04 -4.92129300e-04 -6.06830725e-04 -7.00457221e-04
 -7.69757185e-04 -8.12323864e-04 -8.26678938e-04 -8.12323864e-04
 -7.69757185e-04 -7.00457221e-04 -6.06830725e-04 -4.92129300e-04
 -3.60336468e-04 -2.16029330e-04 -6.42196004e-05  8.98204441e-05
  2.40741069e-04  3.83300876e-04  5.12548834e-04  6.23996226e-04
  7.13772538e-04  7.78759885e-04  8.16701287e-04  8.26279059e-04
  8.07160570e-04  7.60009794e-04  6.86464256e-04  5.89078155e-04
  4.71233662e-04  3.37023458e-04  1.91108597e-04  3.85566284e-05
 -1.15334392e-04 -2.65219907e-04 -4.05894466e-04 -5.32472511e-04
 -6.40558051e-04 -7.26397327e-04 -7.87009185e-04 -8.20288605e-04
 -8.25079808e-04 -8.01216400e-04 -7.49527143e-04 -6.71807181e-04
 -5.70755690e-04 -4.49882136e-04 -3.13384399e-04 -1.66002978e-04
 -1.28563554e-05  1.40736762e-04  2.89442161e-04  4.28095378e-04
  5.51881055e-04  6.56500177e-04  7.38319373e-04  7.94497105e-04
  8.23082346e-04])
result_3.plot()

<pde.tools.plotting.PlotReference at 0x137b513c0>
result_4.plot()

<pde.tools.plotting.PlotReference at 0x1375bb580>
plot_kymograph(storage_2)

<pde.tools.plotting.PlotReference at 0x137ab6580>
plot_interactive(storage_1)
for time, field in storage_1.items():
    print(f"t={time}, field={field.magnitude}")
t=0, field=1.734723475976807e-17
t=0.1, field=1.734723475976807e-18
t=0.2, field=8.673617379884035e-18
t=0.30000000000000004, field=1.1275702593849246e-17
t=0.4, field=1.1709383462843448e-17
t=0.5, field=1.214306433183765e-17
t=0.6, field=1.1926223897340549e-17
t=0.7, field=1.463672932855431e-17
t=0.7999999999999999, field=1.6046192152785466e-17
t=0.8999999999999999, field=1.7184604433895245e-17
t=0.9999999999999999, field=1.7076184216646695e-17
t=1.0999999999999999, field=1.6832238727837456e-17
t=1.2, field=1.6750923564901044e-17
t=1.3, field=1.6886448836461732e-17
t=1.4000000000000001, field=1.6710265983432837e-17
t=1.5000000000000002, field=1.6771252355635147e-17
t=1.6000000000000003, field=1.6757699828479078e-17
t=1.7000000000000004, field=1.669332532448775e-17
t=1.8000000000000005, field=1.6723818510588906e-17
t=1.9000000000000006, field=1.6757699828479078e-17
t=2.0, field=1.6745841367217518e-17
for time, field in storage_2.items():
    print(f"t={time}, field={field.magnitude}")
t=0, field=1.734723475976807e-17
t=0.1, field=1.734723475976807e-18
t=0.2, field=8.673617379884035e-18
t=0.30000000000000004, field=1.1275702593849246e-17
t=0.4, field=1.1709383462843448e-17
t=0.5, field=1.214306433183765e-17
t=0.6, field=1.1926223897340549e-17
t=0.7, field=1.463672932855431e-17
t=0.7999999999999999, field=1.6046192152785466e-17
t=0.8999999999999999, field=1.7184604433895245e-17
t=0.9999999999999999, field=1.7076184216646695e-17
t=1.0999999999999999, field=1.6832238727837456e-17
t=1.2, field=1.6750923564901044e-17
t=1.3, field=1.6886448836461732e-17
t=1.4000000000000001, field=1.6710265983432837e-17
t=1.5000000000000002, field=1.6771252355635147e-17
t=1.6000000000000003, field=1.6757699828479078e-17
t=1.7000000000000004, field=1.669332532448775e-17
t=1.8000000000000005, field=1.6723818510588906e-17
t=1.9000000000000006, field=1.6757699828479078e-17
t=2.0, field=1.6745841367217518e-17
movie(storage_1, filename="test_movie_1.mp4",movie_args = {"framerate": 2.0})
movie(storage_2, filename="test_movie_2.mp4",movie_args = {"framerate": 2.0})
from IPython.display import Video
Video('test_movie_1.mp4')