Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions examples/trial_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from ansys.units import Quantity

from ansys.materials.manager._models._material_models.density import Density
from ansys.materials.manager._models._material_models.elasticity_anisotropic import (
ElasticityAnisotropic,
)
from ansys.materials.manager._models._material_models.elasticity_isotropic import (
ElasticityIsotropic,
)
from ansys.materials.manager._models._material_models.elasticity_orthotropic import (
ElasticityOrthotropic,
)
from ansys.materials.manager._models.material import Material
from ansys.materials.manager.util.visitors.fluent_visitor import FluentVisitor
from ansys.materials.manager.util.visitors.lsdyna_visitor import LsDynaVisitor
from ansys.materials.manager.util.visitors.mapdl_visitor import MapdlVisitor
from ansys.materials.manager.util.visitors.matml_visitor import MatmlVisitor

material_1 = Material(
name="Isotropic Test Material",
material_id=1,
models=[
Density(density=Quantity(value=[3.0], units="kg m^-3")),
ElasticityIsotropic(
youngs_modulus=Quantity(value=[1000000], units="Pa"),
poissons_ratio=Quantity(value=[0.3], units=""),
),
],
)

material_2 = Material(
name="Orthotropic Test Material",
material_id=2,
models=[
ElasticityOrthotropic(
youngs_modulus_x=Quantity(value=[1000000], units="Pa"),
youngs_modulus_y=Quantity(value=[1500000], units="Pa"),
youngs_modulus_z=Quantity(value=[2000000], units="Pa"),
shear_modulus_xy=Quantity(value=[1000000], units="Pa"),
shear_modulus_yz=Quantity(value=[2000000], units="Pa"),
shear_modulus_xz=Quantity(value=[3000000], units="Pa"),
poissons_ratio_xy=Quantity(value=[0.2], units=""),
poissons_ratio_yz=Quantity(value=[0.3], units=""),
poissons_ratio_xz=Quantity(value=[0.4], units=""),
)
],
)

material_3 = Material(
name="Anisotropic Test Material",
material_id=3,
models=[
ElasticityAnisotropic(
c_11=Quantity(value=[100000000], units="Pa"),
c_12=Quantity(value=[0], units="Pa"),
c_13=Quantity(value=[0], units="Pa"),
c_14=Quantity(value=[0], units="Pa"),
c_15=Quantity(value=[0], units="Pa"),
c_16=Quantity(value=[0], units="Pa"),
c_22=Quantity(value=[150000000], units="Pa"),
c_23=Quantity(value=[0], units="Pa"),
c_24=Quantity(value=[0], units="Pa"),
c_25=Quantity(value=[0], units="Pa"),
c_26=Quantity(value=[0], units="Pa"),
c_33=Quantity(value=[200000000], units="Pa"),
c_34=Quantity(value=[0], units="Pa"),
c_35=Quantity(value=[0], units="Pa"),
c_36=Quantity(value=[0], units="Pa"),
c_44=Quantity(value=[50000000], units="Pa"),
c_45=Quantity(value=[0], units="Pa"),
c_46=Quantity(value=[0], units="Pa"),
c_55=Quantity(value=[60000000], units="Pa"),
c_56=Quantity(value=[0], units="Pa"),
c_66=Quantity(value=[70000000], units="Pa"),
),
],
)

materials = [material_1, material_2, material_3]
visitor_matml = MatmlVisitor(materials=materials)
visitor_matml.write("trial.xml", True)
visitor_mapdl = MapdlVisitor(materials=materials)
mapdl = visitor_mapdl.write()
print(mapdl)
visitor_dyna = LsDynaVisitor(materials=materials)
dyna = visitor_dyna.write()
print(dyna)
visitor_fluent = FluentVisitor(materials=materials)
fluent = visitor_fluent.write()
print(fluent)
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from ansys.materials.manager._models._common._exceptions import ModelValidationException

from .cofficient_of_thermal_expansion_isotropic import (
CoefficientofThermalExpansionIsotropic,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,91 @@ class ElasticityAnisotropic(MaterialModel):
default=None, description="The sixth column of the elasticity matrix.", matml_name="D[*,6]"
)

c_11: Quantity | None = Field(
default=None,
description="The C11 component of the elasticity matrix.",
)
c_12: Quantity | None = Field(
default=None,
description="The C12 component of the elasticity matrix.",
)
c_13: Quantity | None = Field(
default=None,
description="The C13 component of the elasticity matrix.",
)
c_14: Quantity | None = Field(
default=None,
description="The C14 component of the elasticity matrix.",
)
c_15: Quantity | None = Field(
default=None,
description="The C15 component of the elasticity matrix.",
)
c_16: Quantity | None = Field(
default=None,
description="The C16 component of the elasticity matrix.",
)
c_22: Quantity | None = Field(
default=None,
description="The C22 component of the elasticity matrix.",
)
c_23: Quantity | None = Field(
default=None,
description="The C23 component of the elasticity matrix.",
)
c_24: Quantity | None = Field(
default=None,
description="The C24 component of the elasticity matrix.",
)
c_25: Quantity | None = Field(
default=None,
description="The C25 component of the elasticity matrix.",
)
c_26: Quantity | None = Field(
default=None,
description="The C26 component of the elasticity matrix.",
)
c_33: Quantity | None = Field(
default=None,
description="The C33 component of the elasticity matrix.",
)
c_34: Quantity | None = Field(
default=None,
description="The C34 component of the elasticity matrix.",
)
c_35: Quantity | None = Field(
default=None,
description="The C35 component of the elasticity matrix.",
)
c_36: Quantity | None = Field(
default=None,
description="The C36 component of the elasticity matrix.",
)
c_44: Quantity | None = Field(
default=None,
description="The C44 component of the elasticity matrix.",
)
c_45: Quantity | None = Field(
default=None,
description="The C45 component of the elasticity matrix.",
)
c_46: Quantity | None = Field(
default=None,
description="The C46 component of the elasticity matrix.",
)
c_55: Quantity | None = Field(
default=None,
description="The C55 component of the elasticity matrix.",
)
c_56: Quantity | None = Field(
default=None,
description="The C56 component of the elasticity matrix.",
)
c_66: Quantity | None = Field(
default=None,
description="The C66 component of the elasticity matrix.",
)

@model_validator(mode="before")
def _initialize_qualifiers(cls, values) -> Dict:
expected_qualifiers = {"Behavior": ["Anisotropic", QualifierType.STRICT]}
Expand Down
50 changes: 49 additions & 1 deletion src/ansys/materials/manager/util/mapdl/writer_mapdl_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def write_temperature_table_values(


def write_table_dep_values(
material_id: str, label: str, dependent_values: list[float], tb_opt: str = ""
material_id: str | None, label: str, dependent_values: list[float], tb_opt: str = ""
) -> str:
"""
Write table of dependent values.
Expand Down Expand Up @@ -526,6 +526,54 @@ def get_labels(self, model: MaterialModel) -> list[str]:
return labels


def map_anisotropic_elasticity(material_model: ElasticityAnisotropic) -> list[float]:
"""Map anisotropic elasticity model to dependent values for MAPDL."""
d = np.zeros((6, 6))
d[0, 0] = material_model.c_11.value[0]
d[0, 1] = material_model.c_12.value[0]
d[0, 2] = material_model.c_13.value[0]
d[0, 3] = material_model.c_14.value[0]
d[0, 4] = material_model.c_15.value[0]
d[0, 5] = material_model.c_16.value[0]
d[1, 0] = material_model.c_12.value[0]
d[1, 1] = material_model.c_22.value[0]
d[1, 2] = material_model.c_23.value[0]
d[1, 3] = material_model.c_24.value[0]
d[1, 4] = material_model.c_25.value[0]
d[1, 5] = material_model.c_26.value[0]
d[2, 0] = material_model.c_13.value[0]
d[2, 1] = material_model.c_23.value[0]
d[2, 2] = material_model.c_33.value[0]
d[2, 3] = material_model.c_34.value[0]
d[2, 4] = material_model.c_35.value[0]
d[2, 5] = material_model.c_36.value[0]
d[3, 0] = material_model.c_14.value[0]
d[3, 1] = material_model.c_24.value[0]
d[3, 2] = material_model.c_34.value[0]
d[3, 3] = material_model.c_44.value[0]
d[3, 4] = material_model.c_45.value[0]
d[3, 5] = material_model.c_46.value[0]
d[4, 0] = material_model.c_15.value[0]
d[4, 1] = material_model.c_25.value[0]
d[4, 2] = material_model.c_35.value[0]
d[4, 3] = material_model.c_45.value[0]
d[4, 4] = material_model.c_55.value[0]
d[4, 5] = material_model.c_56.value[0]
d[5, 0] = material_model.c_16.value[0]
d[5, 1] = material_model.c_26.value[0]
d[5, 2] = material_model.c_36.value[0]
d[5, 3] = material_model.c_46.value[0]
d[5, 4] = material_model.c_56.value[0]
d[5, 5] = material_model.c_66.value[0]

# extract the lower triangular elements column-wise
dependent_values = []
for j in range(6):
dependent_values.extend(d[j:, j])

return dependent_values


def write_anisotropic_elasticity(self, model: ElasticityAnisotropic, material_id: int):
"""Write anisotropic elasticity."""
d = np.column_stack(
Expand Down
21 changes: 21 additions & 0 deletions src/ansys/materials/manager/util/visitors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
106 changes: 106 additions & 0 deletions src/ansys/materials/manager/util/visitors/base_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from abc import abstractmethod
import sys

from ansys.materials.manager._models._common.material_model import MaterialModel
from ansys.materials.manager._models.material import Material

MATERIAL_MODEL_MAP = {}


class BaseVisitor:
"""Base visitor. All visitors should inherit from this class."""

def __init__(self, materials: list[Material]):
"""Initialize the base visitor."""
self._materials: list[Material] = materials
self._material_repr: dict = {material.name: [] for material in materials}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what _material_repr is meant to represent

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not repr. Just an intermediate solver representation of the material obtained from the visit. That is then handled by the write method to complete the representation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could imagine a visitor that doesn't produce any representation, for instance a validation visitor


def get_material_id(self, material_name) -> int:
"""
Get the material id given the material name.

Parameters
----------
material_name : str
Name of the material.

Returns
-------
int
Material id.
"""
return [material.mat_id for material in self._materials if material.name == material_name][
0
]

def is_supported(self, material_model: MaterialModel) -> bool:
"""
Check if the material model is supported.

Parameters
----------
material_model : MaterialModel
Material model to check.

Returns
-------
bool
True if the material model is supported, False otherwise.
"""
module = sys.modules[self.__module__]
mapping = getattr(module, "MATERIAL_MODEL_MAP")
if material_model.__class__ in mapping.keys():
return True
else:
return False

def _populate_dependent_parameters(self, material_model: MaterialModel) -> dict:
"""Populate dependent parameters."""
module = sys.modules[self.__module__]
model_map = getattr(module, "MATERIAL_MODEL_MAP")
if material_model.__class__ in model_map.keys():
mapping = model_map[material_model.__class__]
if mapping.method:
labels, quantities = mapping.method(material_model)
else:
labels = mapping.labels
quantities = [getattr(material_model, label) for label in mapping.attributes]
return dict(zip(labels, quantities))

@abstractmethod
def visit_material_model(self, material_name: str, material_model: MaterialModel):
"""Abstract implementation of the visit material model."""
raise NotImplementedError()

def visit_materials(self):
"""Visit materials."""
for material in self._materials:
for material_model in material.models:
if not self.is_supported(material_model):
print(
f"Material model: {material_model.__class__.__name__} not supported by {self.__class__.__name__}" # noqa: E501
)
continue
self.visit_material_model(material.name, material_model)
Loading