/*
 * ex4.c
 *
 *  Created on: Sep 25, 2017
 *      Author: Fande Kong
 */

static char help[] = "MatAssemblyBegin destroy preallocation info.\n";

#include "petscmat.h"

int main(int argc,char **argv)
{
  Mat             A;
  MPI_Comm        comm;
  PetscErrorCode  ierr;


  ierr = PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr;
  comm = MPI_COMM_WORLD;
  ierr = MatCreateSeqAIJ(comm,10,10,4,NULL,&A);CHKERRQ(ierr);

  /*Assembly matrix before we actually do anything */
  ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);

  /* Insert a value after the matrix is assembled.
   * Causes a malloc error because the preallocation is destroyed
   * */
  ierr = MatSetValue(A,6,6,1.0,INSERT_VALUES);CHKERRQ(ierr);

  ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
  ierr = MatDestroy(&A);CHKERRQ(ierr);
  ierr = PetscFinalize();
}


