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

Contents of /trunk/doc/examples/cookbook/example09a.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3075 - (show annotations)
Wed Jul 28 02:51:20 2010 UTC (9 years, 1 month ago) by ahallam
Original Path: trunk/doc/examples/cookbook/example09.py
File MIME type: text/x-python
File size: 7707 byte(s)
Updates to cookbook example. Lumping turned off for order 2 models until bug resolves.
1
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 ############################################################FILE HEADER
23 # example09.py
24 # Antony Hallam
25 # Seismic Wave Equation Simulation using acceleration solution.
26 # 3D model with multiple layers.
27
28 #######################################################EXTERNAL MODULES
29 from esys.escript import *
30 from esys.finley import Rectangle
31 import os
32 # smoothing operator
33 from esys.escript.pdetools import Projector, Locator
34 from esys.escript.unitsSI import *
35 import numpy as np
36 import pylab as pl
37 import matplotlib.cm as cm
38 from esys.escript.linearPDEs import LinearPDE
39 from esys.finley import ReadMesh
40
41 ########################################################MPI WORLD CHECK
42 if getMPISizeWorld() > 1:
43 import sys
44 print "This example will not run in an MPI world."
45 sys.exit(0)
46
47 #################################################ESTABLISHING VARIABLES
48 # where to save output data
49 savepath = "data/example09"
50 mkDir(savepath)
51 #Geometric and material property related variables.
52 mx = 1000. # model lenght
53 my = 1000. # model width
54 mz=200.0
55 step=5.0 # the element size
56 ndx = int(mx/step) # steps in x direction
57 ndy = int(my/step) # steps in y direction
58 ndz = int(mz/step)
59
60 vel2=1800.; vel1=3000.
61 rho2=2300.; rho1=3100. #density
62 mu2=(vel2**2.)*rho2/8.; mu1=(vel1**2.)*rho1/8. #bulk modulus
63 lam2=mu2*6.; lam1=mu1*6. #lames constant
64
65 # Time related variables.
66 tend=0.5 # end time
67 h=0.0005 # time step
68 # data recording times
69 rtime=0.0 # first time to record
70 rtime_inc=tend/50.0 # time increment to record
71 #Check to make sure number of time steps is not too large.
72 print "Time step size= ",h, "Expected number of outputs= ",tend/h
73
74 U0=0.1 # amplitude of point source
75 ls=500 # length of the source
76 source=np.zeros(ls,'float') # source array
77 decay1=np.zeros(ls,'float') # decay curve one
78 decay2=np.zeros(ls,'float') # decay curve two
79 time=np.zeros(ls,'float') # time values
80 g=np.log(0.01)/ls
81
82 dfeq=50 #Dominant Frequency
83 a = 2.0 * (np.pi * dfeq)**2.0
84 t0 = 5.0 / (2.0 * np.pi * dfeq)
85 srclength = 5. * t0
86 ls = int(srclength/h)
87 print 'source length',ls
88 source=np.zeros(ls,'float') # source array
89 ampmax=0
90 for it in range(0,ls):
91 t = it*h
92 tt = t-t0
93 dum1 = np.exp(-a * tt * tt)
94 source[it] = -2. * a * tt * dum1
95 # source[it] = exp(-a * tt * tt) !gaussian
96 if (abs(source[it]) > ampmax):
97 ampmax = abs(source[it])
98 #source[t]=np.exp(g*t)*U0*np.sin(2.*np.pi*t/(0.75*ls))*(np.exp(-.1*g*t)-1)
99 #decay1[t]=np.exp(g*t)
100 #decay2[t]=(np.exp(-.1*g*t)-1)
101 time[t]=t*h
102 #tdecay=decay1*decay2*U0
103 #decay1=decay1*U0; decay2=decay2*U0
104 pl.clf();
105 pl.plot(source)
106 #pl.plot(time,decay1);pl.plot(time,decay2);
107 #pl.plot(time,tdecay)
108 pl.savefig(os.path.join(savepath,'source.png'))
109
110 # will introduce a spherical source at middle left of bottom face
111 xc=[mx/2,my/2,0.]
112
113 ####################################################DOMAIN CONSTRUCTION
114 domain=ReadMesh(os.path.join(savepath,'example09m.fly')) # create the domain
115 x=domain.getX() # get the locations of the nodes in the domain
116
117 lam=Scalar(0,Function(domain))
118 lam.setTaggedValue("vintfa",lam1)
119 lam.setTaggedValue("vintfb",lam2)
120 mu=Scalar(0,Function(domain))
121 mu.setTaggedValue("vintfa",mu1)
122 mu.setTaggedValue("vintfb",mu2)
123 rho=Scalar(0,Function(domain))
124 rho.setTaggedValue("vintfa",rho1)
125 rho.setTaggedValue("vintfb",rho2)
126
127 ##########################################################ESTABLISH PDE
128 mypde=LinearPDE(domain) # create pde
129 mypde.setSymmetryOn() # turn symmetry on
130 # turn lumping on for more efficient solving
131 #mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().LUMPING)
132 kmat = kronecker(domain) # create the kronecker delta function of the domain
133 mypde.setValue(D=rho*kmat) #set the general form value D
134
135
136
137 ##########################################################ESTABLISH ABC
138 # Define where the boundary decay will be applied.
139 #bn=50.
140 #bleft=xstep*bn; bright=mx-(xstep*bn); bbot=my-(ystep*bn)
141 ## btop=ystep*bn # don't apply to force boundary!!!
142
143 ## locate these points in the domain
144 #left=x[0]-bleft; right=x[0]-bright; bottom=x[1]-bbot
145
146 #tgamma=0.85 # decay value for exponential function
147 #def calc_gamma(G,npts):
148 # func=np.sqrt(abs(-1.*np.log(G)/(npts**2.)))
149 # return func
150
151 #gleft = calc_gamma(tgamma,bleft)
152 #gright = calc_gamma(tgamma,bleft)
153 #gbottom= calc_gamma(tgamma,ystep*bn)
154
155 #print 'gamma', gleft,gright,gbottom
156
157 ## calculate decay functions
158 #def abc_bfunc(gamma,loc,x,G):
159 # func=exp(-1.*(gamma*abs(loc-x))**2.)
160 # return func
161
162 #fleft=abc_bfunc(gleft,bleft,x[0],tgamma)
163 #fright=abc_bfunc(gright,bright,x[0],tgamma)
164 #fbottom=abc_bfunc(gbottom,bbot,x[1],tgamma)
165 ## apply these functions only where relevant
166 #abcleft=fleft*whereNegative(left)
167 #abcright=fright*wherePositive(right)
168 #abcbottom=fbottom*wherePositive(bottom)
169 ## make sure the inside of the abc is value 1
170 #abcleft=abcleft+whereZero(abcleft)
171 #abcright=abcright+whereZero(abcright)
172 #abcbottom=abcbottom+whereZero(abcbottom)
173 ## multiply the conditions together to get a smooth result
174 #abc=abcleft*abcright*abcbottom
175
176 #visualise the boundary function
177 #abcT=abc.toListOfTuples()
178 #abcT=np.reshape(abcT,(ndx+1,ndy+1))
179 #pl.clf(); pl.imshow(abcT); pl.colorbar();
180 #pl.savefig(os.path.join(savepath,"abc.png"))
181
182
183 ############################################FIRST TIME STEPS AND SOURCE
184 # define small radius around point xc
185 src_length = 40; print "src_length = ",src_length
186 # set initial values for first two time steps with source terms
187 xb=FunctionOnBoundary(domain).getX()
188 y=source[0]*(cos(length(xb-xc)*3.1415/src_length)+1)*whereNegative(length(xb-src_length))
189 src_dir=numpy.array([0.,0.,1.0]) # defines direction of point source as down
190 y=y*src_dir
191 mypde.setValue(y=y) #set the source as a function on the boundary
192 # initial value of displacement at point source is constant (U0=0.01)
193 # for first two time steps
194 u=[0.0,0.0,0.0]*whereNegative(x)
195 u_m1=u
196
197 ####################################################ITERATION VARIABLES
198 n=0 # iteration counter
199 t=0 # time counter
200 ##############################################################ITERATION
201 while t<tend:
202 # get current stress
203 g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))#*abc
204 mypde.setValue(X=-stress) # set PDE values
205 accel = mypde.getSolution() #get PDE solution for accelleration
206 u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
207 u_p1=u_p1#*abc # apply boundary conditions
208 u_m1=u; u=u_p1 # shift values by 1
209 # save current displacement, acceleration and pressure
210 if (t >= rtime):
211 saveVTK(os.path.join(savepath,"ex09.%05d.vtu"%n),displacement=length(u),\
212 acceleration=length(accel),tensor=stress)
213 rtime=rtime+rtime_inc #increment data save time
214 # increment loop values
215 t=t+h; n=n+1
216 if (n < ls):
217 y=source[n]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
218 y=y*src_dir; mypde.setValue(y=y) #set the source as a function on the boundary
219 print n,"-th time step t ",t

  ViewVC Help
Powered by ViewVC 1.1.26