/[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 6651 - (show annotations)
Wed Feb 7 02:12:08 2018 UTC (19 months, 1 week ago) by jfenwick
File MIME type: text/x-python
File size: 7251 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 # example09.py
26 # Antony Hallam
27 # Seismic Wave Equation Simulation using acceleration solution.
28 # 3D model with multiple layers. Layercake example.
29
30 #######################################################EXTERNAL MODULES
31 import matplotlib
32 matplotlib.use('agg') #It's just here for automated testing
33 from esys.escript import *
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, SolverOptions
44 try:
45 # This imports the rectangle domain function
46 from esys.finley import Rectangle, ReadMesh
47 HAVE_FINLEY = True
48 except ImportError:
49 print("Finley module not available")
50 HAVE_FINLEY = False
51 ########################################################MPI WORLD CHECK
52 if getMPISizeWorld() > 1:
53 import sys
54 print("This example will not run in an MPI world.")
55 sys.exit(0)
56
57 if HAVE_FINLEY:
58 #################################################ESTABLISHING VARIABLES
59 # where to save output data
60 savepath = "data/example09b"
61 meshpath = "data/example09m"
62 mkDir(savepath)
63 #Geometric and material property related variables.
64 step=4.0 # the element size
65
66 vel=1800. #starting velocity
67 rhoc=2000. #starting density
68 nlayers=9 #number of layers in layercake model.
69
70 ####################################################TESTING SWITCH
71 testing=True
72 if testing:
73 print('The testing end time is currently selected. This severely limits the number of time iterations.')
74 print("Try changing testing to False for more iterations.")
75 tend=0.001
76 #Model Parameters
77 mx=40.
78 my=40.
79 mz=20.
80 outputs=5
81 else:
82 tend=0.1 # end time
83 #Model Parameters
84 mx=100.0 #x width of model
85 my=100.0 #y width of model
86 mz=50.0 #depth of model
87 outputs=200
88
89 ####################################################TIME RELATED VARIABLES
90 h=0.00001 # time step
91 # data recording times
92 rtime=0.0 # first time to record
93 rtime_inc=tend/outputs # time increment to record
94 #Check to make sure number of time steps is not too large.
95 print("Time step size= ",h, "Expected number of outputs= ",tend/h)
96
97 ####################################################CREATING THE SOURCE FUNCTION
98 U0=0.1 # amplitude of point source
99 dfeq=50 #Dominant Frequency
100 a = 2.0 * (np.pi * dfeq)**2.0
101 t0 = 5.0 / (2.0 * np.pi * dfeq)
102 srclength = 5. * t0
103
104 ls = int(srclength/h)
105 print('source length',ls)
106
107 source=np.zeros(ls,'float') # source array
108 decay1=np.zeros(ls,'float') # decay curve one
109 decay2=np.zeros(ls,'float') # decay curve two
110 time=np.zeros(ls,'float') # time values
111 g=np.log(0.01)/ls
112
113 ampmax=0
114 for it in range(0,ls):
115 t = it*h
116 tt = t-t0
117 dum1 = np.exp(-a * tt * tt)
118 source[it] = -2. * a * tt * dum1
119 if (abs(source[it]) > ampmax):
120 ampmax = abs(source[it])
121 time[it]=t*h
122
123 # will introduce a spherical source at middle left of bottom face
124 xc=[mx/2,my/2,0]
125
126 ####################################################DOMAIN CONSTRUCTION
127 domain=ReadMesh(os.path.join(meshpath,'example09lc.fly')) # create the domain
128 x=domain.getX() # get the locations of the nodes in the domain
129
130 lam=Scalar(0,Function(domain))
131 mu=Scalar(0,Function(domain))
132 rho=Scalar(0,Function(domain))
133
134 #Setting parameters for each layer in the model.
135 for i in range(0,nlayers):
136 rho.setTaggedValue("volume_%d"%i,rhoc+i*100.)
137 lamc=(vel+i*100.)**2.*(rhoc+i*100.)/2.
138 muc=(vel+i*100.)**2.*(rhoc+i*100.)/4.
139 lam.setTaggedValue("volume_%d"%i,lamc)
140 mu.setTaggedValue("volume_%d"%i,muc)
141
142 ##########################################################ESTABLISH PDE
143 mypde=LinearPDE(domain) # create pde
144 mypde.setSymmetryOn() # turn symmetry on
145 # turn lumping on for more efficient solving
146 #mypde.getSolverOptions().setSolverMethod(SolverOptions.HRZ_LUMPING)
147 kmat = kronecker(domain) # create the kronecker delta function of the domain
148 mypde.setValue(D=rho*kmat) #set the general form value D
149
150 ############################################FIRST TIME STEPS AND SOURCE
151 # define small radius around point xc
152 src_rad = 20; print("src radius= ",src_rad)
153 # set initial values for first two time steps with source terms
154 xb=FunctionOnBoundary(domain).getX()
155 yx=(cos(length(xb-xc)*3.1415/src_rad)+1)*whereNegative(length(xb-xc)-src_rad)
156 stop=Scalar(0.0,FunctionOnBoundary(domain))
157 stop.setTaggedValue("intface_0",1.0)
158 src_dir=numpy.array([0.,0.,1.0]) # defines direction of point source as down
159
160 mypde.setValue(y=source[0]*yx*src_dir*stop) #set the source as a function on the boundary
161 # initial value of displacement at point source is constant (U0=0.01)
162 # for first two time steps
163 u=[0.0,0.0,0.0]*wherePositive(x)
164 u_m1=u
165
166 ####################################################ITERATION VARIABLES
167 n=0 # iteration counter
168 t=0 # time counter
169 ##############################################################ITERATION
170 while t<tend:
171 # get current stress
172 g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))#*abc
173 mypde.setValue(X=-stress) # set PDE values
174 accel = mypde.getSolution() #get PDE solution for accelleration
175 u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
176 u_p1=u_p1#*abc # apply boundary conditions
177 u_m1=u; u=u_p1 # shift values by 1
178 # save current displacement, acceleration and pressure
179 if (t >= rtime):
180 saveVTK(os.path.join(savepath,"ex09b.%05d.vtu"%n),displacement=length(u),\
181 acceleration=length(accel),tensor=stress)
182 rtime=rtime+rtime_inc #increment data save time
183 # increment loop values
184 t=t+h; n=n+1
185 if (n < ls):
186 mypde.setValue(y=source[n]*yx*src_dir*stop) #set the source as a function on the boundary
187 print("time step %d, t=%s"%(n,t))

  ViewVC Help
Powered by ViewVC 1.1.26