cngf-pf

continuum model for granular flows with pore-pressure dynamics (renamed from 1d_fd_simple_shear)
git clone git://src.adamsgaard.dk/cngf-pf # fast
git clone https://src.adamsgaard.dk/cngf-pf.git # slow
Log | Files | Refs | README | LICENSE Back to index

commit 76e6b4ac97282ee09d512c61cda430007884b345
parent a715c6d8412ec46c3df15b56f94a8331d4717870
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Wed, 14 Jan 2026 21:16:46 +0100

perf(arrays): add early exit for max when threshold exceeded

Diffstat:
Marrays.c | 19+++++++++++++++++++
Marrays.h | 1+
Msimulation.c | 4++--
3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/arrays.c b/arrays.c @@ -174,6 +174,25 @@ max(const double *a, const int n) return maxval; } +/* Return maximum value in array, with early exit if threshold exceeded */ +double +max_with_threshold(const double *a, const int n, const double threshold) +{ + int i; + double maxval; + + check_magnitude(__func__, 1, n); + maxval = -INFINITY; + for (i = 0; i < n; ++i) { + if (a[i] > maxval) + maxval = a[i]; + if (maxval > threshold) + return maxval; + } + + return maxval; +} + /* Return smallest value in array `a` with size `n` */ double min(const double *a, const int n) diff --git a/arrays.h b/arrays.h @@ -27,6 +27,7 @@ double * initval(const double value, const int n); double * empty(const int n); double max(const double *a, const int n); +double max_with_threshold(const double *a, const int n, const double threshold); double min(const double *a, const int n); void print_array(const double *a, const int n); diff --git a/simulation.c b/simulation.c @@ -719,7 +719,7 @@ implicit_1d_sor_poisson_solver(struct simulation *sim, sim->dz, sim->xi, omega); - r_norm_max = max(sim->g_r_norm, sim->nz); + r_norm_max = max_with_threshold(sim->g_r_norm, sim->nz, rel_tol); if (r_norm_max <= rel_tol) { set_bc_dirichlet(sim->g_ghost, sim->nz, -1, 0.0); @@ -887,7 +887,7 @@ coupled_shear_solver(struct simulation *sim, if (sim->transient) { for (i = 0; i < sim->nz; ++i) sim->g_r_norm[i] = fabs(residual(sim->phi_dot[i], sim->old_val[i])); - r_norm_max = max(sim->g_r_norm, sim->nz); + r_norm_max = max_with_threshold(sim->g_r_norm, sim->nz, rel_tol); if (r_norm_max <= rel_tol && coupled_iter > 0) break; if (coupled_iter++ >= max_iter) {