示例#1
0
def test_set_auxiliary_files():
    """Test setting the auxiliary_files property."""
    k = Dakota()
    for auxiliary_file in ["foo.in", ["foo.in"], ("foo.in", )]:
        k.auxiliary_files = auxiliary_file
        if type(auxiliary_file) is not str:
            auxiliary_file = auxiliary_file[0]
        pathified_auxiliary_file = os.path.abspath(auxiliary_file)
        assert_equal(k.auxiliary_files, (pathified_auxiliary_file, ))
示例#2
0
def test_default_run_without_input_file():
    """Test default object run method fails with no input file."""
    if is_dakota_installed():
        if os.path.exists(input_file):
            os.remove(input_file)
        try:
            k = Dakota()
            k.run()
        except CalledProcessError:
            pass
示例#3
0
def test_running_in_different_directory():
    """Test ability to provide parameter names."""
    work_folder = "dakota_runs"
    run_directory = os.path.abspath(os.path.join(os.getcwd(), "running_here"))
    work_directory = os.path.abspath(
        os.path.join(run_directory, "..", "working_here"))
    configuration_file = "another_excellent_yaml.yaml"
    run_log = "run_output_should_be_here.log"
    error_log = "no_errors_inside.log"
    parameters_file = "parameters_here.in"
    results_file = "neato_results_here.out"
    input_file = "dakota_LHC.in"
    output_file = "dakota_LHC.out"

    k = Dakota(
        method="sampling",
        variables="uniform_uncertain",
        input_file=input_file,
        output_file=output_file,
        interface="fork",
        run_directory=run_directory,
        work_directory=work_directory,
        work_folder=work_folder,
        configuration_file=configuration_file,
        run_log=run_log,
        error_log=error_log,
        parameters_file=parameters_file,
        results_file=results_file,
    )
    k.write_input_file()
    k.serialize()
    k.run()

    # teardown. First the run directory.
    lhc_filelist = [f for f in os.listdir(".") if f.startswith("LHS")]
    for f in lhc_filelist:
        os.remove(f)
    os.remove(configuration_file)
    os.remove(run_log)
    os.remove(error_log)
    os.remove(input_file)
    os.remove(output_file)
    teardown_module()
    [os.remove(f) for f in os.listdir(".")]
    os.chdir("..")
    os.rmdir(run_directory)

    # Second the working directory.

    num_runs = k.method.samples
    os.chdir(work_directory)
    for i in range(num_runs):
        folder_name = ".".join([work_folder, str(i + 1)])
        os.chdir(folder_name)
        os.remove(parameters_file)
        os.remove(results_file)
        os.chdir("..")
        os.rmdir(folder_name)
    os.chdir("..")
    os.rmdir(work_directory)
示例#4
0
def test_run_from_config_file():
    """Test running a HydroTrend simulation from a config file."""
    d = Dakota.from_file_like(known_config_file)
    d.run_directory = run_dir
    d.template_file = os.path.join(data_dir, "HYDRO.IN.dtmpl")
    d.auxiliary_files = os.path.join(data_dir, "HYDRO0.HYPS")
    d.serialize(config_file)
    d.write_input_file()
    assert_true(os.path.exists(d.input_file))
    if is_dakota_installed() and is_hydrotrend_installed():
        d.run()
        assert_true(os.path.exists(d.output_file))
示例#5
0
def test_default_run_with_input_file():
    """Test default object run method with input file."""
    if is_dakota_installed():
        k = Dakota()
        k.write_input_file()
        k.run()
        assert_true(os.path.exists(k.output_file))
        assert_true(os.path.exists(k.environment.data_file))
示例#6
0
def test_run_by_setting_attributes():
    """Test running a HydroTrend simulation."""
    d = Dakota(method="vector_parameter_study", plugin="hydrotrend")
    d.template_file = os.path.join(data_dir, "HYDRO.IN.dtmpl")
    d.auxiliary_files = os.path.join(data_dir, "HYDRO0.HYPS")
    d.variables.descriptors = [
        "starting_mean_annual_temperature",
        "total_annual_precipitation",
    ]
    d.variables.initial_point = [10.0, 1.5]
    d.method.final_point = [20.0, 2.5]
    d.method.n_steps = 5
    d.responses.response_descriptors = ["Qs_median", "Q_mean"]
    d.responses.response_files = ["HYDROASCII.QS", "HYDROASCII.Q"]
    d.responses.response_statistics = ["median", "mean"]
    d.setup()
    assert_true(os.path.exists(d.input_file))
    if is_dakota_installed() and is_hydrotrend_installed():
        d.run()
        assert_true(os.path.exists(d.output_file))
示例#7
0
def test_changing_parameter_names():
    """Test ability to provide parameter names."""
    run_directory = "looped_runs"
    configuration_file = "an_excellent_yaml.yaml"
    run_log = "run_output_here.log"
    error_log = "no_errors_here.log"
    if is_dakota_installed():
        k = Dakota(
            method="vector_parameter_study",
            run_directory=run_directory,
            configuration_file=configuration_file,
            run_log=run_log,
            error_log=error_log,
        )
        k.write_input_file()
        k.serialize()
        k.run()

        os.remove(configuration_file)
        os.remove(run_log)
        os.remove(error_log)
        teardown_module()
        os.chdir("..")
        os.rmdir(run_directory)
示例#8
0
def test_set_run_directory():
    """Test setting the run_directory property."""
    k = Dakota()
    run_dir = "/foo/bar"
    k.run_directory = run_dir
    assert_equal(k.run_directory, run_dir)
示例#9
0
def test_init_from_file_like1():
    """Test creating an instance from a config file."""
    k = Dakota.from_file_like(known_config_file)
    assert_is_instance(k, Dakota)
示例#10
0
def test_init_from_file_like2():
    """Test creating an instance from an open config file object."""
    with open(known_config_file, "r") as fp:
        k = Dakota.from_file_like(fp)
    assert_is_instance(k, Dakota)
示例#11
0
def test_init_method_parameter():
    """Test constructor with method parameter."""
    k = Dakota(method="vector_parameter_study")
    assert_is_instance(k, Dakota)
示例#12
0
def test_init_method_parameter_unknown_module():
    """Test constructor with method parameter fails with unknown module."""
    k = Dakota(method="__foo$")
示例#13
0
def setup_module():
    """Fixture called before any tests are performed."""
    print("\n*** " + __name__)
    global d
    d = Dakota()
示例#14
0
def test_init_no_parameters():
    """Test constructor with no parameters."""
    k = Dakota()
    assert_is_instance(k, Dakota)
示例#15
0
def test_set_configuration_file():
    """Test setting the configuration_file property."""
    k = Dakota()
    k.configuration_file = known_config_file
    assert_equal(k.configuration_file, known_config_file)
示例#16
0
def test_set_template_file():
    """Test setting the template_file property."""
    k = Dakota()
    template_file = "foo.tmpl"
    k.template_file = template_file
    assert_equal(os.path.basename(k.template_file), template_file)
示例#17
0
def test_set_auxiliary_files_fails_if_scalar():
    """Test that auxiliary_files fails with a non-string scalar."""
    k = Dakota()
    auxiliary_file = 42
    k.auxiliary_files = auxiliary_file
示例#18
0
def test_write_configuration_file():
    """Test serialize method produces config file."""
    k = Dakota(method="vector_parameter_study")
    k.serialize()
    assert_true(os.path.exists(k.configuration_file))
示例#19
0
def test_setup():
    """Test the setup method."""
    k = Dakota(method="vector_parameter_study")
    k.setup()
    assert_true(os.path.exists(k.configuration_file))
    assert_true(filecmp.cmp(known_file, input_file))
示例#20
0
def test_input_file_contents():
    """Test write_input_file results versus a known input file."""
    k = Dakota(method="vector_parameter_study")
    k.write_input_file()
    assert_true(filecmp.cmp(known_file, input_file))
示例#21
0
def test_write_input_file_with_method_new_name():
    """Test write_input_file works when instanced with method and new name."""
    k = Dakota(method="vector_parameter_study")
    k.write_input_file(input_file=alt_input_file)
    assert_true(os.path.exists(k.input_file))
示例#22
0
def setup_module():
    """Called before any tests are performed."""
    print("\n*** " + __name__)
    global d
    d = Dakota(method="sampling", variables="uniform_uncertain")
示例#23
0
def setup():
    """Called at start of any test using it @with_setup()"""
    global d
    d = Dakota.from_file_like(config_file)