From 66d042a0ee3e78b43d933a616ccd020d42a14701 Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Thu, 4 Apr 2024 14:11:13 -0600 Subject: [PATCH] make checks for exceeding length of variable-length vectors more consistent --- src/EXTRA-FIX/fix_ave_correlate_long.cpp | 4 +++- src/fix_ave_correlate.cpp | 6 ++++-- src/fix_ave_histo.cpp | 6 ++++-- src/fix_ave_time.cpp | 5 +++-- src/thermo.cpp | 10 ++++++++-- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/EXTRA-FIX/fix_ave_correlate_long.cpp b/src/EXTRA-FIX/fix_ave_correlate_long.cpp index fc1760b353..7c80365c57 100644 --- a/src/EXTRA-FIX/fix_ave_correlate_long.cpp +++ b/src/EXTRA-FIX/fix_ave_correlate_long.cpp @@ -454,6 +454,8 @@ void FixAveCorrelateLong::end_of_step() scalar = val.val.f->compute_vector(val.argindex-1); // evaluate equal-style or vector-style variable + // if index exceeds vector length, use a zero value + // this can be useful if vector length is not known a priori } else if (val.which == ArgInfo::VARIABLE) { if (val.argindex == 0) @@ -462,7 +464,7 @@ void FixAveCorrelateLong::end_of_step() double *varvec; int nvec = input->variable->compute_vector(val.val.v,&varvec); int index = val.argindex; - if (nvec < index) scalar = 0.0; + if (index > nvec) scalar = 0.0; else scalar = varvec[index-1]; } } diff --git a/src/fix_ave_correlate.cpp b/src/fix_ave_correlate.cpp index 4b9e316b9d..576ddb3c94 100644 --- a/src/fix_ave_correlate.cpp +++ b/src/fix_ave_correlate.cpp @@ -414,7 +414,9 @@ void FixAveCorrelate::end_of_step() scalar = val.val.f->compute_vector(val.argindex-1); // evaluate equal-style or vector-style variable - + // if index exceeds vector length, use a zero value + // this can be useful if vector length is not known a priori + } else if (val.which == ArgInfo::VARIABLE) { if (val.argindex == 0) scalar = input->variable->compute_equal(val.val.v); @@ -422,7 +424,7 @@ void FixAveCorrelate::end_of_step() double *varvec; int nvec = input->variable->compute_vector(val.val.v,&varvec); int index = val.argindex; - if (nvec < index) scalar = 0.0; + if (index > nvec) scalar = 0.0; else scalar = varvec[index-1]; } } diff --git a/src/fix_ave_histo.cpp b/src/fix_ave_histo.cpp index b3ca9e1106..44e64c4d5a 100644 --- a/src/fix_ave_histo.cpp +++ b/src/fix_ave_histo.cpp @@ -600,6 +600,8 @@ void FixAveHisto::end_of_step() } // evaluate equal-style or vector-style or atom-style variable + // if index exceeds vector length, use a zero value + // this can be useful if vector length is not known a priori } else if (val.which == ArgInfo::VARIABLE) { if (kind == GLOBAL && mode == SCALAR) { @@ -607,7 +609,7 @@ void FixAveHisto::end_of_step() else { double *varvec; int nvec = input->variable->compute_vector(val.val.v,&varvec); - if (nvec < j) bin_one(0.0); + if (j > nvec) bin_one(0.0); else bin_one(varvec[j-1]); } @@ -637,7 +639,7 @@ void FixAveHisto::end_of_step() modify->addstep_compute(nvalid); return; } - + irepeat = 0; nvalid = ntimestep + nfreq - static_cast(nrepeat-1)*nevery; modify->addstep_compute(nvalid); diff --git a/src/fix_ave_time.cpp b/src/fix_ave_time.cpp index c88d8e1659..417e0fd97a 100644 --- a/src/fix_ave_time.cpp +++ b/src/fix_ave_time.cpp @@ -562,7 +562,8 @@ void FixAveTime::invoke_scalar(bigint ntimestep) scalar = val.val.f->compute_vector(val.argindex-1); // evaluate equal-style or vector-style variable - // ensure no out-of-range access to vector-style variable + // if index exceeds vector length, use a zero value + // this can be useful if vector length is not known a priori } else if (val.which == ArgInfo::VARIABLE) { if (val.argindex == 0) @@ -570,7 +571,7 @@ void FixAveTime::invoke_scalar(bigint ntimestep) else { double *varvec; int nvec = input->variable->compute_vector(val.val.v,&varvec); - if (nvec < val.argindex) scalar = 0.0; + if (val.argindex > nvec) scalar = 0.0; else scalar = varvec[val.argindex-1]; } } diff --git a/src/thermo.cpp b/src/thermo.cpp index dbb8b2530e..5ef5eb59b8 100644 --- a/src/thermo.cpp +++ b/src/thermo.cpp @@ -1515,6 +1515,8 @@ void Thermo::compute_fix() int m = field2index[ifield]; Fix *fix = fixes[m]; + // check for out-of-range access if vector/array is variable length + if (argindex1[ifield] == 0) { dvalue = fix->compute_scalar(); if (normflag && fix->extscalar) dvalue /= natoms; @@ -1544,13 +1546,17 @@ void Thermo::compute_variable() { int iarg = argindex1[ifield]; + // evaluate equal-style or vector-style variable + // if index exceeds vector length, use a zero value + // this can be useful if vector length is not known a priori + if (iarg == 0) dvalue = input->variable->compute_equal(variables[field2index[ifield]]); else { double *varvec; int nvec = input->variable->compute_vector(variables[field2index[ifield]], &varvec); - if (iarg > nvec) error->all(FLERR, "Thermo vector-style variable is accessed out-of-range"); - dvalue = varvec[iarg - 1]; + if (iarg > nvec) dvalue = 0.0; + else dvalue = varvec[iarg - 1]; } }