1 |
|
2 |
######################################################## |
3 |
# |
4 |
# Copyright (c) 2003-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) 2003-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 |
|
23 |
|
24 |
# This example demonstrates both interpolation and saving data in CSV format |
25 |
|
26 |
from esys.escript import saveDataCSV, sup, interpolateTable |
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 |
|
42 |
sine_table=[0, 0.70710678118654746, 1, 0.70710678118654746, 0, -0.70710678118654746, -1, -0.70710678118654746, 0] |
43 |
|
44 |
numslices=len(sine_table)-1 |
45 |
|
46 |
minval=0 |
47 |
maxval=1 |
48 |
|
49 |
step=sup(maxval-minval)/numslices #The width of the gap between entries in the table |
50 |
|
51 |
result=interpolateTable(sine_table, x0, minval, step, toobig) |
52 |
|
53 |
#Now we save the input and output for comparison |
54 |
|
55 |
saveDataCSV("1d.csv", inp=x0, out=result) |
56 |
|
57 |
#Now 2D interpolation |
58 |
|
59 |
#This time the sine curve will be at full height along the x (ie x0) axis. |
60 |
#Its amplitude will decrease to a flat line along x1=1.1 |
61 |
|
62 |
#Interpolate works with numpy arrays so we'll use them |
63 |
st=numpy.array(sine_table) |
64 |
|
65 |
table=[st, 0.5*st, 0*st ] #Note that this table is 2D |
66 |
|
67 |
#The y dimension should be the outer the dimension of the table |
68 |
result2=interpolateTable(table, x, (minval,0), (0.55, step), toobig) |
69 |
saveDataCSV("2d.csv",inp0=x0, inp2=x1, out=result2) |