Hopsan
ComponentSimulationFunctions

Functions

double readNode (const size_t idx) const
 Reads a value from the connected node. More...
 
virtual double readNode (const size_t idx, const size_t subPortIdx) const
 Reads a value from the connected node. More...
 
void writeNode (const size_t idx, const double value)
 Writes a value to the connected node. More...
 
virtual void writeNode (const size_t idx, const double value, const size_t subPortIdx)
 Writes a value to the connected node. More...
 
double readNode (const size_t idx, const size_t subPortIdx) const
 Reads a value from the connected node. More...
 
void writeNode (const size_t idx, const double value, const size_t subPortIdx)
 Writes a value to the connected node. More...
 
virtual void initialize ()
 The initialize function must be overloaded in each component, it is used to initialize the component just before simulation begins. More...
 
virtual void simulateOneTimestep ()
 Simulates one time step. This component must be overloaded en each component. More...
 
virtual void finalize ()
 Optional function that is called after every simulation, can be used to clean up memory allocation made in initialize.
 
void stopSimulation (const HString &rReason="")
 Terminate/stop a running initialization or simulation. More...
 
double * getSafeNodeDataPtr (Port *pPort, const int dataId)
 Get a pointer to the node data variable, (Port pointer version) More...
 
double * getSafeNodeDataPtr (const HString &rPortName, const int dataId)
 Get a pointer to the node data variable, (Port name version) More...
 
double * getSafeMultiPortNodeDataPtr (Port *pPort, const size_t portIdx, const int dataId)
 Get a pointer to node data in a subport in a multiport. More...
 
double * getSafeMultiPortNodeDataPtr (Port *pPort, const size_t portIdx, const int dataId, const double defaultValue)
 Get a pointer to node data in a subport in a multiport (also setting initial value at the same time) More...
 
double readNodeSafe (const HString &rPortName, const HString &rDataName, const size_t subPortIdx=0)
 Read value based on the port and node data name. More...
 
double readNodeSafe (const HString &rPortName, const size_t dataId, const size_t subPortIdx=0)
 Read value based on the port name and node data id. More...
 
void writeNodeSafe (const HString &rPortName, const HString &rDataName, const double value, const size_t subPortIdx=0)
 Write node data based on port and data name. More...
 
void writeNodeSafe (const HString &rPortName, const size_t dataId, const double value, const size_t subPortIdx=0)
 Write node data based on port and data id. More...
 
virtual double readNodeSafe (const size_t idx, const size_t subPortIdx=0) const
 Reads a value from the connected node. More...
 
virtual void writeNodeSafe (const size_t idx, const double value, const size_t subPortIdx=0)
 Writes a value to the connected node. More...
 
double readNodeSafe (const size_t idx, const size_t subPortIdx) const
 Reads a value from the connected node. More...
 
void writeNodeSafe (const size_t idx, const double value, const size_t subPortIdx)
 Writes a value to the connected node. More...
 

Detailed Description

Function Documentation

◆ getSafeMultiPortNodeDataPtr() [1/2]

double * getSafeMultiPortNodeDataPtr ( Port pPort,
const size_t  portIdx,
const int  dataId 
)

Get a pointer to node data in a subport in a multiport.

Parameters
[in]pPortA pointer to the port from which to fetch NodeData pointer
[in]portIdxThe index of the subport in a multiport
[in]dataIdThe enum id for the node value to fetch pointer to
Returns
A pointer to the specified NodeData or a pointer to dummy NodeData

◆ getSafeMultiPortNodeDataPtr() [2/2]

double * getSafeMultiPortNodeDataPtr ( Port pPort,
const size_t  portIdx,
const int  dataId,
const double  defaultValue 
)

Get a pointer to node data in a subport in a multiport (also setting initial value at the same time)

Parameters
[in]pPortA pointer to the port from which to fetch NodeData pointer
[in]portIdxThe index of the subport in a multiport
[in]dataIdThe enum id for the node value to fetch pointer to
[in]defaultValueThe initial value to set for this node data
Returns
A pointer to the specified NodeData or a pointer to dummy NodeData

◆ getSafeNodeDataPtr() [1/2]

double * getSafeNodeDataPtr ( const HString rPortName,
const int  dataId 
)

Get a pointer to the node data variable, (Port name version)

Note
This function is slow, you should not run it during simulation The safe in this version means that a dummy pointer will be returned if the desired one was not found, this prevents crash and the need to check that return pointer is ok, giving cleaner component code An error message is given if the desired data was not found
Parameters
[in]rPortNameThe name of the port from which to fetch the node data pointer
[in]dataIdThe enum id for the node value to fetch pointer to, (Ex: ModeHydraulic::Pressure)
Returns
A pointer to the specified NodeData or a pointer to dummy NodeData

◆ getSafeNodeDataPtr() [2/2]

double * getSafeNodeDataPtr ( Port pPort,
const int  dataId 
)

Get a pointer to the node data variable, (Port pointer version)

Note
This function is slow, you should not run it during simulation The safe in this version means that a dummy pointer will be returned if the desired one was not found, this prevents crash and the need to check that return pointer is ok, giving cleaner component code An error message is given if the desired data was not found
Parameters
[in]pPortA pointer to the port from which to fetch the node data pointer
[in]dataIdThe enum id for the node value to fetch pointer to, (Ex: ModeHydraulic::Pressure)
Returns
A pointer to the specified NodeData or a pointer to dummy NodeData

◆ initialize()

void initialize ( )
protectedvirtual

The initialize function must be overloaded in each component, it is used to initialize the component just before simulation begins.

In this function you should get node data ptrs and calculate initial values to write to the nodes You are not allowed to reconnect internal connections in this function, as other components may already have initialized and fetch data pointers to ports/nodes in this component

Reimplemented in DummyComponent.

◆ readNode() [1/3]

double readNode ( const size_t  idx) const
inline

Reads a value from the connected node.

Parameters
[in]idxThe data id of the data to read
Returns
The data value

◆ readNode() [2/3]

virtual double readNode ( const size_t  idx,
const size_t  subPortIdx 
) const
inlinevirtual

Reads a value from the connected node.

Parameters
[in]idxThe data id of the data to read
[in]subPortIdx(Ignored on non multi ports)
Returns
The data value

Reimplemented in MultiPort.

◆ readNode() [3/3]

double readNode ( const size_t  idx,
const size_t  subPortIdx 
) const
inlinevirtual

Reads a value from the connected node.

Parameters
[in]idxThe data id of the data to read
[in]subPortIdxThe subPort to read from (range is NOT checked)
Returns
The data value

Reimplemented from Port.

◆ readNodeSafe() [1/4]

double readNodeSafe ( const HString rPortName,
const HString rDataName,
const size_t  subPortIdx = 0 
)

Read value based on the port and node data name.

Note
This functions is slow but safe, do not use it during simulation It searches for data based on strings, this make it unsuitable for use during simulation but it is suitable during initialize when port pointers are not desired/available This function will also check so that the desired data actually exist in the requested node, error message will be sent if it does not
Parameters
[in]rPortNameThe port to write data to
[in]rDataNameThe data variable name for the data to be written
[in]subPortIdxThe index of a multiport subport
Returns
The value from the requested PortName::DataName or -1 if failure, Note! an error message will also be shown

◆ readNodeSafe() [2/4]

double readNodeSafe ( const HString rPortName,
const size_t  dataId,
const size_t  subPortIdx = 0 
)

Read value based on the port name and node data id.

Note
This functions is slow but safe, do not use it during simulation It searches for data based on strings, this make it unsuitable for use during simulation but it is suitable during initialize when port pointers are not desired/available This function will only check if node data id is within range in the node, not if the data actually exist
Parameters
[in]rPortNameThe port to write data to
[in]dataIdThe data variable id for the data to be written
[in]subPortIdxThe index of a multiport subport
Returns
The value from the requested PortName::DataId or -1 if failure, Note! an error message will also be shown

◆ readNodeSafe() [3/4]

double readNodeSafe ( const size_t  idx,
const size_t  subPortIdx 
) const
virtual

Reads a value from the connected node.

Parameters
[in]idxThe data id of the data to read
[in]subPortIdxThe subPort index in the multi port Safe but slow version, will not crash if idx out of bounds
Returns
The data value or -1 if any of the idxes are out of range

Reimplemented from Port.

◆ readNodeSafe() [4/4]

double readNodeSafe ( const size_t  idx,
const size_t  subPortIdx = 0 
) const
virtual

Reads a value from the connected node.

Parameters
[in]idxThe data id of the data to read
[in]subPortIdxThe subPort index of a port in a multiport, (ignored if not a multiport) Safe but slow version, will not crash if idx out of bounds
Returns
The data value

Reimplemented in MultiPort.

◆ simulateOneTimestep()

void simulateOneTimestep ( )
protectedvirtual

Simulates one time step. This component must be overloaded en each component.

This is the function where all the component model equations should be written. This function is called once for every time step

Reimplemented in DummyComponent.

◆ stopSimulation()

void stopSimulation ( const HString rReason = "")

Terminate/stop a running initialization or simulation.

Parameters
[in]rReasonAn optional HString describing the reason for the stop Typically used inside components simulateOneTimestep method

◆ writeNode() [1/3]

void writeNode ( const size_t  idx,
const double  value 
)
inline

Writes a value to the connected node.

Parameters
[in]idxThe data id of the data to write
[in]valueThe value to write

◆ writeNode() [2/3]

virtual void writeNode ( const size_t  idx,
const double  value,
const size_t  subPortIdx 
)
inlinevirtual

Writes a value to the connected node.

Parameters
[in]idxThe data id of the data to write
[in]valueThe value to write
[in]subPortIdx(Ignored on non multi ports)

Reimplemented in MultiPort.

◆ writeNode() [3/3]

void writeNode ( const size_t  idx,
const double  value,
const size_t  subPortIdx 
)
inlinevirtual

Writes a value to the connected node.

Parameters
[in]idxThe data id of the data to write
[in]valueThe value to write
[in]subPortIdxThe subPort to write to (range is NOT checked)

Reimplemented from Port.

◆ writeNodeSafe() [1/4]

void writeNodeSafe ( const HString rPortName,
const HString rDataName,
const double  value,
const size_t  subPortIdx = 0 
)

Write node data based on port and data name.

Note
This functions is slow but safe, do not use it during simulation It searches for data based on strings, this make it unsuitable for use during simulation but it is suitable during initialize when port pointers are not desired/available This function will also check so that the desired data actually exist in the requested node, error message will be sent if it does not
Parameters
[in]rPortNameThe port to write data to
[in]rDataNameThe data variable name for the data to be written
[in]valueThe value to write
[in]subPortIdxThe index of a multiport subport

◆ writeNodeSafe() [2/4]

void writeNodeSafe ( const HString rPortName,
const size_t  dataId,
const double  value,
const size_t  subPortIdx = 0 
)

Write node data based on port and data id.

Note
This functions is slow but safe, do not use it during simulation It searches for data based on strings, this make it unsuitable for use during simulation but it is suitable during initialize when port pointers are not desired/available This function will only check if node data id is within range in the node, not if the data actually exist
Parameters
[in]rPortNameThe port to write data to
[in]dataIdThe data variable id for the data to be written
[in]valueThe value to write
[in]subPortIdxThe index of a multiport subport

◆ writeNodeSafe() [3/4]

void writeNodeSafe ( const size_t  idx,
const double  value,
const size_t  subPortIdx 
)
virtual

Writes a value to the connected node.

Parameters
[in]idxThe data id of the data to write (Such as NodeHydraulic::Pressure)
[in]valueThe value of the data to read
[in]subPortIdxThe subport to write to, (range check is performed) Safe but slow version, will not crash if idx out of bounds

Reimplemented from Port.

◆ writeNodeSafe() [4/4]

void writeNodeSafe ( const size_t  idx,
const double  value,
const size_t  subPortIdx = 0 
)
virtual

Writes a value to the connected node.

Parameters
[in]idxThe data id of the data to write (Such as NodeHydraulic::Pressure)
[in]valueThe value of the data to read
[in]subPortIdxIgnored for non multi ports Safe but slow version, will not crash if idx out of bounds

Reimplemented in ReadPort, and MultiPort.