void IndexSet::intersection(const IndexSet& s1, const IndexSet& s2) { int start = std::max(s1.offset(), s2.offset()); int end = std::min(s1.offset() + s1.size(), s2.offset() + s2.size()); int interN = end - start; if(interN < 0) { interN = 0; start = -1; } this->offset_ = start; this->size_ = interN; }
void SegmentScan::split(const PointScan &pscan, const IndexSet &idx) { /* No segment to fit */ if(idx.size() < 2) return; /* Fit line segment between first and last point (only do least squares fitting when splitting has ended) */ LineSegment segment(pscan[idx.indexBegin()], pscan[idx.indexEnd() - 1]); #if 0 /* Minimal segment */ if(idx.size() == 2) { const UncertainLineSegment usegment = fittingFunction(pscan, idx); const LineSegmentTuple tuple = { usegment, idx }; clusterSupport.append(tuple); return; } #endif /* Find most distant point from the line */ int maxDistIdx; double maxDist = findMaximumDistance(pscan, idx, segment, &maxDistIdx); /* Split recursively if necessary, otherwise add the segment to the list */ if(maxDist > SQUARE(Config::SLAM::splitThreshold)) { //ldbg << "split 1: " << idx.firstHalfSplit(maxDistIdx) << endl; //ldbg << "split 2: " << idx.secondHalfSplit(maxDistIdx) << endl; split(pscan, idx.firstHalfSplit(maxDistIdx)); split(pscan, idx.secondHalfSplit(maxDistIdx)); } else { const UncertainLineSegment usegment = fittingFunction(pscan, idx); const LineSegmentTuple tuple(usegment, idx); clusterSupport.append(tuple); } }
void CombinedProfile::printDrift(const CombinedProfile& other, llvm::raw_ostream& stream) const { // build union of non-zero histograms IndexSet I; for(unsigned i = 0, E = size(); i < E; ++i) if( (_histograms[i] != NULL) && _histograms[i]->nonZero() ) I.insert(i); for(unsigned i = 0, E = other.size(); i < E; ++i) if( (other._histograms[i] != NULL) && other._histograms[i]->nonZero() ) I.insert(i); if(I.size() == 0) errs() << "Warning: no histograms\n"; // Compute and print drift stream << "#" << getNameStr() << "Index\t0-out\t0-in\n"; for(IndexSet::iterator i = I.begin(), E = I.end(); i != E; ++i) { // check for 0-overlap (100% drift) cases if( (*i > size()) || (*i > other.size()) || (_histograms[*i] == NULL) || (other._histograms[*i] == NULL) || !_histograms[*i]->nonZero() || !other._histograms[*i]->nonZero() ) { errs() << "Warning: histogram " << *i << " only exists in one profile!\n"; stream << *i << "\t1.0\t1.0\n"; continue; } CPHistogram* h1 = _histograms[*i]; CPHistogram* h2 = other._histograms[*i]; if( h1->isPoint() && h2->isPoint() && (h1->min() != h2->min())) { errs() << "Warning: histogram " << *i << " has different point values\n"; stream << *i << "\t1.0\t1.0\n"; continue; } // finally, no exceptional situations! stream << *i << "\t" << 1-h1->overlap(*h2, false) << "\t" << 1-h1->overlap(*h2, true) << "\n"; } }
int main (int argc, char *argv[]) { if (argc != 8) { cout << "usage " << argv[0] << " d d_ c radius jmin max_level W_XBSpline" << endl; exit(1); } int d=atoi(argv[1]), d_=atoi(argv[2]); T c=atof(argv[3]); T radius =atof(argv[4]); int jmin =atoi(argv[5]); int max_level =atoi(argv[6]); int W_XBSpline=atoi(argv[7]); cout.precision(8); Basis1D basis(d,d_,jmin); HelmholtzBilinearForm1D Bil(basis,c); Preconditioner1D P(basis); Compression_W_XBSpline1D compression_w_XBSpline(basis); Compression_WO_XBSpline1D compression_wo_XBSpline(basis); MA_W_XBSpline A_w_XBSpline(Bil,P,compression_w_XBSpline); MA_WO_XBSpline A_wo_XBSpline(Bil,P,compression_wo_XBSpline); cout << "Chosen parameters: d=" << d << ", d_=" << d_ << ", c=" << c << ", radius="<< radius << ", jmin=" << jmin << ", max_level=" << max_level << endl; if (W_XBSpline==1) { std::stringstream filename; filename << "eigenvalues_helmholtz_W_XBSpline_" << jmin << "_" << d << "_" << d_ << ".dat"; std::ofstream file_eigenvalues(filename.str().c_str()); for (int jmax=0; jmax<=max_level; jmax+=1) { for (T r=1.; r<=radius; r+=1.) { IndexSet<Index1D> Lambda = LambdaForEigenvalues(jmin, jmax, basis, r, true); int N = Lambda.size(); cout << "Size of Lambda: " << N << endl; flens::SparseGeMatrix<CRS<T,CRS_General> > A_flens(N,N); toFlensSparseMatrix(A_w_XBSpline, Lambda, Lambda, A_flens); flens::GeMatrix<flens::FullStorage<T, cxxblas::ColMajor> > A_dense; densify(cxxblas::NoTrans,A_flens,A_dense); T cB, CB; computeEV(A_dense, cB, CB); file_eigenvalues << " " << jmax << " " << r << " " << cB << " " << " " << CB << endl; cout << " " << jmax << " " << r << " " << cB << " " << " " << CB << endl; } } } else { std::stringstream filename; filename << "eigenvalues_helmholtz_WO_XBSpline_" << jmin << "_" << d << "_" << d_ << ".dat"; std::ofstream file_eigenvalues(filename.str().c_str()); for (int jmax=0; jmax<=max_level; jmax+=1) { for (T r=1.; r<=radius; r+=1.) { IndexSet<Index1D> Lambda = LambdaForEigenvalues(jmin, jmax, basis, r, false); int N = Lambda.size(); cout << "Size of Lambda: " << N << endl; flens::SparseGeMatrix<CRS<T,CRS_General> > A_flens(N,N); toFlensSparseMatrix(A_wo_XBSpline, Lambda, Lambda, A_flens); flens::GeMatrix<flens::FullStorage<T, cxxblas::ColMajor> > A_dense; densify(cxxblas::NoTrans,A_flens,A_dense); T cB, CB; computeEV(A_dense, cB, CB); file_eigenvalues << " " << jmax << " " << r << " " << cB << " " << " " << CB << endl; cout << " " << jmax << " " << r << " " << cB << " " << " " << CB << endl; } } } return 0; }
int main() { DenseVector sings; GeMat deltas(3,2); std::vector<GeMat> _deltas; Function x2f(x2, sings); Function onef(one, sings); Function x3f(x3, sings); Function cosf(mycos, sings); Function sinf(mysin, sings); Function expf(myexp, sings); Basis basis(4); basis.template enforceBoundaryCondition<lawa::DirichletBC>(); IndexSet indexset; Coeff1D coeff; std::vector<Function> fvec; int rank = 2; int dim = 64; for (int i=1; i<=32; ++i) { fvec.push_back(cosf); fvec.push_back(onef); fvec.push_back(x2f); fvec.push_back(onef); } SepCoeff coeffs(rank, dim); IndexSetVec indexsetvec(dim); lawa::SeparableFunctionD<T> F(fvec, rank, dim); MatInt derivs(rank, dim); for (int i=1; i<=rank; ++i) { for (int j=1; j<=dim; ++j) { derivs(i,j) = 0; _deltas.push_back(deltas); } } lawa::SeparableRHSD<T, Basis> Fint(basis, F, _deltas, derivs); getFullIndexSet(basis, indexset, 2); std::cout << "The index set size is\n" << indexset.size() << std::endl; for (int l=0; (unsigned)l<indexsetvec.size(); ++l) { indexsetvec[l] = indexset; } /* Map */ lawa::Mapwavind<Index1D> map(dim); map.rehash(50); genCoefficients(coeffs, Fint, indexsetvec); lawa::HTCoefficients<T, Basis> f(dim, basis, map); lawa::HTCoefficients<T, Basis> u(dim, basis, map); lawa::HTCoefficients<T, Basis> r(dim, basis, map); Laplace1D LaplaceBil(basis); RefLaplace1D RefLaplaceBil(basis.refinementbasis); Identity1D IdentityBil(basis); RefIdentity1D RefIdentityBil(basis.refinementbasis); LOp_Lapl1D lapl(basis, basis, RefLaplaceBil, LaplaceBil); Sepop A(lapl, dim, dim); lawa::Sepdiagscal<Basis> S(dim, basis); setScaling(S, 0.5); lawa::HTAWGM_Params params; params.maxit_pcg = 100; params.maxit_awgm = 100; params.tol_awgm = 1e-08; params.delta1_pcg = 1e-01; params.delta2_pcg = 1e-01; params.delta3_pcg = 1e-01; params.alpha = 0.95; params.recompr = 1e-02; params.gamma = 0.1; params.theta = 1e-08; std::cout << "HTAWGM params =\n"; std::cout << params << std::endl; unsigned its; double res; its = htawgm(A, S, u, Fint, indexsetvec, res, params); std::cout << "htawgm took " << its << " iterations to reach " << res << " accuracy" << std::endl; std::cout << "Final scaling set to\n" << S << std::endl; return 0; }
//----------------------------------------------------------------------------- void RegularCutRefinement::refine_marked(Mesh& refined_mesh, const Mesh& mesh, const std::vector<int>& refinement_markers, const IndexSet& marked_edges) { // Count the number of cells in refined mesh std::size_t num_cells = 0; // Data structure to hold a cell std::vector<std::size_t> cell_data(3); for (CellIterator cell(mesh); !cell.end(); ++cell) { const int marker = refinement_markers[cell->index()]; switch (marker) { case no_refinement: num_cells += 1; break; case regular_refinement: num_cells += 4; break; case backtrack_bisection: num_cells += 2; break; case backtrack_bisection_refine: num_cells += 3; break; default: num_cells += 2; } } // Initialize mesh editor const std::size_t num_vertices = mesh.num_vertices() + marked_edges.size(); MeshEditor editor; editor.open(refined_mesh, mesh.topology().dim(), mesh.geometry().dim()); editor.init_vertices(num_vertices); editor.init_cells(num_cells); // Set vertex coordinates std::size_t current_vertex = 0; for (VertexIterator vertex(mesh); !vertex.end(); ++vertex) { editor.add_vertex(current_vertex, vertex->point()); current_vertex++; } for (std::size_t i = 0; i < marked_edges.size(); i++) { Edge edge(mesh, marked_edges[i]); editor.add_vertex(current_vertex, edge.midpoint()); current_vertex++; } // Get bisection data for old mesh const std::size_t D = mesh.topology().dim(); const std::vector<std::size_t>* bisection_twins = NULL; if (mesh.data().exists("bisection_twins", D)) bisection_twins = &(mesh.data().array("bisection_twins", D)); // Markers for bisected cells pointing to their bisection twins in // refined mesh std::vector<std::size_t>& refined_bisection_twins = refined_mesh.data().create_array("bisection_twins", D); refined_bisection_twins.resize(num_cells); for (std::size_t i = 0; i < num_cells; i++) refined_bisection_twins[i] = i; // Mapping from old to new unrefined cells (-1 means refined or not // yet processed) std::vector<int> unrefined_cells(mesh.num_cells()); std::fill(unrefined_cells.begin(), unrefined_cells.end(), -1); // Iterate over all cells and add new cells std::size_t current_cell = 0; std::vector<std::vector<std::size_t> > cells(4, std::vector<std::size_t>(3)); for (CellIterator cell(mesh); !cell.end(); ++cell) { // Get marker const int marker = refinement_markers[cell->index()]; if (marker == no_refinement) { // No refinement: just copy cell to new mesh std::vector<std::size_t> vertices; for (VertexIterator vertex(*cell); !vertex.end(); ++vertex) vertices.push_back(vertex->index()); editor.add_cell(current_cell++, vertices); // Store mapping to new cell index unrefined_cells[cell->index()] = current_cell - 1; // Remember unrefined bisection twins if (bisection_twins) { const std::size_t bisection_twin = (*bisection_twins)[cell->index()]; const int twin_marker = refinement_markers[bisection_twin]; dolfin_assert(twin_marker == no_refinement); if (unrefined_cells[bisection_twin] >= 0) { const std::size_t i = current_cell - 1; const std::size_t j = unrefined_cells[bisection_twin]; refined_bisection_twins[i] = j; refined_bisection_twins[j] = i; } } } else if (marker == regular_refinement) { // Regular refinement: divide into subsimplicies dolfin_assert(unrefined_cells[cell->index()] == -1); // Get vertices and edges const unsigned int* v = cell->entities(0); const unsigned int* e = cell->entities(1); dolfin_assert(v); dolfin_assert(e); // Get offset for new vertex indices const std::size_t offset = mesh.num_vertices(); // Compute indices for the six new vertices const std::size_t v0 = v[0]; const std::size_t v1 = v[1]; const std::size_t v2 = v[2]; const std::size_t e0 = offset + marked_edges.find(e[0]); const std::size_t e1 = offset + marked_edges.find(e[1]); const std::size_t e2 = offset + marked_edges.find(e[2]); // Create four new cells cells[0][0] = v0; cells[0][1] = e2; cells[0][2] = e1; cells[1][0] = v1; cells[1][1] = e0; cells[1][2] = e2; cells[2][0] = v2; cells[2][1] = e1; cells[2][2] = e0; cells[3][0] = e0; cells[3][1] = e1; cells[3][2] = e2; // Add cells std::vector<std::vector<std::size_t> >::const_iterator _cell; for (_cell = cells.begin(); _cell != cells.end(); ++_cell) editor.add_cell(current_cell++, *_cell); } else if (marker == backtrack_bisection || marker == backtrack_bisection_refine) { // Special case: backtrack bisected cells dolfin_assert(unrefined_cells[cell->index()] == -1); // Get index for bisection twin dolfin_assert(bisection_twins); const std::size_t bisection_twin = (*bisection_twins)[cell->index()]; dolfin_assert(bisection_twin != cell->index()); // Let lowest number twin handle refinement if (bisection_twin < cell->index()) continue; // Get marker for twin const int twin_marker = refinement_markers[bisection_twin]; // Find common edge(s) and bisected edge(s) const std::pair<std::size_t, std::size_t> common_edges = find_common_edges(*cell, mesh, bisection_twin); const std::pair<std::size_t, std::size_t> bisection_edges = find_bisection_edges(*cell, mesh, bisection_twin); const std::pair<std::size_t, std::size_t> bisection_vertices = find_bisection_vertices(*cell, mesh, bisection_twin, bisection_edges); // Get list of vertices and edges for both cells const Cell twin(mesh, bisection_twin); const unsigned int* vertices_0 = cell->entities(0); const unsigned int* vertices_1 = twin.entities(0); const unsigned int* edges_0 = cell->entities(1); const unsigned int* edges_1 = twin.entities(1); dolfin_assert(vertices_0); dolfin_assert(vertices_1); dolfin_assert(edges_0); dolfin_assert(edges_1); // Get offset for new vertex indices const std::size_t offset = mesh.num_vertices(); // Locate vertices such that v_i is the vertex opposite to // the edge e_i on the parent triangle const std::size_t v0 = vertices_0[common_edges.first]; const std::size_t v1 = vertices_1[common_edges.second]; const std::size_t v2 = vertices_0[bisection_edges.first]; const std::size_t e0 = offset + marked_edges.find(edges_1[bisection_vertices.second]); const std::size_t e1 = offset + marked_edges.find(edges_0[bisection_vertices.first]); const std::size_t e2 = vertices_0[bisection_vertices.first]; // Locate new vertices on bisected edge (if any) std::size_t E0 = 0; std::size_t E1 = 0; if (marker == backtrack_bisection_refine) E0 = offset + marked_edges.find(edges_0[bisection_edges.first]); if (twin_marker == backtrack_bisection_refine) E1 = offset + marked_edges.find(edges_1[bisection_edges.second]); // Add middle two cells (always) dolfin_assert(cell_data.size() == 3); cell_data[0] = e0; cell_data[1] = e1; cell_data[2] = e2; editor.add_cell(current_cell++, cell_data); cell_data[0] = v2; cell_data[1] = e1; cell_data[2] = e0; editor.add_cell(current_cell++, cell_data); // Add one or two remaining cells in current cell (left) if (marker == backtrack_bisection) { cell_data[0] = v0; cell_data[1] = e2; cell_data[2] = e1; editor.add_cell(current_cell++, cell_data); } else { // Add the two cells cell_data[0] = v0; cell_data[1] = E0; cell_data[2] = e1; editor.add_cell(current_cell++, cell_data); cell_data[0] = E0; cell_data[1] = e2; cell_data[2] = e1; editor.add_cell(current_cell++, cell_data); // Set bisection twins refined_bisection_twins[current_cell - 2] = current_cell - 1; refined_bisection_twins[current_cell - 1] = current_cell - 2; } // Add one or two remaining cells in twin cell (right) if (twin_marker == backtrack_bisection) { cell_data[0] = v1; cell_data[1] = e0; cell_data[2] = e2; editor.add_cell(current_cell++, cell_data); } else { // Add the two cells cell_data[0] = v1; cell_data[1] = e0; cell_data[2] = E1; editor.add_cell(current_cell++, cell_data); cell_data[0] = e0; cell_data[1] = e2; cell_data[2] = E1; editor.add_cell(current_cell++, cell_data); // Set bisection twins refined_bisection_twins[current_cell - 2] = current_cell - 1; refined_bisection_twins[current_cell - 1] = current_cell - 2; } } else { // One edge marked for refinement: do bisection dolfin_assert(unrefined_cells[cell->index()] == -1); // Get vertices and edges const unsigned int* v = cell->entities(0); const unsigned int* e = cell->entities(1); dolfin_assert(v); dolfin_assert(e); // Get edge number (equal to marker) dolfin_assert(marker >= 0); const std::size_t local_edge_index = static_cast<std::size_t>(marker); const std::size_t global_edge_index = e[local_edge_index]; const std::size_t ee = mesh.num_vertices() + marked_edges.find(global_edge_index); // Add the two new cells if (local_edge_index == 0) { cell_data[0] = v[0]; cell_data[1] = ee; cell_data[2] = v[1]; editor.add_cell(current_cell++, cell_data); cell_data[0] = v[0]; cell_data[1] = ee; cell_data[2] = v[2]; editor.add_cell(current_cell++, cell_data); } else if (local_edge_index == 1) { cell_data[0] = v[1]; cell_data[1] = ee; cell_data[2] = v[0]; editor.add_cell(current_cell++, cell_data); cell_data[0] = v[1]; cell_data[1] = ee; cell_data[2] = v[2]; editor.add_cell(current_cell++, cell_data); } else { cell_data[0] = v[2]; cell_data[1] = ee; cell_data[2] = v[0]; editor.add_cell(current_cell++, cell_data); cell_data[0] = v[2]; cell_data[1] = ee; cell_data[2] = v[1]; editor.add_cell(current_cell++, cell_data); } // Set bisection twins refined_bisection_twins[current_cell - 2] = current_cell - 1; refined_bisection_twins[current_cell - 1] = current_cell - 2; } } // Close mesh editor dolfin_assert(num_cells == current_cell); editor.close(); }