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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6651 - (show annotations)
Wed Feb 7 02:12:08 2018 UTC (2 years, 3 months ago) by jfenwick
File MIME type: text/x-python
File size: 8532 byte(s)
Make everyone sad by touching all the files

Copyright dates update

1 from __future__ import division, print_function
2 ##############################################################################
3 #
4 # Copyright (c) 2009-2018 by The University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Apache License, version 2.0
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 __copyright__="""Copyright (c) 2009-2018 by The University of Queensland
18 http://www.uq.edu.au
19 Primary Business: Queensland, Australia"""
20 __license__="""Licensed under the Apache License, version 2.0
21 http://www.apache.org/licenses/LICENSE-2.0"""
22 __url__="https://launchpad.net/escript-finley"
23
24 ############################################################FILE HEADER
25 # example08b.py
26 # Antony Hallam
27 # Seismic Wave Equation Simulation using acceleration solution.
28 # Extend the solution in example 08a to use absorbing boundary
29 # conditions.
30
31 #######################################################EXTERNAL MODULES
32 import matplotlib
33 matplotlib.use('agg') #It's just here for automated testing
34 from esys.escript import *
35 from esys.weipa import saveVTK
36 import os
37 # smoothing operator
38 from esys.escript.pdetools import Projector, Locator
39 from esys.escript.unitsSI import *
40 import numpy as np
41
42 import pylab as pl
43 import matplotlib.cm as cm
44 from esys.escript.linearPDEs import LinearPDE, SolverOptions
45 try:
46 # This imports the rectangle domain function
47 from esys.finley import Rectangle
48 HAVE_FINLEY = True
49 except ImportError:
50 print("Finley module not available")
51 HAVE_FINLEY = False
52 ########################################################MPI WORLD CHECK
53 if getMPISizeWorld() > 1:
54 import sys
55 print("This example will not run in an MPI world.")
56 sys.exit(0)
57
58 if HAVE_FINLEY:
59 #################################################ESTABLISHING VARIABLES
60 # where to save output data
61 savepath = "data/example08b"
62 mkDir(savepath)
63 #Geometric and material property related variables.
64 mx = 1000. # model lenght
65 my = 1000. # model width
66 ndx = 300 # steps in x direction
67 ndy = 300 # steps in y direction
68 xstep=mx/ndx # calculate the size of delta x
69 ystep=abs(my/ndy) # calculate the size of delta y
70 lam=3.462e9 #lames constant
71 mu=3.462e9 #bulk modulus
72 rho=1154. #density
73 # Time related variables.
74 testing=True
75 if testing:
76 print('The testing end time is currently selected. This severely limits the number of time iterations.')
77 print("Try changing testing to False for more iterations.")
78 tend=0.001
79 else:
80 tend=0.5 # end time
81
82 h=0.0001 # time step
83 # data recording times
84 rtime=0.0 # first time to record
85 rtime_inc=tend/50.0 # time increment to record
86 #Check to make sure number of time steps is not too large.
87 print("Time step size= ",h, "Expected number of outputs= ",tend/h)
88
89 U0=0.1 # amplitude of point source
90
91 dfeq=50 #Dominant Frequency
92 a = 2.0 * (np.pi * dfeq)**2.0
93 t0 = 5.0 / (2.0 * np.pi * dfeq)
94 srclength = 5. * t0
95 ls = int(srclength/h)
96 print('source length',ls)
97
98 source=np.zeros(ls,'float') # source array
99 decay1=np.zeros(ls,'float') # decay curve one
100 decay2=np.zeros(ls,'float') # decay curve two
101 time=np.zeros(ls,'float') # time values
102 g=np.log(0.01)/ls
103
104 ampmax=0
105 for it in range(0,ls):
106 t = it*h
107 tt = t-t0
108 dum1 = np.exp(-a * tt * tt)
109 source[it] = -2. * a * tt * dum1
110 # source[it] = exp(-a * tt * tt) !gaussian
111 if (abs(source[it]) > ampmax):
112 ampmax = abs(source[it])
113 #source[t]=np.exp(g*t)*U0*np.sin(2.*np.pi*t/(0.75*ls))*(np.exp(-.1*g*t)-1)
114 #decay1[t]=np.exp(g*t)
115 #decay2[t]=(np.exp(-.1*g*t)-1)
116 time[it]=t*h
117 #tdecay=decay1*decay2*U0
118 #decay1=decay1*U0; decay2=decay2*U0
119 pl.clf();
120 pl.plot(source)
121 #pl.plot(time,decay1);pl.plot(time,decay2);
122 #pl.plot(time,tdecay)
123 pl.savefig(os.path.join(savepath,'source.png'))
124
125 # will introduce a spherical source at middle left of bottom face
126 xc=[mx/2,0]
127
128 ####################################################DOMAIN CONSTRUCTION
129 domain=Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy,order=2) # create the domain
130 x=domain.getX() # get the locations of the nodes in the domani
131
132 ##########################################################ESTABLISH PDE
133 mypde=LinearPDE(domain) # create pde
134 mypde.setSymmetryOn() # turn symmetry on
135 # turn lumping on for more efficient solving
136 mypde.getSolverOptions().setSolverMethod(SolverOptions.HRZ_LUMPING)
137 kmat = kronecker(domain) # create the kronecker delta function of the domain
138 mypde.setValue(D=kmat*rho) #set the general form value D
139
140 ##########################################################ESTABLISH ABC
141 # Define where the boundary decay will be applied.
142 bn=50.
143 bleft=xstep*bn; bright=mx-(xstep*bn); bbot=my-(ystep*bn)
144 # btop=ystep*bn # don't apply to force boundary!!!
145
146 # locate these points in the domain
147 left=x[0]-bleft; right=x[0]-bright; bottom=x[1]-bbot
148
149 tgamma=0.85 # decay value for exponential function
150 def calc_gamma(G,npts):
151 func=np.sqrt(abs(-1.*np.log(G)/(npts**2.)))
152 return func
153
154 gleft = calc_gamma(tgamma,bleft)
155 gright = calc_gamma(tgamma,bleft)
156 gbottom= calc_gamma(tgamma,ystep*bn)
157
158 print('gamma', gleft,gright,gbottom)
159
160 # calculate decay functions
161 def abc_bfunc(gamma,loc,x,G):
162 func=exp(-1.*(gamma*abs(loc-x))**2.)
163 return func
164
165 fleft=abc_bfunc(gleft,bleft,x[0],tgamma)
166 fright=abc_bfunc(gright,bright,x[0],tgamma)
167 fbottom=abc_bfunc(gbottom,bbot,x[1],tgamma)
168 # apply these functions only where relevant
169 abcleft=fleft*whereNegative(left)
170 abcright=fright*wherePositive(right)
171 abcbottom=fbottom*wherePositive(bottom)
172 # make sure the inside of the abc is value 1
173 abcleft=abcleft+whereZero(abcleft)
174 abcright=abcright+whereZero(abcright)
175 abcbottom=abcbottom+whereZero(abcbottom)
176 # multiply the conditions together to get a smooth result
177 abc=abcleft*abcright*abcbottom
178
179 #visualise the boundary function
180 #abcT=abc.toListOfTuples()
181 #abcT=np.reshape(abcT,(ndx+1,ndy+1))
182 #pl.clf(); pl.imshow(abcT); pl.colorbar();
183 #pl.savefig(os.path.join(savepath,"abc.png"))
184
185
186 ############################################FIRST TIME STEPS AND SOURCE
187 # define small radius around point xc
188 src_length = 40; print("src_length = ",src_length)
189 # set initial values for first two time steps with source terms
190 y=source[0]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
191 src_dir=np.array([0.,1.]) # defines direction of point source as down
192 y=y*src_dir
193 mypde.setValue(y=y) #set the source as a function on the boundary
194 # turn lumping on for more efficient solving
195 mypde.getSolverOptions().setSolverMethod(SolverOptions.HRZ_LUMPING)
196 # for first two time steps
197 u=[0.0,0.0]*wherePositive(x)
198 u_m1=u
199
200 ####################################################ITERATION VARIABLES
201 n=0 # iteration counter
202 t=0 # time counter
203 ##############################################################ITERATION
204 while t<tend:
205 # get current stress
206 g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))
207 mypde.setValue(X=-stress*abc) # set PDE values
208 accel = mypde.getSolution() #get PDE solution for accelleration
209 u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
210 u_p1=u_p1*abc # apply boundary conditions
211 u_m1=u; u=u_p1 # shift values by 1
212 # save current displacement, acceleration and pressure
213 if (t >= rtime):
214 saveVTK(os.path.join(savepath,"ex08b.%05d.vtu"%n),displacement=length(u),\
215 acceleration=length(accel),tensor=stress)
216 rtime=rtime+rtime_inc #increment data save time
217 # increment loop values
218 t=t+h; n=n+1
219 if (n < ls):
220 y=source[n]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
221 y=y*src_dir; mypde.setValue(y=y) #set the source as a function on the boundary
222 print("time step %d, t=%s"%(n,t))

  ViewVC Help
Powered by ViewVC 1.1.26