OpenMDAO 2.2.0:执行setup时触发TypeError错误求助
Fixing the TypeError in OpenMDAO ImplicitComponent Setup
The TypeError you’re hitting during the setup phase is almost certainly due to missing the mandatory val parameter when adding the obj output to your ImplicitComponent.
Implicit components require initial guess values for all outputs—this is because the solver needs a starting point to converge the implicit equations that define your component. Unlike explicit components (where val is optional, though still good practice), omitting val for an implicit output will trigger an error.
Corrected Code
Here’s the fixed version of your Test1Comp class, with the missing val added to the obj output:
from openmdao.api import Problem, Group, IndepVarComp, ImplicitComponent, ScipyOptimizeDriver class Test1Comp(ImplicitComponent): def setup(self): self.add_input('x', 0.5) self.add_input('design_x', 1.0) self.add_output('z', val=0.0) self.add_output('obj', val=0.0) # Added required initial value here self.declare_partials(of='*', wrt='*', method='fd', form='central', step=1.0e-4) def apply_nonlinear(self, inputs, outputs, resids): x = inputs['x'] design_x = inputs['design_x'] z = outputs['z'] # Complete your residual definitions here (example shown) resids['z'] = z - x * design_x # Residual for z equation resids['obj'] = z**2 # Residual/objective relation def solve_nonlinear(self, inputs, outputs): # Optional: Implement if you want a custom local solver, or rely on OpenMDAO's default outputs['z'] = inputs['x'] * inputs['design_x'] outputs['obj'] = outputs['z']**2
Quick Additional Tips
- Don’t leave the
apply_nonlinearmethod incomplete (your snippet cuts off atresids...). You need to assign values to every residual entry that corresponds to an output (resids['z']andresids['obj']in this case). - If you’re using this component in an optimization, double-check that your residual equations correctly model the relationship between inputs and outputs for your use case.
内容的提问来源于stack exchange,提问作者rJonatan
相关产品推荐
相关产品推荐

