PolyFEM
Loading...
Searching...
No Matches
AMIPSEnergy.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <polyfem/Common.hpp>
9
10#include <Eigen/Dense>
11
12#include <functional>
13#include <iostream>
14#include <vector>
15
16namespace polyfem::assembler
17{
19 {
20 public:
22
26
27 // energy, gradient, and hessian used in newton method
28 double compute_energy(const NonLinearAssemblerData &data) const override;
29 Eigen::VectorXd assemble_gradient(const NonLinearAssemblerData &data) const override;
30 Eigen::MatrixXd assemble_hessian(const NonLinearAssemblerData &data) const override;
31
32 // sets material params
33 void add_multimaterial(const int index, const json &params, const Units &units) override {}
34
35 std::string name() const override { return "AMIPS"; }
36 std::map<std::string, ParamFunc> parameters() const override { return std::map<std::string, ParamFunc>(); }
37
38 void assign_stress_tensor(const OutputData &data,
39 const int all_size,
40 const ElasticityTensorType &type,
41 Eigen::MatrixXd &all,
42 const std::function<Eigen::MatrixXd(const Eigen::MatrixXd &)> &fun) const override;
43
44 private:
45 // utility function that computes energy, the template is used for double, DScalar1, and DScalar2 in energy, gradient and hessian
46 template <typename T>
47 T compute_energy_aux(const NonLinearAssemblerData &data) const;
48 template <int n_basis, int dim>
49 void compute_energy_aux_gradient_fast(const NonLinearAssemblerData &data, Eigen::VectorXd &G_flattened) const;
50 template <int n_basis, int dim>
51 void compute_energy_hessian_aux_fast(const NonLinearAssemblerData &data, Eigen::MatrixXd &H) const;
52 };
53
54 class AMIPSEnergyAutodiff : public GenericElastic<AMIPSEnergyAutodiff>
55 {
56 public:
58
59 // sets material params
60 void add_multimaterial(const int index, const json &params, const Units &units) override;
61
62 std::string name() const override { return "AMIPSAutodiff"; }
63 std::map<std::string, ParamFunc> parameters() const override;
64
65 template <typename T>
67 const RowVectorNd &p,
68 const double t,
69 const int el_id,
70 const DefGradMatrix<T> &def_grad) const
71 {
72 using std::pow;
73 using MatrixNT = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3>;
74
75 MatrixNT F = MatrixNT::Zero(size(), size());
76 for (int i = 0; i < size(); ++i)
77 {
78 for (int j = 0; j < size(); ++j)
79 {
80 for (int k = 0; k < size(); ++k)
81 {
82 F(i, j) += def_grad(i, k) * canonical_transformation_[el_id](k, j);
83 }
84 }
85 }
86
88 if (J <= 0)
89 J = T(std::nan(""));
90 return (F.transpose() * F).trace() / pow(J, 2. / size());
91 }
92
93 private:
94 std::vector<Eigen::MatrixXd> canonical_transformation_;
95 };
96} // namespace polyfem::assembler
std::map< std::string, ParamFunc > parameters() const override
std::vector< Eigen::MatrixXd > canonical_transformation_
void add_multimaterial(const int index, const json &params, const Units &units) override
T elastic_energy(const RowVectorNd &p, const double t, const int el_id, const DefGradMatrix< T > &def_grad) const
std::string name() const override
double compute_energy(const NonLinearAssemblerData &data) const override
std::string name() const override
T compute_energy_aux(const NonLinearAssemblerData &data) const
void assign_stress_tensor(const OutputData &data, const int all_size, const ElasticityTensorType &type, Eigen::MatrixXd &all, const std::function< Eigen::MatrixXd(const Eigen::MatrixXd &)> &fun) const override
void compute_energy_hessian_aux_fast(const NonLinearAssemblerData &data, Eigen::MatrixXd &H) const
void add_multimaterial(const int index, const json &params, const Units &units) override
Eigen::MatrixXd assemble_hessian(const NonLinearAssemblerData &data) const override
void compute_energy_aux_gradient_fast(const NonLinearAssemblerData &data, Eigen::VectorXd &G_flattened) const
Eigen::VectorXd assemble_gradient(const NonLinearAssemblerData &data) const override
std::map< std::string, ParamFunc > parameters() const override
double assemble_energy(const bool is_volume, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const AssemblyValsCache &cache, const double t, const double dt, const Eigen::MatrixXd &displacement, const Eigen::MatrixXd &displacement_prev) const override
void assemble_gradient(const bool is_volume, const int n_basis, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const AssemblyValsCache &cache, const double t, const double dt, const Eigen::MatrixXd &displacement, const Eigen::MatrixXd &displacement_prev, Eigen::MatrixXd &rhs) const override
void assemble_hessian(const bool is_volume, const int n_basis, const bool project_to_psd, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const AssemblyValsCache &cache, const double t, const double dt, const Eigen::MatrixXd &displacement, const Eigen::MatrixXd &displacement_prev, utils::MatrixCache &mat_cache, StiffnessMatrix &grad) const override
Used for test only.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > DefGradMatrix
T determinant(const Eigen::Matrix< T, rows, cols, option, maxRow, maxCol > &mat)
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13