Skip to content

Commit

Permalink
Fix sub-optimization code generation (#208)
Browse files Browse the repository at this point in the history
* Fix openmdao generator (sub optim)

* FIx sub optim template

* np.int instead of np.int
  • Loading branch information
relf authored Aug 28, 2024
1 parent 15b3ded commit e8d66e0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/lib/whats_opt/openmdao_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ def _generate_sub_optimization(gendir, discipline)
@sub_impl = @sub_mda.openmdao_impl || OpenmdaoAnalysisImpl.new(analysis: @sub_mda)
@sub_driver = OpenmdaoDriverFactory.new(@sub_impl.optimization_driver).create_driver

if @driver.optimization?
if @sub_driver.optimization?
_generate(@dimpl.py_filename, "openmdao_discipline_mdo.py.erb", gendir)
_generate(@dimpl.py_basefilename, "openmdao_discipline_mdo_base.py.erb", gendir)
else
# should be simple run_once driver
if @driver.class != WhatsOpt::OpenmdaoRunOnceDriver
if @sub_driver.class != WhatsOpt::OpenmdaoRunOnceDriver
raise RuntimeError.new("Ouch! Should be run_once driver got #{@driver.inspect}")
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/lib/whats_opt/openmdao_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def extended_desc

def default_py_type
if self.type == INTEGER_T
"np.int"
"np.int32"
else
"np.float"
"np.float64"
end
end

Expand All @@ -55,7 +55,7 @@ def default_py_value
if self.type == FLOAT_T
"np.ones(#{self.shape})"
else
"np.ones(#{self.shape}, dtype=np.int)"
"np.ones(#{self.shape}, dtype=np.int32)"
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class <%= @dimpl.py_classname %>MdoBase(om.SubmodelComp):
def __init__(self, inputs=None, outputs=None, reports=False, **kwargs):

# Problem definition
pb = om.Problem(<%= @dimpl.py_classname %>())
pb = om.Problem(model=<%= @dimpl.py_classname %>())
super().__init__(pb, inputs=None, outputs=None, reports=False, **kwargs)

<%- if @sub_driver.pyoptsparse? -%>
Expand Down Expand Up @@ -86,7 +86,7 @@ class <%= @dimpl.py_classname %>MdoBase(om.SubmodelComp):
<% @dimpl.numeric_input_vars.each do |var| -%>
self.add_input('<%= var.py_varname %>', val=<%= var.init_py_value %>, <%= var.ndim > 0 ? "shape=#{var.shape} ," : "" %> <%= var.is_int? ? "tags=['#{Variable::INTEGER_TAG}'] ," : "" %> desc='<%= var.escaped_desc %>'<%= @impl.use_units && !var.units.blank? ? ", units='#{var.units}'":"" %>)
<% end %>
<% @dimpl.numeric_input_vars.each do |var| -%>
<% @dimpl.numeric_output_vars.each do |var| -%>
<% if var.scaling.blank? -%>
self.add_output('<%= var.py_varname %>', val=<%= var.init_py_value %>, <%= var.ndim > 0 ? "shape=#{var.shape} ," : "" %> <%= var.is_int? ? "tags=['#{Variable::INTEGER_TAG}'] ," : "" %> desc='<%= var.escaped_desc %>'<%= @impl.use_units && !var.units.blank? ? ", units='#{var.units}'":"" %>)
<%- else -%>
Expand All @@ -97,6 +97,7 @@ class <%= @dimpl.py_classname %>MdoBase(om.SubmodelComp):
<%- unless @dimpl.numeric_input_vars.empty? || @dimpl.numeric_output_vars.empty? || !@discipline.is_derivable?-%>
def setup_partials(self):
super().setup_partials()
self.declare_partials('*', '*', method='<%= @dimpl.support_derivatives ? "exact" : "fd" %>')
<% end -%>
<% else -%>
Expand Down

0 comments on commit e8d66e0

Please sign in to comment.