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_df()
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 written.
49  *      start_num          - The starting index of the df's to be written.
50  *      num_df_to_get      - The number of sides to write.
51  *      side_set_dist_fact - List of distribution factors for the side set.
52  */
53 /*****************************************************************************/
54 /*****************************************************************************/
55 /*****************************************************************************/
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 #include "exodusII.h"
60 #include "exodusII_int.h"
61 
62 /*
63  * writes the distribution factors for a single side set
64  */
65 
ex_put_n_side_set_df(int exoid,ex_entity_id side_set_id,int64_t start_num,int64_t num_df_to_get,void * side_set_dist_fact)66 int ex_put_n_side_set_df (int   exoid,
67                           ex_entity_id   side_set_id,
68                           int64_t   start_num,
69                           int64_t   num_df_to_get,
70                           void *side_set_dist_fact)
71 {
72   int status;
73   int dimid, side_set_id_ndx;
74   int dist_id;
75   size_t num_df_in_set,  start[1], count[1];
76   char errmsg[MAX_ERR_LENGTH];
77 
78   exerrval = 0; /* clear error code */
79 
80   /* first check if any side sets are specified */
81 
82   if ((status = nc_inq_dimid (exoid, DIM_NUM_SS, &dimid)) != NC_NOERR) {
83     exerrval = status;
84     sprintf(errmsg,
85             "Error: no side sets specified in file id %d",
86             exoid);
87     ex_err("ex_put_n_side_set_df",errmsg,exerrval);
88     return (EX_FATAL);
89   }
90 
91   /* Lookup index of side set id in VAR_SS_IDS array */
92 
93   if ((side_set_id_ndx = ex_id_lkup(exoid, EX_SIDE_SET, side_set_id)) < 0)
94   {
95     if (exerrval == EX_NULLENTITY) {
96       sprintf(errmsg,
97               "Warning: no data allowed for NULL side set %"PRId64" in file id %d",
98               side_set_id, exoid);
99       ex_err("ex_put_side_set_fact",errmsg,EX_MSG);
100       return (EX_WARN);
101     } else {
102       sprintf(errmsg,
103      "Error: failed to locate side set id %"PRId64" in VAR_SS_IDS array in file id %d",
104               side_set_id, exoid);
105       ex_err("ex_put_n_side_set_df",errmsg,exerrval);
106       return (EX_FATAL);
107     }
108   }
109 
110   /* inquire id's of previously defined dimension and variable */
111 
112   if ((status = nc_inq_dimid (exoid, DIM_NUM_DF_SS(side_set_id_ndx), &dimid)) != NC_NOERR) {
113     if (status == NC_EBADDIM) {
114       exerrval = EX_BADPARAM;
115       sprintf(errmsg,
116               "Warning: no dist factors defined for side set %"PRId64" in file id %d",
117               side_set_id, exoid);
118       ex_err("ex_put_n_side_set_df",errmsg,exerrval);
119       return (EX_WARN);
120 
121     } else {
122       exerrval = status;
123       sprintf(errmsg,
124   "Error: failed to locate number of dist factors in side set %"PRId64" in file id %d",
125               side_set_id, exoid);
126       ex_err("ex_put_n_side_set_df",errmsg,exerrval);
127       return (EX_FATAL);
128     }
129   }
130 
131   if ((status = nc_inq_dimlen(exoid, dimid, &num_df_in_set)) != NC_NOERR) {
132     exerrval = status;
133     sprintf(errmsg,
134      "Error: failed to get number of dist factors in side set %"PRId64" in file id %d",
135             side_set_id, exoid);
136     ex_err("ex_put_n_side_set_df",errmsg,exerrval);
137     return (EX_FATAL);
138   }
139 
140   /* Check input parameters for a valid range of numbers */
141   if (start_num < 0 || start_num > num_df_in_set) {
142     exerrval = EX_BADPARAM;
143     sprintf(errmsg, "Error: Invalid input");
144     ex_err("ex_put_n_side_set_df", errmsg, exerrval);
145     return (EX_FATAL);
146   }
147 
148   if (num_df_to_get < 0) {
149     exerrval = EX_BADPARAM;
150     sprintf(errmsg, "Error: Invalid number of df's to put!");
151     ex_err("ex_put_n_side_set_df", errmsg, exerrval);
152     return (EX_FATAL);
153   }
154 
155   /* start_num now starts at 1, not 0 */
156   if ((start_num + num_df_to_get - 1) > num_df_in_set) {
157     exerrval = EX_BADPARAM;
158     sprintf(errmsg, "Error: request larger than number of df's in set!");
159     ex_err("ex_put_n_side_set_df", errmsg, exerrval);
160     return (EX_FATAL);
161   }
162 
163   if ((status = nc_inq_varid (exoid, VAR_FACT_SS(side_set_id_ndx), &dist_id)) != NC_NOERR) {
164     exerrval = status;
165     sprintf(errmsg,
166       "Error: failed to locate dist factors list for side set %"PRId64" in file id %d",
167             side_set_id, exoid);
168     ex_err("ex_put_n_side_set_df",errmsg,exerrval);
169     return (EX_FATAL);
170   }
171 
172 
173   /* write out the distribution factors array */
174   start[0] = --start_num;
175   count[0] = num_df_to_get;
176 
177   if (ex_comp_ws(exoid) == 4) {
178     status = nc_put_vara_float(exoid, dist_id, start, count, side_set_dist_fact);
179   } else {
180     status = nc_put_vara_double(exoid, dist_id, start, count, side_set_dist_fact);
181   }
182 
183   if (status != NC_NOERR) {
184     exerrval = status;
185     sprintf(errmsg,
186             "Error: failed to store dist factors for side set %"PRId64" in file id %d",
187             side_set_id, exoid);
188     ex_err("ex_put_n_side_set_df",errmsg,exerrval);
189     return (EX_FATAL);
190   }
191   return (EX_NOERR);
192 }
193