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

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

Parent Directory Parent Directory | Revision Log Revision Log


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