1 | # ---------------------------------------------------------------------- |
---|
2 | # GRAPH |
---|
3 | # |
---|
4 | # This simple example shows how you can use the Rappture toolkit |
---|
5 | # to handle I/O for a simple simulator--in this case, one that |
---|
6 | # evaluates an x/y graph |
---|
7 | # |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: Martin Hunt, Purdue University |
---|
10 | # Copyright (c) 2015 HUBzero Foundation, LLC |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and |
---|
13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # ====================================================================== |
---|
15 | |
---|
16 | # Note: You will not see stdout and stderr when this |
---|
17 | # tool is run by Rappture. You can either run this tool from the command |
---|
18 | # line by passing in a driver xml file like this: |
---|
19 | # ~/rap/rappture/examples/graph> python graph.py driver1234.xml |
---|
20 | # |
---|
21 | # or you can redirect stdout and stderr to files by uncommenting the |
---|
22 | # two lines after the import sys |
---|
23 | |
---|
24 | import Rappture |
---|
25 | import numpy as np |
---|
26 | import sys |
---|
27 | |
---|
28 | # uncomment these for debugging |
---|
29 | # sys.stderr = open('graph.err', 'w') |
---|
30 | # sys.stdout = open('graph.out', 'w') |
---|
31 | |
---|
32 | io = Rappture.PyXml(sys.argv[1]) |
---|
33 | |
---|
34 | # When reading from xml, all values are strings |
---|
35 | xmin = float(io['input.number(min).current'].value) |
---|
36 | xmax = float(io['input.number(max).current'].value) |
---|
37 | formula = io['input.string(formula).current'].value |
---|
38 | print 'formula = %s' % formula |
---|
39 | |
---|
40 | curve = io['output.curve(result)'] |
---|
41 | curve['about.label'] = 'Formula: Y vs X' |
---|
42 | curve['yaxis.label'] = 'Y' |
---|
43 | curve['xaxis.label'] = 'X' |
---|
44 | |
---|
45 | num_points = 100 |
---|
46 | x = np.linspace(xmin, xmax, num_points) |
---|
47 | y = eval(formula) |
---|
48 | curve['component.xy'] = (x, y) |
---|
49 | |
---|
50 | # Done. Write out the xml file for Rappture. |
---|
51 | io.close() |
---|