| 1 |
// Copyright(C) 1999-2021, 2023, 2024 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include "aprepro.h" // for array, Aprepro, etc |
| 8 |
|
| 9 |
#include <vector> // for vector |
| 10 |
|
| 11 |
namespace SEAMS { |
| 12 |
extern SEAMS::Aprepro *aprepro; |
| 13 |
|
| 14 |
double array_interpolate(const array *arr, double row, double col) |
| 15 |
{ |
| 16 |
/* |
| 17 |
* Bilinear interpolation. |
| 18 |
* Assumes equal grid spacing over the range |
| 19 |
* (0.0 -> rows-1) (0.0 -> cols-1) |
| 20 |
*/ |
| 21 |
|
| 22 |
int cols = arr->cols; |
| 23 |
int rows = arr->rows; |
| 24 |
|
| 25 |
int irl = row; |
| 26 |
int irh = rows > 1 ? irl + 1 : irl; |
| 27 |
int icl = col; |
| 28 |
int ich = cols > 1 ? icl + 1 : icl; |
| 29 |
|
| 30 |
double value = 0.0; |
| 31 |
|
| 32 |
if (irh < rows && ich < cols) { |
| 33 |
double v11 = arr->data[irl * cols + icl]; |
| 34 |
double v21 = arr->data[irh * cols + icl]; |
| 35 |
double v12 = arr->data[irl * cols + ich]; |
| 36 |
double v22 = arr->data[irh * cols + ich]; |
| 37 |
if (rows > 1 && cols > 1) { |
| 38 |
value = (v11 * (irh - row) + v21 * (row - irl)) * (ich - col) + |
| 39 |
(v12 * (irh - row) * v22 * (row - irl)) * (col - icl); |
| 40 |
} |
| 41 |
else if (rows > 1 && cols == 1) { |
| 42 |
value = v11 * (irh - row) + v21 * (row - irl); |
| 43 |
} |
| 44 |
else if (cols > 1 && rows == 1) { |
| 45 |
value = v11 * (ich - col) + v12 * (col - icl); |
| 46 |
} |
| 47 |
} |
| 48 |
else { |
| 49 |
aprepro->error("Row or Column index out of range"); |
| 50 |
} |
| 51 |
return value; |
| 52 |
} |
| 53 |
|
| 54 |
double array_value(array *arr, double row, double col) |
| 55 |
{ |
| 56 |
if (aprepro->ap_options.one_based_index) { |
| 57 |
row--; |
| 58 |
col--; |
| 59 |
} |
| 60 |
|
| 61 |
double value = 0.0; |
| 62 |
auto cols = arr->cols; |
| 63 |
auto rows = arr->rows; |
| 64 |
if (row >= 0 && row < rows && col >= 0 && col < cols) { |
| 65 |
if (row != static_cast<int>(row) || col != static_cast<int>(col)) { |
| 66 |
value = array_interpolate(arr, row, col); |
| 67 |
} |
| 68 |
else { |
| 69 |
|
| 70 |
int irow = row; |
| 71 |
int icol = col; |
| 72 |
int offset = irow * cols + icol; |
| 73 |
value = arr->data[offset]; |
| 74 |
} |
| 75 |
} |
| 76 |
else { |
| 77 |
aprepro->error("Row or Column index out of range"); |
| 78 |
} |
| 79 |
return value; |
| 80 |
} |
| 81 |
|
| 82 |
array *array_add(const array *a, const array *b) |
| 83 |
{ |
| 84 |
auto array_data = aprepro->make_array(a->rows, a->cols); |
| 85 |
for (size_t i = 0; i < a->rows * a->cols; i++) { |
| 86 |
array_data->data[i] = a->data[i] + b->data[i]; |
| 87 |
} |
| 88 |
return array_data; |
| 89 |
} |
| 90 |
|
| 91 |
array *array_sub(const array *a, const array *b) |
| 92 |
{ |
| 93 |
auto array_data = aprepro->make_array(a->rows, a->cols); |
| 94 |
|
| 95 |
for (size_t i = 0; i < a->rows * a->cols; i++) { |
| 96 |
array_data->data[i] = a->data[i] - b->data[i]; |
| 97 |
} |
| 98 |
return array_data; |
| 99 |
} |
| 100 |
|
| 101 |
array *array_scale(const array *a, double s) |
| 102 |
{ |
| 103 |
auto array_data = aprepro->make_array(a->rows, a->cols); |
| 104 |
|
| 105 |
for (size_t i = 0; i < a->rows * a->cols; i++) { |
| 106 |
array_data->data[i] = a->data[i] * s; |
| 107 |
} |
| 108 |
|
| 109 |
return array_data; |
| 110 |
} |
| 111 |
|
| 112 |
array *array_mult(const array *a, const array *b) |
| 113 |
{ |
| 114 |
int ac = a->cols; |
| 115 |
int bc = b->cols; |
| 116 |
|
| 117 |
auto array_data = aprepro->make_array(a->rows, b->cols); |
| 118 |
|
| 119 |
for (size_t i = 0; i < b->cols; i++) { |
| 120 |
for (size_t j = 0; j < a->rows; j++) { |
| 121 |
double sum = 0.0; |
| 122 |
for (size_t k = 0; k < a->cols; k++) { |
| 123 |
sum += a->data[j * ac + k] * b->data[k * bc + i]; |
| 124 |
} |
| 125 |
array_data->data[j * bc + i] = sum; |
| 126 |
} |
| 127 |
} |
| 128 |
return array_data; |
| 129 |
} |
| 130 |
} // namespace SEAMS |