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 * exgotv - ex_get_object_truth_vector
38 *
39 * revision history -
40 *
41 *
42 *****************************************************************************/
43 
44 #include <stdlib.h>
45 #include "exodusII.h"
46 #include "exodusII_int.h"
47 
48 /*!
49  * reads the EXODUS II specified variable truth vector from the database
50  */
51 
ex_get_object_truth_vector(int exoid,ex_entity_type obj_type,ex_entity_id entity_id,int num_var,int * var_vec)52 int ex_get_object_truth_vector (int  exoid,
53 				ex_entity_type obj_type,
54 				ex_entity_id  entity_id,
55 				int  num_var,
56 				int *var_vec)
57 {
58   int statust;
59   int varid, tabid, i, status, ent_ndx;
60   size_t num_var_db = 0;
61   size_t start[2], count[2];
62   char errmsg[MAX_ERR_LENGTH];
63   const char* routine = "ex_get_object_truth_vector";
64 
65   /*
66    * The ent_type and the var_name are used to build the netcdf
67    * variables name.  Normally this is done via a macro defined in
68    * exodusII_int.h
69    */
70   const char* ent_type = NULL;
71   const char* var_name = NULL;
72 
73   exerrval = 0; /* clear error code */
74 
75   switch (obj_type) {
76   case EX_EDGE_BLOCK:
77     status = ex_get_dimension(exoid, DIM_NUM_EDG_VAR,  "edge variables", &num_var_db, &varid, routine);
78     statust = nc_inq_varid(exoid, VAR_EBLK_TAB, &tabid);
79     var_name = "vals_edge_var";
80     ent_type = "eb";
81     break;
82   case EX_FACE_BLOCK:
83     status = ex_get_dimension(exoid, DIM_NUM_FAC_VAR,  "face variables", &num_var_db, &varid, routine);
84     statust = nc_inq_varid (exoid, VAR_FBLK_TAB, &tabid);
85     var_name = "vals_face_var";
86     ent_type = "fb";
87     break;
88   case EX_ELEM_BLOCK:
89     status = ex_get_dimension(exoid, DIM_NUM_ELE_VAR,  "element variables", &num_var_db, &varid, routine);
90     statust = nc_inq_varid (exoid, VAR_ELEM_TAB, &tabid);
91     var_name = "vals_elem_var";
92     ent_type = "eb";
93     break;
94   case EX_NODE_SET:
95     status = ex_get_dimension(exoid, DIM_NUM_NSET_VAR, "nodeset variables", &num_var_db, &varid, routine);
96     statust = nc_inq_varid (exoid, VAR_NSET_TAB, &tabid);
97     var_name = "vals_nset_var";
98     ent_type = "ns";
99     break;
100   case EX_EDGE_SET:
101     status = ex_get_dimension(exoid, DIM_NUM_ESET_VAR, "edgeset variables", &num_var_db, &varid, routine);
102     statust = nc_inq_varid (exoid, VAR_ESET_TAB, &tabid);
103     var_name = "vals_eset_var";
104     ent_type = "es";
105     break;
106   case EX_FACE_SET:
107     status = ex_get_dimension(exoid, DIM_NUM_FSET_VAR, "faceset variables", &num_var_db, &varid, routine);
108     statust = nc_inq_varid (exoid, VAR_FSET_TAB, &tabid);
109     var_name = "vals_fset_var";
110     ent_type = "fs";
111     break;
112   case EX_SIDE_SET:
113     status = ex_get_dimension(exoid, DIM_NUM_SSET_VAR, "sideset variables", &num_var_db, &varid, routine);
114     statust = nc_inq_varid (exoid, VAR_SSET_TAB, &tabid);
115     var_name = "vals_sset_var";
116     ent_type = "ss";
117     break;
118   case EX_ELEM_SET:
119     status = ex_get_dimension(exoid, DIM_NUM_ELSET_VAR, "elemset variables", &num_var_db, &varid, routine);
120     statust = nc_inq_varid (exoid, VAR_ELSET_TAB, &tabid);
121     var_name = "vals_elset_var";
122     ent_type = "es";
123     break;
124   default:
125     exerrval = EX_BADPARAM;
126     sprintf(errmsg,
127 	    "Error: Invalid variable type %d specified in file id %d",
128 	    obj_type, exoid);
129     ex_err(routine,errmsg,exerrval);
130     return (EX_WARN);
131   }
132 
133   if (status != NC_NOERR) {
134     exerrval = status;
135     return (EX_WARN);
136   }
137 
138   /* Determine index of entity_id in id array */
139   ent_ndx = ex_id_lkup(exoid,obj_type,entity_id);
140   if (exerrval != 0) {
141     if (exerrval != EX_NULLENTITY) {
142       sprintf(errmsg,
143 	      "Error: failed to locate %s id %"PRId64" in id variable in file id %d",
144 	      ex_name_of_object(obj_type), entity_id, exoid);
145       ex_err(routine,errmsg,exerrval);
146       return (EX_FATAL);
147     }
148   }
149 
150   /* If this is a null entity, then 'ent_ndx' will be negative.
151    * We don't care in this routine, so make it positive and continue...
152    */
153   if (ent_ndx < 0) ent_ndx = -ent_ndx;
154 
155   if ((int)num_var_db != num_var) {
156     exerrval = EX_FATAL;
157     sprintf(errmsg,
158 	    "Error: # of variables doesn't match those defined in file id %d", exoid);
159     ex_err("ex_get_object_truth_vector",errmsg,exerrval);
160     return (EX_FATAL);
161   }
162 
163   if (statust != NC_NOERR) {
164     /* since truth vector isn't stored in the data file, derive it dynamically */
165     for (i=0; i<num_var; i++) {
166       /* NOTE: names are 1-based */
167       if (nc_inq_varid(exoid, ex_catstr2(var_name, i+1, ent_type, ent_ndx), &tabid) != NC_NOERR) {
168 	/* variable doesn't exist; put a 0 in the truth vector */
169 	var_vec[i] = 0;
170       } else {
171 	/* variable exists; put a 1 in the truth vector */
172 	var_vec[i] = 1;
173       }
174     }
175   } else {
176 
177     /* read in the truth vector */
178 
179     start[0] = ent_ndx-1;
180     start[1] = 0;
181 
182     count[0] = 1;
183     count[1] = num_var;
184 
185     status = nc_get_vara_int(exoid, tabid, start, count, var_vec);
186 
187     if (status != NC_NOERR) {
188       exerrval = status;
189       sprintf(errmsg,
190 	      "Error: failed to get truth vector from file id %d", exoid);
191       ex_err("ex_get_object_truth_vector",errmsg,exerrval);
192       return (EX_FATAL);
193     }
194   }
195   return (EX_NOERR);
196 }
197