| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: evm.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: elementary vector manipulations */ |
| 10 |
/* */ |
| 11 |
/* Author: Klaus Johannsen */ |
| 12 |
/* Institut fuer Computeranwendungen */ |
| 13 |
/* Universitaet Stuttgart */ |
| 14 |
/* Pfaffenwaldring 27 */ |
| 15 |
/* 70569 Stuttgart */ |
| 16 |
/* internet: [email protected] */ |
| 17 |
/* */ |
| 18 |
/* History: 8.12.94 begin, ug3-version */ |
| 19 |
/* */ |
| 20 |
/* Remarks: */ |
| 21 |
/* */ |
| 22 |
/****************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
/****************************************************************************/ |
| 26 |
/* */ |
| 27 |
/* include files */ |
| 28 |
/* system include files */ |
| 29 |
/* application include files */ |
| 30 |
/* */ |
| 31 |
/****************************************************************************/ |
| 32 |
|
| 33 |
#include <config.h> |
| 34 |
#include <cmath> |
| 35 |
#include <cassert> |
| 36 |
#include <cstdlib> |
| 37 |
#include <cstddef> |
| 38 |
|
| 39 |
#include <dune/uggrid/low/architecture.h> |
| 40 |
#include <dune/uggrid/low/misc.h> |
| 41 |
#include <dune/uggrid/ugdevices.h> |
| 42 |
#include <dune/uggrid/low/ugtypes.h> |
| 43 |
|
| 44 |
#include "elements.h" |
| 45 |
#include "evm.h" |
| 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 OneSixth 0.166666666666666667 |
| 61 |
|
| 62 |
/****************************************************************************/ |
| 63 |
/* */ |
| 64 |
/* data structures used in this source file (exported data structures are */ |
| 65 |
/* in the corresponding include file!) */ |
| 66 |
/* */ |
| 67 |
/****************************************************************************/ |
| 68 |
|
| 69 |
/****************************************************************************/ |
| 70 |
/* */ |
| 71 |
/* forward declarations of macros */ |
| 72 |
/* */ |
| 73 |
/****************************************************************************/ |
| 74 |
|
| 75 |
#define MIN_DETERMINANT 0.0001*SMALL_C |
| 76 |
|
| 77 |
/****************************************************************************/ |
| 78 |
/****************************************************************************/ |
| 79 |
/****************************************************************************/ |
| 80 |
/**** ****/ |
| 81 |
/**** 2D routines ****/ |
| 82 |
/**** ****/ |
| 83 |
/****************************************************************************/ |
| 84 |
/****************************************************************************/ |
| 85 |
/****************************************************************************/ |
| 86 |
|
| 87 |
/****************************************************************************/ |
| 88 |
/*D |
| 89 |
M3_Invert - Calculate inverse of a 3x3 DOUBLE matrix |
| 90 |
|
| 91 |
SYNOPSIS: |
| 92 |
INT M3_Invert (DOUBLE *Inverse, const DOUBLE *Matrix); |
| 93 |
|
| 94 |
PARAMETERS: |
| 95 |
. Inverse - inverse of matrix |
| 96 |
. Matrix - matrix |
| 97 |
|
| 98 |
DESCRIPTION: |
| 99 |
This function calculates inverse of a 3x3 DOUBLE matrix. |
| 100 |
The entries of the matrices are given in a linear array with the |
| 101 |
following order - |
| 102 |
|
| 103 |
.vb |
| 104 |
| Matrix[0] Matrix[1] Matrix[2]| |
| 105 |
| Matrix[3] Matrix[4] Matrix[5]| |
| 106 |
| Matrix[6] Matrix[7] Matrix[8]| |
| 107 |
.ve |
| 108 |
|
| 109 |
RETURN VALUE: |
| 110 |
INT |
| 111 |
.n 0 when ok |
| 112 |
.n 1 when matrix is nearly singular. |
| 113 |
D*/ |
| 114 |
/****************************************************************************/ |
| 115 |
|
| 116 |
INT NS_DIM_PREFIX M3_Invert (DOUBLE *Inverse, const DOUBLE *Matrix) |
| 117 |
{ |
| 118 |
DOUBLE determinant,invdet; |
| 119 |
INT i,i1,i2, j,j1,j2; |
| 120 |
|
| 121 |
for (i=0; i<3; i++) |
| 122 |
{ |
| 123 |
i1 = (i+1)%3; |
| 124 |
i2 = (i+2)%3; |
| 125 |
for ( j=0; j<3; j++) |
| 126 |
{ |
| 127 |
j1 = (j+1)%3; |
| 128 |
j2 = (j+2)%3; |
| 129 |
Inverse[j+3*i] = Matrix[i1+3*j1]*Matrix[i2+3*j2] - Matrix[i1+3*j2]*Matrix[i2+3*j1]; |
| 130 |
} |
| 131 |
} |
| 132 |
determinant = Inverse[0+3*0]*Matrix[0+3*0] + Inverse[0+3*1]*Matrix[1+3*0] + Inverse[0+3*2]*Matrix[2+3*0]; |
| 133 |
|
| 134 |
/* check the determinant */ |
| 135 |
if (fabs(determinant) > MIN_DETERMINANT) |
| 136 |
{ |
| 137 |
invdet = 1.0/determinant; |
| 138 |
for (i=0; i<3; i++) |
| 139 |
for (j=0; j<3; j++) |
| 140 |
Inverse[i+3*j] *= invdet; |
| 141 |
return(0); |
| 142 |
} |
| 143 |
|
| 144 |
return(1); |
| 145 |
} |
| 146 |
|
| 147 |
/****************************************************************************/ |
| 148 |
/*D |
| 149 |
vp - Return positive number if vector 2 is "left" of vector 1 |
| 150 |
|
| 151 |
SYNOPSIS: |
| 152 |
DOUBLE vp (const DOUBLE x1, const DOUBLE y1, const DOUBLE x2, const DOUBLE y2); |
| 153 |
|
| 154 |
PARAMETERS: |
| 155 |
. x1,y1 - coordinates of a 2D vector |
| 156 |
. x2,y2 - coordinates of a 2D vector |
| 157 |
|
| 158 |
DESCRIPTION: |
| 159 |
This function returns positive number if vector 2 is "left" of vector 1, i.e. |
| 160 |
the third component of the vector product of (x1,y1,0) and (x2,y2,0). |
| 161 |
|
| 162 |
RETURN VALUE: |
| 163 |
DOUBLE |
| 164 |
D*/ |
| 165 |
/****************************************************************************/ |
| 166 |
|
| 167 |
DOUBLE NS_DIM_PREFIX vp (const DOUBLE x1, const DOUBLE y1, const DOUBLE x2, const DOUBLE y2) |
| 168 |
{ |
| 169 |
DOUBLE l1,l2; |
| 170 |
|
| 171 |
l1 = sqrt(x1*x1+y1*y1); |
| 172 |
l2 = sqrt(x2*x2+y2*y2); |
| 173 |
if ((l1<SMALL_D)||(l2<SMALL_D)) |
| 174 |
return(0.0); |
| 175 |
else |
| 176 |
return((x1*y2-y1*x2)/(l1*l2)); |
| 177 |
} |
| 178 |
|
| 179 |
/****************************************************************************/ |
| 180 |
/*D |
| 181 |
c_tarea - Compute area of triangle |
| 182 |
|
| 183 |
SYNOPSIS: |
| 184 |
DOUBLE c_tarea (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2) |
| 185 |
|
| 186 |
PARAMETERS: |
| 187 |
. x0 - Array with coordinates of first point |
| 188 |
. x1 - Array with coordinates of second point |
| 189 |
. x2 - Array with coordinates of third point |
| 190 |
|
| 191 |
DESCRIPTION: |
| 192 |
This function computes the area of a triangle. |
| 193 |
|
| 194 |
RETURN VALUE: |
| 195 |
DOUBLE area |
| 196 |
D*/ |
| 197 |
/****************************************************************************/ |
| 198 |
DOUBLE NS_DIM_PREFIX c_tarea (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2) |
| 199 |
{ |
| 200 |
return(0.5*fabs((x1[1]-x0[1])*(x2[0]-x0[0])-(x1[0]-x0[0])*(x2[1]-x0[1]))); |
| 201 |
} |
| 202 |
/****************************************************************************/ |
| 203 |
/*D |
| 204 |
c_qarea - Compute area of a convex quadrilateral |
| 205 |
|
| 206 |
SYNOPSIS: |
| 207 |
DOUBLE c_qarea (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2, const DOUBLE *x3); |
| 208 |
|
| 209 |
PARAMETERS: |
| 210 |
. x0 - Array with coordinates of first point |
| 211 |
. x1 - Array with coordinates of second point |
| 212 |
. x2 - Array with coordinates of third point |
| 213 |
. x3 - Array with coordinates of fourth point |
| 214 |
|
| 215 |
DESCRIPTION: |
| 216 |
This function computes the area of a convex quadrilateral. |
| 217 |
|
| 218 |
RETURN VALUE: |
| 219 |
DOUBLE area |
| 220 |
D*/ |
| 221 |
/****************************************************************************/ |
| 222 |
DOUBLE NS_DIM_PREFIX c_qarea (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2, const DOUBLE *x3) |
| 223 |
{ |
| 224 |
return( 0.5*fabs( (x3[1]-x1[1])*(x2[0]-x0[0])-(x3[0]-x1[0])*(x2[1]-x0[1]) ) ); |
| 225 |
} |
| 226 |
|
| 227 |
/****************************************************************************/ |
| 228 |
/****************************************************************************/ |
| 229 |
/****************************************************************************/ |
| 230 |
/**** ****/ |
| 231 |
/**** 3D routines ****/ |
| 232 |
/**** ****/ |
| 233 |
/****************************************************************************/ |
| 234 |
/****************************************************************************/ |
| 235 |
/****************************************************************************/ |
| 236 |
|
| 237 |
/****************************************************************************/ |
| 238 |
/*D |
| 239 |
V3_Normalize - Normalize vector a 3D vector |
| 240 |
|
| 241 |
SYNOPSIS: |
| 242 |
INT V3_Normalize (DOUBLE *a); |
| 243 |
|
| 244 |
PARAMETERS: |
| 245 |
. a - 3D vector |
| 246 |
|
| 247 |
DESCRIPTION: |
| 248 |
This function normalizes vector a. |
| 249 |
|
| 250 |
RETURN VALUE: |
| 251 |
INT |
| 252 |
.n 0 if ok |
| 253 |
.n 2 if vector is nearly 0. |
| 254 |
D*/ |
| 255 |
/****************************************************************************/ |
| 256 |
|
| 257 |
INT NS_DIM_PREFIX V3_Normalize (FieldVector<DOUBLE,3>& a) |
| 258 |
{ |
| 259 |
DOUBLE norm; |
| 260 |
|
| 261 |
V3_EUKLIDNORM(a,norm); |
| 262 |
if (norm < SMALL_C) return(2); |
| 263 |
V3_SCALE(1.0/norm,a); |
| 264 |
|
| 265 |
return(0); |
| 266 |
} |
| 267 |
|
| 268 |
/* Volume computations, orientation is same as in general element definition ! |
| 269 |
Idea of the computations: |
| 270 |
All the shapes are subdivided into pyramids whose bases are either triangles |
| 271 |
or quadrilaterals. The volume of any pyramid is V = S h / 3 where S is |
| 272 |
area of the base and h is height of the pyramid. The areas of the triangles |
| 273 |
and the quadrilaterals are computed by means of vector products. The |
| 274 |
height of the pyramid is then taken into account using scalar products. |
| 275 |
*/ |
| 276 |
|
| 277 |
/* 1. Volume of a tetrahedron. |
| 278 |
A tetrahedron is a prism with a triangular base. |
| 279 |
*/ |
| 280 |
DOUBLE NS_DIM_PREFIX V_te (const DOUBLE *x0, const DOUBLE *x1, |
| 281 |
const DOUBLE *x2, const DOUBLE *x3) |
| 282 |
{ |
| 283 |
FieldVector<DOUBLE,3> a, b, h, n; |
| 284 |
|
| 285 |
V3_SUBTRACT(x1,x0,a); |
| 286 |
V3_SUBTRACT(x2,x0,b); |
| 287 |
V3_SUBTRACT(x3,x0,h); |
| 288 |
V3_VECTOR_PRODUCT(a,b,n); |
| 289 |
|
| 290 |
return(OneSixth*V3_SCAL_PROD(n,h)); |
| 291 |
} |
| 292 |
|
| 293 |
/* 2. Volume of an UG pyramid. |
| 294 |
An UG pyramid is a pyramid with a quadrilateral as the base. |
| 295 |
*/ |
| 296 |
DOUBLE NS_DIM_PREFIX V_py (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2, |
| 297 |
const DOUBLE *x3, const DOUBLE *x4) |
| 298 |
{ |
| 299 |
FieldVector<DOUBLE,3> a,b,h,n; |
| 300 |
|
| 301 |
V3_SUBTRACT(x2,x0,a); |
| 302 |
V3_SUBTRACT(x3,x1,b); |
| 303 |
V3_SUBTRACT(x4,x0,h); |
| 304 |
V3_VECTOR_PRODUCT(a,b,n); |
| 305 |
|
| 306 |
return(OneSixth*V3_SCAL_PROD(n,h)); |
| 307 |
} |
| 308 |
|
| 309 |
/* 3. Volume of an UG prism. |
| 310 |
An UG prism is a shape with two (in general non-parallel) bases that |
| 311 |
are triangles and four sides that are quadrilaterals. This 'prism' |
| 312 |
is subdivided into two pyramids: |
| 313 |
a) {x0, x_1, x_4, x_3; x_5} |
| 314 |
b) {x_0, x_1, x_2; x_5} (a tetrahedron) |
| 315 |
*/ |
| 316 |
DOUBLE NS_DIM_PREFIX V_pr (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2, |
| 317 |
const DOUBLE *x3, const DOUBLE *x4, const DOUBLE *x5) |
| 318 |
{ |
| 319 |
FieldVector<DOUBLE,3> a,b,c,d,e,m,n; |
| 320 |
|
| 321 |
V3_SUBTRACT(x4,x0,a); |
| 322 |
V3_SUBTRACT(x1,x3,b); |
| 323 |
V3_SUBTRACT(x1,x0,c); |
| 324 |
V3_SUBTRACT(x2,x0,d); |
| 325 |
V3_SUBTRACT(x5,x0,e); |
| 326 |
|
| 327 |
V3_VECTOR_PRODUCT(a,b,m); |
| 328 |
V3_VECTOR_PRODUCT(c,d,n); |
| 329 |
V3_ADD(n,m,n); |
| 330 |
|
| 331 |
return(OneSixth*V3_SCAL_PROD(n,e)); |
| 332 |
} |
| 333 |
|
| 334 |
/* 4. Volume of an UG hexahedron. |
| 335 |
An UG hexahedron is subdivided into two UG prisms. |
| 336 |
*/ |
| 337 |
DOUBLE NS_DIM_PREFIX V_he (const DOUBLE *x0, const DOUBLE *x1, const DOUBLE *x2, const DOUBLE *x3, |
| 338 |
const DOUBLE *x4, const DOUBLE *x5, const DOUBLE *x6, const DOUBLE *x7) |
| 339 |
{ |
| 340 |
return(V_pr(x0,x1,x2,x4,x5,x6)+V_pr(x0,x2,x3,x4,x6,x7)); |
| 341 |
} |
| 342 |
|
| 343 |
DOUBLE NS_DIM_PREFIX GeneralElementVolume (INT tag, DOUBLE *x_co[]) |
| 344 |
{ |
| 345 |
switch (tag) |
| 346 |
{ |
| 347 |
# ifdef UG_DIM_2 |
| 348 |
case TRIANGLE : |
| 349 |
return(c_tarea (x_co[0],x_co[1],x_co[2])); |
| 350 |
|
| 351 |
case QUADRILATERAL : |
| 352 |
return(c_qarea (x_co[0],x_co[1],x_co[2],x_co[3])); |
| 353 |
# endif |
| 354 |
|
| 355 |
# ifdef UG_DIM_3 |
| 356 |
case TETRAHEDRON : |
| 357 |
return(V_te(x_co[0],x_co[1],x_co[2],x_co[3])); |
| 358 |
|
| 359 |
case PYRAMID : |
| 360 |
return (V_py(x_co[0],x_co[1],x_co[2],x_co[3],x_co[4])); |
| 361 |
|
| 362 |
case PRISM : |
| 363 |
return (V_pr(x_co[0],x_co[1],x_co[2],x_co[3],x_co[4],x_co[5])); |
| 364 |
|
| 365 |
case HEXAHEDRON : |
| 366 |
return(V_he(x_co[0],x_co[1],x_co[2],x_co[3],x_co[4],x_co[5],x_co[6],x_co[7])); |
| 367 |
# endif |
| 368 |
|
| 369 |
default : |
| 370 |
PrintErrorMessage('E',"GeneralElementVolume","unknown element"); |
| 371 |
return(0.0); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
DOUBLE NS_DIM_PREFIX ElementVolume (const ELEMENT *elem) |
| 376 |
{ |
| 377 |
DOUBLE *x_co[MAX_CORNERS_OF_ELEM]; |
| 378 |
INT i; |
| 379 |
|
| 380 |
for (i=0; i<CORNERS_OF_ELEM(elem); i++) |
| 381 |
x_co[i] = CVECT(MYVERTEX(CORNER(elem,i))).data(); |
| 382 |
|
| 383 |
return (GeneralElementVolume(TAG(elem),x_co)); |
| 384 |
} |