From 3bd733bd89f88157870aaba94d4ea3b81ef5d47f Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Thu, 10 Jul 2014 18:24:53 +0200 Subject: [PATCH 1/9] Added zero-padded statepoint names --- src/state_point.F90 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/state_point.F90 b/src/state_point.F90 index 83d768a1ba..e3ce3f9972 100644 --- a/src/state_point.F90 +++ b/src/state_point.F90 @@ -36,15 +36,26 @@ contains subroutine write_state_point() + character(MAX_FILE_LEN) :: batch_form + character(MAX_FILE_LEN) :: batch_string character(MAX_FILE_LEN) :: filename + integer :: n_digits integer :: i integer :: j integer, allocatable :: temp_array(:) type(TallyObject), pointer :: t => null() + ! Count digits needed to represent largest batch number (for file name). + n_digits = 1 + do while (n_batches / 10**(n_digits) /= 0 & + & .and. n_batches / 10 **(n_digits-1) /= 1) + n_digits = n_digits + 1 + end do + ! Convert the current batch batch to a zero-padded string. + write(batch_form, '("(I", I0, ".", I0, ")")') n_digits, n_digits + write(batch_string, batch_form) current_batch ! Set filename for state point - filename = trim(path_output) // 'statepoint.' // & - trim(to_str(current_batch)) + filename = trim(path_output) // 'statepoint.' // batch_string ! Append appropriate extension #ifdef HDF5 From d1f863b2be48078281e6a56d05b5ae22f1b38093 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 11 Jul 2014 16:04:10 +0200 Subject: [PATCH 2/9] Wrapped batch number zero padding into a function --- src/state_point.F90 | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/state_point.F90 b/src/state_point.F90 index e3ce3f9972..5cb2f5ad7a 100644 --- a/src/state_point.F90 +++ b/src/state_point.F90 @@ -36,26 +36,14 @@ contains subroutine write_state_point() - character(MAX_FILE_LEN) :: batch_form - character(MAX_FILE_LEN) :: batch_string character(MAX_FILE_LEN) :: filename - integer :: n_digits integer :: i integer :: j integer, allocatable :: temp_array(:) type(TallyObject), pointer :: t => null() - ! Count digits needed to represent largest batch number (for file name). - n_digits = 1 - do while (n_batches / 10**(n_digits) /= 0 & - & .and. n_batches / 10 **(n_digits-1) /= 1) - n_digits = n_digits + 1 - end do - ! Convert the current batch batch to a zero-padded string. - write(batch_form, '("(I", I0, ".", I0, ")")') n_digits, n_digits - write(batch_string, batch_form) current_batch ! Set filename for state point - filename = trim(path_output) // 'statepoint.' // batch_string + filename = trim(path_output) // 'statepoint.' // zero_padded_batch() ! Append appropriate extension #ifdef HDF5 @@ -821,6 +809,32 @@ contains end subroutine load_state_point +!=============================================================================== +! STATE_POINT -- This module handles writing and reading state point +!=============================================================================== + + function zero_padded_batch() + character(MAX_FILE_LEN) :: zero_padded_batch + + character(MAX_FILE_LEN) :: batch_form + character(MAX_FILE_LEN) :: batch_string + character(MAX_FILE_LEN) :: filename + integer :: n_digits + + ! Count digits needed to represent largest batch number. + n_digits = 1 + do while (n_batches / 10**(n_digits) /= 0 & + & .and. n_batches / 10 **(n_digits-1) /= 1) + n_digits = n_digits + 1 + end do + + ! Write a format string of the form In.n where n is n_digits. + write(batch_form, '("(I", I0, ".", I0, ")")') n_digits, n_digits + + ! Format the batch number + write(zero_padded_batch, batch_form) current_batch + end function zero_padded_batch + subroutine read_source ! TODO write this routine ! TODO what if n_particles does not match source bank From 244e9970e216f21820f858c1206c8467e24d50cf Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 11 Jul 2014 16:09:16 +0200 Subject: [PATCH 3/9] Added zero padded filenames for source files Also correcteted statepoint filenames in write_source_point --- src/state_point.F90 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/state_point.F90 b/src/state_point.F90 index 5cb2f5ad7a..cb276e2700 100644 --- a/src/state_point.F90 +++ b/src/state_point.F90 @@ -304,7 +304,7 @@ contains if (source_separate) then ! Set filename - filename = trim(path_output) // 'source.' // trim(to_str(current_batch)) + filename = trim(path_output) // 'source.' // zero_padded_batch() #ifdef HDF5 filename = trim(filename) // '.h5' #else @@ -324,8 +324,7 @@ contains else ! Set filename for state point - filename = trim(path_output) // 'statepoint.' // & - trim(to_str(current_batch)) + filename = trim(path_output) // 'statepoint.' // zero_padded_batch() #ifdef HDF5 filename = trim(filename) // '.h5' #else From aa2f94ef3485a40968dc78823d6c740cad036552 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 11 Jul 2014 17:29:56 +0200 Subject: [PATCH 4/9] Updated tests for zero padded filenames --- tests/test_sourcepoint_batch/results.py | 2 +- .../test_sourcepoint_batch.py | 2 +- tests/test_sourcepoint_interval/results.py | 2 +- .../test_sourcepoint_interval.py | 2 +- .../test_sourcepoint_restart.py | 14 +++++------ tests/test_statepoint_batch/results.py | 2 +- .../test_statepoint_batch.py | 14 +++++------ .../test_statepoint_interval.py | 16 ++++++------- tests/test_statepoint_restart/results.py | 2 +- .../test_statepoint_restart.py | 24 +++++++++---------- 10 files changed, 40 insertions(+), 40 deletions(-) diff --git a/tests/test_sourcepoint_batch/results.py b/tests/test_sourcepoint_batch/results.py index 9b0f577eb0..96984b2fae 100644 --- a/tests/test_sourcepoint_batch/results.py +++ b/tests/test_sourcepoint_batch/results.py @@ -10,7 +10,7 @@ import statepoint if len(sys.argv) > 1: sp = statepoint.StatePoint(sys.argv[1]) else: - sp = statepoint.StatePoint('statepoint.8.binary') + sp = statepoint.StatePoint('statepoint.08.binary') sp.read_results() sp.read_source() diff --git a/tests/test_sourcepoint_batch/test_sourcepoint_batch.py b/tests/test_sourcepoint_batch/test_sourcepoint_batch.py index a1c89690b8..4258a54de7 100644 --- a/tests/test_sourcepoint_batch/test_sourcepoint_batch.py +++ b/tests/test_sourcepoint_batch/test_sourcepoint_batch.py @@ -30,7 +30,7 @@ def test_statepoint_exists(): 'Statepoint file detected that is not binary or hdf5.' def test_results(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.8.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.08.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: diff --git a/tests/test_sourcepoint_interval/results.py b/tests/test_sourcepoint_interval/results.py index 9b0f577eb0..96984b2fae 100644 --- a/tests/test_sourcepoint_interval/results.py +++ b/tests/test_sourcepoint_interval/results.py @@ -10,7 +10,7 @@ import statepoint if len(sys.argv) > 1: sp = statepoint.StatePoint(sys.argv[1]) else: - sp = statepoint.StatePoint('statepoint.8.binary') + sp = statepoint.StatePoint('statepoint.08.binary') sp.read_results() sp.read_source() diff --git a/tests/test_sourcepoint_interval/test_sourcepoint_interval.py b/tests/test_sourcepoint_interval/test_sourcepoint_interval.py index a583370c35..2a089747f6 100644 --- a/tests/test_sourcepoint_interval/test_sourcepoint_interval.py +++ b/tests/test_sourcepoint_interval/test_sourcepoint_interval.py @@ -30,7 +30,7 @@ def test_statepoint_exists(): 'Statepoint file detected that is not binary or hdf5.' def test_results(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.8.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.08.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: diff --git a/tests/test_sourcepoint_restart/test_sourcepoint_restart.py b/tests/test_sourcepoint_restart/test_sourcepoint_restart.py index d42763be3e..47033b0121 100644 --- a/tests/test_sourcepoint_restart/test_sourcepoint_restart.py +++ b/tests/test_sourcepoint_restart/test_sourcepoint_restart.py @@ -28,7 +28,7 @@ def test_created_statepoint(): assert len(statepoint) == 2, '2 statepoint files must exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint file must either be binary or hdf5.' - sourcepoint = glob.glob(os.path.join(cwd, 'source.7.*')) + sourcepoint = glob.glob(os.path.join(cwd, 'source.07.*')) assert len(sourcepoint) == 1, 'Either multiple or no source files found.' assert sourcepoint[0].endswith('binary') or sourcepoint[0].endswith('h5'),\ 'Source file must either be binary or hdf5.' @@ -43,8 +43,8 @@ def test_results(): os.remove(statepoint[0]) def test_restart_form1(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) - sourcepoint = glob.glob(os.path.join(cwd, 'source.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) + sourcepoint = glob.glob(os.path.join(cwd, 'source.07.*')) if opts.mpi_exec != '': proc = Popen([opts.mpi_exec, '-np', opts.mpi_np, opts.exe, '-r', statepoint[0], sourcepoint[0], cwd], stderr=STDOUT, stdout=PIPE) @@ -70,8 +70,8 @@ def test_results_form1(): os.remove(statepoint[0]) def test_restart_form2(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) - sourcepoint = glob.glob(os.path.join(cwd, 'source.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) + sourcepoint = glob.glob(os.path.join(cwd, 'source.07.*')) if opts.mpi_exec != '': proc = Popen([opts.mpi_exec, '-np', opts.mpi_np, opts.exe, '--restart', statepoint[0], sourcepoint[0], cwd], stderr=STDOUT, stdout=PIPE) @@ -97,8 +97,8 @@ def test_results_form2(): os.remove(statepoint[0]) def test_restart_serial(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) - sourcepoint = glob.glob(os.path.join(cwd, 'source.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) + sourcepoint = glob.glob(os.path.join(cwd, 'source.07.*')) proc = Popen([opts.exe, '--restart', statepoint[0], sourcepoint[0], cwd], stderr=STDOUT, stdout=PIPE) print(proc.communicate()[0]) returncode = proc.returncode diff --git a/tests/test_statepoint_batch/results.py b/tests/test_statepoint_batch/results.py index 31c0ac3caa..112c005167 100644 --- a/tests/test_statepoint_batch/results.py +++ b/tests/test_statepoint_batch/results.py @@ -10,7 +10,7 @@ import statepoint if len(sys.argv) > 1: sp = statepoint.StatePoint(sys.argv[1]) else: - sp = statepoint.StatePoint('statepoint.9.binary') + sp = statepoint.StatePoint('statepoint.09.binary') sp.read_results() # set up output string diff --git a/tests/test_statepoint_batch/test_statepoint_batch.py b/tests/test_statepoint_batch/test_statepoint_batch.py index a594b2c5a9..e1951d23fd 100644 --- a/tests/test_statepoint_batch/test_statepoint_batch.py +++ b/tests/test_statepoint_batch/test_statepoint_batch.py @@ -24,21 +24,21 @@ def test_run(): assert returncode == 0, 'OpenMC did not exit successfully.' def test_statepoints_exist(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.3.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.3 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.03.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.03 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.3 file is not a binary or hdf5 file.' - statepoint = glob.glob(os.path.join(cwd, 'statepoint.6.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.6 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.06.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.06 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.6 file is not a binary or hdf5 file.' - statepoint = glob.glob(os.path.join(cwd, 'statepoint.9.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.9 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.09.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.09 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.9 file is not a binary or hdf5 file.' def test_results(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.9.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.09.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: diff --git a/tests/test_statepoint_interval/test_statepoint_interval.py b/tests/test_statepoint_interval/test_statepoint_interval.py index b7c30d3384..1099566e58 100644 --- a/tests/test_statepoint_interval/test_statepoint_interval.py +++ b/tests/test_statepoint_interval/test_statepoint_interval.py @@ -25,20 +25,20 @@ def test_run(): assert returncode == 0, 'OpenMC did not exit successfully.' def test_statepoints_exist(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.2.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.2 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.02.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.02 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.2 file is not a binary or hdf5 file.' - statepoint = glob.glob(os.path.join(cwd, 'statepoint.4.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.4 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.04.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.04 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.4 file is not a binary or hdf5 file.' - statepoint = glob.glob(os.path.join(cwd, 'statepoint.6.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.6 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.06.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.06 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.6 file is not a binary or hdf5 file.' - statepoint = glob.glob(os.path.join(cwd, 'statepoint.8.*')) - assert len(statepoint) == 1, 'Either multiple or no statepoint.8 files exist.' + statepoint = glob.glob(os.path.join(cwd, 'statepoint.08.*')) + assert len(statepoint) == 1, 'Either multiple or no statepoint.08 files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint.8 file is not a binary or hdf5 file.' statepoint = glob.glob(os.path.join(cwd, 'statepoint.10.*')) diff --git a/tests/test_statepoint_restart/results.py b/tests/test_statepoint_restart/results.py index d0b3a55e04..dd9dedd035 100644 --- a/tests/test_statepoint_restart/results.py +++ b/tests/test_statepoint_restart/results.py @@ -11,7 +11,7 @@ import statepoint if len(sys.argv) > 1: sp = statepoint.StatePoint(sys.argv[1]) else: - sp = statepoint.StatePoint('statepoint.7.binary') + sp = statepoint.StatePoint('statepoint.07.binary') sp.read_results() # extract tally results and convert to vector diff --git a/tests/test_statepoint_restart/test_statepoint_restart.py b/tests/test_statepoint_restart/test_statepoint_restart.py index 9473cc6923..a26a94442a 100644 --- a/tests/test_statepoint_restart/test_statepoint_restart.py +++ b/tests/test_statepoint_restart/test_statepoint_restart.py @@ -24,13 +24,13 @@ def test_run(): assert returncode == 0, 'OpenMC did not exit successfully.' def test_created_statepoint(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) assert len(statepoint) == 1, 'Either multiple or no statepoint files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint file is not a binary or hdf5 file.' def test_results(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: @@ -38,7 +38,7 @@ def test_results(): assert compare, 'Initial test results do not agree.' def test_restart_form1(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) if opts.mpi_exec != '': proc = Popen([opts.mpi_exec, '-np', opts.mpi_np, opts.exe, '-r', statepoint[0], cwd], stderr=STDOUT, stdout=PIPE) @@ -49,13 +49,13 @@ def test_restart_form1(): assert returncode == 0, 'OpenMC restart 1 did not exit successfully.' def test_created_statepoint_form1(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) assert len(statepoint) == 1, 'Either multiple or no statepoint files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint file is not a binary or hdf5 file.' def test_results_form1(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: @@ -63,7 +63,7 @@ def test_results_form1(): assert compare, 'Restart 1 test results do not agree.' def test_restart_form2(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) if opts.mpi_exec != '': proc = Popen([opts.mpi_exec, '-np', opts.mpi_np, opts.exe, '--restart', statepoint[0], cwd], stderr=STDOUT, stdout=PIPE) @@ -74,13 +74,13 @@ def test_restart_form2(): assert returncode == 0, 'OpenMC restart 2 did not exit successfully.' def test_created_statepoint_form2(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) assert len(statepoint) == 1, 'Either multiple or no statepoint files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint file is not a binary or hdf5 file.' def test_results_form2(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: @@ -88,20 +88,20 @@ def test_results_form2(): assert compare, 'Restart 2 test results do not agree.' def test_restart_serial(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) proc = Popen([opts.exe, '--restart', statepoint[0], cwd], stderr=STDOUT, stdout=PIPE) print(proc.communicate()[0]) returncode = proc.returncode assert returncode == 0, 'OpenMC restart serial did not exit successfully.' def test_created_statepoint_serial(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) assert len(statepoint) == 1, 'Either multiple or no statepoint files exist.' assert statepoint[0].endswith('binary') or statepoint[0].endswith('h5'),\ 'Statepoint file is not a binary or hdf5 file.' def test_results_serial(): - statepoint = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + statepoint = glob.glob(os.path.join(cwd, 'statepoint.07.*')) call(['python', 'results.py', statepoint[0]]) compare = filecmp.cmp('results_test.dat', 'results_true.dat') if not compare: @@ -109,7 +109,7 @@ def test_results_serial(): assert compare, 'Restart serial test results do not agree.' def teardown(): - output = glob.glob(os.path.join(cwd, 'statepoint.7.*')) + output = glob.glob(os.path.join(cwd, 'statepoint.07.*')) output.append(os.path.join(cwd, 'results_test.dat')) for f in output: if os.path.exists(f): From 147af675fefc671bb9a4b1fa4fb9090fdae693c3 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 11 Jul 2014 17:36:09 +0200 Subject: [PATCH 5/9] Commented zero_padded_batch --- src/state_point.F90 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/state_point.F90 b/src/state_point.F90 index cb276e2700..a4c4375722 100644 --- a/src/state_point.F90 +++ b/src/state_point.F90 @@ -809,7 +809,9 @@ contains end subroutine load_state_point !=============================================================================== -! STATE_POINT -- This module handles writing and reading state point +! ZERO_PADDED_BATCH -- This function returns the current batch number as a +! string formatted into a zero padded integer. For example, batch 3 out of 100 +! will be returned as '003'. !=============================================================================== function zero_padded_batch() From df261e9a92a6125d7e88dc52aa128ab29c610630 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 18 Jul 2014 15:27:36 +0200 Subject: [PATCH 6/9] Moved zero-padding features to string.F90 --- src/state_point.F90 | 40 +++++++---------------------------- src/string.F90 | 51 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/src/state_point.F90 b/src/state_point.F90 index a4c4375722..8aff2ec364 100644 --- a/src/state_point.F90 +++ b/src/state_point.F90 @@ -16,7 +16,7 @@ module state_point use error, only: fatal_error, warning use global use output, only: write_message, time_stamp - use string, only: to_str + use string, only: to_str, zero_padded, count_digits use output_interface use tally_header, only: TallyObject @@ -43,7 +43,8 @@ contains type(TallyObject), pointer :: t => null() ! Set filename for state point - filename = trim(path_output) // 'statepoint.' // zero_padded_batch() + filename = trim(path_output) // 'statepoint.' // & + & zero_padded(current_batch, count_digits(n_batches)) ! Append appropriate extension #ifdef HDF5 @@ -304,7 +305,9 @@ contains if (source_separate) then ! Set filename - filename = trim(path_output) // 'source.' // zero_padded_batch() + filename = trim(path_output) // 'source.' // & + & zero_padded(current_batch, count_digits(n_batches)) + #ifdef HDF5 filename = trim(filename) // '.h5' #else @@ -324,7 +327,8 @@ contains else ! Set filename for state point - filename = trim(path_output) // 'statepoint.' // zero_padded_batch() + filename = trim(path_output) // 'statepoint.' // & + & zero_padded(current_batch, count_digits(n_batches)) #ifdef HDF5 filename = trim(filename) // '.h5' #else @@ -808,34 +812,6 @@ contains end subroutine load_state_point -!=============================================================================== -! ZERO_PADDED_BATCH -- This function returns the current batch number as a -! string formatted into a zero padded integer. For example, batch 3 out of 100 -! will be returned as '003'. -!=============================================================================== - - function zero_padded_batch() - character(MAX_FILE_LEN) :: zero_padded_batch - - character(MAX_FILE_LEN) :: batch_form - character(MAX_FILE_LEN) :: batch_string - character(MAX_FILE_LEN) :: filename - integer :: n_digits - - ! Count digits needed to represent largest batch number. - n_digits = 1 - do while (n_batches / 10**(n_digits) /= 0 & - & .and. n_batches / 10 **(n_digits-1) /= 1) - n_digits = n_digits + 1 - end do - - ! Write a format string of the form In.n where n is n_digits. - write(batch_form, '("(I", I0, ".", I0, ")")') n_digits, n_digits - - ! Format the batch number - write(zero_padded_batch, batch_form) current_batch - end function zero_padded_batch - subroutine read_source ! TODO write this routine ! TODO what if n_particles does not match source bank diff --git a/src/string.F90 b/src/string.F90 index e322894b55..d04dbd0532 100644 --- a/src/string.F90 +++ b/src/string.F90 @@ -1,7 +1,7 @@ module string use constants, only: MAX_WORDS, MAX_LINE_LEN, ERROR_INT, ERROR_REAL - use error, only: warning + use error, only: fatal_error, warning use global, only: message implicit none @@ -184,6 +184,39 @@ contains end subroutine upper_case +!=============================================================================== +! ZERO_PADDED returns a string of the input integer padded with zeros to the +! desired number of digits. Do not include the sign in n_digits for negative +! integers. +!=============================================================================== + +function zero_padded(num, n_digits) result(str) + integer, intent(in) :: num + integer, intent(in) :: n_digits + character(11) :: str + + character(8) :: zp_form + + ! Make sure n_digits is reasonable. 10 digits is the maximum needed for the + ! largest integer(4). + if (n_digits > 10) then + message = 'int4_to_padstr called with unreasonably large n_digits (>10)' + call fatal_error() + end if + + ! Write a format string of the form '(In.m)' where n is the max width and + ! m is the min width. If a sign is present, then n must be one greater + ! than m. + if (num < 0) then + write(zp_form, '("(I", I0, ".", I0, ")")') n_digits+1, n_digits + else + write(zp_form, '("(I", I0, ".", I0, ")")') n_digits, n_digits + end if + + ! Format the number. + write(str, zp_form) num +end function zero_padded + !=============================================================================== ! IS_NUMBER determines whether a string of characters is all 0-9 characters !=============================================================================== @@ -268,6 +301,22 @@ contains end function ends_with +!=============================================================================== +! COUNT_DIGITS returns the number of digits needed to represent the input +! integer. +!=============================================================================== + + function count_digits(num) result(n_digits) + integer, intent(in) :: num + integer :: n_digits + + n_digits = 1 + do while (num / 10**(n_digits) /= 0 .and. abs(num / 10 **(n_digits-1)) /= 1) + n_digits = n_digits + 1 + end do + + end function count_digits + !=============================================================================== ! INT4_TO_STR converts an integer(4) to a string. !=============================================================================== From e99f964d475d2023511a937f2ac3f9d26a97bf1b Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Tue, 29 Jul 2014 10:25:46 +0200 Subject: [PATCH 7/9] Fixed zero_padded error message typo --- src/string.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string.F90 b/src/string.F90 index d04dbd0532..c53645c15e 100644 --- a/src/string.F90 +++ b/src/string.F90 @@ -200,7 +200,7 @@ function zero_padded(num, n_digits) result(str) ! Make sure n_digits is reasonable. 10 digits is the maximum needed for the ! largest integer(4). if (n_digits > 10) then - message = 'int4_to_padstr called with unreasonably large n_digits (>10)' + message = 'zero_padded called with an unreasonably large n_digits (>10)' call fatal_error() end if From 0c998286ad92b5e7e890ec8c28275841188e70e5 Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 30 Jul 2014 15:07:03 +0200 Subject: [PATCH 8/9] Fixed overflow error in count_digits --- src/string.F90 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/string.F90 b/src/string.F90 index c53645c15e..f78b94d07f 100644 --- a/src/string.F90 +++ b/src/string.F90 @@ -311,7 +311,10 @@ end function zero_padded integer :: n_digits n_digits = 1 - do while (num / 10**(n_digits) /= 0 .and. abs(num / 10 **(n_digits-1)) /= 1) + do while (num / 10**(n_digits) /= 0 .and. abs(num / 10 **(n_digits-1)) /= 1& + &.and. n_digits /= 10) + ! Note that 10 digits is the maximum needed to represent an integer(4) so + ! the loop automatically exits when n_digits = 10. n_digits = n_digits + 1 end do From 590fd55b1787d076aa9b0c249f2662d301d04c1f Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Wed, 6 Aug 2014 15:38:17 +0200 Subject: [PATCH 9/9] Fixed tests for zero_padded_statepoint --- src/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ee5f882b7e..0025ba79e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -342,9 +342,9 @@ foreach(test ${TESTS}) # Handle restart tests separately if(${test} MATCHES "test_statepoint_restart") - set(RESTART_FILE statepoint.7.h5) + set(RESTART_FILE statepoint.07.h5) elseif(${test} MATCHES "test_sourcepoint_restart") - set(RESTART_FILE statepoint.7.h5 source.7.h5) + set(RESTART_FILE statepoint.07.h5 source.07.h5) elseif(${test} MATCHES "test_particle_restart") set(RESTART_FILE particle_12_192.h5) else(${test} MATCHES "test_statepoint_restart") @@ -355,9 +355,9 @@ foreach(test ${TESTS}) # Handle restart tests separately if(${test} MATCHES "test_statepoint_restart") - set(RESTART_FILE statepoint.7.binary) + set(RESTART_FILE statepoint.07.binary) elseif(${test} MATCHES "test_sourcepoint_restart") - set(RESTART_FILE statepoint.7.binary source.7.binary) + set(RESTART_FILE statepoint.07.binary source.07.binary) elseif(${test} MATCHES "test_particle_restart") set(RESTART_FILE particle_12_192.binary) else(${test} MATCHES "test_statepoint_restart")