PolyFEM
Loading...
Searching...
No Matches
ExpressionValue.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <polyfem/Common.hpp>
5#include <map>
6
7#include <units/units.hpp>
8
9namespace polyfem
10{
11 namespace utils
12 {
14 {
15 public:
17
18 void set_unit_type(const std::string &unit_type)
19 {
20 unit_type_ = units::unit_from_string(unit_type);
21 unit_type_set_ = true;
22 for (auto &expr : mat_expr_)
23 expr.set_unit_type(unit_type);
24 }
25
26 void init(const json &vals, const io::ResourceIO &resources);
27 void init(const double val);
28 void init(const Eigen::MatrixXd &val);
29 void init(const std::string &expr, const io::ResourceIO &resources);
30#ifdef POLYFEM_WITH_PYTHON
31 void init_python(const std::string &path, const std::string &function_name);
32#endif
33
34 void init(const std::function<double(double x, double y, double z)> &func);
35 void init(const std::function<double(double x, double y, double z, double t)> &func);
36 void init(const std::function<double(double x, double y, double z, double t, int index)> &func);
37
38 void init(const std::function<Eigen::MatrixXd(double x, double y, double z)> &func, const int coo);
39 void init(const std::function<Eigen::MatrixXd(double x, double y, double z, double t)> &func, const int coo);
40
41 void set_t(const json &t);
42 void set_index(const int index) { index_ = index; }
43
44 double operator()(double x, double y, double z = 0, double t = 0, int index = -1) const;
45
46 void clear();
47
48 bool is_zero() const
49 {
50 return expr_.empty() && mat_.size() == 0 && mat_expr_.empty() && !sfunc_ && !tfunc_ && fabs(value_) < 1e-10;
51 }
52 bool is_mat() const
53 {
54 if (expr_.empty() && mat_.size() > 0)
55 return true;
56 return false;
57 }
58
59 const Eigen::MatrixXd &get_mat() const
60 {
61 assert(is_mat());
62 return mat_;
63 }
64
65 void set_mat(const Eigen::MatrixXd &mat)
66 {
67 assert(is_mat());
68 assert(mat_.rows() == mat.rows());
69 assert(mat_.cols() == mat.cols());
70 mat_ = mat;
71 }
72
73 double get_val() const
74 {
75 return value_;
76 }
77
78 private:
79 std::function<double(double x, double y, double z, double t, int index)> sfunc_;
80 std::function<Eigen::MatrixXd(double x, double y, double z, double t)> tfunc_;
82
83 std::string expr_;
84 double value_;
85 Eigen::MatrixXd mat_;
86 std::vector<ExpressionValue> mat_expr_;
87 std::map<double, int> t_index_;
88 int index_ = -1;
89
90 units::precise_unit unit_type_;
91 units::precise_unit unit_;
92 bool unit_type_set_ = false;
93 };
94 } // namespace utils
95} // namespace polyfem
double val
Definition Assembler.cpp:90
ElementAssemblyValues vals
Definition Assembler.cpp:26
int y
int z
int x
Read-only interface to a hierarchical collection of simulation resources.
std::vector< ExpressionValue > mat_expr_
std::function< double(double x, double y, double z, double t, int index)> sfunc_
std::function< Eigen::MatrixXd(double x, double y, double z, double t)> tfunc_
std::map< double, int > t_index_
void set_unit_type(const std::string &unit_type)
double operator()(double x, double y, double z=0, double t=0, int index=-1) const
const Eigen::MatrixXd & get_mat() const
void init(const json &vals, const io::ResourceIO &resources)
void set_mat(const Eigen::MatrixXd &mat)
nlohmann::json json
Definition Common.hpp:9