Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

dlpolyplugin.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2016 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: dlpolyplugin.c,v $
00013  *      $Author: johns $       $Locker:  $             $State: Exp $
00014  *      $Revision: 1.24 $       $Date: 2016/11/28 05:01:53 $
00015  *
00016  ***************************************************************************/
00017 
00018 /*
00019  * DLPOLY formatted history file format:
00020  *   http://www.ccp5.ac.uk/DL_POLY/MANUALS/USRMAN4.pdf
00021  *   http://www.ccp5.ac.uk/DL_POLY_CLASSIC/MANUALS/USRMAN.pdf
00022  *
00023  */
00024 
00025 #include "largefiles.h"   /* platform dependent 64-bit file I/O defines */
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <ctype.h>
00031 #include "molfile_plugin.h"
00032 
00033 #ifndef M_PI_2
00034 #define M_PI_2 1.57079632679489661922
00035 #endif
00036 
00037 typedef struct {
00038   FILE *file;
00039   int dlpolyversion;
00040   int numatoms;
00041   int cellwarnflag;
00042 } dlpolydata;
00043  
00044 
00045 static void *open_dlpoly_read(const char *filename, const char *filetype, 
00046                            int *natoms) {
00047   FILE *fd;
00048   dlpolydata *data;
00049   char fbuffer[4096], buf[4096];
00050   int dlpolyversion, scancount, nstep, keytrj, atomcount;
00051 
00052   fd = fopen(filename, "rb");
00053   if (!fd) return NULL;
00054 
00055   if (NULL == fgets(fbuffer, 1024, fd))  
00056     return NULL;
00057 
00058   if (!strcmp(filetype, "dlpolyhist")) {
00059     dlpolyversion=2;
00060   } else if (!strcmp(filetype, "dlpoly3hist")) {
00061     dlpolyversion=3;
00062   } else {
00063     dlpolyversion=2;
00064   }
00065  
00066   /* check to see if the first line is a "timestep" record */ 
00067   scancount = sscanf(fbuffer, "%s %d %d", buf, &nstep, natoms);
00068   if (scancount != 3 || strcmp(buf, "timestep") != 0) {
00069     /* if not a timestep, it might have the normal header on it      */
00070     /* in which case we'll skip the first line, and parse the second */
00071     if (NULL == fgets(fbuffer, 1024, fd))  
00072       return NULL;
00073     scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &nstep, natoms);
00074     if (scancount != 3) {
00075       printf("open_dlpoly_read) unrecognized header record\n");
00076       return NULL;
00077     } 
00078 
00079     /* now check the first timestep record for safety */
00080     if (NULL == fgets(fbuffer, 1024, fd))  
00081       return NULL;
00082     scancount = sscanf(fbuffer, "%s %d %d", buf, &nstep, &atomcount);
00083     if (scancount != 3 || strcmp(buf, "timestep") != 0) {
00084       printf("open_dlpoly_read) unrecognized timestep record\n");
00085       return NULL;
00086     }
00087 
00088     if (atomcount != *natoms) {
00089       printf("open_dlpoly_read) mismatched atom count\n");
00090       return NULL;
00091     }
00092   }
00093  
00094   data = (dlpolydata *) malloc(sizeof(dlpolydata));
00095   memset(data, 0, sizeof(dlpolydata));
00096   data->file = fd;
00097   data->numatoms= *natoms;
00098   data->cellwarnflag = 0;
00099   data->dlpolyversion = dlpolyversion;
00100 
00101   rewind(data->file); /* prepare for first read_timestep call */
00102   return data;
00103 }
00104 
00105 
00106 static void *open_dlpoly_config_read(const char *filename, const char *filetype, 
00107                            int *natoms) {
00108   FILE *fd;
00109   dlpolydata *data;
00110   char fbuffer[4096], buf[4096];
00111   int dlpolyversion, scancount, keytrj, imcon;
00112 
00113   fd = fopen(filename, "rb");
00114   if (!fd) return NULL;
00115 
00116   if (NULL == fgets(fbuffer, 1024, fd))  
00117     return NULL;
00118 
00119   if (!strcmp(filetype, "dlpolyconfig")) {
00120     dlpolyversion=2;
00121   }
00122  
00123   
00124   scancount = sscanf(fbuffer, "%s %d %d", buf, &imcon, natoms);
00125   if (scancount != 3 ) {
00126     /* in which case we'll skip the first line, and parse the second */
00127     if (NULL == fgets(fbuffer, 1024, fd))  
00128       return NULL;
00129     scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &imcon, natoms);
00130     if (scancount != 3) {
00131       printf("open_dlpoly_read) unrecognized header record\n");
00132       return NULL;
00133     } 
00134   }
00135     
00136   data = (dlpolydata *) malloc(sizeof(dlpolydata));
00137   memset(data, 0, sizeof(dlpolydata));
00138   data->file = fd;
00139   data->numatoms= *natoms;
00140   data->cellwarnflag = 0;
00141   data->dlpolyversion = dlpolyversion;
00142 
00143   rewind(data->file); /* prepare for first read_timestep call */
00144   return data;
00145 }
00146 
00147 static int read_dlpoly_structure(void *mydata, int *optflags,
00148                               molfile_atom_t *atoms) {
00149   char fbuffer[4096], buf[4096];
00150   float rsd, x, y, z;
00151   int i, nstep, atomcount, keytrj, imcon, scancount, atomid, atomcount2;
00152   float tstep, mass, charge;
00153   dlpolydata *data = (dlpolydata *)mydata;
00154  
00155   /* we don't read any optional data */
00156   *optflags = MOLFILE_NOOPTIONS;
00157 
00158 
00159   /* if we get nothing, assume we hit end of file */
00160   if (NULL == fgets(fbuffer, 1024, data->file))  
00161     return MOLFILE_EOF;
00162 
00163   /* check to see if the first line is a "timestep" record */
00164   scancount = sscanf(fbuffer, "%s %d %d %d %d %f", buf, 
00165                      &nstep, &atomcount, &keytrj, &imcon, &tstep);
00166   if (scancount != 6 || strcmp(buf, "timestep") != 0) {
00167     /* if not a timestep, it might have the normal header on it      */
00168     /* in which case we'll skip the first line, and parse the second */
00169     if (NULL == fgets(fbuffer, 1024, data->file))
00170       return MOLFILE_EOF;
00171     scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &nstep, &atomcount);
00172     if (scancount != 3) {
00173       printf("dlpoly structure) unrecognized header record\n");
00174       return MOLFILE_ERROR;
00175     } 
00176 
00177     /* now check the first timestep record for safety */
00178     if (NULL == fgets(fbuffer, 1024, data->file))
00179       return MOLFILE_EOF;
00180     scancount = sscanf(fbuffer, "%s %d %d %d %d %f", buf, 
00181                        &nstep, &atomcount2, &keytrj, &imcon, &tstep);
00182     if (scancount != 6 || strcmp(buf, "timestep") != 0) {
00183       printf("dlpoly structure) unrecognized timestep record\n");
00184       return MOLFILE_ERROR;
00185     }
00186 
00187     /* check atom count */
00188     if (atomcount != atomcount2) {
00189       printf("dlpoly structure) header/timestep mismatched atom count\n");
00190       return MOLFILE_ERROR;
00191     }
00192   }
00193 
00194   /* check atom count */
00195   if (atomcount != data->numatoms) {
00196     printf("dlpoly structure) mismatched atom count\n");
00197     return MOLFILE_ERROR;
00198   }
00199 
00200   /* read periodic cell vectors */
00201   if (imcon >= 0) {
00202     float xaxis[3];
00203     float yaxis[3];
00204     float zaxis[3];
00205 
00206     /* eat the data but don't use it for anything */
00207     if (3 != fscanf(data->file, "%f %f %f\n", &xaxis[0], &xaxis[1], &xaxis[2])) {
00208       printf("dlpoly structure) failed reading unit cell basis vectors\n");
00209       return MOLFILE_ERROR;
00210     }
00211     if (3 != fscanf(data->file, "%f %f %f\n", &yaxis[0], &yaxis[1], &yaxis[2])) {
00212       printf("dlpoly structure) failed reading unit cell basis vectors\n");
00213       return MOLFILE_ERROR;
00214     }
00215     if (3 != fscanf(data->file, "%f %f %f\n", &zaxis[0], &zaxis[1], &zaxis[2])) {
00216       printf("dlpoly structure) failed reading unit cell basis vectors\n");
00217       return MOLFILE_ERROR;
00218     }
00219   }
00220 
00221   for (i=0; i<data->numatoms; i++) {
00222     molfile_atom_t *atom = atoms + i;
00223 
00224     /* read the coordinates */
00225     switch (data->dlpolyversion) {
00226       case 3:
00227         if (8 != fscanf(data->file, "%s %d %f %f %f %f %f %f",
00228                buf, &atomid, &mass, &charge, &rsd, &x, &y, &z)) {
00229           printf("dlpoly structure v3) failed parsing atom coordinates\n");
00230           return MOLFILE_ERROR;
00231         }
00232         break;
00233 
00234       case 2:
00235       default: 
00236         if (7 != fscanf(data->file, "%s %d %f %f %f %f %f",
00237                buf, &atomid, &mass, &charge, &x, &y, &z)) {
00238           printf("dlpoly structure v2) failed parsing atom coordinates\n");
00239           return MOLFILE_ERROR;
00240         }
00241         break;
00242     }
00243 
00244     /* read the velocities */
00245     if (keytrj > 0) {
00246       float xv, yv, zv;
00247       if (3 != fscanf(data->file, "%f %f %f", &xv, &yv, &zv)) {
00248         printf("dlpoly structure) failed reading atom velocities\n");
00249         return MOLFILE_ERROR;
00250       }
00251     }
00252 
00253     /* read the forces */
00254     if (keytrj > 1) {
00255       float xf, yf, zf;
00256       if (3 != fscanf(data->file, "%f %f %f", &xf, &yf, &zf)) {
00257         printf("dlpoly structure) failed reading atom forces\n");
00258         return MOLFILE_ERROR;
00259       }
00260     }
00261 
00262     strncpy(atom->name, buf, sizeof(atom->name));
00263     strncpy(atom->type, atom->name, sizeof(atom->type));
00264     atom->resname[0] = '\0';
00265     atom->resid = 1;
00266     atom->chain[0] = '\0';
00267     atom->segid[0] = '\0';
00268   }
00269 
00270   rewind(data->file);
00271   return MOLFILE_SUCCESS;
00272 }
00273 
00274 
00275 static int read_dlpoly_config_structure(void *mydata, int *optflags,
00276                               molfile_atom_t *atoms) {
00277   char fbuffer[4096], buf[4096];
00278   float x, y, z;
00279   int i, atomcount, keytrj, imcon, scancount, atomid;
00280   dlpolydata *data = (dlpolydata *)mydata;
00281  
00282   /* we don't read any optional data */
00283   *optflags = MOLFILE_NOOPTIONS;
00284 
00285 
00286   /* if we get nothing, assume we hit end of file */
00287   if (NULL == fgets(fbuffer, 1024, data->file))  
00288     return MOLFILE_EOF;
00289 
00290     if (NULL == fgets(fbuffer, 1024, data->file))
00291       return MOLFILE_EOF;
00292     scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &imcon, &atomcount);
00293     if (scancount != 3) {
00294       printf("dlpoly structure) unrecognized header record\n");
00295       return MOLFILE_ERROR;
00296     } 
00297 
00298   /* check atom count */
00299   if (atomcount != data->numatoms) {
00300     printf("dlpoly structure) mismatched atom count\n");
00301     return MOLFILE_ERROR;
00302   }
00303 
00304   /* read periodic cell vectors */
00305   if (imcon > 0) {
00306     float xaxis[3];
00307     float yaxis[3];
00308     float zaxis[3];
00309 
00310     /* eat the data but don't use it for anything */
00311     if (3 != fscanf(data->file, "%f %f %f\n", &xaxis[0], &xaxis[1], &xaxis[2])) {
00312       printf("dlpoly structure) failed reading unit cell basis vectors\n");
00313       return MOLFILE_ERROR;
00314     }
00315     if (3 != fscanf(data->file, "%f %f %f\n", &yaxis[0], &yaxis[1], &yaxis[2])) {
00316       printf("dlpoly structure) failed reading unit cell basis vectors\n");
00317       return MOLFILE_ERROR;
00318     }
00319     if (3 != fscanf(data->file, "%f %f %f\n", &zaxis[0], &zaxis[1], &zaxis[2])) {
00320       printf("dlpoly structure) failed reading unit cell basis vectors\n");
00321       return MOLFILE_ERROR;
00322     }
00323   }
00324 
00325   for (i=0; i<data->numatoms; i++) {
00326     molfile_atom_t *atom = atoms + i;
00327 
00328     /* read the coordinates */
00329     if (2 != fscanf(data->file, "%s %d", buf, &atomid)) {
00330       printf("dlpoly structure v3) failed parsing atom labels\n");
00331       return MOLFILE_ERROR;
00332     }
00333     /* read the coordinates */
00334     if (3 != fscanf(data->file, "%f %f %f", &x, &y, &z)) {
00335       printf("dlpoly structure) failed reading atom coordinates\n");
00336       return MOLFILE_ERROR;
00337     }
00338 
00339     /* read the velocities */
00340     if (keytrj > 0) {
00341       float xv, yv, zv;
00342       if (3 != fscanf(data->file, "%f %f %f", &xv, &yv, &zv)) {
00343         printf("dlpoly structure) failed reading atom velocities\n");
00344         return MOLFILE_ERROR;
00345       }
00346     }
00347 
00348     /* read the forces */
00349     if (keytrj > 1) {
00350       float xf, yf, zf;
00351       if (3 != fscanf(data->file, "%f %f %f", &xf, &yf, &zf)) {
00352         printf("dlpoly structure) failed reading atom forces\n");
00353         return MOLFILE_ERROR;
00354       }
00355     }
00356 
00357     strncpy(atom->name, buf, sizeof(atom->name));
00358     strncpy(atom->type, atom->name, sizeof(atom->type));
00359     atom->resname[0] = '\0';
00360     atom->resid = 1;
00361     atom->chain[0] = '\0';
00362     atom->segid[0] = '\0';
00363   }
00364 
00365   rewind(data->file);
00366   return MOLFILE_SUCCESS;
00367 }
00368 
00369 
00370 static int read_dlpoly_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00371   char fbuffer[4096], buf[4096];
00372   float rsd, x, y, z;
00373   int i, nstep, atomcount, keytrj, imcon, scancount, atomid, atomcount2;
00374   float tstep, mass, charge, elapsedps;
00375   dlpolydata *data = (dlpolydata *)mydata;
00376 
00377   /* if we get nothing, assume we hit end of file */
00378   if (NULL == fgets(fbuffer, 1024, data->file))  
00379     return MOLFILE_EOF;
00380 
00381   switch (data->dlpolyversion) {
00382     case 3:
00383       scancount = sscanf(fbuffer, "%s %d %d %d %d %f %f", buf, 
00384                     &nstep, &atomcount, &keytrj, &imcon, &tstep, &elapsedps);
00385       if (scancount != 7 || strcmp(buf, "timestep") != 0) {
00386         /* if not a timestep, it might have the normal header on it      */
00387         /* in which case we'll skip the first line, and parse the second */
00388         if (NULL == fgets(fbuffer, 1024, data->file))
00389           return MOLFILE_EOF;
00390         scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &nstep, &atomcount);
00391         if (scancount != 3) {
00392           printf("dlpoly timestep v3) unrecognized header record\n");
00393           return MOLFILE_ERROR;
00394         } 
00395 
00396         /* now check the first timestep record for safety */
00397         if (NULL == fgets(fbuffer, 1024, data->file))
00398           return MOLFILE_EOF;
00399         scancount = sscanf(fbuffer, "%s %d %d %d %d %f %f", buf, 
00400                     &nstep, &atomcount2, &keytrj, &imcon, &tstep, &elapsedps);
00401         if (scancount != 7 || strcmp(buf, "timestep") != 0) {
00402           printf("dlpoly timestep v3) unrecognized timestep record\n");
00403           return MOLFILE_ERROR;
00404         }
00405 
00406         /* check atom count */
00407         if (atomcount != atomcount2) {
00408           printf("dlpoly timestep v3) header/timestep mismatched atom count\n");
00409           return MOLFILE_ERROR;
00410         }
00411       }
00412       break;
00413 
00414     case 2:
00415     default: 
00416       scancount = sscanf(fbuffer, "%s %d %d %d %d %f", buf, 
00417                          &nstep, &atomcount, &keytrj, &imcon, &tstep);
00418       if (scancount != 6 || strcmp(buf, "timestep") != 0) {
00419         /* if not a timestep, it might have the normal header on it      */
00420         /* in which case we'll skip the first line, and parse the second */
00421         if (NULL == fgets(fbuffer, 1024, data->file))
00422           return MOLFILE_EOF;
00423         scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &nstep, &atomcount);
00424         if (scancount != 3) {
00425           printf("dlpoly timestep v2) unrecognized header record\n");
00426           return MOLFILE_ERROR;
00427         } 
00428 
00429         /* now check the first timestep record for safety */
00430         if (NULL == fgets(fbuffer, 1024, data->file))
00431           return MOLFILE_EOF;
00432         scancount = sscanf(fbuffer, "%s %d %d %d %d %f", buf, 
00433                            &nstep, &atomcount2, &keytrj, &imcon, &tstep);
00434         if (scancount != 6 || strcmp(buf, "timestep") != 0) {
00435           printf("dlpoly timestep v2) unrecognized timestep record\n");
00436           return MOLFILE_ERROR;
00437         }
00438 
00439         /* check atom count */
00440         if (atomcount != atomcount2) {
00441           printf("dlpoly timestep v2) header/timestep mismatched atom count\n");
00442           return MOLFILE_ERROR;
00443         }
00444       }
00445       break;
00446   }
00447 
00448   /* check atom count */
00449   if (atomcount != natoms) {
00450     printf("dlpoly timestep) mismatched atom count\n");
00451     return MOLFILE_ERROR;
00452   }
00453 
00454   /* read periodic cell vectors */
00455   if (imcon >= 0) {
00456     float xaxis[3];
00457     float yaxis[3];
00458     float zaxis[3];
00459 
00460     if (3 != fscanf(data->file, "%f %f %f\n", &xaxis[0], &xaxis[1], &xaxis[2])) {
00461       printf("dlpoly timestep) failed reading unit cell basis vectors\n");
00462       return MOLFILE_ERROR;
00463     }
00464 
00465     if (3 != fscanf(data->file, "%f %f %f\n", &yaxis[0], &yaxis[1], &yaxis[2])) {
00466       printf("dlpoly timestep) failed reading unit cell basis vectors\n");
00467       return MOLFILE_ERROR;
00468     }
00469 
00470     if (3 != fscanf(data->file, "%f %f %f\n", &zaxis[0], &zaxis[1], &zaxis[2])) {
00471       printf("dlpoly timestep) failed reading unit cell basis vectors\n");
00472       return MOLFILE_ERROR;
00473     }
00474 
00475     /* check and copy in periodic cell info */
00476     if (ts != NULL) {
00477       if (xaxis[1] != 0.0 || xaxis[2] != 0.0 || 
00478           yaxis[0] != 0.0 || yaxis[2] != 0.0 || 
00479           zaxis[0] != 0.0 || zaxis[1] != 0.0) {
00480         if (data->cellwarnflag != 1)
00481           printf("dlpoly timestep) non-orthogonal DLPOLY periodic cell data unsupported\n");
00482         data->cellwarnflag = 1;
00483       } else {
00484         ts->A = xaxis[0];
00485         ts->B = yaxis[1];
00486         ts->C = zaxis[2];
00487         if (data->cellwarnflag != 2)
00488           printf("dlpoly timestep) converting DLPOLY periodic cell data\n");
00489         data->cellwarnflag = 2;
00490       }
00491     }
00492   }
00493 
00494   /* read all per-atom data */
00495   for (i=0; i<natoms; i++) {
00496     /* read the coordinates */
00497     switch (data->dlpolyversion) {
00498       case 3:
00499         if (8 != fscanf(data->file, "%s %d %f %f %f %f %f %f",
00500                buf, &atomid, &mass, &charge, &rsd, &x, &y, &z)) {
00501           printf("dlpoly timestep v3) failed parsing atom coordinates\n");
00502           return MOLFILE_ERROR;
00503         }
00504         break;
00505 
00506       case 2:
00507       default: 
00508         if (7 != fscanf(data->file, "%s %d %f %f %f %f %f",
00509                buf, &atomid, &mass, &charge, &x, &y, &z)) {
00510           printf("dlpoly timestep v2) failed parsing atom coordinates\n");
00511           return MOLFILE_ERROR;
00512         }
00513         break;
00514     }
00515 
00516     /* read the velocities */
00517     if (keytrj > 0) {
00518       float xv, yv, zv;
00519       if (3 != fscanf(data->file, "%f %f %f", &xv, &yv, &zv)) {
00520         printf("dlpoly timestep) failed parsing atom velocities\n");
00521         return MOLFILE_ERROR;
00522       }
00523     }
00524 
00525     /* read the forces */
00526     if (keytrj > 1) {
00527       float xf, yf, zf;
00528       if (3 != fscanf(data->file, "%f %f %f", &xf, &yf, &zf)) {
00529         printf("dlpoly timestep) failed parsing atom forces\n");
00530         return MOLFILE_ERROR;
00531       }
00532     }
00533 
00534     /* only save coords if we're given a timestep pointer, */
00535     /* otherwise assume that VMD wants us to skip past it. */
00536     if (ts != NULL) { 
00537       if (atomid > 0 && atomid <= natoms) {
00538         int addr = 3 * (atomid - 1);
00539         ts->coords[addr    ] = x;
00540         ts->coords[addr + 1] = y;
00541         ts->coords[addr + 2] = z;
00542       } else {
00543         fprintf(stderr, "dlpoly timestep) ignoring illegal atom index %d\n", atomid);
00544       }
00545     } 
00546   }
00547 
00548   /* eat remaining spaces and LF character */
00549   fgets(fbuffer, 1024, data->file);
00550 
00551   return MOLFILE_SUCCESS;
00552 }
00553     
00554 
00555 static int read_dlpoly_config_timestep(void *mydata, int natoms, molfile_timestep_t *ts) {
00556   char fbuffer[4096], buf[4096];
00557   float x, y, z;
00558   int i, atomcount, keytrj, imcon, scancount, atomid;
00559   dlpolydata *data = (dlpolydata *)mydata;
00560 
00561   /* if we get nothing, assume we hit end of file */
00562   if (NULL == fgets(fbuffer, 1024, data->file))  
00563     return MOLFILE_EOF;
00564 
00565   if (NULL == fgets(fbuffer, 1024, data->file))
00566     return MOLFILE_EOF;
00567   scancount = sscanf(fbuffer, "%d %d %d", &keytrj, &imcon, &atomcount);
00568   if (scancount != 3) {
00569     printf("dlpoly timestep v3) unrecognized header record\n");
00570     return MOLFILE_ERROR;
00571   } 
00572 
00573   /* check atom count */
00574   if (atomcount != natoms) {
00575     printf("dlpoly timestep) mismatched atom count\n");
00576     return MOLFILE_ERROR;
00577   }
00578 
00579   /* read periodic cell vectors */
00580   if (imcon > 0) {
00581     float xaxis[3];
00582     float yaxis[3];
00583     float zaxis[3];
00584 
00585     if (3 != fscanf(data->file, "%f %f %f\n", &xaxis[0], &xaxis[1], &xaxis[2])) {
00586       printf("dlpoly timestep) failed reading unit cell basis vectors\n");
00587       return MOLFILE_ERROR;
00588     }
00589 
00590     if (3 != fscanf(data->file, "%f %f %f\n", &yaxis[0], &yaxis[1], &yaxis[2])) {
00591       printf("dlpoly timestep) failed reading unit cell basis vectors\n");
00592       return MOLFILE_ERROR;
00593     }
00594 
00595     if (3 != fscanf(data->file, "%f %f %f\n", &zaxis[0], &zaxis[1], &zaxis[2])) {
00596       printf("dlpoly timestep) failed reading unit cell basis vectors\n");
00597       return MOLFILE_ERROR;
00598     }
00599 
00600     /* check and copy in periodic cell info */
00601     if (ts != NULL) {
00602       if (xaxis[1] != 0.0 || xaxis[2] != 0.0 || 
00603           yaxis[0] != 0.0 || yaxis[2] != 0.0 || 
00604           zaxis[0] != 0.0 || xaxis[1] != 0.0) {
00605         if (data->cellwarnflag != 1)
00606           printf("dlpoly timestep) non-orthogonal DLPOLY periodic cell data unsupported\n");
00607         data->cellwarnflag = 1;
00608       } else {
00609         ts->A = xaxis[0];
00610         ts->B = yaxis[1];
00611         ts->C = zaxis[2];
00612         if (data->cellwarnflag != 2)
00613           printf("dlpoly timestep) converting DLPOLY periodic cell data\n");
00614         data->cellwarnflag = 2;
00615       }
00616     }
00617   }
00618 
00619   /* read all per-atom data */
00620   for (i=0; i<natoms; i++) {
00621     /* read the coordinates */
00622     if (2 != fscanf(data->file, "%s %d", buf, &atomid)) {
00623       printf("dlpoly timestep v3) failed parsing atom labels\n");
00624       return MOLFILE_ERROR;
00625     }
00626     /* read the coordinates */
00627     if (3 != fscanf(data->file, "%f %f %f", &x, &y, &z)) {
00628       printf("dlpoly timestep) failed parsing atom coordinates\n");
00629       return MOLFILE_ERROR;
00630     }
00631     /* read the velocities */
00632     if (keytrj > 0) {
00633       float xv, yv, zv;
00634       if (3 != fscanf(data->file, "%f %f %f", &xv, &yv, &zv)) {
00635         printf("dlpoly timestep) failed parsing atom velocities\n");
00636         return MOLFILE_ERROR;
00637       }
00638     }
00639 
00640     /* read the forces */
00641     if (keytrj > 1) {
00642       float xf, yf, zf;
00643       if (3 != fscanf(data->file, "%f %f %f", &xf, &yf, &zf)) {
00644         printf("dlpoly timestep) failed parsing atom forces\n");
00645         return MOLFILE_ERROR;
00646       }
00647     }
00648 
00649     /* only save coords if we're given a timestep pointer, */
00650     /* otherwise assume that VMD wants us to skip past it. */
00651     if (ts != NULL) { 
00652       if (atomid > 0 && atomid <= natoms) {
00653         int addr = 3 * (atomid - 1);
00654         ts->coords[addr    ] = x;
00655         ts->coords[addr + 1] = y;
00656         ts->coords[addr + 2] = z;
00657       } else {
00658         fprintf(stderr, "dlpoly timestep) ignoring illegal atom index %d\n", atomid);
00659       }
00660     } 
00661   }
00662 
00663   /* eat remaining spaces and LF character */
00664   fgets(fbuffer, 1024, data->file);
00665 
00666   return MOLFILE_SUCCESS;
00667 }
00668     
00669     
00670     
00671 static void close_dlpoly_read(void *mydata) {
00672   dlpolydata *data = (dlpolydata *)mydata;
00673   fclose(data->file);
00674   free(data);
00675 }
00676 
00677 
00678 /* registration stuff */
00679 static molfile_plugin_t dlpoly2plugin;
00680 static molfile_plugin_t dlpoly3plugin;
00681 
00682 static molfile_plugin_t dlpolypluginconfig;
00683 
00684 VMDPLUGIN_API int VMDPLUGIN_init() {
00685   memset(&dlpoly2plugin, 0, sizeof(molfile_plugin_t));
00686   dlpoly2plugin.abiversion = vmdplugin_ABIVERSION;
00687   dlpoly2plugin.type = MOLFILE_PLUGIN_TYPE;
00688   dlpoly2plugin.name = "dlpolyhist";
00689   dlpoly2plugin.prettyname = "DL_POLY_C HISTORY";
00690   dlpoly2plugin.author = "John Stone";
00691   dlpoly2plugin.majorv = 0;
00692   dlpoly2plugin.minorv = 8;
00693   dlpoly2plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00694   dlpoly2plugin.filename_extension = "dlpolyhist";
00695   dlpoly2plugin.open_file_read = open_dlpoly_read;
00696   dlpoly2plugin.read_structure = read_dlpoly_structure;
00697   dlpoly2plugin.read_next_timestep = read_dlpoly_timestep;
00698   dlpoly2plugin.close_file_read = close_dlpoly_read;
00699 
00700   memset(&dlpoly3plugin, 0, sizeof(molfile_plugin_t));
00701   dlpoly3plugin.abiversion = vmdplugin_ABIVERSION;
00702   dlpoly3plugin.type = MOLFILE_PLUGIN_TYPE;
00703   dlpoly3plugin.name = "dlpoly3hist";
00704   dlpoly3plugin.prettyname = "DL_POLY_4 HISTORY";
00705   dlpoly3plugin.author = "John Stone";
00706   dlpoly3plugin.majorv = 0;
00707   dlpoly3plugin.minorv = 8;
00708   dlpoly3plugin.is_reentrant = VMDPLUGIN_THREADSAFE;
00709   dlpoly3plugin.filename_extension = "dlpolyhist";
00710   dlpoly3plugin.open_file_read = open_dlpoly_read;
00711   dlpoly3plugin.read_structure = read_dlpoly_structure;
00712   dlpoly3plugin.read_next_timestep = read_dlpoly_timestep;
00713   dlpoly3plugin.close_file_read = close_dlpoly_read;
00714 
00715   memset(&dlpolypluginconfig, 0, sizeof(molfile_plugin_t));
00716   dlpolypluginconfig.abiversion = vmdplugin_ABIVERSION;
00717   dlpolypluginconfig.type = MOLFILE_PLUGIN_TYPE;
00718   dlpolypluginconfig.name = "dlpolyconfig";
00719   dlpolypluginconfig.prettyname = "DL_POLY CONFIG";
00720   dlpolypluginconfig.author = "Alin M Elena";
00721   dlpolypluginconfig.majorv = 0;
00722   dlpolypluginconfig.minorv = 1;
00723   dlpolypluginconfig.is_reentrant = VMDPLUGIN_THREADSAFE;
00724   dlpolypluginconfig.filename_extension = "dlpolyconfig";
00725   dlpolypluginconfig.open_file_read = open_dlpoly_config_read;
00726   dlpolypluginconfig.read_structure = read_dlpoly_config_structure;
00727   dlpolypluginconfig.read_next_timestep = read_dlpoly_config_timestep;
00728   dlpolypluginconfig.close_file_read = close_dlpoly_read;
00729   
00730   return VMDPLUGIN_SUCCESS;
00731 }
00732 
00733 VMDPLUGIN_API int VMDPLUGIN_register(void *v, vmdplugin_register_cb cb) {
00734   (*cb)(v, (vmdplugin_t *)&dlpoly2plugin);
00735   (*cb)(v, (vmdplugin_t *)&dlpoly3plugin);
00736   (*cb)(v, (vmdplugin_t *)&dlpolypluginconfig);
00737 
00738   return VMDPLUGIN_SUCCESS;
00739 }
00740 
00741 VMDPLUGIN_API int VMDPLUGIN_fini() {
00742   return VMDPLUGIN_SUCCESS;
00743 }
00744 
00745 
00746 #ifdef TEST_PLUGIN
00747 
00748 int main(int argc, char *argv[]) {
00749   molfile_timestep_t timestep;
00750   void *v;
00751   int natoms;
00752   int i, nsets, set;
00753 
00754   while (--argc) {
00755     ++argv;
00756     v = open_dlpoly_read(*argv, "dlpoly", &natoms);
00757     if (!v) {
00758       fprintf(stderr, "open_dlpoly_read failed for file %s\n", *argv);
00759       return 1;
00760     }
00761     fprintf(stderr, "open_dlpoly_read succeeded for file %s\n", *argv);
00762     fprintf(stderr, "number of atoms: %d\n", natoms);
00763 
00764     i = 0;
00765     timestep.coords = (float *)malloc(3*sizeof(float)*natoms);
00766     while (!read_dlpoly_timestep(v, natoms, &timestep)) {
00767       i++;
00768     }
00769     fprintf(stderr, "ended read_next_timestep on frame %d\n", i);
00770 
00771     close_dlpoly_read(v);
00772   }
00773   return 0;
00774 }
00775 
00776 #endif
00777 

Generated on Fri Apr 19 03:09:19 2024 for VMD Plugins (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002