Getting Acquainted with the Python and FiPy: A Crash Course - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Getting Acquainted with the Python and FiPy: A Crash Course

Description:

you can create an instance of a one dimensional mesh by typing ... FixedValue(faces=mesh_example.getFacesLeft(),value=leftValue)) Creating the Diffusion Equation ... – PowerPoint PPT presentation

Number of Views:447
Avg rating:3.0/5.0
Slides: 17
Provided by: bicephalo
Category:

less

Transcript and Presenter's Notes

Title: Getting Acquainted with the Python and FiPy: A Crash Course


1
Getting Acquainted with the Python and FiPy A
Crash Course
  • R. Edwin García
  • redwing_at_purdue.edu

2
Goal for Today
  • An extremely brief xemacs overview
  • A very simple python example
  • Introduction to FiPy solving the diffusion
    equation in 25 (or less) lines of code

3
The Interface xemacs
4
Setting Up xemacs Options
line numbers
highlight syntaxis
save options
5
Running Something Simple helloWorld.py
  • this is a comment
  • import math this is how you import modules
  • num_items 10 this is an integer variable
  • dx 0.1 this is a float (real) variable
  • x 0 this is another float variable
  • array this is an empty array
  • next is a for loop
  • for index in range(num_items)
  • print "Hello World!" this is a print
    statement
  • note that only indented things are part of
    the
  • for loop
  • x x dx this increases the value of x by
    dx
  • array.append(x) this adds x to the list
  • print "array", array this prints the list

6
What is FiPy?
  • Simply put
  • Is a set of python libraries to solve PDEs
  • In more detail
  • Provides a numerical framework to solve for
    partial differential equations by using the
    finite volumes technique
  • The emphasis is on microstructural evolution

7
1D Diffusion The Big Picture
F(x, t0) 0
8
Creating the Mesh
  • import fipy imports fipy, a finite volumes set
    of libraries
  • sometimes there are modules within modules and
    you can import them
  • by typing
  • from fipy.meshes import grid1D this imports
    the grid1D module from fipy
  • you can define variables of any kind in
    python.
  • For example, the mesh size
  • mesh_size 50 this is an integer
  • or the size of the domain
  • length 1.0 this is a float (real) number
  • or the size of the control volume
  • deltax length/float(mesh_size) note that I
    just forced an integer to become float
  • you can create an instance of a one
    dimensional mesh by typing
  • mesh_example grid1D.Grid1D(nx mesh_size, dx
    deltax)

9
Defining the Concentration Field
  • create a field with initial values
  • phi_o 0
  • from fipy.variables.cellVariable import
    CellVariable
  • phi CellVariable(name"solution variable",
    meshmesh_example, valuephi_o)

10
Specifying Boundary Conditions
  • Define boundary condition values
  • leftValue 1.0
  • rightValue 0
  • Creation of boundary conditions
  • from fipy.boundaryConditions.fixedValue import
    FixedValue
  • BCs (FixedValue(faces mesh_example.getFacesRig
    ht(), valuerightValue),
  • FixedValue(facesmesh_example.getFacesLeft(
    ),valueleftValue))

11
Creating the Diffusion Equation
transient term
  • D 1.0 diffusivity (m2/s)
  • Transient diffusion equation is defined next
  • from fipy.terms.explicitDiffusionTerm import
    ExplicitDiffusionTerm
  • from fipy.terms.transientTerm import
    TransientTerm
  • eqX TransientTerm() ExplicitDiffusionTerm(coe
    ff D)

diffusion term
12
Specifying the Time Stepping of the Model
  • timeStep 0.09deltax2/(2D) size of time
    step
  • steps 9000 number of time-steps

13
Creating a Viewer
  • create a GUI
  • from fipy import viewers
  • viewer viewers.make(vars phi,
    limits'datamin'0.0, 'datamax'1.0)

14
Looping Over Time
  • iterate until you get tired
  • for step in range(steps)
  • eqX.solve(var phi, boundaryConditions
    BCs, dt timeStep) solving the equation
  • viewer.plot() update the GUI

15
Launching Our Simulation
  • Save your script and assign a descriptive name,
    e.g., diffusion1D.py
  • Type python diffusion1D.py in the command line
  • You should get something like this

16
Final Comments
  • You can cancel your calculation by typing
    CTRL-C in the command line
  • You can re-do this calculation and make it 2D by
    using a grid2D.Grid2D class instead
Write a Comment
User Comments (0)
About PowerShow.com