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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5288 - (show annotations)
Tue Dec 2 23:18:40 2014 UTC (4 years, 9 months ago) by sshaw
File MIME type: text/x-python
File size: 6287 byte(s)
fixing tests for cases where required domains not built
1 from __future__ import division, print_function
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 ############################################################FILE HEADER
25 # example07b.py
26 # Antony Hallam
27 # Acoustic Wave Equation Simulation using acceleration solution
28 # and lumping.
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 sys
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 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
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/example07b"
61 mkDir(savepath) #make sure savepath exists
62 #Geometric and material property related variables.
63 mx = 1000. # model lenght
64 my = 1000. # model width
65 ndx = 500 # steps in x direction
66 ndy = 500 # steps in y direction
67 xstep=mx/ndx # calculate the size of delta x
68 ystep=my/ndy # calculate the size of delta y
69
70 c=380.0*m/sec # velocity of sound in air
71 csq=c*c #square of c
72 # Time related variables.
73 testing=True
74 if testing:
75 print('The testing end time is currently selected. This severely limits the number of time iterations.')
76 print("Try changing testing to False for more iterations.")
77 tend=0.004
78 else:
79 tend=1.0 # end time
80
81 h=0.0005 # time step
82 # data recording times
83 rtime=0.0 # first time to record
84 rtime_inc=tend/20.0 # time increment to record
85 #Check to make sure number of time steps is not too large.
86 print("Time step size= ",h, "Expected number of outputs= ",tend/h)
87
88 U0=0.005 # amplitude of point source
89 # want a spherical source in the middle of area
90 xc=[500,500] # with reference to mx,my this is the source location
91
92 ####################################################DOMAIN CONSTRUCTION
93 mydomain=Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy) #create the domain
94 x=mydomain.getX() #get the node locations of the domain
95
96 ##########################################################ESTABLISH PDE
97 mypde=LinearPDE(mydomain) # create pde
98 # turn lumping on for more efficient solving
99 mypde.getSolverOptions().setSolverMethod(SolverOptions.HRZ_LUMPING)
100 mypde.setSymmetryOn() # turn symmetry on
101 mypde.setValue(D=1.) # set the value of D in the general form to 1.
102
103 ############################################FIRST TIME STEPS AND SOURCE
104 # define small radius around point xc
105 src_radius = 25.
106 print("src_radius = ",src_radius)
107 # set initial values for first two time steps with source terms
108 u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*whereNegative(length(x-xc)-src_radius)
109 u_m1=u
110 #plot source shape
111 cut_loc=[] #where the cross section of the source along x will be
112 src_cut=[] #where the cross section of the source will be
113 # create locations for source cross section
114 for i in range(ndx//2-ndx//10,ndx//2+ndx//10):
115 cut_loc.append(xstep*i)
116 src_cut.append([xstep*i,xc[1]])
117 # locate the nearest nodes to the points in src_cut
118 src=Locator(mydomain,src_cut)
119 src_cut=src.getValue(u) #retrieve the values from the nodes
120 # plot the x locations vs value and save the figure
121 pl.plot(cut_loc,src_cut)
122 pl.axis([xc[0]-src_radius*3,xc[0]+src_radius*3,0.,2.*U0])
123 pl.savefig(os.path.join(savepath,"source_line.png"))
124
125 ###########################SAVING THE VALUE AT A LOC FOR EACH TIME STEP
126 u_rec0=[] # array to hold values
127 rec=Locator(mydomain,[250.,250.]) #location to record
128 u_rec=rec.getValue(u); u_rec0.append(u_rec) #get the first two time steps
129
130 ####################################################ITERATION VARIABLES
131 n=0 # iteration counter
132 t=0 # time counter
133 ##############################################################ITERATION
134 while t<tend:
135 g=grad(u); pres=csq*g # get current pressure
136 mypde.setValue(X=-pres) # set values in pde
137 accel = mypde.getSolution() # get new acceleration
138 u_p1=(2.*u-u_m1)+h*h*accel # calculate the displacement for the next time step
139 u_m1=u; u=u_p1 # shift values back one time step for next iteration
140 # save current displacement, acceleration and pressure
141 if (t >= rtime):
142 saveVTK(os.path.join(savepath,"ex07b.%i.vtu"%n),displacement=length(u),\
143 acceleration=length(accel),tensor=pres)
144 rtime=rtime+rtime_inc #increment data save time
145 u_rec0.append(rec.getValue(u)) #location specific recording
146 # increment loop values
147 t=t+h; n=n+1
148 print("time step %d, t=%s"%(n,t))
149
150 # save location specific recording to file
151 pl.savetxt(os.path.join(savepath,'u_rec.asc'),u_rec0)

  ViewVC Help
Powered by ViewVC 1.1.26