Hopsan
Component Class Reference
Inheritance diagram for Component:
ComponentC ComponentQ ComponentSignal ComponentSystem DummyComponent ConditionalComponentSystem

Public Types

enum  CQSEnumT { CType, QType, SType, UndefinedCQSType }
 Enum type for all CQS types.
 

Public Member Functions

virtual void configure ()
 Configures a component by setting up ports, variables, constants and other resources. More...
 
virtual void reconfigure ()
 Re-configures a component, depending on e.g. parameter values or external files. More...
 
virtual void deconfigure ()
 Deconfigure a component, use this to cleanup and memory/resource allocations you have made in configure. More...
 
virtual bool preInitialize ()
 This function can be used to automate things prior to component initialization, only use this if you know what you are doing. More...
 
virtual bool checkModelBeforeSimulation ()
 
virtual bool initialize (const double startT, const double stopT)
 Virtual Function, base version which gives you an error if you try to use it.
 
virtual void simulate (const double stopT)
 Simulates the component from current simulation position to stopT using previously set timestep. More...
 
void setDisabled (bool value)
 
bool isDisabled () const
 
void setDesiredTimestep (const double timestep)
 
void setInheritTimestep (const bool inherit=true)
 Set whether the component should inherit timestep from its system parent. More...
 
bool doesInheritTimestep () const
 Check if a component inherits timestep from its system parent. More...
 
void setName (HString name)
 Set the desired component name. More...
 
const HStringgetName () const
 Get the component name.
 
const HStringgetTypeName () const
 Get the TypeName of the component.
 
const HStringgetSubTypeName () const
 Get the SubType name of the component.
 
void setSubTypeName (const HString &rSubTypeName)
 Set the SubType name of the component.
 
virtual CQSEnumT getTypeCQS () const
 Get the C, Q or S type of the component as enum.
 
HString getTypeCQSString () const
 Get the CQStype as string.
 
virtual bool isComponentC () const
 Check if a component is a C-Component. More...
 
virtual bool isComponentQ () const
 Check if a component is a Q-Component. More...
 
virtual bool isComponentSystem () const
 Check if a component is a System-Component. More...
 
virtual bool isComponentSignal () const
 Check if a component is a Signal-Component. More...
 
virtual bool isExperimental () const
 Check if component is tagged as experimental. More...
 
virtual bool isObsolete () const
 Check if component is tagged as obsolete. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rQuantityOrUnit, const double defaultValue, double &rData)
 Add (register) a constant parameter with a default value to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rQuantity, const HString &rUnit, const double defaultValue, double &rData)
 Add (register) a constant parameter with a default value to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, HTextBlock &rData)
 
void addConstant (const HString &rName, const HString &rDescription, HFilePath &rData)
 
void addConditionalConstant (const HString &rName, const HString &rDescription, std::vector< HString > &rConditions, int &rData)
 Add (register) a conditional constant parameter to the component. More...
 
void addConditionalConstant (const HString &rName, const HString &rDescription, std::vector< HString > &rConditions, const int defaultValue, int &rData)
 Add (register) a conditional constant parameter to the component. More...
 
void setReconfigurationParameter (const HString &rName)
 
const std::vector< VariameterDescription > * getVariameters ()
 
virtual std::list< HStringgetModelAssets () const
 
virtual size_t loadParameterValues (const HString &rFilePath)
 Loads parameters from a file. More...
 
void registerConditionalParameter (const HString &rName, const HString &rDescription, std::vector< HString > &rConditions, int &rValue)
 Register a conditional parameter value so that it can be accessed for read and write. More...
 
virtual void unRegisterParameter (const HString &rName)
 Removes a parameter from the component.
 
bool hasParameter (const HString &rName) const
 Check if a component has a specific parameter.
 
void getParameterNames (std::vector< HString > &rParameterNames)
 
const std::vector< ParameterEvaluator * > * getParametersVectorPtr () const
 
const ParameterEvaluatorgetParameter (const HString &rName)
 
void getParameterValue (const HString &rName, HString &rValue)
 
void * getParameterDataPtr (const HString &rName)
 Returns a pointer directly to the parameter data variable. More...
 
bool setParameterValue (const HString &rName, const HString &rValue, bool force=false)
 
bool checkParameters (HString &errParName)
 
void evaluateParameters ()
 
bool evaluateParameter (const HString &rName, HString &rEvaluatedParameterValue, const HString &rType)
 
void setDefaultStartValue (Port *pPort, const size_t idx, const double value)
 Set the default startvalue in a port. More...
 
void setDefaultStartValue (const HString &rPortName, const HString &rDataName, const double value)
 Set the default startvalue in a port. More...
 
double getDefaultStartValue (Port *pPort, const size_t idx, const size_t portIdx=0)
 Get the an actual start value of a port. More...
 
double getDefaultStartValue (const HString &rPortName, const HString &rDataName, const size_t portIdx=0)
 Get the an actual start value of a port. More...
 
void disableStartValue (Port *pPort, const size_t idx)
 Disable a start value to prevent the user from setting it, this is usefully if you calculate the value internally. More...
 
void disableStartValue (const HString &rPortName, const size_t idx)
 Disable a start value to prevent the user from setting it, this is usefully if you calculate the value internally. More...
 
void disableStartValue (const HString &rPortName, const HString &rDataName)
 Disable a start value to prevent the user from setting it, this is usefully if you calculate the value internally. More...
 
virtual void loadStartValues ()
 Loads the default start values to the connected node in each port on the component.
 
virtual void loadStartValuesFromSimulation ()
 
std::vector< Port * > getPortPtrVector () const
 
PortgetPort (const HString &rPortname) const
 Returns a pointer to the port with the given name. More...
 
std::vector< HStringgetPortNames ()
 Returns a string vector containing names of all ports in the component. 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...
 
ComponentSystemgetSystemParent () const
 
size_t getModelHierarchyDepth () const
 
double getTimestep () const
 Returns the component simulation time step. More...
 
double * getTimePtr ()
 Returns a pointer to the simulation time variable in the component. More...
 
double getTime () const
 Get the current simulation time.
 
void setMeasuredTime (const double time)
 
double getMeasuredTime () const
 Returns the measured time variable for the component. This is used to measure time requirements when sorting components for multicore purposes.
 
void addDebugMessage (const HString &rMessage, const HString &rTag="") const
 Write an Debug message, i.e. for debugging purposes. More...
 
void addWarningMessage (const HString &rMessage, const HString &rTag="") const
 Write an Warning message. More...
 
void addErrorMessage (const HString &rMessage, const HString &rTag="") const
 Write an Error message. More...
 
void addInfoMessage (const HString &rMessage, const HString &rTag="") const
 Write an Info message. More...
 
void addFatalMessage (const HString &rMessage, const HString &rTag="") const
 Writes a Fatal message and tells the receiver of the message to close program in a controlled way. Also prints message to log file. More...
 
void stopSimulation (const HString &rReason="")
 Terminate/stop a running initialization or simulation. More...
 
HopsanEssentialsgetHopsanEssentials ()
 
HString findFilePath (const HString &rFileName) const
 Find and return the full file path name of fileName within the system search path, parent systems included (path to HMF file is always in here) More...
 
std::vector< HStringgetSearchPaths () const
 
virtual void reInitializeValuesFromNodes ()
 
virtual void solveSystem ()
 
virtual double getStateVariableDerivative (int)
 
virtual double getStateVariableSecondDerivative (int)
 
virtual void getResiduals (double *, double *)
 
virtual void getJacobian (double *, double *, double *)
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, double &rData)
 Add (register) a constant parameter to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, int &rData)
 Add (register) a constant parameter to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, HString &rData)
 Add (register) a constant parameter to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, bool &rData)
 Add (register) a constant parameter to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, const int defaultValue, int &rData)
 Add (register) a constant parameter with a default value to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, const HString &defaultValue, HString &rData)
 Add (register) a constant parameter with a default value to the component. More...
 
void addConstant (const HString &rName, const HString &rDescription, const HString &rUnit, const bool defaultValue, bool &rData)
 Add (register) a constant parameter with a default value to the component. More...
 
void setConstantValue (const HString &rName, const double value)
 Set the value of a constant parameter. More...
 
void setConstantValue (const HString &rName, const int value)
 Set the value of a constant parameter. More...
 
void setConstantValue (const HString &rName, const HString &rValue)
 Set the value of a constant parameter. More...
 
void setConstantValue (const HString &rName, const char *value)
 Set the value of a constant parameter. More...
 
void setConstantValue (const HString &rName, const bool value)
 Set the value of a constant parameter. More...
 

Protected Member Functions

 Component ()
 Component base class Constructor.
 
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.
 
virtual void setTimestep (const double timestep)
 
virtual size_t calcNumSimSteps (const double startT, const double stopT) const
 
PortaddInputVariable (const HString &rName, const HString &rDescription, const HString &rQuantityOrUnit, const double defaultValue, double **ppNodeData=0)
 Add an inputVariable (Scalar signal ReadPort) More...
 
PortaddOutputVariable (const HString &rName, const HString &rDescription, const HString &rQuantityOrUnit, double **ppNodeData=0)
 Add an outputVariable (Scalar signal WritePort) without default value. More...
 
PortaddOutputVariable (const HString &rName, const HString &rDescription, const HString &rQuantityOrUnit, const double defaultValue, double **ppNodeData=0)
 Add an outputVariable (Scalar signal WritePort) with default value. More...
 
void initializeAutoSignalNodeDataPtrs ()
 Automatically retrieve and assign node data pointers that have been registered.
 
PortaddPort (const HString &rPortName, const PortTypesEnumT portType, const HString &rNodeType, const HString &rDescription, const Port::RequireConnectionEnumT reqConnection)
 Adds a port to the component, do not call this function directly unless you have to. More...
 
PortaddPowerPort (const HString &rPortName, const HString &rNodeType, const HString &rDescription="", const Port::RequireConnectionEnumT reqConnect=Port::Required)
 Add a PowerPort with description to the component. More...
 
PortaddReadPort (const HString &rPortName, const HString &rNodeType, const HString &rDescription="", const Port::RequireConnectionEnumT reqConnect=Port::Required)
 Add a ReadPort with description to the component. More...
 
PortaddPowerMultiPort (const HString &rPortName, const HString &rNodeType, const HString &rDescription="", const Port::RequireConnectionEnumT reqConnect=Port::Required)
 Add a PowerMultiPort with description to the component. More...
 
PortaddReadMultiPort (const HString &rPortName, const HString &rNodeType, const HString &rDescription="", const Port::RequireConnectionEnumT reqConnect=Port::Required)
 Add a ReadMultiPort with description to the component. More...
 
PortaddWritePort (const HString &rPortName, const HString &rNodeType, const HString &rDescription="", const Port::RequireConnectionEnumT reqConnect=Port::Required)
 Add a WritePort with description to the component. More...
 
void removePort (const HString &rPortName)
 
bool getPort (const HString &rPortname, Port *&rpPort)
 Get a port as reference to pointer. More...
 
HString renamePort (const HString &rOldname, const HString &rNewname)
 Rename a port. More...
 
void deletePort (const HString &rName)
 Removes and deletes a port from a component. More...
 
virtual HString determineUniquePortName (const HString &rPortname)
 a virtual function that determines a unique port name, needs to be overloaded in ComponentSystem to do this slightly different
 

Protected Attributes

ComponentSystemmpSystemParent
 
bool mInheritTimestep
 
double mTimestep
 
double mDesiredTimestep
 
double mTime
 
size_t mModelHierarchyDepth
 This variable contains the depth of the system in the model hierarchy, (used by connect to figure out where to store nodes)
 
std::vector< HStringmSearchPaths
 

Friends

class ComponentSystem
 
class ConditionalComponentSystem
 
class HopsanEssentials
 
class NumericalIntegrationSolver
 

Member Function Documentation

◆ addPort()

Port * addPort ( const HString rPortName,
const PortTypesEnumT  portType,
const HString rNodeType,
const HString rDescription,
const Port::RequireConnectionEnumT  reqConnection 
)
protected

Adds a port to the component, do not call this function directly unless you have to.

Parameters
[in]rPortNameThe desired name of the port (may be automatically changed)
[in]portTypeThe type of port
[in]rNodeTypeThe type of node that must be connected to the port
[in]rDescriptionA description string describing the port
[in]reqConnectionSpecify if the port must be connected or if it is optional
Returns
A pointer to the created port

◆ configure()

void configure ( )
virtual

Configures a component by setting up ports, variables, constants and other resources.

Every function must overload this function, in this function ports variables and constants will be added The component author is however free to add any other desired code as well. This function is called immediately after a component instance has been created

Reimplemented in ConditionalComponentSystem, ComponentSystem, and DummyComponent.

◆ deconfigure()

void deconfigure ( )
virtual

Deconfigure a component, use this to cleanup and memory/resource allocations you have made in configure.

This function can be optionally overloaded if it is needed You can use it to free memory or other resources that you have created in configure This function is the last one called before a component instance is deleted (destructor called)

◆ deletePort()

void deletePort ( const HString rName)
protected

Removes and deletes a port from a component.

Parameters
[in]rNameThe name of the port to delete
Note
Only use this function to remove systemports, removing ordinary ports from components is a bad idea

◆ disableStartValue() [1/3]

void disableStartValue ( const HString rPortName,
const HString rDataName 
)

Disable a start value to prevent the user from setting it, this is usefully if you calculate the value internally.

Parameters
[in]rPortNameThe name of the port to disable start value on
[in]rDataNameThe name of the node data to disable start value for If the given port or node data name is incorrect the an error message will be added

◆ disableStartValue() [2/3]

void disableStartValue ( const HString rPortName,
const size_t  idx 
)

Disable a start value to prevent the user from setting it, this is usefully if you calculate the value internally.

Parameters
[in]rPortNameThe name of the port to disable start value on
[in]idxThe node data index to disable start value for If the given port name is incorrect the an error message will be added

◆ disableStartValue() [3/3]

void disableStartValue ( Port pPort,
const size_t  idx 
)

Disable a start value to prevent the user from setting it, this is usefully if you calculate the value internally.

Parameters
[in]pPortPointer to the port to disable start value on
[in]idxThe node data index to disable start value for

◆ doesInheritTimestep()

bool doesInheritTimestep ( ) const

Check if a component inherits timestep from its system parent.

Returns
True or False

◆ getModelAssets()

std::list< HString > getModelAssets ( ) const
virtual

◆ getParameterDataPtr()

void * getParameterDataPtr ( const HString rName)

Returns a pointer directly to the parameter data variable.

Warning
Don't use this function unless YOU REALLY KNOW WHAT YOU ARE DOING

◆ getPort() [1/2]

Port * getPort ( const HString rPortname) const

Returns a pointer to the port with the given name.

Parameters
[in]rPortnameThe name of the port
Returns
A pointer to the port, or 0 if port not found

◆ getPort() [2/2]

bool getPort ( const HString rPortname,
Port *&  rpPort 
)
protected

Get a port as reference to pointer.

◆ getPortNames()

std::vector< HString > getPortNames ( )

Returns a string vector containing names of all ports in the component.

Returns
A vector with the port names

◆ getPortPtrVector()

vector< Port * > getPortPtrVector ( ) const

◆ getTimePtr()

double * getTimePtr ( )

Returns a pointer to the simulation time variable in the component.

Returns
pointer to time variable

◆ getTimestep()

double getTimestep ( ) const

Returns the component simulation time step.

◆ getVariameters()

const std::vector< VariameterDescription > * getVariameters ( )

<

◆ isComponentC()

bool isComponentC ( ) const
virtual

Check if a component is a C-Component.

Returns
true or false
See also
getTypeCQS() getTypeCQSString()

◆ isComponentQ()

bool isComponentQ ( ) const
virtual

Check if a component is a Q-Component.

Returns
true or false

◆ isComponentSignal()

bool isComponentSignal ( ) const
virtual

Check if a component is a Signal-Component.

Returns
true or false

Reimplemented in ComponentSignal.

◆ isComponentSystem()

bool isComponentSystem ( ) const
virtual

Check if a component is a System-Component.

Returns
true or false

Reimplemented in ComponentSystem.

◆ isExperimental()

bool isExperimental ( ) const
virtual

Check if component is tagged as experimental.

Returns
true or false

◆ isObsolete()

bool isObsolete ( ) const
virtual

Check if component is tagged as obsolete.

Returns
true or false

◆ loadParameterValues()

size_t loadParameterValues ( const HString rFilePath)
virtual

Loads parameters from a file.

Parameters
[in]rFilePathThe file to load from
Returns
Number of changed parameters

Reimplemented in ComponentSystem.

◆ loadStartValuesFromSimulation()

void loadStartValuesFromSimulation ( )
virtual

The function saves the last values as default startvalues for the next simulation

Reimplemented in ComponentSystem.

◆ reconfigure()

void reconfigure ( )
virtual

Re-configures a component, depending on e.g. parameter values or external files.

This function can be optionally overloaded if it is needed This function must explicitly be called by host environment whenever necessary, but always after calling configure().

◆ registerConditionalParameter()

void registerConditionalParameter ( const HString rName,
const HString rDescription,
std::vector< HString > &  rConditions,
int &  rValue 
)

Register a conditional parameter value so that it can be accessed for read and write.

Parameters
[in]rNameThe name of the parameter
[in]rDescriptionA description of the parameter
[in]rConditionsThe condition descriptions as a vector of text
[in]rValueA reference to the double variable representing the value, its address will be registered This function is used in the constructor of the Component modelling code to register member attributes as HOPSAN parameters

◆ removePort()

void removePort ( const HString rPortName)
protected

<

◆ renamePort()

HString renamePort ( const HString rOldname,
const HString rNewname 
)
protected

Rename a port.

Parameters
[in]rOldnameThe name of the the port to rename
[in]rNewnameThe desired new name of the the port
Returns
The actual new name of the port or old name if not renamed

◆ setInheritTimestep()

void setInheritTimestep ( const bool  inherit = true)

Set whether the component should inherit timestep from its system parent.

Parameters
[in]inheritTrue or False

◆ setMeasuredTime()

void setMeasuredTime ( const double  time)

Sets the measured time variable for the component. This is used to measure time requirements when sorting components for multicore purposes.

See also
getMeasuredTime()

◆ setName()

void setName ( HString  name)

Set the desired component name.

Parameters
[in]nameThe desired component name

Set the desired component name, if name is already taken in a subsystem the desired name will be modified with a suffix. If you set doOnlyLocalRename to true, the smart rename will not be attempted, avoid doing this as the component storage map will not be updated on name change This is a somewhat ugly fix for some special situations where we want to make sure that a smart rename is not attempted

◆ simulate()

void simulate ( const double  stopT)
virtual

Simulates the component from current simulation position to stopT using previously set timestep.

Parameters
[in]stopTStop time

Reimplemented in ConditionalComponentSystem, and ComponentSystem.