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 #include "exodusII.h"     // for ex_err, ex_name_of_object, etc
37 #include "exodusII_int.h" // for ex_check_valid_file_id, etc
38 #include "vtk_netcdf.h"       // for NC_NOERR, nc_get_var_int, etc
39 #include <inttypes.h>     // for PRId64
40 #include <stdio.h>        // for snprintf, NULL
41 
42 /*!
43  * reads the set entry list and set extra list for a single set
44  * \param   exoid                   exodus file id
45  * \param   set_type                set type
46  * \param   set_id                  set id
47  * \param  *set_entry_list          array of entries in set. Set to NULL to not
48  * read.
49  * \param  *set_extra_list          array of extras in set. Set to NULL to not
50  * read.
51  */
52 
ex_get_set(int exoid,ex_entity_type set_type,ex_entity_id set_id,void_int * set_entry_list,void_int * set_extra_list)53 int ex_get_set(int exoid, ex_entity_type set_type, ex_entity_id set_id, void_int *set_entry_list,
54                void_int *set_extra_list) /* NULL if don't want to retrieve data */
55 {
56 
57   int   dimid, entry_list_id, extra_list_id, status;
58   int   set_id_ndx;
59   char  errmsg[MAX_ERR_LENGTH];
60   char *entryptr = NULL;
61   char *extraptr = NULL;
62 
63   EX_FUNC_ENTER();
64   ex_check_valid_file_id(exoid, __func__);
65 
66   /* first check if any sets are specified */
67   if ((status = nc_inq_dimid(exoid, ex_dim_num_objects(set_type), &dimid)) != NC_NOERR) {
68     snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no %ss stored in file id %d",
69              ex_name_of_object(set_type), exoid);
70     ex_err(__func__, errmsg, status);
71     EX_FUNC_LEAVE(EX_WARN);
72   }
73 
74   /* Lookup index of set id in VAR_*S_IDS array */
75   set_id_ndx = ex_id_lkup(exoid, set_type, set_id);
76   if (set_id_ndx <= 0) {
77     ex_get_err(NULL, NULL, &status);
78 
79     if (status != 0) {
80       if (status == EX_NULLENTITY) {
81         snprintf(errmsg, MAX_ERR_LENGTH, "Warning: %s %" PRId64 " is NULL in file id %d",
82                  ex_name_of_object(set_type), set_id, exoid);
83         ex_err(__func__, errmsg, EX_NULLENTITY);
84         EX_FUNC_LEAVE(EX_WARN);
85       }
86 
87       snprintf(errmsg, MAX_ERR_LENGTH,
88                "ERROR: failed to locate %s id %" PRId64 " in VAR_*S_IDS array in file id %d",
89                ex_name_of_object(set_type), set_id, exoid);
90       ex_err(__func__, errmsg, status);
91       EX_FUNC_LEAVE(EX_FATAL);
92     }
93   }
94 
95   /* setup more pointers based on set_type */
96   if (set_type == EX_NODE_SET) {
97     entryptr = VAR_NODE_NS(set_id_ndx);
98     extraptr = NULL;
99   }
100   else if (set_type == EX_EDGE_SET) {
101     entryptr = VAR_EDGE_ES(set_id_ndx);
102     extraptr = VAR_ORNT_ES(set_id_ndx);
103   }
104   else if (set_type == EX_FACE_SET) {
105     entryptr = VAR_FACE_FS(set_id_ndx);
106     extraptr = VAR_ORNT_FS(set_id_ndx);
107   }
108   else if (set_type == EX_SIDE_SET) {
109     entryptr = VAR_ELEM_SS(set_id_ndx);
110     extraptr = VAR_SIDE_SS(set_id_ndx);
111   }
112   if (set_type == EX_ELEM_SET) {
113     entryptr = VAR_ELEM_ELS(set_id_ndx);
114     extraptr = NULL;
115   }
116 
117   /* inquire id's of previously defined dimensions and variables */
118   if ((status = nc_inq_varid(exoid, entryptr, &entry_list_id)) != NC_NOERR) {
119     snprintf(errmsg, MAX_ERR_LENGTH,
120              "ERROR: failed to locate entry list for %s %" PRId64 " in file id %d",
121              ex_name_of_object(set_type), set_id, exoid);
122     ex_err(__func__, errmsg, status);
123     EX_FUNC_LEAVE(EX_FATAL);
124   }
125 
126   /* If client doet not pass in an array to store the
127      extra list, don't access it at all */
128 
129   /* only do extra list for edge, face and side sets */
130   if (set_extra_list) {
131     if ((status = nc_inq_varid(exoid, extraptr, &extra_list_id)) != NC_NOERR) {
132       snprintf(errmsg, MAX_ERR_LENGTH,
133                "ERROR: failed to locate extra list for %s %" PRId64 " in file id %d",
134                ex_name_of_object(set_type), set_id, exoid);
135       ex_err(__func__, errmsg, status);
136       EX_FUNC_LEAVE(EX_FATAL);
137     }
138   }
139 
140   /* read in the entry list and extra list arrays unless they are NULL */
141   if (set_entry_list) {
142     if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
143       status = nc_get_var_longlong(exoid, entry_list_id, set_entry_list);
144     }
145     else {
146       status = nc_get_var_int(exoid, entry_list_id, set_entry_list);
147     }
148 
149     if (status != NC_NOERR) {
150       snprintf(errmsg, MAX_ERR_LENGTH,
151                "ERROR: failed to get entry list for %s %" PRId64 " in file id %d",
152                ex_name_of_object(set_type), set_id, exoid);
153       ex_err(__func__, errmsg, status);
154       EX_FUNC_LEAVE(EX_FATAL);
155     }
156   }
157 
158   /* only do extra list for edge, face and side sets */
159   if (set_extra_list) {
160     if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
161       status = nc_get_var_longlong(exoid, extra_list_id, set_extra_list);
162     }
163     else {
164       status = nc_get_var_int(exoid, extra_list_id, set_extra_list);
165     }
166 
167     if (status != NC_NOERR) {
168       snprintf(errmsg, MAX_ERR_LENGTH,
169                "ERROR: failed to get extra list for %s %" PRId64 " in file id %d",
170                ex_name_of_object(set_type), set_id, exoid);
171       ex_err(__func__, errmsg, status);
172       EX_FUNC_LEAVE(EX_FATAL);
173     }
174   }
175   EX_FUNC_LEAVE(EX_NOERR);
176 }
177