9#ifndef PBAT_SIM_ALGORITHM_NEWTON_HESSIANPRODUCT_H
10#define PBAT_SIM_ALGORITHM_NEWTON_HESSIANPRODUCT_H
14#include "pbat/math/linalg/SelectionMatrix.h"
15#include "pbat/math/linalg/SparsityPattern.h"
20struct HessianOperator;
29struct traits<pbat::sim::algorithm::newton::HessianOperator>
30 :
public Eigen::internal::traits<pbat::CSCMatrix>
43concept CElasticPotential =
requires(T U)
45 { U.ComputeElementElasticity(std::declval<VectorX>(),
false,
true,
true) };
46 { U.ToMatrix(std::declval<CSCMatrix&>()) };
47 { U.GH } -> std::convertible_to<math::linalg::SparsityPattern<>>;
55 static auto constexpr kDims = 3;
56 static auto constexpr kContactInds = 4;
57 using TripletType = Eigen::Triplet<Scalar, CSCMatrix::StorageIndex>;
62 void AllocateContacts(std::size_t nContacts);
69 void SetSparsityPattern(CSCMatrix
const& HS);
76 template <
class TDerivedF>
77 void ImposeDirichletConstraints(Eigen::DenseBase<TDerivedF>
const& F);
87 template <
class TDerivedHCB,
class TDerivedHCIB>
88 void AddContactHessianBlock(
89 Eigen::DenseBase<TDerivedHCB>
const& HCB,
90 Eigen::DenseBase<TDerivedHCIB>
const& HCIB);
105 template <CElasticPotential TElasticPotential,
class TDerivedM>
106 void ConstructContactLessHessian(
107 Eigen::DenseBase<TDerivedM>
const& diagM,
109 TElasticPotential
const& U);
113 void ConstructContactHessian();
117 std::vector<TripletType>
132struct HessianOperator :
public Eigen::EigenBase<HessianOperator>
136 using StorageIndex =
typename CSCMatrix::StorageIndex;
139 ColsAtCompileTime = Eigen::Dynamic,
140 MaxColsAtCompileTime = Eigen::Dynamic,
144 using SelfType = HessianOperator;
148 HessianOperator() =
default;
153 HessianOperator(Hessian* data);
158 [[maybe_unused]] Eigen::Index rows()
const {
return mData->HNC.rows(); }
163 [[maybe_unused]] Eigen::Index cols()
const {
return mData->HNC.cols(); }
172 Eigen::Product<SelfType, Rhs, Eigen::AliasFreeProduct>
173 operator*(Eigen::MatrixBase<Rhs>
const& x)
const
175 return Eigen::Product<SelfType, Rhs, Eigen::AliasFreeProduct>(*
this, x.derived());
181template <
class TDerivedF>
182inline void Hessian::ImposeDirichletConstraints(Eigen::DenseBase<TDerivedF>
const& F)
185 HNC = S.transpose() * (HNC * S);
186 HC = S.transpose() * (HC * S);
189template <
class TDerivedHCB,
class TDerivedHCIB>
190inline void Hessian::AddContactHessianBlock(
191 Eigen::DenseBase<TDerivedHCB>
const& HCB,
192 Eigen::DenseBase<TDerivedHCIB>
const& HCIB)
198 auto const bi = HCIB(ki);
199 auto const bj = HCIB(kj);
200 auto const istart = bi * kDims;
201 auto const jstart = bj * kDims;
202 auto const HCbibj = HCB.template block<kDims, kDims>(ki, kj);
203 using IndexType = std::remove_cvref_t<decltype(std::declval<TripletType>().row()
210 static_cast<IndexType
>(istart + i),
211 static_cast<IndexType
>(jstart + j),
219template <CElasticPotential TElasticPotential,
class TDerivedM>
220inline void Hessian::ConstructContactLessHessian(
221 Eigen::DenseBase<TDerivedM>
const& diagM,
223 TElasticPotential
const& U)
227 HNC.coeffs()(diag) += diagM;
239template <
typename Rhs,
int ProductType>
240struct generic_product_impl<
241 pbat::sim::algorithm::newton::HessianOperator,
246 : generic_product_impl_base<
247 pbat::sim::algorithm::newton::HessianOperator,
249 generic_product_impl<pbat::sim::algorithm::newton::HessianOperator, Rhs>>
251 using HessianOperator = pbat::sim::algorithm::newton::HessianOperator;
252 using Scalar =
typename Product<HessianOperator, Rhs>::Scalar;
253 using Hessian = pbat::sim::algorithm::newton::Hessian;
263 template <
typename Dst>
264 static void scaleAndAddTo(
265 Eigen::MatrixBase<Dst>& dst,
266 HessianOperator
const& lhs,
267 Eigen::MatrixBase<Rhs>
const& rhs,
270 dst += alpha * (lhs.mData->HNC * rhs + lhs.mData->HC * rhs);
constexpr void ForRange(F &&f)
Compile-time for loop over a range of values.
Definition ConstexprFor.h:55
CSCMatrix SelectionMatrix(Eigen::DenseBase< TDerivedC > const &C, Index n=Index(-1))
Construct the selection matrix S s.t. X*S selects all columns C of X.
Definition SelectionMatrix.h:19
Namespace for Newton simulation algorithms.
Definition Config.cpp:8
Eigen::Vector< Index, Eigen::Dynamic > IndexVectorX
Dynamic-size index vector type.
Definition Aliases.h:49
Eigen::SparseMatrix< Scalar, Eigen::ColMajor > CSCMatrix
Column-major sparse matrix type.
Definition Aliases.h:52
double Scalar
Scalar type.
Definition Aliases.h:18