PolyFEM
Loading...
Searching...
No Matches
StackedForm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Form.hpp"
4
5#include <memory>
6#include <vector>
7
8namespace polyfem::solver
9{
10 class StackedForm : public Form
11 {
12 public:
13 class Block
14 {
15 friend class StackedForm;
16
17 public:
18 int id() const { return id_; }
19 int offset() const { return offset_; }
20 int size() const { return size_; }
21
22 private:
23 Block(const int id, const int offset, const int size)
24 : id_(id), offset_(offset), size_(size) {}
25
26 int id_ = -1;
27 int offset_ = 0;
28 int size_ = 0;
29 };
30
31 std::string name() const override { return "stacked"; }
32
33 Block add_block(const int size);
34 void add(const Block &block, std::shared_ptr<Form> form);
35 void add(const Block &a, const Block &b, std::shared_ptr<Form> form);
36
37 int size() const { return size_; }
38
39 void init(const Eigen::VectorXd &x) override;
40 void finish() override;
41 bool is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override;
42 bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override;
43 double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override;
44 void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) override;
45 void line_search_end() override;
46 void post_step(const polysolve::nonlinear::PostStepData &data) override;
47 void solution_changed(const Eigen::VectorXd &new_x) override;
48 void set_project_to_psd(bool val) override;
49 void update_quantities(const double t, const Eigen::VectorXd &x) override;
50 void init_lagging(const Eigen::VectorXd &x) override;
51 void update_lagging(const Eigen::VectorXd &x, const int iter_num) override;
52 int max_lagging_iterations() const override;
53 bool uses_lagging() const override;
54
55 protected:
56 double value_unweighted(const Eigen::VectorXd &x) const override;
57 void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const override;
58 void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const override;
59
60 private:
61 struct Term
62 {
63 int a = -1;
64 int b = -1;
65 std::shared_ptr<Form> form;
66
67 bool has_second_block() const { return b >= 0; }
68 };
69
70 void validate_block(const Block &block) const;
71 int term_size(const Term &term) const;
72 Eigen::VectorXd gather(const Eigen::VectorXd &x, const Term &term) const;
73 int global_index(const Term &term, const int local_index) const;
74
75 std::vector<Block> blocks_;
76 std::vector<Term> terms_;
77 int size_ = 0;
78 };
79} // namespace polyfem::solver
double val
Definition Assembler.cpp:89
int x
Block(const int id, const int offset, const int size)
void update_quantities(const double t, const Eigen::VectorXd &x) override
Update time-dependent fields.
void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) override
Initialize variables used during the line search.
int term_size(const Term &term) const
void validate_block(const Block &block) const
std::vector< Term > terms_
std::vector< Block > blocks_
int max_lagging_iterations() const override
Get the maximum number of lagging iteration allowable.
void line_search_end() override
Clear variables used during the line search.
bool is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override
Determine if a step from solution x0 to solution x1 is allowed.
void add(const Block &block, std::shared_ptr< Form > form)
void solution_changed(const Eigen::VectorXd &new_x) override
Update cached fields upon a change in the solution.
void init(const Eigen::VectorXd &x) override
Initialize the form.
void update_lagging(const Eigen::VectorXd &x, const int iter_num) override
Update lagged fields.
std::string name() const override
Block add_block(const int size)
bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override
Checks if the step is collision free.
bool uses_lagging() const override
Does this form require lagging?
void post_step(const polysolve::nonlinear::PostStepData &data) override
Update fields after a step in the optimization.
void init_lagging(const Eigen::VectorXd &x) override
Initialize lagged fields TODO: more than one step.
void set_project_to_psd(bool val) override
Set project to psd.
Eigen::VectorXd gather(const Eigen::VectorXd &x, const Term &term) const
int global_index(const Term &term, const int local_index) const
double value_unweighted(const Eigen::VectorXd &x) const override
Compute the value of the form.
double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const override
Determine the maximum step size allowable between the current and next solution.
void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const override
Compute the second derivative of the value wrt x.
void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const override
Compute the first derivative of the value wrt x.
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:24
std::shared_ptr< Form > form