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

[framework] Fix diagram scalar conversion losing external constraints #22490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jwnimmer-tri
Copy link
Collaborator

@jwnimmer-tri jwnimmer-tri commented Jan 18, 2025

When a subsystem in a diagram had an ExternalSystemConstraint attached to it, the constraint wasn't being propagated through scalar conversion.

(I noticed this while testing the forthcoming new Python logic for scalar conversion in #21968.)


This change is Reviewable

@jwnimmer-tri jwnimmer-tri added priority: medium release notes: fix This pull request contains fixes (no new features) labels Jan 18, 2025
@jwnimmer-tri jwnimmer-tri force-pushed the diagram-scalar-conversion-constraints-tests branch from 50811c1 to eb193c4 Compare January 18, 2025 18:19
When a subsystem in a diagram had an ExternalSystemConstraint attached
to it, the constraint wasn't being propagated through scalar conversion.

Also fix a docs typo noticed while debugging.
@jwnimmer-tri jwnimmer-tri force-pushed the diagram-scalar-conversion-constraints-tests branch from eb193c4 to cf2c70f Compare January 18, 2025 18:20
@jwnimmer-tri
Copy link
Collaborator Author

+@sherm1 for feature review, please.

Copy link
Member

@sherm1 sherm1 left a comment

Choose a reason for hiding this comment

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

Feature checkpoint. I'm confused about the intended execution sequence -- PTAL.
(Out of time for this evening so no rush to respond)

Reviewed 6 of 8 files at r1, all commit messages.
Reviewable status: 1 unresolved discussion, LGTM missing from assignee sherm1(platform), needs at least two assigned reviewers


systems/framework/system.h line 1437 at r2 (raw file):

    auto result = system_scalar_converter_.Convert<U, T>(*this);
    if (result) {
      result->HandlePostConstructionScalarConversion(*this, result.get());

BTW Stepping through this in the debugger, the Convert above ultimately calls Diagram::ConvertScalarType which now calls HandlePostConstructionScalarConversion().

So it looks like that gets called twice. Is that your intention, or am I misunderstanding?

Copy link
Collaborator Author

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

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

Reviewed 8 of 8 files at r1, all commit messages.
Reviewable status: 1 unresolved discussion, LGTM missing from assignee sherm1(platform), needs at least two assigned reviewers


systems/framework/system.h line 1437 at r2 (raw file):

Previously, sherm1 (Michael Sherman) wrote…

BTW Stepping through this in the debugger, the Convert above ultimately calls Diagram::ConvertScalarType which now calls HandlePostConstructionScalarConversion().

So it looks like that gets called twice. Is that your intention, or am I misunderstanding?

My expectation is that HandlePostConstructionScalarConversion is called exactly once for everything being converted -- both leaf systems and diagrams.

I can also trace it through tomorrow to see if the code is actually doing that =P.

Copy link
Collaborator Author

@jwnimmer-tri jwnimmer-tri left a comment

Choose a reason for hiding this comment

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

Reviewable status: 1 unresolved discussion, LGTM missing from assignee sherm1(platform), needs at least two assigned reviewers


systems/framework/system.h line 1437 at r2 (raw file):

Previously, jwnimmer-tri (Jeremy Nimmer) wrote…

My expectation is that HandlePostConstructionScalarConversion is called exactly once for everything being converted -- both leaf systems and diagrams.

I can also trace it through tomorrow to see if the code is actually doing that =P.

Yes, if I add this logger:

--- a/systems/framework/system.h
+++ b/systems/framework/system.h
@@ -21,6 +21,7 @@
 #include "drake/common/nice_type_name.h"
 #include "drake/common/pointer_cast.h"
 #include "drake/common/random.h"
+#include "drake/common/text_logging.h"
 #include "drake/systems/framework/context.h"
 #include "drake/systems/framework/event_collection.h"
 #include "drake/systems/framework/input_port.h"
@@ -1953,6 +1954,7 @@ class System : public SystemBase {
   static void HandlePostConstructionScalarConversion(const System<U>& from,
                                                      System<T>* to) {
     DRAKE_DEMAND(to != nullptr);
+    drake::log()->warn("AddExternalConstraints from {} {}", from.GetSystemPathname(), from.GetMemoryObjectName());
     to->AddExternalConstraints(from.external_constraints_);
   }
 

Then I see this output:

Two scalar conversions (the test case does ToAutoDiffXd and then ToSymbolic), and for each conversion the function is called both subsystems and then on the diagram.

[ RUN      ] DiagramConstraintTest.SystemConstraintsTest
[2025-01-19 15:56:27.286] [console] [warning] AddExternalConstraints from ::_::drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8abe80 drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8abe80
[2025-01-19 15:56:27.286] [console] [warning] AddExternalConstraints from ::_::drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8c1600 drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8c1600
[2025-01-19 15:56:27.286] [console] [warning] AddExternalConstraints from ::_ drake/systems/Diagram@000064afcd8c11d0
[2025-01-19 15:56:27.286] [console] [warning] AddExternalConstraints from ::_::drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8abe80 drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8abe80
[2025-01-19 15:56:27.286] [console] [warning] AddExternalConstraints from ::_::drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8c1600 drake/systems/(anonymous)/ConstraintTestSystem@000064afcd8c1600
[2025-01-19 15:56:27.286] [console] [warning] AddExternalConstraints from ::_ drake/systems/Diagram@000064afcd8c11d0
[       OK ] DiagramConstraintTest.SystemConstraintsTest (0 ms)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: medium release notes: fix This pull request contains fixes (no new features)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants