Merge lp:~cwilson/spud/pythonbug into lp:spud

Proposed by Cian Wilson
Status: Merged
Merged at revision: 401
Proposed branch: lp:~cwilson/spud/pythonbug
Merge into: lp:spud
Diff against target: 348 lines (+88/-61)
3 files modified
python/libspud.py.in (+13/-3)
python/test.flml (+57/-54)
python/test_ctypes.py (+18/-4)
To merge this branch: bzr merge lp:~cwilson/spud/pythonbug
Reviewer Review Type Date Requested Status
Spud developers Pending
Review via email: mp+66633@code.launchpad.net

Description of the change

Remove the assumption that get_option in the python binding always returns a scalar.

To post a comment you must log in.
lp:~cwilson/spud/pythonbug updated
401. By Cian Wilson

A slightly neater way of extracting non-scalar values from get_option using get_option_rank. Updating the test.flml to the current fluidity schema - not that it matters much but I wanted to add a tensor_field to test the new get_option with. Adding a test to test_ctypes for integer lists and tensor floats. Also correcting a typo.

Revision history for this message
Patrick Farrell (pefarrell) wrote :

Looks great. Thanks for coding that up!

Revision history for this message
Cian Wilson (cwilson) wrote :

Thanks and no problem. I wanted the functionality after all :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'python/libspud.py.in'
2--- python/libspud.py.in 2011-05-18 15:17:14 +0000
3+++ python/libspud.py.in 2011-07-01 17:22:24 +0000
4@@ -163,20 +163,30 @@
5
6 def get_option(s):
7 type = get_option_type(s)
8- # assume scalar values for now ..
9+ rank=0
10 if type is str:
11 strlen = get_option_shape(s)[0]
12 val = create_string_buffer(strlen+1)
13 elif type is None:
14 raise spud_exceptions[SPUD_TYPE_ERROR]("No value exists at the given path")
15 else:
16- val = ctype_map[type]()
17+ val_type = ctype_map[type]
18+ rank = get_option_rank(s)
19+ shape = get_option_shape(s)
20+ for i in range(rank): val_type = val_type*shape[i]
21+ val = val_type()
22
23 out = cget_option(s, byref(c_int(len(s))), byref(val))
24 if out != SPUD_NO_ERROR:
25 raise spud_exceptions[out]
26
27- return val.value
28+ if rank==2:
29+ val_out = [[val[i][j] for j in range(shape[1])] for i in range(shape[0])]
30+ elif rank==1:
31+ val_out = [val[i] for i in range(shape[0])]
32+ else:
33+ val_out = val.value
34+ return val_out
35
36 cadd_option = libspud.cspud_add_option_
37 cadd_option.argtypes = [c_char_p, POINTER(c_int)]
38
39=== modified file 'python/test.flml'
40--- python/test.flml 2009-04-30 15:12:35 +0000
41+++ python/test.flml 2011-07-01 17:22:24 +0000
42@@ -1,4 +1,4 @@
43-<?xml version='1.0' encoding='utf8'?>
44+<?xml version='1.0' encoding='utf-8'?>
45 <fluidity_options>
46 <simulation_name>
47 <string_value lines="1">explicit-hyperc-superman-conservative</string_value>
48@@ -7,14 +7,12 @@
49 <string_value lines="1">multimaterial</string_value>
50 </problem_type>
51 <geometry>
52- <dimension replaces="NDIM">
53+ <dimension>
54 <integer_value rank="0">2</integer_value>
55 </dimension>
56 <mesh name="CoordinateMesh">
57 <from_file file_name="2d_square">
58- <format name="triangle">
59- <string_value>triangle</string_value>
60- </format>
61+ <format name="triangle"/>
62 <stat>
63 <include_in_stat/>
64 </stat>
65@@ -45,7 +43,7 @@
66 </from_mesh>
67 </mesh>
68 <quadrature>
69- <degree replaces="NGI">
70+ <degree>
71 <integer_value rank="0">1</integer_value>
72 </degree>
73 <controlvolume_surface_degree>
74@@ -58,7 +56,9 @@
75 <string_value>vtk</string_value>
76 </dump_format>
77 <dump_period_in_timesteps>
78- <integer_value rank="0">100</integer_value>
79+ <constant>
80+ <integer_value rank="0">100</integer_value>
81+ </constant>
82 </dump_period_in_timesteps>
83 <output_mesh name="PressureMesh"/>
84 <checkpointing>
85@@ -68,19 +68,20 @@
86 </checkpoint_period_in_dumps>
87 <checkpoint_at_end/>
88 </checkpointing>
89+ <stat/>
90 </io>
91 <timestepping>
92- <current_time replaces="ACCTIM">
93+ <current_time>
94 <real_value rank="0">0.0</real_value>
95 </current_time>
96- <timestep replaces="DT">
97+ <timestep>
98 <real_value rank="0">0.025</real_value>
99 <comment>this is overwritten by the adaptive timestepping</comment>
100 </timestep>
101- <finish_time replaces="LTIME">
102+ <finish_time>
103 <real_value rank="0">3.0</real_value>
104 </finish_time>
105- <adaptive_timestep replaces="TIMAUT">
106+ <adaptive_timestep>
107 <requested_cfl>
108 <real_value rank="0">0.25</real_value>
109 </requested_cfl>
110@@ -92,11 +93,12 @@
111 </timestepping>
112 <physical_parameters>
113 <gravity>
114- <magnitude replaces="GRAVTY">
115+ <magnitude>
116 <real_value rank="0">1.0</real_value>
117 </magnitude>
118- <vector_field replaces="BSOUX BSOUY BSOUZ" name="GravityDirection" rank="1">
119+ <vector_field name="GravityDirection" rank="1">
120 <prescribed>
121+ <mesh name="CoordinateMesh"/>
122 <value name="WholeMesh">
123 <constant>
124 <real_value shape="2" dim1="dim" rank="1">1.0 2.0</real_value>
125@@ -114,9 +116,18 @@
126 </gravity>
127 </physical_parameters>
128 <material_phase name="Material1">
129+ <equation_of_state>
130+ <fluids>
131+ <linear>
132+ <reference_density>
133+ <real_value rank="0">1.0</real_value>
134+ </reference_density>
135+ </linear>
136+ </fluids>
137+ </equation_of_state>
138 <scalar_field name="Density" rank="0">
139 <diagnostic>
140- <algorithm name="Internal" material_phase_support="single"/>
141+ <algorithm name="Internal" material_phase_support="multiple"/>
142 <mesh name="PressureMesh"/>
143 <output/>
144 <stat/>
145@@ -183,7 +194,7 @@
146 </vector_field>
147 <scalar_field name="ControlVolumeCFLNumber" rank="0">
148 <diagnostic>
149- <algorithm name="Internal" material_phase_support="single"/>
150+ <algorithm name="Internal" material_phase_support="multiple"/>
151 <mesh name="PressureMesh"/>
152 <output/>
153 <stat/>
154@@ -204,21 +215,21 @@
155 <equation name="AdvectionDiffusion"/>
156 <spatial_discretisation>
157 <control_volumes>
158- <face_value replaces="MOD(INT(ABS(NDISOT)/1),10) = 8,9" name="HyperC">
159- <project_upwind_value_from_point replaces="NDISOT &gt; 0 on simplex meshes">
160+ <face_value name="HyperC">
161+ <project_upwind_value_from_point>
162 <bound_projected_value_locally/>
163- <store_upwind_elements replaces="ABS(NDISOT) &gt;= 1000"/>
164+ <store_upwind_elements/>
165 </project_upwind_value_from_point>
166 <courant_number name="ControlVolumeCFLNumber"/>
167 </face_value>
168 <diffusion_scheme name="BassiRebay"/>
169 </control_volumes>
170- <conservative_advection replaces="TBETA, DEFALT (TBETA = 0.0)">
171+ <conservative_advection>
172 <real_value rank="0">0.0</real_value>
173 </conservative_advection>
174 </spatial_discretisation>
175 <temporal_discretisation>
176- <theta replaces="TTHETA, DEFALT (TTHETA = 0.5)">
177+ <theta>
178 <real_value rank="0">0.0</real_value>
179 </theta>
180 <control_volumes>
181@@ -241,7 +252,7 @@
182 return 0.0</string_value>
183 </python>
184 </initial_condition>
185- <boundary_conditions replaces="boundary, TTPER1 TTPER2 TTPERI" name="LetNoOneLeave">
186+ <boundary_conditions name="LetNoOneLeave">
187 <surface_ids>
188 <integer_value shape="4" rank="1">7 8 9 10</integer_value>
189 </surface_ids>
190@@ -261,21 +272,6 @@
191 <consistent_interpolation/>
192 </prognostic>
193 </scalar_field>
194- <scalar_field name="MaterialDensity" rank="0">
195- <prescribed>
196- <mesh name="PressureMesh"/>
197- <value name="WholeMesh">
198- <constant>
199- <real_value rank="0">1.0</real_value>
200- </constant>
201- </value>
202- <output/>
203- <stat/>
204- <detectors>
205- <exclude_from_detectors/>
206- </detectors>
207- </prescribed>
208- </scalar_field>
209 <scalar_field name="AnalyticalMaterialVolumeFraction" rank="0">
210 <prescribed>
211 <mesh name="PressureMesh"/>
212@@ -301,7 +297,7 @@
213 </scalar_field>
214 <scalar_field name="ScalarAbsoluteDifference" rank="0">
215 <diagnostic field_name_b="MaterialVolumeFraction" field_name_a="AnalyticalMaterialVolumeFraction">
216- <algorithm name="Internal" material_phase_support="single"/>
217+ <algorithm name="Internal" material_phase_support="multiple"/>
218 <mesh name="PressureMesh"/>
219 <output/>
220 <stat>
221@@ -320,7 +316,7 @@
222 </scalar_field>
223 <scalar_field name="ControlVolumeDivergence" rank="0">
224 <diagnostic field_name="Velocity">
225- <algorithm name="Internal" material_phase_support="single"/>
226+ <algorithm name="Internal" material_phase_support="multiple"/>
227 <mesh name="PressureMesh"/>
228 <output/>
229 <stat/>
230@@ -335,8 +331,30 @@
231 </steady_state>
232 </diagnostic>
233 </scalar_field>
234+ <tensor_field name="DummyTensor" rank="2">
235+ <prescribed>
236+ <mesh name="CoordinateMesh"/>
237+ <value name="WholeMesh">
238+ <anisotropic_asymmetric>
239+ <constant>
240+ <real_value symmetric="false" dim2="dim" shape="2 2" dim1="dim" rank="2">1.0 2.0 3.0 4.0</real_value>
241+ </constant>
242+ </anisotropic_asymmetric>
243+ </value>
244+ <output/>
245+ </prescribed>
246+ </tensor_field>
247 </material_phase>
248 <material_phase name="Material2">
249+ <equation_of_state>
250+ <fluids>
251+ <linear>
252+ <reference_density>
253+ <real_value rank="0">1.0</real_value>
254+ </reference_density>
255+ </linear>
256+ </fluids>
257+ </equation_of_state>
258 <scalar_field name="Density" rank="0">
259 <aliased material_phase_name="Material1" field_name="Density"/>
260 </scalar_field>
261@@ -345,7 +363,7 @@
262 </vector_field>
263 <scalar_field name="MaterialVolumeFraction" rank="0">
264 <diagnostic>
265- <algorithm name="Internal" material_phase_support="single"/>
266+ <algorithm name="Internal" material_phase_support="multiple"/>
267 <mesh name="PressureMesh"/>
268 <output/>
269 <stat/>
270@@ -360,20 +378,5 @@
271 </steady_state>
272 </diagnostic>
273 </scalar_field>
274- <scalar_field name="MaterialDensity" rank="0">
275- <prescribed>
276- <mesh name="PressureMesh"/>
277- <value name="WholeMesh">
278- <constant>
279- <real_value rank="0">1.0</real_value>
280- </constant>
281- </value>
282- <output/>
283- <stat/>
284- <detectors>
285- <exclude_from_detectors/>
286- </detectors>
287- </prescribed>
288- </scalar_field>
289 </material_phase>
290 </fluidity_options>
291
292=== modified file 'python/test_ctypes.py'
293--- python/test_ctypes.py 2011-05-18 15:17:14 +0000
294+++ python/test_ctypes.py 2011-07-01 17:22:24 +0000
295@@ -1,4 +1,5 @@
296 import libspud
297+print libspud.__file__
298
299 libspud.load_options('test.flml')
300
301@@ -24,10 +25,23 @@
302 libspud.set_option('/geometry/dimension', 3)
303 print libspud.get_option('/geometry/dimension')
304
305+list_path = '/material_phase::Material1/scalar_field::MaterialVolumeFraction/prognostic/boundary_conditions::LetNoOneLeave/surface_ids'
306+print libspud.get_option_shape(list_path)
307+print libspud.get_option_rank(list_path)
308+print libspud.get_option(list_path)
309+assert(libspud.get_option(list_path)==[7,8,9,10])
310+
311+tensor_path = '/material_phase::Material1/tensor_field::DummyTensor/prescribed/value::WholeMesh/anisotropic_asymmetric/constant'
312+print libspud.get_option_shape(tensor_path)
313+print libspud.get_option_rank(tensor_path)
314+print libspud.get_option(tensor_path)
315+assert(libspud.get_option(tensor_path)==[[1.0,2.0],[3.0,4.0]])
316+
317+
318 try:
319 libspud.add_option('/foo')
320 except libspud.SpudNewKeyWarning, e:
321- print "caugt libspud.SpudNewKeyWarning: "+e.message
322+ print "caught libspud.SpudNewKeyWarning: "+e.message
323 print libspud.option_count('/foo')
324
325 libspud.set_option('/problem_type', 'helloworld')
326@@ -36,7 +50,7 @@
327 try:
328 libspud.set_option_attribute('/foo/bar', 'foobar')
329 except libspud.SpudNewKeyWarning, e:
330- print "caugt libspud.SpudNewKeyWarning: "+e.message
331+ print "caught libspud.SpudNewKeyWarning: "+e.message
332 print libspud.get_option('/foo/bar')
333
334 libspud.delete_option('/foo')
335@@ -45,11 +59,11 @@
336 try:
337 libspud.get_option('/foo')
338 except libspud.SpudKeyError, e:
339- print "caugt libspud.SpudKeyError: "+e.message
340+ print "caught libspud.SpudKeyError: "+e.message
341
342 try:
343 libspud.get_option('/geometry')
344 except libspud.SpudTypeError, e:
345- print "caugt libspud.SpudTypeError: "+e.message
346+ print "caught libspud.SpudTypeError: "+e.message
347
348 libspud.write_options('test_out.flml')

Subscribers

People subscribed via source and target branches