/** Create Netcdf file specified by filename and set up dimension and
  * variable IDs. 
  */
int Traj_AmberNetcdf::setupTrajout(std::string const& fname, Topology* trajParm, 
                                   int NframesToWrite, bool append)
{
  if (!append) {
    filename_.SetFileName( fname );
    // Set up title
    if (Title().empty())
      SetTitle("Cpptraj Generated trajectory");
    // Create NetCDF file.
    if ( NC_create( filename_.Full(), NC_AMBERTRAJ, trajParm->Natom(), HasV(),
                    HasBox(), HasT(), true, 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;
    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;
}
Beispiel #2
0
// Traj_NcEnsemble::closeTraj()
void Traj_NcEnsemble::closeTraj() {
# ifdef HAS_PNETCDF
  if (ncid_ == -1) return;
  //ncmpi_end_indep_data( ncid_ ); // Disable independent data mode.
  ncmpi_close( ncid_ ); 
  ncid_ = -1;
# else 
  NC_close();
# endif
}
Beispiel #3
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;
}
Beispiel #4
0
// NetcdfFile::GetNetcdfConventions()
NetcdfFile::NCTYPE NetcdfFile::GetNetcdfConventions(const char* fname) {
    NCTYPE nctype = NC_UNKNOWN;
#ifdef BINTRAJ
    // NOTE: Do not use checkNCerr so this fails silently. Allows routine to
    //       be used in file autodetection.
    if ( nc_open( fname, NC_NOWRITE, &ncid_ ) != NC_NOERR )
        return NC_UNKNOWN;
    nctype = GetNetcdfConventions();
    NC_close();
#else
    mprintf("Error: Compiled without NETCDF support. Recompile with -DBINTRAJ\n");
#endif
    return nctype;
}
Beispiel #5
0
/** First master performs all necessary setup, then sends info to all children.
  */
int Traj_AmberNetcdf::parallelSetupTrajout(FileName const& fname, Topology* trajParm,
                                           CoordinateInfo const& cInfoIn,
                                           int NframesToWrite, bool append,
                                           Parallel::Comm const& commIn)
{
  int err = 0;
  if (commIn.Master()) {
    err = setupTrajout(fname, trajParm, cInfoIn, NframesToWrite, append);
    // NOTE: setupTrajout leaves file open. Should this change?
    NC_close();
  }
  commIn.MasterBcast(&err, 1, MPI_INT);
  if (err != 0) return 1;
  // Synchronize netcdf info on non-master threads.
  Sync(commIn);
  if (!commIn.Master()) {
    // Non masters need filename and allocate Coord
    filename_ = fname;
    if (Coord_ != 0) delete[] Coord_;
    Coord_ = new float[ Ncatom3() ];
  }
  return 0;
}
Beispiel #6
0
// Traj_NcEnsemble::setupTrajout()
int Traj_NcEnsemble::setupTrajout(FileName const& fname, Topology* trajParm,
                                  CoordinateInfo const& cInfoIn,
                                  int NframesToWrite, bool append)
{
  int err = 0;
# ifdef MPI
  if (NoPnetcdf()) return 1;
# endif
  readAccess_ = false;
  if (!append) {
    CoordinateInfo cInfo = cInfoIn;
    // TODO: File output modifications
    SetCoordInfo( cInfo );
#   ifdef MPI
    ensembleStart_ = Parallel::World().Rank();
    ensembleEnd_ = Parallel::World().Rank() + 1;
#   else
    ensembleStart_ = 0;
    ensembleEnd_ = cInfo.EnsembleSize();;
#   endif
    filename_ = fname;
    // Set up title
    if (Title().empty())
      SetTitle("Cpptraj Generated trajectory");
#   ifdef MPI
    if (Parallel::World().Master()) { // Only master creates file.
#   endif
      // Create NetCDF file.
      err = NC_create(filename_.Full(), NC_AMBERENSEMBLE, trajParm->Natom(), CoordInfo(),
                      Title(), debug_);
      // Close Netcdf file. It will be reopened write. FIXME should NC_create leave it closed?
      NC_close();
#   ifdef MPI
    }
    Parallel::World().MasterBcast(&err, 1, MPI_INT);
#   endif
    if (err != 0) return 1;
#   ifdef MPI
    // Synchronize netcdf info on non-master threads
    Sync(Parallel::World());
    // DEBUG: Print info for all ranks
    DebugVIDs();
#   endif
    // 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;
    if (debug_ > 0)
      mprintf("\tNetCDF: Appending %s starting at frame %i\n", filename_.base(), Ncframe());
  }
  // Open file
# ifdef HAS_PNETCDF
  err = ncmpi_open(MPI_COMM_WORLD, filename_.full(), NC_WRITE, MPI_INFO_NULL, &ncid_);
  // TODO: Graceful error handling
# else
  err = NC_openWrite( filename_.Full() );
# endif  
  if ( err != 0 ) {
    mprinterr("Error: Opening Netcdf file %s for Write.\n", filename_.base());
    return 1;
  }

  return 0;
}
Beispiel #7
0
/** Close netcdf file. Set ncid to -1 since it can change between open
  * and close calls.
  */
void Traj_AmberNetcdf::closeTraj() {
  NC_close();
}
Beispiel #8
0
// Traj_NcEnsemble::setupTrajin()
int Traj_NcEnsemble::setupTrajin(FileName const& fname, Topology* trajParm)
{
# ifdef MPI
  if (NoPnetcdf()) return TRAJIN_ERR;
# endif
  readAccess_ = true;
  filename_ = fname;
  //if (openTrajin()) return TRAJIN_ERR;
  // Open single thread for now
  if (NC_openRead( filename_.Full() )) return TRAJIN_ERR;
  // Sanity check - Make sure this is a Netcdf ensemble trajectory
  if ( GetNetcdfConventions() != NC_AMBERENSEMBLE ) {
    mprinterr("Error: Netcdf file %s conventions do not include \"AMBERENSEMBLE\"\n",
              filename_.base());
    return TRAJIN_ERR;
  }
  // This will warn if conventions are not 1.0 
  CheckConventionsVersion();
  // Get title
  SetTitle( GetNcTitle() );
  // Get Frame info
  if ( SetupFrameDim()!=0 ) return TRAJIN_ERR;
  if ( Ncframe() < 1 ) {
    mprinterr("Error: Netcdf file is empty.\n");
    return TRAJIN_ERR;
  }
  // Get ensemble info
  int ensembleSize = SetupEnsembleDim();
  if (ensembleSize < 1) {
    mprinterr("Error: Could not get ensemble dimension info.\n");
    return TRAJIN_ERR;
  }
  // Setup Coordinates/Velocities
  if ( SetupCoordsVelo( useVelAsCoords_, useFrcAsCoords_ )!=0 ) return TRAJIN_ERR;
  // Check that specified number of atoms matches expected number.
  if (Ncatom() != trajParm->Natom()) {
    mprinterr("Error: Number of atoms in NetCDF file %s (%i) does not\n"
              "Error:   match number in associated parmtop (%i)!\n",
              filename_.base(), Ncatom(), trajParm->Natom());
    return TRAJIN_ERR;
  }
  // Setup Time - FIXME: Allowed to fail silently
  SetupTime();
  // Box info
  Box nc_box; 
  if (SetupBox(nc_box, NC_AMBERENSEMBLE) == 1) // 1 indicates an error
    return TRAJIN_ERR;
  // Replica Temperatures - FIXME: Allowed to fail silently
  SetupTemperature();
  // Replica Dimensions
  ReplicaDimArray remdDim;
  if ( SetupMultiD(remdDim) == -1 ) return TRAJIN_ERR;
  // Set traj info: FIXME - no forces yet
  SetCoordInfo( CoordinateInfo(ensembleSize, remdDim, nc_box, HasVelocities(),
                               HasTemperatures(), HasTimes(), false) ); 
  if (debug_>1) NetcdfDebug();
  //closeTraj();
  // Close single thread for now
  NC_close();
  // Set up local ensemble parameters
# ifdef MPI
  ensembleStart_ = Parallel::World().Rank();
  ensembleEnd_ = Parallel::World().Rank() + 1;
# else
  ensembleStart_ = 0;
  ensembleEnd_ = ensembleSize;
# endif
  // DEBUG: Print info for all ranks
  WriteVIDs();
  // Allocate float array
  if (Coord_ != 0) delete[] Coord_;
  Coord_ = new float[ Ncatom3() ];
  return Ncframe();
}
// Traj_AmberRestartNC::closeTraj()
void Traj_AmberRestartNC::closeTraj() {
  NC_close();
}