Hi All,

How to set different object names when using multiple VecView? I try to use PetscObjectSetName with multiple output, but the object name is overwritten by the last one.

As shown below, as well as the enclosed files as example, the vector name in sol.vtk is vec_v for both vector u and v.

      call PetscViewerCreate(PETSC_COMM_WORLD, viewer, ierr);CHKERRA(ierr)
      call PetscViewerSetType(viewer, PETSCVIEWERVTK, ierr);CHKERRA(ierr)
call PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK, ierr);CHKERRA(ierr)
      call PetscViewerFileSetName(viewer, 'sol.vtk', ierr);CHKERRA(ierr)

      call PetscObjectSetName(u, 'vec_u', ierr);CHKERRA(ierr)
      call VecView(u, viewer, ierr);CHKERRA(ierr)

      call PetscObjectSetName(v, 'vec_v', ierr);CHKERRA(ierr)
      call VecView(v, viewer, ierr);CHKERRA(ierr)

      call PetscViewerDestroy(viewer, ierr);CHKERRA(ierr)

      call DMRestoreGlobalVector(dm, u, ierr);CHKERRA(ierr)
      call DMRestoreGlobalVector(dm, v, ierr);CHKERRA(ierr)

Thanks,

Danyang

      program DMPlexTestField
#include "petsc/finclude/petscdmplex.h"
#include "petsc/finclude/petscdmlabel.h"
      use petscdmplex
      implicit none

      DM :: dm
      DMLabel :: label
      Vec :: u, v
      PetscViewer :: viewer
      PetscSection :: section
      PetscInt :: dim,numCells,numFields,numBC
      PetscInt :: i,val
      PetscInt, target, dimension(3) ::  numComp
      PetscInt, pointer :: pNumComp(:)
      PetscInt, target, dimension(12) ::  numDof
      PetscInt, pointer :: pNumDof(:)
      PetscInt, target, dimension(1) ::  bcField
      PetscInt, pointer :: pBcField(:)
      PetscInt :: zero,eight
      IS, target, dimension(1) ::   bcCompIS
      IS, target, dimension(1) ::   bcPointIS
      IS, pointer :: pBcCompIS(:)
      IS, pointer :: pBcPointIS(:)
      PetscBool :: interpolate
      PetscErrorCode :: ierr

      call PetscInitialize(PETSC_NULL_CHARACTER, ierr)
      if (ierr .ne. 0) then
        print*,'Unable to initialize PETSc'
        stop
      endif
      dim = 2
      call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-dim', dim,PETSC_NULL_BOOL, ierr);CHKERRA(ierr)
      interpolate = PETSC_TRUE
!     Create a mesh
      if (dim .eq. 2) then
         numCells = 2
      else
         numCells = 1
      endif
      call DMPlexCreateBoxMesh(PETSC_COMM_WORLD, dim, numCells,interpolate, dm, ierr);CHKERRA(ierr)
!     Create a scalar field u, a vector field v, and a surface vector field w
      numFields  = 3
      numComp(1) = 1
      numComp(2) = 1
      numComp(3) = 1
      pNumComp => numComp
      do i = 1, numFields*(dim+1)
         numDof(i) = 0
      end do
!     Let u be defined on vertices
      numDof(0*(dim+1)+1)     = 1
!     Let v be defined on cells
      numDof(1*(dim+1)+1) = 1
!     Let v be defined on faces
      numDof(2*(dim+1)+1)   = 1
      pNumDof => numDof
!     Setup boundary conditions
      numBC = 1
!     Test label retrieval
      call DMGetLabel(dm, 'marker', label, ierr);CHKERRA(ierr)
      zero = 0
      call DMLabelGetValue(label, zero, val, ierr);CHKERRA(ierr)
      if (val .ne. -1) then
        CHKERRA(1)
      endif
      eight = 8
      call DMLabelGetValue(label, eight, val, ierr);CHKERRA(ierr)
      if (val .ne. 1) then
        CHKERRA(1)
      endif
!     Prescribe a Dirichlet condition on u on the boundary
!       Label "marker" is made by the mesh creation routine
      bcField(1) = 0
      pBcField => bcField
      call ISCreateStride(PETSC_COMM_WORLD, 1, 0, 1, bcCompIS(1), ierr);CHKERRA(ierr)
      pBcCompIS => bcCompIS
      call DMGetStratumIS(dm, 'marker', 1, bcPointIS(1),ierr);CHKERRA(ierr)
      pBcPointIS => bcPointIS
!     Create a PetscSection with this data layout
      call DMPlexCreateSection(dm,dim,numFields,pNumComp,pNumDof,numBC,pBcField,pBcCompIS,pBcPointIS,PETSC_NULL_IS,section,ierr)
      CHKERRA(ierr)
      call ISDestroy(bcCompIS(1), ierr);CHKERRA(ierr)
      call ISDestroy(bcPointIS(1), ierr);CHKERRA(ierr)
!     Name the Field variables
      call PetscSectionSetFieldName(section, 0, 'u', ierr);CHKERRA(ierr)
      call PetscSectionSetFieldName(section, 1, 'v', ierr);CHKERRA(ierr)
      call PetscSectionSetFieldName(section, 2, 'w', ierr);CHKERRA(ierr)
      call PetscSectionView(section, PETSC_VIEWER_STDOUT_WORLD, ierr);CHKERRA(ierr)
!     Tell the DM to use this data layout
      call DMSetDefaultSection(dm, section, ierr);CHKERRA(ierr)
!     Create a Vec with this layout and view it
      call DMGetGlobalVector(dm, u, ierr);CHKERRA(ierr)
      call VecDuplicate(u,v,ierr);CHKERRA(ierr)

      call PetscViewerCreate(PETSC_COMM_WORLD, viewer, ierr);CHKERRA(ierr)
      call PetscViewerSetType(viewer, PETSCVIEWERVTK, ierr);CHKERRA(ierr)
      call PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK, ierr);CHKERRA(ierr)
      call PetscViewerFileSetName(viewer, 'sol.vtk', ierr);CHKERRA(ierr)

      call PetscObjectSetName(u, 'vec_u', ierr);CHKERRA(ierr)
      call VecView(u, viewer, ierr);CHKERRA(ierr)

      call PetscObjectSetName(v, 'vec_v', ierr);CHKERRA(ierr)
      call VecView(v, viewer, ierr);CHKERRA(ierr)

      call PetscViewerDestroy(viewer, ierr);CHKERRA(ierr)

      call DMRestoreGlobalVector(dm, u, ierr);CHKERRA(ierr)
      call DMRestoreGlobalVector(dm, v, ierr);CHKERRA(ierr)
!     Cleanup
      call PetscSectionDestroy(section, ierr);CHKERRA(ierr)
      call DMDestroy(dm, ierr);CHKERRA(ierr)

      call PetscFinalize(ierr)
      end program DMPlexTestField

!/*TEST
!  build:
!    requires: define(PETSC_USING_F90FREEFORM)
!
!  test:
!    suffix: 0
!    requires: triangle
!
!  test:
!    suffix: 1
!    requires: ctetgen
!    args: -dim 3
!
!TEST*/
CFLAGS          =
FFLAGS          =
CPPFLAGS        =
FPPFLAGS        =
LOCDIR          = src/dm/impls/plex/examples/tutorials/
EXAMPLESC       = ex1.c ex2.c ex5.c
EXAMPLESF       = ex1f90.F90
MANSEC          = DM

include ${PETSC_DIR}/lib/petsc/conf/variables
include ${PETSC_DIR}/lib/petsc/conf/rules

ex1: ex1.o  chkopts
        -${CLINKER} -o ex1 ex1.o ${PETSC_DM_LIB}
        ${RM} -f ex1.o

ex1f90: ex1f90.o  chkopts
        -${FLINKER} -o ex1f90 ex1f90.o  ${PETSC_DM_LIB}
        ${RM} -f ex1f90.o

ex2: ex2.o  chkopts
        -${CLINKER} -o ex2 ex2.o ${PETSC_DM_LIB}
        ${RM} -f ex2.o

ex5: ex5.o  chkopts
        -${CLINKER} -o ex5 ex5.o ${PETSC_DM_LIB}
        ${RM} -f ex5.o

ex6: ex6.o  chkopts
        -${CLINKER} -o ex6 ex6.o ${PETSC_DM_LIB}
        ${RM} -f ex6.o

ex7: ex7.o  chkopts
        -${CLINKER} -o ex7 ex7.o ${PETSC_DM_LIB}
        ${RM} -f ex7.o

#--------------------------------------------------------------------------

runex1:
        -@${MPIEXEC} -n 2 ./ex1 -dim 3

runex1f90:
        -@${MPIEXEC} -n 2 ./ex1 -dim 2

runex2:
        -@${MPIEXEC} -n 1 ./ex2 -dim 3 -dm_refine 2 -dm_view hdf5:ex2.h5
        -${PETSC_DIR}/bin/petsc_gen_xdmf.py ex2.h5

runex5:
        -@${MPIEXEC} -n 2 ./ex5 -filename 
${PETSC_DIR}/share/petsc/datafiles/meshes/square.msh -new_dm_view

include ${PETSC_DIR}/lib/petsc/conf/test
# vtk DataFile Version 2.0
Simplicial Mesh Example
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 8 double
0.000000e+00 0.000000e+00 0.000000e+00
1.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 1.000000e+00 0.000000e+00
1.000000e+00 1.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 1.000000e+00
1.000000e+00 0.000000e+00 1.000000e+00
0.000000e+00 1.000000e+00 1.000000e+00
1.000000e+00 1.000000e+00 1.000000e+00
CELLS 6 30
4  0 7 3 2
4  0 5 7 4
4  0 1 3 7
4  5 1 0 7
4  0 6 7 2
4  7 6 0 4
CELL_TYPES 6
10
10
10
10
10
10
POINT_DATA 8
VECTORS vec_v double
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
VECTORS vec_v double
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00
0.000000e+00 0.000000e+00 0.000000e+00

Reply via email to