xref: /petsc/src/sys/objects/pname.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
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 @*/
22*9371c9d4SSatish Balay PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[]) {
23e5c89e4eSSatish Balay   PetscFunctionBegin;
243cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
259566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->name));
269566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &obj->name));
27e5c89e4eSSatish Balay   PetscFunctionReturn(0);
28e5c89e4eSSatish Balay }
29e5c89e4eSSatish Balay 
30317d6ea6SBarry Smith /*@C
31988878efSVaclav Hapla       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
3598c3331eSBarry Smith -     viewer - ASCII 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:
4095452b02SPatrick Sanan     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 
4347a3fc75SMatthew G. Knepley           If the viewer format is PETSC_VIEWER_ASCII_VTK*, PETSC_VIEWER_ASCII_LATEX, or
449bc64357SJonathan Guyer           PETSC_VIEWER_ASCII_MATRIXMARKET then don't print header information
459bc64357SJonathan Guyer           as these formats can't process it.
469bc64357SJonathan Guyer 
4723f88b65SBarry Smith    Developer Note: The flag donotPetscObjectPrintClassNamePrefixType is useful to prevent double printing of the information when recursion is used
4823f88b65SBarry Smith                    to actually print the object.
4923f88b65SBarry Smith 
50db781477SPatrick Sanan .seealso: `PetscObjectSetName()`, `PetscObjectName()`
51317d6ea6SBarry Smith 
52317d6ea6SBarry Smith @*/
53*9371c9d4SSatish 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));
64*9371c9d4SSatish 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)
65*9371c9d4SSatish 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
86e5c89e4eSSatish Balay          Thus must be cast with a (PetscObject), for example,
87317d6ea6SBarry 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.
93e04113cfSBarry Smith           Use PetscObjectSetName() to set the name of an object to what you want. The SAWs viewer requires that no two published objects
949ddcd10cSBarry Smith           share the same name.
959ddcd10cSBarry Smith 
969ddcd10cSBarry 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.
979ddcd10cSBarry Smith 
98db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
99e5c89e4eSSatish Balay @*/
100*9371c9d4SSatish Balay PetscErrorCode PetscObjectName(PetscObject obj) {
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) {
108*9371c9d4SSatish Balay     union
109*9371c9d4SSatish Balay     {
110*9371c9d4SSatish Balay       MPI_Comm comm;
111*9371c9d4SSatish Balay       void    *ptr;
112*9371c9d4SSatish Balay       char     raw[sizeof(MPI_Comm)];
113*9371c9d4SSatish 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   }
125e5c89e4eSSatish Balay   PetscFunctionReturn(0);
126e5c89e4eSSatish Balay }
127e5c89e4eSSatish Balay 
128*9371c9d4SSatish Balay PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[]) {
129e5c89e4eSSatish Balay   PetscFunctionBegin;
1303cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1319566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->type_name));
1329566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type_name, &obj->type_name));
133f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
1349566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE] * sizeof(PetscFortranCallback)));
135e5c89e4eSSatish Balay   PetscFunctionReturn(0);
136e5c89e4eSSatish Balay }
137