/* * Open the netcdf file, read all dimension and variable IDs, close. * Return the number of frames in the file. */ int Traj_AmberNetcdf::setupTrajin(std::string const& fname, Topology* trajParm) { filename_.SetFileNameWithExpansion( fname ); if (openTrajin()) return TRAJIN_ERR; // Sanity check - Make sure this is a Netcdf trajectory if ( GetNetcdfConventions() != NC_AMBERTRAJ ) { mprinterr("Error: Netcdf file %s conventions do not include \"AMBER\"\n",filename_.base()); return TRAJIN_ERR; } // Get global attributes std::string attrText = GetAttrText("ConventionVersion"); if ( attrText != "1.0") mprintf("Warning: Netcdf file %s has ConventionVersion that is not 1.0 (%s)\n", filename_.base(), attrText.c_str()); // Get title SetTitle( GetAttrText("title") ); // Get Frame info if ( SetupFrame()!=0 ) return TRAJIN_ERR; // Setup Coordinates/Velocities if ( SetupCoordsVelo()!=0 ) return TRAJIN_ERR; SetVelocity( HasVelocities() ); // 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", filename_.base(),Ncatom()); mprinterr(" match number in associated parmtop (%i)!\n",trajParm->Natom()); return TRAJIN_ERR; } // Setup Time if ( SetupTime()!=0 ) return TRAJIN_ERR; // Box info double boxcrd[6]; if (SetupBox(boxcrd, NC_AMBERTRAJ) == 1) // 1 indicates an error return TRAJIN_ERR; SetBox( boxcrd ); // Replica Temperatures - Allowed to fail silently if (SetupTemperature() == 0) SetTemperature( true ); // Replica Dimensions if ( SetupMultiD() == -1 ) return TRAJIN_ERR; // NOTE: TO BE ADDED // labelDID; //int cell_spatialDID, cell_angularDID; //int spatialVID, cell_spatialVID, cell_angularVID; // Amber Netcdf coords are float. Allocate a float array for converting // float to/from double. if (Coord_ != 0) delete[] Coord_; Coord_ = new float[ Ncatom3() ]; if (Veloc_ != 0) delete[] Veloc_; if (velocityVID_ != -1) Veloc_ = new float[ Ncatom3() ]; else Veloc_ = 0; if (debug_>1) NetcdfDebug(); closeTraj(); // NetCDF files are always seekable SetSeekable( true ); return Ncframe(); }
/* * Open the netcdf file, read all dimension and variable IDs, close. * Return the number of frames in the file. */ int Traj_AmberNetcdf::setupTrajin(FileName const& fname, Topology* trajParm) { filename_ = fname; if (openTrajin()) return TRAJIN_ERR; readAccess_ = true; // Sanity check - Make sure this is a Netcdf trajectory if ( GetNetcdfConventions() != NC_AMBERTRAJ ) { mprinterr("Error: Netcdf file %s conventions do not include \"AMBER\"\n",filename_.base()); return TRAJIN_ERR; } // Get global attributes std::string attrText = GetAttrText("ConventionVersion"); if ( attrText != "1.0") mprintf("Warning: Netcdf file %s has ConventionVersion that is not 1.0 (%s)\n", filename_.base(), attrText.c_str()); // Get title SetTitle( GetAttrText("title") ); // Get Frame info if ( SetupFrameDim()!=0 ) return TRAJIN_ERR; if ( Ncframe() < 1 ) { mprinterr("Error: Netcdf file is empty.\n"); return TRAJIN_ERR; } // Setup Coordinates/Velocities if ( SetupCoordsVelo( useVelAsCoords_ )!=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 double boxcrd[6]; if (SetupBox(boxcrd, NC_AMBERTRAJ) == 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(remdDim, Box(boxcrd), HasVelocities(), HasTemperatures(), HasTimes(), false) ); // NOTE: TO BE ADDED // labelDID; //int cell_spatialDID, cell_angularDID; //int spatialVID, cell_spatialVID, cell_angularVID; // Amber Netcdf coords are float. Allocate a float array for converting // float to/from double. if (Coord_ != 0) delete[] Coord_; Coord_ = new float[ Ncatom3() ]; if (debug_>1) NetcdfDebug(); closeTraj(); return Ncframe(); }
/** Determine if Netcdf file contains time; set up timeVID and check units. */ int NetcdfFile::SetupTime() { if ( nc_inq_varid(ncid_, NCTIME, &timeVID_) == NC_NOERR ) { std::string attrText = GetAttrText(timeVID_, "units"); if (attrText!="picosecond") mprintf("Warning: NetCDF file has time units of %s - expected picosecond.\n", attrText.c_str()); // Check for time values which have NOT been filled, which was possible // with netcdf trajectories created by older versions of ptraj/cpptraj. if (ncframe_ > 0 && GetNetcdfConventions() == NC_AMBERTRAJ) { float time; start_[0] = 0; count_[0] = 1; if (checkNCerr(nc_get_vara_float(ncid_, timeVID_, start_, count_, &time))) { mprinterr("Error: Getting time value for NetCDF file.\n"); return -1; } if (time == NC_FILL_FLOAT) { mprintf("Warning: NetCDF file time variable defined but empty. Disabling.\n"); timeVID_ = -1; } } return 0; } timeVID_=-1; return 1; }
/** Set up netcdf restart file for reading, get all variable and dimension IDs. * Also check number of atoms against associated parmtop. */ int Traj_AmberRestartNC::setupTrajin(std::string const& fname, Topology* trajParm) { filename_.SetFileNameWithExpansion( fname ); if (openTrajin()) return TRAJIN_ERR; // Sanity check - Make sure this is a Netcdf restart if ( GetNetcdfConventions() != NC_AMBERRESTART ) { mprinterr("Error: Netcdf restart file %s conventions do not include \"AMBERRESTART\"\n", filename_.base()); return TRAJIN_ERR; } // Get global attributes std::string attrText = GetAttrText("ConventionVersion"); if (attrText!="1.0") mprintf("Warning: Netcdf restart file %s has ConventionVersion that is not 1.0 (%s)\n", filename_.base(), attrText.c_str()); // Get title SetTitle( GetAttrText("title") ); // Setup Coordinates/Velocities if ( SetupCoordsVelo()!=0 ) return TRAJIN_ERR; SetVelocity( HasVelocities() ); // Check that specified number of atoms matches expected number. if (Ncatom() != trajParm->Natom()) { mprinterr("Error: Number of atoms in NetCDF restart file %s (%i) does not\n", filename_.base(), Ncatom()); mprinterr(" match number in associated parmtop (%i)!\n",trajParm->Natom()); return TRAJIN_ERR; } // Setup Time if ( SetupTime()!=0 ) return TRAJIN_ERR; // Box info double boxcrd[6]; if (SetupBox(boxcrd, NC_AMBERRESTART) == 1) // 1 indicates an error return TRAJIN_ERR; SetBox( boxcrd ); // Replica Temperatures - allowed to fail silently if (SetupTemperature() == 0) SetTemperature( true ); if ( SetupMultiD() == -1 ) return TRAJIN_ERR; // NOTE: TO BE ADDED // labelDID; //int cell_spatialDID, cell_angularDID; //int spatialVID, cell_spatialVID, cell_angularVID; closeTraj(); // Only 1 frame for NC restarts return 1; }
// NetcdfFile::GetNetcdfConventions() NetcdfFile::NCTYPE NetcdfFile::GetNetcdfConventions() { NCTYPE nctype = NC_UNKNOWN; std::string attrText = GetAttrText(NC_GLOBAL, "Conventions"); if (attrText == "AMBERENSEMBLE") nctype = NC_AMBERENSEMBLE; else if (attrText == "AMBER") nctype = NC_AMBERTRAJ; else if (attrText == "AMBERRESTART") nctype = NC_AMBERRESTART; else if (attrText.empty()) mprinterr("Error: Could not get conventions from Netcdf file.\n"); else { mprinterr("Error: Netcdf file: Unrecognized conventions \"%s\".\n", attrText.c_str()); mprinterr("Error: Expected \"AMBER\", \"AMBERRESTART\", or \"AMBERENSEMBLE\".\n"); } return nctype; }
/** Setup ncatom, ncatom3, atomDID, coordVID, spatialDID, spatialVID, * velocityVID, frcVID. Check units and spatial dimensions. */ int NetcdfFile::SetupCoordsVelo(bool useVelAsCoords) { int spatial; atomDID_ = GetDimInfo(NCATOM, &ncatom_); if (atomDID_==-1) return 1; ncatom3_ = ncatom_ * 3; // Get coord info coordVID_ = -1; if ( nc_inq_varid(ncid_, NCCOORDS, &coordVID_) == NC_NOERR ) { if (ncdebug_ > 0) mprintf("\tNetcdf file has coordinates.\n"); std::string attrText = GetAttrText(coordVID_, "units"); if (attrText!="angstrom") mprintf("Warning: Netcdf file has length units of %s - expected angstrom.\n", attrText.c_str()); } // Get spatial info spatialDID_ = GetDimInfo(NCSPATIAL, &spatial); if (spatialDID_==-1) return 1; if (spatial!=3) { mprinterr("Error: Expected 3 spatial dimensions, got %i\n",spatial); return 1; } if ( checkNCerr(nc_inq_varid(ncid_, NCSPATIAL, &spatialVID_)) ) { mprintf("Warning: Could not get spatial VID. File may not be Amber NetCDF compliant.\n"); mprintf("Warning: Assuming spatial variables are 'x', 'y', 'z'\n"); } else { start_[0] = 0; count_[0] = 3; char xyz[3]; if (checkNCerr(nc_get_vara_text(ncid_, spatialVID_, start_, count_, xyz))) { mprinterr("Error: Getting spatial variables.\n"); return 1; } if (xyz[0] != 'x' || xyz[1] != 'y' || xyz[2] != 'z') { mprinterr("Error: NetCDF spatial variables are '%c', '%c', '%c', not 'x', 'y', 'z'\n", xyz[0], xyz[1], xyz[2]); return 1; } } // Get velocity info velocityVID_ = -1; if ( nc_inq_varid(ncid_, NCVELO, &velocityVID_) == NC_NOERR ) { if (ncdebug_>0) mprintf("\tNetcdf file has velocities.\n"); } // Return a error if no coords and no velocity if ( coordVID_ == -1 && velocityVID_ == -1 ) { mprinterr("Error: NetCDF file has no coords and no velocities.\n"); return 1; } // If using velocities as coordinates, swap them now. if (useVelAsCoords) { if (velocityVID_ == -1) { mprinterr("Error: Cannot use velocities as coordinates; no velocities present.\n"); return 1; } mprintf("\tUsing velocities as coordinates.\n"); coordVID_ = velocityVID_; velocityVID_ = -1; } // Get force info frcVID_ = -1; if ( nc_inq_varid(ncid_, NCFRC, &frcVID_) == NC_NOERR ) { if (ncdebug_>0) mprintf("\tNetcdf file has forces.\n"); } // Get overall replica and coordinate indices crdidxVID_ = -1; if ( nc_inq_varid(ncid_, NCREMD_REPIDX, &repidxVID_) == NC_NOERR ) { //if (ncdebug_ > 0) mprintf(" Netcdf file has overall replica indices.\n"); if ( checkNCerr(nc_inq_varid(ncid_, NCREMD_CRDIDX, &crdidxVID_)) ) { mprinterr("Error: Getting overall coordinate index variable ID.\n"); return 1; } } else repidxVID_ = -1; return 0; }
/** Get information about a netcdf global attribute. */ std::string NetcdfFile::GetAttrText(const char *attribute) { return GetAttrText(NC_GLOBAL, attribute); }
// 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; } // Get global attributes std::string attrText = GetAttrText("ConventionVersion"); if ( attrText != "1.0") mprintf("Warning: Netcdf file %s has ConventionVersion that is not 1.0 (%s)\n", filename_.base(), attrText.c_str()); // Get title SetTitle( GetAttrText("title") ); // 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_ )!=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 double boxcrd[6]; if (SetupBox(boxcrd, 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, Box(boxcrd), 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(); }