Build a discipline from a simple python function

Let’s consider a simple python function, e.g.:

def f(x=0., y=0.):
    """A simple python function"""
    z = x + 2*y
    return z

Then, we can consider the AutoPyDiscipline to convert it into an MDODiscipline.

Create and instantiate the discipline

For that, we can use the create_discipline() API function with AutoPyDiscipline as first argument:

from gemseo.api import create_discipline
from numpy import array

disc = create_discipline('AutoPyDiscipline', py_func=f)

Execute the discipline

Then, we can execute it easily, even considering default inputs:


which results in:

{'y': array([ 0.]), 'x': array([ 0.]), 'z': array([ 0.])}

or using new inputs:

print(disc.execute({'x': array([1.]), 'y':array([-3.2])}))

which results in:

{'y': array([-3.2]), 'x': array([ 1.]), 'z': array([-5.4])}

Optional arguments

Optional arguments are:

  • py_jac=None: pointer to the jacobian function which must returned a 2D numpy array,

  • use_arrays=False: if True, the function is expected to take arrays as inputs and give outputs as arrays,

  • write_schema=False: if True, write the json schema on the disk.

Here is an example of jacobian function:

def dfdxy(x=0., y=0.):
    """Jacobian function of f"""
    jac = array((2,1))
    jac[0, 0] = 1
    jac[1, 0] = 2
    return jac