def test_problem_read_inputs_before_setup(cleanup):
    """Tests what happens when reading inputs using existing XML with correct var"""

    problem = FASTOADProblem()
    problem.model.add_subsystem("sellar", Sellar(), promotes=["*"])

    problem.input_file_path = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml")

    problem.read_inputs()
    problem.setup()
    problem.run_model()

    assert_allclose(problem.get_val(name="x"), 1.0)
    assert_allclose(problem.get_val(name="z", units="m**2"), [4.0, 3.0])
    assert_allclose(problem["f"], 21.7572, atol=1.0e-4)
Esempio n. 2
0
            def patched_function(inputs_dict: dict) -> dict:
                """
                The patched function perform a run of an openmdao component or group applying FASTOAD formalism.

                @param inputs_dict: dictionary of input (values, units) saved with their key name,
                as an example: inputs_dict = {'in1': (3.0, "m")}.
                @return: dictionary of the component/group outputs saving names as keys and (value, units) as tuple.
                """


                # Read .xml file and construct Independent Variable Component excluding outputs
                reader.path_separator = ":"
                ivc_local = reader.to_ivc()
                for name, value in inputs_dict.items():
                    ivc_local.add_output(name, value[0], units=value[1])
                group_local = AutoUnitsDefaultGroup()
                group_local.add_subsystem('system', system, promotes=["*"])
                group_local.add_subsystem('ivc', ivc_local, promotes=["*"])
                problem_local = FASTOADProblem(group_local)
                problem_local.setup()
                problem_local.run_model()
                if overwrite:
                    problem_local.output_file_path = xml_file_path
                    problem_local.write_outputs()
                # Get output names from component/group and construct dictionary
                outputs_units = [var.units for var in variables if not var.is_input]
                outputs_dict = {}
                for idx in range(len(outputs_names)):
                    value = problem_local.get_val(outputs_names[idx], outputs_units[idx])
                    outputs_dict[outputs_names[idx]] = (value, outputs_units[idx])
                return outputs_dict
def test_problem_read_inputs_after_setup(cleanup):
    """Tests what happens when reading inputs using existing XML with correct var"""

    problem = FASTOADProblem()
    problem.model.add_subsystem("sellar", Sellar(), promotes=["*"])

    problem.input_file_path = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml")

    problem.setup()

    assert problem.get_val(name="x") == [2.0]
    with pytest.raises(RuntimeError):
        # Several default values are defined for "z", thus OpenMDAO raises an error that
        # will be solved only after run_model() has been used.
        _ = problem.get_val(name="z", units="m**2")

    problem.read_inputs()

    problem.run_model()
    assert_allclose(problem.get_val(name="x"), 1.0)
    assert_allclose(problem.get_val(name="z", units="m**2"), [4.0, 3.0])
    assert_allclose(problem["f"], 21.7572, atol=1.0e-4)
def test_problem_with_case_recorder(cleanup):
    """Tests what happens when using a case recorder"""
    # Adding a case recorder may cause a crash in case of deepcopy.

    problem = FASTOADProblem()
    sellar = Sellar()
    sellar.nonlinear_solver = om.NonlinearBlockGS(
    )  # Solver that is compatible with deepcopy
    sellar.add_recorder(
        om.SqliteRecorder(pth.join(RESULTS_FOLDER_PATH, "cases.sql")))

    problem.model.add_subsystem("sellar", sellar, promotes=["*"])

    problem.input_file_path = pth.join(DATA_FOLDER_PATH, "ref_inputs.xml")

    problem.setup()
    problem.read_inputs()
    problem.run_model()

    assert_allclose(problem.get_val(name="x"), 1.0)
    assert_allclose(problem.get_val(name="z", units="m**2"), [4.0, 3.0])
    assert_allclose(problem["f"], 21.7572, atol=1.0e-4)