def test_pipe_zero_energy_flow(self):
     node0 = ConnectionNode()
     node1 = ConnectionNode()
     node0.set_elevation(10, 'm')
     node1.set_elevation(10, 'm')
     node1.set_pressure(10, 'psi')
     node0.set_pressure(10, 'psi')
     self.sys_pipe.output_node = node1
     self.sys_pipe.input_node = node0
     self.sys_pipe.set_c_coefficient(100)
     self.sys_pipe.set_length(100, 'm')
     self.sys_pipe.set_inner_diam(10.75, 'in')
     self.sys_pipe.calculate_gpm_flow()
     self.assertAlmostEqual(self.sys_pipe.get_vol_flow('gpm'), 0, 5)
 def test_pipe_negative_energy_flow(self):
     node0 = ConnectionNode()
     node1 = ConnectionNode()
     node0.set_elevation(10, 'm')
     node1.set_elevation(10, 'm')
     node1.set_pressure(11.26528826, 'psi')
     node0.set_pressure(10, 'psi')
     self.sys_pipe.output_node = node1
     self.sys_pipe.input_node = node0
     self.sys_pipe.set_c_coefficient(100)
     self.sys_pipe.set_length(100, 'm')
     self.sys_pipe.set_inner_diam(10.75, 'in')
     self.sys_pipe.calculate_gpm_flow()
     self.assertClose(self.sys_pipe.get_vol_flow('gpm'), -1135.4647, 5)
class NodeTests(unittest.TestCase):
    def setUp(self):
        self.sys_node = ConnectionNode()

    def test_creation(self):
        self.assertTrue(isinstance(self.sys_node, ConnectionNode))

    def test_pressure(self):
        self.sys_node.set_pressure(8, "psi")
        self.assertEqual(self.sys_node.get_pressure("psi"), 8)
        self.assertAlmostEqual(self.sys_node.get_pressure("Pa"), 8 * 6894.757)
        self.assertAlmostEqual(self.sys_node.get_pressure('kPa'), 8 * 6.894757)
        self.assertAlmostEqual(self.sys_node.get_pressure('mH2O'), 5.625996944)
        self.sys_node.set_pressure(40000, 'Pa')
        self.assertAlmostEqual(self.sys_node.get_pressure('psi'),
                               5.80150975589)
        self.sys_node.set_pressure(100, 'mH2O')
        self.assertAlmostEqual(self.sys_node.get_pressure('kPa'), 980.638)
        self.sys_node.set_pressure(15, 'psi')

    def test_elevation(self):
        self.sys_node.set_elevation(8, 'm')
        self.assertEqual(self.sys_node.get_elevation('m'), 8)
        elevation = self.sys_node.get_elevation('ft')
        self.assertAlmostEqual(elevation, 8 * 3.28083989)

    def test_when_elevation_and_pressure_set_get_energy(self):
        self.sys_node.set_elevation(20, 'm')
        self.sys_node.set_pressure(20, 'mH2O')
        self.assertEqual(self.sys_node.get_energy('mH2O'), 40)
        self.sys_node.set_pressure(15, 'psi')
        self.assertAlmostEqual(self.sys_node.get_energy('psi'), 43.439404)

    def test_name(self):
        self.sys_node.name = 1
        self.assertEqual(self.sys_node.name, '1')

    def test_when_elevation_and_energy_set_get_pressure(self):
        self.sys_node.set_elevation(20, 'm')
        self.sys_node.set_energy(35, 'mH2O')
        self.assertEqual(self.sys_node.get_pressure('mH2O'), 15)
        self.sys_node.set_pressure(10, 'mH2O')
        self.assertEqual(self.sys_node.get_energy('mH2O'), 30)

    def test_input_pipe(self):
        sys_pipe = Pipe()
        sys_pipe2 = Pipe()
        self.sys_node.set_input_pipe(sys_pipe)
        self.sys_node.set_input_pipe(sys_pipe2)
        self.assertTrue(sys_pipe in self.sys_node.get_input_pipes())
        self.assertTrue(sys_pipe2 in self.sys_node.get_input_pipes())

    def test_output_pipe(self):
        sys_pipe = Pipe()
        self.sys_node.set_output_pipe(sys_pipe)
        self.assertTrue(sys_pipe in self.sys_node.get_output_pipes())

    def test_end_node_energy(self):
        node = EndNode()
        node.set_elevation(10, 'm')
        self.assertEqual(node.get_energy('mH2O'), 10)
        self.assertAlmostEqual(node.get_energy('psi'), 14.219702)

    def test_initial_flow_output(self):
        self.assertEqual(self.sys_node.get_output_flow('gpm'), 0)

    def test_flow_output(self):
        self.sys_node.set_output_flow(20, 'gpm')
        self.assertEqual(self.sys_node.get_output_flow('gpm'), 20)
        self.assertAlmostEqual(self.sys_node.get_output_flow('m3/H'), 4.5425,
                               4)

    def test_input_node_is_connection_node(self):
        in_node = InputNode()
        self.assertTrue(isinstance(in_node, ConnectionNode))
class NozzleTests(unittest.TestCase):
    def setUp(self):
        self.nozzle0 = Nozzle()
        self.in_node = ConnectionNode()
        self.out_node = EndNode()

    def test_creation(self):
        self.assertTrue(isinstance(self.nozzle0, Nozzle))
        self.assertTrue(isinstance(self.nozzle0, Edge))

    def test_name(self):
        self.nozzle0.name = 3
        self.assertEqual(self.nozzle0.name, '3')

    def test_input_node(self):
        other_node = ConnectionNode()
        self.nozzle0.input_node = self.in_node
        self.assertEqual(self.nozzle0.input_node, self.in_node)
        with self.assertRaises(IndexError):
            self.nozzle0.input_node = other_node

    def test_false_input(self):
        false_input = 'false node'
        with self.assertRaises(ValueError):
            self.nozzle0.input_node = false_input

    def test_output(self):
        self.nozzle0.output_node = self.out_node
        self.assertEqual(self.nozzle0.output_node, self.out_node)

    def test_end_output(self):
        with self.assertRaises(ValueError):
            self.nozzle0.output_node = self.in_node

    def test_repeated_output(self):
        other_node = EndNode()
        self.nozzle0.output_node = self.out_node
        with self.assertRaises(ValueError):
            self.nozzle0.output_node = other_node

    def test_k_factor(self):
        self.nozzle0.set_factor(1.2, 'gpm/psi^0.5')
        k_lpm = self.nozzle0.get_factor('lpm/bar^0.5')
        self.assertAlmostEqual(k_lpm, 17.29954989, 5)
        self.nozzle0.set_factor(17.29954989, 'lpm/bar^0.5')
        self.assertAlmostEqual(self.nozzle0.get_factor('gpm/psi^0.5'), 1.2)

    def test_gpm_flow(self):
        self.nozzle0.set_factor(2, 'gpm/psi^0.5')
        self.in_node.set_elevation(5, 'm')
        self.in_node.set_pressure(36, 'psi')
        self.out_node.set_elevation(5, 'm')
        self.nozzle0.input_node = self.in_node
        self.nozzle0.output_node = self.out_node
        nozzle_flow = self.nozzle0.calculate_gpm_flow()
        self.assertAlmostEqual(nozzle_flow, 12)

    def test_required_pressure(self):
        self.nozzle0.set_required_pressure(30, 'psi')
        self.assertEqual(self.nozzle0.get_required_pressure('psi'), 30)

    def test_complete_nozzle(self):
        self.nozzle0.input_node = self.in_node
        self.nozzle0.output_node = self.out_node
        self.nozzle0.set_required_pressure(30, 'psi')
        self.nozzle0.set_factor(1.2, 'gpm/psi^0.5')
        self.assertTrue(self.nozzle0.is_complete())

    def test_k_complete(self):
        in_node = ConnectionNode()
        out_node = EndNode()
        self.nozzle0.input_node = in_node
        self.nozzle0.output_node = out_node
        self.nozzle0.set_required_pressure(30, 'psi')
        self.assertFalse(self.nozzle0.is_complete())