diff --git a/tests/test_uesgraphs.py b/tests/test_uesgraphs.py index dfd7f20..43ff736 100644 --- a/tests/test_uesgraphs.py +++ b/tests/test_uesgraphs.py @@ -199,3 +199,71 @@ def test_remove_dead_ends(self): assert len(example_district.edges()) == 12, msg assert removed == [1015], msg + + def test_to_from_json(self): + """Tests the output and input of an uesgraph to JSON + """ + workspace = ug.make_workspace('json_output') + + example_district = ug.simple_bidirectional_network_model() + example_district.to_json(path=workspace, + name='demo', + all_data=True) + example_district_import = ug.UESGraph() + example_district_import.from_json(path=workspace, + network_type='heating') + + assert (len(example_district_import.nodelist_building) == 5) + + mapping = {} + for b in example_district.nodelist_building: + name = example_district.nodes[b]['name'] + for n in example_district_import.nodelist_building: + if name==example_district_import.nodes[n]['name']: + mapping[b] = n + for b in example_district.nodelist_building: + if example_district.nodes[b]['name'] == 'building_1': + assert example_district_import.nodes[mapping[b]][ + 'is_supply_heating'] is True + assert example_district_import.nodes[mapping[b]][ + 'is_supply_cooling'] is True + assert 'input_heat' in example_district_import.nodes[ + mapping[b]] + assert 'input_cool' in example_district_import.nodes[ + mapping[b]] + elif example_district.nodes[b]['name'] == 'building_2': + assert example_district_import.nodes[mapping[b]][ + 'is_supply_heating'] is True + assert example_district_import.nodes[mapping[b]][ + 'is_supply_cooling'] is True + assert 'input_heat' in example_district_import.nodes[ + mapping[b]] + assert 'input_cool' in example_district_import.nodes[ + mapping[b]] + elif example_district.nodes[b]['name'] == 'building_3': + assert example_district_import.nodes[mapping[b]][ + 'is_supply_heating'] is True + if 'is_supply_cooling' in example_district_import.nodes[ + mapping[b]]: + assert example_district_import.nodes[mapping[b]][ + 'is_supply_cooling'] is False + assert 'input_cool' in example_district_import.nodes[ + mapping[b]] + elif example_district.nodes[b]['name'] == 'building_4': + if 'is_supply_heating' in example_district_import.nodes[ + mapping[b]]: + assert example_district_import.nodes[mapping[b]][ + 'is_supply_heating'] is False + assert example_district_import.nodes[mapping[b]][ + 'is_supply_cooling'] is True + assert 'input_heat' in example_district_import.nodes[ + mapping[b]] + elif example_district.nodes[b]['name'] == 'idealPlant': + assert example_district_import.nodes[mapping[b]][ + 'is_supply_heating'] is True + assert example_district_import.nodes[mapping[b]][ + 'is_supply_cooling'] is True + assert 'input_heat' not in example_district_import.nodes[ + mapping[b]] + assert 'input_cool' not in example_district_import.nodes[ + mapping[b]] \ No newline at end of file diff --git a/uesgraphs/examples/example_uesgraph.py b/uesgraphs/examples/example_uesgraph.py index 882fc70..a9c8611 100644 --- a/uesgraphs/examples/example_uesgraph.py +++ b/uesgraphs/examples/example_uesgraph.py @@ -126,6 +126,56 @@ def simple_dhc_model(): return example_district +def simple_bidirectional_network_model(): + """Initializes an UESGraph object, adds 1 bidirectional network + + Returns + ------- + example_district : uesgraphs.uesgraph.UESGraph object + An UESGraph containing 4 buildings connected to 1 ideal plant. + """ + example_district = ug.UESGraph() + ideal_plant_1 = example_district.add_building(name='idealPlant', + position=Point(1, 2), + is_supply_heating=True, + is_supply_cooling=True) + + building_1 = example_district.add_building(name='building_1', + position=Point(2, 3), + is_supply_heating=True, + is_supply_cooling=True, + input_heat=[1500, 1000, 500], + input_cool=[0, 500, 1000]) + building_2 = example_district.add_building(name='building_2', + position=Point(3, 3), + is_supply_heating=True, + is_supply_cooling=True, + input_heat=[1500, 1000, 500], + input_cool=[0, 500, 1000]) + building_3 = example_district.add_building(name='building_3', + position=Point(4, 3), + is_supply_heating=True, + input_cool=[0, 500, 1000]) + building_4 = example_district.add_building(name='building_4', + position=Point(5, 3), + is_supply_cooling=True, + input_heat=[1500, 1000, 500]) + + n_1 = example_district.add_network_node(network_type='heating', + position=Point(2, 2)) + n_2 = example_district.add_network_node(network_type='heating', + position=Point(3, 2)) + n_3 = example_district.add_network_node(network_type='heating', + position=Point(3, 1)) + example_district.add_edge(ideal_plant_1, n_1) + example_district.add_edge(n_1, building_1) + example_district.add_edge(n_1, n_2) + example_district.add_edge(n_2, building_2) + example_district.add_edge(ideal_plant_1, n_3) + example_district.add_edge(n_3, building_3) + example_district.add_edge(n_3, building_4) + + return example_district def add_more_networks(example_district): """Adds an electric grid to the simple_dhc_model example diff --git a/uesgraphs/uesgraph.py b/uesgraphs/uesgraph.py index bc6bf1e..ee04989 100644 --- a/uesgraphs/uesgraph.py +++ b/uesgraphs/uesgraph.py @@ -786,8 +786,27 @@ def from_json(self, path, network_type, check_overlap=False): # Add buildings if 'building' in node['node_type']: building_id = node['name'] - new_node = self.add_building(name=building_id, - position=this_position) + if ('is_supply_heating' in node and + 'is_supply_cooling' in node and + node['is_supply_heating'] is True and + node['is_supply_cooling'] is True): + new_node = self.add_building(name=building_id, + position=this_position, + is_supply_heating=True, + is_supply_cooling=True) + elif ('is_supply_heating' in node and + node['is_supply_heating'] is True): + new_node = self.add_building(name=building_id, + position=this_position, + is_supply_heating=True) + elif ('is_supply_cooling' in node and + node['is_supply_cooling'] is True): + new_node = self.add_building(name=building_id, + position=this_position, + is_supply_cooling=True) + else: + new_node = self.add_building(name=building_id, + position=this_position) # Add supplies @@ -935,10 +954,15 @@ def to_json(self, path, name, description='json export from uesgraph', if 'node_type' in self.nodes[node]: node_type = self.nodes[node]['node_type'] if 'building' in node_type: - if 'is_supply_heating' in self.nodes[node]: + if ('is_supply_heating' in self.nodes[node] and + 'is_supply_cooling' in self.nodes[node]): + if (self.nodes[node]['is_supply_heating'] is True and + self.nodes[node]['is_supply_cooling'] is True): + node_type = 'building' + elif 'is_supply_heating' in self.nodes[node]: if self.nodes[node]['is_supply_heating'] is True: node_type = 'supply_heating' - if 'is_supply_cooling' in self.nodes[node]: + elif 'is_supply_cooling' in self.nodes[node]: if self.nodes[node]['is_supply_cooling'] is True: node_type = 'supply_cooling' nodes[-1]['node_type'] = node_type