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  * exgvp - ex_get_variable_param
38  *
39  * entry conditions -
40  *   input parameters:
41  *       int     exoid                   exodus file id
42  *       int     obj_type                variable type
43  *
44  * exit conditions -
45  *       int*    num_vars                number of variables in database
46  *
47  * revision history -
48  *
49  *
50  *****************************************************************************/
51 
52 #include "exodusII.h"     // for ex_err, etc
53 #include "exodusII_int.h" // for EX_FATAL, EX_NOERR, etc
54 #include "vtk_netcdf.h"       // for NC_NOERR, nc_inq_dimid, etc
55 #include <stddef.h>       // for size_t
56 #include <stdio.h>
57 
58 /*!
59 \ingroup ResultsData
60 
61 The function ex_get_variable_param() reads the number of global,
62 nodal, or element variables stored in the database.
63 
64 \return In case of an error, ex_get_variable_param() returns a negative
65 number; a warning will return a positive number. Possible causes of
66 errors include:
67   -  data file not properly opened with call to ex_create() or ex_open()
68   -  invalid variable type specified.
69 
70 \param[in]  exoid     exodus file ID returned from a previous call to
71 ex_create() or ex_open().
72 \param[in]  obj_type  Variable indicating the type of variable which is
73 described. Use one
74                       of the options in the table below.
75 \param[out] num_vars  Returned number of  var_type variables that are stored
76 in the database.
77 
78 | ex_entity_type|  description              |
79 |---------------|---------------------------|
80 | EX_GLOBAL     |  Global entity type       |
81 | EX_NODAL      |  Nodal entity type        |
82 | EX_NODE_SET   |  Node Set entity type     |
83 | EX_EDGE_BLOCK |  Edge Block entity type   |
84 | EX_EDGE_SET   |  Edge Set entity type     |
85 | EX_FACE_BLOCK |  Face Block entity type   |
86 | EX_FACE_SET   |  Face Set entity type     |
87 | EX_ELEM_BLOCK |  Element Block entity type|
88 | EX_ELEM_SET   |  Element Set entity type  |
89 | EX_SIDE_SET   |  Side Set entity type     |
90 
91 As an example, the following coding will determine the number of
92 global variables stored in the data file:
93 
94 ~~~{.c}
95 int num_glo_vars, error, exoid;
96 
97 \comment{read global variables parameters}
98 error = ex_get_variable_param(exoid, EX_GLOBAL, &num_glo_vars);
99 ~~~
100 
101 */
102 
ex_get_variable_param(int exoid,ex_entity_type obj_type,int * num_vars)103 int ex_get_variable_param(int exoid, ex_entity_type obj_type, int *num_vars)
104 {
105   int         dimid;
106   size_t      dimlen;
107   char        errmsg[MAX_ERR_LENGTH];
108   const char *dnumvar;
109   int         status;
110 
111   EX_FUNC_ENTER();
112   ex_check_valid_file_id(exoid, __func__);
113 
114   *num_vars = 0;
115 
116   switch (obj_type) {
117   case EX_GLOBAL: dnumvar = DIM_NUM_GLO_VAR; break;
118   case EX_NODAL: dnumvar = DIM_NUM_NOD_VAR; break;
119   case EX_EDGE_BLOCK: dnumvar = DIM_NUM_EDG_VAR; break;
120   case EX_FACE_BLOCK: dnumvar = DIM_NUM_FAC_VAR; break;
121   case EX_ELEM_BLOCK: dnumvar = DIM_NUM_ELE_VAR; break;
122   case EX_NODE_SET: dnumvar = DIM_NUM_NSET_VAR; break;
123   case EX_EDGE_SET: dnumvar = DIM_NUM_ESET_VAR; break;
124   case EX_FACE_SET: dnumvar = DIM_NUM_FSET_VAR; break;
125   case EX_SIDE_SET: dnumvar = DIM_NUM_SSET_VAR; break;
126   case EX_ELEM_SET: dnumvar = DIM_NUM_ELSET_VAR; break;
127   default:
128     snprintf(errmsg, MAX_ERR_LENGTH, "Warning: invalid variable type %d requested from file id %d",
129              obj_type, exoid);
130     ex_err(__func__, errmsg, EX_BADPARAM);
131     EX_FUNC_LEAVE(EX_WARN);
132   }
133 
134   if ((status = nc_inq_dimid(exoid, dnumvar, &dimid)) != NC_NOERR) {
135     *num_vars = 0;
136     if (status == NC_EBADDIM) {
137       EX_FUNC_LEAVE(EX_NOERR); /* no global variables defined */
138     }
139 
140     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate %s variable names in file id %d",
141              ex_name_of_object(obj_type), exoid);
142     ex_err(__func__, errmsg, status);
143     EX_FUNC_LEAVE(EX_FATAL);
144   }
145 
146   if ((status = nc_inq_dimlen(exoid, dimid, &dimlen)) != NC_NOERR) {
147     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of %s variables in file id %d",
148              ex_name_of_object(obj_type), exoid);
149     ex_err(__func__, errmsg, status);
150     EX_FUNC_LEAVE(EX_FATAL);
151   }
152   *num_vars = dimlen;
153 
154   EX_FUNC_LEAVE(EX_NOERR);
155 }
156