Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jochen Schulz
haskell_labcourse
Commits
cbb635d8
Commit
cbb635d8
authored
Mar 22, 2016
by
Christoph Ruegge
Browse files
Fix sign in poisson lecture
parent
324e21ca
Changes
5
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
cbb635d8
...
...
@@ -32,8 +32,6 @@ A lecture to teach Haskell in a 2 weeks block lab course.
*
[
26 continous optimization
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/26
continous optimization.ipynb)
*
[
27 inverse
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/27
inverse.ipynb)
*
[
28 minsurf
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/28
minsurf.ipynb)
*
[
Untitled
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/Untitled.ipynb
)
*
[
Untitled1
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/Untitled1.ipynb
)
*
[
shortest path
](
http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/shortest
path.ipynb)
## viewing of the notebooks
...
...
lecture/25 poisson equation (PDE).ipynb
View file @
cbb635d8
%% Cell type:markdown id: tags:
# Poisson equation in 2d
## Poisson equation
Find $u
\i
n C^2(
\O
mega)
\c
ap C(
\o
verline{
\O
mega})$ with
$$
\b
egin{cases}
\D
elta u = f &
\t
ext{in }
\O
mega
\\
-
\D
elta u = f &
\t
ext{in }
\O
mega
\\
u = 0 &
\m
box{on }
\p
artial
\O
mega
\e
nd{cases}
$$
for $
\O
mega = (0,1)^2$ and $f
\i
n C(
\O
mega)$. For $f = 0$, the equation is known as
*Laplace equation*
.
## Laplace operator
$$
\D
elta u :=
\s
um_{i=1}^d
\f
rac{
\p
artial ^2 u}{
\p
artial x_i^2}
$$
### Discretization
-
Equidistant grid $
\O
mega_N = I_N
\t
imes I_N$, where
$$I_N := \left\{ x^N_j \colon j = 0, \ldots, N-1 \right\} \subset (0, 1), \qquad x^N_j := \left( j + \frac{1}{2} \right) h_N, \qquad h_N := \frac{1}{N}.$$
-
Approximation of $
\f
rac{
\p
artial^2 u}{
\p
artial x^2}$:
$$\frac{u(x-h, y) - 2 u(x, y) + u(x+h, y)}{h^2} = \frac{\partial^2 u}{\partial x^2} (x, y) + \mathcal{O}(h^2),$$
and similarly for $\frac{\partial^2 u}{\partial y^2}$.
-
Approximation of Laplace operator:
$$\frac{u(x, y-h) + u(x-h, y) - 4 u(x, y) + u(x, y+h) + u(x+h, y)}{h^2} = \Delta u(x, y) + \mathcal{O}(h^2)$$
-
Define
$$u^N_{jk} := u(x^N_j, x^N_k), \qquad f^N_{jk} := f(x^N_j, x^N_k),$$
for $j, k = 0, 1, \ldots, N-1$, and
$$(\Delta^N u^N)_{jk} := \frac{u^N_{j,k-1} + u^N_{j-1,k} - 4 u^N_{jk} + u^N_{j,k+1} + u^N_{j+1,k}}{h_N^2}.$$
Here, $u^N_{-1,k} = u^N_{N,k} = u^N_{j,-1} = u^N_{j,N} = 0$ (boundary condition).
-
Solve
$$\Delta^N u^N = f^N.$$
$$
-
\Delta^N u^N = f^N.$$
## Code
For performance reasons, the code is not written in the notebook. See files
`01-hmatrix.hs`
,
`02-repa.hs`
and
`03-stencils.hs`
.
...
...
lecture/25-poisson-equation/01-hmatrix.hs
View file @
cbb635d8
...
...
@@ -8,7 +8,7 @@ import Numeric.LinearAlgebra.Helpers
import
Numeric.LinearAlgebra.Repa
(
linearSolve
)
poisson
::
Int
->
(
Double
->
Double
->
Double
)
->
Mat
Double
poisson
n
f
=
computeS
.
reshape
gridshape
.
fromJust
.
linearSolve
l
aplace
$
asColumn
rhs
poisson
n
f
=
computeS
.
reshape
gridshape
.
fromJust
.
linearSolve
negL
aplace
$
asColumn
rhs
where
-- coordinates
h
=
1
/
fromIntegral
n
::
Double
...
...
@@ -34,8 +34,8 @@ poisson n f = computeS . reshape gridshape . fromJust . linearSolve laplace $ as
-- reshape gridshape :: Array r DIM1 Double -> Array D DIM2 Double
matshape
=
Z
:.
(
n
*
n
)
:.
(
n
*
n
)
::
DIM2
l
aplace
::
Mat
Double
l
aplace
=
computeS
$
fromFunction
matshape
computeElem
negL
aplace
::
Mat
Double
negL
aplace
=
computeS
$
fromFunction
matshape
computeElem
where
computeElem
::
DIM2
->
Double
computeElem
(
Z
:.
(
!
j
)
:.
(
!
k
))
|
j
==
k
=
4
*
c
...
...
lecture/25-poisson-equation/02-repa.hs
View file @
cbb635d8
...
...
@@ -5,7 +5,7 @@ import Data.Array.Repa
import
Data.Array.Repa.Helpers
(
runCG
,
saveArrayAsPNG
)
poisson
::
Int
->
(
Double
->
Double
->
Double
)
->
IO
(
Array
U
DIM2
Double
)
poisson
n
f
=
runCG
l
aplace
=<<
computeP
rhs
poisson
n
f
=
runCG
1e-6
negL
aplace
=<<
computeP
rhs
where
-- coordinates
h
=
1
/
fromIntegral
n
::
Double
...
...
@@ -16,8 +16,8 @@ poisson n f = runCG laplace =<< computeP rhs
rhs
=
fromFunction
gridshape
$
\
(
Z
:.
(
!
jx
)
:.
(
!
jy
))
->
f
(
unsafeLinearIndex
xs
jx
)
(
unsafeLinearIndex
xs
jy
)
-- laplace operator
l
aplace
::
Array
U
DIM2
Double
->
Array
D
DIM2
Double
l
aplace
arr
=
fromFunction
ext
computeElem
negL
aplace
::
Array
U
DIM2
Double
->
Array
D
DIM2
Double
negL
aplace
arr
=
fromFunction
ext
computeElem
where
!
ext
=
extent
arr
get
!
idx
|
inShape
ext
idx
=
unsafeIndex
arr
idx
...
...
lecture/25-poisson-equation/03-stencils.hs
View file @
cbb635d8
...
...
@@ -35,7 +35,7 @@ import Data.Array.Repa.Stencil
import
Data.Array.Repa.Stencil.Dim2
poisson
::
Int
->
(
Double
->
Double
->
Double
)
->
IO
(
Array
U
DIM2
Double
)
poisson
n
f
=
runCG
l
aplace
=<<
computeP
rhs
poisson
n
f
=
runCG
1e-6
negL
aplace
=<<
computeP
rhs
where
-- coordinates
h
=
1
/
fromIntegral
n
::
Double
...
...
@@ -46,7 +46,7 @@ poisson n f = runCG laplace =<< computeP rhs
rhs
=
fromFunction
gridshape
$
\
(
Z
:.
(
!
jx
)
:.
(
!
jy
))
->
f
(
unsafeLinearIndex
xs
jx
)
(
unsafeLinearIndex
xs
jy
)
-- laplace stencil
l
aplaceStencil
=
makeStencil2
3
3
computeElem
::
Stencil
DIM2
Double
negL
aplaceStencil
=
makeStencil2
3
3
computeElem
::
Stencil
DIM2
Double
where
computeElem
(
Z
:.
-
1
:.
0
)
=
Just
$
-
c
computeElem
(
Z
:.
1
:.
0
)
=
Just
$
-
c
computeElem
(
Z
:.
0
:.
-
1
)
=
Just
$
-
c
...
...
@@ -55,8 +55,8 @@ poisson n f = runCG laplace =<< computeP rhs
computeElem
_
=
Nothing
c
=
1
/
h
**
2
-- laplace operator
l
aplace
::
Array
U
DIM2
Double
->
Array
D
DIM2
Double
l
aplace
=
delay
.
mapStencil2
(
BoundConst
0
)
l
aplaceStencil
negL
aplace
::
Array
U
DIM2
Double
->
Array
D
DIM2
Double
negL
aplace
=
delay
.
mapStencil2
(
BoundConst
0
)
negL
aplaceStencil
-- There is special syntax for stencils. The laplaceStencil above can be
-- produced more easily by
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment