Getting Acquainted with the Python and FiPy A Crash by htt39969

VIEWS: 26 PAGES: 77

									Getting Acquainted with
 the Python and FiPy: A
     Crash Course
         R. Edwin García
      redwing@purdue.edu



                           1
        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




                                                2
The Interface: xemacs


  run xemacs by typing:
                          xemacs




                                   3
The Interface: xemacs


  run xemacs by typing:
                          xemacs




                                   3
     The Interface: xemacs

open file

           run xemacs by typing:
                                   xemacs




                                            3
     The Interface: xemacs
save file
open file

           run xemacs by typing:
                                   xemacs




                                            3
     The Interface: xemacs
           cut
save file
open file

           run xemacs by typing:
                                   xemacs




                                            3
     The Interface: xemacs
           cut      copy
save file
open file

           run xemacs by typing:
                                   xemacs




                                            3
     The Interface: xemacs
           cut      copy    paste
save file
open file

           run xemacs by typing:
                                   xemacs




                                            3
Setting Up xemacs’ Options




                             4
Setting Up xemacs’ Options




                             4
  Setting Up xemacs’ Options


highlight syntaxis




                               4
Setting Up xemacs’ Options




                             4
Setting Up xemacs’ Options


   line numbers




                             4
Setting Up xemacs’ Options




                             4
Setting Up xemacs’ Options




    save options




                             4
Running Something Simple: helloWorld.py




                                          5
Running Something Simple: helloWorld.py
       ## this is a comment




                                          5
Running Something Simple: helloWorld.py
       ## this is a comment
       import math ## this is how you import modules




                                                       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




                                                       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




                                                       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




                                                       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




                                                       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




                                                       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):




                                                       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




                                                              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




                                                              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




                                                              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



                                                              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

                                                              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

                                                              5
         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


                                                  6
1D Diffusion: The Big Picture




                                7
1D Diffusion: The Big Picture




             L= 1
                                7
1D Diffusion: The Big Picture


          ∂φ
             = ∇ · D∇φ
          ∂t




             L= 1
                                7
1D Diffusion: The Big Picture
       Φ=1


             ∂φ
                = ∇ · D∇φ
             ∂t




                            Φ=0

                L= 1
                                  7
1D Diffusion: The Big Picture
       Φ=1


             ∂φ
                = ∇ · D∇φ
             ∂t


             Φ(x, t=0) = 0

                             Φ=0

                 L= 1
                                   7
Creating the Mesh




                    8
               Creating the Mesh
import fipy ## imports fipy, a finite volumes set of libraries




                                                              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




                                                                           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




                                                                           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




                                                                           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




                                                                                       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)

                                                                                       8
Defining the Concentration Field




                              9
Defining the Concentration Field


 ## create a field with initial values




                                        9
Defining the Concentration Field


 ## create a field with initial values
 phi_o = 0




                                        9
Defining the Concentration Field


 ## create a field with initial values
 phi_o = 0
 from fipy.variables.cellVariable import CellVariable




                                                       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", mesh=mesh_example, value=phi_o)




                                                                                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", mesh=mesh_example, value=phi_o)




                                                                                9
Specifying Boundary Conditions




                             10
Specifying Boundary Conditions
 ## Define boundary condition values
 leftValue = 1.0




                                      10
Specifying Boundary Conditions
 ## Define boundary condition values
 leftValue = 1.0
 rightValue = 0




                                      10
Specifying Boundary Conditions
 ## Define boundary condition values
 leftValue = 1.0
 rightValue = 0


 ## Creation of boundary conditions
 from fipy.boundaryConditions.fixedValue import FixedValue




                                                           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.getFacesRight(), value=rightValue),




                                                                              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.getFacesRight(), value=rightValue),
      FixedValue(faces=mesh_example.getFacesLeft(),value=leftValue))




                                                                              10
Creating the Diffusion Equation
                   ∂φ
                      = ∇ · D∇φ
                   ∂t




                                  11
Creating the Diffusion Equation
                                  ∂φ
                                     = ∇ · D∇φ
                                  ∂t
 D = 1.0 ## diffusivity (m^2/s)




                                                 11
Creating the Diffusion Equation
                                            ∂φ
                                               = ∇ · D∇φ
                                            ∂t
 D = 1.0 ## diffusivity (m^2/s)


 ## Transient diffusion equation is defined next
 from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm




                                                                     11
Creating the Diffusion Equation
                                            ∂φ
                                               = ∇ · D∇φ
                                            ∂t
 D = 1.0 ## diffusivity (m^2/s)
                                                      diffusion
                                                        term
 ## Transient diffusion equation is defined next
 from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm




                                                                     11
Creating the Diffusion Equation
                                            ∂φ
                                               = ∇ · D∇φ
                                            ∂t
 D = 1.0 ## diffusivity (m^2/s)
                                                      diffusion
                                                        term
 ## Transient diffusion equation is defined next
 from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
 from fipy.terms.transientTerm import TransientTerm




                                                                     11
Creating the Diffusion Equation
                                  transient
                                    term ∂φ
                                                  = ∇ · D∇φ
                                            ∂t
 D = 1.0 ## diffusivity (m^2/s)
                                                      diffusion
                                                        term
 ## Transient diffusion equation is defined next
 from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
 from fipy.terms.transientTerm import TransientTerm




                                                                     11
Creating the Diffusion Equation
                                  transient
                                    term ∂φ
                                                  = ∇ · D∇φ
                                            ∂t
 D = 1.0 ## diffusivity (m^2/s)
                                                      diffusion
                                                        term
 ## Transient diffusion equation is defined next
 from fipy.terms.explicitDiffusionTerm import ExplicitDiffusionTerm
 from fipy.terms.transientTerm import TransientTerm
 eqX = TransientTerm() == ExplicitDiffusionTerm(coeff = D)




                                                                     11
Specifying the Time Stepping
        of the Model




                               12
Specifying the Time Stepping
        of the Model

  timeStep = 0.09*deltax**2/(2*D) ## size of time step




                                                         12
Specifying the Time Stepping
        of the Model
                           exponent

  timeStep = 0.09*deltax**2/(2*D) ## size of time step




                                                         12
Specifying the Time Stepping
        of the Model
                           exponent

  timeStep = 0.09*deltax**2/(2*D) ## size of time step
  steps = 9000 ## number of time-steps




                                                         12
Creating a Viewer




                    13
       Creating a Viewer


## create a GUI

from fipy import viewers




                           13
        Creating a Viewer


## create a GUI

from fipy import viewers

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




                                                                           13
Looping Over Time




                    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




                                                                                          14
Launching Our Simulation




                           15
 Launching Our Simulation
• Save your script and assign a descriptive name, e.g.,
  diffusion1D.py




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




                                                          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:




                                                          15
      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




                                                16

								
To top