Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add calc methods and Outputs for whole-body momentum to Model #3487

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ v4.5
- Fixed runtime segfault that can occur when trying to use a `WrapObject` that is not a child of a `PhysicalFrame` (#3465)
- Fixed issues #3083 #2575 where analog data is not pulled out from c3d files, a a new function getAnalogDataTable() has been added to the C3DFileAdapter
- Fixed segfault that can occur when building models with unusual joint topologies (it now throws an `OpenSim::Exception` instead, #3299)
- Add `calcMomentum`, `calcAngularMomentum`, `calcLinearMomentum`, and associated `Output`s to `Model` (#3474)


v4.4
Expand Down
23 changes: 23 additions & 0 deletions OpenSim/Simulation/Model/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,29 @@ SimTK::Vec3 Model::calcMassCenterAcceleration(const SimTK::State &s) const
getMultibodySystem().realize(s, Stage::Acceleration);
return getMatterSubsystem().calcSystemMassCenterAccelerationInGround(s);
}
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These Doxygen-style comments don't get picked up from the .cpp file (only the headers). See, for example, the (blank) documentation for Model::calcMassCenterPosition() (notably, the potentially useful comment "Return the position vector of the system mass center, measured from the Ground origin, and expressed in Ground." does not appear). Perhaps at least put the Doxygen comments for your new methods in the header file, and create an Issue to move the others.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I can move all these over.

* Return the spatial momentum about the system mass center expressed in Ground.
*
*/
SimTK::SpatialVec Model::calcMomentum(const SimTK::State &s) const
{
return getMatterSubsystem().calcSystemCentralMomentum(s);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the examples above (e.g., Model::calcMassCenterAcceleration()), shouldn't this line be preceded by

getMultibodySystem().realize(s, Stage::Velocity);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I think the realize() calls should be here since an API user might call the method directly. IIRC, calling realize() isn't [supposed to be] expensive if the State has already been realized to the required Stage.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was weird these calls were inside the calc-COM methods, but I think you're right, the Stage is probably cached and realize won't re-realize if it is. (I also tried taking out the realize calls in the calc-COM methods and it broke some tests, so there's that.)

}
/**
* Return the angular momentum about the system mass center expressed in Ground.
*
*/
SimTK::Vec3 Model::calcAngularMomentum(const SimTK::State& s) const {
return getMatterSubsystem().calcSystemCentralMomentum(s).get(0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly a style suggestion, but may make future maintenance simpler. (and same for calcLinearMomentum if accepted)

Suggested change
return getMatterSubsystem().calcSystemCentralMomentum(s).get(0);
return calcMomentum(s).get(0);

}
/**
* Return the linear momentum expressed in Ground.
*
*/
SimTK::Vec3 Model::calcLinearMomentum(const SimTK::State& s) const {
return getMatterSubsystem().calcSystemCentralMomentum(s).get(1);
}


/**
* Construct outputs
Expand Down
14 changes: 13 additions & 1 deletion OpenSim/Simulation/Model/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,16 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent);
calcKineticEnergy, SimTK::Stage::Position);

OpenSim_DECLARE_OUTPUT(potential_energy, double,
calcPotentialEnergy, SimTK::Stage::Velocity);
calcPotentialEnergy, SimTK::Stage::Velocity);

OpenSim_DECLARE_OUTPUT(momentum, SimTK::SpatialVec,
calcMomentum, SimTK::Stage::Velocity);

OpenSim_DECLARE_OUTPUT(angular_momentum, SimTK::Vec3,
calcAngularMomentum, SimTK::Stage::Velocity);

OpenSim_DECLARE_OUTPUT(linear_momentum, SimTK::Vec3,
calcLinearMomentum, SimTK::Stage::Velocity);


//=============================================================================
Expand Down Expand Up @@ -833,6 +842,9 @@ OpenSim_OBJECT_NONTEMPLATE_DEFS(Model, ModelComponent);
SimTK::Vec3 calcMassCenterPosition(const SimTK::State &s) const;
SimTK::Vec3 calcMassCenterVelocity(const SimTK::State &s) const;
SimTK::Vec3 calcMassCenterAcceleration(const SimTK::State &s) const;
SimTK::SpatialVec calcMomentum(const SimTK::State &s) const;
SimTK::Vec3 calcAngularMomentum(const SimTK::State &s) const;
SimTK::Vec3 calcLinearMomentum(const SimTK::State &s) const;
/** return the total Kinetic Energy for the underlying system.*/
double calcKineticEnergy(const SimTK::State &s) const {
return getMultibodySystem().calcKineticEnergy(s);
Expand Down