/[ascend]/trunk/test.py
ViewVC logotype

Annotation of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1278 - (hide annotations) (download) (as text)
Fri Feb 9 02:35:43 2007 UTC (13 years, 7 months ago) by johnpye
File MIME type: text/x-python
File size: 35276 byte(s)
Central differences added in some cases (DSGSAT3)
1 johnpye 1008 #!/usr/bin/env python
2 johnpye 1102 # ASCEND modelling environment
3 johnpye 1259 # Copyright (C) 2006, 2007 Carnegie Mellon University
4 johnpye 1102 #
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 johnpye 669 import unittest
25 johnpye 1104 import os, sys
26 johnpye 1098 import math
27     import atexit
28 johnpye 1091
29     import platform
30 johnpye 1028 if platform.system() != "Windows":
31     import dl
32     sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_NOW)
33    
34 johnpye 956 class Ascend(unittest.TestCase):
35 johnpye 669
36 johnpye 933 def setUp(self):
37     import ascpy
38 johnpye 1118 self.L = ascpy.Library()
39 johnpye 933
40     def tearDown(self):
41     self.L.clear()
42     del self.L
43    
44 johnpye 1024 class AscendSelfTester(Ascend):
45    
46 johnpye 1257 def _run(self,modelname,solvername="QRSlv",filename=None,parameters={}):
47 johnpye 1024 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 johnpye 1257 M.setSolver(ascpy.Solver(solvername))
53     for k,v in parameters.iteritems():
54     M.setParameter(k,v)
55 johnpye 1024 M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())
56     M.run(T.getMethod('self_test'))
57     return M
58    
59 johnpye 966 class TestCompiler(Ascend):
60    
61 johnpye 1213 def _run(self,filen,modeln=""):
62     self.L.load('test/compiler/%s.a4c' % filen)
63     T = self.L.findType('%s%s' % (filen,modeln))
64     M = T.getSimulation('sim')
65     M.build()
66    
67     def _runfail(self,filen,n,msg="failed"):
68     try:
69     self._run(filen,'fail%d' % n)
70     except Exception,e:
71     print "(EXPECTED) ERROR: %s" % e
72     return
73     self.fail(msg)
74    
75    
76 johnpye 941 def testloading(self):
77 johnpye 1213 """library startup"""
78 johnpye 941 pass
79    
80     def testsystema4l(self):
81 johnpye 1213 """loading system.a4l?"""
82 johnpye 941 self.L.load('system.a4l')
83    
84     def testatomsa4l(self):
85 johnpye 1213 """loading atoms.a4l?"""
86 johnpye 941 self.L.load('atoms.a4l')
87    
88 johnpye 1213 def testmissingreq(self):
89     """flagging a missing REQUIRE"""
90     self._runfail('missingreq',1)
91    
92     def testmissingsubreq(self):
93     """flagging a subsidiary missing REQUIRE"""
94     self._runfail('missingreq',1)
95    
96 johnpye 1024 class TestSolver(AscendSelfTester):
97 johnpye 966
98     def testlog10(self):
99     self._run('testlog10')
100    
101 johnpye 1156 def testrootsofpoly(self):
102     self._run('roots_of_poly',filename="roots_of_poly.a4c")
103    
104 johnpye 1152 def testcollapsingcan(self):
105     self._run('collapsingcan',filename="collapsingcan.a4c")
106    
107 johnpye 1165 def testdistancecalc(self):
108     self._run('distance_calc',filename="distance_calc.a4c")
109    
110 johnpye 966 def testconopt(self):
111     self._run('testconopt',"CONOPT")
112    
113     def testcmslv2(self):
114 johnpye 974 self._run('testcmslv2',"CMSlv")
115 johnpye 966
116     def testsunpos1(self):
117     self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
118    
119     def testsunpos2(self):
120     self._run('example_1_6_2',"QRSlv","johnpye/sunpos.a4c")
121    
122     def testsunpos3(self):
123     self._run('example_1_7_1',"QRSlv","johnpye/sunpos.a4c")
124    
125     def testsunpos4(self):
126     self._run('example_1_7_2',"QRSlv","johnpye/sunpos.a4c")
127    
128     def testsunpos5(self):
129     self._run('example_1_7_3',"QRSlv","johnpye/sunpos.a4c")
130    
131     def testsunpos6(self):
132     self._run('example_1_8_1',"QRSlv","johnpye/sunpos.a4c")
133    
134 johnpye 1073 def testinstanceas(self):
135     M = self._run('example_1_6_1',"QRSlv","johnpye/sunpos.a4c")
136     self.assertAlmostEqual( float(M.t_solar), M.t_solar.as("s"))
137     self.assertAlmostEqual( float(M.t_solar)/3600, M.t_solar.as("h"))
138    
139 johnpye 1257
140     class TestCMSlv(AscendSelfTester):
141     def testheatex(self):
142     self._run('heatex',"CMSlv","heatex.a4c")
143     def testphaseeq(self):
144     self._run('phaseq',"CMSlv","phaseq.a4c")
145     def testpipeline(self):
146     self._run('pipeline',"CMSlv","pipeline.a4c"
147     ,{'infinity':3.2e9}
148     )
149     def testrachford(self):
150     self._run('rachford',"CMSlv","rachford.a4c")
151     def testlinmassbal(self):
152     self._run('linmassbal',"CMSlv","linmassbal.a4c")
153    
154    
155 johnpye 1126 class TestMatrix(AscendSelfTester):
156     def testlog10(self):
157     M = self._run('testlog10')
158     print M.getMatrix().write(sys.stderr,"mmio")
159    
160    
161 johnpye 966 class TestIntegrator(Ascend):
162    
163 johnpye 941 def testListIntegrators(self):
164     I = ascpy.Integrator.getEngines()
165     s1 = sorted([str(i) for i in I.values()])
166 johnpye 972 s2 = sorted(['IDA','LSODE','AWW'])
167 johnpye 941 assert s1==s2
168    
169 johnpye 942 # this routine is reused by both testIDA and testLSODE
170 johnpye 941 def _testIntegrator(self,integratorname):
171 johnpye 940 self.L.load('johnpye/shm.a4c')
172     M = self.L.findType('shm').getSimulation('sim')
173 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
174 johnpye 1133 P = M.getParameters()
175     M.setParameter('feastol',1e-12)
176 johnpye 979 print M.getChildren()
177     assert float(M.x) == 10.0
178     assert float(M.v) == 0.0
179 johnpye 941 t_end = math.pi
180 johnpye 940
181     I = ascpy.Integrator(M)
182     I.setReporter(ascpy.IntegratorReporterNull(I))
183 johnpye 941 I.setEngine(integratorname);
184 johnpye 940 I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
185 johnpye 1133 I.setMinSubStep(0.0001); # these limits are required by IDA at present (numeric diff)
186     I.setMaxSubStep(0.1);
187 johnpye 941 I.setInitialSubStep(0.001);
188     I.setMaxSubSteps(200);
189 johnpye 944 if(integratorname=='IDA'):
190     I.setParameter('autodiff',False)
191 johnpye 1133 for p in M.getParameters():
192     print p.getName(),"=",p.getValue()
193 johnpye 940 I.analyse();
194     I.solve();
195 johnpye 941 print "At end of simulation,"
196 johnpye 979 print "x = %f" % M.x
197     print "v = %f" % M.v
198     assert abs(float(M.x) + 10) < 1e-2
199     assert abs(float(M.v)) < 1e-2
200 johnpye 940 assert I.getNumObservedVars() == 3
201    
202 johnpye 941 def testInvalidIntegrator(self):
203 johnpye 966 self.L.load('johnpye/shm.a4c')
204 johnpye 941 M = self.L.findType('shm').getSimulation('sim')
205 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
206 johnpye 941 I = ascpy.Integrator(M)
207     try:
208     I.setEngine('___NONEXISTENT____')
209 johnpye 972 except RuntimeError:
210 johnpye 941 return
211     self.fail("setEngine did not raise error!")
212    
213     def testLSODE(self):
214     self._testIntegrator('LSODE')
215    
216 johnpye 972 def testIDA(self):
217     self._testIntegrator('IDA')
218    
219 johnpye 1016 def testparameters(self):
220     self.L.load('johnpye/shm.a4c')
221     M = self.L.findType('shm').getSimulation('sim')
222     M.build()
223     I = ascpy.Integrator(M)
224     I.setEngine('IDA')
225     P = I.getParameters()
226     for p in P:
227     print p.getName(),"=",p.getValue()
228     assert len(P)==11
229     assert P[0].isStr()
230     assert P[0].getName()=="linsolver"
231     assert P[0].getValue()=='SPGMR'
232     assert P[2].getName()=="autodiff"
233     assert P[2].getValue()==True
234     assert P[7].getName()=="atolvect"
235     assert P[7].getBoolValue() == True
236     P[2].setBoolValue(False)
237     assert P[2].getBoolValue()==False
238     I.setParameters(P)
239     assert I.getParameterValue('autodiff')==False
240     I.setParameter('autodiff',True)
241     try:
242     v = I.getParameterValue('nonexist')
243     except KeyError:
244     pass
245     else:
246     self.fail('Failed to trip invalid Integrator parameter')
247    
248 johnpye 972 class TestLSODE(Ascend):
249    
250 johnpye 964 def testzill(self):
251     self.L.load('johnpye/zill.a4c')
252     T = self.L.findType('zill')
253     M = T.getSimulation('sim')
254 johnpye 972 M.setSolver(ascpy.Solver('QRSlv'))
255 johnpye 964 I = ascpy.Integrator(M)
256 johnpye 966 I.setEngine('LSODE')
257     I.setMinSubStep(1e-7)
258     I.setMaxSubStep(0.001)
259     I.setMaxSubSteps(10000)
260 johnpye 964 I.setReporter(ascpy.IntegratorReporterConsole(I))
261 johnpye 1017 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
262 johnpye 964 I.analyse()
263     I.solve()
264     M.run(T.getMethod('self_test'))
265    
266 johnpye 962 def testnewton(self):
267 johnpye 973 sys.stderr.write("STARTING TESTNEWTON\n")
268 johnpye 962 self.L.load('johnpye/newton.a4c')
269     T = self.L.findType('newton')
270     M = T.getSimulation('sim')
271     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
272     I = ascpy.Integrator(M)
273     I.setEngine('LSODE')
274 johnpye 963 I.setParameter('rtolvect',False)
275     I.setParameter('rtol',1e-7)
276     I.setParameter('atolvect',False)
277     I.setParameter('atol',1e-7)
278     I.setMinSubStep(1e-7)
279     I.setMaxSubStep(0.001)
280     I.setMaxSubSteps(10000)
281    
282 johnpye 962 I.setReporter(ascpy.IntegratorReporterConsole(I))
283 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
284 johnpye 962 I.analyse()
285     I.solve()
286     print "At end of simulation,"
287 johnpye 979 print "x = %f" % M.x
288     print "v = %f" % M.v
289 johnpye 962 M.run(T.getMethod('self_test'))
290    
291 johnpye 961 def testlotka(self):
292     self.L.load('johnpye/lotka.a4c')
293     M = self.L.findType('lotka').getSimulation('sim')
294 johnpye 980 M.setSolver(ascpy.Solver("QRSlv"))
295 johnpye 961 I = ascpy.Integrator(M)
296     I.setEngine('LSODE')
297     I.setReporter(ascpy.IntegratorReporterConsole(I))
298 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
299 johnpye 961 I.analyse()
300 johnpye 979 print "Number of vars = %d" % I.getNumVars()
301     assert I.getNumVars()==2
302 johnpye 961 I.solve()
303     assert I.getNumObservedVars() == 3;
304 johnpye 979 assert abs(M.R - 832) < 1.0
305     assert abs(M.F - 21.36) < 0.1
306 johnpye 1017
307     #-------------------------------------------------------------------------------
308 johnpye 1032 # Testing of a external blackbox functions
309 johnpye 1021
310 johnpye 1032 class TestBlackBox(AscendSelfTester):
311     def testparsefail0(self):
312     try:
313     self.L.load('test/blackbox/parsefail0.a4c')
314     self.fail("parsefail0 should not have loaded without errors")
315     except:
316     pass
317    
318     def testparsefail1(self):
319     try:
320     self.L.load('test/blackbox/parsefail1.a4c')
321     self.fail("parsefail1 should not have loaded without errors")
322     except:
323     pass
324    
325     def testparsefail2(self):
326     try:
327     self.L.load('test/blackbox/parsefail2.a4c')
328     self.fail("parsefail2 should not have loaded without errors")
329     except:
330     pass
331    
332     def testparsefail3(self):
333     try:
334     self.L.load('test/blackbox/parsefail3.a4c')
335     self.fail("parsefail3 should not have loaded without errors")
336     except:
337     pass
338    
339     def testparsefail4(self):
340     try:
341     self.L.load('test/blackbox/parsefail4.a4c')
342     self.fail("parsefail4 should not have loaded")
343     except:
344     pass
345    
346     def testfail1(self):
347     """Mismatched arg counts check-- tests bbox, not ascend."""
348     self.L.load('test/blackbox/fail1.a4c')
349 johnpye 1034 try:
350 johnpye 1035 M = self.L.findType('fail1').getSimulation('sim')
351 johnpye 1034 self.fail("expected exception was not raised")
352     except RuntimeError,e:
353     print "Caught exception '%s', assumed ok" % e
354 johnpye 1032
355     def testfail2(self):
356     """Incorrect data arg check -- tests bbox, not ascend"""
357     self.L.load('test/blackbox/fail2.a4c')
358 johnpye 1035 try:
359     M = self.L.findType('fail2').getSimulation('sim')
360     self.fail("expected exception was not raised")
361     except RuntimeError,e:
362     print "Caught exception '%s', assumed ok (should mention errors during instantiation)" % e
363 johnpye 1032
364     def testpass1(self):
365     """simple single bbox forward solve"""
366     M = self._run('pass1',filename='test/blackbox/pass.a4c')
367    
368     def testpass2(self):
369     """simple single bbox reverse solve"""
370     M = self._run('pass2',filename='test/blackbox/pass.a4c')
371    
372     def testpass3(self):
373     """simple double bbox solve"""
374     M = self._run('pass3',filename='test/blackbox/pass3.a4c')
375    
376     def testpass4(self):
377     """simple double bbox reverse solve"""
378     M = self._run('pass4',filename='test/blackbox/pass3.a4c')
379    
380     def testpass5(self):
381     M = self._run('pass5',filename='test/blackbox/pass5.a4c')
382    
383     def testpass6(self):
384     M = self._run('pass6',filename='test/blackbox/pass5.a4c')
385    
386     def testpass7(self):
387     M = self._run('pass7',filename='test/blackbox/passmerge.a4c')
388    
389     def testpass8(self):
390     M = self._run('pass8',filename='test/blackbox/passmerge.a4c')
391    
392     def testpass9(self):
393     M = self._run('pass9',filename='test/blackbox/passmerge.a4c')
394    
395     def testpass10(self):
396     M = self._run('pass10',filename='test/blackbox/passmerge.a4c')
397    
398     def testpass11(self):
399     M = self._run('pass11',filename='test/blackbox/passmerge.a4c')
400    
401     def testpass12(self):
402     M = self._run('pass12',filename='test/blackbox/passmerge.a4c')
403    
404 johnpye 1037 # this test doesn't work: 'system is inconsistent' -- and structurally singular
405     # def testpass13(self):
406     # """cross-merged input/output solve"""
407     # M = self._run('pass13',filename='test/blackbox/passmerge.a4c')
408 johnpye 1032
409     def testpass14(self):
410     """cross-merged input/output reverse solve"""
411     M = self._run('pass14',filename='test/blackbox/passmerge.a4c')
412    
413     def testpass20(self):
414     M = self._run('pass20',filename='test/blackbox/passarray.a4c')
415    
416     def testparsefail21(self):
417     """dense array of black boxes wrong syntax"""
418     try:
419     self.L.load('test/blackbox/parsefail21.a4c')
420     self.fail("parsefail21 should not have loaded without errors")
421     except:
422     pass
423    
424     def testpass22(self):
425     M = self._run('pass22',filename='test/blackbox/passarray.a4c')
426    
427     def testpass23(self):
428     M = self._run('pass23',filename='test/blackbox/passarray.a4c')
429    
430     def testpass61(self):
431     M = self._run('pass61',filename='test/blackbox/reinstantiate.a4c')
432    
433     def testpass62(self):
434     M = self._run('pass62',filename='test/blackbox/reinstantiate.a4c')
435    
436     def testpass64(self):
437     M = self._run('pass64',filename='test/blackbox/reinstantiate.a4c')
438    
439     def testpass65(self):
440     M = self._run('pass65',filename='test/blackbox/reinstantiate.a4c')
441    
442     def testpass66(self):
443     M = self._run('pass66',filename='test/blackbox/reinstantiate.a4c')
444    
445     def testpass67(self):
446     M = self._run('pass67',filename='test/blackbox/reinstantiate.a4c')
447    
448 johnpye 1024 class TestExtFn(AscendSelfTester):
449 johnpye 1021 def testextfntest(self):
450 johnpye 1024 M = self._run('extfntest',filename='johnpye/extfn/extfntest.a4c')
451     self.assertAlmostEqual(M.y, 2);
452     self.assertAlmostEqual(M.x, 1);
453 johnpye 1021 self.assertAlmostEqual(M.y, M.x + 1);
454    
455 johnpye 1039 def testextrelfor(self):
456     M = self._run('extrelfor',filename='johnpye/extfn/extrelfor.a4c')
457 johnpye 1024
458 johnpye 1056 ## @TODO fix bug with badly-named bbox rel in a loop (Ben, maybe)
459     # def testextrelforbadnaming(self):
460     # self.L.load('johnpye/extfn/extrelforbadnaming.a4c')
461     # T = self.L.findType('extrelfor')
462     # M = T.getSimulation('sim')
463     # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
464     # print "x[1] = %f" % M.x[1]
465     # print "x[2] = %f" % M.x[2]
466     # print "x[3] = %f" % M.x[3]
467     # print "x[4] = %f" % M.x[4]
468     # print "x[5] = %f" % M.x[5]
469     # M.run(T.getMethod('self_test'))
470 johnpye 1039
471 johnpye 1024 def testextrelrepeat(self):
472     M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')
473    
474 johnpye 1021 #-------------------------------------------------------------------------------
475 johnpye 1162 # Testing of Sensitivity module
476    
477     class TestSensitivity(AscendSelfTester):
478     def test1(self):
479     self.L.load('sensitivity_test.a4c')
480     T = self.L.findType('sensitivity_test')
481     M = T.getSimulation('sim',False)
482     M.run(T.getMethod('on_load'))
483     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
484     M.run(T.getMethod('analyse'))
485 johnpye 1163 M.run(T.getMethod('self_test'))
486 johnpye 1167
487 johnpye 1168 # def testall(self):
488     # self.L.load('sensitivity_test.a4c')
489     # T = self.L.findType('sensitivity_test_all')
490     # M = T.getSimulation('sim',False)
491     # M.run(T.getMethod('on_load'))
492     # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
493     # M.run(T.getMethod('analyse'))
494     # M.run(T.getMethod('self_test'))
495     # CAUSES CRASH
496 johnpye 1162
497     #-------------------------------------------------------------------------------
498 johnpye 1024 # Testing of a ExtPy - external python methods
499    
500     class TestExtPy(AscendSelfTester):
501 johnpye 1055 def test1(self):
502     self.L.load('johnpye/extpy/extpytest.a4c')
503     T = self.L.findType('extpytest')
504     M = T.getSimulation('sim')
505     M.run(T.getMethod('self_test'))
506    
507     def test2(self):
508     self.L.load('johnpye/extpy/extpytest.a4c')
509     T = self.L.findType('extpytest')
510     M = T.getSimulation('sim')
511     M.run(T.getMethod('pythonthing'))
512     M.run(T.getMethod('pythonthing'))
513     M.run(T.getMethod('pythonthing'))
514     M.run(T.getMethod('pythonthing'))
515 johnpye 1205 M.run(T.getMethod('pythonthing'))
516     M.run(T.getMethod('pythonthing'))
517     M.run(T.getMethod('pythonthing'))
518 johnpye 1055 # causes crash!
519 johnpye 1024
520     #-------------------------------------------------------------------------------
521 johnpye 1042 # Testing of saturated steam properties library (iapwssatprops.a4c)
522    
523     class TestSteam(AscendSelfTester):
524 johnpye 1161
525 johnpye 1042 def testiapwssatprops1(self):
526     M = self._run('testiapwssatprops1',filename='steam/iapwssatprops.a4c')
527     def testiapwssatprops2(self):
528     M = self._run('testiapwssatprops2',filename='steam/iapwssatprops.a4c')
529     def testiapwssatprops3(self):
530     M = self._run('testiapwssatprops3',filename='steam/iapwssatprops.a4c')
531    
532 johnpye 1161 # test the stream model basically works
533 johnpye 1043 def testsatsteamstream(self):
534     M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')
535 johnpye 1042
536 johnpye 1161 # test that we can solve in terms of various (rho,u)
537     def testsatuv(self):
538     self.L.load('steam/iapwssat.a4c')
539     T = self.L.findType('testiapwssatuv')
540     M = T.getSimulation('sim',False)
541     M.run(T.getMethod('on_load'))
542     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
543     print "p = %f bar" % M.p.as('bar');
544     print "T = %f C" % (M.T.as('K') - 273.15);
545     print "x = %f" % M.x;
546     M.run(T.getMethod('self_test'))
547     M.run(T.getMethod('values2'))
548     # M.v.setRealValueWithUnits(1.0/450,"m^3/kg");
549     # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
550     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
551     print "p = %f bar" % M.p.as('bar');
552     print "T = %f C" % (M.T.as('K') - 273.15);
553     print "x = %f" % M.x;
554     M.run(T.getMethod('self_test2'))
555    
556    
557 johnpye 1056 ## @TODO fix error capture from bounds checking during initialisation
558     # def testiapwssat1(self):
559     # M = self._run('testiapwssat1',filename='steam/iapwssat.a4c')
560 johnpye 1042
561 johnpye 1100 def testdsgsat(self):
562 johnpye 1196 self.L.load('steam/dsgsat3.a4c')
563     T = self.L.findType('dsgsat3')
564 johnpye 1100 M = T.getSimulation('sim',False)
565 johnpye 1127 M.run(T.getMethod('on_load'))
566 johnpye 1100 M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
567 johnpye 1152 self.assertAlmostEqual(M.dTw_dt[2],0.0);
568 johnpye 1127 M.run(T.getMethod('configure_dynamic'))
569     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
570 johnpye 1132 return M
571    
572 johnpye 1270 def testdsgsatrepeat(self):
573     self.L.load('steam/dsgsat3.a4c')
574     T = self.L.findType('dsgsat3')
575     M = T.getSimulation('sim',False)
576     M.run(T.getMethod('on_load'))
577     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
578     M.run(T.getMethod('on_load'))
579     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
580     M.run(T.getMethod('on_load'))
581     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
582    
583 johnpye 1273 def testvary(self):
584     self.L.load('steam/dsgsat3.a4c')
585     T = self.L.findType('dsgsat3')
586     M = T.getSimulation('sim',False)
587     M.run(T.getMethod('on_load'))
588     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
589     print "----- setting qdot_s -----"
590     M.qdot_s.setRealValueWithUnits(1000,"W/m")
591     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
592     print "----- setting qdot_s -----"
593     M.qdot_s.setRealValueWithUnits(2000,"W/m")
594     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
595    
596 johnpye 1265 def teststeadylsode(self):
597     "test that steady conditions are stable with LSODE"
598 johnpye 1132 M = self.testdsgsat()
599 johnpye 1265 #M.qdot_s.setRealValueWithUnits(1000,"W/m")
600     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
601     #M.setParameter('
602     I = ascpy.Integrator(M)
603     I.setEngine('LSODE')
604     I.setReporter(ascpy.IntegratorReporterConsole(I))
605     I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 1)
606     I.analyse()
607     I.solve()
608    
609 johnpye 1267 # DOESN'T CONVERGE
610     # def testpeturblsode(self):
611     # "test that steady conditions are stable with LSODE"
612     # M = self.testdsgsat()
613     # # here is the peturbation...
614     # M.qdot_s.setRealValueWithUnits(1000,"W/m")
615     # M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
616     # I = ascpy.Integrator(M)
617     # I.setEngine('LSODE')
618     # I.setReporter(ascpy.IntegratorReporterConsole(I))
619     # I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 1)
620     # I.analyse()
621     # I.solve()
622 johnpye 1132
623 johnpye 1265 def teststeadyida(self):
624 johnpye 1137 M = self.testdsgsat()
625 johnpye 1152 self.assertAlmostEqual(M.dTw_dt[2],0.0)
626     Tw1 = float(M.T_w[2])
627 johnpye 1196 T = self.L.findType('dsgsat3')
628 johnpye 1137 M.run(T.getMethod('free_states'))
629 johnpye 1100 I = ascpy.Integrator(M)
630 johnpye 1132 I.setEngine('IDA')
631     I.setParameter('linsolver','DENSE')
632     I.setParameter('safeeval',True)
633 johnpye 1265 I.setParameter('rtol',1e-5)
634 johnpye 1132 I.setInitialSubStep(0.01)
635     I.setMaxSubSteps(100)
636 johnpye 1100 I.setReporter(ascpy.IntegratorReporterConsole(I))
637 johnpye 1266 I.setLinearTimesteps(ascpy.Units("s"), 0, 3600, 5)
638 johnpye 1273 I.analyse()
639 johnpye 1100 I.solve()
640 johnpye 1152 self.assertAlmostEqual(float(M.T_w[2]),Tw1)
641 johnpye 1127 M.qdot_s.setRealValueWithUnits(1000,"W/m")
642     self.assertAlmostEqual(M.qdot_s.as("W/m"),1000)
643     M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
644 johnpye 1266 print "dTw/dt = %f" % M.dTw_dt[2]
645 johnpye 1152 self.assertNotAlmostEqual(M.dTw_dt[2],0.0)
646 johnpye 1265
647 johnpye 1266 def testpeturbida(self):
648     M = self.testdsgsat()
649     self.assertAlmostEqual(M.dTw_dt[2],0.0)
650     T = self.L.findType('dsgsat3')
651     M.run(T.getMethod('free_states'))
652     # here is the peturbation...
653 johnpye 1277 M.qdot_s.setRealValueWithUnits(6000,"W/m")
654 johnpye 1267 # IDA has its own initial conditions solver, so no need to call QRSlv here
655 johnpye 1266 I = ascpy.Integrator(M)
656     I.setEngine('IDA')
657     I.setParameter('linsolver','DENSE')
658     I.setParameter('safeeval',True)
659     I.setParameter('rtol',1e-5)
660 johnpye 1267 I.setParameter('atolvect',False)
661 johnpye 1270 I.setParameter('atol',1e-5)
662 johnpye 1273 I.setInitialSubStep(0.1)
663 johnpye 1266 I.setReporter(ascpy.IntegratorReporterConsole(I))
664 johnpye 1278 I.setLogTimesteps(ascpy.Units("s"), 0.01, 3600, 20)
665 johnpye 1266 I.analyse()
666     I.solve()
667    
668 johnpye 1042 #-------------------------------------------------------------------------------
669 johnpye 1017 # Testing of freesteam external steam properties functions
670    
671 johnpye 1032 with_freesteam = True
672 johnpye 1017 try:
673 johnpye 1039 # we assume that if the freesteam python module is installed, the ASCEND
674     # external library will also be.
675 johnpye 1017 import freesteam
676     have_freesteam = True
677 johnpye 1018 except ImportError,e:
678 johnpye 1017 have_freesteam = False
679    
680 johnpye 1024 if with_freesteam and have_freesteam:
681 johnpye 1039 class TestFreesteam(AscendSelfTester):
682 johnpye 1119 # def testfreesteamtest(self):
683     # """run the self-test cases bundled with freesteam"""
684     # self._run('testfreesteam',filename='testfreesteam.a4c')
685 johnpye 1039
686 johnpye 1021 def testload(self):
687 johnpye 1039 """check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)"""
688 johnpye 1017 self.L.load('johnpye/thermalequilibrium2.a4c')
689 johnpye 1018
690 johnpye 1021 def testinstantiate(self):
691 johnpye 1039 """load an instantiate 'thermalequilibrium2'"""
692 johnpye 1021 self.testload()
693 johnpye 1018 M = self.L.findType('thermalequilibrium2').getSimulation('sim')
694 johnpye 1039 return M
695 johnpye 1021
696 johnpye 1039 def testintegrate(self):
697     """integrate transfer of heat from one mass of water/steam to another
698     according to Newton's law of cooling"""
699     M = self.testinstantiate()
700 johnpye 1018 M.setSolver(ascpy.Solver("QRSlv"))
701 johnpye 1039 I = ascpy.Integrator(M)
702     I.setEngine('LSODE')
703     I.setReporter(ascpy.IntegratorReporterConsole(I))
704     I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
705 johnpye 1055 I.setMinSubStep(0.01)
706     I.setInitialSubStep(1)
707 johnpye 1039 I.analyse()
708     print "Number of vars = %d" % I.getNumVars()
709     assert I.getNumVars()==2
710     I.solve()
711     assert I.getNumObservedVars() == 3;
712     print "S[1].T = %f K" % M.S[1].T
713     print "S[2].T = %f K" % M.S[2].T
714     print "Q = %f W" % M.Q
715 johnpye 1119 self.assertAlmostEqual(float(M.S[1].T),506.77225109,4);
716 johnpye 1056 self.assertAlmostEqual(float(M.S[2].T),511.605173967,5);
717     self.assertAlmostEqual(float(M.Q),-48.32922877329,3);
718 johnpye 1039 self.assertAlmostEqual(float(M.t),3000);
719     print "Note that the above values have not been verified analytically"
720 johnpye 1017
721 johnpye 1170 def testcollapsingcan2(self):
722     """ solve the collapsing can model using IAPWS-IF97 steam props """
723     M = self._run("collapsingcan2",filename="collapsingcan2.a4c");
724 johnpye 1121
725 johnpye 1017 #-------------------------------------------------------------------------------
726 johnpye 1199 # Testing of IDA's analysis module
727    
728     class TestIDA(Ascend):
729 johnpye 1200 def _run(self,filen,modeln=""):
730     self.L.load('test/ida/%s.a4c' % filen)
731     T = self.L.findType('%s%s' % (filen,modeln))
732 johnpye 1199 M = T.getSimulation('sim')
733     M.build()
734     I = ascpy.Integrator(M)
735     I.setEngine('IDA')
736     I.analyse()
737 johnpye 1246 return M;
738 johnpye 1199
739 johnpye 1200 def _runfail(self,filen,n,msg="failed"):
740     try:
741     self._run(filen,'fail%d' % n)
742     except Exception,e:
743     print "(EXPECTED) ERROR: %s" % e
744     return
745     self.fail(msg)
746    
747 johnpye 1199 def testsinglederiv(self):
748 johnpye 1200 self._run('singlederiv')
749 johnpye 1199
750     def testsinglederivfail1(self):
751 johnpye 1200 self._runfail('singlederiv',1
752     ,"t.ode_id=-1 did not trigger error")
753    
754 johnpye 1199 def testsinglederivfail2(self):
755 johnpye 1200 self._runfail('singlederiv',2
756     ,"dy_dt.ode_id=2 did not trigger error")
757 johnpye 1199
758     def testsinglederivfail3(self):
759 johnpye 1200 self._runfail('singlederiv',3
760     ,"dy_dt.ode_type=3 did not trigger error")
761 johnpye 1199
762     def testsinglederivfail4(self):
763 johnpye 1200 self._runfail('singlederiv',4
764     ,"duplicate ode_type=1 did not trigger error")
765 johnpye 1199
766     def testsinglederivfail5(self):
767 johnpye 1200 self._runfail('singlederiv',5
768     ,"duplicate ode_type=1 did not trigger error")
769 johnpye 1199
770     def testsinglederivfail6(self):
771 johnpye 1200 self._runfail('singlederiv',6
772     ,"duplicate ode_type=1 did not trigger error")
773 johnpye 1199
774 johnpye 1200 def testtwoderiv(self):
775     self._run('twoderiv')
776    
777     def testtwoderivfail1(self):
778     self._runfail('twoderiv',1)
779    
780     def testtwoderivfail2(self):
781     self._runfail('twoderiv',2)
782    
783     def testtwoderivfail3(self):
784     self._runfail('twoderiv',3)
785     def testtwoderivfail4(self):
786     self._runfail('twoderiv',4)
787     def testtwoderivfail5(self):
788     self._runfail('twoderiv',5)
789    
790 johnpye 1201 def testnoderivs(self):
791     self._runfail('noderivs',1)
792    
793     def testnoindeps(self):
794     self._runfail('indeps',1)
795    
796     def testtwoindeps(self):
797     self._runfail('indeps',2)
798    
799     def testfixedvars(self):
800     self._run('fixedvars')
801    
802     def testfixedvars1(self):
803     self._run('fixedvars',1)
804    
805 johnpye 1246 def testfixedvars2(self):
806     self._run('fixedvars',2)
807 johnpye 1201
808 johnpye 1246 def testfixedvars3(self):
809     self._run('fixedvars',3)
810 johnpye 1201
811 johnpye 1213 def testincidence(self):
812     self._run('incidence')
813 johnpye 1201
814 johnpye 1214 def testincidence1(self):
815     self._run('incidence',1)
816     def testincidence2(self):
817     self._run('incidence',2)
818     def testincidence3(self):
819 johnpye 1246 M = self._run('incidence',3)
820    
821 johnpye 1214 def testincidence4(self):
822     self._run('incidence',4)
823     def testincidencefail5(self):
824     self._runfail('incidence',5)
825    
826     # doesn't work yet:
827     # def testincidence5(self):
828     # self._run('incidence',5)
829    
830    
831 johnpye 1199 #-------------------------------------------------------------------------------
832 johnpye 1017 # Testing of IDA models using DENSE linear solver
833    
834 johnpye 1016 class TestIDADENSE(Ascend):
835 johnpye 1017 """IDA DAE integrator, DENSE linear solver"""
836 johnpye 961
837 johnpye 1042 def testlotka(self):
838 johnpye 991 self.L.load('johnpye/lotka.a4c')
839     M = self.L.findType('lotka').getSimulation('sim')
840     M.setSolver(ascpy.Solver("QRSlv"))
841     I = ascpy.Integrator(M)
842     I.setEngine('IDA')
843     I.setReporter(ascpy.IntegratorReporterConsole(I))
844     I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5);
845     I.setParameter('linsolver','DENSE')
846     I.setParameter('rtol',1e-8);
847     I.analyse()
848     assert I.getNumVars()==2
849     assert abs(M.R - 1000) < 1e-300
850     I.solve()
851 johnpye 1017 assert I.getNumObservedVars() == 3
852 johnpye 991 assert abs(M.R - 832) < 1.0
853     assert abs(M.F - 21.36) < 0.1
854 johnpye 972
855 johnpye 975 def testdenx(self):
856 johnpye 1026 print "-----------------------------====="
857 johnpye 942 self.L.load('johnpye/idadenx.a4c')
858     M = self.L.findType('idadenx').getSimulation('sim')
859 johnpye 1017 M.setSolver(ascpy.Solver("QRSlv"))
860 johnpye 942 I = ascpy.Integrator(M)
861     I.setEngine('IDA')
862 johnpye 1228 I.setParameter('calcic','YA_YDP')
863 johnpye 972 I.setParameter('linsolver','DENSE')
864 johnpye 1237 I.setParameter('safeeval',True)
865 johnpye 944 I.setReporter(ascpy.IntegratorReporterConsole(I))
866 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
867 johnpye 950 I.setMaxSubStep(0);
868 johnpye 1017 I.setInitialSubStep(0)
869 johnpye 1228 I.setMaxSubSteps(0)
870 johnpye 944 I.setParameter('autodiff',True)
871     I.analyse()
872     I.solve()
873 johnpye 1022 assert abs(float(M.y1) - 5.1091e-08) < 2e-9
874     assert abs(float(M.y2) - 2.0437e-13) < 2e-14
875 johnpye 1017 assert abs(float(M.y3) - 1.0) < 1e-5
876 johnpye 942
877 johnpye 1247 def testhires(self):
878     self.L.load('test/hires.a4c')
879     T = self.L.findType('hires')
880     M = T.getSimulation('sim')
881     M.setSolver(ascpy.Solver('QRSlv'))
882     I = ascpy.Integrator(M)
883     I.setEngine('IDA')
884     I.setParameter('linsolver','DENSE')
885 johnpye 1251 I.setParameter('rtol',1.1e-15)
886 johnpye 1247 I.setParameter('atolvect',0)
887 johnpye 1251 I.setParameter('atol',1.1e-15)
888 johnpye 1247 I.setReporter(ascpy.IntegratorReporterConsole(I))
889     I.setLogTimesteps(ascpy.Units(""), 1, 321.8122, 5)
890 johnpye 1251 I.setInitialSubStep(1e-5)
891     I.setMaxSubSteps(10000)
892 johnpye 1247 I.analyse()
893     I.solve()
894 johnpye 1251 for i in range(8):
895     print "y[%d] = %.20g" % (i+1, M.y[i+1])
896 johnpye 1247 M.run(T.getMethod('self_test'))
897    
898 johnpye 1253 def testchemakzo(self):
899     self.L.load('test/chemakzo.a4c')
900     T = self.L.findType('chemakzo')
901 johnpye 1252 M = T.getSimulation('sim')
902     M.setSolver(ascpy.Solver('QRSlv'))
903     I = ascpy.Integrator(M)
904     I.setEngine('IDA')
905     I.setParameter('linsolver','DENSE')
906 johnpye 1253 I.setParameter('rtol',1e-15)
907 johnpye 1252 I.setParameter('atolvect',0)
908 johnpye 1253 I.setParameter('atol',1e-15)
909 johnpye 1252 I.setReporter(ascpy.IntegratorReporterConsole(I))
910 johnpye 1253 I.setLinearTimesteps(ascpy.Units("s"), 1, 180, 5)
911     I.setInitialSubStep(1e-13)
912 johnpye 1252 I.setMaxSubSteps(10000)
913     I.analyse()
914     I.solve()
915 johnpye 1255 for i in range(6):
916 johnpye 1254 print "y[%d] = %.20g" % (i+1, M.y[i+1])
917     M.run(T.getMethod('self_test'))
918    
919     def testtransamp(self):
920     self.L.load('test/transamp.a4c')
921     T = self.L.findType('transamp')
922     M = T.getSimulation('sim')
923     M.setSolver(ascpy.Solver('QRSlv'))
924     I = ascpy.Integrator(M)
925     I.setEngine('IDA')
926     I.setParameter('linsolver','DENSE')
927     I.setParameter('rtol',1e-7)
928     I.setParameter('atolvect',0)
929     I.setParameter('atol',1e-7)
930     I.setReporter(ascpy.IntegratorReporterConsole(I))
931     I.setLinearTimesteps(ascpy.Units("s"), 0.05, 0.2, 20)
932     I.setInitialSubStep(0.00001)
933     I.setMaxSubSteps(10000)
934     I.analyse()
935     I.solve()
936 johnpye 1253 for i in range(6):
937 johnpye 1252 print "y[%d] = %.20g" % (i+1, M.y[i+1])
938     M.run(T.getMethod('self_test'))
939    
940 johnpye 1253 # MODEL FAILS ANALYSIS: we need to add support for non-incident differential vars
941     # def testpollution(self):
942     # self.L.load('test/pollution.a4c')
943     # T = self.L.findType('pollution')
944     # M = T.getSimulation('sim')
945     # M.setSolver(ascpy.Solver('QRSlv'))
946     # I = ascpy.Integrator(M)
947     # I.setEngine('IDA')
948     # I.setParameter('linsolver','DENSE')
949     # I.setParameter('rtol',1.1e-15)
950     # I.setParameter('atolvect',0)
951     # I.setParameter('atol',1.1e-15)
952     # I.setReporter(ascpy.IntegratorReporterConsole(I))
953     # I.setLogTimesteps(ascpy.Units("s"), 1, 60, 5)
954     # I.setInitialSubStep(1e-5)
955     # I.setMaxSubSteps(10000)
956     # I.analyse()
957     # I.solve()
958     # for i in range(20):
959     # print "y[%d] = %.20g" % (i+1, M.y[i+1])
960     # M.run(T.getMethod('self_test'))
961    
962 johnpye 1058 ## @TODO fails during IDACalcIC (model too big?)
963     # def testkryx(self):
964     # self.L.load('johnpye/idakryx.a4c')
965     # ascpy.getCompiler().setUseRelationSharing(False)
966     # M = self.L.findType('idakryx').getSimulation('sim')
967     # M.setSolver(ascpy.Solver('QRSlv'))
968     # M.build()
969     # I = ascpy.Integrator(M)
970     # I.setEngine('IDA')
971     # I.setReporter(ascpy.IntegratorReporterConsole(I))
972     # I.setParameter('linsolver','DENSE')
973     # I.setParameter('maxl',8)
974     # I.setParameter('gsmodified',False)
975     # I.setParameter('autodiff',True)
976     # I.setParameter('rtol',0)
977     # I.setParameter('atol',1e-3);
978     # I.setParameter('atolvect',False)
979     # I.setParameter('calcic','YA_YDP')
980     # I.analyse()
981     # I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 11)
982     # I.solve()
983     # assert abs(M.u[2][2].getValue()) < 1e-5
984 johnpye 1017
985     #-------------------------------------------------------------------------------
986     # Testing of IDA models using SPGMR linear solver (Krylov)
987    
988 johnpye 1016 # these tests are disabled until SPGMR preconditioning has been implemented
989     class TestIDASPGMR:#(Ascend):
990     def testlotka(self):
991     self.L.load('johnpye/lotka.a4c')
992     M = self.L.findType('lotka').getSimulation('sim')
993     M.setSolver(ascpy.Solver("QRSlv"))
994 johnpye 951 I = ascpy.Integrator(M)
995     I.setEngine('IDA')
996     I.setReporter(ascpy.IntegratorReporterConsole(I))
997 johnpye 1017 I.setLinearTimesteps(ascpy.Units("s"), 0, 200, 5)
998     I.setParameter('rtol',1e-8)
999 johnpye 951 I.analyse()
1000 johnpye 1016 assert I.getNumVars()==2
1001     assert abs(M.R - 1000) < 1e-300
1002 johnpye 951 I.solve()
1003 johnpye 1017 assert I.getNumObservedVars() == 3
1004 johnpye 1016 assert abs(M.R - 832) < 1.0
1005     assert abs(M.F - 21.36) < 0.1
1006 johnpye 951
1007 johnpye 1016
1008 johnpye 991 def testkryx(self):
1009 johnpye 951 self.L.load('johnpye/idakryx.a4c')
1010     M = self.L.findType('idakryx').getSimulation('sim')
1011 johnpye 952 M.build()
1012 johnpye 951 I = ascpy.Integrator(M)
1013     I.setEngine('IDA')
1014     I.setReporter(ascpy.IntegratorReporterConsole(I))
1015 johnpye 992 I.setParameter('linsolver','SPGMR')
1016 johnpye 993 I.setParameter('prec','JACOBI')
1017 johnpye 970 I.setParameter('maxl',8)
1018 johnpye 952 I.setParameter('gsmodified',False)
1019     I.setParameter('autodiff',True)
1020 johnpye 993 I.setParameter('gsmodified',True)
1021 johnpye 952 I.setParameter('rtol',0)
1022     I.setParameter('atol',1e-3);
1023     I.setParameter('atolvect',False)
1024 johnpye 993 I.setParameter('calcic','Y')
1025 johnpye 952 I.analyse()
1026     I.setLogTimesteps(ascpy.Units("s"), 0.01, 10.24, 10);
1027 johnpye 1017 print M.udot[1][3]
1028 johnpye 952 I.solve()
1029     assert 0
1030 johnpye 967
1031 johnpye 1016 def testzill(self):
1032     self.L.load('johnpye/zill.a4c')
1033     T = self.L.findType('zill')
1034     M = T.getSimulation('sim')
1035     M.setSolver(ascpy.Solver('QRSlv'))
1036     I = ascpy.Integrator(M)
1037     I.setEngine('IDA')
1038     I.setParameter('safeeval',False)
1039     I.setMinSubStep(1e-7)
1040     I.setMaxSubStep(0.001)
1041     I.setMaxSubSteps(10000)
1042     I.setReporter(ascpy.IntegratorReporterConsole(I))
1043 johnpye 1017 I.setLinearTimesteps(ascpy.Units(), 1.0, 1.5, 5)
1044 johnpye 1016 I.analyse()
1045     I.solve()
1046     M.run(T.getMethod('self_test'))
1047    
1048     def testdenxSPGMR(self):
1049     self.L.load('johnpye/idadenx.a4c')
1050     M = self.L.findType('idadenx').getSimulation('sim')
1051     M.setSolver(ascpy.Solver('QRSlv'))
1052     I = ascpy.Integrator(M)
1053     I.setEngine('IDA')
1054     I.setReporter(ascpy.IntegratorReporterConsole(I))
1055 johnpye 1017 I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
1056 johnpye 1016 I.setMaxSubStep(0);
1057     I.setInitialSubStep(0);
1058     I.setMaxSubSteps(0);
1059     I.setParameter('autodiff',True)
1060     I.setParameter('linsolver','SPGMR')
1061     I.setParameter('gsmodified',False)
1062     I.setParameter('maxncf',10)
1063     I.analyse()
1064     I.solve()
1065 johnpye 1017 assert abs(float(M.y1) - 5.1091e-08) < 1e-10
1066     assert abs(float(M.y2) - 2.0437e-13) < 1e-15
1067     assert abs(float(M.y3) - 1.0) < 1e-5
1068 johnpye 1016
1069 johnpye 943 # move code above down here if you want to temporarily avoid testing it
1070 johnpye 932 class NotToBeTested:
1071     def nothing(self):
1072     pass
1073 johnpye 1016
1074 johnpye 1251 def testnewton(self):
1075     sys.stderr.write("STARTING TESTNEWTON\n")
1076     self.L.load('johnpye/newton.a4c')
1077     T = self.L.findType('newton')
1078     M = T.getSimulation('sim')
1079     M.solve(ascpy.Solver("QRSlv"),ascpy.SolverReporter())
1080     I = ascpy.Integrator(M)
1081     I.setEngine('IDA')
1082     I.setParameter('linsolver','DENSE')
1083     I.setParameter('safeeval',True)
1084     I.setParameter('rtol',1e-8)
1085     I.setMaxSubStep(0.001)
1086     I.setMaxSubSteps(10000)
1087    
1088     I.setReporter(ascpy.IntegratorReporterConsole(I))
1089     I.setLinearTimesteps(ascpy.Units("s"), 0, 2*float(M.v)/float(M.g), 2)
1090     I.analyse()
1091     I.solve()
1092     print "At end of simulation,"
1093     print "x = %f" % M.x
1094     print "v = %f" % M.v
1095     M.run(T.getMethod('self_test'))
1096    
1097 johnpye 669 if __name__=='__main__':
1098 johnpye 1118 # a whole bag of tricks to make sure we get the necessary dirs in our ascend, python and ld path vars
1099 johnpye 1098 restart = 0
1100    
1101     if platform.system()=="Windows":
1102 johnpye 1120 LD_LIBRARY_PATH="PATH"
1103 johnpye 1098 SEP = ";"
1104     else:
1105     LD_LIBRARY_PATH="LD_LIBRARY_PATH"
1106     SEP = ":"
1107    
1108 johnpye 1119 freesteamdir = os.path.expanduser("~/freesteam/ascend")
1109     modeldirs = [os.path.abspath(os.path.join(sys.path[0],"models")),os.path.abspath(freesteamdir)]
1110 johnpye 1118 if not os.environ.get('ASCENDLIBRARY'):
1111 johnpye 1119 os.environ['ASCENDLIBRARY'] = SEP.join(modeldirs)
1112 johnpye 1118 restart = 1
1113     else:
1114     envmodelsdir = [os.path.abspath(i) for i in os.environ['ASCENDLIBRARY'].split(SEP)]
1115 johnpye 1119 for l in modeldirs:
1116     if l in envmodelsdir[len(modeldirs):]:
1117     envmodelsdir.remove(l)
1118     restart = 1
1119     for l in modeldirs:
1120     if l not in envmodelsdir:
1121     envmodelsdir.insert(0,l)
1122     restart = 1
1123     os.environ['ASCENDLIBRARY'] = SEP.join(envmodelsdir)
1124 johnpye 1118
1125 johnpye 1102 libdirs = ["pygtk","."]
1126 johnpye 1098 libdirs = [os.path.normpath(os.path.join(sys.path[0],l)) for l in libdirs]
1127     if not os.environ.get(LD_LIBRARY_PATH):
1128 johnpye 1105 os.environ[LD_LIBRARY_PATH]=SEP.join(libdirs)
1129 johnpye 1106 restart = 1
1130 johnpye 1098 else:
1131     envlibdirs = [os.path.normpath(i) for i in os.environ[LD_LIBRARY_PATH].split(SEP)]
1132     for l in libdirs:
1133 johnpye 1106 if l in envlibdirs[len(libdirs):]:
1134     envlibdirs.remove(l)
1135     restart = 1
1136     for l in libdirs:
1137 johnpye 1098 if l not in envlibdirs:
1138     envlibdirs.insert(0,l)
1139 johnpye 1106 restart = 1
1140 johnpye 1098 os.environ[LD_LIBRARY_PATH] = SEP.join(envlibdirs)
1141    
1142 johnpye 1102 pypath = os.path.normpath(os.path.join(sys.path[0],"pygtk"))
1143     if not os.environ.get('PYTHONPATH'):
1144     os.environ['PYTHONPATH']=pypath
1145     else:
1146     envpypath = os.environ['PYTHONPATH'].split(SEP)
1147     if pypath not in envpypath:
1148     envpypath.insert(0,pypath)
1149 johnpye 1137 os.environ['PYTHONPATH']=SEP.join(envpypath)
1150 johnpye 1102 restart = 1
1151    
1152 johnpye 1098 if restart:
1153 johnpye 1167 script = os.path.join(sys.path[0],"test.py")
1154 johnpye 1119 print "Restarting with..."
1155 johnpye 1194 print " export LD_LIBRARY_PATH=%s" % os.environ.get(LD_LIBRARY_PATH)
1156     print " export PYTHONPATH=%s" % os.environ.get('PYTHONPATH')
1157     print " export ASCENDLIBRARY=%s" % os.environ.get('ASCENDLIBRARY')
1158 johnpye 1098
1159 johnpye 1215 os.execvp("python",[script] + sys.argv)
1160 johnpye 1167
1161 johnpye 1098 import ascpy
1162    
1163     try:
1164     import cunit
1165     except:
1166     pass
1167    
1168 johnpye 966 atexit.register(ascpy.shutdown)
1169 johnpye 1008 #suite = unittest.TestSuite()
1170 johnpye 1003 #suite = unittest.defaultTestLoader.loadTestsFromName('__main__')
1171 johnpye 1008 #unittest.TextTestRunner(verbosity=2).run(suite)
1172     unittest.main()

Properties

Name Value
svn:executable *

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22