xref: /petsc/src/sys/objects/pname.c (revision 8ea61f2e51d4708355e25e4061fd2042df7ba4b4)
1af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I    "petscsys.h"   I*/
2665c2dedSJed Brown #include <petscviewer.h>
3e5c89e4eSSatish Balay 
4e5c89e4eSSatish Balay /*@C
5*8ea61f2eSBarry Smith   PetscObjectSetName - Sets a string name for a PETSc object.
6e5c89e4eSSatish Balay 
7e5c89e4eSSatish Balay   Not Collective
8e5c89e4eSSatish Balay 
9e5c89e4eSSatish Balay   Input Parameters:
10*8ea61f2eSBarry Smith + obj  - the PETSc object
11811af0c4SBarry Smith          Thus must be cast with a (`PetscObject`), for example,
12811af0c4SBarry Smith          `PetscObjectSetName`((`PetscObject`)mat,name);
13e5c89e4eSSatish Balay - name - the name to give obj
14e5c89e4eSSatish Balay 
15e5c89e4eSSatish Balay   Level: advanced
16e5c89e4eSSatish Balay 
17811af0c4SBarry Smith   Note:
18*8ea61f2eSBarry Smith   If this routine is not called then `obj` may end up being named by `PetscObjectName()`.
19811af0c4SBarry Smith 
20db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectName()`
21e5c89e4eSSatish Balay @*/
22d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectSetName(PetscObject obj, const char name[])
23d71ae5a4SJacob Faibussowitsch {
24e5c89e4eSSatish Balay   PetscFunctionBegin;
253cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
269566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->name));
279566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &obj->name));
283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29e5c89e4eSSatish Balay }
30e5c89e4eSSatish Balay 
31317d6ea6SBarry Smith /*@C
32811af0c4SBarry 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
3621532e8aSBarry Smith - viewer - `PETSCVIEWERASCII` 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:
41811af0c4SBarry 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 
44811af0c4SBarry Smith   If the viewer format is `PETSC_VIEWER_ASCII_VTK*`, `PETSC_VIEWER_ASCII_LATEX`, or
45811af0c4SBarry Smith   `PETSC_VIEWER_ASCII_MATRIXMARKET` then don't print header information
469bc64357SJonathan Guyer   as these formats can't process it.
479bc64357SJonathan Guyer 
48aec76313SJacob Faibussowitsch   Developer Notes:
49*8ea61f2eSBarry 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 @*/
53d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj, PetscViewer viewer)
54d71ae5a4SJacob Faibussowitsch {
55369bc716SBarry Smith   PetscMPIInt       size;
5698c3331eSBarry Smith   PetscViewerFormat format;
5798c3331eSBarry Smith   PetscBool         flg;
58317d6ea6SBarry Smith 
59317d6ea6SBarry Smith   PetscFunctionBegin;
609566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &flg));
613ba16761SJacob Faibussowitsch   if (obj->donotPetscObjectPrintClassNamePrefixType) PetscFunctionReturn(PETSC_SUCCESS);
623ba16761SJacob Faibussowitsch   if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
6398c3331eSBarry Smith 
649566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
659371c9d4SSatish 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)
663ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
671678b73fSBarry Smith 
689566063dSJacob Faibussowitsch   if (format == PETSC_VIEWER_ASCII_MATLAB) PetscCall(PetscViewerASCIIPrintf(viewer, "%%"));
698cc725e6SPierre Jolivet   PetscCallMPI(MPI_Comm_size(PetscObjectComm(obj), &size));
708cc725e6SPierre 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   }
773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
78317d6ea6SBarry Smith }
79317d6ea6SBarry Smith 
80e5c89e4eSSatish Balay /*@C
81*8ea61f2eSBarry Smith   PetscObjectName - Gives `obj` a name if it does not have one
82e5c89e4eSSatish Balay 
83199a6bf1SJed Brown   Collective
84e5c89e4eSSatish Balay 
852fe279fdSBarry Smith   Input Parameter:
86*8ea61f2eSBarry Smith . obj - the PETSc object
87811af0c4SBarry Smith          Thus must be cast with a (`PetscObject`), for example,
88811af0c4SBarry 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.
94811af0c4SBarry Smith 
95811af0c4SBarry Smith   Use `PetscObjectSetName()` to set the name of an object to what you want. The SAWs viewer requires that no two published objects
969ddcd10cSBarry Smith   share the same name.
979ddcd10cSBarry Smith 
98aec76313SJacob Faibussowitsch   Developer Notes:
99*8ea61f2eSBarry Smith   This needs to generate the exact same string on all MPI processes that share `obj`. The current algorithm may not always work.
1009ddcd10cSBarry Smith 
101db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
102e5c89e4eSSatish Balay @*/
103d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectName(PetscObject obj)
104d71ae5a4SJacob Faibussowitsch {
105480cf27aSJed Brown   PetscCommCounter *counter;
106480cf27aSJed Brown   PetscMPIInt       flg;
107e5c89e4eSSatish Balay   char              name[64];
108e5c89e4eSSatish Balay 
109e5c89e4eSSatish Balay   PetscFunctionBegin;
1103cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
111e5c89e4eSSatish Balay   if (!obj->name) {
1129371c9d4SSatish Balay     union
1139371c9d4SSatish Balay     {
1149371c9d4SSatish Balay       MPI_Comm comm;
1159371c9d4SSatish Balay       void    *ptr;
1169371c9d4SSatish Balay       char     raw[sizeof(MPI_Comm)];
1179371c9d4SSatish Balay     } ucomm;
1189566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(obj->comm, Petsc_Counter_keyval, (void *)&counter, &flg));
11928b400f6SJacob Faibussowitsch     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Bad MPI communicator supplied; must be a PETSc communicator");
1209fe7d45dSJed Brown     ucomm.ptr  = NULL;
1219fe7d45dSJed Brown     ucomm.comm = obj->comm;
1229566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(ucomm.raw, sizeof(MPI_Comm), MPI_BYTE, 0, obj->comm));
1239fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1249fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1259fe7d45dSJed Brown      * abuse acceptable. */
1269566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "%s_%p_%" PetscInt_FMT, obj->class_name, ucomm.ptr, counter->namecount++));
1279566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &obj->name));
128e5c89e4eSSatish Balay   }
1293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
130e5c89e4eSSatish Balay }
131e5c89e4eSSatish Balay 
132d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[])
133d71ae5a4SJacob Faibussowitsch {
134e5c89e4eSSatish Balay   PetscFunctionBegin;
1353cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
1369566063dSJacob Faibussowitsch   PetscCall(PetscFree(obj->type_name));
1379566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(type_name, &obj->type_name));
138f6291634SJed Brown   /* Clear all the old subtype callbacks so they can't accidentally be called (shouldn't happen anyway) */
1399566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE] * sizeof(PetscFortranCallback)));
1403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
141e5c89e4eSSatish Balay }
142