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

Diff of /trunk/test.py

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

revision 1118 by johnpye, Thu Jan 11 12:08:12 2007 UTC revision 1167 by johnpye, Wed Jan 17 09:32:29 2007 UTC
# Line 70  class TestSolver(AscendSelfTester): Line 70  class TestSolver(AscendSelfTester):
70      def testlog10(self):      def testlog10(self):
71          self._run('testlog10')          self._run('testlog10')
72    
73        def testrootsofpoly(self):
74            self._run('roots_of_poly',filename="roots_of_poly.a4c")
75    
76        def testcollapsingcan(self):
77            self._run('collapsingcan',filename="collapsingcan.a4c")
78    
79        def testdistancecalc(self):
80            self._run('distance_calc',filename="distance_calc.a4c")
81    
82      def testconopt(self):      def testconopt(self):
83          self._run('testconopt',"CONOPT")                          self._run('testconopt',"CONOPT")                
84    
# Line 99  class TestSolver(AscendSelfTester): Line 108  class TestSolver(AscendSelfTester):
108          self.assertAlmostEqual( float(M.t_solar), M.t_solar.as("s"))          self.assertAlmostEqual( float(M.t_solar), M.t_solar.as("s"))
109          self.assertAlmostEqual( float(M.t_solar)/3600, M.t_solar.as("h"))          self.assertAlmostEqual( float(M.t_solar)/3600, M.t_solar.as("h"))
110    
111    class TestMatrix(AscendSelfTester):
112        def testlog10(self):
113            M = self._run('testlog10')
114            print M.getMatrix().write(sys.stderr,"mmio")
115    
116            
117  class TestIntegrator(Ascend):  class TestIntegrator(Ascend):
118    
119      def testListIntegrators(self):      def testListIntegrators(self):
# Line 112  class TestIntegrator(Ascend): Line 127  class TestIntegrator(Ascend):
127          self.L.load('johnpye/shm.a4c')          self.L.load('johnpye/shm.a4c')
128          M = self.L.findType('shm').getSimulation('sim')          M = self.L.findType('shm').getSimulation('sim')
129          M.setSolver(ascpy.Solver('QRSlv'))          M.setSolver(ascpy.Solver('QRSlv'))
130            P = M.getParameters()
131            M.setParameter('feastol',1e-12)
132          print M.getChildren()          print M.getChildren()
133          assert float(M.x) == 10.0          assert float(M.x) == 10.0
134          assert float(M.v) == 0.0          assert float(M.v) == 0.0
# Line 121  class TestIntegrator(Ascend): Line 138  class TestIntegrator(Ascend):
138          I.setReporter(ascpy.IntegratorReporterNull(I))          I.setReporter(ascpy.IntegratorReporterNull(I))
139          I.setEngine(integratorname);          I.setEngine(integratorname);
140          I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);          I.setLinearTimesteps(ascpy.Units("s"), 0.0, t_end, 100);
141          I.setMinSubStep(0.0005); # these limits are required by IDA at present (numeric diff)          I.setMinSubStep(0.0001); # these limits are required by IDA at present (numeric diff)
142          I.setMaxSubStep(0.02);          I.setMaxSubStep(0.1);
143          I.setInitialSubStep(0.001);          I.setInitialSubStep(0.001);
144          I.setMaxSubSteps(200);          I.setMaxSubSteps(200);
145          if(integratorname=='IDA'):          if(integratorname=='IDA'):
146              I.setParameter('autodiff',False)              I.setParameter('autodiff',False)
147            for p in M.getParameters():
148                print p.getName(),"=",p.getValue()
149          I.analyse();          I.analyse();
150          I.solve();          I.solve();
151          print "At end of simulation,"          print "At end of simulation,"
# Line 409  class TestExtFn(AscendSelfTester): Line 428  class TestExtFn(AscendSelfTester):
428          M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')          M = self._run('extrelrepeat',filename='johnpye/extfn/extrelrepeat.a4c')
429    
430  #-------------------------------------------------------------------------------  #-------------------------------------------------------------------------------
431    # Testing of Sensitivity module
432    
433    class TestSensitivity(AscendSelfTester):
434        def test1(self):
435            self.L.load('sensitivity_test.a4c')
436            T = self.L.findType('sensitivity_test')
437            M = T.getSimulation('sim',False)
438            M.run(T.getMethod('on_load'))
439            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
440            M.run(T.getMethod('analyse'))
441            M.run(T.getMethod('self_test'))
442    
443        def testall(self):
444            self.L.load('sensitivity_test.a4c')
445            T = self.L.findType('sensitivity_test_all')
446            M = T.getSimulation('sim',False)
447            M.run(T.getMethod('on_load'))
448            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
449            M.run(T.getMethod('analyse'))
450            M.run(T.getMethod('self_test'))
451                    
452    #-------------------------------------------------------------------------------
453  # Testing of a ExtPy - external python methods  # Testing of a ExtPy - external python methods
454    
455  class TestExtPy(AscendSelfTester):  class TestExtPy(AscendSelfTester):
# Line 432  class TestExtPy(AscendSelfTester): Line 473  class TestExtPy(AscendSelfTester):
473  # Testing of saturated steam properties library (iapwssatprops.a4c)  # Testing of saturated steam properties library (iapwssatprops.a4c)
474    
475  class TestSteam(AscendSelfTester):  class TestSteam(AscendSelfTester):
476    
477      def testiapwssatprops1(self):      def testiapwssatprops1(self):
478          M = self._run('testiapwssatprops1',filename='steam/iapwssatprops.a4c')          M = self._run('testiapwssatprops1',filename='steam/iapwssatprops.a4c')
479      def testiapwssatprops2(self):      def testiapwssatprops2(self):
480          M = self._run('testiapwssatprops2',filename='steam/iapwssatprops.a4c')          M = self._run('testiapwssatprops2',filename='steam/iapwssatprops.a4c')
481      def testiapwssatprops3(self):      def testiapwssatprops3(self):
482          M = self._run('testiapwssatprops3',filename='steam/iapwssatprops.a4c')          M = self._run('testiapwssatprops3',filename='steam/iapwssatprops.a4c')
     def testsatsteamstream(self):  
         M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')  
483    
484        # test the stream model basically works
485      def testsatsteamstream(self):      def testsatsteamstream(self):
486          M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')          M = self._run('satsteamstream',filename='steam/satsteamstream.a4c')
487    
488        # test that we can solve in terms of various (rho,u)
489        def testsatuv(self):
490            self.L.load('steam/iapwssat.a4c')
491            T = self.L.findType('testiapwssatuv')
492            M = T.getSimulation('sim',False)
493            M.run(T.getMethod('on_load'))
494            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
495            print "p = %f bar" % M.p.as('bar');
496            print "T = %f C" % (M.T.as('K') - 273.15);
497            print "x = %f" % M.x;
498            M.run(T.getMethod('self_test'))
499            M.run(T.getMethod('values2'))
500    #       M.v.setRealValueWithUnits(1.0/450,"m^3/kg");
501    #       M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
502            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
503            print "p = %f bar" % M.p.as('bar');
504            print "T = %f C" % (M.T.as('K') - 273.15);
505            print "x = %f" % M.x;
506            M.run(T.getMethod('self_test2'))
507            
508    
509  ## @TODO fix error capture from bounds checking during initialisation  ## @TODO fix error capture from bounds checking during initialisation
510  #   def testiapwssat1(self):  #   def testiapwssat1(self):
511  #       M = self._run('testiapwssat1',filename='steam/iapwssat.a4c')  #       M = self._run('testiapwssat1',filename='steam/iapwssat.a4c')
512    
 # @TODO fix bug with unpivoted node[i].hg_expr eqns.  
513      def testdsgsat(self):      def testdsgsat(self):
514          self.L.load('steam/dsgsat2.a4c')          self.L.load('steam/dsgsat2.a4c')
515          T = self.L.findType('dsgsat2')          T = self.L.findType('dsgsat2')
516          M = T.getSimulation('sim',False)          M = T.getSimulation('sim',False)
517          try:          M.run(T.getMethod('on_load'))
             M.run(T.getMethod('on_load'))  
         except:  
             pass  
518          M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())          M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
519          M.run(T.getMethod('fixed_states'))          self.assertAlmostEqual(M.dTw_dt[2],0.0);
520          I = ascpy.Integrator(M)          M.run(T.getMethod('configure_dynamic'))
521            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
522            return M
523    
524        def testintegLSODE(self):
525            M = self.testdsgsat()
526            M.qdot_s.setRealValueWithUnits(1000,"W/m")
527            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
528            #M.setParameter('
529            I = ascpy.Integrator(M)
530          I.setEngine('LSODE')          I.setEngine('LSODE')
531            I.setParameter('meth','AM')
532            I.setParameter('maxord',12)
533          I.setReporter(ascpy.IntegratorReporterConsole(I))          I.setReporter(ascpy.IntegratorReporterConsole(I))
534            I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 1)
535            I.analyse()
536            I.solve()
537    
538        def testintegIDA(self):
539            M = self.testdsgsat()
540            self.assertAlmostEqual(M.dTw_dt[2],0.0)
541            Tw1 = float(M.T_w[2])
542            T = self.L.findType('dsgsat2')
543            M.run(T.getMethod('free_states'))
544            I = ascpy.Integrator(M)
545            I.setEngine('IDA')
546            I.setParameter('linsolver','DENSE')
547            I.setParameter('safeeval',True)
548            I.setParameter('rtol',1e-8)
549            I.setInitialSubStep(0.01)
550            I.setMinSubStep(0.001)      
551            I.setMaxSubSteps(100)      
552          I.setReporter(ascpy.IntegratorReporterConsole(I))          I.setReporter(ascpy.IntegratorReporterConsole(I))
553          I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 100)          I.setLinearTimesteps(ascpy.Units("s"), 0, 3600, 100)
         I.setMinSubStep(0.01)  
         I.setMaxSubStep(0.02)  
         I.setInitialSubStep(0.1)  
554          I.analyse()          I.analyse()
555          I.solve()          I.solve()
556            I.analyse()
557          I.solve()          I.solve()
558          #M.checkStructuralSingularity() causes crash!          self.assertAlmostEqual(float(M.T_w[2]),Tw1)
559            M.qdot_s.setRealValueWithUnits(1000,"W/m")
560            self.assertAlmostEqual(M.qdot_s.as("W/m"),1000)
561            M.solve(ascpy.Solver('QRSlv'),ascpy.SolverReporter())
562            self.assertNotAlmostEqual(M.dTw_dt[2],0.0)
563    #       I = ascpy.Integrator(M)
564    #       I.setEngine('LSODE')
565    #       I.setReporter(ascpy.IntegratorReporterConsole(I))
566    #       I.setReporter(ascpy.IntegratorReporterConsole(I))
567    #       I.setLinearTimesteps(ascpy.Units("s"), 0, 5, 100)
568    #       I.setMinSubStep(0.0001)
569    #       I.setMaxSubStep(100)
570    #       I.setInitialSubStep(0.1)
571    #       I.analyse()
572    #       I.solve()
573                    
574  #-------------------------------------------------------------------------------  #-------------------------------------------------------------------------------
575  # Testing of freesteam external steam properties functions  # Testing of freesteam external steam properties functions
# Line 486  except ImportError,e: Line 585  except ImportError,e:
585    
586  if with_freesteam and have_freesteam:  if with_freesteam and have_freesteam:
587      class TestFreesteam(AscendSelfTester):      class TestFreesteam(AscendSelfTester):
588          def testfreesteamtest(self):  #       def testfreesteamtest(self):
589              """run the self-test cases bundled with freesteam"""  #           """run the self-test cases bundled with freesteam"""
590              self._run('testfreesteam',filename='testfreesteam.a4c')  #           self._run('testfreesteam',filename='testfreesteam.a4c')
591    
592          def testload(self):          def testload(self):
593              """check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)"""              """check that we can load 'thermalequilibrium2' (IMPORT "freesteam", etc)"""
# Line 519  if with_freesteam and have_freesteam: Line 618  if with_freesteam and have_freesteam:
618              print "S[1].T = %f K" % M.S[1].T              print "S[1].T = %f K" % M.S[1].T
619              print "S[2].T = %f K" % M.S[2].T              print "S[2].T = %f K" % M.S[2].T
620              print "Q = %f W" % M.Q                    print "Q = %f W" % M.Q      
621              self.assertAlmostEqual(float(M.S[1].T),506.77225109,5);              self.assertAlmostEqual(float(M.S[1].T),506.77225109,4);
622              self.assertAlmostEqual(float(M.S[2].T),511.605173967,5);              self.assertAlmostEqual(float(M.S[2].T),511.605173967,5);
623              self.assertAlmostEqual(float(M.Q),-48.32922877329,3);              self.assertAlmostEqual(float(M.Q),-48.32922877329,3);
624              self.assertAlmostEqual(float(M.t),3000);              self.assertAlmostEqual(float(M.t),3000);
625              print "Note that the above values have not been verified analytically"              print "Note that the above values have not been verified analytically"
626    
627    
628  #-------------------------------------------------------------------------------  #-------------------------------------------------------------------------------
629  # Testing of IDA models using DENSE linear solver  # Testing of IDA models using DENSE linear solver
630    
# Line 710  if __name__=='__main__': Line 810  if __name__=='__main__':
810      restart = 0      restart = 0
811    
812      if platform.system()=="Windows":      if platform.system()=="Windows":
813          LD_LIBRARY_PATTH="PATH"          LD_LIBRARY_PATH="PATH"
814          SEP = ";"          SEP = ";"
815      else:      else:
816          LD_LIBRARY_PATH="LD_LIBRARY_PATH"          LD_LIBRARY_PATH="LD_LIBRARY_PATH"
817          SEP = ":"          SEP = ":"
818    
819      modelsdir = os.path.normpath(os.path.join(sys.path[0],"models"))      freesteamdir = os.path.expanduser("~/freesteam/ascend")
820        modeldirs = [os.path.abspath(os.path.join(sys.path[0],"models")),os.path.abspath(freesteamdir)]
821      if not os.environ.get('ASCENDLIBRARY'):      if not os.environ.get('ASCENDLIBRARY'):
822          os.environ['ASCENDLIBRARY'] = modelsdir          os.environ['ASCENDLIBRARY'] = SEP.join(modeldirs)
823          restart = 1          restart = 1
824      else:      else:
825          envmodelsdir = [os.path.abspath(i) for i in os.environ['ASCENDLIBRARY'].split(SEP)]          envmodelsdir = [os.path.abspath(i) for i in os.environ['ASCENDLIBRARY'].split(SEP)]
826          if modelsdir not in envmodelsdir:          for l in modeldirs:
827              envmodelsdir.insert(0,modelsdir)              if l in envmodelsdir[len(modeldirs):]:
828              os.environ['ASCENDLIBRARY']=SEP.join(envmodelsdir)                  envmodelsdir.remove(l)
829              restart = 1                      restart = 1
830            for l in modeldirs:
831                if l not in envmodelsdir:
832                    envmodelsdir.insert(0,l)
833                    restart = 1
834            os.environ['ASCENDLIBRARY'] = SEP.join(envmodelsdir)    
835    
836      libdirs = ["pygtk","."]      libdirs = ["pygtk","."]
837      libdirs = [os.path.normpath(os.path.join(sys.path[0],l)) for l in libdirs]      libdirs = [os.path.normpath(os.path.join(sys.path[0],l)) for l in libdirs]
# Line 751  if __name__=='__main__': Line 857  if __name__=='__main__':
857          envpypath = os.environ['PYTHONPATH'].split(SEP)          envpypath = os.environ['PYTHONPATH'].split(SEP)
858          if pypath not in envpypath:          if pypath not in envpypath:
859              envpypath.insert(0,pypath)              envpypath.insert(0,pypath)
860              os.environ['PYTHONPATH']=envpypath              os.environ['PYTHONPATH']=SEP.join(envpypath)
861              restart = 1              restart = 1
862    
863      if restart:      if restart:
864          script = os.path.join(sys.path[0],"test.py")          script = os.path.join(sys.path[0],"test.py")                    
865          print "Restarting"          print "Restarting with..."
866          print "LD_LIBRARY_PATH = %s" % os.environ.get(LD_LIBRARY_PATH)          print "   LD_LIBRARY_PATH = %s" % os.environ.get(LD_LIBRARY_PATH)
867          print "PYTHONPATH = %s" % os.environ.get('PYTHONPATH')          print "   PYTHONPATH = %s" % os.environ.get('PYTHONPATH')
868          print "ASCENDLIBRARY = %s" % os.environ.get('ASCENDLIBRARY')          print "   ASCENDLIBRARY = %s" % os.environ.get('ASCENDLIBRARY')
869          os.execvp("python",[script] + sys.argv)  
870            if sys.argv[0] !="/usr/bin/gdb":
871                os.execvp("python",[script] + sys.argv)
872            else:
873                print "Can't restart from inside GDB"
874    
875      import ascpy      import ascpy
876    

Legend:
Removed from v.1118  
changed lines
  Added in v.1167

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