Skip to content

Commit f4f64d3

Browse files
authored
Move NormalizedClampedLogBarrier into src (#143)
1 parent 55abc56 commit f4f64d3

File tree

4 files changed

+74
-50
lines changed

4 files changed

+74
-50
lines changed

docs/source/cpp-api/barrier.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ Clamped Log Barrier
2727
~~~~~~~~~~~~~~~~~~~
2828

2929
.. doxygenclass:: ipc::ClampedLogBarrier
30+
:allow-dot-graphs:
31+
32+
Normalized Clamped Log Barrier
33+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
35+
.. doxygenclass:: ipc::NormalizedClampedLogBarrier
3036
:allow-dot-graphs:

docs/source/tutorial/getting_started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ you will get the gradient of size :math:`|V|d \times 1` with the order
186186
\frac{\partial B}{\partial x_n} &
187187
\frac{\partial B}{\partial y_n} &
188188
\frac{\partial B}{\partial z_n}
189-
\end{bmatrix}^T,
189+
\end{bmatrix}^\top,
190190
191191
and the Hessian of size :math:`|V|d \times |V|d` with the order
192192

@@ -417,7 +417,7 @@ This returns a scalar value ``friction_potential`` which is the sum of the indiv
417417
Mathematically this is defined as
418418

419419
.. math::
420-
D(x) = \sum_{k \in C} \mu\lambda_k^nf_0\left(\|T_k^Tv\|, \epsilon_v\right),
420+
D(v) = \sum_{k \in C} \mu\lambda_k^nf_0\left(\|T_k^\top v\|, \epsilon_v\right),
421421
422422
where :math:`C` is the lagged collisions, :math:`\lambda_k^n` is the normal force magnitude for the :math:`k`-th collision, :math:`T_k` is the tangential basis for the :math:`k`-th collision, and :math:`f_0` is the smooth friction function used to approximate the non-smooth transition from dynamic to static friction.
423423

src/ipc/barrier/barrier.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,66 @@ class ClampedLogBarrier : public Barrier {
136136
}
137137
};
138138

139+
// ============================================================================
140+
// Normalized Barrier functions from [Li et al. 2023]
141+
// ============================================================================
142+
143+
/// @brief Normalized barrier function from [Li et al. 2023].
144+
class NormalizedClampedLogBarrier : public ClampedLogBarrier {
145+
public:
146+
NormalizedClampedLogBarrier() = default;
147+
148+
/// @brief Function that grows to infinity as d approaches 0 from the right.
149+
///
150+
/// \f\[
151+
/// b(d) =
152+
/// -\left(\frac{d}{\hat{d}}-1\right)^2\ln\left(\frac{d}{\hat{d}}\right)
153+
/// \f\]
154+
///
155+
/// @param d The distance.
156+
/// @param dhat Activation distance of the barrier.
157+
/// @return The value of the barrier function at d.
158+
double operator()(const double d, const double dhat) const override
159+
{
160+
return ClampedLogBarrier::operator()(d / dhat, 1.0);
161+
}
162+
163+
/// @brief Derivative of the barrier function.
164+
///
165+
/// \f\[
166+
/// b'(d) =
167+
/// 2\frac{1}{\hat{d}}\left(1-\frac{d}{\hat{d}}\right)\ln\left(\frac{d}{\hat{d}}\right)
168+
/// + \left(1-\frac{d}{\hat{d}}\right)^2 \frac{1}{d}
169+
/// \f\]
170+
///
171+
/// @param d The distance.
172+
/// @param dhat Activation distance of the barrier.
173+
/// @return The derivative of the barrier wrt d.
174+
double first_derivative(const double d, const double dhat) const override
175+
{
176+
return ClampedLogBarrier::first_derivative(d / dhat, 1.0) / dhat;
177+
}
178+
179+
/// @brief Second derivative of the barrier function.
180+
///
181+
/// \f\[
182+
/// b''(d) = \frac{\hat{d}^2-2 d^2 \ln \left(\frac{d}{\hat{d}}\right)+2
183+
/// \hat{d} d-3 d^2}{\hat{d}^2 d^2}
184+
/// \f\]
185+
///
186+
/// @param d The distance.
187+
/// @param dhat Activation distance of the barrier.
188+
/// @return The second derivative of the barrier wrt d.
189+
double second_derivative(const double d, const double dhat) const override
190+
{
191+
return ClampedLogBarrier::second_derivative(d / dhat, 1.0)
192+
/ (dhat * dhat);
193+
}
194+
195+
/// @brief Get the units of the barrier function.
196+
/// @param dhat The activation distance of the barrier.
197+
/// @return The units of the barrier function.
198+
double units(const double dhat) const override { return 1.0; }
199+
};
200+
139201
} // namespace ipc

tests/src/tests/barrier/test_barrier.cpp

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,27 @@
1212

1313
namespace ipc {
1414

15-
class NormalizedClampedLogBarrier : public ipc::Barrier {
16-
public:
17-
double operator()(const double d, const double dhat) const override
18-
{
19-
if (d <= 0.0) {
20-
return std::numeric_limits<double>::infinity();
21-
}
22-
if (d >= dhat) {
23-
return 0;
24-
}
25-
26-
// b(d) = -(d/d̂-1)²ln(d / d̂)
27-
const auto d_dhat = d / dhat;
28-
const auto d_dhat_minus_1 = d_dhat - 1;
29-
return -d_dhat_minus_1 * d_dhat_minus_1 * std::log(d_dhat);
30-
}
31-
32-
double first_derivative(const double d, const double dhat) const override
33-
{
34-
if (d <= 0.0 || d >= dhat) {
35-
return 0.0;
36-
}
37-
const double t0 = 1.0 / dhat;
38-
const double t1 = d * t0;
39-
const double t2 = 1 - t1;
40-
return t2 * (2 * t0 * std::log(t1) - t2 / d);
41-
}
42-
43-
double second_derivative(const double d, const double dhat) const override
44-
{
45-
if (d <= 0.0 || d >= dhat) {
46-
return 0.0;
47-
}
48-
49-
const double t0 = 1.0 / dhat;
50-
const double t1 = d * t0;
51-
const double t2 = 1 - t1;
52-
return 4 * t0 * t2 / d + (t2 * t2) / (d * d)
53-
- 2 * std::log(t1) / (dhat * dhat);
54-
}
55-
56-
double units(const double dhat) const override { return 1; }
57-
};
58-
5915
/// @warning This implementation will not work with dmin > 0
60-
class PhysicalBarrier : public ipc::NormalizedClampedLogBarrier {
16+
class PhysicalBarrier : public NormalizedClampedLogBarrier {
6117
public:
6218
PhysicalBarrier(const bool _use_dist_sqr) : use_dist_sqr(_use_dist_sqr) { }
6319

6420
double operator()(const double d, const double dhat) const override
6521
{
6622
return (use_dist_sqr ? sqrt(dhat) : dhat)
67-
* ipc::NormalizedClampedLogBarrier::operator()(d, dhat);
23+
* NormalizedClampedLogBarrier::operator()(d, dhat);
6824
}
6925

7026
double first_derivative(const double d, const double dhat) const override
7127
{
7228
return (use_dist_sqr ? sqrt(dhat) : dhat)
73-
* ipc::NormalizedClampedLogBarrier::first_derivative(d, dhat);
29+
* NormalizedClampedLogBarrier::first_derivative(d, dhat);
7430
}
7531

7632
double second_derivative(const double d, const double dhat) const override
7733
{
7834
return (use_dist_sqr ? sqrt(dhat) : dhat)
79-
* ipc::NormalizedClampedLogBarrier::second_derivative(d, dhat);
35+
* NormalizedClampedLogBarrier::second_derivative(d, dhat);
8036
}
8137

8238
private:

0 commit comments

Comments
 (0)