PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
hexElem.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "hexElem.h"
12#include "util/feElementDefs.h"
13#include "util/matrix.h"
14
15#include <cmath>
16#include <vector>
17
18namespace {
19
20// VTK / Gmsh hex corner signs on [-1,1]^3
21constexpr double SX[8] = {-1., 1., 1., -1., -1., 1., 1., -1.};
22constexpr double SY[8] = {-1., -1., 1., 1., -1., -1., 1., 1.};
23constexpr double SZ[8] = {-1., -1., -1., -1., 1., 1., 1., 1.};
24
25} // namespace
26
28 : fe::BaseElem(order, util::vtk_type_hexahedron) {
29 this->init();
30}
31
32double fe::HexElem::elemSize(const std::vector<util::Point> &nodes) {
33 // Reference cube volume is 8; for a parallelepiped V = 8 * det(J(0,0,0)).
34 // General hex: integrate |det J| with the stored reference quadrature.
35 if (d_quads.empty())
36 return 8. * getJacobian(util::Point(0., 0., 0.), nodes, nullptr);
37 double vol = 0.;
38 for (const auto &qd : d_quads)
39 vol += qd.d_w * getJacobian(qd.d_p, nodes, nullptr);
40 return vol;
41}
42
43std::vector<double> fe::HexElem::getShapes(const util::Point &p) {
44 std::vector<double> N(8);
45 for (size_t i = 0; i < 8; ++i)
46 N[i] = 0.125 * (1. + SX[i] * p.d_x) * (1. + SY[i] * p.d_y) *
47 (1. + SZ[i] * p.d_z);
48 return N;
49}
50
51std::vector<std::vector<double>>
53 std::vector<std::vector<double>> r(8, std::vector<double>(3, 0.));
54 for (size_t i = 0; i < 8; ++i) {
55 r[i][0] = 0.125 * SX[i] * (1. + SY[i] * p.d_y) * (1. + SZ[i] * p.d_z);
56 r[i][1] = 0.125 * SY[i] * (1. + SX[i] * p.d_x) * (1. + SZ[i] * p.d_z);
57 r[i][2] = 0.125 * SZ[i] * (1. + SX[i] * p.d_x) * (1. + SY[i] * p.d_y);
58 }
59 return r;
60}
61
63 const std::vector<util::Point> &nodes,
64 std::vector<std::vector<double>> *J) {
65 auto der = getDerShapes(p);
66 std::vector<std::vector<double>> Jloc(3, std::vector<double>(3, 0.));
67 for (size_t a = 0; a < 8; ++a) {
68 Jloc[0][0] += der[a][0] * nodes[a].d_x;
69 Jloc[0][1] += der[a][0] * nodes[a].d_y;
70 Jloc[0][2] += der[a][0] * nodes[a].d_z;
71 Jloc[1][0] += der[a][1] * nodes[a].d_x;
72 Jloc[1][1] += der[a][1] * nodes[a].d_y;
73 Jloc[1][2] += der[a][1] * nodes[a].d_z;
74 Jloc[2][0] += der[a][2] * nodes[a].d_x;
75 Jloc[2][1] += der[a][2] * nodes[a].d_y;
76 Jloc[2][2] += der[a][2] * nodes[a].d_z;
77 }
78 if (J != nullptr)
79 *J = Jloc;
80 return util::det(Jloc);
81}
82
84 if (!d_quads.empty())
85 return;
86
87 if (d_quadOrder == 0) {
88 d_quads.resize(0);
89 return;
90 }
91
92 std::vector<std::vector<double>> ident(3, std::vector<double>(3, 0.));
93 ident[0][0] = ident[1][1] = ident[2][2] = 1.;
94
95 std::vector<double> x;
96 std::vector<double> w;
97 if (d_quadOrder == 1) {
98 x = {0.};
99 w = {2.};
100 } else if (d_quadOrder == 2) {
101 x = {-1. / std::sqrt(3.), 1. / std::sqrt(3.)};
102 w = {1., 1.};
103 } else {
104 // order >= 3: 3-point Gauss on [-1,1]
105 x = {-std::sqrt(3.) / std::sqrt(5.), 0., std::sqrt(3.) / std::sqrt(5.)};
106 w = {5. / 9., 8. / 9., 5. / 9.};
107 }
108
109 const size_t npts = x.size();
110 d_quads.clear();
111 d_quads.reserve(npts * npts * npts);
112 for (size_t i = 0; i < npts; ++i)
113 for (size_t j = 0; j < npts; ++j)
114 for (size_t k = 0; k < npts; ++k) {
115 fe::QuadData qd;
116 qd.d_w = w[i] * w[j] * w[k];
117 qd.d_p = util::Point(x[i], x[j], x[k]);
118 qd.d_shapes = getShapes(qd.d_p);
119 qd.d_derShapes = getDerShapes(qd.d_p);
120 qd.d_J = ident;
121 qd.d_detJ = 1.;
122 d_quads.push_back(qd);
123 }
124}
A base class which provides methods to map points to/from reference element and to compute quadrature...
Definition baseElem.h:84
HexElem(size_t order)
Definition hexElem.cpp:27
double getJacobian(const util::Point &p, const std::vector< util::Point > &nodes, std::vector< std::vector< double > > *J) override
Computes Jacobian of map from reference element to given element .
Definition hexElem.cpp:62
std::vector< double > getShapes(const util::Point &p) override
Returns the values of shape function at point p on reference element.
Definition hexElem.cpp:43
std::vector< std::vector< double > > getDerShapes(const util::Point &p) override
Returns the values of derivative of shape function at point p on reference element.
Definition hexElem.cpp:52
double elemSize(const std::vector< util::Point > &nodes) override
Returns the size of element (length in 1-d, area in 2-d, volume in 3-d element)
Definition hexElem.cpp:32
void init() override
Compute the quadrature points.
Definition hexElem.cpp:83
Definition baseElem.h:17
Collection of methods useful in simulation.
Definition constants.h:14
double det(const std::vector< std::vector< double > > &m)
Computes the determinant of matrix.
Definition matrix.cpp:75
A struct to store the quadrature data. List of data are.
Definition quadData.h:23
std::vector< double > d_shapes
Value of shape functions at quad point p.
Definition quadData.h:37
std::vector< std::vector< double > > d_derShapes
Derivatives of shape functions at the quad point.
Definition quadData.h:46
double d_w
Quadrature weight.
Definition quadData.h:26
util::Point d_p
Quadrature point in 1-d, 2-d or 3-d.
Definition quadData.h:29
std::vector< std::vector< double > > d_J
Jacobian of the map from reference element to the element.
Definition quadData.h:54
double d_detJ
Determinant of the Jacobian of the map from reference element to the element.
Definition quadData.h:60
A structure to represent 3d vectors.
Definition point.h:30
double d_y
the y coordinate
Definition point.h:36
double d_z
the z coordinate
Definition point.h:39
double d_x
the x coordinate
Definition point.h:33