Preparing Your Code for Co-Simulation with Simcenter STAR-CCM+
Your program couples with Simcenter STAR-CCM+ through the Simcenter STAR-CCM+ API server, which is a Simcenter STAR-CCM+ simulation that represents your program. You set up and communicate with the Simcenter STAR-CCM+ API server through the Co-Simulation API library. The Simcenter STAR-CCM+ API server couples with other Simcenter STAR-CCM+ simulations using Simcenter STAR-CCM+ to Simcenter STAR-CCM+ Co-Simulation.
To set up and communicate with the Simcenter STAR-CCM+ API server, prepare your code following the steps below. Each step includes a code example that uses the latest version of the API.
-
Include one of the Co-Simulation API header files in your program.
See Header Files. API versions based on the function suite structure are not suitable for coupling with versions of Simcenter STAR-CCM+ prior to v11.04.
-
For dynamic loading of the Co-Simulation API library, also include the
LibraryLoader.h and
LibraryLoader.cpp files that are provided with the SpindleValve example program.
- If your program is written in C++, you can directly use the LibraryLoader.h and LibraryLoader.cpp files in your program.
- If your program is written in Fortran or C, you can use these files with a C wrapper.
See Example Program and API Library. -
Define pointers to the API structure and the function suites:
StarccmplusCoSimulationApiStruct const * apiStruct; StarccmplusApiSuiteV8 const * apiSuite; StarccmplusFactorySuiteV8 const * factorySuite; StarccmplusPropertiesSuiteV8 const * propertiesSuite;
-
Define the
messageHandler
andfillContainerHandler
callback functions that Simcenter STAR-CCM+ uses to call back into your program:void messageHandler(StarccmplusMessageType const msgType, char const* message) {see SpindleValve program for example message handler}; void fillContainerHandler(int containerIndex, char const* requirement) {see SpindleValve program for example fill container handler}; StarccmplusHandlerSuiteV8 handlerSuite ={ messageHandler, fillContainerHandler};
-
Load the Co-Simulation API library.
apiLib.load();
-
Get a pointer to the API structure defined in
StarccmplusCoSimulationApiStruct.h.
apiLib.getApiStruct(&apiStruct)
-
Verify that the Co-Simulation API version in use is supported.
The Simcenter STAR-CCM+ version that is launched is a user choice, that is, your program cannot use the API to load specific versions of Simcenter STAR-CCM+. Typically, your program verifies whether the version of Simcenter STAR-CCM+ that the user launches is compatible with the API version in use, and issues an error when it is not compatible.
apiStruct->isApiVersionSupported( API_VERSION, &versionsArray, &versionsArraySize);
The API queries Simcenter STAR-CCM+ for a list of supported API versions, then matches the input API_VERSION against this list:- If there is no match, the API call returns 1. In this case, unless your program can handle falling back to a different version of the API, you cannot couple with this version of Simcenter STAR-CCM+.
- If there is a match, the API call returns 0. In this case, your program can couple with this version of Simcenter STAR-CCM+.
Although you cannot use the API to load specific versions of Simcenter STAR-CCM+, you can take into account the Simcenter STAR-CCM+ version in order to determine the capabilities available in Simcenter STAR-CCM+ (for example, to determine whether implicit coupling is available). -
Get pointers to the function suites.
apiStruct->setExternalCodeFunctions( API_VERSION, StarccmplusCoSimulationHandlerSuiteName, &handlerSuite);
apiStruct->getStarccmplusFunctions( API_VERSION, StarccmplusCoSimulationApiSuiteName, (void const * *)&apiSuite);
apiStruct->getStarccmplusFunctions( API_VERSION, StarccmplusCoSimulationFactorySuiteName, (void const * *)&factorySuite);
apiStruct->getStarccmplusFunctions( API_VERSION, StarccmplusCoSimulationPropertiesSuiteName, (void const * *)&propertiesSuite);
After these calls, you make all subsequent calls using the function suites. -
Initialize the library.
apiSuite->initialize(argc,argv);
-
Set up the
Simcenter STAR-CCM+ API Server:
-
Notify
Simcenter STAR-CCM+ that the mesh is ready for use:
apiSuite->notifyOutgoingMeshesReady();
-
Specify the solver settings and run loop. Include the solver time-step and either the coupling time (for explicit coupling) or the number of inner iterations per exchange (for implicit coupling).
-
When complete, finalize the Co-Simulation API library.
apiSuite->finalize();
-
Terminate the API library.
apiLib.close();