19#include <igl/per_face_normals.h>
24 using namespace assembler;
25 using namespace basis;
29 void flattened_tensor_coeffs(
const Eigen::MatrixXd &S, Eigen::MatrixXd &X)
33 X.resize(S.rows(), 3);
38 else if (S.cols() == 9)
41 X.resize(S.rows(), 6);
51 logger().error(
"Invalid tensor dimensions.");
58 const bool is_problem_scalar,
59 const std::vector<basis::ElementBases> &bases,
60 const std::vector<basis::ElementBases> &gbases,
61 const Eigen::MatrixXd &pts,
62 const Eigen::MatrixXi &
faces,
63 const Eigen::MatrixXd &fun,
64 const bool compute_avg,
65 Eigen::MatrixXd &result)
69 logger().error(
"Solve the problem first!");
74 const Mesh3D &mesh3d =
dynamic_cast<const Mesh3D &
>(mesh);
76 Eigen::MatrixXd points, uv;
77 Eigen::VectorXd weights;
80 if (!is_problem_scalar)
83 igl::AABB<Eigen::MatrixXd, 3> tree;
84 tree.init(pts,
faces);
86 result.resize(
faces.rows(), actual_dim);
87 result.setConstant(std::numeric_limits<double>::quiet_NaN());
98 const int face_id = mesh3d.
cell_face(e, lf);
119 for (
size_t j = 0; j < bs.
bases.size(); ++j)
124 for (
int d = 0; d < actual_dim; ++d)
126 for (
size_t g = 0; g < v.
global.size(); ++g)
128 loc_val(d) += (v.
global[g].val * v.
val.array() * fun(v.
global[g].index * actual_dim + d) * weights.array()).sum();
134 Eigen::RowVector3d C;
137 const double dist = tree.squared_distance(pts,
faces, bary, I, C);
138 assert(dist < 1e-16);
140 assert(std::isnan(result(I, 0)));
142 result.row(I) = loc_val / weights.sum();
144 result.row(I) = loc_val;
149 assert(counter == result.rows());
154 const bool is_problem_scalar,
156 const std::vector<basis::ElementBases> &bases,
157 const std::vector<basis::ElementBases> &gbases,
158 const Eigen::VectorXi &disc_orders,
159 const std::map<int, Eigen::MatrixXd> &polys,
160 const std::map<
int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &polys_3d,
165 const Eigen::MatrixXd &fun,
166 std::vector<assembler::Assembler::NamedMatrix> &result_scalar,
167 std::vector<assembler::Assembler::NamedMatrix> &result_tensor,
168 const bool use_sampler,
169 const bool boundary_only)
171 result_scalar.clear();
172 result_tensor.clear();
176 logger().error(
"Solve the problem first!");
179 if (is_problem_scalar)
181 logger().error(
"Define a tensor problem!");
185 assert(!is_problem_scalar);
188 std::vector<Eigen::MatrixXd> avg_scalar;
190 Eigen::MatrixXd areas(n_bases, 1);
193 std::vector<std::pair<std::string, Eigen::MatrixXd>> tmp_s;
194 Eigen::MatrixXd local_val;
197 for (
int i = 0; i < int(bases.size()); ++i)
201 Eigen::MatrixXd local_pts;
223 vals.compute(i, actual_dim == 3, bases[i], gbases[i]);
225 const double area = (
vals.det.array() *
quadrature.weights.array()).sum();
232 for (
size_t j = 0; j < bs.
bases.size(); ++j)
235 if (b.
global().size() > 1)
238 auto &global = b.
global().front();
239 areas(global.index) += area;
242 if (avg_scalar.empty())
244 avg_scalar.resize(tmp_s.size());
245 for (
auto &m : avg_scalar)
247 m.resize(n_bases, 1);
252 for (
int k = 0; k < tmp_s.size(); ++k)
254 local_val = tmp_s[k].second;
256 for (
size_t j = 0; j < bs.
bases.size(); ++j)
259 if (b.
global().size() > 1)
262 auto &global = b.
global().front();
263 avg_scalar[k](global.index) += local_val(j) * area;
268 for (
auto &m : avg_scalar)
270 m.array() /= areas.array();
273 result_scalar.resize(tmp_s.size());
274 for (
int k = 0; k < tmp_s.size(); ++k)
276 result_scalar[k].first = tmp_s[k].first;
278 avg_scalar[k], result_scalar[k].second, use_sampler, boundary_only);
285 const bool is_problem_scalar,
286 const std::vector<basis::ElementBases> &bases,
287 const std::vector<basis::ElementBases> &gbases,
288 const Eigen::VectorXi &disc_orders,
290 const Eigen::MatrixXd &fun,
292 Eigen::MatrixXd &result,
293 Eigen::VectorXd &von_mises)
302 logger().error(
"Solve the problem first!");
305 if (is_problem_scalar)
307 logger().error(
"Define a tensor problem!");
312 assert(!is_problem_scalar);
314 Eigen::MatrixXd local_val, local_stress, local_mises;
316 int num_quadr_pts = 0;
317 result.resize(disc_orders.sum(), actual_dim == 2 ? 3 : 6);
319 von_mises.resize(disc_orders.sum(), 1);
330 f.get_quadrature(disc_orders(e), quadr);
335 f.get_quadrature(disc_orders(e), quadr);
343 f.get_quadrature(disc_orders(e), quadr);
348 f.get_quadrature(disc_orders(e), quadr);
356 std::vector<std::pair<std::string, Eigen::MatrixXd>> tmp_s, tmp_t;
361 local_mises = tmp_s[0].second;
362 local_val = tmp_t[0].second;
364 if (num_quadr_pts + local_val.rows() >= result.rows())
366 result.conservativeResize(
367 std::max(num_quadr_pts + local_val.rows() + 1, 2 * result.rows()),
369 von_mises.conservativeResize(result.rows(), von_mises.cols());
371 flattened_tensor_coeffs(local_val, local_stress);
372 result.block(num_quadr_pts, 0, local_stress.rows(), local_stress.cols()) = local_stress;
373 von_mises.block(num_quadr_pts, 0, local_mises.rows(), local_mises.cols()) = local_mises;
374 num_quadr_pts += local_val.rows();
376 result.conservativeResize(num_quadr_pts, result.cols());
377 von_mises.conservativeResize(num_quadr_pts, von_mises.cols());
382 const bool is_problem_scalar,
383 const std::vector<basis::ElementBases> &bases,
384 const Eigen::VectorXi &disc_orders,
385 const std::map<int, Eigen::MatrixXd> &polys,
386 const std::map<
int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &polys_3d,
389 const Eigen::MatrixXd &fun,
390 Eigen::MatrixXd &result,
391 const bool use_sampler,
392 const bool boundary_only)
395 if (!is_problem_scalar)
398 polys, polys_3d, sampler, n_points,
399 fun, result, use_sampler, boundary_only);
404 const int actual_dim,
405 const std::vector<basis::ElementBases> &basis,
406 const Eigen::VectorXi &disc_orders,
407 const std::map<int, Eigen::MatrixXd> &polys,
408 const std::map<
int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &polys_3d,
411 const Eigen::MatrixXd &fun,
412 Eigen::MatrixXd &result,
413 const bool use_sampler,
414 const bool boundary_only)
418 logger().error(
"Solve the problem first!");
422 std::vector<AssemblyValues> tmp;
424 result.resize(n_points, actual_dim);
428 Eigen::MatrixXi vis_faces_poly, vis_edges_poly;
430 for (
int i = 0; i < int(basis.size()); ++i)
433 Eigen::MatrixXd local_pts;
447 sampler.
sample_polyhedron(polys_3d.at(i).first, polys_3d.at(i).second, local_pts, vis_faces_poly, vis_edges_poly);
449 sampler.
sample_polygon(polys.at(i), local_pts, vis_faces_poly, vis_edges_poly);
474 Eigen::MatrixXd local_res = Eigen::MatrixXd::Zero(local_pts.rows(), actual_dim);
476 for (
size_t j = 0; j < bs.
bases.size(); ++j)
480 for (
int d = 0; d < actual_dim; ++d)
482 for (
size_t ii = 0; ii < b.
global().size(); ++ii)
483 local_res.col(d) += b.
global()[ii].val * tmp[j].val * fun(b.
global()[ii].index * actual_dim + d);
487 result.block(index, 0, local_res.rows(), actual_dim) = local_res;
488 index += local_res.rows();
494 const bool is_problem_scalar,
495 const std::vector<basis::ElementBases> &bases,
496 const std::vector<basis::ElementBases> &gbases,
498 const Eigen::MatrixXd &local_pts,
499 const Eigen::MatrixXd &fun,
500 Eigen::MatrixXd &result,
501 Eigen::MatrixXd &result_grad)
504 if (!is_problem_scalar)
507 local_pts, fun, result, result_grad);
512 const int actual_dim,
513 const std::vector<basis::ElementBases> &bases,
514 const std::vector<basis::ElementBases> &gbases,
516 const Eigen::MatrixXd &local_pts,
517 const Eigen::MatrixXd &fun,
518 Eigen::MatrixXd &result,
519 Eigen::MatrixXd &result_grad)
523 logger().error(
"Solve the problem first!");
527 assert(local_pts.cols() == mesh.
dimension());
528 assert(fun.cols() == 1);
536 result.resize(
vals.val.rows(), actual_dim);
539 result_grad.resize(
vals.val.rows(), mesh.
dimension() * actual_dim);
540 result_grad.setZero();
542 const int n_loc_bases = int(
vals.basis_values.size());
544 for (
int i = 0; i < n_loc_bases; ++i)
546 const auto &
val =
vals.basis_values[i];
548 for (
size_t ii = 0; ii <
val.global.size(); ++ii)
550 for (
int d = 0; d < actual_dim; ++d)
552 result.col(d) +=
val.global[ii].val * fun(
val.global[ii].index * actual_dim + d) *
val.val;
553 result_grad.block(0, d *
val.grad_t_m.cols(), result_grad.rows(),
val.grad_t_m.cols()) +=
val.global[ii].val * fun(
val.global[ii].index * actual_dim + d) *
val.grad_t_m;
563 logger().error(
"Solve the problem first!");
567 assert(fun.cols() == 1);
569 result.resize(
vals.val.rows(), actual_dim);
572 result_grad.resize(
vals.val.rows(), dim * actual_dim);
573 result_grad.setZero();
575 const int n_loc_bases = int(
vals.basis_values.size());
577 for (
int i = 0; i < n_loc_bases; ++i)
579 const auto &
val =
vals.basis_values[i];
581 for (
size_t ii = 0; ii <
val.global.size(); ++ii)
583 for (
int d = 0; d < actual_dim; ++d)
585 result.col(d) +=
val.global[ii].val * fun(
val.global[ii].index * actual_dim + d) *
val.val;
586 result_grad.block(0, d *
val.grad_t_m.cols(), result_grad.rows(),
val.grad_t_m.cols()) +=
val.global[ii].val * fun(
val.global[ii].index * actual_dim + d) *
val.grad_t_m;
594 const bool is_problem_scalar,
595 const std::vector<basis::ElementBases> &bases,
596 const std::vector<basis::ElementBases> &gbases,
597 const Eigen::VectorXi &disc_orders,
598 const std::map<int, Eigen::MatrixXd> &polys,
599 const std::map<
int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &polys_3d,
602 const Eigen::MatrixXd &fun,
604 const bool use_sampler,
605 const bool boundary_only)
609 logger().error(
"Solve the problem first!");
613 assert(!is_problem_scalar);
615 Eigen::MatrixXi vis_faces_poly, vis_edges_poly;
617 std::vector<std::pair<std::string, Eigen::MatrixXd>> tmp_s;
619 for (
int i = 0; i < int(bases.size()); ++i)
626 Eigen::MatrixXd local_pts;
637 sampler.
sample_polyhedron(polys_3d.at(i).first, polys_3d.at(i).second, local_pts, vis_faces_poly, vis_edges_poly);
639 sampler.
sample_polygon(polys.at(i), local_pts, vis_faces_poly, vis_edges_poly);
666 for (
const auto &s : tmp_s)
667 if (std::isnan(s.second.norm()))
676 const bool is_problem_scalar,
677 const std::vector<basis::ElementBases> &bases,
678 const std::vector<basis::ElementBases> &gbases,
679 const Eigen::VectorXi &disc_orders,
680 const std::map<int, Eigen::MatrixXd> &polys,
681 const std::map<
int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &polys_3d,
685 const Eigen::MatrixXd &fun,
687 std::vector<assembler::Assembler::NamedMatrix> &result,
688 const bool use_sampler,
689 const bool boundary_only)
693 logger().error(
"Solve the problem first!");
699 assert(!is_problem_scalar);
703 Eigen::MatrixXi vis_faces_poly, vis_edges_poly;
704 std::vector<std::pair<std::string, Eigen::MatrixXd>> tmp_s;
706 for (
int i = 0; i < int(bases.size()); ++i)
713 Eigen::MatrixXd local_pts;
724 sampler.
sample_polyhedron(polys_3d.at(i).first, polys_3d.at(i).second, local_pts, vis_faces_poly, vis_edges_poly);
726 sampler.
sample_polygon(polys.at(i), local_pts, vis_faces_poly, vis_edges_poly);
755 result.resize(tmp_s.size());
756 for (
int k = 0; k < tmp_s.size(); ++k)
758 result[k].first = tmp_s[k].first;
759 result[k].second.resize(n_points, 1);
763 for (
int k = 0; k < tmp_s.size(); ++k)
765 assert(local_pts.rows() == tmp_s[k].second.rows());
766 result[k].second.block(index, 0, tmp_s[k].second.rows(), 1) = tmp_s[k].second;
768 index += local_pts.rows();
774 const bool is_problem_scalar,
775 const std::vector<basis::ElementBases> &bases,
776 const std::vector<basis::ElementBases> &gbases,
777 const Eigen::VectorXi &disc_orders,
778 const std::map<int, Eigen::MatrixXd> &polys,
779 const std::map<
int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &polys_3d,
783 const Eigen::MatrixXd &fun,
785 std::vector<assembler::Assembler::NamedMatrix> &result,
786 const bool use_sampler,
787 const bool boundary_only)
791 logger().error(
"Solve the problem first!");
798 assert(!is_problem_scalar);
802 Eigen::MatrixXi vis_faces_poly, vis_edges_poly;
803 std::vector<std::pair<std::string, Eigen::MatrixXd>> tmp_t;
805 for (
int i = 0; i < int(bases.size()); ++i)
812 Eigen::MatrixXd local_pts;
823 sampler.
sample_polyhedron(polys_3d.at(i).first, polys_3d.at(i).second, local_pts, vis_faces_poly, vis_edges_poly);
825 sampler.
sample_polygon(polys.at(i), local_pts, vis_faces_poly, vis_edges_poly);
854 result.resize(tmp_t.size());
855 for (
int k = 0; k < tmp_t.size(); ++k)
857 result[k].first = tmp_t[k].first;
858 result[k].second.resize(n_points, actual_dim * actual_dim);
862 for (
int k = 0; k < tmp_t.size(); ++k)
864 assert(local_pts.rows() == tmp_t[k].second.rows());
865 result[k].second.block(index, 0, tmp_t[k].second.rows(), tmp_t[k].second.cols()) = tmp_t[k].second;
867 index += local_pts.rows();
873 const std::shared_ptr<mesh::MeshNodes> mesh_nodes)
875 Eigen::MatrixXd func;
876 func.setZero(n_bases, mesh_nodes->node_position(0).size());
878 for (
int i = 0; i < n_bases; i++)
879 func.row(i) = mesh_nodes->node_position(i);
886 const std::shared_ptr<mesh::MeshNodes> mesh_nodes,
887 const Eigen::MatrixXd &grad)
893 const std::vector<basis::ElementBases> &bases,
894 const std::vector<basis::ElementBases> &gbases,
895 const Eigen::MatrixXd &fun,
897 const int actual_dim)
899 Eigen::VectorXd result;
900 result.setZero(actual_dim);
901 for (
int e = 0; e < bases.size(); ++e)
906 Eigen::MatrixXd u, grad_u;
910 result += u.transpose() *
da;
ElementAssemblyValues vals
std::vector< Eigen::VectorXi > faces
virtual void compute_scalar_value(const OutputData &data, std::vector< NamedMatrix > &result) const
virtual void compute_tensor_value(const OutputData &data, std::vector< NamedMatrix > &result) const
stores per local bases evaluations
std::vector< basis::Local2Global > global
stores per element basis values at given quadrature points and geometric mapping
void compute(const int el_index, const bool is_volume, const Eigen::MatrixXd &pts, const basis::ElementBases &basis, const basis::ElementBases &gbasis)
computes the per element values at the local (ref el) points (pts) sets basis_values,...
Represents one basis function and its gradient.
const std::vector< Local2Global > & global() const
Stores the basis functions for a given element in a mesh (facet in 2d, cell in 3d).
void evaluate_bases(const Eigen::MatrixXd &uv, std::vector< assembler::AssemblyValues > &basis_values) const
evaluate stored bases at given points on the reference element saves results to basis_values
std::vector< Basis > bases
one basis function per node in the element
static Eigen::MatrixXd generate_linear_field(const int n_bases, const std::shared_ptr< mesh::MeshNodes > mesh_nodes, const Eigen::MatrixXd &grad)
bool check_scalar_value(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXi &disc_orders, const std::map< int, Eigen::MatrixXd > &polys, const std::map< int, std::pair< Eigen::MatrixXd, Eigen::MatrixXi > > &polys_3d, const assembler::Assembler &assembler, const utils::RefElementSampler &sampler, const Eigen::MatrixXd &fun, const double t, const bool use_sampler, const bool boundary_only)
checks if mises are not nan
static void interpolate_at_local_vals(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const int el_index, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &fun, Eigen::MatrixXd &result, Eigen::MatrixXd &result_grad)
interpolate solution and gradient at element (calls interpolate_at_local_vals with sol)
static void compute_stress_at_quadrature_points(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXi &disc_orders, const assembler::Assembler &assembler, const Eigen::MatrixXd &fun, const double t, Eigen::MatrixXd &result, Eigen::VectorXd &von_mises)
compute von mises stress at quadrature points for the function fun, also compute the interpolated fun...
static Eigen::MatrixXd get_bases_position(const int n_bases, const std::shared_ptr< mesh::MeshNodes > mesh_nodes)
static void interpolate_boundary_function(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::MatrixXd &pts, const Eigen::MatrixXi &faces, const Eigen::MatrixXd &fun, const bool compute_avg, Eigen::MatrixXd &result)
computes integrated solution (fun) per surface face.
static void interpolate_function(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const Eigen::VectorXi &disc_orders, const std::map< int, Eigen::MatrixXd > &polys, const std::map< int, std::pair< Eigen::MatrixXd, Eigen::MatrixXi > > &polys_3d, const utils::RefElementSampler &sampler, const int n_points, const Eigen::MatrixXd &fun, Eigen::MatrixXd &result, const bool use_sampler, const bool boundary_only)
interpolate the function fun.
static void average_grad_based_function(const mesh::Mesh &mesh, const bool is_problem_scalar, const int n_bases, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXi &disc_orders, const std::map< int, Eigen::MatrixXd > &polys, const std::map< int, std::pair< Eigen::MatrixXd, Eigen::MatrixXi > > &polys_3d, const assembler::Assembler &assembler, const utils::RefElementSampler &sampler, const double t, const int n_points, const Eigen::MatrixXd &fun, std::vector< assembler::Assembler::NamedMatrix > &result_scalar, std::vector< assembler::Assembler::NamedMatrix > &result_tensor, const bool use_sampler, const bool boundary_only)
computes scalar quantity of funtion (ie von mises for elasticity and norm of velocity for fluid) the ...
static Eigen::VectorXd integrate_function(const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::MatrixXd &fun, const int dim, const int actual_dim)
static void compute_scalar_value(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXi &disc_orders, const std::map< int, Eigen::MatrixXd > &polys, const std::map< int, std::pair< Eigen::MatrixXd, Eigen::MatrixXi > > &polys_3d, const assembler::Assembler &assembler, const utils::RefElementSampler &sampler, const int n_points, const Eigen::MatrixXd &fun, const double t, std::vector< assembler::Assembler::NamedMatrix > &result, const bool use_sampler, const bool boundary_only)
computes scalar quantity of funtion (ie von mises for elasticity and norm of velocity for fluid)
static void compute_tensor_value(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const Eigen::VectorXi &disc_orders, const std::map< int, Eigen::MatrixXd > &polys, const std::map< int, std::pair< Eigen::MatrixXd, Eigen::MatrixXi > > &polys_3d, const assembler::Assembler &assembler, const utils::RefElementSampler &sampler, const int n_points, const Eigen::MatrixXd &fun, const double t, std::vector< assembler::Assembler::NamedMatrix > &result, const bool use_sampler, const bool boundary_only)
compute tensor quantity (ie stress tensor or velocy)
virtual int n_cell_faces(const int c_id) const =0
virtual int cell_face(const int c_id, const int lf_id) const =0
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
virtual RowVectorNd face_barycenter(const int f) const =0
face barycenter
bool is_cube(const int el_id) const
checks if element is cube compatible
virtual bool is_boundary_face(const int face_global_id) const =0
is face boundary
bool is_simplex(const int el_id) const
checks if element is simples compatible
virtual bool is_volume() const =0
checks if mesh is volume
int dimension() const
utily for dimension
virtual bool is_boundary_element(const int element_global_id) const =0
is cell boundary
static void quadrature_for_quad_face(int index, int order, const int gid, const mesh::Mesh &mesh, Eigen::MatrixXd &uv, Eigen::MatrixXd &points, Eigen::VectorXd &weights)
static void quadrature_for_tri_face(int index, int order, const int gid, const mesh::Mesh &mesh, Eigen::MatrixXd &uv, Eigen::MatrixXd &points, Eigen::VectorXd &weights)
void sample_polygon(const Eigen::MatrixXd &poly, Eigen::MatrixXd &pts, Eigen::MatrixXi &faces, Eigen::MatrixXi &edges) const
const Eigen::MatrixXd & simplex_points() const
void sample_polyhedron(const Eigen::MatrixXd &vertices, const Eigen::MatrixXi &f, Eigen::MatrixXd &pts, Eigen::MatrixXi &faces, Eigen::MatrixXi &edges) const
const Eigen::MatrixXd & cube_points() const
void q_nodes_2d(const int q, Eigen::MatrixXd &val)
void p_nodes_2d(const int p, Eigen::MatrixXd &val)
void p_nodes_3d(const int p, Eigen::MatrixXd &val)
void q_nodes_3d(const int q, Eigen::MatrixXd &val)
Eigen::VectorXd flatten(const Eigen::MatrixXd &X)
Flatten rowwises.
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd