Actual source code: pname.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

  2:  #include <petsc/private/petscimpl.h>
  3:  #include <petscviewer.h>

  5: /*@C
  6:    PetscObjectSetName - Sets a string name associated with a PETSc object.

  8:    Not Collective

 10:    Input Parameters:
 11: +  obj - the Petsc variable
 12:          Thus must be cast with a (PetscObject), for example,
 13:          PetscObjectSetName((PetscObject)mat,name);
 14: -  name - the name to give obj

 16:    Notes:
 17:     If this routine is not called then the object may end up being name by PetscObjectName().
 18:    Level: advanced

 20:    Concepts: object name^setting

 22: .seealso: PetscObjectGetName(), PetscObjectName()
 23: @*/
 24: PetscErrorCode  PetscObjectSetName(PetscObject obj,const char name[])
 25: {

 30:   PetscFree(obj->name);
 31:   PetscStrallocpy(name,&obj->name);
 32:   return(0);
 33: }

 35: /*@C
 36:       PetscObjectPrintClassNamePrefixType - used in the XXXView() methods to display information about the class, name, prefix and type of an object

 38:    Input Parameters:
 39: +     obj - the PETSc object
 40: -     viewer - ASCII viewer where the information is printed, function does nothing if the viewer is not PETSCVIEWERASCII type

 42:    Level: developer

 44:    Notes:
 45:     If the viewer format is PETSC_VIEWER_ASCII_MATLAB then the information is printed after a % symbol
 46:           so that MATLAB will treat it as a comment.

 48:           If the viewer format is PETSC_VIEWER_ASCII_VTK*, PETSC_VIEWER_ASCII_LATEX, or
 49:           PETSC_VIEWER_ASCII_MATRIXMARKET then don't print header information
 50:           as these formats can't process it.

 52:    Developer Note: The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used
 53:                    to actually print the object.

 55: .seealso: PetscObjectSetName(), PetscObjectName()

 57: @*/
 58: PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
 59: {
 60:   PetscErrorCode    ierr;
 61:   MPI_Comm          comm;
 62:   PetscMPIInt       size;
 63:   PetscViewerFormat format;
 64:   PetscBool         flg;

 67:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg);
 68:   if (obj->donotPetscObjectPrintClassNamePrefixType) return(0);
 69:   if (!flg) return(0);

 71:   PetscViewerGetFormat(viewer,&format);
 72:   if (format == PETSC_VIEWER_ASCII_VTK || format == PETSC_VIEWER_ASCII_VTK_CELL || format == PETSC_VIEWER_ASCII_VTK_COORDS || format == PETSC_VIEWER_ASCII_MATRIXMARKET || format == PETSC_VIEWER_ASCII_LATEX || format == PETSC_VIEWER_ASCII_GLVIS) return(0);

 74:   if (format == PETSC_VIEWER_ASCII_MATLAB) {PetscViewerASCIIPrintf(viewer,"%%");}
 75:   PetscViewerASCIIPrintf(viewer,"%s Object:",obj->class_name);
 76:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
 77:   if (obj->name) {
 78:     PetscViewerASCIIPrintf(viewer," %s",obj->name);
 79:   }
 80:   if (obj->prefix) {
 81:     PetscViewerASCIIPrintf(viewer," (%s)",obj->prefix);
 82:   }
 83:   PetscObjectGetComm(obj,&comm);
 84:   MPI_Comm_size(comm,&size);
 85:   PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size);
 86:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
 87:   if (format == PETSC_VIEWER_ASCII_MATLAB) {PetscViewerASCIIPrintf(viewer,"%%");}
 88:   if (obj->type_name) {
 89:     PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name);
 90:   } else {
 91:     PetscViewerASCIIPrintf(viewer,"  type not yet set\n");
 92:   }
 93:   return(0);
 94: }

 96: /*@C
 97:    PetscObjectName - Gives an object a name if it does not have one

 99:    Collective

101:    Input Parameters:
102: .  obj - the Petsc variable
103:          Thus must be cast with a (PetscObject), for example,
104:          PetscObjectName((PetscObject)mat,name);

106:    Level: developer

108:    Concepts: object name^setting default

110:    Notes:
111:     This is used in a small number of places when an object NEEDS a name, for example when it is saved to MATLAB with that variable name.
112:           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
113:           share the same name.

115:    Developer Note: this needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.



119: .seealso: PetscObjectGetName(), PetscObjectSetName()
120: @*/
121: PetscErrorCode  PetscObjectName(PetscObject obj)
122: {
123:   PetscErrorCode   ierr;
124:   PetscCommCounter *counter;
125:   PetscMPIInt      flg;
126:   char             name[64];

130:   if (!obj->name) {
131:     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
132:     MPI_Comm_get_attr(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg);
133:     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
134:     ucomm.ptr = NULL;
135:     ucomm.comm = obj->comm;
136:     MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm);
137:     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
138:      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
139:      * abuse acceptable. */
140:     PetscSNPrintf(name,64,"%s_%p_%D",obj->class_name,ucomm.ptr,counter->namecount++);
141:     PetscStrallocpy(name,&obj->name);
142:   }
143:   return(0);
144: }



148: PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
149: {

154:   PetscFree(obj->type_name);
155:   PetscStrallocpy(type_name,&obj->type_name);
156:   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
157:   PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback));
158:   return(0);
159: }