How Measure::Plus works

Simbody is useful for internal coordinate and coarse grained molecule modeling, large scale mechanical models like skeletons, and anything else that can be modeled as bodies interconnected by joints, acted upon by forces, and restricted by constraints.
User avatar
Jiang Ping
Posts: 132
Joined: Sun Aug 26, 2012 4:09 am

How Measure::Plus works

Post by Jiang Ping » Wed Jan 06, 2016 6:00 am

hi all,

I have some question about measurement.
Given Measure A ,Measure B ,a State s and a subsystem system

Delay measure of A and B are:
Measure tmpPM_A=Delay(system,A,aDelay);
Measure tmpPM_B=Delay(system,B,aDelay);

Case1: double c1 = tmpPM_A.getValue(s)+tmpPM_A.getValue(s);

Case2: Measure tmpPM = Measure::Plus(system,tmpPM_A,tmpPM_B)
double c2 = tmpPM.getValue(s);

I think c1 should be equal to c2, but they are slightly different.
Which calculation method for plus of two measurement is more accurate for these two cases?

Thanks in advance,
Jiang

User avatar
Michael Sherman
Posts: 800
Joined: Fri Apr 01, 2005 6:05 pm

Re: How Measure::Plus works

Post by Michael Sherman » Wed Jan 06, 2016 10:12 am

Those should be exactly the same, Jiang! Here is the code Measure::Plus uses for calculating its value (from MeasureImplementation.h):

Code: Select all

    void calcCachedValueVirtual(const State& s, int derivOrder, T& value) const  override
    {
        value = left.getValue(s,derivOrder) + right.getValue(s,derivOrder);
    }
That is the same as what you are doing.

So the difference in value is likely due to some difference in the timing of what is being calculated. The first thing I would do (if you haven't already) is run this in Debug mode to get more error checking. You might find you are accessing an out-of-date value.

Adding a new measure to a System invalidates its states, so be sure you are adding all three measures at the same time.

Please let us know what happens -- I would like to understand this to make sure there isn't a bug somewhere.

Thanks,
Sherm

User avatar
Jiang Ping
Posts: 132
Joined: Sun Aug 26, 2012 4:09 am

Re: How Measure::Plus works

Post by Jiang Ping » Thu Jan 07, 2016 8:31 am

Hi sherm,

Thanks for reply, I will have a check.

I also have another question, which maybe stupid.
Will CPU affect the results?
I found the same program generate different results for old and new cpu.

Seems that cpus which support AVX2 have high accuracy than those which are not supporting AVX2.

Thanks again

User avatar
Michael Sherman
Posts: 800
Joined: Fri Apr 01, 2005 6:05 pm

Re: How Measure::Plus works

Post by Michael Sherman » Thu Jan 07, 2016 12:30 pm

Will CPU affect the results?
I found the same program generate different results for old and new cpu.
Seems that cpus which support AVX2 have high accuracy than those which are not supporting AVX2.
Using a different CPU can affect results (by a very small amount) if the compiler generates different instructions for them. Those small differences can get magnified over time in an unstable simulation.

It should not be the case that one CPU or the other is more accurate, just different by very small roundoff errors. AVX2 can allow faster computation but is supposed to produce the same answers as the narrower instructions do. But instruction ordering can result in differences in the last bit due to rounding, so if the compiler makes use of the AVX2 instructions you would expect tiny differences, and those can get magnified until they seem large.

User avatar
Jiang Ping
Posts: 132
Joined: Sun Aug 26, 2012 4:09 am

Re: How Measure::Plus works

Post by Jiang Ping » Tue Jan 12, 2016 2:27 am

Thanks, sherm.
I am checking my measurement now. I wonder whether the calculation priority will affect the results.
Now I have Measure A, Measure B and Measure C.
Meaure A and B are independent.
Measure C is the plus of Measure A and Measure B.
Measure A and B should be conducted before Measure C,but I dont know how to set the priority.
(A,B and C are in different object)

Another question is that is there any methods to access the state in the last simulation step?

Thanks a lot,
Jiang

User avatar
Michael Sherman
Posts: 800
Joined: Fri Apr 01, 2005 6:05 pm

Re: How Measure::Plus works

Post by Michael Sherman » Tue Jan 12, 2016 10:30 am

Regarding Measure calculation order, what matters is which Subsystem the Measures are defined in. All the Measures of a given Subsystem are evaluated before any Measures of the next Subsystem. And within a Subsystem Measures are evaluated in the order they are defined. So if Measures A,B,C are all in the same Subsystem the ordering should be correct since C refers to A and B and thus must have been defined afterwards.
Another question is that is there any methods to access the state in the last simulation step?
The previous state is not saved unless you explicitly request it. If you want the whole thing you can make the integrator return control every successful step in which case you can keep a copy of the last State. A more precise way to do it would be to use an "auto update" discrete variable to record the previous values of interest. You can read about them here. The idea is that you would copy state values of interest into the auto update variable's update cache entry, then that will become the state variable's next value if the state is accepted.

User avatar
Jiang Ping
Posts: 132
Joined: Sun Aug 26, 2012 4:09 am

Re: How Measure::Plus works

Post by Jiang Ping » Tue Jan 12, 2016 11:28 am

hi sherm,

Thanks for your rapid reply. You are right. It's my fault that I didn't describe my problem clearly.
Measure A and B are in subsystem1, and C in subsystem2.
Measure A and B are updated based on C.
C is the plus of A and B.
It likes a feedback loop where A and B are measured based on C in another subsystem while C also depends on A and B.

I think measure in one subsystem should be set to a default value for initialization but have no idea how to do it.
I also dont know how to organize the calculation order.

Any idea would be much appreciated,
Jiang

User avatar
Michael Sherman
Posts: 800
Joined: Fri Apr 01, 2005 6:05 pm

Re: How Measure::Plus works

Post by Michael Sherman » Tue Jan 12, 2016 11:51 am

Hi, Jiang. It usually doesn't matter much in which subsystem you put a Measure, as long as all the related measures are in the same one. The System itself provides a default Subsystem you can use for this purpose if you want. You can in fact use the System as though it were a Subsystem when defining a Measure -- it automatically converts to its default Subsystem when necessary.

So one thing you could try is just to put all of the Measures in the System default Subsystem. If that isn't workable, then put Measure C in the same subsystem as A and B.

Sherm

User avatar
Jiang Ping
Posts: 132
Joined: Sun Aug 26, 2012 4:09 am

Re: How Measure::Plus works

Post by Jiang Ping » Tue Jan 12, 2016 12:28 pm

sherm wrote:Hi, Jiang. It usually doesn't matter much in which subsystem you put a Measure, as long as all the related measures are in the same one. The System itself provides a default Subsystem you can use for this purpose if you want. You can in fact use the System as though it were a Subsystem when defining a Measure -- it automatically converts to its default Subsystem when necessary.

So one thing you could try is just to put all of the Measures in the System default Subsystem. If that isn't workable, then put Measure C in the same subsystem as A and B.

Sherm
hi sherm,

Thanks for the reply. Actually i mean subsystem1 and subsystem2 belong to two different multibodySystems.
MultibodySystem1 is the global one and 2 is the local one.
A and B are measurements of subsystem1 in multibodySystem1.
C is calculated by local forward dynamics simulation in multibodySystem2 based on A and B.
I attached a figure.

Maybe my way is complicated. But to conduct two forward dynamics simulation (global and local), I have to created two multibodySystem. It'd be appreciated if you could give me some comments.


best regards,
Jiang
Attachments
figMeaure.PNG
figMeaure.PNG (8.5 KiB) Viewed 250 times

User avatar
Michael Sherman
Posts: 800
Joined: Fri Apr 01, 2005 6:05 pm

Re: How Measure::Plus works

Post by Michael Sherman » Tue Jan 12, 2016 12:54 pm

Oh, I understand now. Thanks for clarifying.

Each MultibodySystem has its own State. Measures can only reference other Measures from the same MultibodySystem so that they can update values in the State cache as needed.

If you want to solve everything simultaneously, they would have to be in the same MultibodySystem. If it is OK to update them independently, you have to think more carefully about the communication between the separate Systems. For example, should MultibodySystem1 sample the outputs of MultibodySystem2 periodically?

If you are doing model-based control, where Measure C is intended to be the output of the controller, we have some examples of that: TaskSpaceControl-UR10 (robot arm) and TaskSpaceControl-Atlas (humanoid). In each of those examples there are two multibody systems: one is called "real robot" (that's the simulated one) and the other is "model robot" (that's the one in the controller). "Real robot" has a force element that knows how to use the "model robot" to calculate torques, which are then used to actuate the "real robot".

Please take a look at those examples to see if they apply to your case. You can ignore all the "task space control" stuff and just look at the way communication is handled.

Sherm

POST REPLY