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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26