From 7039babc5bfa9d3efe4cea46895742bd88d797a6 Mon Sep 17 00:00:00 2001 From: Seung Hyun Kim Date: Tue, 25 Jun 2024 23:31:11 -0500 Subject: [PATCH] refactor: clarify iterating `systems` vs `block_systems` --- elastica/modules/base_system.py | 72 +++++++++++---------- elastica/modules/callbacks.py | 4 +- elastica/modules/connections.py | 8 +-- elastica/modules/constraints.py | 14 ++-- elastica/modules/contact.py | 12 ++-- elastica/modules/damping.py | 6 +- elastica/modules/forcing.py | 6 +- elastica/modules/memory_block.py | 3 +- elastica/modules/protocol.py | 11 ++-- elastica/systems/analytical.py | 3 + elastica/timestepper/symplectic_steppers.py | 10 +-- tests/test_modules/test_base_system.py | 22 ++++--- tests/test_modules/test_callbacks.py | 2 +- tests/test_modules/test_connections.py | 22 +++---- tests/test_modules/test_constraints.py | 6 +- tests/test_modules/test_contact.py | 10 +-- tests/test_modules/test_damping.py | 6 +- tests/test_modules/test_forcing.py | 2 +- tests/test_restart.py | 6 +- 19 files changed, 117 insertions(+), 108 deletions(-) diff --git a/elastica/modules/base_system.py b/elastica/modules/base_system.py index 10c111ce..43666bf6 100644 --- a/elastica/modules/base_system.py +++ b/elastica/modules/base_system.py @@ -8,8 +8,9 @@ from typing import Type, Generator, Iterable, Any, overload from typing import final from elastica.typing import ( - StaticSystemType, SystemType, + StaticSystemType, + BlockSystemType, SystemIdxType, OperatorType, OperatorCallbackType, @@ -38,8 +39,10 @@ class BaseSystemCollection(MutableSequence): ---------- allowed_sys_types: tuple Tuple of allowed type rod-like objects. Here use a base class for objects, i.e. RodBase. - _systems: list - List of rod-like objects. + systems: Callabke + Returns all system objects. Once finalize, block objects are also included. + blocks: Callable + Returns block objects. Should be called after finalize. Note ---- @@ -68,8 +71,8 @@ def __init__(self) -> None: ) # List of systems to be integrated - self._systems: list[StaticSystemType] = [] - self.__final_systems: list[SystemType] = [] + self.__systems: list[StaticSystemType] = [] + self.__final_blocks: list[BlockSystemType] = [] # Flag Finalize: Finalizing twice will cause an error, # but the error message is very misleading @@ -99,7 +102,7 @@ def _check_type(self, sys_to_be_added: Any) -> bool: return True def __len__(self) -> int: - return len(self._systems) + return len(self.__systems) @overload def __getitem__(self, idx: int, /) -> SystemType: ... @@ -108,22 +111,22 @@ def __getitem__(self, idx: int, /) -> SystemType: ... def __getitem__(self, idx: slice, /) -> list[SystemType]: ... def __getitem__(self, idx, /): # type: ignore - return self._systems[idx] + return self.__systems[idx] def __delitem__(self, idx, /): # type: ignore - del self._systems[idx] + del self.__systems[idx] def __setitem__(self, idx, system, /): # type: ignore self._check_type(system) - self._systems[idx] = system + self.__systems[idx] = system def insert(self, idx, system) -> None: # type: ignore self._check_type(system) - self._systems.insert(idx, system) + self.__systems.insert(idx, system) def __str__(self) -> str: """To be readable""" - return str(self._systems) + return str(self.__systems) @final def extend_allowed_types( @@ -138,38 +141,43 @@ def override_allowed_types( self.allowed_sys_types = allowed_types @final - def _get_sys_idx_if_valid( - self, sys_to_be_added: "SystemType | StaticSystemType" + def get_system_index( + self, system: "SystemType | StaticSystemType" ) -> SystemIdxType: n_systems = len(self) # Total number of systems from mixed-in class sys_idx: SystemIdxType - if isinstance(sys_to_be_added, (int, np.int_)): + if isinstance(system, (int, np.int_)): # 1. If they are indices themselves, check range + # This is only used for testing purposes assert ( - -n_systems <= sys_to_be_added < n_systems - ), "Rod index {} exceeds number of registered rodtems".format( - sys_to_be_added - ) - sys_idx = int(sys_to_be_added) - elif self._check_type(sys_to_be_added): - # 2. If they are rod objects (most likely), lookup indices + -n_systems <= system < n_systems + ), "System index {} exceeds number of registered rodtems".format(system) + sys_idx = int(system) + elif self._check_type(system): + # 2. If they are system object (most likely), lookup indices # index might have some problems : https://stackoverflow.com/a/176921 try: - sys_idx = self._systems.index(sys_to_be_added) + sys_idx = self.__systems.index(system) except ValueError: raise ValueError( - "Rod {} was not found, did you append it to the system?".format( - sys_to_be_added + "System {} was not found, did you append it to the system?".format( + system ) ) return sys_idx @final - def systems(self) -> Generator[SystemType, None, None]: + def systems(self) -> Generator[StaticSystemType, None, None]: + # assert self._finalize_flag, "The simulator is not finalized." + for system in self.__systems: + yield system + + @final + def block_systems(self) -> Generator[BlockSystemType, None, None]: # assert self._finalize_flag, "The simulator is not finalized." - for block in self.__final_systems: + for block in self.__final_blocks: yield block @final @@ -184,15 +192,11 @@ def finalize(self) -> None: assert not self._finalize_flag, "The finalize cannot be called twice." self._finalize_flag = True - # construct memory block - self.__final_systems = construct_memory_block_structures(self._systems) - self._systems.extend( - self.__final_systems - ) # FIXME: We need this to make ring-rod working. + # Construct memory block + self.__final_blocks = construct_memory_block_structures(self.__systems) + # FIXME: We need this to make ring-rod working. # But probably need to be refactored - # TODO: try to remove the _systems list for memory optimization - # self._systems.clear() - # del self._systems + self.__systems.extend(self.__final_blocks) # Recurrent call finalize functions for all components. for finalize in self._feature_group_finalize: diff --git a/elastica/modules/callbacks.py b/elastica/modules/callbacks.py index 4a299da0..de1e5091 100644 --- a/elastica/modules/callbacks.py +++ b/elastica/modules/callbacks.py @@ -51,7 +51,7 @@ def collect_diagnostics( ------- """ - sys_idx: SystemIdxType = self._get_sys_idx_if_valid(system) + sys_idx: SystemIdxType = self.get_system_index(system) # Create _Constraint object, cache it and return to user _callbacks: ModuleProtocol = _CallBack(sys_idx) @@ -77,7 +77,7 @@ def _callback_execution( current_step: int, ) -> None: for sys_id, callback in self._callback_operators: - callback.make_callback(self._systems[sys_id], time, current_step) + callback.make_callback(self[sys_id], time, current_step) class _CallBack: diff --git a/elastica/modules/connections.py b/elastica/modules/connections.py index c55d5bea..aa1f63c8 100644 --- a/elastica/modules/connections.py +++ b/elastica/modules/connections.py @@ -66,8 +66,8 @@ def connect( """ # For each system identified, get max dofs - sys_idx_first = self._get_sys_idx_if_valid(first_rod) - sys_idx_second = self._get_sys_idx_if_valid(second_rod) + sys_idx_first = self.get_system_index(first_rod) + sys_idx_second = self.get_system_index(second_rod) sys_dofs_first = first_rod.n_elems sys_dofs_second = second_rod.n_elems @@ -118,9 +118,9 @@ def apply_forces_and_torques( func = functools.partial( apply_forces_and_torques, connect_instance=connect_instance, - system_one=self._systems[first_sys_idx], + system_one=self[first_sys_idx], first_connect_idx=first_connect_idx, - system_two=self._systems[second_sys_idx], + system_two=self[second_sys_idx], second_connect_idx=second_connect_idx, ) diff --git a/elastica/modules/constraints.py b/elastica/modules/constraints.py index 4a2c5840..029ed961 100644 --- a/elastica/modules/constraints.py +++ b/elastica/modules/constraints.py @@ -57,7 +57,7 @@ def constrain( ------- """ - sys_idx = self._get_sys_idx_if_valid(system) + sys_idx = self.get_system_index(system) # Create _Constraint object, cache it and return to user _constraint: ModuleProtocol = _Constraint(sys_idx) @@ -73,13 +73,13 @@ def _finalize_constraints(self: SystemCollectionProtocol) -> None: """ from elastica._synchronize_periodic_boundary import _ConstrainPeriodicBoundaries - for block in self.systems(): + for block in self.block_systems(): # append the memory block to the simulation as a system. Memory block is the final system in the simulation. if hasattr(block, "ring_rod_flag"): # Apply the constrain to synchronize the periodic boundaries of the memory rod. Find the memory block # sys idx among other systems added and then apply boundary conditions. - memory_block_idx = self._get_sys_idx_if_valid(block) - block_system = cast(BlockSystemType, self._systems[memory_block_idx]) + memory_block_idx = self.get_system_index(block) + block_system = cast(BlockSystemType, self[memory_block_idx]) self.constrain(block_system).using( _ConstrainPeriodicBoundaries, ) @@ -90,7 +90,7 @@ def _finalize_constraints(self: SystemCollectionProtocol) -> None: # dev : the first index stores the rod index to apply the boundary condition # to. self._constraints_operators = [ - (constraint.id(), constraint.instantiate(self._systems[constraint.id()])) + (constraint.id(), constraint.instantiate(self[constraint.id()])) for constraint in self._constraints_list ] @@ -109,11 +109,11 @@ def _finalize_constraints(self: SystemCollectionProtocol) -> None: def _constrain_values(self: SystemCollectionProtocol, time: np.float64) -> None: for sys_id, constraint in self._constraints_operators: - constraint.constrain_values(self._systems[sys_id], time) + constraint.constrain_values(self[sys_id], time) def _constrain_rates(self: SystemCollectionProtocol, time: np.float64) -> None: for sys_id, constraint in self._constraints_operators: - constraint.constrain_rates(self._systems[sys_id], time) + constraint.constrain_rates(self[sys_id], time) class _Constraint: diff --git a/elastica/modules/contact.py b/elastica/modules/contact.py index d3013635..76491664 100644 --- a/elastica/modules/contact.py +++ b/elastica/modules/contact.py @@ -63,8 +63,8 @@ def detect_contact_between( ------- """ - sys_idx_first = self._get_sys_idx_if_valid(first_system) - sys_idx_second = self._get_sys_idx_if_valid(second_system) + sys_idx_first = self.get_system_index(first_system) + sys_idx_second = self.get_system_index(second_system) # Create _Contact object, cache it and return to user _contact = _Contact(sys_idx_first, sys_idx_second) @@ -86,8 +86,8 @@ def apply_contact( second_sys_idx: SystemIdxType, ) -> None: contact_instance.apply_contact( - system_one=self._systems[first_sys_idx], - system_two=self._systems[second_sys_idx], + system_one=self[first_sys_idx], + system_two=self[second_sys_idx], ) for contact in self._contacts: @@ -95,8 +95,8 @@ def apply_contact( contact_instance = contact.instantiate() contact_instance._check_systems_validity( - self._systems[first_sys_idx], - self._systems[second_sys_idx], + self[first_sys_idx], + self[second_sys_idx], ) func = functools.partial( apply_contact, diff --git a/elastica/modules/damping.py b/elastica/modules/damping.py index 646e1a21..ea9ea5ea 100644 --- a/elastica/modules/damping.py +++ b/elastica/modules/damping.py @@ -52,7 +52,7 @@ def dampen(self: SystemCollectionProtocol, system: RodType) -> ModuleProtocol: ------- """ - sys_idx = self._get_sys_idx_if_valid(system) + sys_idx = self.get_system_index(system) # Create _Damper object, cache it and return to user _damper: ModuleProtocol = _Damper(sys_idx) @@ -65,7 +65,7 @@ def _finalize_dampers(self: SystemCollectionProtocol) -> None: # inplace : https://stackoverflow.com/a/1208792 self._damping_operators = [ - (damper.id(), damper.instantiate(self._systems[damper.id()])) + (damper.id(), damper.instantiate(self[damper.id()])) for damper in self._damping_list ] @@ -78,7 +78,7 @@ def _finalize_dampers(self: SystemCollectionProtocol) -> None: def _dampen_rates(self: SystemCollectionProtocol, time: np.float64) -> None: for sys_id, damper in self._damping_operators: - damper.dampen_rates(self._systems[sys_id], time) + damper.dampen_rates(self[sys_id], time) class _Damper: diff --git a/elastica/modules/forcing.py b/elastica/modules/forcing.py index d6475d8a..e36b0899 100644 --- a/elastica/modules/forcing.py +++ b/elastica/modules/forcing.py @@ -53,7 +53,7 @@ def add_forcing_to( ------- """ - sys_idx = self._get_sys_idx_if_valid(system) + sys_idx = self.get_system_index(system) # Create _Constraint object, cache it and return to user _ext_force_torque = _ExtForceTorque(sys_idx) @@ -73,10 +73,10 @@ def _finalize_forcing(self: SystemCollectionProtocol) -> None: forcing_instance = external_force_and_torque.instantiate() apply_forces = functools.partial( - forcing_instance.apply_forces, system=self._systems[sys_id] + forcing_instance.apply_forces, system=self[sys_id] ) apply_torques = functools.partial( - forcing_instance.apply_torques, system=self._systems[sys_id] + forcing_instance.apply_torques, system=self[sys_id] ) self._feature_group_synchronize.add_operators( diff --git a/elastica/modules/memory_block.py b/elastica/modules/memory_block.py index d0e751c1..fa75f744 100644 --- a/elastica/modules/memory_block.py +++ b/elastica/modules/memory_block.py @@ -8,7 +8,6 @@ RigidBodyType, SurfaceType, StaticSystemType, - SystemType, SystemIdxType, BlockSystemType, ) @@ -22,7 +21,7 @@ def construct_memory_block_structures( systems: list[StaticSystemType], -) -> list[SystemType]: +) -> list[BlockSystemType]: """ This function takes the systems (rod or rigid body) appended to the simulator class and separates them into lists depending on if system is Cosserat rod or rigid body. Then using diff --git a/elastica/modules/protocol.py b/elastica/modules/protocol.py index 4ca93a17..eb1b5d84 100644 --- a/elastica/modules/protocol.py +++ b/elastica/modules/protocol.py @@ -10,6 +10,7 @@ OperatorFinalizeType, StaticSystemType, SystemType, + BlockSystemType, ConnectionIndex, ) from elastica.joint import FreeJoint @@ -34,10 +35,12 @@ def id(self) -> Any: ... class SystemCollectionProtocol(Protocol): - _systems: list[StaticSystemType] - def __len__(self) -> int: ... + def systems(self) -> Generator[StaticSystemType, None, None]: ... + + def block_systems(self) -> Generator[BlockSystemType, None, None]: ... + @overload def __getitem__(self, i: slice) -> list[SystemType]: ... @overload @@ -67,9 +70,7 @@ def apply_callbacks(self, time: np.float64, current_step: int) -> None: ... @property def _feature_group_finalize(self) -> list[OperatorFinalizeType]: ... - def systems(self) -> Generator[SystemType, None, None]: ... - - def _get_sys_idx_if_valid( + def get_system_index( self, sys_to_be_added: "SystemType | StaticSystemType" ) -> SystemIdxType: ... diff --git a/elastica/systems/analytical.py b/elastica/systems/analytical.py index 317b3f99..60fe712b 100644 --- a/elastica/systems/analytical.py +++ b/elastica/systems/analytical.py @@ -304,6 +304,9 @@ def __init__(self): def systems(self): return self._memory_blocks + def block_systems(self): + return self._memory_blocks + def __getitem__(self, idx): return self._memory_blocks[idx] diff --git a/elastica/timestepper/symplectic_steppers.py b/elastica/timestepper/symplectic_steppers.py index 29c3a7e0..3937e337 100644 --- a/elastica/timestepper/symplectic_steppers.py +++ b/elastica/timestepper/symplectic_steppers.py @@ -97,7 +97,7 @@ def do_step( """ for kin_prefactor, kin_step, dyn_step in steps_and_prefactors[:-1]: - for system in SystemCollection.systems(): + for system in SystemCollection.block_systems(): kin_step(system, time, dt) time += kin_prefactor(dt) @@ -106,14 +106,14 @@ def do_step( SystemCollection.constrain_values(time) # We need internal forces and torques because they are used by interaction module. - for system in SystemCollection.systems(): + for system in SystemCollection.block_systems(): system.compute_internal_forces_and_torques(time) # system.update_internal_forces_and_torques() # Add external forces, controls etc. SystemCollection.synchronize(time) - for system in SystemCollection.systems(): + for system in SystemCollection.block_systems(): dyn_step(system, time, dt) # Constrain only rates @@ -123,7 +123,7 @@ def do_step( last_kin_prefactor = steps_and_prefactors[-1][0] last_kin_step = steps_and_prefactors[-1][1] - for system in SystemCollection.systems(): + for system in SystemCollection.block_systems(): last_kin_step(system, time, dt) time += last_kin_prefactor(dt) SystemCollection.constrain_values(time) @@ -132,7 +132,7 @@ def do_step( SystemCollection.apply_callbacks(time, round(time / dt)) # Zero out the external forces and torques - for system in SystemCollection.systems(): + for system in SystemCollection.block_systems(): system.zeroed_out_external_forces_and_torques(time) return time diff --git a/tests/test_modules/test_base_system.py b/tests/test_modules/test_base_system.py index e3a74dcd..c126fec4 100644 --- a/tests/test_modules/test_base_system.py +++ b/tests/test_modules/test_base_system.py @@ -25,11 +25,12 @@ def test_check_type_with_illegal_type_throws(self, illegal_type): @pytest.fixture(scope="class") def load_collection(self): bsc = BaseSystemCollection() + bsc.extend_allowed_types((int, float, str, np.ndarray)) # Bypass check, but its fine for testing - bsc._systems.append(3) - bsc._systems.append(5.0) - bsc._systems.append("a") - bsc._systems.append(np.random.randn(3, 5)) + bsc.append(3) + bsc.append(5.0) + bsc.append("a") + bsc.append(np.random.randn(3, 5)) return bsc def test_len(self, load_collection): @@ -70,12 +71,12 @@ def test_str(self, load_collection): def test_extend_allowed_types(self, load_collection): bsc = load_collection - bsc.extend_allowed_types((int, float, str)) from elastica.rod import RodBase from elastica.rigidbody import RigidBodyBase from elastica.surface import SurfaceBase + # Types are extended in the fixture assert bsc.allowed_sys_types == ( RodBase, RigidBodyBase, @@ -83,6 +84,7 @@ def test_extend_allowed_types(self, load_collection): int, float, str, + np.ndarray, ) def test_extend_correctness(self, load_collection): @@ -121,11 +123,11 @@ def test_invalid_idx_in_get_sys_index_throws(self, load_collection): bsc = load_collection bsc.override_allowed_types((RodBase,)) with pytest.raises(AssertionError) as excinfo: - bsc._get_sys_idx_if_valid(100) + bsc.get_system_index(100) assert "exceeds number of" in str(excinfo.value) with pytest.raises(AssertionError) as excinfo: - load_collection._get_sys_idx_if_valid(np.int_(100)) + load_collection.get_system_index(np.int_(100)) assert "exceeds number of" in str(excinfo.value) def test_unregistered_system_in_get_sys_index_throws( @@ -135,11 +137,11 @@ def test_unregistered_system_in_get_sys_index_throws( my_mock_rod = mock_rod with pytest.raises(ValueError) as excinfo: - load_collection._get_sys_idx_if_valid(my_mock_rod) + load_collection.get_system_index(my_mock_rod) assert "was not found, did you" in str(excinfo.value) def test_get_sys_index_returns_correct_idx(self, load_collection): - assert load_collection._get_sys_idx_if_valid(1) == 1 + assert load_collection.get_system_index(1) == 1 @pytest.mark.xfail def test_delitem(self, load_collection): @@ -171,7 +173,7 @@ def load_collection(self): youngs_modulus=1, ) # Bypass check, but its fine for testing - sc._systems.append(rod) + sc.append(rod) return sc, rod diff --git a/tests/test_modules/test_callbacks.py b/tests/test_modules/test_callbacks.py index b6216cc5..1716d2fa 100644 --- a/tests/test_modules/test_callbacks.py +++ b/tests/test_modules/test_callbacks.py @@ -80,7 +80,7 @@ def load_system_with_callbacks(self, request): sys_coll_with_callbacks.append(self.MockRod(2, 3, 4, 5)) return sys_coll_with_callbacks - """ The following calls test _get_sys_idx_if_valid from BaseSystem indirectly, + """ The following calls test get_system_index from BaseSystem indirectly, and are here because of legacy reasons. I have not removed them because there are Callbacks require testing against multiple indices, which is still use ful to cross-verify against. diff --git a/tests/test_modules/test_connections.py b/tests/test_modules/test_connections.py index 53ed22f4..26f0cb24 100644 --- a/tests/test_modules/test_connections.py +++ b/tests/test_modules/test_connections.py @@ -201,7 +201,7 @@ def load_system_with_connects(self, request): sys_coll_with_connects.append(self.MockRod(2, 3, 4, 5)) return sys_coll_with_connects - """ The following calls test _get_sys_idx_if_valid from BaseSystem indirectly, + """ The following calls test get_system_index from BaseSystem indirectly, and are here because of legacy reasons. I have not removed them because there are Connections require testing against multiple indices, which is still use ful to cross-verify against. @@ -401,20 +401,20 @@ def test_connect_call_on_systems(self, load_rod_with_connects_and_indices): connect = connection.instantiate() end_distance_vector = ( - system_collection_with_connections_and_indices._systems[ + system_collection_with_connections_and_indices[ sidx ].position_collection[..., sconnect] - - system_collection_with_connections_and_indices._systems[ + - system_collection_with_connections_and_indices[ fidx ].position_collection[..., fconnect] ) elastic_force = connect.k * end_distance_vector relative_velocity = ( - system_collection_with_connections_and_indices._systems[ + system_collection_with_connections_and_indices[ sidx ].velocity_collection[..., sconnect] - - system_collection_with_connections_and_indices._systems[ + - system_collection_with_connections_and_indices[ fidx ].velocity_collection[..., fconnect] ) @@ -423,16 +423,16 @@ def test_connect_call_on_systems(self, load_rod_with_connects_and_indices): contact_force = elastic_force + damping_force assert_allclose( - system_collection_with_connections_and_indices._systems[ - fidx - ].external_forces[..., fconnect], + system_collection_with_connections_and_indices[fidx].external_forces[ + ..., fconnect + ], contact_force, atol=Tolerance.atol(), ) assert_allclose( - system_collection_with_connections_and_indices._systems[ - sidx - ].external_forces[..., sconnect], + system_collection_with_connections_and_indices[sidx].external_forces[ + ..., sconnect + ], -1 * contact_force, atol=Tolerance.atol(), ) diff --git a/tests/test_modules/test_constraints.py b/tests/test_modules/test_constraints.py index 069479d1..e5e5892e 100644 --- a/tests/test_modules/test_constraints.py +++ b/tests/test_modules/test_constraints.py @@ -227,7 +227,7 @@ def load_system_with_constraints(self, request): sys_coll_with_constraints.append(self.MockRod(2, 3, 4, 5)) return sys_coll_with_constraints - """ The following calls test _get_sys_idx_if_valid from BaseSystem indirectly, + """ The following calls test get_system_index from BaseSystem indirectly, and are here because of legacy reasons. I have not removed them because there are Connections require testing against multiple indices, which is still use ful to cross-verify against. @@ -328,11 +328,11 @@ def test_constraint_properties(self, load_rod_with_constraints): for i in [0, 1, -1]: x, y = scwc._constraints_operators[i] - mock_rod = scwc._systems[i] + mock_rod = scwc[i] # Test system assert type(x) is int assert type(y.system) is type(mock_rod) - assert y.system is mock_rod, f"{len(scwc._systems)}" + assert y.system is mock_rod, f"{len(scwc)}" # Test node indices assert y.constrained_position_idx.size == 0 # Test element indices. TODO: maybe add more generalized test diff --git a/tests/test_modules/test_contact.py b/tests/test_modules/test_contact.py index 5b9ffe75..19ea3560 100644 --- a/tests/test_modules/test_contact.py +++ b/tests/test_modules/test_contact.py @@ -125,7 +125,7 @@ def load_system_with_contacts(self, request): sys_coll_with_contacts.append(self.MockRod(2, 3, 4, 5)) return sys_coll_with_contacts - """ The following calls test _get_sys_idx_if_valid from BaseSystem indirectly, + """ The following calls test get_system_index from BaseSystem indirectly, and are here because of legacy reasons. I have not removed them because there are Contacts require testing against multiple indices, which is still use ful to cross-verify against. @@ -364,8 +364,8 @@ def test_contact_call_on_systems(self, load_system_with_rods_in_contact): fidx, sidx = _contact.id() contact = _contact.instantiate() - system_one = system_collection_with_rods_in_contact._systems[fidx] - system_two = system_collection_with_rods_in_contact._systems[sidx] + system_one = system_collection_with_rods_in_contact[fidx] + system_two = system_collection_with_rods_in_contact[sidx] external_forces_system_one = np.zeros_like(system_one.external_forces) external_forces_system_two = np.zeros_like(system_two.external_forces) @@ -393,12 +393,12 @@ def test_contact_call_on_systems(self, load_system_with_rods_in_contact): ) assert_allclose( - system_collection_with_rods_in_contact._systems[fidx].external_forces, + system_collection_with_rods_in_contact[fidx].external_forces, external_forces_system_one, atol=Tolerance.atol(), ) assert_allclose( - system_collection_with_rods_in_contact._systems[sidx].external_forces, + system_collection_with_rods_in_contact[sidx].external_forces, external_forces_system_two, atol=Tolerance.atol(), ) diff --git a/tests/test_modules/test_damping.py b/tests/test_modules/test_damping.py index 2f18a5d6..70ae6cf7 100644 --- a/tests/test_modules/test_damping.py +++ b/tests/test_modules/test_damping.py @@ -103,7 +103,7 @@ def load_system_with_dampers(self, request): sys_coll_with_dampers.append(self.MockRod(2, 3, 4, 5)) return sys_coll_with_dampers - """ The following calls test _get_sys_idx_if_valid from BaseSystem indirectly, + """ The following calls test get_system_index from BaseSystem indirectly, and are here because of legacy reasons. I have not removed them because there are Connections require testing against multiple indices, which is still use ful to cross-verify against. @@ -195,11 +195,11 @@ def test_damper_properties(self, load_rod_with_dampers): for i in [0, 1, -1]: x, y = scwd._damping_operators[i] - mock_rod = scwd._systems[i] + mock_rod = scwd[i] # Test system assert type(x) is int assert type(y.system) is type(mock_rod) - assert y.system is mock_rod, f"{len(scwd._systems)}" + assert y.system is mock_rod, f"{len(scwd)}" @pytest.mark.xfail def test_dampers_finalize_sorted(self, load_rod_with_dampers): diff --git a/tests/test_modules/test_forcing.py b/tests/test_modules/test_forcing.py index bd384fc6..74cb2bf3 100644 --- a/tests/test_modules/test_forcing.py +++ b/tests/test_modules/test_forcing.py @@ -87,7 +87,7 @@ def load_system_with_forcings(self, request): sys_coll_with_forcings.append(self.MockRod(2, 3, 4, 5)) return sys_coll_with_forcings - """ The following calls test _get_sys_idx_if_valid from BaseSystem indirectly, + """ The following calls test get_system_index from BaseSystem indirectly, and are here because of legacy reasons. I have not removed them because there are Connections require testing against multiple indices, which is still use ful to cross-verify against. diff --git a/tests/test_restart.py b/tests/test_restart.py index f19cf037..28d30595 100644 --- a/tests/test_restart.py +++ b/tests/test_restart.py @@ -41,7 +41,7 @@ def load_collection(self): youngs_modulus=1, ) # Bypass check, but its fine for testing - sc._systems.append(rod) + sc.append(rod) # Also add rods to a separate list rod_list.append(rod) @@ -100,7 +100,7 @@ class BaseSimulatorClass( youngs_modulus=1, ) # Bypass check, but its fine for testing - simulator_class._systems.append(rod) + simulator_class.append(rod) # Also add rods to a separate list rod_list.append(rod) @@ -199,7 +199,7 @@ def load_collection(self): density=1, ) # Bypass check, but its fine for testing - sc._systems.append(cylinder) + sc.append(cylinder) # Also add rods to a separate list cylinder_list.append(cylinder)