Skip to content

Commit

Permalink
Merge pull request #175 from fast-aircraft-design/problem-factory
Browse files Browse the repository at this point in the history
Refactoring of FASTOADProblem
  • Loading branch information
christophe-david committed May 20, 2020
2 parents b239f93 + c290097 commit d7e21bd
Show file tree
Hide file tree
Showing 20 changed files with 555 additions and 345 deletions.
59 changes: 27 additions & 32 deletions src/fastoad/cmd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging
import os
import os.path as pth
import sys
import textwrap as tw
Expand All @@ -30,10 +29,12 @@
from IPython.display import display, HTML
from fastoad.cmd.exceptions import FastFileExistsError
from fastoad.io.configuration import FASTOADProblem
from fastoad.io.configuration.configuration import FASTOADProblemConfigurator
from fastoad.io.xml import VariableLegacy1XmlFormatter
from fastoad.module_management import BundleLoader
from fastoad.module_management import OpenMDAOSystemRegistry
from fastoad.openmdao.variables import VariableList
from fastoad.utils.files import make_parent_dir
from fastoad.utils.postprocessing import OptimizationViewer
from fastoad.utils.resource_management.copy import copy_resource
from whatsopt.show_utils import generate_xdsm_html
Expand Down Expand Up @@ -64,9 +65,7 @@ def generate_configuration_file(configuration_file_path: str, overwrite: bool =
configuration_file_path,
)

dirname = pth.abspath(pth.dirname(configuration_file_path))
if not pth.exists(dirname):
os.makedirs(dirname)
make_parent_dir(configuration_file_path)

copy_resource(resources, SAMPLE_FILENAME, configuration_file_path)
_LOGGER.info("Sample configuration written in %s", configuration_file_path)
Expand All @@ -87,23 +86,22 @@ def generate_inputs(
:param overwrite: if True, file will be written even if one already exists
:raise FastFileExistsError: if overwrite==False and configuration_file_path already exists
"""
problem = FASTOADProblem()
problem.configure(configuration_file_path)
conf = FASTOADProblemConfigurator(configuration_file_path)

inputs_path = pth.normpath(problem.input_file_path)
if not overwrite and pth.exists(inputs_path):
input_file_path = conf.input_file_path
if not overwrite and pth.exists(conf.input_file_path):
raise FastFileExistsError(
"Input file %s not written because it already exists. "
"Use overwrite=True to bypass." % inputs_path,
inputs_path,
"Use overwrite=True to bypass." % input_file_path,
input_file_path,
)

if source_path_schema == "legacy":
problem.write_needed_inputs(source_path, VariableLegacy1XmlFormatter())
conf.write_needed_inputs(source_path, VariableLegacy1XmlFormatter())
else:
problem.write_needed_inputs(source_path)
conf.write_needed_inputs(source_path)

_LOGGER.info("Problem inputs written in %s", inputs_path)
_LOGGER.info("Problem inputs written in %s", input_file_path)


def list_variables(
Expand All @@ -129,8 +127,8 @@ def list_variables(
sys.stdout
:raise FastFileExistsError: if overwrite==False and out parameter is a file path and the file exists
"""
problem = FASTOADProblem()
problem.configure(configuration_file_path)
conf = FASTOADProblemConfigurator(configuration_file_path)
problem = conf.get_problem()

input_variables = VariableList.from_unconnected_inputs(problem, with_optional_inputs=True)
output_variables = VariableList.from_problem(problem, use_inputs=False)
Expand All @@ -145,6 +143,7 @@ def list_variables(
"Use overwrite=True to bypass." % out,
out,
)
make_parent_dir(out)
out_file = open(out, "w")
table_width = MAX_TABLE_WIDTH
else:
Expand Down Expand Up @@ -237,8 +236,8 @@ def list_systems(
"""

if configuration_file_path:
problem = FASTOADProblem()
problem.configure(configuration_file_path)
conf = FASTOADProblemConfigurator(configuration_file_path)
conf.load(configuration_file_path)
# As the problem has been configured, BundleLoader now knows additional registered systems

if isinstance(out, str):
Expand All @@ -248,6 +247,8 @@ def list_systems(
"Use overwrite=True to bypass." % out,
out,
)

make_parent_dir(out)
out_file = open(out, "w")
else:
out_file = out
Expand Down Expand Up @@ -288,11 +289,8 @@ def write_n2(configuration_file_path: str, n2_file_path: str = None, overwrite:
n2_file_path,
)

if not pth.exists(pth.dirname(n2_file_path)):
os.makedirs(pth.dirname(n2_file_path))

problem = FASTOADProblem()
problem.configure(configuration_file_path)
make_parent_dir(n2_file_path)
problem = FASTOADProblemConfigurator(configuration_file_path).get_problem()
problem.setup()
problem.final_setup()

Expand Down Expand Up @@ -327,11 +325,9 @@ def write_xdsm(
xdsm_file_path,
)

if not pth.exists(pth.dirname(xdsm_file_path)):
os.makedirs(pth.dirname(xdsm_file_path))
make_parent_dir(xdsm_file_path)

problem = FASTOADProblem()
problem.configure(configuration_file_path)
problem = FASTOADProblemConfigurator(configuration_file_path).get_problem()
problem.setup()
problem.final_setup()

Expand Down Expand Up @@ -379,8 +375,9 @@ def _run_problem(
:return: the OpenMDAO problem after run
"""

problem = FASTOADProblem()
problem.configure(configuration_file_path, auto_scaling=auto_scaling)
problem = FASTOADProblemConfigurator(configuration_file_path).get_problem(
read_inputs=True, auto_scaling=auto_scaling
)

outputs_path = pth.normpath(problem.output_file_path)
if not overwrite and pth.exists(outputs_path):
Expand All @@ -390,7 +387,6 @@ def _run_problem(
outputs_path,
)

problem.read_inputs()
problem.setup()
if mode == "run_model":
problem.run_model()
Expand Down Expand Up @@ -441,9 +437,8 @@ def optimization_viewer(configuration_file_path: str):
:param configuration_file_path: problem definition
:return: display of the OptimizationViewer
"""
problem = FASTOADProblem()
problem.configure(configuration_file_path)
problem_configuration = FASTOADProblemConfigurator(configuration_file_path)
viewer = OptimizationViewer()
viewer.load(problem)
viewer.load(problem_configuration)

return viewer.display()
5 changes: 3 additions & 2 deletions src/fastoad/cmd/resources/fastoad.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ driver = "om.ScipyOptimizeDriver(tol=1e-6, optimizer='COBYLA')"
nonlinear_solver = "om.NonlinearBlockGS(maxiter=200)"
linear_solver = "om.DirectSolver()"

# AlThough "model" is a mandatory name for the top level of the model, its sub-components can be freely named by user
# Although "model" is a mandatory name for the top level of the model, its
# sub-components can be freely named by user
[model.geometry]
# An OpenMDAO component is identified by its "id"
id = "fastoad.geometry.legacy"
Expand All @@ -39,7 +40,7 @@ driver = "om.ScipyOptimizeDriver(tol=1e-6, optimizer='COBYLA')"
[model.loop]
id = "fastoad.loop.wing_area"

[optimization]
[optimization] # This section is needed only if optimization process is run
[[optimization.design_var]]
name = "data:geometry:wing:MAC:at25percent:x"
lower = 16.0
Expand Down
2 changes: 1 addition & 1 deletion src/fastoad/io/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@

from .configuration import FASTOADProblem
from .exceptions import (
FASTConfigurationNoProblemDefined,
FASTConfigurationError,
FASTConfigurationBadOpenMDAOInstructionError,
)

0 comments on commit d7e21bd

Please sign in to comment.