xref: /petsc/src/sys/objects/pname.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
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
12*811af0c4SBarry Smith          Thus must be cast with a (`PetscObject`), for example,
13*811af0c4SBarry Smith          `PetscObjectSetName`((`PetscObject`)mat,name);
14e5c89e4eSSatish Balay -  name - the name to give obj
15e5c89e4eSSatish Balay 
16e5c89e4eSSatish Balay    Level: advanced
17e5c89e4eSSatish Balay 
18*811af0c4SBarry Smith    Note:
19*811af0c4SBarry Smith     If this routine is not called then the object may end up being name by `PetscObjectName()`.
20*811af0c4SBarry Smith 
21db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectName()`
22e5c89e4eSSatish Balay @*/
239371c9d4SSatish Balay PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[]) {
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
32*811af0c4SBarry Smith       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
36*811af0c4SBarry 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:
41*811af0c4SBarry Smith    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 
44*811af0c4SBarry Smith    If the viewer format is `PETSC_VIEWER_ASCII_VTK*`, `PETSC_VIEWER_ASCII_LATEX`, or
45*811af0c4SBarry Smith    `PETSC_VIEWER_ASCII_MATRIXMARKET` then don't print header information
469bc64357SJonathan Guyer    as these formats can't process it.
479bc64357SJonathan Guyer 
48*811af0c4SBarry Smith    Developer Note:
49*811af0c4SBarry Smith    The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used to actually print the object.
5023f88b65SBarry Smith 
51db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectName()`
52317d6ea6SBarry Smith @*/
539371c9d4SSatish Balay PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj, PetscViewer viewer) {
54369bc716SBarry Smith   PetscMPIInt       size;
5598c3331eSBarry Smith   PetscViewerFormat format;
5698c3331eSBarry Smith   PetscBool         flg;
57317d6ea6SBarry Smith 
58317d6ea6SBarry Smith   PetscFunctionBegin;
599566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg));
6023f88b65SBarry Smith   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(0);
6198c3331eSBarry Smith   if (!flg) PetscFunctionReturn(0);
6298c3331eSBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
649371c9d4SSatish Balay   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)
659371c9d4SSatish Balay     PetscFunctionReturn(0);
661678b73fSBarry Smith 
679566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
688cc725e6SPierre Jolivet   PetscCallMPI(MPI_Comm_size(PetscObjectComm(obj), &size));
698cc725e6SPierre 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" : ""));
709566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
71317d6ea6SBarry Smith   if (obj->type_name) {
729566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  type: %s\n", obj->type_name));
73317d6ea6SBarry Smith   } else {
749566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  type not yet set\n"));
75317d6ea6SBarry Smith   }
76317d6ea6SBarry Smith   PetscFunctionReturn(0);
77317d6ea6SBarry Smith }
78317d6ea6SBarry Smith 
79e5c89e4eSSatish Balay /*@C
80e5c89e4eSSatish Balay    PetscObjectName - Gives an object a name if it does not have one
81e5c89e4eSSatish Balay 
82199a6bf1SJed Brown    Collective
83e5c89e4eSSatish Balay 
84e5c89e4eSSatish Balay    Input Parameters:
85e5c89e4eSSatish Balay .  obj - the Petsc variable
86*811af0c4SBarry Smith          Thus must be cast with a (`PetscObject`), for example,
87*811af0c4SBarry Smith          `PetscObjectName`((`PetscObject`)mat,name);
88e5c89e4eSSatish Balay 
89317d6ea6SBarry Smith    Level: developer
90e5c89e4eSSatish Balay 
9195452b02SPatrick Sanan    Notes:
9295452b02SPatrick 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.
93*811af0c4SBarry Smith 
94*811af0c4SBarry 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 
97*811af0c4SBarry Smith    Developer Note:
98*811af0c4SBarry Smith    This needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.
999ddcd10cSBarry Smith 
100db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
101e5c89e4eSSatish Balay @*/
1029371c9d4SSatish Balay PetscErrorCode PetscObjectName(PetscObject obj) {
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) {
1109371c9d4SSatish Balay     union
1119371c9d4SSatish Balay     {
1129371c9d4SSatish Balay       MPI_Comm comm;
1139371c9d4SSatish Balay       void    *ptr;
1149371c9d4SSatish Balay       char     raw[sizeof(MPI_Comm)];
1159371c9d4SSatish Balay     } ucomm;
1169566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(obj->comm, Petsc_Counter_keyval, (void *)&counter, &flg));
11728b400f6SJacob Faibussowitsch     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Bad MPI communicator supplied; must be a PETSc communicator");
1189fe7d45dSJed Brown     ucomm.ptr  = NULL;
1199fe7d45dSJed Brown     ucomm.comm = obj->comm;
1209566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(ucomm.raw, sizeof(MPI_Comm), MPI_BYTE, 0, obj->comm));
1219fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1229fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1239fe7d45dSJed Brown      * abuse acceptable. */
1249566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "%s_%p_%" PetscInt_FMT, obj->class_name, ucomm.ptr, counter->namecount++));
1259566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &obj->name));
126e5c89e4eSSatish Balay   }
127e5c89e4eSSatish Balay   PetscFunctionReturn(0);
128e5c89e4eSSatish Balay }
129e5c89e4eSSatish Balay 
1309371c9d4SSatish Balay PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[]) {
131e5c89e4eSSatish Balay   PetscFunctionBegin;
1323cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1339566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->type_name));
1349566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type_name, &obj->type_name));
135f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
1369566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE] * sizeof(PetscFortranCallback)));
137e5c89e4eSSatish Balay   PetscFunctionReturn(0);
138e5c89e4eSSatish Balay }
139