# Source code for gemseo.core.mdofunctions.function_generator

# -*- coding: utf-8 -*-
# Copyright 2021 IRT Saint Exupéry, https://www.irt-saintexupery.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Contributors:
#    INITIAL AUTHORS - initial API and implementation and/or initial
#                        documentation
#        :author: Francois Gallard, Charlie Vanaret
#    OTHER AUTHORS   - MACROSCOPIC CHANGES
"""A class to creat MDOFunctions from MDODisciplines."""
from __future__ import division, unicode_literals

import logging
from numbers import Number
from typing import TYPE_CHECKING, Callable, Mapping, Optional, Sequence, Union

from numpy import ndarray
from six import string_types

from gemseo.core.discipline import MDODiscipline
from gemseo.core.mdofunctions.make_function import MakeFunction

if TYPE_CHECKING:
from gemseo.core.mdofunctions.mdo_function import MDOFunction

LOGGER = logging.getLogger(__name__)

OperandType = Union[ndarray, Number]
OperatorType = Callable[[OperandType, OperandType], OperandType]

[docs]class MDOFunctionGenerator(object):
"""Generator of :class:MDOFunction objects from a :class:.MDODiscipline.

It creates a :class:MDOFunction
evaluating some of the outputs of the discipline
from some of its

It uses closures to generate functions instances from a discipline execution.
"""

def __init__(
self,
discipline,  # type: MDODiscipline
):  # type: (...) -> None
"""
Args:
discipline: The discipline from which the generator builds the functions.
"""
self.discipline = discipline

[docs]    def get_function(
self,
input_names_list,  # type: Sequence[str]
output_names_list,  # type: Sequence[str]
default_inputs=None,  # type: Optional[Mapping[str,ndarray]]
differentiable=True,  # type: bool
):  # type: (...) -> MDOFunction
"""Build a function from a discipline input and output lists.

Args:
input_names_list: The names of the inputs of the discipline
to be inputs of the function.
output_names_list: The names of outputs of the discipline
to be returned by the function.
default_inputs: The default values of the inputs.
If None,
use the default values of the inputs
specified by the discipline.
differentiable: If True, then inputs and outputs are added
to the variables to be differentiated.

Returns:
The function.

Raises:
ValueError: If a given input (or output) name is not the name
of an input (or output) variable of the discipline.
"""
if isinstance(input_names_list, string_types):
input_names_list = [input_names_list]

if isinstance(output_names_list, string_types):
output_names_list = [output_names_list]

if input_names_list is None:
input_names_list = self.discipline.get_input_data_names()
if output_names_list is None:
output_names_list = self.discipline.get_output_data_names()

if not self.discipline.is_all_inputs_existing(input_names_list):
raise ValueError(
"Some elements of {} are not inputs of the discipline {}; "
"available inputs are: {}.".format(
input_names_list,
self.discipline.name,
self.discipline.get_input_data_names(),
)
)

if not self.discipline.is_all_outputs_existing(output_names_list):
raise ValueError(
"Some elements of {} are not outputs of the discipline {}; "
"available outputs are: {}.".format(
output_names_list,
self.discipline.name,
", ".join(self.discipline.get_output_data_names()),
)
)

# adds inputs and outputs to the list of variables to be
# differentiated
if differentiable: