Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Exercise_Problems_03
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Numerics III
Exercise_Problems_03
Commits
3f05036c
Commit
3f05036c
authored
4 years ago
by
javak87
Browse files
Options
Downloads
Patches
Plain Diff
second revision still matrix mismach
parent
5baf42bc
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Poisson_equation_FDM.py
+22
-19
22 additions, 19 deletions
Poisson_equation_FDM.py
with
22 additions
and
19 deletions
Poisson_equation_FDM.py
+
22
−
19
View file @
3f05036c
...
@@ -10,34 +10,31 @@ u(x; y) = (x^4)*(y^5) + 17*sin(xy)
...
@@ -10,34 +10,31 @@ u(x; y) = (x^4)*(y^5) + 17*sin(xy)
import
numpy
as
np
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
class
FivePointStencil
_2D
:
class
FivePointStencil
:
'''
'''
This class solve the Poisson equation numerically in 2D
This class solve the Poisson equation numerically in 2D
using the five-point stencil finite difference method.
using the five-point stencil finite difference method.
'''
'''
def
__init__
(
grid_number_x
:
'
int
'
,
grid_number_y
:
'
int
'
,
boundary_x
:
'
tuple
'
,
boundary_y
:
'
tuple
'
):
def
__init__
(
self
,
nx
,
ny
):
'''
'''
Parameters:
Parameters:
grid_number_x (int): the number of grid in the x direction
nx (int): the number of grid in the x direction
grid_number_y (int): the number of grid in the y direction
ny (int): the number of grid in the y direction
boundary_x (tuple): the boundary condition in x direction like [lower_bound,upper_bound]
boundary_y (tuple): the boundary condition in y direction like [lower_bound,upper_bound]
'''
'''
self
.
grid_number_x
=
grid_number_x
self
.
nx
=
nx
self
.
grid_number_y
=
grid_number_y
self
.
ny
=
ny
self
.
boundary_x
=
boundary_x
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
(
0
,
1
,
self
.
n
x
+
1
)
y_grid
=
np
.
linspace
(
self
.
boundary_y
[
0
],
self
.
boundary_y
[
1
],
self
.
grid_number_
y
+
1
)
y_grid
=
np
.
linspace
(
0
,
1
,
self
.
n
y
+
1
)
xx
,
yy
=
np
.
meshgrid
(
x_grid
,
y_grid
,
sparse
=
True
)
xx
,
yy
=
np
.
meshgrid
(
x_grid
,
y_grid
,
sparse
=
True
)
...
@@ -49,17 +46,21 @@ class FivePointStencil_2D:
...
@@ -49,17 +46,21 @@ class FivePointStencil_2D:
values
=
values
.
flatten
()
values
=
values
.
flatten
()
# compute hx and hy
# compute hx and hy
hx
=
(
self
.
boundary_x
[
1
]
-
self
.
boundary_x
[
0
])
/
self
.
grid_number_
x
hx
=
1
/
self
.
n
x
hy
=
(
self
.
boundary_y
[
1
]
-
self
.
boundary_y
[
0
])
/
self
.
grid_number_
y
hy
=
1
/
self
.
n
y
# Au - Bg = f
# Au - Bg = f
# compute "A" Matrix
# compute "A" Matrix
diag_coeff
=
2
*
((
1
/
hx
**
2
)
+
(
1
/
hy
**
2
))
diag_coeff
=
2
*
((
1
/
hx
**
2
)
+
(
1
/
hy
**
2
))
eins
=
(
1
/
hx
**
2
)
*
np
.
ones
((
self
.
grid_number_x
,))
eins
=
(
1
/
hx
**
2
)
*
np
.
ones
((
self
.
nx
+
1
,))
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
]
block_matrix
=
diag_coeff
*
np
.
eye
((
self
.
nx
-
1
))
off_diag_upper
=
np
.
diag
(
-
1
*
eins
,
k
=
1
)
corner_matrix
=
(
1
/
hy
**
2
)
*
np
.
eye
((
self
.
grid_number_x
-
1
)
*
(
self
.
grid_number_x
-
1
))
off_diag_lower
=
np
.
diag
(
eins
,
k
=-
1
)
block_matrix
=
block_matrix
+
off_diag_upper
+
off_diag_lower
corner_matrix
=
(
1
/
hy
**
2
)
*
np
.
eye
((
self
.
nx
-
1
)
*
(
self
.
ny
-
1
))
half_matrix_upper
=
np
.
hstack
((
block_matrix
,
corner_matrix
))
half_matrix_upper
=
np
.
hstack
((
block_matrix
,
corner_matrix
))
half_matrix_lower
=
np
.
hstack
((
corner_matrix
,
block_matrix
))
half_matrix_lower
=
np
.
hstack
((
corner_matrix
,
block_matrix
))
...
@@ -71,7 +72,9 @@ class FivePointStencil_2D:
...
@@ -71,7 +72,9 @@ class FivePointStencil_2D:
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
fdm_obj
=
FivePointStencil_2D
(
grid_number_x
=
10
,
grid_number_y
=
10
,
boundary_x
=
[
0
,
1
],
boundary_y
=
[
0
,
1
])
nx
=
4
ny
=
3
fdm_obj
=
FivePointStencil
(
nx
,
ny
)
solution
=
fdm_obj
.
boundary_initialization
()
solution
=
fdm_obj
.
boundary_initialization
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment