Variable Coefficient Poisson

Derive the form of a test variable-coefficient elliptic equation with periodic boundary conditions for testing the variable-coefficient multigrid solver.

We want to solve an equation of the form $\nabla \cdot (\alpha \nabla \phi) = f$

Note: it is important for solvability that the RHS, f, integrate to zero over our domain. It seems sufficient to ensure that phi integrates to zero to have this condition met.


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

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


IPython console for SymPy 1.0 (Python 2.7.12-64-bit) (ground types: gmpy)

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)
>>> init_printing()

Documentation can be found at http://docs.sympy.org/1.0/
Variable coefficient elliptic problem, using periodic boundary conditions all around.

In [3]:
alpha = 2.0 + cos(2*pi*x)*cos(2*pi*y)

In [4]:
phi = sin(2*pi*x)*sin(2*pi*y)

we want to compute $\nabla \cdot (\alpha \nabla \phi)$


In [5]:
phi_x = diff(phi, x)
phi_y = diff(phi, y)

In [6]:
f = diff(alpha*phi_x, x) + diff(alpha*phi_y, y)

In [7]:
f = simplify(f)
f


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

In [8]:
print(f)


-16.0*pi**2*(cos(2*pi*x)*cos(2*pi*y) + 1)*sin(2*pi*x)*sin(2*pi*y)
boundary conditions check

In [9]:
phi.subs(x, 0)


Out[9]:
$$0$$

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


Out[10]:
$$0$$

In [11]:
phi.subs(y, 0)


Out[11]:
$$0$$

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


Out[12]:
$$0$$

General Elliptic

Derive the form of a test variable-coefficient elliptic equation with periodic boundary conditions for testing the variable-coefficient multigrid solver.

We are solving

$$\alpha \phi + \nabla \cdot (\beta \nabla \phi ) + \gamma \cdot \nabla \phi = f$$

Note: it is important for solvability that the RHS, f, integrate to zero over our domain. It seems sufficient to ensure that phi integrates to zero to have this condition met.


In [13]:
phi = sin(2*pi*x)*sin(2*pi*y)

In [14]:
phi


Out[14]:
$$\sin{\left (2 \pi x \right )} \sin{\left (2 \pi y \right )}$$

In [15]:
alpha = 1.0
beta = 2.0 + cos(2*pi*x)*cos(2*pi*y)
gamma_x = sin(2*pi*x)
gamma_y = sin(2*pi*y)

In [16]:
alpha


Out[16]:
$$1.0$$

In [17]:
beta


Out[17]:
$$\cos{\left (2 \pi x \right )} \cos{\left (2 \pi y \right )} + 2.0$$

In [18]:
gamma_x


Out[18]:
$$\sin{\left (2 \pi x \right )}$$

In [19]:
gamma_y


Out[19]:
$$\sin{\left (2 \pi y \right )}$$

In [20]:
phi_x = diff(phi, x)
phi_y = diff(phi, y)

In [21]:
f = alpha*phi + diff(beta*phi_x, x) + diff(beta*phi_y, y) + gamma_x*phi_x + gamma_y*phi_y

In [22]:
f = simplify(f)
f


Out[22]:
$$\left(- 16.0 \pi^{2} \cos{\left (2 \pi x \right )} \cos{\left (2 \pi y \right )} + 2.0 \pi \cos{\left (2 \pi x \right )} + 2.0 \pi \cos{\left (2 \pi y \right )} - 16.0 \pi^{2} + 1.0\right) \sin{\left (2 \pi x \right )} \sin{\left (2 \pi y \right )}$$

In [23]:
print(f)


(-16.0*pi**2*cos(2*pi*x)*cos(2*pi*y) + 2.0*pi*cos(2*pi*x) + 2.0*pi*cos(2*pi*y) - 16.0*pi**2 + 1.0)*sin(2*pi*x)*sin(2*pi*y)

In [24]:
print(alpha)


1.0

In [25]:
print(beta)


cos(2*pi*x)*cos(2*pi*y) + 2.0

In [26]:
print(gamma_x)


sin(2*pi*x)

In [27]:
print (gamma_y)


sin(2*pi*y)

In [ ]:

Inhomogeneous BCs


In [28]:
phi = cos(Rational(1,2)*pi*x)*cos(Rational(1,2)*pi*y)

In [29]:
phi


Out[29]:
$$\cos{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )}$$

In [30]:
alpha = 10
gamma_x = 1
gamma_y = 1
beta = x*y + 1

In [31]:
phi_x = diff(phi, x)
phi_y = diff(phi, y)
f = alpha*phi + diff(beta*phi_x, x) + diff(beta*phi_y, y) + gamma_x*phi_x + gamma_y*phi_y

In [32]:
f


Out[32]:
$$- \frac{\pi x}{2} \sin{\left (\frac{\pi y}{2} \right )} \cos{\left (\frac{\pi x}{2} \right )} - \frac{\pi y}{2} \sin{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )} - \frac{\pi^{2}}{2} \left(x y + 1\right) \cos{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )} - \frac{\pi}{2} \sin{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )} - \frac{\pi}{2} \sin{\left (\frac{\pi y}{2} \right )} \cos{\left (\frac{\pi x}{2} \right )} + 10 \cos{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )}$$

In [33]:
simplify(f)


Out[33]:
$$- \frac{x y}{2} \pi^{2} \cos{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )} - \frac{\pi x}{2} \sin{\left (\frac{\pi y}{2} \right )} \cos{\left (\frac{\pi x}{2} \right )} - \frac{\pi y}{2} \sin{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )} - \frac{\pi}{2} \sin{\left (\pi \left(\frac{x}{2} + \frac{y}{2}\right) \right )} - \frac{\pi^{2}}{2} \cos{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )} + 10 \cos{\left (\frac{\pi x}{2} \right )} \cos{\left (\frac{\pi y}{2} \right )}$$

boundary conditions


In [34]:
phi.subs(x,0)


Out[34]:
$$\cos{\left (\frac{\pi y}{2} \right )}$$

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


Out[35]:
$$0$$

In [36]:
phi.subs(y,0)


Out[36]:
$$\cos{\left (\frac{\pi x}{2} \right )}$$

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


Out[37]:
$$0$$

In [ ]: