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

Diff of /trunk/test.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1018 by johnpye, Wed Jan 3 06:44:39 2007 UTC revision 1050 by johnpye, Sat Jan 6 04:04:47 2007 UTC
# Line 1  Line 1 
1  #!/usr/bin/env python  #!/usr/bin/env python
2  import unittest  import unittest
3    
4    import platform, sys
5    if platform.system() != "Windows":
6        import dl
7        sys.setdlopenflags(dl.RTLD_GLOBAL|dl.RTLD_NOW)
8    
9  import ascpy  import ascpy
10  import math  import math
11  import os, subprocess, sys  import os, subprocess
12  import atexit  import atexit
13  import cunit  import cunit
14    
# Line 16  class Ascend(unittest.TestCase): Line 22  class Ascend(unittest.TestCase):
22          self.L.clear()          self.L.clear()
23          del self.L          del self.L
24    
25    class AscendSelfTester(Ascend):
26    
27        def _run(self,modelname,solvername="QRSlv",filename=None):
28            if filename==None:
29                filename = 'johnpye/%s.a4c' % modelname
30            self.L.load(filename)
31            T = self.L.findType(modelname)
32            M = T.getSimulation('sim')
33            M.build()
34            M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())    
35            M.run(T.getMethod('self_test'))
36            return M
37    
38  class TestCompiler(Ascend):  class TestCompiler(Ascend):
39    
40      def testloading(self):      def testloading(self):
# Line 27  class TestCompiler(Ascend): Line 46  class TestCompiler(Ascend):
46      def testatomsa4l(self):      def testatomsa4l(self):
47          self.L.load('atoms.a4l')          self.L.load('atoms.a4l')
48    
49  class TestSolver(Ascend):  class TestSolver(AscendSelfTester):
50            
     def _run(self,modelname,solvername="QRSlv",filename=None):  
         if filename==None:  
             filename = 'johnpye/%s.a4c' % modelname  
         self.L.load(filename)  
         T = self.L.findType(modelname)  
         M = T.getSimulation('sim')  
         M.build()  
         M.solve(ascpy.Solver(solvername),ascpy.SolverReporter())      
         M.run(T.getMethod('self_test'))  
   
51      def testlog10(self):      def testlog10(self):
52          self._run('testlog10')          self._run('testlog10')
53    
# Line 209  class TestLSODE(Ascend): Line 218  class TestLSODE(Ascend):
218          assert abs(M.F - 21.36) < 0.1          assert abs(M.F - 21.36) < 0.1
219    
220  #-------------------------------------------------------------------------------  #-------------------------------------------------------------------------------
221    # Testing of a external blackbox functions
222    
223    class TestBlackBox(AscendSelfTester):
224        def testparsefail0(self):
225            try:
226                self.L.load('test/blackbox/parsefail0.a4c')
227                self.fail("parsefail0 should not have loaded without errors")
228            except:
229                pass
230    
231        def testparsefail1(self):
232            try:
233                self.L.load('test/blackbox/parsefail1.a4c')
234                self.fail("parsefail1 should not have loaded without errors")
235            except:
236                pass
237    
238        def testparsefail2(self):
239            try:
240                self.L.load('test/blackbox/parsefail2.a4c')
241                self.fail("parsefail2 should not have loaded without errors")
242            except:
243                pass
244    
245        def testparsefail3(self):
246            try:
247                self.L.load('test/blackbox/parsefail3.a4c')
248                self.fail("parsefail3 should not have loaded without errors")
249            except:
250                pass
251    
252        def testparsefail4(self):
253            try:
254                self.L.load('test/blackbox/parsefail4.a4c')
255                self.fail("parsefail4 should not have loaded")
256            except:
257                pass
258    
259        def testfail1(self):
260            """Mismatched arg counts check-- tests bbox, not ascend."""
261            self.L.load('test/blackbox/fail1.a4c')
262            try:
263                M = self.L.findType('fail1').getSimulation('sim')
264                self.fail("expected exception was not raised")
265            except RuntimeError,e:
266                print "Caught exception '%s', assumed ok" % e
267    
268        def testfail2(self):
269            """Incorrect data arg check -- tests bbox, not ascend"""
270            self.L.load('test/blackbox/fail2.a4c')
271            try:
272                M = self.L.findType('fail2').getSimulation('sim')
273                self.fail("expected exception was not raised")
274            except RuntimeError,e:
275                print "Caught exception '%s', assumed ok (should mention errors during instantiation)" % e
276    
277        def testpass1(self):
278            """simple single bbox forward solve"""
279            M = self._run('pass1',filename='test/blackbox/pass.a4c')
280    
281        def testpass2(self):
282            """simple single bbox reverse solve"""
283            M = self._run('pass2',filename='test/blackbox/pass.a4c')
284    
285        def testpass3(self):
286            """simple double bbox solve"""
287            M = self._run('pass3',filename='test/blackbox/pass3.a4c')
288    
289        def testpass4(self):
290            """simple double bbox reverse solve"""
291            M = self._run('pass4',filename='test/blackbox/pass3.a4c')
292    
293        def testpass5(self):
294            M = self._run('pass5',filename='test/blackbox/pass5.a4c')
295    
296        def testpass6(self):
297            M = self._run('pass6',filename='test/blackbox/pass5.a4c')
298    
299        def testpass7(self):
300            M = self._run('pass7',filename='test/blackbox/passmerge.a4c')
301    
302        def testpass8(self):
303            M = self._run('pass8',filename='test/blackbox/passmerge.a4c')
304    
305        def testpass9(self):
306            M = self._run('pass9',filename='test/blackbox/passmerge.a4c')
307    
308        def testpass10(self):
309            M = self._run('pass10',filename='test/blackbox/passmerge.a4c')
310    
311        def testpass11(self):
312            M = self._run('pass11',filename='test/blackbox/passmerge.a4c')
313    
314        def testpass12(self):
315            M = self._run('pass12',filename='test/blackbox/passmerge.a4c')
316    
317    # this test doesn't work: 'system is inconsistent' -- and structurally singular
318    #   def testpass13(self):
319    #       """cross-merged input/output solve"""
320    #       M = self._run('pass13',filename='test/blackbox/passmerge.a4c')
321    
322        def testpass14(self):
323            """cross-merged input/output reverse solve"""
324            M = self._run('pass14',filename='test/blackbox/passmerge.a4c')
325    
326        def testpass20(self):
327            M = self._run('pass20',filename='test/blackbox/passarray.a4c')
328    
329        def testparsefail21(self):
330            """dense array of black boxes wrong syntax"""
331            try:
332                self.L.load('test/blackbox/parsefail21.a4c')
333                self.fail("parsefail21 should not have loaded without errors")
334            except:
335                pass
336    
337        def testpass22(self):
338            M = self._run('pass22',filename='test/blackbox/passarray.a4c')
339    
340        def testpass23(self):
341            M = self._run('pass23',filename='test/blackbox/passarray.a4c')
342    
343        def testpass61(self):
344            M = self._run('pass61',filename='test/blackbox/reinstantiate.a4c')
345    
346        def testpass62(self):
347            M = self._run('pass62',filename='test/blackbox/reinstantiate.a4c')
348    
349        def testpass64(self):
350            M = self._run('pass64',filename='test/blackbox/reinstantiate.a4c')
351    
352        def testpass65(self):
353            M = self._run('pass65',filename='test/blackbox/reinstantiate.a4c')
354    
355        def testpass66(self):
356            M = self._run('pass66',filename='test/blackbox/reinstantiate.a4c')
357    
358        def testpass67(self):
359            M = self._run('pass67',filename='test/blackbox/reinstantiate.a4c')
360    
361    class TestExtFn(AscendSelfTester):
362        def testextfntest(self):
363            M = self._run('extfntest',filename='johnpye/extfn/extfntest.a4c')
364            self.assertAlmostEqual(M.y, 2);
365            self.assertAlmostEqual(M.x, 1);
366            self.assertAlmostEqual(M.y, M.x + 1);
367    
368        def testextrelfor(self):
369            M = self._run('extrelfor',filename='johnpye/extfn/extrelfor.a4c')
370    
371        def testextrelforbadnaming(self):
372            self.L.load('johnpye/extfn/extrelforbadnaming.a4c')
373            T = self.L.findType('extrelfor')
374            M = T.getSimulation('sim')
375            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
376            print "x[1] = %f" % M.x[1]
377            print "x[2] = %f" % M.x[2]
378            print "x[3] = %f" % M.x[3]
379            print "x[4] = %f" % M.x[4]
380            print "x[5] = %f" % M.x[5]
381            M.run(T.getMethod('self_test'))
382    
383        def testextrelrepeat(self):
384            M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')
385    
386    #-------------------------------------------------------------------------------
387    # Testing of a ExtPy - external python methods
388    
389    class TestExtPy(AscendSelfTester):
390        def testextpytest(self):
391            print "-------------------=--=-=-=-"
392            M = self._run('extpytest',filename='johnpye/extpy/extpytest.a4c')
393    
394    #-------------------------------------------------------------------------------
395    # Testing of saturated steam properties library (iapwssatprops.a4c)
396    
397    class TestSteam(AscendSelfTester):
398        def testiapwssatprops1(self):
399            M = self._run('testiapwssatprops1',filename='steam/iapwssatprops.a4c')
400        def testiapwssatprops2(self):
401            M = self._run('testiapwssatprops2',filename='steam/iapwssatprops.a4c')
402        def testiapwssatprops3(self):
403            M = self._run('testiapwssatprops3',filename='steam/iapwssatprops.a4c')
404        def testsatsteamstream(self):
405            M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')
406    
407        def testsatsteamstream(self):
408            M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')
409    
410        def testiapwssat1(self):
411            M = self._run('testiapwssat1',filename='steam/iapwssat.a4c')
412    
413        def testdsgsat(self):
414            self.L.load('steam/dsgsat2.a4c')
415            T = self.L.findType('dsgsat2')
416            M = T.getSimulation('sim')
417            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
418            M.run(T.getMethod('fixed_states'))
419            I = ascpy.Integrator(M)
420            I.setEngine('LSODE')
421            I.setReporter(ascpy.IntegratorReporterConsole(I))
422            I.setReporter(ascpy.IntegratorReporterConsole(I))
423            I.setLinearTimesteps(ascpy.Units("s"), 0, 10, 100)
424            I.setMinSubStep(0.01)
425            I.setInitialSubStep(0.1)
426            I.analyse()
427            I.solve()
428            #M.checkStructuralSingularity() causes crash!
429            
430    #-------------------------------------------------------------------------------
431  # Testing of freesteam external steam properties functions  # Testing of freesteam external steam properties functions
432    
433    with_freesteam = True
434  try:  try:
435        # we assume that if the freesteam python module is installed, the ASCEND
436        # external library will also be.
437      import freesteam      import freesteam
438      have_freesteam = True      have_freesteam = True
439  except ImportError,e:  except ImportError,e:
440      have_freesteam = False      have_freesteam = False
441    
442  if have_freesteam:  if with_freesteam and have_freesteam:
443      class TestFreesteam(Ascend):      class TestFreesteam(AscendSelfTester):
444          def testimport(self):          def testfreesteamtest(self):
445              self.L.load('johnpye/thermalequilibrium2.a4c')              """run the self-test cases bundled with freesteam"""
446                self._run('testfreesteam',filename='testfreesteam.a4c')
447    
448          def testevaluate(self):          def testload(self):
449                """check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)"""
450              self.L.load('johnpye/thermalequilibrium2.a4c')              self.L.load('johnpye/thermalequilibrium2.a4c')
451    
452            def testinstantiate(self):
453                """load an instantiate 'thermalequilibrium2'"""
454                self.testload()
455              M = self.L.findType('thermalequilibrium2').getSimulation('sim')              M = self.L.findType('thermalequilibrium2').getSimulation('sim')
456              M.setSolver(ascpy.Solver("QRSlv"))              return M
             #I = ascpy.Integrator(M)  
             #I.setEngine('LSODE')  
             #I.setReporter(ascpy.IntegratorReporterConsole(I))  
             #I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)  
             #I.setMinSubStep(0.01)  
             #I.setInitialSubStep(0.1)  
             #I.analyse()  
             #print "Number of vars = %d" % I.getNumVars()  
             #assert I.getNumVars()==2  
             #I.solve()  
             #assert I.getNumObservedVars() == 3;  
             #assert abs(M.R - 832) < 1.0  
             #assert abs(M.F - 21.36) < 0.1  
457    
458            def testintegrate(self):
459                """integrate transfer of heat from one mass of water/steam to another
460                according to Newton's law of cooling"""
461                M = self.testinstantiate()
462                M.setSolver(ascpy.Solver("QRSlv"))
463                I = ascpy.Integrator(M)
464                I.setEngine('LSODE')
465                I.setReporter(ascpy.IntegratorReporterConsole(I))
466                I.setLinearTimesteps(ascpy.Units("s"), 0, 3000, 30)
467                I.setMinSubStep(0.001)
468                I.setInitialSubStep(0.01)
469                I.analyse()
470                print "Number of vars = %d" % I.getNumVars()
471                assert I.getNumVars()==2
472                I.solve()
473                assert I.getNumObservedVars() == 3;
474                print "S[1].T = %f K" % M.S[1].T
475                print "S[2].T = %f K" % M.S[2].T
476                print "Q = %f W" % M.Q      
477                self.assertAlmostEqual(float(M.S[1].T),506.77225109);
478                self.assertAlmostEqual(float(M.S[2].T),511.605173967);
479                self.assertAlmostEqual(float(M.Q),-48.32922877329);
480                self.assertAlmostEqual(float(M.t),3000);
481                print "Note that the above values have not been verified analytically"
482    
483  #-------------------------------------------------------------------------------  #-------------------------------------------------------------------------------
484  # Testing of IDA models using DENSE linear solver  # Testing of IDA models using DENSE linear solver
# Line 270  class TestIDADENSE(Ascend): Line 509  class TestIDADENSE(Ascend):
509          print "v = %f" % M.v          print "v = %f" % M.v
510          M.run(T.getMethod('self_test'))          M.run(T.getMethod('self_test'))
511    
512      def testlotkaDENSE(self):      def testlotka(self):
513          self.L.load('johnpye/lotka.a4c')          self.L.load('johnpye/lotka.a4c')
514          M = self.L.findType('lotka').getSimulation('sim')          M = self.L.findType('lotka').getSimulation('sim')
515          M.setSolver(ascpy.Solver("QRSlv"))          M.setSolver(ascpy.Solver("QRSlv"))
# Line 289  class TestIDADENSE(Ascend): Line 528  class TestIDADENSE(Ascend):
528          assert abs(M.F - 21.36) < 0.1          assert abs(M.F - 21.36) < 0.1
529                    
530      def testdenx(self):      def testdenx(self):
531            print "-----------------------------====="
532          self.L.load('johnpye/idadenx.a4c')          self.L.load('johnpye/idadenx.a4c')
533          M = self.L.findType('idadenx').getSimulation('sim')          M = self.L.findType('idadenx').getSimulation('sim')
534          M.setSolver(ascpy.Solver("QRSlv"))          M.setSolver(ascpy.Solver("QRSlv"))
535          I = ascpy.Integrator(M)          I = ascpy.Integrator(M)
536          I.setEngine('IDA')          I.setEngine('IDA')
537          I.setParameter('calcic','NONE')          I.setParameter('calcic','YA_YPD')
538          I.setParameter('linsolver','DENSE')          I.setParameter('linsolver','DENSE')
539          I.setReporter(ascpy.IntegratorReporterConsole(I))          I.setReporter(ascpy.IntegratorReporterConsole(I))
540          I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)          I.setLogTimesteps(ascpy.Units("s"), 0.4, 4e10, 11)
# Line 304  class TestIDADENSE(Ascend): Line 544  class TestIDADENSE(Ascend):
544          I.setParameter('autodiff',True)          I.setParameter('autodiff',True)
545          I.analyse()          I.analyse()
546          I.solve()          I.solve()
547          assert abs(float(M.y1) - 5.1091e-08) < 1e-10          assert abs(float(M.y1) - 5.1091e-08) < 2e-9
548          assert abs(float(M.y2) - 2.0437e-13) < 1e-15          assert abs(float(M.y2) - 2.0437e-13) < 2e-14
549          assert abs(float(M.y3) - 1.0) < 1e-5          assert abs(float(M.y3) - 1.0) < 1e-5
550    
551      def testkryxDENSE(self):      def testkryx(self):
552          self.L.load('johnpye/idakryx.a4c')          self.L.load('johnpye/idakryx.a4c')
553          M = self.L.findType('idakryx').getSimulation('sim')          M = self.L.findType('idakryx').getSimulation('sim')
554          M.setSolver(ascpy.Solver('QRSlv'))          M.setSolver(ascpy.Solver('QRSlv'))

Legend:
Removed from v.1018  
changed lines
  Added in v.1050

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