DynamicSimComponent Class
(CDPSim::DynamicSimComponent)The DynamicSimComponent should be used as base class for custom simulation components. More...
Header: | #include <DynamicSimComponent/DynamicSimComponent.h> |
Inherits: | CDPComponent and |
Inherited By: |
Public Functions
DynamicSimComponent() | |
virtual | ~DynamicSimComponent() override |
virtual void | EvaluateDiffEquations(double t) |
double | GetOperatorPeriod() const override |
virtual SimSignal * | GetSimSignalWithMatchingFullName(const std::string &simSignalName) |
virtual double | GetSimulatorTimestep() |
virtual StateVariable * | GetStateVariableWithMatchingFullName(const std::string &stateVariableName) |
virtual bool | HasOperatorWithMatchingFullName(const std::string &fullName) |
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 | 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 |
virtual void | SetProperty(std::string propertyName, std::string propertyValue) override |
- 90 public functions inherited from CDPComponent
- 37 public functions inherited from CDPObject
- 46 public functions inherited from CDPBaseObject
- 26 public functions inherited from CDP::StudioAPI::CDPNode
- 22 public functions inherited from CDP::StudioAPI::ICDPNode
Reimplemented Protected Functions
virtual bool | HandleXMLElement(XMLElementEx *pEx) override |
virtual void | ProcessSubSchedulables() override |
virtual bool | ShouldBeSubScheduled(CDPSchedulable *item) const override |
virtual void | UpdateSchedulerLists() override |
- 12 protected functions inherited from CDPComponent
- 13 protected functions inherited from CDPObject
- 1 protected function inherited from CDP::StudioAPI::CDPNode
Related Non-Members
typedef | SimSignalMap |
typedef | SimSignalVector |
typedef | StateVariableMap |
typedef | StateVariableVector |
Additional Inherited Members
- 2 static public members inherited from CDPComponent
- 6 static public members inherited from CDPObject
- 1 static public member inherited from CDPBaseObject
- 12 protected functions inherited from CDPComponent
- 13 protected functions inherited from CDPObject
- 1 protected function inherited from CDP::StudioAPI::CDPNode
- 42 protected variables inherited from CDPComponent
- 9 protected variables inherited from CDPObject
- 11 protected variables inherited from CDPBaseObject
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.
If Operators are used in a DynamicSimComponent, they will be processed every TimeStep of the SimulatorManager.
Choose SimulatorManager TimeStep high enough to be sure to avoid numeric errors and instability. If SimulatorManager's TimeStep or fs is changed during runtime, SimulatorManager's message 'Reset' is automatically called, so DynamicSimComponent::Reset() is called for all DynamicSimComponents.
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.
double DynamicSimComponent::GetOperatorPeriod() const
Returns the Operator sampling period.
For Operators used by DynamicSimComponent, SimulatorManager::GetSimulatorTimestep() is returned.
[virtual]
SimSignal *DynamicSimComponent::GetSimSignalWithMatchingFullName(const std::string &simSignalName)
Returns a pointer to a SimSignal if this component has a SimSignal with name equal simSignalName, or nullptr if not found.
[virtual]
double DynamicSimComponent::GetSimulatorTimestep()
Timestep used in SimulatorManager (requested simulation period). Starting at 1/simulatorFs, but may change dynamically.
[virtual]
StateVariable *DynamicSimComponent::GetStateVariableWithMatchingFullName(const std::string &stateVariableName)
Returns a pointer to a StateVariable if this component has a StateVariable with name equal stateVariableName, or nullptr if not found. stateVariableName may also be equal name.Value or name.ddt.
[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]
bool DynamicSimComponent::HasOperatorWithMatchingFullName(const std::string &fullName)
Returns true if this component has an operator given by fullName.
[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.
[override virtual protected]
void DynamicSimComponent::ProcessSubSchedulables()
Reimplemented from CDPComponent::ProcessSubSchedulables().
CDPComponent::ProcessSubSchedulables() is normally run from a CDPComponent's Process-function. This overrided version will only run CDPComponent::ProcessSubSchedulables() if called during simulation-loop (from IntegrationBase::ScheduleSubComponents()), and not when called from SimulatorManager::RunProcessForDynamicSimComponents().
[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 m_stateVariables (map of StateVariables).
[virtual]
void DynamicSimComponent::Reset()
Resets the value of every CDPSignal, SimSignal and StateVariable back to default, and Init() is called on all operators. Useful to restart simulation.
[override virtual]
void DynamicSimComponent::SetProperty(std::string propertyName, std::string propertyValue)
Reimplemented from CDPBaseObject::SetProperty().
Will check if propertyName corresponds to a registered StateVariable (propertyName must match the name of the StateVariable itself, or be equal to name.Value). If a match is found, StateVariable::OverrideStateValue() will be called with value of propertyValue. If not found, CDPComponent::SetProperty() will be called.
See also CDPComponent::SetProperty().
[override virtual protected]
bool DynamicSimComponent::ShouldBeSubScheduled(CDPSchedulable *item) const
Reimplemented from CDPComponent::ShouldBeSubScheduled().
Returns false if item
is a CDPComponent, because SimulatorManager does the scheduling, otherwise CDPComponent::ShouldBeSubScheduled(item) is returned.
[override virtual protected]
void DynamicSimComponent::UpdateSchedulerLists()
Reimplemented from CDPComponent::UpdateSchedulerLists().
For DynamicSimComponent this function is overrided and should do nothing (because they are scheduled by SimulatorManager).
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.
typedef CDPSim::StateVariableVector
Synonym for std::vector<StateVariable*>.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.