/[escript]/trunk/doc/examples/usersguide/int_save.py
ViewVC logotype

Contents of /trunk/doc/examples/usersguide/int_save.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2646 - (show annotations)
Fri Sep 4 00:13:00 2009 UTC (10 years, 10 months ago) by jfenwick
File MIME type: text/x-python
File size: 2553 byte(s)
Unit tests and user doco for interpolation and saveCSV
1
2 ########################################################
3 #
4 # Copyright (c) 2003-2009 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) 2003-2009 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
23
24 # This example demonstrates both interpolation and saving data in CSV format
25
26 from esys.escript import saveDataCSV, sup
27 import numpy
28 from esys.finley import Rectangle
29
30 n=4 #Change this to whatever you like
31 r=Rectangle(n,n)
32 x=r.getX()
33 x0=x[0]
34 x1=x[1] #we'll use this later
35 toobig=100 #An exception will be thrown if interpolation produces a value larger than this
36
37 #First one dimensional interpolation
38
39 #In this example we will interpolate a sine curve
40 #The values we take from the domain will range from 0 to 1 (inclusive)
41 #Because we will actually see the maximum value in the input we need to add
42 #an extra entry to the table (so we have 1.125 sine cycles)
43
44 sine_table=[0, 0.70710678118654746, 1, 0.70710678118654746, 0, -0.70710678118654746, -1, -0.70710678118654746, 0, 0.70710678118654746]
45
46 numslices=len(sine_table)-1
47
48 minval=0
49 maxval=1.125 # The extra 0.25 is to account for the extra entry in the table
50
51 step=sup(maxval-minval)/numslices #The width of the gap between entries in the table
52
53 result=x0.interpolateTable(sine_table, minval, step, toobig)
54
55 #Now we save the input and output for comparison
56
57 saveDataCSV("1d.csv", inp=x0, out=result)
58
59 #Now 2D interpolation
60
61 #This time the sine curve will be at full height along the x (ie x0) axis.
62 #Its amplitude will decrease to a flat line along x1=1.1
63 #Since 1.1 is larger than any value in the input we don't need to add an extra row
64 #to our table
65
66 #Interpolate works with numpy arrays so we'll use them
67 #st=numpy.array(sine_table)
68
69 #table=[st, 0.5*st, 0*st ] #Note that this table is 2D
70
71 ##note that we call the interpolate table method on the object
72 ##which corresponds to the outer dimension of the table
73 #result2=x1.interpolateTable(table, 0, 0.55, x0, minval, step, 500)
74 #saveDataCSV("2d.csv",inp0=x0, inp2=x1, out=result2)

  ViewVC Help
Powered by ViewVC 1.1.26