# FEM (有限元分析)

Untitled

Untitled

Untitled

Untitled

X10 ,x20,x30 竖向量写入矩阵,逆矩阵后行列式 * 1/6 是四面体面值

大 X 是形变前,小 x 是形变后
矩阵的迹是对角线相加
µ 和 λ 自定义输入,为摩擦力
F0 = -f1 -f2-f3;

unity文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using System;
using System.IO;

public class FVM : MonoBehaviour
{
public bool LaplacianSmoothing = false;
public enum PartType{BasePart, Bonus}
public PartType partType = PartType.BasePart;

float dt = 0.0015f;
float mass = 1;
float stiffness_0 = 20000.0f;
float stiffness_1 = 5000.0f;
float damp = 0.999f;

float restitution = 0.4f; // for collision

Vector3 gravity = new Vector3(0, -9.8f, 0);

int[] Tet;
int tet_number; //The number of tetrahedra

Vector3[] Force;
Vector3[] V;
Vector3[] SmoothV;
bool[] isSmooth;
Vector3[] X;
int number; //The number of vertices

Matrix4x4[] inv_Dm;
float[] V_ref;
int[][] neighbour;

//For Laplacian smoothing.
Vector3[] V_sum;
int[] V_num;

SVD svd = new SVD();

// Start is called before the first frame update
void Start()
{
// FILO IO: Read the house model from files.
// The model is from Jonathan Schewchuk's Stellar lib.
{
string fileContent = File.ReadAllText("Assets/Lab/lab3/house2.ele");
string[] Strings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

tet_number = int.Parse(Strings[0]);
Tet = new int[tet_number * 4];

for (int tet = 0; tet < tet_number; tet++)
{
Tet[tet * 4 + 0] = int.Parse(Strings[tet * 5 + 4]) - 1;
Tet[tet * 4 + 1] = int.Parse(Strings[tet * 5 + 5]) - 1;
Tet[tet * 4 + 2] = int.Parse(Strings[tet * 5 + 6]) - 1;
Tet[tet * 4 + 3] = int.Parse(Strings[tet * 5 + 7]) - 1;
}
}
{
string fileContent = File.ReadAllText("Assets/Lab/lab3/house2.node");
string[] Strings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
number = int.Parse(Strings[0]);
X = new Vector3[number];
for (int i = 0; i < number; i++)
{
X[i].x = float.Parse(Strings[i * 5 + 5]) * 0.4f;
X[i].y = float.Parse(Strings[i * 5 + 6]) * 0.4f;
X[i].z = float.Parse(Strings[i * 5 + 7]) * 0.4f;
}
//Centralize the model.
Vector3 center = Vector3.zero;
for (int i = 0; i < number; i++) center += X[i];
center = center / number;
for (int i = 0; i < number; i++)
{
X[i] -= center;
float temp = X[i].y;
X[i].y = X[i].z;
X[i].z = temp;
}
}
/*tet_number=1;
Tet = new int[tet_number*4];
Tet[0]=0;
Tet[1]=1;
Tet[2]=2;
Tet[3]=3;

number=4;
X = new Vector3[number];
V = new Vector3[number];
Force = new Vector3[number];
X[0]= new Vector3(0, 0, 0);
X[1]= new Vector3(1, 0, 0);
X[2]= new Vector3(0, 1, 0);
X[3]= new Vector3(0, 0, 1);*/

//Create triangle mesh.
Vector3[] vertices = new Vector3[tet_number * 12];
int vertex_number = 0;
for (int tet = 0; tet < tet_number; tet++)
{
vertices[vertex_number++] = X[Tet[tet * 4 + 0]];
vertices[vertex_number++] = X[Tet[tet * 4 + 2]];
vertices[vertex_number++] = X[Tet[tet * 4 + 1]];

vertices[vertex_number++] = X[Tet[tet * 4 + 0]];
vertices[vertex_number++] = X[Tet[tet * 4 + 3]];
vertices[vertex_number++] = X[Tet[tet * 4 + 2]];

vertices[vertex_number++] = X[Tet[tet * 4 + 0]];
vertices[vertex_number++] = X[Tet[tet * 4 + 1]];
vertices[vertex_number++] = X[Tet[tet * 4 + 3]];

vertices[vertex_number++] = X[Tet[tet * 4 + 1]];
vertices[vertex_number++] = X[Tet[tet * 4 + 2]];
vertices[vertex_number++] = X[Tet[tet * 4 + 3]];
}

int[] triangles = new int[tet_number * 12];
for (int t = 0; t < tet_number * 4; t++)
{
triangles[t * 3 + 0] = t * 3 + 0;
triangles[t * 3 + 1] = t * 3 + 1;
triangles[t * 3 + 2] = t * 3 + 2;
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();

V = new Vector3[number];
SmoothV = new Vector3[number];
isSmooth = new bool[number];
Force = new Vector3[number];
V_sum = new Vector3[number];
V_num = new int[number];

//TODO: Need to allocate and assign inv_Dm
inv_Dm = new Matrix4x4[tet_number];
V_ref = new float[tet_number];
for (int tet = 0; tet < tet_number; tet++)
{
inv_Dm[tet] = Build_Edge_Matrix(tet);
inv_Dm[tet] = inv_Dm[tet].inverse;
V_ref[tet] = 1 / (6 * inv_Dm[tet].determinant);
}
neighbour = new int[number][];
}

Matrix4x4 Build_Edge_Matrix(int tet)
{
Matrix4x4 ret = Matrix4x4.zero;
//TODO: Need to build edge matrix here.

int index_0 = Tet[tet * 4 + 0];
int index_1 = Tet[tet * 4 + 1];
int index_2 = Tet[tet * 4 + 2];
int index_3 = Tet[tet * 4 + 3];

Vector3 X10 = X[index_1] - X[index_0];
Vector3 X20 = X[index_2] - X[index_0];
Vector3 X30 = X[index_3] - X[index_0];

ret[0] = X10[0];
ret[1] = X10[1];
ret[2] = X10[2];

ret[0, 1] = X20[0];
ret[1, 1] = X20[1];
ret[2, 1] = X20[2];

ret[0, 2] = X30[0];
ret[1, 2] = X30[1];
ret[2, 2] = X30[2];

ret[3, 3] = 1;
return ret;
}

void _Update()
{
// Jump up.
if (Input.GetKeyDown(KeyCode.Space))
{
for (int i = 0; i < number; i++)
V[i].y += 0.2f;
}

Parallel.For(0, number, (i) =>
{
//TODO: Add gravity to Force.
Force[i] = new Vector3(0, -9.8f, 0);

});
Parallel.For(0, tet_number, (tet) =>
{
//TODO: Deformation Gradient
Matrix4x4 F = Matrix4x4.zero;
F = Build_Edge_Matrix(tet) * inv_Dm[tet];

Matrix4x4 P;
if (partType == PartType.BasePart)
{
//TODO: Green Strain
Matrix4x4 FtF = F.transpose * F;

Matrix4x4 G = MatrixMultiple(MatrixSubtraction(FtF, Matrix4x4.identity), 0.5f);
//TODO: Second PK Stress
float trace;
trace = G[0, 0] + G[1, 1] + G[2, 2];
Matrix4x4 dWdG = MatrixAddition(MatrixMultiple(G, 2 * stiffness_1), MatrixMultiple(Matrix4x4.identity, stiffness_0 * trace));

P = F * dWdG;
}
else
{
//TODO: Principal stretches
Matrix4x4 Lambda = Matrix4x4.identity;
Matrix4x4 U = Matrix4x4.identity;
Matrix4x4 V = Matrix4x4.identity;
svd.svd(F,ref U,ref Lambda,ref V);
U[3, 3] = 1;
Lambda[3, 3] = 1;
V[3, 3] = 1;

Matrix4x4 diag = Matrix4x4.identity;
float Lambda0 = Lambda[0, 0];
float Lambda0_2 = Lambda0 * Lambda0;
float Lambda1 = Lambda[1, 1];
float Lambda1_2 = Lambda1 * Lambda1;
float Lambda2 = Lambda[2, 2];
float Lambda2_2 = Lambda2 * Lambda2;

diag[0, 0] = 2 * stiffness_0 * (Lambda0_2 + Lambda1_2 + Lambda2_2 - 3) * Lambda0 + stiffness_1 * (Lambda0-1) * Lambda0;
diag[1, 1] = 2 * stiffness_0 * (Lambda0_2 + Lambda1_2 + Lambda2_2 - 3) * Lambda1 + stiffness_1 * (Lambda1-1) * Lambda1;
diag[2, 2] = 2 * stiffness_0 * (Lambda0_2 + Lambda1_2 + Lambda2_2 - 3) * Lambda2 + stiffness_1 * (Lambda2-1) * Lambda2;
//TODO: First PK Stress

P = U * diag * V.transpose;
}
//TODO: Elastic Force

Matrix4x4 F123 = MatrixMultiple(P, -V_ref[tet]) * inv_Dm[tet].transpose;

Vector3 f1 = (Vector3)F123.GetColumn(0);
Vector3 f2 = (Vector3)F123.GetColumn(1);
Vector3 f3 = (Vector3)F123.GetColumn(2);

Vector3 f0 = -f1 - f2 - f3;

Force[Tet[tet * 4 + 0]] += f0;
Force[Tet[tet * 4 + 1]] += f1;
Force[Tet[tet * 4 + 2]] += f2;
Force[Tet[tet * 4 + 3]] += f3;

if(LaplacianSmoothing)
{
int index_0 = Tet[tet * 4 + 0];
int index_1 = Tet[tet * 4 + 1];
int index_2 = Tet[tet * 4 + 2];
int index_3 = Tet[tet * 4 + 3];
Vector3 v = (V[index_0] + V[index_1] + V[index_2] + V[index_3]) / 4;
SmoothV[index_0] = v;
SmoothV[index_1] = v;
SmoothV[index_2] = v;
SmoothV[index_3] = v;
}
});
Parallel.For(0, number, (i) =>
//for (int i = 0; i < number; i++)
{
//TODO: Update X and V here.
if(LaplacianSmoothing)
V[i] = SmoothV[i];
V[i] += Force[i] * dt;
isSmooth[i] = false;
SmoothV[i] = Vector3.zero;
X[i] += V[i] * dt;
V[i] *= damp;
//TODO: (Particle) collision with floor.

});
Collision_Impulse(new Vector3(0, -3, 0), new Vector3(0, 1, 0));
}


void Collision_Impulse(Vector3 P, Vector3 N)
{
for (int i = 0; i < V.Length; i++)
{
float sdf = PlaneSignedDistanceFunction(X[i], P, N);
if (sdf < 0)
{
if (Vector3.Dot(V[i], N) < 0)
{
Vector3 v_Ni = Vector3.Dot(V[i], N) * N;
Vector3 v_Ti = V[i] - v_Ni;
float mu_N = restitution;
float mu_T = 0.1f;
float a = Mathf.Max(1 - (mu_T * (1 + mu_N) * v_Ni.magnitude / v_Ti.magnitude), 0);
v_Ni = -mu_N * v_Ni;
v_Ti = a * v_Ti;
Vector3 v_new_i = v_Ni + v_Ti;

V[i] = v_new_i;

X[i] = X[i] - sdf * N;
}
}
}
}

float PlaneSignedDistanceFunction(Vector3 x, Vector3 P, Vector3 N)
{
return Vector3.Dot((x - P), N);
}

// Update is called once per frame
void Update()
{
for (int l = 0; l < 10; l++)
_Update();

// Dump the vertex array for rendering.
Vector3[] vertices = new Vector3[tet_number * 12];
int vertex_number = 0;
for (int tet = 0; tet < tet_number; tet++)
{
vertices[vertex_number++] = X[Tet[tet * 4 + 0]];
vertices[vertex_number++] = X[Tet[tet * 4 + 2]];
vertices[vertex_number++] = X[Tet[tet * 4 + 1]];
vertices[vertex_number++] = X[Tet[tet * 4 + 0]];
vertices[vertex_number++] = X[Tet[tet * 4 + 3]];
vertices[vertex_number++] = X[Tet[tet * 4 + 2]];
vertices[vertex_number++] = X[Tet[tet * 4 + 0]];
vertices[vertex_number++] = X[Tet[tet * 4 + 1]];
vertices[vertex_number++] = X[Tet[tet * 4 + 3]];
vertices[vertex_number++] = X[Tet[tet * 4 + 1]];
vertices[vertex_number++] = X[Tet[tet * 4 + 2]];
vertices[vertex_number++] = X[Tet[tet * 4 + 3]];
}
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.vertices = vertices;
mesh.RecalculateNormals();
}

Matrix4x4 MatrixSubtraction(Matrix4x4 m1, Matrix4x4 m2)
{
m1[0, 0] -= m2[0, 0];
m1[0, 1] -= m2[0, 1];
m1[0, 2] -= m2[0, 2];

m1[1, 0] -= m2[1, 0];
m1[1, 1] -= m2[1, 1];
m1[1, 2] -= m2[1, 2];

m1[2, 0] -= m2[2, 0];
m1[2, 1] -= m2[2, 1];
m1[2, 2] -= m2[2, 2];

m1[3, 3] = 1;
return m1;
}

Matrix4x4 MatrixAddition(Matrix4x4 m1, Matrix4x4 m2)
{
m1[0, 0] += m2[0, 0];
m1[0, 1] += m2[0, 1];
m1[0, 2] += m2[0, 2];

m1[1, 0] += m2[1, 0];
m1[1, 1] += m2[1, 1];
m1[1, 2] += m2[1, 2];

m1[2, 0] += m2[2, 0];
m1[2, 1] += m2[2, 1];
m1[2, 2] += m2[2, 2];

m1[3, 3] = 1;
return m1;
}
Matrix4x4 MatrixMultiple(Matrix4x4 m, float k)
{
m[0, 0] *= k;
m[0, 1] *= k;
m[0, 2] *= k;

m[1, 0] *= k;
m[1, 1] *= k;
m[1, 2] *= k;

m[2, 0] *= k;
m[2, 1] *= k;
m[2, 2] *= k;

m[3, 3] = 1;
return m;
}

}

更新于

请我喝[茶]~( ̄▽ ̄)~*

Natsuneko 微信支付

微信支付

Natsuneko 支付宝

支付宝

Natsuneko 贝宝

贝宝