diff --git a/doc/changelog.d/508.added.md b/doc/changelog.d/508.added.md new file mode 100644 index 000000000..65c780d89 --- /dev/null +++ b/doc/changelog.d/508.added.md @@ -0,0 +1 @@ +provide a way for the user to limit number of threads \ No newline at end of file diff --git a/src/ansys/speos/core/simulation.py b/src/ansys/speos/core/simulation.py index 52dc638f6..9db73dc4c 100644 --- a/src/ansys/speos/core/simulation.py +++ b/src/ansys/speos/core/simulation.py @@ -222,15 +222,27 @@ def set_source_paths(self, source_paths: List[str]) -> BaseSimulation: # self._simulation_instance.geometries.geo_paths[:] = geo_paths # return self - def compute_CPU(self) -> List[job_pb2.Result]: + def compute_CPU(self, threads_number: Optional[int] = None) -> List[job_pb2.Result]: """Compute the simulation on CPU. + Parameters + ---------- + threads_number : int, optional + The number of threads used. + By default, ``None``, means the number of processor available. + Returns ------- List[ansys.api.speos.job.v2.job_pb2.Result] List of simulation results. """ self._job.job_type = ProtoJob.Type.CPU + + if threads_number is not None: + self._simulation_template.metadata["SimulationSetting::OPTThreadNumber"] = ( + "int::" + str(threads_number) + ) + self.result_list = self._run_job() return self.result_list diff --git a/tests/core/test_project.py b/tests/core/test_project.py index 55baf6f7b..288ea37cf 100644 --- a/tests/core/test_project.py +++ b/tests/core/test_project.py @@ -370,6 +370,31 @@ def test_from_file(speos: Speos): assert ssr_data.irradiance_sensor_template.dimensions.x_sampling == 500 +def test_from_file_threads_limited(speos: Speos): + """Test change Number of threads used.""" + # Create a project from a file + p = Project( + speos=speos, + path=str( + Path(test_path) / "LG_50M_Colorimetric_short.sv5" / "LG_50M_Colorimetric_short.sv5" + ), + ) + + assert len(p.scene_link.get().simulations) == 1 + + feat_sims = p.find(name=p.scene_link.get().simulations[0].name) + assert len(feat_sims) == 1 + + # Choose number of threads + threads_nb = 8 + + # Compute on CPU with the amount of threads selected + feat_sims[0].compute_CPU(threads_number=threads_nb) + assert feat_sims[0].simulation_template_link.get().metadata[ + "SimulationSetting::OPTThreadNumber" + ] == "int::" + str(threads_nb) + + def test_find_geom(speos: Speos): """Test find geometry feature in a project.""" # Create a project from a file