/[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 3892 - (show annotations)
Tue Apr 10 08:57:23 2012 UTC (6 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 7691 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



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