Drake
Context< T > Class Template Referenceabstract

Context is an abstract base class template that represents all the inputs to a System: time, state, and input vectors. More...

#include <systems/framework/context.h>

Inheritance diagram for Context< T >:
[legend]

## Public Member Functions

Context ()=default

virtual ~Context ()=default

const Tget_time () const
Returns the current time in seconds. More...

virtual void set_time (const T &time_sec)
Set the current time in seconds. More...

virtual const State< T > & get_state () const =0

virtual State< T > & get_mutable_state ()=0

bool is_stateless () const
Returns true if the Context has no state. More...

bool has_only_continuous_state () const
Returns true if the Context has continuous state, but no discrete or abstract state. More...

bool has_only_discrete_state () const
Returns true if the Context has discrete state, but no continuous or abstract state. More...

int get_num_total_states () const
Returns the total dimension of all of the basic vector states (as if they were muxed). More...

void set_continuous_state (std::unique_ptr< ContinuousState< T >> xc)
Sets the continuous state to xc, deleting whatever was there before. More...

ContinuousState< T > & get_mutable_continuous_state ()
Returns a mutable reference to the continuous component of the state, which may be of size zero. More...

VectorBase< T > & get_mutable_continuous_state_vector ()
Returns a mutable reference to the continuous state vector, devoid of second-order structure. More...

const ContinuousState< T > & get_continuous_state () const
Returns a const reference to the continuous component of the state, which may be of size zero. More...

const VectorBase< T > & get_continuous_state_vector () const
Returns a reference to the continuous state vector, devoid of second-order structure. More...

int get_num_discrete_state_groups () const
Returns the number of elements in the discrete state. More...

const DiscreteValues< T > & get_discrete_state () const

const BasicVector< T > & get_discrete_state_vector () const
Returns a reference to the discrete state vector. More...

DiscreteValues< T > & get_mutable_discrete_state ()
Returns a mutable pointer to the discrete component of the state, which may be of size zero. More...

BasicVector< T > & get_mutable_discrete_state (int index)
Returns a mutable pointer to element index of the discrete state. More...

void set_discrete_state (std::unique_ptr< DiscreteValues< T >> xd)
Sets the discrete state to xd, deleting whatever was there before. More...

const BasicVector< T > & get_discrete_state (int index) const
Returns a const pointer to the discrete component of the state at index. More...

int get_num_abstract_state_groups () const
Returns the number of elements in the abstract state. More...

const AbstractValuesget_abstract_state () const
Returns a const reference to the abstract component of the state, which may be of size zero. More...

AbstractValuesget_mutable_abstract_state ()
Returns a mutable reference to the abstract component of the state, which may be of size zero. More...

template<typename U >
U & get_mutable_abstract_state (int index)
Returns a mutable pointer to element index of the abstract state. More...

void set_abstract_state (std::unique_ptr< AbstractValues > xa)
Sets the abstract state to xa, deleting whatever was there before. More...

template<typename U >
const U & get_abstract_state (int index) const
Returns a const reference to the abstract component of the state at index. More...

virtual int get_num_input_ports () const =0
Returns the number of input ports. More...

FreestandingInputPortValueFixInputPort (int index, std::unique_ptr< AbstractValue > value)
Connects the input port at index to a FreestandingInputPortValue with the given abstract value. More...

FreestandingInputPortValueFixInputPort (int index, std::unique_ptr< BasicVector< T >> vec)
Connects the input port at index to a FreestandingInputPortValue with the given vector vec. More...

FreestandingInputPortValueFixInputPort (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 BasicVector in the FreestandingInputPortValue. More...

const InputPortValueEvalInputPort (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, which should be the Diagram containing the System that allocated this Context. More...

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. More...

const AbstractValueEvalAbstractInput (const detail::InputPortEvaluatorInterface< T > *evaluator, const InputPortDescriptor< T > &descriptor) const
Evaluates and returns the abstract value of the input port with the given descriptor. More...

template<typename V >
const V * EvalInputValue (const detail::InputPortEvaluatorInterface< T > *evaluator, const InputPortDescriptor< T > &descriptor) const
Evaluates and returns the data of the input port at index. More...

virtual const Parameters< T > & get_parameters () const =0

virtual Parameters< T > & get_mutable_parameters ()=0

int num_numeric_parameters () const
Returns the number of vector-valued parameters. More...

const BasicVector< T > & get_numeric_parameter (int index) const
Returns a const pointer to the vector-valued parameter at index. More...

BasicVector< T > & get_mutable_numeric_parameter (int index)
Returns a mutable pointer to element index of the vector-valued parameters. More...

int num_abstract_parameters () const
Returns the number of abstract-valued parameters. More...

const AbstractValueget_abstract_parameter (int index) const
Returns a const reference to the abstract-valued parameter at index. More...

AbstractValueget_mutable_abstract_parameter (int index)
Returns a mutable reference to element index of the abstract-valued parameters. More...

void set_accuracy (const optional< double > &accuracy)
Records the user's requested accuracy. More...

const optional< double > & get_accuracy () const
Returns the accuracy setting (if any). More...

std::unique_ptr< Context< T > > Clone () const
Returns a deep copy of this Context. More...

std::unique_ptr< State< T > > CloneState () const
Returns a deep copy of this Context's State. More...

void SetTimeStateAndParametersFrom (const Context< double > &source)
Initializes this context's time, state, and parameters from the real values in source, regardless of this context's scalar type. More...

void set_parent (const Context< T > *parent)
Declares that parent is the context of the enclosing Diagram. More...

template<typename T1 = T>
void VerifyInputPort (const InputPortDescriptor< T1 > &descriptor) const
Throws an exception unless the given descriptor matches the inputs actually connected to this context in shape. More...

Does not allow copy, move, or assignment
Context (const Context &)=delete

Contextoperator= (const Context &)=delete

Context (Context &&)=delete

Contextoperator= (Context &&)=delete

## Protected Member Functions

virtual Context< T > * DoClone () const =0
Contains the return-type-covariant implementation of Clone(). More...

virtual State< T > * DoCloneState () const =0
Contains the return-type-covariant implementation of CloneState(). More...

const StepInfo< T > & get_step_info () const
Returns a const reference to current time and step information. More...

StepInfo< T > * get_mutable_step_info ()
Provides writable access to time and step information, with the side effect of invaliding any computation that is dependent on them. More...

virtual const InputPortValueGetInputPortValue (int index) const =0
Returns the InputPortValue at the given index, which may be nullptr if it has never been set with SetInputPortValue(). More...

virtual void SetInputPortValue (int index, std::unique_ptr< InputPortValue > port_value)=0
Connects the input port at index to the value source port_value. More...

## Static Protected Member Functions

static const InputPortValueGetInputPortValue (const Context< T > &context, int index)
Allows derived classes to invoke the protected method on subcontexts. More...

static void SetInputPortValue (Context< T > *context, int index, std::unique_ptr< InputPortValue > port_value)
Allows derived classes to invoke the protected method on subcontexts. More...

## Detailed Description

### template<typename T> class drake::systems::Context< T >

Context is an abstract base class template that represents all the inputs to a System: time, state, and input vectors.

The framework provides two concrete subclasses of Context: LeafContext (for leaf Systems) and DiagramContext (for composite Systems). Users are discouraged from creating additional subclasses.

Template Parameters
 T The mathematical type of the context, which must be a valid Eigen scalar.

## Constructor & Destructor Documentation

 Context ( const Context< T > & )
delete
 Context ( Context< T > && )
delete
 Context ( )
default
 virtual ~Context ( )
virtualdefault

## Member Function Documentation

 std::unique_ptr > Clone ( ) const
inline

Returns a deep copy of this Context.

The clone's input ports will hold deep copies of the data that appears on this context's input ports at the time the clone is created.

Here is the caller graph for this function:

 std::unique_ptr > CloneState ( ) const
inline

Returns a deep copy of this Context's State.

Here is the caller graph for this function:

 virtual Context* DoClone ( ) const
protectedpure virtual

Contains the return-type-covariant implementation of Clone().

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

 virtual State* DoCloneState ( ) const
protectedpure virtual

Contains the return-type-covariant implementation of CloneState().

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

 const AbstractValue* EvalAbstractInput ( const detail::InputPortEvaluatorInterface< T > * evaluator, const InputPortDescriptor< T > & descriptor ) const
inline

Evaluates and returns the abstract value of the input port with the given descriptor.

This is a recursive operation that may invoke long chains of evaluation through all the Systems that are prerequisite to the specified port.

Returns nullptr if the port is not connected. Aborts if the port does not exist.

This is a framework implementation detail. User code should not call it; consider calling System::EvalAbstractInput instead.

Here is the caller graph for this function:

 const InputPortValue* EvalInputPort ( const detail::InputPortEvaluatorInterface< T > * evaluator, const InputPortDescriptor< T > & descriptor ) const
inline

Evaluates and returns the value of the input port identified by descriptor, using the given evaluator, which should be the Diagram containing the System that allocated this Context.

The evaluation will be performed in this Context's parent. It is a recursive operation that may invoke long chains of evaluation through all the Systems that are prerequisites to the specified port.

Returns nullptr if the port is not connected to a value source. Aborts if the port does not exist.

This is a framework implementation detail. User code should not call it.

Here is the caller graph for this function:

 const V* EvalInputValue ( const detail::InputPortEvaluatorInterface< T > * evaluator, const InputPortDescriptor< T > & descriptor ) const
inline

Evaluates and returns the data of the input port at index.

This is a recursive operation that may invoke long chains of evaluation through all the Systems that are prerequisite to the specified port.

Returns nullptr if the port is not connected. Throws std::bad_cast if the port does not have type V. Aborts if the port does not exist.

This is a framework implementation detail. User code should not call it; consider calling System::EvalInputValue instead.

Template Parameters
 V The type of data expected.
 const BasicVector* EvalVectorInput ( const detail::InputPortEvaluatorInterface< T > * evaluator, const InputPortDescriptor< T > & descriptor ) const
inline

Evaluates and returns the vector value of the input port with the given descriptor.

This is a recursive operation that may invoke long chains of evaluation through all the Systems that are prerequisite to the specified port.

Returns nullptr if the port is not connected. Throws std::bad_cast if the port is not vector-valued. Aborts if the port does not exist.

This is a framework implementation detail. User code should not call it; consider calling System::EvalVectorInput instead.

Here is the caller graph for this function:

 FreestandingInputPortValue& FixInputPort ( int index, std::unique_ptr< AbstractValue > value )
inline

Connects the input port at index to a FreestandingInputPortValue with the given abstract value.

Aborts if index is out of range. Returns a reference to the allocated FreestandingInputPortValue. The reference will remain valid until this input port's value source is replaced or the Context is destroyed. You may use that reference to modify the input port's value using the appropriate FreestandingInputPortValue method, which will ensure that invalidation notifications are delivered.

Here is the caller graph for this function:

 FreestandingInputPortValue& FixInputPort ( int index, std::unique_ptr< BasicVector< T >> vec )
inline

Connects the input port at index to a FreestandingInputPortValue with the given vector vec.

Otherwise same as above method.

 FreestandingInputPortValue& FixInputPort ( int index, const Eigen::Ref< const VectorX< T >> & data )
inline

Same as above method but starts with an Eigen vector whose contents are used to initialize a BasicVector in the FreestandingInputPortValue.

 const AbstractValue& get_abstract_parameter ( int index ) const
inline

Returns a const reference to the abstract-valued parameter at index.

Asserts if index doesn't exist.

Here is the caller graph for this function:

 const AbstractValues& get_abstract_state ( ) const
inline

Returns a const reference to the abstract component of the state, which may be of size zero.

Here is the caller graph for this function:

 const U& get_abstract_state ( int index ) const
inline

Returns a const reference to the abstract component of the state at index.

Asserts if index doesn't exist.

 const optional& get_accuracy ( ) const
inline

Returns the accuracy setting (if any).

See also
set_accuracy() for details.

Here is the caller graph for this function:

 const ContinuousState& get_continuous_state ( ) const
inline

Returns a const reference to the continuous component of the state, which may be of size zero.

Here is the caller graph for this function:

 const VectorBase& get_continuous_state_vector ( ) const
inline

Returns a reference to the continuous state vector, devoid of second-order structure.

The vector may be of size zero.

Here is the caller graph for this function:

 const DiscreteValues& get_discrete_state ( ) const
inline

Here is the caller graph for this function:

 const BasicVector& get_discrete_state ( int index ) const
inline

Returns a const pointer to the discrete component of the state at index.

Asserts if index doesn't exist.

 const BasicVector& get_discrete_state_vector ( ) const
inline

Returns a reference to the discrete state vector.

The vector may be of size zero.

Here is the caller graph for this function:

 AbstractValue& get_mutable_abstract_parameter ( int index )
inline

Returns a mutable reference to element index of the abstract-valued parameters.

Asserts if index doesn't exist.

 AbstractValues& get_mutable_abstract_state ( )
inline

Returns a mutable reference to the abstract component of the state, which may be of size zero.

 U& get_mutable_abstract_state ( int index )
inline

Returns a mutable pointer to element index of the abstract state.

Asserts if index doesn't exist.

 ContinuousState& get_mutable_continuous_state ( )
inline

Returns a mutable reference to the continuous component of the state, which may be of size zero.

Here is the caller graph for this function:

 VectorBase& get_mutable_continuous_state_vector ( )
inline

Returns a mutable reference to the continuous state vector, devoid of second-order structure.

The vector may be of size zero.

Here is the caller graph for this function:

 DiscreteValues& get_mutable_discrete_state ( )
inline

Returns a mutable pointer to the discrete component of the state, which may be of size zero.

Here is the caller graph for this function:

 BasicVector& get_mutable_discrete_state ( int index )
inline

Returns a mutable pointer to element index of the discrete state.

Asserts if index doesn't exist.

 BasicVector& get_mutable_numeric_parameter ( int index )
inline

Returns a mutable pointer to element index of the vector-valued parameters.

Asserts if index doesn't exist.

Here is the caller graph for this function:

 virtual Parameters& get_mutable_parameters ( )
pure virtual

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

Here is the caller graph for this function:

 virtual State& get_mutable_state ( )
pure virtual

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

Here is the caller graph for this function:

 StepInfo* get_mutable_step_info ( )
inlineprotected

Provides writable access to time and step information, with the side effect of invaliding any computation that is dependent on them.

TODO(david-german-tri) Invalidate all cached time- and step-dependent computations.

Here is the caller graph for this function:

 int get_num_abstract_state_groups ( ) const
inline

Returns the number of elements in the abstract state.

Here is the caller graph for this function:

 int get_num_discrete_state_groups ( ) const
inline

Returns the number of elements in the discrete state.

Here is the caller graph for this function:

 virtual int get_num_input_ports ( ) const
pure virtual

Returns the number of input ports.

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

Here is the caller graph for this function:

 int get_num_total_states ( ) const
inline

Returns the total dimension of all of the basic vector states (as if they were muxed).

Exceptions
 std::runtime_error if the system contains any abstract state.

Here is the caller graph for this function:

 const BasicVector& get_numeric_parameter ( int index ) const
inline

Returns a const pointer to the vector-valued parameter at index.

Asserts if index doesn't exist.

Here is the caller graph for this function:

 virtual const Parameters& get_parameters ( ) const
pure virtual

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

Here is the caller graph for this function:

 virtual const State& get_state ( ) const
pure virtual

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

Here is the caller graph for this function:

 const StepInfo& get_step_info ( ) const
inlineprotected

Returns a const reference to current time and step information.

Here is the caller graph for this function:

 const T& get_time ( ) const
inline

Returns the current time in seconds.

Here is the caller graph for this function:

 virtual const InputPortValue* GetInputPortValue ( int index ) const
protectedpure virtual

Returns the InputPortValue at the given index, which may be nullptr if it has never been set with SetInputPortValue().

Asserts if index is out of range.

Implemented in DiagramContext< T >, LeafContext< T >, and LeafContext< double >.

Here is the caller graph for this function:

 static const InputPortValue* GetInputPortValue ( const Context< T > & context, int index )
inlinestaticprotected

Allows derived classes to invoke the protected method on subcontexts.

 bool has_only_continuous_state ( ) const
inline

Returns true if the Context has continuous state, but no discrete or abstract state.

Here is the caller graph for this function:

 bool has_only_discrete_state ( ) const
inline

Returns true if the Context has discrete state, but no continuous or abstract state.

Here is the caller graph for this function:

 bool is_stateless ( ) const
inline

Returns true if the Context has no state.

Here is the caller graph for this function:

 int num_abstract_parameters ( ) const
inline

Returns the number of abstract-valued parameters.

Here is the caller graph for this function:

 int num_numeric_parameters ( ) const
inline

Returns the number of vector-valued parameters.

Here is the caller graph for this function:

 Context& operator= ( Context< T > && )
delete
 Context& operator= ( const Context< T > & )
delete
 void set_abstract_state ( std::unique_ptr< AbstractValues > xa )
inline

Sets the abstract state to xa, deleting whatever was there before.

Here is the caller graph for this function:

 void set_accuracy ( const optional< double > & accuracy )
inline

Records the user's requested accuracy.

If no accuracy is requested, computations are free to choose suitable defaults, or to refuse to proceed without an explicit accuracy setting.

Requested accuracy is stored in the Context for two reasons:

• It permits all computations performed over a System to see the same accuracy request since accuracy is stored in one shared place, and
• it allows us to invalidate accuracy-dependent cached computations when the requested accuracy has changed.

The accuracy of a complete simulation or other numerical study depends on the accuracy of all contributing computations, so it is important that each computation is done in accordance with the overall requested accuracy. Some examples of where this is needed:

• Error-controlled numerical integrators use the accuracy setting to decide what step sizes to take.
• The Simulator employs a numerical integrator, but also uses accuracy to decide how precisely to isolate witness function zero crossings.
• Iterative calculations reported as results or cached internally depend on accuracy to decide how strictly to converge the results. Examples of these are: constraint projection, calculation of distances between smooth shapes, and deformation calculations for soft contact.

The common thread among these examples is that they all share the same Context, so by keeping accuracy here it can be used effectively to control all accuracy-dependent computations.

Here is the caller graph for this function:

 void set_continuous_state ( std::unique_ptr< ContinuousState< T >> xc )
inline

Sets the continuous state to xc, deleting whatever was there before.

Here is the caller graph for this function:

 void set_discrete_state ( std::unique_ptr< DiscreteValues< T >> xd )
inline

Sets the discrete state to xd, deleting whatever was there before.

Here is the caller graph for this function:

 void set_parent ( const Context< T > * parent )
inline

Declares that parent is the context of the enclosing Diagram.

The enclosing Diagram context is needed to evaluate inputs recursively. Aborts if the parent has already been set to something else.

This is a dangerous implementation detail. Conceptually, a Context ought to be completely ignorant of its parent Context. However, we need this pointer so that we can cause our inputs to be evaluated in EvalInputPort. See https://github.com/RobotLocomotion/drake/pull/3455.

 virtual void set_time ( const T & time_sec )
inlinevirtual

Set the current time in seconds.

Reimplemented in DiagramContext< T >.

Here is the caller graph for this function:

 virtual void SetInputPortValue ( int index, std::unique_ptr< InputPortValue > port_value )
protectedpure virtual

Connects the input port at index to the value source port_value.

Disconnects whatever value source was previously there, and de-registers it from the output port on which it depends. In some Context implementations, may require a recursive search through a tree of subcontexts. Implementations must abort if index is out of range.

Here is the caller graph for this function:

 static void SetInputPortValue ( Context< T > * context, int index, std::unique_ptr< InputPortValue > port_value )
inlinestaticprotected

Allows derived classes to invoke the protected method on subcontexts.

 void SetTimeStateAndParametersFrom ( const Context< double > & source )
inline

Initializes this context's time, state, and parameters from the real values in source, regardless of this context's scalar type.

Requires a constructor T(double).

Here is the caller graph for this function:

 void VerifyInputPort ( const InputPortDescriptor< T1 > & descriptor ) const
inline

Throws an exception unless the given descriptor matches the inputs actually connected to this context in shape.

Supports any scalar type of descriptor, but expects T by default.

Template Parameters
 T1 the scalar type of the InputPortDescriptor to check.

Here is the caller graph for this function:

The documentation for this class was generated from the following file: