xref: /petsc/src/sys/objects/pname.c (revision dde44402f31e700b7bab43b25c911203680f93f4)
1af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I    "petscsys.h"   I*/
2665c2dedSJed Brown #include <petscviewer.h>
3e5c89e4eSSatish Balay 
4e5c89e4eSSatish Balay /*@C
58ea61f2eSBarry Smith   PetscObjectSetName - Sets a string name for a PETSc object.
6e5c89e4eSSatish Balay 
7e5c89e4eSSatish Balay   Not Collective
8e5c89e4eSSatish Balay 
9e5c89e4eSSatish Balay   Input Parameters:
10*dde44402SBarry 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 @*/
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 
30317d6ea6SBarry Smith /*@C
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 
47*dde44402SBarry 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 @*/
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));
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)
653ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
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   }
763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
77317d6ea6SBarry Smith }
78317d6ea6SBarry Smith 
79e5c89e4eSSatish Balay /*@C
808ea61f2eSBarry Smith   PetscObjectName - Gives `obj` a name if it does not have one
81e5c89e4eSSatish Balay 
82199a6bf1SJed Brown   Collective
83e5c89e4eSSatish Balay 
842fe279fdSBarry Smith   Input Parameter:
85*dde44402SBarry Smith . obj - the PETSc object. It must be cast with a (`PetscObject`), for example, `PetscObjectName`((`PetscObject`)mat,name);
86e5c89e4eSSatish Balay 
87317d6ea6SBarry Smith   Level: developer
88e5c89e4eSSatish Balay 
8995452b02SPatrick Sanan   Notes:
9095452b02SPatrick 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.
91811af0c4SBarry Smith 
92811af0c4SBarry Smith   Use `PetscObjectSetName()` to set the name of an object to what you want. The SAWs viewer requires that no two published objects
939ddcd10cSBarry Smith   share the same name.
949ddcd10cSBarry Smith 
95*dde44402SBarry Smith   Developer Note:
968ea61f2eSBarry Smith   This needs to generate the exact same string on all MPI processes that share `obj`. The current algorithm may not always work.
979ddcd10cSBarry Smith 
98db781477SPatrick Sanan .seealso: `PetscObjectGetName()`, `PetscObjectSetName()`
99e5c89e4eSSatish Balay @*/
100d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectName(PetscObject obj)
101d71ae5a4SJacob Faibussowitsch {
102480cf27aSJed Brown   PetscCommCounter *counter;
103480cf27aSJed Brown   PetscMPIInt       flg;
104e5c89e4eSSatish Balay   char              name[64];
105e5c89e4eSSatish Balay 
106e5c89e4eSSatish Balay   PetscFunctionBegin;
1073cfa8680SLisandro Dalcin   PetscValidHeader(obj, 1);
108e5c89e4eSSatish Balay   if (!obj->name) {
1099371c9d4SSatish Balay     union
1109371c9d4SSatish Balay     {
1119371c9d4SSatish Balay       MPI_Comm comm;
1129371c9d4SSatish Balay       void    *ptr;
1139371c9d4SSatish Balay       char     raw[sizeof(MPI_Comm)];
1149371c9d4SSatish Balay     } ucomm;
1159566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(obj->comm, Petsc_Counter_keyval, (void *)&counter, &flg));
11628b400f6SJacob Faibussowitsch     PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Bad MPI communicator supplied; must be a PETSc communicator");
1179fe7d45dSJed Brown     ucomm.ptr  = NULL;
1189fe7d45dSJed Brown     ucomm.comm = obj->comm;
1199566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(ucomm.raw, sizeof(MPI_Comm), MPI_BYTE, 0, obj->comm));
1209fe7d45dSJed Brown     /* If the union has extra bytes, their value is implementation-dependent, but they will normally be what we set last
1219fe7d45dSJed Brown      * in 'ucomm.ptr = NULL'.  This output is always implementation-defined (and varies from run to run) so the union
1229fe7d45dSJed Brown      * abuse acceptable. */
1239566063dSJacob Faibussowitsch     PetscCall(PetscSNPrintf(name, 64, "%s_%p_%" PetscInt_FMT, obj->class_name, ucomm.ptr, counter->namecount++));
1249566063dSJacob Faibussowitsch     PetscCall(PetscStrallocpy(name, &obj->name));
125e5c89e4eSSatish Balay   }
1263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
127e5c89e4eSSatish Balay }
128e5c89e4eSSatish Balay 
129d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscObjectChangeTypeName(PetscObject obj, const char type_name[])
130d71ae5a4SJacob Faibussowitsch {
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)));
1373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
138e5c89e4eSSatish Balay }
139