xref: /petsc/src/sys/objects/pname.c (revision 28b400f66ebc7ae0049166a2294dfcd3df27e64b)
1e5c89e4eSSatish Balay 
2af0996ceSBarry Smith #include <petsc/private/petscimpl.h>        /*I    "petscsys.h"   I*/
3665c2dedSJed Brown #include <petscviewer.h>
4e5c89e4eSSatish Balay 
5e5c89e4eSSatish Balay /*@C
6e5c89e4eSSatish Balay    PetscObjectSetName - Sets a string name associated with a PETSc object.
7e5c89e4eSSatish Balay 
8e5c89e4eSSatish Balay    Not Collective
9e5c89e4eSSatish Balay 
10e5c89e4eSSatish Balay    Input Parameters:
11e5c89e4eSSatish Balay +  obj - the Petsc variable
12e5c89e4eSSatish Balay          Thus must be cast with a (PetscObject), for example,
13e5c89e4eSSatish Balay          PetscObjectSetName((PetscObject)mat,name);
14e5c89e4eSSatish Balay -  name - the name to give obj
15e5c89e4eSSatish Balay 
1695452b02SPatrick Sanan    Notes:
1795452b02SPatrick Sanan     If this routine is not called then the object may end up being name by PetscObjectName().
18e5c89e4eSSatish Balay    Level: advanced
19e5c89e4eSSatish Balay 
20343722f2SBarry Smith .seealso: PetscObjectGetName(), PetscObjectName()
21e5c89e4eSSatish Balay @*/
227087cfbeSBarry Smith PetscErrorCode  PetscObjectSetName(PetscObject obj,const char name[])
23e5c89e4eSSatish Balay {
24e5c89e4eSSatish Balay   PetscFunctionBegin;
253cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
265f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(obj->name));
275f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(name,&obj->name));
28e5c89e4eSSatish Balay   PetscFunctionReturn(0);
29e5c89e4eSSatish Balay }
30e5c89e4eSSatish Balay 
31317d6ea6SBarry Smith /*@C
32988878efSVaclav Hapla       PetscObjectPrintClassNamePrefixType - used in the XXXView() methods to display information about the class, name, prefix and type of an object
33317d6ea6SBarry Smith 
34317d6ea6SBarry Smith    Input Parameters:
35317d6ea6SBarry Smith +     obj - the PETSc object
3698c3331eSBarry Smith -     viewer - ASCII viewer where the information is printed, function does nothing if the viewer is not PETSCVIEWERASCII type
37317d6ea6SBarry Smith 
38317d6ea6SBarry Smith    Level: developer
39317d6ea6SBarry Smith 
4095452b02SPatrick Sanan    Notes:
4195452b02SPatrick Sanan     If the viewer format is PETSC_VIEWER_ASCII_MATLAB then the information is printed after a % symbol
4298c3331eSBarry Smith           so that MATLAB will treat it as a comment.
4398c3331eSBarry Smith 
4447a3fc75SMatthew G. Knepley           If the viewer format is PETSC_VIEWER_ASCII_VTK*, PETSC_VIEWER_ASCII_LATEX, or
459bc64357SJonathan Guyer           PETSC_VIEWER_ASCII_MATRIXMARKET then don't print header information
469bc64357SJonathan Guyer           as these formats can't process it.
479bc64357SJonathan Guyer 
4823f88b65SBarry Smith    Developer Note: The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used
4923f88b65SBarry Smith                    to actually print the object.
5023f88b65SBarry Smith 
51317d6ea6SBarry Smith .seealso: PetscObjectSetName(), PetscObjectName()
52317d6ea6SBarry Smith 
53317d6ea6SBarry Smith @*/
54dae58748SBarry Smith PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
55317d6ea6SBarry Smith {
56369bc716SBarry Smith   MPI_Comm          comm;
57369bc716SBarry Smith   PetscMPIInt       size;
5898c3331eSBarry Smith   PetscViewerFormat format;
5998c3331eSBarry Smith   PetscBool         flg;
60317d6ea6SBarry Smith 
61317d6ea6SBarry Smith   PetscFunctionBegin;
625f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg));
6323f88b65SBarry Smith   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(0);
6498c3331eSBarry Smith   if (!flg) PetscFunctionReturn(0);
6598c3331eSBarry Smith 
665f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerGetFormat(viewer,&format));
678ec8862eSJed Brown   if (format == PETSC_VIEWER_ASCII_VTK_DEPRECATED || format == PETSC_VIEWER_ASCII_VTK_CELL_DEPRECATED || format == PETSC_VIEWER_ASCII_VTK_COORDS_DEPRECATED || format == PETSC_VIEWER_ASCII_MATRIXMARKET || format == PETSC_VIEWER_ASCII_LATEX || format == PETSC_VIEWER_ASCII_GLVIS) PetscFunctionReturn(0);
681678b73fSBarry Smith 
695f80ce2aSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) CHKERRQ(PetscViewerASCIIPrintf(viewer,"%%"));
705f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPrintf(viewer,"%s Object:",obj->class_name));
715f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIUseTabs(viewer,PETSC_FALSE));
72317d6ea6SBarry Smith   if (obj->name) {
735f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer," %s",obj->name));
74317d6ea6SBarry Smith   }
75317d6ea6SBarry Smith   if (obj->prefix) {
765f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer," (%s)",obj->prefix));
77317d6ea6SBarry Smith   }
785f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm(obj,&comm));
795f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_size(comm,&size));
805f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIPrintf(viewer," %d MPI processes\n",size));
815f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerASCIIUseTabs(viewer,PETSC_TRUE));
825f80ce2aSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) CHKERRQ(PetscViewerASCIIPrintf(viewer,"%%"));
83317d6ea6SBarry Smith   if (obj->type_name) {
845f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name));
85317d6ea6SBarry Smith   } else {
865f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscViewerASCIIPrintf(viewer,"  type not yet set\n"));
87317d6ea6SBarry Smith   }
88317d6ea6SBarry Smith   PetscFunctionReturn(0);
89317d6ea6SBarry Smith }
90317d6ea6SBarry Smith 
91e5c89e4eSSatish Balay /*@C
92e5c89e4eSSatish Balay    PetscObjectName - Gives an object a name if it does not have one
93e5c89e4eSSatish Balay 
94199a6bf1SJed Brown    Collective
95e5c89e4eSSatish Balay 
96e5c89e4eSSatish Balay    Input Parameters:
97e5c89e4eSSatish Balay .  obj - the Petsc variable
98e5c89e4eSSatish Balay          Thus must be cast with a (PetscObject), for example,
99317d6ea6SBarry Smith          PetscObjectName((PetscObject)mat,name);
100e5c89e4eSSatish Balay 
101317d6ea6SBarry Smith    Level: developer
102e5c89e4eSSatish Balay 
10395452b02SPatrick Sanan    Notes:
10495452b02SPatrick Sanan     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.
105e04113cfSBarry Smith           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
1069ddcd10cSBarry Smith           share the same name.
1079ddcd10cSBarry Smith 
1089ddcd10cSBarry Smith    Developer Note: this needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.
1099ddcd10cSBarry Smith 
110e5c89e4eSSatish Balay .seealso: PetscObjectGetName(), PetscObjectSetName()
111e5c89e4eSSatish Balay @*/
1127087cfbeSBarry Smith PetscErrorCode  PetscObjectName(PetscObject obj)
113e5c89e4eSSatish Balay {
114480cf27aSJed Brown   PetscCommCounter *counter;
115480cf27aSJed Brown   PetscMPIInt      flg;
116e5c89e4eSSatish Balay   char             name[64];
117e5c89e4eSSatish Balay 
118e5c89e4eSSatish Balay   PetscFunctionBegin;
1193cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
120e5c89e4eSSatish Balay   if (!obj->name) {
1219fe7d45dSJed Brown     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
1225f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_get_attr(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg));
123*28b400f6SJacob Faibussowitsch     PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
1249fe7d45dSJed Brown     ucomm.ptr = NULL;
1259fe7d45dSJed Brown     ucomm.comm = obj->comm;
1265f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm));
1279fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1289fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1299fe7d45dSJed Brown      * abuse acceptable. */
1305f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSNPrintf(name,64,"%s_%p_%" PetscInt_FMT,obj->class_name,ucomm.ptr,counter->namecount++));
1315f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrallocpy(name,&obj->name));
132e5c89e4eSSatish Balay   }
133e5c89e4eSSatish Balay   PetscFunctionReturn(0);
134e5c89e4eSSatish Balay }
135e5c89e4eSSatish Balay 
1367087cfbeSBarry Smith PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
137e5c89e4eSSatish Balay {
138e5c89e4eSSatish Balay   PetscFunctionBegin;
1393cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
1405f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(obj->type_name));
1415f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscStrallocpy(type_name,&obj->type_name));
142f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
1435f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback)));
144e5c89e4eSSatish Balay   PetscFunctionReturn(0);
145e5c89e4eSSatish Balay }
146