Wildmeshing Toolkit
PerSimplexAutodiffFunction.cpp
Go to the documentation of this file.
6 
7 
8 namespace wmtk::function {
9 
11  const Mesh& mesh,
12  const PrimitiveType primitive_type,
13  const attribute::MeshAttributeHandle& variable_attribute_handle)
14  : PerSimplexFunction(mesh, primitive_type, variable_attribute_handle)
15 {}
16 
18 
19 std::vector<PerSimplexAutodiffFunction::DSVec> PerSimplexAutodiffFunction::get_coordinates(
20  const simplex::Simplex& domain_simplex,
21  const std::optional<simplex::Simplex>& variable_simplex_opt) const
22 {
24  return get_coordinates(pos, domain_simplex, variable_simplex_opt);
25 }
26 
27 std::vector<PerSimplexAutodiffFunction::DSVec> PerSimplexAutodiffFunction::get_coordinates(
28  const attribute::Accessor<double>& accessor,
29  const simplex::Simplex& domain_simplex,
30  const std::optional<simplex::Simplex>& variable_simplex_opt) const
31 {
32  auto [attrs, index] = utils::get_simplex_attributes(
33  mesh(),
34  accessor,
36  domain_simplex,
37  variable_simplex_opt.has_value() ? variable_simplex_opt->tuple() : std::optional<Tuple>());
38 
39  std::vector<DSVec> ret;
40  ret.reserve(attrs.size());
41 
42  for (size_t i = 0; i < attrs.size(); ++i) {
43  ret.emplace_back(
44  i == index ? utils::as_DScalar<DScalar>(attrs[i]) : attrs[i].cast<DScalar>());
45  }
46 
47  return ret;
48 }
49 
50 
51 double PerSimplexAutodiffFunction::get_value(const simplex::Simplex& domain_simplex) const
52 {
54 
55  // return the energy
56  return eval(domain_simplex, get_coordinates(domain_simplex)).getValue();
57 }
58 
60  const simplex::Simplex& domain_simplex,
61  const simplex::Simplex& variable_simplex) const
62 {
64 
65  // return the gradient
66  return eval(domain_simplex, get_coordinates(domain_simplex, variable_simplex)).getGradient();
67 }
68 
70  const simplex::Simplex& domain_simplex,
71  const simplex::Simplex& variable_simplex) const
72 {
74 
75  // return the hessian
76  return eval(domain_simplex, get_coordinates(domain_simplex, variable_simplex)).getHessian();
77 }
78 
79 } // namespace wmtk::function
const attribute::Accessor< T, Mesh, D > create_const_accessor(const attribute::MeshAttributeHandle &handle) const
A CachingAccessor that uses tuples for accessing attributes instead of indices.
Definition: Accessor.hpp:25
Eigen::VectorXd get_gradient(const simplex::Simplex &domain_simplex, const simplex::Simplex &variable_simplex) const override
Eigen::MatrixXd get_hessian(const simplex::Simplex &domain_simplex, const simplex::Simplex &variable_simplex) const override
std::vector< DSVec > get_coordinates(const simplex::Simplex &domain_simplex, const std::optional< simplex::Simplex > &variable_simplex_opt={}) const
This is a helper function that obtains the coordinates of the variables for the function f(x) where f...
PerSimplexAutodiffFunction(const Mesh &mesh, const PrimitiveType primitive_type, const attribute::MeshAttributeHandle &variable_attribute_handle)
double get_value(const simplex::Simplex &domain_simplex) const override
This function is defined over a simplex (normally a triangle or tetrahedron).
virtual DScalar eval(const simplex::Simplex &domain_simplex, const std::vector< DSVec > &coordinates) const =0
This function defines a function f(x) where f is defined over a simplex domain and the variables for ...
const attribute::MeshAttributeHandle & attribute_handle() const
const Tuple & tuple() const
Definition: Simplex.hpp:53
std::tuple< std::vector< std::decay_t< typename attribute::ConstMapResult< T > > >, int64_t > get_simplex_attributes(const Mesh &mesh, const wmtk::attribute::Accessor< T > &accessor, const PrimitiveType primitive_type, const simplex::Simplex &simplex_in, const std::optional< wmtk::Tuple > &vertex_marker)
get attributes from a simplex
const Scalar & getValue() const
Definition: autodiff.h:545
const Hessian & getHessian() const
Definition: autodiff.h:547
const Gradient & getGradient() const
Definition: autodiff.h:546