PolyFEM
Loading...
Searching...
No Matches
MatParams.cpp
Go to the documentation of this file.
1#include "MatParams.hpp"
2
5#include <polyfem/utils/StringUtils.hpp> // utils::resolve_path
6#include <iostream>
7#include <sstream>
8#include <algorithm>
9
10namespace polyfem::assembler
11{
12 namespace
13 {
14 double convert_to_lambda(const bool is_volume, const double E, const double nu)
15 {
16 if (is_volume)
17 return (E * nu) / ((1.0 + nu) * (1.0 - 2.0 * nu));
18
19 return (nu * E) / (1.0 - nu * nu);
20 }
21
22 // Reads a named VECTORS array under CELL_DATA from a legacy ASCII VTK
23 // file. Returns one Eigen::Vector3d per cell, in file order.
24 std::vector<Eigen::Vector3d> read_cell_vectors_legacy_vtk(
25 std::istream &in,
26 const std::string &description,
27 const std::string &field_name)
28 {
29 std::vector<Eigen::Vector3d> out;
30 std::string line;
31 int n_cell_data = -1;
32 bool found = false;
33
34 while (std::getline(in, line))
35 {
36 std::istringstream ss(line);
37 std::string tok;
38 ss >> tok;
39 if (tok == "CELL_DATA")
40 {
41 ss >> n_cell_data;
42 }
43 else if (tok == "VECTORS")
44 {
45 std::string name;
46 ss >> name;
47 if (name != field_name)
48 continue;
49 if (n_cell_data < 0)
50 log_and_throw_error("VECTORS encountered before CELL_DATA in fiber VTK");
51 out.resize(n_cell_data);
52 for (int i = 0; i < n_cell_data; ++i)
53 {
54 if (!(in >> out[i].x() >> out[i].y() >> out[i].z()))
55 log_and_throw_error(fmt::format(
56 "Fiber VTK '{}' ended early: expected {} vectors", description, n_cell_data));
57 }
58 found = true;
59 break;
60 }
61 }
62 if (!found)
63 log_and_throw_error(fmt::format(
64 "VECTORS '{}' not found under CELL_DATA in {}", field_name, description));
65 return out;
66 }
67
68 double convert_to_mu(const double E, const double nu)
69 {
70 return E / (2.0 * (1.0 + nu));
71 }
72 } // namespace
73
74 GenericMatParam::GenericMatParam(const std::string &param_name)
75 : param_name_(param_name)
76 {
77 }
78
80 {
81 return !param_.empty() && nonconstant_count_ == 0;
82 }
83
85 {
86 return param_.empty() || time_dependent_count_ != 0;
87 }
88
89 void GenericMatParam::add_multimaterial(const int index, const json &params, const std::string &unit_type, const io::ResourceIO &resources)
90 {
91 for (int i = param_.size(); i <= index; ++i)
92 {
93 param_.emplace_back();
94 param_.back().set_unit_type(unit_type);
95 }
96
97 if (params.count(param_name_))
98 {
99 const bool was_constant = param_[index].is_constant();
100 const bool was_time_dependent = param_[index].is_time_dependent();
101 param_[index].init(params[param_name_], resources);
102 nonconstant_count_ -= !was_constant;
103 nonconstant_count_ += !param_[index].is_constant();
104 time_dependent_count_ -= was_time_dependent;
105 time_dependent_count_ += param_[index].is_time_dependent();
106 if (params.contains(MATERIAL_ELEMENT_INDEX))
107 param_[index].set_index(params[MATERIAL_ELEMENT_INDEX]);
108 }
109 }
110
111 double GenericMatParam::operator()(const RowVectorNd &p, double t, int index) const
112 {
113 const double x = p(0);
114 const double y = p(1);
115 const double z = p.size() == 3 ? p(2) : 0;
116
117 return (*this)(x, y, z, t, index);
118 }
119
120 double GenericMatParam::operator()(double x, double y, double z, double t, int index) const
121 {
122 assert(param_.size() == 1 || index < param_.size());
123
124 const auto &tmp_param = param_.size() == 1 ? param_[0] : param_[index];
125
126 return tmp_param(x, y, z, t, index);
127 }
128
129 GenericMatParams::GenericMatParams(const std::string &param_name)
130 : param_name_(param_name)
131 {
132 }
133
134 void GenericMatParams::add_multimaterial(const int index, const json &params, const std::string &unit_type, const io::ResourceIO &resources)
135 {
136 if (!params.contains(param_name_))
137 return;
138
139 std::vector<json> params_array = utils::json_as_array(params[param_name_]);
140 assert(params_array.size() == params_.size() || params_.empty());
141
142 if (params_.empty())
143 for (int i = 0; i < params_array.size(); ++i)
144 params_.emplace_back(param_name_ + "_" + std::to_string(i));
145
146 for (int i = 0; i < params_.size(); ++i)
147 {
148 for (int j = params_.at(i).param_.size(); j <= index; ++j)
149 {
150 params_.at(i).param_.emplace_back();
151 params_.at(i).param_.back().set_unit_type(unit_type);
152 }
153
154 const bool was_constant = params_.at(i).param_[index].is_constant();
155 const bool was_time_dependent = params_.at(i).param_[index].is_time_dependent();
156 params_.at(i).param_[index].init(params_array[i], resources);
157 params_.at(i).nonconstant_count_ -= !was_constant;
158 params_.at(i).nonconstant_count_ += !params_.at(i).param_[index].is_constant();
159 params_.at(i).time_dependent_count_ -= was_time_dependent;
160 params_.at(i).time_dependent_count_ += params_.at(i).param_[index].is_time_dependent();
161 if (params.contains(MATERIAL_ELEMENT_INDEX))
162 params_.at(i).param_[index].set_index(params[MATERIAL_ELEMENT_INDEX]);
163 }
164 }
165
166 void ElasticityTensor::resize(const int size)
167 {
168 if (size == 2)
169 stiffness_tensor_.resize(3, 3);
170 else
171 stiffness_tensor_.resize(6, 6);
172
173 stiffness_tensor_.setZero();
174
175 size_ = size;
176 }
177
178 double ElasticityTensor::operator()(int i, int j) const
179 {
180 if (j < i)
181 {
182 std::swap(i, j);
183 }
184
185 assert(j >= i);
186 return stiffness_tensor_(i, j);
187 }
188
189 double &ElasticityTensor::operator()(int i, int j)
190 {
191 if (j < i)
192 {
193 std::swap(i, j);
194 }
195
196 assert(j >= i);
197 return stiffness_tensor_(i, j);
198 }
199
200 void ElasticityTensor::set_from_entries(const std::vector<double> &entries, const std::string &stress_units, const io::ResourceIO &resources)
201 {
202 if (size_ == 2)
203 {
204 if (entries.size() == 4)
205 {
207 entries[0],
208 entries[1],
209 entries[2],
210 entries[3], stress_units, resources);
211
212 return;
213 }
214
215 assert(entries.size() >= 6);
216
217 (*this)(0, 0) = entries[0];
218 (*this)(0, 1) = entries[1];
219 (*this)(0, 2) = entries[2];
220
221 (*this)(1, 1) = entries[3];
222 (*this)(1, 2) = entries[4];
223
224 (*this)(2, 2) = entries[5];
225 }
226 else
227 {
228 if (entries.size() == 5)
229 {
231 entries[0],
232 entries[1],
233 entries[2],
234 entries[3],
235 entries[4],
236 stress_units, resources);
237
238 return;
239 }
240 else if (entries.size() == 9)
241 {
243 entries[0],
244 entries[1],
245 entries[2],
246 entries[3],
247 entries[4],
248 entries[5],
249 entries[6],
250 entries[7],
251 entries[8], stress_units, resources);
252
253 return;
254 }
255 assert(entries.size() >= 21);
256
257 (*this)(0, 0) = entries[0];
258 (*this)(0, 1) = entries[1];
259 (*this)(0, 2) = entries[2];
260 (*this)(0, 3) = entries[3];
261 (*this)(0, 4) = entries[4];
262 (*this)(0, 5) = entries[5];
263
264 (*this)(1, 1) = entries[6];
265 (*this)(1, 2) = entries[7];
266 (*this)(1, 3) = entries[8];
267 (*this)(1, 4) = entries[9];
268 (*this)(1, 5) = entries[10];
269
270 (*this)(2, 2) = entries[11];
271 (*this)(2, 3) = entries[12];
272 (*this)(2, 4) = entries[13];
273 (*this)(2, 5) = entries[14];
274
275 (*this)(3, 3) = entries[15];
276 (*this)(3, 4) = entries[16];
277 (*this)(3, 5) = entries[17];
278
279 (*this)(4, 4) = entries[18];
280 (*this)(4, 5) = entries[19];
281
282 (*this)(5, 5) = entries[20];
283 }
284 }
285
286 void ElasticityTensor::set_from_lambda_mu(const double lambda, const double mu, const std::string &stress_units, const io::ResourceIO &resources)
287 {
288 if (size_ == 2)
289 {
290 (*this)(0, 0) = 2 * mu + lambda;
291 (*this)(0, 1) = lambda;
292 (*this)(0, 2) = 0;
293
294 (*this)(1, 1) = 2 * mu + lambda;
295 (*this)(1, 2) = 0;
296
297 (*this)(2, 2) = mu;
298 }
299 else
300 {
301 (*this)(0, 0) = 2 * mu + lambda;
302 (*this)(0, 1) = lambda;
303 (*this)(0, 2) = lambda;
304 (*this)(0, 3) = 0;
305 (*this)(0, 4) = 0;
306 (*this)(0, 5) = 0;
307
308 (*this)(1, 1) = 2 * mu + lambda;
309 (*this)(1, 2) = lambda;
310 (*this)(1, 3) = 0;
311 (*this)(1, 4) = 0;
312 (*this)(1, 5) = 0;
313
314 (*this)(2, 2) = 2 * mu + lambda;
315 (*this)(2, 3) = 0;
316 (*this)(2, 4) = 0;
317 (*this)(2, 5) = 0;
318
319 (*this)(3, 3) = mu;
320 (*this)(3, 4) = 0;
321 (*this)(3, 5) = 0;
322
323 (*this)(4, 4) = mu;
324 (*this)(4, 5) = 0;
325
326 (*this)(5, 5) = mu;
327 }
328 }
329
330 void ElasticityTensor::set_from_young_poisson(const double young, const double nu, const std::string &stress_units, const io::ResourceIO &resources)
331 {
332 if (size_ == 2)
333 {
334 stiffness_tensor_ << 1.0, nu, 0.0,
335 nu, 1.0, 0.0,
336 0.0, 0.0, (1.0 - nu) / 2.0;
337 stiffness_tensor_ *= young / (1.0 - nu * nu);
338 }
339 else
340 {
341 assert(size_ == 3);
342 const double v = nu;
343 stiffness_tensor_ << 1. - v, v, v, 0, 0, 0,
344 v, 1. - v, v, 0, 0, 0,
345 v, v, 1. - v, 0, 0, 0,
346 0, 0, 0, (1. - 2. * v) / 2., 0, 0,
347 0, 0, 0, 0, (1. - 2. * v) / 2., 0,
348 0, 0, 0, 0, 0, (1. - 2. * v) / 2.;
349 stiffness_tensor_ *= young / ((1. + v) * (1. - 2. * v));
350 }
351 }
352
354 double Ex, double Ey, double Ez,
355 double nuXY, double nuXZ, double nuYZ,
356 double muYZ, double muZX, double muXY, const std::string &stress_units, const io::ResourceIO &resources)
357 {
358 assert(size_ == 3);
359
360 // from https://www.efunda.com/formulae/solid_mechanics/mat_mechanics/hooke_orthotropic.cfm
361 double nuYX = nuXY * Ey / Ex;
362 double nuZX = nuXZ * Ez / Ex;
363 double nuZY = nuYZ * Ez / Ey;
364
365 Eigen::MatrixXd compliance;
366 compliance.setZero(6, 6);
367 compliance << 1 / Ex, -nuYX / Ey, -nuZX / Ez, 0, 0, 0,
368 -nuXY / Ex, 1 / Ey, -nuZY / Ez, 0, 0, 0,
369 -nuXZ / Ex, -nuYZ / Ey, 1 / Ez, 0, 0, 0,
370 0, 0, 0, 1 / (2 * muYZ), 0, 0,
371 0, 0, 0, 0, 1 / (2 * muZX), 0,
372 0, 0, 0, 0, 0, 1 / (2 * muXY);
373 stiffness_tensor_ = compliance.inverse();
374 }
375
376 void ElasticityTensor::set_orthotropic(double Ex, double Ey, double nuXY, double muXY, const std::string &stress_units, const io::ResourceIO &resources)
377 {
378 assert(size_ == 2);
379
380 // from https://www.efunda.com/formulae/solid_mechanics/mat_mechanics/hooke_orthotropic.cfm
381 double nuYX = nuXY * Ey / Ex;
382
383 Eigen::MatrixXd compliance;
384 compliance.setZero(3, 3);
385 compliance << 1.0 / Ex, -nuYX / Ey, 0.0,
386 -nuXY / Ex, 1.0 / Ey, 0.0,
387 0.0, 0.0, 1.0 / (2 * muXY);
388 stiffness_tensor_ = compliance.inverse();
389 }
390
392 double Et, double Ea,
393 double nu_t, double nu_a,
394 double Ga, const std::string &stress_units, const io::ResourceIO &resources)
395 {
396 assert(size_ == 3);
397
398 // from https://osupdocs.forestry.oregonstate.edu/index.php/Transversely_Isotropic_Material
399 Eigen::MatrixXd compliance;
400 compliance.setZero(6, 6);
401 compliance << 1 / Et, -nu_t / Et, -nu_a / Ea, 0, 0, 0,
402 -nu_t / Et, 1 / Et, -nu_a / Ea, 0, 0, 0,
403 -nu_a / Ea, -nu_a / Ea, 1 / Ea, 0, 0, 0,
404 0, 0, 0, 1 / Ga, 0, 0,
405 0, 0, 0, 0, 1 / Ga, 0,
406 0, 0, 0, 0, 0, (2 * (1 + nu_t)) / Et;
407 stiffness_tensor_ = compliance.inverse();
408 }
409
410 template <int DIM>
411 double ElasticityTensor::compute_stress(const std::array<double, DIM> &strain, const int j) const
412 {
413 double res = 0;
414
415 for (int k = 0; k < DIM; ++k)
416 res += (*this)(j, k) * strain[k];
417
418 return res;
419 }
420
421 void ElasticityTensor::rotate_stiffness(const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6> &rotation_mtx_voigt)
422 {
424 stiffness_tensor_ = rotation_mtx_voigt * stiffness_tensor_ * rotation_mtx_voigt.transpose();
425 }
426
431
433 {
434 lambda_or_E_.emplace_back();
435 lambda_or_E_.back().init(1.0);
436
437 mu_or_nu_.emplace_back();
438 mu_or_nu_.back().init(1.0);
439 size_ = -1;
440 is_lambda_mu_ = true;
441 }
442
443 void LameParameters::lambda_mu(double px, double py, double pz, double x, double y, double z, double t, int el_id, double &lambda, double &mu) const
444 {
445 assert(lambda_or_E_.size() == 1 || el_id < lambda_or_E_.size());
446 assert(mu_or_nu_.size() == 1 || el_id < mu_or_nu_.size());
447 assert(size_ == 2 || size_ == 3);
448
449 const auto &tmp1 = lambda_or_E_.size() == 1 ? lambda_or_E_[0] : lambda_or_E_[el_id];
450 const auto &tmp2 = mu_or_nu_.size() == 1 ? mu_or_nu_[0] : mu_or_nu_[el_id];
451
452 double llambda = tmp1(x, y, z, t, el_id);
453 double mmu = tmp2(x, y, z, t, el_id);
454
455 if (!is_lambda_mu_)
456 {
457 lambda = convert_to_lambda(size_ == 3, llambda, mmu);
458 mu = convert_to_mu(llambda, mmu);
459 }
460 else
461 {
462 lambda = llambda;
463 mu = mmu;
464 }
465
466 if (lambda_mat_.size() > el_id && mu_mat_.size() > el_id)
467 {
468 lambda = lambda_mat_(el_id);
469 mu = mu_mat_(el_id);
470 }
471
472 assert(!std::isnan(lambda));
473 assert(!std::isnan(mu));
474 assert(!std::isinf(lambda));
475 assert(!std::isinf(mu));
476 }
477
478 void LameParameters::add_multimaterial(const int index, const json &params, const bool is_volume, const std::string &stress_unit, const io::ResourceIO &resources)
479 {
480 const int size = is_volume ? 3 : 2;
481 assert(size_ == -1 || size == size_);
482 size_ = size;
483
484 for (int i = lambda_or_E_.size(); i <= index; ++i)
485 {
486 lambda_or_E_.emplace_back();
487 lambda_or_E_.back().set_unit_type(stress_unit);
488 mu_or_nu_.emplace_back();
489 mu_or_nu_.back().set_unit_type("");
490 }
491
492 if (params.count("young"))
493 {
494 set_e_nu(index, params["young"], params["nu"], stress_unit, resources);
495 }
496 else if (params.count("E"))
497 {
498 set_e_nu(index, params["E"], params["nu"], stress_unit, resources);
499 }
500 else if (params.count("lambda"))
501 {
502 lambda_or_E_[index].init(params["lambda"], resources);
503 mu_or_nu_[index].init(params["mu"], resources);
504
505 lambda_or_E_[index].set_unit_type(stress_unit);
506 mu_or_nu_[index].set_unit_type(stress_unit);
507 is_lambda_mu_ = true;
508 }
509
510 if (params.contains(MATERIAL_ELEMENT_INDEX))
511 {
512 lambda_or_E_[index].set_index(params[MATERIAL_ELEMENT_INDEX]);
513 mu_or_nu_[index].set_index(params[MATERIAL_ELEMENT_INDEX]);
514 }
515 }
516
517 void LameParameters::set_e_nu(const int index, const json &E, const json &nu, const std::string &stress_unit, const io::ResourceIO &resources)
518 {
519 // TODO: conversion is always called
520 is_lambda_mu_ = false;
521 lambda_or_E_[index].init(E, resources);
522 mu_or_nu_[index].init(nu, resources);
523
524 lambda_or_E_[index].set_unit_type(stress_unit);
525 // nu has no unit
526 mu_or_nu_[index].set_unit_type("");
527 }
528
530 {
531 rho_.emplace_back();
532 rho_.back().init(1.0);
533 }
534
536 {
537 return nonconstant_count_ == 0;
538 }
539
541 {
542 return time_dependent_count_ != 0;
543 }
544
545 double Density::operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
546 {
547 assert(rho_.size() == 1 || el_id < rho_.size());
548
549 const auto &tmp = rho_.size() == 1 ? rho_[0] : rho_[el_id];
550 const double res = tmp(x, y, z, t, el_id);
551 assert(!std::isnan(res));
552 assert(!std::isinf(res));
553 return res;
554 }
555
556 void Density::add_multimaterial(const int index, const json &params, const std::string &density_unit, const io::ResourceIO &resources)
557 {
558 for (int i = rho_.size(); i <= index; ++i)
559 {
560 rho_.emplace_back();
561 }
562
563 const bool was_constant = rho_[index].is_constant();
564 const bool was_time_dependent = rho_[index].is_time_dependent();
565 if (params.count("rho"))
566 {
567 rho_[index].init(params["rho"], resources);
568 }
569 else if (params.count("density"))
570 {
571 rho_[index].init(params["density"], resources);
572 }
573 if (params.contains(MATERIAL_ELEMENT_INDEX))
574 rho_[index].set_index(params[MATERIAL_ELEMENT_INDEX]);
575
576 rho_[index].set_unit_type(density_unit);
577 nonconstant_count_ -= !was_constant;
578 nonconstant_count_ += !rho_[index].is_constant();
579 time_dependent_count_ -= was_time_dependent;
580 time_dependent_count_ += rho_[index].is_time_dependent();
581 }
582
584 : rho_("rho"), heat_capacity_("heat_capacity")
585 {
586 }
587
589 {
591 }
592
597
598 void ThermalMassDensity::add_multimaterial(const int index, const json &params, const std::string &density_unit, const io::ResourceIO &resources)
599 {
600 add_multimaterial(index, params, density_unit, "", resources);
601 }
602
603 void ThermalMassDensity::add_multimaterial(const int index, const json &params, const std::string &density_unit, const std::string &heat_capacity_unit, const io::ResourceIO &resources)
604 {
605 rho_.add_multimaterial(index, params, density_unit, resources);
606 heat_capacity_.add_multimaterial(index, params, heat_capacity_unit, resources);
607 }
608
609 double ThermalMassDensity::operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
610 {
611 const double rho = rho_(x, y, z, t, el_id);
612 const double heat_capacity = heat_capacity_(x, y, z, t, el_id);
613 const double res = rho * heat_capacity;
614 assert(!std::isnan(res));
615 assert(!std::isinf(res));
616 return res;
617 }
618
619 double ThermalMassDensity::rho(const RowVectorNd &p, double t, int el_id) const
620 {
621 return rho_(p, t, el_id);
622 }
623
624 double ThermalMassDensity::heat_capacity(const RowVectorNd &p, double t, int el_id) const
625 {
626 return heat_capacity_(p, t, el_id);
627 }
628
632
633 void FiberDirection::resize(const int size)
634 {
635 assert(size == 2 || size == 3);
636 size_ = size;
637 if (!dir_.empty())
638 {
639 for (const auto &m : dir_)
640 {
641 assert((m.rows() == size && m.cols() == size) || (m.rows() == size && m.cols() == 1));
642 }
643 }
644 }
645
646 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> FiberDirection::operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
647 {
648 // Per-element fiber file: bound by global el_id, returned as a size_ x 1
649 // column vector so downstream is_a_vector logic is unchanged.
651 {
652 if (el_id < 0 || el_id >= static_cast<int>(per_el_fibers_.size()))
653 log_and_throw_error(fmt::format(
654 "Fiber el_id {} out of range [0,{})", el_id, per_el_fibers_.size()));
655 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> res;
656 res.resize(size_, 1);
657 for (int i = 0; i < size_; ++i)
658 res(i, 0) = per_el_fibers_[el_id](i);
659 return res;
660 }
661
662 assert(dir_.size() == 1 || el_id < dir_.size());
663
664 const auto &tmp = dir_.size() == 1 ? dir_[0] : dir_[el_id];
665 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> res;
666 res.resize(tmp.rows(), tmp.cols());
667 for (int i = 0; i < tmp.rows(); ++i)
668 {
669 for (int j = 0; j < tmp.cols(); ++j)
670 {
671 res(i, j) = tmp(i, j)(x, y, z, t, el_id);
672
673 assert(!std::isnan(res(i, j)));
674 assert(!std::isinf(res(i, j)));
675 }
676 }
677 return res;
678 }
679
680 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 6, 6> FiberDirection::stiffness_rotation_voigt(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
681 {
682 // Rotate stiffness mtx in voigt notation according to:
683 // https://scicomp.stackexchange.com/questions/35600/4th-order-tensor-rotation-sources-to-refer
684
685 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> rot = (*this)(px, py, pz, x, y, z, t, el_id);
686 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 6, 6> res;
687
688 int dim = rot.rows();
689
690 static const double sqrt2 = std::sqrt(2.0);
691
692 if (dim == 2)
693 {
694 res.resize(3, 3);
695 // Still need to compute for 2d
696 assert(false);
697 // res << rot(0, 0) * rot(0, 0), rot(0, 1) * rot(0, 1), 0,
698 // rot(1, 0) * rot(1, 0), rot(1, 1) * rot(1, 1), 0,
699 // 0, 0, 1;
700 }
701 else
702 {
703 assert(dim == 3);
704 res.resize(6, 6);
705 res << rot(0, 0) * rot(0, 0), rot(0, 1) * rot(0, 1), rot(0, 2) * rot(0, 2), sqrt2 * rot(0, 1) * rot(0, 2), sqrt2 * rot(0, 0) * rot(0, 2), sqrt2 * rot(0, 0) * rot(0, 1),
706 rot(1, 0) * rot(1, 0), rot(1, 1) * rot(1, 1), rot(1, 2) * rot(1, 2), sqrt2 * rot(1, 1) * rot(1, 2), sqrt2 * rot(1, 0) * rot(1, 2), sqrt2 * rot(1, 0) * rot(1, 1),
707 rot(2, 0) * rot(2, 0), rot(2, 1) * rot(2, 1), rot(2, 2) * rot(2, 2), sqrt2 * rot(2, 1) * rot(2, 2), sqrt2 * rot(2, 0) * rot(2, 2), sqrt2 * rot(2, 0) * rot(2, 1),
708 sqrt2 * rot(1, 0) * rot(2, 0), sqrt2 * rot(1, 1) * rot(2, 1), sqrt2 * rot(1, 2) * rot(2, 2), rot(1, 1) * rot(2, 2) + rot(1, 2) * rot(2, 1), rot(1, 0) * rot(2, 2) + rot(1, 2) * rot(2, 0), rot(1, 0) * rot(2, 1) + rot(1, 1) * rot(2, 0),
709 sqrt2 * rot(0, 0) * rot(2, 0), sqrt2 * rot(0, 1) * rot(2, 1), sqrt2 * rot(0, 2) * rot(2, 2), rot(0, 1) * rot(2, 2) + rot(0, 2) * rot(2, 1), rot(0, 0) * rot(2, 2) + rot(0, 2) * rot(2, 0), rot(0, 0) * rot(2, 1) + rot(0, 1) * rot(2, 0),
710 sqrt2 * rot(0, 0) * rot(1, 0), sqrt2 * rot(0, 1) * rot(1, 1), sqrt2 * rot(0, 2) * rot(1, 2), rot(0, 1) * rot(1, 2) + rot(0, 2) * rot(1, 1), rot(0, 0) * rot(1, 2) + rot(0, 2) * rot(1, 0), rot(0, 0) * rot(1, 1) + rot(0, 1) * rot(1, 0);
711 }
712
713 return res;
714 }
715
716 void FiberDirection::add_multimaterial(const int index, const json &dir, const std::string &unit, const io::ResourceIO &resources)
717 {
718 // Per-element fiber file:
719 // { "type": "per_element_file", "path": "...vtk", "field": "FIB_DIR1" }
720 // Read once at setup, bound by global el_id, normalized at load.
721 // NOTE: a JSON object with keys {type, path, field} reports size() == 3, so
722 // this MUST precede the size-based checks below to avoid being misread as a
723 // 3-component vector.
724 if (dir.is_object() && dir.value("type", std::string()) == "per_element_file")
725 {
726 const std::string field = dir.value("field", std::string("FIB_DIR1"));
727 const std::string path = dir.at("path").get<std::string>();
728 const std::string description = resources.describe(path);
729 auto input = resources.open(path, false);
730
731 per_el_fibers_ = read_cell_vectors_legacy_vtk(*input, description, field);
732 for (auto &v : per_el_fibers_)
733 {
734 const double n = v.norm();
735 if (n < 1e-12)
736 log_and_throw_error("Zero-length fiber vector in per-element file");
737 v /= n;
738 }
740 has_rotation_ = false; // a direction vector, not a rotation matrix
741 logger().info("FiberDirection: loaded {} per-element fibers ('{}') from {}",
742 per_el_fibers_.size(), field, description);
743 return; // dir_ left empty; operator() short-circuits
744 }
745
746 for (int i = dir_.size(); i <= index; ++i)
747 {
748 dir_.emplace_back();
749 }
750
751 if (dir.size() == 3 || dir.size() == 2)
752 {
753 const int size = dir.size();
754 const int other_size = dir[0].is_array() ? size : 1;
755
756 assert(size == size_);
757 dir_[index].resize(size, other_size);
758 for (int i = 0; i < size; ++i)
759 {
760 if (other_size == 1)
761 {
762 if (dir[i].is_array())
763 {
764 log_and_throw_error(fmt::format("Fiber must be a {} vector, row {} is {}", size, i, dir[i].dump()));
765 }
766 dir_[index](i, 0).init(dir[i], resources);
767 dir_[index](i, 0).set_unit_type(unit);
768 continue;
769 }
770 if (dir[i].size() != size)
771 {
772 log_and_throw_error(fmt::format("Fiber must be {}x{}, row {} is {}", size, other_size, i, dir[i].dump()));
773 }
774 for (int j = 0; j < size; ++j)
775 {
776 dir_[index](i, j).init(dir[i][j], resources);
777 dir_[index](i, j).set_unit_type(unit);
778 }
779 }
780 has_rotation_ = true;
781 }
782 else if (dir.size() == 9 || dir.size() == 4)
783 {
784 const int size = dir.size() == 9 ? 3 : 2;
785 assert(size == size_);
786 dir_[index].resize(size, size);
787 for (int i = 0; i < size; ++i)
788 {
789 for (int j = 0; j < size; ++j)
790 {
791 dir_[index](i, j).init(dir[i * size + j], resources);
792 dir_[index](i, j).set_unit_type(unit);
793 }
794 }
795 has_rotation_ = true;
796 }
797 else if (dir.empty())
798 {
799 dir_[index].resize(size_, size_);
800 for (int i = 0; i < size_; ++i)
801 {
802 for (int j = 0; j < size_; ++j)
803 {
804 dir_[index](i, j).init(i == j ? 1.0 : 0.0);
805 dir_[index](i, j).set_unit_type(unit);
806 }
807 }
808 has_rotation_ = false;
809 }
810 else
811 {
812 log_and_throw_error("Fiber direction must be a 3x3 or 2x2 matrix");
813 }
814 }
815
816 // template instantiation
817 template double ElasticityTensor::compute_stress<3>(const std::array<double, 3> &strain, const int j) const;
818 template double ElasticityTensor::compute_stress<6>(const std::array<double, 6> &strain, const int j) const;
819
820} // namespace polyfem::assembler
std::vector< Eigen::Triplet< double > > entries
int y
int z
int x
std::vector< utils::ExpressionValue > rho_
virtual bool is_time_dependent() const
virtual void add_multimaterial(const int index, const json &params, const std::string &density_unit, const io::ResourceIO &resources)
virtual double operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
virtual bool is_constant() const
Each material entry is independent of coordinates, time and index.
double operator()(int i, int j) const
void set_from_young_poisson(const double young, const double poisson, const std::string &stress_unit, const io::ResourceIO &resources)
void set_from_entries(const std::vector< double > &entries, const std::string &stress_unit, const io::ResourceIO &resources)
void rotate_stiffness(const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6 > &rotation_mtx_voigt)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6 > stiffness_tensor_
Definition MatParams.hpp:77
void set_transversely_isotropic(double Et, double Ea, double nu_t, double nu_a, double Ga, const std::string &stress_units, const io::ResourceIO &resources)
void set_orthotropic(double Ex, double Ey, double Ez, double nuXY, double nuXZ, double nuYZ, double muYZ, double muZX, double muXY, const std::string &stress_unit, const io::ResourceIO &resources)
void set_from_lambda_mu(const double lambda, const double mu, const std::string &stress_unit, const io::ResourceIO &resources)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6 > reference_stiffness_tensor_
Definition MatParams.hpp:78
double compute_stress(const std::array< double, DIM > &strain, const int j) const
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 1, 6, 6 > stiffness_rotation_voigt(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
std::vector< Eigen::Matrix< utils::ExpressionValue, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3 > > dir_
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3 > operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
std::vector< Eigen::Vector3d > per_el_fibers_
void add_multimaterial(const int index, const json &params, const std::string &unit, const io::ResourceIO &resources)
double operator()(const RowVectorNd &p, double t, int index) const
GenericMatParam(const std::string &param_name)
Definition MatParams.cpp:74
void add_multimaterial(const int index, const json &params, const std::string &unit_type, const io::ResourceIO &resources)
Definition MatParams.cpp:89
std::vector< utils::ExpressionValue > param_
Definition MatParams.hpp:26
std::vector< GenericMatParam > params_
Definition MatParams.hpp:45
void add_multimaterial(const int index, const json &params, const std::string &unit_type, const io::ResourceIO &resources)
GenericMatParams(const std::string &param_name)
std::vector< utils::ExpressionValue > mu_or_nu_
void lambda_mu(double px, double py, double pz, double x, double y, double z, double t, int el_id, double &lambda, double &mu) const
void add_multimaterial(const int index, const json &params, const bool is_volume, const std::string &stress_unit, const io::ResourceIO &resources)
std::vector< utils::ExpressionValue > lambda_or_E_
void set_e_nu(const int index, const json &E, const json &nu, const std::string &stress_unit, const io::ResourceIO &resources)
bool is_constant() const override
Each material entry is independent of coordinates, time and index.
bool is_time_dependent() const override
double rho(const RowVectorNd &p, double t, int el_id) const
double heat_capacity(const RowVectorNd &p, double t, int el_id) const
double operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const override
void add_multimaterial(const int index, const json &params, const std::string &density_unit, const io::ResourceIO &resources) override
Read-only interface to a hierarchical collection of simulation resources.
virtual std::string describe(const std::string &path) const =0
virtual std::unique_ptr< std::istream > open(const std::string &path, bool binary) const =0
Used for test only.
constexpr const char * MATERIAL_ELEMENT_INDEX
Definition MatParams.hpp:10
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
Definition JSONUtils.hpp:49
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
nlohmann::json json
Definition Common.hpp:9
double convert_to_mu(const double E, const double nu)
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13
double convert_to_lambda(const bool is_volume, const double E, const double nu)
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73