Deep Dive into the Results Provider Template

The results provider templates provides a good starting point to develop your own results provider.

This page discusses:

The template illustrates the necessary calls to the SIMULIA Co-Simulation Services. You need to provide the coding to read your results file, manage your data, and cast your data to what the SIMULIA Co-Simulation Services require.

To obtain the results provider template, follow the instructions in Results Provider Template. The template is in C++ and uses the C++ Template Library (STL). The C++ STL library makes it easy to manipulate strings and read from a file.

Parsing the Command Line Arguments

When Abaqus launches the results provider, the Abaqus execution driver spawns the results provider with predefined arguments (see Command Line Arguments). We recommend that you adhere to these arguments such that you can automate the execution process from Abaqus.

Example

The following example illustrates how to read and parse the command line arguments:

int main(int argc, char** argv) {

               :
  
   // Process command line arguments
   //
   // The Results Provider is invoked by Abaqus with the following arguments. We recomend using
   // these arguments so that Abaqus can automatically start the Results Provider. If your
   // choose different arguments, you must start the Results Provider and CSS Director manually.
   //   
   int timeout        = 3600;                 // timeout in seconds; exist and error if process do not connect after timeout
   string codeName    = "myResultsProvider";  // INSTRUCTIONS: replace with executable name without extension. 
   string workDir     = ".//";                // CSE diagnostic files will be written to the current work directory
   string cssDirector = "";                   // "host:port" of CSS director 
   string jobName     = "";                   // This is the results file name without it extension
   string specFile;                           // Results provider specification file generate by Abaqus
   string resultsFile;                        // Results file name with file extension.   
   
   if ( (argc < 10) || (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1],"-help") == 0) ) {
      cout << "Usage: myResultsProvider -job <jobname> -file <results filename> -spec <spec filename> -csedirector control_machine:port -timeout seconds" << endl;
      exit(0);
   } 
   
   int iArg = 1;
   while (iArg < argc) {
     string argument = argv[iArg++];
     if (argument == "-csedirector") {
        cssDirector = argv[iArg++];  
     } else if (argument == "-timeout") {  
        timeout = atof(argv[iArg++]);  
	 } else if (argument == "-job") {  
        jobName = argv[iArg++];   
	 } else if (argument == "-file") {  
        resultsFile = argv[iArg++];   
	 } else if (argument == "-spec") {  
        specFile = argv[iArg++]; 
     } 
   } 

             :

}

Opening the Results File and Defining Your Database

After parsing the command line arguments, you should check if the results file exists. If it does not exist, report an error and exit.

The template uses a pointer myDb to point to any object that contains information about your results file. The myDb object can be as simple as a file pointer; or it can be more complex, containing information about your database.

Example

The following example illustrates how to define your database structure:

 
   // INSTRUCTIONS: You can create any structure/class to provide access to
   // your data. Assign this to an opaque handle, which will be passed to SMACSeConnect.  
   // You will then be able to access your database in the myPutFieldFnWithTime and 
   // myPutFramePtsForField services called by the CSE.
   void* myDb = static_cast<void*>(....);   

Initializing the SIMULIA Co-Simulation Services

You must initialize the SIMULIA Co-Simulation Services before calling any services. Use the SMACseInitialize function to initialize the SIMULIA Co-Simulation Services (see Initialization). Typically the results provider is a serial process, and you do not need to provide an implementation of the DMP interface.

Establishing the Connections

Establish the connection with the SIMULIA Co-Simulation Services Director and Abaqus. Use the SMACseConnect function to establish the connection (see Establishing Connections) .

Example

The following example illustrates how to establish the connections:

    // Initialize the CSS Runtime environment
   SMACseInitialize(NULL);
   
   // Connect Results Provider with CSS Director and Abaqus 
   double startTime   = 0.0;
   int cStatus = SMACseConnectStatus_failure;   
   cStatus = SMACseConnect( codeName.c_str(), jobName.c_str(), workDir.c_str(), 
                            cssDirector.c_str(), timeout,
                            startTime, NULL, myDd, myMessageHandler );
   if ( cStatus != SMACseConnectStatus_success ) {
      cerr << "Error: Failed to connect with CSS Director." << endl; 
      exit(1);
   }

Registering the Sequential Callback Services

After establishing the connection, you must register the putFieldFnWithTime and putFramePtsForField callback functions.

Use the SMACseRegisterSequentialCallbacks function to register the functions.

Example

The following example illustrates how to register the sequential callback services:

// Register CSS callback functions 
SMACseRegisterSequentialCallbacks(PutFieldFnWithTime, PutFramePtsForField);  

Registering the Interface Model

Register the interface regions and the outgoing fields as discussed in Defining the Interface Model. The region and field names that you must register are defined in the results provider specification file. The specification file is an XML document. You can either parse the XML document yourself, or you can use the SMACseQueryRequiredFields utility to return a comma-separated string containing the field type and region name separated by a period.

For each region, you define and register a mesh. For each of the fields, you define the field type and register it as an outgoing field. When all the regions and outgoing fields are registered, you must register the interface model using SMACseRegisterModel.

Extracting and Sending Field Data

After registering the model, the results provider is ready to extract and send field data.

First, call SMACseInitialConditionsStart and SMACseInitialConditionsEnd to send the initial fields to the downstream application. Then, call SMACseProvideResults. SMACseProvideResults is a blocking call and provides the results to the downstream application as the analysis progresses. It returns once the downstream analysis completes. During this call, the putFieldFnWithTime and putFramePtsForField functions are called, requesting updated information when required.

Example

The following example illustrates the main body exchanging field data:

   SMACseSolutionStatus  cssStatus;                        
   cssStatus = SMACseInitialConditionsStart( );   
   cssStatus = SMACseInitialConditionsEnd( );     
   cssStatus = SMACseProvideResults();

Disconnecting and Finalizing the SIMULIA Co-Simulation Services

When SMACseProvideResults returns, you can disconnect and finalize theSIMULIA Co-Simulation Services, and then you can end the results provider.

Example

The following example illustrates how to disconnect and finalize the SIMULIA Co-Simulation Services:

   SMACseDisconnectExt(“Results provider terminated succefully.” );
   SMACseFinalize ( );

putFieldFnWithTime

The putFieldFnWithTime function extracts data from your results file and copies it into a SIMULIA Co-Simulation Services memory buffer to be transferred and mapped to the downstream application. The SIMULIA Co-Simulation Services call the putFieldFnWithTime function for each field type, mesh region, and collection for a particular analysis time.

Example

The following example illustrates the use of the putFieldFnWithTime function:

//
// PutFieldFnWithTime
//
// PutFieldFnWithTime service: This function is called by the CSE. It extracts field data from the  
// client and copies it to CSE memory for export. Field data is to be copied in the order the 
// members are defined in a collection when you define your mesh. 
//
void myPutFieldFnWithTime(const char* fieldType, const char* meshName, const char* collectionName,
	                    unsigned int nMembers, int* members, double* time, void* userData, 
                          void* targetBuffer)
{
    // INSTRUCTIONS: 
    // 
    // Recast userData to your dabase structure 
    double* bufferT = static_cast<double*>(targetBuffer);
    
    vector<double> &timeFrames = mydbPtr->timeFrames;
    int numFrames = timeFrames.size();
    
    // if time before first time point, use first time point
    if ( time <= timeFrames[0] )
    {	      
        // INSTRUCTIONS: copy field data for time point 0 from your memory
        //               to the CSS memory buffer. Field data is in order members
        //               are register in a collection.
        return;
    }    
    
    // if time is after last time point, use last time point
    if ( time >= timeFrames[numFrames-1] )
    {
        // INSTRUCTIONS: copy field data for the last time point from your memory
        //               to the CSS memory buffer. Field data is in order members
        //               are register in a collection.        
        return;
    }
    
    // if at time point or between time frame points
    for (int  i =0; i < numFrames - 1; ++i )
    {        
        if (time == timeFrames[i] ) {
        // INSTRUCTIONS: copy field data at time point from your memory
        //               to the CSS memory buffer. Field data is in order members
        //               are register in a collection.               
            return;
        }
        else if ( time > timeFrames[i] &&  time < timeFrames[i+1])
        {
        // INSTRUCTIONS: interpolate and copy field data from your memory
        //               to the CSS memory buffer. Field data is in order members
        //               are register in a collection.                   
            return;           
        }
    }    
}

putFramePtsForField

The putFramePtsForField function extracts the time points in the database for a given field. You can provide a maximum of two time points. Provide one time point if the time of the results frame matches the given time. Otherwise, provide two time points that sandwich the given time.

Example

The following example illustrates the use of the putFramePtsForField function:

//
// PutFramePtsForField
//
// PutFramePtsForField service is called by the CSS. For the given field and time, it extracts the time frames 
// points the database. A maximum of only two time frame points can be provided if time falls 
// between two time frames. 

void myPutFramePtsForField(const char* iFieldType, const char* iMeshName, const int iMaxNumberOfFrames,
	                    int* oNumberOfFrames, double* oframeTimePoints, const double* time, 
                           void* userData) 
{

    // Provide one time point if the time of results frame matches the given time (*oNumberOfFrames =1),
    // otherwise, provide two time points that sandwich the given time (*oNumberOfFrames = 2).
    // Fill the oframeTimePoints with time points
    
    // Recast opaque pointers to proper datapointers; useData was registered in SMACseConnect
    struct DataBaseStruc *mydbPtr =  static_cast<struct DataBaseStruc*>(userData);
    
    vector<double> &timeFrames = mydbPtr->timeFrames;
    int numFrames = timeFrames.size();
    
    double iTime = *time; 
    *oNumberOfFrames = 0; 
    
    // if time before first time point, use first time point
    if ( iTime <= timeFrames[0] )
    {	
        *oNumberOfFrames = 1;
        oframeTimePoints[0] = timeFrames[0];
        if (debug) cout << "myPutFramePtsForField: time=" << iTime << " Time Frame = " << 0 << endl;
        return;
    }
    
    // if time is after last time point, use last time point
    if ( iTime >= timeFrames[numFrames-1] )
    {
        *oNumberOfFrames = 1;
        oframeTimePoints[0] = timeFrames[numFrames-1];
        if (debug) cout << "myPutFramePtsForField: time=" << iTime << " Time Frame = " << numFrames-1 << endl;
        return;
    }
    
    // provide time points at, or before and after   
    for (int  i =0; i < (numFrames - 1); ++i )
    {
        double t = timeFrames[i];
        if (abs(iTime - t) < 1.e-6) {
            *oNumberOfFrames = 1;
            oframeTimePoints[0] = t;
            if (debug) cout << "myPutFramePtsForField: time=" << iTime << " Time Frame = " << i << endl;
            return;
        } else if ( iTime > timeFrames[i] &&  iTime < timeFrames[i+1]) {
            *oNumberOfFrames = 2;
            oframeTimePoints[0] = timeFrames[i];
            oframeTimePoints[1] = timeFrames[i+1];
            if (debug) cout << "myPutFramePtsForField: time=" << iTime << " Time Frame = " << i << ", " << i+1 << endl;
            return;           
        }
    }
}