Dear Sir or Madam,

I met a problem about loading data from hdf5 file. Attached is a simple
test program.

A introduction of the program is as follows:
I create a 3d DMDA object(50*50*50), and create 2 global vectors x and y by
DMDA, then I write the vector x into file test.h5.
When I try to load the vector y from test.h5, the error occurs that "Global
size of array in file is 2500, not 125000 as expected", but the size of the
array should be 125000 which I've checked in python.

Thank you for your help!

Best regards
ZHOU Yingjie
#include <petscdm.h>
#include <petscdmda.h>
#include <petscdmdatypes.h>
#include <petscdmtypes.h>
#include <petscerror.h>
#include <petscksp.h>
#include <petscmat.h>
#include <petscmath.h>
#include <petscsys.h>
#include <petscsystypes.h>
#include <petscvec.h>
#include <petscviewer.h>
#include <petscviewerhdf5.h>

int main(int argc, char **argv) {
  PetscCall(
      PetscInitialize(&argc, &argv, (char *)0, "Toplogical Optimiazation\n"));

  DM dm;
  Vec x;
  Vec y;
  PetscInt m = 50;
  PetscCall(DMDACreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,
                         DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, m, m, m,
                         PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 1, 1, NULL,
                         NULL, NULL, &dm));
  PetscCall(DMSetFromOptions(dm));
  PetscCall(DMSetUp(dm));
  PetscCall(DMCreateGlobalVector(dm, &x));
  PetscCall(VecDuplicate(x, &y));
  PetscCall(PetscObjectSetName((PetscObject)x, "xvalue"));
  char str[80];

  PetscViewer viewer;
  sprintf(str, "test.h5");
  PetscCall(VecSet(x, 1.0));
  PetscCall(PetscObjectSetName((PetscObject)x, "xvalue"));
  PetscCall(
      PetscViewerHDF5Open(PETSC_COMM_SELF, str, FILE_MODE_WRITE, &viewer));
  PetscCall(PetscViewerSetFromOptions(viewer));
  PetscCall(VecView(x, viewer));
  PetscCall(PetscViewerDestroy(&viewer));

  sprintf(str, "test.h5");
  PetscCall(PetscViewerHDF5Open(PETSC_COMM_SELF, str, FILE_MODE_READ, &viewer));
  PetscCall(PetscViewerSetFromOptions(viewer));

  PetscCall(VecCreateSeq(PETSC_COMM_SELF, m * m * m, &y));
  PetscCall(PetscObjectSetName((PetscObject)y, "xvalue"));
  PetscCall(VecLoad(y, viewer));

  PetscCall(PetscViewerDestroy(&viewer));
  PetscCall(VecDestroy(&x));
  PetscCall(VecDestroy(&y));

  PetscCall(PetscFinalize());
}

Reply via email to