You are on page 1of 76

Finite Volumes Lab II:

Cooking a Simulation from


Scratch
R. Edwin Garca
redwing@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 Denitions
Equation(s) Denition(s)
Boundary Condition Specication
Viewer Creation
Problem Solving
4
A PDE is Solved in Four Steps
Variables Denitions
Equation(s) Denition(s)
Boundary Condition Specication
Viewer Creation
Problem Solving
5
Variables Denitions
6
Variables Denitions
nx = 50
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
Grid2D
Grid3D
other grids
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
Grid2D
Grid3D
other grids
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
Grid2D
Grid3D
other grids
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
D = 1.0
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
D = 1.0
valueLeft = 1
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
D = 1.0
valueLeft = 1
valueRight = 0
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
D = 1.0
valueLeft = 1
valueRight = 0
timeStepDuration = 0.9*(dx)**2/(2*D)
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
D = 1.0
valueLeft = 1
valueRight = 0
timeStepDuration = 0.9*(dx)**2/(2*D)
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
A =
2Dt
x
2
{
6
Variables Denitions
nx = 50
dx = 1.0/oat(nx)
from py.meshes.grid1D import Grid1D
mesh = Grid1D(nx, dx)
from py.variables.cellVariable import CellVariable
phi = CellVariable(name="solution variable", mesh=mesh, value=0)
D = 1.0
valueLeft = 1
valueRight = 0
timeStepDuration = 0.9*(dx)**2/(2*D)
steps = 900
Grid2D
Grid3D
other grids
{
other variables
Variable
NoiseVariable
{
A =
2Dt
x
2
{
6
A PDE is Solved in Four Steps
Variables Denitions
Equation(s) Denition(s)
Boundary Condition Specication
Viewer Creation
Problem Solving
7
Equation Denition
8
Equation Denition
from py.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
8
Equation Denition
from py.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
from py.terms.transientTerm import TransientTerm
8
Equation Denition
from py.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
from py.terms.transientTerm import TransientTerm
eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)
8
Equation Denition
from py.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
from py.terms.transientTerm import TransientTerm
eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)

t
= D
8
Equation Denition
from py.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
from py.terms.transientTerm import TransientTerm
eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)

t
= D
8
Equation Denition
from py.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
from py.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
(D
1
[ D
2
()])
DiffusionTerm
9
Term FiPy Representation
Other Equation Terms
(v)
ConvectionTerm
(D
1
[ D
2
()])
DiffusionTerm
9
Term FiPy Representation
Other Equation Terms
(v)
ConvectionTerm
(D
1
[ D
2
()])
DiffusionTerm

SourceTerm
9
A PDE is Solved in Four Steps
Variables Denitions
Equation(s) Denition(s)
Boundary Condition Specication
Viewer Creation
Problem Solving
10
Boundary Conditions
11
Boundary Conditions
from py.boundaryConditions.xedValue import FixedValue
11
Boundary Conditions
from py.boundaryConditions.xedValue import FixedValue
BCs = (FixedValue(faces = mesh.getFacesRight(), value=valueRight),
FixedValue(faces=mesh.getFacesLeft(),value=valueLeft))
11
Boundary Conditions
from py.boundaryConditions.xedValue 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 Denitions
Equation(s) Denition(s)
Boundary Condition Specication
Viewer Creation
Problem Solving
13
Viewer Creation
from py import viewers
viewer = viewers.make(vars = phi, limits={'datamin':0.0, 'datamax':1.0})
14
A PDE is Solved in Four Steps
Variables Denitions
Equation(s) Denition(s)
Boundary Condition Specication
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 le under adequate name (extension


must be .py)
17
Launching the Simulation

Save le under adequate name (extension


must be .py)
short-cut: CTRL-X CTRL-S
17
Launching the Simulation

Save le under adequate name (extension


must be .py) Example: coolSimulation.py
short-cut: CTRL-X CTRL-S
17
Launching the Simulation

Save le 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
Launching the Simulation

Save le under adequate name (extension


must be .py)

Run it by calling python from the command


line.
Example: coolSimulation.py
Example: python coolSimulation.py
short-cut: CTRL-X CTRL-S
17
Launching the Simulation

Save le under adequate name (extension


must be .py)

Run it by calling python from the command


line.

You can cancel your simulation by typing


CTRL-C
Example: coolSimulation.py
Example: python coolSimulation.py
short-cut: CTRL-X CTRL-S
17
Fun Things to Try:
18
Fun Things to Try:

Try changing the boundary and initial


conditions.
18
Fun Things to Try:

Try changing the boundary and initial


conditions.
Example: initialize the eld to 0.5
18
Fun Things to Try:

Try changing the boundary and initial


conditions.

Try changing the Amplication factor in the


time step.
Example: initialize the eld to 0.5
18
Fun Things to Try:

Try changing the boundary and initial


conditions.

Try changing the Amplication factor in the


time step.
Example: initialize the eld to 0.5
Example: set A=1.1
18
Fun Things to Try:

Try changing the boundary and initial


conditions.

Try changing the Amplication factor in the


time step.

Try replacing the ExplicitDiffusionTerm with


an ImplicitDiffusionTerm
Example: initialize the eld to 0.5
Example: set A=1.1
18
Three Exercises

Run diffusionX.py with an amplication


factor of 0.9

Run diffusionX.py with an amplication


factor of 1.0

Run diffusionX.py with an amplication


factor of 1.1
(Explicit Method)
19
Three More Exercises

Run diffusionCN.py with an amplication


factor of 1

Run diffusionCN.py with an amplication


factor of 10

Run diffusionCN.py with an amplication


factor of 100
(Semi-Implicit Method)
20
Three Final Exercises

Run diffusionI.py with an amplication factor


of 0.9

Run diffusionI.py with an amplication factor


of 1.0

Run diffusionI.py with an amplication factor


of 1.1

Run diffusionI.py with an amplication factor


of 2, 5, 7, 10, 100,...
(Implicit Method)
21
FiPy Resources
22
FiPy Resources

FiPy Manual (tutorials and useful examples)


22
FiPy Resources

FiPy Manual (tutorials and useful examples)

FiPy Reference (what every single command


does)
22
FiPy Resources

FiPy Manual (tutorials and useful examples)

FiPy Reference (what every single command


does)

Mailing List: py@nist.gov


22
FiPy Resources

FiPy Manual (tutorials and useful examples)

FiPy Reference (what every single command


does)

Mailing List: py@nist.gov

You can also email:

John Guyer: guyer@nist.gov

Dan Wheeler: daniel.wheeler@nist.gov


22
FiPy Resources

FiPy Manual (tutorials and useful examples)

FiPy Reference (what every single command


does)

Mailing List: py@nist.gov

You can also email:

John Guyer: guyer@nist.gov

Dan Wheeler: daniel.wheeler@nist.gov

FiPy Website http://www.ctcms.nist.gov/py/


22

You might also like