-
Notifications
You must be signed in to change notification settings - Fork 240
/
dot_product_comp.py
202 lines (175 loc) · 8.34 KB
/
dot_product_comp.py
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
"""Definition of the Dot Product Component."""
import numpy as np
from openmdao.core.explicitcomponent import ExplicitComponent
class DotProductComp(ExplicitComponent):
"""
Computes a vectorized dot product.
math::
c = np.dot(a, b)
where a is of shape (vec_size, n)
b is of shape (vec_size, n)
c is of shape (vec_size,)
Vectors a and b must be of the same length, specified by the option 'length'.
Parameters
----------
**kwargs : dict of keyword arguments
Keyword arguments that will be mapped into the Component options.
Attributes
----------
_products : list
Cache the data provided during `add_product`
so everything can be saved until setup is called.
"""
def __init__(self, **kwargs):
"""
Initialize the Dot Product component.
"""
super().__init__(**kwargs)
self._products = []
opt = self.options
self.add_product(c_name=opt['c_name'], a_name=opt['a_name'], b_name=opt['b_name'],
c_units=opt['c_units'], a_units=opt['a_units'], b_units=opt['b_units'],
vec_size=opt['vec_size'], length=opt['length'])
self._no_check_partials = True
def initialize(self):
"""
Declare options.
"""
self.options.declare('vec_size', types=int, default=1,
desc='The number of points at which the dot product is computed')
self.options.declare('length', types=int, default=3,
desc='The length of vectors a and b')
self.options.declare('a_name', types=str, default='a',
desc='The variable name for input vector a.')
self.options.declare('b_name', types=str, default='b',
desc='The variable name for input vector b.')
self.options.declare('c_name', types=str, default='c',
desc='The variable name for output vector c.')
self.options.declare('a_units', types=str, default=None, allow_none=True,
desc='The units for vector a.')
self.options.declare('b_units', types=str, default=None, allow_none=True,
desc='The units for vector b.')
self.options.declare('c_units', types=str, default=None, allow_none=True,
desc='The units for vector c.')
def add_product(self, c_name, a_name='a', b_name='b', c_units=None, a_units=None, b_units=None,
vec_size=1, length=3):
"""
Add a new output product to the dot product component.
Parameters
----------
c_name : str
The name of the vector product output.
a_name : str
The name of the first vector input.
b_name : str
The name of the second input.
c_units : str or None
The units of the output.
a_units : str or None
The units of input a.
b_units : str or None
The units of input b.
vec_size : int
The number of points at which the dot vector product
should be computed simultaneously. The shape of
the output is (vec_size,).
length : int
The length of the vectors a and b. Their shapes are
(vec_size, length).
"""
self._products.append({
'a_name': a_name,
'b_name': b_name,
'c_name': c_name,
'a_units': a_units,
'b_units': b_units,
'c_units': c_units,
'vec_size': vec_size,
'length': length
})
# add inputs and outputs for all products
if self._static_mode:
var_rel2meta = self._static_var_rel2meta
var_rel_names = self._static_var_rel_names
else:
var_rel2meta = self._var_rel2meta
var_rel_names = self._var_rel_names
if c_name not in var_rel2meta:
self.add_output(name=c_name, shape=(vec_size,), units=c_units)
elif c_name in var_rel_names['input']:
raise NameError(f"{self.msginfo}: '{c_name}' specified as an output, "
"but it has already been defined as an input.")
else:
raise NameError(f"{self.msginfo}: Multiple definition of output '{c_name}'.")
if a_name not in var_rel2meta:
self.add_input(name=a_name, shape=(vec_size, length), units=a_units)
elif a_name in var_rel_names['output']:
raise NameError(f"{self.msginfo}: '{a_name}' specified as an input, "
"but it has already been defined as an output.")
else:
meta = var_rel2meta[a_name]
if a_units != meta['units']:
raise ValueError(f"{self.msginfo}: Conflicting units '{a_units}' specified "
f"for input '{a_name}', which has already been defined "
f"with units '{meta['units']}'.")
if vec_size != meta['shape'][0]:
raise ValueError(f"{self.msginfo}: Conflicting vec_size={vec_size} specified "
f"for input '{a_name}', which has already been defined "
f"with vec_size={meta['shape'][0]}.")
if length != meta['shape'][1]:
raise ValueError(f"{self.msginfo}: Conflicting length={length} specified "
f"for input '{a_name}', which has already been defined "
f"with length={meta['shape'][1]}.")
if b_name not in var_rel2meta:
self.add_input(name=b_name, shape=(vec_size, length), units=b_units)
elif b_name in var_rel_names['output']:
raise NameError(f"{self.msginfo}: '{b_name}' specified as an input, "
"but it has already been defined as an output.")
else:
meta = var_rel2meta[b_name]
if b_units != meta['units']:
raise ValueError(f"{self.msginfo}: Conflicting units '{b_units}' specified "
f"for input '{b_name}', which has already been defined "
f"with units '{meta['units']}'.")
if vec_size != meta['shape'][0]:
raise ValueError(f"{self.msginfo}: Conflicting vec_size={vec_size} specified "
f"for input '{b_name}', which has already been defined "
f"with vec_size={meta['shape'][0]}.")
if length != meta['shape'][1]:
raise ValueError(f"{self.msginfo}: Conflicting length={length} specified "
f"for input '{b_name}', which has already been defined "
f"with length={meta['shape'][1]}.")
row_idxs = np.repeat(np.arange(vec_size), length)
col_idxs = np.arange(vec_size * length)
self.declare_partials(of=c_name, wrt=a_name, rows=row_idxs, cols=col_idxs)
self.declare_partials(of=c_name, wrt=b_name, rows=row_idxs, cols=col_idxs)
def compute(self, inputs, outputs):
"""
Compute the dot product of inputs `a` and `b` using np.einsum.
Parameters
----------
inputs : Vector
Unscaled, dimensional input variables read via inputs[key].
outputs : Vector
Unscaled, dimensional output variables read via outputs[key].
"""
for product in self._products:
a = inputs[product['a_name']]
b = inputs[product['b_name']]
outputs[product['c_name']] = np.einsum('ni,ni->n', a, b)
def compute_partials(self, inputs, partials):
"""
Compute the sparse partials for the dot product w.r.t. the inputs.
Parameters
----------
inputs : Vector
Unscaled, dimensional input variables read via inputs[key].
partials : Jacobian
Sub-jac components written to partials[output_name, input_name].
"""
for product in self._products:
a = inputs[product['a_name']]
b = inputs[product['b_name']]
# Use the following for sparse partials
partials[product['c_name'], product['a_name']] = b.ravel()
partials[product['c_name'], product['b_name']] = a.ravel()