Page 1 of 1

Spatial Vectors in Python

Posted: Wed Aug 07, 2024 1:48 pm
by juanberet
Hello everyone!

I am writing to ask about the use of Spatial Vectors in Python. Specifically, I am trying to calculate Coriolis forces using the following function:
Vector calcCoriolis(const State& s, const Model& model) {
Vector c;
model.getMatterSubsystem().calcResidualForceIgnoringConstraints(
s, Vector(0), Vector_<SpatialVec>(0), Vector(0), c);
return c;
}
My problem is that I am not able to correctly define the SpatialVec in Python. I am defining the Vector(0) as opensim.Vec3(0).

I would be very grateful if someone could give me a hand with this.

Re: Spatial Vectors in Python

Posted: Thu Aug 08, 2024 10:32 am
by tkuchida
The third argument of calcResidualForceIgnoringConstraints() is a Vector of SpatialVecs (doxygen: https://simtk.org/api_docs/simbody/3.5/ ... d43db05df0), which is bound to "VectorOfSpatialVec" in Python (see https://github.com/opensim-org/opensim- ... ody.i#L222).

Re: Spatial Vectors in Python

Posted: Thu Aug 08, 2024 11:56 am
by juanberet
Thank you very much for your answer Thomas!
I am trying to use:
opensim.VectorOfSpatialVec(size,osim.SpatialVec(osim.Vec3(0), osim.Vec3(0)))
and the data type seems correct, but when I use calcResidualForceIgnoringConstraints function the output is:
TypeError: in method 'SimbodyMatterSubsystem_calcResidualForceIgnoringConstraints', argument 3 of type 'SimTK::Vector const &'
Any idea how I could fix this and calculate the Coriolis forces? Or is there any other python implementation for this?

Re: Spatial Vectors in Python

Posted: Thu Aug 08, 2024 2:23 pm
by tkuchida
The method takes a reference. I don't know what your code looks like, but I'm guessing that you're passing in "opensim.VectorOfSpatialVec(size,osim.SpatialVec(osim.Vec3(0), osim.Vec3(0)))" as the third argument. If so, I would instead try assigning this to a variable on the line above and passing in the variable when calling calcResidualForceIgnoringConstraints(). You are unlikely to find an example that does exactly what you are trying to do, but the examples under opensim-core/Bindings/Python/examples (https://github.com/opensim-org/opensim- ... n/examples) and opensim-core/Bindings/Python/tests (https://github.com/opensim-org/opensim- ... thon/tests) often use this strategy.