PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
function.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 "function.h"
12#include "util/io.h"
13#include <cmath> // definition of sin, cosine etc
14#include <iostream> // cerr
15#include <stdexcept> // invalid_argument
16
17bool util::isGreater(const double &a, const double &b) {
18 return (a - b) > ((std::abs(a) < std::abs(b) ? std::abs(b) : std::abs(a)) *
20}
21
22bool util::isLess(const double &a, const double &b) {
23 return (b - a) > ((std::abs(a) < std::abs(b) ? std::abs(b) : std::abs(a)) *
25}
26
27double util::hatFunction(const double &x, const double &x_min,
28 const double &x_max) {
29
30 if (util::isGreater(x, x_min - 1.0E-12) and
31 util::isLess(x, x_max + 1.0E-12)) {
32
33 double x_mid = 0.5 * (x_min + x_max);
34 double l = x_mid - x_min;
35
36 // support below this width is treated as a point load (dirac)
37 if (l < 1.0E-12)
38 return 1.0;
39
40 if (util::isLess(x, x_mid))
41 return (x - x_min) / l;
42 else
43 return (x_max - x) / l;
44 } else
45 return 0.0;
46}
47
48double util::hatFunctionQuick(const double &x, const double &x_min,
49 const double &x_max) {
50
51 double x_mid = 0.5 * (x_min + x_max);
52 double l = x_mid - x_min;
53
54 // support below this width is treated as a point load (dirac)
55 if (l < 1.0E-12)
56 return 1.0;
57
58 if (util::isLess(x, x_mid))
59 return (x - x_min) / l;
60 else
61 return (x_max - x) / l;
62}
63
64double util::linearStepFunc(const double &x, const double &x1,
65 const double &x2) {
66
67 //
68 // a = floor(x/(x1+x2))
69 // xl = a * (x1 + x2), xm = xl + x1, xr = xm + x2
70 // fl = a * x1
71 //
72 // At xl, value of the function is = period number \times x1
73 //
74 // From xl to xm, the function grows linear with slope 1
75 // so the value in between [xl, xm) will be
76 // fl + (x - xl) = a*x1 + (x - a*(x1+x2)) = x - a*x2
77 //
78 // In [xm, xr) function is constant and the value is
79 // fl + (xm - xl) = a*x1 + (xl + x1 - xl) = (a+1)*x1
80
81 double period = std::floor(x / (x1 + x2));
82
83 if (util::isLess(x, period * (x1 + x2) + x1))
84 return x - period * x2;
85 else
86 return (period + 1.) * x1;
87}
88
89double util::gaussian(const double &r, const double &a,
90 const double &beta) {
91 return a * std::exp(-std::pow(r, 2) / beta);
92}
93
94double util::gaussian2d(const util::Point &x, const size_t &dof,
95 const std::vector<double> &params) {
96
97 if (params.size() < 6) {
98 throw std::runtime_error(
100 << "Error: Not enough parameters to compute guassian 2-d "
101 "function.\n");
102 }
103
104 return util::gaussian(
105 x.dist(util::Point(params[0], params[1], 0.)), params[5],
106 params[4]) *
107 params[2 + dof];
108}
109
111 const size_t &dof,
112 const std::vector<double> &params) {
113
114 if (params.size() < 10) {
115 throw std::runtime_error(
117 << "Error: Not enough parameters to compute guassian 2-d "
118 "function.\n");
119 }
120
121 return util::gaussian(
122 x.dist(util::Point(params[0], params[1], 0.)), params[9],
123 params[8]) *
124 params[4 + dof] +
126 x.dist(util::Point(params[2], params[3], 0.)), params[9],
127 params[8]) *
128 params[6 + dof];
129}
130
131double util::equivalentMass(const double &m1, const double &m2) {
132 return 2. * m1 * m2 / (m1 + m2);
133}
134
135double util::harmonicMean(const double &m1, const double &m2) {
136 return 2. * m1 * m2 / (m1 + m2);
137}
138
139double util::normalContactStiffness(const double &K1, const double &K2,
140 const double &horizon, int horizonPower) {
141 if (!(horizon > 0.))
142 throw std::invalid_argument(
143 "normalContactStiffness: horizon must be positive");
144 return 18. * util::harmonicMean(K1, K2) /
145 (M_PI * std::pow(horizon, horizonPower));
146}
147
148double util::selfContactStiffness(const double &K, const double &horizon,
149 int horizonPower) {
150 if (!(horizon > 0.))
151 throw std::invalid_argument(
152 "selfContactStiffness: horizon must be positive");
153 // Grouping preserved from BaseParticle for bit-for-bit equality.
154 return (18. / (M_PI * std::pow(horizon, horizonPower))) * K;
155}
Collects a message with stream syntax for use in an exception.
Definition io.h:52
#define COMPARE_EPS
Definition function.h:18
double selfContactStiffness(const double &K, const double &horizon, int horizonPower=5)
Contact stiffness for a body against itself.
Definition function.cpp:148
double normalContactStiffness(const double &K1, const double &K2, const double &horizon, int horizonPower=5)
Silling normal contact stiffness from two bulk moduli and the horizon.
Definition function.cpp:139
bool isGreater(const double &a, const double &b)
Returns true if a > b.
Definition function.cpp:17
double hatFunction(const double &x, const double &x_min, const double &x_max)
Computes hat function at given point.
Definition function.cpp:27
double linearStepFunc(const double &x, const double &x1, const double &x2)
Compute linear step function.
Definition function.cpp:64
double doubleGaussian2d(const util::Point &x, const size_t &dof, const std::vector< double > &params)
Compute sum of two gaussian function in 2-d.
Definition function.cpp:110
double equivalentMass(const double &m1, const double &m2)
Compute harmonic mean of m1 and m2.
Definition function.cpp:131
double hatFunctionQuick(const double &x, const double &x_min, const double &x_max)
Computes hat function at given point.
Definition function.cpp:48
double gaussian(const double &r, const double &a, const double &beta)
Compute gaussian function in 1-d.
Definition function.cpp:89
bool isLess(const double &a, const double &b)
Returns true if a < b.
Definition function.cpp:22
double gaussian2d(const util::Point &x, const size_t &dof, const std::vector< double > &params)
Compute gaussian function in 2-d.
Definition function.cpp:94
double harmonicMean(const double &m1, const double &m2)
Definition function.cpp:135
A structure to represent 3d vectors.
Definition point.h:30
double dist(const Point &b) const
Computes the distance between a given point from this point.
Definition point.h:146