def test_parsers(self): # populate a property tree from an input file ptree = PropertyTree() # INFO parser info_file = 'input.info' today = 'april 8th, 2016' with open(info_file, 'w') as fout: fout.write('date "' + today + '"') ptree.parse_info(info_file) self.assertEqual(ptree.get_string('date'), today) remove(info_file) # XML parser xml_file = 'input.xml' pi = 3.14 with open(xml_file, 'w') as fout: fout.write('<pi>' + str(pi) + '</pi>') ptree.parse_xml(xml_file) self.assertEqual(ptree.get_double('pi'), pi) remove(xml_file) # JSON parser json_file = 'input.json' with open(json_file, 'w') as fout: fout.write('{') fout.write(' "foo":') fout.write(' {') fout.write(' "bar": false') fout.write(' }') fout.write('}') ptree.parse_json(json_file) self.assertFalse(ptree.get_bool('foo.bar')) remove(json_file)
def test_export_eclab_ascii_format(self): # define dummy experiment # it is quicker than building an actual EIS experiment class DummyExperiment(Experiment): def __new__(cls, *args, **kwargs): return object.__new__(DummyExperiment) def __init__(self, ptree): Experiment.__init__(self) dummy = DummyExperiment(PropertyTree()) # produce dummy data for the experiment # here just a circle on the complex plane n = 10 f = ones(n, dtype=float) Z = ones(n, dtype=complex) for i in range(n): f[i] = 10**(i / (n - 1)) Z[i] = cos(2 * pi * i / (n - 1)) + 1j * sin(2 * pi * i / (n - 1)) dummy._data['frequency'] = f dummy._data['impedance'] = Z # need a supercapacitor here to make sure method inspect() is kept in # sync with the EC-Lab headers ptree = PropertyTree() ptree.parse_info('super_capacitor.info') super_capacitor = EnergyStorageDevice(ptree) dummy._extra_data = super_capacitor.inspect() # export the data to ECLab format eclab = ECLabAsciiFile('untitled.mpt') eclab.update(dummy) # check that all lines end up with Windows-style line break '/r/n' # file need to be open in byte mode or the line ending will be # converted to '\n'... # also check that the number of lines in the headers has been computed # correctly and that the last one contains the column headers with open('untitled.mpt', mode='rb') as fin: lines = fin.readlines() for line in lines: self.assertNotEqual(line.find(b'\r\n'), -1) self.assertNotEqual(line.find(b'\r\n'), len(line) - 4) header_lines = int(lines[1].split( b':')[1].lstrip(b'').rstrip(b'\r\n')) self.assertEqual( header_lines, len(eclab._unformated_headers) ) self.assertEqual(lines[header_lines - 1].find(b'freq/Hz'), 0) # check Nyquist plot does not throw nyquist = NyquistPlot('nyquist.png') nyquist.update(dummy)
# Copyright (c) 2016, the Cap authors. # # This file is subject to the Modified BSD License and may not be distributed # without copyright and license information. Please refer to the file LICENSE # for the text and further information on this license. from pycap import PropertyTree, EnergyStorageDevice from pycap import Charge from pycap import initialize_data from mpi4py import MPI import unittest comm = MPI.COMM_WORLD filename = 'series_rc.info' ptree = PropertyTree() ptree.parse_info(filename) device = EnergyStorageDevice(ptree, comm) class capChargeTestCase(unittest.TestCase): def test_charge_constant_current(self): ptree = PropertyTree() ptree.put_string('charge_mode', 'constant_current') ptree.put_double('charge_current', 10e-3) ptree.put_string('charge_stop_at_1', 'voltage_greater_than') ptree.put_double('charge_voltage_limit', 1.4) ptree.put_double('time_step', 0.2) charge = Charge(ptree) data = initialize_data() charge.run(device, data) self.assertAlmostEqual(data['current'][0], 10e-3)
# Copyright (c) 2016, the Cap authors. # # This file is subject to the Modified BSD License and may not be distributed # without copyright and license information. Please refer to the file LICENSE # for the text and further information on this license. from pycap import PropertyTree, EnergyStorageDevice from pycap import Charge from pycap import initialize_data from mpi4py import MPI import unittest comm = MPI.COMM_WORLD filename = 'series_rc.info' ptree = PropertyTree() ptree.parse_info(filename) device = EnergyStorageDevice(ptree, comm) class capChargeTestCase(unittest.TestCase): def test_charge_constant_current(self): ptree = PropertyTree() ptree.put_string('charge_mode', 'constant_current') ptree.put_double('charge_current', 10e-3) ptree.put_string('charge_stop_at_1', 'voltage_greater_than') ptree.put_double('charge_voltage_limit', 1.4) ptree.put_double('time_step', 0.2) charge = Charge(ptree) data = initialize_data() charge.run(device, data)