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