### Matrix Class

299

views

0

Hi all,

I'm Valerio and I'm using FEniCS fenics/2016.1.0 in C++. I have a problem with the manipulation of an object of the class Matrix. In my main I have on object defined:

Thank you in advance for your help

I'm Valerio and I'm using FEniCS fenics/2016.1.0 in C++. I have a problem with the manipulation of an object of the class Matrix. In my main I have on object defined:

`Matrix vss_fenics;`

How can I set the values and the dimensions of the Matrix vss_fenics? Thank you in advance for your help

Community: FEniCS Project

### 1 Answer

0

Hi,

Just a few hints. Hope this will get you started.

To initialze the dimensions and the sparsity pattern of a Matrix object, use the TensorLayout class (to see how this is used, consult the AssemblerBase class). In fact, if your matrix has a layout similar to a known Form, let FEniCS do the work and use the AssemblerBase::init_global_tensor() method.

To set the values in a matrix, use Matrix::set_local(). Don't forget to finalize the manual assemblage of your Matrix object using Matrix::apply("insert") or Matrix::apply("add")

Just a few hints. Hope this will get you started.

To initialze the dimensions and the sparsity pattern of a Matrix object, use the TensorLayout class (to see how this is used, consult the AssemblerBase class). In fact, if your matrix has a layout similar to a known Form, let FEniCS do the work and use the AssemblerBase::init_global_tensor() method.

To set the values in a matrix, use Matrix::set_local(). Don't forget to finalize the manual assemblage of your Matrix object using Matrix::apply("insert") or Matrix::apply("add")

Indeed, the segfault is because size and layout of your matrix are not initialized. What info do you have a priori about the Matrix? Is it somehow related to a certain Form?

By the way, dolfin::la_index is just an index type being compatible with the linear algebra backend.

By the way, dolfin::la_index is just an index type being compatible with the linear algebra backend.

written
10 months ago by
Jakob Maljaars

The info that I know about the Matrix is that it is double and it isn't related to a certain Form. The dolfin::Matrix in fact have to store the values of an Eigen::Matrix of double, with Dynamic rows and Dynamic columns

written
10 months ago by
Valerio

Then I think the only option is to study the TensorLayout class to initialize your matrix properly. Although you might have a look at this post on the old forum.

By the way, if your Matrix is not related to a certain Form why can't you use Eigen as the backend to do the matrix manipulation and why do you really want to cast it into a dolfin::Matrix?

By the way, if your Matrix is not related to a certain Form why can't you use Eigen as the backend to do the matrix manipulation and why do you really want to cast it into a dolfin::Matrix?

written
10 months ago by
Jakob Maljaars

In my code I have

```
Matrix A;
Vector f;
std::vector<std::shared_ptr<const DirichletBC>> dirichlet_matrici({dirichlet});
assemble_system(A,f,a,L,dirichlet_matrici);
solve(A, *(u.vector()), f);
```

and the line

`solve(A, *(u.vector()), f);`

resolves the linear system

$Au=f$`A``u`=`ƒ`

I don't want to resolve that system, but I want to resolve the system

$A_ru_r=f_r$`A`_{r}`u`_{r}=`ƒ` _{r}

With

$A_r=Y^TAY$`A`_{r}=`Y`^{T}`A``Y` $f_r=Y^Tf$`ƒ` _{r}=`Y`^{T}`ƒ`

The problem is that A is a dolfin::Matrix, f is a dolfin::Vector but Y is a Eigen::Matrix. Then I need to convert or all in dolfin::Matrix (as i tried to do) or all in Eigen::Matrix (I don't know how, i searched and i didn't find anything)

written
10 months ago by
Valerio

Clear. So in which FunctionSpace does $u_r$

`u`_{r}live?
written
10 months ago by
Jakob Maljaars

My idea was to calculate $u_r$

$u_2=Yu_r$

and then convert the vector $u_2$

`u`_{r}just as a simple vector, then calculate the new vector$u_2=Yu_r$

`u`_{2}=`Y``u`_{r}and then convert the vector $u_2$

`u`_{2}in a function of the same space of $u$`u`of the system`solve(A, *(u.vector()), f);`

written
10 months ago by
Valerio

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

but I had a segmentation fault. My aim is to copy an Eigen::Matrix in a dolfin::Matrix. Likely the segmentation fault is due to the fact that the size of the Matrix A isn't set. How do I have to correct the test code? thank you for your help