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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3001 - (show annotations)
Wed Mar 31 04:29:10 2010 UTC (9 years, 10 months ago) by ahallam
File MIME type: text/x-python
File size: 7575 byte(s)
Pressue wave problem, looking at sampling theorem, and Crank-Nicolson
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 # You can shorten the execution time by reducing variable tend from 60 to 0.5
23 # Antony Hallam
24 # Acoustic Wave Equation Simulation
25
26 # Importing all the necessary modules required.
27 from esys.escript import *
28 from esys.finley import Rectangle
29 import sys
30 import os
31 from cblib1 import wavesolver2d
32 # smoothing operator
33 from esys.escript.pdetools import Projector
34 import numpy as np
35 import pylab as pl
36 import matplotlib.cm as cm
37
38 # Establish a save path.
39 savepath = "data/wavesolver2d009mpltestABCnolump0_0006"
40 mkDir(savepath)
41
42
43 #Geometric and material property related variables.
44 mx = 1000. # model lenght
45 my = 1000. # model width
46 ndx = 200 # steps in x direction
47 ndy = 200 # steps in y direction
48
49 xstep=mx/ndx
50 ystep=my/ndy
51
52 lam=3.462e9 #lames constant
53 mu=3.462e9 #bulk modulus
54 rho=1154. #density
55 # Time related variables.
56 tend=0.5 #end time
57 #calculating )the timestep
58 h=(1./5.)*sqrt(rho/(lam+2*mu))*(mx/ndx)
59 #Check to make sure number of time steps is not too large.
60 print "Time step size= ",h, "Expected number of outputs= ",tend/h
61
62 #uncomment the following lines to give the user a chance to stop
63 #proceeder = raw_input("Is this ok?(y/n)")
64 #Exit if user thinks too many outputs.
65 #if proceeder == "n":
66 # sys.exit()
67
68 U0=0.01 # amplitude of point source
69 # spherical source at middle of bottom face
70
71 xc=[500,500]
72
73 mydomain=Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy)
74 #wavesolver2d(mydomain,h,tend,lam,mu,rho,U0,xc,savepath,output="mpl")
75
76
77
78
79 domain=mydomain
80 output="mpl"
81
82
83
84
85
86 from esys.escript.linearPDEs import LinearPDE
87 x=domain.getX()
88
89 ## boundary conditions
90
91 bleft=xstep*50.
92 bright=mx-(xstep*50.)
93 bbot=my-(ystep*50.)
94 btop=ystep*50.
95
96 left=x[0]-bleft
97 right=x[0]-bright
98 bottom=x[1]-bbot
99 top=x[1]-btop
100
101 decay=0.0006
102 fleft=exp(-1.0*(decay*(bleft-x[0]))**2)
103 fright=exp(-1.0*(decay*(x[0]-bright))**2)
104 fbottom=exp(-1.0*(decay*(x[1]-bbot))**2)
105 ftop=exp(-1.0*(decay*(btop-x[1]))**2)
106
107 abcleft=fleft*whereNegative(left)
108 abcright=fright*wherePositive(right)
109 abcbottom=fbottom*wherePositive(bottom)
110 abctop=ftop*whereNegative(top)
111
112 abcleft=abcleft+whereZero(abcleft)
113 abcright=abcright+whereZero(abcright)
114 abcbottom=abcbottom+whereZero(abcbottom)
115 abctop=abctop+whereZero(abctop)
116
117 abc=abcleft*abcright*abcbottom*abctop
118
119 #~ fleftT=fleft.toListOfTuples()
120 #~ fleftT=np.reshape(fleftT,(ndx+1,ndy+1))
121 #~ pl.imshow(fleftT)
122 #~ pl.colorbar()
123 #~ pl.savefig("fleftT.png")
124 #~
125 #~ frightT=fright.toListOfTuples()
126 #~ frightT=np.reshape(frightT,(ndx+1,ndy+1))
127 #~ pl.clf()
128 #~ pl.imshow(frightT)
129 #~ pl.colorbar()
130 #~ pl.savefig("frightT.png")
131 #~
132 #~ fbottomT=fbottom.toListOfTuples()
133 #~ fbottomT=np.reshape(fbottomT,(ndx+1,ndy+1))
134 #~ pl.clf()
135 #~ pl.imshow(fbottomT)
136 #~ pl.colorbar()
137 #~ pl.savefig("fbottomT.png")
138 #~
139 #~ #tester=fright*wherePositive(right)
140 #~ tester=fleft*whereNegative(left)
141 #~ tester=tester.toListOfTuples()
142 #~ tester=np.reshape(tester,(ndx+1,ndy+1))
143 #~ pl.clf()
144 #~ pl.imshow(tester)
145 #~ pl.colorbar()
146 #~ pl.savefig("tester1.png")
147 #~
148 #~ tester=fright*wherePositive(right)
149 #~ tester=tester.toListOfTuples()
150 #~ tester=np.reshape(tester,(ndx+1,ndy+1))
151 #~ pl.clf()
152 #~ pl.imshow(tester)
153 #~ pl.colorbar()
154 #~ pl.savefig("tester2.png")
155 #~
156 #~ tester=fbottom*wherePositive(bottom)
157 #~ tester=tester.toListOfTuples()
158 #~ tester=np.reshape(tester,(ndx+1,ndy+1))
159 #~ pl.clf()
160 #~ pl.imshow(tester)
161 #~ pl.colorbar()
162 #~ pl.savefig("tester3.png")
163
164
165 # ... open new PDE ...
166 mypde=LinearPDE(domain)
167 print mypde.isUsingLumping()
168 print mypde.getSolverOptions()
169 #mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().LUMPING)
170 mypde.setSymmetryOn()
171 kmat = kronecker(domain)
172 mypde.setValue(D=kmat*rho)
173
174 # define small radius around point xc
175 # Lsup(x) returns the maximum value of the argument x
176 src_radius = 50#2*Lsup(domain.getSize())
177 print "src_radius = ",src_radius
178
179 dunit=numpy.array([0.,1.]) # defines direction of point source
180 #~ dunit=(x-xc)
181 #~ absrc=length(dunit)
182 #~ dunit=dunit/maximum(absrc,1e-10)
183
184 # ... set initial values ....
185 n=0
186 # initial value of displacement at point source is constant (U0=0.01)
187 # for first two time steps
188 u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*whereNegative(length(x-xc)-src_radius)*dunit
189 #u=whereNegative(length(x-xc)-src_radius)*dunit
190
191 maxi=0.02
192
193 print u
194 u_m1=u
195 t=0
196
197 #~ u_pot = cbphones(domain,u,[[0,500],[250,500],[400,500]],2)
198 #~ u_pc_x1 = u_pot[0,0]
199 #~ u_pc_y1 = u_pot[0,1]
200 #~ u_pc_x2 = u_pot[1,0]
201 #~ u_pc_y2 = u_pot[1,1]
202 #~ u_pc_x3 = u_pot[2,0]
203 #~ u_pc_y3 = u_pot[2,1]
204 #~
205 #~ # open file to save displacement at point source
206 #~ u_pc_data=open(os.path.join(savepath,'U_pc.out'),'w')
207 #~ u_pc_data.write("%f %f %f %f %f %f %f\n"%(t,u_pc_x1,u_pc_y1,u_pc_x2,u_pc_y2,u_pc_x3,u_pc_y3))
208
209 while t<tend:
210 # ... get current stress ....
211 # t=1.
212 ##OLD WAY
213 g=grad(u)
214 stress=lam*trace(g)*kmat+mu*(g+transpose(g))
215 ### ... get new acceleration ....
216 #mypde.setValue(X=-stress)
217 #a=mypde.getSolution()
218 ### ... get new displacement ...
219 #u_p1=2*u-u_m1+h*h*a
220 ###NEW WAY
221 mypde.setValue(X=-stress*(h*h),Y=(rho*2*u-rho*u_m1))
222 u_p1 = mypde.getSolution()
223 # ... shift displacements ....
224 u_m1=u
225 u=u_p1*abc
226 #stress =
227 t+=h
228 n+=1
229 print n,"-th time step t ",t
230 #~ u_pot = cbphones(domain,u,[[300.,200.],[500.,200.],[750.,200.]],2)
231 #~
232 #~ # print "u at point charge=",u_pc
233 #~ u_pc_x1 = u_pot[0,0]
234 #~ u_pc_y1 = u_pot[0,1]
235 #~ u_pc_x2 = u_pot[1,0]
236 #~ u_pc_y2 = u_pot[1,1]
237 #~ u_pc_x3 = u_pot[2,0]
238 #~ u_pc_y3 = u_pot[2,1]
239
240 # save displacements at point source to file for t > 0
241 #~ u_pc_data.write("%f %f %f %f %f %f %f\n"%(t,u_pc_x1,u_pc_y1,u_pc_x2,u_pc_y2,u_pc_x3,u_pc_y3))
242
243 # ... save current acceleration in units of gravity and displacements
244 #saveVTK(os.path.join(savepath,"usoln.%i.vtu"%n),acceleration=length(a)/9.81,
245 #displacement = length(u), tensor = stress, Ux = u[0] )
246 if output == "vtk":
247 saveVTK(os.path.join(savepath,"tonysol.%i.vtu"%n),output1 = length(u),tensor=stress)
248 if output == "mpl":
249 uT=np.array(u.toListOfTuples())
250 uT=np.reshape(uT,(ndx+1,ndy+1,2))
251 uTz=uT[:,:,1]+uT[:,:,0]
252 uTz=np.transpose(uTz)
253 pl.clf()
254 # plot wave
255 uTz[0,0]=maxi
256 uTz[0,1]=-maxi
257 CS = pl.imshow(uTz,cmap=cm.spectral)
258 pl.colorbar()
259 # labels and formatting
260 pl.title("Wave Equation Cookbook Example ABC.")
261 pl.xlabel("Horizontal Displacement (m)")
262 pl.ylabel("Depth (m)")
263 if getMPIRankWorld() == 0: #check for MPI processing
264 pl.savefig(os.path.join(savepath,"ws04mpl%05d.png"%n))
265
266 #~ u_pc_data.close()
267 #~ os.system("mencoder mf://"+savepath+"/*.png -mf type=png:\
268 #~ w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
269 #~ wsmpl.avi")
270
271 #mencoder mf://*.png -mf type=png:\w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o wsmpl.avi

  ViewVC Help
Powered by ViewVC 1.1.26