Drake
context.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <utility>
5 
6 #include "drake/common/drake_copyable.h"
7 #include "drake/common/drake_optional.h"
8 #include "drake/common/drake_throw.h"
9 #include "drake/systems/framework/input_port_evaluator_interface.h"
10 #include "drake/systems/framework/input_port_value.h"
11 #include "drake/systems/framework/parameters.h"
12 #include "drake/systems/framework/state.h"
13 #include "drake/systems/framework/value.h"
14 
15 namespace drake {
16 namespace systems {
17 
18 /// Contains information about the independent variable including time and
19 /// step number.
20 // TODO(sherm1) Add step information.
21 template <typename T>
22 struct StepInfo {
23  /// The time, in seconds. For typical T implementations based on
24  /// doubles, time resolution will gradually degrade as time increases.
25  // TODO(sherm1): Consider whether this is sufficiently robust.
26  T time_sec{0.0};
27 };
28 
29 /// Context is an abstract base class template that represents all
30 /// the inputs to a System: time, state, and input vectors. The framework
31 /// provides two concrete subclasses of Context: LeafContext (for
32 /// leaf Systems) and DiagramContext (for composite Systems). Users are
33 /// discouraged from creating additional subclasses.
34 ///
35 /// @tparam T The mathematical type of the context, which must be a valid Eigen
36 /// scalar.
37 template <typename T>
38 class Context {
39  public:
41 
42  Context() = default;
43  virtual ~Context() = default;
44 
45  // =========================================================================
46  // Accessors and Mutators for Time.
47 
48  /// Returns the current time in seconds.
49  const T& get_time() const { return get_step_info().time_sec; }
50 
51  /// Set the current time in seconds.
52  virtual void set_time(const T& time_sec) {
53  get_mutable_step_info()->time_sec = time_sec;
54  }
55 
56  // =========================================================================
57  // Accessors and Mutators for State.
58 
59  virtual const State<T>& get_state() const = 0;
60  virtual State<T>& get_mutable_state() = 0;
61 
62  /// Returns true if the Context has no state.
63  bool is_stateless() const {
64  const int nxc = get_continuous_state().size();
65  const int nxd = get_num_discrete_state_groups();
66  const int nxa = get_num_abstract_state_groups();
67  return nxc == 0 && nxd == 0 && nxa == 0;
68  }
69 
70  /// Returns true if the Context has continuous state, but no discrete or
71  /// abstract state.
73  const int nxc = get_continuous_state().size();
74  const int nxd = get_num_discrete_state_groups();
75  const int nxa = get_num_abstract_state_groups();
76  return nxc > 0 && nxd == 0 && nxa == 0;
77  }
78 
79  /// Returns true if the Context has discrete state, but no continuous or
80  /// abstract state.
81  bool has_only_discrete_state() const {
82  const int nxc = get_continuous_state().size();
83  const int nxd = get_num_discrete_state_groups();
84  const int nxa = get_num_abstract_state_groups();
85  return nxd > 0 && nxc == 0 && nxa == 0;
86  }
87 
88  /// Returns the total dimension of all of the basic vector states (as if they
89  /// were muxed).
90  /// @throws std::runtime_error if the system contains any abstract state.
91  int get_num_total_states() const {
92  DRAKE_THROW_UNLESS(get_num_abstract_state_groups() == 0);
93  int count = get_continuous_state().size();
94  for (int i = 0; i < get_num_discrete_state_groups(); i++)
95  count += get_discrete_state(i).size();
96  return count;
97  }
98 
99  /// Sets the continuous state to @p xc, deleting whatever was there before.
100  void set_continuous_state(std::unique_ptr<ContinuousState<T>> xc) {
101  get_mutable_state().set_continuous_state(std::move(xc));
102  }
103 
104  /// Returns a mutable reference to the continuous component of the state,
105  /// which may be of size zero.
107  return get_mutable_state().get_mutable_continuous_state();
108  }
109 
110  /// Returns a mutable reference to the continuous state vector, devoid
111  /// of second-order structure. The vector may be of size zero.
113  return get_mutable_continuous_state().get_mutable_vector();
114  }
115 
116  /// Returns a const reference to the continuous component of the state,
117  /// which may be of size zero.
119  return get_state().get_continuous_state();
120  }
121 
122  /// Returns a reference to the continuous state vector, devoid of second-order
123  /// structure. The vector may be of size zero.
125  return get_continuous_state().get_vector();
126  }
127 
128  /// Returns the number of elements in the discrete state.
130  return get_state().get_discrete_state().num_groups();
131  }
132 
134  return get_state().get_discrete_state();
135  }
136 
137  /// Returns a reference to the discrete state vector. The vector may be of
138  /// size zero.
140  return get_discrete_state().get_vector();
141  }
142 
143  /// Returns a mutable pointer to the discrete component of the state,
144  /// which may be of size zero.
146  return get_mutable_state().get_mutable_discrete_state();
147  }
148 
149  /// Returns a mutable pointer to element @p index of the discrete state.
150  /// Asserts if @p index doesn't exist.
152  DiscreteValues<T>& xd = get_mutable_discrete_state();
153  return xd.get_mutable_vector(index);
154  }
155 
156  /// Sets the discrete state to @p xd, deleting whatever was there before.
157  void set_discrete_state(std::unique_ptr<DiscreteValues<T>> xd) {
158  get_mutable_state().set_discrete_state(std::move(xd));
159  }
160 
161  /// Returns a const pointer to the discrete component of the
162  /// state at @p index. Asserts if @p index doesn't exist.
163  const BasicVector<T>& get_discrete_state(int index) const {
164  const DiscreteValues<T>& xd = get_state().get_discrete_state();
165  return xd.get_vector(index);
166  }
167 
168  /// Returns the number of elements in the abstract state.
170  return get_state().get_abstract_state().size();
171  }
172 
173  /// Returns a const reference to the abstract component of the state, which
174  /// may be of size zero.
176  return get_state().get_abstract_state();
177  }
178 
179  /// Returns a mutable reference to the abstract component of the state,
180  /// which may be of size zero.
182  return get_mutable_state().get_mutable_abstract_state();
183  }
184 
185  /// Returns a mutable pointer to element @p index of the abstract state.
186  /// Asserts if @p index doesn't exist.
187  template <typename U>
189  AbstractValues& xa = get_mutable_abstract_state();
190  return xa.get_mutable_value(index).GetMutableValue<U>();
191  }
192 
193  /// Sets the abstract state to @p xa, deleting whatever was there before.
194  void set_abstract_state(std::unique_ptr<AbstractValues> xa) {
195  get_mutable_state().set_abstract_state(std::move(xa));
196  }
197 
198  /// Returns a const reference to the abstract component of the
199  /// state at @p index. Asserts if @p index doesn't exist.
200  template <typename U>
201  const U& get_abstract_state(int index) const {
202  const AbstractValues& xa = get_state().get_abstract_state();
203  return xa.get_value(index).GetValue<U>();
204  }
205 
206  // =========================================================================
207  // Accessors and Mutators for Input.
208 
209  /// Returns the number of input ports.
210  virtual int get_num_input_ports() const = 0;
211 
212  /// Connects the input port at @p index to a FreestandingInputPortValue with
213  /// the given abstract @p value. Aborts if @p index is out of range.
214  /// Returns a reference to the allocated FreestandingInputPortValue. The
215  /// reference will remain valid until this input port's value source is
216  /// replaced or the %Context is destroyed. You may use that reference to
217  /// modify the input port's value using the appropriate
218  /// FreestandingInputPortValue method, which will ensure that invalidation
219  /// notifications are delivered.
221  int index, std::unique_ptr<AbstractValue> value) {
222  auto free_value_ptr =
223  std::make_unique<FreestandingInputPortValue>(std::move(value));
224  FreestandingInputPortValue& free_value = *free_value_ptr;
225  SetInputPortValue(index, std::move(free_value_ptr));
226  return free_value;
227  }
228 
229  /// Connects the input port at @p index to a FreestandingInputPortValue with
230  /// the given vector @p vec. Otherwise same as above method.
232  int index, std::unique_ptr<BasicVector<T>> vec) {
233  return FixInputPort(
234  index, std::make_unique<Value<BasicVector<T>>>(std::move(vec)));
235  }
236 
237  /// Same as above method but starts with an Eigen vector whose contents are
238  /// used to initialize a BasicVector in the FreestandingInputPortValue.
240  int index, const Eigen::Ref<const VectorX<T>>& data) {
241  auto vec = std::make_unique<BasicVector<T>>(data);
242  return FixInputPort(index, std::move(vec));
243  }
244 
245  /// Evaluates and returns the value of the input port identified by
246  /// @p descriptor, using the given @p evaluator, which should be the Diagram
247  /// containing the System that allocated this Context. The evaluation will be
248  /// performed in this Context's parent. It is a recursive operation that may
249  /// invoke long chains of evaluation through all the Systems that are
250  /// prerequisites to the specified port.
251  ///
252  /// Returns nullptr if the port is not connected to a value source. Aborts if
253  /// the port does not exist.
254  ///
255  /// This is a framework implementation detail. User code should not call it.
258  const InputPortDescriptor<T>& descriptor) const {
259  const InputPortValue* port_value =
260  GetInputPortValue(descriptor.get_index());
261  if (port_value == nullptr) return nullptr;
262  if (port_value->requires_evaluation()) {
263  DRAKE_DEMAND(evaluator != nullptr);
264  evaluator->EvaluateSubsystemInputPort(parent_, descriptor);
265  }
266  return port_value;
267  }
268 
269  /// Evaluates and returns the vector value of the input port with the given
270  /// @p descriptor. This is a recursive operation that may invoke long chains
271  /// of evaluation through all the Systems that are prerequisite to the
272  /// specified port.
273  ///
274  /// Returns nullptr if the port is not connected.
275  /// Throws std::bad_cast if the port is not vector-valued.
276  /// Aborts if the port does not exist.
277  ///
278  /// This is a framework implementation detail. User code should not call it;
279  /// consider calling System::EvalVectorInput instead.
282  const InputPortDescriptor<T>& descriptor) const {
283  const InputPortValue* port_value = EvalInputPort(evaluator, descriptor);
284  if (port_value == nullptr) return nullptr;
285  return port_value->template get_vector_data<T>();
286  }
287 
288  /// Evaluates and returns the abstract value of the input port with the given
289  /// @p descriptor. This is a recursive operation that may invoke long chains
290  /// of evaluation through all the Systems that are prerequisite to the
291  /// specified port.
292  ///
293  /// Returns nullptr if the port is not connected.
294  /// Aborts if the port does not exist.
295  ///
296  /// This is a framework implementation detail. User code should not call it;
297  /// consider calling System::EvalAbstractInput instead.
300  const InputPortDescriptor<T>& descriptor) const {
301  const InputPortValue* port_value = EvalInputPort(evaluator, descriptor);
302  if (port_value == nullptr) return nullptr;
303  return port_value->get_abstract_data();
304  }
305 
306  /// Evaluates and returns the data of the input port at @p index.
307  /// This is a recursive operation that may invoke long chains of evaluation
308  /// through all the Systems that are prerequisite to the specified port.
309  ///
310  /// Returns nullptr if the port is not connected.
311  /// Throws std::bad_cast if the port does not have type V.
312  /// Aborts if the port does not exist.
313  ///
314  /// This is a framework implementation detail. User code should not call it;
315  /// consider calling System::EvalInputValue instead.
316  ///
317  /// @tparam V The type of data expected.
318  template <typename V>
319  const V* EvalInputValue(
321  const InputPortDescriptor<T>& descriptor) const {
322  const AbstractValue* value = EvalAbstractInput(evaluator, descriptor);
323  if (value == nullptr) return nullptr;
324  return &(value->GetValue<V>());
325  }
326 
327  // =========================================================================
328  // Accessors and Mutators for Parameters.
329 
330  virtual const Parameters<T>& get_parameters() const = 0;
331  virtual Parameters<T>& get_mutable_parameters() = 0;
332 
333  /// Returns the number of vector-valued parameters.
335  return get_parameters().num_numeric_parameters();
336  }
337 
338  /// Returns a const pointer to the vector-valued parameter at @p index.
339  /// Asserts if @p index doesn't exist.
340  const BasicVector<T>& get_numeric_parameter(int index) const {
341  return get_parameters().get_numeric_parameter(index);
342  }
343 
344  /// Returns a mutable pointer to element @p index of the vector-valued
345  /// parameters. Asserts if @p index doesn't exist.
347  return get_mutable_parameters().get_mutable_numeric_parameter(index);
348  }
349 
350  /// Returns the number of abstract-valued parameters.
352  return get_parameters().num_abstract_parameters();
353  }
354 
355  /// Returns a const reference to the abstract-valued parameter at @p index.
356  /// Asserts if @p index doesn't exist.
357  const AbstractValue& get_abstract_parameter(int index) const {
358  return get_parameters().get_abstract_parameter(index);
359  }
360 
361  /// Returns a mutable reference to element @p index of the abstract-valued
362  /// parameters. Asserts if @p index doesn't exist.
364  return get_mutable_parameters().get_mutable_abstract_parameter(index);
365  }
366 
367  // =========================================================================
368  // Accessors and Mutators for Accuracy.
369 
370  /// Records the user's requested accuracy. If no accuracy is requested,
371  /// computations are free to choose suitable defaults, or to refuse to
372  /// proceed without an explicit accuracy setting.
373  ///
374  /// Requested accuracy is stored in the %Context for two reasons:
375  /// - It permits all computations performed over a System to see the _same_
376  /// accuracy request since accuracy is stored in one shared place, and
377  /// - it allows us to invalidate accuracy-dependent cached computations when
378  /// the requested accuracy has changed.
379  ///
380  /// The accuracy of a complete simulation or other numerical study depends on
381  /// the accuracy of _all_ contributing computations, so it is important that
382  /// each computation is done in accordance with the overall requested
383  /// accuracy. Some examples of where this is needed:
384  /// - Error-controlled numerical integrators use the accuracy setting to
385  /// decide what step sizes to take.
386  /// - The Simulator employs a numerical integrator, but also uses accuracy to
387  /// decide how precisely to isolate witness function zero crossings.
388  /// - Iterative calculations reported as results or cached internally depend
389  /// on accuracy to decide how strictly to converge the results. Examples of
390  /// these are: constraint projection, calculation of distances between
391  /// smooth shapes, and deformation calculations for soft contact.
392  ///
393  /// The common thread among these examples is that they all share the
394  /// same %Context, so by keeping accuracy here it can be used effectively to
395  /// control all accuracy-dependent computations.
396  // TODO(edrumwri) Invalidate all cached accuracy-dependent computations, and
397  // propagate accuracy to all subcontexts in a diagram context.
398  void set_accuracy(const optional<double>& accuracy) { accuracy_ = accuracy; }
399 
400  /// Returns the accuracy setting (if any).
401  /// @see set_accuracy() for details.
402  const optional<double>& get_accuracy() const { return accuracy_; }
403 
404  // =========================================================================
405  // Miscellaneous Public Methods
406 
407  /// Returns a deep copy of this Context. The clone's input ports will
408  /// hold deep copies of the data that appears on this context's input ports
409  /// at the time the clone is created.
410  std::unique_ptr<Context<T>> Clone() const {
411  return std::unique_ptr<Context<T>>(DoClone());
412  }
413 
414  /// Returns a deep copy of this Context's State.
415  std::unique_ptr<State<T>> CloneState() const {
416  return std::unique_ptr<State<T>>(DoCloneState());
417  }
418 
419  /// Initializes this context's time, state, and parameters from the real
420  /// values in @p source, regardless of this context's scalar type.
421  /// Requires a constructor T(double).
423  set_time(T(source.get_time()));
424  set_accuracy(source.get_accuracy());
425  get_mutable_state().SetFrom(source.get_state());
426  get_mutable_parameters().SetFrom(source.get_parameters());
427  }
428 
429  /// Declares that @p parent is the context of the enclosing Diagram. The
430  /// enclosing Diagram context is needed to evaluate inputs recursively.
431  /// Aborts if the parent has already been set to something else.
432  ///
433  /// This is a dangerous implementation detail. Conceptually, a Context
434  /// ought to be completely ignorant of its parent Context. However, we
435  /// need this pointer so that we can cause our inputs to be evaluated in
436  /// EvalInputPort. See https://github.com/RobotLocomotion/drake/pull/3455.
437  void set_parent(const Context<T>* parent) {
438  DRAKE_DEMAND(parent_ == nullptr || parent_ == parent);
439  parent_ = parent;
440  }
441 
442  /// Throws an exception unless the given @p descriptor matches the inputs
443  /// actually connected to this context in shape.
444  /// Supports any scalar type of `descriptor`, but expects T by default.
445  ///
446  /// @tparam T1 the scalar type of the InputPortDescriptor to check.
447  template<typename T1 = T>
448  void VerifyInputPort(const InputPortDescriptor<T1>& descriptor) const {
449  const int i = descriptor.get_index();
450  const InputPortValue* port_value = GetInputPortValue(i);
451  // If the port isn't connected, we don't have anything else to check.
452  if (port_value == nullptr) { return; }
453  // TODO(david-german-tri, sherm1): Consider checking sampling here.
454 
455  // In the vector-valued case, check the size.
456  if (descriptor.get_data_type() == kVectorValued) {
457  const BasicVector<T>* input_vector =
458  port_value->template get_vector_data<T>();
459  DRAKE_THROW_UNLESS(input_vector != nullptr);
460  DRAKE_THROW_UNLESS(input_vector->size() == descriptor.size());
461  }
462  // In the abstract-valued case, there is nothing else to check.
463  }
464 
465  protected:
466  /// Contains the return-type-covariant implementation of Clone().
467  virtual Context<T>* DoClone() const = 0;
468 
469  /// Contains the return-type-covariant implementation of CloneState().
470  virtual State<T>* DoCloneState() const = 0;
471 
472  /// Returns a const reference to current time and step information.
473  const StepInfo<T>& get_step_info() const { return step_info_; }
474 
475  /// Provides writable access to time and step information, with the side
476  /// effect of invaliding any computation that is dependent on them.
477  /// TODO(david-german-tri) Invalidate all cached time- and step-dependent
478  /// computations.
479  StepInfo<T>* get_mutable_step_info() { return &step_info_; }
480 
481  /// Returns the InputPortValue at the given @p index, which may be nullptr if
482  /// it has never been set with SetInputPortValue().
483  /// Asserts if @p index is out of range.
484  virtual const InputPortValue* GetInputPortValue(int index) const = 0;
485 
486  /// Allows derived classes to invoke the protected method on subcontexts.
487  static const InputPortValue* GetInputPortValue(const Context<T>& context,
488  int index) {
489  return context.GetInputPortValue(index);
490  }
491 
492  /// Connects the input port at @p index to the value source @p port_value.
493  /// Disconnects whatever value source was previously there, and de-registers
494  /// it from the output port on which it depends. In some Context
495  /// implementations, may require a recursive search through a tree of
496  /// subcontexts. Implementations must abort if @p index is out of range.
497  virtual void SetInputPortValue(
498  int index, std::unique_ptr<InputPortValue> port_value) = 0;
499 
500  /// Allows derived classes to invoke the protected method on subcontexts.
501  static void SetInputPortValue(Context<T>* context, int index,
502  std::unique_ptr<InputPortValue> port_value) {
503  context->SetInputPortValue(index, std::move(port_value));
504  }
505 
506  private:
507  // Current time and step information.
508  StepInfo<T> step_info_;
509 
510  // Accuracy setting.
511  optional<double> accuracy_;
512 
513  // The context of the enclosing Diagram, used in EvalInputPort.
514  // This pointer MUST be treated as a black box. If you call any substantive
515  // methods on it, you are probably making a mistake.
516  const Context<T>* parent_ = nullptr;
517 };
518 
519 } // namespace systems
520 } // namespace drake
const InputPortValue * EvalInputPort(const detail::InputPortEvaluatorInterface< T > *evaluator, const InputPortDescriptor< T > &descriptor) const
Evaluates and returns the value of the input port identified by descriptor, using the given evaluator...
Definition: context.h:256
int get_num_abstract_state_groups() const
Returns the number of elements in the abstract state.
Definition: context.h:169
void set_parent(const Context< T > *parent)
Declares that parent is the context of the enclosing Diagram.
Definition: context.h:437
AbstractValues is a container for non-numerical state and parameters.
Definition: abstract_values.h:18
static const InputPortValue * GetInputPortValue(const Context< T > &context, int index)
Allows derived classes to invoke the protected method on subcontexts.
Definition: context.h:487
const AbstractValue & get_abstract_parameter(int index) const
Returns a const reference to the abstract-valued parameter at index.
Definition: context.h:357
DiscreteValues< T > & get_mutable_discrete_state()
Returns a mutable pointer to the discrete component of the state, which may be of size zero...
Definition: context.h:145
int size() const override
Returns the number of elements in the vector.
Definition: basic_vector.h:62
DiscreteValues is a container for numerical but non-continuous state and parameters.
Definition: discrete_values.h:32
double T
Definition: lifetime_test_util_py.cc:14
const AbstractValues & get_abstract_state() const
Returns a const reference to the abstract component of the state, which may be of size zero...
Definition: context.h:175
std::unique_ptr< Context< T > > Clone() const
Returns a deep copy of this Context.
Definition: context.h:410
virtual void SetInputPortValue(int index, std::unique_ptr< InputPortValue > port_value)=0
Connects the input port at index to the value source port_value.
const BasicVector< T > & get_numeric_parameter(int index) const
Returns a const pointer to the vector-valued parameter at index.
Definition: context.h:340
bool has_only_discrete_state() const
Returns true if the Context has discrete state, but no continuous or abstract state.
Definition: context.h:81
virtual const State< T > & get_state() const =0
Definition: automotive_demo.cc:89
const BasicVector< T > & get_discrete_state(int index) const
Returns a const pointer to the discrete component of the state at index.
Definition: context.h:163
const AbstractValue * EvalAbstractInput(const detail::InputPortEvaluatorInterface< T > *evaluator, const InputPortDescriptor< T > &descriptor) const
Evaluates and returns the abstract value of the input port with the given descriptor.
Definition: context.h:298
void set_discrete_state(std::unique_ptr< DiscreteValues< T >> xd)
Sets the discrete state to xd, deleting whatever was there before.
Definition: context.h:157
int num_abstract_parameters() const
Returns the number of abstract-valued parameters.
Definition: context.h:351
const VectorBase< T > & get_continuous_state_vector() const
Returns a reference to the continuous state vector, devoid of second-order structure.
Definition: context.h:124
Context is an abstract base class template that represents all the inputs to a System: time...
Definition: context.h:38
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:46
void set_continuous_state(std::unique_ptr< ContinuousState< T >> xc)
Sets the continuous state to xc, deleting whatever was there before.
Definition: context.h:100
VectorBase is an abstract base class that real-valued signals between Systems and real-valued System ...
Definition: vector_base.h:27
void set_accuracy(const optional< double > &accuracy)
Records the user&#39;s requested accuracy.
Definition: context.h:398
#define DRAKE_THROW_UNLESS(condition)
Evaluates condition and iff the value is false will throw an exception with a message showing at leas...
Definition: drake_throw.h:23
StepInfo< T > * get_mutable_step_info()
Provides writable access to time and step information, with the side effect of invaliding any computa...
Definition: context.h:479
virtual const Parameters< T > & get_parameters() const =0
InputPortDescriptor is a notation for specifying the kind of input a System accepts, on a given port.
Definition: input_port_descriptor.h:21
stx::optional< T > optional
Definition: drake_optional.h:20
FreestandingInputPortValue & FixInputPort(int index, std::unique_ptr< AbstractValue > value)
Connects the input port at index to a FreestandingInputPortValue with the given abstract value...
Definition: context.h:220
const T & GetValue() const
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:125
const BasicVector< T > & get_discrete_state_vector() const
Returns a reference to the discrete state vector.
Definition: context.h:139
U & get_mutable_abstract_state(int index)
Returns a mutable pointer to element index of the abstract state.
Definition: context.h:188
T time_sec
The time, in seconds.
Definition: context.h:26
BasicVector< T > & get_mutable_vector()
Returns a mutable reference to the BasicVector containing the values for the only group...
Definition: discrete_values.h:111
const V * EvalInputValue(const detail::InputPortEvaluatorInterface< T > *evaluator, const InputPortDescriptor< T > &descriptor) const
Evaluates and returns the data of the input port at index.
Definition: context.h:319
Definition: framework_common.h:25
void SetTimeStateAndParametersFrom(const Context< double > &source)
Initializes this context&#39;s time, state, and parameters from the real values in source, regardless of this context&#39;s scalar type.
Definition: context.h:422
virtual void set_time(const T &time_sec)
Set the current time in seconds.
Definition: context.h:52
FreestandingInputPortValue & FixInputPort(int index, const Eigen::Ref< const VectorX< T >> &data)
Same as above method but starts with an Eigen vector whose contents are used to initialize a BasicVec...
Definition: context.h:239
int value
Definition: copyable_unique_ptr_test.cc:61
static void SetInputPortValue(Context< T > *context, int index, std::unique_ptr< InputPortValue > port_value)
Allows derived classes to invoke the protected method on subcontexts.
Definition: context.h:501
const U & get_abstract_state(int index) const
Returns a const reference to the abstract component of the state at index.
Definition: context.h:201
InputPortValue identifies the value source for a single System input port.
Definition: input_port_value.h:28
#define DRAKE_DEMAND(condition)
Evaluates condition and iff the value is false will trigger an assertion failure with a message showi...
Definition: drake_assert.h:45
A FreestandingInputPortValue encapsulates a vector or abstract value for use as an internal value sou...
Definition: input_port_value.h:117
int get_num_discrete_state_groups() const
Returns the number of elements in the discrete state.
Definition: context.h:129
bool has_only_continuous_state() const
Returns true if the Context has continuous state, but no discrete or abstract state.
Definition: context.h:72
State is a container for all the data comprising the complete state of a particular System at a parti...
Definition: state.h:27
int num_numeric_parameters() const
Returns the number of vector-valued parameters.
Definition: context.h:334
const StepInfo< T > & get_step_info() const
Returns a const reference to current time and step information.
Definition: context.h:473
const T & get_time() const
Returns the current time in seconds.
Definition: context.h:49
virtual void EvaluateSubsystemInputPort(const Context< T > *context, const InputPortDescriptor< T > &id) const =0
Evaluates the input port with the given id in the given context.
const AbstractValue * get_abstract_data() const
Returns the data on this port, which must be connected to a value source.
Definition: input_port_value.h:43
VectorBase< T > & get_mutable_continuous_state_vector()
Returns a mutable reference to the continuous state vector, devoid of second-order structure...
Definition: context.h:112
BasicVector is a semantics-free wrapper around an Eigen vector that satisfies VectorBase.
Definition: basic_vector.h:25
AbstractValue & get_mutable_abstract_parameter(int index)
Returns a mutable reference to element index of the abstract-valued parameters.
Definition: context.h:363
A fully type-erased container class.
Definition: value.h:99
void VerifyInputPort(const InputPortDescriptor< T1 > &descriptor) const
Throws an exception unless the given descriptor matches the inputs actually connected to this context...
Definition: context.h:448
AbstractValues & get_mutable_abstract_state()
Returns a mutable reference to the abstract component of the state, which may be of size zero...
Definition: context.h:181
const AbstractValue & get_value(int index) const
Returns the element of AbstractValues at the given index, or aborts if the index is out-of-bounds...
Definition: abstract_values.cc:33
const BasicVector< T > * EvalVectorInput(const detail::InputPortEvaluatorInterface< T > *evaluator, const InputPortDescriptor< T > &descriptor) const
Evaluates and returns the vector value of the input port with the given descriptor.
Definition: context.h:280
BasicVector< T > & get_mutable_numeric_parameter(int index)
Returns a mutable pointer to element index of the vector-valued parameters.
Definition: context.h:346
ContinuousState< T > & get_mutable_continuous_state()
Returns a mutable reference to the continuous component of the state, which may be of size zero...
Definition: context.h:106
Parameters is a container for variables that parameterize a System so that it can represent a family ...
Definition: parameters.h:26
int get_num_total_states() const
Returns the total dimension of all of the basic vector states (as if they were muxed).
Definition: context.h:91
BasicVector< T > & get_mutable_discrete_state(int index)
Returns a mutable pointer to element index of the discrete state.
Definition: context.h:151
const optional< double > & get_accuracy() const
Returns the accuracy setting (if any).
Definition: context.h:402
Contains information about the independent variable including time and step number.
Definition: context.h:22
virtual bool requires_evaluation() const =0
Returns true if this InputPortValue is not in control of its own data.
InputPortEvaluatorInterface is implemented by classes that are able to evaluate the OutputPortValue c...
Definition: input_port_evaluator_interface.h:26
int size() const
Definition: input_port_descriptor.h:72
ContinuousState is a container for all the continuous state variables xc.
Definition: continuous_state.h:44
int get_index() const
Definition: input_port_descriptor.h:70
const DiscreteValues< T > & get_discrete_state() const
Definition: context.h:133
A container class for an arbitrary type T.
Definition: value.h:222
AbstractValue & get_mutable_value(int index)
Returns the element of AbstractValues at the given index, or aborts if the index is out-of-bounds...
Definition: abstract_values.cc:39
#define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname)
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:33
const BasicVector< T > & get_vector() const
Returns a const reference to the BasicVector containing the values for the only group.
Definition: discrete_values.h:104
virtual const InputPortValue * GetInputPortValue(int index) const =0
Returns the InputPortValue at the given index, which may be nullptr if it has never been set with Set...
const ContinuousState< T > & get_continuous_state() const
Returns a const reference to the continuous component of the state, which may be of size zero...
Definition: context.h:118
std::unique_ptr< State< T > > CloneState() const
Returns a deep copy of this Context&#39;s State.
Definition: context.h:415
bool is_stateless() const
Returns true if the Context has no state.
Definition: context.h:63
FreestandingInputPortValue & FixInputPort(int index, std::unique_ptr< BasicVector< T >> vec)
Connects the input port at index to a FreestandingInputPortValue with the given vector vec...
Definition: context.h:231
PortDataType get_data_type() const
Definition: input_port_descriptor.h:71
void set_abstract_state(std::unique_ptr< AbstractValues > xa)
Sets the abstract state to xa, deleting whatever was there before.
Definition: context.h:194
int data
Definition: value_test.cc:19
T & GetMutableValue()
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:142