inputoutput.doxy

Go to the documentation of this file.
1 
40 bool subdivide false @endcode
41 
42 The results from the program are stored in a similar structure. At the end of a program, it is possible to write the input and output data into one file. The output file has the same syntax and structure as the input file and can therefore be used as an input file to reproduce the results at a later time.
43 See the <a href="#Results">results section</a> for an example.
44 
45 @dontinclude inputoutput.py
46 @section commented Commented Program
47 
48 First we have to import certain modules from the \c libconceptspy package and other required python modules.
49 
50  @skip from libconceptspy import concepts
51  @until import argparse
52 
53 All code in this example is in one large routine, the main program.
54 
55  @skip def main(args,argv):
56  @until def main(args,argv):
57 
58 Some default values for the parameters used. \c l and \c p are just two variables. If \c debug is set to true, more information is printed to screen.
59 
60  @skip l = 0
61  @until debug = False
62 
63 First, set up the input parameter class: some parameters are set up with a default value.
64 
65  @skip inp = concepts.InputParser(True)
66  @until inputParameters.addString('parameterout',"inputoutput.out")
67 
68 The variable outputParameter is for easier access to the output area. There, the results of the computations can be stored and eventually written to disk if necessary.
69 
70  @skip outputParameters = concepts.InOutParameters()
71  @until outputParameters = concepts.InOutParameters()
72 
73 Prepare an array for values computed later. This array is then added to \c table which is able to nicely format the content of the different arrays (e.g. for later processing with Gnuplot).
74 
75  @skip outputParameters.addArrayDouble("error")
76  @until inputfile = ''
77 
78 @subsection parsing Command Line Parsing
79 Here, we start with the command line parsing.
80  @code
81  parser = argparse.ArgumentParser()
82  parser.add_argument('-l',help='LEVEL: level of refinement',required=False, type = int)
83  parser.add_argument('-p',help='DEGREE: polynomial degree', required=False, type = int)
84  parser.add_argument('-f',help='FILE: name of the input file',required=False, type = str)
85  parser.add_argument('-d',action = "store_true",help='-d: print the matrices',required=False)
86  args = parser.parse_args()
87  @endcode
88 
89 Enter \c -h as command line argument for more information. The parameters are processed in the order they appear in the command line. When first specifying an input file with \c -f, the values in the file can be overridden with additional command line arguments after \c -f.
90 
91  @skip if (len(argv)>1):
92  @until inputParameters.addBool('debug', True)
93 
94 If there is an error reading the input file, then an exception error message is returned.
95 
96  @skip except RuntimeError:
97  @until print("Input file not found.Cannot open input file.")
98 
99 Print the parameters to show the user what is going on.
100 
101  @skip print('['+argv[0]+']')
102  @until print(' input file = %s\n%s'%(inputfile,inputParameters))
103 
104 Next, the parameters from the command line or the input file are stored in the respective variables. This is only used for abbrevation.
105 
106  @skip l = inputParameters.getInt('level')
107  @until p = inputParameters.getInt('polynomial')
108 
109 @subsection comp Computations
110 Here are some dummy computations to fill the output area with content.
111 
112  @skip outputParameters.addInt("nelm", 10)
113  @until outputParameters.addArrayDouble("error",i,1.0/(1<<i))
114 
115 @section output Output
116 Finally, the input and output data are written to disk with some more information about the user and the system in the header of the file.
117 
118  @skip print 'Writing gathered data to disk: ',inputParameters.getString("parameterout")
119  @until ofile.close()
120 
121 This prints the table and its content to the screen and also stores it with high precision in a file suitable for later processing with Gnuplot.
122 
123  @skip print(table)
124  @until gnu_file.close()
125 
126 @section Results
127 The output of the program called without parameters: @code
128 [inputoutput]
129 --
130 Parameters:
131  input file =
132 string author "(empty)"
133 string comment "(empty)"
134 string parameterout "inputoutput.out"
135 string title "(empty)"
136 int level 0
137 int polynomial 1
138 bool debug false
139 --
140 --
141 Writing gathered data to disk: inputoutput.out
142 ResultsTable(
143 error error
144 0 1
145 1 0.5
146 2 0.25
147 3 0.125
148 4 0.0625
149 5 0.03125
150 6 0.015625
151 7 0.0078125
152 8 0.00390625
153 9 0.00195312
154 ) @endcode
155 The program creates the following output files:
156  - \c inputoutput.out:
157 @code
158 /* program: inputoutput
159  * command: inputoutput
160  * input file:
161  */
162 string author "(empty)"
163 string comment "(empty)"
164 string parameterout "inputoutput.out"
165 string title "(empty)"
166 int level 0
167 int polynomial 1
168 bool debug false
169 end
170 // output starts here
171 int nelm 10
172 array double error {
173  0 1
174  1 0.5
175  2 0.25
176  3 0.125
177  4 0.0625
178  5 0.03125
179  6 0.015625
180  7 0.0078125
181  8 0.00390625
182  9 0.00195312
183 } @endcode
184  - \c inputoutput.gnuplot:
185 @code
186 # error error
187 0 1q
188 1 0.5
189 2 0.25
190 3 0.125
191 4 0.0625
192 5 0.03125
193 6 0.015625
194 7 0.0078125
195 8 0.00390625
196 9 0.00195312 @endcode
197 
198 Note the \c end keyword at the end of the input part and right before the output part. When reading in this file as input file, the parsing stops right there, ie. the previous output data is not read in.
199 
200  @section complete Complete Source Code
201 */
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich