7#include <boost/timer/timer.hpp>
9#include "dune/istl/solvers.hh"
17#include "dune/istl/solvers.hh"
18#include "dune/istl/preconditioners.hh"
39bool fabscompare(
double a,
double b ) {
return fabs( a ) < fabs( b ) ; }
47 template <
class Functional>
50 template <
int row,
int col>
51 class D2:
public Functional::template
D2<row,col>
53 typedef typename Functional::template
D2<row,col> d2;
56 static bool const present = d2::present && (row==0) && (col==0);
74 typedef typename EvolutionEquation::AnsatzVars::VariableSet
State;
82 typedef boost::fusion::vector< typename SpaceType<typename Eq::AnsatzVars::Spaces,0>::type
const*,
SpaceEx const*> ExSpaces;
88 typedef boost::fusion::vector<VariableDescription<1,1,0> > ExVariableDescriptions;
98 typedef typename Eq::AnsatzVars::Grid::Traits::LeafIndexSet IS ;
108 gridManager(gridManager_), ansatzVars(ansatzVars_), eq(&eq_,0),
109 assembler(ansatzVars.spaces),
110 iteSteps(10000), iteEps(1e-6),
extrap(0),
123 std::vector<std::pair<double,double> >
const& tolX)
125 std::vector<std::vector<bool> > tmp ;
126 return step(x,dt,order,tolX,tmp);
147 std::vector<std::pair<double,double> >
const& tolX,
149 std::vector< std::vector<bool> > &refinements )
151 boost::timer::cpu_timer timer;
153 std::vector<double> stepFractions(order+1);
154 for (
int i=0; i<=order; ++i) stepFractions[i] = 1.0/(i+1);
157 typedef typename EvolutionEquation::AnsatzVars::Grid Grid;
159 int const dim = EvolutionEquation::AnsatzVars::Grid::dimension;
160 int const nvars = EvolutionEquation::AnsatzVars::noOfVariables;
161 int const neq = EvolutionEquation::TestVars::noOfVariables;
162 size_t nnz = assembler.
nnz(0,neq,0,nvars,
false);
163 size_t size = ansatzVars.degreesOfFreedom(0,nvars);
165 std::vector<double> rhs(size), sol(size);
167 State dx(x), dxsum(x), tmp(x);
168 double const t = eq.time();
172 eq.temporalEvaluationRange(t,t+dt);
176 bool iterative = true ;
179 typedef typename EvolutionEquation::AnsatzVars::template CoefficientVectorRepresentation<0,neq>::type
181 typedef typename EvolutionEquation::TestVars::template CoefficientVectorRepresentation<0,neq>::type
184 for (
int i=0; i<=order; ++i) {
185 double const tau = stepFractions[i]*dt;
190 bool accurate =
true;
191 int refinement_count = 0 ;
219 CoefficientVectors solution(EvolutionEquation::AnsatzVars::template CoefficientVectorRepresentation<0,neq>::init(ansatzVars.spaces)
222 CoefficientVectors rhs(assembler.
rhs());
228 Dune::BiCGSTABSolver<LinearSpaceX> cg(A,p,1e-7,2000,0);
229 Dune::InverseOperatorResult res;
230 cg.apply(solution,rhs,res);
231 if ( !(res.converged) || (res.iterations == 2001) ) {
232 std::cout <<
" no of iterations in cg = " << res.iterations << std::endl;
233 std::cout <<
" convergence status of cg = " << res.converged << std::endl;
236 dx.data = solution.data;
268 A.getAssembler().toSequence(0,neq,rhs.begin());
269 for (
int k=0; k<rhs.size(); ++k) assert(std::isfinite(rhs[k]));
270 matrix->solve(rhs,sol);
272 for (
int k=0; k<sol.size(); ++k) assert(std::isfinite(sol[k]));
273 dx.read(sol.begin());
313 typedef typename EvolutionEquation::AnsatzVars::GridView::template Codim<0>::Iterator CellIterator ;
317 IS
const& is = gridManager.
grid().leafIndexSet();
318 std::vector<double> errorDistribution(is.size(0),0.0);
319 double maxErr = 0.0, errNorm = 0.0 ;
321 if (!tolX.empty() && i==0) {
329 SpaceEx spaceEx(gridManager,gridManager.
grid().leafGridView(), boost::fusion::at_c<0>(ansatzVars.spaces)->mapper().maxOrder()+1);
331 ExSpaces exSpaces(&spaceH1,&spaceEx);
332 std::string exVarNames[2] = {
"ev",
"ew" };
341 int const estNvars = ErrorEstimator::AnsatzVars::noOfVariables;
343 int const estNeq = ErrorEstimator::TestVars::noOfVariables;
345 size_t estNnz = estAssembler.
nnz(0,estNeq,0,estNvars,
false);
348 std::vector<int> estRidx(estNnz), estCidx(estNnz);
349 std::vector<double> estData(estNnz), estRhs(estSize), estSolVec(estSize);
351 estAssembler.
toSequence(0,estNeq,estRhs.begin());
353 typedef typename ExVariableSet::template CoefficientVectorRepresentation<0,estNeq>::type ExCoefficientVectors;
368 AssEstOperator agro(estAssembler);
369 Dune::InverseOperatorResult estRes;
370 ExCoefficientVectors estRhside(estAssembler.
rhs());
371 ExCoefficientVectors estSol(ExVariableSet::template CoefficientVectorRepresentation<0,estNeq>::init(exVariableSet.
spaces));
374 jprec.
apply(estSol,estRhside);
376 estSol.write(estSolVec.begin());
381 CellIterator ciEnd = ansatzVars.gridView.template end<0>() ;
382 for (CellIterator ci=ansatzVars.gridView.template begin<0>(); ci!=ciEnd; ++ci) {
383 typedef typename SpaceEx::Mapper::GlobalIndexRange GIR;
385 GIR gix = spaceEx.
mapper().globalIndices(*ci);
386 for (
typename GIR::iterator j=gix.begin(); j!=gix.end(); ++j)
387 err += fabs(boost::fusion::at_c<0>(estSol.data)[*j]);
389 errorDistribution[is.index(*ci)] = err;
390 if (fabs(err)>maxErr) maxErr = fabs(err);
400 for (
size_t k = 0; k < estRhs.size() ; k++ ) errNorm += fabs( estRhs[k] * estSolVec[k] );
403 std::cout <<
"errNorm = " << errNorm << std::endl ;
408 double overallTol = tolX[0].first ;
421 double fractionOfCells = 0.05;
422 unsigned long noToRefine = 0, noToCoarsen = 0;
425 double errLevel = 0.5*maxErr ;
449 size_t maxNoOfVertices = 30000;
450 size_t maxNoOfElements = 2000000;
453 if( errNorm > overallTol && refinement_count < nRefMax
454 && gridManager.
grid().size(0) < maxNoOfElements )
469 std::vector<bool> toRefine( is.size(0),
false ) ;
472 size_t noCells = gridManager.
grid().size(0);
498 unsigned long noToRefineOld = 0;
500 noToRefineOld = noToRefine;
501 for(
size_t cno = 0 ; cno < noCells ; cno++ )
503 if (fabs(errorDistribution[cno]) >= alpha*errLevel)
505 if( !toRefine[cno] ) noToRefine++;
506 toRefine[cno] = true ;
512 }
while ( noToRefine < fractionOfCells * noCells && (!noToRefineOld == noToRefine) ) ;
532 for (CellIterator ci=ansatzVars.gridView.template begin<0>(); ci!=ansatzVars.gridView.template end<0>(); ++ci)
533 if( toRefine[is.index(*ci)] && ci->level() < maxLevel ) gridManager.
mark(1,*ci);
539 std::cout <<
"nothing has been refined (probably due to maxLevel constraint), stopping\n";
545 nnz = assembler.
nnz(0,neq,0,nvars,
false);
546 size = ansatzVars.degreesOfFreedom(0,nvars);
553 if( errNorm > overallTol && refinement_count > nRefMax )
554 std::cout <<
"max no of refinements exceeded\n";
569 for (
int j=1; j<=i; ++j) {
571 eq.time(eq.time()+tau);
572 tmp = x; tmp += dxsum;
573 State zero(x); zero = 0;
587 A.getAssembler().toSequence(0,neq,rhs.begin());
609 matrix->solve(rhs,sol);
611 for (
int k=0; k<rhs.size(); ++k) assert(std::isfinite(rhs[k]));
612 for (
int k=0; k<sol.size(); ++k) assert(std::isfinite(sol[k]));
613 dx.read(sol.begin());
619 CoefficientVectors solution(EvolutionEquation::AnsatzVars::template
620 CoefficientVectorRepresentation<0,neq>::init(ansatzVars.spaces) );
623 CoefficientVectors rhs(assembler.
rhs());
630 Dune::BiCGSTABSolver<LinearSpaceX> cg(A,p,1e-7,2000,0);
631 Dune::InverseOperatorResult res;
632 cg.apply(solution,rhs,res);
633 if ( !(res.converged) || (res.iterations == 2001) ) {
634 std::cout <<
" no of iterations in cg = " << res.iterations << std::endl;
635 std::cout <<
" convergence status of cg = " << res.converged << std::endl;
638 dx.data = solution.data;
687 std::vector<std::pair<double,double> > e(ansatzVars.noOfVariables);
691 ansatzVars.spaces,eq.scaling(),e.begin());
696 template <
class OutStream>
710 typename EvolutionEquation::AnsatzVars
const& ansatzVars;
An adaptor presenting a Dune::LinearOperator <domain_type,range_type> interface to a contiguous sub-b...
static bool const present
A representation of a finite element function space defined over a domain covered by a grid.
LocalToGlobalMapper const & mapper() const
Provides read access to the node manager.
Abstract base class for matrix factorizations.
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.
Defines assembly of hierarchically extended problems for defining DLY style error estimators.
virtual void apply(domain_type &x, range_type const &b)
Extrapolated linearly implicit Euler method.
EvolutionEquation::AnsatzVars::VariableSet State
State const & step(State const &x, double dt, int order)
double matrixAssemblyTime
void advanceTime(double dt)
State const & step(State const &x, double dt, int order, std::vector< std::pair< double, double > > const &tolX)
std::vector< std::pair< double, double > > estimateError(State const &x, int i, int j) const
State const & step(State const &x, double dt, int order, std::vector< std::pair< double, double > > const &tolX, std::vector< std::vector< bool > > &refinements)
ExtrapolationTableau< State > extrap
Limex(GridManager< typename EvolutionEquation::AnsatzVars::Grid > &gridManager_, EvolutionEquation &eq_, typename EvolutionEquation::AnsatzVars const &ansatzVars_, DirectType st=DirectType::UMFPACK)
void reportTime(OutStream &out) const
std::vector< SparseIndexInt > ridx
row indices
std::vector< SparseIndexInt > cidx
column indices
std::vector< Scalar > data
data
Self & setTau(Scalar dt)
Sets the time step size.
Proxy class for the linearization of semi-linear time stepping schemes.
Factorization of sparse linear systems with UMFPack.
A class that stores information about a set of variables.
size_t degreesOfFreedom(int first=0, int last=noOfVariables) const
Computes the total number of scalar degrees of freedom collected in the variables [first,...
Spaces spaces
A heterogeneous sequence of pointers to (const) spaces involved.
void toSequence(int rbegin, int rend, DataOutIter xi) const
Writes the subvector defined by the half-open block range [rbegin,rend) to the output iterator xi.
static unsigned int const RHS
TestVariableSetDescription::template CoefficientVectorRepresentation< first, last >::type rhs() const
Returns a contiguous subrange of the rhs coefficient vectors.
static unsigned int const MATRIX
size_t nnz(size_t rbegin=0, size_t rend=TestVariableSetDescription::noOfVariables, size_t cbegin=0, size_t cend=AnsatzVariableSetDescription::noOfVariables, bool extractOnlyLowerTriangle=false) const
Returns the number of structurally nonzero entries in the submatrix defined by the half-open block ra...
void assemble(F const &f, unsigned int flags=Assembler::EVERYTHING, int nThreads=0, bool verbose=false)
Assembly without block filter or cell filter.
Error estimation via hierachic FEM.
DirectType
Available direct solvers for linear equation systems.
@ UMFPACK3264
NO LONGER SUPPORTED.
@ UMFPACK
UMFPack from SuiteSparse, using 32 bit integer indices.
Hierarchic Finite Elements.
bool fabscompare(double a, double b)
void relativeError(Variables const &varDesc, Functions const &f1, Functions const &f2, Functions const &f3, Spaces const &spaces, Scaling const &scaling, OutIter out)
std::remove_pointer_t< typename boost::fusion::result_of::value_at_c< Spaces, Idx >::type > type
Output of mesh and solution for visualization software.