// Traj_NcEnsemble::readArray() //TODO RemdValues int Traj_NcEnsemble::readArray(int set, FrameArray& f_ensemble) { # ifdef HAS_PNETCDF MPI_Offset pstart_[4]; MPI_Offset pcount_[4]; # define start_ pstart_ # define count_ pcount_ # endif start_[0] = set; // Frame start_[2] = 0; // Atoms start_[3] = 0; // XYZ count_[0] = 1; // Frame count_[1] = 1; // Ensemble count_[3] = 3; // XYZ //rprintf("DEBUG: Reading frame %i\n", set+1); for (int member = ensembleStart_; member != ensembleEnd_; member++) { # ifdef MPI Frame& frm = f_ensemble[0]; # else Frame& frm = f_ensemble[member]; # endif start_[1] = member; // Ensemble count_[2] = Ncatom(); // Atoms // Read Coords # ifdef HAS_PNETCDF if (checkPNCerr(ncmpi_get_vara_float_all(ncid_, coordVID_, start_, count_, Coord_))) # else if (NC::CheckErr(nc_get_vara_float(ncid_, coordVID_, start_, count_, Coord_))) # endif { rprinterr("Error: Getting coordinates for frame %i\n", set+1); return 1; } FloatToDouble(frm.xAddress(), Coord_); //mprintf("Frm=%8i Rep=%8i ", set+1, member+1); // DEBUG //frm.printAtomCoord(0); // DEBUG // Read Velocities if (velocityVID_ != -1) { # ifdef HAS_PNETCDF if (checkPNCerr(ncmpi_get_vara_float_all(ncid_, velocityVID_, start_, count_, Coord_))) # else if (NC::CheckErr(nc_get_vara_float(ncid_, velocityVID_, start_, count_, Coord_))) # endif { rprinterr("Error: Getting velocities for frame %i\n", set+1); return 1; } FloatToDouble(frm.vAddress(), Coord_); } // Read Box if (cellLengthVID_ != -1) { count_[2] = 3; # ifdef HAS_PNETCDF if (checkPNCerr(ncmpi_get_vara_double_all(ncid_, cellLengthVID_, start_, count_, frm.bAddress()))) # else if (NC::CheckErr(nc_get_vara_double(ncid_, cellLengthVID_, start_, count_, frm.bAddress()))) # endif { rprinterr("Error: Getting cell lengths for frame %i.\n", set+1); return 1; } # ifdef HAS_PNETCDF if (checkPNCerr(ncmpi_get_vara_double_all(ncid_, cellAngleVID_, start_, count_, frm.bAddress()+3))) # else if (NC::CheckErr(nc_get_vara_double(ncid_, cellAngleVID_, start_, count_, frm.bAddress()+3))) # endif { rprinterr("Error: Getting cell angles for frame %i.\n", set+1); return 1; } } // Read Temperature if (TempVID_!=-1) { # ifdef HAS_PNETCDF if (checkPNCerr(ncmpi_get_vara_double_all(ncid_, TempVID_, start_, count_, frm.tAddress()))) # else if (NC::CheckErr(nc_get_vara_double(ncid_, TempVID_, start_, count_, frm.tAddress()))) # endif { rprinterr("Error: Getting replica temperature for frame %i.\n", set+1); return 1; } //fprintf(stderr,"DEBUG: Replica Temperature %lf\n",F->T); } // Read indices if (indicesVID_!=-1) { count_[2] = remd_dimension_; # ifdef HAS_PNETCDF if (checkPNCerr(ncmpi_get_vara_int_all(ncid_, indicesVID_, start_, count_, frm.iAddress()))) # else if (NC::CheckErr(nc_get_vara_int(ncid_, indicesVID_, start_, count_, frm.iAddress()))) # endif { rprinterr("Error: Getting replica indices for frame %i.\n", set+1); return 1; } // DEBUG //char buffer[128]; //char* ptr = buffer; //ptr += sprintf(buffer,"DEBUG:\tReplica indices:"); //for (int dim=0; dim < remd_dimension_; dim++) ptr += sprintf(ptr, " %i", frm.RemdIndices()[dim]); //sprintf(ptr,"\n"); //rprintf("%s", buffer); } } # ifdef HAS_PNETCDF // DEBUG # undef start_ # undef count_ # endif return 0; }
// Traj_AmberNetcdf::writeFrame() int Traj_AmberNetcdf::writeFrame(int set, Frame const& frameOut) { DoubleToFloat(Coord_, frameOut.xAddress()); // Write coords start_[0] = ncframe_; start_[1] = 0; start_[2] = 0; count_[0] = 1; count_[1] = Ncatom(); count_[2] = 3; if (checkNCerr(nc_put_vara_float(ncid_,coordVID_,start_,count_,Coord_)) ) { mprinterr("Error: Netcdf Writing coords frame %i\n", set+1); return 1; } // Write velocity. FIXME: Should check in setup if (CoordInfo().HasVel() && frameOut.HasVelocity()) { DoubleToFloat(Coord_, frameOut.vAddress()); if (checkNCerr(nc_put_vara_float(ncid_, velocityVID_, start_, count_, Coord_)) ) { mprinterr("Error: Netcdf writing velocity frame %i\n", set+1); return 1; } } // Write box if (cellLengthVID_ != -1) { count_[1] = 3; count_[2] = 0; if (checkNCerr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,frameOut.bAddress())) ) { mprinterr("Error: Writing cell lengths frame %i.\n", set+1); return 1; } if (checkNCerr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_, frameOut.bAddress()+3)) ) { mprinterr("Error: Writing cell angles frame %i.\n", set+1); return 1; } } // Write temperature if (TempVID_!=-1) { if ( checkNCerr( nc_put_vara_double(ncid_,TempVID_,start_,count_,frameOut.tAddress())) ) { mprinterr("Error: Writing temperature frame %i.\n", set+1); return 1; } } // Write time if (timeVID_ != -1) { float tVal = (float)frameOut.Time(); if ( checkNCerr( nc_put_vara_float(ncid_,timeVID_,start_,count_,&tVal)) ) { mprinterr("Error: Writing time frame %i.\n", set+1); return 1; } } // Write indices if (indicesVID_ != -1) { count_[1] = remd_dimension_; if ( checkNCerr(nc_put_vara_int(ncid_,indicesVID_,start_,count_,frameOut.iAddress())) ) { mprinterr("Error: Writing indices frame %i.\n", set+1); return 1; } } nc_sync(ncid_); // Necessary after every write?? ++ncframe_; return 0; }
/** Get the specified frame from amber netcdf file * Coords are a 1 dimensional array of format X1,Y1,Z1,X2,Y2,Z2,... */ int Traj_AmberNetcdf::readFrame(int set, Frame& frameIn) { start_[0] = set; start_[1] = 0; start_[2] = 0; count_[0] = 1; count_[1] = Ncatom(); count_[2] = 3; // Get temperature if (TempVID_!=-1) { if ( checkNCerr(nc_get_vara_double(ncid_, TempVID_, start_, count_, frameIn.tAddress())) ) { mprinterr("Error: Getting replica temperature for frame %i.\n", set+1); return 1; } //fprintf(stderr,"DEBUG: Replica Temperature %lf\n",F->T); } // Get time if (timeVID_!=-1) { float time; if (checkNCerr(nc_get_vara_float(ncid_, timeVID_, start_, count_, &time))) { mprinterr("Error: Getting time for frame %i.\n", set + 1); return 1; } frameIn.SetTime( (double)time ); } // Read Coords if ( checkNCerr(nc_get_vara_float(ncid_, coordVID_, start_, count_, Coord_)) ) { mprinterr("Error: Getting coordinates for frame %i\n", set+1); return 1; } FloatToDouble(frameIn.xAddress(), Coord_); // Read Velocities if (velocityVID_ != -1) { if ( checkNCerr(nc_get_vara_float(ncid_, velocityVID_, start_, count_, Coord_)) ) { mprinterr("Error: Getting velocities for frame %i\n", set+1); return 1; } FloatToDouble(frameIn.vAddress(), Coord_); } // Read indices. Input array must be allocated to be size remd_dimension. if (indicesVID_!=-1) { count_[1] = remd_dimension_; if ( checkNCerr(nc_get_vara_int(ncid_, indicesVID_, start_, count_, frameIn.iAddress())) ) { mprinterr("Error: Getting replica indices for frame %i.\n", set+1); return 1; } //mprintf("DEBUG:\tReplica indices:"); //for (int dim=0; dim < remd_dimension_; dim++) mprintf(" %i",remd_indices[dim]); //mprintf("\n"); } // Read box info if (cellLengthVID_ != -1) { count_[1] = 3; count_[2] = 0; if (checkNCerr(nc_get_vara_double(ncid_, cellLengthVID_, start_, count_, frameIn.bAddress()))) { mprinterr("Error: Getting cell lengths for frame %i.\n", set+1); return 1; } if (checkNCerr(nc_get_vara_double(ncid_, cellAngleVID_, start_, count_, frameIn.bAddress()+3))) { mprinterr("Error: Getting cell angles for frame %i.\n", set+1); return 1; } } return 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_NcEnsemble::writeArray() int Traj_NcEnsemble::writeArray(int set, FramePtrArray const& Farray) { # ifdef HAS_PNETCDF MPI_Offset pstart_[4]; MPI_Offset pcount_[4]; # define start_ pstart_ # define count_ pcount_ # endif start_[0] = ncframe_; // Frame start_[2] = 0; // Atoms start_[3] = 0; // XYZ count_[0] = 1; // Frame count_[1] = 1; // Ensemble count_[3] = 3; // XYZ for (int member = ensembleStart_; member != ensembleEnd_; member++) { //rprintf("DEBUG: Writing set %i, member %i\n", set+1, member); # ifdef MPI Frame* frm = Farray[0]; # else Frame* frm = Farray[member]; # endif start_[1] = member; // Ensemble count_[2] = Ncatom(); // Atoms // Write Coords //WriteIndices(); // DEBUG DoubleToFloat(Coord_, frm->xAddress()); # ifdef HAS_PNETCDF if (ncmpi_put_vara_float_all(ncid_, coordVID_, start_, count_, Coord_)) # else if (NC::CheckErr(nc_put_vara_float(ncid_, coordVID_, start_, count_, Coord_))) # endif { mprinterr("Error: Netcdf Writing coords frame %i\n", set+1); return 1; } // Write velocity. if (velocityVID_ != -1) { DoubleToFloat(Coord_, frm->vAddress()); # ifdef HAS_PNETCDF if (ncmpi_put_vara_float_all(ncid_, velocityVID_, start_, count_, Coord_)) # else if (NC::CheckErr(nc_put_vara_float(ncid_, velocityVID_, start_, count_, Coord_)) ) # endif { mprinterr("Error: Netcdf writing velocity frame %i\n", set+1); return 1; } } // Write box if (cellLengthVID_ != -1) { count_[2] = 3; # ifdef HAS_PNETCDF if (ncmpi_put_vara_double_all(ncid_,cellLengthVID_,start_,count_,frm->bAddress())) # else if (NC::CheckErr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,frm->bAddress())) ) # endif { mprinterr("Error: Writing cell lengths frame %i.\n", set+1); return 1; } # ifdef HAS_PNETCDF if (ncmpi_put_vara_double_all(ncid_,cellAngleVID_,start_,count_,frm->bAddress()+3)) # else if (NC::CheckErr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_,frm->bAddress()+3))) # endif { mprinterr("Error: Writing cell angles frame %i.\n", set+1); return 1; } } // Write temperature if (TempVID_!=-1) { # ifdef HAS_PNETCDF if (ncmpi_put_vara_double_all(ncid_,TempVID_,start_,count_,frm->tAddress())) # else if (NC::CheckErr(nc_put_vara_double(ncid_,TempVID_,start_,count_,frm->tAddress()))) # endif { mprinterr("Error: Writing temperature frame %i.\n", set+1); return 1; } } // Write indices if (indicesVID_ != -1) { count_[2] = remd_dimension_; # ifdef HAS_PNETCDF if (ncmpi_put_vara_int_all(ncid_,indicesVID_,start_,count_,frm->iAddress())) # else if (NC::CheckErr(nc_put_vara_int(ncid_,indicesVID_,start_,count_,frm->iAddress()))) # endif { mprinterr("Error: Writing indices frame %i.\n", set+1); return 1; } } } # ifdef HAS_PNETCDF //ncmpi_sync(ncid_); # else nc_sync(ncid_); // Necessary after every write?? # endif ++ncframe_; # ifdef HAS_PNETCDF // DEBUG # undef start_ # undef count_ # endif return 0; }
// Traj_AmberRestartNC::writeFrame() int Traj_AmberRestartNC::writeFrame(int set, double *X, double *V,double *box, double T) { // Set up file for this set bool V_present = (HasV() && V != 0); std::string fname; // Create filename for this set // If just writing 1 frame dont modify output filename if (singleWrite_) fname = filename_.Full(); else fname = NumberFilename(filename_.Full(), set+1); if ( NC_create( fname.c_str(), NC_AMBERRESTART, Ncatom(), V_present, HasBox(), HasT(), (time0_ >= 0), Title() ) ) return 1; // write coords start_[0] = 0; start_[1] = 0; count_[0] = Ncatom(); count_[1] = 3; if (checkNCerr(nc_put_vara_double(ncid_,coordVID_,start_,count_,X)) ) { mprinterr("Error: Netcdf restart Writing coordinates %i\n",set); return 1; } // write velocity if (V_present) { mprintf("DEBUG: Writing V, VID=%i\n",velocityVID_); if (checkNCerr(nc_put_vara_double(ncid_,velocityVID_,start_,count_,V)) ) { mprinterr("Error: Netcdf restart writing velocity %i\n",set); return 1; } } // write box if (cellLengthVID_ != -1) { count_[0] = 3; count_[1] = 0; if (checkNCerr(nc_put_vara_double(ncid_,cellLengthVID_,start_,count_,box)) ) { mprinterr("Error: Writing cell lengths.\n"); return 1; } if (checkNCerr(nc_put_vara_double(ncid_,cellAngleVID_,start_,count_, box+3)) ) { mprinterr("Error: Writing cell angles.\n"); return 1; } } // write time if (timeVID_ != -1) { restartTime_ = (time0_ + (double)set) * dt_; if (checkNCerr(nc_put_var_double(ncid_,timeVID_,&restartTime_)) ) { mprinterr("Error: Writing restart time.\n"); return 1; } } // write temperature if (TempVID_ != -1) { if (checkNCerr(nc_put_var_double(ncid_,TempVID_,&T)) ) { mprinterr("Error: Writing restart temperature.\n"); return 1; } } //nc_sync(ncid_); // Necessary? File about to close anyway... // Close file for this set closeTraj(); return 0; }