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

Annotation of /trunk/doc/examples/cookbook/example08c.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3195 - (hide annotations)
Wed Sep 22 00:28:04 2010 UTC (8 years, 5 months ago) by ahallam
File MIME type: text/x-python
File size: 9531 byte(s)
Shortened runtime of cookbook examples to aid testing.
1 ahallam 3075
2     ########################################################
3     #
4     # Copyright (c) 2009-2010 by University of Queensland
5     # Earth Systems Science Computational Center (ESSCC)
6     # http://www.uq.edu.au/esscc
7     #
8     # Primary Business: Queensland, Australia
9     # Licensed under the Open Software License version 3.0
10     # http://www.opensource.org/licenses/osl-3.0.php
11     #
12     ########################################################
13    
14     __copyright__="""Copyright (c) 2009-2010 by University of Queensland
15     Earth Systems Science Computational Center (ESSCC)
16     http://www.uq.edu.au/esscc
17     Primary Business: Queensland, Australia"""
18     __license__="""Licensed under the Open Software License version 3.0
19     http://www.opensource.org/licenses/osl-3.0.php"""
20     __url__="https://launchpad.net/escript-finley"
21    
22     """
23     Author: Antony Hallam antony.hallam@uqconnect.edu.au
24     """
25    
26     ############################################################FILE HEADER
27     # example08c.py
28     # Create either a 2D syncline or anticline model using pycad meshing
29     # tools. Wave equation solution.
30    
31     #######################################################EXTERNAL MODULES
32     import matplotlib
33     matplotlib.use('agg') #It's just here for automated testing
34     from esys.pycad import * #domain constructor
35     from esys.pycad.gmsh import Design #Finite Element meshing package
36     from esys.finley import MakeDomain #Converter for escript
37     import os #file path tool
38     from math import * # math package
39     from esys.escript import *
40     from esys.escript.unitsSI import *
41     from esys.escript.linearPDEs import LinearPDE
42     from esys.escript.pdetools import Projector
43     from cblib import toRegGrid, subsample
44 jfenwick 3148 import matplotlib
45     matplotlib.use('agg') #It's just here for automated testing
46    
47 ahallam 3075 import pylab as pl #Plotting package
48     import numpy as np
49    
50     ########################################################MPI WORLD CHECK
51     if getMPISizeWorld() > 1:
52     import sys
53     print "This example will not run in an MPI world."
54     sys.exit(0)
55    
56     #################################################ESTABLISHING VARIABLES
57     #set modal to 1 for a syncline or -1 for an anticline structural
58     #configuration
59     modal=-1
60    
61     # the folder to put our outputs in, leave blank "" for script path -
62     # note this folder path must exist to work
63     save_path= os.path.join("data","example08c")
64     mkDir(save_path)
65    
66     ################################################ESTABLISHING PARAMETERS
67     #Model Parameters
68     width=1000.0*m #width of model
69     depth=1000.0*m #depth of model
70     dx=5
71     xstep=dx # calculate the size of delta x
72     ystep=dx # calculate the size of delta y
73    
74     sspl=51 #number of discrete points in spline
75     dsp=width/(sspl-1) #dx of spline steps for width
76     dep_sp=500.0*m #avg depth of spline
77     h_sp=250.*m #heigh of spline
78     orit=-1.0 #orientation of spline 1.0=>up -1.0=>down
79    
80     vel2=1800.; vel1=3000.
81     rho2=2300.; rho1=3100. #density
82     mu2=(vel2**2.)*rho2/8.; mu1=(vel1**2.)*rho1/8. #bulk modulus
83     lam2=mu2*6.; lam1=mu1*6. #lames constant
84    
85    
86     # Time related variables.
87 ahallam 3195 testing=True
88     if testing:
89     print 'The testing end time is curerntly sellected this severely limits the number of time iterations.'
90     print "Try changing testing to False for more iterations."
91     tend=0.001
92     else:
93     tend=0.5 # end time
94    
95 ahallam 3075 h=0.0001 # time step
96     # data recording times
97     rtime=0.0 # first time to record
98     rtime_inc=tend/50.0 # time increment to record
99     # will introduce a spherical source at middle left of bottom face
100     xc=[width/2,0]
101     #Check to make sure number of time steps is not too large.
102     print "Time step size= ",h, "Expected number of outputs= ",tend/h
103    
104     U0=0.1 # amplitude of point source
105     ls=500 # length of the source
106     source=np.zeros(ls,'float') # source array
107     decay1=np.zeros(ls,'float') # decay curve one
108     decay2=np.zeros(ls,'float') # decay curve two
109     time=np.zeros(ls,'float') # time values
110     g=np.log(0.01)/ls
111    
112     dfeq=50 #Dominant Frequency
113     a = 2.0 * (np.pi * dfeq)**2.0
114     t0 = 5.0 / (2.0 * np.pi * dfeq)
115     srclength = 5. * t0
116     ls = int(srclength/h)
117     print 'source length',ls
118     source=np.zeros(ls,'float') # source array
119     ampmax=0
120     for it in range(0,ls):
121     t = it*h
122     tt = t-t0
123     dum1 = np.exp(-a * tt * tt)
124     source[it] = -2. * a * tt * dum1
125     if (abs(source[it]) > ampmax):
126     ampmax = abs(source[it])
127     time[t]=t*h
128    
129     ####################################################DOMAIN CONSTRUCTION
130     # Domain Corners
131     p0=Point(0.0, 0.0, 0.0)
132     p1=Point(0.0, depth, 0.0)
133     p2=Point(width, depth, 0.0)
134     p3=Point(width, 0.0, 0.0)
135    
136     # Generate Material Boundary
137     x=[ Point(i*dsp\
138     ,dep_sp+modal*orit*h_sp*cos(pi*i*dsp/dep_sp+pi))\
139     for i in range(0,sspl)\
140     ]
141     mysp = Spline(*tuple(x))
142     # Start and end of material boundary.
143     x1=mysp.getStartPoint()
144     x2=mysp.getEndPoint()
145    
146     # Create TOP BLOCK
147     # lines
148     tbl1=Line(p0,x1)
149     tbl2=mysp
150     tbl3=Line(x2,p3)
151     l30=Line(p3, p0)
152     # curve
153     tblockloop = CurveLoop(tbl1,tbl2,tbl3,l30)
154     # surface
155     tblock = PlaneSurface(tblockloop)
156     # Create BOTTOM BLOCK
157     # lines
158     bbl1=Line(x1,p1)
159     bbl3=Line(p2,x2)
160     bbl4=-mysp
161     l12=Line(p1, p2)
162     # curve
163     bblockloop = CurveLoop(bbl1,l12,bbl3,bbl4)
164    
165     # surface
166     bblock = PlaneSurface(bblockloop)
167    
168     #clockwise check as splines must be set as polygons in the point order
169     #they were created. Otherwise get a line across plot.
170     bblockloop2=CurveLoop(mysp,Line(x2,p2),Line(p2,p1),Line(p1,x1))
171    
172     ################################################CREATE MESH FOR ESCRIPT
173     # Create a Design which can make the mesh
174     d=Design(dim=2, element_size=dx, order=2)
175     # Add the subdomains and flux boundaries.
176     d.addItems(PropertySet("top",tblock),PropertySet("bottom",bblock),\
177     PropertySet("linetop",l30))
178     # Create the geometry, mesh and Escript domain
179     d.setScriptFileName(os.path.join(save_path,"example08c.geo"))
180     d.setMeshFileName(os.path.join(save_path,"example08c.msh"))
181     domain=MakeDomain(d, optimizeLabeling=True)
182     x=domain.getX()
183     print "Domain has been generated ..."
184    
185     lam=Scalar(0,Function(domain))
186     lam.setTaggedValue("top",lam1)
187     lam.setTaggedValue("bottom",lam2)
188     mu=Scalar(0,Function(domain))
189     mu.setTaggedValue("top",mu1)
190     mu.setTaggedValue("bottom",mu2)
191     rho=Scalar(0,Function(domain))
192     rho.setTaggedValue("top",rho1)
193     rho.setTaggedValue("bottom",rho2)
194    
195     ##########################################################ESTABLISH PDE
196     mypde=LinearPDE(domain) # create pde
197     mypde.setSymmetryOn() # turn symmetry on
198     # turn lumping on for more efficient solving
199     #mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().LUMPING)
200     kmat = kronecker(domain) # create the kronecker delta function of the domain
201     mypde.setValue(D=rho*kmat) #set the general form value D
202    
203     ##########################################################ESTABLISH ABC
204     # Define where the boundary decay will be applied.
205 ahallam 3089 bn=20.
206 ahallam 3075 bleft=xstep*bn; bright=width-(xstep*bn); bbot=depth-(ystep*bn)
207     # btop=ystep*bn # don't apply to force boundary!!!
208    
209     # locate these points in the domain
210     left=x[0]-bleft; right=x[0]-bright; bottom=x[1]-bbot
211    
212     tgamma=0.85 # decay value for exponential function
213     def calc_gamma(G,npts):
214     func=np.sqrt(abs(-1.*np.log(G)/(npts**2.)))
215     return func
216    
217     gleft = calc_gamma(tgamma,bleft)
218     gright = calc_gamma(tgamma,bleft)
219     gbottom= calc_gamma(tgamma,ystep*bn)
220    
221     print 'gamma', gleft,gright,gbottom
222    
223     # calculate decay functions
224     def abc_bfunc(gamma,loc,x,G):
225     func=exp(-1.*(gamma*abs(loc-x))**2.)
226     return func
227    
228     fleft=abc_bfunc(gleft,bleft,x[0],tgamma)
229     fright=abc_bfunc(gright,bright,x[0],tgamma)
230     fbottom=abc_bfunc(gbottom,bbot,x[1],tgamma)
231     # apply these functions only where relevant
232     abcleft=fleft*whereNegative(left)
233     abcright=fright*wherePositive(right)
234     abcbottom=fbottom*wherePositive(bottom)
235     # make sure the inside of the abc is value 1
236     abcleft=abcleft+whereZero(abcleft)
237     abcright=abcright+whereZero(abcright)
238     abcbottom=abcbottom+whereZero(abcbottom)
239     # multiply the conditions together to get a smooth result
240     abc=abcleft*abcright*abcbottom
241    
242     ############################################FIRST TIME STEPS AND SOURCE
243     # define small radius around point xc
244     src_length = 40; print "src_length = ",src_length
245     # set initial values for first two time steps with source terms
246     xb=FunctionOnBoundary(domain).getX()
247     y=source[0]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(xb-src_length))
248     src_dir=numpy.array([0.,1.]) # defines direction of point source as down
249     y=y*src_dir
250     mypde.setValue(y=y) #set the source as a function on the boundary
251     # initial value of displacement at point source is constant (U0=0.01)
252     # for first two time steps
253     u=[0.0,0.0]*wherePositive(x)
254     u_m1=u
255    
256     ####################################################ITERATION VARIABLES
257     n=0 # iteration counter
258     t=0 # time counter
259     ##############################################################ITERATION
260     while t<tend:
261     # get current stress
262     g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))
263     mypde.setValue(X=-stress*abc) # set PDE values
264     accel = mypde.getSolution() #get PDE solution for accelleration
265     u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
266     u_p1=u_p1*abc # apply boundary conditions
267     u_m1=u; u=u_p1 # shift values by 1
268     # save current displacement, acceleration and pressure
269     if (t >= rtime):
270     saveVTK(os.path.join(save_path,"ex08c.%05d.vtu"%n),\
271     vector_displacement=u,displacement=length(u),\
272     vector_acceleration=accel,acceleration=length(accel),\
273     tensor=stress)
274     rtime=rtime+rtime_inc #increment data save time
275     # increment loop values
276     t=t+h; n=n+1
277     if (n < ls):
278     y=source[n]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
279     y=y*src_dir; mypde.setValue(y=y) #set the source as a function on the boundary
280     print n,"-th time step t ",t

  ViewVC Help
Powered by ViewVC 1.1.26