Modifying Model after Model::initSystem()

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Moon Ki Jung
Posts: 44
Joined: Wed Oct 24, 2007 2:56 am

Modifying Model after Model::initSystem()

Post by Moon Ki Jung » Thu May 13, 2010 8:53 am

Hi,

I tried to make a simple program using OpenSim API according to the following flow:

Model osimModel("Arm26_Optimize.osim");
// Get the state variables to modify the coordinate values of the model
State& si = Model.initSystem();

// Modify a coordinate as a desired value
OpenSim::Set<OpenSim::Coordinate>& refCoordinateSet = pModel->updCoordinateSet();
OpenSim::Coordinate& refCoord = refCoordinateSet.get("r_elbow_flex");
refCoord.setValue(si, 0.000);

// Add a constraint between a body and the ground
Model.invalidateSystem();
OpenSim::PointConstraint* constraint1 = new OpenSim::PointConstraint();
constraint1->setBody1ByName("ground");
constraint1->setBody1PointLocation(Vec3(a,b,c));
constraint1->setBody2ByName(“r_ulna_radius_hand”);
constraint1->setBody2PointLocation(Vec3(x,y,z));
Model.addConstraint(constraint1);
si = Model.initSystem();
constraint1->initState(si);

But when I debug the above code, a critical error occurred at ‘si= Model.initSystem()’ operation. It is a crash.

I found that most of OpenSim API examples in the OpenSim developer’s guide called this Model::initSystem() function after constructing the model before simulation. But in my case, to change the human posture to find a correct initial configuration considering the environment, Coordinate::setValue() and Coordinate::getValue() functions are needed. To use these functions, I have to call ‘si = Model.initSystem()’ to use the state variables.

I found ‘Model::invalidateSystem()’ function. I thought that if I use this function, I can change the OpenSim model by adding some constraints or forces even after calling Model::initSystem() function. But when I see a critical error, I think that my guess may be wrong.
My question is that whether adding some constraints, forces or even bodies(joints) can be allowed after calling Model::initSystem() function? Is there any possible way to do this?

If it is now allowed, I think that users should know all coordinate values of the initial configuration that satisfies the kinematic constraints of the system. But it is very hard if we try to model a product with an OpenSim human model.

My idea is as follows:
(1) Load an Osim model that includes a human.
(2) Render a product model that is not an OpenSim object yet.
(3) Change the posture of human model to have interaction with the product model.
(4) Add the bodies(joints) that represent the product model to existing OpenSim Model by OpenSim API.
(5) Add some constraints to make interaction between the product and human model by OpenSim API
(6) Perform a simulation

If my idea is not achievable, would you please give me some advice?

Best regards,
Moonki

User avatar
Moon Ki Jung
Posts: 44
Joined: Wed Oct 24, 2007 2:56 am

RE: Modifying Model after Model::initSystem()

Post by Moon Ki Jung » Fri May 14, 2010 7:23 am

Hi,

I could solve this problem by virtue of Ayman’s help.

Some important notifications:
1) Declaring a new reference and using it in the second call to initSystem, or using a "Copy" of the state rather than a reference (you may need to call realize explicitly since the cache is not copied by the State assignment operator) should get you around this problem.
2) You should not need to invalidate the System explicitly unless you have a reason, otherwise the code that adds/removes components takes care of it for you.
3) The PointConstraint is broken in version 2.0.2 (May not have worked in other versions either). Use the PointOnLineConstraint.

The most critical problem is the use of PointConstraint because it is obsolete.

Hope this may help and thanks to Ayman again,
Moonki

POST REPLY