|
| 1 | +#ifndef LINEARTHERMAL_H |
| 2 | +#define LINEARTHERMAL_H |
| 3 | + |
| 4 | +#include "matmodel.h" |
| 5 | +#include <Eigen/StdVector> // For Eigen's aligned_allocator |
| 6 | + |
| 7 | +class LinearThermalIsotropic : public ThermalModel, public LinearModel<1> { |
| 8 | + public: |
| 9 | + LinearThermalIsotropic(vector<double> l_e, json materialProperties) |
| 10 | + : ThermalModel(l_e) |
| 11 | + { |
| 12 | + try { |
| 13 | + conductivity = materialProperties["conductivity"].get<vector<double>>(); |
| 14 | + } catch (const std::exception &e) { |
| 15 | + throw std::runtime_error("Missing material properties for the requested material model."); |
| 16 | + } |
| 17 | + n_mat = conductivity.size(); |
| 18 | + |
| 19 | + double kappa_ref = (*max_element(conductivity.begin(), conductivity.end()) + |
| 20 | + *min_element(conductivity.begin(), conductivity.end())) / |
| 21 | + 2; |
| 22 | + kapparef_mat = kappa_ref * Matrix3d::Identity(); |
| 23 | + |
| 24 | + Matrix3d phase_kappa; |
| 25 | + phase_stiffness = new Matrix<double, 8, 8>[n_mat]; |
| 26 | + |
| 27 | + for (size_t i = 0; i < n_mat; ++i) { |
| 28 | + phase_stiffness[i] = Matrix<double, 8, 8>::Zero(); |
| 29 | + phase_kappa = conductivity[i] * Matrix3d::Identity(); |
| 30 | + |
| 31 | + for (int p = 0; p < 8; ++p) { |
| 32 | + phase_stiffness[i] += B_int[p].transpose() * phase_kappa * B_int[p] * v_e * 0.1250; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + void get_sigma(int i, int mat_index, ptrdiff_t element_idx) override |
| 38 | + { |
| 39 | + sigma(i + 0, 0) = conductivity[mat_index] * eps(i + 0, 0); |
| 40 | + sigma(i + 1, 0) = conductivity[mat_index] * eps(i + 1, 0); |
| 41 | + sigma(i + 2, 0) = conductivity[mat_index] * eps(i + 2, 0); |
| 42 | + } |
| 43 | + |
| 44 | + private: |
| 45 | + vector<double> conductivity; |
| 46 | +}; |
| 47 | + |
| 48 | +class LinearThermalTriclinic : public ThermalModel, public LinearModel<1> { |
| 49 | + public: |
| 50 | + EIGEN_MAKE_ALIGNED_OPERATOR_NEW // Ensure proper alignment for Eigen structures |
| 51 | + |
| 52 | + LinearThermalTriclinic(vector<double> l_e, json materialProperties) |
| 53 | + : ThermalModel(l_e) |
| 54 | + { |
| 55 | + vector<string> K_keys = { |
| 56 | + "K_11", "K_12", "K_13", |
| 57 | + "K_22", "K_23", |
| 58 | + "K_33"}; |
| 59 | + |
| 60 | + try { |
| 61 | + n_mat = materialProperties.at("K_11").get<vector<double>>().size(); |
| 62 | + size_t num_constants = K_keys.size(); |
| 63 | + |
| 64 | + // Initialize matrix to hold all constants |
| 65 | + K_constants.resize(num_constants, n_mat); |
| 66 | + |
| 67 | + // Load material constants into matrix |
| 68 | + for (size_t k = 0; k < num_constants; ++k) { |
| 69 | + const auto &values = materialProperties.at(K_keys[k]).get<vector<double>>(); |
| 70 | + if (values.size() != n_mat) { |
| 71 | + throw std::runtime_error("Inconsistent size for material property: " + K_keys[k]); |
| 72 | + } |
| 73 | + K_constants.row(k) = Eigen::Map<const RowVectorXd>(values.data(), values.size()); |
| 74 | + } |
| 75 | + } catch (const std::exception &e) { |
| 76 | + throw std::runtime_error("Missing or inconsistent material properties for the requested material model."); |
| 77 | + } |
| 78 | + |
| 79 | + // Assemble conductivity matrices for each material |
| 80 | + K_mats.resize(n_mat); |
| 81 | + kapparef_mat = Matrix3d::Zero(); |
| 82 | + |
| 83 | + for (size_t i = 0; i < n_mat; ++i) { |
| 84 | + Matrix3d K_i; |
| 85 | + K_i << K_constants(0, i), K_constants(1, i), K_constants(2, i), |
| 86 | + K_constants(1, i), K_constants(3, i), K_constants(4, i), |
| 87 | + K_constants(2, i), K_constants(4, i), K_constants(5, i); |
| 88 | + |
| 89 | + K_mats[i] = K_i; |
| 90 | + kapparef_mat += K_i; |
| 91 | + } |
| 92 | + |
| 93 | + kapparef_mat /= n_mat; |
| 94 | + |
| 95 | + // Compute phase stiffness matrices |
| 96 | + phase_stiffness = new Matrix<double, 8, 8>[n_mat]; |
| 97 | + for (size_t i = 0; i < n_mat; ++i) { |
| 98 | + phase_stiffness[i] = Matrix<double, 8, 8>::Zero(); |
| 99 | + for (int p = 0; p < 8; ++p) { |
| 100 | + phase_stiffness[i] += B_int[p].transpose() * K_mats[i] * B_int[p] * v_e * 0.1250; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + void get_sigma(int i, int mat_index, ptrdiff_t element_idx) override |
| 106 | + { |
| 107 | + sigma.segment<3>(i) = K_mats[mat_index] * eps.segment<3>(i); |
| 108 | + } |
| 109 | + |
| 110 | + private: |
| 111 | + std::vector<Matrix3d, Eigen::aligned_allocator<Matrix3d>> K_mats; |
| 112 | + MatrixXd K_constants; |
| 113 | +}; |
| 114 | + |
| 115 | +#endif // LINEARTHERMAL_H |
0 commit comments