def test_validate(self): filename = os.path.abspath("tests/geojson/data/geojson_1.json") json = UrbanOptGeoJson(filename) valid, results = json.validate() self.assertFalse(valid) self.assertEqual(len(results["building"]), 3) self.assertEqual(results["building"][0]["id"], "5a6b99ec37f4de7f94020090")
def test_heating_indirect(self): project_name = "heating_indirect" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "time_series_ex1.json") self.gj = UrbanOptGeoJson(filename) # scaffold the project ourselves scaffold = Scaffold(self.output_dir, project_name) scaffold.create() # load system parameter data filename = os.path.join(self.data_dir, "time_series_system_params_ets.json") sys_params = SystemParameters(filename) # currently we must setup the root project before we can run to_modelica package = PackageParser.new_from_template(scaffold.project_path, scaffold.project_name, order=[]) package.save() # now test the connector (independent of the larger geojson translator) geojson_load_id = self.gj.buildings[0].feature.properties["id"] self.heating_indirect = HeatingIndirect(sys_params, geojson_load_id) self.heating_indirect.to_modelica(scaffold) root_path = os.path.abspath( os.path.join(scaffold.substations_path.files_dir)) geojson_id = self.gj.buildings[0].feature.properties["id"] model_filepath = os.path.join(root_path, f'HeatingIndirect_{geojson_id}.mo') self.assertTrue(os.path.exists(model_filepath), f"File does not exist: {model_filepath}")
def test_validate(self): filename = os.path.join(self.data_dir, "geojson_1_invalid.json") with self.assertRaises(GeoJsonValidationError) as ctx: UrbanOptGeoJson(filename) self.assertIn("is not valid under any of the given schemas", str(ctx.exception))
def test_district_system(self): project_name = "chilled_water_plant_stub" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "time_series_ex1.json") self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = os.path.join(self.data_dir, "time_series_system_params_ets.json") sys_params = SystemParameters(filename) cooling_plant = CoolingPlant(sys_params) # create chilled water stub for the ets chilled_water_stub = NetworkChilledWaterStub(sys_params) cp_cw_coupling = Coupling(cooling_plant, chilled_water_stub) graph = CouplingGraph([ cp_cw_coupling, ]) district = District(root_dir=self.output_dir, project_name=project_name, system_parameters=sys_params, coupling_graph=graph) district.to_modelica() root_path = os.path.abspath( os.path.join(district._scaffold.districts_path.files_dir)) self.run_and_assert_in_docker( os.path.join(root_path, 'DistrictEnergySystem.mo'), project_path=district._scaffold.project_path, project_name=district._scaffold.project_name)
def setUp(self): project_name = "base_classes" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "spawn_geojson_ex1.json") self.gj = UrbanOptGeoJson(filename)
def setUp(self): self.project_name = 'district_heating_and_cooling_systems' self.data_dir, self.output_dir = self.set_up(Path(__file__).parent, self.project_name) # load in the example geojson with a single office building filename = Path(self.data_dir) / "time_series_ex1.json" self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = Path(self.data_dir) / "time_series_system_params_ets.json" self.sys_params = SystemParameters(filename)
def setUp(self): self.project_name = 'mixed_loads' _, self.output_dir = self.set_up( Path(__file__).parent, self.project_name) filename = self.SHARED_DATA_DIR / 'mixed_loads_district' / 'geojson.json' self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = self.SHARED_DATA_DIR / 'mixed_loads_district' / 'system_params.json' self.sys_params = SystemParameters(filename)
def setUp(self): self.project_name = 'teaser_district_heating_and_cooling_systems' self.data_dir, self.output_dir = self.set_up( Path(__file__).parent, self.project_name) filename = Path(self.data_dir) / "teaser_geojson_two_loads.json" self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = Path(self.data_dir) / "teaser_system_params_two_loads.json" self.sys_params = SystemParameters(filename)
def from_geojson(cls, filename): """ Initialize the translator from a GeoJSON file :param filename: string, GeoJSON file :return: object, GeoJsonModelicaTranslator """ if os.path.exists(filename): json = UrbanOptGeoJson(filename) klass = GeoJsonModelicaTranslator() klass.buildings = json.buildings return klass else: raise Exception(f"GeoJSON file does not exist: {filename}")
def test_district_system(self): project_name = "district_system" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "time_series_ex1.json") self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = os.path.join(self.data_dir, "time_series_system_params_ets.json") sys_params = SystemParameters(filename) # Create the time series load, ets and their coupling time_series_load = TimeSeries(sys_params, self.gj.buildings[0]) geojson_load_id = self.gj.buildings[0].feature.properties["id"] cooling_indirect_system = CoolingIndirect(sys_params, geojson_load_id) ts_ci_coupling = Coupling(time_series_load, cooling_indirect_system) # create chilled water stub for the ets chilled_water_stub = NetworkChilledWaterStub(sys_params) ci_cw_coupling = Coupling(cooling_indirect_system, chilled_water_stub) # create hot water stub for the load hot_water_stub = EtsHotWaterStub(sys_params) ts_hw_coupling = Coupling(time_series_load, hot_water_stub) graph = CouplingGraph([ ts_ci_coupling, ci_cw_coupling, ts_hw_coupling, ]) district = District(root_dir=self.output_dir, project_name=project_name, system_parameters=sys_params, coupling_graph=graph) district.to_modelica() root_path = os.path.abspath( os.path.join(district._scaffold.districts_path.files_dir)) self.run_and_assert_in_docker( os.path.join(root_path, 'DistrictEnergySystem.mo'), project_path=district._scaffold.project_path, project_name=district._scaffold.project_name)
def test_Teaser_heating(self): project_name = 'teaser_district_heating' self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "teaser_geojson_two_loads.json") self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = os.path.join(self.data_dir, "teaser_system_params_two_loads.json") sys_params = SystemParameters(filename) # create network and plant network = Network2Pipe(sys_params) heating_plant = HeatingPlantWithOptionalCHP(sys_params) # create our our load/ets/stubs all_couplings = [Coupling(network, heating_plant)] for geojson_load in self.gj.buildings: teaser_load = Teaser(sys_params, geojson_load) geojson_load_id = geojson_load.feature.properties["id"] heating_indirect_system = HeatingIndirect(sys_params, geojson_load_id) cold_water_stub = EtsColdWaterStub(sys_params) all_couplings.append(Coupling(teaser_load, heating_indirect_system)) all_couplings.append(Coupling(teaser_load, cold_water_stub)) all_couplings.append(Coupling(heating_indirect_system, network)) # create the couplings and graph graph = CouplingGraph(all_couplings) district = District(root_dir=self.output_dir, project_name=project_name, system_parameters=sys_params, coupling_graph=graph) district.to_modelica() root_path = os.path.abspath( os.path.join(district._scaffold.districts_path.files_dir)) self.run_and_assert_in_docker( os.path.join(root_path, 'DistrictEnergySystem.mo'), project_path=district._scaffold.project_path, project_name=district._scaffold.project_name)
def test_no_ets_and_run(self): project_name = "time_series_no_ets" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "time_series_ex1.json") self.gj = UrbanOptGeoJson(filename) # scaffold the project ourselves scaffold = Scaffold(self.output_dir, project_name) scaffold.create() # load system parameter data filename = os.path.join(self.data_dir, "time_series_system_params_no_ets.json") sys_params = SystemParameters(filename) # now test the connector (independent of the larger geojson translator) self.time_series = TimeSeries(sys_params, self.gj.buildings[0]) self.assertIsNotNone(self.time_series) self.assertIsNotNone(self.time_series.building) self.assertEqual( "time_series", self.time_series.system_parameters.get_param("buildings.custom")[0] ["load_model"]) # currently we must setup the root project before we can run to_modelica package = PackageParser.new_from_template(scaffold.project_path, scaffold.project_name, order=[]) package.save() self.time_series.to_modelica(scaffold) root_path = os.path.abspath( os.path.join(scaffold.loads_path.files_dir, 'B5a6b99ec37f4de7f94020090')) files = [ os.path.join(root_path, 'building.mo'), ] # verify that there are only 2 files that matter (coupling and building) for file in files: self.assertTrue(os.path.exists(file), f"File does not exist: {file}")
def test_chp_system(self): self.project_name = 'heat_with_chp' self.data_dir, self.output_dir = self.set_up( Path(__file__).parent, self.project_name) # load in the example geojson with a single office building filename = Path(self.data_dir) / "time_series_ex1.json" self.gj = self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = Path(self.data_dir) / "time_series_system_params_chp.json" self.sys_params = SystemParameters(filename) # create network and plant network = Network2Pipe(self.sys_params) heating_plant = HeatingPlantWithOptionalCHP(self.sys_params) # create our our load/ets/stubs all_couplings = [Coupling(network, heating_plant)] for geojson_load in self.gj.buildings: time_series_load = TimeSeries(self.sys_params, geojson_load) geojson_load_id = geojson_load.feature.properties["id"] heating_indirect_system = HeatingIndirect(self.sys_params, geojson_load_id) cold_water_stub = EtsColdWaterStub(self.sys_params) all_couplings.append( Coupling(time_series_load, heating_indirect_system)) all_couplings.append(Coupling(time_series_load, cold_water_stub)) all_couplings.append(Coupling(heating_indirect_system, network)) # create the couplings and graph graph = CouplingGraph(all_couplings) district = District(root_dir=self.output_dir, project_name=self.project_name, system_parameters=self.sys_params, coupling_graph=graph) district.to_modelica()
def __init__( self, geojson_filepath, sys_params_filepath, root_dir, project_name, ): """Create an instance of this class :param geojson_filepath: str, path to GeoJSON file :param sys_params_filepath: str, path to system parameters file :param root_dir: str, where to create the package :project_name: str, name of the package """ if not Path(geojson_filepath).exists(): raise FileNotFoundError( f'GeoJSON file path does not exist: {geojson_filepath}') if not Path(sys_params_filepath).exists(): raise FileNotFoundError( f'System parameters file path does not exist: {sys_params_filepath}' ) self._system_parameters = SystemParameters(sys_params_filepath) geojson_ids = self._system_parameters.get_default( '$.buildings.custom[*].geojson_id', []) self._geojson = UrbanOptGeoJson(geojson_filepath, geojson_ids) self._root_dir = root_dir self._project_name = project_name self._couplings = _parse_couplings(self._geojson, self._system_parameters) self._coupling_graph = CouplingGraph(self._couplings) self._district = District(self._root_dir, self._project_name, self._system_parameters, self._coupling_graph) self._package_created = False
def test_spawn_single(self): project_name = "spawn_single" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "spawn_geojson_ex1.json") self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = os.path.join(self.data_dir, "spawn_system_params_ex1.json") sys_params = SystemParameters(filename) # build spawn model with hot and cold water stubbed out spawn = Spawn(sys_params, self.gj.buildings[0]) hot_stub = EtsHotWaterStub(sys_params) cold_stub = EtsColdWaterStub(sys_params) graph = CouplingGraph([ Coupling(spawn, hot_stub), Coupling(spawn, cold_stub), ]) district = District(root_dir=self.output_dir, project_name=project_name, system_parameters=sys_params, coupling_graph=graph) district.to_modelica() root_path = os.path.abspath( os.path.join(district._scaffold.districts_path.files_dir)) self.run_and_assert_in_docker( os.path.join(root_path, 'DistrictEnergySystem.mo'), project_path=district._scaffold.project_path, project_name=district._scaffold.project_name)
def test_load_geojson(self): filename = os.path.join(self.data_dir, "geojson_1.json") json = UrbanOptGeoJson(filename) self.assertIsNotNone(json.data) self.assertEqual(len(json.data.features), 4)
def test_mft_time_series_to_modelica_and_run(self): project_name = "time_series_massflow" self.data_dir, self.output_dir = self.set_up(os.path.dirname(__file__), project_name) # load in the example geojson with a single office building filename = os.path.join(self.data_dir, "time_series_ex1.json") self.gj = UrbanOptGeoJson(filename) # load system parameter data filename = os.path.join(self.data_dir, "time_series_system_params_massflow_ex1.json") sys_params = SystemParameters(filename) # create the load, ETSes and their couplings time_series_mft_load = TimeSeriesMFT(sys_params, self.gj.buildings[0]) geojson_load_id = self.gj.buildings[0].feature.properties["id"] heating_indirect_system = HeatingIndirect(sys_params, geojson_load_id) ts_hi_coupling = Coupling(time_series_mft_load, heating_indirect_system) cooling_indirect_system = CoolingIndirect(sys_params, geojson_load_id) ts_ci_coupling = Coupling(time_series_mft_load, cooling_indirect_system) # create network stubs for the ETSes heated_water_stub = NetworkHeatedWaterStub(sys_params) hi_hw_coupling = Coupling(heating_indirect_system, heated_water_stub) chilled_water_stub = NetworkChilledWaterStub(sys_params) ci_cw_coupling = Coupling(cooling_indirect_system, chilled_water_stub) # build the district system district = District(root_dir=self.output_dir, project_name=project_name, system_parameters=sys_params, coupling_graph=CouplingGraph([ ts_hi_coupling, ts_ci_coupling, hi_hw_coupling, ci_cw_coupling, ])) district.to_modelica() root_path = os.path.abspath( os.path.join(district._scaffold.districts_path.files_dir)) mo_file_name = os.path.join(root_path, 'DistrictEnergySystem.mo') # set the run time to 31536000 (full year in seconds) mofile = Model(mo_file_name) mofile.update_model_annotation({"experiment": {"StopTime": 31536000}}) mofile.save() self.run_and_assert_in_docker( mo_file_name, project_path=district._scaffold.project_path, project_name=district._scaffold.project_name) # Check the results results_dir = f'{district._scaffold.project_path}_results' mat_file = f'{results_dir}/time_series_massflow_Districts_DistrictEnergySystem_result.mat' mat_results = Reader(mat_file, 'dymola') # hack to get the name of the loads (rather the 8 character connector shas) timeseries_load_var = None coolflow_var = None heatflow_var = None for var in mat_results.varNames(): m = re.match("TimeSerMFTLoa_(.{8})", var) if m: timeseries_load_var = m[1] continue m = re.match("cooInd_(.{8})", var) if m: coolflow_var = m[1] continue m = re.match("heaInd_(.{8})", var) if m: heatflow_var = m[1] continue if None not in (timeseries_load_var, coolflow_var, heatflow_var): break (time1, ts_hea_load) = mat_results.values( f"TimeSerMFTLoa_{timeseries_load_var}.ports_aChiWat[1].m_flow") (_time1, ts_chi_load) = mat_results.values( f"TimeSerMFTLoa_{timeseries_load_var}.ports_aHeaWat[1].m_flow") (_time1, cool_q_flow) = mat_results.values(f"cooInd_{coolflow_var}.Q_flow") (_time1, heat_q_flow) = mat_results.values(f"heaInd_{heatflow_var}.Q_flow") # if any of these assertions fail, then it is likely that the change in the timeseries massflow model # has been updated and we need to revalidate the models. self.assertEqual(ts_hea_load.min(), 0) self.assertAlmostEqual(ts_hea_load.max(), 51, delta=1) self.assertAlmostEqual(ts_hea_load.mean(), 4, delta=1) self.assertEqual(ts_chi_load.min(), 0) self.assertAlmostEqual(ts_chi_load.max(), 61, delta=1) self.assertAlmostEqual(ts_chi_load.mean(), 4, delta=1) self.assertAlmostEqual(cool_q_flow.min(), -51750, delta=10) self.assertAlmostEqual(cool_q_flow.max(), 354100, delta=10) self.assertAlmostEqual(cool_q_flow.mean(), 3160, delta=10) self.assertAlmostEqual(heat_q_flow.min(), -343210, delta=10) self.assertAlmostEqual(heat_q_flow.max(), 39475, delta=10) self.assertAlmostEqual(heat_q_flow.mean(), -23270, delta=10)
def test_valid_instance(self): """No exception should be raised when the geojson file is valid""" filename = os.path.join(self.data_dir, "geojson_1.json") UrbanOptGeoJson(filename)
def test_missing_file(self): fn = "non-existent-path" with self.assertRaises(Exception) as exc: UrbanOptGeoJson(fn) self.assertEqual(f"URBANopt GeoJSON file does not exist: {fn}", str(exc.exception))
def test_load_geojson(self): filename = os.path.abspath("tests/geojson/data/geojson_1.json") json = UrbanOptGeoJson(filename) self.assertIsNotNone(json.data) self.assertEqual(len(json.data.features), 4)