|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "#\n", |
| 10 | + "# Program 7.3: Laplace equation by FEM (laplacefem.ipynb)\n", |
| 11 | + "# J Wang, Computational modeling and visualization with Python\n", |
| 12 | + "#\n", |
| 13 | + "\n", |
| 14 | + "from scipy.linalg import solve\n", |
| 15 | + "from mpl_toolkits.mplot3d import Axes3D\n", |
| 16 | + "import matplotlib.pyplot as plt, numpy as np\n", |
| 17 | + "%matplotlib notebook\n", |
| 18 | + "\n", |
| 19 | + "def mesh(L, N): # generate mesh\n", |
| 20 | + " elm, bv = [], [] # elements: $[[n_1,n_2,n_3],..]$, bndry value\n", |
| 21 | + " x, y = np.linspace(0,L,N+1), np.linspace(0,L,N+1) # same x,y grids\n", |
| 22 | + " ndn = np.arange((N+1)*(N+1)).reshape(N+1,N+1) # node number \n", |
| 23 | + " node = [[xi, yj] for xi in x for yj in y] # nodes \n", |
| 24 | + " for j in range(N):\n", |
| 25 | + " for i in range(N):\n", |
| 26 | + " elm.append([ndn[i,j], ndn[i+1,j+1], ndn[i,j+1]]) # upper \n", |
| 27 | + " elm.append([ndn[i,j], ndn[i+1,j], ndn[i+1,j+1]]) # lower \n", |
| 28 | + "\n", |
| 29 | + " ip = ndn[1:-1,1:-1].flatten() # internal nodes \n", |
| 30 | + " bp = np.delete(ndn, ip) # boundary nodes=all-internal \n", |
| 31 | + " for p in bp:\n", |
| 32 | + " bv.append((node[p][0]*node[p][1])**2) # boundary values\n", |
| 33 | + " return node, elm, bp, ip, bv, x, y\n", |
| 34 | + "\n", |
| 35 | + "def fem_mat(node, elm): # fills matrix $A_{ij} = \\langle \\nabla \\phi_i \\cdot \\nabla \\phi_j \\rangle $\n", |
| 36 | + " A = np.zeros((len(node),len(node)))\n", |
| 37 | + " for e in elm:\n", |
| 38 | + " (x1,y1), (x2,y2), (x3,y3) = node[e[0]], node[e[1]], node[e[2]]\n", |
| 39 | + " beta, gama = [y2-y3, y3-y1, y1-y2], [x3-x2, x1-x3, x2-x1]\n", |
| 40 | + " ar = 2*(x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2))\n", |
| 41 | + " for i in range(3):\n", |
| 42 | + " for j in range(i,3):\n", |
| 43 | + " A[e[i],e[j]] += (beta[i]*beta[j] + gama[i]*gama[j])/ar\n", |
| 44 | + " if (i != j): A[e[j],e[i]] = A[e[i],e[j]] # symmetry\n", |
| 45 | + " return A\n", |
| 46 | + " \n", |
| 47 | + "L, N = 1.0, 40 # length of square, number of intervals\n", |
| 48 | + "node, elm, bp, ip, bv, x, y = mesh(L, N) # generate mesh \n", |
| 49 | + "ip.sort() # sort ip, just in case \n", |
| 50 | + "A, b = fem_mat(node,elm), np.zeros(len(ip)) # build matrices\n", |
| 51 | + "for j in range(len(ip)): \n", |
| 52 | + " b[j] = np.dot(A[ip[j], bp], bv) # boundary condition \n", |
| 53 | + "\n", |
| 54 | + "A = np.delete(A, bp, axis=0) # delete rows specified by bp \n", |
| 55 | + "A = np.delete(A, bp, axis=1) # delete cols specified by bp \n", |
| 56 | + "u = solve(A, -b) # solve \n", |
| 57 | + "\n", |
| 58 | + "u = np.concatenate((u, bv)) # combine internal+boundary values \n", |
| 59 | + "all = np.concatenate((ip, bp)) # internal+boundary nodes\n", |
| 60 | + "idx = np.argsort(all) # index sort nodes \n", |
| 61 | + "u = np.take(u, idx) # now u[n] is the value at node n \n", |
| 62 | + "u = np.reshape(u, (N+1, N+1)) # reshape grid for graphing \n", |
| 63 | + "x, y = np.meshgrid(x, y) \n", |
| 64 | + "\n", |
| 65 | + "plt.figure()\n", |
| 66 | + "ax = plt.subplot(111, projection='3d')\n", |
| 67 | + "ax.plot_surface(x, y, u, rstride=1, cstride=1,\n", |
| 68 | + " linewidth=0, cmap=plt.cm.jet)\n", |
| 69 | + "ax.set_xlabel('x'), ax.set_ylabel('y'), ax.set_zlabel('V')\n", |
| 70 | + "plt.figure()\n", |
| 71 | + "plt.subplot(111, aspect='equal')\n", |
| 72 | + "plt.contour(x, y, u, 26)\n", |
| 73 | + "plt.show()" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": { |
| 80 | + "collapsed": true |
| 81 | + }, |
| 82 | + "outputs": [], |
| 83 | + "source": [] |
| 84 | + } |
| 85 | + ], |
| 86 | + "metadata": { |
| 87 | + "kernelspec": { |
| 88 | + "display_name": "Python 3", |
| 89 | + "language": "python", |
| 90 | + "name": "python3" |
| 91 | + }, |
| 92 | + "language_info": { |
| 93 | + "codemirror_mode": { |
| 94 | + "name": "ipython", |
| 95 | + "version": 3 |
| 96 | + }, |
| 97 | + "file_extension": ".py", |
| 98 | + "mimetype": "text/x-python", |
| 99 | + "name": "python", |
| 100 | + "nbconvert_exporter": "python", |
| 101 | + "pygments_lexer": "ipython3", |
| 102 | + "version": "3.6.1" |
| 103 | + } |
| 104 | + }, |
| 105 | + "nbformat": 4, |
| 106 | + "nbformat_minor": 2 |
| 107 | +} |
0 commit comments