PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
lineElem.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 "lineElem.h"
12#include "util/io.h"
13#include <stdexcept>
14#include "util/feElementDefs.h" // global definition of elements
15#include "util/function.h"
16#include <iostream>
17
19 : fe::BaseElem(order, util::vtk_type_line) {
20
21 // compute quad data
22 this->init();
23}
24
25double fe::LineElem::elemSize(const std::vector<util::Point> &nodes) {
26 return (nodes[1].d_x - nodes[0].d_x);
27}
28
29std::vector<double>
31 const std::vector<util::Point> &nodes) {
32 return getShapes(mapPointToRefElem(p, nodes));
33}
34
35std::vector<std::vector<double>>
37 const std::vector<util::Point> &nodes) {
38 // get derivatives of shape function in reference triangle
39 auto ders_ref = getDerShapes(mapPointToRefElem(p, nodes));
40
41 // get Jacobian
42 auto detJ = getJacobian(p, nodes, nullptr);
43
44 // modify derivatives of shape function
45 ders_ref[0][0] = ders_ref[0][0] / detJ;
46 ders_ref[1][0] = ders_ref[1][0] / detJ;
47
48 return ders_ref;
49}
50
51std::vector<double> fe::LineElem::getShapes(const util::Point &p) {
52
53 // N1 = (1 - xi)/2
54 // N2 = (1 + xi)/2
55 return std::vector<double>{0.5 * (1. - p.d_x), 0.5 * (1. + p.d_x)};
56}
57
58std::vector<std::vector<double>>
60
61 // N1 = (1 - xi)/2
62 // --> d N1/d xi = -1/2
63 //
64 // N2 = (1 + xi)/2
65 // --> d N2/d xi = 1/2
66 std::vector<std::vector<double>> r;
67 r.push_back(std::vector<double>{-0.5});
68 r.push_back(std::vector<double>{0.5});
69 return r;
70}
71
74 const std::vector<util::Point> &nodes) {
75 auto xi = (2. * p.d_x - nodes[0].d_x - nodes[1].d_x) /
76 (nodes[0].d_x - nodes[1].d_x);
77
78 if (util::isLess(xi, -1. - 1.0E-8) ||
79 util::isGreater(xi, 1. + 1.0E-8) ) {
80 throw std::runtime_error(
82 << "Error: Trying to map point p = " << p.d_x
83 << " in given line to reference line.\n"
84 << "But the point p does not belong to line = {"
85 << nodes[0].d_x << ", " << nodes[1].d_x << "}.\n");
86 }
87
88 if (util::isLess(xi, -1.))
89 xi = -1.;
90 if (util::isGreater(xi, 1.))
91 xi = 1.;
92
93 return {xi, 0., 0.};
94}
95
97 const std::vector<util::Point> &nodes,
98 std::vector<std::vector<double>> *J) {
99 // Reference line is [-1, 1], dN/dξ = ±1/2, so J = dx/dξ = L/2.
100 const double Jxi = 0.5 * (nodes[1].d_x - nodes[0].d_x);
101
102 if (J != nullptr) {
103 J->resize(1);
104 (*J)[0] = std::vector<double>{Jxi};
105 }
106
107 return Jxi;
108}
109
111
112 //
113 // compute quad data for reference line element with vertex at p1 = -1 and
114 // p2 = 1
115 //
116 // Shape functions are
117 // N1 = (1 - xi)/2
118 // N2 = (1 + xi)/2
119
120 if (!d_quads.empty())
121 return;
122
123 // no point in zeroth order
124 if (d_quadOrder == 0)
125 d_quads.resize(0);
126
127 // 1x1 identity matrix
128 std::vector<std::vector<double>> ident_mat;
129 ident_mat.push_back(std::vector<double>{1.});
130
131 //
132 // first order quad points
133 //
134 if (d_quadOrder == 1) {
135 d_quads.clear();
136 // 1-d points are: {0} and weights are: {2}
137 fe::QuadData qd;
138 qd.d_w = 2.;
139 qd.d_p = util::Point();
140 qd.d_shapes = getShapes(qd.d_p);
141 qd.d_derShapes = getDerShapes(qd.d_p);
142 qd.d_J = ident_mat;
143 qd.d_detJ = 1.;
144 d_quads.push_back(qd);
145 }
146
147 //
148 // second order quad points
149 //
150 if (d_quadOrder == 2) {
151 d_quads.clear();
152 // 1-d points are: {-1/sqrt{3], 1/sqrt{3}} and weights are: {1,1}
153 fe::QuadData qd;
154 qd.d_w = 1.;
155 qd.d_p = util::Point(-1. / std::sqrt(3.), 0., 0.);
156 qd.d_shapes = getShapes(qd.d_p);
157 qd.d_derShapes = getDerShapes(qd.d_p);
158 qd.d_J = ident_mat;
159 qd.d_detJ = 1.;
160 d_quads.push_back(qd);
161
162 qd.d_w = 1.;
163 qd.d_p = util::Point(1. / std::sqrt(3.), 0., 0.);
164 qd.d_shapes = getShapes(qd.d_p);
165 qd.d_derShapes = getDerShapes(qd.d_p);
166 qd.d_J = ident_mat;
167 qd.d_detJ = 1.;
168 d_quads.push_back(qd);
169 }
170
171 //
172 // third order quad points for triangle
173 //
174 if (d_quadOrder == 3) {
175 d_quads.clear();
176 // 1-d points are: {-sqrt{3}/sqrt{5}, 0, sqrt{3}/sqrt{5}}
177 // weights are: {5/9, 8/9, 5/9}
178 fe::QuadData qd;
179 qd.d_w = 5. / 9.;
180 qd.d_p = util::Point(-std::sqrt(3.) / std::sqrt(5.), 0., 0.);
181 qd.d_shapes = getShapes(qd.d_p);
182 qd.d_derShapes = getDerShapes(qd.d_p);
183 qd.d_J = ident_mat;
184 qd.d_detJ = 1.;
185 d_quads.push_back(qd);
186
187 qd.d_w = 8. / 9.;
188 qd.d_p = util::Point();
189 qd.d_shapes = getShapes(qd.d_p);
190 qd.d_derShapes = getDerShapes(qd.d_p);
191 qd.d_J = ident_mat;
192 qd.d_detJ = 1.;
193 d_quads.push_back(qd);
194
195 qd.d_w = 5. / 9.;
196 qd.d_p = util::Point(std::sqrt(3.) / std::sqrt(5.), 0., 0.);
197 qd.d_shapes = getShapes(qd.d_p);
198 qd.d_derShapes = getDerShapes(qd.d_p);
199 qd.d_J = ident_mat;
200 qd.d_detJ = 1.;
201 d_quads.push_back(qd);
202 }
203
204 //
205 // fourth order quad points for triangle
206 //
207 if (d_quadOrder == 4) {
208 d_quads.clear();
209 fe::QuadData qd;
210 qd.d_w = 0.6521451548625461;
211 qd.d_p = util::Point(-0.3399810435848563, 0., 0.);
212 qd.d_shapes = getShapes(qd.d_p);
213 qd.d_derShapes = getDerShapes(qd.d_p);
214 qd.d_J = ident_mat;
215 qd.d_detJ = 1.;
216 d_quads.push_back(qd);
217
218 qd.d_w = 0.6521451548625461;
219 qd.d_p = util::Point(0.3399810435848563, 0., 0.);
220 qd.d_shapes = getShapes(qd.d_p);
221 qd.d_derShapes = getDerShapes(qd.d_p);
222 qd.d_J = ident_mat;
223 qd.d_detJ = 1.;
224 d_quads.push_back(qd);
225
226 qd.d_w = 0.3478548451374538;
227 qd.d_p = util::Point(-0.8611363115940526, 0., 0.);
228 qd.d_shapes = getShapes(qd.d_p);
229 qd.d_derShapes = getDerShapes(qd.d_p);
230 qd.d_J = ident_mat;
231 qd.d_detJ = 1.;
232 d_quads.push_back(qd);
233
234 qd.d_w = 0.3478548451374538;
235 qd.d_p = util::Point(0.8611363115940526, 0., 0.);
236 qd.d_shapes = getShapes(qd.d_p);
237 qd.d_derShapes = getDerShapes(qd.d_p);
238 qd.d_J = ident_mat;
239 qd.d_detJ = 1.;
240 d_quads.push_back(qd);
241 }
242
243 //
244 // fifth order quad points for triangle
245 //
246 if (d_quadOrder == 5) {
247 d_quads.clear();
248 fe::QuadData qd;
249 qd.d_w = 0.5688888888888889;
250 qd.d_p = util::Point();
251 qd.d_shapes = getShapes(qd.d_p);
252 qd.d_derShapes = getDerShapes(qd.d_p);
253 qd.d_J = ident_mat;
254 qd.d_detJ = 1.;
255 d_quads.push_back(qd);
256
257 qd.d_w = 0.4786286704993665;
258 qd.d_p = util::Point(-0.5384693101056831, 0., 0.);
259 qd.d_shapes = getShapes(qd.d_p);
260 qd.d_derShapes = getDerShapes(qd.d_p);
261 qd.d_J = ident_mat;
262 qd.d_detJ = 1.;
263 d_quads.push_back(qd);
264
265 qd.d_w = 0.4786286704993665;
266 qd.d_p = util::Point(0.5384693101056831, 0., 0.);
267 qd.d_shapes = getShapes(qd.d_p);
268 qd.d_derShapes = getDerShapes(qd.d_p);
269 qd.d_J = ident_mat;
270 qd.d_detJ = 1.;
271 d_quads.push_back(qd);
272
273 qd.d_w = 0.2369268850561891;
274 qd.d_p = util::Point(-0.9061798459386640, 0., 0.);
275 qd.d_shapes = getShapes(qd.d_p);
276 qd.d_derShapes = getDerShapes(qd.d_p);
277 qd.d_J = ident_mat;
278 qd.d_detJ = 1.;
279 d_quads.push_back(qd);
280
281 qd.d_w = 0.2369268850561891;
282 qd.d_p = util::Point(0.9061798459386640, 0., 0.);
283 qd.d_shapes = getShapes(qd.d_p);
284 qd.d_derShapes = getDerShapes(qd.d_p);
285 qd.d_J = ident_mat;
286 qd.d_detJ = 1.;
287 d_quads.push_back(qd);
288 }
289}
A base class which provides methods to map points to/from reference element and to compute quadrature...
Definition baseElem.h:84
std::vector< double > getShapes(const util::Point &p, const std::vector< util::Point > &nodes) override
Returns the values of shape function at point p.
Definition lineElem.cpp:30
LineElem(size_t order)
Constructor for line element.
Definition lineElem.cpp:18
util::Point mapPointToRefElem(const util::Point &p, const std::vector< util::Point > &nodes) override
Maps point p in a given element to the reference element.
Definition lineElem.cpp:73
void init() override
Compute the quadrature points for line element.
Definition lineElem.cpp:110
double getJacobian(const util::Point &p, const std::vector< util::Point > &nodes, std::vector< std::vector< double > > *J) override
Computes Jacobian of the map .
Definition lineElem.cpp:96
std::vector< std::vector< double > > getDerShapes(const util::Point &p, const std::vector< util::Point > &nodes) override
Returns the values of derivative of shape function at point p.
Definition lineElem.cpp:36
double elemSize(const std::vector< util::Point > &nodes) override
Returns the length of element.
Definition lineElem.cpp:25
Collects a message with stream syntax for use in an exception.
Definition io.h:52
Definition baseElem.h:17
Collection of methods useful in simulation.
Definition constants.h:14
bool isGreater(const double &a, const double &b)
Returns true if a > b.
Definition function.cpp:17
bool isLess(const double &a, const double &b)
Returns true if a < b.
Definition function.cpp:22
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_x
the x coordinate
Definition point.h:33