From ae47e8768c629d21a3462d93755abb13df526d55 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 18:37:44 +0100 Subject: [PATCH 1/5] Added get_stiffness_matrix method to SystemElements and corresponding test_stifness --- anastruct/fem/system.py | 23 +++++++++++++++++++++++ anastruct/getst.py | 10 ++++++++++ getst.py | 9 +++++++++ stifness.py | 15 +++++++++++++++ tests/test_stifness.py | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 anastruct/getst.py create mode 100644 getst.py create mode 100644 stifness.py create mode 100644 tests/test_stifness.py diff --git a/anastruct/fem/system.py b/anastruct/fem/system.py index 43485a5..01ad8e5 100644 --- a/anastruct/fem/system.py +++ b/anastruct/fem/system.py @@ -2291,6 +2291,29 @@ def apply_load_case(self, loadcase: LoadCase) -> None: kwargs = re.sub(r".??(\w+).?:", r"\1=", kwargs) exec(f"self.{method}({kwargs})") # pylint: disable=exec-used + def get_stiffness_matrix(self, element_id): + """ + Print the stiffness matrix for a specific element by its ID. + Args: + element_id (int): ID of the element. + """ + try: + element = self.element_map[element_id] + except KeyError: + print(f"Element with ID {element_id} does not exist.") + return None + + if isinstance(element, Element): + if hasattr(element, 'stiffness_matrix'): + print(f"Stiffness Matrix for Element ID {element_id}:") + + return element.stiffness_matrix + else: + print(f"Element ID {element_id} does not have a stiffness matrix.") + else: + print(f"Invalid element type for element ID {element_id}.") + return None + def __deepcopy__(self, _: str) -> "SystemElements": """Deepcopy the SystemElements object. diff --git a/anastruct/getst.py b/anastruct/getst.py new file mode 100644 index 0000000..697f5a8 --- /dev/null +++ b/anastruct/getst.py @@ -0,0 +1,10 @@ + +from anastruct.fem import system as se +ss=se.SystemElements() + +# Add an element to the system (Example: Beam element between two nodes) +ss.add_element(location=[[0, 0], [5, 0]]) # Simple beam element + +ss.add_element(location=[[0, 0], [5, 0]]) + +ss.get_stiffness_matrix(element_id=2) # Element ID 1 diff --git a/getst.py b/getst.py new file mode 100644 index 0000000..ac223d6 --- /dev/null +++ b/getst.py @@ -0,0 +1,9 @@ + +from anastruct.fem.system import SystemElements +ss=SystemElements() + +# Add an element to the system (Example: Beam element between two nodes) +ss.add_element(location=[[0, 0], [5, 0]]) # Simple beam element + + +ss.get_stiffness_matrix(1) # Element ID 1 \ No newline at end of file diff --git a/stifness.py b/stifness.py new file mode 100644 index 0000000..c069598 --- /dev/null +++ b/stifness.py @@ -0,0 +1,15 @@ + +from anastruct.fem import system as se +ss=se.SystemElements() + +# Add an element to the system (Example: Beam element between two nodes) +ss.add_element(location=[[0, 0], [5, 0]]) # Simple beam element + +ss.add_element(location=[[0, 0], [5, 0]]) + +ss.add_element(location=[[0, 4], [8, 0]]) + +ss=ss.get_stiffness_matrix(element_id=4) # Element ID 1 + +print(ss) + diff --git a/tests/test_stifness.py b/tests/test_stifness.py new file mode 100644 index 0000000..c8ae5f5 --- /dev/null +++ b/tests/test_stifness.py @@ -0,0 +1,32 @@ +import unittest +import numpy as np +from anastruct.fem import system as se +from anastruct.fem.elements import Element + +class TestSystemElements(unittest.TestCase): + def test_get_stiffness_matrix(self): + # Create a system + system = se.SystemElements() + + # Add a simple beam element (make sure it has a stiffness matrix) + system.add_element(location=[[0, 0], [5, 0]]) # Adjust as necessary based on your implementation + + # Here, we assume that the added element has a stiffness matrix defined + # Check if stiffness matrix is correctly returned for element 1 + stiffness_matrix = system.get_stiffness_matrix(1) + + # Assert that a stiffness matrix was returned + self.assertIsNotNone(stiffness_matrix) + print("Stiffness matrix returned successfully.") + + def test_invalid_element(self): + # Create a system without adding any elements + system = se.SystemElements() + + # Try to get the stiffness matrix of an element that doesn't exist + stiffness_matrix = system.get_stiffness_matrix(999) # Nonexistent ID + self.assertIsNone(stiffness_matrix) + print("Handled invalid element ID correctly.") + +if __name__ == '__main__': + unittest.main() From ffc35ccbd433bca07bdbb8b22ca6d699a097b737 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 13 Oct 2024 18:38:54 +0100 Subject: [PATCH 2/5] Added get_stiffness_matrix method to SystemElements and corresponding test_stifness --- getst.py | 9 --------- stifness.py | 15 --------------- 2 files changed, 24 deletions(-) delete mode 100644 getst.py delete mode 100644 stifness.py diff --git a/getst.py b/getst.py deleted file mode 100644 index ac223d6..0000000 --- a/getst.py +++ /dev/null @@ -1,9 +0,0 @@ - -from anastruct.fem.system import SystemElements -ss=SystemElements() - -# Add an element to the system (Example: Beam element between two nodes) -ss.add_element(location=[[0, 0], [5, 0]]) # Simple beam element - - -ss.get_stiffness_matrix(1) # Element ID 1 \ No newline at end of file diff --git a/stifness.py b/stifness.py deleted file mode 100644 index c069598..0000000 --- a/stifness.py +++ /dev/null @@ -1,15 +0,0 @@ - -from anastruct.fem import system as se -ss=se.SystemElements() - -# Add an element to the system (Example: Beam element between two nodes) -ss.add_element(location=[[0, 0], [5, 0]]) # Simple beam element - -ss.add_element(location=[[0, 0], [5, 0]]) - -ss.add_element(location=[[0, 4], [8, 0]]) - -ss=ss.get_stiffness_matrix(element_id=4) # Element ID 1 - -print(ss) - From 6d5715fa33ab9120a3d986e42f3872cf0afcdedd Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 16 Oct 2024 08:21:24 +0100 Subject: [PATCH 3/5] Fix Pylint issues --- anastruct/fem/system.py | 38 +++++++++++++++++++------------------- anastruct/getst.py | 10 ---------- 2 files changed, 19 insertions(+), 29 deletions(-) delete mode 100644 anastruct/getst.py diff --git a/anastruct/fem/system.py b/anastruct/fem/system.py index 01ad8e5..1c25098 100644 --- a/anastruct/fem/system.py +++ b/anastruct/fem/system.py @@ -2292,27 +2292,27 @@ def apply_load_case(self, loadcase: LoadCase) -> None: exec(f"self.{method}({kwargs})") # pylint: disable=exec-used def get_stiffness_matrix(self, element_id): - """ - Print the stiffness matrix for a specific element by its ID. - Args: - element_id (int): ID of the element. - """ - try: - element = self.element_map[element_id] - except KeyError: - print(f"Element with ID {element_id} does not exist.") - return None + """ + Print the stiffness matrix for a specific element by its ID. + Args: + element_id (int): ID of the element. + """ + try: + element = self.element_map[element_id] + except KeyError: + print(f"Element with ID {element_id} does not exist.") + return None + + if isinstance(element, Element): + if hasattr(element, 'stiffness_matrix'): + print(f"Stiffness Matrix for Element ID {element_id}:") + return element.stiffness_matrix - if isinstance(element, Element): - if hasattr(element, 'stiffness_matrix'): - print(f"Stiffness Matrix for Element ID {element_id}:") - - return element.stiffness_matrix - else: print(f"Element ID {element_id} does not have a stiffness matrix.") - else: - print(f"Invalid element type for element ID {element_id}.") - return None + else: + print(f"Invalid element type for element ID {element_id}.") + return None + def __deepcopy__(self, _: str) -> "SystemElements": diff --git a/anastruct/getst.py b/anastruct/getst.py deleted file mode 100644 index 697f5a8..0000000 --- a/anastruct/getst.py +++ /dev/null @@ -1,10 +0,0 @@ - -from anastruct.fem import system as se -ss=se.SystemElements() - -# Add an element to the system (Example: Beam element between two nodes) -ss.add_element(location=[[0, 0], [5, 0]]) # Simple beam element - -ss.add_element(location=[[0, 0], [5, 0]]) - -ss.get_stiffness_matrix(element_id=2) # Element ID 1 From 304199f78979c02ee096ec431487e742fcb4c1cc Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 17 Oct 2024 15:27:19 +0100 Subject: [PATCH 4/5] Fix Pylint issues --- anastruct/fem/system.py | 43 +++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/anastruct/fem/system.py b/anastruct/fem/system.py index 1c25098..d80b39c 100644 --- a/anastruct/fem/system.py +++ b/anastruct/fem/system.py @@ -2291,29 +2291,31 @@ def apply_load_case(self, loadcase: LoadCase) -> None: kwargs = re.sub(r".??(\w+).?:", r"\1=", kwargs) exec(f"self.{method}({kwargs})") # pylint: disable=exec-used - def get_stiffness_matrix(self, element_id): - """ - Print the stiffness matrix for a specific element by its ID. - Args: - element_id (int): ID of the element. - """ - try: - element = self.element_map[element_id] - except KeyError: - print(f"Element with ID {element_id} does not exist.") - return None - - if isinstance(element, Element): - if hasattr(element, 'stiffness_matrix'): - print(f"Stiffness Matrix for Element ID {element_id}:") - return element.stiffness_matrix - - print(f"Element ID {element_id} does not have a stiffness matrix.") - else: - print(f"Invalid element type for element ID {element_id}.") + + def get_stiffness_matrix(self, element_id: int) -> Optional[Union[list, None]]: + """ + Return the stiffness matrix for a specific element by its ID. + Args: + element_id (int): ID of the element. + + Returns: + Optional[Union[list, None]]: The stiffness matrix of the element if it exists, otherwise None. + """ + try: + element = self.element_map[element_id] + except KeyError: + print(f"Element with ID {element_id} does not exist.") return None + if isinstance(element, Element): + if hasattr(element, 'stiffness_matrix'): + print(f"Stiffness Matrix for Element ID {element_id}:") + return element.stiffness_matrix + print(f"Element ID {element_id} does not have a stiffness matrix.") + else: + print(f"Invalid element type for element ID {element_id}.") + return None def __deepcopy__(self, _: str) -> "SystemElements": """Deepcopy the SystemElements object. @@ -2337,7 +2339,6 @@ def __deepcopy__(self, _: str) -> "SystemElements": return system - def _negative_index_to_id(idx: int, collection: Collection[int]) -> int: """Convert a negative index to a positive index. (That is, allowing the Pythonic negative indexing) From f64a2a1b99102f4ecb8b875e54235b76cc11964d Mon Sep 17 00:00:00 2001 From: smith120bh <42363318+smith120bh@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:16:04 +1100 Subject: [PATCH 5/5] Fix formatting & typing --- anastruct/fem/system.py | 5 +++-- tests/{test_stifness.py => test_stiffness.py} | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) rename tests/{test_stifness.py => test_stiffness.py} (84%) diff --git a/anastruct/fem/system.py b/anastruct/fem/system.py index d80b39c..84d37eb 100644 --- a/anastruct/fem/system.py +++ b/anastruct/fem/system.py @@ -2292,7 +2292,7 @@ def apply_load_case(self, loadcase: LoadCase) -> None: exec(f"self.{method}({kwargs})") # pylint: disable=exec-used - def get_stiffness_matrix(self, element_id: int) -> Optional[Union[list, None]]: + def get_stiffness_matrix(self, element_id: int) -> Optional[np.ndarray]: """ Return the stiffness matrix for a specific element by its ID. Args: @@ -2308,7 +2308,7 @@ def get_stiffness_matrix(self, element_id: int) -> Optional[Union[list, None]]: return None if isinstance(element, Element): - if hasattr(element, 'stiffness_matrix'): + if hasattr(element, "stiffness_matrix"): print(f"Stiffness Matrix for Element ID {element_id}:") return element.stiffness_matrix @@ -2339,6 +2339,7 @@ def __deepcopy__(self, _: str) -> "SystemElements": return system + def _negative_index_to_id(idx: int, collection: Collection[int]) -> int: """Convert a negative index to a positive index. (That is, allowing the Pythonic negative indexing) diff --git a/tests/test_stifness.py b/tests/test_stiffness.py similarity index 84% rename from tests/test_stifness.py rename to tests/test_stiffness.py index c8ae5f5..8fe33c8 100644 --- a/tests/test_stifness.py +++ b/tests/test_stiffness.py @@ -1,7 +1,7 @@ import unittest -import numpy as np + from anastruct.fem import system as se -from anastruct.fem.elements import Element + class TestSystemElements(unittest.TestCase): def test_get_stiffness_matrix(self): @@ -9,7 +9,9 @@ def test_get_stiffness_matrix(self): system = se.SystemElements() # Add a simple beam element (make sure it has a stiffness matrix) - system.add_element(location=[[0, 0], [5, 0]]) # Adjust as necessary based on your implementation + system.add_element( + location=[[0, 0], [5, 0]] + ) # Adjust as necessary based on your implementation # Here, we assume that the added element has a stiffness matrix defined # Check if stiffness matrix is correctly returned for element 1 @@ -28,5 +30,6 @@ def test_invalid_element(self): self.assertIsNone(stiffness_matrix) print("Handled invalid element ID correctly.") -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main()