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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26