コード例 #1
0
ファイル: test_composite.py プロジェクト: ischoegl/cantera
    def test_write_hdf(self):
        outfile = self.test_work_path / "solutionarray.h5"
        # In Python >= 3.8, this can be replaced by the missing_ok argument
        if outfile.is_file():
            outfile.unlink()

        extra = {'foo': range(7), 'bar': range(7)}
        meta = {'spam': 'eggs', 'hello': 'world'}
        states = ct.SolutionArray(self.gas, 7, extra=extra, meta=meta)
        states.TPX = np.linspace(300, 1000, 7), 2e5, 'H2:0.5, O2:0.4'
        states.equilibrate('HP')

        states.write_hdf(outfile, attrs={'foobar': 'spam and eggs'})

        b = ct.SolutionArray(self.gas)
        attr = b.read_hdf(outfile)
        self.assertArrayNear(states.T, b.T)
        self.assertArrayNear(states.P, b.P)
        self.assertArrayNear(states.X, b.X)
        self.assertArrayNear(states.foo, b.foo)
        self.assertArrayNear(states.bar, b.bar)
        self.assertEqual(b.meta['spam'], 'eggs')
        self.assertEqual(b.meta['hello'], 'world')
        self.assertEqual(attr['foobar'], 'spam and eggs')

        gas = ct.Solution('gri30.yaml', transport_model=None)
        ct.SolutionArray(gas, 10).write_hdf(outfile)

        with _h5py.File(outfile, 'a') as hdf:
            hdf.create_group('spam')

        c = ct.SolutionArray(self.gas)
        with self.assertRaisesRegex(IOError, 'does not contain valid data'):
            c.read_hdf(outfile, group='spam')
        with self.assertRaisesRegex(IOError, 'does not contain group'):
            c.read_hdf(outfile, group='eggs')
        with self.assertRaisesRegex(IOError, 'phases do not match'):
            c.read_hdf(outfile, group='group1')
        with self.assertRaisesRegex(IOError, 'does not contain data'):
            c.read_hdf(outfile, subgroup='foo')

        states.write_hdf(outfile, group='foo/bar/baz')
        c.read_hdf(outfile, group='foo/bar/baz')
        self.assertArrayNear(states.T, c.T)
コード例 #2
0
    def test_write_hdf(self):

        outfile = pjoin(self.test_work_dir, 'solutionarray.h5')
        if os.path.exists(outfile):
            os.remove(outfile)

        extra = {'foo': range(7), 'bar': range(7)}
        meta = {'spam': 'eggs', 'hello': 'world'}
        states = ct.SolutionArray(self.gas, 7, extra=extra, meta=meta)
        states.TPX = np.linspace(300, 1000, 7), 2e5, 'H2:0.5, O2:0.4'
        states.equilibrate('HP')

        states.write_hdf(outfile, attrs={'foobar': 'spam and eggs'})

        b = ct.SolutionArray(self.gas)
        attr = b.read_hdf(outfile)
        self.assertTrue(np.allclose(states.T, b.T))
        self.assertTrue(np.allclose(states.P, b.P))
        self.assertTrue(np.allclose(states.X, b.X))
        self.assertTrue(np.allclose(states.foo, b.foo))
        self.assertTrue(np.allclose(states.bar, b.bar))
        self.assertEqual(b.meta['spam'], 'eggs')
        self.assertEqual(b.meta['hello'], 'world')
        self.assertEqual(attr['foobar'], 'spam and eggs')

        gas = ct.Solution('gri30.yaml')
        ct.SolutionArray(gas, 10).write_hdf(outfile)

        with _h5py.File(outfile, 'a') as hdf:
            hdf.create_group('spam')

        c = ct.SolutionArray(self.gas)
        with self.assertRaisesRegex(IOError, 'does not contain valid data'):
            c.read_hdf(outfile, group='spam')
        with self.assertRaisesRegex(IOError, 'does not contain group'):
            c.read_hdf(outfile, group='eggs')
        with self.assertRaisesRegex(IOError, 'phases do not match'):
            c.read_hdf(outfile, group='group1')
        with self.assertRaisesRegex(IOError, 'does not contain data'):
            c.read_hdf(outfile, subgroup='foo')

        states.write_hdf(outfile, group='foo/bar/baz')
        c.read_hdf(outfile, group='foo/bar/baz')
        self.assertTrue(np.allclose(states.T, c.T))