From 56cb7d52b7ae2d8c075ea3efbed831bf1ebcdf73 Mon Sep 17 00:00:00 2001 From: Ipuch Date: Fri, 29 Nov 2024 23:47:33 -0500 Subject: [PATCH] refactor: to simplify a loop of external force to generalized forces --- bionc/bionc_numpy/external_force.py | 6 +----- bionc/bionc_numpy/external_force_global.py | 3 +++ bionc/bionc_numpy/external_force_global_local_point.py | 4 ++++ bionc/bionc_numpy/external_force_in_local.py | 4 ++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/bionc/bionc_numpy/external_force.py b/bionc/bionc_numpy/external_force.py index 54d00448..8b20c81d 100644 --- a/bionc/bionc_numpy/external_force.py +++ b/bionc/bionc_numpy/external_force.py @@ -157,12 +157,8 @@ def to_natural_external_forces(self, Q: NaturalCoordinates) -> np.ndarray: natural_external_forces = np.zeros((12 * Q.nb_qi(), 1)) for segment_index, segment_external_forces in enumerate(self.external_forces): - segment_forces_on_proximal = [] - for external_force in segment_external_forces: - segment_forces_on_proximal += [external_force.transport_on_proximal(Q.vector(segment_index))] - segment_natural_external_forces = np.zeros((12, 1)) - for external_force in segment_forces_on_proximal: + for external_force in segment_external_forces: segment_natural_external_forces += external_force.to_generalized_natural_forces( Q.vector(segment_index) )[:, np.newaxis] diff --git a/bionc/bionc_numpy/external_force_global.py b/bionc/bionc_numpy/external_force_global.py index 3f5db247..dd4c1a8f 100644 --- a/bionc/bionc_numpy/external_force_global.py +++ b/bionc/bionc_numpy/external_force_global.py @@ -95,3 +95,6 @@ def transport_on_proximal( return ExternalForceInGlobalOnProximal(external_forces=new_external_forces) + def to_generalized_natural_forces(self, Qi: SegmentNaturalCoordinates): + """This function returns the external force in the generalized natural forces [12x1] format.""" + return self.transport_on_proximal(Qi).to_generalized_natural_forces(Qi) diff --git a/bionc/bionc_numpy/external_force_global_local_point.py b/bionc/bionc_numpy/external_force_global_local_point.py index 1f5aa7ab..07746de9 100644 --- a/bionc/bionc_numpy/external_force_global_local_point.py +++ b/bionc/bionc_numpy/external_force_global_local_point.py @@ -99,3 +99,7 @@ def transport_on_proximal( new_external_forces[0:3] += additional_torque return ExternalForceInGlobalOnProximal(external_forces=new_external_forces) + + def to_generalized_natural_forces(self, Qi: SegmentNaturalCoordinates) -> np.ndarray: + """This function returns the external force in the generalized natural forces [12x1] format.""" + return self.transport_on_proximal(Qi).to_generalized_natural_forces(Qi) diff --git a/bionc/bionc_numpy/external_force_in_local.py b/bionc/bionc_numpy/external_force_in_local.py index afe47360..925e9f13 100644 --- a/bionc/bionc_numpy/external_force_in_local.py +++ b/bionc/bionc_numpy/external_force_in_local.py @@ -125,3 +125,7 @@ def transport_on_proximal( new_external_forces[0:3] += additional_torque return ExternalForceInGlobalOnProximal(external_forces=new_external_forces) + + def to_generalized_natural_forces(self, Qi: SegmentNaturalCoordinates) -> np.ndarray: + """This function returns the external force in the generalized natural forces [12x1] format.""" + return self.transport_on_proximal(Qi).to_generalized_natural_forces(Qi)