def create_model(sys_param_file: Path, geojson_feature_file: Path, project_path: Path, overwrite: bool):
    """Build Modelica model from user data

    SYS_PARAM_FILE: Path/name to sys-param file, possibly created with this CLI.

    GEOJSON_FEATURE_FILE: Path to sdk json feature file with data about the buildings.

    PROJECT_PATH: Path for Modelica project directory created with this command

    \f
    :param model_type: String, type of model to create
    :param sys_param_file: Path, location and name of file created with this cli
    :param geojson_feature_file: Path, location and name of sdk feature_file
    :param project_path: Path, location and name of Modelica model dir to be created
    :param overwrite: Boolean, flag to overwrite an existing file of the same name/location
    """
    project_path = Path(project_path)
    if project_path.exists():
        if overwrite:
            rmtree(project_path, ignore_errors=True)
        else:
            raise SystemExit(f"Output dir '{project_path}' already exists and overwrite flag is not given")
    if len(project_path.name.split()) > 1:  # Modelica can't handle spaces in project name
        raise SystemExit(
            f"\n'{project_path}' failed. Modelica does not support spaces in project names or paths. "
            "Please choose a different 'project_path'")

    gmt = GeoJsonModelicaTranslator(
        geojson_feature_file,
        sys_param_file,
        project_path.parent,
        project_path.name
    )

    gmt.to_modelica()
Example #2
0
    def test_to_modelica_defaults(self):
        feature_json_file = self.data_dir / f"{self.project_name}.json"
        sys_params_json_file = self.data_dir / 'geojson_8_system_params.json'

        gmt = GeoJsonModelicaTranslator(
            feature_json_file,
            sys_params_json_file,
            self.output_dir,
            self.project_name,
        )
        gmt.to_modelica()
    def test_to_modelica_is_successful_when_inputs_are_valid(self):
        # -- Setup, Act
        project_name = 'generate_package'
        _, output_dir = self.set_up(ROOT_DIR, project_name)
        gmt = GeoJsonModelicaTranslator(
            self.geojson_file,
            self.sys_params_file,
            output_dir,
            project_name,
        )

        gmt.to_modelica()

        # -- Assert
        self.assertTrue((output_dir / project_name / 'package.mo').exists())
Example #4
0
    def test_to_modelica_defaults(self):
        feature_json_file = self.data_dir / f"{self.project_name}.json"

        gj = GeoJsonModelicaTranslator.from_geojson(feature_json_file)
        sys_params_json_file = self.data_dir / 'spawn_system_params.json'
        gj.set_system_parameters(SystemParameters(sys_params_json_file))
        gj.process_loads()

        self.assertEqual(len(gj.loads), 2)
        gj.to_modelica(self.project_name, self.output_dir)

        self.assertTrue(self.results_path / "Loads" / "Resources" / "Data" /
                        "B5a6b99ec37f4de7f94020090" /
                        "RefBldgSmallOfficeNew2004_Chicago.idf")  # noqa

        mr = ModelicaRunner()
        file_to_run = Path(gj.scaffold.loads_path.files_dir
                           ) / 'B5a6b99ec37f4de7f94020090' / 'coupling.mo'
        run_path = Path(gj.scaffold.project_path).parent

        exitcode = mr.run_in_docker(file_to_run,
                                    run_path=run_path,
                                    project_name=gj.scaffold.project_name)
        self.assertEqual(0, exitcode)

        results_path = Path(run_path / f"{gj.scaffold.project_name}_results")
        self.assertTrue(Path(results_path) / 'stdout.log')
        self.assertTrue(
            Path(results_path) /
            'spawn_single_Loads_B5a6b99ec37f4de7f94020090_SpawnCouplingETS.fmu'
        )
Example #5
0
    def test_to_modelica_defaults(self):
        self.results_path = os.path.abspath("tests/output/geojson_urbanopt")
        if os.path.exists(self.results_path):
            shutil.rmtree(self.results_path)

        filename = os.path.abspath(
            "tests/geojson/data/example_geojson_13buildings.json")
        gj = GeoJsonModelicaTranslator.from_geojson(filename)
        sys_params = SystemParameters()
        gj.set_system_parameters(sys_params)
        gj.to_modelica("geojson_urbanopt", "tests/output")

        # go through the generated buildings and ensure that the resources are created
        resource_names = [
            "InternalGains_Floor",
            "InternalGains_ICT",
            "InternalGains_Meeting",
            "InternalGains_Office",
            "InternalGains_Restroom",
            "InternalGains_Storage",
        ]
        for b in gj.buildings:
            for resource_name in resource_names:
                # TEASER 0.7.2 used .txt for schedule files
                path = os.path.join(
                    gj.scaffold.loads_path.files_dir,
                    "Resources",
                    "Data",
                    b.dirname,
                    f"{resource_name}.txt",
                )
                self.assertTrue(os.path.exists(path),
                                f"Path not found: {path}")
Example #6
0
    def test_to_modelica_defaults(self):
        feature_json_file = self.data_dir / f"{self.project_name}.json"
        sys_params_json_file = self.data_dir / 'spawn_system_params.json'

        gmt = GeoJsonModelicaTranslator(
            feature_json_file,
            sys_params_json_file,
            self.output_dir,
            self.project_name,
        )

        gmt.to_modelica()

        self.assertTrue(self.results_path / "Loads" / "Resources" / "Data" /
                        "B5a6b99ec37f4de7f94020090" /
                        "RefBldgSmallOfficeNew2004_Chicago.idf")  # noqa
    def test_successfully_creates_and_simulates_when_inputs_are_valid(self):
        # -- Setup
        project_name = 'simulate_package'
        _, output_dir = self.set_up(ROOT_DIR, project_name)

        gmt = GeoJsonModelicaTranslator(
            self.geojson_file,
            self.sys_params_file,
            output_dir,
            project_name,
        )

        package = gmt.to_modelica()

        # -- Act
        success, results_dir = package.simulate()

        # -- Assert
        self.assertTrue(success)
        self.assertTrue((results_dir / 'stdout.log').exists())
Example #8
0
    def test_to_modelica_defaults(self):
        self.results_path = os.path.abspath("tests/output/geojson_1")
        if os.path.exists(self.results_path):
            shutil.rmtree(self.results_path)

        filename = os.path.abspath("tests/geojson/data/geojson_1.json")
        gj = GeoJsonModelicaTranslator.from_geojson(filename)
        sys_params = SystemParameters()
        gj.set_system_parameters(sys_params)
        gj.to_modelica("geojson_1", "tests/output")

        # setup what we are going to check
        model_names = [
            "Floor",
            "ICT",
            "Meeting",
            "Office",
            "package",
            "Restroom",
            "Storage",
        ]
        building_paths = [
            os.path.join(gj.scaffold.loads_path.files_dir, b.dirname)
            for b in gj.buildings
        ]
        path_checks = [
            f"{os.path.sep.join(r)}.mo"
            for r in itertools.product(building_paths, model_names)
        ]

        for p in path_checks:
            self.assertTrue(os.path.exists(p), f"Path not found {p}")

        # go through the generated buildings and ensure that the resources are created
        resource_names = [
            "InternalGains_Floor",
            "InternalGains_ICT",
            "InternalGains_Meeting",
            "InternalGains_Office",
            "InternalGains_Restroom",
            "InternalGains_Storage",
        ]
        for b in gj.buildings:
            for resource_name in resource_names:
                # TEASER 0.7.2 used .txt for schedule files
                path = os.path.join(
                    gj.scaffold.loads_path.files_dir,
                    "Resources",
                    "Data",
                    b.dirname,
                    f"{resource_name}.txt",
                )
                self.assertTrue(os.path.exists(path),
                                f"Path not found: {path}")
Example #9
0
    def test_cli_makes_model(self):
        # WARNING: This test assumes test_cli_builds_sys_params has already run
        # successfully! This test should be refactored to avoid this

        # -- Setup
        # first verify the package can be generated without the CLI (ie verify our
        # files are valid)
        project_name = 'modelica_project'
        if (self.output_dir / project_name).exists():
            rmtree(self.output_dir / project_name)

        sys_params_filepath = self.output_dir / 'test_sys_param.json'
        geojson_filepath = self.feature_file_path

        gmt = GeoJsonModelicaTranslator(
            geojson_filepath,
            sys_params_filepath,
            self.output_dir,
            project_name,
        )

        gmt.to_modelica()

        # great! we know our files are good, let's cleanup and test the CLI
        rmtree(self.output_dir / project_name)

        # -- Act
        res = self.runner.invoke(cli, [
            'create-model',
            str(sys_params_filepath),
            str(geojson_filepath),
            str(self.output_dir / project_name)
        ])

        assert res.exit_code == 0

        # If this file exists, the cli command ran successfully
        assert (self.output_dir / 'modelica_project' / 'Districts' /
                'DistrictEnergySystem.mo').exists()
Example #10
0
    def test_to_modelica_defaults(self):
        feature_json_file = self.data_dir / f"{self.project_name}.json"

        gj = GeoJsonModelicaTranslator.from_geojson(feature_json_file)
        sys_params_json_file = self.data_dir / 'geojson_8_system_params.json'
        sys_params = SystemParameters(sys_params_json_file)
        gj.set_system_parameters(sys_params)
        gj.process_loads()

        self.assertEqual(len(gj.loads), 8)

        all_couplings = []

        for geojson_load in gj.json_loads:
            teaser_load = Teaser(sys_params, geojson_load)

            hot_stub = EtsHotWaterStub(sys_params)
            cold_stub = EtsColdWaterStub(sys_params)
            all_couplings.append(Coupling(teaser_load, hot_stub))
            all_couplings.append(Coupling(teaser_load, cold_stub))

        graph = CouplingGraph(all_couplings)

        district = District(
            root_dir=self.output_dir,
            project_name=self.project_name,
            system_parameters=sys_params,
            coupling_graph=graph
        )

        district.to_modelica()

        # run a single file to make sure it simulates
        mr = ModelicaRunner()

        root_path = os.path.abspath(os.path.join(district._scaffold.districts_path.files_dir))
        exitcode = mr.run_in_docker(os.path.join(root_path, 'DistrictEnergySystem.mo'),
                                    run_path=Path(district._scaffold.project_path).resolve().parent,
                                    project_name=district._scaffold.project_name)
        self.assertEqual(0, exitcode)
Example #11
0
    def setUp(self):
        root_dir, project_name = "tests/model_connectors/output", "spawn_two_building"

        if os.path.exists(os.path.join(root_dir, project_name)):
            shutil.rmtree(os.path.join(root_dir, project_name))

        # load in the example geojson with a single offie building
        filename = os.path.abspath(
            "tests/model_connectors/data/spawn_geojson_ex2.json")
        self.gj = GeoJsonModelicaTranslator.from_geojson(filename)
        self.gj.scaffold_directory(
            root_dir, project_name
        )  # use the GeoJson translator to scaffold out the directory

        # load system parameter data
        filename = os.path.abspath(
            "tests/model_connectors/data/spawn_system_params_ex2.json")
        sys_params = SystemParameters(filename)

        # now test the spawn connector (independent of the larger geojson translator
        self.spawn = SpawnConnector(sys_params)

        for b in self.gj.buildings:
            self.spawn.add_building(b)
Example #12
0
 def test_missing_geojson(self):
     fn = "non-existent-path"
     with self.assertRaises(Exception) as exc:
         GeoJsonModelicaTranslator.from_geojson(fn)
     self.assertEqual(f"GeoJSON file does not exist: {fn}",
                      str(exc.exception))
Example #13
0
    def test_from_geojson(self):
        filename = os.path.abspath("tests/geojson/data/geojson_1.json")
        gj = GeoJsonModelicaTranslator.from_geojson(filename)

        self.assertEqual(len(gj.buildings), 3)
Example #14
0
    def test_to_modelica_rc_order_4(self):
        self.results_path = os.path.abspath("tests/output/rc_order_4")
        if os.path.exists(self.results_path):
            shutil.rmtree(self.results_path)

        filename = os.path.abspath("tests/geojson/data/geojson_1.json")
        gj = GeoJsonModelicaTranslator.from_geojson(filename)
        sys_params = SystemParameters.loadd({
            "buildings": {
                "default": {
                    "load_model_parameters": {
                        "rc": {
                            "order": 4
                        }
                    }
                }
            }
        })
        self.assertEqual(len(sys_params.validate()), 0)
        gj.set_system_parameters(sys_params)

        gj.to_modelica("rc_order_4", "tests/output")

        # setup what we are going to check
        model_names = [
            "Floor",
            "ICT",
            "Meeting",
            "Office",
            "package",
            "Restroom",
            "Storage",
        ]
        building_paths = [
            os.path.join(gj.scaffold.loads_path.files_dir, b.dirname)
            for b in gj.buildings
        ]
        path_checks = [
            f"{os.path.sep.join(r)}.mo"
            for r in itertools.product(building_paths, model_names)
        ]

        for p in path_checks:
            self.assertTrue(os.path.exists(p), f"Path not found: {p}")

        resource_names = [
            "InternalGains_Floor",
            "InternalGains_ICT",
            "InternalGains_Meeting",
            "InternalGains_Office",
            "InternalGains_Restroom",
            "InternalGains_Storage",
        ]
        for b in gj.buildings:
            for resource_name in resource_names:
                # TEASER 0.7.2 used .txt for schedule files
                path = os.path.join(
                    gj.scaffold.loads_path.files_dir,
                    "Resources",
                    "Data",
                    b.dirname,
                    f"{resource_name}.txt",
                )
                self.assertTrue(os.path.exists(path),
                                f"Path not found: {path}")