39 #define access(PATH,MODE) _access(PATH,00)
42 #if defined(WIN32) && !defined(__CYGWIN__)
43 #define PATHSEPSTR "\\"
44 #define MKDIR(X) mkdir(X)
46 #define PATHSEPSTR "/"
47 #define MKDIR(X) mkdir(X,0777)
50 #define NAMD_open NAMD_open64
51 #define NAMD_write NAMD_write64
52 #define NAMD_close NAMD_close64
55 #define O_LARGEFILE 0x0
64 while ( (fd = _open(fname, O_WRONLY|O_CREAT|O_EXCL|O_BINARY|
O_LARGEFILE,_S_IREAD|_S_IWRITE)) < 0) {
67 while ( (fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC|
O_LARGEFILE,
69 while ( (fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|
O_LARGEFILE,
71 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
73 if ( errno != EINTR ) {
75 sprintf(errmsg,
"Unable to open binary file %s", fname);
84 void NAMD_write(
int fd,
const char *buf,
size_t count,
const char *errmsg=
"NAMD_write64") {
85 double firsttime = 0.;
87 #if defined(WIN32) && !defined(__CYGWIN__)
88 long retval = _write(fd,buf,count);
90 ssize_t retval = write(fd,buf,count);
92 if ( retval < 0 && errno == EINTR ) retval = 0;
93 if ( retval < 0 && errno == ENOMEM ) {
94 if ( firsttime == 0. ) firsttime = CmiWallTimer();
95 if ( (CmiWallTimer() - firsttime) < 300. ) retval = 0;
98 if ( retval > count )
NAMD_bug(
"extra bytes written in NAMD_write64()");
102 if ( firsttime != 0. ) {
103 iout <<
iWARN << errmsg <<
": NAMD_write64() retried for " << (CmiWallTimer() - firsttime) <<
" seconds.\n" <<
endi;
110 while ( _close(fd) ) {
112 while ( close(fd) ) {
114 if ( errno != EINTR ) {
116 sprintf(errmsg,
"Error on closing file %s", fname);
123 #define seek_dcdfile NAMD_seek
126 #define namdMyNode Node::Object()
127 #define simParams simParameters
133 unitcell[0] = unitcell[2] = unitcell[5] = 0.0;
134 unitcell[1] = unitcell[3] = unitcell[4] = 0.0;
140 unitcell[0] = (lattice->
a_p()) ? a.
length() : 0.0;
141 unitcell[2] = (lattice->
b_p()) ? b.
length() : 0.0;
142 unitcell[5] = (lattice->
c_p()) ? c.
length() : 0.0;
143 double cosAB = (lattice->
a_p() && lattice->
b_p() ) ?
144 (a*b)/(unitcell[0]*unitcell[2]) : 0.0;
145 double cosAC = (lattice->
a_p() && lattice->
c_p() ) ?
146 (a*c)/(unitcell[0]*unitcell[5]) : 0.0;
147 double cosBC = (lattice->
b_p() && lattice->
c_p() ) ?
148 (b*c)/(unitcell[2]*unitcell[5]) : 0.0;
149 if (cosAB > 1.0) cosAB = 1.0;
else if (cosAB < -1.0) cosAB = -1.0;
150 if (cosAC > 1.0) cosAC = 1.0;
else if (cosAC < -1.0) cosAC = -1.0;
151 if (cosBC > 1.0) cosBC = 1.0;
else if (cosBC < -1.0) cosBC = -1.0;
204 int positionsNeeded = 0;
206 if ( timestep >= 0 ) {
211 { positionsNeeded |= 1; }
216 { positionsNeeded |= 2; }
219 if ( simParams->
IMDon &&
220 ( ((timestep % simParams->
IMDfreq) == 0) ||
222 { positionsNeeded |= 1; }
230 positionsNeeded |= 2;
233 return positionsNeeded;
236 template <
class xVector,
class xDone>
243 const int wrapAll = simParams->
wrapAll;
247 #ifndef MEM_OPT_VERSION
249 for ( i = 0; i < n; ++i ) {
254 for ( i = 0; i < n; ++i ) {
255 if ( ! wrapAll && ! molecule->
is_water(i) )
continue;
259 Vector trans = ( wrapNearest ?
263 coor[i] = coor[i] + con[ci];
281 double coor_wrapped = 0;
282 float fcoor_wrapped = 0;
284 if ( timestep >= 0 ) {
291 output_dcdfile(timestep, n, fcoor,
299 iout <<
"WRITING COORDINATES TO RESTART FILE AT STEP "
300 << timestep <<
"\n" <<
endi;
302 output_restart_coordinates(coor, n, timestep);
303 iout <<
"FINISHED WRITING RESTART COORDINATES\n" <<
endi;
308 if ( simParams->
IMDon &&
309 ( ((timestep % simParams->
IMDfreq) == 0) ||
332 iout <<
"WRITING COORDINATES TO OUTPUT FILE AT STEP "
333 << realstep <<
"\n" <<
endi;
336 output_final_coordinates(coor, n, realstep);
371 int velocitiesNeeded = 0;
373 if ( timestep >= 0 ) {
378 { velocitiesNeeded |= 1; }
383 { velocitiesNeeded |= 2; }
390 velocitiesNeeded |= 2;
393 return velocitiesNeeded;
400 if ( timestep >= 0 ) {
406 output_veldcdfile(timestep, n, vel);
413 iout <<
"WRITING VELOCITIES TO RESTART FILE AT STEP "
414 << timestep <<
"\n" <<
endi;
415 output_restart_velocities(timestep, n, vel);
416 iout <<
"FINISHED WRITING RESTART VELOCITIES\n" <<
endi;
427 iout <<
"WRITING VELOCITIES TO OUTPUT FILE AT STEP "
428 << realstep <<
"\n" <<
endi;
430 output_final_velocities(realstep, n, vel);
466 int forcesNeeded = 0;
468 if ( timestep >= 0 ) {
473 { forcesNeeded |= 1; }
490 if ( timestep >= 0 ) {
496 output_forcedcdfile(timestep, n, frc);
505 iout <<
"WRITING FORCES TO OUTPUT FILE AT STEP "
506 << realstep <<
"\n" <<
endi;
508 output_forces(realstep, n, frc);
531 void Output::output_restart_coordinates(
Vector *coor,
int n,
int timestep)
535 char timestepstr[20];
537 int baselen = strlen(
namdMyNode->simParams->restartFilename);
538 char *restart_name =
new char[baselen+26];
539 const char *bsuffix =
".old";
541 strcpy(restart_name,
namdMyNode->simParams->restartFilename);
543 sprintf(timestepstr,
".%d",timestep);
544 strcat(restart_name, timestepstr);
547 strcat(restart_name,
".coor");
555 sprintf(comment,
"RESTART COORDINATES WRITTEN BY NAMD AT TIMESTEP %d", timestep);
558 namdMyNode->pdbData->write(restart_name, comment);
563 write_binary_file(restart_name, n, coor);
566 delete [] restart_name;
568 if (
namdMyNode->simParams->restartSaveDcd ) {
569 if ( ! output_dcdfile(
END_OF_RUN, 0, 0, 0) ) {
570 const char *old_name =
namdMyNode->simParams->dcdFilename;
571 int old_len = strlen(old_name);
572 char *new_name =
new char[old_len+26];
573 strcpy(new_name, old_name);
574 if ( old_len >= 4 && ! strcmp(new_name+old_len-4,
".dcd") ) {
576 new_name[old_len] = 0;
578 sprintf(timestepstr,
".%d",timestep);
579 strcat(new_name, timestepstr);
580 strcat(new_name,
".dcd");
581 iout <<
"RENAMING COORDINATE DCD FILE " << old_name <<
" TO " << new_name <<
"\n" <<
endi;
583 while ( rename(old_name, new_name) ) {
584 if ( errno == EINTR || errno == EXDEV )
continue;
586 sprintf(err_msg,
"Unable to rename DCD file %s to %s", old_name, new_name);
610 void Output::output_restart_velocities(
int timestep,
int n,
Vector *vel)
614 char timestepstr[20];
616 int baselen = strlen(
namdMyNode->simParams->restartFilename);
617 char *restart_name =
new char[baselen+26];
618 const char *bsuffix =
".old";
620 strcpy(restart_name,
namdMyNode->simParams->restartFilename);
622 sprintf(timestepstr,
".%d",timestep);
623 strcat(restart_name, timestepstr);
626 strcat(restart_name,
".vel");
635 sprintf(comment,
"RESTART VELOCITIES WRITTEN BY NAMD AT TIMESTEP %d", timestep);
639 namdMyNode->pdbData->write(restart_name, comment);
645 write_binary_file(restart_name, n, vel);
648 delete [] restart_name;
658 if ( ! output )
return;
667 if ( ! output )
return;
674 replicaDcdActive = 1;
675 replicaDcdIndex = index;
679 replicaDcdActive = 1;
680 replicaDcdIndex = index;
683 msg->
srcPart = CmiMyPartition();
685 strcpy(msg->
data, filename);
690 replicaDcdFile &f = replicaDcdFiles[msg->
dcdIndex];
692 iout <<
"CLOSING REPLICA DCD FILE " << msg->
dcdIndex <<
" " << f.filename.c_str() <<
"\n" <<
endi;
696 f.filename = (
const char*) msg->
data;
701 if ( ! replicaDcdFiles.count(msg->
dcdIndex) ) {
703 sprintf(err_msg,
"Unknown replicaDcdFile identifier %d\n", msg->
dcdIndex);
706 replicaDcdFile &f = replicaDcdFiles[msg->
dcdIndex];
710 iout <<
"OPENING REPLICA DCD FILE " << msg->
dcdIndex <<
" " << f.filename.c_str() <<
"\n" <<
endi;
716 sprintf(err_msg,
"DCD file %s already exists!!", f.filename.c_str());
718 }
else if (f.fileid < 0) {
720 sprintf(err_msg,
"Couldn't open DCD file %s", f.filename.c_str());
722 }
else if (! f.fileid) {
723 NAMD_bug(
"Output::recvReplicaDcdData open_dcd_write returned fileid of zero");
732 NAMD_err(
"Writing of DCD header failed!!");
737 iout <<
"WRITING TO REPLICA DCD FILE " << msg->
dcdIndex <<
" " << f.filename.c_str() <<
"\n" <<
endi;
738 float *msgx = (
float*) msg->
data;
743 if (ret_code < 0)
NAMD_err(
"Writing of DCD step failed!!");
764 #define RAD2DEG 180.0/3.14159265359
766 int Output::output_dcdfile(
int timestep,
int n,
FloatVector *coor,
773 static float *
x, *
y, *
z;
783 for ( std::map<int,replicaDcdFile>::iterator it = replicaDcdFiles.begin();
784 it != replicaDcdFiles.end(); ++it ) {
785 replicaDcdFile &f = it->second;
787 iout <<
"CLOSING REPLICA DCD FILE " << it->first <<
" " << f.filename.c_str() <<
"\n" <<
endi;
805 if ( replicaDcdActive ) {
808 float *msgx = (
float*) msg->
data;
809 float *msgy = msgx + n;
810 float *msgz = msgy + n;
811 for (i=0; i<n; i++) { msgx[i] = coor[i].
x; }
812 for (i=0; i<n; i++) { msgy[i] = coor[i].
y; }
813 for (i=0; i<n; i++) { msgz[i] = coor[i].
z; }
818 msg->
NPRIV = timestep;
822 msg->
srcPart = CmiMyPartition();
834 delete []
x; x =
new float[3*n];
841 iout <<
"OPENING COORDINATE DCD FILE\n" <<
endi;
849 sprintf(err_msg,
"DCD file %s already exists!!",
858 sprintf(err_msg,
"Couldn't open DCD file %s",
864 int NSAVC, NFILE, NPRIV, NSTEP;
867 NSTEP = NPRIV - NSAVC;
873 n, NFILE, NPRIV, NSAVC, NSTEP,
879 NAMD_err(
"Writing of DCD header failed!!");
894 iout <<
"WRITING COORDINATES TO DCD FILE " << simParams->
dcdFilename <<
" AT STEP "
895 << timestep <<
"\n" <<
endi;
906 NAMD_err(
"Writing of DCD step failed!!");
928 void Output::output_final_coordinates(
Vector *coor,
int n,
int timestep)
931 char output_name[140];
935 strcpy(output_name,
namdMyNode->simParams->outputFilename);
936 strcat(output_name,
".coor");
944 sprintf(comment,
"FINAL COORDINATES WRITTEN BY NAMD AT TIMESTEP %d", timestep);
947 namdMyNode->pdbData->write(output_name, comment);
952 write_binary_file(output_name, n, coor);
971 void Output::output_final_velocities(
int timestep,
int n,
Vector *vel)
974 char output_name[140];
978 strcpy(output_name,
namdMyNode->simParams->outputFilename);
979 strcat(output_name,
".vel");
987 sprintf(comment,
"FINAL VELOCITIES WRITTEN BY NAMD AT TIMESTEP %d", timestep);
991 namdMyNode->pdbData->write(output_name, comment);
997 write_binary_file(output_name, n, vel);
1018 void Output::output_veldcdfile(
int timestep,
int n,
Vector *vel)
1023 static float *
x, *
y, *
z;
1033 iout <<
"CLOSING VELOCITY DCD FILE\n" <<
endi;
1036 iout <<
"VELOCITY DCD FILE WAS NOT CREATED\n" <<
endi;
1048 if ( n > n_alloc ) {
1049 delete []
x; x =
new float[3*n];
1056 iout <<
"OPENING VELOCITY DCD FILE\n" <<
endi;
1064 sprintf(err_msg,
"Velocity DCD file %s already exists!!",
1069 else if (fileid < 0)
1073 sprintf(err_msg,
"Couldn't open velocity DCD file %s",
1079 int NSAVC, NFILE, NPRIV, NSTEP;
1082 NSTEP = NPRIV - NSAVC;
1086 const int with_unitcell = 0;
1089 n, NFILE, NPRIV, NSAVC, NSTEP,
1095 NAMD_err(
"Writing of velocity DCD header failed!!");
1110 iout <<
"WRITING VELOCITIES TO DCD FILE AT STEP "
1111 << timestep <<
"\n" <<
endi;
1117 NAMD_err(
"Writing of velocity DCD step failed!!");
1137 void Output::output_forces(
int timestep,
int n,
Vector *frc)
1140 char output_name[140];
1144 strcpy(output_name,
namdMyNode->simParams->outputFilename);
1145 strcat(output_name,
".force");
1153 sprintf(comment,
"FORCES WRITTEN BY NAMD AT TIMESTEP %d", timestep);
1156 namdMyNode->pdbData->write(output_name, comment);
1161 write_binary_file(output_name, n, frc);
1182 void Output::output_forcedcdfile(
int timestep,
int n,
Vector *frc)
1187 static float *
x, *
y, *
z;
1197 iout <<
"CLOSING FORCE DCD FILE\n" <<
endi;
1200 iout <<
"FORCE DCD FILE WAS NOT CREATED\n" <<
endi;
1210 if ( n > n_alloc ) {
1211 delete []
x; x =
new float[3*n];
1218 iout <<
"OPENING FORCE DCD FILE\n" <<
endi;
1226 sprintf(err_msg,
"Force DCD file %s already exists!!",
1231 else if (fileid < 0)
1235 sprintf(err_msg,
"Couldn't open force DCD file %s",
1241 int NSAVC, NFILE, NPRIV, NSTEP;
1244 NSTEP = NPRIV - NSAVC;
1248 const int with_unitcell = 0;
1251 n, NFILE, NPRIV, NSAVC, NSTEP,
1257 NAMD_err(
"Writing of force DCD header failed!!");
1272 iout <<
"WRITING FORCES TO DCD FILE AT STEP "
1273 << timestep <<
"\n" <<
endi;
1279 NAMD_err(
"Writing of force DCD step failed!!");
1299 void Output::write_binary_file(
char *fname,
int n,
Vector *vecs)
1308 sprintf(errmsg,
"Error on write to binary file %s", fname);
1333 void Output::scale_vels(
Vector *v,
int n,
Real fact)
1348 #ifdef MEM_OPT_VERSION
1349 void ParOutput::velocityMaster(
int timestep,
int n){
1353 if ( timestep >= 0 ) {
1359 output_veldcdfile_master(timestep, n);
1366 iout <<
"WRITING VELOCITIES TO RESTART FILE AT STEP "
1367 << timestep <<
"\n" <<
endi;
1368 output_restart_velocities_master(timestep, n);
1369 iout <<
"FINISHED WRITING RESTART VELOCITIES\n" <<
endi;
1380 iout <<
"WRITING VELOCITIES TO OUTPUT FILE AT STEP "
1381 << realstep <<
"\n" <<
endi;
1383 output_final_velocities_master(n);
1396 void ParOutput::velocitySlave(
int timestep,
int fID,
int tID,
Vector *vecs){
1399 if ( timestep >= 0 ) {
1405 output_veldcdfile_slave(timestep, fID, tID, vecs);
1413 output_restart_velocities_slave(timestep, fID, tID, vecs, offset);
1422 output_final_velocities_slave(fID, tID, vecs, offset);
1433 void ParOutput::output_veldcdfile_master(
int timestep,
int n){
1440 if ( ! veldcdFirst ) {
1441 iout <<
"CLOSING VELOCITY DCD FILE\n" <<
endi;
1444 iout <<
"VELOCITY DCD FILE WAS NOT CREATED\n" <<
endi;
1452 iout <<
"OPENING VELOCITY DCD FILE\n" <<
endi;
1454 #ifndef OUTPUT_SINGLE_FILE
1455 #error OUTPUT_SINGLE_FILE not defined!
1458 #if OUTPUT_SINGLE_FILE
1461 char *veldcdFilename = buildFileName(
veldcdType);
1469 sprintf(err_msg,
"Velocity DCD file %s already exists!",veldcdFilename);
1472 else if (veldcdFileID < 0)
1475 sprintf(err_msg,
"Couldn't open velocity DCD file %s",veldcdFilename);
1479 #if !OUTPUT_SINGLE_FILE
1484 NAMD_write(veldcdFileID, (
char *) &tmpFlt,
sizeof(
float));
1489 int NSAVC, NFILE, NPRIV, NSTEP;
1492 NSTEP = NPRIV - NSAVC;
1496 const int with_unitcell = 0;
1499 n, NFILE, NPRIV, NSAVC, NSTEP,
1505 NAMD_err(
"Writing of velocity DCD header failed!!");
1508 #if !OUTPUT_SINGLE_FILE
1510 delete [] veldcdFilename;
1513 veldcdFirst =
FALSE;
1517 iout <<
"WRITING VELOCITIES TO DCD FILE AT STEP "
1518 << timestep <<
"\n" <<
endi;
1525 #if OUTPUT_SINGLE_FILE
1527 int totalAtoms =
namdMyNode->molecule->numAtoms;
1536 void ParOutput::output_veldcdfile_slave(
int timestep,
int fID,
int tID,
Vector *vecs){
1543 if ( ! veldcdFirst ) {
1546 #if OUTPUT_SINGLE_FILE
1554 int parN = tID-fID+1;
1559 #if OUTPUT_SINGLE_FILE
1560 char *veldcdFilename =
namdMyNode->simParams->velDcdFilename;
1562 char *veldcdFilename = buildFileName(
veldcdType);
1565 if(veldcdFileID < 0)
1568 sprintf(err_msg,
"Couldn't open velocity DCD file %s",veldcdFilename);
1571 #if OUTPUT_SINGLE_FILE
1576 veldcdX =
new float[parN];
1577 veldcdY =
new float[parN];
1578 veldcdZ =
new float[parN];
1585 #if !OUTPUT_SINGLE_FILE
1586 delete [] veldcdFilename;
1591 NAMD_write(veldcdFileID, (
char *) &tmpFlt,
sizeof(
float));
1592 NAMD_write(veldcdFileID, (
char *) &outputID,
sizeof(
int));
1593 NAMD_write(veldcdFileID, (
char *) &fID,
sizeof(
int));
1594 NAMD_write(veldcdFileID, (
char *) &tID,
sizeof(
int));
1597 veldcdFirst =
FALSE;
1600 #if OUTPUT_SINGLE_FILE
1604 CmiAssert(
sizeof(off_t)==8);
1605 int totalAtoms =
namdMyNode->molecule->numAtoms;
1607 for(
int i=0; i<parN; i++){
1608 veldcdX[i] = vecs[i].
x;
1609 veldcdY[i] = vecs[i].
y;
1610 veldcdZ[i] = vecs[i].
z;
1617 int atomsRemains = (totalAtoms-1)-(tID+1)+1;
1618 off_t offset = ((off_t)atomsRemains)*
sizeof(float)+1*
sizeof(
int);
1623 NAMD_write(veldcdFileID, (
char *)×tep,
sizeof(
int));
1629 void ParOutput::output_restart_velocities_master(
int timestep,
int n){
1630 #if OUTPUT_SINGLE_FILE
1631 char timestepstr[20];
1633 int baselen = strlen(
namdMyNode->simParams->restartFilename);
1634 char *restart_name =
new char[baselen+26];
1636 strcpy(restart_name,
namdMyNode->simParams->restartFilename);
1638 sprintf(timestepstr,
".%d",timestep);
1639 strcat(restart_name, timestepstr);
1641 strcat(restart_name,
".vel");
1643 char *restart_name = NULL;
1645 restart_name = buildFileName(
velType);
1647 restart_name = buildFileName(
velType,timestep);
1653 write_binary_file_master(restart_name, n);
1655 delete [] restart_name;
1658 void ParOutput::output_restart_velocities_slave(
int timestep,
int fID,
int tID,
Vector *vecs,
int64 offset){
1659 #if OUTPUT_SINGLE_FILE
1660 char timestepstr[20];
1662 int baselen = strlen(
namdMyNode->simParams->restartFilename);
1663 char *restart_name =
new char[baselen+26];
1665 strcpy(restart_name,
namdMyNode->simParams->restartFilename);
1667 sprintf(timestepstr,
".%d",timestep);
1668 strcat(restart_name, timestepstr);
1670 strcat(restart_name,
".vel");
1672 char *restart_name = NULL;
1674 restart_name = buildFileName(
velType);
1676 restart_name = buildFileName(
velType,timestep);
1682 write_binary_file_slave(restart_name, fID, tID, vecs, offset);
1684 delete [] restart_name;
1687 void ParOutput::output_final_velocities_master(
int n){
1688 #if OUTPUT_SINGLE_FILE
1689 char *output_name =
new char[strlen(
namdMyNode->simParams->outputFilename)+8];
1691 strcpy(output_name,
namdMyNode->simParams->outputFilename);
1692 strcat(output_name,
".vel");
1694 char *output_name = buildFileName(
velType);
1700 write_binary_file_master(output_name, n);
1703 void ParOutput::output_final_velocities_slave(
int fID,
int tID,
Vector *vecs,
int64 offset){
1704 #if OUTPUT_SINGLE_FILE
1705 char *output_name =
new char[strlen(
namdMyNode->simParams->outputFilename)+8];
1707 strcpy(output_name,
namdMyNode->simParams->outputFilename);
1708 strcat(output_name,
".vel");
1710 char *output_name = buildFileName(
velType);
1715 write_binary_file_slave(output_name, fID, tID, vecs, offset);
1717 delete [] output_name;
1720 void ParOutput::write_binary_file_master(
char *fname,
int n){
1727 sprintf(errmsg,
"Error on write to binary file %s", fname);
1729 #if !OUTPUT_SINGLE_FILE
1734 NAMD_write(fd, (
char *) &tmpFlt,
sizeof(
float), errmsg);
1735 tmpInt =
namdMyNode->simParams->numoutputprocs;
1745 void ParOutput::write_binary_file_slave(
char *fname,
int fID,
int tID,
Vector *vecs,
int64 offset){
1748 #if OUTPUT_SINGLE_FILE
1750 FILE *ofp = fopen(fname,
"rb+");
1752 sprintf(errmsg,
"Error on opening binary file %s", fname);
1758 if ( _fseeki64(ofp, offset, SEEK_SET) )
1760 if ( fseeko(ofp, offset, SEEK_SET) )
1763 sprintf(errmsg,
"Error on seeking binary file %s", fname);
1768 FILE *ofp = fopen(fname,
"wb+");
1770 sprintf(errmsg,
"Error on opening binary file %s", fname);
1777 fwrite(&tmpInt,
sizeof(
int32), 1, ofp);
1778 fwrite(&tmpFlt,
sizeof(
float), 1, ofp);
1779 fwrite(&outputID,
sizeof(
int), 1, ofp);
1780 fwrite(&fID,
sizeof(
int), 1, ofp);
1781 fwrite(&tID,
sizeof(
int), 1, ofp);
1784 int parN = tID-fID+1;
1785 if ( fwrite(vecs,
sizeof(
Vector), parN, ofp) != parN ) {
1786 sprintf(errmsg,
"Error on writing to binary file %s", fname);
1790 if ( fclose(ofp) ) {
1791 sprintf(errmsg,
"Error on closing binary file %s", fname);
1799 void ParOutput::forceMaster(
int timestep,
int n){
1802 if ( timestep >= 0 ) {
1808 output_forcedcdfile_master(timestep, n);
1817 iout <<
"WRITING FORCES TO OUTPUT FILE AT STEP "
1818 << realstep <<
"\n" <<
endi;
1820 output_forces_master(n);
1827 void ParOutput::forceSlave(
int timestep,
int fID,
int tID,
Vector *vecs){
1830 if ( timestep >= 0 ) {
1836 output_forcedcdfile_slave(timestep, fID, tID, vecs);
1845 output_forces_slave(fID, tID, vecs, offset);
1851 void ParOutput::output_forcedcdfile_master(
int timestep,
int n){
1858 if ( ! forcedcdFirst ) {
1859 iout <<
"CLOSING FORCE DCD FILE\n" <<
endi;
1862 iout <<
"FORCE DCD FILE WAS NOT CREATED\n" <<
endi;
1870 iout <<
"OPENING FORCE DCD FILE\n" <<
endi;
1872 #if OUTPUT_SINGLE_FILE
1883 sprintf(err_msg,
"Force DCD file %s already exists!",forcedcdFilename);
1886 else if (forcedcdFileID < 0)
1889 sprintf(err_msg,
"Couldn't open force DCD file %s",forcedcdFilename);
1893 #if !OUTPUT_SINGLE_FILE
1898 NAMD_write(forcedcdFileID, (
char *) &tmpFlt,
sizeof(
float));
1903 int NSAVC, NFILE, NPRIV, NSTEP;
1906 NSTEP = NPRIV - NSAVC;
1910 const int with_unitcell = 0;
1913 n, NFILE, NPRIV, NSAVC, NSTEP,
1919 NAMD_err(
"Writing of force DCD header failed!!");
1922 #if !OUTPUT_SINGLE_FILE
1924 delete [] forcedcdFilename;
1927 forcedcdFirst =
FALSE;
1931 iout <<
"WRITING FORCES TO DCD FILE AT STEP "
1932 << timestep <<
"\n" <<
endi;
1939 #if OUTPUT_SINGLE_FILE
1941 int totalAtoms =
namdMyNode->molecule->numAtoms;
1950 void ParOutput::output_forcedcdfile_slave(
int timestep,
int fID,
int tID,
Vector *vecs){
1957 if ( ! forcedcdFirst ) {
1960 #if OUTPUT_SINGLE_FILE
1961 delete [] forcedcdX;
1962 delete [] forcedcdY;
1963 delete [] forcedcdZ;
1968 int parN = tID-fID+1;
1973 #if OUTPUT_SINGLE_FILE
1974 char *forcedcdFilename =
namdMyNode->simParams->forceDcdFilename;
1979 if(forcedcdFileID < 0)
1982 sprintf(err_msg,
"Couldn't open force DCD file %s",forcedcdFilename);
1985 #if OUTPUT_SINGLE_FILE
1990 forcedcdX =
new float[parN];
1991 forcedcdY =
new float[parN];
1992 forcedcdZ =
new float[parN];
1999 #if !OUTPUT_SINGLE_FILE
2000 delete [] forcedcdFilename;
2005 NAMD_write(forcedcdFileID, (
char *) &tmpFlt,
sizeof(
float));
2006 NAMD_write(forcedcdFileID, (
char *) &outputID,
sizeof(
int));
2007 NAMD_write(forcedcdFileID, (
char *) &fID,
sizeof(
int));
2008 NAMD_write(forcedcdFileID, (
char *) &tID,
sizeof(
int));
2011 forcedcdFirst =
FALSE;
2014 #if OUTPUT_SINGLE_FILE
2018 CmiAssert(
sizeof(off_t)==8);
2019 int totalAtoms =
namdMyNode->molecule->numAtoms;
2021 for(
int i=0; i<parN; i++){
2022 forcedcdX[i] = vecs[i].
x;
2023 forcedcdY[i] = vecs[i].
y;
2024 forcedcdZ[i] = vecs[i].
z;
2030 int atomsRemains = (totalAtoms-1)-(tID+1)+1;
2031 off_t offset = ((off_t)atomsRemains)*
sizeof(float)+1*
sizeof(
int);
2035 NAMD_write(forcedcdFileID, (
char *)×tep,
sizeof(
int));
2041 void ParOutput::output_forces_master(
int n){
2042 #if OUTPUT_SINGLE_FILE
2043 char *output_name =
new char[strlen(
namdMyNode->simParams->outputFilename)+8];
2045 strcpy(output_name,
namdMyNode->simParams->outputFilename);
2046 strcat(output_name,
".force");
2048 char *output_name = buildFileName(
forceType);
2054 write_binary_file_master(output_name, n);
2057 void ParOutput::output_forces_slave(
int fID,
int tID,
Vector *vecs,
int64 offset){
2058 #if OUTPUT_SINGLE_FILE
2059 char *output_name =
new char[strlen(
namdMyNode->simParams->outputFilename)+8];
2061 strcpy(output_name,
namdMyNode->simParams->outputFilename);
2062 strcat(output_name,
".force");
2064 char *output_name = buildFileName(
forceType);
2069 write_binary_file_slave(output_name, fID, tID, vecs, offset);
2071 delete [] output_name;
2077 void ParOutput::coordinateMaster(
int timestep,
int n,
Lattice &lat){
2080 if ( timestep >= 0 ) {
2085 output_dcdfile_master(timestep, n,
2093 iout <<
"WRITING COORDINATES TO RESTART FILE AT STEP "
2094 << timestep <<
"\n" <<
endi;
2095 output_restart_coordinates_master(timestep, n);
2096 iout <<
"FINISHED WRITING RESTART COORDINATES\n" <<
endi;
2127 iout <<
"WRITING COORDINATES TO OUTPUT FILE AT STEP "
2128 << realstep <<
"\n" <<
endi;
2130 output_final_coordinates_master(n);
2140 void ParOutput::coordinateSlave(
int timestep,
int fID,
int tID,
Vector *vecs,
FloatVector *fvecs){
2143 if ( timestep >= 0 ) {
2148 output_dcdfile_slave(timestep, fID, tID, fvecs);
2156 output_restart_coordinates_slave(timestep, fID, tID, vecs, offset);
2185 output_final_coordinates_slave(fID, tID, vecs, offset);
2195 void ParOutput::output_dcdfile_master(
int timestep,
int n,
const Lattice *lattice){
2203 iout <<
"CLOSING COORDINATE DCD FILE\n" <<
endi;
2206 iout <<
"COORDINATE DCD FILE WAS NOT CREATED\n" <<
endi;
2214 iout <<
"OPENING COORDINATE DCD FILE\n" <<
endi;
2216 #if OUTPUT_SINGLE_FILE
2219 char *dcdFilename = buildFileName(
dcdType);
2228 sprintf(err_msg,
"DCD file %s already exists!!",dcdFilename);
2231 else if (dcdFileID < 0)
2234 sprintf(err_msg,
"Couldn't open DCD file %s",dcdFilename);
2238 #if !OUTPUT_SINGLE_FILE
2243 NAMD_write(dcdFileID, (
char *) &tmpFlt,
sizeof(
float));
2248 int NSAVC, NFILE, NPRIV, NSTEP;
2251 NSTEP = NPRIV - NSAVC;
2257 n, NFILE, NPRIV, NSAVC, NSTEP,
2263 NAMD_err(
"Writing of DCD header failed!!");
2266 #if !OUTPUT_SINGLE_FILE
2268 delete [] dcdFilename;
2275 iout <<
"WRITING COORDINATES TO DCD FILE AT STEP "
2276 << timestep <<
"\n" <<
endi;
2291 #if OUTPUT_SINGLE_FILE
2293 int totalAtoms =
namdMyNode->molecule->numAtoms;
2300 void ParOutput::output_dcdfile_slave(
int timestep,
int fID,
int tID,
FloatVector *fvecs){
2310 #if OUTPUT_SINGLE_FILE
2318 int parN = tID-fID+1;
2323 #if OUTPUT_SINGLE_FILE
2326 char *dcdFilename = buildFileName(
dcdType);
2332 sprintf(err_msg,
"Couldn't open DCD file %s", dcdFilename);
2336 #if OUTPUT_SINGLE_FILE
2337 dcdX =
new float[parN];
2338 dcdY =
new float[parN];
2339 dcdZ =
new float[parN];
2344 skipbytes +=
sizeof(int)*2 + 6*
sizeof(
double);
2349 #if !OUTPUT_SINGLE_FILE
2350 delete [] dcdFilename;
2356 NAMD_write(dcdFileID, (
char *) &tmpFlt,
sizeof(
float));
2357 NAMD_write(dcdFileID, (
char *) &outputID,
sizeof(
int));
2358 NAMD_write(dcdFileID, (
char *) &fID,
sizeof(
int));
2359 NAMD_write(dcdFileID, (
char *) &tID,
sizeof(
int));
2364 #if OUTPUT_SINGLE_FILE
2368 CmiAssert(
sizeof(off_t)==8);
2369 int totalAtoms =
namdMyNode->molecule->numAtoms;
2371 for(
int i=0; i<parN; i++){
2372 dcdX[i] = fvecs[i].
x;
2373 dcdY[i] = fvecs[i].
y;
2374 dcdZ[i] = fvecs[i].
z;
2389 int atomsRemains = (totalAtoms-1)-(tID+1)+1;
2390 off_t offset = ((off_t)atomsRemains)*
sizeof(float)+1*
sizeof(
int);
2393 offset +=
sizeof(int)*2 + 6*
sizeof(
double);
2398 NAMD_write(dcdFileID, (
char *)×tep,
sizeof(
int));
2404 void ParOutput::output_restart_coordinates_master(
int timestep,
int n){
2405 #if OUTPUT_SINGLE_FILE
2406 char timestepstr[20];
2408 int baselen = strlen(
namdMyNode->simParams->restartFilename);
2409 char *restart_name =
new char[baselen+26];
2411 strcpy(restart_name,
namdMyNode->simParams->restartFilename);
2413 sprintf(timestepstr,
".%d",timestep);
2414 strcat(restart_name, timestepstr);
2416 strcat(restart_name,
".coor");
2418 char *restart_name = NULL;
2420 restart_name = buildFileName(
coorType);
2422 restart_name = buildFileName(
coorType,timestep);
2428 write_binary_file_master(restart_name, n);
2430 delete [] restart_name;
2432 void ParOutput::output_restart_coordinates_slave(
int timestep,
int fID,
int tID,
Vector *vecs,
int64 offset){
2433 #if OUTPUT_SINGLE_FILE
2434 char timestepstr[20];
2436 int baselen = strlen(
namdMyNode->simParams->restartFilename);
2437 char *restart_name =
new char[baselen+26];
2439 strcpy(restart_name,
namdMyNode->simParams->restartFilename);
2441 sprintf(timestepstr,
".%d",timestep);
2442 strcat(restart_name, timestepstr);
2444 strcat(restart_name,
".coor");
2446 char *restart_name = NULL;
2448 restart_name = buildFileName(
coorType);
2450 restart_name = buildFileName(
coorType,timestep);
2456 write_binary_file_slave(restart_name, fID, tID, vecs, offset);
2458 delete [] restart_name;
2461 void ParOutput::output_final_coordinates_master(
int n){
2462 #if OUTPUT_SINGLE_FILE
2463 char *output_name =
new char[strlen(
namdMyNode->simParams->outputFilename)+8];
2466 strcpy(output_name,
namdMyNode->simParams->outputFilename);
2467 strcat(output_name,
".coor");
2469 char *output_name = buildFileName(
coorType);
2475 write_binary_file_master(output_name, n);
2477 delete [] output_name;
2479 void ParOutput::output_final_coordinates_slave(
int fID,
int tID,
Vector *vecs,
int64 offset){
2480 #if OUTPUT_SINGLE_FILE
2481 char *output_name =
new char[strlen(
namdMyNode->simParams->outputFilename)+8];
2484 strcpy(output_name,
namdMyNode->simParams->outputFilename);
2485 strcat(output_name,
".coor");
2487 char *output_name = buildFileName(
coorType);
2492 write_binary_file_slave(output_name, fID, tID, vecs, offset);
2494 delete [] output_name;
2499 #if !OUTPUT_SINGLE_FILE
2500 char *ParOutput::buildFileName(
OUTPUTFILETYPE type,
int timestep){
2501 char *filename = NULL;
2502 const char *typeName = NULL;
2508 typeName =
"forcedcd";
2511 typeName =
"veldcd";
2523 typeName =
"invalid";
2526 int baselen = strlen(
namdMyNode->simParams->outputFilename);
2527 filename =
new char[baselen+32];
2528 memset(filename, 0, baselen+32);
2531 strcpy(filename,
namdMyNode->simParams->outputFilename);
2534 if(access(filename, F_OK)!=0) {
2535 int ret =
MKDIR(filename);
2538 sprintf(errmsg,
"Error in creating top-level directory %s!", filename);
2544 strcat(filename, typeName);
2547 if(access(filename, F_OK)!=0) {
2548 int ret =
MKDIR(filename);
2551 sprintf(errmsg,
"Error in creating middle-level directory %s!", filename);
2560 if(outputID == -1) {
2562 if(timestep!=-9999) {
2564 sprintf(tmpstr,
"%smeta.%d",
PATHSEPSTR, timestep);
2570 sprintf(tmpstr,
"%s%d",
PATHSEPSTR, outputID);
2571 strcat(filename, tmpstr);
2573 if(access(filename, F_OK)!=0) {
2574 int ret =
MKDIR(filename);
2577 sprintf(errmsg,
"Error in creating last-level directory %s!", filename);
2582 if(timestep!=-9999) {
2584 sprintf(tmpstr,
"%s%s_%d.%d",
PATHSEPSTR,typeName,outputID,timestep);
2586 sprintf(tmpstr,
"%s%s_%d",
PATHSEPSTR,typeName,outputID);
2590 strcat(filename, tmpstr);
int open_dcd_write_par_slave(char *dcdname)
static int velocityNeeded(int)
void NAMD_err(const char *err_msg)
void sendReplicaDcdInit(int dstPart, ReplicaDcdInitMsg *msg, int msgsize)
static int coordinateNeeded(int)
void setReplicaDcdIndex(int index)
int update_dcdstep_par_header(int fd)
ScriptTcl * getScript(void)
SimParameters * simParameters
char velDcdFilename[NAMD_FILENAME_BUFFER_SIZE]
Vector wrap_delta(const Position &pos1) const
std::ostream & endi(std::ostream &s)
std::ostream & iWARN(std::ostream &s)
void coordinate(int, int, Vector *, FloatVector *, Lattice &)
int get_cluster(int anum) const
void gather_coordinates(int timestep, int N, FloatVector *coords)
#define OUTPUT_FILE_VERSION
BigReal length(void) const
static int forceNeeded(int)
int open_dcd_write(const char *dcdname)
void wrap_coor(Vector *coor, Lattice &lattice, double *done)
void recvReplicaDcdInit(ReplicaDcdInitMsg *msg)
int write_dcdstep_par_XYZUnits(int fd, int N)
int write_dcdstep_par_cell(int fd, double *cell)
#define OUTPUT_MAGIC_NUMBER
void sendReplicaDcdData(int dstPart, ReplicaDcdDataMsg *msg, int msgsize)
void replicaDcdInit(int index, const char *filename)
void NAMD_bug(const char *err_msg)
char dcdFilename[NAMD_FILENAME_BUFFER_SIZE]
void velocity(int, int, Vector *)
void recvReplicaDcdData(ReplicaDcdDataMsg *msg)
int write_dcdstep(int fd, int N, float *X, float *Y, float *Z, double *cell)
void NAMD_die(const char *err_msg)
int write_dcdheader(int fd, const char *filename, int N, int NFILE, int NPRIV, int NSAVC, int NSTEP, double DELTA, int with_unitcell)
void NAMD_backup_file(const char *filename, const char *extension)
char forceDcdFilename[NAMD_FILENAME_BUFFER_SIZE]
void sendReplicaDcdAck(int dstPart, ReplicaDcdAckMsg *msg)
int write_dcdstep_par_slave(int fd, int parL, int parU, int N, float *X, float *Y, float *Z)
Vector wrap_nearest_delta(Position pos1) const
void close_dcd_write(int fd)
void wrap_coor_int(xVector *coor, Lattice &lattice, xDone *done)
int get_clusterSize(int anum) const
void force(int, int, Vector *)
static void lattice_to_unitcell(const Lattice *lattice, double *unitcell)