Skip to content
Snippets Groups Projects
Commit 5baf42bc authored by javak87's avatar javak87
Browse files

first edition FDM

parent 7d330054
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ class FivePointStencil_2D: ...@@ -17,6 +17,7 @@ class FivePointStencil_2D:
''' '''
def __init__ (grid_number_x:'int',grid_number_y:'int', boundary_x:'tuple', boundary_y:'tuple'): def __init__ (grid_number_x:'int',grid_number_y:'int', boundary_x:'tuple', boundary_y:'tuple'):
''' '''
Parameters: Parameters:
grid_number_x (int): the number of grid in the x direction grid_number_x (int): the number of grid in the x direction
...@@ -30,50 +31,49 @@ class FivePointStencil_2D: ...@@ -30,50 +31,49 @@ class FivePointStencil_2D:
self.boundary_x = boundary_x self.boundary_x = boundary_x
self.boundary_y = boundary_y self.boundary_y = boundary_y
def boundary_initialization (self): def boundary_initialization (self):
''' '''
Initialization of boundary condition based on the right hand side function Initialization of boundary condition based on the right hand side function
right hand side function: u(x; y) = (x^4)*(y^5) + 17*sin(xy) right hand side function: u(x; y) = (x^4)*(y^5) + 17*sin(xy)
''' '''
x_grid = np.linspace(self.boundary_x[0], self.boundary_x[1], self.grid_number_x+1) x_grid = np.linspace(self.boundary_x[0], self.boundary_x[1], self.grid_number_x+1)
y_grid = np.linspace(self.boundary_y[0], self.boundary_y[1], self.grid_number_y+1)
#xx, yy = np.meshgrid(grid, grid, sparse=True)
xx, yy = np.meshgrid(x_grid, y_grid, sparse=True)
'''
initialize boundary condition # Initialize all nodes (inner and outer) by the given function
''' values = (np.power(xx, 4))*(np.power(yy, 5)) + np.sin(xx*yy)
''' # initialize all inner node by zero
# 100*100 the problem dimension values = values[1:-1, 1:-1]
values = values.flatten()
# construct A matrxi (100^2*100^2)
eins = np.ones((3,)) # compute hx and hy
A = -2*np.eye(3)+np.diag(eins, k=1)[0:-1, 0:-1]+np.diag(eins, k=-1)[0:-1, 0:-1] hx = (self.boundary_x[1] - self.boundary_x[0])/self.grid_number_x
hy = (self.boundary_y[1] - self.boundary_y[0])/self.grid_number_y
new = np.hstack((base,np.eye(3) ))
new =np.hstack((new,np.eye(3) )) # Au - Bg = f
new2= np.hstack((np.eye(3), base )) # compute "A" Matrix
new2= np.hstack((new2, np.eye(3) ))
new3 = np.hstack((np.eye(3), np.eye(3) )) diag_coeff = 2*((1/hx**2) + (1/hy**2))
new3 = np.hstack((new3, base )) eins = (1/hx**2)*np.ones((self.grid_number_x,))
A = np.vstack((new,new2)) block_matrix = diag_coeff * np.eye((self.grid_number_x-1)*(self.grid_number_x-1)) + np.diag(-1*eins, k=1)[0:-1, 0:-1] + np.diag(eins, k=-1)[0:-1, 0:-1]
A = np.vstack((A,new3))
''' corner_matrix = (1/hy**2)*np.eye((self.grid_number_x-1)*(self.grid_number_x-1))
'''
eins = np.ones((9,)) half_matrix_upper = np.hstack((block_matrix,corner_matrix))
A1 = 4*np.eye(9) half_matrix_lower = np.hstack((corner_matrix,block_matrix))
A11=np.diag(eins, k=1)[0:-1, 0:-1] A = np.vstack((half_matrix_upper,half_matrix_lower))
A12 = np.diag(eins, k=-1)[0:-1, 0:-1]
A2 = np.diag(eins, k=3)[0:-3, 0:-3] u = np.linalg.solve(A, values)
A3 = np.diag(eins, k=-3)[0:-3, 0:-3]
# ADD perodic return u
Aper1= np.diag(eins, k=6)[0:-6, 0:-6]
Aper2= np.diag(eins, k=-6)[0:-6, 0:-6] if __name__=="__main__":
A = A1+A11+A12+A2+A3+Aper1+Aper2
fdm_obj = FivePointStencil_2D(grid_number_x=10,grid_number_y=10, boundary_x=[0,1], boundary_y=[0,1])
b= np.array([0.0625, 0.0625, 0.0625]) solution = fdm_obj.boundary_initialization()
x = np.linalg.solve(A, b)
'''
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment