-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
[framework] Fix diagram scalar conversion losing external constraints #22490
Conversation
50811c1
to
eb193c4
Compare
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.
eb193c4
to
cf2c70f
Compare
+@sherm1 for feature review, please. |
There was a problem hiding this 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?
There was a problem hiding this 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.
There was a problem hiding this 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)
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