1 /*
2  * Copyright(C) 1999-2020 National Technology & Engineering Solutions
3  * of Sandia, LLC (NTESS).  Under the terms of Contract DE-NA0003525 with
4  * NTESS, the U.S. Government retains certain rights in this software.
5  *
6  * See packages/seacas/LICENSE for details
7  */
8 /*****************************************************************************
9  *
10  * expnam - ex_put_name
11  *
12  * environment - UNIX
13  *
14  * entry conditions -
15  *   input parameters:
16  *       int     exoid          exodus file id
17  *       int     obj_type       object type
18  *       int     entity_id      id of entity name to write
19  *       char*   name           ptr to entity name
20  *
21  * exit conditions -
22  *
23  * revision history -
24  *
25  *
26  *****************************************************************************/
27 
28 #include "exodusII.h"     // for ex_err, etc
29 #include "exodusII_int.h" // for EX_FATAL, ex__id_lkup, etc
30 
31 /*!
32  * writes the name of the specified entity to the database. The entity
33  * with id `entity_id` must exist before calling ex_put_name().
34  *
35  * \param  exoid          exodus file id
36  * \param  obj_type       object type
37  * \param  entity_id      id of entity name to write
38  * \param  name           ptr to entity name
39  */
40 
ex_put_name(int exoid,ex_entity_type obj_type,ex_entity_id entity_id,const char * name)41 int ex_put_name(int exoid, ex_entity_type obj_type, ex_entity_id entity_id, const char *name)
42 {
43   int         status;
44   int         varid, ent_ndx;
45   char        errmsg[MAX_ERR_LENGTH];
46   const char *vobj;
47 
48   EX_FUNC_ENTER();
49   if (ex__check_valid_file_id(exoid, __func__) == EX_FATAL) {
50     EX_FUNC_LEAVE(EX_FATAL);
51   }
52 
53   switch (obj_type) {
54   case EX_ASSEMBLY:
55     snprintf(errmsg, MAX_ERR_LENGTH,
56              "ERROR: Assembly name is written using `ex_put_assembly()` function");
57     ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
58     EX_FUNC_LEAVE(EX_FATAL);
59     break;
60   case EX_EDGE_BLOCK: vobj = VAR_NAME_ED_BLK; break;
61   case EX_FACE_BLOCK: vobj = VAR_NAME_FA_BLK; break;
62   case EX_ELEM_BLOCK: vobj = VAR_NAME_EL_BLK; break;
63   case EX_NODE_SET: vobj = VAR_NAME_NS; break;
64   case EX_SIDE_SET: vobj = VAR_NAME_SS; break;
65   case EX_EDGE_SET: vobj = VAR_NAME_ES; break;
66   case EX_FACE_SET: vobj = VAR_NAME_FS; break;
67   case EX_ELEM_SET: vobj = VAR_NAME_ELS; break;
68   case EX_NODE_MAP: vobj = VAR_NAME_NM; break;
69   case EX_EDGE_MAP: vobj = VAR_NAME_EDM; break;
70   case EX_FACE_MAP: vobj = VAR_NAME_FAM; break;
71   case EX_ELEM_MAP: vobj = VAR_NAME_EM; break;
72   default:
73     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid type specified in file id %d", exoid);
74     ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
75     EX_FUNC_LEAVE(EX_FATAL);
76   }
77 
78   if ((status = nc_inq_varid(exoid, vobj, &varid)) != NC_NOERR) {
79     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s names in file id %d",
80              ex_name_of_object(obj_type), exoid);
81     ex_err_fn(exoid, __func__, errmsg, status);
82     EX_FUNC_LEAVE(EX_FATAL);
83   }
84 
85   ent_ndx = ex__id_lkup(exoid, obj_type, entity_id);
86 
87   if (ent_ndx == -EX_LOOKUPFAIL) { /* could not find the element block id */
88     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s id %" PRId64 " not found in file id %d",
89              ex_name_of_object(obj_type), entity_id, exoid);
90     ex_err_fn(exoid, __func__, errmsg, EX_LOOKUPFAIL);
91     EX_FUNC_LEAVE(EX_FATAL);
92   }
93 
94   /* If this is a null entity, then 'ent_ndx' will be negative.
95    * We don't care in this __func__, so make it positive and continue...
96    */
97   if (ent_ndx < 0) {
98     ent_ndx = -ent_ndx;
99   }
100 
101   /* write EXODUS entityname */
102   status = ex__put_name(exoid, varid, ent_ndx - 1, name, obj_type, "", __func__);
103 
104   EX_FUNC_LEAVE(status);
105 }
106