How to get the direct sum of mass matrix and itself
Hello,
I would like to take the direct sum of two matrices.
Ex:
A=
| 1 2 3 |
| 2 3 1 |
| 3 2 1 |
B = A \oplus A=
| 1 2 3 0 0 0 |
| 2 3 1 0 0 0 |
| 3 2 1 0 0 0 |
| 0 0 0 1 2 3 |
| 0 0 0 2 3 1 |
| 0 0 0 3 2 1 |
In my problem, I need the direct sum of the mass matrix and itself in C++. I hope that this sum is also a sparse matrix. Is there any way to do this?
Thanks in advance.
Best regards.
Dang
Question information
- Language:
- English Edit question
- Status:
- Solved
- For:
- DOLFIN Edit question
- Assignee:
- No assignee Edit question
- Solved by:
- Kent-Andre Mardal
- Solved:
- Last query:
- Last reply:
Revision history for this message
|
#1 |
In Python, you can do it with the block_kronecker function in cbc.block (
https:/
which uses the Python interface of Dolfin and Trilinos.
If you want do it in C++ it is probably best to cast the matrix to its PETSc
or Trilinos type and let PETSc or Trilinos perform the sum.
Kent
On 31 August 2011 17:55, Nguyen Van Dang <
<email address hidden>> wrote:
> New question #169703 on DOLFIN:
> https:/
>
> Hello,
> I would like to take the direct sum of two matrices.
> Ex:
> A=
> | 1 2 3 |
> | 2 3 1 |
> | 3 2 1 |
>
> B = A \oplus A=
>
> | 1 2 3 0 0 0 |
> | 2 3 1 0 0 0 |
> | 3 2 1 0 0 0 |
> | 0 0 0 1 2 3 |
> | 0 0 0 2 3 1 |
> | 0 0 0 3 2 1 |
>
> In my problem, I need the direct sum of the mass matrix and itself in C++.
> I hope that this sum is also a sparse matrix. Is there any way to do this?
> Thanks in advance.
> Best regards.
> Dang
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>
Revision history for this message
|
#2 |
Dear Kent,
I still have problem when I use 'solve' function for direct-sum matrix. I send you my code as well as the errors I got. Would you like to help me again?
Thanks in advance.
Dang
My code:
#include <dolfin.h>
#include "mass_matrix.h"
using namespace dolfin;
int main()
{
#ifdef HAS_SLEPC
// Create mesh
const double L1=1.0;
const double L2=1.0;
const double nx=2;
const double ny=2;
Rectangle mesh(-L1,
// Build stiffness matrix
PETScMatrix A;
mass_
mass_
assemble(A, a);
int neqn = A.size(0);
// variables
Array<double> x1(neqn)
PETScVector rhs1(neqn)
PETScVector Y1(neqn)
for (int j=0;j<neqn;j++)
{
x1[j] = j;
x2[j] = j;
x2[j+neqn] = j;
}
rhs1.
rhs2.
solve(A,Y1,rhs1);
for (int j=0;j<neqn;j++)
{
}
dolfin::uint row=0;
std::
std::
PETScMatrix M=A;
M.resize(
for (row=0;
{
A.getrow(row, columns, values);
for (int j=0;j<columns.
{
}
}
solve(M,Y2,rhs2);
#else
cout << "SLEPc must be installed to run this demo." << endl;
#endif
return 0;
}
errors:
nguyenvandang@
2.571429 -2.571429 19.714286 -2.571429 -2.571429 11.142857 67.714286 11.142857 43.714286
[0]PETSC ERROR: -------
[0]PETSC ERROR: Object is in wrong state!
[0]PETSC ERROR: Not for unassembled matrix!
[0]PETSC ERROR: -------
[0]PETSC ERROR: Petsc Release Version 3.1.0, Patch 5, Mon Sep 27 11:51:54 CDT 2010
[0]PETSC ERROR: See docs/changes/
[0]PETSC ERROR: See docs/faq.html for hints about trouble shooting.
[0]PETSC ERROR: See docs/index.html for manual pages.
[0]PETSC ERROR: -------
[0]PETSC ERROR: Unknown Name on a linux-gnu named ubuntu by nguyenvandang Wed Aug 24 22:15:51 2011
[0]PETSC ERROR: Libraries linked from /build/
[0]PETSC ERROR: Configure run at Mon Mar 7 18:34:33 2011
[0]PETSC ERROR: Configure options --with-shared --with-debugging=0 --useThreads 0 --with-
[0]PETSC ERROR: -------
[0]PETSC ERROR: MatGetOrdering() line 188 in src/mat/
[0]PETSC ERROR: PCSetUp_LU() line 125 in src/ksp/
[0]PETSC ERROR: PCSetUp() line 795 in src/ksp/
[0]PETSC ERROR: KSPSetUp() line 237 in src/ksp/
[0]PETSC ERROR: KSPSolve() line 353 in src/ksp/
Revision history for this message
|
#3 |
On 1 September 2011 11:41, Nguyen Van Dang <
<email address hidden>> wrote:
> Question #169703 on DOLFIN changed:
> https:/
>
> Status: Answered => Open
>
> Nguyen Van Dang is still having a problem:
> Dear Kent,
> I still have problem when I use 'solve' function for direct-sum matrix. I
> send you my code as well as the errors I got. Would you like to help me
> again?
>
I think you have to finialize the matrix with MatAssemblyEnd or something
similar.
Kent
> Thanks in advance.
> Dang
>
> My code:
> #include <dolfin.h>
> #include "mass_matrix.h"
>
> using namespace dolfin;
>
> int main()
> {
> #ifdef HAS_SLEPC
>
> // Create mesh
> const double L1=1.0;
> const double L2=1.0;
> const double nx=2;
> const double ny=2;
> Rectangle mesh(-L1,
>
> // Build stiffness matrix
> PETScMatrix A;
> mass_matrix:
> mass_matrix:
> assemble(A, a);
> int neqn = A.size(0);
> // variables
> Array<double> x1(neqn)
> PETScVector rhs1(neqn)
> PETScVector Y1(neqn)
>
> for (int j=0;j<neqn;j++)
> {
> x1[j] = j;
> x2[j] = j;
> x2[j+neqn] = j;
> }
> rhs1.set_local(x1);
> rhs2.set_local(x2);
> solve(A,Y1,rhs1);
> for (int j=0;j<neqn;j++)
> {
> printf(" %f",Y1[j]);
> }
> printf("\n");
> dolfin::uint row=0;
> std::vector<
> std::vector<double> values;
> PETScMatrix M=A;
> M.resize(
> for (row=0;
> {
> A.getrow(row, columns, values);
> M.setrow(row, columns, values);
> for (int j=0;j<columns.
> {
> columns[j] = columns[j] + neqn;
> }
> M.setrow(row+neqn, columns, values);
> }
>
> solve(M,Y2,rhs2);
>
> #else
>
> cout << "SLEPc must be installed to run this demo." << endl;
>
> #endif
>
> return 0;
> }
>
> errors:
>
> nguyenvandang@
> 2.571429 -2.571429 19.714286 -2.571429 -2.571429 11.142857 67.714286
> 11.142857 43.714286
> [0]PETSC ERROR: -------
> -------
> [0]PETSC ERROR: Object is in wrong state!
> [0]PETSC ERROR: Not for unassembled matrix!
> [0]PETSC ERROR:
> -------
> [0]PETSC ERROR: Petsc Release Version 3.1.0, Patch 5, Mon Sep 27 11:51:54
> CDT 2010
> [0]PETSC ERROR: See docs/changes/
> [0]PETSC ERROR: See docs/faq.html for hints about trouble shooting.
> [0]PETSC ERROR: See docs/index.html for manual pages.
> [0]PETSC ERROR:
> -------
> [0]PETSC ERROR: Unknown Name on a linux-gnu named ubuntu by nguyenvandang
> Wed Aug 24 22:15:51 2011
> [0]PETSC ERROR: Libraries linked from
> /build/
> [0]PETSC ERROR: Configure run at Mon Mar 7 18:34:33 2011
> [0]PETSC ERROR: Configure options --with-shared --with-debugging=0
> --useThreads 0 --with-
> --with-
> --with-mpi-shared=1 --with-
> --with-blacs=1 --with-
> --with-
> --with-scalapack=1 --with-
> --with-
> --with-
> --with-
> --with-umfpack=1 --with-
> --with-
> --with-spooles=1 --with-
> --with-
> --with-
> --with-
> --with-
> [0]PETSC ERROR:
> -------
> [0]PETSC ERROR: MatGetOrdering() line 188 in src/mat/
> [0]PETSC ERROR: PCSetUp_LU() line 125 in src/ksp/
> [0]PETSC ERROR: PCSetUp() line 795 in src/ksp/
> [0]PETSC ERROR: KSPSetUp() line 237 in src/ksp/
> [0]PETSC ERROR: KSPSolve() line 353 in src/ksp/
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>
Revision history for this message
|
#4 |
I just call M.apply("insert"); before call "solve"
Revision history for this message
|
#5 |
Thank you very much for your help. This solved my problem.