3 #include <polysolve/nonlinear/Problem.hpp>
9 #include <polysolve/nonlinear/Solver.hpp>
17 const Eigen::Matrix<T, -1, 1>
convert(
const polysolve::nonlinear::Problem::TVector& v)
23 const Eigen::Matrix<Rational, -1, 1>
convert(
const polysolve::nonlinear::Problem::TVector& v)
25 Eigen::Matrix<Rational, -1, 1> res(v.size());
26 for (int64_t d = 0; d < v.size(); ++d) {
27 res[d] = Rational(v[d],
true);
38 using typename polysolve::nonlinear::Problem::Scalar;
39 using typename polysolve::nonlinear::Problem::THessian;
40 using typename polysolve::nonlinear::Problem::TVector;
50 double value(
const TVector& x)
override;
51 void gradient(
const TVector& x, TVector& gradv)
override;
54 throw std::runtime_error(
"Sparse functions do not exist, use dense solver");
60 bool is_step_valid(
const TVector& x0,
const TVector& x1)
override;
76 : m_accessor(
std::move(accessor))
79 , m_invariants(invariants)
85 if constexpr (std::is_same_v<T, Rational>) {
86 const Eigen::Matrix<T, -1, 1> tmp = m_accessor.const_vector_attribute(m_simplex.tuple());
87 Eigen::Matrix<double, -1, 1> tmp1(tmp.size());
88 for (int64_t d = 0; d < tmp1.size(); ++d) {
89 tmp1[d] = tmp[d].to_double();
93 return m_accessor.const_vector_attribute(m_simplex.tuple());
100 auto tmp = m_accessor.vector_attribute(m_simplex.tuple());
101 m_accessor.vector_attribute(m_simplex.tuple()) = convert<T>(x);
102 double res =
m_energy.get_value(m_simplex);
104 m_accessor.vector_attribute(m_simplex.tuple()) = tmp;
109 template <
typename T>
112 auto tmp = m_accessor.vector_attribute(m_simplex.tuple());
113 m_accessor.vector_attribute(m_simplex.tuple()) = convert<T>(x);
114 gradv =
m_energy.get_gradient(m_simplex);
116 m_accessor.vector_attribute(m_simplex.tuple()) = tmp;
119 template <
typename T>
122 auto tmp = m_accessor.vector_attribute(m_simplex.tuple());
123 m_accessor.vector_attribute(m_simplex.tuple()) = convert<T>(x);
124 hessian =
m_energy.get_hessian(m_simplex);
126 m_accessor.vector_attribute(m_simplex.tuple()) = tmp;
129 template <
typename T>
135 template <
typename T>
138 auto tmp = m_accessor.vector_attribute(m_simplex.tuple());
139 m_accessor.vector_attribute(m_simplex.tuple()) = convert<T>(x1);
141 auto domain =
m_energy.domain(m_simplex);
142 std::vector<Tuple> dom_tmp;
143 dom_tmp.reserve(domain.size());
147 std::back_inserter(dom_tmp),
152 m_accessor.vector_attribute(m_simplex.tuple()) = tmp;
170 m_solver = polysolve::nonlinear::Solver::create(
180 if (
m_energy->attribute_handle().holds<
double>()) {
188 accessor.vector_attribute(simplex.
tuple()) = x;
190 }
catch (
const std::exception&) {
205 for (int64_t d = 0; d <
m_energy->attribute_handle().dimension(); ++d) {
206 accessor.vector_attribute(simplex.
tuple())[d] =
Rational(x[d],
true);
208 }
catch (
const std::exception&) {
attribute::Accessor< T, Mesh, D > create_accessor(const attribute::MeshAttributeHandle &handle)
A CachingAccessor that uses tuples for accessing attributes instead of indices.
bool after(const std::vector< Tuple > &top_dimension_tuples_before, const std::vector< Tuple > &top_dimension_tuples_after) const override
virtual std::vector< simplex::Simplex > execute(const simplex::Simplex &simplex) override
returns an empty vector in case of failure
const Mesh & mesh() const
invariants::InvariantCollection m_invariants
void solution_changed(const TVector &new_x) override
invariants::InvariantCollection & m_invariants
const simplex::Simplex & m_simplex
void gradient(const TVector &x, TVector &gradv) override
TVector initial_value() const
void hessian(const TVector &x, THessian &hessian) override
double value(const TVector &x) override
WMTKProblem(attribute::Accessor< T > &&handle, const simplex::Simplex &simplex, invariants::InvariantCollection &invariants, const wmtk::function::Function &energy)
const wmtk::function::Function & m_energy
attribute::Accessor< T > m_accessor
bool is_step_valid(const TVector &x0, const TVector &x1) override
polysolve::json m_linear_solver_params
std::shared_ptr< wmtk::function::Function > m_energy
std::shared_ptr< polysolve::nonlinear::Solver > m_solver
std::vector< simplex::Simplex > execute(const simplex::Simplex &simplex) override
returns an empty vector in case of failure
polysolve::json m_nonlinear_solver_params
OptimizationSmoothing(std::shared_ptr< wmtk::function::Function > energy)
const Tuple & tuple() const
int8_t convert(PrimitiveType from, PrimitiveType to, int8_t source)
spdlog::logger & opt_logger()
Retrieves the logger for the optimization.