PeriDEM 0.2.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 - 2024 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 <cmath> // definition of sin, cosine etc
13#include <iostream> // cerr
14
15bool util::isGreater(const double &a, const double &b) {
16 return (a - b) > ((std::abs(a) < std::abs(b) ? std::abs(b) : std::abs(a)) *
18}
19
20bool util::isLess(const double &a, const double &b) {
21 return (b - a) > ((std::abs(a) < std::abs(b) ? std::abs(b) : std::abs(a)) *
23}
24
25double util::hatFunction(const double &x, const double &x_min,
26 const double &x_max) {
27
28 if (util::isGreater(x, x_min - 1.0E-12) and
29 util::isLess(x, x_max + 1.0E-12)) {
30
31 double x_mid = 0.5 * (x_min + x_max);
32 double l = x_mid - x_min;
33
34 // check if this is essentially a point load (dirac)
35 if (l < 1.0E-12)
36 return 1.0;
37
38 if (util::isLess(x, x_mid))
39 return (x - x_min) / l;
40 else
41 return (x_max - x) / l;
42 } else
43 return 0.0;
44}
45
46double util::hatFunctionQuick(const double &x, const double &x_min,
47 const double &x_max) {
48
49 double x_mid = 0.5 * (x_min + x_max);
50 double l = x_mid - x_min;
51
52 // check if this is essentially a point load (dirac)
53 if (l < 1.0E-12)
54 return 1.0;
55
56 if (util::isLess(x, x_mid))
57 return (x - x_min) / l;
58 else
59 return (x_max - x) / l;
60}
61
62double util::linearStepFunc(const double &x, const double &x1,
63 const double &x2) {
64
65 //
66 // a = floor(x/(x1+x2))
67 // xl = a * (x1 + x2), xm = xl + x1, xr = xm + x2
68 // fl = a * x1
69 //
70 // At xl, value of the function is = period number \times x1
71 //
72 // From xl to xm, the function grows linear with slope 1
73 // so the value in between [xl, xm) will be
74 // fl + (x - xl) = a*x1 + (x - a*(x1+x2)) = x - a*x2
75 //
76 // In [xm, xr) function is constant and the value is
77 // fl + (xm - xl) = a*x1 + (xl + x1 - xl) = (a+1)*x1
78
79 double period = std::floor(x / (x1 + x2));
80
81 if (util::isLess(x, period * (x1 + x2) + x1))
82 return x - period * x2;
83 else
84 return (period + 1.) * x1;
85}
86
87double util::gaussian(const double &r, const double &a,
88 const double &beta) {
89 return a * std::exp(-std::pow(r, 2) / beta);
90}
91
92double util::gaussian2d(const util::Point &x, const size_t &dof,
93 const std::vector<double> &params) {
94
95 if (params.size() < 6) {
96 std::cerr << "Error: Not enough parameters to compute guassian 2-d "
97 "function.\n";
98 exit(1);
99 }
100
101 return util::gaussian(
102 x.dist(util::Point(params[0], params[1], 0.)), params[5],
103 params[4]) *
104 params[2 + dof];
105}
106
108 const size_t &dof,
109 const std::vector<double> &params) {
110
111 if (params.size() < 10) {
112 std::cerr << "Error: Not enough parameters to compute guassian 2-d "
113 "function.\n";
114 exit(1);
115 }
116
117 return util::gaussian(
118 x.dist(util::Point(params[0], params[1], 0.)), params[9],
119 params[8]) *
120 params[4 + dof] +
122 x.dist(util::Point(params[2], params[3], 0.)), params[9],
123 params[8]) *
124 params[6 + dof];
125}
126
127double util::equivalentMass(const double &m1, const double &m2) {
128 return 2. * m1 * m2 / (m1 + m2);
129}
#define COMPARE_EPS
Definition function.h:18
bool isGreater(const double &a, const double &b)
Returns true if a > b.
Definition function.cpp:15
double hatFunction(const double &x, const double &x_min, const double &x_max)
Computes hat function at given point.
Definition function.cpp:25
double linearStepFunc(const double &x, const double &x1, const double &x2)
Compute linear step function.
Definition function.cpp:62
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:107
double equivalentMass(const double &m1, const double &m2)
Compute harmonic mean of m1 and m2.
Definition function.cpp:127
double hatFunctionQuick(const double &x, const double &x_min, const double &x_max)
Computes hat function at given point.
Definition function.cpp:46
double gaussian(const double &r, const double &a, const double &beta)
Compute gaussian function in 1-d.
Definition function.cpp:87
bool isLess(const double &a, const double &b)
Returns true if a < b.
Definition function.cpp:20
double gaussian2d(const util::Point &x, const size_t &dof, const std::vector< double > &params)
Compute gaussian function in 2-d.
Definition function.cpp:92
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:140