from pycap import PropertyTree,EnergyStorageDevice,Stage,initialize_data
import unittest

device_database=PropertyTree()
device_database.parse_xml('device.xml')
device=EnergyStorageDevice(device_database.get_child('device'))

class capStageTestCase(unittest.TestCase):
    def test_constant_current_charge_for_given_time(self):
        ptree=PropertyTree()
        ptree.put_string('mode','constant_current')
        ptree.put_double('current',5e-3)
        ptree.put_string('end_criterion','time')
        ptree.put_double('duration',15.0)
        ptree.put_double('time_step',0.1)
        stage=Stage(ptree)
        data=initialize_data()
        steps=stage.run(device,data)
        self.assertEqual(steps,150)
        self.assertAlmostEqual(data['time'][-1],15.0)
        self.assertAlmostEqual(data['current'][-1],5e-3)
    def test_force_discharge(self):
        ptree=PropertyTree()
        ptree.put_string('mode','constant_voltage')
        ptree.put_double('voltage',0.0)
        ptree.put_string('end_criterion','current_less_than')
        ptree.put_double('current_limit',1e-5)
        ptree.put_double('time_step',1.0)
        stage=Stage(ptree)
        data=initialize_data()
        steps=stage.run(device,data)
        self.assertGreaterEqual(steps,1)
        self.assertAlmostEqual(data['voltage'][-1],0.0)
        self.assertLessEqual(data['current'][-1],1e-5)

if __name__ == '__main__':
    unittest.main()

@@ -47,15 +47,62 @@
 vice
-,Stage,
+%0Afrom pycap import Stage,MultiStage%0Afrom pycap import 
 init
@@ -754,16 +754,66 @@
 ps,150)%0A
+        self.assertEqual(steps,len(data%5B'time'%5D))%0A
         
@@ -1359,16 +1359,66 @@
 teps,1)%0A
+        self.assertEqual(steps,len(data%5B'time'%5D))%0A
         
@@ -1461,24 +1461,24 @@
 '%5D%5B-1%5D,0.0)%0A
-
         self
@@ -1519,16 +1519,906 @@
 1%5D,1e-5)
+%0A    def test_time_steps(self):%0A        ptree=PropertyTree()%0A        ptree.put_int('stages',2)%0A        ptree.put_int('cycles',1)%0A        ptree.put_double('time_step',1.0)%0A        ptree.put_string('stage_0.mode','hold')%0A        ptree.put_string('stage_0.end_criterion','time')%0A        ptree.put_double('stage_0.duration',2.0)%0A        ptree.put_string('stage_1.mode','rest')%0A        ptree.put_string('stage_1.end_criterion','time')%0A        ptree.put_double('stage_1.duration',1.0)%0A        ptree.put_double('stage_1.time_step',0.1)%0A        multi=MultiStage(ptree)%0A        data=initialize_data()%0A        steps=multi.run(device,data)%0A        self.assertEqual(steps,12)%0A        self.assertEqual(steps,len(data%5B'time'%5D))%0A        self.assertAlmostEqual(data%5B'time'%5D%5B5%5D,2.4)%0A        self.assertAlmostEqual(data%5B'voltage'%5D%5B0%5D,data%5B'voltage'%5D%5B1%5D)%0A        self.assertAlmostEqual(data%5B'current'%5D%5B3%5D,0.0)
 %0A%0Aif __n

update test stage/multistage