mdo_discipline_adapter module¶
A function computing some outputs of a discipline from some of its inputs.
- class gemseo.core.mdofunctions.mdo_discipline_adapter.MDODisciplineAdapter(input_names, output_names, default_inputs, discipline, names_to_sizes=None, linear_candidate=False)[source]¶
Bases:
LinearCandidateFunction
An
MDOFunction
executing a discipline for some inputs and outputs.Initialize self. See help(type(self)) for accurate signature.
- Parameters:
input_names (Sequence[str]) – The names of the inputs.
output_names (Sequence[str]) – The names of the outputs.
default_inputs (Mapping[str, ndarray] | None) – The default input values to overload the ones of the discipline at each evaluation of the outputs with
_fun()
or their derivatives with_jac()
. IfNone
, do not overload them.discipline (MDODiscipline) – The discipline to be adapted.
names_to_sizes (MutableMapping[str, int] | None) – The sizes of the input variables. If
None
, determine them from the default inputs and local data of the disciplineMDODiscipline
.linear_candidate (bool) –
Whether the final MDOFunction could be linear.
By default it is set to False.
- class ApproximationMode(value)¶
Bases:
StrEnum
The approximation derivation modes.
- CENTERED_DIFFERENCES = 'centered_differences'¶
The centered differences method used to approximate the Jacobians by perturbing each variable with a small real number.
- COMPLEX_STEP = 'complex_step'¶
The complex step method used to approximate the Jacobians by perturbing each variable with a small complex number.
- FINITE_DIFFERENCES = 'finite_differences'¶
The finite differences method used to approximate the Jacobians by perturbing each variable with a small real number.
- class ConstraintType(value)¶
Bases:
StrEnum
The type of constraint.
- EQ = 'eq'¶
The type of function for equality constraint.
- INEQ = 'ineq'¶
The type of function for inequality constraint.
- class FunctionType(value)¶
Bases:
StrEnum
An enumeration.
- EQ = 'eq'¶
- INEQ = 'ineq'¶
- NONE = ''¶
- OBJ = 'obj'¶
- OBS = 'obs'¶
- check_grad(x_vect, approximation_mode=ApproximationMode.FINITE_DIFFERENCES, step=1e-06, error_max=1e-08)¶
Check the gradients of the function.
- Parameters:
x_vect (ndarray[Any, dtype[number]]) – The vector at which the function is checked.
approximation_mode (ApproximationMode) –
The approximation mode.
By default it is set to “finite_differences”.
step (float) –
The step for the approximation of the gradients.
By default it is set to 1e-06.
error_max (float) –
The maximum value of the error.
By default it is set to 1e-08.
- Raises:
ValueError – Either if the approximation method is unknown, if the shapes of the analytical and approximated Jacobian matrices are inconsistent or if the analytical gradients are wrong.
- Return type:
None
- evaluate(x_vect)¶
Evaluate the function and store the dimension of the output space.
- static filt_0(arr, floor_value=1e-06)¶
Set the non-significant components of a vector to zero.
The component of a vector is non-significant if its absolute value is lower than a threshold.
- static from_pickle(file_path)¶
Deserialize a function from a file.
- Parameters:
file_path (str | Path) – The path to the file containing the function.
- Returns:
The function instance.
- Return type:
- classmethod generate_input_names(input_dim, input_names=None)¶
Generate the names of the inputs of the function.
- Parameters:
input_dim (int) – The dimension of the input space of the function.
input_names (Sequence[str] | None) – The initial names of the inputs of the function. If there is only one name, e.g.
["var"]
, use this name as a base name and generate the names of the inputs, e.g.["var!0", "var!1", "var!2"]
if the dimension of the input space is equal to 3. IfNone
, use"x"
as a base name and generate the names of the inputs, i.e.["x!0", "x!1", "x!2"]
.
- Returns:
The names of the inputs of the function.
- Return type:
Sequence[str]
- get_indexed_name(index)¶
Return the name of function component.
- static init_from_dict_repr(**attributes)¶
Initialize a new function.
This is typically used for deserialization.
- Parameters:
**attributes (Any) – The values of the serializable attributes listed in
MDOFunction.DICT_REPR_ATTR
.- Returns:
A function initialized from the provided data.
- Raises:
ValueError – If the name of an argument is not in
MDOFunction.DICT_REPR_ATTR
.- Return type:
- is_constraint()¶
Check if the function is a constraint.
The type of a constraint function is either ‘eq’ or ‘ineq’.
- Returns:
Whether the function is a constraint.
- Return type:
- offset(value)¶
Add an offset value to the function.
- static rel_err(a_vect, b_vect, error_max)¶
Compute the 2-norm of the difference between two vectors.
Normalize it with the 2-norm of the reference vector if the latter is greater than the maximal error.
- set_pt_from_database(database, design_space, normalize=False, jac=True, x_tolerance=1e-10)¶
Set the original function and Jacobian function from a database.
For a given input vector, the method
MDOFunction.func()
will return either the output vector stored in the database if the input vector is present orNone
. The same for the methodMDOFunction.jac()
.- Parameters:
database (Database) – The database to read.
design_space (DesignSpace) – The design space used for normalization.
normalize (bool) –
If
True
, the values of the inputs are unnormalized before call.By default it is set to False.
jac (bool) –
If
True
, a Jacobian pointer is also generated.By default it is set to True.
x_tolerance (float) –
The tolerance on the distance between inputs.
By default it is set to 1e-10.
- Return type:
None
- to_dict()¶
Create a dictionary representation of the function.
This is used for serialization. The pointers to the functions are removed.
- to_pickle(file_path)¶
Serialize the function and store it in a file.
- Parameters:
file_path (str | Path) – The path to the file to store the function.
- Return type:
None
- COEFF_FORMAT_ND: str = '{: .2e}'¶
The format to be applied to a number when represented in a matrix.
- DICT_REPR_ATTR: ClassVar[list[str]] = ['name', 'f_type', 'expr', 'input_names', 'dim', 'special_repr', 'output_names']¶
The names of the attributes to be serialized.
- property func: Callable[[ndarray[Any, dtype[number]]], ndarray[Any, dtype[number]] | Number]¶
The function to be evaluated from a given input vector.
- property has_jac: bool¶
Check if the function has an implemented Jacobian function.
- Returns:
Whether the function has an implemented Jacobian function.
- property input_dimension: int | None¶
The input variable dimension, needed for linear candidates.
If
None
this cannot be determined nor byMDODiscipline
default inputs nor byMDODisciplineAdapter.__input_names_to_sizes
.
- property input_names: list[str]¶
The names of the inputs of the function.
Use a copy of the original names.
- property jac: Callable[[ndarray[Any, dtype[number]]], ndarray[Any, dtype[number]]]¶
The Jacobian function to be evaluated from a given input vector.
- last_eval: OutputType | None¶
The value of the function output at the last evaluation.
None
if it has not yet been evaluated.
- property n_calls: int¶
The number of times the function has been evaluated.
This count is both multiprocess- and multithread-safe, thanks to the locking process used by
MDOFunction.evaluate()
.