/[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 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (9 years, 4 months ago) by jfenwick
File MIME type: text/x-python
File size: 7784 byte(s)
Don't panic.
Updating copyright stamps

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