void Action_Dihedral::Print() { if (range360_) { DataSet_double* ds = (DataSet_double*)dih_; for (int i = 0; i < ds->Size(); i++) { if ( (*ds)[i] < 0.0 ) (*ds)[i] += 360.0; } } }
int KDE::CalcKDE(DataSet_double& Out, DataSet_1D const& Pdata, std::vector<double> const& Increments, HistBin const& Xdim, double bandwidth) const { int inSize = (int)Pdata.Size(); // Allocate output set, set all to zero. Out.Zero( Xdim.Bins() ); Out.SetDim( Dimension::X, Xdim ); int outSize = (int)Out.Size(); int frame, bin; double increment, val; double total = 0.0; # ifdef _OPENMP int original_num_threads; # pragma omp parallel { # pragma omp master { original_num_threads = omp_get_num_threads(); } } // Ensure we only execute with the desired number of threads if (numthreads_ < original_num_threads) omp_set_num_threads( numthreads_ ); # endif // Calculate KDE, loop over input data # ifdef _OPENMP int mythread; double **P_thread; # pragma omp parallel private(frame, bin, val, increment, mythread) reduction(+:total) { mythread = omp_get_thread_num(); // Prevent race conditions by giving each thread its own histogram # pragma omp master { P_thread = new double*[ numthreads_ ]; for (int nt = 0; nt < numthreads_; nt++) { P_thread[nt] = new double[ outSize ]; std::fill(P_thread[nt], P_thread[nt] + outSize, 0.0); } } # pragma omp barrier # pragma omp for # endif for (frame = 0; frame < inSize; frame++) { val = Pdata.Dval(frame); increment = Increments[frame]; total += increment; // Apply kernel across histogram for (bin = 0; bin < outSize; bin++) # ifdef _OPENMP P_thread[mythread][bin] += # else Out[bin] += # endif (increment * (this->*Kernel_)( (Xdim.Coord(bin) - val) / bandwidth )); } # ifdef _OPENMP } // END parallel block // Combine results from each thread histogram into Out for (int i = 0; i < numthreads_; i++) { for (int j = 0; j < outSize; j++) Out[j] += P_thread[i][j]; delete[] P_thread[i]; } delete[] P_thread; // Restore original number of threads if (original_num_threads != numthreads_) omp_set_num_threads( original_num_threads ); # endif // Normalize for (unsigned int j = 0; j < Out.Size(); j++) Out[j] /= (total * bandwidth); return 0; }
int DataIO_XVG::ReadData(FileName const& fname, DataSetList& datasetlist, std::string const& dsname) { std::vector<std::string> Legends; BufferedLine infile; if (infile.OpenFileRead( fname )) return 1; const char* ptr = infile.Line(); if (ptr == 0) return 1; // Skip any comments while (ptr != 0 && ptr[0] == '#') ptr = infile.Line(); // Try to get set legends while (ptr != 0 && ptr[0] == '@') { ArgList line(ptr, " \t"); if (line.Nargs() > 3 && line[1][0] == 's') { std::string legend = line.GetStringKey("legend"); if (!legend.empty()) { // Spaces will cause issues with data set selection. for (std::string::iterator s = legend.begin(); s != legend.end(); ++s) if (*s == ' ') *s = '_'; Legends.push_back( legend ); } } ptr = infile.Line(); } if (Legends.empty()) { mprinterr("Error: No set legends found in XVG file.\n"); return 1; } if (ptr == 0) { mprinterr("Error: No data in XVG file.\n"); return 1; } // Create 1 data set for each legend DataSetList::DataListType inputSets; for (unsigned int i = 0; i != Legends.size(); i++) { MetaData md( dsname, i ); md.SetLegend( Legends[i] ); DataSet_double* ds = new DataSet_double(); if (ds == 0) return 1; ds->SetMeta( md ); inputSets.push_back( ds ); } mprintf("\t%s has %zu columns of data.\n", fname.base(), inputSets.size()); // Should now be positioned at first line of data. Assume first column is time values. DataSetList::Darray Xvals; int expectedCols = (int)inputSets.size() + 1; while (ptr != 0) { int ncols = infile.TokenizeLine(" \t"); if (ncols != expectedCols) mprinterr("Error: Line %i: %i columns != expected # cols %i\n", infile.LineNumber(), ncols, expectedCols); else { Xvals.push_back( atof( infile.NextToken() ) ); for (unsigned int i = 0; i != inputSets.size(); i++) ((DataSet_double*)inputSets[i])->AddElement( atof( infile.NextToken() ) ); } ptr = infile.Line(); } infile.CloseFile(); return (datasetlist.AddOrAppendSets( "", Xvals, inputSets )); }
// DataIO_Std::Read_1D() int DataIO_Std::Read_1D(std::string const& fname, DataSetList& datasetlist, std::string const& dsname) { ArgList labels; bool hasLabels = false; // Buffer file BufferedLine buffer; if (buffer.OpenFileRead( fname )) return 1; // Read the first line. Attempt to determine the number of columns const char* linebuffer = buffer.Line(); if (linebuffer == 0) return 1; int ntoken = buffer.TokenizeLine( SEPARATORS ); if ( ntoken == 0 ) { mprinterr("Error: No columns detected in %s\n", buffer.Filename().full()); return 1; } // Try to skip past any comments. If line begins with a '#', assume it // contains labels. bool isCommentLine = true; const char* ptr = linebuffer; while (isCommentLine) { // Skip past any whitespace while ( *ptr != '\0' && isspace(*ptr) ) ++ptr; // Assume these are column labels until proven otherwise. if (*ptr == '#') { labels.SetList(ptr+1, SEPARATORS ); if (!labels.empty()) { hasLabels = true; // If first label is Frame assume it is the index column if (labels[0] == "Frame" && indexcol_ == -1) indexcol_ = 0; } linebuffer = buffer.Line(); ptr = linebuffer; if (ptr == 0) { mprinterr("Error: No data found in file.\n"); return 1; } } else // Not a recognized comment character, assume data. isCommentLine = false; } // Special case: check if labels are '#F1 F2 <name> [nframes <#>]'. If so, assume // this is a cluster matrix file. if ((labels.Nargs() == 3 || labels.Nargs() == 5) && labels[0] == "F1" && labels[1] == "F2") { mprintf("Warning: Header format '#F1 F2 <name>' detected, assuming cluster pairwise matrix.\n"); return IS_ASCII_CMATRIX; } // Column user args start from 1 if (indexcol_ > -1) mprintf("\tUsing column %i as index column.\n", indexcol_ + 1); // Should be at first data line. Tokenize the line. ntoken = buffer.TokenizeLine( SEPARATORS ); // If # of data columns does not match # labels, clear labels. if ( !labels.empty() && ntoken != labels.Nargs() ) { labels.ClearList(); hasLabels = false; } // Index column checks if (indexcol_ != -1 ) { if (indexcol_ >= ntoken) { mprinterr("Error: Specified index column %i is out of range (%i columns).\n", indexcol_+1, ntoken); return 1; } if (!onlycols_.Empty() && !onlycols_.InRange(indexcol_)) { mprinterr("Error: Index column %i specified, but not in given column range '%s'\n", indexcol_+1, onlycols_.RangeArg()); return 1; } } // Determine the type of data stored in each column. Assume numbers should // be read with double precision. MetaData md( dsname ); DataSetList::DataListType inputSets; unsigned int nsets = 0; for (int col = 0; col != ntoken; ++col) { std::string token( buffer.NextToken() ); if (!onlycols_.Empty() && !onlycols_.InRange( col )) { mprintf("\tSkipping column %i\n", col+1); inputSets.push_back( 0 ); } else { md.SetIdx( col+1 ); if (hasLabels) md.SetLegend( labels[col] ); if ( col == indexcol_ ) { // Always save the index column as floating point inputSets.push_back( new DataSet_double() ); } else if (validInteger(token)) { // Integer number inputSets.push_back( datasetlist.Allocate(DataSet::INTEGER) ); } else if (validDouble(token)) { // Floating point number inputSets.push_back( new DataSet_double() ); } else { // Assume string. Not allowed for index column. if (col == indexcol_) { mprintf("Warning: '%s' index column %i has string values. No indices will be read.\n", buffer.Filename().full(), indexcol_+1); indexcol_ = -1; } inputSets.push_back( new DataSet_string() ); } inputSets.back()->SetMeta( md ); nsets++; } } if (inputSets.empty() || nsets == 0) { mprinterr("Error: No data detected.\n"); return 1; } // Read in data while (linebuffer != 0) { if ( buffer.TokenizeLine( SEPARATORS ) != ntoken ) { PrintColumnError(buffer.LineNumber()); break; } // Convert data in columns for (int i = 0; i < ntoken; ++i) { const char* token = buffer.NextToken(); if (inputSets[i] != 0) { if (inputSets[i]->Type() == DataSet::DOUBLE) ((DataSet_double*)inputSets[i])->AddElement( atof(token) ); else if (inputSets[i]->Type() == DataSet::INTEGER) ((DataSet_integer*)inputSets[i])->AddElement( atoi(token) ); else ((DataSet_string*)inputSets[i])->AddElement( std::string(token) ); } } //Ndata++; linebuffer = buffer.Line(); } buffer.CloseFile(); mprintf("\tDataFile %s has %i columns, %i lines.\n", buffer.Filename().full(), ntoken, buffer.LineNumber()); // Create list containing only data sets. DataSetList::DataListType mySets; DataSet_double* Xptr = 0; for (int idx = 0; idx != (int)inputSets.size(); idx++) { if (inputSets[idx] != 0) { if ( idx != indexcol_ ) mySets.push_back( inputSets[idx] ); else Xptr = (DataSet_double*)inputSets[idx]; } } mprintf("\tRead %zu data sets.\n", mySets.size()); std::string Xlabel; if (indexcol_ != -1 && indexcol_ < labels.Nargs()) Xlabel = labels[indexcol_]; if (Xptr == 0) datasetlist.AddOrAppendSets(Xlabel, DataSetList::Darray(), mySets); else { datasetlist.AddOrAppendSets(Xlabel, Xptr->Data(), mySets); delete Xptr; } return 0; }