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 {
15 bool python_gil_held();
16
18 {
19 public:
21
22 void set_unit_type(const std::string &unit_type)
23 {
24 unit_type_ = units::unit_from_string(unit_type);
25 unit_type_set_ = true;
26 for (auto &expr : mat_expr_)
27 expr.set_unit_type(unit_type);
28 }
29
30 void init(const json &vals, const io::ResourceIO &resources);
31 void init(const double val);
32 void init(const Eigen::MatrixXd &val);
33 void init(const std::string &expr, const io::ResourceIO &resources);
34#ifdef POLYFEM_WITH_PYTHON
35 void init_python(const std::string &path, const std::string &function_name);
36#endif
37
38 void init(const std::function<double(double x, double y, double z)> &func);
39 void init(const std::function<double(double x, double y, double z, double t)> &func);
40 void init(const std::function<double(double x, double y, double z, double t, int index)> &func);
41
42 void init(const std::function<Eigen::MatrixXd(double x, double y, double z)> &func, const int coo);
43 void init(const std::function<Eigen::MatrixXd(double x, double y, double z, double t)> &func, const int coo);
44
45 void set_t(const json &t);
46 void set_index(const int index) { index_ = index; }
47
48 double operator()(double x, double y, double z = 0, double t = 0, int index = -1) const;
49
50 void clear();
51
54 bool is_constant() const
55 {
56 return expr_.find_first_of("xyzt") == std::string::npos && mat_.size() == 0 && mat_expr_.empty()
57 && t_index_.empty() && !sfunc_ && !tfunc_;
58 }
59
62 bool is_time_dependent() const
63 {
64 if (!t_index_.empty() || sfunc_ || tfunc_ || expr_.find('t') != std::string::npos)
65 return true;
66 for (const auto &value : mat_expr_)
67 if (value.is_time_dependent())
68 return true;
69 return false;
70 }
71
72 bool is_zero() const
73 {
74 return expr_.empty() && mat_.size() == 0 && mat_expr_.empty() && !sfunc_ && !tfunc_ && fabs(value_) < 1e-10;
75 }
76 bool is_mat() const
77 {
78 if (expr_.empty() && mat_.size() > 0)
79 return true;
80 return false;
81 }
82
83 const Eigen::MatrixXd &get_mat() const
84 {
85 assert(is_mat());
86 return mat_;
87 }
88
89 void set_mat(const Eigen::MatrixXd &mat)
90 {
91 assert(is_mat());
92 assert(mat_.rows() == mat.rows());
93 assert(mat_.cols() == mat.cols());
94 mat_ = mat;
95 }
96
97 double get_val() const
98 {
99 return value_;
100 }
101
102 private:
103 std::function<double(double x, double y, double z, double t, int index)> sfunc_;
104 std::function<Eigen::MatrixXd(double x, double y, double z, double t)> tfunc_;
106
107 std::string expr_;
108 double value_;
109 Eigen::MatrixXd mat_;
110 std::vector<ExpressionValue> mat_expr_;
111 std::map<double, int> t_index_;
112 int index_ = -1;
113
114 units::precise_unit unit_type_;
115 units::precise_unit unit_;
116 bool unit_type_set_ = false;
117 };
118 } // namespace utils
119} // namespace polyfem
double val
ElementAssemblyValues vals
int y
int z
int x
Read-only interface to a hierarchical collection of simulation resources.
bool is_constant() const
Independent of coordinates, time and index.
bool is_time_dependent() const
Potentially time-dependent: callbacks/time tables or a formula containing 't' (including conservative...
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)
bool python_gil_held()
Whether the caller holds Python's interpreter lock.
nlohmann::json json
Definition Common.hpp:9