Bug? on transferring from simple vector to state

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
barak ostraich
Posts: 42
Joined: Thu Apr 12, 2018 12:17 am

Bug? on transferring from simple vector to state

Post by barak ostraich » Wed Jul 28, 2021 7:23 am

I'm using a vector of 1 to 10 to check how it transfers to state

Code: Select all

st=model.initSystem(); %empty state
vs=org.opensim.modeling.Vector; % empty vector
v=[1:st.getNQ]; % matlab vector of 1 to 10
st.setQ(vs.createFromMat(v)); %passing the 1:10 vector to state Q
so=model.getStateVariableValues(st).getAsMat;
I get:
1 0 2 0 3 0 4 0 7 0 9 0 5 0 8 0 10 0 6
The zeros are the U, but why the numbers are mixed??

Tags:

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: Bug? on transferring from simple vector to state

Post by Ayman Habib » Wed Jul 28, 2021 9:42 am

Hello,

I don't believe the OpenSim API makes any claims about the ordering of the Qs in the state vector, as such I don't believe this is a bug. There're model methods to obtain coordinates in multibody tree order e.g.

Code: Select all

getCoordinatesInMultibodyTreeOrder
that you may find useful, however.

Hope this helps,
-Ayman

User avatar
barak ostraich
Posts: 42
Joined: Thu Apr 12, 2018 12:17 am

Re: Bug? on transferring from simple vector to state

Post by barak ostraich » Wed Aug 04, 2021 12:31 am

thanks
i understand the meaning. But how can i get members of the vector i got in Matlab
i get "org.opensim.modeling.SWIGTYPE_p_std__vectorT_SimTK__ReferencePtrT_OpenSim__Coordinate"

how can i get the members of the vector in Matlab?

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: Bug? on transferring from simple vector to state

Post by Thomas Uchida » Wed Aug 04, 2021 6:02 am

But how can i get members of the vector i got in Matlab
You can find some examples here:
- The "Scripting" section of the User's Guide: https://simtk-confluence.stanford.edu/d ... /Scripting
- Your "Resources" directory (e.g., C:\OpenSim 4.1\Resources\Code\Matlab)
- On GitHub: https://github.com/opensim-org/opensim- ... b/examples
It isn't clear what you are trying to do with "org.opensim.modeling.SWIGTYPE_p_std__vectorT_SimTK__ReferencePtrT_OpenSim__Coordinate", but you can try the "get()" method.

User avatar
barak ostraich
Posts: 42
Joined: Thu Apr 12, 2018 12:17 am

Re: Bug? on transferring from simple vector to state

Post by barak ostraich » Fri Aug 06, 2021 8:37 am

i cant get any value from "getCoordinatesInMultibodyTreeOrder"
the get() is not working

i see that similar question was asked on 2017
https://github.com/opensim-org/opensim-core/issues/1858

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: Bug? on transferring from simple vector to state

Post by Thomas Uchida » Fri Aug 06, 2021 9:11 am

What is it you are trying to do? If you need the value of a coordinate, you can do something like the following:

Code: Select all

joint = model.getJointSet().get('myJoint')
pin = osim.PinJoint.safeDownCast(joint)
angle = pin.getCoordinate().getValue(state)
The recommended strategy for interacting with the model is to use the OpenSim modelling interface.

User avatar
barak ostraich
Posts: 42
Joined: Thu Apr 12, 2018 12:17 am

Re: Bug? on transferring from simple vector to state

Post by barak ostraich » Sat Aug 07, 2021 4:51 am

Im sorry for the misunderstanding.
I need to use the state.setY(someVector).
But when i use it, I get it in wrong order in

Code: Select all

model.getStateVariableValues(state)
How will i know in advance what is the order of setY?

thanks

User avatar
Thomas Uchida
Posts: 1793
Joined: Wed May 16, 2012 11:40 am

Re: Bug? on transferring from simple vector to state

Post by Thomas Uchida » Sat Aug 07, 2021 5:38 am

How will i know in advance what is the order of setY?
I think the ordering of states in the state vector will emerge from a depth-first traversal of the Component ownership tree. (The ordering will be the same each time you run your code, but the API doesn't guarantee this.) You could use the following algorithm to determine where each coordinate ends up in the state:

Code: Select all

for i from 1 to the number of coordinates in the model :
    set all states to zero
    set the ith model coordinate to 0.1
    find the index of the only nonzero entry in the state vector
    store the index in a lookup table that relates each coordinate to the corresponding index in the state vector
end for

POST REPLY