void CSpace::put_coordinates(RealMatrix& coordinates, const Uint elem_idx) const { if (bound_fields().has_coordinates()) { CConnectivity::ConstRow indexes = indexes_for_element(elem_idx); Field& coordinates_field = bound_fields().coordinates(); cf_assert(coordinates.rows() == indexes.size()); cf_assert(coordinates.cols() == coordinates_field.row_size()); for (Uint i=0; i<coordinates.rows(); ++i) { for (Uint j=0; j<coordinates.cols(); ++j) { coordinates(i,j) = coordinates_field[i][j]; } } } else { const ShapeFunction& space_sf = shape_function(); const CEntities& geometry = support(); const ElementType& geometry_etype = element_type(); const ShapeFunction& geometry_sf = geometry_etype.shape_function(); RealMatrix geometry_coordinates = geometry.get_coordinates(elem_idx); cf_assert(coordinates.rows() == space_sf.nb_nodes()); cf_assert(coordinates.cols() == geometry_etype.dimension()); for (Uint node=0; node<space_sf.nb_nodes(); ++node) { coordinates.row(node) = geometry_sf.value( space_sf.local_coordinates().row(node) ) * geometry_coordinates; } } }
vector<double> calculatePercentageContacts(const RealMatrix& M) { vector<long> sum(M.cols()-1, 0); for (uint i=1; i<M.cols(); ++i) for (uint j=0; j<M.rows(); ++j) sum[i-1] += M(j, i); vector<double> occ(M.cols()-1, 0.0); for (uint i=0; i<M.cols()-1; ++i) occ[i] = static_cast<double>(sum[i]) / M.rows(); return(occ); }
Datum blocker(const RealMatrix& Ua, const RealMatrix sa, const vGroup& ensemble, const uint blocksize, uint repeats, ExtractPolicy& policy) { TimeSeries<double> coverlaps; for (uint i=0; i<repeats; ++i) { vector<uint> picks = pickFrames(ensemble.size(), blocksize); if (debug) { cerr << "***Block " << blocksize << ", replica " << i << ", picks " << picks.size() << endl; dumpPicks(picks); } vGroup subset = subgroup(ensemble, picks); boost::tuple<RealMatrix, RealMatrix> pca_result = pca(subset, policy); RealMatrix s = boost::get<0>(pca_result); RealMatrix U = boost::get<1>(pca_result); if (length_normalize) for (uint j=0; j<s.rows(); ++j) s[j] /= blocksize; coverlaps.push_back(covarianceOverlap(sa, Ua, s, U)); } return( Datum(coverlaps.average(), coverlaps.variance(), coverlaps.size()) ); }
inline void copy(const std::vector<Real>& vector, RealMatrix& realmatrix) { cf3_assert(realmatrix.size() == vector.size()); for (Uint row=0; row<realmatrix.rows(); ++row) { for (Uint col=0; col<realmatrix.cols(); ++col) { realmatrix(row,col) = vector[realmatrix.cols()*row + col]; } } }
RealMatrix::RealMatrix(const RealMatrix& A) { delete[] entries; m = A.rows(); n = A.columns(); entries = new double[m * n]; for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) this->operator()(i,j) = A(i,j); }
void CElements::put_coordinates(RealMatrix& elem_coords, const Uint elem_idx) const { const CTable<Real>& coords_table = nodes().coordinates(); CConnectivity::ConstRow elem_nodes = node_connectivity()[elem_idx]; const Uint nb_nodes=elem_coords.rows(); const Uint dim=elem_coords.cols(); for(Uint node = 0; node != nb_nodes; ++node) for (Uint d=0; d<dim; ++d) elem_coords(node,d) = coords_table[elem_nodes[node]][d]; }
void CCellFaces::put_coordinates(RealMatrix& elem_coords, const Uint face_idx) const { const CTable<Real>& coords_table = nodes().coordinates(); std::vector<Uint> face_nodes = m_cell_connectivity->face_nodes(face_idx); const Uint nb_nodes=elem_coords.rows(); const Uint dim=elem_coords.cols(); cf_assert(nb_nodes == face_nodes.size()); cf_assert(dim==coords_table.row_size()); for(Uint node = 0; node != nb_nodes; ++node) for (Uint d=0; d<dim; ++d) elem_coords(node,d) = coords_table[face_nodes[node]][d]; }
int main(int argc, char *argv[]) { string hdr = invocationHeader(argc, argv); opts::BasicOptions* bopts = new opts::BasicOptions(fullHelpMessage()); ToolOptions* topts = new ToolOptions; opts::RequiredArguments* ropts = new opts::RequiredArguments; ropts->addArgument("matrix", "matrix-file"); opts::AggregateOptions options; options.add(bopts).add(topts).add(ropts); if (!options.parse(argc, argv)) exit(-1); string matrix_name = ropts->value("matrix"); RealMatrix A; readAsciiMatrix(matrix_name, A); uint m = A.rows(); if (rows.empty()) for (uint i=0; i<m; ++i) rows.push_back(i); uint resid = 1; uint total_chunks = rows.size() / (chunksize ? chunksize : rows.size()); uint chunk = 1; AtomicGroup model; // Warn about pymol... if (rows.size() >= 100000 && bonds) cerr << "Warning- PyMol may not correctly handle the CONECT records generated by this tool.\n"; for (uint atomid = 0; atomid < rows.size(); ++atomid, ++resid) { if (chunksize && resid > chunksize) { if (bonds) { for (uint i=atomid - chunksize; i<atomid-1; ++i) model[i]->addBond(model[i+1]); } resid = 1; ++chunk; } GCoord c(scales[0] * A(rows[atomid], columns[0]), scales[1] * A(rows[atomid], columns[1]), scales[2] * A(rows[atomid], columns[2])); pAtom pa(new Atom(atomid+1, atom_name, c)); pa->resid(resid); pa->resname(residue_name); ostringstream segstream; segstream << boost::format(segid_fmt) % chunk; pa->segid(segstream.str()); double b; double q = 0.0; if (chunksize) { b = (100.0 * (resid-1)) / chunksize; q = static_cast<double>(chunk-1) / total_chunks; } else b = 100.0 * atomid / rows.size(); pa->bfactor(b); pa->occupancy(q); model.append(pa); } if (bonds) { if (chunksize && resid > 1) for (uint i=rows.size() - chunksize; i<rows.size()-1; ++i) model[i]->addBond(model[i+1]); else if (!chunksize) for (uint i=0; i<rows.size()-1; ++i) model[i]->addBond(model[i+1]); } PDB pdb = PDB::fromAtomicGroup(model); pdb.remarks().add(hdr); cout << pdb; }
RealMatrix Reconstruct::gradient(const RealMatrix& from_states, const CoordRef orientation) const { cf_assert_desc("matrix dimensions don't match ["+to_str((Uint)from_states.rows())+"!="+to_str((Uint)m_gradient_reconstruction_matrix[orientation].cols())+"] ",from_states.rows() == m_gradient_reconstruction_matrix[orientation].cols()); return m_gradient_reconstruction_matrix[orientation] * from_states; }
RealMatrix Reconstruct::value(const RealMatrix& from_states) const { cf_assert_desc("matrix dimensions don't match ["+to_str((Uint)from_states.rows())+"!="+to_str((Uint)m_value_reconstruction_matrix.cols())+"] ",from_states.rows() == m_value_reconstruction_matrix.cols()); return m_value_reconstruction_matrix * from_states; }
int main(int argc, char *argv[]) { string hdr = invocationHeader(argc, argv); opts::BasicOptions* bopts = new opts::BasicOptions(fullHelpMessage()); opts::BasicSelection* sopts = new opts::BasicSelection; opts::BasicTrajectory* tropts = new opts::BasicTrajectory; opts::BasicConvergence* copts = new opts::BasicConvergence; ToolOptions* topts = new ToolOptions; opts::AggregateOptions options; options.add(bopts).add(sopts).add(tropts).add(copts).add(topts); if (!options.parse(argc, argv)) exit(-1); cout << "# " << hdr << endl; cout << "# " << vectorAsStringWithCommas<string>(options.print()) << endl; AtomicGroup model = tropts->model; pTraj traj = tropts->trajectory; if (tropts->skip) cerr << "Warning: --skip option ignored\n"; if (blocksizes.empty()) { uint n = traj->nframes(); uint half = n / 2; uint step = half / nsteps; if (step < 1) step = 1; cout << "# Auto block-sizes - " << step << ":" << step << ":" << half << endl; for (uint i = step; i <= half; i += step) blocksizes.push_back(i); } AtomicGroup subset = selectAtoms(model, sopts->selection); vector<AtomicGroup> ensemble; readTrajectory(ensemble, subset, traj); // First, align the input trajectory... boost::tuple<std::vector<XForm>, greal, int> ares = iterativeAlignment(ensemble); cout << "# Alignment converged to " << boost::get<1>(ares) << " in " << boost::get<2>(ares) << " iterations\n"; cout << "# n\tCoverlap\tVariance\tN_blocks\n"; // Handle the gold-standard, either using the whole input traj, or the alternate traj... NoAlignPolicy policy; RealMatrix Us; RealMatrix UA; if (gold_standard_trajectory_name.empty()) { AtomicGroup avg = averageStructure(ensemble); policy = NoAlignPolicy(avg, local_average); boost::tuple<RealMatrix, RealMatrix> res = pca(ensemble, policy); Us = boost::get<0>(res); UA = boost::get<1>(res); if (length_normalize) for (uint i=0; i<Us.rows(); ++i) Us[i] /= traj->nframes(); } else { // Must read in another trajectory, process it, and get the PCA pTraj gold = createTrajectory(gold_standard_trajectory_name, model); vector<AtomicGroup> gold_ensemble; readTrajectory(gold_ensemble, subset, gold); boost::tuple<vector<XForm>, greal, int> bres = iterativeAlignment(gold_ensemble); cout << "# Gold Alignment converged to " << boost::get<1>(bres) << " in " << boost::get<2>(bres) << " iterations\n"; AtomicGroup avg = averageStructure(gold_ensemble); policy = NoAlignPolicy(avg, local_average); boost::tuple<RealMatrix, RealMatrix> res = pca(gold_ensemble, policy); Us = boost::get<0>(res); UA = boost::get<1>(res); if (length_normalize) for (uint i=0; i<Us.rows(); ++i) Us[i] /= gold->nframes(); } // Now iterate over all requested block sizes... PercentProgress watcher; ProgressCounter<PercentTrigger, EstimatingCounter> slayer(PercentTrigger(0.1), EstimatingCounter(blocksizes.size())); slayer.attach(&watcher); slayer.start(); for (vector<uint>::iterator i = blocksizes.begin(); i != blocksizes.end(); ++i) { Datum result = blocker(UA, Us, ensemble, *i, nreps, policy); cout << *i << "\t" << result.avg_coverlap << "\t" << result.var_coverlap << "\t" << result.nblocks << endl; slayer.update(); } slayer.finish(); }
void check_close(const RealMatrix& a, const RealMatrix& b, const Real eps) { for(Uint i = 0; i != a.rows(); ++i) for(Uint j = 0; j != a.cols(); ++j) BOOST_CHECK_CLOSE(a(i,j), b(i,j), eps); }