-
Notifications
You must be signed in to change notification settings - Fork 1
/
E.bas
240 lines (227 loc) · 7.51 KB
/
E.bas
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
' reset area
Sub reset_area()
Dim SheetName As String
SheetName = "Sheet1"
Sheets(SheetName).Range("L2", "L13").ClearContents
Sheets(SheetName).Range("M2", "M13").ClearContents
End Sub
Sub calculate()
Worksheets("Sheet1").Activate
E = Cells(7, 2)
nu = Cells(8, 2)
t = Cells(9, 2)
Nodes = 6
one = Array(Cells(2, 2), Cells(2, 3))
three = Array(Cells(3, 2), Cells(3, 3))
two = Array((one(0) + three(0)) / 2, (one(1) + three(1)) / 2)
four = Array(Cells(4, 2), Cells(4, 3))
six = Array(Cells(5, 2), Cells(5, 3))
five = Array((four(0) + six(0)) / 2, (four(1) + six(1)) / 2)
cordinates = Array(one, two, three, four, five, six) ' 1~6の座標
sections = Array(Array(1, 2, 5), Array(2, 3, 6), Array(1, 5, 4), Array(2, 6, 5))
'Call print_array(sections, "sections")
'Call print_array(cordinates, "c")
coefficient = E * t / 4 / (1 - nu * nu)
nu_stress = Array(Array(1, nu, 0), Array(nu, 1, 0), Array(0, 0, (1 - nu) / 2))
' 全体は12×13ですね
main = create_matrix(12, 13, 0)
For i = 0 To UBound(main)
main(i)(UBound(main(0))) = Cells(2 + i, 11)
Next i
Dim k_() As Variant
ReDim k_(4) ' magic number
' それぞれのkを作ります
' それぞれのA(面積)
' 1/2A はBの2回分
' e/1-v ^2はDから。
' そこから Bt D B をそれぞれ用意。βとか使って
For i = 0 To 3 ' k作るために4周
Dim tmp()
x1 = cordinates(sections(i)(0) - 1)(0)
y1 = cordinates(sections(i)(0) - 1)(1)
x2 = cordinates(sections(i)(1) - 1)(0)
y2 = cordinates(sections(i)(1) - 1)(1)
x3 = cordinates(sections(i)(2) - 1)(0)
y3 = cordinates(sections(i)(2) - 1)(1)
A = (x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) / 2
B1 = Array(y2 - y3, y3 - y1, y1 - y2, 0, 0, 0)
B2 = Array(0, 0, 0, x3 - x2, x1 - x3, x2 - x1)
B3 = Array(x3 - x2, x1 - x3, x2 - x1, y2 - y3, y3 - y1, y1 - y2)
B = Array(B1, B2, B3)
Bt = matrix_t(B)
tmp = matrix_cross(matrix_cross(Bt, nu_stress), B)
tmp = multiply_each(tmp, 1 / A)
tmp = multiply_each(tmp, coefficient)
'Call print_array(tmp, "k_" & i)
k_(i) = tmp
Next i
'Call print_array(main, "hello")
' それを全体係数行列にいれます
For i = 0 To 3 'ubound(k)
For j = 0 To 2 ' 3行 * 分
For k = 0 To 2 ' 一つずつ × 2
'Debug.Print (row & col & " / " & row + 6 & col + 6)
row = sections(i)(j) - 1
col = sections(i)(k) - 1
main(row)(col) = main(row)(col) + k_(i)(j)(k)
main(row + 6)(col) = main(row + 6)(col) + k_(i)(j + 3)(k)
main(row)(col + 6) = main(row)(col + 6) + k_(i)(j)(k + 3)
main(row + 6)(col + 6) = main(row + 6)(col + 6) + k_(i)(j + 3)(k + 3)
Next k
Next j
Next i
' debug
For i = 0 To UBound(main)
For j = 0 To UBound(main(0))
'Cells(j + 20)(i + 20) = main(i)(j)
Next j
Next i
Call print_array(main, "hello")
big = shrink_array(main)
' 掃きだします
main = shrink_array(shrink_array(shrink_array(main, 6, 6), 3, 3), 0, 0)
Call forward_elimination(main)
Call backward_substitution(main)
Call print_array(main, "last")
For i = 0 To UBound(main)
For j = 0 To UBound(main(0))
'Cells(j + 20)(i + 34) = main(i)(j)
Next j
Next i
Cells(2, 12) = 0
Cells(3, 12) = main(0)(9)
Cells(4, 12) = main(1)(9)
Cells(5, 12) = 0
Cells(6, 12) = main(2)(9)
Cells(7, 12) = main(3)(9)
Cells(8, 12) = 0
Cells(9, 12) = main(4)(9)
Cells(10, 12) = main(5)(9)
Cells(11, 12) = main(6)(9)
Cells(12, 12) = main(7)(9)
Cells(13, 12) = main(8)(9)
For i = 0 To 11
Dim num
num = 0
For j = 0 To 11
num = num + big(i)(j) * Cells(2 + j, 12)
Next j
Debug.Print (Round(num, 3))
Cells(2 + i, 13) = Round(num, 3)
Next i
End Sub
' Create a matrix of arbitrary size (jug array)
Function create_matrix(row_size, col_size, Optional initial = "")
Dim ans, row As Variant
ans = Array()
ReDim ans(row_size - 1)
For i = 0 To row_size - 1
row = Array() ' new array Instance
ReDim row(col_size - 1)
For j = 0 To col_size - 1
row(j) = initial
Next j
ans(i) = row
Next i
create_matrix = ans
End Function
' multiply matrixs
Function matrix_cross(m1, m2)
ans = create_matrix(UBound(m1) + 1, UBound(m2(0)) + 1)
For i = 0 To UBound(ans)
For j = 0 To UBound(ans(0))
sum_ = 0
For k = 0 To UBound(m1(0))
sum_ = sum_ + m1(i)(k) * m2(k)(j)
Next k
ans(i)(j) = sum_
Next j
Next i
matrix_cross = ans
End Function
' transpose matrix
Function matrix_t(m)
ans = create_matrix(UBound(m(0)) + 1, UBound(m) + 1)
For i = 0 To UBound(m(0))
For j = 0 To UBound(m)
ans(i)(j) = m(j)(i)
Next j
Next i
matrix_t = ans
End Function
Function multiply_each(jug_array, number)
new_array = create_matrix(UBound(jug_array) + 1, UBound(jug_array(0)) + 1)
For i = 0 To UBound(jug_array)
For j = 0 To UBound(jug_array(0))
new_array(i)(j) = jug_array(i)(j) * number
Next j
Next i
multiply_each = new_array
End Function
Sub print_array(arr, Optional msg As String)
Debug.Print ("--")
For i = 0 To UBound(arr)
tmp = ""
For j = 0 To UBound(arr(0))
'tmp = tmp & arr(i)(j) & " "
tmp = tmp & Round(arr(i)(j), 6) & " "
Next j
Debug.Print (tmp)
Next i
Debug.Print ("--- " & Now & " " & msg & " ---")
End Sub
Sub forward_elimination(arr)
For i = 0 To UBound(arr)
Key = arr(i)(i)
For j = 0 To UBound(arr(0)) - i
' divide each element with key
arr(i)(j + i) = arr(i)(j + i) / Key
Next j
For k = i + 1 To UBound(arr)
num = arr(k)(i)
For L = i To UBound(arr(0))
arr(k)(L) = arr(k)(L) - arr(i)(L) * num
Next L
Next k
Next i
End Sub
Sub backward_substitution(arr)
For i = UBound(arr) To 1 Step -1
For j = i To 1 Step -1
Key = arr(j - 1)(i)
For k = UBound(arr(0)) To i Step -1
arr(j - 1)(k) = arr(j - 1)(k) - arr(i)(k) * Key
Next k
Next j
Next i
End Sub
Function shrink_array(arr_, Optional row As Integer = -1, Optional col As Integer = -1)
arr = arr_
new_array_row = UBound(arr)
new_array_col = UBound(arr(0))
' row詰める
If row >= 0 Then
new_array_row = new_array_row - 1
For i = 0 To UBound(arr(0))
For j = row To UBound(arr) - 1
arr(j)(i) = arr(j + 1)(i)
Next j
Next i
End If
' col 詰める
If col >= 0 Then
new_array_col = new_array_col - 1
For i = 0 To UBound(arr)
For j = col To UBound(arr(0)) - 1
arr(i)(j) = arr(i)(j + 1)
Next j
Next i
End If
new_array = create_matrix(new_array_row + 1, new_array_col + 1)
For i = 0 To new_array_row
For j = 0 To new_array_col
new_array(i)(j) = arr(i)(j)
Next j
Next i
shrink_array = new_array
End Function