PolyFEM
Loading...
Searching...
No Matches
ElasticityUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <polyfem/Common.hpp>
9
10#include <Eigen/Dense>
11
12#include <vector>
13#include <array>
14#include <functional>
15
16namespace polyfem
17{
18 constexpr int SMALL_N = POLYFEM_SMALL_N;
19 constexpr int BIG_N = POLYFEM_BIG_N;
20
22 {
23 CAUCHY,
24 PK1,
25 PK2,
26 F
27 };
28
29 Eigen::VectorXd
30 gradient_from_energy(const int size, const int n_bases, const assembler::NonLinearAssemblerData &data,
31 const std::function<DScalar1<double, Eigen::Matrix<double, 6, 1>>(const assembler::NonLinearAssemblerData &)> &fun6,
32 const std::function<DScalar1<double, Eigen::Matrix<double, 8, 1>>(const assembler::NonLinearAssemblerData &)> &fun8,
33 const std::function<DScalar1<double, Eigen::Matrix<double, 12, 1>>(const assembler::NonLinearAssemblerData &)> &fun12,
34 const std::function<DScalar1<double, Eigen::Matrix<double, 18, 1>>(const assembler::NonLinearAssemblerData &)> &fun18,
35 const std::function<DScalar1<double, Eigen::Matrix<double, 24, 1>>(const assembler::NonLinearAssemblerData &)> &fun24,
36 const std::function<DScalar1<double, Eigen::Matrix<double, 30, 1>>(const assembler::NonLinearAssemblerData &)> &fun30,
37 const std::function<DScalar1<double, Eigen::Matrix<double, 60, 1>>(const assembler::NonLinearAssemblerData &)> &fun60,
38 const std::function<DScalar1<double, Eigen::Matrix<double, 81, 1>>(const assembler::NonLinearAssemblerData &)> &fun81,
39 const std::function<DScalar1<double, Eigen::Matrix<double, Eigen::Dynamic, 1, 0, SMALL_N, 1>>(const assembler::NonLinearAssemblerData &)> &funN,
40 const std::function<DScalar1<double, Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 1000, 1>>(const assembler::NonLinearAssemblerData &)> &funBigN,
42
43 Eigen::MatrixXd hessian_from_energy(const int size, const int n_bases, const assembler::NonLinearAssemblerData &data,
44 const std::function<DScalar2<double, Eigen::Matrix<double, 6, 1>, Eigen::Matrix<double, 6, 6>>(const assembler::NonLinearAssemblerData &)> &fun6,
45 const std::function<DScalar2<double, Eigen::Matrix<double, 8, 1>, Eigen::Matrix<double, 8, 8>>(const assembler::NonLinearAssemblerData &)> &fun8,
46 const std::function<DScalar2<double, Eigen::Matrix<double, 12, 1>, Eigen::Matrix<double, 12, 12>>(const assembler::NonLinearAssemblerData &)> &fun12,
47 const std::function<DScalar2<double, Eigen::Matrix<double, 18, 1>, Eigen::Matrix<double, 18, 18>>(const assembler::NonLinearAssemblerData &)> &fun18,
48 const std::function<DScalar2<double, Eigen::Matrix<double, 24, 1>, Eigen::Matrix<double, 24, 24>>(const assembler::NonLinearAssemblerData &)> &fun24,
49 const std::function<DScalar2<double, Eigen::Matrix<double, 30, 1>, Eigen::Matrix<double, 30, 30>>(const assembler::NonLinearAssemblerData &)> &fun30,
50 const std::function<DScalar2<double, Eigen::Matrix<double, 60, 1>, Eigen::Matrix<double, 60, 60>>(const assembler::NonLinearAssemblerData &)> &fun60,
51 const std::function<DScalar2<double, Eigen::Matrix<double, 81, 1>, Eigen::Matrix<double, 81, 81>>(const assembler::NonLinearAssemblerData &)> &fun81,
52 const std::function<DScalar2<double, Eigen::Matrix<double, Eigen::Dynamic, 1, 0, SMALL_N, 1>, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, SMALL_N, SMALL_N>>(const assembler::NonLinearAssemblerData &)> &funN,
54
55 double von_mises_stress_for_stress_tensor(const Eigen::MatrixXd &stress);
56 Eigen::MatrixXd pk1_from_cauchy(const Eigen::MatrixXd &stress, const Eigen::MatrixXd &F);
57 Eigen::MatrixXd pk2_from_cauchy(const Eigen::MatrixXd &stress, const Eigen::MatrixXd &F);
58 void compute_diplacement_grad(const int size, const assembler::ElementAssemblyValues &vals, const Eigen::MatrixXd &local_pts, const int p, const Eigen::MatrixXd &displacement, Eigen::MatrixXd &displacement_grad);
59 void compute_diplacement_grad(const int size, const basis::ElementBases &bs, const assembler::ElementAssemblyValues &vals, const Eigen::MatrixXd &local_pts, const int p, const Eigen::MatrixXd &displacement, Eigen::MatrixXd &displacement_grad);
60
61 double convert_to_lambda(const bool is_volume, const double E, const double nu);
62 double convert_to_mu(const double E, const double nu);
63 Eigen::Matrix2d d_lambda_mu_d_E_nu(const bool is_volume, const double E, const double nu);
64 Eigen::Matrix2d d_E_nu_d_lambda_mu(const bool is_volume, const double lambda, const double mu);
65 double convert_to_E(const bool is_volume, const double lambda, const double mu);
66 double convert_to_nu(const bool is_volume, const double lambda, const double mu);
67
68 typedef std::array<double, 2> DampingParameters;
69
70 template <typename AutoDiffVect>
73 const int size,
74 AutoDiffVect &local_disp)
75 {
76 typedef typename AutoDiffVect::Scalar T;
77
78 assert(data.x.cols() == 1);
79
80 Eigen::Matrix<double, Eigen::Dynamic, 1> local_dispv(data.vals.basis_values.size() * size, 1);
81 local_dispv.setZero();
82 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
83 {
84 const auto &bs = data.vals.basis_values[i];
85 for (size_t ii = 0; ii < bs.global.size(); ++ii)
86 {
87 for (int d = 0; d < size; ++d)
88 {
89 local_dispv(i * size + d) += bs.global[ii].val * data.x(bs.global[ii].index * size + d);
90 }
91 }
92 }
93
94 DiffScalarBase::setVariableCount(local_dispv.rows());
95 local_disp.resize(local_dispv.rows(), 1);
96
97 const AutoDiffAllocator<T> allocate_auto_diff_scalar;
98
99 for (long i = 0; i < local_dispv.rows(); ++i)
100 {
101 local_disp(i) = allocate_auto_diff_scalar(i, local_dispv(i));
102 }
103 }
104
105 template <typename AutoDiffVect, typename AutoDiffGradMat>
108 const AutoDiffVect &local_disp,
109 const int p, const int size,
110 AutoDiffGradMat &def_grad)
111 {
112 typedef typename AutoDiffVect::Scalar T;
113
114 for (long k = 0; k < def_grad.size(); ++k)
115 def_grad(k) = T(0);
116
117 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
118 {
119 const auto &bs = data.vals.basis_values[i];
120 const Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1> grad = bs.grad.row(p);
121 assert(grad.size() == size);
122
123 for (int d = 0; d < size; ++d)
124 {
125 for (int c = 0; c < size; ++c)
126 {
127 def_grad(d, c) += grad(c) * local_disp(i * size + d);
128 }
129 }
130 }
131
132 AutoDiffGradMat jac_it(size, size);
133 for (long k = 0; k < jac_it.size(); ++k)
134 jac_it(k) = T(data.vals.jac_it[p](k));
135 def_grad = def_grad * jac_it;
136 }
137
138 // https://en.wikipedia.org/wiki/Invariants_of_tensors
139 template <typename AutoDiffGradMat>
140 typename AutoDiffGradMat::Scalar first_invariant(const AutoDiffGradMat &B)
141 {
142 return B.trace();
143 }
144
145 template <typename AutoDiffGradMat>
146 typename AutoDiffGradMat::Scalar second_invariant(const AutoDiffGradMat &B)
147 {
148 return 0.5 * (B.trace() * B.trace() - (B * B).trace());
149 }
150
151 template <typename AutoDiffGradMat>
152 typename AutoDiffGradMat::Scalar third_invariant(const AutoDiffGradMat &B)
153 {
155 }
156} // namespace polyfem
ElementAssemblyValues vals
Definition Assembler.cpp:22
stores per element basis values at given quadrature points and geometric mapping
std::vector< Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > > jac_it
Stores the basis functions for a given element in a mesh (facet in 2d, cell in 3d).
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AutoDiffGradMat
Eigen::Matrix< T, Eigen::Dynamic, 1 > AutoDiffVect
T determinant(const Eigen::Matrix< T, rows, cols, option, maxRow, maxCol > &mat)
constexpr int SMALL_N
Eigen::MatrixXd pk2_from_cauchy(const Eigen::MatrixXd &stress, const Eigen::MatrixXd &F)
void compute_disp_grad_at_quad(const assembler::NonLinearAssemblerData &data, const AutoDiffVect &local_disp, const int p, const int size, AutoDiffGradMat &def_grad)
AutoDiffGradMat::Scalar third_invariant(const AutoDiffGradMat &B)
Eigen::MatrixXd hessian_from_energy(const int size, const int n_bases, const assembler::NonLinearAssemblerData &data, const std::function< DScalar2< double, Eigen::Matrix< double, 6, 1 >, Eigen::Matrix< double, 6, 6 > >(const assembler::NonLinearAssemblerData &)> &fun6, const std::function< DScalar2< double, Eigen::Matrix< double, 8, 1 >, Eigen::Matrix< double, 8, 8 > >(const assembler::NonLinearAssemblerData &)> &fun8, const std::function< DScalar2< double, Eigen::Matrix< double, 12, 1 >, Eigen::Matrix< double, 12, 12 > >(const assembler::NonLinearAssemblerData &)> &fun12, const std::function< DScalar2< double, Eigen::Matrix< double, 18, 1 >, Eigen::Matrix< double, 18, 18 > >(const assembler::NonLinearAssemblerData &)> &fun18, const std::function< DScalar2< double, Eigen::Matrix< double, 24, 1 >, Eigen::Matrix< double, 24, 24 > >(const assembler::NonLinearAssemblerData &)> &fun24, const std::function< DScalar2< double, Eigen::Matrix< double, 30, 1 >, Eigen::Matrix< double, 30, 30 > >(const assembler::NonLinearAssemblerData &)> &fun30, const std::function< DScalar2< double, Eigen::Matrix< double, 60, 1 >, Eigen::Matrix< double, 60, 60 > >(const assembler::NonLinearAssemblerData &)> &fun60, const std::function< DScalar2< double, Eigen::Matrix< double, 81, 1 >, Eigen::Matrix< double, 81, 81 > >(const assembler::NonLinearAssemblerData &)> &fun81, const std::function< DScalar2< double, Eigen::Matrix< double, Eigen::Dynamic, 1, 0, SMALL_N, 1 >, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, SMALL_N, SMALL_N > >(const assembler::NonLinearAssemblerData &)> &funN, const std::function< DScalar2< double, Eigen::VectorXd, Eigen::MatrixXd >(const assembler::NonLinearAssemblerData &)> &funn)
double convert_to_E(const bool is_volume, const double lambda, const double mu)
constexpr int BIG_N
double convert_to_mu(const double E, const double nu)
AutoDiffGradMat::Scalar second_invariant(const AutoDiffGradMat &B)
double von_mises_stress_for_stress_tensor(const Eigen::MatrixXd &stress)
double convert_to_nu(const bool is_volume, const double lambda, const double mu)
void compute_diplacement_grad(const int size, const ElementAssemblyValues &vals, const Eigen::MatrixXd &local_pts, const int p, const Eigen::MatrixXd &displacement, Eigen::MatrixXd &displacement_grad)
AutoDiffGradMat::Scalar first_invariant(const AutoDiffGradMat &B)
void get_local_disp(const assembler::NonLinearAssemblerData &data, const int size, AutoDiffVect &local_disp)
Eigen::MatrixXd pk1_from_cauchy(const Eigen::MatrixXd &stress, const Eigen::MatrixXd &F)
Eigen::VectorXd gradient_from_energy(const int size, const int n_bases, const assembler::NonLinearAssemblerData &data, const std::function< DScalar1< double, Eigen::Matrix< double, 6, 1 > >(const assembler::NonLinearAssemblerData &)> &fun6, const std::function< DScalar1< double, Eigen::Matrix< double, 8, 1 > >(const assembler::NonLinearAssemblerData &)> &fun8, const std::function< DScalar1< double, Eigen::Matrix< double, 12, 1 > >(const assembler::NonLinearAssemblerData &)> &fun12, const std::function< DScalar1< double, Eigen::Matrix< double, 18, 1 > >(const assembler::NonLinearAssemblerData &)> &fun18, const std::function< DScalar1< double, Eigen::Matrix< double, 24, 1 > >(const assembler::NonLinearAssemblerData &)> &fun24, const std::function< DScalar1< double, Eigen::Matrix< double, 30, 1 > >(const assembler::NonLinearAssemblerData &)> &fun30, const std::function< DScalar1< double, Eigen::Matrix< double, 60, 1 > >(const assembler::NonLinearAssemblerData &)> &fun60, const std::function< DScalar1< double, Eigen::Matrix< double, 81, 1 > >(const assembler::NonLinearAssemblerData &)> &fun81, const std::function< DScalar1< double, Eigen::Matrix< double, Eigen::Dynamic, 1, 0, SMALL_N, 1 > >(const assembler::NonLinearAssemblerData &)> &funN, const std::function< DScalar1< double, Eigen::Matrix< double, Eigen::Dynamic, 1, 0, BIG_N, 1 > >(const assembler::NonLinearAssemblerData &)> &funBigN, const std::function< DScalar1< double, Eigen::VectorXd >(const assembler::NonLinearAssemblerData &)> &funn)
double convert_to_lambda(const bool is_volume, const double E, const double nu)
Eigen::Matrix2d d_E_nu_d_lambda_mu(const bool is_volume, const double lambda, const double mu)
Eigen::Matrix2d d_lambda_mu_d_E_nu(const bool is_volume, const double E, const double nu)
std::array< double, 2 > DampingParameters
Automatic differentiation scalar with first-order derivatives.
Definition autodiff.h:112
Automatic differentiation scalar with first- and second-order derivatives.
Definition autodiff.h:493
static void setVariableCount(size_t value)
Set the independent variable count used by the automatic differentiation layer.
Definition autodiff.h:54