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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4087 - (hide annotations)
Thu Nov 22 22:28:01 2012 UTC (6 years, 8 months ago) by caltinay
File MIME type: text/x-python
File size: 5635 byte(s)
Moved matplotlib imports in test scripts before escript since there is an
import chain which pulls it so the use() function has no effect.

1 ahallam 3003
2 jfenwick 3893 from __future__ import print_function
3 jfenwick 3981 ##############################################################################
4 ahallam 3003 #
5 jfenwick 3911 # Copyright (c) 2009-2012 by University of Queensland
6 jfenwick 3981 # http://www.uq.edu.au
7 ahallam 3003 #
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 jfenwick 3981 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
13     # Development since 2012 by School of Earth Sciences
14     #
15     ##############################################################################
16 ahallam 3003
17 jfenwick 3911 __copyright__="""Copyright (c) 2009-2012 by University of Queensland
18 jfenwick 3981 http://www.uq.edu.au
19 ahallam 3003 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 ahallam 3025 ############################################################FILE HEADER
25     # example07b.py
26 ahallam 3003 # Antony Hallam
27     # Acoustic Wave Equation Simulation using acceleration solution
28     # and lumping.
29    
30 ahallam 3025 #######################################################EXTERNAL MODULES
31 caltinay 4087 import matplotlib
32     matplotlib.use('agg') #It's just here for automated testing
33 ahallam 3003 from esys.escript import *
34     from esys.finley import Rectangle
35 caltinay 3346 from esys.weipa import saveVTK
36 ahallam 3003 import sys
37     import os
38     # smoothing operator
39 ahallam 3025 from esys.escript.pdetools import Projector, Locator
40     from esys.escript.unitsSI import *
41 ahallam 3003 import numpy as np
42     import pylab as pl
43     import matplotlib.cm as cm
44     from esys.escript.linearPDEs import LinearPDE
45    
46 ahallam 3025 ########################################################MPI WORLD CHECK
47     if getMPISizeWorld() > 1:
48     import sys
49 jfenwick 3892 print("This example will not run in an MPI world.")
50 ahallam 3025 sys.exit(0)
51 ahallam 3003
52 ahallam 3025 #################################################ESTABLISHING VARIABLES
53     # where to save output data
54 ahallam 3029 savepath = "data/example07b"
55 ahallam 3025 mkDir(savepath) #make sure savepath exists
56 ahallam 3003 #Geometric and material property related variables.
57     mx = 1000. # model lenght
58     my = 1000. # model width
59 ahallam 3385 ndx = 500 # steps in x direction
60     ndy = 500 # steps in y direction
61 ahallam 3025 xstep=mx/ndx # calculate the size of delta x
62     ystep=my/ndy # calculate the size of delta y
63 ahallam 3003
64 ahallam 3025 c=380.0*m/sec # velocity of sound in air
65     csq=c*c #square of c
66 ahallam 3003 # Time related variables.
67 ahallam 3195 testing=True
68     if testing:
69 jfenwick 3892 print('The testing end time is currently selected. This severely limits the number of time iterations.')
70     print("Try changing testing to False for more iterations.")
71 ahallam 3195 tend=0.004
72     else:
73 ahallam 3385 tend=1.0 # end time
74 ahallam 3195
75 ahallam 3385 h=0.0005 # time step
76 ahallam 3025 # data recording times
77     rtime=0.0 # first time to record
78     rtime_inc=tend/20.0 # time increment to record
79 ahallam 3003 #Check to make sure number of time steps is not too large.
80 jfenwick 3892 print("Time step size= ",h, "Expected number of outputs= ",tend/h)
81 ahallam 3003
82 ahallam 3025 U0=0.005 # amplitude of point source
83     # want a spherical source in the middle of area
84     xc=[500,500] # with reference to mx,my this is the source location
85 ahallam 3003
86 ahallam 3025 ####################################################DOMAIN CONSTRUCTION
87     mydomain=Rectangle(l0=mx,l1=my,n0=ndx, n1=ndy) #create the domain
88     x=mydomain.getX() #get the node locations of the domain
89 ahallam 3003
90 ahallam 3025 ##########################################################ESTABLISH PDE
91     mypde=LinearPDE(mydomain) # create pde
92     # turn lumping on for more efficient solving
93 ahallam 3385 mypde.getSolverOptions().setSolverMethod(mypde.getSolverOptions().HRZ_LUMPING)
94 ahallam 3025 mypde.setSymmetryOn() # turn symmetry on
95     mypde.setValue(D=1.) # set the value of D in the general form to 1.
96 ahallam 3003
97 ahallam 3025 ############################################FIRST TIME STEPS AND SOURCE
98 ahallam 3003 # define small radius around point xc
99 ahallam 3025 src_radius = 25.
100 jfenwick 3892 print("src_radius = ",src_radius)
101 ahallam 3025 # set initial values for first two time steps with source terms
102 ahallam 3003 u=U0*(cos(length(x-xc)*3.1415/src_radius)+1)*whereNegative(length(x-xc)-src_radius)
103     u_m1=u
104     #plot source shape
105 ahallam 3025 cut_loc=[] #where the cross section of the source along x will be
106     src_cut=[] #where the cross section of the source will be
107     # create locations for source cross section
108 jfenwick 3893 for i in range(ndx//2-ndx//10,ndx//2+ndx//10):
109 ahallam 3025 cut_loc.append(xstep*i)
110     src_cut.append([xstep*i,xc[1]])
111     # locate the nearest nodes to the points in src_cut
112     src=Locator(mydomain,src_cut)
113     src_cut=src.getValue(u) #retrieve the values from the nodes
114     # plot the x locations vs value and save the figure
115     pl.plot(cut_loc,src_cut)
116     pl.axis([xc[0]-src_radius*3,xc[0]+src_radius*3,0.,2.*U0])
117 ahallam 3003 pl.savefig(os.path.join(savepath,"source_line.png"))
118    
119 ahallam 3025 ###########################SAVING THE VALUE AT A LOC FOR EACH TIME STEP
120     u_rec0=[] # array to hold values
121     rec=Locator(mydomain,[250.,250.]) #location to record
122     u_rec=rec.getValue(u); u_rec0.append(u_rec) #get the first two time steps
123    
124     ####################################################ITERATION VARIABLES
125     n=0 # iteration counter
126     t=0 # time counter
127     ##############################################################ITERATION
128 ahallam 3003 while t<tend:
129 ahallam 3025 g=grad(u); pres=csq*g # get current pressure
130     mypde.setValue(X=-pres) # set values in pde
131     accel = mypde.getSolution() # get new acceleration
132     u_p1=(2.*u-u_m1)+h*h*accel # calculate the displacement for the next time step
133     u_m1=u; u=u_p1 # shift values back one time step for next iteration
134     # save current displacement, acceleration and pressure
135 ahallam 3004 if (t >= rtime):
136 ahallam 3025 saveVTK(os.path.join(savepath,"ex07b.%i.vtu"%n),displacement=length(u),\
137     acceleration=length(accel),tensor=pres)
138     rtime=rtime+rtime_inc #increment data save time
139     u_rec0.append(rec.getValue(u)) #location specific recording
140     # increment loop values
141     t=t+h; n=n+1
142 caltinay 4005 print("time step %d, t=%s"%(n,t))
143 ahallam 3003
144 ahallam 3025 # save location specific recording to file
145     pl.savetxt(os.path.join(savepath,'u_rec.asc'),u_rec0)

  ViewVC Help
Powered by ViewVC 1.1.26