10#ifndef PECLET_AMR_REFINE_HPP
11#define PECLET_AMR_REFINE_HPP
21#include "peclet/core/common/types.hpp"
33template <
int Dim,
unsigned Bits,
class SdfFn>
35 unsigned targetLevel, Real band = 1.0,
bool balance =
true) {
39 std::vector<Code> toRefine;
40 for (Index i = 0; i < t.
numLeaves(); ++i) {
41 const unsigned L = t.
level(i);
45 Vec<Dim> c = geo.
center(b);
52 for (
int d = 0; d < Dim; ++d)
53 halfDiag2 += w[d] * w[d];
54 const Real halfDiag = 0.5 * std::sqrt(halfDiag2);
55 if (std::fabs(
static_cast<Real
>(sdf(c))) <= halfDiag + band * geo.
hMax())
56 toRefine.push_back(t.
code(i));
60 std::sort(toRefine.begin(), toRefine.end());
61 toRefine.erase(std::unique(toRefine.begin(), toRefine.end()), toRefine.end());
63 [&](Code c,
unsigned) {
return std::binary_search(toRefine.begin(), toRefine.end(), c); });
84template <
int Dim,
unsigned Bits,
class SdfFn,
class TargetFn>
86 TargetFn&& targetFn, Real band = 2.0,
bool balance =
true) {
88 const Real halfDiagFactor = 0.5 * std::sqrt(
static_cast<Real
>(Dim));
91 std::vector<Code> toRefine;
92 for (Index i = 0; i < t.
numLeaves(); ++i) {
93 const unsigned L = t.
level(i);
97 Vec<Dim> c = geo.
center(b);
101 Real halfDiag2 = 0.0;
103 for (
int d = 0; d < Dim; ++d) {
104 halfDiag2 += w[d] * w[d];
105 wMax = w[d] > wMax ? w[d] : wMax;
107 const Real halfDiag = 0.5 * std::sqrt(halfDiag2);
110 if (std::fabs(
static_cast<Real
>(sdf(c))) > halfDiag + band * 0.5 * wMax)
113 toRefine.push_back(t.
code(i));
115 if (toRefine.empty())
117 std::sort(toRefine.begin(), toRefine.end());
118 toRefine.erase(std::unique(toRefine.begin(), toRefine.end()), toRefine.end());
120 [&](Code c,
unsigned) {
return std::binary_search(toRefine.begin(), toRefine.end(), c); });
137template <
int Dim,
class GapFn>
140auto gapFloorTarget(GapFn&& gapFn, Real h0,
unsigned coarsestLevel, Real n = 4.0) {
141 return [gapFn = std::forward<GapFn>(gapFn), h0, coarsestLevel, n](
const Vec<Dim>& p) ->
unsigned {
142 const Real g =
static_cast<Real
>(gapFn(p));
144 while (L < coarsestLevel && h0 *
static_cast<Real
>(Index(1) << (L + 1)) * n <= g)
Per-block adaptive octree over block-local Morton codes.
Index balance2to1()
Enforce 2:1 (graded) balance within this block: no two face-adjacent leaves differ by more than one l...
Index refineIf(Pred &&pred)
Split every leaf for which pred(code, level) is true (and level > 0) into its 2^Dim children one leve...
unsigned level(Index i) const
std::array< std::array< Coord, Dim >, 2 > bounds(Index i) const
Inclusive integer bounds [lo, hi] of leaf i in fine units.
typename M::code_type Code
Index refineToSdf(BlockOctree< Dim, Bits > &t, const AmrGeometry< Dim > &geo, SdfFn &&sdf, unsigned targetLevel, Real band=1.0, bool balance=true)
Refine leaves near an SDF interface down to targetLevel.
Index refineToSdfGraded(BlockOctree< Dim, Bits > &t, const AmrGeometry< Dim > &geo, SdfFn &&sdf, TargetFn &&targetFn, Real band=2.0, bool balance=true)
Graded variant of refineToSdf: a per-point TARGET LEVEL function instead of one global targetLevel — ...
auto gapFloorTarget(GapFn &&gapFn, Real h0, unsigned coarsestLevel, Real n=4.0)
The plan's §7 criterion 1 as a ready-made targetFn for refineToSdfGraded: the GAP-WIDTH FLOOR.
World-space placement of a block-local octree: fine coordinate (0,..,0) sits at origin,...
Vec< Dim > center(const std::array< std::array< Coord, Dim >, 2 > &b) const
World coordinate of a leaf centre, from its integer bounds [lo,hi] (inclusive, fine units).
Real leafSize(unsigned level, int d) const
World width of a leaf at level on axis d (it covers 2^level fine cells).