0

I have been creating a project to optimize an aircraft shape for lowest drag, and have been running into two problems, one occurs with a constraint applied and received the following error

 File "/home/name/Desktop/x1ac3opt.py", line 202, in <module>
    top.setup()

  File "/usr/local/lib/python2.7/dist-packages/openmdao/core/problem.py", line 498, in setup
    connections = self._setup_connections(params_dict, unknowns_dict)

  File "/usr/local/lib/python2.7/dist-packages/openmdao/core/problem.py", line 197, in _setup_connections
    connections = self.root._get_explicit_connections()

  File "/usr/local/lib/python2.7/dist-packages/openmdao/core/group.py", line 685, in _get_explicit_connections
    (src, tgt, tgt))

NameError: Source 'p.Sp1' cannot be connected to target 'con.Sp1': 'con.Sp1' does not exist.

and this occurs with the constraints quoted out `

File "/home/name/Desktop/x1ac3opt.py", line 63, in solve_nonlinear
    unknowns['Cdi'] = (324/((7750)*(m.pi)*(((Sp1+Sp2+Sp3+Sp4+Sp5)**2)/(Sp1*(Rc+Tc1)/2+Sp2*(Tc1+Tc2)/2+Sp3*(Tc2+Tc3)/2+Sp4*(Tc3+Tc4)/2+Sp5*(Tc4+Tc5)/2))))

  File "/usr/local/lib/python2.7/dist-packages/openmdao/core/vec_wrapper.py", line 435, in __setitem__
    self._dat[name].set(value)

  File "/usr/local/lib/python2.7/dist-packages/openmdao/core/vec_wrapper.py", line 313, in _set_scalar
    self.val[0] = value

ValueError: setting an array element with a sequence.

`

any ideas on why this is happening?

EDIT: here is the entireity of the code

`# For printing, use this import if you are running Python 2.x from future import print_function

import math as m

from openmdao.api import IndepVarComp, Component, Problem, Group, ExecComp, ScipyOptimizer, SqliteRecorder

class Outershell(Component): """Component containing Outershell."""

def __init__(self):
    super(Outershell, self).__init__()
    self.add_param('Sp1', val=23)      #Sec1Span
    self.add_param('Sp2', val=13)      #Sec2Span
    self.add_param('Sp3', val=20)      #Sec3Span
    self.add_param('Sp4', val=35)      #Sec4Span
    self.add_param('Sp5', val=35)      #Sec5Span

    self.add_param('Sw1', val=60)      #Sec1Sweep
    self.add_param('Sw2', val=60)      #Sec2sweep
    self.add_param('Sw3', val=50)      #Sec3sweep
    self.add_param('Sw4', val=37)      #Sec4sweep
    self.add_param('Sw5', val=35)      #Sec5sweep

    self.add_param('Rc', val=130)      #Sec1RC
    self.add_param('Tc1', val=90)      #Sec1TC
    self.add_param('Tc2', val=66)      #Sec2TC
    self.add_param('Tc3', val=42)     #Sec3TC
    self.add_param('Tc4', val=24)      #Sec4TC
    self.add_param('Tc5', val=10)      #Sec5TC

    self.add_output('Cdi', shape=1)     #Objective output as low as possible


def solve_nonlinear(self, params, unknowns, resids):

    #0.0324 and 0.775 are the squared Cl and the oswald efficiency number in the case that I can find a way to add in those values to the optimization problem

    Sp1 = params['Sp1']
    Sp2 = params['Sp2']
    Sp3 = params['Sp3']
    Sp4 = params['Sp4']
    Sp5 = params['Sp5']

    Sw1 = params['Sw1']
    Sw2 = params['Sw2']
    Sw3 = params['Sw3']
    Sw4 = params['Sw4']
    Sw5 = params['Sw5']

    Rc = params['Rc']
    Tc1 = params['Tc1']
    Tc2 = params['Tc2']
    Tc3 = params['Tc3']
    Tc4 = params['Tc4']
    Tc5 = params['Tc5']


    unknowns['Cdi'] = (324/((7750)*(m.pi)*(((Sp1+Sp2+Sp3+Sp4+Sp5)**2)/(Sp1*(Rc+Tc1)/2+Sp2*(Tc1+Tc2)/2+Sp3*(Tc2+Tc3)/2+Sp4*(Tc3+Tc4)/2+Sp5*(Tc4+Tc5)/2))))


def linearize(self, params, unknowns, resids):


    Sp1 = params['Sp1']
    Sp2 = params['Sp2']
    Sp3 = params['Sp3']
    Sp4 = params['Sp4']
    Sp5 = params['Sp5']

    sw1 = params['Sw1']
    sw2 = params['Sw2']
    sw3 = params['Sw3']
    sw4 = params['Sw4']
    sw5 = params['Sw5']

    Rc = params['Rc']
    Tc1 = params['Tc1']
    Tc2 = params['Tc2']
    Tc3 = params['Tc3']
    Tc4 = params['Tc4']
    Tc5 = params['Tc5']

    J ={}
    J['Cdi', 'Sp1']=unknowns['Cdi']/Sp1
    J['Cdi', 'Sp2']=unknowns['Cdi']/Sp2
    J['Cdi', 'Sp3']=unknowns['Cdi']/Sp3
    J['Cdi', 'Sp4']=unknowns['Cdi']/Sp4
    J['Cdi', 'Sp5']=unknowns['Cdi']/Sp5
    J['Cdi', 'Sw1']=unknowns['Cdi']/sw1
    J['Cdi', 'Sw2']=unknowns['Cdi']/sw2
    J['Cdi', 'Sw3']=unknowns['Cdi']/sw3
    J['Cdi', 'Sw4']=unknowns['Cdi']/sw4
    J['Cdi', 'Sw5']=unknowns['Cdi']/sw5
    J['Cdi', 'Tc1']=unknowns['Cdi']/Tc1
    J['Cdi', 'Tc2']=unknowns['Cdi']/Tc2
    J['Cdi', 'Tc3']=unknowns['Cdi']/Tc3
    J['Cdi', 'Tc4']=unknowns['Cdi']/Tc4
    J['Cdi', 'Tc5']=unknowns['Cdi']/Tc5
    J['Cdi', 'Rc']=unknowns['Cdi']/Rc

if __name__ == "__main__":

top = Problem()

root = top.root = Group()

root.add('p1', IndepVarComp('Sp1', 23))
root.add('p2', IndepVarComp('Sp2', 13))
root.add('p3', IndepVarComp('Sp3', 20))
root.add('p4', IndepVarComp('Sp4', 35))
root.add('p5', IndepVarComp('Sp5', 35))
root.add('p6', IndepVarComp('Sw1', 60))
root.add('p7', IndepVarComp('Sw2', 60))
root.add('p8', IndepVarComp('Sw3', 50))
root.add('p9', IndepVarComp('Sw4', 37))
root.add('p10', IndepVarComp('Sw5', 35))
root.add('p11', IndepVarComp('Tc1', 90))
root.add('p12', IndepVarComp('Tc2', 66))
root.add('p13', IndepVarComp('Tc3', 42))
root.add('p14', IndepVarComp('Tc4', 24))
root.add('p15', IndepVarComp('Tc5', 10))
root.add('p16', IndepVarComp('Rc', 130))
root.add('p', Outershell())


root.add('con', ExecComp('L = (15067/100000000)/(Sp1(Rc+Tc1)/2+Sp2(Tc1+Tc2)/2+Sp3(Tc2+Tc3)/2+Sp4(Tc3+Tc4)/2+Sp5(Tc4+Tc5)/2)'))
 #Cl=0.18 rho = 0.000737 v**2 = 810471.67 Area = ... 597.31762079


root.connect('p1.Sp1', 'p.Sp1')
root.connect('p2.Sp2', 'p.Sp2')
root.connect('p3.Sp3', 'p.Sp3')
root.connect('p4.Sp4', 'p.Sp4')
root.connect('p5.Sp5', 'p.Sp5')
root.connect('p6.Sw1', 'p.Sw1')
root.connect('p7.Sw2', 'p.Sw2')
root.connect('p8.Sw3', 'p.Sw3')
root.connect('p9.Sw4', 'p.Sw4')
root.connect('p10.Sw5', 'p.Sw5')
root.connect('p11.Tc1', 'p.Tc1')
root.connect('p12.Tc2', 'p.Tc2')
root.connect('p13.Tc3', 'p.Tc3')
root.connect('p14.Tc4', 'p.Tc4')
root.connect('p15.Tc5', 'p.Tc5')
root.connect('p16.Rc', 'p.Rc')

root.connect('p.Sp1', 'con.Sp1')
root.connect('p.Sp2', 'con.Sp2')
root.connect('p.Sp3', 'con.Sp3')
root.connect('p.Sp4', 'con.Sp4')
root.connect('p.Sp5', 'con.Sp5')
root.connect('p.Sw1', 'con.Sw1')
root.connect('p.Sw2', 'con.Sw2')
root.connect('p.Sw3', 'con.Sw3')
root.connect('p.Sw4', 'con.Sw4')
root.connect('p.Sw5', 'con.Sw5')
root.connect('p.Tc1', 'con.Tc1')
root.connect('p.Tc2', 'con.Tc2')
root.connect('p.Tc3', 'con.Tc3')
root.connect('p.Tc4', 'con.Tc4')
root.connect('p.Tc5', 'con.Tc5')
root.connect('p.Rc', 'con.Rc')



top.driver = ScipyOptimizer()
top.driver.options['optimizer'] = 'COBYLA'


top.driver.add_desvar('p1.Sp1', lower=13, upper=33)
top.driver.add_desvar('p2.Sp2', lower=3, upper=23)
top.driver.add_desvar('p3.Sp3', lower=10, upper=30)
top.driver.add_desvar('p4.Sp4', lower=25, upper=45)
top.driver.add_desvar('p5.Sp5', lower=25, upper=45)
top.driver.add_desvar('p6.Sw1', lower=55, upper=65)
top.driver.add_desvar('p7.Sw2', lower=55, upper=65)
top.driver.add_desvar('p8.Sw3', lower=45, upper=55)
top.driver.add_desvar('p9.Sw4', lower=32, upper=42)
top.driver.add_desvar('p10.Sw5', lower=30, upper=40)
top.driver.add_desvar('p11.Tc1', lower=80, upper=100)
top.driver.add_desvar('p12.Tc2', lower=56, upper=76)
top.driver.add_desvar('p13.Tc3', lower=37, upper=45)
top.driver.add_desvar('p14.Tc4', lower=19, upper=29)
top.driver.add_desvar('p15.Tc5', lower=5, upper=15)
top.driver.add_objective('p.Cdi')
top.driver.add_constraint('con.L', lower=220000, upper=240000)


recorder = SqliteRecorder('Outershell')
recorder.options['record_params'] = True
recorder.options['record_metadata'] = True
top.driver.add_recorder(recorder)

top.setup()
top.run()
top.cleanup()  # this closes all recorders


print('\n')
print('Minimum of %f found at: ' % (top['p.Cdi']))
print('\n')
print('Lift produced is: %f ' % (top['con.L']))
print('SP1 = %f' % (top['p.Sp1']))
print('\n')
print('SP2 = %f' % (top['p.Sp2']))
print('\n')
print('SP3 = %f' % (top['p.Sp3']))
print('\n')
print('SP4 = %f' % (top['p.Sp4']))
print('\n')
print('SP5 = %f' % (top['p.Sp5']))
print('\n')
print('SW1 = %f' % (top['p.Sw1']))
print('\n')
print('SW2 = %f' % (top['p.Sw2']))
print('\n')
print('SW3 = %f' % (top['p.Sw3']))
print('\n')
print('SW4 = %f' % (top['p.Sw4']))
print('\n')
print('SW5 = %f' % (top['p.Sw5']))
print('\n')
print('Rc = %f' % (top['p.Rc']))
print('\n')
print('TC1 = %f' % (top['p.Tc1']))
print('\n')
print('TC2 = %f' % (top['p.Tc2']))
print('\n')
print('TC3 = %f' % (top['p.Tc3']))
print('\n')
print('TC4 = %f' % (top['p.Tc4']))
print('\n')
print('TC5 = %f' % (top['p.Tc5']))
print('\n')

`

2 Answers 2

1

For the first error:

L = (15067/100000000)/(Sp1(Rc+Tc1)/2+Sp2(Tc1+Tc2)/2+Sp3(Tc2+Tc3)/2+Sp4(Tc3+Tc4)/2+Sp5(Tc4+Tc5)/2)')

I think you need to put the multiplications in explicitly, so

L = (15067/100000000)/(Sp1*(Rc+Tc1)/2+Sp2*(Tc1+Tc2)/2+Sp3*(Tc2+Tc3)/2+Sp4*(Tc3+Tc4)/2+Sp5*(Tc4+Tc5)/2)')
Sign up to request clarification or add additional context in comments.

5 Comments

That worked, but it seems as one of the previous errors has changed slightly, and now reads ` File "/usr/local/lib/python2.7/dist-packages/openmdao/core/_checks.py", line 97, in _check_types_match type(tval), _both_names(tgt, to_prom_name))) TypeError: Type <type 'int'> of source 'p16.Rc' must be the same as type <type 'float'> of target 'con.Rc'.
you also want to make your variables float type: self.add_param('Sp1', val=23.)
As a quick question, I am now getting results, however they are not within the bounds I specified i put in top.driver.add_desvar('p15.Tc5', lower=5., upper=15.) and it is outputting a negative value. could this be a result of the use of root.add('p15', IndepVarComp('Tc5', 1.)) and as part of that, what is the purpose of the value after the name of the parameter I inputted?
The value in the indepvarcomp is the initial value for the variable (p15.Tc5 here). I am not sure why Cobyla is forcing it beyond the bounds, but you may want to turn on some more verbose output to see what it is doing.
Interestingly enough, changing the optimizer to Nedler-Mead seemed to fix the errors...
0

For the first error:

without seeing your actual model I can't diagnose why this is happening. But you're trying to connect (what I am guessing to be) the output of an IndepVarComp to the input of some other component (maybe an exec comp). You've probably promoted the source, the target, or both of them though. So you're not referencing them with names that are correct considering promotions.

If you've already promoted both variables, since both are names Sp1 then are are automatically connected. If you've only promoted one of them, then you need to account for that in the connect statement. For example:

self.connect('Sp1', 'con.Sp1')

For your second error: There is some kind of a size mismatch between Cdi and the equation that you have to compute the value. It looks like that equation might be scalar, but I can't tell for sure because I don't know what any of the intermediate variables are. Regardless, one side or the other of that assignment is not the correct size. You could debug that by adding some print statements to see what size OpenMDAO things things are.

print(unknowns['Cdi'])
print((324/((7750)*(m.pi)*(((Sp1+Sp2+Sp3+Sp4+Sp5)**2)/(Sp1*(Rc+Tc1)/2+Sp2*(Tc1+Tc2)/2+Sp3*(Tc2+Tc3)/2+Sp4*(Tc3+Tc4)/2+Sp5*(Tc4+Tc5)/2))))))

2 Comments

missing a link or something? I don't see any new code
Just added it to original question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.