#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 ); /* Struct containing displacements, velocity and accelerations at the start and end of increment. */ struct myUserDataStruc { double u_n0[3], u_n1[3]; // displacement at n and n+1 double v_n0[3], v_n1[3]; // velocity at n and n+1 double a_n0[3], a_n1[3]; // acceleration at n and n+1 }; /* Simulator B input: interface force output: displacement */ int main(int argc, char** argv) { // Process command line rguments if ( (argc < 6) || (strcmp(argv[1], "-h") == 0) || (strcmp(argv[1],"-help") == 0) ) { printf("Usage: SimulatorB -csedirector host:port -alpha alpha -beta beta\n."); 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 // Sub-system B terms const double M = 1.0; const double C = 0.0; const double K = (1.0-alf)/(1.0-bet)*w2; // output file ofstream outFile; outFile.open("DualMassSpring_SystemB.dat"); ofstream iterFile; iterFile.open("DualMassSpring_SystemB_iter.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; // Initial conditions double fb[3] = { g, 0., 0. }; // Body force double lambda[3] = { 0., 0., 0. }; // Interface force double Feff[3] = { 0., 0., 0. }; // Effective force for Newmark time integration struct myUserDataStruc db; // user data passed to putField for (unsigned int k1=0; k1<3; ++k1) { db.u_n0[k1] = 0.0; db.v_n0[k1] = 0.0; db.a_n0[k1] = 0.0; db.u_n1[k1] = 0.0; db.v_n1[k1] = 0.0; db.a_n1[k1] = 0.0; } db.a_n0[0] = -g; db.a_n1[0] = -g; /* 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 = "SimulatorB"; string jobName = "SimulatorB"; 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 &db, // 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 B"); unsigned int nNodes = 1; unsigned int nLabels[] = { 1 }; double coords[ ] = { 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 ); SMACseRegisterOutgoingField( "displacement", "Mesh B" ); SMACseRegisterIncomingField( "force", "Mesh B" ); /* 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 field from remote client double t = startTime; double dt = Period/20.0; // integrate a periord with 20 time points double targetTime = 0.0; int isLockstep = 0; int controls[1] = { 0 }; outFile << 0.0 << ", " << db.u_n0[0] << ", " << db.v_n0[0] << ", " << db.a_n0[0] << endl; while (SMACseGetTargetTime(t, dt, &targetTime, &dt, &isLockstep, &controls[0]) != SMACseSolutionStatus_CosimulationEventExceeded) { // // increments // // Newmark method for constant average acceleration const double gamma = 0.5; const double beta = 0.25; 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++) { db.u_n0[kdir] = db.u_n1[kdir]; db.v_n0[kdir] = db.v_n1[kdir]; db.a_n0[kdir] = db.a_n1[kdir]; } int iter = 0; status = SMACseNotifyStart( ); do { // // iterations // // Compute effective stiffness double Keff = K + a0*M + a1*C; // get interface constraint force SMACseGetField("force", "Mesh B", NULL, nNodes, nLabels, t+dt, (void*) lambda, 3*nNodes*sizeof(double), NULL); Feff[0] = -fb[0] + lambda[0]/((1-bet)*mass) + M*(a0*db.u_n0[0]+a2*db.v_n0[0]+a3*db.a_n0[0]) + C*(a1*db.u_n0[0]+a4*db.v_n0[0]+a5*db.a_n0[0]); Feff[1] = 0.0; Feff[2] = 0.0; // Compute displacements, velocity and acceleration at t+dt db.u_n1[0] = Feff[0]/Keff; db.u_n1[1] = 0.0; db.u_n1[2] = 0.0; db.a_n1[0] = a0*(db.u_n1[0]-db.u_n0[0])-a2*db.v_n0[0]-a3*db.a_n0[0]; db.a_n1[1] = 0.0; db.a_n1[2] = 0.0; db.v_n1[0] = db.v_n0[0]+a6*db.a_n0[0]+a7*db.a_n1[0]; db.v_n1[1] = 0.0; db.v_n1[2] = 0.0; iter++; iterFile << "Time = " << t+dt << " Iter " << iter << ": u = " << db.u_n1[0] << endl; } while (SMACseNotifyIteration(t+dt,0) == SMACseSolutionStatus_DoFurtherIterations); status = SMACseNotifyEnd(t+dt); outFile << t+dt << ", " << db.u_n1[0] << ", " << db.v_n1[0] << ", " << db.a_n1[0] << endl; iterFile << endl; t += dt; } /* Disconnect and finalize */ outFile.close( ); SMACseDisconnect( ); SMACseFinalize( ); exit(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 ) { double* target = (double *) targetBuffer; struct myUserDataStruc *myDb = (struct myUserDataStruc*) userData; if (strcmp(fieldType,"displacement") == 0) { memcpy(targetBuffer, (void *) myDb->u_n1, nMembers*3*sizeof(double)); // // or copying field value by value // // for (unsigned int k=0; k