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