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 1033 of file dcdlib.C.

01035 {
01036 #ifdef WIN32
01037         _close(fd);
01038 #else
01039         close(fd);
01040 #endif
01041 
01042         if (num_fixed)
01043         {
01044                 delete [] indexes;
01045         }
01046 }

void close_dcd_write ( int  fd  ) 

Definition at line 1063 of file dcdlib.C.

References NAMD_err().

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

01065 {
01066 #ifdef WIN32
01067   if ( _close(fd) )
01068 #else
01069   if ( fsync(fd) || close(fd) )
01070 #endif
01071   {
01072     NAMD_err("Error closing DCD file");
01073   }
01074 }

int get_dcdheader_size (  ) 

Definition at line 1005 of file dcdlib.C.

01005                         {
01006         int headersize = 0;
01007         int totalInt32s = 27; /* 27 writes from out_integer */
01008         int totalChars = 164; /* 3 writes from title_string */
01009         int totalFloats = 1; /* 1 write from out_float */
01010         headersize = sizeof(int32)*totalInt32s+totalChars+sizeof(float)*totalFloats;
01011         return headersize;
01012 }

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 839 of file dcdlib.C.

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

00840 {
00841         int32 NSAVC,NSTEP,NFILE;
00842         /* don't update header until after write succeeds */
00843         OFF_T end = LSEEK(fd,0,SEEK_CUR);
00844         LSEEK(fd,NSAVC_POS,SEEK_SET);
00845         READ(fd,(void*) &NSAVC,sizeof(int32));
00846         LSEEK(fd,NSTEP_POS,SEEK_SET);
00847         READ(fd,(void*) &NSTEP,sizeof(int32));
00848         LSEEK(fd,NFILE_POS,SEEK_SET);
00849         READ(fd,(void*) &NFILE,sizeof(int32));
00850         NSTEP += NSAVC;
00851         NFILE += 1;
00852         LSEEK(fd,NSTEP_POS,SEEK_SET);
00853         NAMD_write(fd,(char*) &NSTEP,sizeof(int32));
00854         LSEEK(fd,NFILE_POS,SEEK_SET);
00855         NAMD_write(fd,(char*) &NFILE,sizeof(int32));
00856         LSEEK(fd,end,SEEK_SET);
00857 
00858         return(0);
00859 }

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 915 of file dcdlib.C.

References NAMD_write, and pad().

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

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

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   // Note: the value of out_integer wraps for N >= 2^30.
00752         out_integer = N*4;
00753   // Use a separate byte count stored without overflow.
00754   size_t nbytes = ((size_t) N) * 4;
00755 
00756         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00757         NAMD_write(fd, (char *) X, nbytes);
00758         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00759         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00760         NAMD_write(fd, (char *) Y, nbytes);
00761         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00762         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00763         NAMD_write(fd, (char *) Z, nbytes);
00764         NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00765 
00766         /* don't update header until after write succeeds */
00767         OFF_T end = LSEEK(fd,0,SEEK_CUR);
00768         LSEEK(fd,NSAVC_POS,SEEK_SET);
00769         READ(fd,(void*) &NSAVC,sizeof(int32));
00770         LSEEK(fd,NSTEP_POS,SEEK_SET);
00771         READ(fd,(void*) &NSTEP,sizeof(int32));
00772         LSEEK(fd,NFILE_POS,SEEK_SET);
00773         READ(fd,(void*) &NFILE,sizeof(int32));
00774         NSTEP += NSAVC;
00775         NFILE += 1;
00776         LSEEK(fd,NSTEP_POS,SEEK_SET);
00777         NAMD_write(fd,(char*) &NSTEP,sizeof(int32));
00778         LSEEK(fd,NFILE_POS,SEEK_SET);
00779         NAMD_write(fd,(char*) &NFILE,sizeof(int32));
00780         LSEEK(fd,end,SEEK_SET);
00781 
00782         return(0);
00783 }

int write_dcdstep_par_cell ( int  fd,
double *  cell 
)

Definition at line 785 of file dcdlib.C.

References NAMD_write.

00785                                                 {
00786         if (cell) {
00787           int32 out_integer = 48;
00788           NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00789           NAMD_write(fd, (char *) cell, out_integer);
00790           NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00791         }
00792         return 0;
00793 }

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

Definition at line 864 of file dcdlib.C.

References LSEEK, NAMD_write, and OFF_T.

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

int write_dcdstep_par_XYZUnits ( int  fd,
int  N 
)

Definition at line 810 of file dcdlib.C.

References LSEEK, NAMD_write, and OFF_T.

00811 {
00812   int32 out_integer;
00813 
00814   // number of elements
00815   // Note: the value of out_integer wraps for N >= 2^30.
00816   out_integer = N*sizeof(float);
00817 
00818   // For byte seeking, use a properly sized variable.
00819   OFF_T nbytes = ((OFF_T) N) * sizeof(float);
00820 
00821   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00822   // seek to the end of each x y z block and write out the count
00823   LSEEK(fd, nbytes, SEEK_CUR);
00824   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00825 
00826   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00827   LSEEK(fd, nbytes, SEEK_CUR);
00828   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00829 
00830   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00831   LSEEK(fd, nbytes, SEEK_CUR);
00832   NAMD_write(fd, (char *) &out_integer, sizeof(int32));
00833 
00834   return(0);
00835 }


Generated on Sat Jul 21 01:17:16 2018 for NAMD by  doxygen 1.4.7