introvoyz042's picture
Migrated from GitHub
5a600cf verified
Raw History Blame Contribute Delete
7.56 kB
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "refinementIterator.H"
#include "polyMesh.H"
#include "Time.H"
#include "refineCell.H"
#include "undoableMeshCutter.H"
#include "polyTopoChange.H"
#include "polyTopoChangeMap.H"
#include "cellCuts.H"
#include "OFstream.H"
#include "meshTools.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(refinementIterator, 0);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// Construct from components
Foam::refinementIterator::refinementIterator
(
polyMesh& mesh,
undoableMeshCutter& meshRefiner,
const cellLooper& cellWalker,
const bool writeMesh
)
:
edgeVertex(mesh),
mesh_(mesh),
meshRefiner_(meshRefiner),
cellWalker_(cellWalker),
writeMesh_(writeMesh)
{}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::refinementIterator::~refinementIterator()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::Map<Foam::label> Foam::refinementIterator::setRefinement
(
const List<refineCell>& refCells
)
{
Map<label> addedCells(2*refCells.size());
Time& runTime = const_cast<Time&>(mesh_.time());
label nRefCells = refCells.size();
label oldRefCells = -1;
// Operate on copy.
List<refineCell> currentRefCells(refCells);
bool stop = false;
do
{
if (writeMesh_)
{
// Need different times to write meshes.
runTime++;
}
polyTopoChange meshMod(mesh_);
if (debug)
{
Pout<< "refinementIterator : refining "
<< currentRefCells.size() << " cells." << endl;
}
// Determine cut pattern.
cellCuts cuts(mesh_, cellWalker_, currentRefCells);
label nCuts = cuts.nLoops();
reduce(nCuts, sumOp());
if (nCuts == 0)
{
if (debug)
{
Pout<< "refinementIterator : exiting iteration since no valid"
<< " loops found for " << currentRefCells.size()
<< " cells" << endl;
fileName cutsFile("failedCuts_" + runTime.name() + ".obj");
Pout<< "Writing cuts for time " << runTime.name()
<< " to " << cutsFile << endl;
OFstream cutsStream(cutsFile);
labelList refCellsDebug(currentRefCells.size());
forAll(currentRefCells, i)
{
refCellsDebug[i] = currentRefCells[i].cellNo();
}
meshTools::writeOBJ
(
cutsStream,
mesh().cells(),
mesh().faces(),
mesh().points(),
refCellsDebug
);
}
break;
}
if (debug)
{
fileName cutsFile("cuts_" + runTime.name() + ".obj");
Pout<< "Writing cuts for time " << runTime.name()
<< " to " << cutsFile << endl;
OFstream cutsStream(cutsFile);
cuts.writeOBJ(cutsStream);
}
// Insert mesh refinement into polyTopoChange.
meshRefiner_.setRefinement(cuts, meshMod);
//
// Do all changes
//
autoPtr<polyTopoChangeMap> map = meshMod.changeMesh(mesh_);
// Update stored refinement pattern
meshRefiner_.topoChange(map());
// Write resulting mesh
if (writeMesh_)
{
if (debug)
{
Pout<< "Writing refined polyMesh to time "
<< runTime.name() << endl;
}
mesh_.write();
}
// Update currentRefCells for new cell numbers. Use helper function
// in meshCutter class.
updateLabels
(
map->reverseCellMap(),
currentRefCells
);
// Update addedCells for new cell numbers
updateLabels
(
map->reverseCellMap(),
addedCells
);
// Get all added cells from cellCutter (already in new numbering
// from meshRefiner.topoChange call) and add to global list of added
const Map<label>& addedNow = meshRefiner_.addedCells();
forAllConstIter(Map<label>, addedNow, iter)
{
if (!addedCells.insert(iter.key(), iter()))
{
FatalErrorInFunction
<< "Master cell " << iter.key()
<< " already has been refined" << endl
<< "Added cell:" << iter() << abort(FatalError);
}
}
// Get failed refinement in new cell numbering and reconstruct input
// to the meshRefiner. Is done by removing all refined cells from
// current list of cells to refine.
// Update refCells for new cell numbers.
updateLabels
(
map->reverseCellMap(),
currentRefCells
);
// Pack refCells acc. to refined status
nRefCells = 0;
forAll(currentRefCells, refI)
{
const refineCell& refCell = currentRefCells[refI];
if (!addedNow.found(refCell.cellNo()))
{
if (nRefCells != refI)
{
currentRefCells[nRefCells++] =
refineCell
(
refCell.cellNo(),
refCell.direction()
);
}
}
}
oldRefCells = currentRefCells.size();
currentRefCells.setSize(nRefCells);
if (debug)
{
Pout<< endl;
}
// Stop only if all finished or all can't refine any further.
stop = (nRefCells == 0) || (nRefCells == oldRefCells);
reduce(stop, andOp());
}
while (!stop);
if (returnReduce((nRefCells == oldRefCells), andOp()))
{
WarningInFunction
<< "stopped refining."
<< "Did not manage to refine a single cell" << endl
<< "Wanted :" << oldRefCells << endl;
}
return addedCells;
}
// ************************************************************************* //