CG Solver with own VectorType

45 views
Skip to first unread message

Stefan Karch

unread,
Apr 25, 2023, 9:15:42 AM4/25/23
to deal.II User Group
Hello everyone,
I am trying to implement a CG Solver with my own VectorType. According to Lecture 21, what one needs is that the Vector and Operator are compatible, and that the operator has a vmult-function. However, the compiler misses a lot of member function of my own Vector class. Am I doing something wrong or do I need to implement all additional member function for "MyVector"?

Thanks in advance,
Stefan

Examplary error message:  ‘class MyVector’ has no member named ‘reinit’

Minimal (Not) working example:

#include <deal.II/lac/solver_cg.h>
#include <deal.II/lac/vector_memory.templates.h>

class MyVector : public dealii::Subscriptor
{
public:
MyVector(){};

using value_type = double;
};
template class dealii::VectorMemory<MyVector>;
template class dealii::GrowingVectorMemory<MyVector>;

class MySystemMatrix : public dealii::Subscriptor
{
public:
MySystemMatrix();

void vmult(MyVector & dst,
const MyVector &src) const;

};

class InverseMatrix : public dealii::Subscriptor
{
public:
InverseMatrix(const MySystemMatrix & matrix);

void vmult(MyVector &dst, const MyVector &src) const;

private:
const dealii::SmartPointer<const MySystemMatrix> _matrix;
};

void InverseMatrix::vmult(
MyVector& dst,
const MyVector& src) const
{
dealii::SolverControl solver_control( 100, 1e-6 );

dealii::GrowingVectorMemory<MyVector> GVM;
dealii::SolverCG<MyVector> solver(solver_control, GVM);

solver.solve<MySystemMatrix, MySystemMatrix>(*_matrix, dst, src, *_matrix);
}

InverseMatrix::InverseMatrix(
const MySystemMatrix & matrix)
: _matrix(&matrix)
{}

Wolfgang Bangerth

unread,
Apr 25, 2023, 12:12:44 PM4/25/23
to dea...@googlegroups.com
On 4/25/23 07:15, Stefan Karch wrote:
> I am trying to implement a CG Solver with my own VectorType. According
> to Lecture 21, what one needs is that the Vector and Operator are
> compatible, and that the operator has a vmult-function. However, the
> compiler misses a lot of member function of my own Vector class. Am I
> doing something wrong or do I need to implement all additional member
> function for "MyVector"?

Stefan:
that's the only requirement on the "operator* (that it has to have a
vmult function), but the vector really needs to satisfy pretty much the
entire VectorSpaceVector interface. CG and the other iterative solvers
require dot products, vector updates, and norms -- all functions you
need to implement in the vector class you want to use.

While there are good reasons to implement your own operator class, it
isn't quite clear to me why anyone would want to implement their own
vector class. Can you elaborate on this point?

Best
W.



--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

Stefan Karch

unread,
Apr 25, 2023, 2:15:59 PM4/25/23
to deal.II User Group
Thanks for your quick response. I was motivated by the lecture to try to implement my own vector structure, but it wasn't at first clear for me that it requires way more effort than a vmult operation. (But of course it makes sense.) I tried this to get more used to the solver/vector/matrix structure, but of course the dealii Vector classes provide me a sufficient structure.
Reply all
Reply to author
Forward
0 new messages