Repository navigation
Expand file tree
/
Copy pathcalc_Spring_for3dim.c
More file actions
176 lines (159 loc) · 6.72 KB
/
Copy pathcalc_Spring_for3dim.c
File metadata and controls
176 lines (159 loc) · 6.72 KB
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
#include "Ce_MPS.h"
void calc_Spring_for3dim(void){
int i, j, k, l, s, s_old, t;
double xij, yij, zij, distance;
double n1[3], n2[3], a[3], b[3], c[3], N1, N2, theta;
int p[4], q[4], tmp, pa[3], pb[3];
// spring for streching
for(i=0; i<num_t; i++){
if((pcl[tri[i].p1].isIgn == YES) || (pcl[tri[i].p2].isIgn == YES) || (pcl[tri[i].p3].isIgn == YES)) continue;
p[0] = pcl[tri[i].p1].num;
p[1] = pcl[tri[i].p2].num;
p[2] = pcl[tri[i].p3].num;
p[3] = pcl[tri[i].p1].num;
for(j=0; j<2; j++){
// calculate normal vector (xij, yij, zij)
distance = calDistance(p[j], p[j+1]);
xij = (Position[p[j+1]*3+X] - Position[p[j]*3+X]) / distance;
yij = (Position[p[j+1]*3+Y] - Position[p[j]*3+Y]) / distance;
zij = (Position[p[j+1]*3+Z] - Position[p[j]*3+Z]) / distance;
Acceleration[p[j]*3+X] += SPRING_CONSTANT * (distance - meanWallDistance) * xij / 2;
Acceleration[p[j]*3+Y] += SPRING_CONSTANT * (distance - meanWallDistance) * yij / 2;
Acceleration[p[j]*3+Z] += SPRING_CONSTANT * (distance - meanWallDistance) * zij / 2;
Acceleration[p[j+1]*3+X] -= SPRING_CONSTANT * (distance - meanWallDistance) * xij / 2;
Acceleration[p[j+1]*3+Y] -= SPRING_CONSTANT * (distance - meanWallDistance) * yij / 2;
Acceleration[p[j+1]*3+Z] -= SPRING_CONSTANT * (distance - meanWallDistance) * zij / 2;
}
}
// spring for bending
for(i=0; i<num_t; i++){
// 20171106
// adjacentTriangleを用いて隣接三角形間のバネ計算を書く
for(j=0; j<3; j++){
s = 0;
t = 0;
if(adjacentTriangle[i][j] == -1) continue;
pa[0] = pcl[tri[i].p1].num;
pa[1] = pcl[tri[i].p2].num;
pa[2] = pcl[tri[i].p3].num;
pb[0] = pcl[tri[adjacentTriangle[i][j]].p1].num;
pb[1] = pcl[tri[adjacentTriangle[i][j]].p2].num;
pb[2] = pcl[tri[adjacentTriangle[i][j]].p3].num;
// acquire the pair particles as p[0], p[1], and the others q[0], q[1]
for(k=0; k<3; k++){
s_old = s;
for(l=0; l<3; l++){
if(pa[k] == pb[l]){
p[s] = pa[k];
s++;
break;
}
}
if(s == s_old){
q[t] = pa[k];
t++;
}
}
s = 0;
for(k=0; k<3; k++){
s_old = s;
for(l=0; l<3; l++){
if(pb[k] == pa[l]){
s++;
break;
}
}
if(s == s_old){
q[t] = pb[k];
}
}
// 粒子iから各粒子への位置ベクトルa,b,cを求める
a[X] = Position[p[1]*3+X] - Position[p[0]*3+X];
a[Y] = Position[p[1]*3+Y] - Position[p[0]*3+Y];
a[Z] = Position[p[1]*3+Z] - Position[p[0]*3+Z];
b[X] = Position[q[0]*3+X] - Position[p[0]*3+X];
b[Y] = Position[q[0]*3+Y] - Position[p[0]*3+Y];
b[Z] = Position[q[0]*3+Z] - Position[p[0]*3+Z];
c[X] = Position[q[1]*3+X] - Position[p[0]*3+X];
c[Y] = Position[q[1]*3+Y] - Position[p[0]*3+Y];
c[Z] = Position[q[1]*3+Z] - Position[p[0]*3+Z];
// 外積を用いて法線ベクトルn1,n2を求め正規化
n1[X] = a[Y]*b[Z] - a[Z]*b[Y];
n1[Y] = a[Z]*b[X] - a[X]*b[Z];
n1[Z] = a[X]*b[Y] - a[Y]*b[X];
n2[X] = - a[Y]*c[Z] + a[Z]*c[Y];
n2[Y] = - a[Z]*c[X] + a[X]*c[Z];
n2[Z] = - a[X]*c[Y] + a[Y]*c[X];
N1 = sqrt(n1[X]*n1[X] + n1[Y]*n1[Y] + n1[Z]*n1[Z]);
N2 = sqrt(n2[X]*n2[X] + n2[Y]*n2[Y] + n2[Z]*n2[Z]);
if(N1 != 0){
n1[X] /= N1;
n1[Y] /= N1;
n1[Z] /= N1;
}
if(N2 != 0){
n2[X] /= N2;
n2[Y] /= N2;
n2[Z] /= N2;
}
// n1とn2の内積を利用して2つの三角形の成す角thetaを求める
theta = M_PI - acos(fmin(fmax(n1[X]*n2[X] + n1[Y]*n2[Y] + n1[Z]*n2[Z], -1), 1));
if(theta == 0){
tmp = p[1];
p[1] = p[2];
p[2] = tmp;
// 粒子iから各粒子への位置ベクトルa,b,cを求める
a[X] = Position[p[1]*3+X] - Position[p[0]*3+X];
a[Y] = Position[p[1]*3+Y] - Position[p[0]*3+Y];
a[Z] = Position[p[1]*3+Z] - Position[p[0]*3+Z];
b[X] = Position[q[0]*3+X] - Position[p[0]*3+X];
b[Y] = Position[q[0]*3+Y] - Position[p[0]*3+Y];
b[Z] = Position[q[0]*3+Z] - Position[p[0]*3+Z];
c[X] = Position[q[1]*3+X] - Position[p[0]*3+X];
c[Y] = Position[q[1]*3+Y] - Position[p[0]*3+Y];
c[Z] = Position[q[1]*3+Z] - Position[p[0]*3+Z];
// 外積を用いて法線ベクトルn1,n2を求め正規化
n1[X] = a[Y]*b[Z] - a[Z]*b[Y];
n1[Y] = a[Z]*b[X] - a[X]*b[Z];
n1[Z] = a[X]*b[Y] - a[Y]*b[X];
n2[X] = a[Y]*c[Z] - a[Z]*c[Y];
n2[Y] = a[Z]*c[X] - a[X]*c[Z];
n2[Z] = a[X]*c[Y] - a[Y]*c[X];
N1 = sqrt(n1[X]*n1[X] + n1[Y]*n1[Y] + n1[Z]*n1[Z]);
N2 = sqrt(n2[X]*n2[X] + n2[Y]*n2[Y] + n2[Z]*n2[Z]);
if(N1 != 0){
n1[X] /= N1;
n1[Y] /= N1;
n1[Z] /= N1;
}
if(N2 != 0){
n2[X] /= N2;
n2[Y] /= N2;
n2[Z] /= N2;
}
// n1とn2の内積を利用して2つの三角形の成す角thetaを求める
theta = M_PI - acos(fmin(fmax(n1[X]*n2[X] + n1[Y]*n2[Y] + n1[Z]*n2[Z], -1), 1));
/* printf("theta(re) = %e\n", theta); */
/* printf("a = (%e, %e, %e)\n", a[X], a[Y], a[Z]); */
/* printf("b = (%e, %e, %e)\n", b[X], b[Y], b[Z]); */
/* printf("c = (%e, %e, %e)\n", c[X], c[Y], c[Z]); */
/* printf("n1 = (%e, %e, %e)\n", n1[X], n1[Y], n1[Z]); */
/* printf("n2 = (%e, %e, %e)\n", n2[X], n2[Y], n2[Z]); */
/* printf("\n"); */
}
// 力を求める
Acceleration[q[0]*3+X] += 0.5 * BENDING_CONSTANT * tan((M_PI - theta)/2) * n1[X];
Acceleration[q[0]*3+Y] += 0.5 * BENDING_CONSTANT * tan((M_PI - theta)/2) * n1[Y];
Acceleration[q[0]*3+Z] += 0.5 * BENDING_CONSTANT * tan((M_PI - theta)/2) * n1[Z];
Acceleration[q[1]*3+X] += 0.5 * BENDING_CONSTANT * tan((M_PI - theta)/2) * n2[X];
Acceleration[q[1]*3+Y] += 0.5 * BENDING_CONSTANT * tan((M_PI - theta)/2) * n2[Y];
Acceleration[q[1]*3+Z] += 0.5 * BENDING_CONSTANT * tan((M_PI - theta)/2) * n2[Z];
Acceleration[p[0]*3+X] -= 0.5 * (BENDING_CONSTANT * tan((M_PI - theta)/2) * (n1[X] + n2[X])) / 2;
Acceleration[p[0]*3+Y] -= 0.5 * (BENDING_CONSTANT * tan((M_PI - theta)/2) * (n1[Y] + n2[Y])) / 2;
Acceleration[p[0]*3+Z] -= 0.5 * (BENDING_CONSTANT * tan((M_PI - theta)/2) * (n1[Z] + n2[Z])) / 2;
Acceleration[p[1]*3+X] -= 0.5 * (BENDING_CONSTANT * tan((M_PI - theta)/2) * (n1[X] + n2[X])) / 2;
Acceleration[p[1]*3+Y] -= 0.5 * (BENDING_CONSTANT * tan((M_PI - theta)/2) * (n1[Y] + n2[Y])) / 2;
Acceleration[p[1]*3+Z] -= 0.5 * (BENDING_CONSTANT * tan((M_PI - theta)/2) * (n1[Z] + n2[Z])) / 2;
}
}
}