Browse Source

change into Eigen-3.3.7

cflin
Chen Wei 2 years ago
parent
commit
aa321cc257
  1. 1
      .gitignore
  2. 3
      cmake/recipes/eigen.cmake
  3. 208
      static_sim/Material.cpp
  4. 28
      static_sim/StaticSim.cpp
  5. 2
      static_sim/StaticSim.h
  6. 25
      static_sim/Utils.cpp
  7. 9
      static_sim/Utils.hpp

1
.gitignore

@ -1,4 +1,5 @@
cmake-*/ cmake-*/
build/
.idea/ .idea/
output/ output/
*.und/ *.und/

3
cmake/recipes/eigen.cmake

@ -25,8 +25,7 @@ else()
FetchContent_Declare( FetchContent_Declare(
eigen eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
# GIT_TAG tags/3.3.7 GIT_TAG tags/3.3.7
GIT_TAG tags/3.4.0
GIT_SHALLOW TRUE GIT_SHALLOW TRUE
) )
FetchContent_GetProperties(eigen) FetchContent_GetProperties(eigen)

208
static_sim/Material.cpp

@ -7,14 +7,14 @@
#include "Material.hpp" #include "Material.hpp"
namespace ssim{ namespace ssim {
void Material::computeN_tet(const Eigen::RowVector3d &P, const Eigen::Matrix<double, 4, 3> &X, void Material::computeN_tet(const Eigen::RowVector3d &P, const Eigen::Matrix<double, 4, 3> &X,
Eigen::Matrix<double, 3, 12> &N){ Eigen::Matrix<double, 3, 12> &N) {
Eigen::Matrix<double, 4, 4> H; Eigen::Matrix<double, 4, 4> H;
H.col(0).setOnes(); H.col(0).setOnes();
H(Eigen::all, Eigen::seq(1, Eigen::last)) = X; H.rightCols<3>() = X;
double V6 = H.determinant(); double V6 = H.determinant();
Eigen::Matrix<int, 4, 3> index; Eigen::Matrix<int, 4, 3> index;
@ -28,25 +28,39 @@ void Material::computeN_tet(const Eigen::RowVector3d &P, const Eigen::Matrix<dou
Eigen::RowVector4d c; Eigen::RowVector4d c;
Eigen::RowVector4d d; Eigen::RowVector4d d;
a(0) = H(index.row(0), index.row(0)).determinant(); auto SubMatrix = [](const Eigen::Matrix4d &original,
a(1) = -H(index.row(1), index.row(0)).determinant(); const Eigen::Vector3i &rowIdx,
a(2) = H(index.row(2), index.row(0)).determinant(); const Eigen::Vector3i &colIdx) {
a(3) = -H(index.row(3), index.row(0)).determinant(); Eigen::Matrix3d ret;
for (int i = 0; i < 3; ++i) {
b(0) = -H(index.row(0), index.row(1)).determinant(); int rowI = rowIdx(i);
b(1) = H(index.row(1), index.row(1)).determinant(); for (int j = 0; j < 3; ++j) {
b(2) = -H(index.row(2), index.row(1)).determinant(); int colI = colIdx(j);
b(3) = H(index.row(3), index.row(1)).determinant(); ret(i, j) = original(rowI, colI);
}
c(0) = H(index.row(0), index.row(2)).determinant(); }
c(1) = -H(index.row(1), index.row(2)).determinant(); return ret;
c(2) = H(index.row(2), index.row(2)).determinant(); };
c(3) = -H(index.row(3), index.row(2)).determinant();
a(0) = SubMatrix(H, index.row(0), index.row(0)).determinant();
d(0) = -H(index.row(0), index.row(3)).determinant(); a(1) = -SubMatrix(H, index.row(1), index.row(0)).determinant();
d(1) = H(index.row(1), index.row(3)).determinant(); a(2) = SubMatrix(H, index.row(2), index.row(0)).determinant();
d(2) = -H(index.row(2), index.row(3)).determinant(); a(3) = -SubMatrix(H, index.row(3), index.row(0)).determinant();
d(3) = H(index.row(3), index.row(3)).determinant();
b(0) = -SubMatrix(H, index.row(0), index.row(1)).determinant();
b(1) = SubMatrix(H, index.row(1), index.row(1)).determinant();
b(2) = -SubMatrix(H, index.row(2), index.row(1)).determinant();
b(3) = SubMatrix(H, index.row(3), index.row(1)).determinant();
c(0) = SubMatrix(H, index.row(0), index.row(2)).determinant();
c(1) = -SubMatrix(H, index.row(1), index.row(2)).determinant();
c(2) = SubMatrix(H, index.row(2), index.row(2)).determinant();
c(3) = -SubMatrix(H, index.row(3), index.row(2)).determinant();
d(0) = -SubMatrix(H, index.row(0), index.row(3)).determinant();
d(1) = SubMatrix(H, index.row(1), index.row(3)).determinant();
d(2) = -SubMatrix(H, index.row(2), index.row(3)).determinant();
d(3) = SubMatrix(H, index.row(3), index.row(3)).determinant();
Eigen::RowVector4d NN; Eigen::RowVector4d NN;
NN(0) = (a(0) + b(0) * P(0) + c(0) * P(1) + d(0) * P(2)) / V6; NN(0) = (a(0) + b(0) * P(0) + c(0) * P(1) + d(0) * P(2)) / V6;
@ -55,17 +69,19 @@ void Material::computeN_tet(const Eigen::RowVector3d &P, const Eigen::Matrix<dou
NN(3) = (a(3) + b(3) * P(0) + c(3) * P(1) + d(3) * P(2)) / V6; NN(3) = (a(3) + b(3) * P(0) + c(3) * P(1) + d(3) * P(2)) / V6;
N.setZero(); N.setZero();
N(0, Eigen::seq(0, Eigen::last, 3)) = NN; for (int i = 0; i < 4; ++i) {
N(1, Eigen::seq(1, Eigen::last, 3)) = NN; N(0, i * 3) = NN(i);
N(2, Eigen::seq(2, Eigen::last, 3)) = NN; N(1, i * 3 + 1) = NN(i);
N(2, i * 3 + 2) = NN(i);
}
} }
void Material::computeB_tet(const Eigen::Matrix<double, 4, 3> &X, Eigen::Matrix<double, 6, 12> &B){ void Material::computeB_tet(const Eigen::Matrix<double, 4, 3> &X, Eigen::Matrix<double, 6, 12> &B) {
Eigen::Matrix<double, 4, 4> H; Eigen::Matrix<double, 4, 4> H;
H.col(0).setOnes(); H.col(0).setOnes();
H(Eigen::all, Eigen::seq(1, Eigen::last)) = X; H.rightCols<3>() = X;
double V6 = H.determinant(); double V6 = H.determinant();
Eigen::Matrix<int, 4, 3> index; Eigen::Matrix<int, 4, 3> index;
@ -78,42 +94,58 @@ void Material::computeB_tet(const Eigen::Matrix<double, 4, 3> &X, Eigen::Matrix<
Eigen::RowVector4d c; Eigen::RowVector4d c;
Eigen::RowVector4d d; Eigen::RowVector4d d;
b(0) = -H(index.row(0), index.row(1)).determinant(); auto SubMatrix = [](const Eigen::Matrix4d &original,
b(1) = H(index.row(1), index.row(1)).determinant(); const Eigen::Vector3i &rowIdx,
b(2) = -H(index.row(2), index.row(1)).determinant(); const Eigen::Vector3i &colIdx) {
b(3) = H(index.row(3), index.row(1)).determinant(); Eigen::Matrix3d ret;
for (int i = 0; i < 3; ++i) {
c(0) = H(index.row(0), index.row(2)).determinant(); int rowI = rowIdx(i);
c(1) = -H(index.row(1), index.row(2)).determinant(); for (int j = 0; j < 3; ++j) {
c(2) = H(index.row(2), index.row(2)).determinant(); int colI = colIdx(j);
c(3) = -H(index.row(3), index.row(2)).determinant(); ret(i, j) = original(rowI, colI);
}
d(0) = -H(index.row(0), index.row(3)).determinant(); }
d(1) = H(index.row(1), index.row(3)).determinant(); return ret;
d(2) = -H(index.row(2), index.row(3)).determinant(); };
d(3) = H(index.row(3), index.row(3)).determinant();
b(0) = -SubMatrix(H, index.row(0), index.row(1)).determinant();
b(1) = SubMatrix(H, index.row(1), index.row(1)).determinant();
b(2) = -SubMatrix(H, index.row(2), index.row(1)).determinant();
b(3) = SubMatrix(H, index.row(3), index.row(1)).determinant();
c(0) = SubMatrix(H, index.row(0), index.row(2)).determinant();
c(1) = -SubMatrix(H, index.row(1), index.row(2)).determinant();
c(2) = SubMatrix(H, index.row(2), index.row(2)).determinant();
c(3) = -SubMatrix(H, index.row(3), index.row(2)).determinant();
d(0) = -SubMatrix(H, index.row(0), index.row(3)).determinant();
d(1) = SubMatrix(H, index.row(1), index.row(3)).determinant();
d(2) = -SubMatrix(H, index.row(2), index.row(3)).determinant();
d(3) = SubMatrix(H, index.row(3), index.row(3)).determinant();
B.setZero(); B.setZero();
B(0, Eigen::seq(0, Eigen::last, 3)) = b; for (int i = 0; i < 4; ++i) {
B(1, Eigen::seq(1, Eigen::last, 3)) = c; B(0, 3 * i) = b(i);
B(2, Eigen::seq(2, Eigen::last, 3)) = d; B(1, 3 * i + 1) = c(i);
B(3, Eigen::seq(0, Eigen::last, 3)) = c; B(2, 3 * i + 2) = d(i);
B(3, Eigen::seq(1, Eigen::last, 3)) = b; B(3, 3 * i) = c(i);
B(4, Eigen::seq(1, Eigen::last, 3)) = d; B(3, 3 * i + 1) = b(i);
B(4, Eigen::seq(2, Eigen::last, 3)) = c; B(4, 3 * i + 1) = d(i);
B(5, Eigen::seq(0, Eigen::last, 3)) = d; B(4, 3 * i + 2) = c(i);
B(5, Eigen::seq(2, Eigen::last, 3)) = b; B(5, 3 * i) = d(i);
B(5, 3 * i + 2) = b(i);
}
B /= V6; B /= V6;
} }
void Material::computeKe_tet(const Eigen::Matrix<double, 4, 3> &X, const Eigen::Matrix<double, 6, 6> &D, void Material::computeKe_tet(const Eigen::Matrix<double, 4, 3> &X, const Eigen::Matrix<double, 6, 6> &D,
Eigen::Matrix<double, 12, 12> &Ke, double &Vol){ Eigen::Matrix<double, 12, 12> &Ke, double &Vol) {
Eigen::Matrix<double, 4, 4> H; Eigen::Matrix<double, 4, 4> H;
H.col(0).setOnes(); H.col(0).setOnes();
H(Eigen::all, Eigen::seq(1, Eigen::last)) = X; H.rightCols<3>() = X;
double V6 = H.determinant(); double V6 = H.determinant();
Vol = V6 / 6.0; Vol = V6 / 6.0;
@ -127,35 +159,51 @@ void Material::computeKe_tet(const Eigen::Matrix<double, 4, 3> &X, const Eigen::
Eigen::RowVector4d c; Eigen::RowVector4d c;
Eigen::RowVector4d d; Eigen::RowVector4d d;
b(0) = -H(index.row(0), index.row(1)).determinant(); auto SubMatrix = [](const Eigen::Matrix4d &original,
b(1) = H(index.row(1), index.row(1)).determinant(); const Eigen::Vector3i &rowIdx,
b(2) = -H(index.row(2), index.row(1)).determinant(); const Eigen::Vector3i &colIdx) {
b(3) = H(index.row(3), index.row(1)).determinant(); Eigen::Matrix3d ret;
for (int i = 0; i < 3; ++i) {
c(0) = H(index.row(0), index.row(2)).determinant(); int rowI = rowIdx(i);
c(1) = -H(index.row(1), index.row(2)).determinant(); for (int j = 0; j < 3; ++j) {
c(2) = H(index.row(2), index.row(2)).determinant(); int colI = colIdx(j);
c(3) = -H(index.row(3), index.row(2)).determinant(); ret(i, j) = original(rowI, colI);
}
d(0) = -H(index.row(0), index.row(3)).determinant(); }
d(1) = H(index.row(1), index.row(3)).determinant(); return ret;
d(2) = -H(index.row(2), index.row(3)).determinant(); };
d(3) = H(index.row(3), index.row(3)).determinant();
b(0) = -SubMatrix(H, index.row(0), index.row(1)).determinant();
b(1) = SubMatrix(H, index.row(1), index.row(1)).determinant();
b(2) = -SubMatrix(H, index.row(2), index.row(1)).determinant();
b(3) = SubMatrix(H, index.row(3), index.row(1)).determinant();
c(0) = SubMatrix(H, index.row(0), index.row(2)).determinant();
c(1) = -SubMatrix(H, index.row(1), index.row(2)).determinant();
c(2) = SubMatrix(H, index.row(2), index.row(2)).determinant();
c(3) = -SubMatrix(H, index.row(3), index.row(2)).determinant();
d(0) = -SubMatrix(H, index.row(0), index.row(3)).determinant();
d(1) = SubMatrix(H, index.row(1), index.row(3)).determinant();
d(2) = -SubMatrix(H, index.row(2), index.row(3)).determinant();
d(3) = SubMatrix(H, index.row(3), index.row(3)).determinant();
Eigen::Matrix<double, 6, 12> B; Eigen::Matrix<double, 6, 12> B;
B.setZero(); B.setZero();
B(0, Eigen::seq(0, Eigen::last, 3)) = b; for (int i = 0; i < 4; ++i) {
B(1, Eigen::seq(1, Eigen::last, 3)) = c; B(0, 3 * i) = b(i);
B(2, Eigen::seq(2, Eigen::last, 3)) = d; B(1, 3 * i + 1) = c(i);
B(3, Eigen::seq(0, Eigen::last, 3)) = c; B(2, 3 * i + 2) = d(i);
B(3, Eigen::seq(1, Eigen::last, 3)) = b; B(3, 3 * i) = c(i);
B(4, Eigen::seq(1, Eigen::last, 3)) = d; B(3, 3 * i + 1) = b(i);
B(4, Eigen::seq(2, Eigen::last, 3)) = c; B(4, 3 * i + 1) = d(i);
B(5, Eigen::seq(0, Eigen::last, 3)) = d; B(4, 3 * i + 2) = c(i);
B(5, Eigen::seq(2, Eigen::last, 3)) = b; B(5, 3 * i) = d(i);
B(5, 3 * i + 2) = b(i);
}
B /= V6; B /= V6;
Ke = Vol * (B.transpose() * D * B); Ke = Vol * (B.transpose() * D * B);
} }
} // namespace SIM } // namespace SIM

28
static_sim/StaticSim.cpp

@ -96,8 +96,8 @@ namespace ssim {
int cnt = 0; int cnt = 0;
for (const auto &item: vFLoc[vI]) { for (const auto &item: vFLoc[vI]) {
int eleI = item.first; int eleI = item.first;
const auto &U_eleI = U(eDof[eleI]); const auto &U_eleI = Utils::SubVector(U, eDof[eleI]);
Eigen::Matrix<double, 4, 3> X = TV(TT.row(eleI), Eigen::all); Eigen::Matrix<double, 4, 3> X = Utils::SubMatrix(TV, TT.row(eleI), Eigen::Vector3i(0, 1, 2));
Eigen::Matrix<double, 6, 12> B; Eigen::Matrix<double, 6, 12> B;
Material::computeB_tet(X, B); Material::computeB_tet(X, B);
@ -126,8 +126,8 @@ namespace ssim {
Qstress.resize(nQ, 6); Qstress.resize(nQ, 6);
for (int qI = 0; qI < nQ; ++qI) { for (int qI = 0; qI < nQ; ++qI) {
int tI = tetI(qI); int tI = tetI(qI);
const auto &U_tI = U(eDof[tI]); const auto &U_tI = Utils::SubVector(U, eDof[tI]);
Eigen::Matrix<double, 4, 3> X = TV(TT.row(tI), Eigen::all); Eigen::Matrix<double, 4, 3> X = Utils::SubMatrix(TV, TT.row(tI), Eigen::Vector3i(0, 1, 2));
Eigen::Matrix<double, 3, 12> N; Eigen::Matrix<double, 3, 12> N;
Eigen::Matrix<double, 6, 12> B; Eigen::Matrix<double, 6, 12> B;
@ -171,9 +171,11 @@ namespace ssim {
#endif #endif
{ {
Eigen::VectorXi TT_I = TT.row(eI); Eigen::VectorXi TT_I = TT.row(eI);
eDof[eI](Eigen::seq(0, Eigen::last, 3)) = TT_I * 3; for (int i_ = 0; i_ < 4; ++i_) {
eDof[eI](Eigen::seq(1, Eigen::last, 3)) = (TT_I * 3).array() + 1; eDof[eI](3 * i_) = TT_I(i_) * 3;
eDof[eI](Eigen::seq(2, Eigen::last, 3)) = (TT_I * 3).array() + 2; eDof[eI](3 * i_ + 1) = TT_I(i_) * 3 + 1;
eDof[eI](3 * i_ + 2) = TT_I(i_) * 3 + 2;
}
} }
#ifdef USE_TBB #ifdef USE_TBB
); );
@ -184,8 +186,9 @@ namespace ssim {
vNeighbor.resize(nN); vNeighbor.resize(nN);
for (int eI = 0; eI < nEle; ++eI) { for (int eI = 0; eI < nEle; ++eI) {
const Eigen::Matrix<int, 1, 4> &eleVInd = TT.row(eI); const Eigen::Matrix<int, 1, 4> &eleVInd = TT.row(eI);
for (const auto &nI: eleVInd) { std::vector<int> eleVInd_vec{eleVInd(0), eleVInd(1), eleVInd(2), eleVInd(3)};
vNeighbor[nI].insert(eleVInd.begin(), eleVInd.end()); for (const auto &nI: eleVInd_vec) {
vNeighbor[nI].insert(eleVInd_vec.begin(), eleVInd_vec.end());
} }
} }
for (int nI = 0; nI < nN; ++nI) { // remove itself for (int nI = 0; nI < nN; ++nI) { // remove itself
@ -215,7 +218,7 @@ namespace ssim {
#endif #endif
{ {
// eleKe // eleKe
Eigen::Matrix<double, 4, 3> X = TV(TT.row(eI), Eigen::all); Eigen::Matrix<double, 4, 3> X = Utils::SubMatrix(TV, TT.row(eI), Eigen::Vector3i(0, 1, 2));
Eigen::Matrix<double, 12, 12> eleKe_I; Eigen::Matrix<double, 12, 12> eleKe_I;
double vol; double vol;
Material::computeKe_tet(X, D, eleKe_I, vol); Material::computeKe_tet(X, D, eleKe_I, vol);
@ -257,7 +260,7 @@ namespace ssim {
compliance_ = load.dot(U); compliance_ = load.dot(U);
spdlog::info("compliance C = {:e}", compliance_); spdlog::info("compliance C = {:e}", compliance_);
TV1 = TV + U.reshaped(3, nN).transpose(); // TV1 = TV + U.reshaped(3, nN).transpose();
// Utils::writeTetVTK(outputPath + "deformed.vtk", TV1, TT); // Utils::writeTetVTK(outputPath + "deformed.vtk", TV1, TT);
@ -352,7 +355,8 @@ namespace ssim {
spdlog::debug("#DBC nodes: {}, #NBC particles: {}", nDBC, nNBC); spdlog::debug("#DBC nodes: {}, #NBC particles: {}", nDBC, nNBC);
// ensure (DBC intersect NBC) = (empty) // ensure (DBC intersect NBC) = (empty)
for (const int &nI: DBC_nI) { for (int i_ = 0; i_ < DBC_nI.size(); ++i_) {
int nI = DBC_nI(i_);
load.segment<DIM_>(nI * DIM_).setZero(); load.segment<DIM_>(nI * DIM_).setZero();
} }
} }

2
static_sim/StaticSim.h

@ -118,7 +118,7 @@ namespace ssim {
int eleNodeNum; int eleNodeNum;
int eleDofNum; int eleDofNum;
std::vector<Eigen::Vector<int, 12>> eDof; std::vector<Eigen::VectorXi> eDof;
// owned features // owned features
Eigen::VectorXd load; // load of each dof Eigen::VectorXd load; // load of each dof

25
static_sim/Utils.cpp

@ -318,11 +318,32 @@ void Utils::elasticMatrix(double YM, double PR, Eigen::Matrix<double, 6, 6> &D)
D *= YM / (1.0 + PR) / (1.0 - 2.0 * PR); D *= YM / (1.0 + PR) / (1.0 - 2.0 * PR);
} }
double Utils::vonStress(const Eigen::Vector<double, 6> &stress) { double Utils::vonStress(const Eigen::VectorXd &stress) {
return sqrt(0.5 * (pow(stress(0) - stress(1), 2.0) + return sqrt(0.5 * (pow(stress(0) - stress(1), 2.0) +
pow(stress(1) - stress(2), 2.0) + pow(stress(1) - stress(2), 2.0) +
pow(stress(2) - stress(0), 2.0)) pow(stress(2) - stress(0), 2.0))
+ 3.0 * stress({3, 4, 5}).squaredNorm()); + 3.0 * Utils::SubVector(stress, Eigen::Vector3i(3, 4, 5)).squaredNorm());
}
Eigen::MatrixXd Utils::SubMatrix(const Eigen::MatrixXd &original,
const Eigen::VectorXi &rowIdx,
const Eigen::VectorXi &colIdx) {
Eigen::MatrixXd ret(rowIdx.size(), colIdx.size());
for (int i = 0; i < rowIdx.size(); ++i) {
for (int j = 0; j < colIdx.size(); ++j) {
ret(i, j) = original(rowIdx(i), colIdx(j));
}
}
return ret;
}
Eigen::VectorXd Utils::SubVector(const Eigen::VectorXd &original,
const Eigen::VectorXi &index) {
Eigen::VectorXd ret(index.size());
for (int i = 0; i < index.size(); ++i) {
ret(i) = original(index(i));
}
return ret;
} }
} // namespace SIM } // namespace SIM

9
static_sim/Utils.hpp

@ -88,7 +88,7 @@ static void writeOBJ(const std::string &path, const Eigen::MatrixXd &V, const Ei
static void elasticMatrix(double YM, double PR, Eigen::Matrix<double, 6, 6> &D); static void elasticMatrix(double YM, double PR, Eigen::Matrix<double, 6, 6> &D);
static double vonStress(const Eigen::Vector<double, 6> &stress); static double vonStress(const Eigen::VectorXd &stress);
template <int dim> template <int dim>
static void addBlockToMatrix(const Eigen::MatrixXd& block, static void addBlockToMatrix(const Eigen::MatrixXd& block,
@ -128,6 +128,13 @@ static void addBlockToMatrix(const Eigen::MatrixXd& block,
} }
static Eigen::MatrixXd SubMatrix(const Eigen::MatrixXd &original,
const Eigen::VectorXi &rowIdx,
const Eigen::VectorXi &colIdx);
static Eigen::VectorXd SubVector(const Eigen::VectorXd &original,
const Eigen::VectorXi &index);
}; };
} // namespace SIM } // namespace SIM

Loading…
Cancel
Save