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