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

revision 4853 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 29  Author: Antony Hallam antony.hallam@uqco Line 28  Author: Antony Hallam antony.hallam@uqco
28  # example01a.py  # example01a.py
29  # Model temperature diffusion between two granite blocks of unequal  # Model temperature diffusion between two granite blocks of unequal
30  # initial temperature. Solve for total energy in the system.  # initial temperature. Solve for total energy in the system.
31    import sys
32  #######################################################EXTERNAL MODULES  #######################################################EXTERNAL MODULES
33  # To solve the problem it is necessary to import the modules we require.  # To solve the problem it is necessary to import the modules we require.
34  from esys.escript import * # This imports everything from the escript library  from esys.escript import * # This imports everything from the escript library
35  from esys.escript.unitsSI import *  from esys.escript.unitsSI import *
36  from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE  from esys.escript.linearPDEs import LinearPDE # This defines LinearPDE as LinearPDE
37  from esys.finley import Rectangle # This imports the rectangle domain function  try:
38        from esys.finley import Rectangle
39  #################################################ESTABLISHING VARIABLES      HAVE_FINLEY = True
40  #Domain related.  except ImportError:
41  mx = 500*m #meters - model length      print("Finley module not available")
42  my = 100*m #meters - model width      HAVE_FINLEY = False
43  ndx = 100 # mesh steps in x direction
44  ndy = 1 # mesh steps in y direction - one dimension means one element  if HAVE_FINLEY:
45  boundloc = mx/2 # location of boundary between the two blocks      #################################################ESTABLISHING VARIABLES
46  #PDE related      #Domain related.
47  rho = 2750. *kg/m**3 #kg/m{3} density of iron      mx = 500*m #meters - model length
48  cp = 790.*J/(kg*K) # J/Kg.K thermal capacity      my = 100*m #meters - model width
49  rhocp = rho*cp      ndx = 100 # mesh steps in x direction
50  kappa = 2.2*W/m/K # watts/m.Kthermal conductivity      ndy = 1 # mesh steps in y direction - one dimension means one element
51  qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source      boundloc = mx/2 # location of boundary between the two blocks
52  T1=20 * Celsius # initial temperature at Block 1      #PDE related
53  T2=2273. * Celsius # base temperature at Block 2      rho = 2750. *kg/m**3 #kg/m{3} density of iron
54        cp = 790.*J/(kg*K) # J/Kg.K thermal capacity
55        rhocp = rho*cp
56  ################################################ESTABLISHING PARAMETERS      kappa = 2.2*W/m/K # watts/m.Kthermal conductivity
57  t=0 * day  # our start time, usually zero      qH=0 * J/(sec*m**3) # J/(sec.m{3}) no heat source
58  tend=50 * yr # - time to end simulation      T1=20 * Celsius # initial temperature at Block 1
59  outputs = 200 # number of time steps required.      T2=2273. * Celsius # base temperature at Block 2
60  h=(tend-t)/outputs #size of time step
61  #user warning statement
62  print("Expected Number of time outputs is: ", (tend-t)/h)      ################################################ESTABLISHING PARAMETERS
63  i=0 #loop counter      t=0 * day  # our start time, usually zero
64  #the folder to put our outputs in, leave blank "" for script path      tend=50 * yr # - time to end simulation
65  save_path= os.path.join("data","example01")      outputs = 200 # number of time steps required.
66  #ensure the dir exists      h=(tend-t)/outputs #size of time step
67  mkDir(save_path, os.path.join(save_path,"tempT"))      #user warning statement
68        print("Expected Number of time outputs is: ", (tend-t)/h)
69  ####################################################DOMAIN CONSTRUCTION      i=0 #loop counter
70  blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)      #the folder to put our outputs in, leave blank "" for script path
71        save_path= os.path.join("data","example01")
72  ###############################################ESCRIPT PDE CONSTRUCTION      #ensure the dir exists
73  #... open PDE and set coefficients ...      mkDir(save_path, os.path.join(save_path,"tempT"))
74  mypde=LinearPDE(blocks)
75  mypde.setSymmetryOn()      ####################################################DOMAIN CONSTRUCTION
76  A=zeros((2,2))      blocks = Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
77  A[0,0]=kappa
78  mypde.setValue(A=A,D=rhocp/h)      ###############################################ESCRIPT PDE CONSTRUCTION
79  # ... set initial temperature ....      #... open PDE and set coefficients ...
80  x=Solution(blocks).getX()      mypde=LinearPDE(blocks)
81  T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))      mypde.setSymmetryOn()
82        A=zeros((2,2))
83  ########################################################START ITERATION      A[0,0]=kappa
84  while t<tend:      mypde.setValue(A=A,D=rhocp/h)
85        i+=1      # ... set initial temperature ....
86        t+=h      x=Solution(blocks).getX()
87        mypde.setValue(Y=qH+rhocp/h*T)      T= T1*whereNegative(x[0]-boundloc)+T2*(1-whereNegative(x[0]-boundloc))
88        T=mypde.getSolution()
89        totE=integrate(rhocp*T)      ########################################################START ITERATION
90        print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))      while t<tend:
91              i+=1
92              t+=h
93              mypde.setValue(Y=qH+rhocp/h*T)
94              T=mypde.getSolution()
95              totE=integrate(rhocp*T)
96              print("time step %s at t=%e days completed. total energy = %e."%(i,t/day,totE))

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