PolyFEM
Loading...
Searching...
No Matches
DiffCache.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <polyfem/Common.hpp>
5#include <ipc/ipc.hpp>
6#include <ipc/collisions/collisions.hpp>
7#include <ipc/friction/friction_collisions.hpp>
8
9namespace polyfem::solver
10{
11 enum class CacheLevel
12 {
13 None,
16 };
17
19 {
20 public:
21 void init(const int dimension, const int ndof, const int n_time_steps = 0)
22 {
23 cur_size_ = 0;
24 n_time_steps_ = n_time_steps;
25
26 u_.setZero(ndof, n_time_steps + 1);
27 disp_grad_.assign(n_time_steps + 1, Eigen::MatrixXd::Zero(dimension,dimension));
28 if (n_time_steps_ > 0)
29 {
30 bdf_order_.setZero(n_time_steps + 1);
31 v_.setZero(ndof, n_time_steps + 1);
32 acc_.setZero(ndof, n_time_steps + 1);
33 // gradu_h_prev_.resize(n_time_steps + 1);
34 }
35 gradu_h_.resize(n_time_steps + 1);
36 collision_set_.resize(n_time_steps + 1);
37 friction_collision_set_.resize(n_time_steps + 1);
38 }
39
41 const Eigen::MatrixXd &u,
43 const ipc::Collisions &contact_set,
44 const ipc::FrictionCollisions &friction_constraint_set,
45 const Eigen::MatrixXd &disp_grad)
46 {
47 u_ = u;
48
49 gradu_h_[0] = gradu_h;
50 collision_set_[0] = contact_set;
51 friction_collision_set_[0] = friction_constraint_set;
53
54 cur_size_ = 1;
55 }
56
58 const int cur_step,
59 const int cur_bdf_order,
60 const Eigen::MatrixXd &u,
61 const Eigen::MatrixXd &v,
62 const Eigen::MatrixXd &acc,
64 // const StiffnessMatrix &gradu_h_prev,
65 const ipc::Collisions &collision_set,
66 const ipc::FrictionCollisions &friction_collision_set)
67 {
68 bdf_order_(cur_step) = cur_bdf_order;
69
70 u_.col(cur_step) = u;
71 v_.col(cur_step) = v;
72 acc_.col(cur_step) = acc;
73
74 gradu_h_[cur_step] = gradu_h;
75 // gradu_h_prev_[cur_step] = gradu_h_prev;
76
77 collision_set_[cur_step] = collision_set;
79
80 cur_size_++;
81 }
82
84 const int cur_step,
85 const Eigen::MatrixXd &u,
87 const ipc::Collisions &contact_set,
88 const Eigen::MatrixXd &disp_grad)
89 {
90 u_.col(cur_step) = u;
91 gradu_h_[cur_step] = gradu_h;
92 collision_set_[cur_step] = contact_set;
93 disp_grad_[cur_step] = disp_grad;
94
95 cur_size_++;
96 }
97
98 void cache_adjoints(const Eigen::MatrixXd &adjoint_mat) { adjoint_mat_ = adjoint_mat; }
99 const Eigen::MatrixXd &adjoint_mat() const { return adjoint_mat_; }
100
101 inline int size() const { return cur_size_; }
102 inline int bdf_order(int step) const
103 {
104 assert(step < size());
105 if (step < 0)
106 step += bdf_order_.size();
107 return bdf_order_(step);
108 }
109
110 Eigen::MatrixXd disp_grad(int step = 0) const { assert(step < size()); if (step < 0) step += disp_grad_.size(); return disp_grad_[step]; }
111
112 Eigen::VectorXd u(int step) const
113 {
114 assert(step < size());
115 if (step < 0)
116 step += u_.cols();
117 return u_.col(step);
118 }
119 Eigen::VectorXd v(int step) const
120 {
121 assert(step < size());
122 if (step < 0)
123 step += v_.cols();
124 return v_.col(step);
125 }
126 Eigen::VectorXd acc(int step) const
127 {
128 assert(step < size());
129 if (step < 0)
130 step += acc_.cols();
131 return acc_.col(step);
132 }
133
134 const StiffnessMatrix &gradu_h(int step) const
135 {
136 assert(step < size());
137 if (step < 0)
138 step += gradu_h_.size();
139 return gradu_h_[step];
140 }
141 // const StiffnessMatrix &gradu_h_prev(const int step) const { assert(step < size()); return gradu_h_prev_[step]; }
142
143 const ipc::Collisions &collision_set(int step) const
144 {
145 assert(step < size());
146 if (step < 0)
147 step += collision_set_.size();
148 return collision_set_[step];
149 }
150 const ipc::FrictionCollisions &friction_collision_set(int step) const
151 {
152 assert(step < size());
153 if (step < 0)
154 step += friction_collision_set_.size();
155 return friction_collision_set_[step];
156 }
157
158 private:
160 int cur_size_ = 0;
161
162 std::vector<Eigen::MatrixXd> disp_grad_; // macro linear displacement in homogenization
163 Eigen::MatrixXd u_; // PDE solution
164 Eigen::MatrixXd v_; // velocity in transient elastic simulations
165 Eigen::MatrixXd acc_; // acceleration in transient elastic simulations
166
167 Eigen::VectorXi bdf_order_; // BDF orders used at each time step in forward simulation
168
169 std::vector<StiffnessMatrix> gradu_h_; // gradient of force at time T wrt. u at time T
170 // std::vector<StiffnessMatrix> gradu_h_prev_; // gradient of force at time T wrt. u at time (T-1) in transient simulations
171
172 std::vector<ipc::Collisions> collision_set_;
173 std::vector<ipc::FrictionCollisions> friction_collision_set_;
174
175 Eigen::MatrixXd adjoint_mat_;
176 };
177} // namespace polyfem::solver
std::vector< ipc::FrictionCollisions > friction_collision_set_
const StiffnessMatrix & gradu_h(int step) const
int bdf_order(int step) const
Eigen::MatrixXd adjoint_mat_
std::vector< ipc::Collisions > collision_set_
Eigen::MatrixXd disp_grad(int step=0) const
const ipc::Collisions & collision_set(int step) const
void cache_quantities_quasistatic(const int cur_step, const Eigen::MatrixXd &u, const StiffnessMatrix &gradu_h, const ipc::Collisions &contact_set, const Eigen::MatrixXd &disp_grad)
Definition DiffCache.hpp:83
void init(const int dimension, const int ndof, const int n_time_steps=0)
Definition DiffCache.hpp:21
std::vector< Eigen::MatrixXd > disp_grad_
const Eigen::MatrixXd & adjoint_mat() const
Definition DiffCache.hpp:99
std::vector< StiffnessMatrix > gradu_h_
Eigen::VectorXd acc(int step) const
void cache_quantities_static(const Eigen::MatrixXd &u, const StiffnessMatrix &gradu_h, const ipc::Collisions &contact_set, const ipc::FrictionCollisions &friction_constraint_set, const Eigen::MatrixXd &disp_grad)
Definition DiffCache.hpp:40
void cache_adjoints(const Eigen::MatrixXd &adjoint_mat)
Definition DiffCache.hpp:98
Eigen::VectorXi bdf_order_
void cache_quantities_transient(const int cur_step, const int cur_bdf_order, const Eigen::MatrixXd &u, const Eigen::MatrixXd &v, const Eigen::MatrixXd &acc, const StiffnessMatrix &gradu_h, const ipc::Collisions &collision_set, const ipc::FrictionCollisions &friction_collision_set)
Definition DiffCache.hpp:57
Eigen::VectorXd v(int step) const
Eigen::VectorXd u(int step) const
const ipc::FrictionCollisions & friction_collision_set(int step) const
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22