/[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 3892 - (show annotations)
Tue Apr 10 08:57:23 2012 UTC (6 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 5527 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



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

  ViewVC Help
Powered by ViewVC 1.1.26