def run_model(modelica_project: Path):
    """
    \b
    Run the Modelica project in a docker-based environment.
    Results are saved at the same level as the project path that is passed.
    The model that runs is hard coded to be the Districts/DistrictEnergySystem.mo within the package.

    \b
    MODELICA_PROJECT: Path to the Modelica project, possibly created by this cli
        default = ./model_from_sdk

    \f
    :param modelica_project: Path, name & location of modelica project, possibly created with this cli
    """
    run_path = Path(modelica_project).resolve()
    project_name = run_path.stem
    file_to_run = run_path / 'Districts' / 'DistrictEnergySystem.mo'

    if len(str(run_path).split()) > 1:  # Modelica can't handle spaces in project name or path
        raise SystemExit(
            f"\n'{run_path}' failed. Modelica does not support spaces in project names or paths. "
            "Please update your directory tree to not include spaces in any name")

    # setup modelica runner
    mr = ModelicaRunner()
    mr.run_in_docker(file_to_run, run_path=run_path.parent, project_name=project_name)

    if (run_path.parent / f'{project_name}_results' / f'{project_name}_Districts_DistrictEnergySystem_result.mat').exists():
        print(f"\nModelica model {project_name} ran successfully")
    else:
        raise SystemExit(f"{project_name} failed. Please check your inputs and try again.")
예제 #2
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'
        )
예제 #3
0
    def simulate(self):
        """Simulate the package.

        :return: tuple(bool, pathlib.Path), True or False depending on simulation success
            followed by the path to the results directory
        """
        modelica_runner = ModelicaRunner()
        return modelica_runner.run_in_docker(self._file_to_run,
                                             run_path=self._project_path,
                                             project_name=self._project_name)
예제 #4
0
    def test_spawn_to_modelica_and_run(self):
        self.spawn.to_modelica(self.gj.scaffold)

        # make sure the model can run using the ModelicaRunner class
        mr = ModelicaRunner()
        file_to_run = os.path.abspath(
            os.path.join(self.gj.scaffold.loads_path.files_dir,
                         'B5a6b99ec37f4de7f94020090', 'coupling.mo'), )
        run_path = Path(os.path.abspath(self.gj.scaffold.project_path)).parent
        exitcode = mr.run_in_docker(file_to_run, run_path=run_path)
        self.assertEqual(0, exitcode)
예제 #5
0
    def test_run_in_docker(self):
        mr = ModelicaRunner()
        mr.run_in_docker(os.path.join(self.run_path, 'BouncingBall.mo'))

        results_path = os.path.join(self.run_path, 'BouncingBall_results')
        self.assertTrue(
            os.path.exists(os.path.join(results_path, 'stdout.log')))
        self.assertTrue(
            os.path.exists(
                os.path.join(results_path, 'BouncingBall_result.mat')))
        self.assertFalse(
            os.path.exists(os.path.join(results_path, 'jm_ipython.sh')))
        self.assertFalse(
            os.path.exists(os.path.join(results_path, 'jmodelica.py')))
    def run_and_assert_in_docker(self, file_to_run, project_path, project_name):
        """Run the test in docker.

        :param file_to_run: Full path to the file to run. Typically this is the .mo file of interest (e.g., coupling.mo)
        :param project_path: Full path to the location of the project to run. This is typically the the full path to
        where the directory named with the `project_name` comes come from.
        :param project_name: The name of the project that is running. This is the directory where the root package.mo
        lives.
        :return: None
        """
        mr = ModelicaRunner()
        run_path = Path(os.path.abspath(project_path)).parent
        success, results_path = mr.run_in_docker(file_to_run, run_path=run_path, project_name=project_name)
        # on the exit of the docker command it should return a zero exit code, otherwise there was an issue.
        # Look at the stdout.log if this is non-zero.
        self.assertTrue(success)

        # make sure that the results log exist
        self.assertTrue((Path(results_path) / 'stdout.log').exists())
예제 #7
0
 def test_run_setup(self):
     prev_mod_path = os.environ.get('MODELICAPATH', None)
     try:
         os.environ['MODELICAPATH'] = 'A_PATH/to_something'
         mr = ModelicaRunner()
         self.assertEqual(mr.modelica_lib_path, 'A_PATH/to_something')
     finally:
         if prev_mod_path:
             os.environ['MODELICAPATH'] = prev_mod_path
     print(mr.jmodelica_py_path)
     self.assertTrue(os.path.exists(mr.jmodelica_py_path))
     self.assertTrue(os.path.exists(mr.jm_ipython_path))
예제 #8
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)
예제 #9
0
    def test_run_in_docker_errors(self):
        mr = ModelicaRunner()
        file_to_run = os.path.join(self.run_path, 'no_file.mo')
        with self.assertRaises(Exception) as exc:
            mr.run_in_docker(file_to_run)
        self.assertEqual(f'File not found to run {file_to_run}', str(exc.exception))

        file_to_run = os.path.join(self.run_path)
        with self.assertRaises(Exception) as exc:
            mr.run_in_docker(file_to_run)
        self.assertEqual(f'Expecting to run a file, not a folder in {file_to_run}', str(exc.exception))
예제 #10
0
 def test_run_in_docker(self):
     mr = ModelicaRunner()
     mr.run_in_docker(os.path.join(self.run_path, 'BouncingBall.mo'))
     mr.cleanup_path(self.run_path)
     self.assertTrue(os.path.exists(os.path.join(self.run_path, 'stdout.log')))
     self.assertTrue(os.path.exists(os.path.join(self.run_path, 'BouncingBall_result.mat')))
예제 #11
0
 def test_docker_enabled(self):
     mr = ModelicaRunner()
     self.assertTrue(mr.docker_configured, 'Docker is not running, unable to run all tests')