/** Allocate a character buffer based on number of coords and whether * velocities/box info is present. */ int Traj_AmberRestart::setupTrajout(FileName const& fname, Topology* trajParm, CoordinateInfo const& cInfoIn, int NframesToWrite, bool append) { if (append) { mprinterr("Error: Append not supported for Amber Restart.\n"); return 1; } CoordinateInfo cInfo = cInfoIn; if (!cInfo.HasTemp() && outputTemp_) cInfo.SetTemperature(true); // If temperature requested write time as well or format will break. if (cInfo.HasTemp()) { outputTime_ = true; if (!cInfo.HasTime() && time0_ < 0.0) time0_ = 1.0; } if (cInfo.HasVel() && !outputVel_) cInfo.SetVelocity(false); if (outputTime_) { if (!cInfo.HasTime() && time0_ >= 0) cInfo.SetTime(true); } else cInfo.SetTime(false); SetCoordInfo( cInfo ); if (file_.SetupWrite( fname, debug_ )) return 1; readAccess_ = false; // Set trajectory info natom3_ = trajParm->Natom() * 3; // Calculate the length of coordinate frame in bytes file_.SetupFrameBuffer( natom3_, 12, 6 ); // Dont know ahead of time if velocities will be used, allocate space // just in case. Velocity will not be written if V input is null. file_.ResizeBuffer( natom3_ ); // If box coords are present, allocate extra space for them if (CoordInfo().HasBox()) { numBoxCoords_ = 6; file_.ResizeBuffer( numBoxCoords_ ); } // If number of frames to write == 1 set singleWrite so we dont append // frame # to filename. if (NframesToWrite==1) singleWrite_ = true; // Set up title std::string outTitle = Title(); if (outTitle.empty()) { outTitle.assign("Cpptraj Generated Restart"); outTitle.resize(80, ' '); } else { if ( outTitle.size() > 80) { mprintf("Warning: Amber restart title for %s too long: truncating.\n[%s]\n", file_.Filename().base(), outTitle.c_str()); outTitle.resize(80); } } SetTitle( outTitle ); return 0; }
/** Create Netcdf file specified by filename and set up dimension and * variable IDs. */ int Traj_AmberNetcdf::setupTrajout(FileName const& fname, Topology* trajParm, CoordinateInfo const& cInfoIn, int NframesToWrite, bool append) { readAccess_ = false; if (!append) { CoordinateInfo cInfo = cInfoIn; // Deal with output options // For backwards compatibility always write temperature if remdtraj is true. if (outputTemp_ && !cInfo.HasTemp()) cInfo.SetTemperature(true); // Explicitly write velocity - initial frames may not have velocity info. if (outputVel_ && !cInfo.HasVel()) cInfo.SetVelocity(true); if (outputFrc_ && !cInfo.HasForce()) cInfo.SetForce(true); SetCoordInfo( cInfo ); filename_ = fname; // Set up title if (Title().empty()) SetTitle("Cpptraj Generated trajectory"); // Create NetCDF file. if (NC_create( filename_.Full(), NC_AMBERTRAJ, trajParm->Natom(), CoordInfo(), Title() )) return 1; if (debug_>1) NetcdfDebug(); // Close Netcdf file. It will be reopened write. NC_close(); // Allocate memory if (Coord_!=0) delete[] Coord_; Coord_ = new float[ Ncatom3() ]; } else { // NOTE: File existence is checked for in Trajout // Call setupTrajin to set input parameters. This will also allocate // memory for coords. if (setupTrajin(fname, trajParm) == TRAJIN_ERR) return 1; // Check output options. if (outputTemp_ && !CoordInfo().HasTemp()) mprintf("Warning: Cannot append temperature data to NetCDF file '%s'; no temperature dimension.\n", filename_.base()); if (outputVel_ && !CoordInfo().HasVel()) mprintf("Warning: Cannot append velocity data to NetCDF file '%s'; no velocity dimension.\n", filename_.base()); if (outputFrc_ && !CoordInfo().HasForce()) mprintf("Warning: Cannot append force data to NetCDF file '%s'; no force dimension.\n", filename_.base()); if (debug_ > 0) mprintf("\tNetCDF: Appending %s starting at frame %i\n", filename_.base(), Ncframe()); } // Open file if ( NC_openWrite( filename_.Full() ) != 0 ) { mprinterr("Error: Opening Netcdf file %s for Write.\n", filename_.base()); return 1; } return 0; }
/** Loop over all filenames in replica_filenames, set up TrajectoryIO. */ int TrajIOarray::SetupIOarray(ArgList& argIn, TrajFrameCounter& counter, CoordinateInfo& cInfo, Topology* trajParm) { // Sanity check if (!IOarray_.empty()) { mprinterr("Internal Error: SetupIOarray() has been called twice.\n"); return 1; } // Save arguments that have not been processed so they can be passed // to each replica in turn. Only the lowest replica will use argIn. ArgList argCopy( argIn ); bool lowestRep = true; int rep0Frames = TrajectoryIO::TRAJIN_UNK; // Total frames in replica 0 int totalFrames = TrajectoryIO::TRAJIN_UNK; // Total # frames to be read from ensemble TrajectoryFile::TrajFormatType lastRepFmt = TrajectoryFile::UNKNOWN_TRAJ; // Right now enforce that all replicas have the same metadata as lowest // replica, e.g. if replica 0 has temperature, replica 1 does too etc. for (File::NameArray::const_iterator repfile = replica_filenames_.begin(); repfile != replica_filenames_.end(); ++repfile) { // Detect format TrajectoryFile::TrajFormatType repformat = TrajectoryFile::UNKNOWN_TRAJ; TrajectoryIO* replica0 = TrajectoryFile::DetectFormat( *repfile, repformat ); if ( replica0 == 0 ) { mprinterr("Error: Could not set up replica file %s\n", repfile->full()); return 1; } if (repformat != lastRepFmt) mprintf("\tReading '%s' as %s\n", repfile->full(), TrajectoryFile::FormatString(repformat)); lastRepFmt = repformat; replica0->SetDebug( debug_ ); // Pushing replica0 here allows the destructor to handle it on errors IOarray_.push_back( replica0 ); // Process format-specific read args. Do not exit on error in case // replicas have different formats supporting different args. if (lowestRep) { replica0->processReadArgs( argIn ); } else { ArgList argtmp( argCopy ); replica0->processReadArgs( argtmp ); } // Set up replica for reading and get the number of frames. int nframes = replica0->setupTrajin( *repfile, trajParm ); if (nframes == TrajectoryIO::TRAJIN_ERR) { mprinterr("Error: Could not set up %s for reading.\n", repfile->full()); return 1; } // TODO: Do not allow unknown number of frames? if (lowestRep) { cInfo = replica0->CoordInfo(); rep0Frames = nframes; totalFrames = nframes; if (cInfo.ReplicaDimensions().Ndims() > 0) { mprintf("\tReplica dimensions:\n"); for (int rd = 0; rd < cInfo.ReplicaDimensions().Ndims(); rd++) mprintf("\t\t%i: %s\n", rd+1, cInfo.ReplicaDimensions().Description(rd)); } } else { // Check total frames in this replica against lowest rep. if (nframes != rep0Frames) mprintf("Warning: Replica %s frames (%i) does not match # frames in first replica (%i).\n", repfile->base(), nframes, rep0Frames); //if (repframes < 0) { // mprinterr("Error: RemdTraj: Unknown # of frames in replica.\n"); // return 1; //} if (nframes < totalFrames) { totalFrames = nframes; mprintf("Warning: Setting total # of frames to read from replica ensemble to %i\n", totalFrames); } // Check box info against lowest rep. if ( replica0->CoordInfo().HasBox() != cInfo.HasBox() ) { mprinterr("Error: Replica %s box info does not match first replica.\n", repfile->full()); return 1; } // TODO: Check specific box type // Check velocity info against lowest rep. if ( replica0->CoordInfo().HasVel() != cInfo.HasVel() ) { mprinterr("Error: Replica %s velocity info does not match first replica.\n", repfile->full()); return 1; } // Check # dimensions and types against lowest rep if ( replica0->CoordInfo().ReplicaDimensions() != cInfo.ReplicaDimensions() ) { mprinterr("Error: Replica %s dimension info does not match first replica.\n", repfile->full()); ReplicaDimArray const& thisRepDims = replica0->CoordInfo().ReplicaDimensions(); for (int rd = 0; rd < thisRepDims.Ndims(); rd++) mprinterr("\t\t%i: %s\n", rd+1, thisRepDims.Description(rd)); return 1; } // If temperature/time info does not match set to false. if (cInfo.HasTemp() != replica0->CoordInfo().HasTemp()) cInfo.SetTemperature( false ); if (cInfo.HasTime() != replica0->CoordInfo().HasTime()) cInfo.SetTime( false ); } lowestRep = false; } // Check how many frames will actually be read if (counter.CheckFrameArgs( totalFrames, argIn )) return 1; // Check for errors. if (IOarray_.empty()) { mprinterr("Error: No replica trajectories set up.\n"); return 1; } if (IOarray_.size() != replica_filenames_.size()) { // SANITY CHECK mprinterr("Error: Not all replica files were set up.\n"); return 1; } // Update ensemble size cInfo.SetEnsembleSize( (int)IOarray_.size() ); if (debug_ > 0) cInfo.PrintCoordInfo( replica_filenames_[0].full(), trajParm->c_str() ); return 0; }