DynamicSimComponent Class

(CDPSim::DynamicSimComponent)

The DynamicSimComponent should be used as base class for custom simulation components. More...

Header: #include <DynamicSimComponent>
Inherits: CDPComponent

Public Functions

DynamicSimComponent()
virtual ~DynamicSimComponent() override
virtual void EvaluateDiffEquations(double t)
virtual double GetSimulatorTimestep()
virtual void PostIntegrate()
virtual void PreIntegrate()
virtual void ReallocateStateVariables(int &offset, Matrix1D &Y, Matrix1D &Y_ddt, Matrix1D &Y_tmp)
virtual void RegisterSimSignal(SimSignal *pSimSignal)
virtual void RegisterStateVariable(StateVariable *pStateVariable)
virtual void RegisterStateVariableArray(StateVariableArray *pStateVariableArray)
virtual void Reset()

Reimplemented Public Functions

virtual void Configure(const char *xml) override
virtual void Create(const char *fullName) override
virtual void FillNodeChildren(CDP::StudioAPI::NodeStream &serializer) const override

Reimplemented Protected Functions

virtual bool HandleXMLElement(XMLElementEx *pEx) override

Additional Inherited Members

Detailed Description

The DynamicSimComponent should be used as base class for custom simulation components.

Description

The DynamicSimComponent is a component base class that can be used for implementing advanced dynamic real-time simulation. All simulation components automatically connect to SimulatorManager when created. The SimulatorManager runs all DynamicSimComponent components periodically and allows configuring parameters like simulation time step and integration algorithm.

Override method EvaluateDiffEquations() to create new simulator components. Implement the first order differential state equations for `dx/dt` for state variables in EvaluateDiffEquations(). This simulation method also enables easy implementation of non-linear equations, which can be entered directly in the code.

The dynamic simulation runs independent of the state machine, so the state machine can be used to create states to control the simulation component behaviour.

Choose SimulatorManager TimeStep high enough to be sure to avoid numeric errors and instability.

Example of simple simulation component

State variables:

  • `v`: Speed
  • `x`: Position

Process:

  • `(dv)/(dt) = F/m`
  • `(dx)/(dt) = v`

New simulation component class:

class SimpleExampleSimulator : public CDPSim::DynamicSimComponent
{
public:
  //
  virtual void EvaluateDiffEquations(double t);

  // Creates a component instance.
  virtual void Create(const char* fullName);

protected:
  // Declaring state variables
  CDPSim::StateVariable x;
  CDPSim::StateVariable v;

  // Declaring arrays of state variables. The size is set by Create() or SetSize()
  CDPSim::StateVariableArray pos;
  CDPSim::StateVariableArray vel;

  // Declaring SimSignals. They are useful when Routing values from another DynamicSimComponent
  CDPSim::SimSignal F;

  // Standard CDPProperties can be used
  CDPProperty<double> m;
};


// Create must set up signals to point into buffers.
void SimpleExampleSimulator::Create(const char* fullName)
{
  CDPSim::DynamicSimComponent::Create(fullName); // Always call base class.
  x.Create("x", this);
  v.Create("v", this);

  pos.Create("pos", this, arraySize);
  vel.Create("vel", this, arraySize);
}


// This method must set all the derivatives of the state variables.
// To calculate the derivatives, use state variables v and x, and all other signals that are have been created.
void SimpleExampleSimulator::EvaluateDiffEquations(double t)
{
  x.ddt = v;
  v.ddt = F / static_cast<double>(m);
}

// In this function, fresh values of state variables (and derivatives) may be read and used in calculations of output SimSignals.
// This function is called just after EvaluateDiffEquations(), once per simulator time period.
void SimpleExampleSimulator::PostIntegrate()
{
  if (x < 0) // Simulate a wall at x==0
  {
    x.OverrideStateValue(0);
    v.OverrideStateValue(0); // Reset speed to 0 after hitting a wall
    x.ddt = 0;
  }
}

Member Function Documentation

DynamicSimComponent::DynamicSimComponent()

Constructs a DynamicSimComponent. Initializes all member variables to safe default values.

[override virtual] DynamicSimComponent::~DynamicSimComponent()

Destructs the instance.

[override virtual] void DynamicSimComponent::Configure(const char *xml)

Reimplemented from CDPObject::Configure().

Reads configuration from XML. Called automatically by parent.

[override virtual] void DynamicSimComponent::Create(const char *fullName)

Reimplemented from CDPComponent::Create().

Creates the instance.

[virtual] void DynamicSimComponent::EvaluateDiffEquations(double t)

Implements `dydt = f(y,t)`.

To obtain a high degree of accuracy, this method is run with very small time steps, much smaller than the periodic processes of standard CDP components.

Input:

  • t - simulator time in seconds.
  • All state variables.

Output:

  • All state variable derivatives must be set by this method.

Note, that state variables must not be written in this method, only the derivatives!

[override virtual] void DynamicSimComponent::FillNodeChildren(CDP::StudioAPI::NodeStream &serializer) const

Reimplemented from CDPNode::FillNodeChildren().

Exposes child nodes of this node to StudioAPI.

Must be overridden if node has child nodes to expose.

[virtual] double DynamicSimComponent::GetSimulatorTimestep()

Timestep used in SimulatorManager (requested simulation period). Starting at 1/simulatorFs, but may change dynamically.

[override virtual protected] bool DynamicSimComponent::HandleXMLElement(XMLElementEx *pEx)

Reimplemented from CDPComponent::HandleXMLElement().

Handles creation of objects that have an XML configuration in component configuration file. Handle XMLElement <Name...>

Return true for each element that should not be handled by CDP (properties are generated for unhandled elements)

[virtual] void DynamicSimComponent::PostIntegrate()

In this function, fresh values of state variables (and derivatives) may read and used in calculations of output SimSignals. This function is called just after EvaluateDiffEquations() (in end of Integrate()), once per simulator time period.

It is also possible to overwrite the calculated state using StateVariable::OverrideStateValue().

[virtual] void DynamicSimComponent::PreIntegrate()

This function is called just before EvaluateDiffEquations(), once per simulator time period. In this function, the state variables may be set directly using StateVariable::OverrideStateValue(). The integration will then update the state normally, causing the overridden value to be different after the intergration.

[virtual] void DynamicSimComponent::ReallocateStateVariables(int &offset, Matrix1D &Y, Matrix1D &Y_ddt, Matrix1D &Y_tmp)

Maps StateVariables into Matrixes

[virtual] void DynamicSimComponent::RegisterSimSignal(SimSignal *pSimSignal)

Adds SimSignal to list of SimSignal.

[virtual] void DynamicSimComponent::RegisterStateVariable(StateVariable *pStateVariable)

Adds StateVariable to list of StateVariables.

[virtual] void DynamicSimComponent::RegisterStateVariableArray(StateVariableArray *pStateVariableArray)

Adds StateVariable to list of StateVariables.

[virtual] void DynamicSimComponent::Reset()

Resets the value of every CDPSignal, SimSignal and StateVariable back to default. Useful to restart simulation.

Related Non-Members

typedef CDPSim::SimSignalMap

Synonym for std::map<std::string, SimSignal*>. Contains SimSignal fullName and pointer.

typedef CDPSim::SimSignalVector

Synonym for std::vector<SimSignal*>.

typedef CDPSim::StateVariableMap

Synonym for std::map<std::string, StateVariable*>. Contains StateVariable fullName and pointer.