1 /*
2  * Copyright(C) 1999-2020 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  * See packages/seacas/LICENSE for details
7  */
8 /*****************************************************************************
9 *
10 * expssd - ex_put_set_dist_fact
11 *
12 * entry conditions -
13 *   input parameters:
14 *       int     exoid                   exodus file id
15 *       int     set_type                set type
16 *       int     set_id                  set id
17 *       void*   set_dist_fact           array of dist factors for set
18 
19 * exit conditions -
20 *
21 * revision history -
22 *
23 *
24 *****************************************************************************/
25 
26 #include "exodusII.h"     // for ex_err, ex_name_of_object, etc
27 #include "exodusII_int.h" // for EX_FATAL, EX_WARN, etc
28 
29 /*!
30  * writes the distribution factors for a single set
31  * \param  exoid                   exodus file id
32  * \param  set_type                set type
33  * \param  set_id                  set id
34  * \param *set_dist_fact           array of dist factors for set
35  */
36 
ex_put_set_dist_fact(int exoid,ex_entity_type set_type,ex_entity_id set_id,const void * set_dist_fact)37 int ex_put_set_dist_fact(int exoid, ex_entity_type set_type, ex_entity_id set_id,
38                          const void *set_dist_fact)
39 {
40   int   status;
41   int   dimid, set_id_ndx;
42   int   dist_id;
43   char  errmsg[MAX_ERR_LENGTH];
44   char *factptr = NULL;
45 
46   EX_FUNC_ENTER();
47 
48   if (ex__check_valid_file_id(exoid, __func__) == EX_FATAL) {
49     EX_FUNC_LEAVE(EX_FATAL);
50   }
51 
52   /* first check if any sets are specified */
53   if ((status = nc_inq_dimid(exoid, ex__dim_num_objects(set_type), &dimid)) != NC_NOERR) {
54     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %ss specified in file id %d",
55              ex_name_of_object(set_type), exoid);
56     ex_err_fn(exoid, __func__, errmsg, status);
57     EX_FUNC_LEAVE(EX_FATAL);
58   }
59 
60   /* Lookup index of set id in VAR_*S_IDS array */
61   set_id_ndx = ex__id_lkup(exoid, set_type, set_id);
62   if (set_id_ndx <= 0) {
63     ex_get_err(NULL, NULL, &status);
64 
65     if (status != 0) {
66       if (status == EX_NULLENTITY) {
67         snprintf(errmsg, MAX_ERR_LENGTH,
68                  "Warning: no data allowed for NULL %s %" PRId64 " in file id %d",
69                  ex_name_of_object(set_type), set_id, exoid);
70         ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
71         EX_FUNC_LEAVE(EX_WARN);
72       }
73       snprintf(errmsg, MAX_ERR_LENGTH,
74                "ERROR: failed to locate %s id %" PRId64 " in VAR_*S_IDS array in file id %d",
75                ex_name_of_object(set_type), set_id, exoid);
76       ex_err_fn(exoid, __func__, errmsg, status);
77       EX_FUNC_LEAVE(EX_FATAL);
78     }
79   }
80 
81   /* setup more pointers based on set_type */
82   if (set_type == EX_NODE_SET) {
83     /* note we are using DIM_NUM_NODE_NS instead of DIM_NUM_DF_NS */
84     factptr = VAR_FACT_NS(set_id_ndx);
85   }
86   else if (set_type == EX_EDGE_SET) {
87     factptr = VAR_FACT_ES(set_id_ndx);
88   }
89   else if (set_type == EX_FACE_SET) {
90     factptr = VAR_FACT_FS(set_id_ndx);
91   }
92   else if (set_type == EX_SIDE_SET) {
93     factptr = VAR_FACT_SS(set_id_ndx);
94   }
95   if (set_type == EX_ELEM_SET) {
96     factptr = VAR_FACT_ELS(set_id_ndx);
97   }
98 
99   /* find id of distribution factors variable
100    */
101 
102   if ((status = nc_inq_varid(exoid, factptr, &dist_id)) != NC_NOERR) {
103     /* this test is only needed for node set because we're using
104        DIM_NUM_NOD_NS instead of  DIM_NUM_DF_NS*/
105     if (status == NC_ENOTVAR) {
106       snprintf(errmsg, MAX_ERR_LENGTH,
107                "Warning: no dist factors defined for %s %" PRId64 " in file id %d",
108                ex_name_of_object(set_type), set_id, exoid);
109       ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
110       EX_FUNC_LEAVE(EX_WARN);
111     }
112     snprintf(errmsg, MAX_ERR_LENGTH,
113              "ERROR: failed to locate dist factors list for %s %" PRId64 " in file id %d",
114              ex_name_of_object(set_type), set_id, exoid);
115     ex_err_fn(exoid, __func__, errmsg, status);
116     EX_FUNC_LEAVE(EX_FATAL);
117   }
118 
119   /* write out the distribution factors array */
120   if (ex__comp_ws(exoid) == 4) {
121     status = nc_put_var_float(exoid, dist_id, set_dist_fact);
122   }
123   else {
124     status = nc_put_var_double(exoid, dist_id, set_dist_fact);
125   }
126 
127   if (status != NC_NOERR) {
128     snprintf(errmsg, MAX_ERR_LENGTH,
129              "ERROR: failed to store dist factors for %s %" PRId64 " in file id %d",
130              ex_name_of_object(set_type), set_id, exoid);
131     ex_err_fn(exoid, __func__, errmsg, status);
132     EX_FUNC_LEAVE(EX_FATAL);
133   }
134 
135   EX_FUNC_LEAVE(EX_NOERR);
136 }
137