Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 2024 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_partial_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 start_num the starting index of the attributes to be written
34 * \param num_ent the number of entities to write attributes for.
35 * \param attrib_index index of attribute to write
36 * \param attrib array of attributes
37 */
38
39 int ex_put_partial_one_attr(int exoid, ex_entity_type obj_type, ex_entity_id obj_id,
40 int64_t start_num, int64_t num_ent, int attrib_index,
41 const void *attrib)
42 {
43 int status;
44 int attrid, obj_id_ndx, temp;
45 size_t num_entries_this_obj, num_attr;
46 size_t start[2], count[2];
47 ptrdiff_t stride[2];
48 char errmsg[MAX_ERR_LENGTH];
49 const char *dnumobjent;
50 const char *dnumobjatt;
51 const char *vattrbname;
52
53 EX_FUNC_ENTER();
54 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
55 EX_FUNC_LEAVE(EX_FATAL);
56 }
57
58 /* Determine index of obj_id in id array */
59 if (obj_type != EX_NODAL) {
60 obj_id_ndx = exi_id_lkup(exoid, obj_type, obj_id);
61 if (obj_id_ndx <= 0) {
62 ex_get_err(NULL, NULL, &status);
63
64 if (status != 0) {
65 if (status == EX_NULLENTITY) {
66 snprintf(errmsg, MAX_ERR_LENGTH,
67 "Warning: no attributes allowed for NULL %s %" PRId64 " in file id %d",
68 ex_name_of_object(obj_type), obj_id, exoid);
69 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
70 EX_FUNC_LEAVE(EX_WARN); /* no attributes for this element block */
71 }
72 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %s id %" PRId64 " in id array in file id %d",
73 ex_name_of_object(obj_type), obj_id, exoid);
74 ex_err_fn(exoid, __func__, errmsg, status);
75 EX_FUNC_LEAVE(EX_FATAL);
76 }
77 }
78 }
79
80 switch (obj_type) {
81 case EX_SIDE_SET:
82 dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
83 dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
84 vattrbname = VAR_SSATTRIB(obj_id_ndx);
85 break;
86 case EX_NODE_SET:
87 dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
88 dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
89 vattrbname = VAR_NSATTRIB(obj_id_ndx);
90 break;
91 case EX_EDGE_SET:
92 dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
93 dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
94 vattrbname = VAR_ESATTRIB(obj_id_ndx);
95 break;
96 case EX_FACE_SET:
97 dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
98 dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
99 vattrbname = VAR_FSATTRIB(obj_id_ndx);
100 break;
101 case EX_ELEM_SET:
102 dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
103 dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
104 vattrbname = VAR_ELSATTRIB(obj_id_ndx);
105 break;
106 case EX_NODAL:
107 dnumobjent = DIM_NUM_NODES;
108 dnumobjatt = DIM_NUM_ATT_IN_NBLK;
109 vattrbname = VAR_NATTRIB;
110 break;
111 case EX_EDGE_BLOCK:
112 dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
113 dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
114 vattrbname = VAR_EATTRIB(obj_id_ndx);
115 break;
116 case EX_FACE_BLOCK:
117 dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
118 dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
119 vattrbname = VAR_FATTRIB(obj_id_ndx);
120 break;
121 case EX_ELEM_BLOCK:
122 dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
123 dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
124 vattrbname = VAR_ATTRIB(obj_id_ndx);
125 break;
126 default:
127 snprintf(errmsg, MAX_ERR_LENGTH,
128 "Internal ERROR: unrecognized object type in switch: %d in file id %d", obj_type,
129 exoid);
130 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
131 EX_FUNC_LEAVE(EX_FATAL);
132 }
133
134 /* inquire id's of previously defined dimensions */
135 if (exi_get_dimension(exoid, dnumobjent, "entries", &num_entries_this_obj, &temp, __func__) !=
136 NC_NOERR) {
137 EX_FUNC_LEAVE(EX_FATAL);
138 }
139
140 if (start_num + num_ent - 1 > (int64_t)num_entries_this_obj) {
141 snprintf(errmsg, MAX_ERR_LENGTH,
142 "ERROR: start index (%" PRId64 ") + count (%" PRId64
143 ") is larger than total number of entities (%zu) in file id %d",
144 start_num, num_ent, num_entries_this_obj, exoid);
145 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
146 EX_FUNC_LEAVE(EX_FATAL);
147 }
148
149 if (exi_get_dimension(exoid, dnumobjatt, "attributes", &num_attr, &temp, __func__) != NC_NOERR) {
150 EX_FUNC_LEAVE(EX_FATAL);
151 }
152
153 if (attrib_index < 1 || attrib_index > (int)num_attr) {
154 snprintf(errmsg, MAX_ERR_LENGTH,
155 "ERROR: Invalid attribute index specified: %d. Valid "
156 "range is 1 to %zu for %s %" PRId64 " in file id %d",
157 attrib_index, num_attr, ex_name_of_object(obj_type), obj_id, exoid);
158 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
159 EX_FUNC_LEAVE(EX_FATAL);
160 }
161
162 if ((status = nc_inq_varid(exoid, vattrbname, &attrid)) != NC_NOERR) {
163 snprintf(errmsg, MAX_ERR_LENGTH,
164 "ERROR: failed to locate attribute variable for %s %" PRId64 " in file id %d",
165 ex_name_of_object(obj_type), obj_id, exoid);
166 ex_err_fn(exoid, __func__, errmsg, status);
167 EX_FUNC_LEAVE(EX_FATAL);
168 }
169
170 /* write out the attributes */
171
172 start[0] = start_num - 1;
173 start[1] = attrib_index - 1;
174
175 count[0] = num_ent;
176 count[1] = 1;
177
178 stride[0] = 1;
179 stride[1] = num_attr;
180
181 if (count[0] == 0) {
182 start[0] = 0;
183 }
184
185 if (exi_comp_ws(exoid) == 4) {
186 status = nc_put_vars_float(exoid, attrid, start, count, stride, attrib);
187 }
188 else {
189 status = nc_put_vars_double(exoid, attrid, start, count, stride, attrib);
190 }
191
192 if (status != NC_NOERR) {
193 snprintf(errmsg, MAX_ERR_LENGTH,
194 "ERROR: failed to put attribute %d for %s %" PRId64 " in file id %d", attrib_index,
195 ex_name_of_object(obj_type), obj_id, exoid);
196 ex_err_fn(exoid, __func__, errmsg, status);
197 EX_FUNC_LEAVE(EX_FATAL);
198 }
199 EX_FUNC_LEAVE(EX_NOERR);
200 }