#include #include #include #include #include using namespace std; #include #include void myMessageHandler( const SMACseMsgHandlerSeverity severity, const char* msg, const int numNodes, const int* nodes, const int numElements, const int* elements ); void myPutField( const char* fieldType, const char* meshName, const char* collectionName, const unsigned int nMembers, int* members, void* targetBuffer, void* userData ); /* SimulatorA input: displacement output: interface force */ int main(int argc, char** argv) { // Process command line rguments if ( (argc < 6) || (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1],"-help") == 0) ) { cout << "Usage: SimulatorA -csedirector host:port -alpha alpha -beta beta" << endl; exit(0); } string cseDirector = ""; double alf = 0.0; double bet = 0.0; unsigned int iArg = 1; while (iArg < argc) { string argument = argv[iArg++]; if (argument == "-csedirector") { cseDirector = argv[iArg++]; } else if (argument == "-alpha") { alf = atof(argv[iArg++]); } else if (argument == "-beta") { bet = atof(argv[iArg++]); } } const double g = 9.81; // gravity // Monolithic System Parameters const double mass = 2.5; // total mass for monolithic system const double stiff = 35000.0; // total stiffness for monolithic system const double w2 = stiff/mass; // radial freq squared const double Period = 6.2832/sqrt(w2); // period const double freq = 1/Period; // frequency // output file ofstream outFile; outFile.open("DualMassSpring_SystemA.dat"); // This file header is for SIMULIA QC purpose outFile << "# File for comparison, place numerical delimiter within [] below, default delimiter is a blank space" << endl; outFile << "# Give tolerance (absolute, not percentage) for floats; default is ZERO" << endl; outFile << "# Give minimum float to be considered, values less than are considered to be ZERO" << endl; outFile << "# The DELIMITER, TOLERANCE, and MINIMUM FLOAT definitions may be reset within the file" << endl; outFile << "DELIMITER = [,]" << endl; outFile << "TOLERANCE = .01" << endl; outFile << "MINIMUM FLOAT = 1.0E-4" << endl; double lambda[3] = { 0., 0., 0. }; // Interface force double u_0[3] = { 0., 0., 0. }; double v_0[3] = { 0., 0., 0. }; double a_0[3] = { -g, 0., 0. }; double u_1[3] = { 0., 0., 0. }; double v_1[3] = { 0., 0., 0. }; double a_1[3] = { -g, 0., 0. }; /* Initialize the CSE Runtime enviornment. The runtime enviornment are the CSE libraries linked to the client application code. It needs to be called prior to calling any CSE routine */ SMACseInitialize ( ); /* Establish connection with CSE Director process and other client applications participating in the co-simulation. */ string codeName = "SimulatorA"; string jobName = "SimulatorA"; string workDir = ".//"; const int timeout = 3600; double startTime = 0.0; SMACseConnectStatus cStatus; cStatus = SMACseConnect( codeName.c_str( ), // defines the component in CSS Configuration file jobName.c_str( ), // defines the componet instance in CSS Configuration FIle workDir.c_str( ), // defines where diagnostics are writen by CSS cseDirector.c_str( ), // host:port of CSS Director managing simulation timeout, // timeout in seconds startTime, // start time myPutField, // function pointer to putField function &lambda[0], // user data passed to putField as an opaque object myMessageHandler ); // function pointer to message handler if (cStatus == SMACseConnectStatus_failure) { myMessageHandler(SMACseMsgHandler_ErrorMessage, "An error occured during the connection process.\n", 0, NULL, 0, NULL); exit(1); } else { cout << "Succesfully connected with CSE Director and other client process." << endl; } /* Create and register mesh for point coupling. The interface is at (0, 0, 0) */ SMACseMesh* mesh = SMACseCreateMesh("Mesh A"); const unsigned int nNodes = 1; unsigned int nLabels[nNodes] = { 1 }; double coords[3*nNodes] = { 0.0, 0.0, 0.0 }; SMACseAddNodeCollection( mesh, "Node Collection", SMACseDim_3d, nNodes, nLabels, coords); SMACseRegisterMesh(mesh); /* Define and register fields */ SMACseCreateFieldDefinition( "force", SMACseFieldPos_atNode, SMACseFieldAlgType_Vector3d , SMACseFieldDataType_Double ); SMACseCreateFieldDefinition( "displacement", SMACseFieldPos_atNode, SMACseFieldAlgType_Vector3d , SMACseFieldDataType_Double ); SMACseRegisterIncomingField( "displacement", "Mesh A" ); SMACseRegisterOutgoingField( "force", "Mesh A" ); /* Finalize and register the co-simulation model */ SMACseRegisterModel( ); /* Initial condition exchange */ SMACseSolutionStatus status; status = SMACseInitialConditionsStart( ); // putField is called by CSS to extract initial fields status = SMACseInitialConditionsEnd( ); // receive initial fields from remote client SMACseGetFieldStatus getFieldStatus; getFieldStatus = SMACseGetField("displacement", "Mesh A", "", nNodes, nLabels, 0.0, (void*) u_1, 3*nNodes*sizeof(double),NULL); outFile << 0.0 << ", " << u_1[0] << ", " << v_1[0] << ", " << a_1[0] << endl; double t = startTime; double dt = Period/20.0; // integrate a period with 20 time points double targetTime = 0.0; int isLockstep = 0; int controls[1] = { 0 }; while (SMACseGetTargetTime(t, dt, &targetTime, &dt, &isLockstep, &controls[0]) != SMACseSolutionStatus_CosimulationEventExceeded) { status = SMACseNotifyStart( ); // Newmark method for constant average acceleration const double gamma = 0.5; // governs damping const double beta = 0.25; // governs stiffness const double a0 = 1.0/(beta*dt*dt); const double a1 = gamma/(beta*dt); const double a2 = 1.0/(beta*dt); const double a3 = 1.0/(2.0*beta) - 1.0; const double a4 = gamma/beta - 1.0; const double a5 = dt/2.0*((gamma/beta) -2.0); const double a6 = dt*(1.0-gamma); const double a7 = gamma*dt; // Copy state at n+1 to n before starting next increment for (unsigned int kdir=0; kdir<3; kdir++) { u_0[kdir] = u_1[kdir]; v_0[kdir] = v_1[kdir]; a_0[kdir] = a_1[kdir]; } do { // // iterations // getFieldStatus = SMACseGetField("displacement", "Mesh A", "", nNodes, nLabels, t+dt, (void*) u_1, 3*nNodes*sizeof(double),NULL); for (unsigned int kdir=0; kdir<3; kdir++) { a_1[kdir] = a0*(u_1[kdir]-u_0[kdir])-a2*v_0[kdir]-a3*a_0[kdir]; v_1[kdir] = v_0[kdir]+a6*a_0[kdir]+a7*a_1[kdir]; lambda[kdir] = -bet*mass*(a_1[kdir]+alf/bet*w2*u_1[kdir]+g); } } while (SMACseNotifyIteration(t+dt, 0) == SMACseSolutionStatus_DoFurtherIterations); outFile << t+dt << ", " << u_1[0] << ", " << v_1[0] << ", " << a_1[0] << endl; status = SMACseNotifyEnd(t+dt); t += dt; } /* Disconnect and finalize */ outFile.close( ); SMACseDisconnect( ); SMACseFinalize( ); return 0; } /* The message handler is a callback function. It is a function written for your application, but is called by the co-simulaion services to report any messages, warning or errors. In this case we redirect the message to stderr */ void myMessageHandler( const SMACseMsgHandlerSeverity severity, const char* msg, const int numNodes, const int* nodes, const int numElements, const int* elements ) { switch (severity) { case SMACseMsgHandler_InformationalMessage: // -- Informational Message, continue execution cerr << "Simulator B: " << msg << endl; break; case SMACseMsgHandler_WarningMessage: // -- Warning Message, continue execution cerr << "Simulator B: " << msg << endl; break; case SMACseMsgHandler_ErrorMessage: // -- Error Message, terminate execution cerr << "Simulator B: " << msg << endl; SMACseDisconnect( ); SMACseFinalize( ); exit(1); break; } } /* myPutField is a callback function. It is a function written for your application, but is called by the co-simulation services to to copy field data from the client's memory to the CSS memory buffer. It will be called for every field exported. */ void myPutField( const char* fieldType, const char* meshName, const char* collectionName, const unsigned int nMembers, int* members, void* targetBuffer, void* userData ) { // user data was registered as interface force, lambda, in SMACseConnect double* force = (double *) userData; double* target = (double *) targetBuffer; if (strcmp(fieldType,"force") == 0) { for (unsigned int k=0; k