示例#1
0
    def test_model_update_annotation_and_add_connect(self):
        """Model annotation should always the last statement for the model"""
        # Setup
        mo_file = """
model Test
equation
end Test;"""
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        # make an insert before and after annotation
        model.add_connect('port_a', 'port_b')
        model.update_model_annotation({'rootModification': 100})
        model.add_connect('port_c', 'port_d')
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(source_file, self.result, [
            'annotation(rootModification=100)',
            'connect(port_a, port_b)',
            'connect(port_c, port_d)',
        ])
        self.assertNoDeletions(source_file, self.result)
        # make sure the annotation was inserted at the end
        self.assertIn('annotation(rootModification=100);\nend Test;',
                      self.result,
                      'Annotation should be at the END of the model')
示例#2
0
    def test_model_update_annotation_can_overwrite_existing_nested_modifications(
            self):
        """Annotation should always be at the end of the model"""
        # Setup
        mo_file = """
model Test
equation
  annotation(rootModification=321, modA(hello=100));
end Test;"""
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        model.update_model_annotation({
            'rootModification': 100,
            'modA': {
                'world': 555,
                'OVERWRITE_MODIFICATIONS': True
            }
        })
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(source_file, self.result, [
            'annotation(rootModification=100, modA(world=555))',
        ])
        self.assertHasDeletions(
            source_file, self.result,
            ['annotation(rootModification=321, modA(hello=100));'])
示例#3
0
    def test_model_update_annotation_can_update_and_insert(self):
        # Setup
        mo_file = """
model Test
equation
  annotation(rootModification=321, rootModification2(childA=0, childB=1));
end Test;"""
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        model.update_model_annotation({
            'rootModification': 100,
            'insertedModification': 555,
            'rootModification2': {
                'childB': 123,
                'childC': 'abc'
            }
        })
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(source_file, self.result, [
            'annotation(rootModification=100, rootModification2(childA=0, childB=123, childC=abc), insertedModification=555);'
        ])
        self.assertHasDeletions(source_file, self.result, [
            'annotation(rootModification=321, rootModification2(childA=0, childB=1));'
        ])
示例#4
0
    def test_model_update_annotation_when_modification_exists_and_has_own_modifications(
            self):
        # Setup
        mo_file = """
model Test
equation
  annotation(rootModification(childModification=321));
end Test;"""
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        model.update_model_annotation(
            {'rootModification': {
                'childModification': 100
            }})
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(
            source_file, self.result,
            ['annotation(rootModification(childModification=100));'])
        self.assertHasDeletions(
            source_file, self.result,
            ['annotation(rootModification(childModification=321));'])
示例#5
0
    def test_model_update_annotation_when_no_model_annotation_exists(self):
        # Setup
        mo_file = '''
model Test
equation
end Test;'''
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        model.update_model_annotation({'rootModification': 100})
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(source_file, self.result,
                                ['annotation(rootModification=100);'])
示例#6
0
    def test_model_update_annotation_keeps_other_modifications_not_updated(
            self):
        # Setup
        mo_file = """
model Test
equation
  annotation(rootModification=321);
end Test;"""
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        model.update_model_annotation({'insertedModification': 555})
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(
            source_file, self.result,
            ['annotation(rootModification=321, insertedModification=555);'])
        self.assertHasDeletions(source_file, self.result,
                                ['annotation(rootModification=321);'])
示例#7
0
    def test_model_update_annotation_can_overwrite_existing_modifications(
            self):
        # Setup
        mo_file = """
model Test
equation
  annotation(rootModification=321);
end Test;"""
        source_file = self.create_tmp_file(mo_file)
        model = Model(source_file)

        # Act
        model.update_model_annotation({
            'modA': 100,
            'OVERWRITE_MODIFICATIONS': True
        })
        self.result = model.execute()

        # Assert
        self.assertHasAdditions(source_file, self.result, [
            'annotation(modA=100)',
        ])
        self.assertHasDeletions(source_file, self.result,
                                ['annotation(rootModification=321);'])
    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)