Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_put_one_attr.c

raw
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 * expoea - ex_put_one_attr
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int obj_type object type (edge, face, elem block)
16 * int obj_id object id (edge, face, elem block ID)
17 * int attrib_index index of attribute to write
18 * float* attrib array of attributes
19 *
20 * exit conditions -
21 *
22 *
23 *****************************************************************************/
24
25 #include "exodusII.h" // for ex_err, etc
26 #include "exodusII_int.h" // for EX_FATAL, exi_get_dimension, etc
27
28 /*!
29 * writes the specified attribute for a block
30 * \param exoid exodus file id
31 * \param obj_type object type (edge, face, elem block)
32 * \param obj_id object id (edge, face, elem block ID)
33 * \param attrib_index index of attribute to write
34 * \param attrib array of attributes
35 */
36
37 int ex_put_one_attr(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, int attrib_index,
38 const void *attrib)
39 {
40 int status;
41 int attrid, obj_id_ndx, temp;
42 size_t num_entries_this_obj, num_attr;
43 size_t start[2], count[2];
44 ptrdiff_t stride[2];
45 char errmsg[MAX_ERR_LENGTH];
46 const char *dnumobjent;
47 const char *dnumobjatt;
48 const char *vattrbname;
49
50 EX_FUNC_ENTER();
51 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
52 EX_FUNC_LEAVE(EX_FATAL);
53 }
54
55 /* Determine index of obj_id in id array */
56 if (obj_type != EX_NODAL) {
57 obj_id_ndx = exi_id_lkup(exoid, obj_type, obj_id);
58 if (obj_id_ndx <= 0) {
59 ex_get_err(NULL, NULL, &status);
60 if (status != 0) {
61 if (status == EX_NULLENTITY) {
62 snprintf(errmsg, MAX_ERR_LENGTH,
63 "Warning: no attributes allowed for NULL %s %" PRId64 " in file id %d",
64 ex_name_of_object(obj_type), obj_id, exoid);
65 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
66 EX_FUNC_LEAVE(EX_WARN); /* no attributes for this element block */
67 }
68 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %s id %" PRId64 " in id array in file id %d",
69 ex_name_of_object(obj_type), obj_id, exoid);
70 ex_err_fn(exoid, __func__, errmsg, status);
71 EX_FUNC_LEAVE(EX_FATAL);
72 }
73 }
74 }
75
76 switch (obj_type) {
77 case EX_SIDE_SET:
78 dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
79 dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
80 vattrbname = VAR_SSATTRIB(obj_id_ndx);
81 break;
82 case EX_NODE_SET:
83 dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
84 dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
85 vattrbname = VAR_NSATTRIB(obj_id_ndx);
86 break;
87 case EX_EDGE_SET:
88 dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
89 dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
90 vattrbname = VAR_ESATTRIB(obj_id_ndx);
91 break;
92 case EX_FACE_SET:
93 dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
94 dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
95 vattrbname = VAR_FSATTRIB(obj_id_ndx);
96 break;
97 case EX_ELEM_SET:
98 dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
99 dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
100 vattrbname = VAR_ELSATTRIB(obj_id_ndx);
101 break;
102 case EX_NODAL:
103 dnumobjent = DIM_NUM_NODES;
104 dnumobjatt = DIM_NUM_ATT_IN_NBLK;
105 vattrbname = VAR_NATTRIB;
106 break;
107 case EX_EDGE_BLOCK:
108 dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
109 dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
110 vattrbname = VAR_EATTRIB(obj_id_ndx);
111 break;
112 case EX_FACE_BLOCK:
113 dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
114 dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
115 vattrbname = VAR_FATTRIB(obj_id_ndx);
116 break;
117 case EX_ELEM_BLOCK:
118 dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
119 dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
120 vattrbname = VAR_ATTRIB(obj_id_ndx);
121 break;
122 default:
123 snprintf(errmsg, MAX_ERR_LENGTH,
124 "Internal ERROR: unrecognized object type in switch: %d in file id %d", obj_type,
125 exoid);
126 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
127 EX_FUNC_LEAVE(EX_FATAL); /* number of attributes not defined */
128 }
129
130 /* inquire id's of previously defined dimensions */
131 if (exi_get_dimension(exoid, dnumobjent, "entries", &num_entries_this_obj, &temp, __func__) !=
132 NC_NOERR) {
133 EX_FUNC_LEAVE(EX_FATAL);
134 }
135
136 if (exi_get_dimension(exoid, dnumobjatt, "attributes", &num_attr, &temp, __func__) != NC_NOERR) {
137 EX_FUNC_LEAVE(EX_FATAL);
138 }
139
140 if (attrib_index < 1 || attrib_index > (int)num_attr) {
141 snprintf(errmsg, MAX_ERR_LENGTH,
142 "ERROR: Invalid attribute index specified: %d. Valid "
143 "range is 1 to %d for %s %" PRId64 " in file id %d",
144 attrib_index, (int)num_attr, ex_name_of_object(obj_type), obj_id, exoid);
145 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
146 EX_FUNC_LEAVE(EX_FATAL);
147 }
148
149 if ((status = nc_inq_varid(exoid, vattrbname, &attrid)) != NC_NOERR) {
150 snprintf(errmsg, MAX_ERR_LENGTH,
151 "ERROR: failed to locate attribute variable for %s %" PRId64 " in file id %d",
152 ex_name_of_object(obj_type), obj_id, exoid);
153 ex_err_fn(exoid, __func__, errmsg, status);
154 EX_FUNC_LEAVE(EX_FATAL);
155 }
156
157 /* write out the attributes */
158
159 start[0] = 0;
160 start[1] = attrib_index - 1;
161
162 count[0] = num_entries_this_obj;
163 count[1] = 1;
164
165 stride[0] = 1;
166 stride[1] = num_attr;
167
168 if (exi_comp_ws(exoid) == 4) {
169 status = nc_put_vars_float(exoid, attrid, start, count, stride, attrib);
170 }
171 else {
172 status = nc_put_vars_double(exoid, attrid, start, count, stride, attrib);
173 }
174
175 if (status != NC_NOERR) {
176 snprintf(errmsg, MAX_ERR_LENGTH,
177 "ERROR: failed to put attribute %d for %s %" PRId64 " in file id %d", attrib_index,
178 ex_name_of_object(obj_type), obj_id, exoid);
179 ex_err_fn(exoid, __func__, errmsg, status);
180 EX_FUNC_LEAVE(EX_FATAL);
181 }
182 EX_FUNC_LEAVE(EX_NOERR);
183 }