/[escript]/trunk/pyvisi/test/python/run_escript_with_lazy_evaluation.py
ViewVC logotype

Contents of /trunk/pyvisi/test/python/run_escript_with_lazy_evaluation.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1159 - (show annotations)
Tue May 22 05:09:31 2007 UTC (12 years, 1 month ago) by jongui
File MIME type: text/x-python
File size: 7762 byte(s)
Some update to test_escript.
1 # Import the necessary modules.
2 from esys.escript import *
3 from esys.escript.pdetools import Locator
4 from esys.escript.linearPDEs import LinearPDE
5 from esys.finley import Rectangle, Brick
6 from numarray import identity,zeros,ones
7 from esys.pyvisi import Scene, DataCollector, Map, Velocity, Ellipsoid, Camera
8 from esys.pyvisi.constant import *
9 import unittest, os
10 from stat import ST_SIZE
11
12 try:
13 PYVISI_WORKDIR=os.environ['PYVISI_WORKDIR']
14 except KeyError:
15 PYVISI_WORKDIR='.'
16 try:
17 PYVISI_TEST_DATA_ROOT=os.environ['PYVISI_TEST_DATA_ROOT']
18 except KeyError:
19 PYVISI_TEST_DATA_ROOT='.'
20
21 PYVISI_TEST_ESCRIPT_REFERENCE_IMAGES_PATH = os.path.join(PYVISI_TEST_DATA_ROOT,\
22 "data_reference_images", "escript")
23 PYVISI_TEST_ESCRIPT_IMAGES_PATH = os.path.join(PYVISI_WORKDIR, \
24 "data_sample_images", "escript")
25
26 MIN_IMAGE_SIZE = 100
27 X_SIZE = 400
28 Y_SIZE = 400
29 JPG_RENDERER = Renderer.OFFLINE_JPG
30
31 class TestEscript:
32 def render(self, file):
33 self.scene.render(image_name = \
34 os.path.join(PYVISI_TEST_ESCRIPT_IMAGES_PATH, file))
35
36 self.failUnless(os.stat(os.path.join(PYVISI_TEST_ESCRIPT_IMAGES_PATH, \
37 file))[ST_SIZE] > MIN_IMAGE_SIZE)
38
39 class TestEscriptMap(unittest.TestCase, TestEscript):
40 def tearDown(self):
41 del self.scene
42
43 def testEscriptMap(self):
44 #... set some parameters ...
45 xc=[0.02,0.002]
46 r=0.001
47 qc=50.e6
48 Tref=0.
49 rhocp=2.6e6
50 eta=75.
51 kappa=240.
52 tend=5.
53 # ... time, time step size and counter ...
54 t=0
55 h=0.1
56 i=0
57
58 #... generate domain ...
59 mydomain = Rectangle(l0=0.05,l1=0.01,n0=250, n1=50)
60 #... open PDE ...
61 mypde=LinearPDE(mydomain)
62 mypde.setSymmetryOn()
63 mypde.setValue(A=kappa*kronecker(mydomain),D=rhocp/h,d=eta,y=eta*Tref)
64 # ... set heat source: ....
65 x=mydomain.getX()
66 qH=qc*whereNegative(length(x-xc)-r)
67 # ... set initial temperature ....
68 T=Tref
69
70 # Create a Scene.
71 s = Scene(renderer = JPG_RENDERER, x_size = X_SIZE, y_size = Y_SIZE)
72 self.scene = s
73
74 # Create a DataCollector reading directly from escript objects.
75 dc = DataCollector(source = Source.ESCRIPT)
76
77 # Create a Map.
78 m = Map(scene = s, data_collector = dc,
79 viewport = Viewport.SOUTH_WEST, lut = Lut.COLOR,
80 cell_to_point = False, outline = True)
81
82 # Create a Camera.
83 c = Camera(scene = s, viewport = Viewport.SOUTH_WEST)
84
85 # ... start iteration:
86 while t<0.5:
87 i+=1
88 t+=h
89 mypde.setValue(Y=qH+rhocp/h*T)
90 T=mypde.getSolution()
91
92 dc.setData(temp = T)
93
94 # Render the object.
95 self.render("TestEscriptMap%02d.jpg" % i)
96
97 class TestEscriptVelocity(unittest.TestCase, TestEscript):
98 def tearDown(self):
99 del self.scene
100
101 def testEscriptVelocity(self):
102 #... set some parameters ...
103 lam=1.
104 mu=0.1
105 alpha=1.e-6
106 xc=[0.3,0.3,1.]
107 beta=8.
108 T_ref=0.
109 T_0=1.
110 #... generate domain ...
111 mydomain = Brick(l0=1.,l1=1., l2=1.,n0=10, n1=10, n2=10)
112 x=mydomain.getX()
113 #... set temperature ...
114 T=T_0*exp(-beta*length(x-xc))
115 #... open symmetric PDE ...
116 mypde=LinearPDE(mydomain)
117 mypde.setSymmetryOn()
118 #... set coefficients ...
119 C=Tensor4(0.,Function(mydomain))
120 for i in range(mydomain.getDim()):
121 for j in range(mydomain.getDim()):
122 C[i,i,j,j]+=lam
123 C[j,i,j,i]+=mu
124 C[j,i,i,j]+=mu
125 msk=whereZero(x[0])*[1.,0.,0.] \
126 +whereZero(x[1])*[0.,1.,0.] \
127 +whereZero(x[2])*[0.,0.,1.]
128 sigma0=(lam+2./3.*mu)*alpha*(T-T_ref)*kronecker(mydomain)
129 mypde.setValue(A=C,X=sigma0,q=msk)
130 #... solve pde ...
131 u=mypde.getSolution()
132 #==============
133 # ... calculate von-Misses
134 u=mydomain.getX()
135 g=grad(u)
136 sigma=mu*(g+transpose(g))+lam*trace(g)*kronecker(mydomain)-sigma0
137 sigma_mises=\
138 sqrt(((sigma[0,0]-sigma[1,1])**2+(sigma[1,1]-sigma[2,2])**2+ \
139 (sigma[2,2]-sigma[0,0])**2)/6. \
140 +sigma[0,1]**2 + sigma[1,2]**2 + sigma[2,0]**2)
141
142 # Create a Scene.
143 s = Scene(renderer = JPG_RENDERER, x_size = X_SIZE, y_size = Y_SIZE)
144 self.scene = s
145
146 # Create a DataCollector reading directly from an escript object.
147 dc = DataCollector(source = Source.ESCRIPT)
148 T=length(mydomain.getX())
149 dc.setData(disp = u, stress = sigma_mises)
150
151 # Create a Velocity.
152 v = Velocity(scene = s, data_collector = dc,
153 viewport = Viewport.SOUTH_WEST,
154 arrow = Arrow.THREE_D, color_mode = ColorMode.SCALAR,
155 lut = Lut.COLOR, cell_to_point = True, outline = True)
156 v.setScaleFactor(scale_factor = 0.3)
157
158 # Create a Camera.
159 c = Camera(scene = s, viewport = Viewport.SOUTH_WEST)
160 c.isometricView()
161
162 # Render the object.
163 self.render("TestEscriptVelocity.jpg")
164
165 ne=32 # number of cells in x_0 and x_1 directions
166 width=10000. # length in x_0 and x_1 directions
167 lam=3.462e9
168 mu=3.462e9
169 rho=1154.
170 tend=60
171 h=(1./5.)*sqrt(rho/(lam+2*mu))*(width/ne)
172
173 U0=0.01 # amplitude of point source
174 class TestEscriptEllipsoid(unittest.TestCase, TestEscript):
175 def tearDown(self):
176 del self.scene
177
178 def testEscriptEllipsoid(self):
179 mydomain=Brick(ne,ne,10,l0=width,l1=width,l2=10.*width/32.)
180 self.wavePropagation(mydomain,h,tend,lam,mu,rho,U0)
181
182 def wavePropagation(self,domain,h,tend,lam,mu,rho,U0):
183 x=domain.getX()
184 # ... open new PDE ...
185 mypde=LinearPDE(domain)
186 mypde.setSolverMethod(LinearPDE.LUMPING)
187 kronecker=identity(mypde.getDim())
188
189 # spherical source at middle of bottom face
190
191 xc=[width/2.,width/2.,0.]
192 # define small radius around point xc
193 # Lsup(x) returns the maximum value of the argument x
194 src_radius = 0.1*Lsup(domain.getSize())
195 dunit=numarray.array([1.,0.,0.]) # defines direction of point source
196
197 mypde.setValue(D=kronecker*rho)
198 # ... set initial values ....
199 n=0
200 # initial value of displacement at point source is constant
201 # (U0=0.01) for first two time steps
202 u=U0*whereNegative(length(x-xc)-src_radius)*dunit
203 u_last=U0*whereNegative(length(x-xc)-src_radius)*dunit
204 t=0
205
206 # define the location of the point source
207 L=Locator(domain,numarray.array(xc))
208 # find potential at point source
209 u_pc=L.getValue(u)
210
211 u_pc_x = u_pc[0]
212 u_pc_y = u_pc[1]
213 u_pc_z = u_pc[2]
214
215 # open file to save displacement at point source
216 #u_pc_data=open('./data/U_pc.out','w')
217 #u_pc_data.write("%f %f %f %f\n"%(t,u_pc_x,u_pc_y,u_pc_z))
218
219 # Create a Scene.
220 s = Scene(renderer = JPG_RENDERER, x_size = X_SIZE, y_size = Y_SIZE)
221 self.scene = s
222
223 # Create a DataCollector reading directly from escript objects.
224 dc = DataCollector(source = Source.ESCRIPT)
225
226 # Create an Ellipsoid.
227 e = Ellipsoid(scene = s, data_collector = dc,
228 viewport = Viewport.SOUTH_WEST,
229 lut = Lut.COLOR, cell_to_point = True, outline = True)
230 e.setScaleFactor(scale_factor = 0.7)
231 e.setMaxScaleFactor(max_scale_factor = 1000)
232 e.setRatio(ratio = 10)
233
234 # Create a Camera.
235 c = Camera(scene = s, viewport = Viewport.SOUTH_WEST)
236 c.isometricView()
237
238 while t<0.4:
239 # ... get current stress ....
240 g=grad(u)
241 stress=lam*trace(g)*kronecker+mu*(g+transpose(g))
242 # ... get new acceleration ....
243 mypde.setValue(X=-stress)
244 a=mypde.getSolution()
245 # ... get new displacement ...
246 u_new=2*u-u_last+h**2*a
247 # ... shift displacements ....
248 u_last=u
249 u=u_new
250 t+=h
251 n+=1
252 u_pc=L.getValue(u)
253
254 u_pc_x=u_pc[0]
255 u_pc_y=u_pc[1]
256 u_pc_z=u_pc[2]
257
258 # ... save current acceleration in units of gravity and displacements
259 # === syntetic data
260 u=domain.getX()
261 if n==1 or n%10==0:
262 dc.setData(acceleration = length(a)/9.81, displacement = u,
263 tensor = stress, Ux = u[0])
264
265 # Render the object.
266 self.render("TestEscriptEllipsoid%02d.jpg" % (t/10))
267
268
269 if __name__ == '__main__':
270 suite = unittest.TestSuite()
271 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestEscriptMap))
272 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestEscriptVelocity))
273 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestEscriptEllipsoid))
274 unittest.TextTestRunner(verbosity=2).run(suite)
275

  ViewVC Help
Powered by ViewVC 1.1.26