Vector StructuredGridBase::cellCenter(Index elem) const { Index dims[3]; for (int c=0; c<3; ++c) { dims[c] = getNumDivisions(c); } auto verts = cellVertices(elem, dims); Vector center(0); for (auto v: verts) { center += getVertex(v); } center *= 1./8; return center; }
// GET INTERPOLATOR //------------------------------------------------------------------------- GridInterface::Interpolator RectilinearGrid::getInterpolator(Index elem, const Vector &point, DataBase::Mapping mapping, GridInterface::InterpolationMode mode) const { vassert(inside(elem, point)); #ifdef INTERPOL_DEBUG if (!inside(elem, point)) { return Interpolator(); } #endif if (mapping == DataBase::Element) { std::vector<Scalar> weights(1, 1.); std::vector<Index> indices(1, elem); return Interpolator(weights, indices); } std::array<Index,3> n = cellCoordinates(elem, m_numDivisions); std::array<Index,8> cl = cellVertices(elem, m_numDivisions); Vector corner0(m_coords[0][n[0]], m_coords[1][n[1]], m_coords[2][n[2]]); Vector corner1(m_coords[0][n[0]+1], m_coords[1][n[1]+1], m_coords[2][n[2]+1]); const Vector diff = point-corner0; const Vector size = corner1-corner0; const Index nvert = 8; std::vector<Index> indices((mode==Linear || mode==Mean) ? nvert : 1); std::vector<Scalar> weights((mode==Linear || mode==Mean) ? nvert : 1); if (mode == Mean) { const Scalar w = Scalar(1)/nvert; for (Index i=0; i<nvert; ++i) { indices[i] = cl[i]; weights[i] = w; } } else if (mode == Linear) { vassert(nvert == 8); for (Index i=0; i<nvert; ++i) { indices[i] = cl[i]; } Vector ss = diff; for (int c=0; c<3; ++c) { ss[c] /= size[c]; } weights[0] = (1-ss[0])*(1-ss[1])*(1-ss[2]); weights[1] = ss[0]*(1-ss[1])*(1-ss[2]); weights[2] = ss[0]*ss[1]*(1-ss[2]); weights[3] = (1-ss[0])*ss[1]*(1-ss[2]); weights[4] = (1-ss[0])*(1-ss[1])*ss[2]; weights[5] = ss[0]*(1-ss[1])*ss[2]; weights[6] = ss[0]*ss[1]*ss[2]; weights[7] = (1-ss[0])*ss[1]*ss[2]; } else { weights[0] = 1; if (mode == First) { indices[0] = cl[0]; } else if(mode == Nearest) { Vector ss = diff; int nearest=0; for (int c=0; c<3; ++c) { nearest <<= 1; ss[c] /= size[c]; if (ss[c] < 0.5) nearest |= 1; } indices[0] = cl[nearest]; } } return Interpolator(weights, indices); }
bool Foam::conformalVoronoiMesh::distributeBackground(const Triangulation& mesh) { if (!Pstream::parRun()) { return false; } Info<< nl << "Redistributing points" << endl; timeCheck("Before distribute"); label iteration = 0; scalar previousLoadUnbalance = 0; while (true) { scalar maxLoadUnbalance = mesh.calculateLoadUnbalance(); if ( maxLoadUnbalance <= foamyHexMeshControls().maxLoadUnbalance() || maxLoadUnbalance <= previousLoadUnbalance ) { // If this is the first iteration, return false, if it was a // subsequent one, return true; return iteration != 0; } previousLoadUnbalance = maxLoadUnbalance; Info<< " Total number of vertices before redistribution " << returnReduce(label(mesh.number_of_vertices()), sumOp<label>()) << endl; const fvMesh& bMesh = decomposition_().mesh(); volScalarField cellWeights ( IOobject ( "cellWeights", bMesh.time().timeName(), bMesh, IOobject::NO_READ, IOobject::NO_WRITE ), bMesh, dimensionedScalar("weight", dimless, 1e-2), zeroGradientFvPatchScalarField::typeName ); meshSearch cellSearch(bMesh, polyMesh::FACE_PLANES); labelList cellVertices(bMesh.nCells(), label(0)); for ( typename Triangulation::Finite_vertices_iterator vit = mesh.finite_vertices_begin(); vit != mesh.finite_vertices_end(); ++vit ) { // Only store real vertices that are not feature vertices if (vit->real() && !vit->featurePoint()) { pointFromPoint v = topoint(vit->point()); label cellI = cellSearch.findCell(v); if (cellI == -1) { // Pout<< "findCell conformalVoronoiMesh::distribute " // << "findCell " // << vit->type() << " " // << vit->index() << " " // << v << " " // << cellI // << " find nearest cellI "; cellI = cellSearch.findNearestCell(v); } cellVertices[cellI]++; } } forAll(cellVertices, cI) { // Give a small but finite weight for empty cells. Some // decomposition methods have difficulty with integer overflows in // the sum of the normalised weight field. cellWeights.internalField()[cI] = max ( cellVertices[cI], 1e-2 ); } autoPtr<mapDistributePolyMesh> mapDist = decomposition_().distribute ( cellWeights ); cellShapeControl_.shapeControlMesh().distribute(decomposition_); distribute(); timeCheck("After distribute"); iteration++; }