From 11fa7032180d0c640fee98c24a51e3796509c92a Mon Sep 17 00:00:00 2001
From: Emilie Sauvage <emilie.sauvage@uclouvain.be>
Date: Mon, 7 Feb 2011 14:34:28 +0000
Subject: [PATCH] Function to extract the CSR structure

---
 Solver/linearSystemPETSc.h | 52 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/Solver/linearSystemPETSc.h b/Solver/linearSystemPETSc.h
index faba13f749..e14e3bc57c 100644
--- a/Solver/linearSystemPETSc.h
+++ b/Solver/linearSystemPETSc.h
@@ -261,6 +261,58 @@ class linearSystemPETSc : public linearSystem<scalar> {
     return 1;
   }
   Mat &getMatrix(){ return _a; }
+  
+  std::vector<double> getData()
+  {
+    _try(MatAssemblyBegin(_a, MAT_FINAL_ASSEMBLY));
+    _try(MatAssemblyEnd(_a, MAT_FINAL_ASSEMBLY));
+    PetscScalar *v;
+    _try(MatGetArray(_a,&v));
+    MatInfo info;
+    _try(MatGetInfo(_a,MAT_LOCAL,&info));
+    std::vector<double> data; // Maybe I should reserve or resize (SAM)
+    for (int i = 0; i < info.nz_allocated; i++)
+      data.push_back(v[i]);
+    _try(MatRestoreArray(_a,&v));
+    return data;
+  }
+
+  std::vector<int> getRowPointers()
+  {
+    _try(MatAssemblyBegin(_a, MAT_FINAL_ASSEMBLY));
+    _try(MatAssemblyEnd(_a, MAT_FINAL_ASSEMBLY));
+    PetscInt *rows;
+    PetscInt *columns;
+    PetscInt n;
+    PetscTruth done;
+    _try(MatGetRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done));        //case done == PETSC_FALSE should be handled
+    std::vector<int> rowPointers; // Maybe I should reserve or resize (SAM)
+    for (int i = 0; i <= n; i++)
+      rowPointers.push_back(rows[i]);
+    _try(MatRestoreRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done));
+    return rowPointers;
+  }
+
+  std::vector<int> getColumnsIndices()
+  {
+    _try(MatAssemblyBegin(_a, MAT_FINAL_ASSEMBLY));
+    _try(MatAssemblyEnd(_a, MAT_FINAL_ASSEMBLY));
+    PetscInt *rows;
+    PetscInt *columns;
+    PetscInt n;
+    PetscTruth done;
+    _try(MatGetRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done));        //case done == PETSC_FALSE should be handled
+    MatInfo info;
+    _try(MatGetInfo(_a,MAT_LOCAL,&info));
+    std::vector<int> columnIndices; // Maybe I should reserve or resize (SAM)
+    for (int i = 0; i <  info.nz_allocated; i++)
+      columnIndices.push_back(columns[i]);
+    _try(MatRestoreRowIJ(_a,0,PETSC_FALSE,PETSC_FALSE,&n,&rows,&columns,&done));
+    return columnIndices;
+  }
+
+
+  
 };
 
 class binding;
-- 
GitLab