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 * expmap - ex_put_map
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid                   exodus file id
42 *       int*    elem_map                element order map array
43 *
44 * exit conditions -
45 *
46 * revision history -
47 *
48 *
49 *****************************************************************************/
50 
51 #include "exodusII.h"
52 #include "exodusII_int.h"
53 #include <stdlib.h> /* for free() */
54 
55 /*!
56 \deprecated Use ex_put_num_map() instead.
57 
58 The function ex_put_map() writes out the optional element order map to
59 the database. See #ElementOrderMap for a description of the element
60 order map. The function ex_put_init() must be invoked before this call
61 is made.
62 
63 In case of an error, ex_put_map() returns a negative number; a warning
64 will return a positive number.  Possible causes of errors include:
65   -  data file not properly opened with call to ex_create() or ex_open()
66   -  data file opened for read only.
67   -  data file not initialized properly with call to ex_put_init().
68   -  an element map already exists in the file.
69 
70 \param[in]   exoid    exodus file ID returned from a previous call to ex_create() or ex_open().
71 \param[in]  elem_map  The element order map.
72 
73 The following code generates a default element order map and outputs
74 it to an open exodus file. This is a trivial case and included just
75 for illustration. Since this map is optional, it should be written out
76 only if it contains something other than the default map.
77 
78 \code
79 int error, exoid;
80 int *elem_map = (int *)calloc(num_elem, sizeof(int));
81 for (i=0; i < num_elem; i++) {
82    elem_map[i] = i+1;
83 }
84 error = ex_put_map(exoid, elem_map);
85 \endcode
86 
87  */
88 
ex_put_map(int exoid,const void_int * elem_map)89 int ex_put_map (int  exoid,
90                 const void_int *elem_map)
91 {
92   int numelemdim, dims[1], mapid, status;
93   int map_int_type;
94   char errmsg[MAX_ERR_LENGTH];
95 
96   exerrval = 0; /* clear error code */
97 
98   /* inquire id's of previously defined dimensions  */
99 
100   /* determine number of elements. Return if zero... */
101   if (nc_inq_dimid(exoid, DIM_NUM_ELEM, &numelemdim) != NC_NOERR)
102     {
103       return (EX_NOERR);
104     }
105 
106   /* put netcdf file into define mode  */
107   if ((status = nc_redef (exoid)) != NC_NOERR) {
108     exerrval = status;
109     sprintf(errmsg,
110 	    "Error: failed to put file id %d into define mode",
111 	    exoid);
112     ex_err("ex_put_map",errmsg,exerrval);
113     return (EX_FATAL);
114   }
115 
116   /* create a variable array in which to store the element map  */
117   dims[0] = numelemdim;
118 
119   map_int_type = NC_INT;
120   if (ex_int64_status(exoid) & EX_MAPS_INT64_DB) {
121     map_int_type = NC_INT64;
122   }
123 
124   if ((status = nc_def_var(exoid, VAR_MAP, map_int_type, 1, dims, &mapid)) != NC_NOERR) {
125     if (status == NC_ENAMEINUSE) {
126       exerrval = status;
127       sprintf(errmsg,
128 	      "Error: element map already exists in file id %d",
129 	      exoid);
130       ex_err("ex_put_map",errmsg,exerrval);
131     }
132     else {
133       exerrval = status;
134       sprintf(errmsg,
135 	      "Error: failed to create element map array in file id %d",
136 	      exoid);
137       ex_err("ex_put_map",errmsg,exerrval);
138     }
139     goto error_ret;         /* exit define mode and return */
140   }
141   ex_compress_variable(exoid, mapid, 1);
142 
143   /* leave define mode  */
144   if ((status = nc_enddef (exoid)) != NC_NOERR)
145     {
146       exerrval = status;
147       sprintf(errmsg,
148 	      "Error: failed to complete definition in file id %d",
149 	      exoid);
150       ex_err("ex_put_map",errmsg,exerrval);
151       return (EX_FATAL);
152     }
153 
154 
155   /* write out the element order map  */
156   if (ex_int64_status(exoid) & EX_MAPS_INT64_API) {
157     status = nc_put_var_longlong(exoid, mapid, elem_map);
158   } else {
159     status = nc_put_var_int(exoid, mapid, elem_map);
160   }
161 
162   if (status != NC_NOERR)
163     {
164       exerrval = status;
165       sprintf(errmsg,
166 	      "Error: failed to store element map in file id %d",
167 	      exoid);
168       ex_err("ex_put_map",errmsg,exerrval);
169       return (EX_FATAL);
170     }
171 
172   return (EX_NOERR);
173 
174   /* Fatal error: exit definition mode and return */
175  error_ret:
176   if (nc_enddef (exoid) != NC_NOERR)     /* exit define mode */
177     {
178       sprintf(errmsg,
179 	      "Error: failed to complete definition for file id %d",
180 	      exoid);
181       ex_err("ex_put_map",errmsg,exerrval);
182     }
183   return (EX_FATAL);
184 }
185 
186