diff --git a/saltproc/app.py b/saltproc/app.py index 8d9098a87..3dc66f575 100644 --- a/saltproc/app.py +++ b/saltproc/app.py @@ -180,6 +180,10 @@ def read_main_input(main_inp_file): output_path = (input_path / output_path) j['output_path'] = output_path.resolve() + # Create output directoy if it doesn't exist + if not Path(j['output_path']).exists(): + Path(j['output_path']).mkdir(parents=True) + # Class settings depcode_input = j['depcode'] simulation_input = j['simulation'] diff --git a/saltproc/input_schema.json b/saltproc/input_schema.json index b7bff026d..9b9f370b5 100644 --- a/saltproc/input_schema.json +++ b/saltproc/input_schema.json @@ -18,7 +18,7 @@ "description": "Path output data storing folder", "type": "string", "pattern": "^(.\\/)*(.*)$", - "default": "saltproc_results" + "default": "saltproc_runtime" }, "n_depletion_steps": { "description": "Number of steps for constant power and depletion interval case", @@ -259,6 +259,7 @@ "db_name": { "description": "Output HDF5 database file name", "type": "string", + "default": "saltproc_results.h5", "pattern": "^(.*)\\.h5$"}, "restart_flag": { "description": "Restart simulation from the step when it stopped?", diff --git a/tests/conftest.py b/tests/conftest.py index ecd251679..7363bbaf4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ from pathlib import Path import pytest -from saltproc.app import read_main_input, _create_depcode_object, _create_reactor_object +from saltproc.app import read_main_input, _create_depcode_object, _create_simulation_object, _create_reactor_object from saltproc import Simulation @@ -23,54 +23,67 @@ def path_test_file(cwd): @pytest.fixture(scope='session') -def serpent_depcode(cwd): - """SerpentDepcode object for unit tests""" +def serpent_runtime(cwd, tmpdir_factory): + """SaltProc objects for Serpent unit tests""" saltproc_input = str(cwd / 'serpent_data' / 'tap_input.json') _, _, _, object_input = read_main_input(saltproc_input) depcode = _create_depcode_object(object_input[0]) depcode.runtime_inputfile = str(cwd / 'serpent_data' / 'tap_reference') + output_dir = str(depcode.output_path).split('/')[-1] + depcode.output_path = tmpdir_factory.mktemp(f'serpent_{output_dir}') - return depcode + simulation = _create_simulation_object(object_input[1], depcode, 1, 1) + #db_path=str( + # cwd / + # 'serpent_data' / + # 'tap_reference_db.h5')) + + reactor = _create_reactor_object(object_input[2]) + + return depcode, simulation, reactor @pytest.fixture(scope='session') -def serpent_reactor(cwd): - saltproc_input = str(cwd / 'serpent_data' / 'tap_input.json') - _, _, _, object_input = read_main_input(saltproc_input) - reactor = _create_reactor_object(object_input[2]) +def serpent_depcode(serpent_runtime): + """SerpentDepcode object for unit tests""" + depcode, _, _ = serpent_runtime + return depcode + +@pytest.fixture(scope='session') +def serpent_reactor(serpent_runtime): + _, _, reactor = serpent_runtime return reactor @pytest.fixture(scope='session') -def openmc_depcode(cwd): - """OpenMCDepcode object for unit tests""" +def simulation(serpent_runtime): + """Simulation object for unit tests""" + _, simulation, _ = serpent_runtime + return simulation + + +@pytest.fixture(scope='session') +def openmc_runtime(cwd, tmpdir_factory): + """SaltProc objects for OpenMC unit tests""" saltproc_input = str(cwd / 'openmc_data' / 'tap_input.json') _, _, _, object_input = read_main_input(saltproc_input) depcode = _create_depcode_object(object_input[0]) + output_dir = str(depcode.output_path).split('/')[-1] + depcode.output_path = tmpdir_factory.mktemp(f'openmc_{output_dir}') + reactor = _create_reactor_object(object_input[2]) - return depcode + return depcode, reactor @pytest.fixture(scope='session') -def openmc_reactor(cwd): - openmc_input = str(cwd / 'openmc_data' / 'tap_input.json') - _, _, _, object_input = read_main_input(openmc_input) - reactor = _create_reactor_object(object_input[2]) - - return reactor +def openmc_depcode(openmc_runtime): + """OpenMCDepcode objects for unit tests""" + depcode, _ = openmc_runtime + return depcode @pytest.fixture(scope='session') -def simulation(cwd, serpent_depcode): - """Simulation object for unit tests""" - simulation = Simulation( - sim_name='test_simulation', - sim_depcode=serpent_depcode, - core_number=1, - node_number=1, - db_path=str( - cwd / - 'serpent_data' / - 'tap_reference_db.h5')) - return simulation +def openmc_reactor(openmc_runtime): + _, reactor = openmc_runtime + return reactor diff --git a/tests/integration_tests/file_interface_openmc/test.py b/tests/integration_tests/file_interface_openmc/test.py index 80526a90e..1893317b5 100644 --- a/tests/integration_tests/file_interface_openmc/test.py +++ b/tests/integration_tests/file_interface_openmc/test.py @@ -79,7 +79,7 @@ def test_write_depletion_settings(openmc_depcode, msr): with open(openmc_depcode.output_path / 'depletion_settings.json') as f: j = json.load(f) assert Path(j['directory']).resolve() == Path( - openmc_depcode.runtime_inputfile['settings']).parents[0] + openmc_depcode.output_path) assert j['timesteps'][0] == msr.depletion_timesteps[0] assert j['operator_kwargs']['chain_file'] == \ openmc_depcode.chain_file_path diff --git a/tests/integration_tests/run_constant_reprocessing/test.py b/tests/integration_tests/run_constant_reprocessing/test.py index 1c12b7a34..0d41bf6b9 100644 --- a/tests/integration_tests/run_constant_reprocessing/test.py +++ b/tests/integration_tests/run_constant_reprocessing/test.py @@ -1,5 +1,6 @@ """Run SaltProc with reprocessing""" from pathlib import Path +import shutil import numpy as np import pytest @@ -11,7 +12,7 @@ @pytest.fixture def setup(scope='module'): cwd = str(Path(__file__).parents[0].resolve()) - test_db = cwd + '/test_db.h5' + test_db = cwd + '/saltproc_runtime/saltproc_results.h5' ref_db = cwd + '/tap_reference_db.h5' tol = 1e-9 @@ -28,6 +29,8 @@ def test_integration_2step_constant_ideal_removal_heavy(setup): np.testing.assert_equal(read_keff(test_db), read_keff(ref_db)) assert_db_almost_equal(test_db, ref_db, tol) + shutil.rmtree(cwd + '/saltproc_runtime') + def read_keff(file): db = tb.open_file(file, mode='r') sim_param = db.root.simulation_parameters diff --git a/tests/integration_tests/run_no_reprocessing/test.py b/tests/integration_tests/run_no_reprocessing/test.py index 7fbe545fd..4c923a2e4 100644 --- a/tests/integration_tests/run_no_reprocessing/test.py +++ b/tests/integration_tests/run_no_reprocessing/test.py @@ -1,7 +1,6 @@ """Run SaltProc without reprocessing""" -import os -import glob from pathlib import Path +import shutil import numpy as np import pytest @@ -14,31 +13,28 @@ @pytest.fixture def setup(): cwd = str(Path(__file__).parents[0].resolve()) - main_input = cwd + '/test_input.json' + saltproc_input = cwd + '/test_input.json' input_path, process_input_file, path_input_file, object_input = \ - app.read_main_input(main_input) + app.read_main_input(saltproc_input) depcode = app._create_depcode_object(object_input[0]) - sss_file = cwd + '/_test' - depcode.runtime_inputfile = sss_file - depcode.runtime_matfile = cwd + '/_test_mat' simulation = app._create_simulation_object(object_input[1], depcode, 1, 1) - reactor = app._create_reactor_object(object_input[2], object_input[0]['codename']) + reactor = app._create_reactor_object(object_input[2]) - return cwd, simulation, reactor, sss_file + return cwd, simulation, reactor @pytest.mark.slow def test_integration_2step_saltproc_no_reproc_heavy(setup): - cwd, simulation, reactor, sss_file = setup + cwd, simulation, reactor = setup runsim_no_reproc(simulation, reactor, 2) - saltproc_out = sss_file + '_dep.m' + output_path = str(simulation.sim_depcode.output_path) ref_result = serpent.parse_dep(cwd + '/reference_dep.m', make_mats=False) - test_result = serpent.parse_dep(saltproc_out, make_mats=False) + test_result = serpent.parse_dep(f'{output_path}/runtime_input.serpent_dep.m', make_mats=False) ref_mdens_error = np.loadtxt(cwd + '/reference_error') @@ -47,13 +43,8 @@ def test_integration_2step_saltproc_no_reproc_heavy(setup): test_mdens_error = np.array(ref_fuel_mdens - test_fuel_mdens) np.testing.assert_array_almost_equal(test_mdens_error, ref_mdens_error) - # Cleaning after testing - out_file_list = glob.glob(cwd + '/_test*') - for file in out_file_list: - try: - os.remove(file) - except OSError: - print("Error while deleting file : ", file) + + shutil.rmtree(cwd + '/saltproc_runtime') def runsim_no_reproc(simulation, reactor, nsteps): diff --git a/tests/unit_tests/test_app.py b/tests/unit_tests/test_app.py index bf1a54230..197f583fd 100644 --- a/tests/unit_tests/test_app.py +++ b/tests/unit_tests/test_app.py @@ -47,7 +47,7 @@ def test_read_main_input(cwd, codename, ext): str((input_path / 'tap_template.ini').resolve()) assert simulation_input['db_name'] == \ - str((data_path / '../temp_data/db_saltproc.h5').resolve()) + str((data_path / f'../{codename}_data/saltproc_runtime/saltproc_results.h5').resolve()) assert simulation_input['restart_flag'] is False np.testing.assert_equal( diff --git a/tests/unit_tests/test_simulation.py b/tests/unit_tests/test_simulation.py index f82a99b12..6d19016f8 100644 --- a/tests/unit_tests/test_simulation.py +++ b/tests/unit_tests/test_simulation.py @@ -1,4 +1,5 @@ """Test Simulation functions""" +from pathlib import Path def test_check_switch_geo_trigger(simulation): @@ -21,4 +22,7 @@ def test_check_switch_geo_trigger(simulation): def test_read_k_eds_delta(simulation): + old_db_path = simulation.db_path + simulation.db_path = str(Path(simulation.db_path).parents[1] / 'tap_reference_db.h5') assert simulation.read_k_eds_delta(7) is False + simulation.db_path = old_db_path