of 76

• date post

10-Oct-2015
• Category

## Documents

• view

18

0

Embed Size (px)

description

python Simulation from Scratch

### Transcript of Finite Volumes Example

• Finite Volumes Lab II: Cooking a Simulation from

Scratch

R. Edwin Garcaredwing@purdue.edu

1

• One Dimensional Diffusion

2

• One Dimensional Diffusion

L= 1

2

• One Dimensional Diffusion

t= D

L= 1

2

• One Dimensional Diffusion=1

=0

t= D

L= 1

2

• One Dimensional Diffusion=1

=0

t= D

(x, t=0) = 0

L= 1

2

• Working Scripts for Todays Class can be are:

3

• Working Scripts for Todays Class can be are:

diffusionX.py diffusionI.py diffusionCN.py(explicit) (implicit) (semi-implicit)

3

• A PDE is Solved in Four Steps

Variables Definitions

Equation(s) Definition(s)

Boundary Condition Specification

Viewer Creation

Problem Solving

4

• A PDE is Solved in Four Steps

Variables Definitions

Equation(s) Definition(s)

Boundary Condition Specification

Viewer Creation

Problem Solving

5

• Variables Definitions

6

• Variables Definitions

nx = 50

6

• Variables Definitions

nx = 50dx = 1.0/float(nx)

6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1D

6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1D

Grid2DGrid3D

other grids{

6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)

Grid2DGrid3D

other grids{

6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariable

Grid2DGrid3D

other grids{

6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariable

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)D = 1.0

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)D = 1.0valueLeft = 1

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)D = 1.0valueLeft = 1valueRight = 0

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)D = 1.0valueLeft = 1valueRight = 0timeStepDuration = 0.9*(dx)**2/(2*D)

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)D = 1.0valueLeft = 1valueRight = 0timeStepDuration = 0.9*(dx)**2/(2*D)

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{A=

2Dtx2

{6

• Variables Definitions

nx = 50dx = 1.0/float(nx)from fipy.meshes.grid1D import Grid1Dmesh = Grid1D(nx, dx)from fipy.variables.cellVariable import CellVariablephi = CellVariable(name="solution variable", mesh=mesh, value=0)D = 1.0valueLeft = 1valueRight = 0timeStepDuration = 0.9*(dx)**2/(2*D)steps = 900

Grid2DGrid3D

other grids{other variablesVariableNoiseVariable

{A=

2Dtx2

{6

• A PDE is Solved in Four Steps

Variables Definitions

Equation(s) Definition(s)

Boundary Condition Specification

Viewer Creation

Problem Solving

7

• Equation Definition

8

• Equation Definition

from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm

8

• Equation Definition

from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm

from fipy.terms.transientTerm import TransientTerm

8

• Equation Definition

from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm

from fipy.terms.transientTerm import TransientTerm

eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)

8

• Equation Definition

from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm

from fipy.terms.transientTerm import TransientTerm

eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)

t= D

8

• Equation Definition

from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm

from fipy.terms.transientTerm import TransientTerm

eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)

t= D

8

• Equation Definition

from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm

from fipy.terms.transientTerm import TransientTerm

eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)

t= D

8

• Other Equation Terms

9

• Term FiPy Representation

Other Equation Terms

9

• Term FiPy Representation

Other Equation Terms

(D1[ D2()]) DiffusionTerm

9

• Term FiPy Representation

Other Equation Terms

(!v) ConvectionTerm

(D1[ D2()]) DiffusionTerm

9

• Term FiPy Representation

Other Equation Terms

(!v) ConvectionTerm

(D1[ D2()]) DiffusionTerm

SourceTerm

9

• A PDE is Solved in Four Steps

Variables Definitions

Equation(s) Definition(s)

Boundary Condition Specification

Viewer Creation

Problem Solving

10

• Boundary Conditions

11

• Boundary Conditions

from fipy.boundaryConditions.fixedValue import FixedValue

11

• Boundary Conditions

from fipy.boundaryConditions.fixedValue import FixedValue

BCs = (FixedValue(faces = mesh.getFacesRight(), value=valueRight),

FixedValue(faces=mesh.getFacesLeft(),value=valueLeft))

11

• Boundary Conditions

from fipy.boundaryConditions.fixedValue import FixedValue

BCs = (FixedValue(faces = mesh.getFacesRight(), value=valueRight),

FixedValue(faces=mesh.getFacesLeft(),value=valueLeft))

BCs = (BC1, BC2, BC3, ...)notation

11

• Other Type of BCs

12

• Other Type of BCs

Dirichlet BC

FixedValue(FaceLocation, Value)

12

• Other Type of BCs

Dirichlet BC

FixedValue(FaceLocation, Value)

Neumann BC

FixedFlux(FaceLocation, Value)

12

• Other Type of BCs

Dirichlet BC

FixedValue(FaceLocation, Value)

Neumann BC

FixedFlux(FaceLocation, Value)

Higher Order BC

NthOrderBoundaryCondition(FaceLocation, Value)

12

• A PDE is Solved in Four Steps

Variables Definitions

Equation(s) Definition(s)

Boundary Condition Specification

Viewer Creation

Problem Solving

13

• Viewer Creation

from fipy import viewers

viewer = viewers.make(vars = phi, limits={'datamin':0.0, 'datamax':1.0})

14

• A PDE is Solved in Four Steps

Variables Definitions

Equation(s) Definition(s)

Boundary Condition Specification

Viewer Creation

Problem Solving

15

• Solving the Problem

16

• Solving the Problem

for step in range(steps):

16

• Solving the Problem

for step in range(steps):

eqX.solve(var = phi, boundaryConditions = BCs, dt = timeStepDuration)

viewer.plot()

16

• Solving the Problem

for step in range(steps):

eqX.solve(var = phi, boundaryConditions = BCs, dt = timeStepDuration)

viewer.plot()

16

• Launching the Simulation

17

• Launching the Simulation

Save file under adequate name (extension must be .py)

17

• Launching the Simulation

Save file under adequate name (extension must be .py)

short-cut: CTRL-X CTRL-S

17

• Launching the Simulation

Save file under adequate name (extension must be .py) Example: coolSimulation.py

short-cut: CTRL-X CTRL-S

17

• Launching the Simulation

Save file under adequate name (extension must be .py)

Run it by calling python from the command line.

Example: coolSimulation.py

short-cut: CTRL-X CTRL-S

17