-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbeam_comps.py
More file actions
248 lines (179 loc) · 7.95 KB
/
beam_comps.py
File metadata and controls
248 lines (179 loc) · 7.95 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
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
from __future__ import division
from six.moves import range
import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.linalg import splu
import openmdao.api as om
class MomentOfInertiaComp(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_elements', types=int)
self.options.declare('b')
def setup(self):
num_elements = self.options['num_elements']
self.add_input('h', shape=num_elements)
self.add_output('I', shape=num_elements)
rows = np.arange(num_elements)
cols = np.arange(num_elements)
self.declare_partials('I', 'h', rows=rows, cols=cols)
def compute(self, inputs, outputs):
b = self.options['b']
outputs['I'] = 1./12. * b * inputs['h'] ** 3
def compute_partials(self, inputs, partials):
b = self.options['b']
partials['I', 'h'] = 1./4. * b * inputs['h'] ** 2
class LocalStiffnessMatrixComp(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_elements', types=int)
self.options.declare('E')
self.options.declare('L')
def setup(self):
num_elements = self.options['num_elements']
E = self.options['E']
L = self.options['L']
self.add_input('I', shape=num_elements)
self.add_output('K_local', shape=(num_elements, 4, 4))
L0 = L / num_elements
coeffs = np.empty((4, 4))
coeffs[0, :] = [12, 6 * L0, -12, 6 * L0]
coeffs[1, :] = [6 * L0, 4 * L0 ** 2, -6 * L0, 2 * L0 ** 2]
coeffs[2, :] = [-12, -6 * L0, 12, -6 * L0]
coeffs[3, :] = [6 * L0, 2 * L0 ** 2, -6 * L0, 4 * L0 ** 2]
coeffs *= E / L0 ** 3
self.mtx = mtx = np.zeros((num_elements, 4, 4, num_elements))
for ind in range(num_elements):
self.mtx[ind, :, :, ind] = coeffs
self.declare_partials('K_local', 'I',
val=self.mtx.reshape(16 * num_elements, num_elements))
def compute(self, inputs, outputs):
num_elements = self.options['num_elements']
outputs['K_local'] = 0
for ind in range(num_elements):
outputs['K_local'][ind, :, :] = self.mtx[ind, :, :, ind] * inputs['I'][ind]
##########################################
# NOTE: This component is Implicit!!!!!!!!
##########################################
class FEM(om.ImplicitComponent):
def initialize(self):
self.options.declare('num_elements', types=int)
self.options.declare('force_vector', types=np.ndarray)
def setup(self):
num_elements = self.options['num_elements']
num_nodes = num_elements + 1
size = 2 * num_nodes + 2
self.add_input('K_local', shape=(num_elements, 4, 4))
self.add_output('u', shape=size)
cols = np.arange(16*num_elements)
rows = np.repeat(np.arange(4), 4)
rows = np.tile(rows, num_elements) + np.repeat(np.arange(num_elements), 16) * 2
self.declare_partials('u', 'K_local', rows=rows, cols=cols)
self.declare_partials('u', 'u')
def apply_nonlinear(self, inputs, outputs, residuals):
force_vector = np.concatenate([self.options['force_vector'], np.zeros(2)])
self.K = self.assemble_CSC_K(inputs)
residuals['u'] = self.K.dot(outputs['u']) - force_vector
def solve_nonlinear(self, inputs, outputs):
# NOTE: Although this FEM is linear, you still solve it in the `solve_nonlinear` method!
# This method is optional, but useful when you have codes that have their own
# customized nonlinear solvers
force_vector = np.concatenate([self.options['force_vector'], np.zeros(2)])
self.K = self.assemble_CSC_K(inputs)
self.lu = splu(self.K)
outputs['u'] = self.lu.solve(force_vector)
def linearize(self, inputs, outputs, jacobian):
num_elements = self.options['num_elements']
self.K = self.assemble_CSC_K(inputs)
self.lu = splu(self.K)
i_elem = np.tile(np.arange(4), 4)
i_d = np.tile(i_elem, num_elements) + np.repeat(np.arange(num_elements), 16) * 2
jacobian['u', 'K_local'] = outputs['u'][i_d]
jacobian['u', 'u'] = self.K.toarray()
# NOTE: this is an advanced OpenMDAO API method, that lets a component handle its own
# linear solve, if it can. Its optional, but very useful if your code has highly
# specialized linear solvers (like CFD and real FEA codes)
def solve_linear(self, d_outputs, d_residuals, mode):
if mode == 'fwd':
d_outputs['u'] = self.lu.solve(d_residuals['u'])
else:
d_residuals['u'] = self.lu.solve(d_outputs['u'])
def assemble_CSC_K(self, inputs):
"""
Assemble the stiffness matrix in sparse CSC format.
Returns
-------
ndarray
Stiffness matrix as dense ndarray.
"""
num_elements = self.options['num_elements']
num_nodes = num_elements + 1
num_entry = num_elements * 12 + 4
ndim = num_entry + 4
data = np.zeros((ndim, ), dtype=inputs._data.dtype)
cols = np.empty((ndim, ))
rows = np.empty((ndim, ))
# First element.
data[:16] = inputs['K_local'][0, :, :].flat
cols[:16] = np.tile(np.arange(4), 4)
rows[:16] = np.repeat(np.arange(4), 4)
j = 16
for ind in range(1, num_elements):
ind1 = 2 * ind
K = inputs['K_local'][ind, :, :]
# NW quadrant gets summed with previous connected element.
data[j-6:j-4] += K[0, :2]
data[j-2:j] += K[1, :2]
# NE quadrant
data[j:j+4] = K[:2, 2:].flat
rows[j:j+4] = np.array([ind1, ind1, ind1 + 1, ind1 + 1])
cols[j:j+4] = np.array([ind1 + 2, ind1 + 3, ind1 + 2, ind1 + 3])
# SE and SW quadrants together
data[j+4:j+12] = K[2:, :].flat
rows[j+4:j+12] = np.repeat(np.arange(ind1 + 2, ind1 + 4), 4)
cols[j+4:j+12] = np.tile(np.arange(ind1, ind1 + 4), 2)
j += 12
# this implements the clamped boundary condition on the left side of the beam
# using a weak formulation for the BC
data[-4:] = 1.0
rows[-4] = 2 * num_nodes
rows[-3] = 2 * num_nodes + 1
rows[-2] = 0.0
rows[-1] = 1.0
cols[-4] = 0.0
cols[-3] = 1.0
cols[-2] = 2 * num_nodes
cols[-1] = 2 * num_nodes + 1
n_K = 2 * num_nodes + 2
return coo_matrix((data, (rows, cols)), shape=(n_K, n_K)).tocsc()
class ComplianceComp(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_elements', types=int)
self.options.declare('force_vector', types=np.ndarray)
def setup(self):
num_elements = self.options['num_elements']
num_nodes = num_elements + 1
force_vector = self.options['force_vector']
self.add_input('displacements', shape=2 * num_nodes)
self.add_output('compliance')
self.declare_partials('compliance', 'displacements',
val=force_vector.reshape((1, 2 * num_nodes)))
def compute(self, inputs, outputs):
force_vector = self.options['force_vector']
outputs['compliance'] = np.dot(force_vector, inputs['displacements'])
class VolumeComp(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_elements', types=int)
self.options.declare('b', default=1.)
self.options.declare('L')
def setup(self):
num_elements = self.options['num_elements']
b = self.options['b']
L = self.options['L']
L0 = L / num_elements
self.add_input('h', shape=num_elements)
self.add_output('volume')
self.declare_partials('volume', 'h', val=b * L0)
def compute(self, inputs, outputs):
num_elements = self.options['num_elements']
b = self.options['b']
L = self.options['L']
L0 = L / num_elements
outputs['volume'] = np.sum(inputs['h'] * b * L0)