xref: /petsc/src/sys/objects/pname.c (revision 317d6ea6c5d3d9f4d5642b94974c11b359ba35b4)
1 #define PETSC_DLL
2 
3 #include "petscsys.h"        /*I    "petscsys.h"   I*/
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "PetscObjectSetName"
7 /*@C
8    PetscObjectSetName - Sets a string name associated with a PETSc object.
9 
10    Not Collective
11 
12    Input Parameters:
13 +  obj - the Petsc variable
14          Thus must be cast with a (PetscObject), for example,
15          PetscObjectSetName((PetscObject)mat,name);
16 -  name - the name to give obj
17 
18    Level: advanced
19 
20    Concepts: object name^setting
21 
22 .seealso: PetscObjectGetName()
23 @*/
24 PetscErrorCode PETSCSYS_DLLEXPORT PetscObjectSetName(PetscObject obj,const char name[])
25 {
26   PetscErrorCode ierr;
27 
28   PetscFunctionBegin;
29   PetscValidHeader(obj,1);
30   ierr = PetscFree(obj->name);CHKERRQ(ierr);
31   ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr);
32   PetscFunctionReturn(0);
33 }
34 
35 #undef __FUNCT__
36 #define __FUNCT__ "PetscObjectPrintClassNamePrefixType"
37 /*@C
38       PetscObjectPrintTypeNamePrefix - used in the XXXView() methods to display information about the class, name, prefix and type of an object
39 
40    Input Parameters:
41 +     obj - the PETSc object
42 .     viewer - ASCII viewer where the information is printed
43 -     string - for example "Matrix Object"
44 
45    Level: developer
46 
47 .seealso: PetscObjectSetName(), PetscObjectName()
48 
49 @*/
50 PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject obj,PetscViewer viewer,const char string[])
51 {
52   PetscErrorCode ierr;
53 
54   PetscFunctionBegin;
55   ierr = PetscViewerASCIIPrintf(viewer,"%s:",string);CHKERRQ(ierr);
56   if (obj->name) {
57     ierr = PetscViewerASCIIPrintf(viewer,"%s",obj->name);CHKERRQ(ierr);
58   }
59   if (obj->prefix) {
60     ierr = PetscViewerASCIIPrintf(viewer,"(%s)",obj->prefix);CHKERRQ(ierr);
61   }
62   ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
63   if (obj->type_name) {
64     ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",obj->type_name);CHKERRQ(ierr);
65   } else {
66     ierr = PetscViewerASCIIPrintf(viewer,"  type not yet set\n");CHKERRQ(ierr);
67   }
68   PetscFunctionReturn(0);
69 }
70 
71 #undef __FUNCT__
72 #define __FUNCT__ "PetscObjectName"
73 /*@C
74    PetscObjectName - Gives an object a name if it does not have one
75 
76    Not Collective
77 
78    Input Parameters:
79 .  obj - the Petsc variable
80          Thus must be cast with a (PetscObject), for example,
81          PetscObjectName((PetscObject)mat,name);
82 
83    Level: developer
84 
85    Concepts: object name^setting default
86 
87    Notes: 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.
88           Use PetscObjectSetName() to set the name of an object to what you want.
89 
90 .seealso: PetscObjectGetName(), PetscObjectSetName()
91 @*/
92 PetscErrorCode PETSCSYS_DLLEXPORT PetscObjectName(PetscObject obj)
93 {
94   PetscErrorCode   ierr;
95   PetscCommCounter *counter;
96   PetscMPIInt      flg;
97   char             name[64];
98 
99   PetscFunctionBegin;
100   PetscValidHeader(obj,1);
101   if (!obj->name) {
102     void *commp = 0;
103     ierr = MPI_Attr_get(obj->comm,Petsc_Counter_keyval,(void*)&counter,&flg);CHKERRQ(ierr);
104     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Bad MPI communicator supplied; must be a PETSc communicator");
105     ierr = PetscMemcpy(&commp,&obj->comm,PetscMin(sizeof(commp),sizeof(obj->comm)));CHKERRQ(ierr);
106     ierr = PetscSNPrintf(name,64,"%s_%p_%D",obj->class_name,commp,counter->namecount++);CHKERRQ(ierr);
107     ierr = PetscStrallocpy(name,&obj->name);CHKERRQ(ierr);
108   }
109   PetscFunctionReturn(0);
110 }
111 
112 
113 
114 #undef __FUNCT__
115 #define __FUNCT__ "PetscObjectChangeTypeName"
116 PetscErrorCode PETSCSYS_DLLEXPORT PetscObjectChangeTypeName(PetscObject obj,const char type_name[])
117 {
118   PetscErrorCode ierr;
119 
120   PetscFunctionBegin;
121   PetscValidHeader(obj,1);
122   ierr = PetscObjectTakeAccess(obj);CHKERRQ(ierr);
123   ierr = PetscFree(obj->type_name);CHKERRQ(ierr);
124   ierr = PetscStrallocpy(type_name,&obj->type_name);CHKERRQ(ierr);
125   ierr = PetscObjectGrantAccess(obj);CHKERRQ(ierr);
126   PetscFunctionReturn(0);
127 }
128 
129