class TestMixture(TestCase): def setUp(self): self.m = Mixture() self.ethane = Compound.objects.get(name='ETHANE') self.methane = Compound.objects.get(name='METHANE') self.co2 = Compound.objects.get(name='CARBON DIOXIDE') def test_add_order_is_preserved(self): self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.2) expected = [self.ethane, self.co2, self.methane] self.assertEqual(list(self.m.compounds), expected) def test_sort(self): self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.2) expected = [ self.methane, self.ethane, self.co2, ] self.m.sort() self.assertEqual(list(self.m.compounds), expected) assert_array_equal(self.m.z, np.array([0.2, 0.1, 0.3])) def test_fraction_order_is_preserved(self): self.m.add(self.ethane, 0.1) self.m.add(self.methane, 0.2) self.m.add(self.co2, 0.3) assert_array_equal(self.m.z, np.array([0.1, 0.2, 0.3])) def test_field_tc(self): self.m.add(self.methane, 0.2) self.m.add(self.co2, 0.1) self.m.add(self.ethane, 0.3) assert_array_equal(self.m.tc, [self.methane.tc, self.co2.tc, self.ethane.tc]) def test_field_pc(self): self.m.add(self.methane, 0.2) self.m.add(self.co2, 0.1) self.m.add(self.ethane, 0.3) assert_array_equal(self.m.pc, [self.methane.pc, self.co2.pc, self.ethane.pc]) def test_field_vc(self): self.m.add(self.methane, 0.2) self.m.add(self.co2, 0.1) self.m.add(self.ethane, 0.3) assert_array_equal(self.m.vc, [self.methane.vc, self.co2.vc, self.ethane.vc]) def test_decimal_round(self): self.m['ETHANE'] = u'0.4' self.m['CARBON DIOXIDE'] = u'0.3' self.m['n-PENTANE'] = u'0.2' self.m['n-HEXANE'] = 0.1 self.assertIsNone(self.m.clean())
class TestFlash(TestCase): def setUp(self): self.m = Mixture() self.ethane = Compound.objects.get(name='ETHANE') self.methane = Compound.objects.get(name='METHANE') self.co2 = Compound.objects.get(name='CARBON DIOXIDE') def test_flash_input(self): self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.6) s = EosSetup.objects.create(eos='RKPR', kij_mode='constants', lij_mode='constants') flash_txt = EosFlash(t=10, p=20, mixture=self.m, setup=s).get_txt() # open('flash_input_generated.txt', 'w').write(flash_txt) self.assertEqual( flash_txt, open(os.path.join(__location__, 'flash_input_expected.txt')).read()) def test_flash_requires_a_clean_mixture(self): self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.5) s = EosSetup.objects.create(eos='RKPR', kij_mode='constants', lij_mode='constants') assert self.m.total_z == Decimal('0.9') with self.assertRaises(ValidationError): self.m.get_flash(s, 10., 20.) self.m[self.methane] = 0.6 # total_z = 1.0 assert self.m.clean() is None # not raises flash = self.m.get_flash(s, 10., 20.) assert_array_equal(flash.x, np.array([0., 1., 0])) assert_array_equal(flash.y, np.array([0.142857, 0, 0.857143])) def test_multi_flashes(self): s = EosSetup.objects.create(eos='RKPR', kij_mode='constants', lij_mode='constants') self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.6) flashes = self.m.get_flashes(s, 10., [20., 21.]) self.assertEqual(len(flashes), 2) self.assertEqual(flashes[0].p, 20.) self.assertEqual(flashes[1].p, 21.)
class TestEnvelope(TestCase): def setUp(self): self.m = Mixture() self.ethane = Compound.objects.get(name='ETHANE') self.methane = Compound.objects.get(name='METHANE') self.co2 = Compound.objects.get(name='CARBON DIOXIDE') @skip('time expire') def test_envelope_requires_a_clean_mixture(self): self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.5) assert self.m.total_z == Decimal('0.9') with self.assertRaises(ValidationError): EosEnvelope.objects.create(mixture=self.m) self.m[self.methane] = 0.6 # total_z = 1.0 assert self.m.clean() is None # not raises EosEnvelope.objects.create(mixture=self.m) def test_simple_rkpr_env(self): m = Mixture() m.add_many('methane propane n-pentane n-decane n-hexadecane', '0.822 0.088 0.050 0.020 0.020') s = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode='constants') m.get_envelope(setup=s) @skip('calc fail') def test_envelope_object_calc_env_on_save(self): self.m.add(self.ethane, 1) env = EosEnvelope.objects.create(mixture=self.m) self.assertIsInstance(env.p, np.ndarray) self.assertIsInstance(env.t, np.ndarray) self.assertIsInstance(env.rho, np.ndarray) self.assertTrue(env.p.shape == env.t.shape == env.rho.shape) self.assertIsInstance(env.p_cri, np.ndarray) self.assertIsInstance(env.t_cri, np.ndarray) self.assertIsInstance(env.rho_cri, np.ndarray) self.assertTrue( env.p_cri.shape == env.t_cri.shape == env.rho_cri.shape) @skip("fail for this mixture case") def test_get_default_envelope_is_the_same(self): self.m.add(self.ethane, 1) env = EosEnvelope.objects.create(mixture=self.m) self.assertEqual(env, self.m.get_envelope())