-
Notifications
You must be signed in to change notification settings - Fork 0
Rmanno/refactor with visitor #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rmanno91
wants to merge
7
commits into
rmanno/trial_protocol_write
Choose a base branch
from
rmanno/refactor_with_visitor
base: rmanno/trial_protocol_write
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8dd8eee
add basic visitors
rmanno91 f7d51fa
implement visitors
rmanno91 e4ca72e
fix
rmanno91 4b74be2
fixes
rmanno91 e328181
add orthotropic elasticity
rmanno91 ba2b3f2
changes
rmanno91 04fab6a
make populate common
rmanno91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
106
src/ansys/materials/manager/util/visitors/base_visitor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | ||
|
|
||
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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