Example #1
0
    def save(self):
        """
        Save the optimization to the files.
        Possible files modified are:
            - the .yml configuration file
            - the input file (initial values)
            - the output file (values)
        """
        conf = self.problem_configuration
        input_variables = DataFile(self.problem_configuration.input_file_path,
                                   None)
        output_variables = DataFile(
            self.problem_configuration.output_file_path, None)
        opt_def = conf.get_optimization_definition()

        variables = self.get_variables()
        for variable in variables:
            name = variable.name
            if name in input_variables.names():
                input_variables[name].value = variable.metadata[
                    "initial_value"]
            if name in output_variables.names():
                output_variables[name].value = variable.metadata["value"]
            self._update_optim_variable(variable, opt_def)

        # Saving modifications
        # Initial values
        input_variables.save()
        # Values
        output_variables.save()

        # Optimization definition
        conf.set_optimization_definition(opt_def)
        conf.save()
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_generate_inputs(cleanup):
    input_file_path = api.generate_inputs(CONFIGURATION_FILE_PATH,
                                          overwrite=False)
    assert input_file_path == pth.join(RESULTS_FOLDER_PATH, "inputs.xml")
    assert pth.exists(input_file_path)
    data = DataFile(input_file_path)
    assert len(data) == 2
    assert "x" in data.names() and "z" in data.names()

    # Let's add another variable to ensure overwrite is correctly done (issue #328)
    data["dummy_var"] = {"value": 0.0}
    data.save()

    # Generating again without forcing overwrite will make it fail
    with pytest.raises(FastPathExistsError):
        api.generate_inputs(CONFIGURATION_FILE_PATH, overwrite=False)

    input_file_path = api.generate_inputs(CONFIGURATION_FILE_PATH,
                                          pth.join(DATA_FOLDER_PATH,
                                                   "inputs.xml"),
                                          overwrite=True)

    assert input_file_path == pth.join(RESULTS_FOLDER_PATH, "inputs.xml")
    assert pth.exists(input_file_path)
    data = DataFile(input_file_path)
    assert len(data) == 2
    assert "x" in data.names() and "z" in data.names()

    # We test without source file to see if variable description in "desc" kwargs
    # is captured (issue #319)
    input_file_path = api.generate_inputs(CONFIGURATION_FILE_PATH,
                                          overwrite=True)
    assert input_file_path == pth.join(RESULTS_FOLDER_PATH, "inputs.xml")
    assert pth.exists(input_file_path)
    data = DataFile(input_file_path)
    assert len(data) == 2
    assert "x" in data.names() and "z" in data.names()