• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Framework - Simulator
  • StateVariable
  • 5.0.0

StateVariable Class

(CDPSim::StateVariable)

The StateVariable wraps a state and its derivative. It is used by classes inheriting from DynamicSimComponent. More...

Header: #include <StateVariable>
Inherits: CDP::StudioAPI::CDPNode
  • List of all members, including inherited members

Public Functions

StateVariable()
virtual ~StateVariable() override
virtual void Configure(XMLElementEx *pStateVariableXML)
virtual void Create(const std::string &name, DynamicSimComponent *componentOwner, ICDPNode *nodeOwner = nullptr)
virtual double *GetValuePointer() const
void OverrideStateValue(double newValue)
virtual void UpdateValueFromConfiguration()
operator double() const

Reimplemented Public Functions

virtual void FillNodeChildren(CDP::StudioAPI::NodeStream &serializer) const override
virtual const std::string GetNodeName() const override
virtual std::string GetNodeTypeName() const override
virtual CDP::StudioAPI::CDPValueType GetNodeValueType() const override
virtual CDP::StudioAPI::CDPVariantValue GetVariantValue() const override
  • 27 public functions inherited from CDP::StudioAPI::CDPNode
  • 22 public functions inherited from CDP::StudioAPI::ICDPNode

Additional Inherited Members

  • 1 protected function inherited from CDP::StudioAPI::CDPNode

Detailed Description

The StateVariable wraps a state and its derivative. It is used by classes inheriting from DynamicSimComponent.

Usage

Instantiate as a member of a parent DynamicSimComponent in the header file and call Create() during initialization.

To read "the state", use the object as if it is a double (e.g. "Output = x;"). To write "the state", use the OverrideStateValue() function (e.g. "x.OverrideStateValue(0);"). To read or write "the state's derivative" (usually done in simulator component DynamicSimComponent::EvaluateDiffEquations() method), read or write the .ddt member (e.g. "x.ddt = v;").

Example

In the class declaration:

CDPSim::StateVariable x, v;

In the implementation file: In Create(...):

x.Create("x", this);
v.Create("v", this);

In EvaluateDiffEquations(...):

x.ddt = v;    // Set "x's derivative" to "v's state".
v.ddt = F/m;

Alternatively, when the DynamicSimComponent's SolverType="1 - Algebraic (DAE)", instead set the residuals in EvaluateAlgebraicEquations(...)

x.residual = x.ddt - v;
v.residual = v.ddt - F/m;

In 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

StateVariable::StateVariable()

Default constructs an instance of StateVariable.

[override virtual] StateVariable::~StateVariable()

Destroys the instance of StateVariable. The destructor is virtual.

[virtual] void StateVariable::Configure(XMLElementEx *pStateVariableXML)

Reads configuration from XML. Called automatically by parent DynamicSimComponent.

[virtual] void StateVariable::Create(const std::string &name, DynamicSimComponent *componentOwner, ICDPNode *nodeOwner = nullptr)

Creates the instance, binds it to name and registers to componentOwner.

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

Reimplemented from CDPNode::FillNodeChildren().

Exposes child nodes of this node to StudioAPI.

[override virtual] const std::string StateVariable::GetNodeName() const

Reimplemented from ICDPNode::GetNodeName().

Returns node's unique name.

[override virtual] std::string StateVariable::GetNodeTypeName() const

Reimplemented from ICDPNode::GetNodeTypeName().

Returns node's model name.

[override virtual] CDP::StudioAPI::CDPValueType StateVariable::GetNodeValueType() const

Reimplemented from ICDPNode::GetNodeValueType().

Returns node's value type which is double.

[virtual] double *StateVariable::GetValuePointer() const

Returns pointer to the node's internal value.

[override virtual] CDP::StudioAPI::CDPVariantValue StateVariable::GetVariantValue() const

Reimplemented from ICDPNode::GetVariantValue().

Returns node's value.

void StateVariable::OverrideStateValue(double newValue)

Use this method to override the state variable's value to something else than what the simulator calculates. This can be done in DynamicSimComponent::Reset(), DynamicSimComponent::PreIntegrate(), DynamicSimComponent::PostIntegrate() and DynamicSimComponent::SetProperty() (when called by CDPBaseObject::MessageSetProperty()).

This method should not be called from DynamicSimComponent::EvaluateDiffEquations() or DynamicSimComponent::EvaluateAlgebraicEquations().

[virtual] void StateVariable::UpdateValueFromConfiguration()

Resets value to initial value read from configuration.

StateVariable::operator double() const

Returns the state variable's value.

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top