Exemplo n.º 1
0
PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer)
{
  PetscInt       dim, dof, M = 0, N = 0, P = 0;
  PetscErrorCode ierr;

  PetscFunctionBegin;
  ierr = DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
  if (!da->coordinates) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP, "VTK output requires DMDA coordinates.");
  /* Write Header */
  ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr);
  ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr);
  ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr);
  ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr);
  ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr);
  ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr);
  if (da->coordinates) {
    DM  dac;
    Vec natural;

    ierr = DMGetCoordinateDM(da, &dac);CHKERRQ(ierr);
    ierr = DMDACreateNaturalVector(dac, &natural);CHKERRQ(ierr);
    ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr);
    ierr = DMDAGlobalToNaturalBegin(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr);
    ierr = DMDAGlobalToNaturalEnd(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr);
    ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS);CHKERRQ(ierr);
    ierr = VecView(natural, viewer);CHKERRQ(ierr);
    ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
    ierr = VecDestroy(&natural);CHKERRQ(ierr);
  }
  PetscFunctionReturn(0);
}
Exemplo n.º 2
0
/**
 * output_mat_and_vec
 * ------
 * Output the matrices and vectors that can be used in MATLAB to solve various problems.
 */
PetscErrorCode output_mat_and_vec(const Mat A, const Vec b, const Vec right_precond, const Mat CF, const GridInfo gi)
{
	PetscFunctionBegin;
	PetscErrorCode ierr;

	char output_name_prefixed[PETSC_MAX_PATH_LEN];
	//const char *prefix = "/out/";
	const char *ind_extension = "_ind";
	const char *A_extension = "_A";
	const char *b_extension = "_b";
	const char *precond_extension = "_pR";
	const char *CF_extension = "_CF";

	//ierr = PetscStrcpy(output_name_prefixed, getenv("FD3D_ROOT")); CHKERRQ(ierr);
	//ierr = PetscStrcat(output_name_prefixed, prefix); CHKERRQ(ierr);
	//ierr = PetscStrcat(output_name_prefixed, gi.output_name); CHKERRQ(ierr);
	ierr = PetscStrcpy(output_name_prefixed, gi.output_name); CHKERRQ(ierr);

	char ind_file[PETSC_MAX_PATH_LEN];
	char A_file[PETSC_MAX_PATH_LEN];
	char b_file[PETSC_MAX_PATH_LEN];
	char precond_file[PETSC_MAX_PATH_LEN];
	char CF_file[PETSC_MAX_PATH_LEN];

	ierr = PetscStrcpy(ind_file, output_name_prefixed); CHKERRQ(ierr);
	ierr = PetscStrcat(ind_file, ind_extension); CHKERRQ(ierr);
	ierr = PetscStrcpy(A_file, output_name_prefixed); CHKERRQ(ierr);
	ierr = PetscStrcat(A_file, A_extension); CHKERRQ(ierr);
	ierr = PetscStrcpy(b_file, output_name_prefixed); CHKERRQ(ierr);
	ierr = PetscStrcat(b_file, b_extension); CHKERRQ(ierr);
	ierr = PetscStrcpy(precond_file, output_name_prefixed); CHKERRQ(ierr);
	ierr = PetscStrcat(precond_file, precond_extension); CHKERRQ(ierr);
	ierr = PetscStrcpy(CF_file, output_name_prefixed); CHKERRQ(ierr);
	ierr = PetscStrcat(CF_file, CF_extension); CHKERRQ(ierr);

	PetscViewer viewer;

	/** It turns out that VecView() shows the DA vector in natural order.  Therefore, even though 
	  indApp is constructed in application order, it is shown in natural order by VecView().  
	  On the other hand, indNat reorder indApp in natural order and then distribute the vector to 
	  processors.  However, because VecView() reorder a vector before it prints out the content of 
	  the vector, indNat is shown messy by VecView(). 
	  Inconsistently, MatView() does not reorder the matrix elements into natural order before it 
	  shows the matrix.  Therefore, when the binaries of matrices and vectors are imported in MATLAB,
	  I need to reorder the matrices but not the vectors. */
	Vec indApp;
	//ierr = create_index(&indApp, gi); CHKERRQ(ierr);
	ierr = createFieldArray(&indApp, set_index_at, gi); CHKERRQ(ierr);
	//ierr = VecView(indApp, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);

	Vec indNat;
	ierr = DMDACreateNaturalVector(gi.da, &indNat); CHKERRQ(ierr);
	ierr = VecCopy(indApp, indNat); CHKERRQ(ierr);
	ierr = VecDestroy(&indApp); CHKERRQ(ierr);
	//ierr = VecView(indNat, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr);

	/** Write the index vector ind_app. */
	ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); CHKERRQ(ierr);
	ierr = PetscViewerSetType(viewer, PETSCVIEWERBINARY); CHKERRQ(ierr);
	ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); CHKERRQ(ierr);
	ierr = PetscViewerFileSetName(viewer, ind_file); CHKERRQ(ierr);
	ierr = VecView(indNat, viewer); CHKERRQ(ierr);
	ierr = VecDestroy(&indNat); CHKERRQ(ierr);
	ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);

	/** Write the coefficient matrix A. */
	ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); CHKERRQ(ierr);
	ierr = PetscViewerSetType(viewer, PETSCVIEWERBINARY); CHKERRQ(ierr);
	ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); CHKERRQ(ierr);
	ierr = PetscViewerFileSetName(viewer, A_file); CHKERRQ(ierr);
	ierr = MatView(A, viewer); CHKERRQ(ierr);
	ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);

	/** Write the RHS vector b. */
	ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); CHKERRQ(ierr);
	ierr = PetscViewerSetType(viewer, PETSCVIEWERBINARY); CHKERRQ(ierr);
	ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); CHKERRQ(ierr);
	ierr = PetscViewerFileSetName(viewer, b_file); CHKERRQ(ierr);
	ierr = VecView(b, viewer); CHKERRQ(ierr);
	ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);

	/** Write the right preconditioner vector pR. */
	ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); CHKERRQ(ierr);
	ierr = PetscViewerSetType(viewer, PETSCVIEWERBINARY); CHKERRQ(ierr);
	ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); CHKERRQ(ierr);
	ierr = PetscViewerFileSetName(viewer, precond_file); CHKERRQ(ierr);
	ierr = VecView(right_precond, viewer); CHKERRQ(ierr);
	ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);

	/** Write the E-to-H converter matrix CF. */
	ierr = PetscViewerCreate(PETSC_COMM_WORLD, &viewer); CHKERRQ(ierr);
	ierr = PetscViewerSetType(viewer, PETSCVIEWERBINARY); CHKERRQ(ierr);
	ierr = PetscViewerFileSetMode(viewer, FILE_MODE_WRITE); CHKERRQ(ierr);
	ierr = PetscViewerFileSetName(viewer, CF_file); CHKERRQ(ierr);
	ierr = MatView(CF, viewer); CHKERRQ(ierr);
	ierr = PetscViewerDestroy(&viewer); CHKERRQ(ierr);

	PetscFunctionReturn(0);
}