PolyFEM
Loading...
Searching...
No Matches
GenericProblem.cpp
Go to the documentation of this file.
1#include "GenericProblem.hpp"
2
6
7namespace polyfem
8{
9 using namespace utils;
10
11 namespace assembler
12 {
13 namespace
14 {
15 bool matches_fe_space(const int entry_fe_space_id, const int fe_space_id)
16 {
17 return entry_fe_space_id < 0 || fe_space_id < 0 || entry_fe_space_id == fe_space_id;
18 }
19
20 int fe_space_id(const json &entry)
21 {
22 return entry.value("fe_space", -1);
23 }
24
25 bool is_body_value_entry(const json &entry)
26 {
27 if (!entry.is_object() || !entry.contains("id") || !entry.contains("value"))
28 return false;
29
30 const json &id = entry["id"];
31 if (id.is_array())
32 {
33 for (const json &value : id)
34 if (value.is_number_integer() && value.get<int>() != -1)
35 return true;
36 return false;
37 }
38
39 return id.is_number_integer() && id.get<int>() != -1;
40 }
41
42 bool has_body_value_entries(const json &value)
43 {
44 return is_body_value_entry(value) || (value.is_array() && !value.empty() && is_body_value_entry(value.front()));
45 }
46
47 template <typename Map>
48 const typename Map::mapped_type *find_for_fe_space(const Map &values, const int fe_space_id)
49 {
50 if (const auto it = values.find(fe_space_id); it != values.end())
51 return &it->second;
52 if (const auto it = values.find(-1); it != values.end())
53 return &it->second;
54 if (fe_space_id < 0 && values.size() == 1)
55 return &values.begin()->second;
56 return nullptr;
57 }
58
59 std::vector<json> flatten_ids(const json &p_j_boundary_tmp)
60 {
61 const std::vector<json> j_boundary_tmp = utils::json_as_array(p_j_boundary_tmp);
62
63 std::vector<json> j_boundary;
64
65 for (size_t i = 0; i < j_boundary_tmp.size(); ++i)
66 {
67 const auto &tmp = j_boundary_tmp[i];
68
69 if (tmp.is_string())
70 {
71 j_boundary.push_back(tmp);
72 continue;
73 }
74 if (!tmp.contains("id"))
75 continue;
76
77 if (tmp["id"].is_array())
78 {
79 for (size_t j = 0; j < tmp["id"].size(); ++j)
80 {
81 json newj = tmp;
82 newj["id"] = tmp["id"][j].get<int>();
83 j_boundary.push_back(newj);
84 }
85 }
86 else
87 j_boundary.push_back(tmp);
88 }
89
90 return j_boundary;
91 }
92 } // namespace
93
94 double TensorBCValue::eval(const RowVectorNd &pts, const int dim, const double t, const int el_id) const
95 {
96 double x = pts(0);
97 double y = pts(1);
98 double z = pts.size() == 2 ? 0 : pts(2);
99
100 double val = value[dim](x, y, z, t, el_id);
101
102 if (interpolation.empty())
103 {
104 }
105 else if (interpolation.size() == 1)
106 val *= interpolation[0]->eval(t);
107 else
108 {
109 assert(dim < interpolation.size());
110 val *= interpolation[dim]->eval(t);
111 }
112
113 return val;
114 }
115
116 double ScalarBCValue::eval(const RowVectorNd &pts, const double t) const
117 {
118 assert(pts.size() == 2 || pts.size() == 3);
119 double x = pts(0), y = pts(1), z = pts.size() == 3 ? pts(2) : 0.0;
120 return value(x, y, z, t) * interpolation->eval(t);
121 }
122
124 : Problem(name), is_all_(false)
125 {
126 }
127
128 void GenericTensorProblem::set_units(const assembler::Assembler &assembler, const Units &units)
129 {
130 if (assembler.is_fluid())
131 {
132 for (int i = 0; i < 3; ++i)
133 {
134 for (auto &[fe_space_id, rhs] : rhs_)
135 rhs[i].set_unit_type(units.force());
136 exact_[i].set_unit_type(units.velocity());
137 }
138 for (int i = 0; i < 3; ++i)
139 exact_grad_[i].set_unit_type("");
140
141 for (auto &v : displacements_)
142 v.set_unit_type(units.velocity());
143
144 for (auto &v : forces_)
145 v.set_unit_type(units.force());
146
147 for (auto &v : normal_aligned_forces_)
148 v.set_unit_type(units.pressure());
149
150 for (auto &v : pressures_)
151 v.set_unit_type(units.pressure());
152
153 for (auto &v : cavity_pressures_)
154 v.second.set_unit_type(units.pressure());
155
156 for (auto &v : initial_position_)
157 for (int i = 0; i < 3; ++i)
158 v.value[i].set_unit_type(units.velocity());
159
160 for (auto &v : initial_velocity_)
161 for (int i = 0; i < 3; ++i)
162 v.value[i].set_unit_type(units.velocity());
163
164 for (auto &v : initial_acceleration_)
165 for (int i = 0; i < 3; ++i)
166 v.value[i].set_unit_type(units.acceleration());
167
168 for (auto &v : nodal_dirichlet_)
169 v.second.set_unit_type(units.velocity());
170
171 for (auto &v : nodal_neumann_)
172 v.second.set_unit_type(units.force());
173 }
174 else
175 {
176 for (int i = 0; i < 3; ++i)
177 {
178 for (auto &[fe_space_id, rhs] : rhs_)
179 rhs[i].set_unit_type(units.acceleration());
180 exact_[i].set_unit_type(units.length());
181 }
182 for (int i = 0; i < 3; ++i)
183 exact_grad_[i].set_unit_type("");
184
185 for (auto &v : displacements_)
186 v.set_unit_type(units.length());
187
188 for (auto &v : forces_)
189 v.set_unit_type(units.force());
190
191 for (auto &v : normal_aligned_forces_)
192 v.set_unit_type(units.pressure());
193
194 for (auto &v : pressures_)
195 v.set_unit_type(units.pressure());
196
197 for (auto &v : cavity_pressures_)
198 v.second.set_unit_type(units.pressure());
199
200 for (auto &v : initial_position_)
201 for (int i = 0; i < 3; ++i)
202 v.value[i].set_unit_type(units.length());
203
204 for (auto &v : initial_velocity_)
205 for (int i = 0; i < 3; ++i)
206 v.value[i].set_unit_type(units.velocity());
207
208 for (auto &v : initial_acceleration_)
209 for (int i = 0; i < 3; ++i)
210 v.value[i].set_unit_type(units.acceleration());
211
212 for (auto &v : nodal_dirichlet_)
213 v.second.set_unit_type(units.length());
214
215 for (auto &v : nodal_neumann_)
216 v.second.set_unit_type(units.force());
217 }
218 }
219
220 void GenericTensorProblem::rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
221 {
222 const auto *rhs = find_for_fe_space(rhs_, fe_space_id);
223 const auto *rhs_size = find_for_fe_space(rhs_size_, fe_space_id);
224 const int size = rhs_size == nullptr ? pts.cols() : *rhs_size;
225 val.resize(pts.rows(), size);
226
227 if (rhs == nullptr || is_rhs_zero(fe_space_id))
228 {
229 val.setZero();
230 return;
231 }
232
233 const bool planar = pts.cols() == 2;
234 for (int i = 0; i < pts.rows(); ++i)
235 {
236 for (int j = 0; j < size; ++j)
237 {
238 double x = pts(i, 0), y = pts(i, 1), z = planar ? 0 : pts(i, 2);
239 val(i, j) = (*rhs)[j](x, y, z, t);
240 }
241 }
242 }
243
244 void GenericTensorProblem::rhs(const assembler::Assembler &assembler, const mesh::Mesh &mesh, const int element_id, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
245 {
246 if (body_rhs_.empty())
247 {
248 rhs(assembler, pts, t, val, fe_space_id);
249 return;
250 }
251
252 int value_size = pts.cols();
253 for (const TensorInitialValue &entry : body_rhs_)
254 {
255 if (matches_fe_space(entry.fe_space_id, fe_space_id))
256 {
257 value_size = entry.size;
258 break;
259 }
260 }
261
262 val.resize(pts.rows(), value_size);
263 val.setZero();
264
265 const int body_id = mesh.get_body_id(element_id);
266 const TensorInitialValue *body_rhs = nullptr;
267 for (const TensorInitialValue &entry : body_rhs_)
268 {
269 if (entry.body_id == body_id && matches_fe_space(entry.fe_space_id, fe_space_id))
270 {
271 body_rhs = &entry;
272 break;
273 }
274 }
275
276 if (body_rhs == nullptr)
277 return;
278
279 const bool planar = pts.cols() == 2;
280 for (int i = 0; i < pts.rows(); ++i)
281 {
282 for (int j = 0; j < val.cols(); ++j)
283 {
284 const double x = pts(i, 0), y = pts(i, 1), z = planar ? 0 : pts(i, 2);
285 val(i, j) = body_rhs->value[j](x, y, z, t);
286 }
287 }
288 }
289
290 bool GenericTensorProblem::is_rhs_time_dependent(const int fe_space_id) const
291 {
292 if (body_rhs_.empty())
293 {
294 const auto *rhs = find_for_fe_space(rhs_, fe_space_id);
295 const auto *size = find_for_fe_space(rhs_size_, fe_space_id);
296 if (rhs && size)
297 for (int i = 0; i < *size; ++i)
298 if ((*rhs)[i].is_time_dependent())
299 return true;
300 }
301 for (const auto &entry : body_rhs_)
302 if (matches_fe_space(entry.fe_space_id, fe_space_id))
303 for (int i = 0; i < entry.size; ++i)
304 if (entry.value[i].is_time_dependent())
305 return true;
306 return false;
307 }
308
309 bool GenericTensorProblem::is_rhs_zero(const int fe_space_id) const
310 {
311 const auto *rhs = find_for_fe_space(rhs_, fe_space_id);
312 const auto *rhs_size = find_for_fe_space(rhs_size_, fe_space_id);
313 if (rhs != nullptr && rhs_size != nullptr)
314 {
315 for (int i = 0; i < *rhs_size; ++i)
316 if (!(*rhs)[i].is_zero())
317 return false;
318 }
319
320 for (const TensorInitialValue &entry : body_rhs_)
321 {
322 if (!matches_fe_space(entry.fe_space_id, fe_space_id))
323 continue;
324 for (int i = 0; i < entry.size; ++i)
325 if (!entry.value[i].is_zero())
326 return false;
327 }
328
329 return true;
330 }
331
332 bool GenericTensorProblem::has_boundary(const BoundaryKind kind, const int tag, const int fe_space_id)
333 {
334 if (kind != BoundaryKind::Dirichlet && kind != BoundaryKind::Neumann)
335 return Problem::has_boundary(kind, tag, fe_space_id);
336
337 if (tag <= 0)
338 return false;
339
340 if (kind == BoundaryKind::Dirichlet)
341 {
342 for (size_t i = 0; i < boundary_ids_.size(); ++i)
343 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(displacements_[i].fe_space_id, fe_space_id))
344 return true;
345 return false;
346 }
347
348 for (size_t i = 0; i < neumann_boundary_ids_.size(); ++i)
349 if (neumann_boundary_ids_[i] == tag && matches_fe_space(forces_[i].fe_space_id, fe_space_id))
350 return true;
351 for (size_t i = 0; i < normal_aligned_neumann_boundary_ids_.size(); ++i)
352 if (normal_aligned_neumann_boundary_ids_[i] == tag && matches_fe_space(normal_aligned_forces_[i].fe_space_id, fe_space_id))
353 return true;
354 return false;
355 }
356
357 bool GenericTensorProblem::is_dimension_dirichet(const int tag, const int dim, const int fe_space_id) const
358 {
359 if (all_dimensions_dirichlet(fe_space_id))
360 return true;
361
362 for (size_t b = 0; b < boundary_ids_.size(); ++b)
363 {
364 if ((boundary_ids_[b] < 0 || tag == boundary_ids_[b]) && matches_fe_space(displacements_[b].fe_space_id, fe_space_id))
365 {
366 auto &tmp = displacements_[b].dirichlet_dimension;
367 return tmp[dim];
368 }
369 }
370
371 assert(false);
372 return true;
373 }
374
375 bool GenericTensorProblem::all_dimensions_dirichlet(const int fe_space_id) const
376 {
377 for (const TensorBCValue &displacement : displacements_)
378 {
379 if (!matches_fe_space(displacement.fe_space_id, fe_space_id))
380 continue;
381
382 for (int d = 0; d < displacement.dirichlet_dimension.size(); ++d)
383 {
384 if (!displacement.dirichlet_dimension(d))
385 return false;
386 }
387 }
388
389 for (const auto &n_dirichlet : nodal_dirichlet_mat_)
390 {
391 for (int i = 0; i < n_dirichlet.rows(); ++i)
392 {
393 for (int d = 1; d < n_dirichlet.cols(); ++d)
394 {
395 if (std::isnan(n_dirichlet(i, d)))
396 return false;
397 }
398 }
399 }
400
401 return true;
402 }
403
404 void GenericTensorProblem::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 int fe_space_id) const
405 {
406 int value_size = mesh.dimension();
407 for (const TensorBCValue &displacement : displacements_)
408 {
409 if (displacement.size > 0 && matches_fe_space(displacement.fe_space_id, fe_space_id))
410 {
411 value_size = displacement.size;
412 break;
413 }
414 }
415 val = Eigen::MatrixXd::Zero(pts.rows(), value_size);
416
417 for (long i = 0; i < pts.rows(); ++i)
418 {
419 const int id = mesh.get_boundary_id(global_ids(i));
420 for (size_t b = 0; b < boundary_ids_.size(); ++b)
421 {
422 if ((boundary_ids_[b] < 0 || id == boundary_ids_[b]) && matches_fe_space(displacements_[b].fe_space_id, fe_space_id))
423 {
424 for (int d = 0; d < std::min<int>(val.cols(), displacements_[b].size); ++d)
425 {
426 val(i, d) = displacements_[b].eval(pts.row(i), d, t);
427 }
428 break;
429 }
430 }
431 }
432 }
433
434 void GenericTensorProblem::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 int fe_space_id) const
435 {
436 val = Eigen::MatrixXd::Zero(pts.rows(), mesh.dimension());
437
438 for (long i = 0; i < pts.rows(); ++i)
439 {
440 const int id = mesh.get_boundary_id(global_ids(i));
441
442 for (size_t b = 0; b < neumann_boundary_ids_.size(); ++b)
443 {
444 if (id == neumann_boundary_ids_[b] && matches_fe_space(forces_[b].fe_space_id, fe_space_id))
445 {
446 for (int d = 0; d < std::min<int>(val.cols(), forces_[b].size); ++d)
447 {
448 val(i, d) = forces_[b].eval(pts.row(i), d, t);
449 }
450
451 break;
452 }
453 }
454
455 for (size_t b = 0; b < normal_aligned_neumann_boundary_ids_.size(); ++b)
456 {
457 if (id == normal_aligned_neumann_boundary_ids_[b] && matches_fe_space(normal_aligned_forces_[b].fe_space_id, fe_space_id))
458 {
459 for (int d = 0; d < val.cols(); ++d)
460 {
461 val(i, d) = normal_aligned_forces_[b].eval(pts.row(i), t) * normals(i, d);
462 }
463 break;
464 }
465 }
466 }
467 }
468
469 void GenericTensorProblem::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
470 {
471 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
472
473 for (long i = 0; i < pts.rows(); ++i)
474 {
475 const int id = mesh.get_boundary_id(global_ids(i));
476
477 for (size_t b = 0; b < pressure_boundary_ids_.size(); ++b)
478 {
479 if (id == pressure_boundary_ids_[b])
480 {
481 val(i) = pressures_[b].eval(pts.row(i), t);
482 break;
483 }
484 }
485 }
486 }
487
488 double GenericTensorProblem::pressure_cavity_bc(const int boundary_id, const double t) const
489 {
490 Eigen::VectorXd pt;
491 pt.setZero(3);
492 return cavity_pressures_.at(boundary_id).eval(pt, t);
493 }
494
495 void GenericTensorProblem::exact(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
496 {
497 assert(has_exact_sol());
498 const bool planar = pts.cols() == 2;
499 val.resize(pts.rows(), pts.cols());
500
501 for (int i = 0; i < pts.rows(); ++i)
502 {
503 for (int j = 0; j < pts.cols(); ++j)
504 {
505 double x = pts(i, 0), y = pts(i, 1), z = planar ? 0 : pts(i, 2);
506 val(i, j) = exact_[j](x, y, z, t);
507 }
508 }
509 }
510
511 void GenericTensorProblem::exact_grad(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
512 {
513 const int size = pts.cols();
514 val.resize(pts.rows(), pts.cols() * size);
515 if (!has_exact_grad_)
516 return;
517
518 const bool planar = size == 2;
519 for (int i = 0; i < pts.rows(); ++i)
520 {
521 for (int j = 0; j < pts.cols() * size; ++j)
522 {
523 double x = pts(i, 0), y = pts(i, 1), z = planar ? 0 : pts(i, 2);
524 val(i, j) = exact_grad_[j](x, y, z, t);
525 }
526 }
527 }
528
529 void GenericTensorProblem::dirichlet_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
530 {
531 val = Eigen::MatrixXd::Zero(1, mesh.dimension());
532
533 for (size_t i = 0; i < boundary_ids_.size(); ++i)
534 {
535 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(displacements_[i].fe_space_id, fe_space_id))
536 {
537 val = Eigen::MatrixXd::Zero(1, displacements_[i].size);
538 for (int d = 0; d < val.cols(); ++d)
539 val(d) = displacements_[i].eval(pt, d, t);
540 return;
541 }
542 }
543
544 for (const auto &n_dirichlet : nodal_dirichlet_mat_)
545 {
546 for (int i = 0; i < n_dirichlet.rows(); ++i)
547 {
548 if (n_dirichlet(i, 0) == node_id)
549 {
550 for (int d = 0; d < val.cols(); ++d)
551 {
552 val(d) = n_dirichlet(i, d + 1);
553 }
554
555 return;
556 }
557 }
558 }
559
560 assert(false);
561 }
562
563 void GenericTensorProblem::neumann_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const Eigen::MatrixXd &normal, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
564 {
565 val = Eigen::MatrixXd::Zero(1, mesh.dimension());
566
567 if (const auto it = nodal_neumann_.find(tag); it != nodal_neumann_.end() && matches_fe_space(it->second.fe_space_id, fe_space_id))
568 {
569 val = Eigen::MatrixXd::Zero(1, it->second.size);
570 for (int d = 0; d < val.cols(); ++d)
571 val(d) = it->second.eval(pt, d, t);
572 return;
573 }
574
575 for (const auto &n_neumann : nodal_neumann_mat_)
576 {
577 for (int i = 0; i < n_neumann.rows(); ++i)
578 {
579 if (n_neumann(i, 0) == node_id)
580 {
581 val.resize(1, n_neumann.cols() - 1);
582 for (int d = 0; d < val.cols(); ++d)
583 val(d) = n_neumann(i, d + 1);
584 return;
585 }
586 }
587 }
588
589 assert(false);
590 }
591
592 bool GenericTensorProblem::is_nodal_dirichlet_boundary(const int n_id, const int tag, const int fe_space_id)
593 {
594 for (size_t i = 0; i < boundary_ids_.size(); ++i)
595 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(displacements_[i].fe_space_id, fe_space_id))
596 return true;
597
598 for (const auto &n_dirichlet : nodal_dirichlet_mat_)
599 {
600 for (int i = 0; i < n_dirichlet.rows(); ++i)
601 {
602 if (n_dirichlet(i, 0) == n_id)
603 return true;
604 }
605 }
606
607 return false;
608 }
609
610 bool GenericTensorProblem::is_nodal_neumann_boundary(const int n_id, const int tag, const int fe_space_id)
611 {
612 if (const auto it = nodal_neumann_.find(tag); it != nodal_neumann_.end())
613 return matches_fe_space(it->second.fe_space_id, fe_space_id);
614
615 for (const auto &n_neumann : nodal_neumann_mat_)
616 {
617 for (int i = 0; i < n_neumann.rows(); ++i)
618 {
619 if (n_neumann(i, 0) == n_id)
620 return true;
621 }
622 }
623
624 return false;
625 }
626
628 {
629 return !nodal_dirichlet_mat_.empty();
630 }
631
632 bool GenericTensorProblem::has_nodal_neumann(const int fe_space_id)
633 {
634 if (!nodal_neumann_mat_.empty())
635 return true;
636
637 for (const auto &[id, force] : nodal_neumann_)
638 {
639 (void)id;
640 if (matches_fe_space(force.fe_space_id, fe_space_id))
641 return true;
642 }
643 return false;
644 }
645
646 bool GenericTensorProblem::is_nodal_dimension_dirichlet(const int n_id, const int tag, const int dim, const int fe_space_id) const
647 {
648 for (size_t i = 0; i < boundary_ids_.size(); ++i)
649 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(displacements_[i].fe_space_id, fe_space_id))
650 return displacements_[i].dirichlet_dimension(dim);
651
652 for (const auto &n_dirichlet : nodal_dirichlet_mat_)
653 {
654 for (int i = 0; i < n_dirichlet.rows(); ++i)
655 {
656 if (n_dirichlet(i, 0) == n_id)
657 {
658 return !std::isnan(n_dirichlet(i, dim + 1));
659 }
660 }
661 }
662
663 assert(false);
664 return true;
665 }
666
667 void GenericTensorProblem::update_nodes(const Eigen::VectorXi &in_node_to_node)
668 {
670 {
671 if (!nodal_dirichlet_mat_.empty() || !nodal_neumann_mat_.empty())
672 logger().debug("Skipping updating in nodes to nodes in problem, already done once...");
673 return;
674 }
675 for (auto &n_dirichlet : nodal_dirichlet_mat_)
676 {
677 for (int n = 0; n < n_dirichlet.rows(); ++n)
678 {
679 const int node_id = in_node_to_node[n_dirichlet(n, 0)];
680 n_dirichlet(n, 0) = node_id;
681 }
682 }
683 for (auto &n_neumann : nodal_neumann_mat_)
684 {
685 for (int n = 0; n < n_neumann.rows(); ++n)
686 {
687 const int node_id = in_node_to_node[n_neumann(n, 0)];
688 n_neumann(n, 0) = node_id;
689 }
690 }
692 }
693
694 void GenericTensorProblem::set_parameters(const json &params, const io::ResourceIO &resources)
695 {
696 if (is_param_valid(params, "is_time_dependent"))
697 {
698 is_time_dept_ = params["is_time_dependent"];
699 }
700
701 if (is_param_valid(params, "rhs"))
702 {
703 const json &rr = params["rhs"];
704 const bool has_fe_spaces = rr.is_array() && !rr.empty() && rr.front().is_object() && rr.front().contains("fe_space") && rr.front().contains("value");
705 const bool has_body_ids = has_body_value_entries(rr);
706 if (has_body_ids)
707 {
708 const std::vector<json> entries = flatten_ids(rr);
709 body_rhs_.resize(entries.size());
710 for (size_t k = 0; k < entries.size(); ++k)
711 {
712 body_rhs_[k].body_id = entries[k]["id"];
713 body_rhs_[k].fe_space_id = fe_space_id(entries[k]);
714 const auto &value = entries[k]["value"];
715 body_rhs_[k].size = value.is_array() ? int(value.size()) : 1;
716 if (body_rhs_[k].size > 3)
717 log_and_throw_error("RHS for body {} and FE space {} has {} components; at most 3 are supported.", body_rhs_[k].body_id, body_rhs_[k].fe_space_id, body_rhs_[k].size);
718 for (int d = 0; d < body_rhs_[k].size; ++d)
719 body_rhs_[k].value[d].init(value.is_array() ? value[d] : value, resources);
720 }
721 }
722 else if (has_fe_spaces)
723 {
724 for (const json &entry : rr)
725 {
726 const int id = fe_space_id(entry);
727 const json &value = entry["value"];
728 const int size = value.is_array() ? int(value.size()) : 1;
729 if (size > 3)
730 log_and_throw_error("RHS for FE space {} has {} components; at most 3 are supported.", id, size);
731 rhs_size_[id] = size;
732 for (int k = 0; k < size; ++k)
733 rhs_[id][k].init(value.is_array() ? value[k] : value, resources);
734 }
735 }
736 else if (rr.is_array() && !rr.empty())
737 {
738 if (rr.size() > 3)
739 log_and_throw_error("RHS has {} components; at most 3 are supported.", rr.size());
740 rhs_size_[-1] = int(rr.size());
741 for (size_t k = 0; k < rr.size(); ++k)
742 rhs_[-1][k].init(rr[k], resources);
743 }
744 else if (!rr.is_array())
745 log_and_throw_error("Invalid tensor problem RHS: expected an array.");
746 }
747
748 if (is_param_valid(params, "reference") && is_param_valid(params["reference"], "solution"))
749 {
750 auto ex = params["reference"]["solution"];
751 has_exact_ = ex.size() > 0;
752 if (ex.is_array())
753 {
754 for (size_t k = 0; k < ex.size(); ++k)
755 exact_[k].init(ex[k], resources);
756 }
757 else
758 {
759 assert(false);
760 }
761 }
762
763 if (is_param_valid(params, "reference") && is_param_valid(params["reference"], "gradient"))
764 {
765 auto ex = params["reference"]["gradient"];
766 has_exact_grad_ = ex.size() > 0;
767 if (ex.is_array())
768 {
769 for (size_t k = 0; k < ex.size(); ++k)
770 exact_grad_[k].init(ex[k], resources);
771 }
772 else
773 {
774 assert(false);
775 }
776 }
777
778 if (is_param_valid(params, "dirichlet_boundary"))
779 {
780 // boundary_ids_.clear();
781 int offset = boundary_ids_.size();
782 std::vector<json> j_boundary = flatten_ids(params["dirichlet_boundary"]);
783
784 boundary_ids_.resize(offset + j_boundary.size());
785 displacements_.resize(offset + j_boundary.size());
786
787 for (size_t i = offset; i < boundary_ids_.size(); ++i)
788 {
789 if (j_boundary[i - offset].is_string())
790 {
791 const std::string path = j_boundary[i - offset].get<std::string>();
792 nodal_dirichlet_mat_.emplace_back(resources.read_matrix(path));
793
794 continue;
795 }
796
797 int current_id = -1;
798 displacements_[i].fe_space_id = fe_space_id(j_boundary[i - offset]);
799
800 if (j_boundary[i - offset]["id"] == "all")
801 {
802 boundary_ids_[i] = -1;
803 is_all_ = true;
804 nodal_dirichlet_[current_id] = TensorBCValue();
805 }
806 else
807 {
808 boundary_ids_[i] = j_boundary[i - offset]["id"];
809 current_id = boundary_ids_[i];
810 nodal_dirichlet_[current_id] = TensorBCValue();
811 }
812 nodal_dirichlet_[current_id].fe_space_id = displacements_[i].fe_space_id;
813
814 auto ff = j_boundary[i - offset]["value"];
815 if (ff.is_array())
816 {
817 if (ff.size() > 3)
818 log_and_throw_error("Dirichlet condition for FE space {} has {} components; at most 3 are supported.", displacements_[i].fe_space_id, ff.size());
819 displacements_[i].size = int(ff.size());
820 for (size_t k = 0; k < ff.size(); ++k)
821 {
822 displacements_[i].value[k].init(ff[k], resources);
823 if (j_boundary[i - offset].contains("time_reference") && j_boundary[i - offset]["time_reference"].size() > 0)
824 displacements_[i].value[k].set_t(j_boundary[i - offset]["time_reference"]);
825 nodal_dirichlet_[current_id].value[k].init(ff[k], resources);
826 }
827 }
828 else
829 {
830 displacements_[i].size = 1;
831 displacements_[i].value[0].init(ff, resources);
832 nodal_dirichlet_[current_id].value[0].init(ff, resources);
833 }
834 nodal_dirichlet_[current_id].size = displacements_[i].size;
835
836 displacements_[i].dirichlet_dimension.setConstant(true);
837 nodal_dirichlet_[current_id].dirichlet_dimension.setConstant(true);
838 if (j_boundary[i - offset].contains("dimension"))
839 {
840 auto &tmp = j_boundary[i - offset]["dimension"];
841 assert(tmp.is_array());
842 for (size_t k = 0; k < tmp.size(); ++k)
843 {
844 displacements_[i].dirichlet_dimension[k] = tmp[k];
845 nodal_dirichlet_[current_id].dirichlet_dimension[k] = tmp[k];
846 }
847 }
848
849 if (j_boundary[i - offset]["interpolation"].is_array())
850 {
851 for (int ii = 0; ii < j_boundary[i - offset]["interpolation"].size(); ++ii)
852 displacements_[i].interpolation.push_back(Interpolation::build(j_boundary[i - offset]["interpolation"][ii]));
853 }
854 else
855 displacements_[i].interpolation.push_back(Interpolation::build(j_boundary[i - offset]["interpolation"]));
856
857 nodal_dirichlet_[current_id].interpolation = displacements_[i].interpolation;
858 }
859 }
860
861 if (is_param_valid(params, "neumann_boundary"))
862 {
863 // neumann_boundary_ids_.clear();
864 const int offset = neumann_boundary_ids_.size();
865
866 auto j_boundary_tmp = params["neumann_boundary"];
867 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
868
869 neumann_boundary_ids_.resize(offset + j_boundary.size());
870 forces_.resize(offset + j_boundary.size());
871
872 for (size_t i = offset; i < neumann_boundary_ids_.size(); ++i)
873 {
874 neumann_boundary_ids_[i] = j_boundary[i - offset]["id"];
875 forces_[i].fe_space_id = fe_space_id(j_boundary[i - offset]);
876
877 auto ff = j_boundary[i - offset]["value"];
878 forces_[i].size = ff.is_array() ? int(ff.size()) : 1;
879 if (forces_[i].size > 3)
880 log_and_throw_error("Neumann condition for FE space {} has {} components; at most 3 are supported.", forces_[i].fe_space_id, forces_[i].size);
881 for (int k = 0; k < forces_[i].size; ++k)
882 forces_[i].value[k].init(ff.is_array() ? ff[k] : ff, resources);
883
884 if (j_boundary[i - offset]["interpolation"].is_array())
885 {
886 for (int ii = 0; ii < j_boundary[i - offset]["interpolation"].size(); ++ii)
887 forces_[i].interpolation.push_back(Interpolation::build(j_boundary[i - offset]["interpolation"][ii]));
888 }
889 else
890 {
891 forces_[i].interpolation.push_back(Interpolation::build(j_boundary[i - offset]["interpolation"]));
892 }
893 }
894 }
895
896 if (is_param_valid(params, "nodal_neumann_boundary"))
897 {
898 std::vector<json> j_boundary = flatten_ids(params["nodal_neumann_boundary"]);
899
900 for (size_t i = 0; i < j_boundary.size(); ++i)
901 {
902 if (j_boundary[i].is_string())
903 {
904 const std::string path = j_boundary[i].get<std::string>();
905 nodal_neumann_mat_.emplace_back(resources.read_matrix(path));
906
907 continue;
908 }
909
910 const int id = j_boundary[i]["id"];
911 TensorBCValue nodal_neumann;
912 nodal_neumann.fe_space_id = fe_space_id(j_boundary[i]);
913
914 auto ff = j_boundary[i]["value"];
915 nodal_neumann.size = ff.is_array() ? int(ff.size()) : 1;
916 if (nodal_neumann.size > 3)
917 log_and_throw_error("Nodal Neumann condition for FE space {} has {} components; at most 3 are supported.", nodal_neumann.fe_space_id, nodal_neumann.size);
918 for (int k = 0; k < nodal_neumann.size; ++k)
919 nodal_neumann.value[k].init(ff.is_array() ? ff[k] : ff, resources);
920
921 if (j_boundary[i]["interpolation"].is_array())
922 {
923 for (int ii = 0; ii < j_boundary[i]["interpolation"].size(); ++ii)
924 nodal_neumann.interpolation.push_back(Interpolation::build(j_boundary[i]["interpolation"][ii]));
925 }
926 else
927 {
928 nodal_neumann.interpolation.push_back(Interpolation::build(j_boundary[i]["interpolation"]));
929 }
930
931 nodal_neumann_[id] = nodal_neumann;
932 }
933 }
934
935 if (is_param_valid(params, "normal_aligned_neumann_boundary"))
936 {
937 const int offset = normal_aligned_neumann_boundary_ids_.size();
938
939 auto j_boundary_tmp = params["normal_aligned_neumann_boundary"];
940 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
941
942 normal_aligned_neumann_boundary_ids_.resize(offset + j_boundary.size());
943 normal_aligned_forces_.resize(offset + j_boundary.size());
944
945 for (size_t i = offset; i < normal_aligned_neumann_boundary_ids_.size(); ++i)
946 {
947 normal_aligned_neumann_boundary_ids_[i] = j_boundary[i - offset]["id"];
948 normal_aligned_forces_[i].fe_space_id = fe_space_id(j_boundary[i - offset]);
949
950 auto ff = j_boundary[i - offset]["value"];
951 normal_aligned_forces_[i].value.init(ff, resources);
952
953 if (j_boundary[i - offset].contains("interpolation"))
954 normal_aligned_forces_[i].interpolation = Interpolation::build(j_boundary[i - offset]["interpolation"]);
955 else
956 normal_aligned_forces_[i].interpolation = std::make_shared<NoInterpolation>();
957 }
958 }
959
960 if (is_param_valid(params, "pressure_boundary"))
961 {
962 // pressure_boundary_ids_.clear();
963 const int offset = pressure_boundary_ids_.size();
964
965 auto j_boundary_tmp = params["pressure_boundary"];
966 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
967
968 pressure_boundary_ids_.resize(offset + j_boundary.size());
969 pressures_.resize(offset + j_boundary.size());
970
971 for (size_t i = offset; i < pressure_boundary_ids_.size(); ++i)
972 {
973 pressure_boundary_ids_[i] = j_boundary[i - offset]["id"];
974
975 auto ff = j_boundary[i - offset]["value"];
976 pressures_[i].value.init(ff, resources);
977 if (j_boundary[i - offset].contains("time_reference") && j_boundary[i - offset]["time_reference"].size() > 0)
978 pressures_[i].value.set_t(j_boundary[i - offset]["time_reference"]);
979
980 pressures_[i].interpolation = std::make_shared<NoInterpolation>();
981 }
982 }
983
984 if (is_param_valid(params, "pressure_cavity"))
985 {
986 const int offset = pressure_cavity_ids_.size();
987
988 auto j_boundary_tmp = params["pressure_cavity"];
989 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
990
991 pressure_cavity_ids_.resize(offset + j_boundary.size());
992
993 for (size_t i = offset; i < pressure_cavity_ids_.size(); ++i)
994 {
995 int boundary_id = j_boundary[i - offset]["id"];
996 pressure_cavity_ids_[i] = boundary_id;
997
998 if (cavity_pressures_.find(boundary_id) == cavity_pressures_.end())
999 {
1000 cavity_pressures_[boundary_id] = ScalarBCValue();
1001
1002 auto ff = j_boundary[i - offset]["value"];
1003 cavity_pressures_[boundary_id].value.init(ff, resources);
1004
1005 cavity_pressures_[boundary_id].interpolation = std::make_shared<NoInterpolation>();
1006 }
1007 }
1008 }
1009
1010 if (is_param_valid(params, "solution"))
1011 {
1012 auto rr = params["solution"];
1013 initial_position_.resize(rr.size());
1014 assert(rr.is_array());
1015
1016 for (size_t k = 0; k < rr.size(); ++k)
1017 {
1018 initial_position_[k].body_id = rr[k]["id"];
1019 initial_position_[k].fe_space_id = fe_space_id(rr[k]);
1020 const auto v = rr[k]["value"];
1021 initial_position_[k].size = v.is_array() ? int(v.size()) : 1;
1022 if (initial_position_[k].size > 3)
1023 log_and_throw_error("Initial solution for FE space {} has {} components; at most 3 are supported.", initial_position_[k].fe_space_id, initial_position_[k].size);
1024 for (int d = 0; d < initial_position_[k].size; ++d)
1025 initial_position_[k].value[d].init(v.is_array() ? v[d] : v, resources);
1026 }
1027 }
1028
1029 if (is_param_valid(params, "velocity"))
1030 {
1031 auto rr = params["velocity"];
1032 initial_velocity_.resize(rr.size());
1033 assert(rr.is_array());
1034
1035 for (size_t k = 0; k < rr.size(); ++k)
1036 {
1037 initial_velocity_[k].body_id = rr[k]["id"];
1038 initial_velocity_[k].fe_space_id = fe_space_id(rr[k]);
1039 const auto v = rr[k]["value"];
1040 initial_velocity_[k].size = v.is_array() ? int(v.size()) : 1;
1041 if (initial_velocity_[k].size > 3)
1042 log_and_throw_error("Initial velocity for FE space {} has {} components; at most 3 are supported.", initial_velocity_[k].fe_space_id, initial_velocity_[k].size);
1043 for (int d = 0; d < initial_velocity_[k].size; ++d)
1044 initial_velocity_[k].value[d].init(v.is_array() ? v[d] : v, resources);
1045 }
1046 }
1047
1048 if (is_param_valid(params, "acceleration"))
1049 {
1050 auto rr = params["acceleration"];
1051 initial_acceleration_.resize(rr.size());
1052 assert(rr.is_array());
1053
1054 for (size_t k = 0; k < rr.size(); ++k)
1055 {
1056 initial_acceleration_[k].body_id = rr[k]["id"];
1057 initial_acceleration_[k].fe_space_id = fe_space_id(rr[k]);
1058 const auto v = rr[k]["value"];
1059 initial_acceleration_[k].size = v.is_array() ? int(v.size()) : 1;
1060 if (initial_acceleration_[k].size > 3)
1061 log_and_throw_error("Initial acceleration for FE space {} has {} components; at most 3 are supported.", initial_acceleration_[k].fe_space_id, initial_acceleration_[k].size);
1062 for (int d = 0; d < initial_acceleration_[k].size; ++d)
1063 initial_acceleration_[k].value[d].init(v.is_array() ? v[d] : v, resources);
1064 }
1065 }
1066 }
1067
1068 void GenericTensorProblem::initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id) const
1069 {
1070 int value_size = pts.cols();
1071 for (const TensorInitialValue &entry : initial_position_)
1072 if (matches_fe_space(entry.fe_space_id, fe_space_id))
1073 {
1074 value_size = entry.size;
1075 break;
1076 }
1077 val.resize(pts.rows(), value_size);
1078 if (initial_position_.empty())
1079 {
1080 val.setZero();
1081 return;
1082 }
1083
1084 const bool planar = pts.cols() == 2;
1085 for (int i = 0; i < pts.rows(); ++i)
1086 {
1087 const int id = mesh.get_body_id(global_ids(i));
1088 int index = -1;
1089 for (int j = 0; j < initial_position_.size(); ++j)
1090 {
1091 if (initial_position_[j].body_id == id && matches_fe_space(initial_position_[j].fe_space_id, fe_space_id))
1092 {
1093 index = j;
1094 break;
1095 }
1096 }
1097 if (index < 0)
1098 {
1099 val.row(i).setZero();
1100 continue;
1101 }
1102
1103 for (int j = 0; j < val.cols(); ++j)
1104 val(i, j) = planar ? initial_position_[index].value[j](pts(i, 0), pts(i, 1)) : initial_position_[index].value[j](pts(i, 0), pts(i, 1), pts(i, 2));
1105 }
1106 }
1107
1108 void GenericTensorProblem::initial_velocity(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id) const
1109 {
1110 int value_size = pts.cols();
1111 for (const TensorInitialValue &entry : initial_velocity_)
1112 if (matches_fe_space(entry.fe_space_id, fe_space_id))
1113 {
1114 value_size = entry.size;
1115 break;
1116 }
1117 val.resize(pts.rows(), value_size);
1118 if (initial_velocity_.empty())
1119 {
1120 val.setZero();
1121 return;
1122 }
1123
1124 const bool planar = pts.cols() == 2;
1125 for (int i = 0; i < pts.rows(); ++i)
1126 {
1127 const int id = mesh.get_body_id(global_ids(i));
1128 int index = -1;
1129 for (int j = 0; j < initial_velocity_.size(); ++j)
1130 {
1131 if (initial_velocity_[j].body_id == id && matches_fe_space(initial_velocity_[j].fe_space_id, fe_space_id))
1132 {
1133 index = j;
1134 break;
1135 }
1136 }
1137 if (index < 0)
1138 {
1139 val.row(i).setZero();
1140 continue;
1141 }
1142
1143 for (int j = 0; j < val.cols(); ++j)
1144 val(i, j) = planar ? initial_velocity_[index].value[j](pts(i, 0), pts(i, 1)) : initial_velocity_[index].value[j](pts(i, 0), pts(i, 1), pts(i, 2));
1145 }
1146 }
1147
1148 void GenericTensorProblem::initial_acceleration(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id) const
1149 {
1150 int value_size = pts.cols();
1151 for (const TensorInitialValue &entry : initial_acceleration_)
1152 if (matches_fe_space(entry.fe_space_id, fe_space_id))
1153 {
1154 value_size = entry.size;
1155 break;
1156 }
1157 val.resize(pts.rows(), value_size);
1158 if (initial_acceleration_.empty())
1159 {
1160 val.setZero();
1161 return;
1162 }
1163
1164 const bool planar = pts.cols() == 2;
1165 for (int i = 0; i < pts.rows(); ++i)
1166 {
1167 const int id = mesh.get_body_id(global_ids(i));
1168 int index = -1;
1169 for (int j = 0; j < initial_acceleration_.size(); ++j)
1170 {
1171 if (initial_acceleration_[j].body_id == id && matches_fe_space(initial_acceleration_[j].fe_space_id, fe_space_id))
1172 {
1173 index = j;
1174 break;
1175 }
1176 }
1177 if (index < 0)
1178 {
1179 val.row(i).setZero();
1180 continue;
1181 }
1182
1183 for (int j = 0; j < val.cols(); ++j)
1184 val(i, j) = planar ? initial_acceleration_[index].value[j](pts(i, 0), pts(i, 1)) : initial_acceleration_[index].value[j](pts(i, 0), pts(i, 1), pts(i, 2));
1185 }
1186 }
1187
1189 {
1190 has_exact_ = false;
1191 has_exact_grad_ = false;
1192 is_time_dept_ = false;
1193
1194 forces_.clear();
1195 displacements_.clear();
1196 normal_aligned_forces_.clear();
1197 pressures_.clear();
1198 cavity_pressures_.clear();
1199
1200 nodal_dirichlet_.clear();
1201 nodal_neumann_.clear();
1202 nodal_dirichlet_mat_.clear();
1203 nodal_neumann_mat_.clear();
1204
1205 initial_position_.clear();
1206 initial_velocity_.clear();
1207 initial_acceleration_.clear();
1208
1209 rhs_.clear();
1210 rhs_size_.clear();
1211 body_rhs_.clear();
1212 for (int i = 0; i < exact_.size(); ++i)
1213 exact_[i].clear();
1214 for (int i = 0; i < exact_grad_.size(); ++i)
1215 exact_grad_[i].clear();
1216 is_all_ = false;
1218 }
1219
1221 : Problem(name), is_all_(false)
1222 {
1223 }
1224
1226 {
1227 // TODO?
1228
1229 for (auto &v : neumann_)
1230 v.set_unit_type("");
1231
1232 for (auto &v : dirichlet_)
1233 v.set_unit_type("");
1234
1235 for (auto &v : nodal_dirichlet_)
1236 v.second.set_unit_type("");
1237
1238 for (auto &v : nodal_neumann_)
1239 v.second.set_unit_type("");
1240
1241 for (auto &v : initial_solution_)
1242 v.value.set_unit_type("");
1243
1244 for (auto &[fe_space_id, rhs] : rhs_)
1245 rhs.set_unit_type("");
1247
1248 for (int i = 0; i < 3; ++i)
1249 exact_grad_[i].set_unit_type("");
1250 }
1251
1252 void GenericScalarProblem::rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
1253 {
1254 val.resize(pts.rows(), 1);
1255 const auto *rhs = find_for_fe_space(rhs_, fe_space_id);
1256 if (rhs == nullptr || rhs->is_zero())
1257 {
1258 val.setZero();
1259 return;
1260 }
1261 const bool planar = pts.cols() == 2;
1262 for (int i = 0; i < pts.rows(); ++i)
1263 {
1264 double x = pts(i, 0), y = pts(i, 1), z = planar ? 0 : pts(i, 2);
1265 val(i) = (*rhs)(x, y, z, t);
1266 }
1267 }
1268
1269 void GenericScalarProblem::rhs(const assembler::Assembler &assembler, const mesh::Mesh &mesh, const int element_id, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
1270 {
1271 if (body_rhs_.empty())
1272 {
1273 rhs(assembler, pts, t, val, fe_space_id);
1274 return;
1275 }
1276
1277 val.resize(pts.rows(), 1);
1278 val.setZero();
1279
1280 const int body_id = mesh.get_body_id(element_id);
1281 const ScalarInitialValue *body_rhs = nullptr;
1282 for (const ScalarInitialValue &entry : body_rhs_)
1283 {
1284 if (entry.body_id == body_id && matches_fe_space(entry.fe_space_id, fe_space_id))
1285 {
1286 body_rhs = &entry;
1287 break;
1288 }
1289 }
1290
1291 if (body_rhs == nullptr)
1292 return;
1293
1294 const bool planar = pts.cols() == 2;
1295 for (int i = 0; i < pts.rows(); ++i)
1296 {
1297 const double x = pts(i, 0), y = pts(i, 1), z = planar ? 0 : pts(i, 2);
1298 val(i) = body_rhs->value(x, y, z, t);
1299 }
1300 }
1301
1302 bool GenericScalarProblem::is_rhs_time_dependent(const int fe_space_id) const
1303 {
1304 if (body_rhs_.empty())
1305 {
1306 const auto *rhs = find_for_fe_space(rhs_, fe_space_id);
1307 if (rhs && rhs->is_time_dependent())
1308 return true;
1309 }
1310 for (const auto &entry : body_rhs_)
1311 if (matches_fe_space(entry.fe_space_id, fe_space_id) && entry.value.is_time_dependent())
1312 return true;
1313 return false;
1314 }
1315
1316 bool GenericScalarProblem::is_rhs_zero(const int fe_space_id) const
1317 {
1318 const auto *rhs = find_for_fe_space(rhs_, fe_space_id);
1319 if (rhs != nullptr && !rhs->is_zero())
1320 return false;
1321
1322 for (const ScalarInitialValue &entry : body_rhs_)
1323 if (matches_fe_space(entry.fe_space_id, fe_space_id) && !entry.value.is_zero())
1324 return false;
1325
1326 return true;
1327 }
1328
1329 bool GenericScalarProblem::has_boundary(const BoundaryKind kind, const int tag, const int fe_space_id)
1330 {
1331 if (kind != BoundaryKind::Dirichlet && kind != BoundaryKind::Neumann)
1332 return Problem::has_boundary(kind, tag, fe_space_id);
1333
1334 if (tag <= 0)
1335 return false;
1336 if (kind == BoundaryKind::Dirichlet)
1337 {
1338 for (size_t i = 0; i < boundary_ids_.size(); ++i)
1339 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(dirichlet_[i].fe_space_id, fe_space_id))
1340 return true;
1341 return false;
1342 }
1343 for (size_t i = 0; i < neumann_boundary_ids_.size(); ++i)
1344 if (neumann_boundary_ids_[i] == tag && matches_fe_space(neumann_[i].fe_space_id, fe_space_id))
1345 return true;
1346 return false;
1347 }
1348
1349 void GenericScalarProblem::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 int fe_space_id) const
1350 {
1351 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
1352
1353 for (long i = 0; i < pts.rows(); ++i)
1354 {
1355 const int id = mesh.get_boundary_id(global_ids(i));
1356 for (size_t b = 0; b < boundary_ids_.size(); ++b)
1357 {
1358 if ((boundary_ids_[b] < 0 || id == boundary_ids_[b]) && matches_fe_space(dirichlet_[b].fe_space_id, fe_space_id))
1359 {
1360 val(i) = dirichlet_[b].eval(pts.row(i), t);
1361 break;
1362 }
1363 }
1364 }
1365 }
1366
1367 void GenericScalarProblem::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 int fe_space_id) const
1368 {
1369 val = Eigen::MatrixXd::Zero(pts.rows(), 1);
1370
1371 for (long i = 0; i < pts.rows(); ++i)
1372 {
1373 const int id = mesh.get_boundary_id(global_ids(i));
1374
1375 for (size_t b = 0; b < neumann_boundary_ids_.size(); ++b)
1376 {
1377 if (id == neumann_boundary_ids_[b] && matches_fe_space(neumann_[b].fe_space_id, fe_space_id))
1378 {
1379 double x = pts(i, 0), y = pts(i, 1), z = pts.cols() == 2 ? 0 : pts(i, 2);
1380 val(i) = neumann_[b].eval(pts.row(i), t);
1381 break;
1382 }
1383 }
1384 }
1385 }
1386
1387 void GenericScalarProblem::initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id) const
1388 {
1389 val.resize(pts.rows(), 1);
1390 if (initial_solution_.empty())
1391 {
1392 val.setZero();
1393 return;
1394 }
1395
1396 const bool planar = pts.cols() == 2;
1397 for (int i = 0; i < pts.rows(); ++i)
1398 {
1399 const int id = mesh.get_body_id(global_ids(i));
1400 int index = -1;
1401 for (int j = 0; j < initial_solution_.size(); ++j)
1402 {
1403 if (initial_solution_[j].body_id == id && matches_fe_space(initial_solution_[j].fe_space_id, fe_space_id))
1404 {
1405 index = j;
1406 break;
1407 }
1408 }
1409 if (index < 0)
1410 {
1411 val(i) = 0;
1412 continue;
1413 }
1414
1415 val(i) = planar ? initial_solution_[index].value(pts(i, 0), pts(i, 1)) : initial_solution_[index].value(pts(i, 0), pts(i, 1), pts(i, 2));
1416 }
1417 }
1418
1419 void GenericScalarProblem::exact(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
1420 {
1421 assert(has_exact_sol());
1422 const bool planar = pts.cols() == 2;
1423 val.resize(pts.rows(), 1);
1424
1425 for (int i = 0; i < pts.rows(); ++i)
1426 {
1427 double x = pts(i, 0), y = pts(i, 1), z = pts.cols() == 2 ? 0 : pts(i, 2);
1428 val(i) = exact_(x, y, z, t);
1429 }
1430 }
1431
1432 void GenericScalarProblem::exact_grad(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const
1433 {
1434 val.resize(pts.rows(), pts.cols());
1435 if (!has_exact_grad_)
1436 return;
1437
1438 const bool planar = pts.cols() == 2;
1439 for (int i = 0; i < pts.rows(); ++i)
1440 {
1441 for (int j = 0; j < pts.cols(); ++j)
1442 {
1443 double x = pts(i, 0), y = pts(i, 1), z = pts.cols() == 2 ? 0 : pts(i, 2);
1444 val(i, j) = exact_grad_[j](x, y, z, t);
1445 }
1446 }
1447 }
1448
1449 void GenericTensorProblem::update_pressure_boundary(const int id, const int time_step, const double val)
1450 {
1451 int index = -1;
1452 for (int i = 0; i < pressure_boundary_ids_.size(); ++i)
1453 {
1454 if (pressure_boundary_ids_[i] == id)
1455 {
1456 index = i;
1457 break;
1458 }
1459 }
1460 if (index == -1)
1461 {
1462 throw "Invalid boundary id for pressure update";
1463 }
1464
1465 if (pressures_[index].value.is_mat())
1466 {
1467 Eigen::MatrixXd curr_val = pressures_[index].value.get_mat();
1468 assert(time_step <= curr_val.size());
1469 assert(curr_val.cols() == 1);
1470 curr_val(time_step) = val;
1471 pressures_[index].value.set_mat(curr_val);
1472 }
1473 else
1474 {
1475 pressures_[index].value.init(val);
1476 }
1477 }
1478
1479 void GenericTensorProblem::update_dirichlet_boundary(const int id, const int time_step, const Eigen::VectorXd &val)
1480 {
1481 int index = -1;
1482 for (int i = 0; i < boundary_ids_.size(); ++i)
1483 {
1484 if (boundary_ids_[i] == id)
1485 {
1486 index = i;
1487 break;
1488 }
1489 }
1490 if (index == -1)
1491 {
1492 throw "Invalid boundary id for dirichlet update";
1493 }
1494
1495 for (int i = 0; i < val.size(); ++i)
1496 {
1497 if (displacements_[index].value[i].is_mat())
1498 {
1499 Eigen::MatrixXd curr_val = displacements_[index].value[i].get_mat();
1500 assert(time_step <= curr_val.size());
1501 assert(curr_val.cols() == 1);
1502 curr_val(time_step) = val(i);
1503 displacements_[index].value[i].set_mat(curr_val);
1504 }
1505 else
1506 {
1507 displacements_[index].value[i].init(val(i));
1508 }
1509 }
1510 }
1511
1512 void GenericTensorProblem::update_dirichlet_nodes(const Eigen::VectorXi &in_node_to_node, const Eigen::VectorXi &node_ids, const Eigen::MatrixXd &nodal_dirichlet)
1513 {
1514 assert(node_ids.size() == nodal_dirichlet.rows());
1515 // NOTE!!! Update nodes called in `State::build_basis()` so the first row of this mat
1516 // must be set to input node ordering if `build_basis` will be called, like in optimization
1517 for (auto &n_dirichlet : nodal_dirichlet_mat_)
1518 for (int i = 0; i < node_ids.size(); ++i)
1519 {
1520 int mapped_node_id = in_node_to_node(node_ids(i));
1521 for (int j = 0; j < n_dirichlet.rows(); ++j)
1522 if (mapped_node_id == n_dirichlet(j, 0))
1523 for (int k = 0; k < n_dirichlet.cols() - 1; ++k)
1524 n_dirichlet(j, k + 1) = nodal_dirichlet(i, k);
1525 }
1526 }
1527
1528 void GenericScalarProblem::dirichlet_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
1529 {
1530 val = Eigen::MatrixXd::Zero(1, 1);
1531
1532 for (size_t i = 0; i < boundary_ids_.size(); ++i)
1533 {
1534 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(dirichlet_[i].fe_space_id, fe_space_id))
1535 {
1536 val(0) = dirichlet_[i].eval(pt, t);
1537 return;
1538 }
1539 }
1540
1541 for (const auto &n_dirichlet : nodal_dirichlet_mat_)
1542 {
1543 for (int i = 0; i < n_dirichlet.rows(); ++i)
1544 {
1545 if (n_dirichlet(i, 0) == node_id)
1546 {
1547 val(0) = n_dirichlet(i, 1);
1548 return;
1549 }
1550 }
1551 }
1552
1553 assert(false);
1554 }
1555
1556 void GenericScalarProblem::neumann_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const Eigen::MatrixXd &normal, const double t, Eigen::MatrixXd &val, const int fe_space_id) const
1557 {
1558 val = Eigen::MatrixXd::Zero(1, 1);
1559
1560 if (const auto it = nodal_neumann_.find(tag); it != nodal_neumann_.end() && matches_fe_space(it->second.fe_space_id, fe_space_id))
1561 {
1562 val(0) = it->second.eval(pt, t);
1563 return;
1564 }
1565
1566 for (const auto &n_neumann : nodal_neumann_mat_)
1567 {
1568 for (int i = 0; i < n_neumann.rows(); ++i)
1569 {
1570 if (n_neumann(i, 0) == node_id)
1571 {
1572 val(0) = n_neumann(i, 1);
1573 return;
1574 }
1575 }
1576 }
1577
1578 assert(false);
1579 }
1580
1581 bool GenericScalarProblem::is_nodal_dirichlet_boundary(const int n_id, const int tag, const int fe_space_id)
1582 {
1583 for (size_t i = 0; i < boundary_ids_.size(); ++i)
1584 if ((boundary_ids_[i] < 0 || boundary_ids_[i] == tag) && matches_fe_space(dirichlet_[i].fe_space_id, fe_space_id))
1585 return true;
1586
1587 for (const auto &n_dirichlet : nodal_dirichlet_mat_)
1588 {
1589 for (int i = 0; i < n_dirichlet.rows(); ++i)
1590 {
1591 if (n_dirichlet(i, 0) == n_id)
1592 return true;
1593 }
1594 }
1595
1596 return false;
1597 }
1598
1599 bool GenericScalarProblem::is_nodal_neumann_boundary(const int n_id, const int tag, const int fe_space_id)
1600 {
1601 if (const auto it = nodal_neumann_.find(tag); it != nodal_neumann_.end())
1602 return matches_fe_space(it->second.fe_space_id, fe_space_id);
1603
1604 for (const auto &n_neumann : nodal_neumann_mat_)
1605 {
1606 for (int i = 0; i < n_neumann.rows(); ++i)
1607 {
1608 if (n_neumann(i, 0) == n_id)
1609 return true;
1610 }
1611 }
1612
1613 return false;
1614 }
1615
1617 {
1618 return nodal_dirichlet_mat_.size() > 0;
1619 }
1620
1622 {
1623 if (!nodal_neumann_mat_.empty())
1624 return true;
1625
1626 for (const auto &[id, neumann] : nodal_neumann_)
1627 {
1628 (void)id;
1629 if (matches_fe_space(neumann.fe_space_id, fe_space_id))
1630 return true;
1631 }
1632 return false;
1633 }
1634
1635 void GenericScalarProblem::update_nodes(const Eigen::VectorXi &in_node_to_node)
1636 {
1638 return;
1639 for (auto &n_dirichlet : nodal_dirichlet_mat_)
1640 {
1641 for (int n = 0; n < n_dirichlet.rows(); ++n)
1642 {
1643 const int node_id = in_node_to_node[n_dirichlet(n, 0)];
1644 n_dirichlet(n, 0) = node_id;
1645 }
1646 }
1647 for (auto &n_neumann : nodal_neumann_mat_)
1648 {
1649 for (int n = 0; n < n_neumann.rows(); ++n)
1650 {
1651 const int node_id = in_node_to_node[n_neumann(n, 0)];
1652 n_neumann(n, 0) = node_id;
1653 }
1654 }
1656 }
1657
1658 void GenericScalarProblem::set_parameters(const json &params, const io::ResourceIO &resources)
1659 {
1660 if (is_param_valid(params, "is_time_dependent"))
1661 {
1662 is_time_dept_ = params["is_time_dependent"];
1663 }
1664
1665 if (is_param_valid(params, "rhs"))
1666 {
1667 const json &rr = params["rhs"];
1668 const bool has_fe_spaces = rr.is_array() && !rr.empty() && rr.front().is_object() && rr.front().contains("fe_space") && rr.front().contains("value");
1669 const bool has_body_ids = has_body_value_entries(rr);
1670 if (has_body_ids)
1671 {
1672 const std::vector<json> entries = flatten_ids(rr);
1673 body_rhs_.resize(entries.size());
1674 for (size_t k = 0; k < entries.size(); ++k)
1675 {
1676 body_rhs_[k].body_id = entries[k]["id"];
1677 body_rhs_[k].fe_space_id = fe_space_id(entries[k]);
1678 body_rhs_[k].value.init(entries[k]["value"], resources);
1679 }
1680 }
1681 else if (has_fe_spaces)
1682 {
1683 for (const json &entry : rr)
1684 rhs_[fe_space_id(entry)].init(entry["value"], resources);
1685 }
1686 else if (!rr.is_array() || !rr.empty())
1687 rhs_[-1].init(rr, resources);
1688 }
1689
1690 if (is_param_valid(params, "reference") && is_param_valid(params["reference"], "solution"))
1691 {
1692 has_exact_ = !params["reference"]["solution"].empty();
1693 exact_.init(params["reference"]["solution"], resources);
1694 }
1695
1696 if (is_param_valid(params, "reference") && is_param_valid(params["reference"], "gradient"))
1697 {
1698 auto ex = params["reference"]["gradient"];
1699 has_exact_grad_ = ex.size() > 0;
1700 if (ex.is_array())
1701 {
1702 for (size_t k = 0; k < ex.size(); ++k)
1703 exact_grad_[k].init(ex[k], resources);
1704 }
1705 else
1706 {
1707 assert(false);
1708 }
1709 }
1710
1711 if (is_param_valid(params, "dirichlet_boundary"))
1712 {
1713 // boundary_ids_.clear();
1714 const int offset = boundary_ids_.size();
1715 std::vector<json> j_boundary = flatten_ids(params["dirichlet_boundary"]);
1716
1717 boundary_ids_.resize(offset + j_boundary.size());
1718 dirichlet_.resize(offset + j_boundary.size());
1719
1720 for (size_t i = offset; i < boundary_ids_.size(); ++i)
1721 {
1722 if (j_boundary[i - offset].is_string())
1723 {
1724 const std::string path = j_boundary[i - offset].get<std::string>();
1725 nodal_dirichlet_mat_.emplace_back(resources.read_matrix(path));
1726
1727 continue;
1728 }
1729
1730 int current_id = -1;
1731 dirichlet_[i].fe_space_id = fe_space_id(j_boundary[i - offset]);
1732
1733 if (j_boundary[i - offset]["id"] == "all")
1734 {
1735 boundary_ids_[i] = -1;
1736 is_all_ = true;
1737 nodal_dirichlet_[current_id] = ScalarBCValue();
1738 }
1739 else
1740 {
1741 boundary_ids_[i] = j_boundary[i - offset]["id"];
1742 current_id = boundary_ids_[i];
1743 nodal_dirichlet_[current_id] = ScalarBCValue();
1744 }
1745 nodal_dirichlet_[current_id].fe_space_id = dirichlet_[i].fe_space_id;
1746
1747 auto ff = j_boundary[i - offset]["value"];
1748 dirichlet_[i].value.init(ff, resources);
1749 nodal_dirichlet_[current_id].value.init(ff, resources);
1750
1751 if (j_boundary[i - offset]["interpolation"].is_array())
1752 {
1753 if (j_boundary[i - offset]["interpolation"].size() == 0)
1754 dirichlet_[i].interpolation = std::make_shared<NoInterpolation>();
1755 else if (j_boundary[i - offset]["interpolation"].size() == 1)
1756 dirichlet_[i].interpolation = Interpolation::build(j_boundary[i - offset]["interpolation"][0]);
1757 else
1758 log_and_throw_error("Only one Dirichlet interpolation supported");
1759 }
1760 else
1761 dirichlet_[i].interpolation = Interpolation::build(j_boundary[i - offset]["interpolation"]);
1762
1763 nodal_dirichlet_[current_id].interpolation = dirichlet_[i].interpolation;
1764 }
1765 }
1766
1767 if (is_param_valid(params, "neumann_boundary"))
1768 {
1769 // neumann_boundary_ids_.clear();
1770 const int offset = neumann_boundary_ids_.size();
1771 auto j_boundary_tmp = params["neumann_boundary"];
1772 std::vector<json> j_boundary = flatten_ids(j_boundary_tmp);
1773
1774 neumann_boundary_ids_.resize(offset + j_boundary.size());
1775 neumann_.resize(offset + j_boundary.size());
1776
1777 for (size_t i = offset; i < neumann_boundary_ids_.size(); ++i)
1778 {
1779 neumann_boundary_ids_[i] = j_boundary[i - offset]["id"];
1780 neumann_[i].fe_space_id = fe_space_id(j_boundary[i - offset]);
1781
1782 auto ff = j_boundary[i - offset]["value"];
1783 neumann_[i].value.init(ff, resources);
1784
1785 if (j_boundary[i - offset]["interpolation"].is_array())
1786 {
1787 if (j_boundary[i - offset]["interpolation"].size() == 0)
1788 neumann_[i].interpolation = std::make_shared<NoInterpolation>();
1789 else if (j_boundary[i - offset]["interpolation"].size() == 1)
1790 neumann_[i].interpolation = Interpolation::build(j_boundary[i - offset]["interpolation"][0]);
1791 else
1792 log_and_throw_error("Only one Neumann interpolation supported");
1793 }
1794 else
1795 neumann_[i].interpolation = Interpolation::build(j_boundary[i - offset]["interpolation"]);
1796 }
1797 }
1798
1799 if (is_param_valid(params, "nodal_neumann_boundary"))
1800 {
1801 std::vector<json> j_boundary = flatten_ids(params["nodal_neumann_boundary"]);
1802
1803 for (size_t i = 0; i < j_boundary.size(); ++i)
1804 {
1805 if (j_boundary[i].is_string())
1806 {
1807 const std::string path = j_boundary[i].get<std::string>();
1808 nodal_neumann_mat_.emplace_back(resources.read_matrix(path));
1809
1810 continue;
1811 }
1812
1813 const int id = j_boundary[i]["id"];
1814 ScalarBCValue nodal_neumann;
1815 nodal_neumann.fe_space_id = fe_space_id(j_boundary[i]);
1816 nodal_neumann.value.init(j_boundary[i]["value"], resources);
1817
1818 if (j_boundary[i]["interpolation"].is_array())
1819 {
1820 if (j_boundary[i]["interpolation"].size() == 0)
1821 nodal_neumann.interpolation = std::make_shared<NoInterpolation>();
1822 else if (j_boundary[i]["interpolation"].size() == 1)
1823 nodal_neumann.interpolation = Interpolation::build(j_boundary[i]["interpolation"][0]);
1824 else
1825 log_and_throw_error("Only one nodal Neumann interpolation supported");
1826 }
1827 else
1828 nodal_neumann.interpolation = Interpolation::build(j_boundary[i]["interpolation"]);
1829
1830 nodal_neumann_[id] = nodal_neumann;
1831 }
1832 }
1833
1834 if (is_param_valid(params, "solution"))
1835 {
1836 auto rr = params["solution"];
1837 initial_solution_.resize(rr.size());
1838 assert(rr.is_array());
1839
1840 for (size_t k = 0; k < rr.size(); ++k)
1841 {
1842 initial_solution_[k].body_id = rr[k]["id"];
1843 initial_solution_[k].fe_space_id = fe_space_id(rr[k]);
1844 initial_solution_[k].value.init(rr[k]["value"], resources);
1845 }
1846 }
1847 }
1848
1850 {
1851 neumann_.clear();
1852 dirichlet_.clear();
1853
1854 nodal_dirichlet_.clear();
1855 nodal_neumann_.clear();
1856 nodal_dirichlet_mat_.clear();
1857 nodal_neumann_mat_.clear();
1858
1859 rhs_.clear();
1860 body_rhs_.clear();
1861 exact_.clear();
1862 for (int i = 0; i < exact_grad_.size(); ++i)
1863 exact_grad_[i].clear();
1864 is_all_ = false;
1865 has_exact_ = false;
1866 has_exact_grad_ = false;
1867 is_time_dept_ = false;
1869 }
1870 } // namespace assembler
1871} // namespace polyfem
double val
std::vector< Eigen::Triplet< double > > entries
int y
int z
int x
std::string force() const
Definition Units.hpp:31
std::string pressure() const
Definition Units.hpp:32
std::string velocity() const
Definition Units.hpp:29
std::string acceleration() const
Definition Units.hpp:30
const std::string & length() const
Definition Units.hpp:19
virtual bool is_fluid() const
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 int fe_space_id=-1) const override
bool is_nodal_neumann_boundary(const int n_id, const int tag, const int fe_space_id=-1) override
std::vector< ScalarBCValue > neumann_
void initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::vector< Eigen::MatrixXd > nodal_neumann_mat_
void update_nodes(const Eigen::VectorXi &in_node_to_node) override
bool is_nodal_dirichlet_boundary(const int n_id, const int tag, const int fe_space_id=-1) override
std::vector< ScalarInitialValue > body_rhs_
void dirichlet_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const double t, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::array< utils::ExpressionValue, 3 > exact_grad_
bool has_nodal_dirichlet(const int fe_space_id=-1) override
void exact_grad(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
bool has_nodal_neumann(const int fe_space_id=-1) override
std::map< int, utils::ExpressionValue > rhs_
void set_parameters(const json &params, const io::ResourceIO &resources) override
void neumann_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const Eigen::MatrixXd &normal, const double t, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::map< int, ScalarBCValue > nodal_dirichlet_
bool is_rhs_time_dependent(const int fe_space_id=-1) const override
Whether volume loads may depend on time; unknown providers default to true.
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::vector< ScalarInitialValue > initial_solution_
bool is_rhs_zero(const int fe_space_id=-1) const override
void set_units(const assembler::Assembler &assembler, const Units &units) override
std::map< int, ScalarBCValue > nodal_neumann_
std::vector< ScalarBCValue > dirichlet_
bool has_boundary(const BoundaryKind kind, const int tag, const int fe_space_id) override
void exact(const Eigen::MatrixXd &pts, 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 int fe_space_id=-1) const override
std::vector< Eigen::MatrixXd > nodal_dirichlet_mat_
void update_pressure_boundary(const int id, const int time_step, const double val)
bool has_boundary(const BoundaryKind kind, const int tag, const int fe_space_id) override
std::map< int, TensorBCValue > nodal_neumann_
std::vector< TensorInitialValue > initial_acceleration_
void initial_acceleration(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
void initial_solution(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::unordered_map< int, ScalarBCValue > cavity_pressures_
std::vector< TensorInitialValue > initial_position_
void rhs(const assembler::Assembler &assembler, const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
void update_nodes(const Eigen::VectorXi &in_node_to_node) override
GenericTensorProblem(const std::string &name)
void dirichlet_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const double t, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::vector< TensorInitialValue > body_rhs_
std::vector< TensorInitialValue > initial_velocity_
bool all_dimensions_dirichlet(const int fe_space_id) const override
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_
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 int fe_space_id=-1) const override
std::vector< ScalarBCValue > normal_aligned_forces_
std::map< int, std::array< utils::ExpressionValue, 3 > > rhs_
bool is_nodal_dimension_dirichlet(const int n_id, const int tag, const int dim, const int fe_space_id=-1) 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 int fe_space_id=-1) const override
std::vector< Eigen::MatrixXd > nodal_neumann_mat_
bool is_nodal_neumann_boundary(const int n_id, const int tag, const int fe_space_id=-1) override
bool is_dimension_dirichet(const int tag, const int dim, const int fe_space_id=-1) const override
bool is_rhs_time_dependent(const int fe_space_id=-1) const override
Whether volume loads may depend on time; unknown providers default to true.
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
void exact(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
std::map< int, TensorBCValue > nodal_dirichlet_
bool has_nodal_neumann(const int fe_space_id=-1) override
std::array< utils::ExpressionValue, 9 > exact_grad_
void set_parameters(const json &params, const io::ResourceIO &resources) override
bool is_rhs_zero(const int fe_space_id=-1) const override
void neumann_nodal_value(const mesh::Mesh &mesh, const int node_id, const int tag, const RowVectorNd &pt, const Eigen::MatrixXd &normal, const double t, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
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
std::vector< ScalarBCValue > pressures_
bool is_nodal_dirichlet_boundary(const int n_id, const int tag, const int fe_space_id=-1) override
void exact_grad(const Eigen::MatrixXd &pts, const double t, Eigen::MatrixXd &val) const override
bool has_nodal_dirichlet(const int fe_space_id=-1) override
void set_units(const assembler::Assembler &assembler, const Units &units) override
std::array< utils::ExpressionValue, 3 > exact_
std::vector< Eigen::MatrixXd > nodal_dirichlet_mat_
void initial_velocity(const mesh::Mesh &mesh, const Eigen::MatrixXi &global_ids, const Eigen::MatrixXd &pts, Eigen::MatrixXd &val, const int fe_space_id=-1) const override
std::vector< int > normal_aligned_neumann_boundary_ids_
Definition Problem.hpp:132
virtual bool has_boundary(const BoundaryKind kind, const int tag, const int fe_space_id)
Definition Problem.cpp:16
std::vector< int > pressure_boundary_ids_
Definition Problem.hpp:133
std::vector< int > boundary_ids_
Definition Problem.hpp:130
virtual void init(const mesh::Mesh &mesh)
Definition Problem.hpp:31
std::vector< int > neumann_boundary_ids_
Definition Problem.hpp:131
std::vector< int > pressure_cavity_ids_
Definition Problem.hpp:134
Read-only interface to a hierarchical collection of simulation resources.
virtual Eigen::MatrixXd read_matrix(const std::string &path) const =0
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
Definition Mesh.hpp:77
virtual int get_body_id(const int primitive) const
Get the volume selection of an element (cell in 3d, face in 2d)
Definition Mesh.hpp:521
virtual int get_boundary_id(const int primitive) const
Get the boundary selection of an element (face in 3d, edge in 2d)
Definition Mesh.hpp:499
int dimension() const
utily for dimension
Definition Mesh.hpp:171
void set_unit_type(const std::string &unit_type)
void init(const json &vals, const io::ResourceIO &resources)
static std::shared_ptr< Interpolation > build(const json &params)
list tmp
Definition p_bases.py:366
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
Definition JSONUtils.hpp:49
bool is_param_valid(const json &params, const std::string &key)
Determine if a key exists and is non-null in a json object.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73
std::shared_ptr< utils::Interpolation > interpolation
double eval(const RowVectorNd &pts, const double t) const
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
std::array< utils::ExpressionValue, 3 > value