xref: /petsc/src/sys/objects/pname.c (revision 8cc725e69398de546bdc828d7b714aa2223f5218)
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 
20db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectName()`
21e5c89e4eSSatish Balay @*/
227087cfbeSBarry Smith PetscErrorCode  PetscObjectSetName(PetscObject obj,const char name[])
23e5c89e4eSSatish Balay {
24e5c89e4eSSatish Balay   PetscFunctionBegin;
253cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
269566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->name));
279566063dSJacob Faibussowitsch   PetscCall(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 
51db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectName()`
52317d6ea6SBarry Smith 
53317d6ea6SBarry Smith @*/
54dae58748SBarry Smith PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)
55317d6ea6SBarry Smith {
56369bc716SBarry Smith   PetscMPIInt       size;
5798c3331eSBarry Smith   PetscViewerFormat format;
5898c3331eSBarry Smith   PetscBool         flg;
59317d6ea6SBarry Smith 
60317d6ea6SBarry Smith   PetscFunctionBegin;
619566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&flg));
6223f88b65SBarry Smith   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(0);
6398c3331eSBarry Smith   if (!flg) PetscFunctionReturn(0);
6498c3331eSBarry Smith 
659566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer,&format));
668ec8862eSJed 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);
671678b73fSBarry Smith 
689566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer,"%%"));
69*8cc725e6SPierre Jolivet   PetscCallMPI(MPI_Comm_size(PetscObjectComm(obj),&size));
70*8cc725e6SPierre Jolivet   PetscCall(PetscViewerASCIIPrintf(viewer,"%s Object:%s%s%s%s%s %d MPI process%s\n",obj->class_name,obj->name?" ":"",obj->name?obj->name:"",obj->prefix?" (":"",obj->prefix?obj->prefix:"",obj->prefix?")":"",size,size>1?"es":""));
719566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer,"%%"));
72317d6ea6SBarry Smith   if (obj->type_name) {
739566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name));
74317d6ea6SBarry Smith   } else {
759566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer,"  type not yet set\n"));
76317d6ea6SBarry Smith   }
77317d6ea6SBarry Smith   PetscFunctionReturn(0);
78317d6ea6SBarry Smith }
79317d6ea6SBarry Smith 
80e5c89e4eSSatish Balay /*@C
81e5c89e4eSSatish Balay    PetscObjectName - Gives an object a name if it does not have one
82e5c89e4eSSatish Balay 
83199a6bf1SJed Brown    Collective
84e5c89e4eSSatish Balay 
85e5c89e4eSSatish Balay    Input Parameters:
86e5c89e4eSSatish Balay .  obj - the Petsc variable
87e5c89e4eSSatish Balay          Thus must be cast with a (PetscObject), for example,
88317d6ea6SBarry Smith          PetscObjectName((PetscObject)mat,name);
89e5c89e4eSSatish Balay 
90317d6ea6SBarry Smith    Level: developer
91e5c89e4eSSatish Balay 
9295452b02SPatrick Sanan    Notes:
9395452b02SPatrick 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.
94e04113cfSBarry Smith           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
959ddcd10cSBarry Smith           share the same name.
969ddcd10cSBarry Smith 
979ddcd10cSBarry 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.
989ddcd10cSBarry Smith 
99db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
100e5c89e4eSSatish Balay @*/
1017087cfbeSBarry Smith PetscErrorCode  PetscObjectName(PetscObject obj)
102e5c89e4eSSatish Balay {
103480cf27aSJed Brown   PetscCommCounter *counter;
104480cf27aSJed Brown   PetscMPIInt      flg;
105e5c89e4eSSatish Balay   char             name[64];
106e5c89e4eSSatish Balay 
107e5c89e4eSSatish Balay   PetscFunctionBegin;
1083cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
109e5c89e4eSSatish Balay   if (!obj->name) {
1109fe7d45dSJed Brown     union {MPI_Comm comm; void *ptr; char raw[sizeof(MPI_Comm)]; } ucomm;
1119566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg));
11228b400f6SJacob Faibussowitsch     PetscCheck(flg,PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
1139fe7d45dSJed Brown     ucomm.ptr = NULL;
1149fe7d45dSJed Brown     ucomm.comm = obj->comm;
1159566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(ucomm.raw,sizeof(MPI_Comm),MPI_BYTE,0,obj->comm));
1169fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1179fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1189fe7d45dSJed Brown      * abuse acceptable. */
1199566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name,64,"%s_%p_%" PetscInt_FMT,obj->class_name,ucomm.ptr,counter->namecount++));
1209566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name,&obj->name));
121e5c89e4eSSatish Balay   }
122e5c89e4eSSatish Balay   PetscFunctionReturn(0);
123e5c89e4eSSatish Balay }
124e5c89e4eSSatish Balay 
1257087cfbeSBarry Smith PetscErrorCode  PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
126e5c89e4eSSatish Balay {
127e5c89e4eSSatish Balay   PetscFunctionBegin;
1283cfa8680SLisandro Dalcin   PetscValidHeader(obj,1);
1299566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->type_name));
1309566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type_name,&obj->type_name));
131f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
1329566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE],obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]*sizeof(PetscFortranCallback)));
133e5c89e4eSSatish Balay   PetscFunctionReturn(0);
134e5c89e4eSSatish Balay }
135