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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3148 - (show annotations)
Fri Sep 3 02:09:47 2010 UTC (10 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 7350 byte(s)
Another attempt to patch the X issue

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

  ViewVC Help
Powered by ViewVC 1.1.26