DUNE PDELab (git)

Assembling a linear system from a PDE

This recipe shows how to assemble a linear system of equations discretizing a PDE.

In particular, we start from a LocalOperator representing the PDE's bilinear form. Rather than defining one by ourselves, we use one already provided in PDELab. Further, we assume that a GridFunctionSpace and Constraints have already been set up.

LOP lop(problem);
Definition: convectiondiffusionfem.hh:47

This per-element LocalOperator can now be plugged into a GridOperator, which is capable of assembling a matrix for the entire domain.

auto go = GO(gfs,cc,gfs,cc,lop,MBE(nonzeros));
Standard grid operator implementation.
Definition: gridoperator.hh:36

Next, we set up our degree of freedom vector.

typedef Dune::PDELab::Backend::Vector<GFS,NumberType> X;
X x(gfs,0.0);

and ensure it matches the Dirichlet boundary conditions at constrained degrees of freedom. In addition to specifying Dirichlet constrained degrees of freedom, it also serves as initial guess at unconstrained ones.

G g(grid.leafGridView(),problem);
Definition: convectiondiffusionparameter.hh:325
void interpolate(const F &f, const GFS &gfs, XG &xg)
interpolation from a given grid function
Definition: interpolate.hh:177

Now, from the GridOperator, we can assemble our residual vector depending on our initial guess

X d(gfs,0.0);
go.residual(x,d);

and print it to console.

Dune::printvector(std::cout, native(d), "Residual vector", "");
void printvector(std::ostream &s, const V &v, std::string title, std::string rowtext, int columns=1, int width=10, int precision=2)
Print an ISTL vector.
Definition: io.hh:89

Likewise, we can assemble our matrix which again depends on the initial guess

typedef GO::Jacobian M;
M A(go);
go.jacobian(x,A);

and print it as well.

Dune::printmatrix(std::cout, native(A), "Stiffness matrix", "");
void printmatrix(std::ostream &s, const M &A, std::string title, std::string rowtext, int width=10, int precision=2)
Print a generic block matrix.
Definition: io.hh:213

Full example code: recipe-linear-system-assembly.cc

Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 18, 22:30, 2024)