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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4576 - (show annotations)
Mon Dec 9 23:35:30 2013 UTC (4 years, 5 months ago) by sshaw
File MIME type: text/x-python
File size: 6597 byte(s)
python3ified things, replaced mixed whitespace and xrange calls
1
2 ##############################################################################
3 #
4 # Copyright (c) 2009-2013 by University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Open Software License version 3.0
9 # http://www.opensource.org/licenses/osl-3.0.php
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development since 2012 by School of Earth Sciences
13 #
14 ##############################################################################
15
16 __copyright__="""Copyright (c) 2009-2013 by University of Queensland
17 http://www.uq.edu.au
18 Primary Business: Queensland, Australia"""
19 __license__="""Licensed under the Open Software License version 3.0
20 http://www.opensource.org/licenses/osl-3.0.php"""
21 __url__="https://launchpad.net/escript-finley"
22
23 ############################################################FILE HEADER
24 # example09.py
25 # Antony Hallam
26 # Seismic Wave Equation Simulation using acceleration solution.
27 # 3D model with multiple layers. Layercake example.
28
29 #######################################################EXTERNAL MODULES
30 import matplotlib
31 matplotlib.use('agg') #It's just here for automated testing
32 from esys.escript import *
33 from esys.finley import Rectangle
34 from esys.weipa import saveVTK
35 import os
36 # smoothing operator
37 from esys.escript.pdetools import Projector, Locator
38 from esys.escript.unitsSI import *
39 import numpy as np
40
41 import pylab as pl
42 import matplotlib.cm as cm
43 from esys.escript.linearPDEs import LinearPDE
44 from esys.finley import ReadMesh
45
46 ########################################################MPI WORLD CHECK
47 if getMPISizeWorld() > 1:
48 import sys
49 print("This example will not run in an MPI world.")
50 sys.exit(0)
51
52 #################################################ESTABLISHING VARIABLES
53 # where to save output data
54 savepath = "data/example09b"
55 meshpath = "data/example09m"
56 mkDir(savepath)
57 #Geometric and material property related variables.
58 step=4.0 # the element size
59
60 vel=1800. #starting velocity
61 rhoc=2000. #starting density
62 nlayers=9 #number of layers in layercake model.
63
64 ####################################################TESTING SWITCH
65 testing=True
66 if testing:
67 print('The testing end time is currently selected. This severely limits the number of time iterations.')
68 print("Try changing testing to False for more iterations.")
69 tend=0.001
70 #Model Parameters
71 mx=40.
72 my=40.
73 mz=20.
74 outputs=5
75 else:
76 tend=0.1 # end time
77 #Model Parameters
78 mx=100.0 #x width of model
79 my=100.0 #y width of model
80 mz=50.0 #depth of model
81 outputs=200
82
83 ####################################################TIME RELATED VARIABLES
84 h=0.00001 # time step
85 # data recording times
86 rtime=0.0 # first time to record
87 rtime_inc=tend/outputs # time increment to record
88 #Check to make sure number of time steps is not too large.
89 print("Time step size= ",h, "Expected number of outputs= ",tend/h)
90
91 ####################################################CREATING THE SOURCE FUNCTION
92 U0=0.1 # amplitude of point source
93 ls=500 # length of the source
94 source=np.zeros(ls,'float') # source array
95 decay1=np.zeros(ls,'float') # decay curve one
96 decay2=np.zeros(ls,'float') # decay curve two
97 time=np.zeros(ls,'float') # time values
98 g=np.log(0.01)/ls
99
100 dfeq=50 #Dominant Frequency
101 a = 2.0 * (np.pi * dfeq)**2.0
102 t0 = 5.0 / (2.0 * np.pi * dfeq)
103 srclength = 5. * t0
104 ls = int(srclength/h)
105 print('source length',ls)
106 source=np.zeros(ls,'float') # source array
107 ampmax=0
108 for it in range(0,ls):
109 t = it*h
110 tt = t-t0
111 dum1 = np.exp(-a * tt * tt)
112 source[it] = -2. * a * tt * dum1
113 if (abs(source[it]) > ampmax):
114 ampmax = abs(source[it])
115 time[t]=t*h
116
117 # will introduce a spherical source at middle left of bottom face
118 xc=[mx/2,my/2,0]
119
120 ####################################################DOMAIN CONSTRUCTION
121 domain=ReadMesh(os.path.join(meshpath,'example09lc.fly')) # create the domain
122 x=domain.getX() # get the locations of the nodes in the domain
123
124 lam=Scalar(0,Function(domain))
125 mu=Scalar(0,Function(domain))
126 rho=Scalar(0,Function(domain))
127
128 #Setting parameters for each layer in the model.
129 for i in range(0,nlayers):
130 rho.setTaggedValue("volume_%d"%i,rhoc+i*100.)
131 lamc=(vel+i*100.)**2.*(rhoc+i*100.)/2.
132 muc=(vel+i*100.)**2.*(rhoc+i*100.)/4.
133 lam.setTaggedValue("volume_%d"%i,lamc)
134 mu.setTaggedValue("volume_%d"%i,muc)
135
136 ##########################################################ESTABLISH PDE
137 mypde=LinearPDE(domain) # create pde
138 mypde.setSymmetryOn() # turn symmetry on
139 # turn lumping on for more efficient solving
140 #mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().HRZ_LUMPING)
141 kmat = kronecker(domain) # create the kronecker delta function of the domain
142 mypde.setValue(D=rho*kmat) #set the general form value D
143
144 ############################################FIRST TIME STEPS AND SOURCE
145 # define small radius around point xc
146 src_rad = 20; print("src radius= ",src_rad)
147 # set initial values for first two time steps with source terms
148 xb=FunctionOnBoundary(domain).getX()
149 yx=(cos(length(xb-xc)*3.1415/src_rad)+1)*whereNegative(length(xb-xc)-src_rad)
150 stop=Scalar(0.0,FunctionOnBoundary(domain))
151 stop.setTaggedValue("intface_0",1.0)
152 src_dir=numpy.array([0.,0.,1.0]) # defines direction of point source as down
153
154 mypde.setValue(y=source[0]*yx*src_dir*stop) #set the source as a function on the boundary
155 # initial value of displacement at point source is constant (U0=0.01)
156 # for first two time steps
157 u=[0.0,0.0,0.0]*wherePositive(x)
158 u_m1=u
159
160 ####################################################ITERATION VARIABLES
161 n=0 # iteration counter
162 t=0 # time counter
163 ##############################################################ITERATION
164 while t<tend:
165 # get current stress
166 g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))#*abc
167 mypde.setValue(X=-stress) # set PDE values
168 accel = mypde.getSolution() #get PDE solution for accelleration
169 u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
170 u_p1=u_p1#*abc # apply boundary conditions
171 u_m1=u; u=u_p1 # shift values by 1
172 # save current displacement, acceleration and pressure
173 if (t >= rtime):
174 saveVTK(os.path.join(savepath,"ex09b.%05d.vtu"%n),displacement=length(u),\
175 acceleration=length(accel),tensor=stress)
176 rtime=rtime+rtime_inc #increment data save time
177 # increment loop values
178 t=t+h; n=n+1
179 if (n < ls):
180 mypde.setValue(y=source[n]*yx*src_dir*stop) #set the source as a function on the boundary
181 print("time step %d, t=%s"%(n,t))

  ViewVC Help
Powered by ViewVC 1.1.26