1 /*
2  * Copyright (c) 1998 Sandia Corporation. Under the terms of Contract
3  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Governement
4  * 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 Sandia Corporation 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 /*****************************************************************************/
38 /*****************************************************************************/
39 /* Function(s) contained in this file:
40  *
41  *      ex_put_n_side_set()
42  *
43  *****************************************************************************
44  *
45  *  Variable Index:
46  *
47  *      exoid               - The NetCDF ID of an already open NemesisI file.
48  *      side_set_id        - ID of side set to read.
49  *      start_side_num     - The starting index of the sides to be read.
50  *      num_sides          - The number of sides to read in.
51  *      side_set_elem_list - List of element IDs in side set.
52  *      side_set_side_list - List of side IDs in side set.
53  */
54 /*****************************************************************************/
55 /*****************************************************************************/
56 /*****************************************************************************/
57 #include <stdio.h>
58 #include <stdlib.h>
59 
60 #include "exodusII.h"
61 #include "exodusII_int.h"
62 
63 /*
64  * writes the side set element list and side set side list for a single side set
65  */
66 
ex_put_n_side_set(int exoid,ex_entity_id side_set_id,int64_t start_side_num,int64_t num_sides,const void_int * side_set_elem_list,const void_int * side_set_side_list)67 int ex_put_n_side_set (int   exoid,
68                        ex_entity_id   side_set_id,
69                        int64_t   start_side_num,
70                        int64_t   num_sides,
71                        const void_int  *side_set_elem_list,
72                        const void_int  *side_set_side_list)
73 {
74   int status;
75   int dimid;
76   int elem_list_id, side_list_id, side_set_id_ndx;
77   size_t  num_side_in_set, start[1], count[1];
78   char errmsg[MAX_ERR_LENGTH];
79 
80   exerrval = 0; /* clear error code */
81 
82   /* first check if any side sets are specified */
83 
84   if ((status = nc_inq_dimid (exoid, DIM_NUM_SS, &dimid)) != NC_NOERR) {
85     exerrval = status;
86     sprintf(errmsg,
87             "Error: no side sets defined in file id %d",
88             exoid);
89     ex_err("ex_put_n_side_set",errmsg,exerrval);
90     return (EX_FATAL);
91   }
92 
93   /* Lookup index of side set id in VAR_SS_IDS array */
94 
95   if ((side_set_id_ndx = ex_id_lkup(exoid, EX_SIDE_SET, side_set_id)) < 0)
96   {
97     if (exerrval == EX_NULLENTITY) {
98       sprintf(errmsg,
99               "Warning: no data allowed for NULL side set %"PRId64" in file id %d",
100               side_set_id, exoid);
101       ex_err("ex_put_n_side_set",errmsg,EX_MSG);
102       return (EX_WARN);
103     } else {
104       sprintf(errmsg,
105      "Error: failed to locate side set id %"PRId64" in VAR_SS_IDS array in file id %d",
106               side_set_id, exoid);
107       ex_err("ex_put_n_side_set",errmsg,exerrval);
108       return (EX_FATAL);
109     }
110   }
111 
112   /* inquire id's of previously defined dimensions  */
113 
114   if ((status = nc_inq_dimid (exoid, DIM_NUM_SIDE_SS(side_set_id_ndx), &dimid)) != NC_NOERR) {
115     exerrval = status;
116     sprintf(errmsg,
117       "Error: failed to locate number of sides in side set %"PRId64" in file id %d",
118             side_set_id, exoid);
119     ex_err("ex_put_n_side_set",errmsg,exerrval);
120     return (EX_FATAL);
121   }
122 
123   if ((status = nc_inq_dimlen(exoid, dimid, &num_side_in_set)) != NC_NOERR) {
124     exerrval = status;
125     sprintf(errmsg,
126          "Error: failed to get number of sides in side set %"PRId64" in file id %d",
127             side_set_id, exoid);
128     ex_err("ex_put_n_side_set",errmsg,exerrval);
129     return (EX_FATAL);
130   }
131 
132   /* Check input parameters for a valid range of side numbers */
133   if (start_side_num < 0 || start_side_num > num_side_in_set) {
134     exerrval = EX_BADPARAM;
135     sprintf(errmsg, "Error: Invalid input");
136     ex_err("ex_put_n_side_set",errmsg,exerrval);
137     return (EX_FATAL);
138   }
139 
140   if (num_sides < 0) {
141     exerrval = EX_BADPARAM;
142     sprintf(errmsg, "Error: Invalid number of elements in side set!");
143     ex_err("ex_put_n_side_set",errmsg,exerrval);
144     return (EX_FATAL);
145   }
146 
147   /* start_side_num now starts at 1, not 0 */
148   if ((start_side_num + num_sides - 1) > num_side_in_set) {
149     exerrval = EX_BADPARAM;
150     sprintf(errmsg, "Error: request larger than number of elements in set!");
151     ex_err("ex_put_n_side_set",errmsg,exerrval);
152     return (EX_FATAL);
153   }
154 
155   /* inquire id's of previously defined variables  */
156   if ((status = nc_inq_varid (exoid, VAR_ELEM_SS(side_set_id_ndx), &elem_list_id)) != NC_NOERR) {
157     exerrval = status;
158     sprintf(errmsg,
159            "Error: failed to locate element list for side set %"PRId64" in file id %d",
160             side_set_id, exoid);
161     ex_err("ex_put_n_side_set",errmsg,exerrval);
162     return (EX_FATAL);
163   }
164 
165   if ((status = nc_inq_varid (exoid, VAR_SIDE_SS(side_set_id_ndx), &side_list_id)) != NC_NOERR) {
166     exerrval = status;
167     sprintf(errmsg,
168             "Error: failed to locate side list for side set %"PRId64" in file id %d",
169             side_set_id, exoid);
170     ex_err("ex_put_n_side_set",errmsg,exerrval);
171     return (EX_FATAL);
172   }
173 
174 
175   /* write out the element list and side list arrays */
176   start[0] = --start_side_num;
177   count[0] = num_sides;
178 
179   if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
180     status = nc_put_vara_longlong(exoid, elem_list_id, start, count, side_set_elem_list);
181   } else {
182     status = nc_put_vara_int(exoid, elem_list_id, start, count, side_set_elem_list);
183   }
184 
185   if (status != NC_NOERR) {
186     exerrval = status;
187     sprintf(errmsg,
188       "Error: failed to store element list for side set %"PRId64" in file id %d",
189             side_set_id, exoid);
190     ex_err("ex_put_n_side_set",errmsg,exerrval);
191     return (EX_FATAL);
192   }
193 
194   if (ex_int64_status(exoid) & EX_BULK_INT64_API) {
195     status = nc_put_vara_longlong(exoid, side_list_id, start, count, side_set_side_list);
196   } else {
197     status = nc_put_vara_int(exoid, side_list_id, start, count, side_set_side_list);
198   }
199 
200   if (status != NC_NOERR) {
201     exerrval = status;
202     sprintf(errmsg,
203       "Error: failed to store side list for side set %"PRId64" in file id %d",
204             side_set_id, exoid);
205     ex_err("ex_put_n_side_set",errmsg,exerrval);
206     return (EX_FATAL);
207   }
208   return (EX_NOERR);
209 }
210