/[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 3148 - (hide annotations)
Fri Sep 3 02:09:47 2010 UTC (8 years, 5 months ago) by jfenwick
File MIME type: text/x-python
File size: 9321 byte(s)
Another attempt to patch the X issue

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     tend=0.5 # end time
88     h=0.0001 # time step
89     # data recording times
90     rtime=0.0 # first time to record
91     rtime_inc=tend/50.0 # time increment to record
92     # will introduce a spherical source at middle left of bottom face
93     xc=[width/2,0]
94     #Check to make sure number of time steps is not too large.
95     print "Time step size= ",h, "Expected number of outputs= ",tend/h
96    
97     U0=0.1 # amplitude of point source
98     ls=500 # length of the source
99     source=np.zeros(ls,'float') # source array
100     decay1=np.zeros(ls,'float') # decay curve one
101     decay2=np.zeros(ls,'float') # decay curve two
102     time=np.zeros(ls,'float') # time values
103     g=np.log(0.01)/ls
104    
105     dfeq=50 #Dominant Frequency
106     a = 2.0 * (np.pi * dfeq)**2.0
107     t0 = 5.0 / (2.0 * np.pi * dfeq)
108     srclength = 5. * t0
109     ls = int(srclength/h)
110     print 'source length',ls
111     source=np.zeros(ls,'float') # source array
112     ampmax=0
113     for it in range(0,ls):
114     t = it*h
115     tt = t-t0
116     dum1 = np.exp(-a * tt * tt)
117     source[it] = -2. * a * tt * dum1
118     if (abs(source[it]) > ampmax):
119     ampmax = abs(source[it])
120     time[t]=t*h
121    
122     ####################################################DOMAIN CONSTRUCTION
123     # Domain Corners
124     p0=Point(0.0, 0.0, 0.0)
125     p1=Point(0.0, depth, 0.0)
126     p2=Point(width, depth, 0.0)
127     p3=Point(width, 0.0, 0.0)
128    
129     # Generate Material Boundary
130     x=[ Point(i*dsp\
131     ,dep_sp+modal*orit*h_sp*cos(pi*i*dsp/dep_sp+pi))\
132     for i in range(0,sspl)\
133     ]
134     mysp = Spline(*tuple(x))
135     # Start and end of material boundary.
136     x1=mysp.getStartPoint()
137     x2=mysp.getEndPoint()
138    
139     # Create TOP BLOCK
140     # lines
141     tbl1=Line(p0,x1)
142     tbl2=mysp
143     tbl3=Line(x2,p3)
144     l30=Line(p3, p0)
145     # curve
146     tblockloop = CurveLoop(tbl1,tbl2,tbl3,l30)
147     # surface
148     tblock = PlaneSurface(tblockloop)
149     # Create BOTTOM BLOCK
150     # lines
151     bbl1=Line(x1,p1)
152     bbl3=Line(p2,x2)
153     bbl4=-mysp
154     l12=Line(p1, p2)
155     # curve
156     bblockloop = CurveLoop(bbl1,l12,bbl3,bbl4)
157    
158     # surface
159     bblock = PlaneSurface(bblockloop)
160    
161     #clockwise check as splines must be set as polygons in the point order
162     #they were created. Otherwise get a line across plot.
163     bblockloop2=CurveLoop(mysp,Line(x2,p2),Line(p2,p1),Line(p1,x1))
164    
165     ################################################CREATE MESH FOR ESCRIPT
166     # Create a Design which can make the mesh
167     d=Design(dim=2, element_size=dx, order=2)
168     # Add the subdomains and flux boundaries.
169     d.addItems(PropertySet("top",tblock),PropertySet("bottom",bblock),\
170     PropertySet("linetop",l30))
171     # Create the geometry, mesh and Escript domain
172     d.setScriptFileName(os.path.join(save_path,"example08c.geo"))
173     d.setMeshFileName(os.path.join(save_path,"example08c.msh"))
174     domain=MakeDomain(d, optimizeLabeling=True)
175     x=domain.getX()
176     print "Domain has been generated ..."
177    
178     lam=Scalar(0,Function(domain))
179     lam.setTaggedValue("top",lam1)
180     lam.setTaggedValue("bottom",lam2)
181     mu=Scalar(0,Function(domain))
182     mu.setTaggedValue("top",mu1)
183     mu.setTaggedValue("bottom",mu2)
184     rho=Scalar(0,Function(domain))
185     rho.setTaggedValue("top",rho1)
186     rho.setTaggedValue("bottom",rho2)
187    
188     ##########################################################ESTABLISH PDE
189     mypde=LinearPDE(domain) # create pde
190     mypde.setSymmetryOn() # turn symmetry on
191     # turn lumping on for more efficient solving
192     #mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().LUMPING)
193     kmat = kronecker(domain) # create the kronecker delta function of the domain
194     mypde.setValue(D=rho*kmat) #set the general form value D
195    
196     ##########################################################ESTABLISH ABC
197     # Define where the boundary decay will be applied.
198 ahallam 3089 bn=20.
199 ahallam 3075 bleft=xstep*bn; bright=width-(xstep*bn); bbot=depth-(ystep*bn)
200     # btop=ystep*bn # don't apply to force boundary!!!
201    
202     # locate these points in the domain
203     left=x[0]-bleft; right=x[0]-bright; bottom=x[1]-bbot
204    
205     tgamma=0.85 # decay value for exponential function
206     def calc_gamma(G,npts):
207     func=np.sqrt(abs(-1.*np.log(G)/(npts**2.)))
208     return func
209    
210     gleft = calc_gamma(tgamma,bleft)
211     gright = calc_gamma(tgamma,bleft)
212     gbottom= calc_gamma(tgamma,ystep*bn)
213    
214     print 'gamma', gleft,gright,gbottom
215    
216     # calculate decay functions
217     def abc_bfunc(gamma,loc,x,G):
218     func=exp(-1.*(gamma*abs(loc-x))**2.)
219     return func
220    
221     fleft=abc_bfunc(gleft,bleft,x[0],tgamma)
222     fright=abc_bfunc(gright,bright,x[0],tgamma)
223     fbottom=abc_bfunc(gbottom,bbot,x[1],tgamma)
224     # apply these functions only where relevant
225     abcleft=fleft*whereNegative(left)
226     abcright=fright*wherePositive(right)
227     abcbottom=fbottom*wherePositive(bottom)
228     # make sure the inside of the abc is value 1
229     abcleft=abcleft+whereZero(abcleft)
230     abcright=abcright+whereZero(abcright)
231     abcbottom=abcbottom+whereZero(abcbottom)
232     # multiply the conditions together to get a smooth result
233     abc=abcleft*abcright*abcbottom
234    
235     ############################################FIRST TIME STEPS AND SOURCE
236     # define small radius around point xc
237     src_length = 40; print "src_length = ",src_length
238     # set initial values for first two time steps with source terms
239     xb=FunctionOnBoundary(domain).getX()
240     y=source[0]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(xb-src_length))
241     src_dir=numpy.array([0.,1.]) # defines direction of point source as down
242     y=y*src_dir
243     mypde.setValue(y=y) #set the source as a function on the boundary
244     # initial value of displacement at point source is constant (U0=0.01)
245     # for first two time steps
246     u=[0.0,0.0]*wherePositive(x)
247     u_m1=u
248    
249     ####################################################ITERATION VARIABLES
250     n=0 # iteration counter
251     t=0 # time counter
252     ##############################################################ITERATION
253     while t<tend:
254     # get current stress
255     g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))
256     mypde.setValue(X=-stress*abc) # set PDE values
257     accel = mypde.getSolution() #get PDE solution for accelleration
258     u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
259     u_p1=u_p1*abc # apply boundary conditions
260     u_m1=u; u=u_p1 # shift values by 1
261     # save current displacement, acceleration and pressure
262     if (t >= rtime):
263     saveVTK(os.path.join(save_path,"ex08c.%05d.vtu"%n),\
264     vector_displacement=u,displacement=length(u),\
265     vector_acceleration=accel,acceleration=length(accel),\
266     tensor=stress)
267     rtime=rtime+rtime_inc #increment data save time
268     # increment loop values
269     t=t+h; n=n+1
270     if (n < ls):
271     y=source[n]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
272     y=y*src_dir; mypde.setValue(y=y) #set the source as a function on the boundary
273     print n,"-th time step t ",t

  ViewVC Help
Powered by ViewVC 1.1.26