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));
461 for (
int n = 0; n < n_dirichlet.rows(); ++n)
463 const int node_id = in_node_to_node[n_dirichlet(n, 0)];
464 n_dirichlet(n, 0) = node_id;
478 auto rr = params[
"rhs"];
481 for (
size_t k = 0; k < rr.size(); ++k)
486 logger().warn(
"Invalid problem rhs: should be an array.");
493 auto ex = params[
"reference"][
"solution"];
497 for (
size_t k = 0; k < ex.size(); ++k)
508 auto ex = params[
"reference"][
"gradient"];
512 for (
size_t k = 0; k < ex.size(); ++k)
525 std::vector<json> j_boundary = flatten_ids(params[
"dirichlet_boundary"]);
532 if (j_boundary[i - offset].is_string())
534 const std::string path =
resolve_path(j_boundary[i - offset], params[
"root_path"]);
535 if (!std::filesystem::is_regular_file(path))
547 if (j_boundary[i - offset][
"id"] ==
"all")
561 auto ff = j_boundary[i - offset][
"value"];
564 for (
size_t k = 0; k < ff.size(); ++k)
567 if (j_boundary[i - offset].contains(
"time_reference") && j_boundary[i - offset][
"time_reference"].size() > 0)
568 displacements_[i].value[k].set_t(j_boundary[i - offset][
"time_reference"]);
582 if (j_boundary[i - offset].contains(
"dimension"))
585 auto &tmp = j_boundary[i - offset][
"dimension"];
586 assert(tmp.is_array());
587 for (
size_t k = 0; k < tmp.size(); ++k)
594 if (j_boundary[i - offset][
"interpolation"].is_array())
596 for (
int ii = 0; ii < j_boundary[i - offset][
"interpolation"].size(); ++ii)
611 auto j_boundary_tmp = params[
"neumann_boundary"];
612 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
615 forces_.resize(offset + j_boundary.size());
621 auto ff = j_boundary[i - offset][
"value"];
622 assert(ff.is_array());
624 for (
size_t k = 0; k < ff.size(); ++k)
627 if (j_boundary[i - offset][
"interpolation"].is_array())
629 for (
int ii = 0; ii < j_boundary[i - offset][
"interpolation"].size(); ++ii)
643 auto j_boundary_tmp = params[
"normal_aligned_neumann_boundary"];
644 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
653 auto ff = j_boundary[i - offset][
"value"];
656 if (j_boundary[i - offset].contains(
"interpolation"))
668 auto j_boundary_tmp = params[
"pressure_boundary"];
669 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
672 pressures_.resize(offset + j_boundary.size());
678 auto ff = j_boundary[i - offset][
"value"];
680 if (j_boundary[i - offset].contains(
"time_reference") && j_boundary[i - offset][
"time_reference"].size() > 0)
681 pressures_[i].value.set_t(j_boundary[i - offset][
"time_reference"]);
683 pressures_[i].interpolation = std::make_shared<NoInterpolation>();
691 auto j_boundary_tmp = params[
"pressure_cavity"];
692 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
698 int boundary_id = j_boundary[i - offset][
"id"];
705 auto ff = j_boundary[i - offset][
"value"];
708 cavity_pressures_[boundary_id].interpolation = std::make_shared<NoInterpolation>();
715 auto rr = params[
"solution"];
717 assert(rr.is_array());
719 for (
size_t k = 0; k < rr.size(); ++k)
722 const auto v = rr[k][
"value"];
723 for (
size_t d = 0; d < v.size(); ++d)
730 auto rr = params[
"velocity"];
732 assert(rr.is_array());
734 for (
size_t k = 0; k < rr.size(); ++k)
737 const auto v = rr[k][
"value"];
738 for (
size_t d = 0; d < v.size(); ++d)
745 auto rr = params[
"acceleration"];
747 assert(rr.is_array());
749 for (
size_t k = 0; k < rr.size(); ++k)
752 const auto v = rr[k][
"value"];
753 for (
size_t d = 0; d < v.size(); ++d)
761 val.resize(pts.rows(), pts.cols());
768 const bool planar = pts.cols() == 2;
769 for (
int i = 0; i < pts.rows(); ++i)
783 val.row(i).setZero();
787 for (
int j = 0; j < pts.cols(); ++j)
794 val.resize(pts.rows(), pts.cols());
801 const bool planar = pts.cols() == 2;
802 for (
int i = 0; i < pts.rows(); ++i)
816 val.row(i).setZero();
820 for (
int j = 0; j < pts.cols(); ++j)
827 val.resize(pts.rows(), pts.cols());
834 const bool planar = pts.cols() == 2;
835 for (
int i = 0; i < pts.rows(); ++i)
849 val.row(i).setZero();
853 for (
int j = 0; j < pts.cols(); ++j)
879 for (
int i = 0; i <
rhs_.size(); ++i)
881 for (
int i = 0; i <
exact_.size(); ++i)
889 :
Problem(name), is_all_(false)
904 v.second.set_unit_type(
"");
909 for (
int i = 0; i < 3; ++i)
915 val.resize(pts.rows(), 1);
921 const bool planar = pts.cols() == 2;
922 for (
int i = 0; i < pts.rows(); ++i)
924 double x = pts(i, 0),
y = pts(i, 1),
z = planar ? 0 : pts(i, 2);
931 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
933 for (
long i = 0; i < pts.rows(); ++i)
957 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
959 for (
long i = 0; i < pts.rows(); ++i)
967 double x = pts(i, 0),
y = pts(i, 1),
z = pts.cols() == 2 ? 0 : pts(i, 2);
977 val.resize(pts.rows(), 1);
984 const bool planar = pts.cols() == 2;
985 for (
int i = 0; i < pts.rows(); ++i)
1010 const bool planar = pts.cols() == 2;
1011 val.resize(pts.rows(), 1);
1013 for (
int i = 0; i < pts.rows(); ++i)
1015 double x = pts(i, 0),
y = pts(i, 1),
z = pts.cols() == 2 ? 0 : pts(i, 2);
1022 val.resize(pts.rows(), pts.cols());
1026 const bool planar = pts.cols() == 2;
1027 for (
int i = 0; i < pts.rows(); ++i)
1029 for (
int j = 0; j < pts.cols(); ++j)
1031 double x = pts(i, 0),
y = pts(i, 1),
z = pts.cols() == 2 ? 0 : pts(i, 2);
1050 throw "Invalid boundary id for pressure update";
1055 Eigen::MatrixXd curr_val =
pressures_[index].value.get_mat();
1056 assert(time_step <= curr_val.size());
1057 assert(curr_val.cols() == 1);
1058 curr_val(time_step) =
val;
1080 throw "Invalid boundary id for dirichlet update";
1083 for (
int i = 0; i <
val.size(); ++i)
1087 Eigen::MatrixXd curr_val =
displacements_[index].value[i].get_mat();
1088 assert(time_step <= curr_val.size());
1089 assert(curr_val.cols() == 1);
1090 curr_val(time_step) =
val(i);
1102 val = Eigen::MatrixXd::Zero(1, 1);
1110 val(0) = tmp.eval(pt, t);
1117 val(0) = it->second.eval(pt, t);
1123 for (
int i = 0; i < n_dirichlet.rows(); ++i)
1125 if (n_dirichlet(i, 0) == node_id)
1127 val(0) = n_dirichlet(i, 1);
1149 for (
int i = 0; i < n_dirichlet.rows(); ++i)
1151 if (n_dirichlet(i, 0) == n_id)
1178 for (
int n = 0; n < n_dirichlet.rows(); ++n)
1180 const int node_id = in_node_to_node[n_dirichlet(n, 0)];
1181 n_dirichlet(n, 0) = node_id;
1200 has_exact_ = !params[
"reference"][
"solution"].empty();
1201 exact_.
init(params[
"reference"][
"solution"]);
1206 auto ex = params[
"reference"][
"gradient"];
1210 for (
size_t k = 0; k < ex.size(); ++k)
1223 std::vector<json> j_boundary = flatten_ids(params[
"dirichlet_boundary"]);
1226 dirichlet_.resize(offset + j_boundary.size());
1230 if (j_boundary[i - offset].is_string())
1232 const std::string path =
resolve_path(j_boundary[i - offset], params[
"root_path"]);
1233 if (!std::filesystem::is_regular_file(path))
1236 Eigen::MatrixXd tmp;
1243 int current_id = -1;
1245 if (j_boundary[i - offset][
"id"] ==
"all")
1260 auto ff = j_boundary[i - offset][
"value"];
1264 if (j_boundary[i - offset][
"interpolation"].is_array())
1266 if (j_boundary[i - offset][
"interpolation"].size() == 0)
1267 dirichlet_[i].interpolation = std::make_shared<NoInterpolation>();
1268 else if (j_boundary[i - offset][
"interpolation"].size() == 1)
1284 auto j_boundary_tmp = params[
"neumann_boundary"];
1285 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
1288 neumann_.resize(offset + j_boundary.size());
1294 auto ff = j_boundary[i - offset][
"value"];
1297 if (j_boundary[i - offset][
"interpolation"].is_array())
1299 if (j_boundary[i - offset][
"interpolation"].size() == 0)
1300 neumann_[i].interpolation = std::make_shared<NoInterpolation>();
1301 else if (j_boundary[i - offset][
"interpolation"].size() == 1)
1313 auto rr = params[
"solution"];
1315 assert(rr.is_array());
1317 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_
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_
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