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 * expss - ex_put_set
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid                   exodus file id
42 *       int     set_type                set type
43 *       int     set_id                  set id
44 *       int*    set_entry_list          array of entries in set
45 *       int*    set_extra_list          array of extras in set
46 
47 * exit conditions -
48 *
49 * revision history -
50 *
51 *
52 *****************************************************************************/
53 
54 #include "exodusII.h"     // for ex_err, ex_name_of_object, etc
55 #include "exodusII_int.h" // for ex_check_valid_file_id, etc
56 #include "vtk_netcdf.h"       // for NC_NOERR, nc_inq_varid, etc
57 #include <inttypes.h>     // for PRId64
58 #include <stdio.h>        // for snprintf, NULL
59 
60 /*!
61  * writes the set entry list and set extra list for a single set
62  * \param   exoid                   exodus file id
63  * \param   set_type                set type
64  * \param   set_id                  set id
65  * \param  *set_entry_list          array of entries in set. Set to NULL to not
66  * write.
67  * \param  *set_extra_list          array of extras in set. Set to NULL to not
68  * write.
69  */
70 
ex_put_set(int exoid,ex_entity_type set_type,ex_entity_id set_id,const void_int * set_entry_list,const void_int * set_extra_list)71 int ex_put_set(int exoid, ex_entity_type set_type, ex_entity_id set_id,
72                const void_int *set_entry_list, const void_int *set_extra_list)
73 {
74   int   dimid, status;
75   int   entry_list_id, extra_list_id, set_id_ndx;
76   char  errmsg[MAX_ERR_LENGTH];
77   char *entryptr = NULL;
78   char *extraptr = NULL;
79 
80   EX_FUNC_ENTER();
81 
82   ex_check_valid_file_id(exoid, __func__);
83 
84   /* first check if any sets are specified */
85   if ((status = nc_inq_dimid(exoid, ex_dim_num_objects(set_type), &dimid)) != NC_NOERR) {
86     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %ss defined in file id %d",
87              ex_name_of_object(set_type), exoid);
88     ex_err(__func__, errmsg, status);
89     EX_FUNC_LEAVE(EX_FATAL);
90   }
91 
92   /* Lookup index of set id in VAR_*S_IDS array */
93   set_id_ndx = ex_id_lkup(exoid, set_type, set_id);
94   if (set_id_ndx <= 0) {
95     ex_get_err(NULL, NULL, &status);
96 
97     if (status != 0) {
98       if (status == EX_NULLENTITY) {
99         snprintf(errmsg, MAX_ERR_LENGTH,
100                  "Warning: no data allowed for NULL %s %" PRId64 " in file id %d",
101                  ex_name_of_object(set_type), set_id, exoid);
102         ex_err(__func__, errmsg, EX_NULLENTITY);
103         EX_FUNC_LEAVE(EX_WARN);
104       }
105       snprintf(errmsg, MAX_ERR_LENGTH,
106                "ERROR: failed to locate %s id %" PRId64 " in VAR_*S_IDS array in file id %d",
107                ex_name_of_object(set_type), set_id, exoid);
108       ex_err(__func__, errmsg, status);
109       EX_FUNC_LEAVE(EX_FATAL);
110     }
111   }
112 
113   /* setup more pointers based on set_type */
114   if (set_type == EX_NODE_SET) {
115     entryptr = VAR_NODE_NS(set_id_ndx);
116     extraptr = NULL;
117   }
118   else if (set_type == EX_EDGE_SET) {
119     entryptr = VAR_EDGE_ES(set_id_ndx);
120     extraptr = VAR_ORNT_ES(set_id_ndx);
121   }
122   else if (set_type == EX_FACE_SET) {
123     entryptr = VAR_FACE_FS(set_id_ndx);
124     extraptr = VAR_ORNT_FS(set_id_ndx);
125   }
126   else if (set_type == EX_SIDE_SET) {
127     entryptr = VAR_ELEM_SS(set_id_ndx);
128     extraptr = VAR_SIDE_SS(set_id_ndx);
129   }
130   else if (set_type == EX_ELEM_SET) {
131     entryptr = VAR_ELEM_ELS(set_id_ndx);
132     extraptr = NULL;
133   }
134 
135   /* inquire id's of previously defined variables  */
136   if ((status = nc_inq_varid(exoid, entryptr, &entry_list_id)) != NC_NOERR) {
137     snprintf(errmsg, MAX_ERR_LENGTH,
138              "ERROR: failed to locate entry list for %s %" PRId64 " in file id %d",
139              ex_name_of_object(set_type), set_id, exoid);
140     ex_err(__func__, errmsg, status);
141     EX_FUNC_LEAVE(EX_FATAL);
142   }
143 
144   /* only do for edge, face and side sets */
145   if (extraptr) {
146     if ((status = nc_inq_varid(exoid, extraptr, &extra_list_id)) != NC_NOERR) {
147       snprintf(errmsg, MAX_ERR_LENGTH,
148                "ERROR: failed to locate extra list for %s %" PRId64 " in file id %d",
149                ex_name_of_object(set_type), set_id, exoid);
150       ex_err(__func__, errmsg, status);
151       EX_FUNC_LEAVE(EX_FATAL);
152     }
153   }
154 
155   /* write out the entry list and extra list arrays */
156   if (set_entry_list != NULL) {
157 
158     if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
159       status = nc_put_var_longlong(exoid, entry_list_id, set_entry_list);
160     }
161     else {
162       status = nc_put_var_int(exoid, entry_list_id, set_entry_list);
163     }
164 
165     if (status != NC_NOERR) {
166       snprintf(errmsg, MAX_ERR_LENGTH,
167                "ERROR: failed to store entry list for %s %" PRId64 " in file id %d",
168                ex_name_of_object(set_type), set_id, exoid);
169       ex_err(__func__, errmsg, status);
170       EX_FUNC_LEAVE(EX_FATAL);
171     }
172   }
173 
174   /* only do for edge, face and side sets */
175   if (extraptr && set_extra_list != NULL) {
176 
177     if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
178       status = nc_put_var_longlong(exoid, extra_list_id, set_extra_list);
179     }
180     else {
181       status = nc_put_var_int(exoid, extra_list_id, set_extra_list);
182     }
183 
184     if (status != NC_NOERR) {
185       snprintf(errmsg, MAX_ERR_LENGTH,
186                "ERROR: failed to store extra list for %s %" PRId64 " in file id %d",
187                ex_name_of_object(set_type), set_id, exoid);
188       ex_err(__func__, errmsg, status);
189       EX_FUNC_LEAVE(EX_FATAL);
190     }
191   }
192 
193   /* warn if extra data was sent in for node sets and elem sets */
194   if ((set_type == EX_NODE_SET || set_type == EX_ELEM_SET) && set_extra_list != NULL) {
195     snprintf(errmsg, MAX_ERR_LENGTH,
196              "Warning: extra list was ignored for %s %" PRId64 " in file id %d",
197              ex_name_of_object(set_type), set_id, exoid);
198     ex_err(__func__, errmsg, EX_BADPARAM);
199     EX_FUNC_LEAVE(EX_WARN);
200   }
201 
202   EX_FUNC_LEAVE(EX_NOERR);
203 }
204