NAMD
Macros | Functions
dcdlib.h File Reference
#include "largefiles.h"
#include "common.h"
#include "Vector.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <pwd.h>
#include <time.h>

Go to the source code of this file.

Macros

#define OFF_T   off_t
 
#define DCD_DNE   -2 /* DCD file does not exist */
 
#define DCD_OPENFAILED   -3 /* Open of DCD file failed */
 
#define DCD_BADREAD   -4 /* read call on DCD file failed */
 
#define DCD_BADEOF   -5 /* premature EOF found in DCD file */
 
#define DCD_BADFORMAT   -6 /* format of DCD file is wrong */
 
#define DCD_FILEEXISTS   -7 /* output file already exists */
 
#define DCD_BADMALLOC   -8 /* malloc failed */
 

Functions

int open_dcd_read (char *)
 
int read_dcdheader (int, int *, int *, int *, int *, double *, int *, int **)
 
int read_dcdstep (int, int, float *, float *, float *, int, int, int *)
 
int open_dcd_write (const char *)
 
int write_dcdstep (int, int, float *, float *, float *, double *unitcell)
 
int write_dcdheader (int, const char *, int, int, int, int, int, double, int)
 
int get_dcdheader_size ()
 
void close_dcd_read (int, int, int *)
 
void close_dcd_write (int)
 
int open_dcd_write_par_slave (char *dcdname)
 
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)
 
OFF_T NAMD_seek (int file, OFF_T offset, int whence)
 

Macro Definition Documentation

#define DCD_BADEOF   -5 /* premature EOF found in DCD file */

Definition at line 50 of file dcdlib.h.

#define DCD_BADFORMAT   -6 /* format of DCD file is wrong */

Definition at line 51 of file dcdlib.h.

#define DCD_BADMALLOC   -8 /* malloc failed */

Definition at line 53 of file dcdlib.h.

#define DCD_BADREAD   -4 /* read call on DCD file failed */

Definition at line 49 of file dcdlib.h.

#define DCD_DNE   -2 /* DCD file does not exist */

Definition at line 47 of file dcdlib.h.

Referenced by open_dcd_read().

#define DCD_FILEEXISTS   -7 /* output file already exists */

Definition at line 52 of file dcdlib.h.

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

#define DCD_OPENFAILED   -3 /* Open of DCD file failed */

Definition at line 48 of file dcdlib.h.

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

#define OFF_T   off_t

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

Definition at line 42 of file dcdlib.h.

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

Function Documentation

void close_dcd_read ( int  ,
int  ,
int *   
)

Definition at line 1033 of file dcdlib.C.

1035 {
1036 #ifdef WIN32
1037  _close(fd);
1038 #else
1039  close(fd);
1040 #endif
1041 
1042  if (num_fixed)
1043  {
1044  delete [] indexes;
1045  }
1046 }
void close_dcd_write ( int  )

Definition at line 1063 of file dcdlib.C.

References NAMD_err().

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

1065 {
1066 #ifdef WIN32
1067  if ( _close(fd) )
1068 #else
1069  if ( fsync(fd) || close(fd) )
1070 #endif
1071  {
1072  NAMD_err("Error closing DCD file");
1073  }
1074 }
void NAMD_err(const char *err_msg)
Definition: common.C:106
int get_dcdheader_size ( )

Definition at line 1005 of file dcdlib.C.

1005  {
1006  int headersize = 0;
1007  int totalInt32s = 27; /* 27 writes from out_integer */
1008  int totalChars = 164; /* 3 writes from title_string */
1009  int totalFloats = 1; /* 1 write from out_float */
1010  headersize = sizeof(int32)*totalInt32s+totalChars+sizeof(float)*totalFloats;
1011  return headersize;
1012 }
short int32
Definition: dumpdcd.c:24
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.

49  {
50  OFF_T retval = LSEEK(file, offset, whence);
51  if ( retval < 0 ) NAMD_err("seek failed while writing DCD file");
52  if ( whence == SEEK_SET && retval != offset ) {
53  char buf[256];
54  sprintf(buf, "seek failed while writing DCD file: SEEK_SET %lld returned %lld\n", offset, retval);
55  NAMD_die(buf);
56  }
57  return retval;
58 }
#define OFF_T
Definition: dcdlib.h:42
void NAMD_err(const char *err_msg)
Definition: common.C:106
void NAMD_die(const char *err_msg)
Definition: common.C:85
#define LSEEK
Definition: dcdlib.C:61
int open_dcd_read ( char *  )

Definition at line 145 of file dcdlib.C.

References DCD_DNE, DCD_OPENFAILED, and O_LARGEFILE.

147 {
148  int dcdfd; /* file descriptor for dcd file */
149 
150  /* Do a stat just to see if the file really exists */
151  if (access(filename, F_OK) != 0)
152  {
153  if (errno == ENOENT)
154  {
155  return(DCD_DNE);
156  }
157  }
158 
159  /* Try and open the file */
160 #ifdef WIN32
161  dcdfd=_open(filename, O_RDONLY|O_BINARY|O_LARGEFILE);
162 #else
163  dcdfd=open(filename, O_RDONLY|O_LARGEFILE);
164 #endif
165 
166  if (dcdfd == -1)
167  {
168  return(DCD_OPENFAILED);
169  }
170 
171  return(dcdfd);
172 }
#define O_LARGEFILE
Definition: dcdlib.C:64
#define DCD_DNE
Definition: dcdlib.h:47
#define DCD_OPENFAILED
Definition: dcdlib.h:48
int open_dcd_write ( const char *  )

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().

664 {
665  int dcdfd;
666  NAMD_backup_file(dcdname,".BAK");
667 
668 #ifdef WIN32
669  while ( (dcdfd = _open(dcdname, O_RDWR|O_CREAT|O_EXCL|O_BINARY|O_LARGEFILE,
670  _S_IREAD|_S_IWRITE)) < 0)
671 #else
672 #ifdef NAMD_NO_O_EXCL
673  while ( (dcdfd = open(dcdname, O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE,
674 #else
675  while ( (dcdfd = open(dcdname, O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE,
676 #endif
677  S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
678 #endif
679  {
680  if ( errno != EINTR ) return(DCD_OPENFAILED);
681  }
682 
683  return(dcdfd);
684 }
#define O_LARGEFILE
Definition: dcdlib.C:64
void NAMD_backup_file(const char *filename, const char *extension)
Definition: common.C:167
#define DCD_OPENFAILED
Definition: dcdlib.h:48
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().

691 {
692 #if OUTPUT_SINGLE_FILE
693  //In the case of single file, the dcd output by slaves has been created
694  //by the master, so the dcd file doesn't need to be created again and
695  //backed up. --Chao Mei
696  int dcdfd;
697 #ifdef WIN32
698  while ( (dcdfd = _open(dcdname, O_WRONLY|O_BINARY|O_LARGEFILE,
699  _S_IREAD|_S_IWRITE)) < 0)
700 #else
701  while ( (dcdfd = open(dcdname, O_WRONLY|O_LARGEFILE,
702  S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
703 #endif
704  {
705  if ( errno != EINTR ) return(DCD_OPENFAILED);
706  }
707 
708  return(dcdfd);
709 #else
710  //In the case of multiple output files, each slave has its own output file and
711  //it needs to be created. --Chao Mei
712  return open_dcd_write(dcdname);
713 #endif
714 }
#define O_LARGEFILE
Definition: dcdlib.C:64
int open_dcd_write(const char *dcdname)
Definition: dcdlib.C:662
#define DCD_OPENFAILED
Definition: dcdlib.h:48
int read_dcdheader ( int  ,
int *  ,
int *  ,
int *  ,
int *  ,
double *  ,
int *  ,
int **   
)
int read_dcdstep ( int  ,
int  ,
float *  ,
float *  ,
float *  ,
int  ,
int  ,
int *   
)
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.

840 {
841  int32 NSAVC,NSTEP,NFILE;
842  /* don't update header until after write succeeds */
843  OFF_T end = LSEEK(fd,0,SEEK_CUR);
844  LSEEK(fd,NSAVC_POS,SEEK_SET);
845  READ(fd,(void*) &NSAVC,sizeof(int32));
846  LSEEK(fd,NSTEP_POS,SEEK_SET);
847  READ(fd,(void*) &NSTEP,sizeof(int32));
848  LSEEK(fd,NFILE_POS,SEEK_SET);
849  READ(fd,(void*) &NFILE,sizeof(int32));
850  NSTEP += NSAVC;
851  NFILE += 1;
852  LSEEK(fd,NSTEP_POS,SEEK_SET);
853  NAMD_write(fd,(char*) &NSTEP,sizeof(int32));
854  LSEEK(fd,NFILE_POS,SEEK_SET);
855  NAMD_write(fd,(char*) &NFILE,sizeof(int32));
856  LSEEK(fd,end,SEEK_SET);
857 
858  return(0);
859 }
#define OFF_T
Definition: dcdlib.h:42
#define NSAVC_POS
Definition: dcdlib.C:630
short int32
Definition: dumpdcd.c:24
#define NAMD_write
Definition: dcdlib.C:24
#define READ
Definition: dcdlib.C:46
#define NFILE_POS
Definition: dcdlib.C:628
#define NSTEP_POS
Definition: dcdlib.C:631
#define LSEEK
Definition: dcdlib.C:61
int write_dcdheader ( int  ,
const char *  ,
int  ,
int  ,
int  ,
int  ,
int  ,
double  ,
int   
)

Definition at line 915 of file dcdlib.C.

References NAMD_write, and pad().

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

917 {
918  int32 out_integer;
919  float out_float;
920  char title_string[200];
921  int user_id;
922 #ifndef WIN32
923  struct passwd *pwbuf;
924 #endif
925  time_t cur_time;
926  struct tm *tmbuf;
927  char time_str[11];
928 
929  out_integer = 84;
930  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
931  strcpy(title_string, "CORD");
932  NAMD_write(fd, title_string, 4);
933  out_integer = NFILE; /* located at fpos 8 */
934  out_integer = 0; /* ignore the lies */
935  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
936  out_integer = NPRIV; /* located at fpos 12 */
937  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
938  out_integer = NSAVC; /* located at fpos 16 */
939  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
940  out_integer = NSTEP; /* located at fpos 20 */
941  out_integer = NPRIV - NSAVC; /* ignore the lies */
942  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
943  out_integer=0;
944  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
945  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
946  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
947  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
948  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
949  out_float = DELTA;
950  NAMD_write(fd, (char *) &out_float, sizeof(float));
951  out_integer = with_unitcell ? 1 : 0;
952  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
953  out_integer = 0;
954  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
955  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
956  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
957  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
958  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
959  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
960  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
961  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
962  out_integer = 24; // PRETEND TO BE CHARMM24 -JCP
963  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
964  out_integer = 84;
965  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
966 
967  out_integer = 164;
968  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
969  out_integer = 2;
970  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
971 
972  sprintf(title_string, "REMARKS FILENAME=%s CREATED BY NAMD", filename);
973  pad(title_string, 80);
974  NAMD_write(fd, title_string, 80);
975 
976  char username[100];
977 #if defined(WIN32) || defined(NO_GETPWUID)
978  sprintf(username,"Win32");
979 #else
980  user_id= (int) getuid();
981  pwbuf=getpwuid(user_id);
982  if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name);
983  else sprintf(username,"%d",user_id);
984 #endif
985  cur_time=time(NULL);
986  tmbuf=localtime(&cur_time);
987  strftime(time_str, 10, "%m/%d/%y", tmbuf);
988 
989  sprintf(title_string, "REMARKS DATE: %s CREATED BY USER: %s",
990  time_str, username);
991  pad(title_string, 80);
992  NAMD_write(fd, title_string, 80);
993  out_integer = 164;
994  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
995  out_integer = 4;
996  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
997  out_integer = N;
998  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
999  out_integer = 4;
1000  NAMD_write(fd, (char *) & out_integer, sizeof(int32));
1001 
1002  return(0);
1003 }
short int32
Definition: dumpdcd.c:24
#define NAMD_write
Definition: dcdlib.C:24
void pad(char *s, int len)
Definition: dcdlib.C:105
int write_dcdstep ( int  ,
int  ,
float *  ,
float *  ,
float *  ,
double *  unitcell 
)

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().

738 {
739  int32 NSAVC,NSTEP,NFILE;
740  int32 out_integer;
741 
742  /* Unit cell */
743  if (cell) {
744  out_integer = 48;
745  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
746  NAMD_write(fd, (char *) cell, out_integer);
747  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
748  }
749 
750  /* Coordinates */
751  // Note: the value of out_integer wraps for N >= 2^30.
752  out_integer = N*4;
753  // Use a separate byte count stored without overflow.
754  size_t nbytes = ((size_t) N) * 4;
755 
756  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
757  NAMD_write(fd, (char *) X, nbytes);
758  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
759  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
760  NAMD_write(fd, (char *) Y, nbytes);
761  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
762  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
763  NAMD_write(fd, (char *) Z, nbytes);
764  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
765 
766  /* don't update header until after write succeeds */
767  OFF_T end = LSEEK(fd,0,SEEK_CUR);
768  LSEEK(fd,NSAVC_POS,SEEK_SET);
769  READ(fd,(void*) &NSAVC,sizeof(int32));
770  LSEEK(fd,NSTEP_POS,SEEK_SET);
771  READ(fd,(void*) &NSTEP,sizeof(int32));
772  LSEEK(fd,NFILE_POS,SEEK_SET);
773  READ(fd,(void*) &NFILE,sizeof(int32));
774  NSTEP += NSAVC;
775  NFILE += 1;
776  LSEEK(fd,NSTEP_POS,SEEK_SET);
777  NAMD_write(fd,(char*) &NSTEP,sizeof(int32));
778  LSEEK(fd,NFILE_POS,SEEK_SET);
779  NAMD_write(fd,(char*) &NFILE,sizeof(int32));
780  LSEEK(fd,end,SEEK_SET);
781 
782  return(0);
783 }
#define OFF_T
Definition: dcdlib.h:42
#define NSAVC_POS
Definition: dcdlib.C:630
short int32
Definition: dumpdcd.c:24
#define X
Definition: msm_defn.h:29
#define NAMD_write
Definition: dcdlib.C:24
#define Z
Definition: msm_defn.h:33
#define READ
Definition: dcdlib.C:46
#define NFILE_POS
Definition: dcdlib.C:628
#define NSTEP_POS
Definition: dcdlib.C:631
#define Y
Definition: msm_defn.h:31
#define LSEEK
Definition: dcdlib.C:61
int write_dcdstep_par_cell ( int  fd,
double *  cell 
)

Definition at line 785 of file dcdlib.C.

References NAMD_write.

785  {
786  if (cell) {
787  int32 out_integer = 48;
788  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
789  NAMD_write(fd, (char *) cell, out_integer);
790  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
791  }
792  return 0;
793 }
short int32
Definition: dumpdcd.c:24
#define NAMD_write
Definition: dcdlib.C:24
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.

864  {
865 
866  /* Coordinates for the N elements handled by this writer */
867  int parN = parU-parL+1;
868  size_t nbytes = ((size_t)parN) * 4;
869 
870  /* x's 1st number of Xs */
871  /* skip field for the bytes in X, and the first parL atoms in X*/
872  OFF_T xoffset = sizeof(int)+sizeof(float)*((OFF_T)parL);
873  LSEEK(fd, xoffset, SEEK_CUR);
874  NAMD_write(fd, (char *) X, nbytes);
875 
876  /* skip field for the bytes in X and Y; */
877  /* skip the remaining atoms in X at number of (N-1)-(parU+1)+1
878  * where N-1 is the last atom id, paru+1 is the next atom id. */
879  /* skip the first parL atoms in Y; */
880  OFF_T yoffset = 2*sizeof(int)+sizeof(float)*((OFF_T)(N-parU+parL-1));
881  LSEEK(fd, yoffset, SEEK_CUR);
882  NAMD_write(fd, (char *) Y, nbytes);
883 
884  OFF_T zoffset = yoffset;
885  LSEEK(fd, zoffset, SEEK_CUR);
886  NAMD_write(fd, (char *) Z, nbytes);
887  return(0);
888 }
#define OFF_T
Definition: dcdlib.h:42
#define X
Definition: msm_defn.h:29
#define NAMD_write
Definition: dcdlib.C:24
#define Z
Definition: msm_defn.h:33
#define Y
Definition: msm_defn.h:31
#define LSEEK
Definition: dcdlib.C:61
int write_dcdstep_par_XYZUnits ( int  fd,
int  N 
)

Definition at line 810 of file dcdlib.C.

References LSEEK, NAMD_write, and OFF_T.

811 {
812  int32 out_integer;
813 
814  // number of elements
815  // Note: the value of out_integer wraps for N >= 2^30.
816  out_integer = N*sizeof(float);
817 
818  // For byte seeking, use a properly sized variable.
819  OFF_T nbytes = ((OFF_T) N) * sizeof(float);
820 
821  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
822  // seek to the end of each x y z block and write out the count
823  LSEEK(fd, nbytes, SEEK_CUR);
824  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
825 
826  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
827  LSEEK(fd, nbytes, SEEK_CUR);
828  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
829 
830  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
831  LSEEK(fd, nbytes, SEEK_CUR);
832  NAMD_write(fd, (char *) &out_integer, sizeof(int32));
833 
834  return(0);
835 }
#define OFF_T
Definition: dcdlib.h:42
short int32
Definition: dumpdcd.c:24
#define NAMD_write
Definition: dcdlib.C:24
#define LSEEK
Definition: dcdlib.C:61