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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3346 - (show annotations)
Fri Nov 12 01:19:02 2010 UTC (9 years ago) by caltinay
File MIME type: text/x-python
File size: 7879 byte(s)
Replaced usage of esys.escript.util.saveVTK by weipa.saveVTK in all python
scripts.

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