Пример #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()
Пример #2
0
    def write_needed_inputs(self,
                            source_file_path: str = None,
                            source_formatter: IVariableIOFormatter = None):
        """
        Writes the input file of the problem with unconnected inputs of the
        configured problem.

        Written value of each variable will be taken:

            1. from input_data if it contains the variable
            2. from defined default values in component definitions

        :param source_file_path: if provided, variable values will be read from it
        :param source_formatter: the class that defines format of input file. if
                                 not provided, expected format will be the default one.
        """
        problem = self.get_problem(read_inputs=False)
        problem.setup()
        variables = DataFile(self.input_file_path, load_data=False)
        variables.update(
            VariableList.from_unconnected_inputs(problem,
                                                 with_optional_inputs=True),
            add_variables=True,
        )
        if source_file_path:
            ref_vars = DataFile(source_file_path, formatter=source_formatter)
            variables.update(ref_vars, add_variables=False)
            for var in variables:
                var.is_input = True
        variables.save()
Пример #3
0
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()