dcdlib.C File Reference

#include "dcdlib.h"

Go to the source code of this file.

Defines

#define OUTPUT_SINGLE_FILE   1
#define NAMD_write   NAMD_write64
#define LSEEK   lseek
#define READ   read
#define LSEEK   NAMD_seek
#define O_LARGEFILE   0x0
#define CHECK_FREAD(X, msg)
#define CHECK_FEOF(X, msg)
#define NFILE_POS   (off_t) 8
#define NPRIV_POS   (off_t) 12
#define NSAVC_POS   (off_t) 16
#define NSTEP_POS   (off_t) 20

Functions

void NAMD_write (int fd, const char *buf, size_t count)
OFF_T NAMD_seek (int file, OFF_T offset, int whence)
void pad (char *s, int len)
int open_dcd_read (char *filename)
int open_dcd_write (const char *dcdname)
int open_dcd_write_par_slave (char *dcdname)
int write_dcdstep (int fd, int N, float *X, float *Y, float *Z, double *cell)
int write_dcdstep_par_cell (int fd, double *cell)
int write_dcdstep_par_XYZUnits (int fd, int N)
int update_dcdstep_par_header (int fd)
int write_dcdstep_par_slave (int fd, int parL, int parU, int N, float *X, float *Y, float *Z)
int write_dcdheader (int fd, const char *filename, int N, int NFILE, int NPRIV, int NSAVC, int NSTEP, double DELTA, int with_unitcell)
int get_dcdheader_size ()
void close_dcd_read (int fd, int num_fixed, int *indexes)
void close_dcd_write (int fd)


Define Documentation

#define CHECK_FEOF ( X,
msg   ) 

Value:

if (X==0) \
                             { \
                                return(DCD_BADEOF); \
                             }

Definition at line 97 of file dcdlib.C.

#define CHECK_FREAD ( X,
msg   ) 

Value:

if (X==-1) \
                             { \
                                return(DCD_BADREAD); \
                             }

Definition at line 80 of file dcdlib.C.

#define LSEEK   NAMD_seek

Definition at line 61 of file dcdlib.C.

#define LSEEK   lseek

Definition at line 61 of file dcdlib.C.

Referenced by NAMD_seek(), update_dcdstep_par_header(), write_dcdstep(), write_dcdstep_par_slave(), and write_dcdstep_par_XYZUnits().

#define NAMD_write   NAMD_write64

Definition at line 24 of file dcdlib.C.

Referenced by ofstream_namd::flush(), update_dcdstep_par_header(), write_dcdheader(), write_dcdstep(), write_dcdstep_par_cell(), write_dcdstep_par_slave(), and write_dcdstep_par_XYZUnits().

#define NFILE_POS   (off_t) 8

Definition at line 628 of file dcdlib.C.

Referenced by update_dcdstep_par_header(), and write_dcdstep().

#define NPRIV_POS   (off_t) 12

Definition at line 629 of file dcdlib.C.

#define NSAVC_POS   (off_t) 16

Definition at line 630 of file dcdlib.C.

Referenced by update_dcdstep_par_header(), and write_dcdstep().

#define NSTEP_POS   (off_t) 20

Definition at line 631 of file dcdlib.C.

Referenced by update_dcdstep_par_header(), and write_dcdstep().

#define O_LARGEFILE   0x0

Definition at line 64 of file dcdlib.C.

Referenced by NAMD_open(), open_dcd_read(), open_dcd_write(), and open_dcd_write_par_slave().

#define OUTPUT_SINGLE_FILE   1

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 16 of file dcdlib.C.

#define READ   read

Definition at line 46 of file dcdlib.C.

Referenced by update_dcdstep_par_header(), and write_dcdstep().


Function Documentation

void close_dcd_read ( int  fd,
int  num_fixed,
int *  indexes 
)

Definition at line 1025 of file dcdlib.C.

01027 {
01028 #ifdef WIN32
01029         _close(fd);
01030 #else
01031         close(fd);
01032 #endif
01033 
01034         if (num_fixed)
01035         {
01036                 delete [] indexes;
01037         }
01038 }

void close_dcd_write ( int  fd  ) 

Definition at line 1055 of file dcdlib.C.

References NAMD_err().

Referenced by Output::recvReplicaDcdInit(), and ComputeQMMgr::~ComputeQMMgr().

01057 {
01058 #ifdef WIN32
01059   if ( _close(fd) )
01060 #else
01061   if ( fsync(fd) || close(fd) )
01062 #endif
01063   {
01064     NAMD_err("Error closing DCD file");
01065   }
01066 }

int get_dcdheader_size (  ) 

Definition at line 997 of file dcdlib.C.

00997                         {
00998         int headersize = 0;
00999         int totalInt32s = 27; /* 27 writes from out_integer */
01000         int totalChars = 164; /* 3 writes from title_string */
01001         int totalFloats = 1; /* 1 write from out_float */
01002         headersize = sizeof(int32)*totalInt32s+totalChars+sizeof(float)*totalFloats;
01003         return headersize;
01004 }

OFF_T NAMD_seek ( int  file,
OFF_T  offset,
int  whence 
)

Definition at line 49 of file dcdlib.C.

References LSEEK, NAMD_die(), NAMD_err(), and OFF_T.

00049                                                     {
00050   OFF_T retval = LSEEK(file, offset, whence);
00051   if ( retval < 0 ) NAMD_err("seek failed while writing DCD file");
00052   if ( whence == SEEK_SET && retval != offset ) {
00053     char buf[256];
00054     sprintf(buf, "seek failed while writing DCD file: SEEK_SET %lld returned %lld\n", offset, retval);
00055     NAMD_die(buf);
00056   }
00057   return retval;
00058 }

void NAMD_write ( int  fd,
const char *  buf,
size_t  count 
)

Definition at line 26 of file dcdlib.C.

References NAMD_bug(), and NAMD_die().

00026                                                        {
00027   while ( count ) {
00028 #if defined(WIN32) && !defined(__CYGWIN__)
00029     long retval = _write(fd,buf,count);
00030 #else
00031     ssize_t retval = write(fd,buf,count);
00032 #endif
00033     if ( retval < 0 && errno == EINTR ) retval = 0;
00034     if ( retval < 0 ) NAMD_die(strerror(errno));
00035     if ( retval > count ) NAMD_bug("extra bytes written in NAMD_write64()");
00036     buf += retval;
00037     count -= retval;
00038   }
00039 }

int open_dcd_read ( char *  filename  ) 

Definition at line 145 of file dcdlib.C.

References DCD_DNE, DCD_OPENFAILED, and O_LARGEFILE.

00147 {
00148         int dcdfd;              /*  file descriptor for dcd file        */
00149 
00150         /*  Do a stat just to see if the file really exists     */
00151         if (access(filename, F_OK) != 0)
00152         {
00153                 if (errno == ENOENT)
00154                 {
00155                         return(DCD_DNE);
00156                 }
00157         }
00158 
00159         /*  Try and open the file                               */
00160 #ifdef WIN32
00161         dcdfd=_open(filename, O_RDONLY|O_BINARY|O_LARGEFILE);
00162 #else
00163         dcdfd=open(filename, O_RDONLY|O_LARGEFILE);
00164 #endif
00165 
00166         if (dcdfd == -1)
00167         {
00168                 return(DCD_OPENFAILED);
00169         }
00170 
00171         return(dcdfd);
00172 }

int open_dcd_write ( const char *  dcdname  ) 

Definition at line 662 of file dcdlib.C.

References DCD_OPENFAILED, NAMD_backup_file(), and O_LARGEFILE.

Referenced by open_dcd_write_par_slave(), ComputeQMMgr::recvPartQM(), and Output::recvReplicaDcdData().

00664 {
00665         int dcdfd;
00666         NAMD_backup_file(dcdname,".BAK");
00667 
00668 #ifdef WIN32
00669         while ( (dcdfd = _open(dcdname, O_RDWR|O_CREAT|O_EXCL|O_BINARY|O_LARGEFILE,
00670                                 _S_IREAD|_S_IWRITE)) < 0)
00671 #else
00672 #ifdef NAMD_NO_O_EXCL
00673         while ( (dcdfd = open(dcdname, O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE,
00674 #else
00675         while ( (dcdfd = open(dcdname, O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE,
00676 #endif
00677                                 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
00678 #endif
00679         {
00680                 if ( errno != EINTR ) return(DCD_OPENFAILED);
00681         }
00682 
00683         return(dcdfd);
00684 }

int open_dcd_write_par_slave ( char *  dcdname  ) 

Definition at line 689 of file dcdlib.C.

References DCD_OPENFAILED, O_LARGEFILE, and open_dcd_write().

00691 {
00692 #if OUTPUT_SINGLE_FILE
00693         //In the case of single file, the dcd output by slaves has been created
00694         //by the master, so the dcd file doesn't need to be created again and
00695         //backed up. --Chao Mei
00696         int dcdfd;
00697 #ifdef WIN32
00698         while ( (dcdfd = _open(dcdname, O_WRONLY|O_BINARY|O_LARGEFILE,
00699                                 _S_IREAD|_S_IWRITE)) < 0)
00700 #else
00701         while ( (dcdfd = open(dcdname, O_WRONLY|O_LARGEFILE,
00702                                 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
00703 #endif
00704         {
00705                 if ( errno != EINTR ) return(DCD_OPENFAILED);
00706         }
00707 
00708         return(dcdfd);
00709 #else
00710         //In the case of multiple output files, each slave has its own output file and
00711         //it needs to be created. --Chao Mei
00712         return open_dcd_write(dcdname);
00713 #endif
00714 }

void pad ( char *  s,
int  len 
)

Definition at line 105 of file dcdlib.C.

Referenced by write_dcdheader().

00106 {
00107         int curlen;
00108         int i;
00109 
00110         curlen=strlen(s);
00111 
00112         if (curlen>len)
00113         {
00114                 s[len]='\0';
00115                 return;
00116         }
00117 
00118         for (i=curlen; i<len; i++)
00119         {
00120                 s[i]=' ';
00121         }
00122 
00123         s[i]='\0';
00124 }

int update_dcdstep_par_header ( int  fd  ) 

Definition at line 830 of file dcdlib.C.

References LSEEK, NAMD_write, NFILE_POS, NSAVC_POS, NSTEP_POS, OFF_T, and READ.

00831 {
00832         int32 NSAVC,NSTEP,NFILE;
00833         /* don't update header until after write succeeds */
00834         OFF_T end = LSEEK(fd,0,SEEK_CUR);
00835         LSEEK(fd,NSAVC_POS,SEEK_SET);
00836         READ(fd,(void*) &NSAVC,sizeof(int32));
00837         LSEEK(fd,NSTEP_POS,SEEK_SET);
00838         READ(fd,(void*) &NSTEP,sizeof(int32));
00839         LSEEK(fd,NFILE_POS,SEEK_SET);
00840         READ(fd,(void*) &NFILE,sizeof(int32));
00841         NSTEP += NSAVC;
00842         NFILE += 1;
00843         LSEEK(fd,NSTEP_POS,SEEK_SET);
00844         NAMD_write(fd,(char*) &NSTEP,sizeof(int32));
00845         LSEEK(fd,NFILE_POS,SEEK_SET);
00846         NAMD_write(fd,(char*) &NFILE,sizeof(int32));
00847         LSEEK(fd,end,SEEK_SET);
00848 
00849         return(0);
00850 }

int write_dcdheader ( int  fd,
const char *  filename,
int  N,
int  NFILE,
int  NPRIV,
int  NSAVC,
int  NSTEP,
double  DELTA,
int  with_unitcell 
)

Definition at line 907 of file dcdlib.C.

References NAMD_write, and pad().

Referenced by ComputeQMMgr::recvPartQM(), and Output::recvReplicaDcdData().

00909 {
00910         int32   out_integer;
00911         float   out_float;
00912         char    title_string[200];
00913         int     user_id;
00914 #ifndef WIN32
00915         struct  passwd *pwbuf;
00916 #endif
00917         time_t  cur_time;
00918         struct  tm *tmbuf;
00919         char    time_str[11];
00920 
00921         out_integer = 84;
00922         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00923         strcpy(title_string, "CORD");
00924         NAMD_write(fd, title_string, 4);
00925         out_integer = NFILE;  /* located at fpos 8 */
00926         out_integer = 0;  /* ignore the lies */
00927         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00928         out_integer = NPRIV;  /* located at fpos 12 */
00929         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00930         out_integer = NSAVC;  /* located at fpos 16 */
00931         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00932         out_integer = NSTEP;  /* located at fpos 20 */
00933         out_integer = NPRIV - NSAVC;  /* ignore the lies */
00934         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00935         out_integer=0;
00936         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00937         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00938         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00939         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00940         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00941         out_float = DELTA;
00942         NAMD_write(fd, (char *) &out_float, sizeof(float));
00943   out_integer = with_unitcell ? 1 : 0;
00944         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00945   out_integer = 0;
00946         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00947         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00948         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00949         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00950         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00951         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00952         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00953         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00954         out_integer = 24;  // PRETEND TO BE CHARMM24 -JCP
00955         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00956         out_integer = 84;
00957         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00958 
00959         out_integer = 164;
00960         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00961         out_integer = 2;
00962         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00963 
00964         sprintf(title_string, "REMARKS FILENAME=%s CREATED BY NAMD", filename);
00965         pad(title_string, 80);
00966         NAMD_write(fd, title_string, 80);
00967 
00968         char username[100];
00969 #if defined(WIN32) || defined(NO_GETPWUID)
00970         sprintf(username,"Win32");
00971 #else
00972         user_id= (int) getuid();
00973         pwbuf=getpwuid(user_id);
00974         if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name);
00975         else sprintf(username,"%d",user_id);
00976 #endif
00977         cur_time=time(NULL);
00978         tmbuf=localtime(&cur_time);
00979         strftime(time_str, 10, "%m/%d/%y", tmbuf);
00980 
00981         sprintf(title_string, "REMARKS DATE: %s CREATED BY USER: %s",
00982            time_str, username);
00983         pad(title_string, 80);
00984         NAMD_write(fd, title_string, 80);
00985         out_integer = 164;
00986         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00987         out_integer = 4;
00988         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00989         out_integer = N;
00990         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00991         out_integer = 4;
00992         NAMD_write(fd, (char *) & out_integer, sizeof(int32));
00993 
00994         return(0);
00995 }

int write_dcdstep ( int  fd,
int  N,
float *  X,
float *  Y,
float *  Z,
double *  cell 
)

Definition at line 736 of file dcdlib.C.

References LSEEK, NAMD_write, NFILE_POS, NSAVC_POS, NSTEP_POS, OFF_T, and READ.

Referenced by ComputeQMMgr::procQMRes(), and Output::recvReplicaDcdData().

00738 {
00739         int32 NSAVC,NSTEP,NFILE;
00740         int32 out_integer;
00741 
00742   /* Unit cell */
00743   if (cell) {
00744     out_integer = 48;
00745     NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00746     NAMD_write(fd, (char *) cell, out_integer);
00747     NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00748   }
00749 
00750   /* Coordinates */
00751         out_integer = N*4;
00752         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00753         NAMD_write(fd, (char *) X, out_integer);
00754         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00755         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00756         NAMD_write(fd, (char *) Y, out_integer);
00757         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00758         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00759         NAMD_write(fd, (char *) Z, out_integer);
00760         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00761 
00762         /* don't update header until after write succeeds */
00763         OFF_T end = LSEEK(fd,0,SEEK_CUR);
00764         LSEEK(fd,NSAVC_POS,SEEK_SET);
00765         READ(fd,(void*) &NSAVC,sizeof(int32));
00766         LSEEK(fd,NSTEP_POS,SEEK_SET);
00767         READ(fd,(void*) &NSTEP,sizeof(int32));
00768         LSEEK(fd,NFILE_POS,SEEK_SET);
00769         READ(fd,(void*) &NFILE,sizeof(int32));
00770         NSTEP += NSAVC;
00771         NFILE += 1;
00772         LSEEK(fd,NSTEP_POS,SEEK_SET);
00773         NAMD_write(fd,(char*) &NSTEP,sizeof(int32));
00774         LSEEK(fd,NFILE_POS,SEEK_SET);
00775         NAMD_write(fd,(char*) &NFILE,sizeof(int32));
00776         LSEEK(fd,end,SEEK_SET);
00777 
00778         return(0);
00779 }

int write_dcdstep_par_cell ( int  fd,
double *  cell 
)

Definition at line 781 of file dcdlib.C.

References NAMD_write.

00781                                                 {
00782         if (cell) {
00783           int32 out_integer = 48;
00784           NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00785           NAMD_write(fd, (char *) cell, out_integer);
00786           NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00787         }
00788         return 0;
00789 }

int write_dcdstep_par_slave ( int  fd,
int  parL,
int  parU,
int  N,
float *  X,
float *  Y,
float *  Z 
)

Definition at line 855 of file dcdlib.C.

References LSEEK, NAMD_write, and OFF_T.

00855                                                                                             {
00856 
00857         int parN = parU-parL+1;
00858         int32 out_integer;
00859   /* Coordinates for the N elements handled by this writer */
00860         out_integer = parN*4;
00861 
00862         /* x's 1st number of Xs */
00863         /* skip field for the bytes in X, and the first parL atoms in X*/
00864         OFF_T xoffset = sizeof(int)+sizeof(float)*((OFF_T)parL);
00865         LSEEK(fd, xoffset, SEEK_CUR);
00866         NAMD_write(fd, (char *) X, out_integer);
00867 
00868         /* skip field for the bytes in X and Y; */
00869         /* skip the remaining atoms in X at number of (N-1)-(parU+1)+1
00870          * where N-1 is the last atom id, paru+1 is the next atom id. */
00871         /* skip the first parL atoms in Y; */
00872         OFF_T yoffset = 2*sizeof(int)+sizeof(float)*((OFF_T)(N-parU+parL-1));
00873         LSEEK(fd, yoffset, SEEK_CUR);
00874         NAMD_write(fd, (char *) Y, out_integer);
00875 
00876         OFF_T zoffset = yoffset;
00877         LSEEK(fd, zoffset, SEEK_CUR);
00878         NAMD_write(fd, (char *) Z, out_integer);
00879         return(0);
00880 }

int write_dcdstep_par_XYZUnits ( int  fd,
int  N 
)

Definition at line 806 of file dcdlib.C.

References LSEEK, and NAMD_write.

00807 {
00808   int32 out_integer;
00809 
00810   // number of elements
00811   out_integer = N*sizeof(float);
00812   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00813   // seek to the end of each x y z block and write out the count
00814   LSEEK(fd, out_integer, SEEK_CUR);
00815   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00816 
00817   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00818   LSEEK(fd, out_integer, SEEK_CUR);
00819   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00820 
00821   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00822   LSEEK(fd, out_integer, SEEK_CUR);
00823   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00824 
00825   return(0);
00826 }


Generated on Sun Nov 19 01:17:16 2017 for NAMD by  doxygen 1.4.7