Skip to content
Snippets Groups Projects
Commit 02e3f41e authored by penrose's avatar penrose
Browse files

all eq components assembled, still buggy

parent 9c6c69bf
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import numpy as np import numpy as np
from scipy.sparse import csr_matrix from scipy.sparse import csr_matrix
from scipy.sparse.linalg import spsolve from scipy.sparse.linalg import spsolve
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
**The following poisson problem is given with Dirichlet boundary condition is given:** **The following poisson problem is given with Dirichlet boundary condition is given:**
$$-\Delta u = f \quad in \; \Omega = (0,1)^2$$ $$-\Delta u = f \quad in \; \Omega = (0,1)^2$$
$$u = g \quad on \; \partial \Omega$$ $$u = g \quad on \; \partial \Omega$$
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#given exact solution #given exact solution
def u(x,y): def u(x,y):
return pow(x,4)*pow(y,5)-17*np.sin(x*y) return pow(x,4)*pow(y,5)-17*np.sin(x*y)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#right-hand-side of the poisson equation #right-hand-side of the poisson equation
def fun(x,y): def fun(x,y):
return -(17*(x**2 + y**2)*np.sin(x*y) + 4*x**2*y**3+(5*x**2 + 3*y**2)) return -(17*(x**2 + y**2)*np.sin(x*y) + 4*x**2*y**3+(5*x**2 + 3*y**2))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
First, the components of the following equation will be assembled: First, the components of the following equation will be assembled:
$$A \underline{u} = \underline{f} + B\underline{g}$$ $$A \underline{u} = \underline{f} + B\underline{g}$$
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def matrix_A(h): def matrix_A(h):
N = int(1/h) N = int(1/h)
m = pow(N-1,2) m = pow(N-1,2)
A = pow(h,-2)*(np.zeros((m,m))-4*np.eye(m)+np.eye(m,k=1)+np.eye(m,k=-1)+np.eye(m,k=N-1)+np.eye(m,k=-(N-1))) A = pow(h,-2)*(np.zeros((m,m))-4*np.eye(m)+np.eye(m,k=1)+np.eye(m,k=-1)+np.eye(m,k=N-1)+np.eye(m,k=-(N-1)))
for i in range(N-2): for i in range(N-2):
A[(i+1)*(N-1)-1][(i+1)*(N-1)] = 0 A[(i+1)*(N-1)-1][(i+1)*(N-1)] = 0
A[(i+1)*(N-1)][(i+1)*(N-1)-1] = 0 A[(i+1)*(N-1)][(i+1)*(N-1)-1] = 0
#A = csr_matrix(A) #A = csr_matrix(A)
return A return A
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def vector_f(h): def vector_f(h):
N = int(1/h) N = int(1/h)
l = pow(N-1,2) l = pow(N-1,2)
f = np.zeros(l) f = np.zeros(l)
for i in range(N-1): for i in range(N-1):
for k in range(N-1): for k in range(N-1):
f[k+i*(N-1)]=fun((k+1)/(N),(i+1)/(N)) f[k+i*(N-1)]=fun((k+1)/(N),(i+1)/(N))
return f return f
``` ```
%% Cell type:raw id: tags: %% Cell type:code id: tags:
# just the initialisation of matrix B ``` python
def matrix_B(h): def matrix_B(h):
N = int(1/h) N = int(1/h)
m = pow(N-1,2) m = pow(N-1,2)
l = 4*N l = 4*N
B = np.zeros((m,l)) B = np.zeros((m,l))
for i in range(N-1):
B[(i+1)*(N-1)-1][2*(N+i)]= pow(h,-2)
B[(N-2)*(N-1)+i][N+2+2*i]=pow(h,-2)
return B return B
```
%% Cell type:raw id: tags: %% Cell type:code id: tags:
``` python
def vector_g(h): def vector_g(h):
N = int(1/h) N = int(1/h)
l = 4*N l = 4*N
g = np.zeros(l) g = np.zeros(l)
g[-1] = u(1,1) for i in range(N):
g[3*N+i] = u((i+1)*h,1)
for i in range(N-1):
g[N+2+2*i] = u(1,(i+1)*h)
return g return g
```
%% Cell type:markdown id: tags:
Since the exact solution is zero at the boundary, the product B*g is zero.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
n = 6 n = 2
h = pow(2,-n) h = pow(2,-n)
N = pow(2,n) N = pow(2,n)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
A=matrix_A(h) A=matrix_A(h)
f=vector_f(h) f=vector_f(h)
appr_u = np.linalg.solve(A,f) B=matrix_B(h)
g=vector_g(h)
RHS = f+np.matmul(B,g)
appr_u = np.linalg.solve(A,RHS)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
A[3968,3905]
```
%% Output
4096.0
%% Cell type:code id: tags:
``` python
def exact_solution(h): def exact_solution(h):
N = int(1/h) N = int(1/h)
l = pow(N-1,2) l = pow(N-1,2)
v = np.zeros(l) v = np.zeros(l)
for i in range(N-1): for i in range(N-1):
for k in range(N-1): for k in range(N-1):
v[k+i*(N-1)]=u((k+1)/(N),(i+1)/(N)) v[k+i*(N-1)]=u((k+1)/(N),(i+1)/(N))
return v return v
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
v= exact_solution(h) v= exact_solution(h)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
error = max(abs(appr_u - v)) error = max(abs(appr_u - v))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
appr_u[-1] appr_u[-1]
``` ```
%% Output %% Output
0.015864236841443172 7.389346886114219
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
error error
``` ```
%% Output %% Output
13.161396848144852 16.380407649947266
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment