Skip to content

Commit

Permalink
Upgrade GEMSEO generated code to GEMSEO 5.x (#204)
Browse files Browse the repository at this point in the history
* Fix code to take into account grammar handling changes

* Fix module change

* Make run_doe work again

* Make run_mdo work again
  • Loading branch information
relf authored Aug 28, 2024
1 parent 85f1c86 commit a61fdb9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 66 deletions.
101 changes: 49 additions & 52 deletions app/lib/whats_opt/templates/gemseo/gemseo_analysis_base.py.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from gemseo.mda.jacobi import MDAJacobi
from gemseo.core.doe_scenario import DOEScenario
from gemseo.core.mdo_scenario import MDOScenario
from gemseo.core.discipline import MDODiscipline
from gemseo.core.json_grammar import JSONGrammar
from gemseo.core.grammars.factory import GrammarFactory
from gemseo.algos.design_space import DesignSpace

class <%= @impl.py_classname %>MDABase(MDAJacobi):
Expand All @@ -23,19 +23,21 @@ class <%= @impl.py_classname %>MDABase(MDAJacobi):
]
super().__init__(disciplines, **mda_options)

def _instantiate_grammars(self, input_grammar_file, output_grammar_file, grammar_type=MDODiscipline.JSON_GRAMMAR_TYPE):
self.default_inputs = {<% @mda.input_variables.each do |var| %>
'<%= var.py_varname %>': np.atleast_1d(<%= var.init_py_value %>),<% end -%>
def _instantiate_grammars(self, input_grammar_file, output_grammar_file, grammar_type=MDODiscipline.GrammarType.JSON):
factory = GrammarFactory()

}
self.default_outputs = {<% @mda.output_variables.each do |var| %>
'<%= var.py_varname %>': np.atleast_1d(<%= var.init_py_value %>),<% end -%>
self.input_grammar = factory.create(
grammar_type,
name="<%= @mda.impl.py_classname %>_analysis_input",
file_path=input_grammar_file,
)

self.output_grammar = factory.create(
grammar_type,
name="<%= @mda.impl.py_classname %>_analysis_output",
file_path=output_grammar_file,
)

}
self.input_grammar = JSONGrammar("<%= @discipline.impl.py_classname %>")
self.input_grammar.initialize_from_base_dict(self.default_inputs)
self.output_grammar = JSONGrammar("<%= @discipline.impl.py_classname %>")
self.output_grammar.initialize_from_base_dict(self.default_outputs)

<% @mda.plain_disciplines.each do |disc| -%>
def create_<%= disc.impl.basename %>(self):
Expand All @@ -49,7 +51,7 @@ class <%= @impl.py_classname %>DesignSpace(DesignSpace):
def __init__(self):
super().__init__()

# Initial solution<% @mda.input_variables.each do |dv| %>
# Initial solution<% @mda.variables.where(io_mode: WhatsOpt::Variable::OUT).each do |dv| %>
<%= dv.name %> = np.atleast_1d(<%= dv.init_py_value %>)<% end -%>


Expand All @@ -61,7 +63,17 @@ class <%= @impl.py_classname %>DOEScenarioBase(DOEScenario):
""" A base class for <%= @impl.py_classname %> DOE Scenario """

def __init__(self, **kwargs):
self._init_from_whatsopt()
super().__init__(
disciplines=self._disciplines,
formulation=self._formulation,
objective_name=self._objective_name,
design_space=self._design_space,
name=self._name,
**kwargs,
)

def _init_from_whatsopt(self):
self._disciplines = [
<%= @mda.disciplines.nodes.map{|d| "self.create_#{d.impl.basename}()"}.join(', ') %>
]
Expand All @@ -70,23 +82,11 @@ class <%= @impl.py_classname %>DOEScenarioBase(DOEScenario):
self._formulation = "MDF"
self._objective_name = "<%= @mda.has_objective? ? @mda.min_objective_variables.first.name : 'NO_OBJECTIVE_ERROR' %>"
self._design_space = <%= @impl.py_classname %>DesignSpace()
self._name = "<%= @impl.py_classname %>DOEScenario"

super().__init__(
self._disciplines,
self._formulation,
self._objective_name,
self._design_space,
self._name,
**kwargs,
)

def _init_base_grammar(self, name):
name = "DOEScenario"
self.comp_dir = path.dirname(inspect.getfile(DOEScenario))
input_grammar_file = self.auto_get_grammar_file(True, name, self.comp_dir)
output_grammar_file = self.auto_get_grammar_file(False, name, self.comp_dir)
self._instantiate_grammars(input_grammar_file, output_grammar_file)
self._name = "DOEScenario"

def auto_get_grammar_file(self, is_input=True, name = "DOEScenario", comp_dir = None):
comp_dir = path.dirname(inspect.getfile(DOEScenario))
return super().auto_get_grammar_file(is_input, name, comp_dir)

<% @mda.plain_disciplines.each do |disc| -%>
def create_<%= disc.impl.basename %>(self):
Expand All @@ -98,23 +98,13 @@ class <%= @impl.py_classname %>MDOScenarioBase(MDOScenario):
""" A base class for <%= @impl.py_classname %> MDO Scenario """

def __init__(self, **kwargs):

self._disciplines = [
<%= @mda.disciplines.nodes.map{|d| "self.create_#{d.impl.basename}()"}.join(', ') %>
]
for disc in self._disciplines:
disc.set_jacobian_approximation()
self._formulation = "MDF"
self._objective_name = "<%= @mda.has_objective? ? @mda.min_objective_variables.first.name : 'NO_OBJECTIVE_ERROR' %>"
self._design_space = <%= @impl.py_classname %>DesignSpace()
self._name = "<%= @impl.py_classname %>MDOScenario"

self._init_from_whatsopt()
super().__init__(
self._disciplines,
self._formulation,
self._objective_name,
self._design_space,
self._name,
disciplines=self._disciplines,
formulation=self._formulation,
objective_name=self._objective_name,
design_space=self._design_space,
name=self._name,
**kwargs,
)
<% @mda.neg_constraint_variables.each do |voi| %>
Expand All @@ -128,13 +118,20 @@ class <%= @impl.py_classname %>MDOScenarioBase(MDOScenario):
<% @mda.eq_constraint_variables.each do |voi| %>
self.add_constraint('<%= voi.name %>', value=<%= voi.cstr_init_py_value %>)<% end -%>

def _init_from_whatsopt(self):
self._disciplines = [
<%= @mda.disciplines.nodes.map{|d| "self.create_#{d.impl.basename}()"}.join(', ') %>
]
for disc in self._disciplines:
disc.set_jacobian_approximation()
self._formulation = "MDF"
self._objective_name = "<%= @mda.has_objective? ? @mda.min_objective_variables.first.name : 'NO_OBJECTIVE_ERROR' %>"
self._design_space = <%= @impl.py_classname %>DesignSpace()
self._name = "MDOScenario"

def _init_base_grammar(self, name):
name = "MDOScenario"
self.comp_dir = path.dirname(inspect.getfile(MDOScenario))
input_grammar_file = self.auto_get_grammar_file(True, name, self.comp_dir)
output_grammar_file = self.auto_get_grammar_file(False, name, self.comp_dir)
self._instantiate_grammars(input_grammar_file, output_grammar_file)
def auto_get_grammar_file(self, is_input=True, name = "MDOScenario", comp_dir = None):
comp_dir = path.dirname(inspect.getfile(DOEScenario))
return super().auto_get_grammar_file(is_input, name, comp_dir)

<% @mda.plain_disciplines.each do |disc| -%>
def create_<%= disc.impl.basename %>(self):
Expand Down
39 changes: 29 additions & 10 deletions app/lib/whats_opt/templates/gemseo/gemseo_discipline_base.py.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,43 @@

import numpy as np
from numpy import nan
from os import path
from importlib import import_module
from yaml import load, FullLoader

from gemseo.core.discipline import MDODiscipline
from gemseo.core.json_grammar import JSONGrammar
from gemseo.core.grammars.factory import GrammarFactory

class <%= @discipline.impl.py_classname %>Base(MDODiscipline):
""" An GEMSEO discipline to encapsulate <%= @discipline.impl.py_classname %> discipline """

def __init__(self, **kwargs):
self.comp_dir = "./"
super().__init__(grammar_type=MDODiscipline.SIMPLE_GRAMMAR_TYPE, **kwargs)
super().__init__(grammar_type=MDODiscipline.GrammarType.JSON, **kwargs)

def _instantiate_grammars(self, input_grammar_file, output_grammar_file, grammar_type=MDODiscipline.GrammarType.JSON):
inputs = {
<% @discipline.impl.numeric_input_vars.each do |var| %>
'<%= var.py_varname %>': np.atleast_1d(<%= var.init_py_value %>),<% end -%>
}
outputs = {
<% @discipline.impl.numeric_output_vars.each do |var| %>
'<%= var.py_varname %>': np.atleast_1d(<%= var.init_py_value %>),<% end -%>
}

factory = GrammarFactory()

self.input_grammar = factory.create(
grammar_type,
name="<%= @discipline.impl.py_classname %>_discipline_input",
file_path=input_grammar_file,
)
self.input_grammar.update_from_data(inputs)

self.output_grammar = factory.create(
grammar_type,
name="<%= @discipline.impl.py_classname %>_discipline_output",
file_path=output_grammar_file,
)
self.output_grammar.update_from_data(outputs)

def _instantiate_grammars(self, input_grammar_file, output_grammar_file, grammar_type=MDODiscipline.JSON_GRAMMAR_TYPE):
self.default_inputs = {
<% @discipline.impl.numeric_input_vars.each do |var| %>
'<%= var.py_varname %>': np.atleast_1d(<%= var.init_py_value %>),<% end -%>
Expand All @@ -24,10 +47,6 @@ class <%= @discipline.impl.py_classname %>Base(MDODiscipline):
<% @discipline.impl.numeric_output_vars.each do |var| %>
'<%= var.py_varname %>': np.atleast_1d(<%= var.init_py_value %>),<% end -%>
}
self.input_grammar = JSONGrammar("<%= @discipline.impl.py_classname %>")
self.input_grammar.initialize_from_base_dict(self.default_inputs)
self.output_grammar = JSONGrammar("<%= @discipline.impl.py_classname %>")
self.output_grammar.initialize_from_base_dict(self.default_outputs)

def _run(self):
<% @discipline.impl.numeric_input_vars.each do |var| %>
Expand Down
4 changes: 2 additions & 2 deletions app/lib/whats_opt/templates/gemseo/run_doe.py.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from optparse import OptionParser
from gemseo.api import configure_logger
from gemseo import configure_logger
from <%= @impl.py_modulename %> import <%= @impl.py_classname %>DOEScenario

parser = OptionParser()
Expand All @@ -18,7 +18,7 @@ scenario.add_observable('<%= voi.name %>')<% end -%>
scenario.execute(input_data={"max_iter": 10, "algo": "OT_LHS", "n_samples": options.n_cases })

problem = scenario.formulation.opt_problem
problem.export_hdf("<%= @impl.py_modulename %>_doe.hdf5")
problem.to_hdf("<%= @impl.py_modulename %>_doe.hdf5")

### Post-processing Example
# from gemseo.api import execute_post
Expand Down
4 changes: 2 additions & 2 deletions app/lib/whats_opt/templates/gemseo/run_mdo.py.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemseo.api import configure_logger
from gemseo import configure_logger
from <%= @impl.py_modulename %> import <%= @impl.py_classname %>MDOScenario

configure_logger()
Expand All @@ -11,7 +11,7 @@ scenario.add_observable('<%= voi.name %>')<% end -%>
scenario.execute(input_data={"max_iter": 10, "algo": "SLSQP"})

problem = scenario.formulation.opt_problem
problem.export_hdf("<%= @impl.py_modulename %>_optimization.hdf5")
problem.to_hdf("<%= @impl.py_modulename %>_optimization.hdf5")

### Post-processing Example
# from gemseo.api import execute_post
Expand Down

0 comments on commit a61fdb9

Please sign in to comment.