Marine systems simulation
Creating a new SimObject

SimObjects are usually compiled into a shared library (dll on Windows). They can therefore be compiled independent of the fhSim core. The procedures for creating a new simObject are presented here, while the page Creating a new SimObject library shows how to create a new shared library with one or more simObjects.

Recipe for creating a new SimObject

A new simObject is simply a new class inheriting from CSimObject. The clue to adding a new simobject is that the dll project that is a library of SimObjects need to access its definition either through a .lib file, or through its source files. The procedure described here shows how to access it through a .lib file. The simplest way of creating a new simObject as follows:

• Copy the source and header files of an existing simObject which resembles the desired simObject as much as possible, and place the copies in the correct source directory with the desired name.
• Assuming that the simObject is to be built into an existing shared library, from an existing directory: Add the source and header files to the project. If CMake is used, this means adding the names in the correct place in the file CMakeLists.txt of the project.
• If Visual Studio is used, either build the project or run the cmake configure command, to assure that the solution file is updated.
• Change the include guard name of the header file.
• Replace all occurences of the old class name with the new one in both source and header file.
• Modify the class to behave as desired.
• Compile the library.
• Add a line in the .cpp file which tells which name in the input file should correspond to the new SimObject class:

ADD_SIMOBJECT_REF(<Class name>, <Object reference name>);

• Update the input file to use the new SimObject. Remember both the instantiation and the mapping of input ports.

The most fundamental methods to implement when creating a new SimObject

Constructor
The constructor is responsible for reading the parameters belonging to the SimObject from the input file and defining the interface of the SimObject, in terms of:
• Input ports
• Output ports
• States
OutputPortFunctions
Each output port is implemented as a method responsible for returning a pointer to the data structure containing the value of the output port. If more than one output port depends on running the same code for updating some data, it is recommended to put these calculations in a common method, which is called from all port functions, and to start the method by a test to check if the method have already been run in this time step.
OdeFcn
This method is responsible for calculating the derivative of the states of the SimObject.

Example SimObjects

To better understand the implementation of SimObjects, view the implementation of a linear spring and a mass object. The following files are disclosed:

Including the new SimObject into a SimObject library

To be able to use the new SimObject, it must be included in a SimObject library. This is explained in Creating a new SimObject library.