Commit f7e25361 authored by Qicang's avatar Qicang
Browse files

Add the changes for dumping matrix to the master

parent ef0deee4
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -96,6 +96,8 @@ TYPE,EXTENDS(BaseFileType) :: FortranFileType
  LOGICAL(SBK),PRIVATE :: formatstat=.FALSE.
  !> Whether or not the file is direct or sequential
  LOGICAL(SBK),PRIVATE :: accessstat=.FALSE.
  !> Whether or not the file is stream
  LOGICAL(SBK),PRIVATE :: accessstream=.FALSE.
  !> The 'new' status of a file
  LOGICAL(SBK) :: newstat=.FALSE.
  !> Whether or not to replace the file (implies new)
@@ -142,6 +144,9 @@ TYPE,EXTENDS(BaseFileType) :: FortranFileType
    !> @copybrief FileType_Fortran::isDirect_fortran_file
    !> @copydetails FileType_Fortran::isDirect_fortran_file
    PROCEDURE,PASS :: isDirect => isDirect_fortran_file
    !> @copybrief FileType_Fortran::isStream_fortran_file
    !> @copydetails FileType_Fortran::isStream_fortran_file
    PROCEDURE,PASS :: isStream => isStream_fortran_file
    !> @copybrief FileType_Fortran::getRecLen_fortran_file
    !> @copydetails FileType_Fortran::getRecLen_fortran_file
    PROCEDURE,PASS :: getRecLen => getRecLen_fortran_file
@@ -383,7 +388,8 @@ SUBROUTINE init_fortran_file(fileobj,unit,file,status,access,form, &
    ENDIF
    fileobj%formatstat=(TRIM(formval) == 'FORMATTED')
    fileobj%padstat=(TRIM(padval) ==  'YES')
    IF(TRIM(accessval) == 'DIRECT' .OR. TRIM(accessval) == 'STREAM') THEN
    fileobj%accessstream=(TRIM(accessval)=='STREAM')
    IF(TRIM(accessval) == 'DIRECT') THEN
      fileobj%accessstat=.TRUE.
      IF(fileobj%reclval < 1) CALL fileobj%e%raiseError(modName//'::'// &
          myName//' - Record length must be set to greater than 0 for '// &
@@ -502,6 +508,18 @@ PURE FUNCTION isDirect_fortran_file(file) RESULT(bool)
ENDFUNCTION isDirect_fortran_file
!
!-------------------------------------------------------------------------------
!> @brief Returns whether or not the FORTRAN file is stream
!> access.
!> @param file the fortran file type object
!> @returns bool TRUE/FALSE if the file is stream access
!>
PURE FUNCTION isStream_fortran_file(file) RESULT(bool)
  CLASS(FortranFileType),INTENT(IN) :: file
  LOGICAL(SBK) :: bool
  bool=file%accessstream
ENDFUNCTION isStream_fortran_file
!
!-------------------------------------------------------------------------------
!> @brief Returns the record length for a direct access file.
!> @param file the fortran file type object
!> @returns val the size of the records
@@ -591,8 +609,12 @@ SUBROUTINE open_fortran_file(file)
      IF(file%isDirect()) THEN
        accessvar='DIRECT'
        reclval=file%reclval
      ELSE
        IF(file%isStream()) THEN
          accessvar='STREAM'
        ELSE
          accessvar='SEQUENTIAL'
        ENDIF
        reclval=0
      ENDIF
      !ACTION clause value
+12 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ PUBLIC :: MatrixType_Paramsflag
!> set enumeration scheme for matrix types
INTEGER(SIK),PARAMETER,PUBLIC :: SPARSE=0,TRIDIAG=1,DENSESQUARE=2,DENSERECT=3, &
    BANDED=4,DISTRIBUTED_BANDED=5,DISTR_BLOCKBANDED=6
INTEGER(SIK),PARAMETER,PUBLIC :: ASCII_MATLAB=1,BINARY=2
INTEGER(SIK),PARAMETER,PUBLIC :: FILE_WRITE=1,FILE_APPEND=2
PUBLIC :: SparseMatrixType_reqParams,SparseMatrixType_optParams
PUBLIC :: TriDiagMatrixType_reqParams,TriDiagMatrixType_optParams
PUBLIC :: BandedMatrixType_reqParams,BandedMatrixType_optParams
@@ -114,6 +116,16 @@ ABSTRACT INTERFACE
  ENDSUBROUTINE matrix_transpose_sub_absintfc
ENDINTERFACE

!> Explicitly defines the interface for the get routine of all matrix types
ABSTRACT INTERFACE
  SUBROUTINE matrix_write_absintfc(matrix,format_mode,FILE_MODE,filename)
    IMPORT :: SIK,SRK,MatrixType
    CLASS(MatrixType),INTENT(INOUT) :: matrix
    INTEGER(SIK),INTENT(IN),OPTIONAL :: format_mode
    INTEGER(SIK),INTENT(IN),OPTIONAL :: FILE_MODE
    CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: filename
  ENDSUBROUTINE matrix_write_absintfc
ENDINTERFACE
!> @brief The extended type of matrices for square matrices
TYPE,ABSTRACT,EXTENDS(MatrixType) :: SquareMatrixType
  !> Indicates whether or not the matrix is symmetric
+807 −0

File changed.

Preview size limit exceeded, changes collapsed.

+85 −0
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ TYPE,EXTENDS(DistributedMatrixType) :: PETScMatrixType

  !> Number of columns for nonsquare matrices:
  INTEGER(SIK) :: m
  INTEGER(SIK) :: mat_write_counter=0
!
!List of Type Bound Procedures
  CONTAINS
@@ -80,6 +81,9 @@ TYPE,EXTENDS(DistributedMatrixType) :: PETScMatrixType
    !> @copybrief MatrixTypes::zeroentries_PETScMatrixType
    !> @copydetails MatrixTypes::zeroentries_PETScMatrixType
    PROCEDURE,PASS :: zeroentries => zeroentries_PETScMatrixType
    !> @copybrief MatrixTypes::zeroentries_PETScMatrixType
    !> @copydetails MatrixTypes::zeroentries_PETScMatrixType
    PROCEDURE,PASS :: writematrix => writematrix_PETScMatrixType
ENDTYPE PETScMatrixType

!> Name of module
@@ -192,6 +196,7 @@ SUBROUTINE init_PETScMatrixParam(matrix,Params)
    CALL eMatrixType%raiseError('Incorrect call to '// &
        modName//'::'//myName//' - MatrixType already initialized')
  ENDIF
  matrix%mat_write_counter=0
ENDSUBROUTINE init_PETScMatrixParam

!
@@ -604,6 +609,86 @@ SUBROUTINE zeroentries_PETScMatrixType(matrix)
  REQUIRE(matrix%isInit)
  CALL MatZeroEntries(matrix%a,ierr)
ENDSUBROUTINE zeroentries_PETScMatrixType
!
!-------------------------------------------------------------------------------
!> @brief dump the matrix
!> @param matrix declare the matrix type to act on
!> @param format_mode the format of the file
!> @param file_mode declare whether to refresh or append to the file
!> @param filename the name of the file for the matrix
SUBROUTINE writematrix_PETScMatrixType(matrix,format_mode,file_mode,filename)
  CHARACTER(LEN=*),PARAMETER :: myName='writematrix_PETScMatrixType'
  CLASS(PETScMatrixType),INTENT(INOUT) :: matrix
  INTEGER(SIK),INTENT(IN),OPTIONAL :: format_mode
  INTEGER(SIK),INTENT(IN),OPTIONAL :: file_mode
  CHARACTER(LEN=*),INTENT(IN),OPTIONAL :: filename
  CHARACTER(LEN=80):: matname
  INTEGER(SIK) :: file_format
  LOGICAL(SBK) :: lexist
  PetscErrorCode  :: ierr
  PetscViewerFormat :: vformat
  PetscViewer ::viewer
  PetscFileMode :: file_write_mode
  REQUIRE(matrix%isInit)
  IF (.NOT.(matrix%isAssembled)) CALL matrix%assemble()
  file_format=ASCII_MATLAB
  IF(PRESENT(format_mode)) THEN
    file_format=format_mode
  ENDIF
  SELECTCASE(file_format)
    CASE(ASCII_MATLAB)
      vformat=PETSC_VIEWER_ASCII_MATLAB
      CALL PetscViewerCreate(PETSC_COMM_WORLD,viewer, ierr)
      CALL PetscViewerSetType(viewer,PETSCVIEWERASCII, ierr)
      IF(PRESENT(file_mode)) THEN
        file_write_mode=file_mode
      ELSE
        file_write_mode=FILE_MODE_WRITE
      ENDIF
      INQUIRE(FILE=TRIM(filename), EXIST=lexist)
      IF(.NOT. lexist) file_write_mode=FILE_MODE_WRITE
      CALL PetscViewerFileSetMode(viewer,file_write_mode,ierr)
      IF(file_write_mode==FILE_MODE_APPEND) THEN
        matrix%mat_write_counter=matrix%mat_write_counter+1
        WRITE(matname,'(a,i0)') 'A',matrix%mat_write_counter
      ELSE
        matname='A'
      ENDIF
      CALL PetscObjectSetName(matrix%a,TRIM(matname),ierr)
      IF(PRESENT(filename)) THEN
        CALL PetscViewerFileSetName(viewer,TRIM(filename),ierr)
      ELSE
        ! CALL PetscViewerASCIIOpen(PETSC_COMM_WORLD, "Amat.m", viewer,ierr)
        CALL PetscViewerFileSetName(viewer,"Amat.m",ierr)
      ENDIF

      CALL PetscViewerPushFormat(viewer, &
        vformat,ierr)
      call MatView(matrix%A,viewer,ierr)
      call PetscViewerPopFormat(viewer,ierr)
      call PetscViewerDestroy(viewer,ierr)
    CASE(BINARY)
      IF(PRESENT(file_mode)) THEN
        file_write_mode=file_mode
      ELSE
        file_write_mode=FILE_MODE_WRITE
      ENDIF
      IF(PRESENT(filename)) THEN
        INQUIRE(FILE=TRIM(filename), EXIST=lexist)
        IF(.NOT. lexist) file_write_mode=FILE_MODE_WRITE
        CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD,TRIM(filename), &
            file_write_mode,viewer,ierr)
      ELSE
        CALL PetscViewerBinaryOpen(PETSC_COMM_WORLD,"Amat.bin", &
            file_write_mode,viewer,ierr)
      ENDIF
      call MatView(matrix%A,viewer,ierr)
      call PetscViewerDestroy(viewer,ierr)
    CASE DEFAULT
      CALL eMatrixType%raiseError('Unsupported format for dumping matrix '// &
        modName//'::'//myName//'!')
  ENDSELECT
ENDSUBROUTINE writematrix_PETScMatrixtype
#endif

ENDMODULE MatrixTypes_PETSc
+8 −1
Original line number Diff line number Diff line
@@ -829,6 +829,8 @@ SUBROUTINE testMatrix()
    CALL mat%set(4,3,4.0_SRK)
    CALL mat%set(4,4,4.0_SRK)
  ENDSELECT
  CALL thisMatrix%writematrix()
  CALL thisMatrix%writematrix(Format_Model=2)
  ! Test both the VectorType and native interfaces
  IF(ALLOCATED(dummyvec)) DEALLOCATE(dummyvec)
  ALLOCATE(dummyvec(4))
@@ -1229,6 +1231,8 @@ SUBROUTINE testMatrix()
           (dummyvec(9) == 6._SRK)
    ASSERT(bool, 'tridiag%get(...)') !column three check
  ENDSELECT
  CALL thisMatrix%writematrix()
  CALL thisMatrix%writematrix(Format_Model=2)
  !test with out of bounds i,j, make sure no crash.
  SELECT TYPE(thisMatrix)
  TYPE IS(TriDiagMatrixType)
@@ -1511,6 +1515,8 @@ SUBROUTINE testMatrix()
      ASSERT(bool, 'banded%matvec(...)')
    ENDDO
  ENDSELECT
  CALL thisMatrix%writematrix()
  CALL thisMatrix%writematrix(Format_Model=2)
  ! Check for non-trivial vector
  dummyvec=(/1._SRK,2._SRK,3._SRK,4._SRK/)
  SELECT TYPE(thisMatrix)
@@ -1621,7 +1627,8 @@ SUBROUTINE testMatrix()
      ASSERT(bool, 'denserect%set(...)')
    ENDDO
  ENDSELECT

  CALL thisMatrix%writematrix()
  CALL thisMatrix%writematrix(Format_Model=2)
  !Test BLAS_matvec
  COMPONENT_TEST("denseRect%matvec")
  x=1.0_SRK
Loading