Getting Started
Creating a Simulator Project
There are two ways to create simulation models in CDP Simulator:
- Writing C++ code - creating a library project and inheriting from the DynamicSimComponent class. The basics are:
- Create a library project and add a code-based simulator component model to it.
- Create a system project and add a simulator application to it.
- Add the simulator component created in the first step to the simulator application.
- Run and test the application.
The code-based approach is recommended for models with complicated logic or when the highest performance is needed.
- The no-code approach - using the SimComponent and connecting blocks like Integrators and CDPOperators in the Configure mode Block Diagram. The basics are:
- Create a system project and add a simulator application to it.
- Add the SimComponent and navigate into it.
- Build your logic by adding blocks like Integrators and CDPOperators and connecting them in the Block Diagram.
- Run and test the application.
The no-code approach is recommended for simple models or fast prototyping where creating a library project and inheriting from the DynamicSimComponent class would be too cumbersome. The no-code approach typically runs 2 to 5 times slower than the code-based approach which in practice will not be a problem unless the simulation falls behind real-time and the NotRealTime alarm is triggered.
In this getting started guide, we will cover both approaches. First, we will create a simple code-based simulation library and system. Next, we will add a no-code simulation model to the system.
Add Code-based Simulator Component to Your Library
First, create a library as described in How to Create a Library manual.
To add a DynamicSimComponent model:
Right-click on a library
Select Add new... option in the context menu.
Select CDP from the left column.
Select Simulator Component Model from the middle column.
Click Choose...
- Type in the name (e.g. MySimulator and a description for the class.
- Add the created files to your version control system or just click Finish.
A simulator component model with the files MySimulator.cpp and MySimulator.h have been created. As an example, they contain one SimSignal and two StateVariables which can be removed or modified as needed. The MySimulator::EvaluteDiffEquations()
and MySimulator::EvaluateAlgebraicEquations()
methods have been filled with sample code that calculates velocity and displacement based on acceleration. This should give an idea of how to use CDPSim with differential or algebraic equations.
Note: It is not necessary to implement both EvaluteDiffEquations()
and EvaluateAlgebraicEquations()
methods. If only one type of equation is used, the other method can be removed and the SolverType property adjusted accordingly. It is recommended to use EvaluteDiffEquations()
when possible as it is more efficient.
When done, build the library.
Add Simulator Application
First, create a system as described in How to Create a System manual. The default application added under the system can be removed if not needed for other purposes.
Next, add a simulator application:
Right-click on the system name in the Project tree
Select Add new... option in the context menu.
Select CDP from the left column.
Select Simulator Application from the middle column.
Click Choose...
- Set the name for the application and click Next.
- Add the created files to your version control system or just click Finish.
The simulator application is a special kind of CDP application:
- Simulator components like the MySimulator created in the last step can only be added into simulator application. It is not possible to drag them into a regular CDP application.
- Simulator application includes the SimulatorManager component which holds many configuration options for the simulation.
- It includes Properties like TimeStep and Autostart. The TimeStep sets the simulation frequency for all simulator components within the application. The Autostart property (default true) decides if simulation should start automatically or not.
- It has Start, Stop and Reset Messages which control the simulation.
- It sets the integration algorithm used to evaluate differential and/or algebraic equations in simulator components. See more in the Choosing an Integration Method documentation.
To test the MySimulator component, add it to your simulator application using the Resource tree.
Create a No-code Simulation Model
Next, we will create a similar no-code simulation model that calculates displacement based on acceleration. It is recommended to go to the Configure mode Block Diagram tab to build a no-code model.
We are looking to use a SimComponent that has one input SimSignal for acceleration a and one output SimSignal for displacement x.
To create a simple integrator that calculates the displacement of an object moving with a constant acceleration, follow these steps:
- In the simulator application, add a CDPSim.SimComponent from the Resource tree.
- Navigate into the SimComponent.
- Add input and output signals to the SimComponent:
- Add integrators to the SimComponent which will integrate the acceleration to first get the velocity and then the displacement:
The final configuration will look like this:
How to Run the Tutorial
To run the tutorial from CDP Studio, select Configure mode, right-click on the system project and select Run & Connect. See the Running and Connecting to the System tutorial for more information.
To test the code-based simulator component, in Configure mode, open the MySimulator component. If the template code was unchanged, it is possible to modify the SimSignal a (acceleration) and then the StateVariables x (displacement) and v (velocity) will be affected.
To test the no-code simulation model, in Configure mode, open the SimComponent. Modify the SimSignal a (acceleration) and notice how the displacement x changes.
For further information, see the CDP Simulator Examples, Configuration Manual and Choosing an Integration Method.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.