| 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 | #include "exodusII.h" |
| 37 | #include "exodusII_int.h" |
| 38 | |
| 39 | /*! |
| 40 | The function ex_put_nodal_var() writes the values of a single nodal |
| 41 | variable for a single time step. The function ex_put_variable_param() |
| 42 | must be invoked before this call is made. |
| 43 | |
| 44 | Because nodal variables are floating point values, the application |
| 45 | code must declare the array passed to be the appropriate type (\c |
| 46 | float or \c double) to match the compute word size passed in |
| 47 | ex_create() or ex_open(). |
| 48 | |
| 49 | \return In case of an error, ex_put_nodal_var() returns a negative number; a |
| 50 | warning will return a positive number. Possible causes of errors |
| 51 | include: |
| 52 | - data file not properly opened with call to ex_create() or ex_open() |
| 53 | - data file opened for read only. |
| 54 | - data file not initialized properly with call to ex_put_init(). |
| 55 | - ex_put_variable_param() not called previously specifying the number of nodal variables. |
| 56 | |
| 57 | |
| 58 | \param[in] exoid exodus file ID returned from a previous call to ex_create() or |
| 59 | ex_open(). |
| 60 | |
| 61 | \param[in] time_step The time step number, as described under ex_put_time(). This |
| 62 | is essentially a counter that is incremented when results variables |
| 63 | are output. The first time step is 1. |
| 64 | |
| 65 | \param[in] nodal_var_index The index of the nodal variable. The first variable has an index of 1. |
| 66 | |
| 67 | \param[in] num_nodes The number of nodal points. |
| 68 | |
| 69 | \param[in] nodal_var_vals Array of \c num_nodes values of the \c nodal_var_index-th nodal |
| 70 | variable for the \c time_step-th time step. |
| 71 | |
| 72 | |
| 73 | As an example, the following code segment writes all the nodal |
| 74 | variables for a single time step: |
| 75 | |
| 76 | \code |
| 77 | int num_nod_vars, num_nodes, error, exoid, time_step; |
| 78 | float *nodal_var_vals; |
| 79 | |
| 80 | \comment{write nodal variables} |
| 81 | nodal_var_vals = (float *) calloc(num_nodes, sizeof(float)); |
| 82 | for (k=1; k <= num_nod_vars; k++) { |
| 83 | for (j=0; j < num_nodes; j++) { |
| 84 | \comment{application code fills in this array} |
| 85 | nodal_var_vals[j] = 10.0; |
| 86 | } |
| 87 | error = ex_put_nodal_var(exoid, time_step, k, num_nodes, |
| 88 | nodal_var_vals); |
| 89 | } |
| 90 | \endcode |
| 91 | |
| 92 | */ |
| 93 | |
| 94 | int ex_put_nodal_var (int exoid, |
| 95 | int time_step, |
| 96 | int nodal_var_index, |
| 97 | int64_t num_nodes, |
| 98 | const void *nodal_var_vals) |
| 99 | |
| 100 | { |
| 101 | int status; |
| 102 | int varid; |
| 103 | size_t start[3], count[3]; |
| 104 | char errmsg[MAX_ERR_LENGTH]; |
| 105 | |
| 106 | exerrval = 0; /* clear error code */ |
| 107 | |
| 108 | if (ex_large_model(exoid) == 0) { |
| 109 | /* write values of the nodal variable */ |
| 110 | if ((status = nc_inq_varid(exoid, VAR_NOD_VAR, &varid)) != NC_NOERR) { |
| 111 | exerrval = status; |
| 112 | sprintf(errmsg, |
| 113 | "Warning: could not find nodal variables in file id %d", |
| 114 | exoid); |
| 115 | ex_err("ex_put_nodal_var",errmsg,exerrval); |
| 116 | return (EX_WARN); |
| 117 | } |
| 118 | start[0] = --time_step; |
| 119 | start[1] = --nodal_var_index; |
| 120 | start[2] = 0; |
| 121 | |
| 122 | count[0] = 1; |
| 123 | count[1] = 1; |
| 124 | count[2] = num_nodes; |
| 125 | } else { |
| 126 | /* nodal variables stored separately, find variable for this variable |
| 127 | index */ |
| 128 | if ((status = nc_inq_varid(exoid, VAR_NOD_VAR_NEW(nodal_var_index), &varid)) != NC_NOERR) { |
| 129 | exerrval = status; |
| 130 | sprintf(errmsg, |
| 131 | "Warning: could not find nodal variable %d in file id %d", |
| 132 | nodal_var_index, exoid); |
| 133 | ex_err("ex_put_nodal_var",errmsg,exerrval); |
| 134 | return (EX_WARN); |
| 135 | } |
| 136 | |
| 137 | start[0] = --time_step; |
| 138 | start[1] = 0; |
| 139 | |
| 140 | count[0] = 1; |
| 141 | count[1] = num_nodes; |
| 142 | } |
| 143 | |
| 144 | if (ex_comp_ws(exoid) == 4) { |
| 145 | status = nc_put_vara_float(exoid, varid, start, count, nodal_var_vals); |
| 146 | } else { |
| 147 | status = nc_put_vara_double(exoid, varid, start, count, nodal_var_vals); |
| 148 | } |
| 149 | |
| 150 | if (status != NC_NOERR) { |
| 151 | exerrval = status; |
| 152 | sprintf(errmsg, |
| 153 | "Error: failed to store nodal variables in file id %d", |
| 154 | exoid); |
| 155 | ex_err("ex_put_nodal_var",errmsg,exerrval); |
| 156 | return (EX_FATAL); |
| 157 | } |
| 158 | return (EX_NOERR); |
| 159 | } |