In [1]:
%pylab inline


Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.

In [2]:
from sympy import init_session
init_session()


Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.
IPython console for SymPy 0.7.5 (Python 2.7.5-64-bit) (ground types: python)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

Documentation can be found at http://www.sympy.org
This is our exact solution, defined on [0,1]x[0,1]x[0,1]

In [25]:
phi = (cos(pi*x) + cos(pi*y) + cos(pi*z))*(1-x)*(1-y)*(1-z)
phi


Out[25]:
$$\left(- x + 1\right) \left(- y + 1\right) \left(- z + 1\right) \left(\cos{\left (\pi x \right )} + \cos{\left (\pi y \right )} + \cos{\left (\pi z \right )}\right)$$
Generate the RHS of our Poisson problem by differentiating

In [16]:
f = diff(phi, x, 2) + diff(phi, y, 2) + diff(phi, z, 2)
f


Out[16]:
$$\pi \left(x - 1\right) \left(y - 1\right) \left(\pi \left(z - 1\right) \cos{\left (\pi z \right )} + 2 \sin{\left (\pi z \right )}\right) + \pi \left(x - 1\right) \left(z - 1\right) \left(\pi \left(y - 1\right) \cos{\left (\pi y \right )} + 2 \sin{\left (\pi y \right )}\right) + \pi \left(y - 1\right) \left(z - 1\right) \left(\pi \left(x - 1\right) \cos{\left (\pi x \right )} + 2 \sin{\left (\pi x \right )}\right)$$

In [24]:
print(f)


pi*(x - 1)*(y - 1)*(pi*(z - 1)*cos(pi*z) + 2*sin(pi*z)) + pi*(x - 1)*(z - 1)*(pi*(y - 1)*cos(pi*y) + 2*sin(pi*y)) + pi*(y - 1)*(z - 1)*(pi*(x - 1)*cos(pi*x) + 2*sin(pi*x))
boundary conditions
x = 0

In [27]:
xl = phi.subs(x, 0)
xl


Out[27]:
$$\left(- y + 1\right) \left(- z + 1\right) \left(\cos{\left (\pi y \right )} + \cos{\left (\pi z \right )} + 1\right)$$

In [28]:
print(xl)


(-y + 1)*(-z + 1)*(cos(pi*y) + cos(pi*z) + 1)
x = 1

In [19]:
phi.subs(x, 1)


Out[19]:
$$0$$
y = 0

In [29]:
yl = phi.subs(y, 0)
yl


Out[29]:
$$\left(- x + 1\right) \left(- z + 1\right) \left(\cos{\left (\pi x \right )} + \cos{\left (\pi z \right )} + 1\right)$$

In [30]:
print(yl)


(-x + 1)*(-z + 1)*(cos(pi*x) + cos(pi*z) + 1)
y = 1

In [21]:
phi.subs(y, 1)


Out[21]:
$$0$$
z = 0

In [31]:
zl = phi.subs(z, 0)
zl


Out[31]:
$$\left(- x + 1\right) \left(- y + 1\right) \left(\cos{\left (\pi x \right )} + \cos{\left (\pi y \right )} + 1\right)$$

In [32]:
print(zl)


(-x + 1)*(-y + 1)*(cos(pi*x) + cos(pi*y) + 1)
z = 1

In [23]:
phi.subs(z, 1)


Out[23]:
$$0$$

In [ ]: