diff --git a/plugins/UnityMol/UMolMDDriver.cpp b/plugins/UnityMol/UMolMDDriver.cpp index f81b18a..9abf546 100644 --- a/plugins/UnityMol/UMolMDDriver.cpp +++ b/plugins/UnityMol/UMolMDDriver.cpp @@ -100,6 +100,8 @@ API int MDDriver_getNbCustomInt(MDDriverAdapter *mddinstance); API int MDDriver_getPositions(MDDriverAdapter *mddinstance, float *verts, int nbParticles); API int MDDriver_getCustomFloat(MDDriverAdapter *mddinstance, char * datanamefloat, float *customfloat, int nbfloat); API int MDDriver_getCustomInt(MDDriverAdapter *mddinstance, char * datanameint, int * customint, int nbint); +API void MDDriver_sendCustomInt(MDDriverAdapter *mddinstance, int nbint, const char *datanameint, int *customint); +API void MDDriver_sendCustomFloat(MDDriverAdapter *mddinstance, int nbfloat, const char *datanamefloat, float *customfloat); API void MDDriver_pause(MDDriverAdapter *mddinstance); API void MDDriver_play(MDDriverAdapter *mddinstance); API void MDDriver_setForces(MDDriverAdapter *mddinstance, int nbforces, int *atomslist, float *forceslist); @@ -205,14 +207,14 @@ int MDDriver_getNbParticles(MDDriverAdapter *mddinstance) { int MDDriver_getNbCustomFloat(MDDriverAdapter *mddinstance) { if (mddinstance->N_floats <= 0) { - IIMD_get_custom_float( &(mddinstance->datanamefloat), &(mddinstance->N_floats), (float **) & (mddinstance->customfloat) ); + IIMD_get_nb_custom_float(&(mddinstance->N_floats)); } return mddinstance->N_floats; } int MDDriver_getNbCustomInt(MDDriverAdapter *mddinstance) { if (mddinstance->N_ints <= 0) { - IIMD_get_custom_int( &(mddinstance->datanameint), &(mddinstance->N_ints), (int **) & (mddinstance->customint) ); + IIMD_get_nb_custom_int(&(mddinstance->N_ints)); } return mddinstance->N_ints; } @@ -234,20 +236,26 @@ int MDDriver_getPositions(MDDriverAdapter *mddinstance, float *coordinates, int int MDDriver_getCustomFloat(MDDriverAdapter *mddinstance, char * datanamefloat, float *customfloat, int nbfloat) { - IIMD_get_custom_float(&(mddinstance->datanamefloat), &(mddinstance->N_floats), (float **) & (mddinstance->customfloat) ); + int rc = IIMD_get_custom_float(&(mddinstance->datanamefloat), &(mddinstance->N_floats), (float **) & (mddinstance->customfloat) ); memcpy(customfloat, mddinstance->customfloat, nbfloat * sizeof(float)); memcpy(datanamefloat, mddinstance->datanamefloat, 8 * sizeof(char)); - return nbfloat; + return rc; } int MDDriver_getCustomInt(MDDriverAdapter *mddinstance, char * datanameint, int * customint, int nbint) { - IIMD_get_custom_float(&(mddinstance->datanameint) , &(mddinstance->N_ints), (float **) & (mddinstance->customint) ); + int rc = IIMD_get_custom_int(&(mddinstance->datanameint) , &(mddinstance->N_ints), (int **) & (mddinstance->customint) ); memcpy(customint, mddinstance->customint, nbint * sizeof(int)); memcpy(datanameint, mddinstance->datanameint, 8 * sizeof(char)); - return nbint; + return rc; } +void MDDriver_sendCustomInt(MDDriverAdapter *mddinstance, int nbint, const char *datanameint, int *customint) { + IIMD_send_custom_int(datanameint, &nbint, customint); +} +void MDDriver_sendCustomFloat(MDDriverAdapter *mddinstance, int nbfloat, const char *datanamefloat, float *customfloat) { + IIMD_send_custom_float(datanamefloat, &nbfloat, customfloat); +} void MDDriver_setForces(MDDriverAdapter *mddinstance, int nbforces, int *atomslist, float * forceslist) { if (mddinstance->nb_forces != nbforces || mddinstance->forces_list == NULL) { diff --git a/protocol/include/imd.h b/protocol/include/imd.h index 3afbbd2..0bd2c36 100644 --- a/protocol/include/imd.h +++ b/protocol/include/imd.h @@ -125,6 +125,12 @@ typedef struct float sizecellz; //!< Size of the cell on z axis } IMDGrid; +typedef struct +{ + char *dataname ; + int nb; + void *data; +} IMDCustomData; // Swap little <-> big endian diff --git a/protocol/include/imd_interface.h b/protocol/include/imd_interface.h index 457727a..d93cef5 100644 --- a/protocol/include/imd_interface.h +++ b/protocol/include/imd_interface.h @@ -70,7 +70,9 @@ extern imd_int64 imd_value; #define IIMD_send_custom_float iimd_send_custom_float_ #define IIMD_send_custom_int iimd_send_custom_int_ #define IIMD_get_custom_float iimd_get_custom_float_ +#define IIMD_get_nb_custom_float iimd_get_nb_custom_float_ #define IIMD_get_custom_int iimd_get_custom_int_ +#define IIMD_get_nb_custom_int iimd_get_nb_custom_int_ #define IIMD_get_coords iimd_get_coords_ #define IIMD_get_energies iimd_get_energies_ #define IIMD_get_grid iimd_get_grid_ @@ -89,7 +91,9 @@ extern imd_int64 imd_value; #define IIMD_send_custom_float iimd_send_custom_float #define IIMD_send_custom_int iimd_send_custom_int #define IIMD_get_custom_float iimd_get_custom_float +#define IIMD_get_nb_custom_float iimd_get_nb_custom_float #define IIMD_get_custom_int iimd_get_custom_int +#define IIMD_get_nb_custom_int iimd_get_nb_custom_int #define IIMD_get_coords iimd_get_coords #define IIMD_get_energies iimd_get_energies #define IIMD_get_grid iimd_get_grid @@ -241,6 +245,13 @@ Get custom float data */ extern int IIMD_get_custom_float(char ** dataname, int *n_floats, float **data); +/** +Get custom number of float data +@param n_floats number of floats +@return return number of floats +*/ +extern int IIMD_get_nb_custom_float(int *n_floats); + /** Get custom int data @param dataname string id of the data array @@ -250,6 +261,13 @@ Get custom int data */ extern int IIMD_get_custom_int(char ** dataname, int *n_ints, int **data); +/** +Get custom number of int data +@param n_ints number of ints +@return return number of ints +*/ +extern int IIMD_get_nb_custom_int(int *n_ints); + /** Terminate properly a MDDriver connection diff --git a/protocol/src/imd_interface.c b/protocol/src/imd_interface.c index 84048c9..0f5993c 100644 --- a/protocol/src/imd_interface.c +++ b/protocol/src/imd_interface.c @@ -440,7 +440,8 @@ void IIMD_treatprotocol() if ( (vmd_length == 0) || ( n_forces == 0) ) { - fprintf(IMDlog, "MDDriver > Warning no force \n"); + if (IMDmsg > 0) + fprintf(IMDlog, "MDDriver > Warning no force \n"); free( vmd_atoms ); free( vmd_forces ); vmd_atoms = 0; vmd_forces = 0; @@ -567,6 +568,11 @@ void IIMD_treatprotocol() fprintf(IMDlog, "MDDriver > Error reading custom float, killing connection\n"); SL_DelSocket(i_client); } + else + { + if ( IMDswap ) imd_swap4( (char *) vmd_custom_float, vmd_Nfloat); + vmd_new_custom_float = 1; + } } if (IMDmsg > 0) @@ -609,6 +615,11 @@ void IIMD_treatprotocol() fprintf(IMDlog, "MDDriver > Error reading custom int, killing connection\n"); SL_DelSocket(i_client); } + else + { + if ( IMDswap ) imd_swap4( (char *) vmd_custom_int, vmd_Nint); + vmd_new_custom_int = 1; + } } if (IMDmsg > 0) @@ -719,7 +730,7 @@ void IIMD_treatprotocol() if (vmd_Nint != 0) free( vmd_custom_int ); vmd_Nint=vmd_length; - vmd_custom_int = (int*) malloc( vmd_Nfloat * sizeof(int)); + vmd_custom_int = (int*) malloc( vmd_Nint * sizeof(int)); if (imd_recv_custom_int(sock, customdatanameint, vmd_Nint, vmd_custom_int)) { fprintf(IMDlog, "MDDriver > \n"); @@ -848,6 +859,8 @@ FILE *IIMD_init( const char *hostname, imd_int32 *mode, imd_int32 *IMD fprintf(IMDlog, "MDDriver > Interactive MD bind to /%s/%d \n", str, IMDport ); } + *IMDport_ = IMDport; + fflush( IMDlog ); // Listening incomming requests on sock @@ -1180,6 +1193,15 @@ void IIMD_send_custom_float ( const char * dataname, int *n_floats , float * dat imd_send_custom_float(clientsock, dataname, N, data); } + // From client side to server + if(sock && !IMDserver) + { + if ( IMDswap ) imd_swap4( data, N); + if (imd_send_custom_float(sock, dataname, N, data)) { + fprintf( IMDlog, "MDDriver > ---- Failed to send custom float in %s\n", __FUNCTION__); + } + } + if (IMDmsg >= 2) { fprintf(IMDlog, "MDDriver > \n"); @@ -1213,6 +1235,15 @@ void IIMD_send_custom_int ( const char * dataname, int *n_ints, int * data ) imd_send_custom_int(clientsock, dataname, N, data); } + // From client side to server + if(sock && !IMDserver) + { + if ( IMDswap ) imd_swap4( data, N); + if (imd_send_custom_int(sock, dataname, N, data)) { + fprintf( IMDlog, "MDDriver > ---- Failed to send custom int in %s\n", __FUNCTION__); + } + } + if (IMDmsg >= 2) { fprintf(IMDlog, "MDDriver > \n"); @@ -1398,6 +1429,30 @@ int IIMD_get_custom_float (char ** dataname, int *n_floats, float **data return rc; } +int IIMD_get_nb_custom_float (int *n_floats) { + int rc = 0; + + if (IMDmsg >= 1) + fprintf(IMDlog, "MDDriver > ---- Entering in %s\n", __FUNCTION__); + + if (vmd_new_custom_float) { + *n_floats = vmd_Nfloat; + rc = vmd_Nfloat; + + if (IMDmsg >= 2) { + fprintf(IMDlog, "MDDriver > \n"); + fprintf(IMDlog, "MDDriver > Get number of float (step %d) \n", vmd_energies.tstep); + fprintf(IMDlog, "MDDriver > ------------------------------------------ \n"); + fprintf(IMDlog, "MDDriver > Number of floats: %d\n", *n_floats); + } + } + + if (IMDmsg >= 1) + fprintf(IMDlog, "MDDriver > ---- Leaving %s\n", __FUNCTION__); + + return rc; +} + int IIMD_get_custom_int (char ** dataname, int *n_ints, int **data) { int rc = 0; @@ -1432,6 +1487,30 @@ int IIMD_get_custom_int (char ** dataname, int *n_ints, int **data) return rc; } +int IIMD_get_nb_custom_int (int *n_ints) { + int rc = 0; + + if (IMDmsg >= 1) + fprintf(IMDlog, "MDDriver > ---- Entering in %s\n", __FUNCTION__); + + if (vmd_new_custom_int) { + *n_ints = vmd_Nint; + rc = vmd_Nint; + + if (IMDmsg >= 2) { + fprintf(IMDlog, "MDDriver > \n"); + fprintf(IMDlog, "MDDriver > Get number of int (step %d) \n", vmd_energies.tstep); + fprintf(IMDlog, "MDDriver > ------------------------------------------ \n"); + fprintf(IMDlog, "MDDriver > Number of ints: %d\n", *n_ints); + } + } + + if (IMDmsg >= 1) + fprintf(IMDlog, "MDDriver > ---- Leaving %s\n", __FUNCTION__); + + return rc; +} + int IIMD_get_energies( IMDEnergies **energ_ ) { diff --git a/protocol/test_exe/clienttest.c b/protocol/test_exe/clienttest.c index b741cc9..b5b402b 100644 --- a/protocol/test_exe/clienttest.c +++ b/protocol/test_exe/clienttest.c @@ -141,14 +141,18 @@ int main() int atoms_list[N_FORCE];//Force float forces_list[N_FORCE*3]; - int nbfloat; - float * customfloat; - char * datanamefloat=(char *) malloc(sizeof(char)*8); - - int nbint; - int * customint; - char * datanameint=(char *) malloc(sizeof(char)*8); - + int nb = 5; + int intdata[5] = {6, 7, 8, 9, 10}; + float floatdata[5] = {6.0, 7.0, 8.0, 9.0, 10.0}; + + // cintCS : custom int Client Server + IMDCustomData float_send = {.dataname="cfloCS", .nb=nb, .data=&floatdata}; + IMDCustomData int_send = {.dataname="cintCS", .nb=nb, .data=&intdata}; + + IMDCustomData float_get, int_get; + float_get.dataname = (char *) malloc(sizeof(char)*8); + int_get.dataname = (char *) malloc(sizeof(char)*8); + int cont = 1; // Main loop control int ffreq = 10; int i; @@ -266,36 +270,36 @@ int main() } } - if ( IIMD_get_custom_float(&datanamefloat, &nbfloat, (float **) &customfloat) ) + if ( IIMD_get_custom_float(&float_get.dataname, &float_get.nb, (float**)&float_get.data) ) { - if (log) + { fprintf(IMDlog, "MYMDD > \n"); fprintf(IMDlog, "MYMDD > Receive float array (Time step=%d)\n" , i); fprintf(IMDlog, "MYMDD > ================================\n"); fprintf(IMDlog, "MYMDD > \n"); - fprintf(IMDlog, "MYMDD > MYPROGRAM float array %s \n", datanamefloat); + fprintf(IMDlog, "MYMDD > MYPROGRAM float array %s \n", float_get.dataname); fprintf(IMDlog, "MYMDD > ------------------------------------------ \n"); - for (unsigned j = 0; j < nbfloat; j++) + for (unsigned j = 0; j < float_get.nb; j++) { - fprintf(IMDlog, "MYMDD > float array[%d] = %f\n", j, customfloat[j]); + fprintf(IMDlog, "MYMDD > float array[%d] = %f\n", j, ((float*)(float_get.data))[j]); } fprintf(IMDlog, "MYMDD > \n"); } } - if ( IIMD_get_custom_int(&datanameint, &nbint, (int **) &customint) ) + if ( IIMD_get_custom_int(&int_get.dataname, &int_get.nb, (int**)&int_get.data) ) { - if (log) + { fprintf(IMDlog, "MYMDD > \n"); fprintf(IMDlog, "MYMDD > Receive int array (Time step=%d)\n" , i); fprintf(IMDlog, "MYMDD > ================================\n"); fprintf(IMDlog, "MYMDD > \n"); - fprintf(IMDlog, "MYMDD > MYPROGRAM int array %s \n", datanameint); + fprintf(IMDlog, "MYMDD > MYPROGRAM int array %s \n", int_get.dataname); fprintf(IMDlog, "MYMDD > ------------------------------------------ \n"); - for (unsigned j = 0; j < nbint; j++) + for (unsigned j = 0; j < int_get.nb; j++) { - fprintf(IMDlog, "MYMDD > int array[%d] = %d\n", j, customint[j]); + fprintf(IMDlog, "MYMDD > int array[%d] = %d\n", j, ((int*)(int_get.data))[j]); } fprintf(IMDlog, "MYMDD > \n"); } @@ -307,6 +311,9 @@ int main() { IIMD_send_forces ( &N_forces, atoms_list, forces_list ); } + + IIMD_send_custom_float(float_send.dataname, &float_send.nb, float_send.data); + IIMD_send_custom_int(int_send.dataname, &int_send.nb, int_send.data); } #if !defined(_WIN32) // Deals with keyboard events diff --git a/protocol/test_exe/servertest.c b/protocol/test_exe/servertest.c index 56229bc..1bb9816 100644 --- a/protocol/test_exe/servertest.c +++ b/protocol/test_exe/servertest.c @@ -85,23 +85,12 @@ // #define CURRCALC 1 -float custom_float[5] = -{ -1.0, -2.0, -3.0, -4.0, -5.0 -}; +int nb = 5; +int intdata[5] = {1, 2, 3, 4, 5}; +float floatdata[5] = {1.0, 2.0, 3.0, 4.0, 5.0}; -int custom_int[5] = -{ -1, -2, -3, -4, -5 -}; +IMDCustomData float_send, int_send; +IMDCustomData float_get, int_get; // Example coordinates for deca-alanine, 104 atoms, use test.pdb for visualization in VMD float coords[N][NDIM] = @@ -228,8 +217,7 @@ const double NmToAngstrom = 10.0; // Communication buffer for energies static IMDEnergies energies; -static int nbint; -static int nbfloat; + // Communication buffer for grid static IMDGrid grid; @@ -392,48 +380,44 @@ void myimd_send_grid(int step_) // Send custom float void myimd_send_float(int step_) { - - // should we print the grid to the log file? if ( MYIMDdebug ) { - fprintf(MYIMDlog, "MYMDD > \n"); - fprintf(MYIMDlog, "MYMDD > Send custom float (Time step=%d)\n" , step_); + fprintf(MYIMDlog, "MYMDD > Send float array (Time step=%d)\n" , step_); fprintf(MYIMDlog, "MYMDD > ================================\n"); fprintf(MYIMDlog, "MYMDD > \n"); - fprintf(MYIMDlog, "MYMDD > MYPROGRAM custom float \n"); + fprintf(MYIMDlog, "MYMDD > MYPROGRAM float array %s \n", float_send.dataname); fprintf(MYIMDlog, "MYMDD > ------------------------------------------ \n"); - fprintf(MYIMDlog, "MYMDD > Custom float %s %f, %f, %f, %f, %f\n", "myfloat" , custom_float[0],custom_float[1],custom_float[2], custom_float[3], custom_float[4]); + for (unsigned j = 0; j < float_send.nb; j++) + { + fprintf(MYIMDlog, "MYMDD > float array[%d] = %f\n", j, ((float*)(float_send.data))[j]); + } fprintf(MYIMDlog, "MYMDD > \n"); } - // SEND custom float - nbfloat = 5; - IIMD_send_custom_float("myfloat", &nbfloat, custom_float); + IIMD_send_custom_float(float_send.dataname, &float_send.nb, float_send.data); // ..whatever you want, for example unit conversion } // Send custom float void myimd_send_int(int step_) { - - // should we print the grid to the log file? if ( MYIMDdebug ) { - fprintf(MYIMDlog, "MYMDD > \n"); - fprintf(MYIMDlog, "MYMDD > Send custom int (Time step=%d)\n" , step_); + fprintf(MYIMDlog, "MYMDD > Send int array (Time step=%d)\n" , step_); fprintf(MYIMDlog, "MYMDD > ================================\n"); fprintf(MYIMDlog, "MYMDD > \n"); - fprintf(MYIMDlog, "MYMDD > MYPROGRAM custom int \n"); + fprintf(MYIMDlog, "MYMDD > MYPROGRAM int array %s \n", int_send.dataname); fprintf(MYIMDlog, "MYMDD > ------------------------------------------ \n"); - fprintf(MYIMDlog, "MYMDD > Custom int %s %d, %d, %d, %d, %d\n", "myint" , custom_int[0],custom_int[1],custom_int[2], custom_int[3], custom_int[4]); + for (unsigned j = 0; j < int_send.nb; j++) + { + fprintf(MYIMDlog, "MYMDD > int array[%d] = %d\n", j, ((int*)(int_send.data))[j]); + } fprintf(MYIMDlog, "MYMDD > \n"); } - // SEND custom int - nbint = 5; - IIMD_send_custom_int("myint", &nbint, custom_int); + IIMD_send_custom_int(int_send.dataname, &int_send.nb, int_send.data); // ..whatever you want, for example unit conversion } @@ -505,6 +489,49 @@ void myimd_ext_forces( ) } } +void myimd_get_custom_float(int step_) +{ + if ( IIMD_get_custom_float(&float_get.dataname, &float_get.nb, (float**)&float_get.data) ) + { + if ( MYIMDdebug ) + { + fprintf(MYIMDlog, "MYMDD > \n"); + fprintf(MYIMDlog, "MYMDD > Receive float array (Time step=%d)\n" , step_); + fprintf(MYIMDlog, "MYMDD > ================================\n"); + fprintf(MYIMDlog, "MYMDD > \n"); + fprintf(MYIMDlog, "MYMDD > MYPROGRAM float array %s \n", float_get.dataname); + fprintf(MYIMDlog, "MYMDD > ------------------------------------------ \n"); + for (unsigned j = 0; j < float_get.nb; j++) + { + fprintf(MYIMDlog, "MYMDD > float array[%d] = %f\n", j, ((float*)(float_get.data))[j]); + } + fprintf(MYIMDlog, "MYMDD > \n"); + } + } +} + +void myimd_get_custom_int(int step_) +{ + if ( IIMD_get_custom_int(&int_get.dataname, &int_get.nb, (int**)&int_get.data) ) + { + if ( MYIMDdebug ) + { + fprintf(MYIMDlog, "MYMDD > \n"); + fprintf(MYIMDlog, "MYMDD > Receive int array (Time step=%d)\n" , step_); + fprintf(MYIMDlog, "MYMDD > ================================\n"); + fprintf(MYIMDlog, "MYMDD > \n"); + fprintf(MYIMDlog, "MYMDD > MYPROGRAM int array %s \n", int_get.dataname); + fprintf(MYIMDlog, "MYMDD > ------------------------------------------ \n"); + for (unsigned j = 0; j < int_get.nb; j++) + { + fprintf(MYIMDlog, "MYMDD > int array[%d] = %d\n", j, ((int*)(int_get.data))[j]); + } + fprintf(MYIMDlog, "MYMDD > \n"); + } + + } +} + // ----------------------------------------------------------- // THE TWO FUNCTIONS BELOW ARE FOR OUR FAKE CALCULATION MODULE @@ -566,6 +593,19 @@ int main() int n = N; float tmpcoords[N][NDIM]; int i = 0; + + // cfloSC : custom float Server Client + float_send.dataname="cfloSC"; + float_send.nb=nb; + float_send.data=&floatdata; + + int_send.dataname="cintSC"; + int_send.nb=nb; + int_send.data=&intdata; + + float_get.dataname = (char *) malloc(sizeof(char)*8); + int_get.dataname = (char *) malloc(sizeof(char)*8); + // initialize the coordinate array init_coords(tmpcoords); @@ -588,6 +628,8 @@ int main() myimd_send_float(i); // send float data myimd_send_int(i); // send int data myimd_ext_forces(); // receive VMD's forces + myimd_get_custom_float(i); // get float data + myimd_get_custom_int(i); // get int data } // Treats extra events switch ( imd_event )