xref: /petsc/src/sys/objects/pname.c (revision 5d2a865bca654049dd32bfe43807bdf4c92c46f4)
1af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I    "petscsys.h"   I*/
2665c2dedSJed Brown #include <petscviewer.h>
3e5c89e4eSSatish Balay 
4ffeef943SBarry Smith /*@
58ea61f2eSBarry Smith   PetscObjectSetName - Sets a string name for a PETSc object.
6e5c89e4eSSatish Balay 
7e5c89e4eSSatish Balay   Not Collective
8e5c89e4eSSatish Balay 
9e5c89e4eSSatish Balay   Input Parameters:
10dde44402SBarry Smith + obj  - the PETSc object. It must be cast with a (`PetscObject`), for example,
11811af0c4SBarry Smith          `PetscObjectSetName`((`PetscObject`)mat,name);
12e5c89e4eSSatish Balay - name - the name to give obj
13e5c89e4eSSatish Balay 
14e5c89e4eSSatish Balay   Level: advanced
15e5c89e4eSSatish Balay 
16811af0c4SBarry Smith   Note:
178ea61f2eSBarry Smith   If this routine is not called then `obj` may end up being named by `PetscObjectName()`.
18811af0c4SBarry Smith 
19db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectName()`
20e5c89e4eSSatish Balay @*/
PetscObjectSetName(PetscObject obj,const char name[])21d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[])
22d71ae5a4SJacob Faibussowitsch {
23e5c89e4eSSatish Balay   PetscFunctionBegin;
243cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
259566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->name));
269566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &obj->name));
273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28e5c89e4eSSatish Balay }
29e5c89e4eSSatish Balay 
30ffeef943SBarry Smith /*@
31811af0c4SBarry Smith   PetscObjectPrintClassNamePrefixType - used in the `XXXView()` methods to display information about the class, name, prefix and type of an object
32317d6ea6SBarry Smith 
33317d6ea6SBarry Smith   Input Parameters:
34317d6ea6SBarry Smith + obj    - the PETSc object
3521532e8aSBarry Smith - viewer - `PETSCVIEWERASCII` viewer where the information is printed, function does nothing if the viewer is not `PETSCVIEWERASCII` type
36317d6ea6SBarry Smith 
37317d6ea6SBarry Smith   Level: developer
38317d6ea6SBarry Smith 
3995452b02SPatrick Sanan   Notes:
40811af0c4SBarry Smith   If the viewer format is `PETSC_VIEWER_ASCII_MATLAB` then the information is printed after a % symbol
4198c3331eSBarry Smith   so that MATLAB will treat it as a comment.
4298c3331eSBarry Smith 
43811af0c4SBarry Smith   If the viewer format is `PETSC_VIEWER_ASCII_VTK*`, `PETSC_VIEWER_ASCII_LATEX`, or
44811af0c4SBarry Smith   `PETSC_VIEWER_ASCII_MATRIXMARKET` then don't print header information
459bc64357SJonathan Guyer   as these formats can't process it.
469bc64357SJonathan Guyer 
47dde44402SBarry Smith   Developer Note:
488ea61f2eSBarry Smith   The flag `donotPetscObjectPrintClassNamePrefixType` is useful to prevent double printing of the information when recursion is used to actually print the object.
4923f88b65SBarry Smith 
50db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectName()`
51317d6ea6SBarry Smith @*/
PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer)52d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj, PetscViewer viewer)
53d71ae5a4SJacob Faibussowitsch {
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));
603ba16761SJacob Faibussowitsch   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(PETSC_SUCCESS);
613ba16761SJacob Faibussowitsch   if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
6298c3331eSBarry Smith 
639566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
64ce78bad3SBarry Smith   if (format == PETSC_VIEWER_ASCII_MATRIXMARKET || format == PETSC_VIEWER_ASCII_LATEX || format == PETSC_VIEWER_ASCII_GLVIS) PetscFunctionReturn(PETSC_SUCCESS);
651678b73fSBarry Smith 
669566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
678cc725e6SPierre Jolivet   PetscCallMPI(MPI_Comm_size(PetscObjectComm(obj), &size));
688cc725e6SPierre 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" : ""));
699566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
70317d6ea6SBarry Smith   if (obj->type_name) {
719566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  type: %s\n", obj->type_name));
72317d6ea6SBarry Smith   } else {
739566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "  type not yet set\n"));
74317d6ea6SBarry Smith   }
753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
76317d6ea6SBarry Smith }
77317d6ea6SBarry Smith 
78ffeef943SBarry Smith /*@
798ea61f2eSBarry Smith   PetscObjectName - Gives `obj` a name if it does not have one
80e5c89e4eSSatish Balay 
81199a6bf1SJed Brown   Collective
82e5c89e4eSSatish Balay 
832fe279fdSBarry Smith   Input Parameter:
84dde44402SBarry Smith . obj - the PETSc object. It must be cast with a (`PetscObject`), for example, `PetscObjectName`((`PetscObject`)mat,name);
85e5c89e4eSSatish Balay 
86317d6ea6SBarry Smith   Level: developer
87e5c89e4eSSatish Balay 
8895452b02SPatrick Sanan   Notes:
8995452b02SPatrick 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.
90811af0c4SBarry Smith 
91811af0c4SBarry Smith   Use `PetscObjectSetName()` to set the name of an object to what you want. The SAWs viewer requires that no two published objects
929ddcd10cSBarry Smith   share the same name.
939ddcd10cSBarry Smith 
94dde44402SBarry Smith   Developer Note:
958ea61f2eSBarry Smith   This needs to generate the exact same string on all MPI processes that share `obj`. The current algorithm may not always work.
969ddcd10cSBarry Smith 
97db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
98e5c89e4eSSatish Balay @*/
PetscObjectName(PetscObject obj)99d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectName(PetscObject obj)
100d71ae5a4SJacob Faibussowitsch {
101480cf27aSJed Brown   PetscCommCounter *counter;
102480cf27aSJed Brown   PetscMPIInt       flg;
103e5c89e4eSSatish Balay   char              name[64];
104e5c89e4eSSatish Balay 
105e5c89e4eSSatish Balay   PetscFunctionBegin;
1063cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
107e5c89e4eSSatish Balay   if (!obj->name) {
1089371c9d4SSatish Balay     union
1099371c9d4SSatish Balay     {
1109371c9d4SSatish Balay       MPI_Comm comm;
1119371c9d4SSatish Balay       void    *ptr;
1129371c9d4SSatish Balay       char     raw[sizeof(MPI_Comm)];
1139371c9d4SSatish Balay     } ucomm;
1149566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(obj->comm, Petsc_Counter_keyval, (void *)&counter, &flg));
11528b400f6SJacob Faibussowitsch     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Bad MPI communicator supplied; must be a PETSc communicator");
1169fe7d45dSJed Brown     ucomm.ptr  = NULL;
1179fe7d45dSJed Brown     ucomm.comm = obj->comm;
1189566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(ucomm.raw, sizeof(MPI_Comm), MPI_BYTE, 0, obj->comm));
1199fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1209fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1219fe7d45dSJed Brown      * abuse acceptable. */
1229566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "%s_%p_%" PetscInt_FMT, obj->class_name, ucomm.ptr, counter->namecount++));
1239566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &obj->name));
124e5c89e4eSSatish Balay   }
1253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
126e5c89e4eSSatish Balay }
127e5c89e4eSSatish Balay 
PetscObjectChangeTypeName(PetscObject obj,const char type_name[])128d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[])
129d71ae5a4SJacob Faibussowitsch {
130e5c89e4eSSatish Balay   PetscFunctionBegin;
1313cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1329566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->type_name));
1339566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type_name, &obj->type_name));
134f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
135*418fb43bSPierre Jolivet   PetscCall(PetscArrayzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]));
1363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
137e5c89e4eSSatish Balay }
138