Skip to content

Commit

Permalink
Ported "Spectral methods in MATLAB" to python. Ported Series solution…
Browse files Browse the repository at this point in the history
…s of Laplace's equation to python
  • Loading branch information
mutablehurdle committed Jan 28, 2022
1 parent e7cf1cd commit 57a9cdc
Show file tree
Hide file tree
Showing 60 changed files with 3,193 additions and 0 deletions.

Large diffs are not rendered by default.

295 changes: 295 additions & 0 deletions series-solution-laplace/LaplaceDisk.ipynb

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions series-solution-laplace/LaplaceDisk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@



#import necessary modules

import numpy as np
import matplotlib.pyplot as plt

#define center of disk using complex coordinates (real numbers are x, imaginary are y)

c = 3 +1j #center of disk
r = 1 #radius of disk

#use power series expansion to solve Laplace's equation

N =10 #number of expansion terms
npts = 3*N #number of sample points

npts_range=np.linspace(0,npts,npts)
z = c + r*np.exp(2j*np.pi*npts_range/npts) #sample points along disk boundary

rhs = -np.log(np.abs(z))+np.log(np.abs(z-c)) #right-hand side
A = np.ones((npts,2*N+1))

for k in np.arange(1,N+1):
A[:,2*k-1] = np.real((z-c)**(-k))
A[:,2*k] = np.imag((z-c)**(-k))

a = np.linalg.lstsq(A,rhs)[0]


#plot disk to check that it is centered where you want it, with the radius given and sample points
plt.plot(np.real(z-c),np.imag(z-c),'.')


#Define power series solution to Laplace's equation in complex numbers
def disk1fun(z):
u = np.log(np.abs(z)) - np.log(np.abs(z-c)) + a[0]

for k in np.arange(1,N+1):
u+=a[2*k-1]*np.real((z-c)**(-k))+a[2*k]*np.imag((z-c)**(-k))
u[abs(z-c)<=r]=np.NaN

return u


#contour plot the solution on a grid
xx,yy = np.linspace(-5,5,145),np.linspace(-4,4,115)
[xx,yy] = np.meshgrid(xx,yy)
zz = xx + 1j*yy
uu = disk1fun(zz)
levels = np.linspace(-3,-.25,10)
plt.contourf(xx,yy,uu)
252 changes: 252 additions & 0 deletions series-solution-laplace/LaplaceManyDisks.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAQEAAAD8CAYAAAB3lxGOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAZ8ElEQVR4nO2dXawdV3mGn6/5pUBxEg6WZTsNCEsoFyXER2kQqKKJQMRFJBcBBaHGQpYstakEohJNWqkVUi+gFwQiVVCrQTUVP0n5UawoLbhOUNULAjb5x6Q5RIliK4lNSAIVom3g68VeE28O53jW7LP2nnfNfI+0dWavPX73mpm13nnXmtljc3eCIBgvv9V3BYIg6JcwgSAYOWECQTBywgSCYOSECQTByAkTCIKR07sJmNm7zexRM1sxsxt7rMfnzeyEmT08VXa+mR00s8fS3/NSuZnZLanOD5rZpQuq43Yzu8fMfmBmj5jZh9XqaWbnmtl3zeyBVMePp/LXm9m9qS63mdnZqfyc9H4lfX7RvOs4VdczzOw+M7tTsY5m9oSZPWRm95vZ4VRW/li7e28v4AzgR8AbgLOBB4CLe6rLHwCXAg9Plf0dcGNavhH4ZFreBfwrYMDlwL0LquMW4NK0/Grgv4CLleqZvutVafks4N703bcD16XyzwF/kpb/FPhcWr4OuG2Bx/yjwJeAO9N7qToCTwCvXVVW/FgvZGefZiPfCnxz6v1NwE091ueiVSbwKLAlLW8BHk3L/wB8YK31FlzfO4B3qtYT+G3g+8DvAz8Gzlx93IFvAm9Ny2em9WwBddsGHAKuAO5MnUetjmuZQPFj3fdwYCvw1NT7Y6lMhc3u/nRafgbYnJZ7r3eKpG9hcqaVqmeK2fcDJ4CDTNLeC+7+0hr1eLmO6fMXgQvmXUfg08DHgF+l9xcI1tGBb5nZETPbm8qKH+szS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vdLMXt0sA+8CHgYOALvTaruBO9LyAeD6dJXgcuDFqWHDhojYHLF5Fi3Fzpur5e5ZLzNrfa1HznBgM/CNJHIm8CV3/zcz+x5wu5ntAZ4E3p/WvwvYBawAPwc+lPEdWURsPkXE5nyUh24KQ4NWE3D3x4E3r1H+HHDlGuUO3FCkdmvXR7KhLVqrayIYulYb80gXQzECuTsGc6g94uVqlUJ17qLmeRDVdjMLVZoA6B6EuHw4HK02VLW6Uq0JgG7nVdYqxRiSSi6qWrlUbQIQDburVi6KWsrmqqiVS/Um0KDYaHO0hhCba47gip130UYwGBOI2HyKmODLR7XzLtIIVB4v9jPg0b7r0cJrgR/3XYkWaqgj1FHPIdbxd939N27PVfkPSR/1NZ59poSZHY46lqGGeo6pjoMZDgRBMBthAkEwclRMYF/fFcgg6liOGuo5mjpKTAwGQdAfKkkgCIKeCBMIgpETJhAEIydMIAhGTphAEIyc/wegafmDpYEJkAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"'''\n",
"Spectral methods in MATLAB.\n",
"compare to Trefethen p16.m\n",
"'''\n",
"\n",
"# Poisson equation on [-1,1]x[-1,1] with u=o on boundary\n",
"\n",
"from mayavi import mlab\n",
"from numpy import *\n",
"import time\n",
"from cheb import *\n",
"from numpy.linalg import matrix_power,solve\n",
"from matplotlib import pyplot as plt\n",
"from scipy.interpolate import interp2d,griddata,bisplev,bisplrep\n",
"\n",
"# Set up grids and tensor product Laplacian and solve for u:\n",
"\n",
"N = 24\n",
"D,x = cheb(N)\n",
"y = x\n",
"xx,yy = meshgrid(x[1:N],y[1:N])\n",
"xx = hstack(xx[:]); yy = hstack(yy[:]); # stretch 2D grids to 1D vectors\n",
"f = 10*sin(8*xx*(yy-1))\n",
"D2 = matrix_power(D, 2)\n",
"D2 = D2[1:N,1:N]\n",
"I = identity(N-1)\n",
"L = kron(I,D2) + kron(D2,I) # Laplacian\n",
"plt.figure(1)\n",
"plt.spy(L)\n",
"tic = time.time(); u = solve(L,f); toc = time.time() - tic; # Solve problem and watch the clock\n",
"\n",
"# Reshape long 1D results onto 2D grid:\n",
"uu = zeros((N+1,N+1))\n",
"uu[1:N,1:N]=u.reshape(N-1,N-1)\n",
"xx,yy = meshgrid(x,y)\n",
"value = uu[int(N/4.+1),int(N/4.+1)]\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"uuu = interp2d(x, y, uu, kind='cubic')\n",
"\n",
"mlab.surf(uuu(arange(-1,1,0.04),arange(-1,1,0.04)),warp_scale=\"auto\")\n",
"mlab.show()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(50, 50)"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"uuu(arange(-1,1,0.04),arange(-1,1,0.04)).shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<mayavi.modules.surface.Surface at 0x10c826e30>"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'''\n",
"Spectral methods in MATLAB.\n",
"compare to Trefethen p18.m\n",
"'''\n",
"\n",
"# 2nd order wave eq. on Chebyshev grid\n",
"\n",
"from numpy import *\n",
"from matplotlib import pyplot as plt\n",
"from mpl_toolkits.mplot3d import axes3d\n",
"from chebfft import *\n",
"from mayavi import mlab\n",
"\n",
"N = 80\n",
"x = cos(pi*arange(0,N+1)/N)\n",
"dt = 8.0/N**2\n",
"v = exp(-200*x**2)\n",
"vold = exp(-200*(x-dt)**2)\n",
"tmax = 4\n",
"tplot = 0.075\n",
"plotgap = int(round(tplot/dt))\n",
"dt = tplot/plotgap\n",
"nplots = int(round(tmax/tplot))\n",
"plotdata = vstack((v, zeros((nplots,N+1))))\n",
"tdata = 0\n",
"for i in range(0,nplots):\n",
" for n in range(0,plotgap):\n",
" w = chebfft(chebfft(v)).T\n",
" w[0] = 0\n",
" w[N] = 0\n",
" vnew = 2*v - vold +dt**2*w\n",
" vold = v\n",
" v = vnew\n",
" plotdata[i+1,:] = v\n",
" tdata = vstack((tdata, dt*i*plotgap))\n",
"\n",
"# Plot results\n",
"\n",
"# fig = plt.figure()\n",
"# ax = axes3d.Axes3D(fig)\n",
"X, Y = meshgrid(x, tdata)\n",
"\n",
"mlab.surf(plotdata,warp_scale=\"auto\")\n",
"\n",
"# ax.plot_wireframe(X,Y,plotdata)\n",
"# ax.set_xlim(-1, 1)\n",
"# ax.set_ylim(0, tmax)\n",
"# ax.set_zlim(-2, 2)\n",
"# plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"mlab.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 57a9cdc

Please sign in to comment.