8#ifndef KASKADE_BRIDGE_HH_
9#define KASKADE_BRIDGE_HH_
11#include <boost/signals2.hpp>
12#include <boost/bind.hpp>
20 template <
class Value>
23 if(val < 0) val = newVal;
28 template <
class Linearization>
32 template <
typename... Args>
54 template<
class Functional>
66 typedef typename DomainElement::Descriptions::template CoefficientVectorRepresentation<>::type
CoefficientVector;
67 typedef Dune::LinearOperator<CoefficientVector, CoefficientVector>
OperatorType;
105 int cols(
int cbegin=0,
int cend=-1)
const
109 return x.descriptions.degreesOfFreedom(cbegin,cend);
113 int rows(
int rbegin=0,
int rend=-1)
const
116 assert(rbegin < rend);
117 return x.descriptions.degreesOfFreedom(rbegin,rend);
128 mat =
ass->template get<MatrixAsTriplet<Scalar> >(
false,rbegin,rend,cbegin,cend);
139 void getRHSBlocks(std::vector<Scalar>& rhs,
int rbegin,
int rend)
const
142 rhs.resize(
rows(rbegin,rend),0.0);
143 ass->toSequence(rbegin,rend,rhs.begin());
152 int nColBlocks()
const {
return Functional::AnsatzVars::noOfVariables; }
155 int nRowBlocks()
const {
return Functional::TestVars::noOfVariables; }
165 x = Bridge::getImpl<DomainElement>(x_);
198 assert(rbegin<=rend);
199 std::vector<Scalar> rhs(
rows(rbegin,rend),0.0);
208 d2axpy(1.0,y,
x,rbegin,rend,cbegin,cend);
215 assert( cbegin <= cend );
216 assert( rbegin <= rend );
217 std::vector<Scalar> result, argument;
222 mat.
axpy(result, argument, a);
230 d2taxpy(1.0,y,
x,rbegin,rend,cbegin,cend);
238 assert( cbegin <= cend );
239 assert( rbegin <= rend );
240 std::vector<Scalar> result, argument;
272 for(
int i=rbegin; i<rend; ++i)
274 std::vector<Scalar> rhsD;
278 assert(rhsD.size() ==
rows(i,i+1));
279 for(
int k=
rows(rbegin,i), l=0;k<
rows(rbegin,i+1);++k, ++l)
287 for(
int i=rbegin; i<rend; ++i)
288 for(
int j=cbegin; j<cend; ++j)
299 int toDoFlag= ((~ass->valid()) & flags);
307 mutable std::shared_ptr<Assembler>
ass;
308 std::unique_ptr<Vector<DomainElement> >
xptr;
313 template<
class Functional,
int stateId=1,
int adjo
intId=2>
317 typedef typename Base::Scalar Scalar;
318 typedef typename Base::Assembler Assembler;
319 typedef typename Base::DomainElement DomainElement;
320 typedef typename Base::ImageElement ImageElement;
321 using Base::nRowBlocks;
322 using Base::nColBlocks;
339 if(rbegin==adjointId && rend==adjointId+1 && cbegin==adjointId && cend==adjointId+1)
return;
341 if(rbegin==stateId && rend==stateId+1 && cbegin==stateId && cend==stateId+1)
343 mat = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,adjointId,adjointId+1,adjointId,adjointId+1);
347 if(rbegin<=stateId && rend>stateId && cbegin<=stateId && cend > stateId)
349 size_t rows0 = 0, cols0 = 0, rows1 = 0, cols1 = 0;
353 tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,rbegin,stateId,cbegin,cend);
359 tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,stateId,stateId+1,cbegin,stateId);
364 tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,adjointId,adjointId+1,adjointId,adjointId+1);
370 tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,stateId,stateId+1,stateId+1,cend);
376 tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,stateId+1,rend,cbegin,cend);
383 mat = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,rbegin,rend,cbegin,cend);
389 this->getDiscreteMatrixBlocks(matD,rbegin,rend,cbegin,cend);
398 if(rbegin==adjointId && rend==adjointId+1 && cbegin==adjointId && cend==adjointId+1)
return;
399 assert(cbegin<=cend);
400 assert(rbegin<=rend);
401 std::vector<Scalar> result, argument;
406 mat.
axpy(result, argument);
414 if( rbegin == adjointId && rend == adjointId+1 && cbegin == adjointId && cend == adjointId+1 )
return;
415 assert( cbegin <= cend );
416 assert( rbegin <= rend );
417 std::vector<Scalar> result, argument;
422 mat.
axpy(result, argument, a);
427 template<
class Functional,
int stateId=1,
int adjo
intId=2>
431 typedef typename Base::Scalar Scalar;
432 typedef typename Base::Assembler Assembler;
433 typedef typename Base::DomainElement DomainElement;
434 typedef typename Base::ImageElement ImageElement;
435 using Base::nRowBlocks;
436 using Base::nColBlocks;
454 if(rbegin==adjointId && rend==adjointId+1 && cbegin==adjointId && cend==adjointId+1)
return;
457 if(rend > adjointId && cend > adjointId)
459 mat = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,rbegin,rend-1,cbegin,cend-1);
460 size_t rows = mat.
nrows(), cols = mat.
ncols();
461 auto tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,rend-1,rend,cbegin,cend-1);
462 tmp.shiftIndices(rows,0);
464 tmp = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,rbegin,rend-1,cend-1,cend);
469 mat = this->ass->template get<MatrixAsTriplet<Scalar> >(
false,rbegin,rend,cbegin,cend);
474 this->getDiscreteMatrixBlocks(matD,rbegin,rend,cbegin,cend);
483 if(rbegin==adjointId && rend==adjointId+1 && cbegin==adjointId && cend==adjointId+1)
return;
484 assert(cbegin<=cend);
485 assert(rbegin<=rend);
486 std::vector<Scalar> result, argument;
491 mat.
axpy(result, argument);
499 if( rbegin == adjointId && rend == adjointId+1 && cbegin == adjointId && cend == adjointId+1 )
return;
500 assert( cbegin <= cend );
501 assert( rbegin <= rend );
502 std::vector<Scalar> result, argument;
507 mat.
axpy(result, argument, a);
Interfaces for function space oriented algorithms.
Abstract Vector for function space algorithms.
ConnectedLinearization(const Args &... args)
boost::signals2::signal< void()> changed
virtual ~ConnectedLinearization()
boost::signals2::connection flushconn
virtual void connectToSignalForFlush(boost::signals2::signal< void()> &sig)
Object that represents the linearization of a nonlinear functional, implements AbstractLineariztion.
FunctionalImpl::Scalar Scalar
Bridge::Linearization class that uses a VariationalFunctionalAssembler to create linear systems.
Dune::LinearOperator< CoefficientVector, CoefficientVector > OperatorType
double eval() const
return the current value Functional(Origin)
int nRowBlocks() const
return number of rows
KaskadeLinearization(Functional const &fu_, DomainElement const &x_)
Creation of a linearization for a functional fu at x_.
void getRHSBlocks(std::vector< Scalar > &rhs, int rbegin, int rend) const
write components of the gradient into rhs
void ddxpy(AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
DomainElement::Descriptions::template CoefficientVectorRepresentation ::type CoefficientVector
void addDiscreteRHSBlocks(std::vector< Scalar > &rhs, int rbegin, int rend) const
void flush()
flush all data, gathered so far
double getValue() const
value of function
void getMatrixBlocks(MatrixAsTriplet< Scalar > &mat, int rbegin, int rend, int cbegin, int cend) const
write blocks of the hessian matrix into mat
Assembler const & getValidAssembler() const
KaskadeLinearization(KaskadeLinearization const &other)
void ddtxpy(AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
void d2axpy(double a, AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
Evaluate hessian times second argument: y = y+a*ddf*x.
int rows(int rbegin=0, int rend=-1) const
Number of rows of components [rbegin, rend)
Functional::AnsatzVars::VariableSet DomainElement
void d2taxpy(double a, AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
Evaluate hessian times second argument: y = y+a*ddf*x.
int nColBlocks() const
return number of columns
void setOrigin(AbstractFunctionSpaceElement const &x_)
double evalL1norm() const
Evaluate L1 norm of integrand.
virtual ~KaskadeLinearization()
void getDiscreteMatrixBlocks(MatrixAsTriplet< Scalar > &mat, int rbegin, int rend, int cbegin, int cend) const
Functional::Scalar Scalar
LinearizationAt< Functional > Implementation
KaskadeLinearization(Functional const &fu_, DomainElement const &x_, std::shared_ptr< Assembler > const &ass_)
Creation of a linearization for a functional fu at x_.
static const int nThreads
VariationalFunctionalAssembler< Implementation > Assembler
void doAssemble(int flags) const
Functional::TestVars::VariableSet ImageElement
AbstractFunctionSpaceElement const & getOrigin() const
return point of linearization
bool inDomain(DomainElement const &x)
return whether x is in the domain of definition
std::shared_ptr< Assembler > ass
Functional const & getFunctional() const
void evald(AbstractFunctionSpaceElement &v, int rbegin, int rend) const
Evaluate derivative.
int cols(int cbegin=0, int cend=-1) const
Number of columns of components [cbegin, cend)
Implementation const & getLinImpl() const
return the implementation
std::unique_ptr< Vector< DomainElement > > xptr
NormalStepLinearization(Base const &other)
void getMatrixBlocks(MatrixAsTriplet< Scalar > &mat, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
write blocks of the hessian matrix into mat
void ddxpy(AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
NormalStepLinearization(Functional const &fu, DomainElement const &x, std::shared_ptr< Assembler > const &assembler)
Creation of a linearization for a functional fu at x_.
NormalStepLinearization(Functional const &fu, DomainElement const &x)
Creation of a linearization for a functional fu at x_.
void d2axpy(double a, AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
NormalStepLinearization()
TangentialStepLinearization()
TangentialStepLinearization(Functional const &fu, DomainElement const &x)
Creation of a linearization for a functional fu at x_.
void d2axpy(double a, AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
TangentialStepLinearization(Functional const &fu, DomainElement const &x, std::shared_ptr< Assembler > const &assembler)
Creation of a linearization for a functional fu at x_.
void getMatrixBlocks(MatrixAsTriplet< Scalar > &mat, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
write blocks of the hessian matrix into mat
TangentialStepLinearization(Base const &other)
void ddxpy(AbstractFunctionSpaceElement &y, AbstractFunctionSpaceElement const &x, int rbegin=0, int rend=-1, int cbegin=0, int cend=-1) const
Mathematical Vector that supports copy-on-write, implements AbstractFunctionSpaceElement.
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
SparseIndexInt ncols() const
Returns number of cols (computes them, if not known)
void shiftIndices(SparseIndexInt row, SparseIndexInt col)
Shifts matrix indices. Can be used together with += to concatenate sparese matrices.
MatrixAsTriplet & transpose()
Transposition.
void axpy(Y &out, X const &in, Scalar alpha=1.0, int nvectors=1) const
Matrix-vector multiplication: out += alpha * (*this) * in.
Abstract base class for a sparse linear system.
A class for assembling Galerkin representation matrices and right hand sides for variational function...
static unsigned int const VALUE
DEPRECATED, enums in the Assembler namespace.
static unsigned int const RHS
static unsigned int const MATRIX
void assignIfNegative(Value &val, Value newVal)
Bridge classes that connect low level FE algorithms to higher level algorithms.
static void getMatrixBlock(MatrixAsTriplet< Scalar > &mat, Functional const &fu, Vars const &origin, int row, int col)
static Scalar getValue(Functional const &fu, Vars const &origin)
static void getRHSBlock(std::vector< Scalar > &rhs, Functional const &fu, Vars const &origin, int row)
static bool inDomain(DomainElement const &x)