/[escript]/trunk/doc/examples/cookbook/example01c.py
ViewVC logotype

Diff of /trunk/doc/examples/cookbook/example01c.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5287 by jfenwick, Wed Apr 9 05:41:57 2014 UTC revision 5288 by sshaw, Tue Dec 2 23:18:40 2014 UTC
# Line 1  Line 1 
1  from __future__ import division  from __future__ import division, print_function
 from __future__ import print_function  
2  ##############################################################################  ##############################################################################
3  #  #
4  # Copyright (c) 2009-2014 by University of Queensland  # Copyright (c) 2009-2014 by University of Queensland
# Line 38  matplotlib.use('agg') #It's just here fo Line 37  matplotlib.use('agg') #It's just here fo
37  from esys.escript import * # This imports everything from the escript library  from esys.escript import * # This imports everything from the escript library
38  from esys.escript.unitsSI import *  from esys.escript.unitsSI import *
39  from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE  from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE
 from esys.finley import Rectangle # This imports the rectangle domain function  
40  import pylab as pl #Plotting package.  import pylab as pl #Plotting package.
41  import numpy as np #Array package.  import numpy as np #Array package.
42  import os, sys #This package is necessary to handle saving our data.  import os, sys #This package is necessary to handle saving our data.
43    try:
44        # This imports the rectangle domain function
45        from esys.finley import MakeDomain#Converter for escript
46        HAVE_FINLEY = True
47    except ImportError:
48        print("Finley module not available")
49        HAVE_FINLEY = False
50    
51  ########################################################MPI WORLD CHECK  ########################################################MPI WORLD CHECK
52  if getMPISizeWorld() > 1:  if getMPISizeWorld() > 1:
53      import sys      import sys
54      print("This example will not run in an MPI world.")      print("This example will not run in an MPI world.")
55      sys.exit(0)      sys.exit(0)
56    
57  #################################################ESTABLISHING VARIABLES  if HAVE_FINLEY:
58  #Domain related.      #################################################ESTABLISHING VARIABLES
59  mx = 500*m #meters - model length      #Domain related.
60  my = 100*m #meters - model width      mx = 500*m #meters - model length
61  ndx = 100 # mesh steps in x direction      my = 100*m #meters - model width
62  ndy = 1 # mesh steps in y direction - one dimension means one element      ndx = 100 # mesh steps in x direction
63  boundloc = mx/2 # location of boundary between the two blocks      ndy = 1 # mesh steps in y direction - one dimension means one element
64  #PDE related      boundloc = mx/2 # location of boundary between the two blocks
65  rho = 2750. *kg/m**3 #kg/m{3} density of iron      #PDE related
66  cp = 790.*J/(kg*K) # J/Kg.K thermal capacity      rho = 2750. *kg/m**3 #kg/m{3} density of iron
67  rhocp = rho*cp      cp = 790.*J/(kg*K) # J/Kg.K thermal capacity
68  kappa = 2.2*W/m/K # watts/m.Kthermal conductivity      rhocp = rho*cp
69  qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source      kappa = 2.2*W/m/K # watts/m.Kthermal conductivity
70  T1=20 * Celsius # initial temperature at Block 1      qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source
71  T2=2273. * Celsius # base temperature at Block 2      T1=20 * Celsius # initial temperature at Block 1
72        T2=2273. * Celsius # base temperature at Block 2
73  ################################################ESTABLISHING PARAMETERS  
74  t=0 * day  # our start time, usually zero      ################################################ESTABLISHING PARAMETERS
75  tend=50 * yr # - time to end simulation      t=0 * day  # our start time, usually zero
76  outputs = 200 # number of time steps required.      tend=50 * yr # - time to end simulation
77  h=(tend-t)/outputs #size of time step      outputs = 200 # number of time steps required.
78  #user warning statement      h=(tend-t)/outputs #size of time step
79  print("Expected Number of time outputs is: ", (tend-t)/h)      #user warning statement
80  i=0 #loop counter      print("Expected Number of time outputs is: ", (tend-t)/h)
81  #the folder to put our outputs in, leave blank "" for script path      i=0 #loop counter
82  save_path= os.path.join("data","example01")      #the folder to put our outputs in, leave blank "" for script path
83  #ensure the dir exists      save_path= os.path.join("data","example01")
84  mkDir(save_path, os.path.join(save_path,"tempT"))      #ensure the dir exists
85        mkDir(save_path, os.path.join(save_path,"tempT"))
86  ####################################################DOMAIN CONSTRUCTION  
87  blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)      ####################################################DOMAIN CONSTRUCTION
88        blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
89  ###############################################ESCRIPT PDE CONSTRUCTION  
90  #... open PDE and set coefficients ...      ###############################################ESCRIPT PDE CONSTRUCTION
91  mypde=LinearPDE(blocks)      #... open PDE and set coefficients ...
92  mypde.setSymmetryOn()      mypde=LinearPDE(blocks)
93  A=zeros((2,2))      mypde.setSymmetryOn()
94  A[0,0]=kappa      A=zeros((2,2))
95  mypde.setValue(A=A,D=rhocp/h)      A[0,0]=kappa
96  # ... set initial temperature ....      mypde.setValue(A=A,D=rhocp/h)
97  x=Solution(blocks).getX()      # ... set initial temperature ....
98  T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))      x=Solution(blocks).getX()
99        T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))
100  # ... open a collector for the time marks and corresponding total energy  
101  t_list=[]      # ... open a collector for the time marks and corresponding total energy
102  E_list=[]      t_list=[]
103  # ... convert solution points for plotting      E_list=[]
104  plx = x.toListOfTuples()      # ... convert solution points for plotting
105  plx = np.array(plx) #convert to tuple to numpy array      plx = x.toListOfTuples()
106  plx = plx[:,0] #extract x locations      plx = np.array(plx) #convert to tuple to numpy array
107  ########################################################START ITERATION      plx = plx[:,0] #extract x locations
108  while t<tend:      ########################################################START ITERATION
109        i+=1      while t<tend:
110        t+=h            i+=1
111        mypde.setValue(Y=qH+rhocp/h*T)            t+=h
112        T=mypde.getSolution()            mypde.setValue(Y=qH+rhocp/h*T)
113        totE=integrate(rhocp*T)            T=mypde.getSolution()
114        print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))            totE=integrate(rhocp*T)
115        t_list.append(t)            print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))
116        E_list.append(totE)            t_list.append(t)
117              E_list.append(totE)
118        #establish figure 1 for temperature vs x plots  
119        tempT = T.toListOfTuples()            #establish figure 1 for temperature vs x plots
120        pl.figure(1) #current figure            tempT = T.toListOfTuples()
121        pl.plot(plx,tempT) #plot solution            pl.figure(1) #current figure
122        # add title            pl.plot(plx,tempT) #plot solution
123        pl.axis([0,mx,T1*.9,T2*1.1])            # add title
124        pl.title("Temperature across blocks at time %d days"%(t/day))            pl.axis([0,mx,T1*.9,T2*1.1])
125        pl.ylabel('Temperature (K)')            pl.title("Temperature across blocks at time %d days"%(t/day))
126        pl.xlabel("Length (m)")            pl.ylabel('Temperature (K)')
127        #save figure to file            pl.xlabel("Length (m)")
128        pl.savefig(os.path.join(save_path,"tempT", "blockspyplot%03d.png"%i))            #save figure to file
129        pl.clf() #clear figure            pl.savefig(os.path.join(save_path,"tempT", "blockspyplot%03d.png"%i))
130                    pl.clf() #clear figure
131  ###############################################################PLOTTING            
132  # plot the total energy over time:      ###############################################################PLOTTING
133  pl.figure(2)      # plot the total energy over time:
134  pl.plot(t_list,E_list)      pl.figure(2)
135  pl.title("Total Energy")      pl.plot(t_list,E_list)
136  pl.axis([0,max(t_list),0,max(E_list)*1.1])      pl.title("Total Energy")
137  pl.ylabel('Energy (W)')      pl.axis([0,max(t_list),0,max(E_list)*1.1])
138  pl.xlabel('Time (s)')      pl.ylabel('Energy (W)')
139  pl.savefig(os.path.join(save_path,"totE_ex01c.png"))      pl.xlabel('Time (s)')
140  pl.clf()      pl.savefig(os.path.join(save_path,"totE_ex01c.png"))
141        pl.clf()
142  ###########################################################MAKE A MOVIE  
143  # compile the *.png files to create a*.avi video that show T change      ###########################################################MAKE A MOVIE
144  # with time. This opperation uses linux mencoder. For other operating      # compile the *.png files to create a*.avi video that show T change
145  # systems it may be possible to use your favourite video compiler to      # with time. This opperation uses linux mencoder. For other operating
146  # convert image files to videos. To enable this step uncomment the      # systems it may be possible to use your favourite video compiler to
147  # following lines.      # convert image files to videos. To enable this step uncomment the
148        # following lines.
149  #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\  
150  #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \      #os.system("mencoder mf://"+save_path+"/tempT"+"/*.png -mf type=png:\
151  #example01tempT.avi")      #w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
152        #example01tempT.avi")

Legend:
Removed from v.5287  
changed lines
  Added in v.5288

  ViewVC Help
Powered by ViewVC 1.1.26