def test_problem_definition_with_xml_ref_with_indep(cleanup): """Tests what happens when writing inputs of a problem with indeps using data from existing XML file""" for extension in ["toml", "yml"]: clear_openmdao_registry() conf = FASTOADProblemConfigurator( pth.join(DATA_FOLDER_PATH, "valid_sellar_with_indep.%s" % extension) ) result_folder_path = pth.join( RESULTS_FOLDER_PATH, "problem_definition_with_xml_ref_with_indep" ) 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 = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml") conf.write_needed_inputs(ref_input_data_path) input_data = DataFile(conf.input_file_path) assert len(input_data) == 2 assert "system:x" in input_data.names() assert "z" in input_data.names() problem = conf.get_problem(read_inputs=True, auto_scaling=True) # runs evaluation without optimization loop to check that inputs are taken into account problem.setup() # system:x is not in ref_inputs.xml problem["system:x"] = 1.0 problem.run_model() assert problem["f"] == pytest.approx(28.58830817, abs=1e-6) problem.write_outputs()
def test_problem_definition_with_xml_ref(cleanup): """Tests what happens when writing inputs using data from existing XML file""" for extension in ["toml", "yml"]: clear_openmdao_registry() conf = FASTOADProblemConfigurator(pth.join(DATA_FOLDER_PATH, "valid_sellar.%s" % extension)) 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 = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml") conf.write_needed_inputs(ref_input_data_path) input_data = DataFile(conf.input_file_path) assert len(input_data) == 2 assert "x" in input_data.names() assert "z" in input_data.names() problem = conf.get_problem(read_inputs=True, auto_scaling=True) # runs evaluation without optimization loop to check that inputs are taken into account problem.setup() problem.run_model() assert problem["f"] == pytest.approx(28.58830817, abs=1e-6) problem.write_outputs() # Test with alternate submodel ######################################### alt_conf = FASTOADProblemConfigurator( pth.join(DATA_FOLDER_PATH, "valid_sellar_alternate.%s" % extension) ) alt_conf.input_file_path = pth.join(result_folder_path, "inputs.xml") alt_conf.output_file_path = pth.join(result_folder_path, "outputs_alt.xml") alt_problem = alt_conf.get_problem(read_inputs=True, auto_scaling=True) # runs evaluation without optimization loop to check that inputs are taken into account alt_problem.setup() alt_problem.run_model() alt_problem.write_outputs() assert alt_problem["f"] == pytest.approx(0.58830817, abs=1e-6) assert alt_problem["g2"] == pytest.approx(-11.94151185, abs=1e-6) with pytest.raises(KeyError): alt_problem["g1"] # submodel for g1 computation has been deactivated.
def test_problem_definition_with_custom_xml(cleanup): """Tests what happens when writing inputs using existing XML with some unwanted var""" conf = FASTOADProblemConfigurator(pth.join(DATA_FOLDER_PATH, "valid_sellar.toml")) result_folder_path = pth.join(RESULTS_FOLDER_PATH, "problem_definition_with_custom_xml") conf.input_file_path = pth.join(result_folder_path, "inputs.xml") conf.output_file_path = pth.join(result_folder_path, "outputs.xml") input_data = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml") os.makedirs(result_folder_path, exist_ok=True) shutil.copy(input_data, conf.input_file_path) problem = conf.get_problem(read_inputs=True, auto_scaling=True) problem.setup() problem.run_model() assert problem["f"] == pytest.approx(28.58830817, abs=1e-6) problem.write_outputs()
def test_problem_definition_with_xml_ref_run_optim(cleanup): """ Tests what happens when writing inputs using data from existing XML file and running an optimization problem """ for extension in ["toml", "yml"]: clear_openmdao_registry() conf = FASTOADProblemConfigurator( pth.join(DATA_FOLDER_PATH, "valid_sellar.%s" % extension)) result_folder_path = pth.join( RESULTS_FOLDER_PATH, "problem_definition_with_xml_ref_run_optim") conf.input_file_path = pth.join(result_folder_path, "inputs.xml") input_data = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml") conf.write_needed_inputs(input_data) # Runs optimization problem with semi-analytic FD problem1 = conf.get_problem(read_inputs=True) problem1.setup() problem1.run_model() assert problem1["f"] == pytest.approx(28.58830817, abs=1e-6) problem1.run_driver() assert problem1["f"] == pytest.approx(3.18339395, abs=1e-6) problem1.output_file_path = pth.join(result_folder_path, "outputs_1.xml") problem1.write_outputs() # Runs optimization problem with monolithic FD problem2 = conf.get_problem(read_inputs=True) problem2.model.approx_totals() problem2.setup() problem2.run_model() # checks problem has been reset assert problem2["f"] == pytest.approx(28.58830817, abs=1e-6) problem2.run_driver() assert problem2["f"] == pytest.approx(3.18339395, abs=1e-6) problem2.output_file_path = pth.join(result_folder_path, "outputs_2.xml") problem2.write_outputs()