前言
接前文有限元编程示例matlab + C++,为了方便查看,将C++程序单独拿出来。具体问题分析可以查看前文
一、三连杆问题


#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
MatrixXf Bar1D2Node_Stiffness(float E, float A, float L)
{
MatrixXf k(2, 2);
k << E * A / L, -E * A / L, -E * A / L, E* A / L;
return k;
}
MatrixXf Bar1D2Node_Assembly(MatrixXf KK, MatrixXf k, int i, float j)
{
Vector2i Dof;
Dof << i, j;
for (int n1 = 0; n1 < 2; n1++)
{
for (int n2 = 0; n2 < 2; n2++)
{
KK(Dof(n1) - 1, Dof(n2) - 1) = KK(Dof(n1) - 1, Dof(n2) - 1) + k(n1, n2);
}
}
return KK;
}
int main()
{
float E1 = 2e5; float E2 = E1; float E3 = E1;
float A3 = 0.06; float A2 = A3 / 2; float A1 = A3 / 3;
float L1 = 0.1; float L2 = L1; float L3 = L1;
MatrixXf k1(2, 2); MatrixXf k2(2, 2); MatrixXf k3(2, 2);
k1 = Bar1D2Node_Stiffness(E1, A1, L1);
k2 = Bar1D2Node_Stiffness(E2, A2, L2);
k3 = Bar1D2Node_Stiffness(E3, A3, L3);
MatrixXf KK(4, 4);
KK.setZero(4, 4);
KK = Bar1D2Node_Assembly(KK, k1, 1, 2);
KK = Bar1D2Node_Assembly(KK, k2, 2, 3);
KK = Bar1D2Node_Assembly(KK, k3, 3, 4);
MatrixXf kk(3, 3);
kk = KK.block<3, 3>(0, 0);
Vector3f P(-100, 0, 50);
Vector3f x(0, 0, 0);
x = kk.lu().solve(P);
system("pause");
return 0;
}
二、平面3节点的三角形单元


#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
MatrixXf Tri2D3Node_Stiffness(float E, float mu, float t, float xi, float yi, float xj, float yj, float xm, float ym, int Id)
{
float A, bi, bj, bm, ci, cj, cm;
A = (xi * (yj - ym) + xj * (ym - yi) + xm * (yi - yj)) / 2;
bi = yj - ym;
bj = ym - yi;
bm = yi - yj;
ci = xm - xj;
cj = xi - xm;
cm = xj - xi;
MatrixXf B(3, 6);
B << bi, 0, bj, 0, bm, 0,
0, ci, 0, cj, 0, cm,
ci, bi, cj, bj, cm, bm;
B = B / (2 * A);
MatrixXf D(3, 3);
if (Id == 1)
{
D << 1, mu, 0,
mu, 1, 0,
0, 0, (1 - mu) / 2;
D = (E / (1 - mu * mu)) * D;
}
else if (Id == 2)
{
D << 1 - mu, mu, 0,
mu, 1 - mu, 0,
0, 0, (1 - 2 * mu) / 2;
D = (E / (1 + mu) / (1 - 2 * mu)) * D;
}
MatrixXf k(6, 6);
k = t * A * B.transpose() * D * B;
return k;
}
MatrixXf Tri2D3Node_Assembly(MatrixXf KK, MatrixXf k, int i, int j, int m)
{
VectorXi Dof(6);
Dof << 2 * i - 1, 2 * i, 2 * j - 1, 2 * j, 2 * m - 1, 2 * m;
for (int n1 = 0; n1 < 6; n1++)
{
for (int n2 = 0; n2 < 6; n2++)
{
KK(Dof(n1) - 1, Dof(n2) - 1) = KK(Dof(n1) - 1, Dof(n2) - 1) + k(n1, n2);
}
}
return KK;
}
int main()
{
float E = 1e7;
float mu = 1.0 / 3;
float t = 0.1;
int Id = 1;
MatrixXf k1(6, 6), k2(6, 6);
k1 = Tri2D3Node_Stiffness(E, mu, t, 2, 0, 0, 1, 0, 0, Id);
k2 = Tri2D3Node_Stiffness(E, mu, t, 0, 1, 2, 0, 2, 1, Id);
cout << "k1:" << endl << k1 << endl;
cout << "k2:" << endl << k2 << endl;
MatrixXf KK(8, 8);
KK.setZero(8, 8);
KK = Tri2D3Node_Assembly(KK, k1, 2, 3, 4);
KK = Tri2D3Node_Assembly(KK, k2, 3, 2, 1);
cout << "总体刚度矩阵 KK = " << endl << KK << endl;
MatrixXf kk(4, 4);
kk = KK.block<4, 4>(0, 0);
cout << "kk:" << endl << kk << endl;
Vector4f p(0, -5000, 0, -5000);
Vector4f u(0, 0, 0, 0);
u = kk.lu().solve(p);
cout << "节点位移 u = " << endl << u << endl;
VectorXf U(8);
U << u, 0, 0, 0, 0;
VectorXf P(8);
P = KK * U;
cout << "节点位移 U = " << endl << U << endl;
cout << "支反力 P = " << endl << P << endl;
system("pause");
return 0;
}
总结
一维数组名称的用途: