/[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 6756 - (show annotations)
Thu Nov 29 07:23:43 2018 UTC (22 months ago) by aellery
File MIME type: text/x-python
File size: 2671 byte(s)
Temporarily undoing last commit.
1 from __future__ import division
2 ##############################################################################
3 #
4 # Copyright (c) 2003-2018 by The University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Apache License, version 2.0
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 __copyright__="""Copyright (c) 2003-2018 by The University of Queensland
18 http://www.uq.edu.au
19 Primary Business: Queensland, Australia"""
20 __license__="""Licensed under the Apache License, version 2.0
21 http://www.apache.org/licenses/LICENSE-2.0"""
22 __url__="https://launchpad.net/escript-finley"
23
24
25
26 # This example demonstrates both interpolation and saving data in CSV format
27
28 from esys.escript import saveDataCSV, sup, interpolateTable
29 import numpy
30 try:
31 from esys.finley import Rectangle
32 HAVE_FINLEY = True
33 except ImportError:
34 HAVE_FINLEY = False
35
36 if not HAVE_FINLEY:
37 print("Finley module not available")
38 else:
39
40 n=4 #Change this to whatever you like
41 r=Rectangle(n,n)
42 x=r.getX()
43 x0=x[0]
44 x1=x[1] #we'll use this later
45 toobig=100 #An exception will be thrown if interpolation produces a value larger than this
46
47 #First one dimensional interpolation
48
49 #In this example we will interpolate a sine curve
50 #The values we take from the domain will range from 0 to 1 (inclusive)
51
52 sine_table=[0, 0.70710678118654746, 1, 0.70710678118654746, 0, -0.70710678118654746, -1, -0.70710678118654746, 0]
53
54 numslices=len(sine_table)-1
55
56 minval=0
57 maxval=1
58
59 step=sup(maxval-minval)/numslices #The width of the gap between entries in the table
60
61 result=interpolateTable(sine_table, x0, minval, step, toobig)
62
63 #Now we save the input and output for comparison
64
65 saveDataCSV("1d.csv", inp=x0, out=result)
66
67 #Now 2D interpolation
68
69 #This time the sine curve will be at full height along the x (ie x0) axis.
70 #Its amplitude will decrease to a flat line along x1=1.1
71
72 #Interpolate works with numpy arrays so we'll use them
73 st=numpy.array(sine_table)
74
75 table=[st, 0.5*st, 0*st ] #Note that this table is 2D
76
77 #The y dimension should be the outer the dimension of the table
78 #Note that the order of tuples for the 2nd and 3rd param is (x,y)
79 result2=interpolateTable(table, x, (minval,0), (0.55, step), toobig)
80 saveDataCSV("2d.csv",inp0=x0, inp2=x1, out=result2)

  ViewVC Help
Powered by ViewVC 1.1.26