1 /*
2  * Copyright (c) 2005-2017 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  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  *     * Neither the name of NTESS nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 /*****************************************************************************
36  *
37  * exgpa - ex_get_prop_array: read object property array
38  *
39  * entry conditions -
40  *   input parameters:
41  *       int     exoid                   exodus file id
42  *       int     obj_type                type of object (element block, node
43  *                                               set or side set)
44  *       char*   prop_name               name of the property for which the
45  *                                               values will be read
46  *
47  * exit conditions -
48  *       int*    values                  returned array of property values
49  *
50  * revision history -
51  *
52  *
53  *****************************************************************************/
54 
55 #include "exodusII.h"     // for ex_err, etc
56 #include "exodusII_int.h" // for EX_FATAL, ATT_PROP_NAME, etc
57 #include "vtk_netcdf.h"       // for NC_NOERR, nc_get_att_text, etc
58 #include <stdio.h>
59 #include <string.h> // for memset, strcmp
60 
61 /*!
62 
63 The function ex_get_prop_array() reads an array of integer property
64 values for all element blocks, node sets, or side sets. The order of
65 the values in the array correspond to the order in which the element
66 blocks, node sets, or side sets were introduced into the file. Before
67 this function is invoked, memory must be allocated for the returned
68 array of(num_elem_blk, num_node_sets, or {num_side_sets})
69 integer values.
70 
71 
72 This function can be used in place of
73  - ex_get_elem_blk_ids(),
74  - ex_get_node_set_ids(), and
75  - ex_get_side_set_ids()
76 to get element block, node set, and side set IDs, respectively, by
77 requesting the property name \b ID. One should also note that this
78 same function can be accomplished with multiple calls to
79 ex_get_prop().
80 
81 \return In case of an error, ex_get_prop_array() returns a negative
82 number; a warning will return a positive number.  Possible causes of
83 errors include:
84   -  data file not properly opened with call to ex_create() or ex_open()
85   -  invalid object type specified.
86   -  a warning value is returned if a property with the specified name is not
87 found.
88 
89 \param[in]  exoid      exodus file ID returned from a previous call to
90 ex_create() or ex_open().
91 \param[in]  obj_type   Type of object; use one of the options in the table
92 below.
93 \param[in]  prop_name  The name of the property (maximum length of \p
94 MAX_STR_LENGTH )
95                        for which the values are desired.
96 \param[out]  values    Returned array of property values.
97 
98 | ex_entity_type | description               |
99 | -------------- | ------------------------- |
100 |  EX_NODE_SET   |  Node Set entity type     |
101 |  EX_EDGE_BLOCK |  Edge Block entity type   |
102 |  EX_EDGE_SET   |  Edge Set entity type     |
103 |  EX_FACE_BLOCK |  Face Block entity type   |
104 |  EX_FACE_SET   |  Face Set entity type     |
105 |  EX_ELEM_BLOCK |  Element Block entity type|
106 |  EX_ELEM_SET   |  Element Set entity type  |
107 |  EX_SIDE_SET   |  Side Set entity type     |
108 |  EX_ELEM_MAP   |  Element Map entity type  |
109 |  EX_NODE_MAP   |  Node Map entity type     |
110 |  EX_EDGE_MAP   |  Edge Map entity type     |
111 |  EX_FACE_MAP   |  Face Map entity type     |
112 
113 For an example of code to read an array of object properties, refer to
114 the description for ex_get_prop_names().
115 */
116 
ex_get_prop_array(int exoid,ex_entity_type obj_type,const char * prop_name,void_int * values)117 int ex_get_prop_array(int exoid, ex_entity_type obj_type, const char *prop_name, void_int *values)
118 {
119   int   num_props, i, propid, status;
120   int   found = EX_FALSE;
121   char *name;
122   char  tmpstr[MAX_STR_LENGTH + 1];
123 
124   char errmsg[MAX_ERR_LENGTH];
125 
126   EX_FUNC_ENTER();
127   ex_check_valid_file_id(exoid, __func__);
128 
129   /* open appropriate variable, depending on obj_type and prop_name */
130 
131   num_props = ex_get_num_props(exoid, obj_type);
132 
133   for (i = 1; i <= num_props; i++) {
134     switch (obj_type) {
135     case EX_ELEM_BLOCK: name = VAR_EB_PROP(i); break;
136     case EX_EDGE_BLOCK: name = VAR_ED_PROP(i); break;
137     case EX_FACE_BLOCK: name = VAR_FA_PROP(i); break;
138     case EX_NODE_SET: name = VAR_NS_PROP(i); break;
139     case EX_EDGE_SET: name = VAR_ES_PROP(i); break;
140     case EX_FACE_SET: name = VAR_FS_PROP(i); break;
141     case EX_ELEM_SET: name = VAR_ELS_PROP(i); break;
142     case EX_SIDE_SET: name = VAR_SS_PROP(i); break;
143     case EX_ELEM_MAP: name = VAR_EM_PROP(i); break;
144     case EX_FACE_MAP: name = VAR_FAM_PROP(i); break;
145     case EX_EDGE_MAP: name = VAR_EDM_PROP(i); break;
146     case EX_NODE_MAP: name = VAR_NM_PROP(i); break;
147     default:
148       snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type,
149                exoid);
150       ex_err(__func__, errmsg, EX_BADPARAM);
151       EX_FUNC_LEAVE(EX_FATAL);
152     }
153 
154     if ((status = nc_inq_varid(exoid, name, &propid)) != NC_NOERR) {
155       snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate property array %s in file id %d",
156                name, exoid);
157       ex_err(__func__, errmsg, status);
158       EX_FUNC_LEAVE(EX_FATAL);
159     }
160 
161     /*   compare stored attribute name with passed property name   */
162     memset(tmpstr, 0, MAX_STR_LENGTH + 1);
163     if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, tmpstr)) != NC_NOERR) {
164       snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get property name in file id %d", exoid);
165       ex_err(__func__, errmsg, status);
166       EX_FUNC_LEAVE(EX_FATAL);
167     }
168 
169     if (strcmp(tmpstr, prop_name) == 0) {
170       found = EX_TRUE;
171       break;
172     }
173   }
174 
175   /* if property is not found, return warning */
176   if (!found) {
177     snprintf(errmsg, MAX_ERR_LENGTH,
178              "Warning: object type %d, property %s not defined in file id %d", obj_type, prop_name,
179              exoid);
180     ex_err(__func__, errmsg, EX_BADPARAM);
181     EX_FUNC_LEAVE(EX_WARN);
182   }
183 
184   /* read num_obj values from property variable */
185   if (ex_int64_status(exoid) & EX_IDS_INT64_API) {
186     status = nc_get_var_longlong(exoid, propid, values);
187   }
188   else {
189     status = nc_get_var_int(exoid, propid, values);
190   }
191 
192   if (status != NC_NOERR) {
193     snprintf(errmsg, MAX_ERR_LENGTH,
194              "ERROR: failed to read values in %s property array in file id %d",
195              ex_name_of_object(obj_type), exoid);
196     ex_err(__func__, errmsg, status);
197     EX_FUNC_LEAVE(EX_FATAL);
198   }
199 
200   EX_FUNC_LEAVE(EX_NOERR);
201 }
202