| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
/****************************************************************************/ |
| 4 |
/* */ |
| 5 |
/* File: shapes.c */ |
| 6 |
/* */ |
| 7 |
/* Purpose: shape functions for triangles and quadrilaterals */ |
| 8 |
/* */ |
| 9 |
/* Author: Peter Bastian */ |
| 10 |
/* Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */ |
| 11 |
/* Universitaet Heidelberg */ |
| 12 |
/* Im Neuenheimer Feld 368 */ |
| 13 |
/* 6900 Heidelberg */ |
| 14 |
/* internet: [email protected] */ |
| 15 |
/* */ |
| 16 |
/* History: 08.04.92 begin, ug version 2.0 */ |
| 17 |
/* 20.11.94 moved shapes.c from ug to cd folder */ |
| 18 |
/* */ |
| 19 |
/* Remarks: */ |
| 20 |
/* */ |
| 21 |
/****************************************************************************/ |
| 22 |
|
| 23 |
/****************************************************************************/ |
| 24 |
/* */ |
| 25 |
/* include files */ |
| 26 |
/* system include files */ |
| 27 |
/* application include files */ |
| 28 |
/* */ |
| 29 |
/****************************************************************************/ |
| 30 |
|
| 31 |
#include <config.h> |
| 32 |
#include <cmath> |
| 33 |
#include <cassert> |
| 34 |
#include <cstddef> |
| 35 |
|
| 36 |
#include <algorithm> |
| 37 |
|
| 38 |
#include <dune/common/math.hh> |
| 39 |
#include <dune/uggrid/low/architecture.h> |
| 40 |
#include <dune/uggrid/low/misc.h> |
| 41 |
#include <dune/uggrid/low/ugtypes.h> |
| 42 |
#include "gm.h" |
| 43 |
#include "evm.h" |
| 44 |
#include "shapes.h" |
| 45 |
|
| 46 |
|
| 47 |
USING_UG_NAMESPACE |
| 48 |
USING_UGDIM_NAMESPACE |
| 49 |
|
| 50 |
/****************************************************************************/ |
| 51 |
/* */ |
| 52 |
/* defines in the following order */ |
| 53 |
/* */ |
| 54 |
/* compile time constants defining static data size (i.e. arrays) */ |
| 55 |
/* other constants */ |
| 56 |
/* macros */ |
| 57 |
/* */ |
| 58 |
/****************************************************************************/ |
| 59 |
|
| 60 |
#define SMALL_DIFF 1e-20 |
| 61 |
#define MAX_ITER 20 |
| 62 |
|
| 63 |
/****************************************************************************/ |
| 64 |
/* */ |
| 65 |
/* data structures used in this source file (exported data structures are */ |
| 66 |
/* in the corresponding include file!) */ |
| 67 |
/* */ |
| 68 |
/****************************************************************************/ |
| 69 |
|
| 70 |
/****************************************************************************/ |
| 71 |
/* */ |
| 72 |
/* definition of exported global variables */ |
| 73 |
/* */ |
| 74 |
/****************************************************************************/ |
| 75 |
|
| 76 |
/****************************************************************************/ |
| 77 |
/* */ |
| 78 |
/* forward declarations of functions used before they are defined */ |
| 79 |
/* */ |
| 80 |
/****************************************************************************/ |
| 81 |
|
| 82 |
|
| 83 |
/****************************************************************************/ |
| 84 |
/** \brief Transform global coordinates to local |
| 85 |
|
| 86 |
\param n - number of corners |
| 87 |
\param Corners - coordinates of corners |
| 88 |
\param EvalPoint - global coordinates |
| 89 |
\param LocalCoord - local coordinates |
| 90 |
|
| 91 |
This function transforms global coordinates to local in an evaluated point |
| 92 |
in 3D. |
| 93 |
|
| 94 |
\return <ul> |
| 95 |
<li> 0 if ok </li> |
| 96 |
<li> 1 if error occurred. </li> |
| 97 |
</ul> |
| 98 |
*/ |
| 99 |
/****************************************************************************/ |
| 100 |
|
| 101 |
INT NS_DIM_PREFIX UG_GlobalToLocal (INT n, const DOUBLE **Corners, |
| 102 |
const FieldVector<DOUBLE,DIM>& EvalPoint, FieldVector<DOUBLE,DIM>& LocalCoord) |
| 103 |
{ |
| 104 |
DOUBLE_VECTOR tmp,diff,M[DIM],IM[DIM]; |
| 105 |
DOUBLE s,IMdet; |
| 106 |
INT i; |
| 107 |
|
| 108 |
V_DIM_SUBTRACT(EvalPoint,Corners[0],diff); |
| 109 |
if (n == DIM+1) |
| 110 |
{ |
| 111 |
TRANSFORMATION(DIM+1,Corners,LocalCoord,M); |
| 112 |
M_DIM_INVERT(M,IM,IMdet); |
| 113 |
if (IMdet==0) return (2); |
| 114 |
MT_TIMES_V_DIM(IM,diff,LocalCoord); |
| 115 |
return(0); |
| 116 |
} |
| 117 |
V_DIM_CLEAR(LocalCoord); |
| 118 |
TRANSFORMATION(n,Corners,LocalCoord,M); |
| 119 |
M_DIM_INVERT(M,IM,IMdet); |
| 120 |
if (IMdet==0) return (3); |
| 121 |
MT_TIMES_V_DIM(IM,diff,LocalCoord); |
| 122 |
for (i=0; i<MAX_ITER; i++) |
| 123 |
{ |
| 124 |
LOCAL_TO_GLOBAL (n,Corners,LocalCoord,tmp); |
| 125 |
V_DIM_SUBTRACT(tmp,EvalPoint,diff); |
| 126 |
V_DIM_EUKLIDNORM(diff,s); |
| 127 |
PRINTDEBUG(gm,1,("UG_GlobalToLocal %d %g\n",i,s)); |
| 128 |
if (s * s <= SMALL_DIFF * IMdet) |
| 129 |
return (0); |
| 130 |
TRANSFORMATION(n,Corners,LocalCoord,M); |
| 131 |
M_DIM_INVERT(M,IM,IMdet); |
| 132 |
if (IMdet==0) return (4); |
| 133 |
MT_TIMES_V_DIM(IM,diff,tmp); |
| 134 |
V_DIM_SUBTRACT(LocalCoord,tmp,LocalCoord); |
| 135 |
} |
| 136 |
|
| 137 |
return(1); |
| 138 |
} |
| 139 |
|
| 140 |
/****************************************************************************/ |
| 141 |
/** \brief Calculate inner normals of tetrahedra |
| 142 |
|
| 143 |
\param theCorners - list of pointers to phys corner vectors |
| 144 |
\param theNormals - normals of tetrahedra |
| 145 |
|
| 146 |
This function calculates the inner normals on the sides of a tetrahedron. |
| 147 |
|
| 148 |
\return <ul> |
| 149 |
<li> 0 if ok </li> |
| 150 |
<li> 1 if error occurred. </li> |
| 151 |
</ul> |
| 152 |
*/ |
| 153 |
/****************************************************************************/ |
| 154 |
|
| 155 |
#ifdef UG_DIM_3 |
| 156 |
INT NS_DIM_PREFIX TetraSideNormals (ELEMENT *theElement, DOUBLE **theCorners, DOUBLE_VECTOR theNormals[MAX_SIDES_OF_ELEM]) |
| 157 |
{ |
| 158 |
DOUBLE_VECTOR a, b; |
| 159 |
DOUBLE h; |
| 160 |
INT j; |
| 161 |
|
| 162 |
for (j=0; j<4; j++) |
| 163 |
{ |
| 164 |
INT k = element_descriptors[TETRAHEDRON]->side_opp_to_corner[j]; |
| 165 |
V3_SUBTRACT(theCorners[(j+1)%4],theCorners[(j+2)%4],a) |
| 166 |
V3_SUBTRACT(theCorners[(j+1)%4],theCorners[(j+3)%4],b) |
| 167 |
V3_VECTOR_PRODUCT(a,b,theNormals[k]) |
| 168 |
V3_Normalize(theNormals[k]); |
| 169 |
V3_SUBTRACT(theCorners[j],theCorners[(j+1)%4],a) |
| 170 |
V3_SCALAR_PRODUCT(theNormals[k],a,h); |
| 171 |
if (std::abs(h)<SMALL_C) return (1); |
| 172 |
if (h<0.0) |
| 173 |
V3_SCALE(-1.0,theNormals[k]); |
| 174 |
} |
| 175 |
|
| 176 |
return (0); |
| 177 |
|
| 178 |
} |
| 179 |
#endif |
| 180 |
|
| 181 |
/****************************************************************************/ |
| 182 |
/** \brief Calculate maximal side angle of Tetrahedron |
| 183 |
|
| 184 |
\param theCorners - list of pointers to phys corner vectors |
| 185 |
\param MaxAngle - max side angle |
| 186 |
|
| 187 |
This function calculates the maximal side angle of the tetrahedron. |
| 188 |
|
| 189 |
\return <ul> |
| 190 |
<li> 0 if ok </li> |
| 191 |
<li> 1 if error occurred. </li> |
| 192 |
</ul> |
| 193 |
*/ |
| 194 |
/****************************************************************************/ |
| 195 |
|
| 196 |
#ifdef UG_DIM_3 |
| 197 |
INT NS_DIM_PREFIX TetMaxSideAngle (ELEMENT *theElement, const DOUBLE **theCorners, DOUBLE *MaxAngle) |
| 198 |
{ |
| 199 |
DOUBLE_VECTOR theNormal[MAX_SIDES_OF_ELEM]; |
| 200 |
DOUBLE max,help; |
| 201 |
INT i; |
| 202 |
|
| 203 |
if (TetraSideNormals (theElement,(DOUBLE **)theCorners,theNormal)) return (1); |
| 204 |
max = -1.0; |
| 205 |
for (i=0; i<EDGES_OF_ELEM(theElement); i++) |
| 206 |
{ |
| 207 |
V3_SCALAR_PRODUCT(theNormal[SIDE_WITH_EDGE(theElement,i,0)],theNormal[SIDE_WITH_EDGE(theElement,i,1)],help) |
| 208 |
max = std::max(help,max); |
| 209 |
} |
| 210 |
max = std::min(max,1.0); |
| 211 |
*MaxAngle = 180.0/Dune::StandardMathematicalConstants<double>::pi()*acos(-max); |
| 212 |
|
| 213 |
return (0); |
| 214 |
} |
| 215 |
#endif |
| 216 |
|
| 217 |
/****************************************************************************/ |
| 218 |
/** \brief Calculates side angle and length of edge of Tetrahedron |
| 219 |
|
| 220 |
\param theCorners - list of pointers to phys corner vectors |
| 221 |
\param Angle - side angle |
| 222 |
\param Length - sidelength |
| 223 |
|
| 224 |
This function calculates the side angle of a tetrahedron and the lengths of |
| 225 |
the edges belonging to this side. |
| 226 |
|
| 227 |
\return <ul> |
| 228 |
<li> 0 if ok </li> |
| 229 |
<li> 1 if error occurred. </li> |
| 230 |
</ul> |
| 231 |
*/ |
| 232 |
/****************************************************************************/ |
| 233 |
|
| 234 |
#ifdef UG_DIM_3 |
| 235 |
INT NS_DIM_PREFIX TetAngleAndLength (ELEMENT *theElement, const DOUBLE **theCorners, DOUBLE *Angle, DOUBLE *Length) |
| 236 |
{ |
| 237 |
DOUBLE_VECTOR theNormals[MAX_SIDES_OF_ELEM],theEdge[MAX_EDGES_OF_ELEM]; |
| 238 |
DOUBLE h; |
| 239 |
INT j,k; |
| 240 |
|
| 241 |
for (j=0; j<EDGES_OF_ELEM(theElement); j++) |
| 242 |
{ |
| 243 |
V3_SUBTRACT(theCorners[CORNER_OF_EDGE(theElement,j,1)],theCorners[CORNER_OF_EDGE(theElement,j,0)],theEdge[j]) |
| 244 |
V3_EUKLIDNORM(theEdge[j],Length[j]) |
| 245 |
} |
| 246 |
for (j=0; j<SIDES_OF_ELEM(theElement); j++) |
| 247 |
{ |
| 248 |
V3_VECTOR_PRODUCT(theEdge[EDGE_OF_SIDE(theElement,j,0)],theEdge[EDGE_OF_SIDE(theElement,j,1)],theNormals[j]) |
| 249 |
V3_Normalize(theNormals[j]); |
| 250 |
k = EDGE_OF_CORNER(theElement,CORNER_OPP_TO_SIDE(theElement,j),0); |
| 251 |
V3_SCALAR_PRODUCT(theNormals[j],theEdge[k],h) |
| 252 |
if (std::abs(h)<SMALL_C) return (1); |
| 253 |
if ( (h<0.0 && CORNER_OF_EDGE(theElement,k,1)==CORNER_OPP_TO_SIDE(theElement,j)) || |
| 254 |
(h>0.0 && CORNER_OF_EDGE(theElement,k,0)==CORNER_OPP_TO_SIDE(theElement,j)) ) |
| 255 |
V3_SCALE(-1.0,theNormals[j]); |
| 256 |
} |
| 257 |
for (j=0; j<EDGES_OF_ELEM(theElement); j++) |
| 258 |
{ |
| 259 |
V3_SCALAR_PRODUCT(theNormals[SIDE_WITH_EDGE(theElement,j,0)],theNormals[SIDE_WITH_EDGE(theElement,j,1)],Angle[j]) |
| 260 |
Angle[j] = std::clamp(Angle[j], -1.0, 1.0); |
| 261 |
Angle[j] = (DOUBLE)acos((double)Angle[j]); |
| 262 |
} |
| 263 |
|
| 264 |
return (0); |
| 265 |
} |
| 266 |
#endif |