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

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

  ViewVC Help
Powered by ViewVC 1.1.26