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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4821 - (show annotations)
Tue Apr 1 04:58:33 2014 UTC (5 years, 4 months ago) by sshaw
File MIME type: text/x-python
File size: 9762 byte(s)
moved SolverOptions to c++, split into SolverOptions for the options and SolverBuddy as the state as a precursor to per-pde solving... does break some use cases (e.g. pde.getSolverOptions().DIRECT will now fail, new value access is with SolverOptions.DIRECT), examples and documentation updated to match
1
2 ##############################################################################
3 #
4 # Copyright (c) 2009-2014 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 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-2014 by University of Queensland
18 http://www.uq.edu.au
19 Primary Business: Queensland, Australia"""
20 __license__="""Licensed under the Open Software License version 3.0
21 http://www.opensource.org/licenses/osl-3.0.php"""
22 __url__="https://launchpad.net/escript-finley"
23
24 """
25 Author: Antony Hallam antony.hallam@uqconnect.edu.au
26 """
27
28 ############################################################FILE HEADER
29 # example08c.py
30 # Create either a 2D syncline or anticline model using pycad meshing
31 # tools. Wave equation solution.
32
33 #######################################################EXTERNAL MODULES
34 import matplotlib
35 matplotlib.use('agg') #It's just here for automated testing
36 from esys.pycad import * #domain constructor
37 from esys.pycad.gmsh import Design #Finite Element meshing package
38 from esys.finley import MakeDomain #Converter for escript
39 import os #file path tool
40 from math import * # math package
41 from esys.escript import *
42 from esys.escript.unitsSI import *
43 from esys.escript.linearPDEs import LinearPDE, SolverOptions
44 from esys.escript.pdetools import Projector
45 from esys.weipa import saveVTK
46 from cblib import toRegGrid, subsample
47 import matplotlib
48 matplotlib.use('agg') #It's just here for automated testing
49
50 import pylab as pl #Plotting package
51 import numpy as np
52
53 ########################################################MPI WORLD CHECK
54 if getMPISizeWorld() > 1:
55 import sys
56 print("This example will not run in an MPI world.")
57 sys.exit(0)
58
59 #################################################ESTABLISHING VARIABLES
60 #set modal to 1 for a syncline or -1 for an anticline structural
61 #configuration
62 modal=-1
63
64 # the folder to put our outputs in, leave blank "" for script path -
65 # note this folder path must exist to work
66 save_path= os.path.join("data","example08c")
67 mkDir(save_path)
68
69 ################################################ESTABLISHING PARAMETERS
70 #Model Parameters
71 width=1000.0*m #width of model
72 depth=1000.0*m #depth of model
73 dx=5
74 xstep=dx # calculate the size of delta x
75 ystep=dx # calculate the size of delta y
76
77 sspl=51 #number of discrete points in spline
78 dsp=width/(sspl-1) #dx of spline steps for width
79 dep_sp=500.0*m #avg depth of spline
80 h_sp=250.*m #heigh of spline
81 orit=-1.0 #orientation of spline 1.0=>up -1.0=>down
82
83 vel2=1800.; vel1=3000.
84 rho2=2300.; rho1=3100. #density
85 mu2=(vel2**2.)*rho2/8.; mu1=(vel1**2.)*rho1/8. #bulk modulus
86 lam2=mu2*6.; lam1=mu1*6. #lames constant
87
88
89 # Time related variables.
90 testing=True
91 if testing:
92 print('The testing end time is currently selected. This severely limits the number of time iterations.')
93 print("Try changing testing to False for more iterations.")
94 tend=0.001
95 else:
96 tend=0.5 # end time
97
98 h=0.0001 # time step
99 # data recording times
100 rtime=0.0 # first time to record
101 rtime_inc=tend/50.0 # time increment to record
102 # will introduce a spherical source at middle left of bottom face
103 xc=[width/2,0]
104 #Check to make sure number of time steps is not too large.
105 print("Time step size= ",h, "Expected number of outputs= ",tend/h)
106
107 U0=0.1 # amplitude of point source
108 ls=500 # length of the source
109 source=np.zeros(ls,'float') # source array
110 decay1=np.zeros(ls,'float') # decay curve one
111 decay2=np.zeros(ls,'float') # decay curve two
112 time=np.zeros(ls,'float') # time values
113 g=np.log(0.01)/ls
114
115 dfeq=50 #Dominant Frequency
116 a = 2.0 * (np.pi * dfeq)**2.0
117 t0 = 5.0 / (2.0 * np.pi * dfeq)
118 srclength = 5. * t0
119 ls = int(srclength/h)
120 print('source length',ls)
121 source=np.zeros(ls,'float') # source array
122 ampmax=0
123 for it in range(0,ls):
124 t = it*h
125 tt = t-t0
126 dum1 = np.exp(-a * tt * tt)
127 source[it] = -2. * a * tt * dum1
128 if (abs(source[it]) > ampmax):
129 ampmax = abs(source[it])
130 time[t]=t*h
131
132 ####################################################DOMAIN CONSTRUCTION
133 # Domain Corners
134 p0=Point(0.0, 0.0, 0.0)
135 p1=Point(0.0, depth, 0.0)
136 p2=Point(width, depth, 0.0)
137 p3=Point(width, 0.0, 0.0)
138
139 # Generate Material Boundary
140 x=[ Point(i*dsp\
141 ,dep_sp+modal*orit*h_sp*cos(pi*i*dsp/dep_sp+pi))\
142 for i in range(0,sspl)\
143 ]
144 mysp = Spline(*tuple(x))
145 # Start and end of material boundary.
146 x1=mysp.getStartPoint()
147 x2=mysp.getEndPoint()
148
149 # Create TOP BLOCK
150 # lines
151 tbl1=Line(p0,x1)
152 tbl2=mysp
153 tbl3=Line(x2,p3)
154 l30=Line(p3, p0)
155 # curve
156 tblockloop = CurveLoop(tbl1,tbl2,tbl3,l30)
157 # surface
158 tblock = PlaneSurface(tblockloop)
159 # Create BOTTOM BLOCK
160 # lines
161 bbl1=Line(x1,p1)
162 bbl3=Line(p2,x2)
163 bbl4=-mysp
164 l12=Line(p1, p2)
165 # curve
166 bblockloop = CurveLoop(bbl1,l12,bbl3,bbl4)
167
168 # surface
169 bblock = PlaneSurface(bblockloop)
170
171 #clockwise check as splines must be set as polygons in the point order
172 #they were created. Otherwise get a line across plot.
173 bblockloop2=CurveLoop(mysp,Line(x2,p2),Line(p2,p1),Line(p1,x1))
174
175 ################################################CREATE MESH FOR ESCRIPT
176 # Create a Design which can make the mesh
177 d=Design(dim=2, element_size=dx, order=2)
178 # Add the subdomains and flux boundaries.
179 d.addItems(PropertySet("top",tblock),PropertySet("bottom",bblock),\
180 PropertySet("linetop",l30))
181 # Create the geometry, mesh and Escript domain
182 d.setScriptFileName(os.path.join(save_path,"example08c.geo"))
183 d.setMeshFileName(os.path.join(save_path,"example08c.msh"))
184 domain=MakeDomain(d, optimizeLabeling=True)
185 x=domain.getX()
186 print("Domain has been generated ...")
187
188 lam=Scalar(0,Function(domain))
189 lam.setTaggedValue("top",lam1)
190 lam.setTaggedValue("bottom",lam2)
191 mu=Scalar(0,Function(domain))
192 mu.setTaggedValue("top",mu1)
193 mu.setTaggedValue("bottom",mu2)
194 rho=Scalar(0,Function(domain))
195 rho.setTaggedValue("top",rho1)
196 rho.setTaggedValue("bottom",rho2)
197
198 ##########################################################ESTABLISH PDE
199 mypde=LinearPDE(domain) # create pde
200 mypde.setSymmetryOn() # turn symmetry on
201 # turn lumping on for more efficient solving
202 #mypde.getSolverOptions().setSolverMethod(SolverOptions.LUMPING)
203 kmat = kronecker(domain) # create the kronecker delta function of the domain
204 mypde.setValue(D=rho*kmat) #set the general form value D
205
206 ##########################################################ESTABLISH ABC
207 # Define where the boundary decay will be applied.
208 bn=20.
209 bleft=xstep*bn; bright=width-(xstep*bn); bbot=depth-(ystep*bn)
210 # btop=ystep*bn # don't apply to force boundary!!!
211
212 # locate these points in the domain
213 left=x[0]-bleft; right=x[0]-bright; bottom=x[1]-bbot
214
215 tgamma=0.85 # decay value for exponential function
216 def calc_gamma(G,npts):
217 func=np.sqrt(abs(-1.*np.log(G)/(npts**2.)))
218 return func
219
220 gleft = calc_gamma(tgamma,bleft)
221 gright = calc_gamma(tgamma,bleft)
222 gbottom= calc_gamma(tgamma,ystep*bn)
223
224 print('gamma', gleft,gright,gbottom)
225
226 # calculate decay functions
227 def abc_bfunc(gamma,loc,x,G):
228 func=exp(-1.*(gamma*abs(loc-x))**2.)
229 return func
230
231 fleft=abc_bfunc(gleft,bleft,x[0],tgamma)
232 fright=abc_bfunc(gright,bright,x[0],tgamma)
233 fbottom=abc_bfunc(gbottom,bbot,x[1],tgamma)
234 # apply these functions only where relevant
235 abcleft=fleft*whereNegative(left)
236 abcright=fright*wherePositive(right)
237 abcbottom=fbottom*wherePositive(bottom)
238 # make sure the inside of the abc is value 1
239 abcleft=abcleft+whereZero(abcleft)
240 abcright=abcright+whereZero(abcright)
241 abcbottom=abcbottom+whereZero(abcbottom)
242 # multiply the conditions together to get a smooth result
243 abc=abcleft*abcright*abcbottom
244
245 ############################################FIRST TIME STEPS AND SOURCE
246 # define small radius around point xc
247 src_length = 40; print("src_length = ",src_length)
248 # set initial values for first two time steps with source terms
249 xb=FunctionOnBoundary(domain).getX()
250 y=source[0]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(xb-src_length))
251 src_dir=numpy.array([0.,1.]) # defines direction of point source as down
252 y=y*src_dir
253 mypde.setValue(y=y) #set the source as a function on the boundary
254 # initial value of displacement at point source is constant (U0=0.01)
255 # for first two time steps
256 u=[0.0,0.0]*wherePositive(x)
257 u_m1=u
258
259 ####################################################ITERATION VARIABLES
260 n=0 # iteration counter
261 t=0 # time counter
262 ##############################################################ITERATION
263 while t<tend:
264 # get current stress
265 g=grad(u); stress=lam*trace(g)*kmat+mu*(g+transpose(g))
266 mypde.setValue(X=-stress*abc) # set PDE values
267 accel = mypde.getSolution() #get PDE solution for accelleration
268 u_p1=(2.*u-u_m1)+h*h*accel #calculate displacement
269 u_p1=u_p1*abc # apply boundary conditions
270 u_m1=u; u=u_p1 # shift values by 1
271 # save current displacement, acceleration and pressure
272 if (t >= rtime):
273 saveVTK(os.path.join(save_path,"ex08c.%05d.vtu"%n),\
274 vector_displacement=u,displacement=length(u),\
275 vector_acceleration=accel,acceleration=length(accel),\
276 tensor=stress)
277 rtime=rtime+rtime_inc #increment data save time
278 # increment loop values
279 t=t+h; n=n+1
280 if (n < ls):
281 y=source[n]*(cos(length(x-xc)*3.1415/src_length)+1)*whereNegative(length(x-xc)-src_length)
282 y=y*src_dir; mypde.setValue(y=y) #set the source as a function on the boundary
283 print("time step %d, t=%s"%(n,t))

  ViewVC Help
Powered by ViewVC 1.1.26