/[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 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: 7538 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/wavesolver2d009mpltestABCnolump0_0006"
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.0006
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 # ... open new PDE ...
164 mypde=LinearPDE(domain)
165 print mypde.isUsingLumping()
166 print mypde.getSolverOptions()
167 #mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().LUMPING)
168 mypde.setSymmetryOn()
169 kmat = kronecker(domain)
170 mypde.setValue(D=kmat*rho)
171
172 # define small radius around point xc
173 # Lsup(x) returns the maximum value of the argument x
174 src_radius = 50#2*Lsup(domain.getSize())
175 print "src_radius = ",src_radius
176
177 dunit=numpy.array([0.,1.]) # defines direction of point source
178 #~ dunit=(x-xc)
179 #~ absrc=length(dunit)
180 #~ dunit=dunit/maximum(absrc,1e-10)
181
182 # ... set initial values ....
183 n=0
184 # initial value of displacement at point source is constant (U0=0.01)
185 # for first two time steps
186 u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*whereNegative(length(x-xc)-src_radius)*dunit
187 #u=whereNegative(length(x-xc)-src_radius)*dunit
188
189 maxi=0.02
190
191 print u
192 u_m1=u
193 t=0
194
195 #~ u_pot = cbphones(domain,u,[[0,500],[250,500],[400,500]],2)
196 #~ u_pc_x1 = u_pot[0,0]
197 #~ u_pc_y1 = u_pot[0,1]
198 #~ u_pc_x2 = u_pot[1,0]
199 #~ u_pc_y2 = u_pot[1,1]
200 #~ u_pc_x3 = u_pot[2,0]
201 #~ u_pc_y3 = u_pot[2,1]
202 #~
203 #~ # open file to save displacement at point source
204 #~ u_pc_data=open(os.path.join(savepath,'U_pc.out'),'w')
205 #~ 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))
206
207 while t<tend:
208 # ... get current stress ....
209 # t=1.
210 ##OLD WAY
211 g=grad(u)
212 stress=lam*trace(g)*kmat+mu*(g+transpose(g))
213 ### ... get new acceleration ....
214 #mypde.setValue(X=-stress)
215 #a=mypde.getSolution()
216 ### ... get new displacement ...
217 #u_p1=2*u-u_m1+h*h*a
218 ###NEW WAY
219 mypde.setValue(X=-stress*(h*h),Y=(rho*2*u-rho*u_m1))
220 u_p1 = mypde.getSolution()
221 # ... shift displacements ....
222 u_m1=u
223 u=u_p1*abc
224 #stress =
225 t+=h
226 n+=1
227 print n,"-th time step t ",t
228 #~ u_pot = cbphones(domain,u,[[300.,200.],[500.,200.],[750.,200.]],2)
229 #~
230 #~ # print "u at point charge=",u_pc
231 #~ u_pc_x1 = u_pot[0,0]
232 #~ u_pc_y1 = u_pot[0,1]
233 #~ u_pc_x2 = u_pot[1,0]
234 #~ u_pc_y2 = u_pot[1,1]
235 #~ u_pc_x3 = u_pot[2,0]
236 #~ u_pc_y3 = u_pot[2,1]
237
238 # save displacements at point source to file for t > 0
239 #~ 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))
240
241 # ... save current acceleration in units of gravity and displacements
242 #saveVTK(os.path.join(savepath,"usoln.%i.vtu"%n),acceleration=length(a)/9.81,
243 #displacement = length(u), tensor = stress, Ux = u[0] )
244 if output == "vtk":
245 saveVTK(os.path.join(savepath,"tonysol.%i.vtu"%n),output1 = length(u),tensor=stress)
246 if output == "mpl":
247 uT=np.array(u.toListOfTuples())
248 uT=np.reshape(uT,(ndx+1,ndy+1,2))
249 uTz=uT[:,:,1]+uT[:,:,0]
250 uTz=np.transpose(uTz)
251 pl.clf()
252 # plot wave
253 uTz[0,0]=maxi
254 uTz[0,1]=-maxi
255 CS = pl.imshow(uTz,cmap=cm.spectral)
256 pl.colorbar()
257 # labels and formatting
258 pl.title("Wave Equation Cookbook Example ABC.")
259 pl.xlabel("Horizontal Displacement (m)")
260 pl.ylabel("Depth (m)")
261 if getMPIRankWorld() == 0: #check for MPI processing
262 pl.savefig(os.path.join(savepath,"ws04mpl%05d.png"%n))
263
264 #~ u_pc_data.close()
265 #~ os.system("mencoder mf://"+savepath+"/*.png -mf type=png:\
266 #~ w=800:h=600:fps=25 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o \
267 #~ wsmpl.avi")
268
269 #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