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

Fix exception message when center of mass is NAN. #22016

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
42 changes: 32 additions & 10 deletions multibody/tree/spatial_inertia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ boolean<T> SpatialInertia<T>::IsPhysicallyValid() const {
// This spatial inertia is not physically valid if the mass is negative or
// non-finite or the center of mass or unit inertia matrix have NaN elements.
boolean<T> ret_value = is_nonnegative_finite(mass_);
if constexpr (scalar_predicate<T>::is_bool) {
if (!p_PScm_E_.allFinite() || !G_SP_E_.get_moments().allFinite() ||
!G_SP_E_.get_products().allFinite())
ret_value = false;
}
if (ret_value) {
// Form a rotational inertia about the body's center of mass and then use
// the well-documented tests in RotationalInertia to test validity.
Expand All @@ -356,13 +361,19 @@ void SpatialInertia<T>::ThrowNotPhysicallyValid() const {
std::string error_message =
fmt::format("Spatial inertia fails SpatialInertia::IsPhysicallyValid().");
const T& mass = get_mass();
const Vector3<T>& p_PBcm = get_com();

if (!is_positive_finite(mass)) {
error_message +=
fmt::format("\nmass = {} is not positive and finite.\n", mass);
} else if (!p_PBcm.array().allFinite()) {
error_message += CenterOfMassPositionEqualsString(*this) +=
" is not finite.\n";
} else {
error_message += fmt::format("{}", *this);
WriteExtraCentralInertiaProperties(&error_message);
}

throw std::runtime_error(error_message);
}

Expand Down Expand Up @@ -554,30 +565,41 @@ void SpatialInertia<T>::WriteExtraCentralInertiaProperties(
}
}

template <typename T>
std::string CenterOfMassPositionEqualsString(const SpatialInertia<T>& M) {
// TODO(jwnimmer-tri) Rewrite this to use fmt to our advantage.
const Vector3<T>& p_PBcm = M.get_com();
if constexpr (scalar_predicate<T>::is_bool) {
const T& x = p_PBcm.x();
const T& y = p_PBcm.y();
const T& z = p_PBcm.z();
return fmt::format("\n Center of mass = [{} {} {}]", x, y, z);
} else {
// Print symbolic results.
return fmt::format("\n Center of mass = {}", fmt_eigen(p_PBcm.transpose()));
}
}

template <typename T>
std::ostream& operator<<(std::ostream& out, const SpatialInertia<T>& M) {
// Write the data associated with the spatial inertia M of a body
// (or composite body) B about a point P, expressed in a frame E.
// Typically point P is either Bo (B's origin) or Bcm (B's center of mass)
// and frame E is usually the body frame B. More spatial inertia information
// can be written via SpatialInertia::WriteExtraCentralInertiaProperties().
const T& mass = M.get_mass();
const Vector3<T>& p_PBcm = M.get_com();
const T& x = p_PBcm.x();
const T& y = p_PBcm.y();
const T& z = p_PBcm.z();

// TODO(jwnimmer-tri) Rewrite this to use fmt to our advantage.
const T& mass = M.get_mass();
if constexpr (scalar_predicate<T>::is_bool) {
out << "\n"
<< fmt::format(" mass = {}\n", mass)
<< fmt::format(" Center of mass = [{} {} {}]\n", x, y, z);
out << fmt::format("\n mass = {}", mass);
} else {
// Print symbolic results.
out << " mass = " << mass << "\n"
<< fmt::format(" Center of mass = {}\n", fmt_eigen(p_PBcm.transpose()));
out << "\n mass = " << mass;
}

// Append a string similar to "\n Center of mass = [x y z]\n".
out << CenterOfMassPositionEqualsString(M) << "\n";

// Get G_BP (unit inertia about point P) and use it to calculate I_BP
// (rotational inertia about P) without validity checks such as
// IsPhysicallyValid(). Hence, this method works for error messages.
Expand Down
17 changes: 17 additions & 0 deletions multibody/tree/test/spatial_inertia_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,23 @@ GTEST_TEST(SpatialInertia, IsPhysicallyValidWithBadInertia) {
expected_message);
}

// Ensure IsPhysicallyValid() fails if the center of mass position is NAN.
// Note: This tests that the old exception message was improved from:
// "CalcPrincipalMomentsAndMaybeAxesOfInertia(): Unable to calculate eigenvalues
// or eigenvectors of the 3x3 matrix associated with a RotationalInertia." to
// a message that clearly communicates a problem with center of mass position.
GTEST_TEST(SpatialInertia, IsPhysicallyValidWithCMPositionAsNAN) {
const std::string expected_message =
"Spatial inertia fails SpatialInertia::IsPhysicallyValid\\(\\).\n"
" Center of mass = \\[7 nan 9\\] is not finite.\n";
const Vector3<double> p_BoBcm_B(7, NAN, 9);
const UnitInertia<double> G_BBo_B =
UnitInertia<double>::SolidSphere(/* radius = */ 1.0);
DRAKE_EXPECT_THROWS_MESSAGE(
SpatialInertia<double>(/* mass = */ 1.0, p_BoBcm_B, G_BBo_B),
expected_message);
}

// Tests IsPhysicallyValid() fails within the constructor since the COM given is
// inconsistently too far out for the unit inertia provided.
GTEST_TEST(SpatialInertia, IsPhysicallyValidWithCOMTooFarOut) {
Expand Down