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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3055 - (show annotations)
Sun Jul 4 21:52:36 2010 UTC (11 years, 5 months ago) by ahallam
File MIME type: text/x-python
File size: 7214 byte(s)
3d wave equation and ABC
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
40 ########################################################MPI WORLD CHECK
41 if getMPISizeWorld() > 1:
42 import sys
43 print "This example will not run in an MPI world."
44 sys.exit(0)
45
46 #################################################ESTABLISHING VARIABLES
47 # where to save output data
48 savepath = "data/example08b"
49 mkDir(savepath)
50 #Geometric and material property related variables.
51 mx = 1000. # model lenght
52 my = 1000. # model width
53 ndx = 500 # steps in x direction
54 ndy = 500 # steps in y direction
55 xstep=mx/ndx # calculate the size of delta x
56 ystep=abs(my/ndy) # calculate the size of delta y
57 lam=3.462e9 #lames constant
58 mu=3.462e9 #bulk modulus
59 rho=1154. #density
60 # Time related variables.
61 tend=0.5 # end time
62 h=0.0005 # time step
63 # data recording times
64 rtime=0.0 # first time to record
65 rtime_inc=tend/50.0 # time increment to record
66 #Check to make sure number of time steps is not too large.
67 print "Time step size= ",h, "Expected number of outputs= ",tend/h
68
69 U0=0.1 # amplitude of point source
70 ls=500 # length of the source
71 source=np.zeros(ls,'float') # source array
72 decay1=np.zeros(ls,'float') # decay curve one
73 decay2=np.zeros(ls,'float') # decay curve two
74 time=np.zeros(ls,'float') # time values
75 g=np.log(0.01)/ls
76
77 dfeq=50 #Dominant Frequency
78 a = 2.0 * (np.pi * dfeq)**2.0
79 t0 = 5.0 / (2.0 * np.pi * dfeq)
80 srclength = 5. * t0
81 ls = int(srclength/h)
82 print 'source length',ls
83 source=np.zeros(ls,'float') # source array
84 ampmax=0
85 for it in range(0,ls):
86 t = it*h
87 tt = t-t0
88 dum1 = np.exp(-a * tt * tt)
89 source[it] = -2. * a * tt * dum1
90 # source[it] = exp(-a * tt * tt) !gaussian
91 if (abs(source[it]) > ampmax):
92 ampmax = abs(source[it])
93 #source[t]=np.exp(g*t)*U0*np.sin(2.*np.pi*t/(0.75*ls))*(np.exp(-.1*g*t)-1)
94 #decay1[t]=np.exp(g*t)
95 #decay2[t]=(np.exp(-.1*g*t)-1)
96 time[t]=t*h
97 #tdecay=decay1*decay2*U0
98 #decay1=decay1*U0; decay2=decay2*U0
99 pl.clf();
100 pl.plot(source)
101 #pl.plot(time,decay1);pl.plot(time,decay2);
102 #pl.plot(time,tdecay)
103 pl.savefig(os.path.join(savepath,'source.png'))
104
105 # will introduce a spherical source at middle left of bottom face
106 xc=[mx/2,0]
107
108 ####################################################DOMAIN CONSTRUCTION
109 domain=Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy) # create the domain
110 x=domain.getX() # get the locations of the nodes in the domani
111
112 ##########################################################ESTABLISH PDE
113 mypde=LinearPDE(domain) # create pde
114 mypde.setSymmetryOn() # turn symmetry on
115 # turn lumping on for more efficient solving
116 mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().LUMPING)
117 kmat = kronecker(domain) # create the kronecker delta function of the domain
118 mypde.setValue(D=kmat*rho) #set the general form value D
119
120 ##########################################################ESTABLISH ABC
121 # Define where the boundary decay will be applied.
122 bn=50.
123 bleft=xstep*bn; bright=mx-(xstep*bn); bbot=my-(ystep*bn)
124 # btop=ystep*bn # don't apply to force boundary!!!
125
126 # locate these points in the domain
127 left=x[0]-bleft; right=x[0]-bright; bottom=x[1]-bbot
128
129 tgamma=0.85 # decay value for exponential function
130 def calc_gamma(G,npts):
131 func=np.sqrt(abs(-1.*np.log(G)/(npts**2.)))
132 return func
133
134 gleft = calc_gamma(tgamma,bleft)
135 gright = calc_gamma(tgamma,bleft)
136 gbottom= calc_gamma(tgamma,ystep*bn)
137
138 print 'gamma', gleft,gright,gbottom
139
140 # calculate decay functions
141 def abc_bfunc(gamma,loc,x,G):
142 func=exp(-1.*(gamma*abs(loc-x))**2.)
143 return func
144
145 fleft=abc_bfunc(gleft,bleft,x[0],tgamma)
146 fright=abc_bfunc(gright,bright,x[0],tgamma)
147 fbottom=abc_bfunc(gbottom,bbot,x[1],tgamma)
148 # apply these functions only where relevant
149 abcleft=fleft*whereNegative(left)
150 abcright=fright*wherePositive(right)
151 abcbottom=fbottom*wherePositive(bottom)
152 # make sure the inside of the abc is value 1
153 abcleft=abcleft+whereZero(abcleft)
154 abcright=abcright+whereZero(abcright)
155 abcbottom=abcbottom+whereZero(abcbottom)
156 # multiply the conditions together to get a smooth result
157 abc=abcleft*abcright*abcbottom
158
159 #visualise the boundary function
160 abcT=abc.toListOfTuples()
161 abcT=np.reshape(abcT,(ndx+1,ndy+1))
162 pl.clf(); pl.imshow(abcT); pl.colorbar();
163 pl.savefig(os.path.join(savepath,"abc.png"))
164
165
166 ############################################FIRST TIME STEPS AND SOURCE
167 # define small radius around point xc
168 src_length = 40; print "src_length = ",src_length
169 # set initial values for first two time steps with source terms
170 y=source[0]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
171 src_dir=numpy.array([0.,-1.]) # defines direction of point source as down
172 y=y*src_dir
173 mypde.setValue(y=y) #set the source as a function on the boundary
174 # initial value of displacement at point source is constant (U0=0.01)
175 # for first two time steps
176 u=[0.0,0.0]*whereNegative(x)
177 u_m1=u
178
179 ####################################################ITERATION VARIABLES
180 n=0 # iteration counter
181 t=0 # time counter
182 ##############################################################ITERATION
183 while t<tend:
184 # get current stress
185 g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))*abc
186 mypde.setValue(X=-stress) # set PDE values
187 accel = mypde.getSolution() #get PDE solution for accelleration
188 u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
189 u_p1=u_p1*abc # apply boundary conditions
190 u_m1=u; u=u_p1 # shift values by 1
191 # save current displacement, acceleration and pressure
192 if (t >= rtime):
193 saveVTK(os.path.join(savepath,"ex08b.%05d.vtu"%n),displacement=length(u),\
194 acceleration=length(accel),tensor=stress)
195 rtime=rtime+rtime_inc #increment data save time
196 # increment loop values
197 t=t+h; n=n+1
198 if (n < ls):
199 y=source[n]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
200 y=y*src_dir; mypde.setValue(y=y) #set the source as a function on the boundary
201 print n,"-th time step t ",t

  ViewVC Help
Powered by ViewVC 1.1.26