Skip to content

Commit b9b8efb

Browse files
committed
fix runtime validation checks in tools
Replace assert-based checks that validate CLI inputs and file/data shape with exceptions so they remain active in release builds. Keep internal invariant assertions unchanged and preserve minimal diffs without broad formatting edits.
1 parent b43d236 commit b9b8efb

8 files changed

Lines changed: 50 additions & 39 deletions

File tree

c++/coef.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <vector>
55
#include <fstream>
6+
#include <stdexcept>
67
#include "traits.hpp"
78
#include "params.hpp"
89
#include "numerics.hpp" // read_vector
@@ -41,10 +42,10 @@ class coef_table {
4142
template <scalar S> auto read_matrix_table(std::istream &F, const bool nr_is_max_index = false) {
4243
const auto nr = read_one<size_t>(F);
4344
const auto len = nr_is_max_index ? nr+1 : nr;
44-
assert(len >= 1);
45+
if (len < 1) throw std::runtime_error("read_matrix_table(): expected at least one matrix.");
4546
const auto size1 = read_one<size_t>(F);
4647
const auto size2 = read_one<size_t>(F);
47-
assert(size1 == size2);
48+
if (size1 != size2) throw std::runtime_error("read_matrix_table(): expected square matrices.");
4849
using t_matrix = Matrix_traits<S>;
4950
std::vector<t_matrix> vec(len);
5051
for (auto j = 0; j < len; j++)

tools/adapt/adapt.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <map>
1515
#include <algorithm>
1616
#include <functional>
17+
#include <stdexcept>
1718

1819
using namespace std;
1920
using namespace std::string_literals;
@@ -285,24 +286,26 @@ class Adapt {
285286
void set_parameters(const int PREC = 16) {
286287
std::cout << std::setprecision(PREC);
287288
Lambda = LAMBDA(P.P("Lambda", 2.0));
288-
assert(Lambda > 1.0);
289+
if (!(Lambda > 1.0)) throw std::invalid_argument("Lambda must be greater than 1.");
289290
adapt = P.Pbool("adapt", false); // Enable adaptable g(x)? Default is false!!
290291
hardgap = P.Pbool("hardgap", false); // Exclude an interval around omega=0 ?
291292
boundary = P.P("boundary", 0.0); // The boundary of the exclusion interval.
292293
bandrescale = P.P("bandrescale", 1.0); // band rescaling parameter
293294
xmax = P.P("xmax", 30); // Integrate over [1..xmax]
294-
assert(xmax > 1);
295+
if (!(xmax > 1)) throw std::invalid_argument("xmax must be greater than 1.");
295296
xfine = P.P("xfine", 5); // Fine stepsize integral [1..xfine]
296-
assert(xfine > 1);
297+
if (!(xfine > 1)) throw std::invalid_argument("xfine must be greater than 1.");
297298
output_step = P.P("outputstep", 1.0 / 64.0); // Stepsize for output file
298-
assert(output_step <= 1.0);
299+
if (!(output_step <= 1.0)) throw std::invalid_argument("outputstep must be less than or equal to 1.");
299300
dx_fine = P.P("dx_fine", 1e-5); // Integration stepsize in [1..xfine]
300301
dx_fast = P.P("dx_fast", 1e-4); // Integration stepsize in [xfine..xmax]
301302
allowed_error = P.P("allowed_error", 1e-10); // error control for adaptable stepsize
302303
max_subdiv = P.Pint("max_subdiv", 10); // maximum nr of integ. step subdivisions
303-
assert(dx_fine * pow(0.5, max_subdiv) > DBL_EPSILON);
304+
if (!(dx_fine * pow(0.5, max_subdiv) > DBL_EPSILON)) {
305+
throw std::invalid_argument("dx_fine and max_subdiv imply a sub-step below machine precision.");
306+
}
304307
max_abs = P.P("max_abs", 100.0); // Maximal |f(x)|
305-
assert(max_abs > 0.0);
308+
if (!(max_abs > 0.0)) throw std::invalid_argument("max_abs must be greater than 0.");
306309
convergence_eps = P.P("secant_eps", 1e-4);
307310
factor0 = 1.0 + P.P("secant_factor", 1e-7);
308311
max_iter = P.Pint("secant_max_iter", 10);

tools/binavg/binavg.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <map>
1515
#include <string>
1616
#include <algorithm>
17+
#include <stdexcept>
1718
#include <unistd.h> // getopt
1819

1920
namespace NRG::BinAvg {
@@ -79,7 +80,7 @@ class BinAvg {
7980
}
8081
name = std::string(argv[optind]); // Name of spectral density files
8182
Nz = atoi(argv[optind + 1]); // Number of z-values
82-
assert(Nz >= 1);
83+
if (!(Nz >= 1)) throw std::invalid_argument("Nz must be greater than or equal to 1.");
8384
std::cout << "Processing: " << name << std::endl;
8485
std::cout << "Nz=" << Nz << std::endl;
8586
}
@@ -101,7 +102,7 @@ class BinAvg {
101102
f.seekg(0, std::ios::end);
102103
const auto end_pos = f.tellg();
103104
const long len = end_pos - begin_pos;
104-
assert(len % (rows * sizeof(double)) == 0);
105+
if (len % (rows * sizeof(double)) != 0) throw std::runtime_error("Input binary file has incomplete row data.");
105106
const int nr = len / (rows * sizeof(double)); // number of lines
106107
if (verbose) { std::cout << "len=" << len << " nr=" << nr << " data points" << std::endl; }
107108
// Allocate the read buffer. The data will be kept in memory for the duration of the calculation!

tools/broaden/broaden.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ auto file_size(std::ifstream &f) {
142142

143143
auto nr_doubles(std::ifstream &f) {
144144
const auto len = file_size(f);
145-
assert(len % sizeof(double) == 0);
145+
if (len % sizeof(double) != 0) throw std::runtime_error("Binary input size is not a multiple of sizeof(double).");
146146
return len/sizeof(double);
147147
}
148148

149149
auto nr_rows(std::ifstream &f, const int cols) {
150150
const auto d = nr_doubles(f);
151-
assert(d % cols == 0);
151+
if (d % cols != 0) throw std::runtime_error("Binary input does not contain a whole number of rows.");
152152
return d / cols;
153153
}
154154

@@ -313,25 +313,25 @@ class Broaden {
313313
}
314314
name = std::string(argv[optind]); // Name of spectral density files
315315
Nz = atoi(argv[optind + 1]); // Number of z-values
316-
assert(Nz >= 1);
316+
if (!(Nz >= 1)) throw std::invalid_argument("Nz must be greater than or equal to 1.");
317317
alpha = atof(argv[optind + 2]); // High-energy broadening parameter
318-
assert(alpha > 0.0);
318+
if (!(alpha > 0.0)) throw std::invalid_argument("alpha must be greater than 0.");
319319
T = atof(argv[optind + 3]); // Temperature
320-
assert(T > 0.0);
320+
if (!(T > 0.0)) throw std::invalid_argument("T must be greater than 0.");
321321
if (remaining == 5) {
322322
omega0_ratio = atof(argv[optind + 4]); // omega0/T
323-
assert(omega0_ratio > 0.0);
323+
if (!(omega0_ratio > 0.0)) throw std::invalid_argument("omega0_ratio must be greater than 0.");
324324
omega0 = omega0_ratio * T;
325325
}
326-
if (remaining == 4) {
327-
omega0_ratio = 1e-9; // Effectively zero
328-
omega0 = omega0_ratio * T;
329-
}
330-
if (finalgaussian && ggamma <= 0.0) throw std::invalid_argument("Final Gaussian width must be greater than 0.");
331-
if (finalderfd && dgamma <= 0.0) throw std::invalid_argument("Final derFD width must be greater than 0.");
332-
if (!meshpositive && !meshnegative) throw std::invalid_argument("Output mesh cannot be empty.");
333-
std::cout << "Processing: " << name << std::endl;
334-
if (verbose) { std::cout << "Nz=" << Nz << " alpha=" << alpha << " T=" << T << " omega0_ratio=" << omega0_ratio << std::endl; }
326+
if (remaining == 4) {
327+
omega0_ratio = 1e-9; // Effectively zero
328+
omega0 = omega0_ratio * T;
329+
}
330+
if (finalgaussian && ggamma <= 0.0) throw std::invalid_argument("Final Gaussian width must be greater than 0.");
331+
if (finalderfd && dgamma <= 0.0) throw std::invalid_argument("Final derFD width must be greater than 0.");
332+
if (!meshpositive && !meshnegative) throw std::invalid_argument("Output mesh cannot be empty.");
333+
std::cout << "Processing: " << name << std::endl;
334+
if (verbose) { std::cout << "Nz=" << Nz << " alpha=" << alpha << " T=" << T << " omega0_ratio=" << omega0_ratio << std::endl; }
335335
}
336336
void check_buffer_normalisation(const std::vector<double> &buffer, const int col_) {
337337
const auto cols = 1 + nrcol;

tools/bw/bw.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <map>
2727
#include <string>
2828
#include <algorithm>
29+
#include <stdexcept>
2930

3031
#include <unistd.h>
3132
#include <getopt.h>
@@ -147,9 +148,9 @@ void cmd_line(int argc, char *argv[]) {
147148
}
148149
name = string(argv[optind]); // Name of spectral density files
149150
b0 = atof(argv[optind + 1]); // Parameter b0 (asymptotic broadening)
150-
assert(b0 > 0.0);
151+
if (!(b0 > 0.0)) throw std::invalid_argument("b0 must be greater than 0.");
151152
Nz = atoi(argv[optind + 2]); // Number of z-values
152-
assert(Nz >= 1);
153+
if (!(Nz >= 1)) throw std::invalid_argument("Nz must be greater than or equal to 1.");
153154

154155
cout << "Processing: " << name << endl;
155156
cout << "b0=" << b0 << " Nz=" << Nz << endl;
@@ -185,7 +186,7 @@ void load(int i) {
185186
f.seekg(0, ios::end);
186187
const ios::pos_type end_pos = f.tellg();
187188
const long len = end_pos - begin_pos;
188-
assert(len % (2 * sizeof(double)) == 0);
189+
if (len % (2 * sizeof(double)) != 0) throw std::runtime_error("Input binary file has incomplete data pairs.");
189190
const int nr = len / (2 * sizeof(double)); // number of pairs of double
190191
if (verbose) { cout << "len=" << len << " nr=" << nr << " data points" << endl; }
191192

tools/kk/kk.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cmath>
2828
#include <functional>
2929
#include <memory>
30+
#include <stdexcept>
3031

3132
#include <gsl/gsl_errno.h>
3233
#include <gsl/gsl_spline.h>
@@ -135,7 +136,7 @@ class KK {
135136
if (im.empty()) throw std::runtime_error("No input data points provided.");
136137
std::sort(im.begin(), im.end());
137138
len = im.size();
138-
assert(len % 2 == 0);
139+
if (len % 2 != 0) throw std::runtime_error("Input grid must contain an even number of points.");
139140
std::tie (Xmin, Xmax) = x_range(im);
140141
if (mode == MODE::FILES) std::cout << "Range: [" << Xmin << " ; " << Xmax << "]" << std::endl;
141142
if (gsl_fcmp(-Xmin, Xmax, 1.e-8) != 0) throw std::runtime_error("Only symmetric intervals are supported!");
@@ -157,7 +158,9 @@ class KK {
157158
if (mode == MODE::FILES) std::cout << "Sum=" << sum << std::endl;
158159
const auto nr = Xpts.size()/2;
159160
for (auto i = nr; i < len; i++)
160-
assert(gsl_fcmp(Xpts[len - i - 1], -Xpts[i], 1e-8) == 0); // Check for the symmetry of the grid
161+
if (gsl_fcmp(Xpts[len - i - 1], -Xpts[i], 1e-8) != 0) {
162+
throw std::runtime_error("Input grid is not symmetric around zero.");
163+
}
161164
Xpos = DVEC(nr); // Xpos are positive and increasing!
162165
std::copy(Xpts.begin() + nr, Xpts.end(), Xpos.begin());
163166
w.reset(gsl_integration_workspace_alloc(workspace_limit));

tools/mats/mats.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <string>
2121
#include <algorithm>
2222
#include <complex>
23+
#include <stdexcept>
2324

2425
#include <unistd.h>
2526
#include <getopt.h>
@@ -93,11 +94,11 @@ void cmd_line(int argc, char *argv[]) {
9394
}
9495
name = string(argv[optind]); // Name of spectral density files
9596
Nz = atoi(argv[optind + 1]); // Number of z-values
96-
assert(Nz >= 1);
97+
if (!(Nz >= 1)) throw std::invalid_argument("Nz must be greater than or equal to 1.");
9798
T = atof(argv[optind + 2]); // Temperature
98-
assert(T > 0.0);
99+
if (!(T > 0.0)) throw std::invalid_argument("T must be greater than 0.");
99100
nrmats = atoi(argv[optind + 3]); // Number of Matsubara points
100-
assert(nrmats >= 1);
101+
if (!(nrmats >= 1)) throw std::invalid_argument("nrmats must be greater than or equal to 1.");
101102
cout << "Processing: " << name << endl;
102103
cout << "Nz=" << Nz << " T=" << T << " nrmats=" << nrmats << endl;
103104
}
@@ -131,7 +132,7 @@ void load(int i) {
131132
f.seekg(0, ios::end);
132133
const ios::pos_type end_pos = f.tellg();
133134
const long len = end_pos - begin_pos;
134-
assert(len % (rows * sizeof(double)) == 0);
135+
if (len % (rows * sizeof(double)) != 0) throw std::runtime_error("Input binary file has incomplete row data.");
135136
const int nr = len / (rows * sizeof(double)); // number of lines
136137
if (verbose) cout << "len=" << len << " nr=" << nr << " data points" << endl;
137138
// Allocate the read buffer. The data will be kept in memory for the

tools/nrgchain/nrgchain.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <algorithm>
1616
#include <ctime>
1717
#include <limits>
18+
#include <stdexcept>
1819

1920
#include <gmp.h>
2021

@@ -145,27 +146,27 @@ void set_parameters() {
145146
cout << setprecision(PREC);
146147

147148
Lambda = LAMBDA(P("Lambda", 2.0));
148-
assert(Lambda > 1.0);
149+
if (!(Lambda > 1.0)) throw std::invalid_argument("Lambda must be greater than 1.");
149150

150151
z = P("z", 1.0);
151-
assert(0 < z && z <= 1.0);
152+
if (!(0 < z && z <= 1.0)) throw std::invalid_argument("z must satisfy 0 < z <= 1.");
152153

153154
adapt = Pbool("adapt", false); // Enable adaptable g(x)? Default is false!!
154155

155156
bandrescale = P("bandrescale", 1.0);
156157
rescalexi = Pbool("rescalexi", false);
157158

158159
xmax = P("xmax", 30); // Interval [1..xmax]
159-
assert(xmax >= 1.0);
160+
if (!(xmax >= 1.0)) throw std::invalid_argument("xmax must be greater than or equal to 1.");
160161

161162
Nmax = Pint("Nmax", 0); // Maximal site index in the Wilson chain
162163

163164
mMAX = Pint("mMAX", 2 * Nmax); // Maximal index of coefficients (e,f)
164165

165-
assert(mMAX > 0);
166+
if (!(mMAX > 0)) throw std::invalid_argument("mMAX must be greater than 0.");
166167

167168
preccpp = Pint("preccpp", 2000); // Precision for GMP
168-
assert(preccpp > 10);
169+
if (!(preccpp > 10)) throw std::invalid_argument("preccpp must be greater than 10.");
169170

170171
band = Pstr("band", "adapt"); // Default: load FSOL*.dat
171172

0 commit comments

Comments
 (0)