Note
Go to the end to download the full example code.
Create a discipline from analytical expressions#
from __future__ import annotations
from numpy import array
from gemseo import configure_logger
from gemseo import create_discipline
Import#
configure_logger()
<RootLogger root (INFO)>
Introduction#
A simple Discipline
can be created
using analytic formulas, e.g. \(y_1=2x^2\) and \(y_2=5+3x^2z^3\),
thanks to the AnalyticDiscipline
class
which is a quick alternative to model a simple analytic MDO problem.
Create the dictionary of analytic outputs#
First of all, we have to define the output expressions in a dictionary
where keys are output names and values are formula with string
format:
expressions = {"y_1": "2*x**2", "y_2": "5+3*x**2+z**3"}
Create the discipline#
Then, we create and instantiate the corresponding
AnalyticDiscipline
,
which is a particular Discipline
.
For that, we use the API function create_discipline()
with:
discipline_name="AnalyticDiscipline"
,name="analytic"
,expressions=expr_dict
.
In practice, we write:
disc = create_discipline("AnalyticDiscipline", expressions=expressions)
Note
GEMSEO takes care of the grammars and
Discipline._run()
method generation
from the expressions
argument.
In the background, GEMSEO considers that x
is a mono-dimensional
float input parameter and y_1
and y_2
are
mono-dimensional float output parameters.
Execute the discipline#
Lastly, we can execute this discipline any other:
input_data = {"x": array([2.0]), "z": array([3.0])}
disc.execute(input_data)
{'x': array([2.]), 'z': array([3.]), 'y_1': array([8.]), 'y_2': array([44.])}
About the analytic jacobian#
The discipline will provide analytic derivatives (Jacobian) automatically using the sympy library.
This can be checked easily using
Discipline.check_jacobian()
:
disc.check_jacobian(
input_data,
derr_approx=disc.ApproximationMode.FINITE_DIFFERENCES,
step=1e-5,
threshold=1e-3,
)
INFO - 00:58:36: Jacobian: dp y_1/dp x succeeded.
INFO - 00:58:36: Jacobian: dp y_1/dp z succeeded.
INFO - 00:58:36: Jacobian: dp y_2/dp x succeeded.
INFO - 00:58:36: Jacobian: dp y_2/dp z succeeded.
INFO - 00:58:36: Linearization of Discipline: AnalyticDiscipline is correct.
True
Total running time of the script: (0 minutes 0.013 seconds)