1 |
#!/usr/bin/env python |
2 |
# ASCEND modelling environment |
3 |
# Copyright (C) 2006 Carnegie Mellon University |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2, or (at your option) |
8 |
# any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, |
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 59 Temple Place - Suite 330, |
18 |
# Boston, MA 02111-1307, USA. |
19 |
|
20 |
# This script gives a test suite for the high-level interface of ASCEND via |
21 |
# Python. It is also planned to be a wrapper for the CUnit test suite, although |
22 |
# this is still experimental. |
23 |
|
24 |
import unittest |
25 |
import os, sys |
26 |
import math |
27 |
import atexit |
28 |
|
29 |
import platform |
30 |
if platform.system() != "Windows": |
31 |
import dl |
32 |
sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_NOW) |
33 |
|
34 |
class Ascend(unittest.TestCase): |
35 |
|
36 |
def setUp(self): |
37 |
import ascpy |
38 |
self.L = ascpy.Library() |
39 |
|
40 |
def tearDown(self): |
41 |
self.L.clear() |
42 |
del self.L |
43 |
|
44 |
class AscendSelfTester(Ascend): |
45 |
|
46 |
def _run(self,modelname,solvername="QRSlv",filename=None): |
47 |
if filename==None: |
48 |
filename = 'johnpye/%s.a4c' % modelname |
49 |
self.L.load(filename) |
50 |
T = self.L.findType(modelname) |
51 |
M = T.getSimulation('sim') |
52 |
M.build() |
53 |
M.solve(ascpy.Solver(solvername),ascpy.SolverReporter()) |
54 |
M.run(T.getMethod('self_test')) |
55 |
return M |
56 |
|
57 |
class TestCompiler(Ascend): |
58 |
|
59 |
def testloading(self): |
60 |
pass |
61 |
|
62 |
def testsystema4l(self): |
63 |
self.L.load('system.a4l') |
64 |
|
65 |
def testatomsa4l(self): |
66 |
self.L.load('atoms.a4l') |
67 |
|
68 |
class TestSolver(AscendSelfTester): |
69 |
|
70 |
def testlog10(self): |
71 |
self._run('testlog10') |
72 |
|
73 |
def testconopt(self): |
74 |
self._run('testconopt',"CONOPT") |
75 |
|
76 |
def testcmslv2(self): |
77 |
self._run('testcmslv2',"CMSlv") |
78 |
|
79 |
def testsunpos1(self): |
80 |
self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c") |
81 |
|
82 |
def testsunpos2(self): |
83 |
self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c") |
84 |
|
85 |
def testsunpos3(self): |
86 |
self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c") |
87 |
|
88 |
def testsunpos4(self): |
89 |
self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c") |
90 |
|
91 |
def testsunpos5(self): |
92 |
self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c") |
93 |
|
94 |
def testsunpos6(self): |
95 |
self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c") |
96 |
|
97 |
def testinstanceas(self): |
98 |
M = self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c") |
99 |
self.assertAlmostEqual( float(M.t_solar), M.t_solar.as("s")) |
100 |
self.assertAlmostEqual( float(M.t_solar)/3600, M.t_solar.as("h")) |
101 |
|
102 |
class TestMatrix(AscendSelfTester): |
103 |
def testlog10(self): |
104 |
M = self._run('testlog10') |
105 |
print M.getMatrix().write(sys.stderr,"mmio") |
106 |
|
107 |
|
108 |
class TestIntegrator(Ascend): |
109 |
|
110 |
def testListIntegrators(self): |
111 |
I = ascpy.Integrator.getEngines() |
112 |
s1 = sorted([str(i) for i in I.values()]) |
113 |
s2 = sorted(['IDA','LSODE','AWW']) |
114 |
assert s1==s2 |
115 |
|
116 |
# this routine is reused by both testIDA and testLSODE |
117 |
def _testIntegrator(self,integratorname): |
118 |
self.L.load('johnpye/shm.a4c') |
119 |
M = self.L.findType('shm').getSimulation('sim') |
120 |
M.setSolver(ascpy.Solver('QRSlv')) |
121 |
print M.getChildren() |
122 |
assert float(M.x) == 10.0 |
123 |
assert float(M.v) == 0.0 |
124 |
t_end = math.pi |
125 |
|
126 |
I = ascpy.Integrator(M) |
127 |
I.setReporter(ascpy.IntegratorReporterNull(I)) |
128 |
I.setEngine(integratorname); |
129 |
I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100); |
130 |
I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff) |
131 |
I.setMaxSubStep(0.02); |
132 |
I.setInitialSubStep(0.001); |
133 |
I.setMaxSubSteps(200); |
134 |
if(integratorname=='IDA'): |
135 |
I.setParameter('autodiff',False) |
136 |
I.analyse(); |
137 |
I.solve(); |
138 |
print "At end of simulation," |
139 |
print "x = %f" % M.x |
140 |
print "v = %f" % M.v |
141 |
assert abs(float(M.x) + 10) < 1e-2 |
142 |
assert abs(float(M.v)) < 1e-2 |
143 |
assert I.getNumObservedVars() == 3 |
144 |
|
145 |
def testInvalidIntegrator(self): |
146 |
self.L.load('johnpye/shm.a4c') |
147 |
M = self.L.findType('shm').getSimulation('sim') |
148 |
M.setSolver(ascpy.Solver('QRSlv')) |
149 |
I = ascpy.Integrator(M) |
150 |
try: |
151 |
I.setEngine('___NONEXISTENT____') |
152 |
except RuntimeError: |
153 |
return |
154 |
self.fail("setEngine did not raise error!") |
155 |
|
156 |
def testLSODE(self): |
157 |
self._testIntegrator('LSODE') |
158 |
|
159 |
def testIDA(self): |
160 |
self._testIntegrator('IDA') |
161 |
|
162 |
def testparameters(self): |
163 |
self.L.load('johnpye/shm.a4c') |
164 |
M = self.L.findType('shm').getSimulation('sim') |
165 |
M.build() |
166 |
I = ascpy.Integrator(M) |
167 |
I.setEngine('IDA') |
168 |
P = I.getParameters() |
169 |
for p in P: |
170 |
print p.getName(),"=",p.getValue() |
171 |
assert len(P)==11 |
172 |
assert P[0].isStr() |
173 |
assert P[0].getName()=="linsolver" |
174 |
assert P[0].getValue()=='SPGMR' |
175 |
assert P[2].getName()=="autodiff" |
176 |
assert P[2].getValue()==True |
177 |
assert P[7].getName()=="atolvect" |
178 |
assert P[7].getBoolValue() == True |
179 |
P[2].setBoolValue(False) |
180 |
assert P[2].getBoolValue()==False |
181 |
I.setParameters(P) |
182 |
assert I.getParameterValue('autodiff')==False |
183 |
I.setParameter('autodiff',True) |
184 |
try: |
185 |
v = I.getParameterValue('nonexist') |
186 |
except KeyError: |
187 |
pass |
188 |
else: |
189 |
self.fail('Failed to trip invalid Integrator parameter') |
190 |
|
191 |
class TestLSODE(Ascend): |
192 |
|
193 |
def testzill(self): |
194 |
self.L.load('johnpye/zill.a4c') |
195 |
T = self.L.findType('zill') |
196 |
M = T.getSimulation('sim') |
197 |
M.setSolver(ascpy.Solver('QRSlv')) |
198 |
I = ascpy.Integrator(M) |
199 |
I.setEngine('LSODE') |
200 |
I.setMinSubStep(1e-7) |
201 |
I.setMaxSubStep(0.001) |
202 |
I.setMaxSubSteps(10000) |
203 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
204 |
I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5) |
205 |
I.analyse() |
206 |
I.solve() |
207 |
M.run(T.getMethod('self_test')) |
208 |
|
209 |
def testnewton(self): |
210 |
sys.stderr.write("STARTING TESTNEWTON\n") |
211 |
self.L.load('johnpye/newton.a4c') |
212 |
T = self.L.findType('newton') |
213 |
M = T.getSimulation('sim') |
214 |
M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter()) |
215 |
I = ascpy.Integrator(M) |
216 |
I.setEngine('LSODE') |
217 |
I.setParameter('rtolvect',False) |
218 |
I.setParameter('rtol',1e-7) |
219 |
I.setParameter('atolvect',False) |
220 |
I.setParameter('atol',1e-7) |
221 |
I.setMinSubStep(1e-7) |
222 |
I.setMaxSubStep(0.001) |
223 |
I.setMaxSubSteps(10000) |
224 |
|
225 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
226 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2) |
227 |
I.analyse() |
228 |
I.solve() |
229 |
print "At end of simulation," |
230 |
print "x = %f" % M.x |
231 |
print "v = %f" % M.v |
232 |
M.run(T.getMethod('self_test')) |
233 |
|
234 |
def testlotka(self): |
235 |
self.L.load('johnpye/lotka.a4c') |
236 |
M = self.L.findType('lotka').getSimulation('sim') |
237 |
M.setSolver(ascpy.Solver("QRSlv")) |
238 |
I = ascpy.Integrator(M) |
239 |
I.setEngine('LSODE') |
240 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
241 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5) |
242 |
I.analyse() |
243 |
print "Number of vars = %d" % I.getNumVars() |
244 |
assert I.getNumVars()==2 |
245 |
I.solve() |
246 |
assert I.getNumObservedVars() == 3; |
247 |
assert abs(M.R - 832) < 1.0 |
248 |
assert abs(M.F - 21.36) < 0.1 |
249 |
|
250 |
#------------------------------------------------------------------------------- |
251 |
# Testing of a external blackbox functions |
252 |
|
253 |
class TestBlackBox(AscendSelfTester): |
254 |
def testparsefail0(self): |
255 |
try: |
256 |
self.L.load('test/blackbox/parsefail0.a4c') |
257 |
self.fail("parsefail0 should not have loaded without errors") |
258 |
except: |
259 |
pass |
260 |
|
261 |
def testparsefail1(self): |
262 |
try: |
263 |
self.L.load('test/blackbox/parsefail1.a4c') |
264 |
self.fail("parsefail1 should not have loaded without errors") |
265 |
except: |
266 |
pass |
267 |
|
268 |
def testparsefail2(self): |
269 |
try: |
270 |
self.L.load('test/blackbox/parsefail2.a4c') |
271 |
self.fail("parsefail2 should not have loaded without errors") |
272 |
except: |
273 |
pass |
274 |
|
275 |
def testparsefail3(self): |
276 |
try: |
277 |
self.L.load('test/blackbox/parsefail3.a4c') |
278 |
self.fail("parsefail3 should not have loaded without errors") |
279 |
except: |
280 |
pass |
281 |
|
282 |
def testparsefail4(self): |
283 |
try: |
284 |
self.L.load('test/blackbox/parsefail4.a4c') |
285 |
self.fail("parsefail4 should not have loaded") |
286 |
except: |
287 |
pass |
288 |
|
289 |
def testfail1(self): |
290 |
"""Mismatched arg counts check-- tests bbox, not ascend.""" |
291 |
self.L.load('test/blackbox/fail1.a4c') |
292 |
try: |
293 |
M = self.L.findType('fail1').getSimulation('sim') |
294 |
self.fail("expected exception was not raised") |
295 |
except RuntimeError,e: |
296 |
print "Caught exception '%s', assumed ok" % e |
297 |
|
298 |
def testfail2(self): |
299 |
"""Incorrect data arg check -- tests bbox, not ascend""" |
300 |
self.L.load('test/blackbox/fail2.a4c') |
301 |
try: |
302 |
M = self.L.findType('fail2').getSimulation('sim') |
303 |
self.fail("expected exception was not raised") |
304 |
except RuntimeError,e: |
305 |
print "Caught exception '%s', assumed ok (should mention errors during instantiation)" % e |
306 |
|
307 |
def testpass1(self): |
308 |
"""simple single bbox forward solve""" |
309 |
M = self._run('pass1',filename='test/blackbox/pass.a4c') |
310 |
|
311 |
def testpass2(self): |
312 |
"""simple single bbox reverse solve""" |
313 |
M = self._run('pass2',filename='test/blackbox/pass.a4c') |
314 |
|
315 |
def testpass3(self): |
316 |
"""simple double bbox solve""" |
317 |
M = self._run('pass3',filename='test/blackbox/pass3.a4c') |
318 |
|
319 |
def testpass4(self): |
320 |
"""simple double bbox reverse solve""" |
321 |
M = self._run('pass4',filename='test/blackbox/pass3.a4c') |
322 |
|
323 |
def testpass5(self): |
324 |
M = self._run('pass5',filename='test/blackbox/pass5.a4c') |
325 |
|
326 |
def testpass6(self): |
327 |
M = self._run('pass6',filename='test/blackbox/pass5.a4c') |
328 |
|
329 |
def testpass7(self): |
330 |
M = self._run('pass7',filename='test/blackbox/passmerge.a4c') |
331 |
|
332 |
def testpass8(self): |
333 |
M = self._run('pass8',filename='test/blackbox/passmerge.a4c') |
334 |
|
335 |
def testpass9(self): |
336 |
M = self._run('pass9',filename='test/blackbox/passmerge.a4c') |
337 |
|
338 |
def testpass10(self): |
339 |
M = self._run('pass10',filename='test/blackbox/passmerge.a4c') |
340 |
|
341 |
def testpass11(self): |
342 |
M = self._run('pass11',filename='test/blackbox/passmerge.a4c') |
343 |
|
344 |
def testpass12(self): |
345 |
M = self._run('pass12',filename='test/blackbox/passmerge.a4c') |
346 |
|
347 |
# this test doesn't work: 'system is inconsistent' -- and structurally singular |
348 |
# def testpass13(self): |
349 |
# """cross-merged input/output solve""" |
350 |
# M = self._run('pass13',filename='test/blackbox/passmerge.a4c') |
351 |
|
352 |
def testpass14(self): |
353 |
"""cross-merged input/output reverse solve""" |
354 |
M = self._run('pass14',filename='test/blackbox/passmerge.a4c') |
355 |
|
356 |
def testpass20(self): |
357 |
M = self._run('pass20',filename='test/blackbox/passarray.a4c') |
358 |
|
359 |
def testparsefail21(self): |
360 |
"""dense array of black boxes wrong syntax""" |
361 |
try: |
362 |
self.L.load('test/blackbox/parsefail21.a4c') |
363 |
self.fail("parsefail21 should not have loaded without errors") |
364 |
except: |
365 |
pass |
366 |
|
367 |
def testpass22(self): |
368 |
M = self._run('pass22',filename='test/blackbox/passarray.a4c') |
369 |
|
370 |
def testpass23(self): |
371 |
M = self._run('pass23',filename='test/blackbox/passarray.a4c') |
372 |
|
373 |
def testpass61(self): |
374 |
M = self._run('pass61',filename='test/blackbox/reinstantiate.a4c') |
375 |
|
376 |
def testpass62(self): |
377 |
M = self._run('pass62',filename='test/blackbox/reinstantiate.a4c') |
378 |
|
379 |
def testpass64(self): |
380 |
M = self._run('pass64',filename='test/blackbox/reinstantiate.a4c') |
381 |
|
382 |
def testpass65(self): |
383 |
M = self._run('pass65',filename='test/blackbox/reinstantiate.a4c') |
384 |
|
385 |
def testpass66(self): |
386 |
M = self._run('pass66',filename='test/blackbox/reinstantiate.a4c') |
387 |
|
388 |
def testpass67(self): |
389 |
M = self._run('pass67',filename='test/blackbox/reinstantiate.a4c') |
390 |
|
391 |
class TestExtFn(AscendSelfTester): |
392 |
def testextfntest(self): |
393 |
M = self._run('extfntest',filename='johnpye/extfn/extfntest.a4c') |
394 |
self.assertAlmostEqual(M.y, 2); |
395 |
self.assertAlmostEqual(M.x, 1); |
396 |
self.assertAlmostEqual(M.y, M.x + 1); |
397 |
|
398 |
def testextrelfor(self): |
399 |
M = self._run('extrelfor',filename='johnpye/extfn/extrelfor.a4c') |
400 |
|
401 |
## @TODO fix bug with badly-named bbox rel in a loop (Ben, maybe) |
402 |
# def testextrelforbadnaming(self): |
403 |
# self.L.load('johnpye/extfn/extrelforbadnaming.a4c') |
404 |
# T = self.L.findType('extrelfor') |
405 |
# M = T.getSimulation('sim') |
406 |
# M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter()) |
407 |
# print "x[1] = %f" % M.x[1] |
408 |
# print "x[2] = %f" % M.x[2] |
409 |
# print "x[3] = %f" % M.x[3] |
410 |
# print "x[4] = %f" % M.x[4] |
411 |
# print "x[5] = %f" % M.x[5] |
412 |
# M.run(T.getMethod('self_test')) |
413 |
|
414 |
def testextrelrepeat(self): |
415 |
M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c') |
416 |
|
417 |
#------------------------------------------------------------------------------- |
418 |
# Testing of a ExtPy - external python methods |
419 |
|
420 |
class TestExtPy(AscendSelfTester): |
421 |
def test1(self): |
422 |
self.L.load('johnpye/extpy/extpytest.a4c') |
423 |
T = self.L.findType('extpytest') |
424 |
M = T.getSimulation('sim') |
425 |
M.run(T.getMethod('self_test')) |
426 |
|
427 |
def test2(self): |
428 |
self.L.load('johnpye/extpy/extpytest.a4c') |
429 |
T = self.L.findType('extpytest') |
430 |
M = T.getSimulation('sim') |
431 |
M.run(T.getMethod('pythonthing')) |
432 |
M.run(T.getMethod('pythonthing')) |
433 |
M.run(T.getMethod('pythonthing')) |
434 |
M.run(T.getMethod('pythonthing')) |
435 |
# causes crash! |
436 |
|
437 |
#------------------------------------------------------------------------------- |
438 |
# Testing of saturated steam properties library (iapwssatprops.a4c) |
439 |
|
440 |
class TestSteam(AscendSelfTester): |
441 |
def testiapwssatprops1(self): |
442 |
M = self._run('testiapwssatprops1',filename='steam/iapwssatprops.a4c') |
443 |
def testiapwssatprops2(self): |
444 |
M = self._run('testiapwssatprops2',filename='steam/iapwssatprops.a4c') |
445 |
def testiapwssatprops3(self): |
446 |
M = self._run('testiapwssatprops3',filename='steam/iapwssatprops.a4c') |
447 |
def testsatsteamstream(self): |
448 |
M = self._run('satsteamstream',filename='steam/satsteamstream.a4c') |
449 |
|
450 |
def testsatsteamstream(self): |
451 |
M = self._run('satsteamstream',filename='steam/satsteamstream.a4c') |
452 |
|
453 |
## @TODO fix error capture from bounds checking during initialisation |
454 |
# def testiapwssat1(self): |
455 |
# M = self._run('testiapwssat1',filename='steam/iapwssat.a4c') |
456 |
|
457 |
# @TODO fix bug with unpivoted node[i].hg_expr eqns. |
458 |
def testdsgsat(self): |
459 |
self.L.load('steam/dsgsat2.a4c') |
460 |
T = self.L.findType('dsgsat2') |
461 |
M = T.getSimulation('sim',False) |
462 |
M.run(T.getMethod('on_load')) |
463 |
M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter()) |
464 |
self.assertAlmostEqual(M.dTw_dt[4],0.0); |
465 |
M.run(T.getMethod('configure_dynamic')) |
466 |
M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter()) |
467 |
self.assertAlmostEqual(M.dTw_dt[4],0.0) |
468 |
Tw1 = float(M.T_w[4]) |
469 |
I = ascpy.Integrator(M) |
470 |
I.setEngine('LSODE') |
471 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
472 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
473 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 3600, 10) |
474 |
I.setMinSubStep(0.0001) |
475 |
I.setMaxSubStep(100) |
476 |
I.setInitialSubStep(0.1) |
477 |
I.analyse() |
478 |
I.solve() |
479 |
self.assertAlmostEqual(float(M.T_w[4]),Tw1) |
480 |
M.qdot_s.setRealValueWithUnits(1000,"W/m") |
481 |
self.assertAlmostEqual(M.qdot_s.as("W/m"),1000) |
482 |
M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter()) |
483 |
self.assertNotAlmostEqual(M.dTw_dt[4],0.0) |
484 |
# I = ascpy.Integrator(M) |
485 |
# I.setEngine('LSODE') |
486 |
# I.setReporter(ascpy.IntegratorReporterConsole(I)) |
487 |
# I.setReporter(ascpy.IntegratorReporterConsole(I)) |
488 |
# I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 100) |
489 |
# I.setMinSubStep(0.0001) |
490 |
# I.setMaxSubStep(100) |
491 |
# I.setInitialSubStep(0.1) |
492 |
# I.analyse() |
493 |
# I.solve() |
494 |
|
495 |
#------------------------------------------------------------------------------- |
496 |
# Testing of freesteam external steam properties functions |
497 |
|
498 |
with_freesteam = True |
499 |
try: |
500 |
# we assume that if the freesteam python module is installed, the ASCEND |
501 |
# external library will also be. |
502 |
import freesteam |
503 |
have_freesteam = True |
504 |
except ImportError,e: |
505 |
have_freesteam = False |
506 |
|
507 |
if with_freesteam and have_freesteam: |
508 |
class TestFreesteam(AscendSelfTester): |
509 |
# def testfreesteamtest(self): |
510 |
# """run the self-test cases bundled with freesteam""" |
511 |
# self._run('testfreesteam',filename='testfreesteam.a4c') |
512 |
|
513 |
def testload(self): |
514 |
"""check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)""" |
515 |
self.L.load('johnpye/thermalequilibrium2.a4c') |
516 |
|
517 |
def testinstantiate(self): |
518 |
"""load an instantiate 'thermalequilibrium2'""" |
519 |
self.testload() |
520 |
M = self.L.findType('thermalequilibrium2').getSimulation('sim') |
521 |
return M |
522 |
|
523 |
def testintegrate(self): |
524 |
"""integrate transfer of heat from one mass of water/steam to another |
525 |
according to Newton's law of cooling""" |
526 |
M = self.testinstantiate() |
527 |
M.setSolver(ascpy.Solver("QRSlv")) |
528 |
I = ascpy.Integrator(M) |
529 |
I.setEngine('LSODE') |
530 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
531 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30) |
532 |
I.setMinSubStep(0.01) |
533 |
I.setInitialSubStep(1) |
534 |
I.analyse() |
535 |
print "Number of vars = %d" % I.getNumVars() |
536 |
assert I.getNumVars()==2 |
537 |
I.solve() |
538 |
assert I.getNumObservedVars() == 3; |
539 |
print "S[1].T = %f K" % M.S[1].T |
540 |
print "S[2].T = %f K" % M.S[2].T |
541 |
print "Q = %f W" % M.Q |
542 |
self.assertAlmostEqual(float(M.S[1].T),506.77225109,4); |
543 |
self.assertAlmostEqual(float(M.S[2].T),511.605173967,5); |
544 |
self.assertAlmostEqual(float(M.Q),-48.32922877329,3); |
545 |
self.assertAlmostEqual(float(M.t),3000); |
546 |
print "Note that the above values have not been verified analytically" |
547 |
|
548 |
|
549 |
#------------------------------------------------------------------------------- |
550 |
# Testing of IDA models using DENSE linear solver |
551 |
|
552 |
class TestIDADENSE(Ascend): |
553 |
"""IDA DAE integrator, DENSE linear solver""" |
554 |
|
555 |
def testnewton(self): |
556 |
sys.stderr.write("STARTING TESTNEWTON\n") |
557 |
self.L.load('johnpye/newton.a4c') |
558 |
T = self.L.findType('newton') |
559 |
M = T.getSimulation('sim') |
560 |
M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter()) |
561 |
I = ascpy.Integrator(M) |
562 |
I.setEngine('IDA') |
563 |
I.setParameter('linsolver','DENSE') |
564 |
I.setParameter('safeeval',True) |
565 |
I.setParameter('rtol',1e-8) |
566 |
I.setMaxSubStep(0.001) |
567 |
I.setMaxSubSteps(10000) |
568 |
|
569 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
570 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2) |
571 |
I.analyse() |
572 |
I.solve() |
573 |
print "At end of simulation," |
574 |
print "x = %f" % M.x |
575 |
print "v = %f" % M.v |
576 |
M.run(T.getMethod('self_test')) |
577 |
|
578 |
def testlotka(self): |
579 |
self.L.load('johnpye/lotka.a4c') |
580 |
M = self.L.findType('lotka').getSimulation('sim') |
581 |
M.setSolver(ascpy.Solver("QRSlv")) |
582 |
I = ascpy.Integrator(M) |
583 |
I.setEngine('IDA') |
584 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
585 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5); |
586 |
I.setParameter('linsolver','DENSE') |
587 |
I.setParameter('rtol',1e-8); |
588 |
I.analyse() |
589 |
assert I.getNumVars()==2 |
590 |
assert abs(M.R - 1000) < 1e-300 |
591 |
I.solve() |
592 |
assert I.getNumObservedVars() == 3 |
593 |
assert abs(M.R - 832) < 1.0 |
594 |
assert abs(M.F - 21.36) < 0.1 |
595 |
|
596 |
def testdenx(self): |
597 |
print "-----------------------------=====" |
598 |
self.L.load('johnpye/idadenx.a4c') |
599 |
M = self.L.findType('idadenx').getSimulation('sim') |
600 |
M.setSolver(ascpy.Solver("QRSlv")) |
601 |
I = ascpy.Integrator(M) |
602 |
I.setEngine('IDA') |
603 |
I.setParameter('calcic','YA_YPD') |
604 |
I.setParameter('linsolver','DENSE') |
605 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
606 |
I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11) |
607 |
I.setMaxSubStep(0); |
608 |
I.setInitialSubStep(0) |
609 |
I.setMaxSubSteps(0); |
610 |
I.setParameter('autodiff',True) |
611 |
I.analyse() |
612 |
I.solve() |
613 |
assert abs(float(M.y1) - 5.1091e-08) < 2e-9 |
614 |
assert abs(float(M.y2) - 2.0437e-13) < 2e-14 |
615 |
assert abs(float(M.y3) - 1.0) < 1e-5 |
616 |
|
617 |
## @TODO fails during IDACalcIC (model too big?) |
618 |
# def testkryx(self): |
619 |
# self.L.load('johnpye/idakryx.a4c') |
620 |
# ascpy.getCompiler().setUseRelationSharing(False) |
621 |
# M = self.L.findType('idakryx').getSimulation('sim') |
622 |
# M.setSolver(ascpy.Solver('QRSlv')) |
623 |
# M.build() |
624 |
# I = ascpy.Integrator(M) |
625 |
# I.setEngine('IDA') |
626 |
# I.setReporter(ascpy.IntegratorReporterConsole(I)) |
627 |
# I.setParameter('linsolver','DENSE') |
628 |
# I.setParameter('maxl',8) |
629 |
# I.setParameter('gsmodified',False) |
630 |
# I.setParameter('autodiff',True) |
631 |
# I.setParameter('rtol',0) |
632 |
# I.setParameter('atol',1e-3); |
633 |
# I.setParameter('atolvect',False) |
634 |
# I.setParameter('calcic','YA_YDP') |
635 |
# I.analyse() |
636 |
# I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11) |
637 |
# I.solve() |
638 |
# assert abs(M.u[2][2].getValue()) < 1e-5 |
639 |
|
640 |
#------------------------------------------------------------------------------- |
641 |
# Testing of IDA models using SPGMR linear solver (Krylov) |
642 |
|
643 |
# these tests are disabled until SPGMR preconditioning has been implemented |
644 |
class TestIDASPGMR:#(Ascend): |
645 |
def testlotka(self): |
646 |
self.L.load('johnpye/lotka.a4c') |
647 |
M = self.L.findType('lotka').getSimulation('sim') |
648 |
M.setSolver(ascpy.Solver("QRSlv")) |
649 |
I = ascpy.Integrator(M) |
650 |
I.setEngine('IDA') |
651 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
652 |
I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5) |
653 |
I.setParameter('rtol',1e-8) |
654 |
I.analyse() |
655 |
assert I.getNumVars()==2 |
656 |
assert abs(M.R - 1000) < 1e-300 |
657 |
I.solve() |
658 |
assert I.getNumObservedVars() == 3 |
659 |
assert abs(M.R - 832) < 1.0 |
660 |
assert abs(M.F - 21.36) < 0.1 |
661 |
|
662 |
|
663 |
def testkryx(self): |
664 |
self.L.load('johnpye/idakryx.a4c') |
665 |
M = self.L.findType('idakryx').getSimulation('sim') |
666 |
M.build() |
667 |
I = ascpy.Integrator(M) |
668 |
I.setEngine('IDA') |
669 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
670 |
I.setParameter('linsolver','SPGMR') |
671 |
I.setParameter('prec','JACOBI') |
672 |
I.setParameter('maxl',8) |
673 |
I.setParameter('gsmodified',False) |
674 |
I.setParameter('autodiff',True) |
675 |
I.setParameter('gsmodified',True) |
676 |
I.setParameter('rtol',0) |
677 |
I.setParameter('atol',1e-3); |
678 |
I.setParameter('atolvect',False) |
679 |
I.setParameter('calcic','Y') |
680 |
I.analyse() |
681 |
I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10); |
682 |
print M.udot[1][3] |
683 |
I.solve() |
684 |
assert 0 |
685 |
|
686 |
def testzill(self): |
687 |
self.L.load('johnpye/zill.a4c') |
688 |
T = self.L.findType('zill') |
689 |
M = T.getSimulation('sim') |
690 |
M.setSolver(ascpy.Solver('QRSlv')) |
691 |
I = ascpy.Integrator(M) |
692 |
I.setEngine('IDA') |
693 |
I.setParameter('safeeval',False) |
694 |
I.setMinSubStep(1e-7) |
695 |
I.setMaxSubStep(0.001) |
696 |
I.setMaxSubSteps(10000) |
697 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
698 |
I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5) |
699 |
I.analyse() |
700 |
I.solve() |
701 |
M.run(T.getMethod('self_test')) |
702 |
|
703 |
def testdenxSPGMR(self): |
704 |
self.L.load('johnpye/idadenx.a4c') |
705 |
M = self.L.findType('idadenx').getSimulation('sim') |
706 |
M.setSolver(ascpy.Solver('QRSlv')) |
707 |
I = ascpy.Integrator(M) |
708 |
I.setEngine('IDA') |
709 |
I.setReporter(ascpy.IntegratorReporterConsole(I)) |
710 |
I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11) |
711 |
I.setMaxSubStep(0); |
712 |
I.setInitialSubStep(0); |
713 |
I.setMaxSubSteps(0); |
714 |
I.setParameter('autodiff',True) |
715 |
I.setParameter('linsolver','SPGMR') |
716 |
I.setParameter('gsmodified',False) |
717 |
I.setParameter('maxncf',10) |
718 |
I.analyse() |
719 |
I.solve() |
720 |
assert abs(float(M.y1) - 5.1091e-08) < 1e-10 |
721 |
assert abs(float(M.y2) - 2.0437e-13) < 1e-15 |
722 |
assert abs(float(M.y3) - 1.0) < 1e-5 |
723 |
|
724 |
# move code above down here if you want to temporarily avoid testing it |
725 |
class NotToBeTested: |
726 |
def nothing(self): |
727 |
pass |
728 |
|
729 |
if __name__=='__main__': |
730 |
# a whole bag of tricks to make sure we get the necessary dirs in our ascend, python and ld path vars |
731 |
restart = 0 |
732 |
|
733 |
if platform.system()=="Windows": |
734 |
LD_LIBRARY_PATH="PATH" |
735 |
SEP = ";" |
736 |
else: |
737 |
LD_LIBRARY_PATH="LD_LIBRARY_PATH" |
738 |
SEP = ":" |
739 |
|
740 |
freesteamdir = os.path.expanduser("~/freesteam/ascend") |
741 |
modeldirs = [os.path.abspath(os.path.join(sys.path[0],"models")),os.path.abspath(freesteamdir)] |
742 |
if not os.environ.get('ASCENDLIBRARY'): |
743 |
os.environ['ASCENDLIBRARY'] = SEP.join(modeldirs) |
744 |
restart = 1 |
745 |
else: |
746 |
envmodelsdir = [os.path.abspath(i) for i in os.environ['ASCENDLIBRARY'].split(SEP)] |
747 |
for l in modeldirs: |
748 |
if l in envmodelsdir[len(modeldirs):]: |
749 |
envmodelsdir.remove(l) |
750 |
restart = 1 |
751 |
for l in modeldirs: |
752 |
if l not in envmodelsdir: |
753 |
envmodelsdir.insert(0,l) |
754 |
restart = 1 |
755 |
os.environ['ASCENDLIBRARY'] = SEP.join(envmodelsdir) |
756 |
|
757 |
libdirs = ["pygtk","."] |
758 |
libdirs = [os.path.normpath(os.path.join(sys.path[0],l)) for l in libdirs] |
759 |
if not os.environ.get(LD_LIBRARY_PATH): |
760 |
os.environ[LD_LIBRARY_PATH]=SEP.join(libdirs) |
761 |
restart = 1 |
762 |
else: |
763 |
envlibdirs = [os.path.normpath(i) for i in os.environ[LD_LIBRARY_PATH].split(SEP)] |
764 |
for l in libdirs: |
765 |
if l in envlibdirs[len(libdirs):]: |
766 |
envlibdirs.remove(l) |
767 |
restart = 1 |
768 |
for l in libdirs: |
769 |
if l not in envlibdirs: |
770 |
envlibdirs.insert(0,l) |
771 |
restart = 1 |
772 |
os.environ[LD_LIBRARY_PATH] = SEP.join(envlibdirs) |
773 |
|
774 |
pypath = os.path.normpath(os.path.join(sys.path[0],"pygtk")) |
775 |
if not os.environ.get('PYTHONPATH'): |
776 |
os.environ['PYTHONPATH']=pypath |
777 |
else: |
778 |
envpypath = os.environ['PYTHONPATH'].split(SEP) |
779 |
if pypath not in envpypath: |
780 |
envpypath.insert(0,pypath) |
781 |
os.environ['PYTHONPATH']=envpypath |
782 |
restart = 1 |
783 |
|
784 |
if restart: |
785 |
script = os.path.join(sys.path[0],"test.py") |
786 |
print "Restarting with..." |
787 |
print " LD_LIBRARY_PATH = %s" % os.environ.get(LD_LIBRARY_PATH) |
788 |
print " PYTHONPATH = %s" % os.environ.get('PYTHONPATH') |
789 |
print " ASCENDLIBRARY = %s" % os.environ.get('ASCENDLIBRARY') |
790 |
os.execvp("python",[script] + sys.argv) |
791 |
|
792 |
import ascpy |
793 |
|
794 |
try: |
795 |
import cunit |
796 |
except: |
797 |
pass |
798 |
|
799 |
atexit.register(ascpy.shutdown) |
800 |
#suite = unittest.TestSuite() |
801 |
#suite = unittest.defaultTestLoader.loadTestsFromName('__main__') |
802 |
#unittest.TextTestRunner(verbosity=2).run(suite) |
803 |
unittest.main() |