Skip to content

Commit

Permalink
Merge 17d3b5b into d41e1e8
Browse files Browse the repository at this point in the history
  • Loading branch information
relf committed Apr 9, 2020
2 parents d41e1e8 + 17d3b5b commit aaee72c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<h1>CHANGELOG</h1>

<h3>1.8.0 (??/05/2020)</h3>
<ul>
<li>Add mechanism (whatsopt docking) to ease the connection of known discipline implementations</li>
</ul>

<h3>1.7.0 (05/04/2020)</h3>
<ul>
<li>User has access to SEGOMOE optimizer as a web service</li>
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.0
1.7.1
5 changes: 5 additions & 0 deletions app/lib/whats_opt/code_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(mda)
@server_module = "server"
@server_host = "localhost"
@server_port = 31400
@generator = self
end

# options: only_base: false, with_run: true, with_server: false, with_runops: true, user_agent: nil, sqlite_filename: nil
Expand All @@ -45,6 +46,10 @@ def generate(options = {})
return stringio.read, zip_filename
end

def render_partial(file)
ERB.new(File.read(File.join(@template_dir, file))).result(binding)
end

def _generate(filename, template_filename, gendir)
template = File.join(@template_dir, template_filename)
Rails.logger.info "Creating #{filename} from #{File.basename(template)}"
Expand Down
3 changes: 3 additions & 0 deletions app/lib/whats_opt/templates/base_header_partial.py.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DO NOT EDIT unless you know what you are doing
# whatsopt_url: <%= @whatsopt_url %>
# analysis_id: <%= @mda.id %>
9 changes: 7 additions & 2 deletions app/lib/whats_opt/templates/openmdao_discipline.py.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ class <%= @discipline.py_classname %>(<%= @discipline.py_classname %>Base):

def compute(self, inputs, outputs):
""" <%= @discipline.py_classname %> computation """
<% @discipline.output_variables.numeric.each do |var_out| %>
outputs['<%= var_out.py_varname %>'] = <%= var_out.default_py_value %> <% end %>
if self._impl:
# Docking mechanism: use implementation if referenced in .whatsopt_dock.yml file
self._impl.compute(inputs, outputs)
else:
<%= "pass" if @discipline.output_variables.numeric.empty? -%>
<% @discipline.output_variables.numeric.each do |var_out| %>
outputs['<%= var_out.py_varname %>'] = <%= var_out.default_py_value %> <% end %>

# Reminder: inputs of compute()
# <% @discipline.input_variables.numeric.each do |var_in| %>
Expand Down
19 changes: 16 additions & 3 deletions app/lib/whats_opt/templates/openmdao_discipline_base.py.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# DO NOT EDIT unless you know what you are doing
# whatsopt_url: <%= @whatsopt_url %>
# analysis_id: <%= @mda.id %>
<%= @generator.render_partial('base_header_partial.py.erb') %>

import numpy as np
from os import path
from importlib import import_module
from yaml import load, FullLoader
from openmdao.api import <%= @dimpl.implicit_component ? "ImplicitComponent" : "ExplicitComponent" %>

class <%= @discipline.py_classname %>Base(<%= @dimpl.implicit_component ? "ImplicitComponent" : "ExplicitComponent" %>):
""" An OpenMDAO base component to encapsulate <%= @discipline.py_classname %> discipline """

def __init__(self, **kwargs):
super(<%= @discipline.py_classname %>Base, self).__init__(**kwargs)
self._impl = None
dockconf = path.join(path.dirname(__file__), ".whatsopt_dock.yml")
if path.exists(dockconf):
with open(dockconf) as dockfile:
dock = load(dockfile, Loader=FullLoader)
impl = dock.get("<%= @discipline.py_modulename %>")
if impl:
module = import_module(impl['module'])
self._impl = getattr(module, impl['class'])()

<% unless @discipline.variables.empty? -%>
def setup(self):
<% @discipline.input_variables.numeric.each do |var| -%>
Expand Down
3 changes: 1 addition & 2 deletions app/lib/whats_opt/templates/openmdao_main_base.py.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# DO NOT EDIT unless you know what you are doing
# analysis_id: <%= @mda.id %>
<%= @generator.render_partial('base_header_partial.py.erb') %>

import numpy as np
from packaging import version
Expand Down

0 comments on commit aaee72c

Please sign in to comment.