In some cases, an MDODiscipline may return the cached value for invalid inputs.
When an MDODiscipline with array inputs is executed using valid input data, it will be possible to recover the cached values from that run if we run the discipline again using floats instead of arrays.
The code below should fail for the second execution of the discipline, as I am providing floats instead of arrays:
from gemseo import configure_logger
from gemseo import create_discipline
from numpy import array
configure_logger()
def f(x=0.0, y=0.0):
"""A simple Python function."""
z = x + 2 * y
return z
disc = create_discipline("AutoPyDiscipline", py_func=f)
disc.execute({"x": array([1.]), "y": array([-3.])})
disc.execute({"x": 1., "y": -3.})
At first I thought that it was a casting problem related to the AutoPyDiscipline, but I was able to reproduce the same behavior for AnalyticDiscipline and even for a Sellar1 discipline coded from scratch.
The issue seems to come from the execute() method of the MDODiscipline, which checks if the given input value is cached and returns the result when it is, all of which happens before checking if the data is valid:
if self.cache is not None:
# Check if the cache already contains the outputs associated to these inputs
in_names = self.get_input_data_names()
_, out_cached, out_jac = self.cache[input_data]
if out_cached:
self.__update_local_data_from_cache(input_data, out_cached, out_jac)
return self._local_data # IF THE INPUT DATA IS CACHED, THE RESULT IS RETURNED. IT SEEMS THAT FLOAT VALUES ARE CONSIDERED EQUAL TO ARRAYS OF THE SAME VALUE
# Cache was not loaded, see self.linearize
self._cache_was_loaded = False
if self.cache is not None:
# Save the state of the inputs
cached_inputs = self.__get_input_data_for_cache(input_data, in_names)
if self.activate_input_data_check:
self.check_input_data(input_data) # THE CHECK HAPPENS HERE