0

I am seeing some dimension errors from the linearize method. Specifically,

File "/usr/local/lib/python2.7/site-packages/openmdao/core/system.py", line 726, in _apply_linear_jac
    arg_vec[param] += J.T.dot(dresids._flat(unknown)).reshape(shape)
ValueError: shapes (6,108) and (36,) not aligned: 108 (dim 1) != 36 (dim 0)

I have some 2d arrays that define a part of the Jacobian for a single output vector wrt all of the input vectors. I have not been able to pinpoint the exact cause of the error since there are several cases that meet the shapes given. I am wondering if the problem may be solved by flattening the array myself.

Any other thoughts on how to troubleshoot this error would be appreciated. These gradients worked fine in pre-1.0.

The full error message is below:

Traceback (most recent call last):
  File "/Users/user/directory/exampleOptimization.py", line 82, in <module>
    prob.run()
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/problem.py", line 789, in run
    self.driver.run(self)
  File "/usr/local/lib/python2.7/site-packages/openmdao/drivers/scipy_optimizer.py", line 191, in run
    options=self.opt_settings)
  File "/usr/local/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 452, in minimize
    constraints, callback=callback, **options)
  File "/usr/local/lib/python2.7/site-packages/scipy/optimize/slsqp.py", line 383, in _minimize_slsqp
    g = append(fprime(x),0.0)
  File "/usr/local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 285, in function_wrapper
    return function(*(wrapper_args + args))
  File "/usr/local/lib/python2.7/site-packages/openmdao/drivers/scipy_optimizer.py", line 306, in _gradfunc
    return_format='array')
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/driver.py", line 666, in calc_gradient
    sparsity=sparsity)
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/problem.py", line 899, in calc_gradient
    sparsity=sparsity)
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/problem.py", line 1261, in _calc_gradient_ln_solver
    dx_mat = root.ln_solver.solve(rhs, root, mode)
  File "/usr/local/lib/python2.7/site-packages/openmdao/solvers/scipy_gmres.py", line 104, in solve
    callback=self.monitor)
  File "<string>", line 2, in gmres
  File "/usr/local/lib/python2.7/site-packages/scipy/sparse/linalg/isolve/iterative.py", line 85, in non_reentrant
    return func(*a, **kw)
  File "/usr/local/lib/python2.7/site-packages/scipy/sparse/linalg/isolve/iterative.py", line 478, in gmres
    work[slice2] += sclr1*matvec(work[slice1])
  File "/usr/local/lib/python2.7/site-packages/scipy/sparse/linalg/interface.py", line 220, in matvec
    y = self._matvec(x)
  File "/usr/local/lib/python2.7/site-packages/scipy/sparse/linalg/interface.py", line 460, in _matvec
    return self.__matvec_impl(x)
  File "/usr/local/lib/python2.7/site-packages/openmdao/solvers/scipy_gmres.py", line 162, in mult
    system._sys_apply_linear(mode, ls_inputs=self.system._ls_inputs, vois=(voi,))
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/group.py", line 680, in _sys_apply_linear
    gs_outputs=gs_outputs)
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/group.py", line 680, in _sys_apply_linear
    gs_outputs=gs_outputs)
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/system.py", line 619, in _sys_apply_linear
    self.apply_linear(self.params, self.unknowns, dparams, dunknowns, dresids, mode)
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/component.py", line 512, in apply_linear
    mode)
  File "/usr/local/lib/python2.7/site-packages/openmdao/core/system.py", line 726, in _apply_linear_jac
    arg_vec[param] += J.T.dot(dresids._flat(unknown)).reshape(shape)
ValueError: shapes (6,108) and (36,) not aligned: 108 (dim 1) != 36 (dim 0)
Makoto
  • 104,088
  • 27
  • 192
  • 230
jthomas
  • 2,437
  • 1
  • 13
  • 15
  • This is tough to tell from the error, but I suspect that one of the arrays returned by your linearize function might not be sized right. Problem is the error returned from python doesn't tell you which input/output pair, so it might be hard to find it. Could you share the code from your linearize function? – Kenneth Moore Nov 06 '15 at 21:28

1 Answers1

3

Just pushed up a fix to dev that improves the error messages for mis-sized Jacobians. They will look like this now:

"In component 'comp', the derivative of 'y2' wrt 'x1' should have shape '(3, 2)' but has shape '(3, 3)' instead."

If you pull down the latest and try running again, I think the new message will help to pinpoint the problem.

Kenneth Moore
  • 2,167
  • 1
  • 9
  • 13
  • Thanks, I'll try that now. My linearize function is rather involved and I was trying to figure out a reasonable way to post it, but I like this alternative much better. – jthomas Nov 09 '15 at 20:23
  • 2
    It turns out I had several incorrectly shaped arrays for the linearize function in several components due to bringing these components over from old OpenMDAO where I was building the actual jacobian to return. The improved error message was VERY helpful in figuring this out and correcting the problem. – jthomas Nov 09 '15 at 21:38