xref: /petsc/src/sys/objects/pname.c (revision aec76313382a76d73a95f2051cbe4b1eab55c1c7)
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
12811af0c4SBarry Smith          Thus must be cast with a (`PetscObject`), for example,
13811af0c4SBarry Smith          `PetscObjectSetName`((`PetscObject`)mat,name);
14e5c89e4eSSatish Balay - name - the name to give obj
15e5c89e4eSSatish Balay 
16e5c89e4eSSatish Balay   Level: advanced
17e5c89e4eSSatish Balay 
18811af0c4SBarry Smith   Note:
19811af0c4SBarry Smith   If this routine is not called then the object may end up being name by `PetscObjectName()`.
20811af0c4SBarry Smith 
21db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectName()`
22e5c89e4eSSatish Balay @*/
23d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[])
24d71ae5a4SJacob Faibussowitsch {
25e5c89e4eSSatish Balay   PetscFunctionBegin;
263cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
279566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->name));
289566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &obj->name));
293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30e5c89e4eSSatish Balay }
31e5c89e4eSSatish Balay 
32317d6ea6SBarry Smith /*@C
33811af0c4SBarry Smith   PetscObjectPrintClassNamePrefixType - used in the `XXXView()` methods to display information about the class, name, prefix and type of an object
34317d6ea6SBarry Smith 
35317d6ea6SBarry Smith   Input Parameters:
36317d6ea6SBarry Smith + obj    - the PETSc object
3721532e8aSBarry Smith - viewer - `PETSCVIEWERASCII` viewer where the information is printed, function does nothing if the viewer is not `PETSCVIEWERASCII` type
38317d6ea6SBarry Smith 
39317d6ea6SBarry Smith   Level: developer
40317d6ea6SBarry Smith 
4195452b02SPatrick Sanan   Notes:
42811af0c4SBarry Smith   If the viewer format is `PETSC_VIEWER_ASCII_MATLAB` then the information is printed after a % symbol
4398c3331eSBarry Smith   so that MATLAB will treat it as a comment.
4498c3331eSBarry Smith 
45811af0c4SBarry Smith   If the viewer format is `PETSC_VIEWER_ASCII_VTK*`, `PETSC_VIEWER_ASCII_LATEX`, or
46811af0c4SBarry Smith   `PETSC_VIEWER_ASCII_MATRIXMARKET` then don't print header information
479bc64357SJonathan Guyer   as these formats can't process it.
489bc64357SJonathan Guyer 
49*aec76313SJacob Faibussowitsch   Developer Notes:
50811af0c4SBarry Smith   The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used to actually print the object.
5123f88b65SBarry Smith 
52db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectName()`
53317d6ea6SBarry Smith @*/
54d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj, PetscViewer viewer)
55d71ae5a4SJacob Faibussowitsch {
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));
623ba16761SJacob Faibussowitsch   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(PETSC_SUCCESS);
633ba16761SJacob Faibussowitsch   if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
6498c3331eSBarry Smith 
659566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
669371c9d4SSatish 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)
673ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
681678b73fSBarry Smith 
699566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
708cc725e6SPierre Jolivet   PetscCallMPI(MPI_Comm_size(PetscObjectComm(obj), &size));
718cc725e6SPierre 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" : ""));
729566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
73317d6ea6SBarry Smith   if (obj->type_name) {
749566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  type: %s\n", obj->type_name));
75317d6ea6SBarry Smith   } else {
769566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  type not yet set\n"));
77317d6ea6SBarry Smith   }
783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
79317d6ea6SBarry Smith }
80317d6ea6SBarry Smith 
81e5c89e4eSSatish Balay /*@C
82e5c89e4eSSatish Balay   PetscObjectName - Gives an object a name if it does not have one
83e5c89e4eSSatish Balay 
84199a6bf1SJed Brown   Collective
85e5c89e4eSSatish Balay 
862fe279fdSBarry Smith   Input Parameter:
87e5c89e4eSSatish Balay . obj - the Petsc variable
88811af0c4SBarry Smith          Thus must be cast with a (`PetscObject`), for example,
89811af0c4SBarry Smith          `PetscObjectName`((`PetscObject`)mat,name);
90e5c89e4eSSatish Balay 
91317d6ea6SBarry Smith   Level: developer
92e5c89e4eSSatish Balay 
9395452b02SPatrick Sanan   Notes:
9495452b02SPatrick 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.
95811af0c4SBarry Smith 
96811af0c4SBarry Smith   Use `PetscObjectSetName()` to set the name of an object to what you want. The SAWs viewer requires that no two published objects
979ddcd10cSBarry Smith   share the same name.
989ddcd10cSBarry Smith 
99*aec76313SJacob Faibussowitsch   Developer Notes:
100811af0c4SBarry Smith   This needs to generate the exact same string on all ranks that share the object. The current algorithm may not always work.
1019ddcd10cSBarry Smith 
102db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
103e5c89e4eSSatish Balay @*/
104d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectName(PetscObject obj)
105d71ae5a4SJacob Faibussowitsch {
106480cf27aSJed Brown   PetscCommCounter *counter;
107480cf27aSJed Brown   PetscMPIInt       flg;
108e5c89e4eSSatish Balay   char              name[64];
109e5c89e4eSSatish Balay 
110e5c89e4eSSatish Balay   PetscFunctionBegin;
1113cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
112e5c89e4eSSatish Balay   if (!obj->name) {
1139371c9d4SSatish Balay     union
1149371c9d4SSatish Balay     {
1159371c9d4SSatish Balay       MPI_Comm comm;
1169371c9d4SSatish Balay       void    *ptr;
1179371c9d4SSatish Balay       char     raw[sizeof(MPI_Comm)];
1189371c9d4SSatish Balay     } ucomm;
1199566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(obj->comm, Petsc_Counter_keyval, (void *)&counter, &flg));
12028b400f6SJacob Faibussowitsch     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Bad MPI communicator supplied; must be a PETSc communicator");
1219fe7d45dSJed Brown     ucomm.ptr  = NULL;
1229fe7d45dSJed Brown     ucomm.comm = obj->comm;
1239566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(ucomm.raw, sizeof(MPI_Comm), MPI_BYTE, 0, obj->comm));
1249fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1259fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1269fe7d45dSJed Brown      * abuse acceptable. */
1279566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "%s_%p_%" PetscInt_FMT, obj->class_name, ucomm.ptr, counter->namecount++));
1289566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &obj->name));
129e5c89e4eSSatish Balay   }
1303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
131e5c89e4eSSatish Balay }
132e5c89e4eSSatish Balay 
133d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[])
134d71ae5a4SJacob Faibussowitsch {
135e5c89e4eSSatish Balay   PetscFunctionBegin;
1363cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1379566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->type_name));
1389566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type_name, &obj->type_name));
139f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
1409566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE] * sizeof(PetscFortranCallback)));
1413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
142e5c89e4eSSatish Balay }
143