| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022, 2023 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, exi_id_lkup, etc |
| 30 |
|
| 31 |
int exi_put_assembly_name(int exoid, ex_entity_type obj_type, ex_entity_id entity_id, |
| 32 |
const char *name) |
| 33 |
{ |
| 34 |
/* Internal function to handle renaming of an existing assembly. |
| 35 |
Note that assembly must exist or an error will be returned. |
| 36 |
*/ |
| 37 |
/* See if an assembly with this id has already been defined or exists on file... */ |
| 38 |
int entlst_id = 0; |
| 39 |
char errmsg[MAX_ERR_LENGTH]; |
| 40 |
if (nc_inq_varid(exoid, VAR_ENTITY_ASSEMBLY(entity_id), &entlst_id) == NC_NOERR) { |
| 41 |
int status; |
| 42 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 43 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 44 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 45 |
EX_FUNC_LEAVE(EX_FATAL); |
| 46 |
} |
| 47 |
size_t length = strlen(name) + 1; |
| 48 |
if ((status = nc_put_att_text(exoid, entlst_id, EX_ATTRIBUTE_NAME, length, name)) != NC_NOERR) { |
| 49 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store assembly name %s in file id %d", |
| 50 |
name, exoid); |
| 51 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 52 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 53 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode in file id %d", exoid); |
| 54 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 55 |
} |
| 56 |
EX_FUNC_LEAVE(EX_FATAL); |
| 57 |
} |
| 58 |
/* Update the maximum_name_length attribute on the file. */ |
| 59 |
exi_update_max_name_length(exoid, length - 1); |
| 60 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 61 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode in file id %d", exoid); |
| 62 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 63 |
EX_FUNC_LEAVE(EX_FATAL); |
| 64 |
} |
| 65 |
EX_FUNC_LEAVE(EX_NOERR); |
| 66 |
} |
| 67 |
|
| 68 |
/* Assembly not found... */ |
| 69 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s id %" PRId64 " not found in file id %d", |
| 70 |
ex_name_of_object(obj_type), entity_id, exoid); |
| 71 |
ex_err_fn(exoid, __func__, errmsg, EX_LOOKUPFAIL); |
| 72 |
EX_FUNC_LEAVE(EX_FATAL); |
| 73 |
} |
| 74 |
|
| 75 |
/*! |
| 76 |
* writes the name of the specified entity to the database. The entity |
| 77 |
* with id `entity_id` must exist before calling ex_put_name(). |
| 78 |
* |
| 79 |
* \param exoid exodus file id |
| 80 |
* \param obj_type object type |
| 81 |
* \param entity_id id of entity name to write |
| 82 |
* \param name ptr to entity name |
| 83 |
*/ |
| 84 |
|
| 85 |
int ex_put_name(int exoid, ex_entity_type obj_type, ex_entity_id entity_id, const char *name) |
| 86 |
{ |
| 87 |
int status; |
| 88 |
int varid, ent_ndx; |
| 89 |
char errmsg[MAX_ERR_LENGTH]; |
| 90 |
const char *vobj; |
| 91 |
|
| 92 |
EX_FUNC_ENTER(); |
| 93 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 94 |
EX_FUNC_LEAVE(EX_FATAL); |
| 95 |
} |
| 96 |
|
| 97 |
switch (obj_type) { |
| 98 |
case EX_ASSEMBLY: return exi_put_assembly_name(exoid, obj_type, entity_id, name); |
| 99 |
case EX_EDGE_BLOCK: vobj = VAR_NAME_ED_BLK; break; |
| 100 |
case EX_FACE_BLOCK: vobj = VAR_NAME_FA_BLK; break; |
| 101 |
case EX_ELEM_BLOCK: vobj = VAR_NAME_EL_BLK; break; |
| 102 |
case EX_NODE_SET: vobj = VAR_NAME_NS; break; |
| 103 |
case EX_SIDE_SET: vobj = VAR_NAME_SS; break; |
| 104 |
case EX_EDGE_SET: vobj = VAR_NAME_ES; break; |
| 105 |
case EX_FACE_SET: vobj = VAR_NAME_FS; break; |
| 106 |
case EX_ELEM_SET: vobj = VAR_NAME_ELS; break; |
| 107 |
case EX_NODE_MAP: vobj = VAR_NAME_NM; break; |
| 108 |
case EX_EDGE_MAP: vobj = VAR_NAME_EDM; break; |
| 109 |
case EX_FACE_MAP: vobj = VAR_NAME_FAM; break; |
| 110 |
case EX_ELEM_MAP: vobj = VAR_NAME_EM; break; |
| 111 |
default: |
| 112 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid type specified in file id %d", exoid); |
| 113 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 114 |
EX_FUNC_LEAVE(EX_FATAL); |
| 115 |
} |
| 116 |
|
| 117 |
if ((status = nc_inq_varid(exoid, vobj, &varid)) != NC_NOERR) { |
| 118 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s names in file id %d", |
| 119 |
ex_name_of_object(obj_type), exoid); |
| 120 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 121 |
EX_FUNC_LEAVE(EX_FATAL); |
| 122 |
} |
| 123 |
|
| 124 |
ent_ndx = exi_id_lkup(exoid, obj_type, entity_id); |
| 125 |
if (ent_ndx == -EX_LOOKUPFAIL) { /* could not find the entity with `entity_id` */ |
| 126 |
if (obj_type == EX_NODE_MAP || obj_type == EX_ELEM_MAP || obj_type == EX_FACE_MAP || |
| 127 |
obj_type == EX_EDGE_MAP) { |
| 128 |
ent_ndx = entity_id; |
| 129 |
} |
| 130 |
else { |
| 131 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s id %" PRId64 " not found in file id %d", |
| 132 |
ex_name_of_object(obj_type), entity_id, exoid); |
| 133 |
ex_err_fn(exoid, __func__, errmsg, EX_LOOKUPFAIL); |
| 134 |
EX_FUNC_LEAVE(EX_FATAL); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/* If this is a null entity, then 'ent_ndx' will be negative. |
| 139 |
* We don't care in this function, so make it positive and continue... |
| 140 |
*/ |
| 141 |
if (ent_ndx < 0) { |
| 142 |
ent_ndx = -ent_ndx; |
| 143 |
} |
| 144 |
|
| 145 |
/* write EXODUS entityname */ |
| 146 |
status = exi_put_name(exoid, varid, ent_ndx - 1, name, obj_type, "", __func__); |
| 147 |
|
| 148 |
EX_FUNC_LEAVE(status); |
| 149 |
} |