Skip to content

Commit

Permalink
Merge pull request #468 from fast-aircraft-design/issue-278_nan_value…
Browse files Browse the repository at this point in the history
…s_warning

Added warning for nan values when generating inputs
  • Loading branch information
christophe-david committed Nov 21, 2022
2 parents 77321f8 + 37b3aac commit 0a98f52
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/fastoad/io/configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from importlib.resources import open_text
from typing import Dict

import numpy as np
import openmdao.api as om
import tomlkit
from jsonschema import validate
Expand Down Expand Up @@ -230,8 +231,14 @@ def write_needed_inputs(
if source_file_path:
ref_vars = DataFile(source_file_path, formatter=source_formatter)
variables.update(ref_vars, add_variables=False)
nan_variable_names = []
for var in variables:
var.is_input = True
# Checking if variables have NaN values
if np.any(np.isnan(var.value)):
nan_variable_names.append(var.name)
if nan_variable_names:
_LOGGER.warning("The following variables have NaN values: %s", nan_variable_names)
variables.save()

def get_optimization_definition(self) -> Dict:
Expand Down
23 changes: 23 additions & 0 deletions src/fastoad/io/configuration/tests/data/ref_inputs_with_nan.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
~ This file is part of FAST-OAD : A framework for rapid Overall Aircraft Design
~ Copyright (C) 2021 ONERA & ISAE-SUPAERO
~ FAST is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~ 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 General Public License for more details.
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<aircraft>
<x>nan</x><!-- This variable should appear in the warning for nan values -->
<z units="m**2">5.0 2.0</z>
<y1>nan</y1><!-- If this variable is kept in the input IVC, OpenMDAO will raise an exception because it is an output of a component
As it should be ignored, its NaN value should bring no harm.-->
<foo>nan</foo><!-- This totally unused variable is expected to be transferred in output file -->
<bar>42</bar><!-- This totally unused variable is expected to be transferred in output file -->
</aircraft>
13 changes: 12 additions & 1 deletion src/fastoad/io/configuration/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_problem_definition_correct_configuration(cleanup):
assert conf.output_file_path == pth.join(RESULTS_FOLDER_PATH, "outputs.xml")


def test_problem_definition_with_xml_ref(cleanup):
def test_problem_definition_with_xml_ref(cleanup, caplog):
"""Tests what happens when writing inputs using data from existing XML file"""
for extension in ["toml", "yml"]:
clear_openmdao_registry()
Expand All @@ -124,7 +124,18 @@ def test_problem_definition_with_xml_ref(cleanup):
result_folder_path = pth.join(RESULTS_FOLDER_PATH, "problem_definition_with_xml_ref")
conf.input_file_path = pth.join(result_folder_path, "inputs.xml")
conf.output_file_path = pth.join(result_folder_path, "outputs.xml")
ref_input_data_path_with_nan = pth.join(DATA_FOLDER_PATH, "ref_inputs_with_nan.xml")
ref_input_data_path = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml")

# Test that the presence of NaN values in inputs logs a warning
caplog.clear()
conf.write_needed_inputs(ref_input_data_path_with_nan)
assert len(caplog.records) == 1
record = caplog.records[0]
assert record.levelname == "WARNING"
assert record.message == "The following variables have NaN values: ['x']"

# Test normal process without NaN in inputs
conf.write_needed_inputs(ref_input_data_path)
input_data = DataFile(conf.input_file_path)
assert len(input_data) == 2
Expand Down
2 changes: 1 addition & 1 deletion src/fastoad/openmdao/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _get_problem_inputs(self) -> Tuple[VariableList, VariableList]:
for name in unused_variables.names():
del input_variables[name]

nan_variable_names = [var.name for var in input_variables if np.all(np.isnan(var.value))]
nan_variable_names = [var.name for var in input_variables if np.any(np.isnan(var.value))]
if nan_variable_names:
raise FASTOpenMDAONanInInputFile(self.input_file_path, nan_variable_names)

Expand Down

0 comments on commit 0a98f52

Please sign in to comment.