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

  ViewVC Help
Powered by ViewVC 1.1.26