10 using namespace utils;
16 std::vector<json> flatten_ids(
const json &p_j_boundary_tmp)
20 std::vector<json> j_boundary;
22 for (
size_t i = 0; i < j_boundary_tmp.size(); ++i)
24 const auto &
tmp = j_boundary_tmp[i];
28 j_boundary.push_back(tmp);
31 if (!
tmp.contains(
"id"))
34 if (tmp[
"id"].is_array())
36 for (
size_t j = 0; j <
tmp[
"id"].size(); ++j)
39 newj[
"id"] =
tmp[
"id"][j].get<
int>();
40 j_boundary.push_back(newj);
44 j_boundary.push_back(tmp);
55 double z = pts.size() == 2 ? 0 : pts(2);
75 assert(pts.size() == 2 || pts.size() == 3);
76 double x = pts(0),
y = pts(1),
z = pts.size() == 3 ? pts(2) : 0.0;
89 for (
int i = 0; i < 3; ++i)
94 for (
int i = 0; i < 3; ++i)
101 v.set_unit_type(units.
force());
110 v.second.set_unit_type(units.
pressure());
113 for (
int i = 0; i < 3; ++i)
114 v.second[i].set_unit_type(units.
velocity());
117 for (
int i = 0; i < 3; ++i)
118 v.second[i].set_unit_type(units.
velocity());
121 for (
int i = 0; i < 3; ++i)
125 v.second.set_unit_type(units.
velocity());
128 v.second.set_unit_type(units.
force());
132 for (
int i = 0; i < 3; ++i)
137 for (
int i = 0; i < 3; ++i)
141 v.set_unit_type(units.
length());
144 v.set_unit_type(units.
force());
153 v.second.set_unit_type(units.
pressure());
156 for (
int i = 0; i < 3; ++i)
157 v.second[i].set_unit_type(units.
length());
160 for (
int i = 0; i < 3; ++i)
161 v.second[i].set_unit_type(units.
velocity());
164 for (
int i = 0; i < 3; ++i)
168 v.second.set_unit_type(units.
length());
171 v.second.set_unit_type(units.
force());
177 val.resize(pts.rows(), pts.cols());
185 const bool planar = pts.cols() == 2;
186 for (
int i = 0; i < pts.rows(); ++i)
188 for (
int j = 0; j < pts.cols(); ++j)
190 double x = pts(i, 0),
y = pts(i, 1),
z = planar ? 0 : pts(i, 2);
222 val = Eigen::MatrixXd::Zero(pts.rows(), mesh.
dimension());
224 for (
long i = 0; i < pts.rows(); ++i)
229 for (
int d = 0; d <
val.cols(); ++d)
241 for (
int d = 0; d <
val.cols(); ++d)
255 val = Eigen::MatrixXd::Zero(pts.rows(), mesh.
dimension());
257 for (
long i = 0; i < pts.rows(); ++i)
265 for (
int d = 0; d <
val.cols(); ++d)
267 val(i, d) =
forces_[b].eval(pts.row(i), d, t);
278 for (
int d = 0; d <
val.cols(); ++d)
290 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
292 for (
long i = 0; i < pts.rows(); ++i)
317 const bool planar = pts.cols() == 2;
318 val.resize(pts.rows(), pts.cols());
320 for (
int i = 0; i < pts.rows(); ++i)
322 for (
int j = 0; j < pts.cols(); ++j)
324 double x = pts(i, 0),
y = pts(i, 1),
z = planar ? 0 : pts(i, 2);
332 const int size = pts.cols();
333 val.resize(pts.rows(), pts.cols() * size);
337 const bool planar = size == 2;
338 for (
int i = 0; i < pts.rows(); ++i)
340 for (
int j = 0; j < pts.cols() * size; ++j)
342 double x = pts(i, 0),
y = pts(i, 1),
z = planar ? 0 : pts(i, 2);
358 for (
int d = 0; d <
val.cols(); ++d)
360 val(d) = tmp.eval(pt, d, t);
369 for (
int d = 0; d <
val.cols(); ++d)
371 val(d) = it->second.eval(pt, d, t);
379 for (
int i = 0; i < n_dirichlet.rows(); ++i)
381 if (n_dirichlet(i, 0) == node_id)
383 for (
int d = 0; d <
val.cols(); ++d)
385 val(d) = n_dirichlet(i, d + 1);
409 for (
int i = 0; i < n_dirichlet.rows(); ++i)
411 if (n_dirichlet(i, 0) == n_id)
439 return it->second.dirichlet_dimension(dim);
444 for (
int i = 0; i < n_dirichlet.rows(); ++i)
446 if (n_dirichlet(i, 0) == n_id)
448 return !std::isnan(n_dirichlet(i, dim + 1));
462 logger().debug(
"Skipping updating in nodes to nodes in problem, already done once...");
467 for (
int n = 0; n < n_dirichlet.rows(); ++n)
469 const int node_id = in_node_to_node[n_dirichlet(n, 0)];
470 n_dirichlet(n, 0) = node_id;
485 auto rr = params[
"rhs"];
488 for (
size_t k = 0; k < rr.size(); ++k)
493 logger().warn(
"Invalid problem rhs: should be an array.");
500 auto ex = params[
"reference"][
"solution"];
504 for (
size_t k = 0; k < ex.size(); ++k)
515 auto ex = params[
"reference"][
"gradient"];
519 for (
size_t k = 0; k < ex.size(); ++k)
532 std::vector<json> j_boundary = flatten_ids(params[
"dirichlet_boundary"]);
539 if (j_boundary[i - offset].is_string())
541 const std::string path =
resolve_path(j_boundary[i - offset], params[
"root_path"]);
542 if (!std::filesystem::is_regular_file(path))
554 if (j_boundary[i - offset][
"id"] ==
"all")
568 auto ff = j_boundary[i - offset][
"value"];
571 for (
size_t k = 0; k < ff.size(); ++k)
574 if (j_boundary[i - offset].contains(
"time_reference") && j_boundary[i - offset][
"time_reference"].size() > 0)
575 displacements_[i].value[k].set_t(j_boundary[i - offset][
"time_reference"]);
589 if (j_boundary[i - offset].contains(
"dimension"))
592 auto &tmp = j_boundary[i - offset][
"dimension"];
593 assert(tmp.is_array());
594 for (
size_t k = 0; k < tmp.size(); ++k)
601 if (j_boundary[i - offset][
"interpolation"].is_array())
603 for (
int ii = 0; ii < j_boundary[i - offset][
"interpolation"].size(); ++ii)
618 auto j_boundary_tmp = params[
"neumann_boundary"];
619 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
622 forces_.resize(offset + j_boundary.size());
628 auto ff = j_boundary[i - offset][
"value"];
629 assert(ff.is_array());
631 for (
size_t k = 0; k < ff.size(); ++k)
634 if (j_boundary[i - offset][
"interpolation"].is_array())
636 for (
int ii = 0; ii < j_boundary[i - offset][
"interpolation"].size(); ++ii)
650 auto j_boundary_tmp = params[
"normal_aligned_neumann_boundary"];
651 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
660 auto ff = j_boundary[i - offset][
"value"];
663 if (j_boundary[i - offset].contains(
"interpolation"))
675 auto j_boundary_tmp = params[
"pressure_boundary"];
676 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
679 pressures_.resize(offset + j_boundary.size());
685 auto ff = j_boundary[i - offset][
"value"];
687 if (j_boundary[i - offset].contains(
"time_reference") && j_boundary[i - offset][
"time_reference"].size() > 0)
688 pressures_[i].value.set_t(j_boundary[i - offset][
"time_reference"]);
690 pressures_[i].interpolation = std::make_shared<NoInterpolation>();
698 auto j_boundary_tmp = params[
"pressure_cavity"];
699 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
705 int boundary_id = j_boundary[i - offset][
"id"];
712 auto ff = j_boundary[i - offset][
"value"];
715 cavity_pressures_[boundary_id].interpolation = std::make_shared<NoInterpolation>();
722 auto rr = params[
"solution"];
724 assert(rr.is_array());
726 for (
size_t k = 0; k < rr.size(); ++k)
729 const auto v = rr[k][
"value"];
730 for (
size_t d = 0; d < v.size(); ++d)
737 auto rr = params[
"velocity"];
739 assert(rr.is_array());
741 for (
size_t k = 0; k < rr.size(); ++k)
744 const auto v = rr[k][
"value"];
745 for (
size_t d = 0; d < v.size(); ++d)
752 auto rr = params[
"acceleration"];
754 assert(rr.is_array());
756 for (
size_t k = 0; k < rr.size(); ++k)
759 const auto v = rr[k][
"value"];
760 for (
size_t d = 0; d < v.size(); ++d)
768 val.resize(pts.rows(), pts.cols());
775 const bool planar = pts.cols() == 2;
776 for (
int i = 0; i < pts.rows(); ++i)
790 val.row(i).setZero();
794 for (
int j = 0; j < pts.cols(); ++j)
801 val.resize(pts.rows(), pts.cols());
808 const bool planar = pts.cols() == 2;
809 for (
int i = 0; i < pts.rows(); ++i)
823 val.row(i).setZero();
827 for (
int j = 0; j < pts.cols(); ++j)
834 val.resize(pts.rows(), pts.cols());
841 const bool planar = pts.cols() == 2;
842 for (
int i = 0; i < pts.rows(); ++i)
856 val.row(i).setZero();
860 for (
int j = 0; j < pts.cols(); ++j)
886 for (
int i = 0; i <
rhs_.size(); ++i)
888 for (
int i = 0; i <
exact_.size(); ++i)
897 :
Problem(name), is_all_(false)
912 v.second.set_unit_type(
"");
917 for (
int i = 0; i < 3; ++i)
923 val.resize(pts.rows(), 1);
929 const bool planar = pts.cols() == 2;
930 for (
int i = 0; i < pts.rows(); ++i)
932 double x = pts(i, 0),
y = pts(i, 1),
z = planar ? 0 : pts(i, 2);
939 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
941 for (
long i = 0; i < pts.rows(); ++i)
965 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
967 for (
long i = 0; i < pts.rows(); ++i)
975 double x = pts(i, 0),
y = pts(i, 1),
z = pts.cols() == 2 ? 0 : pts(i, 2);
985 val.resize(pts.rows(), 1);
992 const bool planar = pts.cols() == 2;
993 for (
int i = 0; i < pts.rows(); ++i)
1018 const bool planar = pts.cols() == 2;
1019 val.resize(pts.rows(), 1);
1021 for (
int i = 0; i < pts.rows(); ++i)
1023 double x = pts(i, 0),
y = pts(i, 1),
z = pts.cols() == 2 ? 0 : pts(i, 2);
1030 val.resize(pts.rows(), pts.cols());
1034 const bool planar = pts.cols() == 2;
1035 for (
int i = 0; i < pts.rows(); ++i)
1037 for (
int j = 0; j < pts.cols(); ++j)
1039 double x = pts(i, 0),
y = pts(i, 1),
z = pts.cols() == 2 ? 0 : pts(i, 2);
1058 throw "Invalid boundary id for pressure update";
1063 Eigen::MatrixXd curr_val =
pressures_[index].value.get_mat();
1064 assert(time_step <= curr_val.size());
1065 assert(curr_val.cols() == 1);
1066 curr_val(time_step) =
val;
1088 throw "Invalid boundary id for dirichlet update";
1091 for (
int i = 0; i <
val.size(); ++i)
1095 Eigen::MatrixXd curr_val =
displacements_[index].value[i].get_mat();
1096 assert(time_step <= curr_val.size());
1097 assert(curr_val.cols() == 1);
1098 curr_val(time_step) =
val(i);
1110 assert(node_ids.size() == nodal_dirichlet.rows());
1114 for (
int i = 0; i < node_ids.size(); ++i)
1116 int mapped_node_id = in_node_to_node(node_ids(i));
1117 for (
int j = 0; j < n_dirichlet.rows(); ++j)
1118 if (mapped_node_id == n_dirichlet(j, 0))
1119 for (
int k = 0; k < n_dirichlet.cols() - 1; ++k)
1120 n_dirichlet(j, k + 1) = nodal_dirichlet(i, k);
1126 val = Eigen::MatrixXd::Zero(1, 1);
1134 val(0) = tmp.eval(pt, t);
1141 val(0) = it->second.eval(pt, t);
1147 for (
int i = 0; i < n_dirichlet.rows(); ++i)
1149 if (n_dirichlet(i, 0) == node_id)
1151 val(0) = n_dirichlet(i, 1);
1173 for (
int i = 0; i < n_dirichlet.rows(); ++i)
1175 if (n_dirichlet(i, 0) == n_id)
1204 for (
int n = 0; n < n_dirichlet.rows(); ++n)
1206 const int node_id = in_node_to_node[n_dirichlet(n, 0)];
1207 n_dirichlet(n, 0) = node_id;
1227 has_exact_ = !params[
"reference"][
"solution"].empty();
1228 exact_.
init(params[
"reference"][
"solution"]);
1233 auto ex = params[
"reference"][
"gradient"];
1237 for (
size_t k = 0; k < ex.size(); ++k)
1250 std::vector<json> j_boundary = flatten_ids(params[
"dirichlet_boundary"]);
1253 dirichlet_.resize(offset + j_boundary.size());
1257 if (j_boundary[i - offset].is_string())
1259 const std::string path =
resolve_path(j_boundary[i - offset], params[
"root_path"]);
1260 if (!std::filesystem::is_regular_file(path))
1263 Eigen::MatrixXd tmp;
1270 int current_id = -1;
1272 if (j_boundary[i - offset][
"id"] ==
"all")
1287 auto ff = j_boundary[i - offset][
"value"];
1291 if (j_boundary[i - offset][
"interpolation"].is_array())
1293 if (j_boundary[i - offset][
"interpolation"].size() == 0)
1294 dirichlet_[i].interpolation = std::make_shared<NoInterpolation>();
1295 else if (j_boundary[i - offset][
"interpolation"].size() == 1)
1311 auto j_boundary_tmp = params[
"neumann_boundary"];
1312 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
1315 neumann_.resize(offset + j_boundary.size());
1321 auto ff = j_boundary[i - offset][
"value"];
1324 if (j_boundary[i - offset][
"interpolation"].is_array())
1326 if (j_boundary[i - offset][
"interpolation"].size() == 0)
1327 neumann_[i].interpolation = std::make_shared<NoInterpolation>();
1328 else if (j_boundary[i - offset][
"interpolation"].size() == 1)
1340 auto rr = params[
"solution"];
1342 assert(rr.is_array());
1344 for (
size_t k = 0; k < rr.size(); ++k)
std::string force() const
std::string pressure() const
std::string velocity() const
std::string acceleration() const
const std::string & length() const
virtual bool is_fluid() const
bool is_nodal_neumann_boundary(const int n_id, const int tag) override
void dirichlet_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
bool has_nodal_dirichlet() override
void initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const override
std::vector< std::pair< int, utils::ExpressionValue > > initial_solution_
void set_parameters(const json ¶ms) override
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::vector< ScalarBCValue > neumann_
void update_nodes(const Eigen::VectorXi &in_node_to_node) override
bool is_rhs_zero() const override
bool is_nodal_dirichlet_boundary(const int n_id, const int tag) override
void dirichlet_nodal_value(const mesh::Mesh &mesh, const int node_id, const RowVectorNd &pt, const double t, Eigen::MatrixXd &val) const override
utils::ExpressionValue exact_
std::array< utils::ExpressionValue, 3 > exact_grad_
bool has_nodal_neumann() override
void exact_grad(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::map< int, ScalarBCValue > nodal_dirichlet_
void set_units(const assembler::Assembler &assembler, const Units &units) override
GenericScalarProblem(const std::string &name)
utils::ExpressionValue rhs_
std::map< int, ScalarBCValue > nodal_neumann_
void neumann_nodal_value(const mesh::Mesh &mesh, const int node_id, const RowVectorNd &pt, const Eigen::MatrixXd &normal, const double t, Eigen::MatrixXd &val) const override
bool has_exact_sol() const override
void neumann_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &normals, const double t, Eigen::MatrixXd &val) const override
std::vector< ScalarBCValue > dirichlet_
void exact(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::vector< Eigen::MatrixXd > nodal_dirichlet_mat_
void update_pressure_boundary(const int id, const int time_step, const double val)
std::map< int, TensorBCValue > nodal_neumann_
std::unordered_map< int, ScalarBCValue > cavity_pressures_
void neumann_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &normals, const double t, Eigen::MatrixXd &val) const override
std::vector< std::pair< int, std::array< utils::ExpressionValue, 3 > > > initial_position_
void set_parameters(const json ¶ms) override
bool all_dimensions_dirichlet() const override
void update_nodes(const Eigen::VectorXi &in_node_to_node) override
GenericTensorProblem(const std::string &name)
void initial_velocity(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const override
bool all_dimensions_dirichlet_
std::vector< std::pair< int, std::array< utils::ExpressionValue, 3 > > > initial_acceleration_
std::vector< std::pair< int, std::array< utils::ExpressionValue, 3 > > > initial_velocity_
void update_dirichlet_nodes(const Eigen::VectorXi &in_node_to_node, const Eigen::VectorXi &node_ids, const Eigen::MatrixXd &nodal_dirichlet)
std::vector< TensorBCValue > displacements_
std::vector< ScalarBCValue > normal_aligned_forces_
bool is_nodal_dirichlet_boundary(const int n_id, const int tag) override
bool is_rhs_zero() const override
bool has_nodal_neumann() override
void neumann_nodal_value(const mesh::Mesh &mesh, const int node_id, const RowVectorNd &pt, const Eigen::MatrixXd &normal, const double t, Eigen::MatrixXd &val) const override
void initial_acceleration(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const override
bool has_nodal_dirichlet() override
void pressure_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &normals, const double t, Eigen::MatrixXd &val) const override
bool is_dimension_dirichet(const int tag, const int dim) const override
void exact(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::map< int, TensorBCValue > nodal_dirichlet_
std::array< utils::ExpressionValue, 3 > rhs_
std::array< utils::ExpressionValue, 9 > exact_grad_
std::vector< TensorBCValue > forces_
void update_dirichlet_boundary(const int id, const int time_step, const Eigen::VectorXd &val)
double pressure_cavity_bc(const int boundary_id, const double t) const override
void dirichlet_nodal_value(const mesh::Mesh &mesh, const int node_id, const RowVectorNd &pt, const double t, Eigen::MatrixXd &val) const override
void dirichlet_bc(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &uv, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::vector< ScalarBCValue > pressures_
bool is_nodal_neumann_boundary(const int n_id, const int tag) override
bool has_exact_sol() const override
void exact_grad(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
bool is_nodal_dimension_dirichlet(const int n_id, const int tag, const int dim) const override
void set_units(const assembler::Assembler &assembler, const Units &units) override
void initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val) const override
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::array< utils::ExpressionValue, 3 > exact_
std::vector< Eigen::MatrixXd > nodal_dirichlet_mat_
std::vector< int > normal_aligned_neumann_boundary_ids_
bool updated_dirichlet_node_ordering_
std::vector< int > pressure_boundary_ids_
std::vector< int > boundary_ids_
virtual void init(const mesh::Mesh &mesh)
std::vector< int > neumann_boundary_ids_
std::vector< int > pressure_cavity_ids_
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
virtual int get_body_id(const int primitive) const
Get the volume selection of an element (cell in 3d, face in 2d)
virtual int get_boundary_id(const int primitive) const
Get the boundary selection of an element (face in 3d, edge in 2d)
int dimension() const
utily for dimension
virtual int get_node_id(const int node_id) const
Get the boundary selection of a node.
void set_unit_type(const std::string &unit_type)
void init(const json &vals)
static std::shared_ptr< Interpolation > build(const json ¶ms)
bool read_matrix(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
Reads a matrix from a file. Determines the file format based on the path's extension.
std::string resolve_path(const std::string &path, const std::string &input_file_path, const bool only_if_exists=false)
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
bool is_param_valid(const json ¶ms, const std::string &key)
Determine if a key exists and is non-null in a json object.
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)
std::shared_ptr< utils::Interpolation > interpolation
double eval(const RowVectorNd &pts, const double t) const
utils::ExpressionValue value
std::vector< std::shared_ptr< utils::Interpolation > > interpolation
std::array< utils::ExpressionValue, 3 > value
double eval(const RowVectorNd &pts, const int dim, const double t, const int el_id=-1) const