### How can I get the Matrix and the right side vector associated with the finite element problem in c++?

320

views

0

Hi all

I'm Valerio and I'm using FEniCS fenics/2016.1.0 in C++. My aim is to simply solve a differential problem. I saw several demos and the command used is:

// Compute solution

Function u(V);

solve(a == L, u, bc);

with "a" the left side of my weak formulation problem, "L" the right side of my weak formulation problem, "u" the solution (my "u" is a vector field) and "bc" the boundary condition. I want to resolve that problem in a Matrix form, in particular:

find u such that:

Au=f

with A Matrix associated to "a", f vector associated to "L" and u the same solution of the previous problem. How can i build the Matrix A and the vector f from "a" and "L"? What is the command that I have to launch to calculate u when A and f are available?

I need to do that because I have to multiply the Matrix A and the vector f with a Matrix P before the calculation of the solution u.

Thank you for you attention

I'm Valerio and I'm using FEniCS fenics/2016.1.0 in C++. My aim is to simply solve a differential problem. I saw several demos and the command used is:

// Compute solution

Function u(V);

solve(a == L, u, bc);

with "a" the left side of my weak formulation problem, "L" the right side of my weak formulation problem, "u" the solution (my "u" is a vector field) and "bc" the boundary condition. I want to resolve that problem in a Matrix form, in particular:

find u such that:

Au=f

with A Matrix associated to "a", f vector associated to "L" and u the same solution of the previous problem. How can i build the Matrix A and the vector f from "a" and "L"? What is the command that I have to launch to calculate u when A and f are available?

I need to do that because I have to multiply the Matrix A and the vector f with a Matrix P before the calculation of the solution u.

Thank you for you attention

Community: FEniCS Project

### 2 Answers

3

Hi,

it's not a full MWE, but consider the following C++ snippets (note there is not even that much difference with the python implementation)

1) Make sure to include at least the Form class, the Matrix class, the Vector class, Function class, the assemble class and the solve class, i.e.

2) Instantiate Matrix and Vector and assemble into system

3) Solve, given properly defined function

it's not a full MWE, but consider the following C++ snippets (note there is not even that much difference with the python implementation)

1) Make sure to include at least the Form class, the Matrix class, the Vector class, Function class, the assemble class and the solve class, i.e.

```
#include <dolfin/fem/Form.h>
#include <dolfin/la/Matrix.h>
#include <dolfin/la/Vector.h>
#include <dolfin/function/Function.h>
#include <dolfin/fem/assemble.h>
#include "dolfin/la/solve.h"
```

2) Instantiate Matrix and Vector and assemble into system

```
Matrix A;
Vector f;
// Assemble, note YOUR_LHS/RHS_FORM must be Form objects
assemble(A, [YOUR_LHS_FORM])
assemble(f, [YOUR_RHS_FORM])
```

3) Solve, given properly defined function

```
// Initialize funcion
Function u([YOUR_FUNCTION_SPACE])
// Solve system
solve(A, *(u.vector()), f)
```

Hope this gives you some guidance

3

In the elasticity demo you can find how to assemble and solve your system (for the assemble part look the lines 129 to 133, whereas for solving look the line 162)

Please login to add an answer/comment or follow this question.