exercise 4 - cse-lab€¦ · question 1: diffusion ∂ρ(r,⃗t) ∂t = d∇2ρ(r,⃗t) diffusion...

16
Exercise 4 HPCSE I Fall, 2019 CSElab Computational Science & Engineering Laboratory http://www.cse-lab.ethz.ch

Upload: others

Post on 09-Aug-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Exercise 4HPCSE IFall, 2019

CSElab Computational Science & Engineering Laboratory http://www.cse-lab.ethz.ch

Page 2: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

Credit: "Skittles colour diffusion experiment" by Movie Vertigo. https://www.youtube.com/watch?v=2y3imqlWR_A

4X

Page 3: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

Miscible Liquids Immiscible Liquids

Homogeneous solutions

Credit: "10 Amazing Experiments with Water" by Drew the Science Dude. https://www.youtube.com/watch?v=CCxbI1qRsWY

Inhomogeneous solution

Page 4: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

∂ρ( ⃗r, t)∂t

= D∇2ρ( ⃗r, t)

second order spatial derivatives

first ordertime derivative

PDE:

Physical significance:

By JrPol - Own work, CC BY 3.0,https://commons.wikimedia.org/w/index.php?curid=4586487

• Describes the macroscopically-observed spread of a solute substance within a solvent, due to molecular collisions.

: concentration of solute.ρ

• Find the concentration, as a function of space and time, of a certain species/substance, in a given domain.

Goal:

SOLVENThttps://commons.wikimedia.org/wiki/File:Blausen_0315_Diffusion.png

SOLUTE

Page 5: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion∂ρ( ⃗r, t)

∂t= D∇2ρ( ⃗r, t)diffusion

coefficient

• Atomic diffusion in a solid, having a 4-coordinated lattice structure.

• Atoms often block each other from moving to adjacent sites.

• Net flux (or movement of atoms) is always in the opposite direction of the concentration gradient.

By Runningamok19 - Own work, CC BY 3.0,https://commons.wikimedia.org/w/index.php?curid=3163040

from Wikipedia: https://en.wikipedia.org/wiki/Mass_diffusivity

is prescribed for a given pair of species:D

Page 6: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

Assume 1-d space

x

Concentration profile of a species

x

. . .

Microscopic picture:

Page 7: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

in 2 dimensions:

∂ρ( ⃗r, t)∂t

= D∇2ρ( ⃗r, t)

∂ρ(x, y, t)∂t

= D∇2ρ(x, y, t)

Boundary Conditions:

Initial Conditions:

Table 1: Example parameters.

D ⌦ �t

Set 1 1 128⇥ 128 0.00001

Set 2 1 256⇥ 256 0.000001

Set 3 1 1024⇥ 1024 0.00000001

Figure 1: Density for a system using parameter set #2 (256⇥ 256 grid).

c) Make a 2D density plot of ⇢(x, y, t) at t = 0, 0.5, 1, 2.The real-space Laplacian in equation (1) pushes the cloud away from the center andsmoothens the edges of the initial density at t = 0, see fig. 1.

Question 2: Barrier - Synchronization with threadingA barrier is a synchronization point between multiple execution units. In this exercise we want toimplement a barrier class using C++11 manual threading which fulfills the following syntax.

1 barrier b(nthreads);2 // ... spawn ‘nthreads‘ threads ...3 // inside each thread:4 b.wait()

The b.wait() statement returns only when all nthreads called that function.

a) Implement the barrier class and provide a small test for it.

2

high concentrationlow concentration

?

?

?

?

Page 8: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

Table 1: Example parameters.

D ⌦ �t

Set 1 1 128⇥ 128 0.00001

Set 2 1 256⇥ 256 0.000001

Set 3 1 1024⇥ 1024 0.00000001

Figure 1: Density for a system using parameter set #2 (256⇥ 256 grid).

c) Make a 2D density plot of ⇢(x, y, t) at t = 0, 0.5, 1, 2.The real-space Laplacian in equation (1) pushes the cloud away from the center andsmoothens the edges of the initial density at t = 0, see fig. 1.

Question 2: Barrier - Synchronization with threadingA barrier is a synchronization point between multiple execution units. In this exercise we want toimplement a barrier class using C++11 manual threading which fulfills the following syntax.

1 barrier b(nthreads);2 // ... spawn ‘nthreads‘ threads ...3 // inside each thread:4 b.wait()

The b.wait() statement returns only when all nthreads called that function.

a) Implement the barrier class and provide a small test for it.

2

Table 1: Example parameters.

D ⌦ �t

Set 1 1 128⇥ 128 0.00001

Set 2 1 256⇥ 256 0.000001

Set 3 1 1024⇥ 1024 0.00000001

Figure 1: Density for a system using parameter set #2 (256⇥ 256 grid).

c) Make a 2D density plot of ⇢(x, y, t) at t = 0, 0.5, 1, 2.The real-space Laplacian in equation (1) pushes the cloud away from the center andsmoothens the edges of the initial density at t = 0, see fig. 1.

Question 2: Barrier - Synchronization with threadingA barrier is a synchronization point between multiple execution units. In this exercise we want toimplement a barrier class using C++11 manual threading which fulfills the following syntax.

1 barrier b(nthreads);2 // ... spawn ‘nthreads‘ threads ...3 // inside each thread:4 b.wait()

The b.wait() statement returns only when all nthreads called that function.

a) Implement the barrier class and provide a small test for it.

2

Initial Condition

Fully homogeneous material

Given:• Initial concentration in a 2-D plate.• Boundary conditions along the boundaries of the plate.Find:• Concentration in the plate as a function of time and position.

Page 9: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion1. Spatial discretization

x

y

ΔxΔy

Lx

Ly

j = 1 j = 2 j = 3 j = Nx…i = 1

i = 2

i = 3

i = Ny

Δx =Lx

Nx − 1

Δy =Ly

Ny − 1

Grid step:

Page 10: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

∂ρ(x, y, t)∂t

= D∇2ρ(x, y, t) = D ( ∂2ρ∂x2

+∂2ρ∂y2 )

1. Spatial discretization: using central finite difference scheme.

( ∂2ρ∂x2 )

≈ D ( ∂2ρ∂x2 )ρi,j−1 − 2ρi,j + ρi,j+1

Δx2+

ρi−i,j − 2ρi,j + ρi+i,j

Δy2

ρi,j ρi,j+1ρi,j−1

ρi−1,j

ρi+1,j

Page 11: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion2. Time discretization: using explicit Euler scheme.

∂ρ(x, y, t)∂t

= D∇2ρ(x, y, t)

∂ρ(x, y, t)∂t

≈ρn+1 − ρn

Δt

Combining all together:

time index

ρn+1i,j − ρn

i,j

Δt= D (

ρni,j−1 − 2ρn

i,j + ρni,j+1

Δx2+

ρni−i,j − 2ρn

i,j + ρni+i,j

Δy2 )

Page 12: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion

ρn+1i,j = ρn

i,j + ΔtD (ρn

i,j−1 − 2ρni,j + ρn

i,j+1

Δx2+

ρni−i,j − 2ρn

i,j + ρni+i,j

Δy2 )

ρn+1i,j − ρn

i,j

Δt= D (

ρni,j−1 − 2ρn

i,j + ρni,j+1

Δx2+

ρni−i,j − 2ρn

i,j + ρni+i,j

Δy2 )

Δx = Δy = Δrtaking:

ρn+1i,j = ρn

i,j +ΔtDΔr2 (ρn

i,j−1 + ρni,j+1 + ρn

i−i,j + ρni+i,j − 4ρn

i,j)Implemented

inside function advance(..)

Page 13: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: Diffusion3. Boundary nodes: application of the Boundary Condition

boundary nodes

ghost nodes

ρ(x, y, t) = 0 ∀ t ≥ 0and (x, y) ∉ Ω

ρi,j+1ρi,j−1

ρi−1,j

ρi,j

ρi+1,j

Page 14: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: DiffusionNote: global and local indexing

x

y

j = 1 j = 2 j = Nx…i = 1

i = 2

i = Ny

g = (i − 1)Nx + jGlobal indexing:

(Ny − 1)Nx + 1 NyNx

Nx

1 2

Nx + 1 Nx + 2

Page 15: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: DiffusionHW: Parallelization with MPI

rank 3

rank 2

rank 1

rank 0

Page 16: Exercise 4 - CSE-Lab€¦ · Question 1: Diffusion ∂ρ(r,⃗t) ∂t = D∇2ρ(r,⃗t) diffusion coefficient • Atomic diffusion in a solid, having a 4-coordinated lattice structure

Question 1: DiffusionHW: Parallelization with MPI

rank 3

rank 2

rank 1

rank 0

ρi,j+1ρi,j−1

ρi−1,j

ρi,j

ρi+1,j

Now let's have a look at the skeleton code ...