V_HexMetric.cpp 88.5 KB
Newer Older
Clint Stimpson's avatar
Clint Stimpson committed
1
2
3
4
/*=========================================================================

  Module:    V_HexMetric.cpp

Clinton Stimpson's avatar
Clinton Stimpson committed
5
  Copyright 2003,2006,2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
6
7
  Under the terms of Contract DE-NA0003525 with NTESS,
  the U.S. Government retains certain rights in this software.
Clint Stimpson's avatar
Clint Stimpson committed
8

9
  See LICENSE for details.
Clint Stimpson's avatar
Clint Stimpson committed
10
11
12
13
14
15
16
17
18
19
20

=========================================================================*/

/*
 *
 * HexMetric.cpp contains quality calculations for hexes
 *
 * This file is part of VERDICT
 *
 */

21
#include "V_HexMetric.hpp"
Clint Stimpson's avatar
Clint Stimpson committed
22
#include "V_GaussIntegration.hpp"
23
24
25
#include "VerdictVector.hpp"
#include "verdict.h"

Matt Staten's avatar
Matt Staten committed
26
#include <algorithm>
27
#include <cmath> // for std::isnan
Clint Stimpson's avatar
Clint Stimpson committed
28

29
namespace VERDICT_NAMESPACE
Matt Staten's avatar
Matt Staten committed
30
{
31
extern void quad_minimum_maximum_angle(double min_max_angles[2], const double coordinates[][3]);
Matt Staten's avatar
Matt Staten committed
32

33
34
static const double one_third = 1.0 / 3.0;
static const double two_thirds = 2.0 / 3.0;
35
static const double sqrt3 = std::sqrt(3.0);
36

Clint Stimpson's avatar
Clint Stimpson committed
37
//! weights based on the average size of a hex
38
39
static int hex_get_weight(
  VerdictVector& v1, VerdictVector& v2, VerdictVector& v3, double average_size)
Clint Stimpson's avatar
Clint Stimpson committed
40
{
41
42
  if (average_size == 0)
  {
Clint Stimpson's avatar
Clint Stimpson committed
43
    return 0;
44
  }
Clint Stimpson's avatar
Clint Stimpson committed
45

46
47
48
  v1.set(1, 0, 0);
  v2.set(0, 1, 0);
  v3.set(0, 0, 1);
Clint Stimpson's avatar
Clint Stimpson committed
49

50
  double scale = std::pow(average_size / (VerdictVector::Dot(v1, (v2 * v3))), 0.33333333333);
Clint Stimpson's avatar
Clint Stimpson committed
51
52
53
54
55
56
57
  v1 *= scale;
  v2 *= scale;
  v3 *= scale;

  return 1;
}

58
59
60
61
62
static const double HEX27_node_local_coord[27][3] = { { -1, -1, -1 }, { 1, -1, -1 }, { 1, 1, -1 },
  { -1, 1, -1 }, { -1, -1, 1 }, { 1, -1, 1 }, { 1, 1, 1 }, { -1, 1, 1 }, { 0, -1, -1 },
  { 1, 0, -1 }, { 0, 1, -1 }, { -1, 0, -1 }, { -1, -1, 0 }, { 1, -1, 0 }, { 1, 1, 0 }, { -1, 1, 0 },
  { 0, -1, 1 }, { 1, 0, 1 }, { 0, 1, 1 }, { -1, 0, 1 }, { 0, 0, 0 }, { 0, 0, -1 }, { 0, 0, 1 },
  { -1, 0, 0 }, { 1, 0, 0 }, { 0, -1, 0 }, { 0, 1, 0 } };
Clint Stimpson's avatar
Clint Stimpson committed
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
static int hex20_subtet_conn[36][4] = { { 0, 12, 8, 20 }, { 4, 16, 12, 20 }, { 16, 5, 13, 20 },
  { 1, 8, 13, 20 }, { 8, 12, 16, 20 }, { 8, 16, 13, 20 },

  { 1, 13, 9, 20 }, { 5, 17, 13, 20 }, { 6, 14, 17, 20 }, { 2, 9, 14, 20 }, { 9, 17, 14, 20 },
  { 9, 13, 17, 20 },

  { 7, 15, 18, 20 }, { 3, 10, 15, 20 }, { 2, 14, 10, 20 }, { 6, 18, 14, 20 }, { 10, 18, 15, 20 },
  { 10, 14, 18, 20 },

  { 7, 19, 15, 20 }, { 4, 12, 19, 20 }, { 0, 11, 12, 20 }, { 3, 15, 11, 20 }, { 11, 19, 12, 20 },
  { 11, 15, 19, 20 },

  { 4, 19, 16, 20 }, { 5, 16, 17, 20 }, { 6, 17, 18, 20 }, { 7, 18, 19, 20 }, { 16, 18, 17, 20 },
  { 16, 19, 18, 20 },

  { 0, 8, 11, 20 }, { 8, 1, 9, 20 }, { 2, 10, 9, 20 }, { 3, 11, 10, 20 }, { 8, 9, 10, 20 },
  { 8, 10, 11, 20 } };

static int hex27_subtet_conn[48][4] = { { 0, 12, 8, 20 }, { 4, 16, 12, 20 }, { 16, 5, 13, 20 },
  { 1, 8, 13, 20 }, { 25, 8, 12, 20 }, { 25, 12, 16, 20 }, { 25, 16, 13, 20 }, { 25, 13, 8, 20 },

  { 1, 13, 9, 20 }, { 5, 17, 13, 20 }, { 6, 14, 17, 20 }, { 2, 9, 14, 20 }, { 24, 9, 13, 20 },
  { 24, 13, 17, 20 }, { 24, 17, 14, 20 }, { 24, 14, 9, 20 },

  { 7, 15, 18, 20 }, { 3, 10, 15, 20 }, { 2, 14, 10, 20 }, { 6, 18, 14, 20 }, { 26, 10, 14, 20 },
  { 26, 14, 18, 20 }, { 26, 18, 15, 20 }, { 26, 15, 10, 20 },

  { 7, 19, 15, 20 }, { 4, 12, 19, 20 }, { 0, 11, 12, 20 }, { 3, 15, 11, 20 }, { 23, 11, 15, 20 },
  { 23, 15, 19, 20 }, { 23, 19, 12, 20 }, { 23, 12, 11, 20 },

  { 4, 19, 16, 20 }, { 5, 16, 17, 20 }, { 6, 17, 18, 20 }, { 7, 18, 19, 20 }, { 22, 16, 19, 20 },
  { 22, 19, 18, 20 }, { 22, 18, 17, 20 }, { 22, 17, 16, 20 },

  { 0, 8, 11, 20 }, { 8, 1, 9, 20 }, { 2, 10, 9, 20 }, { 3, 11, 10, 20 }, { 21, 8, 9, 20 },
  { 21, 9, 10, 20 }, { 21, 10, 11, 20 }, { 21, 11, 8, 20 } };

static double compute_tet_volume(VerdictVector& v1, VerdictVector& v2, VerdictVector& v3)
101
{
102
  return (double)((v3 % (v1 * v2)) / 6.0);
103
104
}

105
106
// Compute interior node
VerdictVector hex20_auxillary_node_coordinate(const double coordinates[][3])
107
{
108
  VerdictVector aux_node(0.0, 0.0, 0.0);
109
110
111
112
113
114
  for (int i = 0; i < 8; i++)
  {
    VerdictVector tmp_vec(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
    aux_node += tmp_vec;
  }
  aux_node /= 6;
115

116
  return aux_node;
117
118
}

119
120
static void HEX27_gradients_of_the_shape_functions_for_RST(
  const double rst[3], double dhdr[27], double dhds[27], double dhdt[27])
Clint Stimpson's avatar
Clint Stimpson committed
121
122
123
124
125
{
  double g1r = -0.5 * rst[0] * (1 - rst[0]);
  double g1s = -0.5 * rst[1] * (1 - rst[1]);
  double g1t = -0.5 * rst[2] * (1 - rst[2]);

126
127
128
  double g2r = (1 + rst[0]) * (1 - rst[0]);
  double g2s = (1 + rst[1]) * (1 - rst[1]);
  double g2t = (1 + rst[2]) * (1 - rst[2]);
Clint Stimpson's avatar
Clint Stimpson committed
129
130
131
132
133
134
135
136
137

  double g3r = 0.5 * rst[0] * (1 + rst[0]);
  double g3s = 0.5 * rst[1] * (1 + rst[1]);
  double g3t = 0.5 * rst[2] * (1 + rst[2]);

  double g1r_r = rst[0] - 0.5;
  double g1s_s = rst[1] - 0.5;
  double g1t_t = rst[2] - 0.5;

138
139
140
  double g2r_r = -2 * rst[0];
  double g2s_s = -2 * rst[1];
  double g2t_t = -2 * rst[2];
Clint Stimpson's avatar
Clint Stimpson committed
141
142
143
144
145

  double g3r_r = rst[0] + 0.5;
  double g3s_s = rst[1] + 0.5;
  double g3t_t = rst[2] + 0.5;

146
  // dh/dr;
Clint Stimpson's avatar
Clint Stimpson committed
147
148
149
150
151
152
153
154
155
156
  dhdr[0] = g1r_r * g1s * g1t;
  dhdr[1] = g3r_r * g1s * g1t;
  dhdr[2] = g3r_r * g3s * g1t;
  dhdr[3] = g1r_r * g3s * g1t;
  dhdr[4] = g1r_r * g1s * g3t;
  dhdr[5] = g3r_r * g1s * g3t;
  dhdr[6] = g3r_r * g3s * g3t;
  dhdr[7] = g1r_r * g3s * g3t;
  dhdr[8] = g2r_r * g1s * g1t;
  dhdr[9] = g3r_r * g2s * g1t;
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  dhdr[10] = g2r_r * g3s * g1t;
  dhdr[11] = g1r_r * g2s * g1t;
  dhdr[16] = g2r_r * g1s * g3t;
  dhdr[17] = g3r_r * g2s * g3t;
  dhdr[18] = g2r_r * g3s * g3t;
  dhdr[19] = g1r_r * g2s * g3t;
  dhdr[12] = g1r_r * g1s * g2t;
  dhdr[13] = g3r_r * g1s * g2t;
  dhdr[14] = g3r_r * g3s * g2t;
  dhdr[15] = g1r_r * g3s * g2t;
  dhdr[23] = g1r_r * g2s * g2t;
  dhdr[24] = g3r_r * g2s * g2t;
  dhdr[25] = g2r_r * g1s * g2t;
  dhdr[26] = g2r_r * g3s * g2t;
  dhdr[21] = g2r_r * g2s * g1t;
  dhdr[22] = g2r_r * g2s * g3t;
  dhdr[20] = g2r_r * g2s * g2t;

  // dh/ds;
Clint Stimpson's avatar
Clint Stimpson committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  dhds[0] = g1r * g1s_s * g1t;
  dhds[1] = g3r * g1s_s * g1t;
  dhds[2] = g3r * g3s_s * g1t;
  dhds[3] = g1r * g3s_s * g1t;
  dhds[4] = g1r * g1s_s * g3t;
  dhds[5] = g3r * g1s_s * g3t;
  dhds[6] = g3r * g3s_s * g3t;
  dhds[7] = g1r * g3s_s * g3t;
  dhds[8] = g2r * g1s_s * g1t;
  dhds[9] = g3r * g2s_s * g1t;
  dhds[10] = g2r * g3s_s * g1t;
  dhds[11] = g1r * g2s_s * g1t;
  dhds[16] = g2r * g1s_s * g3t;
  dhds[17] = g3r * g2s_s * g3t;
  dhds[18] = g2r * g3s_s * g3t;
  dhds[19] = g1r * g2s_s * g3t;
  dhds[12] = g1r * g1s_s * g2t;
  dhds[13] = g3r * g1s_s * g2t;
  dhds[14] = g3r * g3s_s * g2t;
  dhds[15] = g1r * g3s_s * g2t;
  dhds[23] = g1r * g2s_s * g2t;
  dhds[24] = g3r * g2s_s * g2t;
  dhds[25] = g2r * g1s_s * g2t;
  dhds[26] = g2r * g3s_s * g2t;
  dhds[21] = g2r * g2s_s * g1t;
  dhds[22] = g2r * g2s_s * g3t;
  dhds[20] = g2r * g2s_s * g2t;

204
  // dh/dt;
Clint Stimpson's avatar
Clint Stimpson committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  dhdt[0] = g1r * g1s * g1t_t;
  dhdt[1] = g3r * g1s * g1t_t;
  dhdt[2] = g3r * g3s * g1t_t;
  dhdt[3] = g1r * g3s * g1t_t;
  dhdt[4] = g1r * g1s * g3t_t;
  dhdt[5] = g3r * g1s * g3t_t;
  dhdt[6] = g3r * g3s * g3t_t;
  dhdt[7] = g1r * g3s * g3t_t;
  dhdt[8] = g2r * g1s * g1t_t;
  dhdt[9] = g3r * g2s * g1t_t;
  dhdt[10] = g2r * g3s * g1t_t;
  dhdt[11] = g1r * g2s * g1t_t;
  dhdt[16] = g2r * g1s * g3t_t;
  dhdt[17] = g3r * g2s * g3t_t;
  dhdt[18] = g2r * g3s * g3t_t;
  dhdt[19] = g1r * g2s * g3t_t;
  dhdt[12] = g1r * g1s * g2t_t;
  dhdt[13] = g3r * g1s * g2t_t;
  dhdt[14] = g3r * g3s * g2t_t;
  dhdt[15] = g1r * g3s * g2t_t;
  dhdt[23] = g1r * g2s * g2t_t;
  dhdt[24] = g3r * g2s * g2t_t;
  dhdt[25] = g2r * g1s * g2t_t;
  dhdt[26] = g2r * g3s * g2t_t;
  dhdt[21] = g2r * g2s * g1t_t;
  dhdt[22] = g2r * g2s * g3t_t;
  dhdt[20] = g2r * g2s * g2t_t;

233
  for (int i = 0; i < 27; i++)
Clint Stimpson's avatar
Clint Stimpson committed
234
235
236
237
238
239
240
  {
    dhdr[i] *= 2;
    dhds[i] *= 2;
    dhdt[i] *= 2;
  }
}

241
242
243
244
#define make_hex_nodes(coord, pos)                                                                 \
  for (int mhcii = 0; mhcii < 8; mhcii++)                                                          \
  {                                                                                                \
    pos[mhcii].set(coord[mhcii][0], coord[mhcii][1], coord[mhcii][2]);                             \
Clint Stimpson's avatar
Clint Stimpson committed
245
246
  }

247
248
249
250
251
#define make_edge_length_squares(edges, lengths)                                                   \
  {                                                                                                \
    for (int melii = 0; melii < 12; melii++)                                                       \
      lengths[melii] = edges[melii].length_squared();                                              \
  }
Clint Stimpson's avatar
Clint Stimpson committed
252
253

//! make VerdictVectors from coordinates
254
static void make_hex_edges(const double coordinates[][3], VerdictVector edges[12])
Clint Stimpson's avatar
Clint Stimpson committed
255
{
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  edges[0].set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
    coordinates[1][2] - coordinates[0][2]);
  edges[1].set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
    coordinates[2][2] - coordinates[1][2]);
  edges[2].set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
    coordinates[3][2] - coordinates[2][2]);
  edges[3].set(coordinates[0][0] - coordinates[3][0], coordinates[0][1] - coordinates[3][1],
    coordinates[0][2] - coordinates[3][2]);
  edges[4].set(coordinates[5][0] - coordinates[4][0], coordinates[5][1] - coordinates[4][1],
    coordinates[5][2] - coordinates[4][2]);
  edges[5].set(coordinates[6][0] - coordinates[5][0], coordinates[6][1] - coordinates[5][1],
    coordinates[6][2] - coordinates[5][2]);
  edges[6].set(coordinates[7][0] - coordinates[6][0], coordinates[7][1] - coordinates[6][1],
    coordinates[7][2] - coordinates[6][2]);
  edges[7].set(coordinates[4][0] - coordinates[7][0], coordinates[4][1] - coordinates[7][1],
    coordinates[4][2] - coordinates[7][2]);
  edges[8].set(coordinates[4][0] - coordinates[0][0], coordinates[4][1] - coordinates[0][1],
    coordinates[4][2] - coordinates[0][2]);
  edges[9].set(coordinates[5][0] - coordinates[1][0], coordinates[5][1] - coordinates[1][1],
    coordinates[5][2] - coordinates[1][2]);
  edges[10].set(coordinates[6][0] - coordinates[2][0], coordinates[6][1] - coordinates[2][1],
    coordinates[6][2] - coordinates[2][2]);
  edges[11].set(coordinates[7][0] - coordinates[3][0], coordinates[7][1] - coordinates[3][1],
    coordinates[7][2] - coordinates[3][2]);
Clint Stimpson's avatar
Clint Stimpson committed
280
281
}

282
#if 0  /* Not currently used and not exposed in verdict.h */
Clint Stimpson's avatar
Clint Stimpson committed
283
284
/*!
  localizes hex coordinates
285
286
 */
static void localize_hex_coordinates(const double coordinates[][3], VerdictVector position[8] )
Clint Stimpson's avatar
Clint Stimpson committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
{

  int ii;
  for ( ii = 0; ii < 8; ii++ )
  {
    position[ii].set( coordinates[ii][0],
        coordinates[ii][1],
        coordinates[ii][2] );
  }
  
  // ... Make centroid of element the center of coordinate system
  VerdictVector point_1256 = position[1];
  point_1256 += position[2];
  point_1256 += position[5];
  point_1256 += position[6];

  VerdictVector point_0374 = position[0];
  point_0374 += position[3];
  point_0374 += position[7];
  point_0374 += position[4];

  VerdictVector centroid = point_1256;
  centroid += point_0374;
  centroid /= 8.0;

  int i;
  for ( i = 0; i < 8; i++)
314
315
316
  {
  position[i] -= centroid;
  }
Clint Stimpson's avatar
Clint Stimpson committed
317
318
319
320
321
322

  // ... Rotate element such that center of side 1-2 and 0-3 define X axis
  double DX = point_1256.x() - point_0374.x();
  double DY = point_1256.y() - point_0374.y();
  double DZ = point_1256.z() - point_0374.z();

323
324
  double AMAGX = std::sqrt(DX*DX + DZ*DZ);
  double AMAGY = std::sqrt(DX*DX + DY*DY + DZ*DZ);
Clint Stimpson's avatar
Clint Stimpson committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  double FMAGX = AMAGX == 0.0 ? 1.0 : 0.0;
  double FMAGY = AMAGY == 0.0 ? 1.0 : 0.0;

  double CZ = DX / (AMAGX + FMAGX) + FMAGX;
  double SZ = DZ / (AMAGX + FMAGX);
  double CY = AMAGX / (AMAGY + FMAGY) + FMAGY;
  double SY = DY / (AMAGY + FMAGY);

  double temp;
 
  for (i = 0; i < 8; i++) 
  {
    temp =  CY * CZ * position[i].x() + CY * SZ * position[i].z() +
      SY * position[i].y();
    position[i].y( -SY * CZ * position[i].x() - SY * SZ * position[i].z() +
        CY * position[i].y());
    position[i].z( -SZ * position[i].x() + CZ * position[i].z());
    position[i].x(temp);
  }

  // ... Now, rotate about Y
  VerdictVector delta = -position[0];
  delta -= position[1];
  delta += position[2];
  delta += position[3];
  delta -= position[4];
  delta -= position[5];
  delta += position[6];
  delta += position[7];

  DY = delta.y();
  DZ = delta.z();

358
  AMAGY = std::sqrt(DY*DY + DZ*DZ);
Clint Stimpson's avatar
Clint Stimpson committed
359
360
361
362
363
364
  FMAGY = AMAGY == 0.0 ? 1.0 : 0.0;

  double CX = DY / (AMAGY + FMAGY) + FMAGY;
  double SX = DZ / (AMAGY + FMAGY);
  
  for (i = 0; i < 8; i++) 
365
  {
Clint Stimpson's avatar
Clint Stimpson committed
366
367
368
    temp =  CX * position[i].y() + SX * position[i].z();
    position[i].z(-SX * position[i].y() + CX * position[i].z());
    position[i].y(temp);
369
  }
Clint Stimpson's avatar
Clint Stimpson committed
370
371
}

Matt Staten's avatar
Matt Staten committed
372
static double safe_ratio3( const double numerator, 
Clint Stimpson's avatar
Clint Stimpson committed
373
374
375
    const double denominator,
    const double max_ratio )
{
376
  // this filter is essential for good running time in practice
Clint Stimpson's avatar
Clint Stimpson committed
377
378
379
380
  double return_value;

  const double filter_n = max_ratio * 1.0e-16;
  const double filter_d = 1.0e-16;
381
  if ( std::abs( numerator ) <= filter_n && std::abs( denominator ) >= filter_d )
382
  {
Clint Stimpson's avatar
Clint Stimpson committed
383
    return_value = numerator / denominator;
384
  }
Clint Stimpson's avatar
Clint Stimpson committed
385
  else
386
  {
387
    return_value = std::abs(numerator) / max_ratio >= std::abs(denominator) ?
Clint Stimpson's avatar
Clint Stimpson committed
388
389
390
391
      ( (numerator >= 0.0 && denominator >= 0.0) ||
        (numerator < 0.0 && denominator < 0.0) ?
        max_ratio : -max_ratio )
      : numerator / denominator;
392
  }
Clint Stimpson's avatar
Clint Stimpson committed
393
394
395
396
  return return_value;
}
#endif /* Not currently used and not exposed in verdict.h */

397
static double safe_ratio(const double numerator, const double denominator)
Clint Stimpson's avatar
Clint Stimpson committed
398
399
400
{

  double return_value;
401
  const double filter_n = VERDICT_DBL_MAX;
Clint Stimpson's avatar
Clint Stimpson committed
402
  const double filter_d = VERDICT_DBL_MIN;
403
  if (std::abs(numerator) <= filter_n && std::abs(denominator) >= filter_d)
404
  {
Clint Stimpson's avatar
Clint Stimpson committed
405
    return_value = numerator / denominator;
406
  }
Clint Stimpson's avatar
Clint Stimpson committed
407
  else
408
  {
Clint Stimpson's avatar
Clint Stimpson committed
409
    return_value = VERDICT_DBL_MAX;
410
  }
Clint Stimpson's avatar
Clint Stimpson committed
411
412
413
414

  return return_value;
}

Matt Staten's avatar
Matt Staten committed
415
static double condition_comp(
416
  const VerdictVector& xxi, const VerdictVector& xet, const VerdictVector& xze)
Clint Stimpson's avatar
Clint Stimpson committed
417
{
Matt Staten's avatar
Matt Staten committed
418
  double det = VerdictVector::Dot(xxi, (xet * xze));
Clint Stimpson's avatar
Clint Stimpson committed
419

420
421
422
423
424
425
426
427
428
  if (det <= VERDICT_DBL_MIN)
  {
    return VERDICT_DBL_MAX;
  }

  double term1 =
    VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze);
  double term2 = VerdictVector::Dot((xxi * xet), (xxi * xet)) +
    VerdictVector::Dot((xet * xze), (xet * xze)) + VerdictVector::Dot((xze * xxi), (xze * xxi));
Clint Stimpson's avatar
Clint Stimpson committed
429

430
  return std::sqrt(term1 * term2) / det;
431
}
Clint Stimpson's avatar
Clint Stimpson committed
432

433
434
static double oddy_comp(
  const VerdictVector& xxi, const VerdictVector& xet, const VerdictVector& xze)
Clint Stimpson's avatar
Clint Stimpson committed
435
436
{
  double g11, g12, g13, g22, g23, g33, rt_g;
437

Matt Staten's avatar
Matt Staten committed
438
439
440
441
442
443
444
  g11 = VerdictVector::Dot(xxi, xxi);
  g12 = VerdictVector::Dot(xxi, xet);
  g13 = VerdictVector::Dot(xxi, xze);
  g22 = VerdictVector::Dot(xet, xet);
  g23 = VerdictVector::Dot(xet, xze);
  g33 = VerdictVector::Dot(xze, xze);
  rt_g = VerdictVector::Dot(xxi, (xet * xze));
445

Clint Stimpson's avatar
Clint Stimpson committed
446
  double oddy_metric;
447
  if (rt_g > VERDICT_DBL_MIN)
Clint Stimpson's avatar
Clint Stimpson committed
448
  {
449
450
451
    double norm_G_squared =
      g11 * g11 + 2.0 * g12 * g12 + 2.0 * g13 * g13 + g22 * g22 + 2.0 * g23 * g23 + g33 * g33;

Clint Stimpson's avatar
Clint Stimpson committed
452
    double norm_J_squared = g11 + g22 + g33;
453

454
455
    oddy_metric = (norm_G_squared - one_third * norm_J_squared * norm_J_squared) /
      std::pow(rt_g, 4. * one_third);
Clint Stimpson's avatar
Clint Stimpson committed
456
457
  }
  else
458
  {
Clint Stimpson's avatar
Clint Stimpson committed
459
    oddy_metric = VERDICT_DBL_MAX;
460
  }
Clint Stimpson's avatar
Clint Stimpson committed
461
462
463
  return oddy_metric;
}

464
465
//! calculates edge lengths of a hex
static double hex_edge_length(int max_min, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
466
467
468
{
  double temp[3], edge[12];
  int i;
469
470
471

  // lengths^2 of edges
  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
472
473
474
475
  {
    temp[i] = coordinates[1][i] - coordinates[0][i];
    temp[i] = temp[i] * temp[i];
  }
476
  edge[0] = std::sqrt(temp[0] + temp[1] + temp[2]);
477
478

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
479
480
481
482
  {
    temp[i] = coordinates[2][i] - coordinates[1][i];
    temp[i] = temp[i] * temp[i];
  }
483
  edge[1] = std::sqrt(temp[0] + temp[1] + temp[2]);
484
485

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
486
487
488
489
  {
    temp[i] = coordinates[3][i] - coordinates[2][i];
    temp[i] = temp[i] * temp[i];
  }
490
  edge[2] = std::sqrt(temp[0] + temp[1] + temp[2]);
491
492

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
493
494
495
496
  {
    temp[i] = coordinates[0][i] - coordinates[3][i];
    temp[i] = temp[i] * temp[i];
  }
497
  edge[3] = std::sqrt(temp[0] + temp[1] + temp[2]);
498
499

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
500
501
502
503
  {
    temp[i] = coordinates[5][i] - coordinates[4][i];
    temp[i] = temp[i] * temp[i];
  }
504
  edge[4] = std::sqrt(temp[0] + temp[1] + temp[2]);
505
506

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
507
508
509
510
  {
    temp[i] = coordinates[6][i] - coordinates[5][i];
    temp[i] = temp[i] * temp[i];
  }
511
  edge[5] = std::sqrt(temp[0] + temp[1] + temp[2]);
512
513

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
514
515
516
517
  {
    temp[i] = coordinates[7][i] - coordinates[6][i];
    temp[i] = temp[i] * temp[i];
  }
518
  edge[6] = std::sqrt(temp[0] + temp[1] + temp[2]);
519
520

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
521
522
523
524
  {
    temp[i] = coordinates[4][i] - coordinates[7][i];
    temp[i] = temp[i] * temp[i];
  }
525
  edge[7] = std::sqrt(temp[0] + temp[1] + temp[2]);
526
527

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
528
529
530
531
  {
    temp[i] = coordinates[4][i] - coordinates[0][i];
    temp[i] = temp[i] * temp[i];
  }
532
  edge[8] = std::sqrt(temp[0] + temp[1] + temp[2]);
533
534

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
535
536
537
538
  {
    temp[i] = coordinates[5][i] - coordinates[1][i];
    temp[i] = temp[i] * temp[i];
  }
539
  edge[9] = std::sqrt(temp[0] + temp[1] + temp[2]);
540
541

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
542
543
544
545
  {
    temp[i] = coordinates[6][i] - coordinates[2][i];
    temp[i] = temp[i] * temp[i];
  }
546
  edge[10] = std::sqrt(temp[0] + temp[1] + temp[2]);
547
548

  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
549
550
551
552
  {
    temp[i] = coordinates[7][i] - coordinates[3][i];
    temp[i] = temp[i] * temp[i];
  }
553
  edge[11] = std::sqrt(temp[0] + temp[1] + temp[2]);
554

Clint Stimpson's avatar
Clint Stimpson committed
555
  double _edge = edge[0];
556
557

  if (max_min == 0)
Clint Stimpson's avatar
Clint Stimpson committed
558
  {
559
560
561
562
    for (i = 1; i < 12; i++)
    {
      _edge = std::min(_edge, edge[i]);
    }
Clint Stimpson's avatar
Clint Stimpson committed
563
    return _edge;
564
  }
Clint Stimpson's avatar
Clint Stimpson committed
565
566
  else
  {
567
568
569
570
    for (i = 1; i < 12; i++)
    {
      _edge = std::max(_edge, edge[i]);
    }
Clint Stimpson's avatar
Clint Stimpson committed
571
572
573
574
    return _edge;
  }
}

575
static double diag_length(int max_min, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
576
577
578
{
  double temp[3], diag[4];
  int i;
579
580
581

  // lengths^2  f diag nals
  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
582
583
584
585
  {
    temp[i] = coordinates[6][i] - coordinates[0][i];
    temp[i] = temp[i] * temp[i];
  }
586
  diag[0] = std::sqrt(temp[0] + temp[1] + temp[2]);
Clint Stimpson's avatar
Clint Stimpson committed
587

588
  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
589
590
591
592
  {
    temp[i] = coordinates[4][i] - coordinates[2][i];
    temp[i] = temp[i] * temp[i];
  }
593
  diag[1] = std::sqrt(temp[0] + temp[1] + temp[2]);
Clint Stimpson's avatar
Clint Stimpson committed
594

595
  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
596
597
598
599
  {
    temp[i] = coordinates[7][i] - coordinates[1][i];
    temp[i] = temp[i] * temp[i];
  }
600
  diag[2] = std::sqrt(temp[0] + temp[1] + temp[2]);
Clint Stimpson's avatar
Clint Stimpson committed
601

602
  for (i = 0; i < 3; i++)
Clint Stimpson's avatar
Clint Stimpson committed
603
604
605
606
  {
    temp[i] = coordinates[5][i] - coordinates[3][i];
    temp[i] = temp[i] * temp[i];
  }
607
  diag[3] = std::sqrt(temp[0] + temp[1] + temp[2]);
Clint Stimpson's avatar
Clint Stimpson committed
608
609

  double diagonal = diag[0];
610
611
612
613
614
615
  if (max_min == 0) // Return min diagonal
  {
    for (i = 1; i < 4; i++)
    {
      diagonal = std::min(diagonal, diag[i]);
    }
Clint Stimpson's avatar
Clint Stimpson committed
616
617
    return diagonal;
  }
618
  else // Return max diagonal
Clint Stimpson's avatar
Clint Stimpson committed
619
  {
620
621
622
623
624
    for (i = 1; i < 4; i++)
    {
      diagonal = std::max(diagonal, diag[i]);
    }
    return diagonal;
Clint Stimpson's avatar
Clint Stimpson committed
625
626
627
628
  }
}

//! calculates efg values
629
static VerdictVector calc_hex_efg(int efg_index, VerdictVector coordinates[8])
Clint Stimpson's avatar
Clint Stimpson committed
630
{
631

Clint Stimpson's avatar
Clint Stimpson committed
632
  VerdictVector efg;
633
634
635

  switch (efg_index)
  {
Clint Stimpson's avatar
Clint Stimpson committed
636
    case 1:
637
      efg = coordinates[1];
Clint Stimpson's avatar
Clint Stimpson committed
638
639
640
641
642
643
644
645
      efg += coordinates[2];
      efg += coordinates[5];
      efg += coordinates[6];
      efg -= coordinates[0];
      efg -= coordinates[3];
      efg -= coordinates[4];
      efg -= coordinates[7];
      break;
646

Clint Stimpson's avatar
Clint Stimpson committed
647
    case 2:
648
      efg = coordinates[2];
Clint Stimpson's avatar
Clint Stimpson committed
649
650
651
652
653
654
655
656
      efg += coordinates[3];
      efg += coordinates[6];
      efg += coordinates[7];
      efg -= coordinates[0];
      efg -= coordinates[1];
      efg -= coordinates[4];
      efg -= coordinates[5];
      break;
657

Clint Stimpson's avatar
Clint Stimpson committed
658
    case 3:
659
      efg = coordinates[4];
Clint Stimpson's avatar
Clint Stimpson committed
660
661
662
663
664
665
666
667
668
669
      efg += coordinates[5];
      efg += coordinates[6];
      efg += coordinates[7];
      efg -= coordinates[0];
      efg -= coordinates[1];
      efg -= coordinates[2];
      efg -= coordinates[3];
      break;

    case 12:
670
      efg = coordinates[0];
Clint Stimpson's avatar
Clint Stimpson committed
671
672
673
674
675
676
677
678
      efg += coordinates[2];
      efg += coordinates[4];
      efg += coordinates[6];
      efg -= coordinates[1];
      efg -= coordinates[3];
      efg -= coordinates[5];
      efg -= coordinates[7];
      break;
679

Clint Stimpson's avatar
Clint Stimpson committed
680
    case 13:
681
      efg = coordinates[0];
Clint Stimpson's avatar
Clint Stimpson committed
682
683
684
685
686
687
688
689
690
      efg += coordinates[3];
      efg += coordinates[5];
      efg += coordinates[6];
      efg -= coordinates[1];
      efg -= coordinates[2];
      efg -= coordinates[4];
      efg -= coordinates[7];
      break;

691
692
    case 23:
      efg = coordinates[0];
Clint Stimpson's avatar
Clint Stimpson committed
693
694
695
696
697
698
699
700
701
      efg += coordinates[1];
      efg += coordinates[6];
      efg += coordinates[7];
      efg -= coordinates[2];
      efg -= coordinates[3];
      efg -= coordinates[4];
      efg -= coordinates[5];
      break;

702
703
    case 123:
      efg = coordinates[0];
Clint Stimpson's avatar
Clint Stimpson committed
704
705
706
707
708
709
710
711
      efg += coordinates[2];
      efg += coordinates[5];
      efg += coordinates[7];
      efg -= coordinates[1];
      efg -= coordinates[5];
      efg -= coordinates[6];
      efg -= coordinates[2];
      break;
712
713
714

    default:
      efg.set(0, 0, 0);
Clint Stimpson's avatar
Clint Stimpson committed
715
716
717
718
719
720
721
  }
  return efg;
}

/*!
   the edge ratio of a hex

722
   NB (P. Pebay 01/23/07):
Clint Stimpson's avatar
Clint Stimpson committed
723
724
     Hmax / Hmin where Hmax and Hmin are respectively the maximum and the
     minimum edge lengths
725
726
 */
double hex_edge_ratio(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
727
728
{
  VerdictVector edges[12];
Matt Staten's avatar
Matt Staten committed
729
  make_hex_edges(coordinates, edges);
730

Clint Stimpson's avatar
Clint Stimpson committed
731
732
733
734
735
736
737
738
739
740
741
742
743
  double a2 = edges[0].length_squared();
  double b2 = edges[1].length_squared();
  double c2 = edges[2].length_squared();
  double d2 = edges[3].length_squared();
  double e2 = edges[4].length_squared();
  double f2 = edges[5].length_squared();
  double g2 = edges[6].length_squared();
  double h2 = edges[7].length_squared();
  double i2 = edges[8].length_squared();
  double j2 = edges[9].length_squared();
  double k2 = edges[10].length_squared();
  double l2 = edges[11].length_squared();

744
745
  double mab, mcd, mef, Mab, Mcd, Mef;
  double mgh, mij, mkl, Mgh, Mij, Mkl;
Clint Stimpson's avatar
Clint Stimpson committed
746

747
748
749
750
751
  if (a2 < b2)
  {
    mab = a2;
    Mab = b2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
752
  else // b2 <= a2
753
754
755
756
757
758
759
760
761
  {
    mab = b2;
    Mab = a2;
  }
  if (c2 < d2)
  {
    mcd = c2;
    Mcd = d2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
762
  else // d2 <= c2
763
764
765
766
767
768
769
770
771
  {
    mcd = d2;
    Mcd = c2;
  }
  if (e2 < f2)
  {
    mef = e2;
    Mef = f2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
772
  else // f2 <= e2
773
774
775
776
777
778
779
780
781
  {
    mef = f2;
    Mef = e2;
  }
  if (g2 < h2)
  {
    mgh = g2;
    Mgh = h2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
782
  else // h2 <= g2
783
784
785
786
787
788
789
790
791
  {
    mgh = h2;
    Mgh = g2;
  }
  if (i2 < j2)
  {
    mij = i2;
    Mij = j2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
792
  else // j2 <= i2
793
794
795
796
797
798
799
800
801
  {
    mij = j2;
    Mij = i2;
  }
  if (k2 < l2)
  {
    mkl = k2;
    Mkl = l2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
802
  else // l2 <= k2
803
804
805
806
  {
    mkl = l2;
    Mkl = k2;
  }
Clint Stimpson's avatar
Clint Stimpson committed
807
808
809

  double m2;
  m2 = mab < mcd ? mab : mcd;
810
811
812
813
  m2 = m2 < mef ? m2 : mef;
  m2 = m2 < mgh ? m2 : mgh;
  m2 = m2 < mij ? m2 : mij;
  m2 = m2 < mkl ? m2 : mkl;
Clint Stimpson's avatar
Clint Stimpson committed
814

815
816
  if (m2 < VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
817
    return (double)VERDICT_DBL_MAX;
818
  }
Clint Stimpson's avatar
Clint Stimpson committed
819
820
821

  double M2;
  M2 = Mab > Mcd ? Mab : Mcd;
822
823
824
825
826
  M2 = M2 > Mef ? M2 : Mef;
  M2 = M2 > Mgh ? M2 : Mgh;
  M2 = M2 > Mij ? M2 : Mij;
  M2 = M2 > Mkl ? M2 : Mkl;
  m2 = m2 < mef ? m2 : mef;
Clint Stimpson's avatar
Clint Stimpson committed
827
828

  M2 = Mab > Mcd ? Mab : Mcd;
829
  M2 = M2 > Mef ? M2 : Mef;
Clint Stimpson's avatar
Clint Stimpson committed
830

831
  double edge_ratio = std::sqrt(M2 / m2);
832
833
834
835
836
837

  if (edge_ratio > 0)
  {
    return (double)std::min(edge_ratio, VERDICT_DBL_MAX);
  }
  return (double)std::max(edge_ratio, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
838
839
840
841
842
843
}

/*!
  max edge ratio of a hex

  Maximum edge length ratio at hex center
844
845
 */
double hex_max_edge_ratio(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
846
847
848
{
  double aspect;
  VerdictVector node_pos[8];
849
850
  make_hex_nodes(coordinates, node_pos);

Clint Stimpson's avatar
Clint Stimpson committed
851
  double aspect_12, aspect_13, aspect_23;
852
853
854
855

  VerdictVector efg1 = calc_hex_efg(1, node_pos);
  VerdictVector efg2 = calc_hex_efg(2, node_pos);
  VerdictVector efg3 = calc_hex_efg(3, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
856
857
858
859
860

  double mag_efg1 = efg1.length();
  double mag_efg2 = efg2.length();
  double mag_efg3 = efg3.length();

861
862
863
  aspect_12 = safe_ratio(std::max(mag_efg1, mag_efg2), std::min(mag_efg1, mag_efg2));
  aspect_13 = safe_ratio(std::max(mag_efg1, mag_efg3), std::min(mag_efg1, mag_efg3));
  aspect_23 = safe_ratio(std::max(mag_efg2, mag_efg3), std::min(mag_efg2, mag_efg3));
Clint Stimpson's avatar
Clint Stimpson committed
864

865
866
867
868
869
870
871
  aspect = std::max({ aspect_12, aspect_13, aspect_23 });

  if (aspect > 0)
  {
    return (double)std::min(aspect, VERDICT_DBL_MAX);
  }
  return (double)std::max(aspect, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
872
873
}

874
double hex_equiangle_skew(int /*num_nodes*/, const double coordinates[][3])
875
876
{
  double quad[4][3];
877
878
  double min_angle = 360.0;
  double max_angle = 0.0;
879
880
  double min_max_angle[2];

881
882
883
884
885
886
887
888
889
890
891
892
  quad[0][0] = coordinates[0][0];
  quad[0][1] = coordinates[0][1];
  quad[0][2] = coordinates[0][2];
  quad[1][0] = coordinates[1][0];
  quad[1][1] = coordinates[1][1];
  quad[1][2] = coordinates[1][2];
  quad[2][0] = coordinates[5][0];
  quad[2][1] = coordinates[5][1];
  quad[2][2] = coordinates[5][2];
  quad[3][0] = coordinates[4][0];
  quad[3][1] = coordinates[4][1];
  quad[3][2] = coordinates[4][2];
893

Matt Staten's avatar
Matt Staten committed
894
  quad_minimum_maximum_angle(min_max_angle, quad);
895
896
897
898
899
900
901
902
  if (min_max_angle[0] < min_angle)
  {
    min_angle = min_max_angle[0];
  }
  if (min_max_angle[1] > max_angle)
  {
    max_angle = min_max_angle[1];
  }
903

904
905
906
907
908
909
910
911
912
913
914
915
  quad[0][0] = coordinates[1][0];
  quad[0][1] = coordinates[1][1];
  quad[0][2] = coordinates[1][2];
  quad[1][0] = coordinates[2][0];
  quad[1][1] = coordinates[2][1];
  quad[1][2] = coordinates[2][2];
  quad[2][0] = coordinates[6][0];
  quad[2][1] = coordinates[6][1];
  quad[2][2] = coordinates[6][2];
  quad[3][0] = coordinates[5][0];
  quad[3][1] = coordinates[5][1];
  quad[3][2] = coordinates[5][2];
916

Matt Staten's avatar
Matt Staten committed
917
  quad_minimum_maximum_angle(min_max_angle, quad);
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
  if (min_max_angle[0] < min_angle)
  {
    min_angle = min_max_angle[0];
  }
  if (min_max_angle[1] > max_angle)
  {
    max_angle = min_max_angle[1];
  }

  quad[0][0] = coordinates[2][0];
  quad[0][1] = coordinates[2][1];
  quad[0][2] = coordinates[2][2];
  quad[1][0] = coordinates[3][0];
  quad[1][1] = coordinates[3][1];
  quad[1][2] = coordinates[3][2];
  quad[2][0] = coordinates[7][0];
  quad[2][1] = coordinates[7][1];
  quad[2][2] = coordinates[7][2];
  quad[3][0] = coordinates[6][0];
  quad[3][1] = coordinates[6][1];
  quad[3][2] = coordinates[6][2];
939

Matt Staten's avatar
Matt Staten committed
940
  quad_minimum_maximum_angle(min_max_angle, quad);
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
  if (min_max_angle[0] < min_angle)
  {
    min_angle = min_max_angle[0];
  }
  if (min_max_angle[1] > max_angle)
  {
    max_angle = min_max_angle[1];
  }

  quad[0][0] = coordinates[3][0];
  quad[0][1] = coordinates[3][1];
  quad[0][2] = coordinates[3][2];
  quad[1][0] = coordinates[0][0];
  quad[1][1] = coordinates[0][1];
  quad[1][2] = coordinates[0][2];
  quad[2][0] = coordinates[4][0];
  quad[2][1] = coordinates[4][1];
  quad[2][2] = coordinates[4][2];
  quad[3][0] = coordinates[7][0];
  quad[3][1] = coordinates[7][1];
  quad[3][2] = coordinates[7][2];
962

Matt Staten's avatar
Matt Staten committed
963
  quad_minimum_maximum_angle(min_max_angle, quad);
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
  if (min_max_angle[0] < min_angle)
  {
    min_angle = min_max_angle[0];
  }
  if (min_max_angle[1] > max_angle)
  {
    max_angle = min_max_angle[1];
  }

  quad[0][0] = coordinates[4][0];
  quad[0][1] = coordinates[4][1];
  quad[0][2] = coordinates[4][2];
  quad[1][0] = coordinates[5][0];
  quad[1][1] = coordinates[5][1];
  quad[1][2] = coordinates[5][2];
  quad[2][0] = coordinates[6][0];
  quad[2][1] = coordinates[6][1];
  quad[2][2] = coordinates[6][2];
  quad[3][0] = coordinates[7][0];
  quad[3][1] = coordinates[7][1];
  quad[3][2] = coordinates[7][2];
985

Matt Staten's avatar
Matt Staten committed
986
  quad_minimum_maximum_angle(min_max_angle, quad);
987
988
989
990
991
992
993
994
  if (min_max_angle[0] < min_angle)
  {
    min_angle = min_max_angle[0];
  }
  if (min_max_angle[1] > max_angle)
  {
    max_angle = min_max_angle[1];
  }
995

996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
  quad[0][0] = coordinates[3][0];
  quad[0][1] = coordinates[3][1];
  quad[0][2] = coordinates[3][2];
  quad[1][0] = coordinates[2][0];
  quad[1][1] = coordinates[2][1];
  quad[1][2] = coordinates[2][2];
  quad[2][0] = coordinates[1][0];
  quad[2][1] = coordinates[1][1];
  quad[2][2] = coordinates[1][2];
  quad[3][0] = coordinates[0][0];
  quad[3][1] = coordinates[0][1];
  quad[3][2] = coordinates[0][2];
1008

1009
1010
1011
1012
1013
1014
1015
1016
1017
  quad_minimum_maximum_angle(min_max_angle, quad);
  if (min_max_angle[0] < min_angle)
  {
    min_angle = min_max_angle[0];
  }
  if (min_max_angle[1] > max_angle)
  {
    max_angle = min_max_angle[1];
  }
1018

1019
1020
  double skew_max = (max_angle - 90.0) / 90.0;
  double skew_min = (90.0 - min_angle) / 90.0;
1021

1022
1023
  if (skew_max > skew_min)
  {
1024
    return skew_max;
1025
  }
1026
1027
1028
  return skew_min;
}

Clint Stimpson's avatar
Clint Stimpson committed
1029
1030
1031
1032
/*!
  skew of a hex

  Maximum ||cosA|| where A is the angle between edges at hex center.
1033
1034
 */
double hex_skew(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1035
1036
{
  VerdictVector node_pos[8];
1037
1038
  make_hex_nodes(coordinates, node_pos);

Clint Stimpson's avatar
Clint Stimpson committed
1039
  double skew_1, skew_2, skew_3;
1040
1041
1042
1043
1044
1045
1046

  VerdictVector efg1 = calc_hex_efg(1, node_pos);
  VerdictVector efg2 = calc_hex_efg(2, node_pos);
  VerdictVector efg3 = calc_hex_efg(3, node_pos);

  if (efg1.normalize() <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
1047
    return VERDICT_DBL_MAX;
1048
1049
1050
  }
  if (efg2.normalize() <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
1051
    return VERDICT_DBL_MAX;
1052
1053
1054
  }
  if (efg3.normalize() <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
1055
    return VERDICT_DBL_MAX;
1056
  }
Clint Stimpson's avatar
Clint Stimpson committed
1057

1058
1059
1060
  skew_1 = std::abs(VerdictVector::Dot(efg1, efg2));
  skew_2 = std::abs(VerdictVector::Dot(efg1, efg3));
  skew_3 = std::abs(VerdictVector::Dot(efg2, efg3));
Clint Stimpson's avatar
Clint Stimpson committed
1061

1062
  double skew = std::max({ skew_1, skew_2, skew_3 });
Clint Stimpson's avatar
Clint Stimpson committed
1063

1064
1065
1066
1067
1068
  if (skew > 0)
  {
    return (double)std::min(skew, VERDICT_DBL_MAX);
  }
  return (double)std::max(skew, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1069
1070
1071
1072
1073
1074
}

/*!
  taper of a hex

  Maximum ratio of lengths derived from opposite edges.
1075
1076
 */
double hex_taper(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1077
1078
{
  VerdictVector node_pos[8];
1079
  make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1080

1081
1082
1083
  VerdictVector efg1 = calc_hex_efg(1, node_pos);
  VerdictVector efg2 = calc_hex_efg(2, node_pos);
  VerdictVector efg3 = calc_hex_efg(3, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1084

1085
1086
1087
  VerdictVector efg12 = calc_hex_efg(12, node_pos);
  VerdictVector efg13 = calc_hex_efg(13, node_pos);
  VerdictVector efg23 = calc_hex_efg(23, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1088

1089
1090
1091
  double taper_1 = std::abs(safe_ratio(efg12.length(), std::min(efg1.length(), efg2.length())));
  double taper_2 = std::abs(safe_ratio(efg13.length(), std::min(efg1.length(), efg3.length())));
  double taper_3 = std::abs(safe_ratio(efg23.length(), std::min(efg2.length(), efg3.length())));
Clint Stimpson's avatar
Clint Stimpson committed
1092

1093
1094
1095
1096
1097
1098
1099
  double taper = std::max({ taper_1, taper_2, taper_3 });

  if (taper > 0)
  {
    return (double)std::min(taper, VERDICT_DBL_MAX);
  }
  return (double)std::max(taper, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1100
1101
1102
1103
}

/*!
  volume of a hex
1104
1105
  Split the hex into 24 tets.
  sum the volume of each tet.
1106
1107
 */
double hex_volume(int num_nodes, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1108
{
1109
  double volume = 0.0;
1110

1111
1112
1113
  if (num_nodes > 9)
  {
    int(*subtet_conn_array)[4];
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
    int num_subtets = 0;
    if (27 == num_nodes)
    {
      num_subtets = 48;
      subtet_conn_array = hex27_subtet_conn;
    }
    else if (20 == num_nodes)
    {
      num_subtets = 36;
      subtet_conn_array = hex20_subtet_conn;
    }
    else
1126
    {
1127
      return 0.0;
1128
    }
1129

1130
    VerdictVector aux_node = hex20_auxillary_node_coordinate(coordinates);
1131

1132
1133
1134
1135
1136
1137
    for (int k = 0; k < num_subtets; k++)
    {
      VerdictVector v1(
        coordinates[subtet_conn_array[k][1]][0] - coordinates[subtet_conn_array[k][0]][0],
        coordinates[subtet_conn_array[k][1]][1] - coordinates[subtet_conn_array[k][0]][1],
        coordinates[subtet_conn_array[k][1]][2] - coordinates[subtet_conn_array[k][0]][2]);
1138

1139
1140
1141
1142
      VerdictVector v2(
        coordinates[subtet_conn_array[k][2]][0] - coordinates[subtet_conn_array[k][0]][0],
        coordinates[subtet_conn_array[k][2]][1] - coordinates[subtet_conn_array[k][0]][1],
        coordinates[subtet_conn_array[k][2]][2] - coordinates[subtet_conn_array[k][0]][2]);
1143
1144

      VerdictVector v3(aux_node.x() - coordinates[subtet_conn_array[k][0]][0],
1145
        aux_node.y() - coordinates[subtet_conn_array[k][0]][1],
1146
        aux_node.z() - coordinates[subtet_conn_array[k][0]][2]);
1147

1148
      volume += compute_tet_volume(v1, v2, v3);
1149
    }
1150
  }
1151
  else
1152
  {
1153
1154
1155
1156

    VerdictVector node_pos[8];
    make_hex_nodes(coordinates, node_pos);

1157
1158
1159
1160
1161
1162
1163
1164
    // define the nodes of each face of the hex
    int faces[6][4] = {
      { 0, 1, 5, 4 },
      { 1, 2, 6, 5 },
      { 2, 3, 7, 6 },
      { 3, 0, 4, 7 },
      { 3, 2, 1, 0 },
      { 4, 5, 6, 7 },
1165
1166
    };

1167
    // calculate the center of each face
1168
1169
1170
    VerdictVector fcenter[6];
    for (int f = 0; f < 6; f++)
    {
1171
1172
1173
      fcenter[f] = (node_pos[faces[f][0]] + node_pos[faces[f][1]] + node_pos[faces[f][2]] +
                     node_pos[faces[f][3]]) *
        0.25;
1174
1175
    }

1176
1177
1178
1179
1180
    // calculate the center of the hex
    VerdictVector hcenter = (node_pos[0] + node_pos[1] + node_pos[2] + node_pos[3] + node_pos[4] +
                              node_pos[5] + node_pos[6] + node_pos[7]) *
      0.125;

1181
    for (int i = 0; i < 6; i++)
1182
    {
1183
1184
      // for each face calculate the vectors from the nodes and center of the face to the center of
      // the hex. These vectors define three of the sides of the tets.
1185
      VerdictVector side[5];
1186
      side[4] = hcenter - fcenter[i]; // vector from center of face to center of hex.
1187
1188
      for (int s = 0; s < 4; s++)
      {
1189
        side[s] = hcenter - node_pos[faces[i][s]]; // vector from face node to center of hex.
1190
1191
      }

1192
1193
1194
1195
1196
1197
      // for each of the four tets that originate from this face.
      // calculate the volume of the tet.
      // This is done by calculating the triple product of three vectors that originate from a
      // corner node of the tet. This is also the jacobain at the corner node of the tet. The volume
      // is 1/6 of jacobian at a corner node.
      for (int j = 0; j < 3; j++) // first three tets
1198
1199
1200
      {
        volume += (double)(VerdictVector::Dot(side[4], (side[j + 1] * side[j])) / 6.0);
      }
1201
      volume += (double)(VerdictVector::Dot(side[4], (side[0] * side[3])) / 6.0); // fourth tet.
1202
1203
    }
  }
Clint Stimpson's avatar
Clint Stimpson committed
1204

1205
  if (volume > 0)
1206
  {
1207
    return (double)std::min(volume, VERDICT_DBL_MAX);
1208
  }
1209
  return (double)std::max(volume, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1210
1211
1212
1213
1214
1215
}

/*!
  stretch of a hex

  sqrt(3) * minimum edge length / maximum diagonal length
1216
1217
 */
double hex_stretch(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1218
{
1219
1220
1221
1222
  double min_edge = hex_edge_length(0, coordinates);
  double max_diag = diag_length(1, coordinates);

  double stretch = sqrt3 * safe_ratio(min_edge, max_diag);
Clint Stimpson's avatar
Clint Stimpson committed
1223

1224
1225
1226
1227
1228
  if (stretch > 0)
  {
    return (double)std::min(stretch, VERDICT_DBL_MAX);
  }
  return (double)std::max(stretch, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1229
1230
1231
1232
}

/*!
  diagonal ratio of a hex
1233

Clint Stimpson's avatar
Clint Stimpson committed
1234
  Minimum diagonal length / maximum diagonal length
1235
1236
 */
double hex_diagonal(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1237
{
1238
1239
1240
1241
  double min_diag = diag_length(0, coordinates);
  double max_diag = diag_length(1, coordinates);

  double diagonal = safe_ratio(min_diag, max_diag);
Clint Stimpson's avatar
Clint Stimpson committed
1242

1243
1244
1245
1246
1247
  if (diagonal > 0)
  {
    return (double)std::min(diagonal, VERDICT_DBL_MAX);
  }
  return (double)std::max(diagonal, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1248
1249
1250
1251
1252
1253
1254
}

#define SQR(x) ((x) * (x))

/*!
  dimension of a hex

1255
  Pronto-specific characteristic length for stable time step calculation.
Clint Stimpson's avatar
Clint Stimpson committed
1256
1257
  Char_length = Volume / 2 grad Volume
*/
1258
double hex_dimension(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
{
  double gradop[9][4];

  double x1 = coordinates[0][0];
  double x2 = coordinates[1][0];
  double x3 = coordinates[2][0];
  double x4 = coordinates[3][0];
  double x5 = coordinates[4][0];
  double x6 = coordinates[5][0];
  double x7 = coordinates[6][0];
  double x8 = coordinates[7][0];

  double y1 = coordinates[0][1];
  double y2 = coordinates[1][1];
  double y3 = coordinates[2][1];
  double y4 = coordinates[3][1];
  double y5 = coordinates[4][1];
  double y6 = coordinates[5][1];
  double y7 = coordinates[6][1];
  double y8 = coordinates[7][1];

  double z1 = coordinates[0][2];
  double z2 = coordinates[1][2];
  double z3 = coordinates[2][2];
  double z4 = coordinates[3][2];
  double z5 = coordinates[4][2];
  double z6 = coordinates[5][2];
  double z7 = coordinates[6][2];
  double z8 = coordinates[7][2];

  double z24 = z2 - z4;
  double z52 = z5 - z2;
  double z45 = z4 - z5;
1292
1293
1294
1295
  gradop[1][1] = (y2 * (z6 - z3 - z45) + y3 * z24 + y4 * (z3 - z8 - z52) + y5 * (z8 - z6 - z24) +
                   y6 * z52 + y8 * z45) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1296
1297
1298
  double z31 = z3 - z1;
  double z63 = z6 - z3;
  double z16 = z1 - z6;
1299
1300
1301
  gradop[2][1] = (y3 * (z7 - z4 - z16) + y4 * z31 + y1 * (z4 - z5 - z63) + y6 * (z5 - z7 - z31) +
                   y7 * z63 + y5 * z16) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1302
1303
1304
1305

  double z42 = z4 - z2;
  double z74 = z7 - z4;
  double z27 = z2 - z7;
1306
1307
1308
1309
  gradop[3][1] = (y4 * (z8 - z1 - z27) + y1 * z42 + y2 * (z1 - z6 - z74) + y7 * (z6 - z8 - z42) +
                   y8 * z74 + y6 * z27) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1310
1311
1312
  double z13 = z1 - z3;
  double z81 = z8 - z1;
  double z38 = z3 - z8;
1313
1314
1315
1316
  gradop[4][1] = (y1 * (z5 - z2 - z38) + y2 * z13 + y3 * (z2 - z7 - z81) + y8 * (z7 - z5 - z13) +
                   y5 * z81 + y7 * z38) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1317
1318
1319
  double z86 = z8 - z6;
  double z18 = z1 - z8;
  double z61 = z6 - z1;
1320
1321
1322
1323
  gradop[5][1] = (y8 * (z4 - z7 - z61) + y7 * z86 + y6 * (z7 - z2 - z18) + y1 * (z2 - z4 - z86) +
                   y4 * z18 + y2 * z61) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1324
1325
1326
  double z57 = z5 - z7;
  double z25 = z2 - z5;
  double z72 = z7 - z2;
1327
1328
1329
1330
  gradop[6][1] = (y5 * (z1 - z8 - z72) + y8 * z57 + y7 * (z8 - z3 - z25) + y2 * (z3 - z1 - z57) +
                   y1 * z25 + y3 * z72) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1331
1332
1333
  double z68 = z6 - z8;
  double z36 = z3 - z6;
  double z83 = z8 - z3;
1334
1335
1336
1337
  gradop[7][1] = (y6 * (z2 - z5 - z83) + y5 * z68 + y8 * (z5 - z4 - z36) + y3 * (z4 - z2 - z68) +
                   y2 * z36 + y4 * z83) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1338
1339
1340
  double z75 = z7 - z5;
  double z47 = z4 - z7;
  double z54 = z5 - z4;
1341
1342
1343
1344
  gradop[8][1] = (y7 * (z3 - z6 - z54) + y6 * z75 + y5 * (z6 - z1 - z47) + y4 * (z1 - z3 - z75) +
                   y3 * z47 + y1 * z54) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1345
1346
1347
  double x24 = x2 - x4;
  double x52 = x5 - x2;
  double x45 = x4 - x5;
1348
1349
1350
  gradop[1][2] = (z2 * (x6 - x3 - x45) + z3 * x24 + z4 * (x3 - x8 - x52) + z5 * (x8 - x6 - x24) +
                   z6 * x52 + z8 * x45) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1351
1352
1353
1354

  double x31 = x3 - x1;
  double x63 = x6 - x3;
  double x16 = x1 - x6;
1355
1356
1357
  gradop[2][2] = (z3 * (x7 - x4 - x16) + z4 * x31 + z1 * (x4 - x5 - x63) + z6 * (x5 - x7 - x31) +
                   z7 * x63 + z5 * x16) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1358
1359
1360
1361

  double x42 = x4 - x2;
  double x74 = x7 - x4;
  double x27 = x2 - x7;
1362
1363
1364
  gradop[3][2] = (z4 * (x8 - x1 - x27) + z1 * x42 + z2 * (x1 - x6 - x74) + z7 * (x6 - x8 - x42) +
                   z8 * x74 + z6 * x27) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1365
1366
1367
1368

  double x13 = x1 - x3;
  double x81 = x8 - x1;
  double x38 = x3 - x8;
1369
1370
1371
  gradop[4][2] = (z1 * (x5 - x2 - x38) + z2 * x13 + z3 * (x2 - x7 - x81) + z8 * (x7 - x5 - x13) +
                   z5 * x81 + z7 * x38) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1372
1373
1374
1375

  double x86 = x8 - x6;
  double x18 = x1 - x8;
  double x61 = x6 - x1;
1376
1377
1378
1379
  gradop[5][2] = (z8 * (x4 - x7 - x61) + z7 * x86 + z6 * (x7 - x2 - x18) + z1 * (x2 - x4 - x86) +
                   z4 * x18 + z2 * x61) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1380
1381
1382
  double x57 = x5 - x7;
  double x25 = x2 - x5;
  double x72 = x7 - x2;
1383
1384
1385
  gradop[6][2] = (z5 * (x1 - x8 - x72) + z8 * x57 + z7 * (x8 - x3 - x25) + z2 * (x3 - x1 - x57) +
                   z1 * x25 + z3 * x72) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1386
1387
1388
1389

  double x68 = x6 - x8;
  double x36 = x3 - x6;
  double x83 = x8 - x3;
1390
1391
1392
  gradop[7][2] = (z6 * (x2 - x5 - x83) + z5 * x68 + z8 * (x5 - x4 - x36) + z3 * (x4 - x2 - x68) +
                   z2 * x36 + z4 * x83) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1393
1394
1395
1396

  double x75 = x7 - x5;
  double x47 = x4 - x7;
  double x54 = x5 - x4;
1397
1398
1399
1400
  gradop[8][2] = (z7 * (x3 - x6 - x54) + z6 * x75 + z5 * (x6 - x1 - x47) + z4 * (x1 - x3 - x75) +
                   z3 * x47 + z1 * x54) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1401
1402
1403
  double y24 = y2 - y4;
  double y52 = y5 - y2;
  double y45 = y4 - y5;
1404
1405
1406
1407
  gradop[1][3] = (x2 * (y6 - y3 - y45) + x3 * y24 + x4 * (y3 - y8 - y52) + x5 * (y8 - y6 - y24) +
                   x6 * y52 + x8 * y45) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1408
1409
1410
  double y31 = y3 - y1;
  double y63 = y6 - y3;
  double y16 = y1 - y6;
1411
1412
1413
1414
  gradop[2][3] = (x3 * (y7 - y4 - y16) + x4 * y31 + x1 * (y4 - y5 - y63) + x6 * (y5 - y7 - y31) +
                   x7 * y63 + x5 * y16) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1415
1416
1417
  double y42 = y4 - y2;
  double y74 = y7 - y4;
  double y27 = y2 - y7;
1418
1419
1420
1421
  gradop[3][3] = (x4 * (y8 - y1 - y27) + x1 * y42 + x2 * (y1 - y6 - y74) + x7 * (y6 - y8 - y42) +
                   x8 * y74 + x6 * y27) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1422
1423
1424
  double y13 = y1 - y3;
  double y81 = y8 - y1;
  double y38 = y3 - y8;
1425
1426
1427
1428
  gradop[4][3] = (x1 * (y5 - y2 - y38) + x2 * y13 + x3 * (y2 - y7 - y81) + x8 * (y7 - y5 - y13) +
                   x5 * y81 + x7 * y38) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1429
1430
1431
  double y86 = y8 - y6;
  double y18 = y1 - y8;
  double y61 = y6 - y1;
1432
1433
1434
1435
  gradop[5][3] = (x8 * (y4 - y7 - y61) + x7 * y86 + x6 * (y7 - y2 - y18) + x1 * (y2 - y4 - y86) +
                   x4 * y18 + x2 * y61) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1436
1437
1438
  double y57 = y5 - y7;
  double y25 = y2 - y5;
  double y72 = y7 - y2;
1439
1440
1441
1442
  gradop[6][3] = (x5 * (y1 - y8 - y72) + x8 * y57 + x7 * (y8 - y3 - y25) + x2 * (y3 - y1 - y57) +
                   x1 * y25 + x3 * y72) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1443
1444
1445
  double y68 = y6 - y8;
  double y36 = y3 - y6;
  double y83 = y8 - y3;
1446
1447
1448
1449
  gradop[7][3] = (x6 * (y2 - y5 - y83) + x5 * y68 + x8 * (y5 - y4 - y36) + x3 * (y4 - y2 - y68) +
                   x2 * y36 + x4 * y83) /
    12.0;

Clint Stimpson's avatar
Clint Stimpson committed
1450
1451
1452
  double y75 = y7 - y5;
  double y47 = y4 - y7;
  double y54 = y5 - y4;
1453
1454
1455
  gradop[8][3] = (x7 * (y3 - y6 - y54) + x6 * y75 + x5 * (y6 - y1 - y47) + x4 * (y1 - y3 - y75) +
                   x3 * y47 + x1 * y54) /
    12.0;
Clint Stimpson's avatar
Clint Stimpson committed
1456
1457

  //     calculate element volume and characteristic element aspect ratio
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
  //     (used in time step and hourglass control) -

  double volume = coordinates[0][0] * gradop[1][1] + coordinates[1][0] * gradop[2][1] +
    coordinates[2][0] * gradop[3][1] + coordinates[3][0] * gradop[4][1] +
    coordinates[4][0] * gradop[5][1] + coordinates[5][0] * gradop[6][1] +
    coordinates[6][0] * gradop[7][1] + coordinates[7][0] * gradop[8][1];
  double aspect = .5 * SQR(volume) /
    (SQR(gradop[1][1]) + SQR(gradop[2][1]) + SQR(gradop[3][1]) + SQR(gradop[4][1]) +
      SQR(gradop[5][1]) + SQR(gradop[6][1]) + SQR(gradop[7][1]) + SQR(gradop[8][1]) +
      SQR(gradop[1][2]) + SQR(gradop[2][2]) + SQR(gradop[3][2]) + SQR(gradop[4][2]) +
      SQR(gradop[5][2]) + SQR(gradop[6][2]) + SQR(gradop[7][2]) + SQR(gradop[8][2]) +
      SQR(gradop[1][3]) + SQR(gradop[2][3]) + SQR(gradop[3][3]) + SQR(gradop[4][3]) +
      SQR(gradop[5][3]) + SQR(gradop[6][3]) + SQR(gradop[7][3]) + SQR(gradop[8][3]));

1472
  return (double)std::sqrt(aspect);
Clint Stimpson's avatar
Clint Stimpson committed
1473
1474
1475
1476
1477
1478
}

/*!
  oddy of a hex

  General distortion measure based on left Cauchy-Green Tensor
1479
1480
 */
double hex_oddy(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1481
1482
1483
{
  double oddy = 0.0, current_oddy;
  VerdictVector xxi, xet, xze;
1484

Clint Stimpson's avatar
Clint Stimpson committed
1485
  VerdictVector node_pos[8];
1486
1487
  make_hex_nodes(coordinates, node_pos);

Matt Staten's avatar
Matt Staten committed
1488
1489
1490
  xxi = calc_hex_efg(1, node_pos);
  xet = calc_hex_efg(2, node_pos);
  xze = calc_hex_efg(3, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1491

1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
    coordinates[1][2] - coordinates[0][2]);

  xet.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
    coordinates[3][2] - coordinates[0][2]);

  xze.set(coordinates[4][0] - coordinates[0][0], coordinates[4][1] - coordinates[0][1],
    coordinates[4][2] - coordinates[0][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
    coordinates[2][2] - coordinates[1][2]);

  xet.set(coordinates[0][0] - coordinates[1][0], coordinates[0][1] - coordinates[1][1],
    coordinates[0][2] - coordinates[1][2]);

  xze.set(coordinates[5][0] - coordinates[1][0], coordinates[5][1] - coordinates[1][1],
    coordinates[5][2] - coordinates[1][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
    coordinates[3][2] - coordinates[2][2]);

  xet.set(coordinates[1][0] - coordinates[2][0], coordinates[1][1] - coordinates[2][1],
    coordinates[1][2] - coordinates[2][2]);

  xze.set(coordinates[6][0] - coordinates[2][0], coordinates[6][1] - coordinates[2][1],
    coordinates[6][2] - coordinates[2][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[0][0] - coordinates[3][0], coordinates[0][1] - coordinates[3][1],
    coordinates[0][2] - coordinates[3][2]);

  xet.set(coordinates[2][0] - coordinates[3][0], coordinates[2][1] - coordinates[3][1],
    coordinates[2][2] - coordinates[3][2]);

  xze.set(coordinates[7][0] - coordinates[3][0], coordinates[7][1] - coordinates[3][1],
    coordinates[7][2] - coordinates[3][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[7][0] - coordinates[4][0], coordinates[7][1] - coordinates[4][1],
    coordinates[7][2] - coordinates[4][2]);

  xet.set(coordinates[5][0] - coordinates[4][0], coordinates[5][1] - coordinates[4][1],
    coordinates[5][2] - coordinates[4][2]);

  xze.set(coordinates[0][0] - coordinates[4][0], coordinates[0][1] - coordinates[4][1],
    coordinates[0][2] - coordinates[4][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[4][0] - coordinates[5][0], coordinates[4][1] - coordinates[5][1],
    coordinates[4][2] - coordinates[5][2]);

  xet.set(coordinates[6][0] - coordinates[5][0], coordinates[6][1] - coordinates[5][1],
    coordinates[6][2] - coordinates[5][2]);

  xze.set(coordinates[1][0] - coordinates[5][0], coordinates[1][1] - coordinates[5][1],
    coordinates[1][2] - coordinates[5][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[5][0] - coordinates[6][0], coordinates[5][1] - coordinates[6][1],
    coordinates[5][2] - coordinates[6][2]);

  xet.set(coordinates[7][0] - coordinates[6][0], coordinates[7][1] - coordinates[6][1],
    coordinates[7][2] - coordinates[6][2]);

  xze.set(coordinates[2][0] - coordinates[6][0], coordinates[2][1] - coordinates[6][1],
    coordinates[2][2] - coordinates[6][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  xxi.set(coordinates[6][0] - coordinates[7][0], coordinates[6][1] - coordinates[7][1],
    coordinates[6][2] - coordinates[7][2]);

  xet.set(coordinates[4][0] - coordinates[7][0], coordinates[4][1] - coordinates[7][1],
    coordinates[4][2] - coordinates[7][2]);

  xze.set(coordinates[3][0] - coordinates[7][0], coordinates[3][1] - coordinates[7][1],
    coordinates[3][2] - coordinates[7][2]);

  current_oddy = oddy_comp(xxi, xet, xze);
  if (current_oddy > oddy)
  {
    oddy = current_oddy;
  }

  if (oddy > 0)
  {
    return (double)std::min(oddy, VERDICT_DBL_MAX);
  }
  return (double)std::max(oddy, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1623
1624
1625
1626
1627
}

/*!
   the average Frobenius aspect of a hex

1628
   NB (P. Pebay 01/20/07):
Clint Stimpson's avatar
Clint Stimpson committed
1629
1630
     this function is calculated by averaging the 8 Frobenius aspects at
     each corner of the hex, when the reference corner is right isosceles.
1631
1632
 */
double hex_med_aspect_frobenius(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1633
1634
{
  VerdictVector node_pos[8];
1635
  make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1636
1637
1638
1639
1640
1641
1642
1643

  VerdictVector xxi, xet, xze;

  // J(0,0,0):
  xxi = node_pos[1] - node_pos[0];
  xet = node_pos[3] - node_pos[0];
  xze = node_pos[4] - node_pos[0];

1644
  double med_aspect_frobenius = condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1645
1646
1647
1648
1649
1650

  // J(1,0,0):
  xxi = node_pos[2] - node_pos[1];
  xet = node_pos[0] - node_pos[1];
  xze = node_pos[5] - node_pos[1];

1651
  med_aspect_frobenius += condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1652
1653
1654
1655
1656
1657

  // J(1,1,0):
  xxi = node_pos[3] - node_pos[2];
  xet = node_pos[1] - node_pos[2];
  xze = node_pos[6] - node_pos[2];

1658
  med_aspect_frobenius += condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1659
1660
1661
1662
1663
1664

  // J(0,1,0):
  xxi = node_pos[0] - node_pos[3];
  xet = node_pos[2] - node_pos[3];
  xze = node_pos[7] - node_pos[3];

1665
  med_aspect_frobenius += condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1666
1667
1668
1669
1670
1671

  // J(0,0,1):
  xxi = node_pos[7] - node_pos[4];
  xet = node_pos[5] - node_pos[4];
  xze = node_pos[0] - node_pos[4];

1672
  med_aspect_frobenius += condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1673
1674
1675
1676
1677
1678

  // J(1,0,1):
  xxi = node_pos[4] - node_pos[5];
  xet = node_pos[6] - node_pos[5];
  xze = node_pos[1] - node_pos[5];

1679
  med_aspect_frobenius += condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1680
1681
1682
1683
1684
1685

  // J(1,1,1):
  xxi = node_pos[5] - node_pos[6];
  xet = node_pos[7] - node_pos[6];
  xze = node_pos[2] - node_pos[6];

1686
  med_aspect_frobenius += condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1687
1688
1689
1690
1691
1692

  // J(1,1,1):
  xxi = node_pos[6] - node_pos[7];
  xet = node_pos[4] - node_pos[7];
  xze = node_pos[3] - node_pos[7];

1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
  med_aspect_frobenius += condition_comp(xxi, xet, xze);

  if (med_aspect_frobenius >= VERDICT_DBL_MAX)
  {
    return VERDICT_DBL_MAX;
  }
  if (med_aspect_frobenius <= -VERDICT_DBL_MAX)
  {
    return -VERDICT_DBL_MAX;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1703

1704
  return med_aspect_frobenius / 24.;
Clint Stimpson's avatar
Clint Stimpson committed
1705
1706
1707
1708
1709
1710
}

/*!
  maximum Frobenius condition number of a hex

  Maximum Frobenius condition number of the Jacobian matrix at 8 corners
1711
   NB (P. Pebay 01/25/07):
Clint Stimpson's avatar
Clint Stimpson committed
1712
1713
     this function is calculated by taking the maximum of the 8 Frobenius aspects at
     each corner of the hex, when the reference corner is right isosceles.
1714
1715
 */
double hex_max_aspect_frobenius(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1716
1717
{
  VerdictVector node_pos[8];
1718
  make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1719
1720
1721
1722
1723
1724
1725
1726

  VerdictVector xxi, xet, xze;

  // J(0,0,0):
  xxi = node_pos[1] - node_pos[0];
  xet = node_pos[3] - node_pos[0];
  xze = node_pos[4] - node_pos[0];

1727
  double condition = condition_comp(xxi, xet, xze);
Clint Stimpson's avatar
Clint Stimpson committed
1728
1729
1730
1731
1732
1733

  // J(1,0,0):
  xxi = node_pos[2] - node_pos[1];
  xet = node_pos[0] - node_pos[1];
  xze = node_pos[5] - node_pos[1];

1734
1735
1736
1737
1738
  double current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1739
1740
1741
1742
1743
1744

  // J(1,1,0):
  xxi = node_pos[3] - node_pos[2];
  xet = node_pos[1] - node_pos[2];
  xze = node_pos[6] - node_pos[2];

1745
1746
1747
1748
1749
  current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1750
1751
1752
1753
1754
1755

  // J(0,1,0):
  xxi = node_pos[0] - node_pos[3];
  xet = node_pos[2] - node_pos[3];
  xze = node_pos[7] - node_pos[3];

1756
1757
1758
1759
1760
  current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1761
1762
1763
1764
1765
1766

  // J(0,0,1):
  xxi = node_pos[7] - node_pos[4];
  xet = node_pos[5] - node_pos[4];
  xze = node_pos[0] - node_pos[4];

1767
1768
1769
1770
1771
  current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1772
1773
1774
1775
1776
1777

  // J(1,0,1):
  xxi = node_pos[4] - node_pos[5];
  xet = node_pos[6] - node_pos[5];
  xze = node_pos[1] - node_pos[5];

1778
1779
1780
1781
1782
  current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1783
1784
1785
1786
1787
1788

  // J(1,1,1):
  xxi = node_pos[5] - node_pos[6];
  xet = node_pos[7] - node_pos[6];
  xze = node_pos[2] - node_pos[6];

1789
1790
1791
1792
1793
  current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1794
1795
1796
1797
1798
1799

  // J(1,1,1):
  xxi = node_pos[6] - node_pos[7];
  xet = node_pos[4] - node_pos[7];
  xze = node_pos[3] - node_pos[7];

1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
  current_condition = condition_comp(xxi, xet, xze);
  if (current_condition > condition)
  {
    condition = current_condition;
  }

  if (condition >= VERDICT_DBL_MAX)
  {
    return VERDICT_DBL_MAX;
  }
  if (condition <= -VERDICT_DBL_MAX)
  {
    return -VERDICT_DBL_MAX;
  }
Clint Stimpson's avatar
Clint Stimpson committed
1814

1815
  return condition / 3.;
Clint Stimpson's avatar
Clint Stimpson committed
1816
1817
1818
1819
}

/*!
  The maximum Frobenius condition of a hex, a.k.a. condition
1820
  NB (P. Pebay 01/25/07):
Clint Stimpson's avatar
Clint Stimpson committed
1821
1822
1823
     this method is maintained for backwards compatibility only.
     It will become deprecated at some point.

1824
1825
 */
double hex_condition(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1826
{
Matt Staten's avatar
Matt Staten committed
1827
  return hex_max_aspect_frobenius(8, coordinates);
Clint Stimpson's avatar
Clint Stimpson committed
1828
1829
1830
1831
1832
1833
}

/*!
  jacobian of a hex

  Minimum pointwise volume of local map at 8 corners & center of hex
1834
1835
 */
double hex_jacobian(int num_nodes, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1836
{
1837
  if (num_nodes == 27)
Clint Stimpson's avatar
Clint Stimpson committed
1838
1839
1840
1841
1842
  {
    double dhdr[27];
    double dhds[27];
    double dhdt[27];
    double min_determinant = VERDICT_DBL_MAX;
Clint Stimpson's avatar
Clint Stimpson committed
1843

1844
    for (int i = 0; i < 27; i++)
Clint Stimpson's avatar
Clint Stimpson committed
1845
1846
    {
      HEX27_gradients_of_the_shape_functions_for_RST(HEX27_node_local_coord[i], dhdr, dhds, dhdt);
1847
      double jacobian[3][3] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
Clint Stimpson's avatar
Clint Stimpson committed
1848

1849
      for (int j = 0; j < 27; j++)
Clint Stimpson's avatar
Clint Stimpson committed
1850
      {
1851
1852
1853
1854
1855
1856
1857
1858
1859
        jacobian[0][0] += coordinates[j][0] * dhdr[j];
        jacobian[0][1] += coordinates[j][0] * dhds[j];
        jacobian[0][2] += coordinates[j][0] * dhdt[j];
        jacobian[1][0] += coordinates[j][1] * dhdr[j];
        jacobian[1][1] += coordinates[j][1] * dhds[j];
        jacobian[1][2] += coordinates[j][1] * dhdt[j];
        jacobian[2][0] += coordinates[j][2] * dhdr[j];
        jacobian[2][1] += coordinates[j][2] * dhds[j];
        jacobian[2][2] += coordinates[j][2] * dhdt[j];
Clint Stimpson's avatar
Clint Stimpson committed
1860
      }
1861
1862
      double det = VerdictVector::Dot(
        (VerdictVector(jacobian[0]) * VerdictVector(jacobian[1])), VerdictVector(jacobian[2]));
Matt Staten's avatar
Matt Staten committed
1863
      min_determinant = std::min(det, min_determinant);
Clint Stimpson's avatar
Clint Stimpson committed
1864
1865
1866
1867
1868
1869
    }
    return min_determinant;
  }
  else
  {
    VerdictVector node_pos[8];
1870
    make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1871

Clint Stimpson's avatar
Clint Stimpson committed
1872
1873
1874
    double jacobian = VERDICT_DBL_MAX;
    double current_jacobian;
    VerdictVector xxi, xet, xze;
Clint Stimpson's avatar
Clint Stimpson committed
1875

1876
1877
1878
    xxi = calc_hex_efg(1, node_pos);
    xet = calc_hex_efg(2, node_pos);
    xze = calc_hex_efg(3, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
1879

Matt Staten's avatar
Matt Staten committed
1880
    current_jacobian = VerdictVector::Dot(xxi, (xet * xze)) / 64.0;
1881
1882
1883
1884
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1885

Clint Stimpson's avatar
Clint Stimpson committed
1886
1887
1888
1889
    // J(0,0,0):
    xxi = node_pos[1] - node_pos[0];
    xet = node_pos[3] - node_pos[0];
    xze = node_pos[4] - node_pos[0];
Clint Stimpson's avatar
Clint Stimpson committed
1890

Matt Staten's avatar
Matt Staten committed
1891
    current_jacobian = VerdictVector::Dot(xxi, (xet * xze));
1892
1893
1894
1895
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1896

Clint Stimpson's avatar
Clint Stimpson committed
1897
1898
1899
1900
    // J(1,0,0):
    xxi = node_pos[2] - node_pos[1];
    xet = node_pos[0] - node_pos[1];
    xze = node_pos[5] - node_pos[1];
Clint Stimpson's avatar
Clint Stimpson committed
1901

Matt Staten's avatar
Matt Staten committed
1902
    current_jacobian = VerdictVector::Dot(xxi, (xet * xze));
1903
1904
1905
1906
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1907

Clint Stimpson's avatar
Clint Stimpson committed
1908
1909
1910
1911
    // J(1,1,0):
    xxi = node_pos[3] - node_pos[2];
    xet = node_pos[1] - node_pos[2];
    xze = node_pos[6] - node_pos[2];
Clint Stimpson's avatar
Clint Stimpson committed
1912

Matt Staten's avatar
Matt Staten committed
1913
    current_jacobian = VerdictVector::Dot(xxi, (xet * xze));
1914
1915
1916
1917
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1918

Clint Stimpson's avatar
Clint Stimpson committed
1919
1920
1921
1922
    // J(0,1,0):
    xxi = node_pos[0] - node_pos[3];
    xet = node_pos[2] - node_pos[3];
    xze = node_pos[7] - node_pos[3];
Clint Stimpson's avatar
Clint Stimpson committed
1923

Matt Staten's avatar
Matt Staten committed
1924
    current_jacobian = VerdictVector::Dot(xxi, (xet * xze));
1925
1926
1927
1928
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1929

Clint Stimpson's avatar
Clint Stimpson committed
1930
1931
1932
1933
    // J(0,0,1):
    xxi = node_pos[7] - node_pos[4];
    xet = node_pos[5] - node_pos[4];
    xze = node_pos[0] - node_pos[4];
Clint Stimpson's avatar
Clint Stimpson committed
1934

Clint Stimpson's avatar
Clint Stimpson committed
1935
    current_jacobian = xxi % (xet * xze);
1936
1937
1938
1939
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1940

Clint Stimpson's avatar
Clint Stimpson committed
1941
1942
1943
1944
    // J(1,0,1):
    xxi = node_pos[4] - node_pos[5];
    xet = node_pos[6] - node_pos[5];
    xze = node_pos[1] - node_pos[5];
Clint Stimpson's avatar
Clint Stimpson committed
1945

Clint Stimpson's avatar
Clint Stimpson committed
1946
    current_jacobian = xxi % (xet * xze);
1947
1948
1949
1950
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1951
1952
1953
1954
1955

    // J(1,1,1):
    xxi = node_pos[5] - node_pos[6];
    xet = node_pos[7] - node_pos[6];
    xze = node_pos[2] - node_pos[6];
Clint Stimpson's avatar
Clint Stimpson committed
1956

Clint Stimpson's avatar
Clint Stimpson committed
1957
    current_jacobian = xxi % (xet * xze);
1958
1959
1960
1961
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1962

Clint Stimpson's avatar
Clint Stimpson committed
1963
1964
1965
1966
1967
1968
    // J(0,1,1):
    xxi = node_pos[6] - node_pos[7];
    xet = node_pos[4] - node_pos[7];
    xze = node_pos[3] - node_pos[7];

    current_jacobian = xxi % (xet * xze);
1969
1970
1971
1972
    if (current_jacobian < jacobian)
    {
      jacobian = current_jacobian;
    }
Clint Stimpson's avatar
Clint Stimpson committed
1973

1974
1975
1976
1977
1978
    if (jacobian > 0)
    {
      return (double)std::min(jacobian, VERDICT_DBL_MAX);
    }
    return (double)std::max(jacobian, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
1979
  }
Clint Stimpson's avatar
Clint Stimpson committed
1980
1981
1982
1983
1984
1985
}

/*!
  scaled jacobian of a hex

  Minimum Jacobian divided by the lengths of the 3 edge vectors
1986
1987
 */
double hex_scaled_jacobian(int num_nodes, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
1988
{
1989
1990
  double jacobi, min_norm_jac = VERDICT_DBL_MAX;

1991
#if 0
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
  if(num_nodes == 27)
  {
    double dhdr[27];
    double dhds[27];
    double dhdt[27];

    for(int i=0; i<27; i++)
    {
      HEX27_gradients_of_the_shape_functions_for_RST(HEX27_node_local_coord[i], dhdr, dhds, dhdt);
      double jacobian[3][3] = {{0,0,0},{0,0,0},{0,0,0}};

      for(int j=0; j<27; j++)
      {
        jacobian[0][0]+=coordinates[j][0]*dhdr[j];
        jacobian[1][0]+=coordinates[j][0]*dhds[j];
        jacobian[2][0]+=coordinates[j][0]*dhdt[j];
        jacobian[0][1]+=coordinates[j][1]*dhdr[j];
        jacobian[1][1]+=coordinates[j][1]*dhds[j];
        jacobian[2][1]+=coordinates[j][1]*dhdt[j];
        jacobian[0][2]+=coordinates[j][2]*dhdr[j];
        jacobian[1][2]+=coordinates[j][2]*dhds[j];
        jacobian[2][2]+=coordinates[j][2]*dhdt[j];
      }
      VerdictVector xxi(jacobian[0]);
      VerdictVector xet(jacobian[1]);
      VerdictVector xze(jacobian[2]);
      jacobi = xxi % ( xet * xze );
2019
      double scale = std::sqrt(xxi.length_squared() * xet.length_squared() * xze.length_squared());
2020
2021
2022
2023
2024
2025
2026
2027
      jacobi /= scale;
      if(jacobi < min_norm_jac)
      {
        min_norm_jac = jacobi;
      }
    }
  }
  else
2028
#endif
2029
  {
2030
2031
2032
2033
    double min_jacobi = VERDICT_DBL_MAX;
    double temp_norm_jac, lengths;
    double len1_sq, len2_sq, len3_sq;
    VerdictVector xxi, xet, xze;
2034

2035
2036
    VerdictVector node_pos[8];
    make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
2037

2038
2039
2040
    xxi = calc_hex_efg(1, node_pos);
    xet = calc_hex_efg(2, node_pos);
    xze = calc_hex_efg(3, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
2041

2042
2043
2044
2045
2046
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2047

2048
2049
2050
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2051

2052
2053
2054
2055
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2056

2057
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2058
    temp_norm_jac = jacobi / lengths;
Clint Stimpson's avatar
Clint Stimpson committed
2059

2060
2061
2062
2063
2064
2065
2066
2067
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2068

2069
2070
2071
2072
    // J(0,0,0):
    xxi = node_pos[1] - node_pos[0];
    xet = node_pos[3] - node_pos[0];
    xze = node_pos[4] - node_pos[0];
Clint Stimpson's avatar
Clint Stimpson committed
2073

2074
2075
2076
2077
2078
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2079

2080
2081
2082
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2083

2084
2085
2086
2087
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2088

2089
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2090
2091
2092
2093
2094
2095
2096
2097
2098
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2099

2100
2101
2102
2103
    // J(1,0,0):
    xxi = node_pos[2] - node_pos[1];
    xet = node_pos[0] - node_pos[1];
    xze = node_pos[5] - node_pos[1];
Clint Stimpson's avatar
Clint Stimpson committed
2104

2105
2106
2107
2108
2109
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2110

2111
2112
2113
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2114

2115
2116
2117
2118
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2119

2120
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2121
2122
2123
2124
2125
2126
2127
2128
2129
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2130

2131
2132
2133
2134
    // J(1,1,0):
    xxi = node_pos[3] - node_pos[2];
    xet = node_pos[1] - node_pos[2];
    xze = node_pos[6] - node_pos[2];
Clint Stimpson's avatar
Clint Stimpson committed
2135

2136
2137
2138
2139
2140
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2141

2142
2143
2144
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2145

2146
2147
2148
2149
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2150

2151
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2152
2153
2154
2155
2156
2157
2158
2159
2160
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2161

2162
2163
2164
2165
    // J(0,1,0):
    xxi = node_pos[0] - node_pos[3];
    xet = node_pos[2] - node_pos[3];
    xze = node_pos[7] - node_pos[3];
Clint Stimpson's avatar
Clint Stimpson committed
2166

2167
2168
2169
2170
2171
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2172

2173
2174
2175
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2176

2177
2178
2179
2180
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2181

2182
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2183
2184
2185
2186
2187
2188
2189
2190
2191
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2192

2193
2194
2195
2196
    // J(0,0,1):
    xxi = node_pos[7] - node_pos[4];
    xet = node_pos[5] - node_pos[4];
    xze = node_pos[0] - node_pos[4];
Clint Stimpson's avatar
Clint Stimpson committed
2197

2198
2199
2200
2201
2202
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2203

2204
2205
2206
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2207

2208
2209
2210
2211
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2212

2213
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2214
2215
2216
2217
2218
2219
2220
2221
2222
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2223

2224
2225
2226
2227
    // J(1,0,1):
    xxi = node_pos[4] - node_pos[5];
    xet = node_pos[6] - node_pos[5];
    xze = node_pos[1] - node_pos[5];
Clint Stimpson's avatar
Clint Stimpson committed
2228

2229
2230
2231
2232
2233
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2234

2235
2236
2237
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2238

2239
2240
2241
2242
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2243

2244
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2245
2246
2247
2248
2249
2250
2251
2252
2253
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2254

2255
2256
2257
2258
    // J(1,1,1):
    xxi = node_pos[5] - node_pos[6];
    xet = node_pos[7] - node_pos[6];
    xze = node_pos[2] - node_pos[6];
Clint Stimpson's avatar
Clint Stimpson committed
2259

2260
2261
2262
2263
2264
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2265

2266
2267
2268
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2269

2270
2271
2272
2273
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2274

2275
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2276
2277
2278
2279
2280
2281
2282
2283
2284
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2285

2286
2287
2288
2289
    // J(0,1,1):
    xxi = node_pos[6] - node_pos[7];
    xet = node_pos[4] - node_pos[7];
    xze = node_pos[3] - node_pos[7];
Clint Stimpson's avatar
Clint Stimpson committed
2290

2291
2292
2293
2294
2295
    jacobi = VerdictVector::Dot(xxi, (xet * xze));
    if (jacobi < min_jacobi)
    {
      min_jacobi = jacobi;
    }
Clint Stimpson's avatar
Clint Stimpson committed
2296

2297
2298
2299
    len1_sq = xxi.length_squared();
    len2_sq = xet.length_squared();
    len3_sq = xze.length_squared();
Clint Stimpson's avatar
Clint Stimpson committed
2300

2301
2302
2303
2304
2305
    if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
    {
      return (double)VERDICT_DBL_MAX;
    }

2306
    lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
2307
2308
2309
2310
2311
2312
2313
2314
2315
    temp_norm_jac = jacobi / lengths;
    if (temp_norm_jac < min_norm_jac)
    {
      min_norm_jac = temp_norm_jac;
    }
    else
    {
      temp_norm_jac = jacobi;
    }
2316
  }
Clint Stimpson's avatar
Clint Stimpson committed
2317

2318
2319
2320
2321
2322
  if (min_norm_jac > 0)
  {
    return (double)std::min(min_norm_jac, VERDICT_DBL_MAX);
  }
  return (double)std::max(min_norm_jac, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
2323
2324
}

Matt Staten's avatar
Matt Staten committed
2325
2326
2327
/*!
  Nodal jacobian ratio of a hex
  Minimum nodal jacobian divided by the maximum.  Detects element skewness.
2328
 */
2329
2330
2331
inline std::pair<double /*min*/, double /*max*/> minMaxVal(const double a1, const double a2)
{
  return (a1 < a2) ? std::pair<double, double>(a1, a2) : std::pair<double, double>(a2, a1);
Matt Staten's avatar
Matt Staten committed
2332
}
2333

2334
2335
inline std::pair<double, double> minMaxValPair(
  const std::pair<double, double>& a1, const std::pair<double, double>& a2)
2336
{
Matt Staten's avatar
Matt Staten committed
2337
2338
  return std::pair<double, double>(std::min(a1.first, a2.first), std::max(a1.second, a2.second));
}
2339

2340
double hex_nodal_jacobian_ratio(int num_nodes, const double coordinates[][3])
2341
{
2342
  return verdict::hex_nodal_jacobian_ratio2(num_nodes, (double*)coordinates);
2343
2344
}

2345
double hex_nodal_jacobian_ratio2(int /*num_nodes*/, const double* coordinates)
2346
{
Matt Staten's avatar
Matt Staten committed
2347
2348
2349
2350
2351
2352
  double Jdet8x[8];
  verdict::hex_nodal_jacobians(coordinates, Jdet8x);
  //
  //  Compute the minimum and maximum nodal determinates, use an optimal algorithm
  //
  // std::pair<double, double> minMaxResult = std::minmax_element(Jdet8x, Jdet8x+8);
2353

Matt Staten's avatar
Matt Staten committed
2354
2355
2356
2357
  std::pair<double, double> m01(minMaxVal(Jdet8x[0], Jdet8x[1]));
  std::pair<double, double> m23(minMaxVal(Jdet8x[2], Jdet8x[3]));
  std::pair<double, double> m45(minMaxVal(Jdet8x[4], Jdet8x[5]));
  std::pair<double, double> m67(minMaxVal(Jdet8x[6], Jdet8x[7]));
2358

Matt Staten's avatar
Matt Staten committed
2359
2360
  std::pair<double, double> m0123(minMaxValPair(m01, m23));
  std::pair<double, double> m4567(minMaxValPair(m45, m67));
2361

Matt Staten's avatar
Matt Staten committed
2362
  std::pair<double, double> m01234567(minMaxValPair(m0123, m4567));
2363

Matt Staten's avatar
Matt Staten committed
2364
2365
  //
  //  Turn the determinates into a normalized quality ratio.
2366
2367
  //  If the maximum nodal jacobian is negative the element is fully inverted, return huge negative
  //  number Otherwise return ratio of the minimal nodal determinate to the maximum
Matt Staten's avatar
Matt Staten committed
2368
  //
2369

2370
2371
  if (m01234567.second <= VERDICT_DBL_MIN)
  {
2372
    return -VERDICT_DBL_MAX;
2373
2374
2375
  }
  else
  {
Matt Staten's avatar
Matt Staten committed
2376
    return m01234567.first / m01234567.second;
2377
2378
2379
  }
}

Clint Stimpson's avatar
Clint Stimpson committed
2380
2381
/*!
  shear of a hex
2382

Clint Stimpson's avatar
Clint Stimpson committed
2383
  3/Condition number of Jacobian Skew matrix
2384
2385
 */
double hex_shear(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
2386
2387
{
  double shear;
2388
  double min_shear = 1.0;
Clint Stimpson's avatar
Clint Stimpson committed
2389
2390
2391
2392
  VerdictVector xxi, xet, xze;
  double det, len1_sq, len2_sq, len3_sq, lengths;

  VerdictVector node_pos[8];
2393
  make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403

  // J(0,0,0):
  xxi = node_pos[1] - node_pos[0];
  xet = node_pos[3] - node_pos[0];
  xze = node_pos[4] - node_pos[0];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2404
2405
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2406
    return 0;
2407
  }
Clint Stimpson's avatar
Clint Stimpson committed
2408

2409
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2410
  det = VerdictVector::Dot(xxi, (xet * xze));
2411
2412
2413
2414
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2415
2416

  shear = det / lengths;
2417
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427

  // J(1,0,0):
  xxi = node_pos[2] - node_pos[1];
  xet = node_pos[0] - node_pos[1];
  xze = node_pos[5] - node_pos[1];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2428
2429
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2430
    return 0;
2431
  }
Clint Stimpson's avatar
Clint Stimpson committed
2432

2433
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2434
  det = VerdictVector::Dot(xxi, (xet * xze));
2435
2436
2437
2438
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2439

2440
2441
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451

  // J(1,1,0):
  xxi = node_pos[3] - node_pos[2];
  xet = node_pos[1] - node_pos[2];
  xze = node_pos[6] - node_pos[2];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2452
2453
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2454
    return 0;
2455
  }
Clint Stimpson's avatar
Clint Stimpson committed
2456

2457
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2458
  det = VerdictVector::Dot(xxi, (xet * xze));
2459
2460
2461
2462
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2463

2464
2465
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475

  // J(0,1,0):
  xxi = node_pos[0] - node_pos[3];
  xet = node_pos[2] - node_pos[3];
  xze = node_pos[7] - node_pos[3];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2476
2477
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2478
    return 0;
2479
  }
Clint Stimpson's avatar
Clint Stimpson committed
2480

2481
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2482
  det = VerdictVector::Dot(xxi, (xet * xze));
2483
2484
2485
2486
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2487

2488
2489
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499

  // J(0,0,1):
  xxi = node_pos[7] - node_pos[4];
  xet = node_pos[5] - node_pos[4];
  xze = node_pos[0] - node_pos[4];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2500
2501
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2502
    return 0;
2503
  }
Clint Stimpson's avatar
Clint Stimpson committed
2504

2505
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2506
  det = VerdictVector::Dot(xxi, (xet * xze));
2507
2508
2509
2510
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2511

2512
2513
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523

  // J(1,0,1):
  xxi = node_pos[4] - node_pos[5];
  xet = node_pos[6] - node_pos[5];
  xze = node_pos[1] - node_pos[5];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2524
2525
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2526
    return 0;
2527
  }
Clint Stimpson's avatar
Clint Stimpson committed
2528

2529
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2530
  det = VerdictVector::Dot(xxi, (xet * xze));
2531
2532
2533
2534
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2535

2536
2537
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547

  // J(1,1,1):
  xxi = node_pos[5] - node_pos[6];
  xet = node_pos[7] - node_pos[6];
  xze = node_pos[2] - node_pos[6];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2548
2549
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2550
    return 0;
2551
  }
Clint Stimpson's avatar
Clint Stimpson committed
2552

2553
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2554
  det = VerdictVector::Dot(xxi, (xet * xze));
2555
2556
2557
2558
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2559

2560
2561
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571

  // J(0,1,1):
  xxi = node_pos[6] - node_pos[7];
  xet = node_pos[4] - node_pos[7];
  xze = node_pos[3] - node_pos[7];

  len1_sq = xxi.length_squared();
  len2_sq = xet.length_squared();
  len3_sq = xze.length_squared();

2572
2573
  if (len1_sq <= VERDICT_DBL_MIN || len2_sq <= VERDICT_DBL_MIN || len3_sq <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2574
    return 0;
2575
  }
Clint Stimpson's avatar
Clint Stimpson committed
2576

2577
  lengths = std::sqrt(len1_sq * len2_sq * len3_sq);
Matt Staten's avatar
Matt Staten committed
2578
  det = VerdictVector::Dot(xxi, (xet * xze));
2579
2580
2581
2582
  if (det < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2583

2584
2585
  shear = det / lengths;
  min_shear = std::min(shear, min_shear);
Clint Stimpson's avatar
Clint Stimpson committed
2586

2587
2588
  if (min_shear <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2589
    min_shear = 0;
2590
2591
2592
2593
2594
2595
2596
  }

  if (min_shear > 0)
  {
    return (double)std::min(min_shear, VERDICT_DBL_MAX);
  }
  return (double)std::max(min_shear, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
2597
2598
2599
2600
2601
2602
}

/*!
  shape of a hex

  3/Condition number of weighted Jacobian matrix
2603
2604
 */
double hex_shape(int /*num_nodes*/, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
2605
2606
{
  double det, shape;
2607
  double min_shape = 1.0;
Clint Stimpson's avatar
Clint Stimpson committed
2608
2609
2610
2611

  VerdictVector xxi, xet, xze;

  VerdictVector node_pos[8];
2612
  make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
2613
2614
2615
2616
2617
2618

  // J(0,0,0):
  xxi = node_pos[1] - node_pos[0];
  xet = node_pos[3] - node_pos[0];
  xze = node_pos[4] - node_pos[0];

Matt Staten's avatar
Matt Staten committed
2619
  det = VerdictVector::Dot(xxi, (xet * xze));
2620
2621
  if (det > VERDICT_DBL_MIN)
  {
2622
    shape = 3 * std::pow(det, two_thirds) /
2623
2624
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2625
  else
2626
  {
Clint Stimpson's avatar
Clint Stimpson committed
2627
    return 0;
2628
2629
2630
2631
2632
2633
  }

  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2634
2635
2636
2637
2638
2639

  // J(1,0,0):
  xxi = node_pos[2] - node_pos[1];
  xet = node_pos[0] - node_pos[1];
  xze = node_pos[5] - node_pos[1];

Matt Staten's avatar
Matt Staten committed
2640
  det = VerdictVector::Dot(xxi, (xet * xze));
2641
2642
  if (det > VERDICT_DBL_MIN)
  {
2643
    shape = 3 * std::pow(det, two_thirds) /
2644
2645
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2646
  else
2647
  {
Clint Stimpson's avatar
Clint Stimpson committed
2648
    return 0;
2649
2650
2651
2652
2653
2654
  }

  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2655
2656
2657
2658
2659
2660

  // J(1,1,0):
  xxi = node_pos[3] - node_pos[2];
  xet = node_pos[1] - node_pos[2];
  xze = node_pos[6] - node_pos[2];

Matt Staten's avatar
Matt Staten committed
2661
  det = VerdictVector::Dot(xxi, (xet * xze));
2662
2663
  if (det > VERDICT_DBL_MIN)
  {
2664
    shape = 3 * std::pow(det, two_thirds) /
2665
2666
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2667
  else
2668
  {
Clint Stimpson's avatar
Clint Stimpson committed
2669
    return 0;
2670
2671
2672
2673
2674
2675
  }

  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2676
2677
2678
2679
2680
2681

  // J(0,1,0):
  xxi = node_pos[0] - node_pos[3];
  xet = node_pos[2] - node_pos[3];
  xze = node_pos[7] - node_pos[3];

Matt Staten's avatar
Matt Staten committed
2682
  det = VerdictVector::Dot(xxi, (xet * xze));
2683
2684
  if (det > VERDICT_DBL_MIN)
  {
2685
    shape = 3 * std::pow(det, two_thirds) /
2686
2687
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2688
  else
2689
  {
Clint Stimpson's avatar
Clint Stimpson committed
2690
    return 0;
2691
  }
Clint Stimpson's avatar
Clint Stimpson committed
2692

2693
2694
2695
2696
  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2697

2698
  // J(0,0,1):
Clint Stimpson's avatar
Clint Stimpson committed
2699
2700
2701
2702
  xxi = node_pos[7] - node_pos[4];
  xet = node_pos[5] - node_pos[4];
  xze = node_pos[0] - node_pos[4];

Matt Staten's avatar
Matt Staten committed
2703
  det = VerdictVector::Dot(xxi, (xet * xze));
2704
2705
  if (det > VERDICT_DBL_MIN)
  {
2706
    shape = 3 * std::pow(det, two_thirds) /
2707
2708
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2709
  else
2710
  {
Clint Stimpson's avatar
Clint Stimpson committed
2711
    return 0;
2712
  }
Clint Stimpson's avatar
Clint Stimpson committed
2713

2714
2715
2716
2717
  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2718

2719
  // J(1,0,1):
Clint Stimpson's avatar
Clint Stimpson committed
2720
2721
2722
2723
  xxi = node_pos[4] - node_pos[5];
  xet = node_pos[6] - node_pos[5];
  xze = node_pos[1] - node_pos[5];

Matt Staten's avatar
Matt Staten committed
2724
  det = VerdictVector::Dot(xxi, (xet * xze));
2725
2726
  if (det > VERDICT_DBL_MIN)
  {
2727
    shape = 3 * std::pow(det, two_thirds) /
2728
2729
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2730
  else
2731
  {
Clint Stimpson's avatar
Clint Stimpson committed
2732
    return 0;
2733
  }
Clint Stimpson's avatar
Clint Stimpson committed
2734

2735
2736
2737
2738
  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2739

2740
  // J(1,1,1):
Clint Stimpson's avatar
Clint Stimpson committed
2741
2742
2743
2744
  xxi = node_pos[5] - node_pos[6];
  xet = node_pos[7] - node_pos[6];
  xze = node_pos[2] - node_pos[6];

Matt Staten's avatar
Matt Staten committed
2745
  det = VerdictVector::Dot(xxi, (xet * xze));
2746
2747
  if (det > VERDICT_DBL_MIN)
  {
2748
    shape = 3 * std::pow(det, two_thirds) /
2749
2750
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2751
  else
2752
  {
Clint Stimpson's avatar
Clint Stimpson committed
2753
    return 0;
2754
  }
Clint Stimpson's avatar
Clint Stimpson committed
2755

2756
2757
2758
2759
  if (shape < min_shape)
  {
    min_shape = shape;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2760

2761
  // J(1,1,1):
Clint Stimpson's avatar
Clint Stimpson committed
2762
2763
2764
2765
  xxi = node_pos[6] - node_pos[7];
  xet = node_pos[4] - node_pos[7];
  xze = node_pos[3] - node_pos[7];

Matt Staten's avatar
Matt Staten committed
2766
  det = VerdictVector::Dot(xxi, (xet * xze));
2767
2768
  if (det > VERDICT_DBL_MIN)
  {
2769
    shape = 3 * std::pow(det, two_thirds) /
2770
2771
      (VerdictVector::Dot(xxi, xxi) + VerdictVector::Dot(xet, xet) + VerdictVector::Dot(xze, xze));
  }
Clint Stimpson's avatar
Clint Stimpson committed
2772
  else
2773
  {
Clint Stimpson's avatar
Clint Stimpson committed
2774
    return 0;
2775
  }
Clint Stimpson's avatar
Clint Stimpson committed
2776

2777
2778
2779
2780
2781
2782
2783
  if (shape < min_shape)
  {
    min_shape = shape;
  }

  if (min_shape <= VERDICT_DBL_MIN)
  {
Clint Stimpson's avatar
Clint Stimpson committed
2784
    min_shape = 0;
2785
  }
Clint Stimpson's avatar
Clint Stimpson committed
2786

2787
2788
2789
2790
2791
  if (min_shape > 0)
  {
    return (double)std::min(min_shape, VERDICT_DBL_MAX);
  }
  return (double)std::max(min_shape, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
2792
2793
2794
2795
2796
2797
}

/*!
  relative size of a hex

  Min( J, 1/J ), where J is determinant of weighted Jacobian matrix
2798
2799
2800
 */
double hex_relative_size_squared(
  int /*num_nodes*/, const double coordinates[][3], double average_hex_volume)
Clint Stimpson's avatar
Clint Stimpson committed
2801
2802
{
  double size = 0;
2803
  double tau;
Clint Stimpson's avatar
Clint Stimpson committed
2804
2805
2806
2807

  VerdictVector xxi, xet, xze;
  double det, det_sum = 0;

2808
2809
2810
  hex_get_weight(xxi, xet, xze, average_hex_volume);

  // This is the average relative size
Matt Staten's avatar
Matt Staten committed
2811
  double detw = VerdictVector::Dot(xxi, (xet * xze));
Clint Stimpson's avatar
Clint Stimpson committed
2812

2813
2814
2815
2816
  if (detw < VERDICT_DBL_MIN)
  {
    return 0;
  }
Clint Stimpson's avatar
Clint Stimpson committed
2817
2818

  VerdictVector node_pos[8];
2819
  make_hex_nodes(coordinates, node_pos);
Clint Stimpson's avatar
Clint Stimpson committed
2820
2821
2822
2823
2824
2825

  // J(0,0,0):
  xxi = node_pos[1] - node_pos[0];
  xet = node_pos[3] - node_pos[0];
  xze = node_pos[4] - node_pos[0];

2826
2827
  det = VerdictVector::Dot(xxi, (xet * xze));
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2828
2829
2830
2831
2832
2833

  // J(1,0,0):
  xxi = node_pos[2] - node_pos[1];
  xet = node_pos[0] - node_pos[1];
  xze = node_pos[5] - node_pos[1];

Matt Staten's avatar
Matt Staten committed
2834
  det = VerdictVector::Dot(xxi, (xet * xze));
2835
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2836
2837
2838
2839
2840
2841

  // J(0,1,0):
  xxi = node_pos[3] - node_pos[2];
  xet = node_pos[1] - node_pos[2];
  xze = node_pos[6] - node_pos[2];

Matt Staten's avatar
Matt Staten committed
2842
  det = VerdictVector::Dot(xxi, (xet * xze));
2843
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2844
2845
2846
2847
2848
2849

  // J(1,1,0):
  xxi = node_pos[0] - node_pos[3];
  xet = node_pos[2] - node_pos[3];
  xze = node_pos[7] - node_pos[3];

Matt Staten's avatar
Matt Staten committed
2850
  det = VerdictVector::Dot(xxi, (xet * xze));
2851
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2852
2853
2854
2855
2856
2857

  // J(0,1,0):
  xxi = node_pos[7] - node_pos[4];
  xet = node_pos[5] - node_pos[4];
  xze = node_pos[0] - node_pos[4];

Matt Staten's avatar
Matt Staten committed
2858
  det = VerdictVector::Dot(xxi, (xet * xze));
2859
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2860
2861
2862
2863
2864
2865

  // J(1,0,1):
  xxi = node_pos[4] - node_pos[5];
  xet = node_pos[6] - node_pos[5];
  xze = node_pos[1] - node_pos[5];

Matt Staten's avatar
Matt Staten committed
2866
  det = VerdictVector::Dot(xxi, (xet * xze));
2867
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2868
2869
2870
2871
2872
2873

  // J(1,1,1):
  xxi = node_pos[5] - node_pos[6];
  xet = node_pos[7] - node_pos[6];
  xze = node_pos[2] - node_pos[6];

Matt Staten's avatar
Matt Staten committed
2874
  det = VerdictVector::Dot(xxi, (xet * xze));
2875
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2876
2877
2878
2879
2880
2881

  // J(1,1,1):
  xxi = node_pos[6] - node_pos[7];
  xet = node_pos[4] - node_pos[7];
  xze = node_pos[3] - node_pos[7];

Matt Staten's avatar
Matt Staten committed
2882
  det = VerdictVector::Dot(xxi, (xet * xze));
2883
  det_sum += det;
Clint Stimpson's avatar
Clint Stimpson committed
2884

2885
  if (det_sum > VERDICT_DBL_MIN)
Clint Stimpson's avatar
Clint Stimpson committed
2886
  {
2887
    tau = det_sum / (8 * detw);
Clint Stimpson's avatar
Clint Stimpson committed
2888

2889
    tau = std::min(tau, 1.0 / tau);
Clint Stimpson's avatar
Clint Stimpson committed
2890

2891
    size = tau * tau;
Clint Stimpson's avatar
Clint Stimpson committed
2892
2893
  }

2894
2895
2896
2897
2898
  if (size > 0)
  {
    return (double)std::min(size, VERDICT_DBL_MAX);
  }
  return (double)std::max(size, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
2899
2900
2901
2902
2903
2904
}

/*!
  shape and size of a hex

  Product of Shape and Relative Size
2905
2906
 */
double hex_shape_and_size(int num_nodes, const double coordinates[][3], double average_hex_volume)
2907
{
2908
2909
  double size = hex_relative_size_squared(num_nodes, coordinates, average_hex_volume);
  double shape = hex_shape(num_nodes, coordinates);
Clint Stimpson's avatar
Clint Stimpson committed
2910
2911
2912

  double shape_size = size * shape;

2913
2914
2915
2916
2917
  if (shape_size > 0)
  {
    return (double)std::min(shape_size, VERDICT_DBL_MAX);
  }
  return (double)std::max(shape_size, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
2918
2919
2920
2921
2922
2923
}

/*!
  shear and size of a hex

  Product of Shear and Relative Size
2924
2925
 */
double hex_shear_and_size(int num_nodes, const double coordinates[][3], double average_hex_volume)
2926
{
2927
2928
  double size = hex_relative_size_squared(num_nodes, coordinates, average_hex_volume);
  double shear = hex_shear(num_nodes, coordinates);
Clint Stimpson's avatar
Clint Stimpson committed
2929

2930
  double shear_size = shear * size;
Clint Stimpson's avatar
Clint Stimpson committed
2931

2932
2933
2934
2935
2936
  if (shear_size > 0)
  {
    return (double)std::min(shear_size, VERDICT_DBL_MAX);
  }
  return (double)std::max(shear_size, -VERDICT_DBL_MAX);
Clint Stimpson's avatar
Clint Stimpson committed
2937
2938
2939
2940
}

/*!
  distortion of a hex
2941
2942
 */
double hex_distortion(int num_nodes, const double coordinates[][3])
Clint Stimpson's avatar
Clint Stimpson committed
2943
{
2944
2945
  // use 2x2 gauss points for linear hex and 3x3 for 2nd order hex
  int number_of_gauss_points = 0;
2946
2947
  if (num_nodes < 20)
  {
2948
    // 2x2 quadrature rule
2949
2950
2951
2952
2953
    number_of_gauss_points = 2;
    num_nodes = 8;
  }
  else if (num_nodes >= 20)
  {
2954
    // 3x3 quadrature rule
2955
2956
2957
    number_of_gauss_points = 3;
    num_nodes = 20;
  }
2958

2959
  int number_dimension = 3;
2960
2961
  int total_number_of_gauss_points =
    number_of_gauss_points * number_of_gauss_points * number_of_gauss_points;
2962
  double distortion = VERDICT_DBL_MAX;
2963

2964
2965
2966
2967
2968
  // maxTotalNumberGaussPoints =27, maxNumberNodes = 20
  // they are defined in GaussIntegration.hpp
  // This is used to make these arrays static.
  // I tried dynamically allocated arrays but the new and delete
  // was very expensive
2969

2970
2971
2972
2973
2974
  double shape_function[maxTotalNumberGaussPoints][maxNumberNodes];
  double dndy1[maxTotalNumberGaussPoints][maxNumberNodes];
  double dndy2[maxTotalNumberGaussPoints][maxNumberNodes];
  double dndy3[maxTotalNumberGaussPoints][maxNumberNodes];
  double weight[maxTotalNumberGaussPoints];
2975
2976
2977
2978

  // create an object of GaussIntegration
  GaussIntegration gint{};
  gint.initialize(number_of_gauss_points, num_nodes, number_dimension);
2979
  gint.calculate_shape_function_3d_hex();
2980
2981
  gint.get_shape_func(shape_function[0], dndy1[0], dndy2[0], dndy3[0], weight);

2982
  VerdictVector xxi, xet, xze, xin;
2983

2984
  double jacobian, minimum_jacobian;
2985
  double element_volume = 0.0;
2986
2987
2988
  minimum_jacobian = VERDICT_DBL_MAX;
  // calculate element volume
  int ife, ja;
2989
2990
2991
2992
2993
2994
2995
  for (ife = 0; ife < total_number_of_gauss_points; ife++)
  {
    xxi.set(0.0, 0.0, 0.0);
    xet.set(0.0, 0.0, 0.0);
    xze.set(0.0, 0.0, 0.0);

    for (ja = 0; ja < num_nodes; ja++)
2996
2997
    {
      xin.set(coordinates[ja][0], coordinates[ja][1], coordinates[ja][2]);
2998
2999
3000
      xxi += dndy1[ife][ja] * xin;
      xet += dndy2[ife][ja] * xin;
      xze += dndy3[ife][ja] * xin;
3001
    }
3002

3003
3004
    jacobian = VerdictVector::Dot(xxi, (xet * xze));
    if (minimum_jacobian > jacobian)
3005
    {
3006
      minimum_jacobian = jacobian;
3007
3008
3009
    }

    element_volume += weight[ife] * jacobian;
3010
  }
3011

3012
3013
3014
3015
  // loop through all nodes
  double dndy1_at_node[maxNumberNodes][maxNumberNodes];
  double dndy2_at_node[maxNumberNodes][maxNumberNodes];
  double dndy3_at_node[maxNumberNodes][maxNumberNodes];
3016
3017

  gint.calculate_derivative_at_nodes_3d(dndy1_at_node, dndy2_at_node, dndy3_at_node);
3018
  int node_id;
3019
3020
3021
3022
3023
3024
3025
  for (node_id = 0; node_id < num_nodes; node_id++)
  {
    xxi.set(0.0, 0.0, 0.0);
    xet.set(0.0, 0.0, 0.0);
    xze.set(0.0, 0.0, 0.0);

    for (ja = 0; ja < num_nodes; ja++)
3026
3027
    {
      xin.set(coordinates[ja][0], coordinates[ja][1], coordinates[ja][2]);
3028
3029
3030
      xxi += dndy1_at_node[node_id][ja] * xin;
      xet += dndy2_at_node[node_id][ja] * xin;
      xze += dndy3_at_node[node_id][ja] * xin;
3031
    }
3032

3033
3034
    jacobian = VerdictVector::Dot(xxi, (xet * xze));
    if (minimum_jacobian > jacobian)
3035
    {
3036
      minimum_jacobian = jacobian;
3037
    }
3038
  }
3039
  if (std::abs(element_volume) > 0.0)
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
  {
    distortion = minimum_jacobian / element_volume * 8.;
  }
  if (distortion > VERDICT_DBL_MAX)
  {
    distortion = VERDICT_DBL_MAX;
  }
  else if (distortion < -VERDICT_DBL_MAX)
  {
    distortion = -VERDICT_DBL_MAX;
  }
  else if (std::isnan(distortion))
  {
    distortion = VERDICT_DBL_MAX; // 0/0, or should we return some other value?
  }
3055
  return (double)distortion;
Clint Stimpson's avatar
Clint Stimpson committed
3056
}
3057
3058
3059

double hex_timestep(int num_nodes, const double coordinates[][3], double density,
  double poissons_ratio, double youngs_modulus)
3060
{
3061
3062
3063
  double char_length = hex_dimension(num_nodes, coordinates);
  double M =
    youngs_modulus * (1 - poissons_ratio) / ((1 - 2 * poissons_ratio) * (1 + poissons_ratio));
3064
  double denominator = std::sqrt(M / density);
Clint Stimpson's avatar
Clint Stimpson committed
3065

3066
3067
  return char_length / denominator;
}
Clint Stimpson's avatar
Clint Stimpson committed
3068
3069

/*
3070
double hex_jac_normjac_oddy_cond( int choices[],
3071
                      const double coordinates[][3],
Clint Stimpson's avatar
Clint Stimpson committed
3072
3073
3074
                      double answers[4]  )
{
  //Define variables
3075
  int i;
3076

Clint Stimpson's avatar
Clint Stimpson committed
3077
  double xxi[3], xet[3], xze[3];
3078
  double norm_jacobian = 0.0, current_norm_jac = 0.0;
Clint Stimpson's avatar
Clint Stimpson committed
3079
        double jacobian = 0.0, current_jacobian = 0.0;
3080
  double oddy = 0.0, current_oddy = 0.0;
Clint Stimpson's avatar
Clint Stimpson committed
3081
3082
  double condition = 0.0, current_condition = 0.0;

3083
3084
3085
3086
3087
3088
  for( i=0; i<3; i++)
    xxi[i] = calc_hex_efg( 2, i, coordinates );
  for( i=0; i<3; i++)
    xet[i] = calc_hex_efg( 3, i, coordinates );
  for( i=0; i<3; i++)
    xze[i] = calc_hex_efg( 6, i, coordinates );
Clint Stimpson's avatar
Clint Stimpson committed
3089
3090
3091
3092
3093
3094
3095

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze  );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3096
3097

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3098
3099
3100
3101
3102

    current_jacobian /= 64.0;
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3103
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3104
3105
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3106
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3107
3108
3109
3110
3111
3112
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3113
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3114
3115
3116
3117
3118
3119
3120
3121
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
    xxi[i] = coordinates[1][i] - coordinates[0][i];
    xet[i] = coordinates[3][i] - coordinates[0][i];
    xze[i] = coordinates[4][i] - coordinates[0][i];
3122
  }
Clint Stimpson's avatar
Clint Stimpson committed
3123
3124
3125
3126
3127
3128
3129

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze  );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3130
3131

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3132
3133
3134
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3135
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3136
3137
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3138
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3139
3140
3141
3142
3143
3144
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3145
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[1][i] - coordinates[0][i];
          xet[i] = coordinates[2][i] - coordinates[1][i];
          xze[i] = coordinates[5][i] - coordinates[1][i];
  }

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi,  xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3162
3163

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3164
3165
3166
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3167
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3168
3169
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3170
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3171
3172
3173
3174
3175
3176
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3177
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
    if ( current_condition > condition ) { condition = current_condition; }
  }


  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[2][i] - coordinates[3][i];
          xet[i] = coordinates[3][i] - coordinates[0][i];
          xze[i] = coordinates[7][i] - coordinates[3][i];
  }

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3195
3196

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3197
3198
3199
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3200
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3201
3202
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3203
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3204
3205
3206
3207
3208
3209
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3210
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3211
3212
3213
3214
3215
3216
3217
3218
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[5][i] - coordinates[4][i];
          xet[i] = coordinates[7][i] - coordinates[4][i];
          xze[i] = coordinates[4][i] - coordinates[0][i];
3219
  }
Clint Stimpson's avatar
Clint Stimpson committed
3220
3221
3222
3223
3224
3225
3226

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3227
3228

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3229
3230
3231
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3232
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3233
3234
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3235
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3236
3237
3238
3239
3240
3241
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3242
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3243
3244
3245
3246
3247
3248
3249
3250
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[2][i] - coordinates[3][i];
          xet[i] = coordinates[2][i] - coordinates[1][i];
          xze[i] = coordinates[6][i] - coordinates[2][i];
3251
  }
Clint Stimpson's avatar
Clint Stimpson committed
3252
3253
3254
3255
3256
3257
3258

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3259
3260

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3261
3262
3263
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3264
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3265
3266
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3267
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3268
3269
3270
3271
3272
3273
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3274
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[5][i] - coordinates[4][i];
          xet[i] = coordinates[6][i] - coordinates[5][i];
          xze[i] = coordinates[5][i] - coordinates[1][i];
  }

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3291
3292

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3293
3294
3295
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3296
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3297
3298
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3299
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3300
3301
3302
3303
3304
3305
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3306
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[6][i] - coordinates[7][i];
          xet[i] = coordinates[7][i] - coordinates[4][i];
          xze[i] = coordinates[7][i] - coordinates[3][i];
  }

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3323
3324

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3325
3326
3327
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3328
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3329
3330
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3331
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3332
3333
3334
3335
3336
3337
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3338
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
    if ( current_condition > condition ) { condition = current_condition; }
  }

  for( i=0; i<3; i++)
  {
          xxi[i] = coordinates[6][i] - coordinates[7][i];
          xet[i] = coordinates[6][i] - coordinates[5][i];
          xze[i] = coordinates[6][i] - coordinates[2][i];
  }

  // norm jacobian and jacobian
  if ( choices[0] || choices[1] )
  {
    current_jacobian = determinant( xxi, xet, xze );
    current_norm_jac = normalize_jacobian( current_jacobian,
            xxi, xet, xze );
3355
3356

    if (current_norm_jac < norm_jacobian) { norm_jacobian = current_norm_jac; }
Clint Stimpson's avatar
Clint Stimpson committed
3357
3358
3359
    if (current_jacobian < jacobian) { jacobian = current_jacobian; }
  }

3360
  // oddy
Clint Stimpson's avatar
Clint Stimpson committed
3361
3362
  if ( choices[2] )
  {
Matt Staten's avatar
Matt Staten committed
3363
    current_oddy = oddy_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3364
3365
3366
3367
3368
3369
      if ( current_oddy > oddy ) { oddy = current_oddy; }
  }

  // condition
  if ( choices[3] )
  {
Matt Staten's avatar
Matt Staten committed
3370
    current_condition = condition_comp( xxi, xet, xze );
Clint Stimpson's avatar
Clint Stimpson committed
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
    if ( current_condition > condition ) { condition = current_condition; }

    condition /= 3.0;
  }

  answers[0] = jacobian;
  answers[1] = norm_jacobian;
  answers[2] = oddy;
  answers[3] = condition;

  return 1.0;
}
*/
Matt Staten's avatar
Matt Staten committed
3384
} // namespace verdict