Reputation: 77
Hello everyone, please forgive me if I make a few mistakes, I don't speak english that well.
I get a TypeError while trying to add a constraint to a Group in OpenMdao. I declared a Component :
class ShipOpenmdao(Component):
def __init__(self, **d):
super(ShipOpenmdao, self).__init__()
[...]
self.add_param('x', val=np.zeros(3))
and then a Group :
class Point(Group):
def __init__(self, **d):
super(Point, self).__init__()
self.add('p', IndepVarComp('x', np.zeros(3)))
self.add('s', ShipOpenmdao(**d))
self.connect('p.x', 's.x')
ship = self.s
for i in range(len(ship.Azimuth)):
n = len(ship.Tunnel) + len(ship.Thruster) + 2*i
self.add('con%d' % i,
ExecComp('c{0} = x[{1}]*x[{1}] + x[{2}]*x[{2}]'.format(i, n, n+1)))
self.connect('p.x', 'con%d.x' % i)
And i get this:
TypeError: Type ('numpy.ndarray') of source 'p.x' must be the same as type ('float') of target 'con0.x'.
I've been trying to find what is wrong for hours, but i don't see why my 'x' would be considered as a float ... I followed the way the Paraboloid tutorial declares constraints, and the Sellar Problem tutorial shows that a ndarray can be used inside the string to declare the constraint.
Does anyone see what is wrong in my code ?
Thank you by advance
Gaël
Upvotes: 1
Views: 42
Reputation: 2202
You are connecting the source 'p.x' to the target 'con0.x' (and subsequent constraints), and x is an ndarray, so the target must also be an ndarray. You can specify this on the Execomp by passing an additional keyword argument where the keyword is the target input's name. If we just give it the same size as 'p.x':
self.add('con%d' % i,
ExecComp('c{0} = x[{1}]*x[{1}] + x[{2}]*x[{2}]'.format(i, n, n+1),
x=np.zeros(3)))
then it makes it through setup without an error.
So yeah, ExecComp assumes anything coming in is a float, so you have to explicitly size any ndarrays.
Upvotes: 1