Skip to content

Commit

Permalink
Add support to run simulations (#39)
Browse files Browse the repository at this point in the history
* Update parametric study to run simulations

* Add newline at end of file

* Fix pre-commit checks

* Complete run simulations method

* Enforce unique simulation ids

* Fix pre-commit checks

* Update parameter description
  • Loading branch information
pkrull-ansys authored Aug 16, 2023
1 parent 2df47a0 commit 2232fa9
Show file tree
Hide file tree
Showing 12 changed files with 1,252 additions and 101 deletions.
2 changes: 1 addition & 1 deletion examples/00_additive_single_bead.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
# :meth:`plot() <pandas.DataFrame.plot>` method can be used to plot the melt
# pool dimensions as a function of bead length.

df = summary.melt_pool.data_frame.multiply(1e6) # convert from meters to microns
df = summary.melt_pool.data_frame().multiply(1e6) # convert from meters to microns
df.index *= 1e3 # convert bead length from meters to millimeters

df.plot(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"
[project]
# Check https://flit.readthedocs.io/en/latest/pyproject_toml.html for all available sections
name = "ansys-additive"
version = "0.14.0.dev2"
version = "0.14.0.dev3"
description = "A python client for the Ansys additive service"
readme = "README.rst"
requires-python = ">=3.8"
Expand Down
1 change: 1 addition & 0 deletions src/ansys/additive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
from .microstructure import *
from .porosity import *
from .server_utils import *
from .simulation import *
from .single_bead import *
from .thermal_history import *
51 changes: 29 additions & 22 deletions src/ansys/additive/additive.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from ansys.additive.porosity import PorositySummary
from ansys.additive.progress_logger import ProgressLogger, ProgressState
from ansys.additive.server_utils import find_open_port, launch_server
from ansys.additive.simulation import SimulationError
from ansys.additive.single_bead import SingleBeadSummary
from ansys.additive.thermal_history import ThermalHistoryInput, ThermalHistorySummary

Expand Down Expand Up @@ -219,7 +220,8 @@ def simulate(self, inputs, nproc: typing.Optional[int] = None):
-------
A :class:`SingleBeadSummary`, :class:`PorositySummary`,
:class:`MicrostructureSummary`, :class:`ThermalHistorySummary`,
or, if a list of inputs was provided, a list of these summary types.
:class:`SimulationError`, or, if a list of inputs was provided,
a list of these types.
"""
if type(inputs) is not list:
Expand Down Expand Up @@ -257,7 +259,8 @@ def _simulate(self, input, show_progress: bool = False):
-------
One of the follow summary objects.
:class:`SingleBeadSummary`, :class:`PorositySummary`,
:class:`MicrostructureSummary`, :class:`ThermalHistorySummary`
:class:`MicrostructureSummary`, :class:`ThermalHistorySummary`,
:class:`SimulationError`
"""
logger = None
Expand All @@ -267,26 +270,30 @@ def _simulate(self, input, show_progress: bool = False):
if input.material == AdditiveMaterial():
raise ValueError("Material must be specified")

request = None
if isinstance(input, ThermalHistoryInput):
return self._simulate_thermal_history(input, USER_DATA_PATH, logger)
else:
request = input._to_simulation_request()

for response in self._simulation_stub.Simulate(request):
if response.HasField("progress"):
if logger:
logger.log_progress(response.progress)
if response.progress.state == ProgressState.PROGRESS_STATE_ERROR:
raise Exception(response.progress.message)
if response.HasField("melt_pool"):
return SingleBeadSummary(input, response.melt_pool)
if response.HasField("porosity_result"):
return PorositySummary(input, response.porosity_result)
if response.HasField("microstructure_result"):
return MicrostructureSummary(
input, response.microstructure_result, self._user_data_path
)
try:
request = None
if isinstance(input, ThermalHistoryInput):
return self._simulate_thermal_history(input, USER_DATA_PATH, logger)
else:
request = input._to_simulation_request()

for response in self._simulation_stub.Simulate(request):
if response.HasField("progress"):
if logger:
logger.log_progress(response.progress)
if response.progress.state == ProgressState.PROGRESS_STATE_ERROR:
raise Exception(response.progress.message)
if response.HasField("melt_pool"):
return SingleBeadSummary(input, response.melt_pool)
if response.HasField("porosity_result"):
return PorositySummary(input, response.porosity_result)
if response.HasField("microstructure_result"):
return MicrostructureSummary(
input, response.microstructure_result, self._user_data_path
)
except Exception as e:
print(f"Error: {e}")
return SimulationError(input, str(e))

def get_materials_list(self) -> list[str]:
"""Retrieve a list of material names used in additive simulations.
Expand Down
22 changes: 22 additions & 0 deletions src/ansys/additive/microstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ def __repr__(self):
repr += k.replace("_", "", 1) + ": " + str(getattr(self, k)) + "\n"
return repr

def __eq__(self, __o: object) -> bool:
if not isinstance(__o, MicrostructureInput):
return False
return (
self.id == __o.id
and self.sample_min_x == __o.sample_min_x
and self.sample_min_y == __o.sample_min_y
and self.sample_min_z == __o.sample_min_z
and self.sample_size_x == __o.sample_size_x
and self.sample_size_y == __o.sample_size_y
and self.sample_size_z == __o.sample_size_z
and self.sensor_dimension == __o.sensor_dimension
and self.use_provided_thermal_parameters == __o.use_provided_thermal_parameters
and self.cooling_rate == __o.cooling_rate
and self.thermal_gradient == __o.thermal_gradient
and self.melt_pool_width == __o.melt_pool_width
and self.melt_pool_depth == __o.melt_pool_depth
and self.random_seed == __o.random_seed
and self.machine == __o.machine
and self.material == __o.material
)

@staticmethod
def __validate_range(value, min, max, name):
if value < min or value > max:
Expand Down
Loading

0 comments on commit 2232fa9

Please sign in to comment.