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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3892 - (hide annotations)
Tue Apr 10 08:57:23 2012 UTC (7 years ago) by jfenwick
File MIME type: text/x-python
File size: 6501 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



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

  ViewVC Help
Powered by ViewVC 1.1.26