17#include <boost/timer/timer.hpp>
18#include <boost/type_traits/remove_pointer.hpp>
19#include <boost/type_traits/remove_reference.hpp>
21#include <boost/fusion/algorithm.hpp>
22#include <boost/fusion/sequence.hpp>
36 namespace CoarseningDetail
61 template <
class SpacePtr>
63 typedef typename boost::remove_pointer<typename boost::remove_reference<SpacePtr>::type>::type
Space;
67 template <
class SpacePtr>
86 template <
class Projector>
99 for (
int i=0; i<children.size(); ++i)
101 auto gi = p.
space->mapper().globalIndices(children[i]);
102 p.
gidx.insert(p.
gidx.end(),gi.begin(),gi.end());
105 std::sort(p.
gidx.begin(),p.
gidx.end());
113 typedef typename Space::Grid Grid;
114 typedef typename Space::Scalar Scalar;
115 int const sfComponents = Space::sfComponents;
117 std::vector<Dune::FieldVector<typename Grid::ctype,Grid::dimension> > iNodes;
118 for (
int i=0; i<children.size(); ++i)
121 auto gi = p.
space->mapper().globalIndices(children[i]);
122 p.
idx.resize(gi.size());
123 for (
int j=0; j<gi.size(); ++j)
124 p.
idx[j] = std::lower_bound(p.
gidx.begin(),p.
gidx.end(),gi[j]) - p.
gidx.begin();
128 auto const& sfs = p.
space->mapper().shapefunctions(children[i]);
132 for(
size_t k=0; k<p.
idx.size(); ++k)
133 for(
int j=0; j<p.
q.M(); ++j)
152 for (
int j=0; j<p.
q.M(); ++j)
157 for (
int i=0; i<p.
q.N(); ++i)
159 tmp = std::sqrt(tmp);
160 for (
int i=0; i<p.
q.N(); ++i)
164 for (
int k=j+1; k<p.
q.M(); ++k)
167 for (
int i=0; i<p.
q.N(); ++i)
168 tmp += p.
q[i][j] * p.
q[i][k];
169 for (
int i=0; i<p.
q.N(); ++i)
170 p.
q[i][k] -= tmp*p.
q[i][j];
177 std::vector<Cell>
const& children;
183 template <
class Projectors>
191 template <
class Pair>
194 typedef typename boost::remove_reference<typename result_of::value_at_c<Pair,0>::type>::type VarDesc;
195 typedef typename boost::remove_reference<typename result_of::value_at_c<Pair,1>::type>::type
Function;
197 int const sIdx = VarDesc::spaceIndex;
198 auto const& proj = at_c<sIdx>(projectors);
199 int const n = proj.gidx.size();
205 auto const& q = proj.q;
207 typename Function::StorageType x(n);
208 typename Function::StorageType y(m);
211 for (
int i=0; i<m; ++i) {
213 for (
int j=0; j<n; ++j)
214 y[i].
axpy(q[j][i][0][0],(at_c<1>(pair).coefficients())[proj.gidx[j]]);
218 for (
int i=0; i<n; ++i) {
219 typename Function::StorageValueType x(0);
220 for (
int j=0; j<m; ++j)
221 x.axpy(q[i][j][0][0],y[j]);
222 (at_c<1>(pair).coefficients())[proj.gidx[i]] = x;
227 Projectors
const& projectors;
231 template <
class Projectors>
240 :
nGroups(*std::max_element(groups_.begin(),groups_.end())+1)
244 size_t operator[](
size_t idx)
const {
return (*groups)[idx]; }
249 std::vector<size_t>
const* groups;
262 template <
class VariableSetDescription,
class Scaling>
266 std::vector<std::pair<double,double> >
const& tol,
269 std::vector<bool> tmp;
270 coarsening(varDesc, sol, scaling, tol, gridManager, tmp, verbosity, minRefLevel);
288 template <
class VariableSetDescription,
class Scaling>
292 std::vector<std::pair<double,double>>
const& tol,
294 std::vector<bool>& coarsenedCells,
int verbosity=1,
int minRefLevel=0)
297 using namespace CoarseningDetail;
300 typedef typename Grid::LeafIndexSet IndexSet;
301 typedef typename Grid::template Codim<0>::Entity
Cell;
302 typedef typename Cell::HierarchicIterator HierarchicIterator;
304 IndexSet
const& indexSet = varDesc.
indexSet;
310 auto projectors = as_vector(transform(varDesc.
spaces,CreateProjection()));
314 std::vector<Cell> children;
324 std::vector<size_t> coarseningGroup(indexSet.size(0),0);
325 size_t coarseningGroupCount = 0;
328 for (
auto const& cell: Dune::elements(gridManager.
grid().leafGridView()))
331 if (coarseningGroup[indexSet.index(cell)]==0 && cell.level()>0)
334 Cell father = cell.father();
335 bool canBeCoarsened =
true;
337 HierarchicIterator first = father.hbegin(father.level()+1), last = father.hend(father.level()+1);
338 for (HierarchicIterator hi=first; canBeCoarsened && hi!=last; ++hi)
340 canBeCoarsened = canBeCoarsened && hi->isLeaf();
341 children.push_back(*hi);
347 ++coarseningGroupCount;
348 for (
auto const& c: children)
349 coarseningGroup[indexSet.index(c)] = coarseningGroupCount;
351 for_each(projectors,GetLocalTransferProjection<Cell>(father,children));
373 for (
int j=0; j<=coarseningGroupCount; ++j)
377 std::cout <<
"coarsening solution norms2: ";
378 std::copy(norm2.begin(),norm2.end(),
379 std::ostream_iterator<double>(std::cout,
" ")); std::cout <<
'\n';
383 double toli =
power(tol[i].first,2) + norm2[i]*
power(tol[i].second,2);
384 for (
int j=0; j<=coarseningGroupCount; ++j)
385 sum.sums[j][i] /= toli;
420 std::vector<std::pair<double,size_t>> e(coarseningGroupCount);
421 for (
int j=0; j<coarseningGroupCount; ++j)
425 e[j].first =
std::max(e[j].first,sum.sums[j+1][i]);
428 std::sort(e.begin(),e.end(),
FirstLess());
430 std::vector<size_t> selectedCoarseningGroups;
431 double totalError = 0;
432 for (
int i=0; i<e.size(); ++i) {
433 totalError += e[i].first;
435 selectedCoarseningGroups.push_back(e[i].second);
441 coarsenedCells.clear() ;
442 coarsenedCells.resize( indexSet.size(0),
false ) ;
447 if (selectedCoarseningGroups.empty())
452 std::sort(selectedCoarseningGroups.begin(),selectedCoarseningGroups.end());
453 for (
Cell const& c: Dune::elements(gridManager.
grid().leafGridView()))
455 auto idx = indexSet.index(c);
456 if (c.level()>minRefLevel && std::binary_search(selectedCoarseningGroups.begin(),selectedCoarseningGroups.end(),
457 coarseningGroup[idx]))
459 coarsenedCells[idx] = true ;
460 gridManager.
mark(-1,c);
Function is the interface for evaluatable functions .
GroupByCell(std::vector< size_t > const &groups_)
size_t operator[](size_t idx) const
A LAPACK-compatible dense matrix class with shape specified at runtime.
void setSize(size_type r, size_type c)
Resizes the matrix to r x c, leaving the entries in an undefined state.
Grid const & grid() const
Returns a const reference on the owned grid.
bool adaptAtOnce()
DEPRECATED Performs grid refinement without prolongating registered FE functions.
bool mark(int refCount, typename Grid::template Codim< 0 >::Entity const &cell)
Marks the given cell for refinement or coarsening.
Class that takes care of the transfer of data (of each FunctionSpaceElement) after modification of th...
A class that stores information about a set of variables.
IndexSet const & indexSet
index set
static int const noOfVariables
Number of variables in this set.
Spaces spaces
A heterogeneous sequence of pointers to (const) spaces involved.
typename Variables_Detail::ImplicitIdVariableDescriptions< VariableList >::type Variables
type of boost::fusion vector of VariableDescription s
SpaceType< Spaces, 0 >::type::Grid Grid
Grid type.
A class for storing a heterogeneous collection of FunctionSpaceElement s.
A callable that allows to implement weighted (scaled) norms.
Tools for transfer of data between grids.
void for_each2(Seq1 const &s1, Seq2 &s2, Func const &func)
void coarsening(VariableSetDescription const &varDesc, typename VariableSetDescription::VariableSet const &sol, Scaling const &scaling, std::vector< std::pair< double, double > > const &tol, GridManager< typename VariableSetDescription::Grid > &gridManager, int verbosity=1, int minRefLevel=0)
typename GridView::template Codim< 0 >::Entity Cell
The type of cells (entities of codimension 0) in the grid view.
Dune::FieldVector< T, n > max(Dune::FieldVector< T, n > x, Dune::FieldVector< T, n > const &y)
Componentwise maximum.
R power(R base, int exp)
Computes integral powers of values in a multiplicative half-group.
R square(R x)
returns the square of its argument.
ProjectCoefficients< Projectors > getCoefficientProjectors(Projectors const &ps)
void axpy(Dune::BCRSMatrix< Dune::FieldMatrix< Scalar, 1, 1 > > const &P, Dune::BlockVector< Dune::FieldVector< Scalar, n > > const &x, Dune::BlockVector< Dune::FieldVector< Scalar, n > > &y, Scalar alpha=1.0)
Compute . If resetSolution=true computes .
void scaledTwoNormSquared(Variables const &varDesc, Functions const &f, Spaces const &spaces, Scaling const &scaling, Collector &sum)
Evaluates the square of the scaled -norms of a set of functions.
void localProlongationMatrix(ChildSpace const &childSpace, Cell< typename ChildSpace::Grid > const &child, FatherSpace const &fatherSpace, Cell< typename FatherSpace::Grid > const &father, DynamicMatrix< Dune::FieldMatrix< typename ChildSpace::Scalar, 1, 1 > > &prolongation, typename ChildSpace::Mapper::ShapeFunctionSet const &childSfs, typename FatherSpace::Mapper::ShapeFunctionSet const &fatherSfs)
Computes a local prolongation matrix for global shape functions from father cell to child cell.
boost::remove_pointer< typenameboost::remove_reference< SpacePtr >::type >::type Space
result< CreateProjection(SpacePtr)>::type operator()(SpacePtr space) const
void operator()(Projector &p) const
GetLocalTransferProjection(Cell father_, std::vector< Cell > const &children_)
ProjectCoefficients(Projectors const &projectors_)
void operator()(Pair const &pair) const
DynamicMatrix< Dune::FieldMatrix< typename Space::Scalar, 1, 1 > > q
std::vector< size_t > gidx
DynamicMatrix< Dune::FieldMatrix< typename Space::Scalar, 1, 1 > > pLocal
A Collector coalescing the information of multi-variable functions on cell groups.
A comparator functor that supports sorting std::pair by their first component.