class TestNetworks: def setup_Network_tests(self): self.nw = Network(['water'], p_unit='bar', v_unit='m3 / s') self.source = Source('source') self.sink = Sink('sink') def offdesign_TESPyNetworkError(self, **kwargs): with raises(TESPyNetworkError): self.nw.solve('offdesign', **kwargs) def test_Network_linear_dependency(self): """Test network linear dependency.""" self.setup_Network_tests() a = Connection(self.source, 'out1', self.sink, 'in1', m=1, p=1, x=1, T=280) self.nw.add_conns(a) self.nw.solve('design') msg = ('This test must result in a linear dependency of the jacobian ' 'matrix.') assert self.nw.lin_dep, msg def test_Network_no_progress(self): """Test no convergence progress.""" self.setup_Network_tests() pi = Pipe('pipe', pr=1, Q=-100e3) a = Connection(self.source, 'out1', pi, 'in1', m=1, p=1, T=280, fluid={'water': 1}) b = Connection(pi, 'out1', self.sink, 'in1') self.nw.add_conns(a, b) self.nw.solve('design') msg = ('This test must result in a calculation making no progress, as ' 'the pipe\'s outlet enthalpy is below fluid property range.') assert self.nw.progress is False, msg def test_Network_max_iter(self): """Test reaching maximum iteration count.""" self.setup_Network_tests() pi = Pipe('pipe', pr=1, Q=100e3) a = Connection(self.source, 'out1', pi, 'in1', m=1, p=1, T=280, fluid={'water': 1}) b = Connection(pi, 'out1', self.sink, 'in1') self.nw.add_conns(a, b) self.nw.solve('design', max_iter=2) msg = ('This test must result in the itercount being equal to the max ' 'iter statement.') assert self.nw.max_iter == self.nw.iter + 1, msg def test_Network_delete_conns(self): """Test deleting a network's connection.""" self.setup_Network_tests() a = Connection(self.source, 'out1', self.sink, 'in1') self.nw.add_conns(a) self.nw.check_network() msg = ('After the network check, the .checked-property must be True.') assert self.nw.checked, msg self.nw.del_conns(a) msg = ('A connection has been deleted, the network consistency check ' 'must be repeated (.checked-property must be False).') assert self.nw.checked is False, msg def test_Network_missing_connection_in_init_path(self): """Test debug message for missing connection in init_path.""" self.setup_Network_tests() IF = SubsystemInterface('IF') a = Connection(self.source, 'out1', self.sink, 'in1') self.nw.add_conns(a) self.nw.solve('design', init_only=True) self.nw.save('tmp') msg = ('After the network check, the .checked-property must be True.') assert self.nw.checked, msg self.nw.del_conns(a) a = Connection(self.source, 'out1', IF, 'in1') b = Connection(IF, 'out1', self.sink, 'in1') self.nw.add_conns(a, b) self.nw.solve('design', init_path='tmp', init_only=True) msg = ('After the network check, the .checked-property must be True.') assert self.nw.checked, msg shutil.rmtree('./tmp', ignore_errors=True) def test_Network_export_no_chars_busses(self): """Test export of network without characteristics or busses.""" self.setup_Network_tests() a = Connection(self.source, 'out1', self.sink, 'in1') self.nw.add_conns(a) self.nw.solve('design', init_only=True) self.nw.save('tmp') msg = ('The exported network does not contain any char_line, there ' 'must be no file char_line.csv!') assert os.path.isfile('tmp/components/char_line.csv') is False, msg msg = ('The exported network does not contain any char_map, there ' 'must be no file char_map.csv!') assert os.path.isfile('tmp/components/char_map.csv') is False, msg msg = ('The exported network does not contain any busses, there ' 'must be no file bus.csv!') assert os.path.isfile('tmp/components/bus.csv') is False, msg shutil.rmtree('./tmp', ignore_errors=True) def test_Network_reader_no_chars_busses(self): """Test import of network without characteristics or busses.""" self.setup_Network_tests() a = Connection(self.source, 'out1', self.sink, 'in1') self.nw.add_conns(a) self.nw.solve('design', init_only=True) self.nw.save('tmp') imported_nwk = load_network('tmp') imported_nwk.solve('design', init_only=True) msg = ('If the network import was successful the network check ' 'should have been successful, too, but it is not.') assert imported_nwk.checked, msg shutil.rmtree('./tmp', ignore_errors=True) def test_Network_reader_deleted_chars(self): """Test import of network with missing characteristics.""" self.setup_Network_tests() comp = Compressor('compressor') a = Connection(self.source, 'out1', comp, 'in1') b = Connection(comp, 'out1', self.sink, 'in1') self.nw.add_conns(a, b) self.nw.solve('design', init_only=True) self.nw.save('tmp') # # remove char_line and char_map folders os.unlink('tmp/components/char_line.csv') os.unlink('tmp/components/char_map.csv') # import network with missing files imported_nwk = load_network('tmp') imported_nwk.solve('design', init_only=True) msg = ('If the network import was successful the network check ' 'should have been successful, too, but it is not.') assert imported_nwk.checked, msg shutil.rmtree('./tmp', ignore_errors=True) def test_Network_missing_data_in_design_case_files(self): """Test for missing data in design case files.""" self.setup_Network_tests() pi = Pipe('pipe', Q=0, pr=0.95, design=['pr'], offdesign=['zeta']) a = Connection(self.source, 'out1', pi, 'in1', m=1, p=1, T=293.15, fluid={'water': 1}) b = Connection(pi, 'out1', self.sink, 'in1') self.nw.add_conns(a, b) self.nw.solve('design') self.nw.save('tmp') self.nw.save('tmp2') inputs = open('./tmp/connections.csv') all_lines = inputs.readlines() all_lines.pop(len(all_lines) - 1) inputs.close() with open('./tmp2/connections.csv', 'w') as out: for line in all_lines: out.write(line.strip() + '\n') self.offdesign_TESPyNetworkError(design_path='tmp2', init_only=True) shutil.rmtree('./tmp', ignore_errors=True) shutil.rmtree('./tmp2', ignore_errors=True) def test_Network_missing_data_in_individual_design_case_file(self): """Test for missing data in individual design case files.""" self.setup_Network_tests() pi = Pipe('pipe', Q=0, pr=0.95, design=['pr'], offdesign=['zeta']) a = Connection(self.source, 'out1', pi, 'in1', m=1, p=1, T=293.15, fluid={'water': 1}) b = Connection(pi, 'out1', self.sink, 'in1', design_path='tmp2') self.nw.add_conns(a, b) self.nw.solve('design') self.nw.save('tmp') self.nw.save('tmp2') inputs = open('./tmp/connections.csv') all_lines = inputs.readlines() all_lines.pop(len(all_lines) - 1) inputs.close() with open('./tmp2/connections.csv', 'w') as out: for line in all_lines: out.write(line.strip() + '\n') self.offdesign_TESPyNetworkError(design_path='tmp', init_only=True) shutil.rmtree('./tmp', ignore_errors=True) shutil.rmtree('./tmp2', ignore_errors=True) def test_Network_missing_connection_in_design_path(self): """Test for missing connection data in design case files.""" self.setup_Network_tests() pi = Pipe('pipe', Q=0, pr=0.95, design=['pr'], offdesign=['zeta']) a = Connection(self.source, 'out1', pi, 'in1', m=1, p=1, T=293.15, fluid={'water': 1}) b = Connection(pi, 'out1', self.sink, 'in1') self.nw.add_conns(a, b) self.nw.solve('design') self.nw.save('tmp') inputs = open('./tmp/connections.csv') all_lines = inputs.readlines() all_lines.pop(len(all_lines) - 1) inputs.close() with open('./tmp/connections.csv', 'w') as out: for line in all_lines: out.write(line.strip() + '\n') self.offdesign_TESPyNetworkError(design_path='tmp') shutil.rmtree('./tmp', ignore_errors=True)
pib21_k4 = Connection(pib21, 'out1', k4.comps['valve_1'], 'in1', T=Ref(h4_pib21, 1, -pib21.L.val / dT_return), design=['T']) nw.add_conns(k4_pif21, pif21_h4) nw.add_conns(h4_pib21, pib21_k4) nw.add_subsys(h4) # %% busses # heat_losses = Bus('network losses') heat_consumer = Bus('network consumer') nw.check_network() for comp in nw.comps.index: if isinstance(comp, Pipe): comp.set_attr(Tamb=0) heat_losses.add_comps({'comp': comp}) if (isinstance(comp, HeatExchangerSimple) and not isinstance(comp, Pipe)): heat_consumer.add_comps({'comp': comp}) nw.add_busses(heat_losses, heat_consumer) # %% solve # design case: 0 °C ambient temperature
class TestNetworkErrors: def setup(self): self.nw = Network(['water']) def test_add_conns_TypeError(self): with raises(TypeError): self.nw.add_conns(Component('test')) def test_no_connections_error(self): with raises(TESPyNetworkError): self.nw.solve('design') def test_bad_fluids_in_fluid_vector(self): source1 = Source('source1') sink1 = Sink('sink1') a = Connection(source1, 'out1', sink1, 'in1', fluid={'air': 1}) self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.solve('design') def test_duplicate_Connection_labels(self): source1 = Source('source1') source2 = Source('source2') sink1 = Sink('sink1') sink2 = Sink('sink2') a = Connection(source1, 'out1', sink1, 'in1', label='myconn') b = Connection(source2, 'out1', sink2, 'in1', label='myconn') with raises(ValueError): self.nw.add_conns(a, b) def test_Connection_error_source(self): source = Source('source') sink1 = Sink('sink1') sink2 = Sink('sink2') a = Connection(source, 'out1', sink1, 'in1') b = Connection(source, 'out1', sink2, 'in1') self.nw.add_conns(a, b) with raises(TESPyNetworkError): self.nw.check_network() def test_Connection_error_target(self): source1 = Source('source1') source2 = Source('source2') sink = Sink('sink') a = Connection(source1, 'out1', sink, 'in1') b = Connection(source2, 'out1', sink, 'in1') self.nw.add_conns(a, b) with raises(TESPyNetworkError): self.nw.check_network() def test_consistency_inlets(self): merge = Merge('merge') sink = Sink('label') a = Connection(merge, 'out1', sink, 'in1') self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.check_network() def test_consistency_outlets(self): source = Source('source') splitter = Splitter('splitter') a = Connection(source, 'out1', splitter, 'in1') self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.check_network() def test_component_label_duplicates(self): source = Source('label') sink = Sink('label') a = Connection(source, 'out1', sink, 'in1') self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.check_network() def test_missing_offdesign_path(self): source = Source('source') sink = Sink('sink') a = Connection(source, 'out1', sink, 'in1') self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.solve('offdesign') def test_bad_mode_specification(self): source = Source('source') sink = Sink('sink') a = Connection(source, 'out1', sink, 'in1') self.nw.add_conns(a) with raises(ValueError): self.nw.solve('ofdesign') def test_underdetermination(self): source = Source('source') sink = Sink('sink') a = Connection(source, 'out1', sink, 'in1', m=1) self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.solve('design') def test_overdetermination(self): source = Source('source') sink = Sink('sink') a = Connection(source, 'out1', sink, 'in1', m=1, p=1e5, x=1, h=1e6, fluid={'water': 1}, fluid_balance=True) self.nw.add_conns(a) with raises(TESPyNetworkError): self.nw.solve('design') def test_add_Bus_TypeError(self): source = Source('label') sink = Sink('label') a = Connection(source, 'out1', sink, 'in1') with raises(TypeError): self.nw.add_busses(a) def test_Bus_duplicate(self): with raises(TESPyNetworkError): b = Bus('mybus') self.nw.add_busses(b, b) def test_buslabel_duplicate(self): with raises(TESPyNetworkError): a = Bus('mybus') b = Bus('mybus') self.nw.add_busses(a, b)