Page 1 of 1

Bug? on transferring from simple vector to state

Posted: Wed Jul 28, 2021 7:23 am
by ostr1969
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??

Re: Bug? on transferring from simple vector to state

Posted: Wed Jul 28, 2021 9:42 am
by aymanh
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

Re: Bug? on transferring from simple vector to state

Posted: Wed Aug 04, 2021 12:31 am
by ostr1969
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?

Re: Bug? on transferring from simple vector to state

Posted: Wed Aug 04, 2021 6:02 am
by tkuchida
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.

Re: Bug? on transferring from simple vector to state

Posted: Fri Aug 06, 2021 8:37 am
by ostr1969
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

Re: Bug? on transferring from simple vector to state

Posted: Fri Aug 06, 2021 9:11 am
by tkuchida
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.

Re: Bug? on transferring from simple vector to state

Posted: Sat Aug 07, 2021 4:51 am
by ostr1969
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

Re: Bug? on transferring from simple vector to state

Posted: Sat Aug 07, 2021 5:38 am
by tkuchida
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