def setUp(self): self.m = Mixture() self.m.add_many('methane propane n-pentane n-decane n-hexadecane', '0.822 0.088 0.050 0.020 0.020') self.s = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode='constants')
def setUp(self): K0InteractionParameter.objects.all().delete() 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') self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.2) self.s = EosSetup.objects.create(eos='RKPR')
def setUp(self): self.GC = Mixture() elementos = ['methane', 'n-butane', 'n-heptane'] fracciones = [0.5, 0.2, 0.3] for elemento, fraccion in zip(elementos, fracciones): self.GC[elemento] = fraccion self.GC.sort(True) self.setup = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) self.setup.set_interaction('k0', 'methane', 'n-butane', 0.02613) self.setup.set_interaction('k0', 'methane', 'n-heptane', 0.05613)
def test_matrix_tstar(self): m = Mixture() m['ethane'] = 0.1 m['methane'] = 0.9 s = EosSetup.objects.create(eos='PR') s.set_interaction('tstar', 'ethane', 'methane', value=0.43) assert_array_equal(s.tstar(m), np.array([[0, 0.43], [0.43, 0]]))
class TestIsochore(TestCase): def setUp(self): self.m = Mixture() self.m.add_many('methane propane n-pentane n-decane n-hexadecane', '0.822 0.088 0.050 0.020 0.020') self.s = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode='constants') def test_isochore_input(self): flash_txt = Isochore(v=10., ts=467.01, ps=3.86, t_sup=465.0, t_step=5.0, t_inf=270.0, mixture=self.m, setup=self.s).get_txt() #open('isochore_input_generated.txt', 'w').write(flash_txt) self.assertEqual( flash_txt, open(os.path.join(__location__, 'isochore_input_expected.txt')).read()) def test_simple_isochore(self): iso = Isochore(v=10., ts=467.01, ps=3.86, t_sup=465.0, t_step=5.0, t_inf=270.0, mixture=self.m, setup=self.s) iso._calc() #assert_array_equal(.x, np.array([0., 1., 0])) assert_array_almost_equal(iso.t[[0, -1]], np.array([467.01, 270.0])) assert_array_almost_equal(iso.p[[0, -1]], np.array([3.86, 2.1])) self.assertEqual(iso.t.size, 41) self.assertEqual(iso.p.size, 41) assert_array_almost_equal(iso.t_monophasic[[0, -1]], np.array([467.01, 1002.01])) assert_array_almost_equal(iso.p_monophasic[[0, -1]], np.array([3.86, 8.356])) self.assertEqual(iso.t_monophasic.size, 108) self.assertEqual(iso.p_monophasic.size, 108)
def test_matrix_kij_rkpr(self): m = Mixture() m['ethane'] = 0.1 m['methane'] = 0.8 m['co2'] = 0.1 s = EosSetup.objects.create(eos='RKPR') s.set_interaction('kij', 'ethane', 'co2', value=0.43) assert_array_equal(s.kij(m), np.array([[0, 0, 0.43], [0, 0, 0.], [0.43, 0, 0]]))
def test_defaults_priority(self): K0InteractionParameter.objects.all().delete() m = Mixture() m.add(self.methane, 0.5) m.add(self.co2, 0.5) user = User.objects.create(username='******') s = EosSetup.objects.create(eos='SRK', user=user) # default staff set_interaction('lij', self.methane, self.co2, value=0.4, eos='SRK') assert_array_equal(s.lij(m), np.array([[0., 0.4], [0.4, 0.]])) # now user defined set_interaction('lij', self.methane, self.co2, value=0.6, user=user, eos='SRK') assert_array_equal(s.lij(m), np.array([[0., 0.6], [0.6, 0.]])) # now setup s.set_interaction('lij', self.methane, self.co2, value=0.9) assert_array_equal(s.lij(m), np.array([[0., 0.9], [0.9, 0.]]))
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)
def test_complex_lij_(self): GC = Mixture() elementos = [ 'methane', 'n-butane', 'n-heptane', 'n-decane', 'n-tetradecane' ] fracciones = [0.80, 0.14, 0.04, 0.014, 0.006] for elemento, fraccion in zip(elementos, fracciones): GC[elemento] = fraccion GC.sort(True) setup1 = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) setup1.set_interaction('k0', 'methane', 'n-butane', 0.02613) setup1.set_interaction('k0', 'methane', 'n-heptane', 0.05613) setup1.set_interaction('k0', 'methane', 'n-decane', 0.08224) setup1.set_interaction('k0', 'methane', 'n-tetradecane', 0.10738) setup1.set_interaction('lij', 'methane', 'n-butane', -0.06565) setup1.set_interaction('lij', 'methane', 'n-heptane', -0.11875) setup1.set_interaction('lij', 'methane', 'n-decane', -0.14551) setup1.set_interaction('lij', 'methane', 'n-tetradecane', -0.13602) setup1.set_interaction('lij', 'n-butane', 'n-heptane', -0.01754) setup1.set_interaction('lij', 'n-butane', 'n-decane', -0.02433) setup1.set_interaction('lij', 'n-butane', 'n-tetradecane', -0.02203) setup1.set_interaction('lij', 'n-heptane', 'n-decane', -0.00586) setup1.set_interaction('lij', 'n-heptane', 'n-tetradecane', -0.00388) setup1.set_interaction('lij', 'n-decane', 'n-tetradecane', 0.00188) setup_lij_menos20 = EosSetup.objects.create( eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) setup_lij_menos20.set_interaction_matrix('k0', GC, setup1.k0(GC)) assert_array_equal( setup1.k0(GC) - setup_lij_menos20.k0(GC), np.zeros((5, 5))) setup_lij_menos20.set_interaction_matrix('lij', GC, setup1.lij(GC)) assert_array_equal( setup1.lij(GC) - setup_lij_menos20.lij(GC), np.zeros((5, 5)))
def test_auto_tstart(self): m = Mixture() m.add(self.ethane, 0.5) m.add(self.co2, 0.5) assert self.ethane < self.co2 s = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP) assert_array_equal( s.tstar(m), np.array([[0., self.ethane.tc], [self.ethane.tc, 0.]]))
class TestK0(TestCase): def setUp(self): K0InteractionParameter.objects.all().delete() 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') self.m.add(self.ethane, 0.1) self.m.add(self.co2, 0.3) self.m.add(self.methane, 0.2) self.s = EosSetup.objects.create(eos='RKPR') def test_all_zeros_by_default(self): assert_array_equal(self.s.k0(self.m), np.zeros((3, 3))) def test_there_is_a_global_k(self): k = K0InteractionParameter.objects.create(eos='RKPR', value=0.1) k.compounds.add(self.ethane) k.compounds.add(self.methane) expected = np.zeros((3, 3)) expected[0, 2] = expected[2, 0] = k.value assert_array_equal(self.s.k0(self.m), expected) def test_there_is_a_global_k_a_setup_override(self): set_interaction('k0', self.ethane, self.methane, eos='RKPR', value=0.1) k2 = self.s.set_interaction('k0', self.ethane, self.methane, value=0.2) expected = np.zeros((3, 3)) expected[0, 2] = expected[2, 0] = k2.value assert_array_equal(self.s.k0(self.m), expected) def test_global_k_for_same_custom_for_other_interaction(self): k = set_interaction('k0', self.ethane, self.methane, value=0.1, eos='RKPR') k2 = self.s.set_interaction('k0', self.ethane, self.co2, value=0.2) expected = np.zeros((3, 3)) expected[0, 1] = expected[1, 0] = k2.value expected[0, 2] = expected[2, 0] = k.value assert_array_equal(self.s.k0(self.m), expected)
def test_kij_pr_matrix(self): c24 = Compound.objects.find('c24')[1] c3 = Compound.objects.find('c3')[4] lij = KijInteractionParameter.objects.create(eos='PR', value=0.03222) lij.compounds.add(c24) lij.compounds.add(c3) assert list(KijInteractionParameter.objects.filter(compounds=c3).\ filter(compounds=c24).values('value')) == [{'value': 0.03222}] s = EosSetup(eos='PR', kij_mode=EosSetup.T_DEP) m = Mixture() m.add(c3, 0.5) m.add(c24, 0.5) kij_matrix = s.kij(m) assert_array_equal(kij_matrix, np.array([[0., 0.03222], [0.03222, 0.]]))
def test_same_setup_and_user_different_eos_doesn_superpose(self): m = Mixture() m.add(self.methane, 0.2) m.add(self.co2, 0.1) user = User.objects.create(username='******') s1 = EosSetup.objects.create(eos='RKPR', user=user) s2 = EosSetup.objects.create(eos='PR', user=user) s3 = EosSetup.objects.create(eos='SRK', user=user) s1.set_interaction('kij', self.methane, self.co2, value=0.1) s2.set_interaction('kij', self.methane, self.co2, value=0.2) # default for user set_interaction('kij', self.methane, self.co2, eos='SRK', value=0.3, user=user) kij_rkpr = s1.kij(m) kij_pr = s2.kij(m) kij_srk = s3.kij(m) assert_array_equal(kij_pr, np.array([[0., 0.2], [0.2, 0.]])) assert_array_equal(kij_rkpr, np.array([[0., 0.1], [0.1, 0.]])) assert_array_equal(kij_srk, np.array([[0., 0.3], [0.3, 0.]]))
class TestMixtureMagicMeths(TestCase): def setUp(self): Mixture.objects.all().delete() 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__len__is_zero(self): assert self.m.fractions.count() == 0 self.assertEqual(len(self.m), 0) def test__len__zero(self): self.m.add(self.methane, 0.2) assert self.m.fractions.count() == 1 self.assertEqual(len(self.m), 1) def test_simple_get_attr(self): self.m.add(self.methane, 0.2) self.assertEqual(self.m[self.methane], Decimal('0.2')) def test_get_attr_using_str(self): self.m.add(self.methane, 0.2) self.assertEqual(self.m['methane'], Decimal('0.2')) def test_get_attr_raises_type_exception_for_other_types(self): self.m.add(self.methane, 0.2) with self.assertRaises(TypeError): self.m[1j] def test_get_attr_raises_keyexception_for_unknown_key(self): with self.assertRaises(KeyError) as e: self.m['methane'] self.assertIn('is not part of this mixture', e.exception.message) def test_get_attr_raises_keyexception_for_unknown_keys(self): with self.assertRaises(KeyError) as e: self.m['unknown_compound'] self.assertIn('unknown compound', e.exception.message) def test_get_attr_raises_keyexception_for_unknown_compound_keys(self): with self.assertRaises(KeyError): self.m[self.methane] def test_simple_set_attr(self): self.m[self.methane] = 0.2 self.assertEqual(self.m[self.methane], Decimal('0.2')) def test_simple_set_attr_as_str(self): self.m[self.ethane] = '0.1' self.assertEqual(self.m[self.ethane], Decimal('0.1')) def test_set_attr_overrides_previous(self): self.m.add(self.ethane, '0.1') self.m[self.ethane] = '0.4' self.assertEqual(self.m[self.ethane], Decimal('0.4')) def test_set_overrides_fail_is_sum_is_gt_1(self): self.m[self.ethane] = '0.4' self.m[self.methane] = '0.5' with self.assertRaises(ValueError): self.m[self.methane] = '0.7' assert_array_equal(self.m.z, [0.4, 0.5]) def test_set_overrides_not_fail_is_sum_is_lte_1(self): self.m[self.ethane] = '0.4' self.m[self.methane] = '0.5' self.m[self.methane] = '0.3' assert_array_equal(self.m.z, [0.4, 0.3]) def test_set_attr_raises_keyexception_for_unknown_keys(self): with self.assertRaises(KeyError): self.m['unknown_compound'] = 0.4 def test_del_item(self): self.m[self.methane] = 0.2 del self.m[self.methane] self.assertEqual(len(self.m), 0) def test_del_item_preserve_positions(self): self.m[self.methane] = 0.1 self.m[self.ethane] = 0.2 self.m[self.co2] = 0.3 del self.m[self.ethane] expected = [(self.methane, Decimal('0.1')), (self.co2, Decimal('0.3'))] self.assertEqual(list(self.m), expected) del self.m[self.methane] self.assertEqual(list(self.m), [(self.co2, Decimal('0.3'))]) def test_del_attr_raises_keyexception_for_unknown_keys(self): with self.assertRaises(KeyError) as e: del self.m['unknown_compound'] self.assertIn('unknown compound', e.exception.message) def test_del_attr_raises_keyexception_for_compound_not_in_the_mixture_keys( self): self.m[self.methane] = 0.2 with self.assertRaises(KeyError) as e: del self.m['ethane'] self.assertIn('is not part of this mixture', e.exception.message) def test_iter(self): self.m.add(self.ethane, 0.1) self.m.add(self.methane, 0.2) self.m.add(self.co2, 0.3) expected = [(self.ethane, Decimal('0.1')), (self.methane, Decimal('0.2')), (self.co2, Decimal('0.3'))] self.assertEqual([i for i in self.m], expected)
def setUp(self): MixtureFraction.objects.all().delete() self.m = Mixture() self.ethane = Compound.objects.get(name='ETHANE') self.co2 = Compound.objects.get(name='CARBON DIOXIDE') self.methane = Compound.objects.get(name='METHANE')
class TestSetInteractionMatrix(TestCase): def setUp(self): self.GC = Mixture() elementos = ['methane', 'n-butane', 'n-heptane'] fracciones = [0.5, 0.2, 0.3] for elemento, fraccion in zip(elementos, fracciones): self.GC[elemento] = fraccion self.GC.sort(True) self.setup = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) self.setup.set_interaction('k0', 'methane', 'n-butane', 0.02613) self.setup.set_interaction('k0', 'methane', 'n-heptane', 0.05613) def test_matrix_could_be_text(self): setup = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) matrix = """0. 0.02 0.05 0.02 0 0 0.05 0 0""" setup.set_interaction_matrix('k0', self.GC, matrix) assert_array_equal( setup.k0(self.GC), np.array([[0., .02, 0.05], [0.02, 0, 0], [0.05, 0, 0]])) def test_text_matrix_replaces_semicolon(self): setup = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) matrix = """0 0,02 0,05 0,02 0 0 0,05 0 0""" setup.set_interaction_matrix('k0', self.GC, matrix) assert_array_equal( setup.k0(self.GC), np.array([[0., .02, 0.05], [0.02, 0, 0], [0.05, 0, 0]])) def test_set_matrix_could_be_ndarray(self): setup = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) matrix = np.array([[0., .02, 0.05], [0.02, 0, 0], [0.05, 0, 0]]) setup.set_interaction_matrix('k0', self.GC, matrix) assert_array_equal(setup.k0(self.GC), matrix) def test_set_lij_matrix(self): setup = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) matrix = np.array([[0., .02, 0.05], [0.02, 0, 0], [0.05, 0, 0]]) setup.set_interaction_matrix('lij', self.GC, matrix) assert_array_equal(setup.lij(self.GC), matrix) def test_complex_lij_(self): GC = Mixture() elementos = [ 'methane', 'n-butane', 'n-heptane', 'n-decane', 'n-tetradecane' ] fracciones = [0.80, 0.14, 0.04, 0.014, 0.006] for elemento, fraccion in zip(elementos, fracciones): GC[elemento] = fraccion GC.sort(True) setup1 = EosSetup.objects.create(eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) setup1.set_interaction('k0', 'methane', 'n-butane', 0.02613) setup1.set_interaction('k0', 'methane', 'n-heptane', 0.05613) setup1.set_interaction('k0', 'methane', 'n-decane', 0.08224) setup1.set_interaction('k0', 'methane', 'n-tetradecane', 0.10738) setup1.set_interaction('lij', 'methane', 'n-butane', -0.06565) setup1.set_interaction('lij', 'methane', 'n-heptane', -0.11875) setup1.set_interaction('lij', 'methane', 'n-decane', -0.14551) setup1.set_interaction('lij', 'methane', 'n-tetradecane', -0.13602) setup1.set_interaction('lij', 'n-butane', 'n-heptane', -0.01754) setup1.set_interaction('lij', 'n-butane', 'n-decane', -0.02433) setup1.set_interaction('lij', 'n-butane', 'n-tetradecane', -0.02203) setup1.set_interaction('lij', 'n-heptane', 'n-decane', -0.00586) setup1.set_interaction('lij', 'n-heptane', 'n-tetradecane', -0.00388) setup1.set_interaction('lij', 'n-decane', 'n-tetradecane', 0.00188) setup_lij_menos20 = EosSetup.objects.create( eos='RKPR', kij_mode=EosSetup.T_DEP, lij_mode=EosSetup.CONSTANTS) setup_lij_menos20.set_interaction_matrix('k0', GC, setup1.k0(GC)) assert_array_equal( setup1.k0(GC) - setup_lij_menos20.k0(GC), np.zeros((5, 5))) setup_lij_menos20.set_interaction_matrix('lij', GC, setup1.lij(GC)) assert_array_equal( setup1.lij(GC) - setup_lij_menos20.lij(GC), np.zeros((5, 5)))
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')
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 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 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())
class TestMixtureAdd(TestCase): def setUp(self): MixtureFraction.objects.all().delete() self.m = Mixture() self.ethane = Compound.objects.get(name='ETHANE') self.co2 = Compound.objects.get(name='CARBON DIOXIDE') self.methane = Compound.objects.get(name='METHANE') def test_simple_add(self): assert MixtureFraction.objects.all().count() == 0 self.m.add(self.ethane, 0.1) self.assertEqual(MixtureFraction.objects.count(), 1) mf = MixtureFraction.objects.all().get() self.assertEqual(mf.mixture, self.m) self.assertEqual(mf.compound, self.ethane) self.assertEqual(mf.fraction, Decimal('0.1')) def test_add_by_name(self): self.m.add('ethane', 0.1) mf = MixtureFraction.objects.all().get() self.assertEqual(mf.mixture, self.m) self.assertEqual(mf.compound, self.ethane) self.assertEqual(mf.fraction, Decimal('0.1')) def test_add_by_formula(self): self.m.add('co2', 0.1) mf = MixtureFraction.objects.all().get() self.assertEqual(mf.mixture, self.m) self.assertEqual(mf.compound, self.co2) self.assertEqual(mf.fraction, Decimal('0.1')) def test_add_fraction_as_str(self): self.m.add('ethane', '0.1') mf = MixtureFraction.objects.all().get() self.assertEqual(mf.mixture, self.m) self.assertEqual(mf.compound, self.ethane) self.assertEqual(mf.fraction, Decimal('0.1')) def test_add_many_iterables(self): self.m.add_many([self.ethane, self.methane, self.co2], [0.1, 0.2, 0.3]) expected = [(self.ethane, Decimal('0.1')), (self.methane, Decimal('0.2')), (self.co2, Decimal('0.3'))] self.assertEqual([i for i in self.m], expected) def test_add_many_string(self): self.m.add_many("ethane methane co2", "0.1 0.2 0.3") expected = [(self.ethane, Decimal('0.1')), (self.methane, Decimal('0.2')), (self.co2, Decimal('0.3'))] self.assertEqual([i for i in self.m], expected) def test_add_many_must_have_same_size(self): with self.assertRaises(ValueError): self.m.add_many("ethane methane", "0.1 0.2 0.3") def test_cant_add_greater_than_1(self): with self.assertRaises(ValueError) as v: self.m.add('ethane', '1.2') self.assertEqual( v.exception.message, 'Add this fraction would exceed 1.0. ' 'Max fraction allowed is 1.0') def test_cant_add_greater_than_remaining(self): self.m.add('ethane', '0.6') with self.assertRaises(ValueError) as v: self.m.add('methane', '0.6') self.assertEqual( v.exception.message, 'Add this fraction would exceed 1.0. ' 'Max fraction allowed is 0.400000') def test_add_without_fraction_add_remaining(self): self.m.add('ethane', '0.6') self.m.add('co2') mf = MixtureFraction.objects.get(mixture=self.m, compound=self.co2) self.assertEqual(mf.fraction, Decimal('0.4'))