tImprove error handling and error documentation - cngf-pf - continuum model for granular flows with pore-pressure dynamics (renamed from 1d_fd_simple_shear)
 (HTM) git clone git://src.adamsgaard.dk/cngf-pf
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 400068f8de728591976cfe3ae79d324009a1787e
 (DIR) parent 182fc9c9dfc2e04105703ae653fffbc9d3cf09f9
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Thu, 16 Apr 2020 13:22:28 +0200
       
       Improve error handling and error documentation
       
       Diffstat:
         M 1d_fd_simple_shear.1                |       4 +++-
         M 1d_fd_simple_shear.c                |       9 +++++++--
         M fluid.c                             |       5 +++--
         M fluid.h                             |       2 +-
         M max_depth_simple_shear.1            |      15 ++++++++++++++-
         M max_depth_simple_shear.c            |      17 ++++++++++-------
         M shear_flux.1                        |      11 +++++++++++
         M shear_flux.c                        |       2 +-
       
       8 files changed, 50 insertions(+), 15 deletions(-)
       ---
 (DIR) diff --git a/1d_fd_simple_shear.1 b/1d_fd_simple_shear.1
       t@@ -229,7 +229,7 @@ and output files are generated with the interval specified with
        .Fl I Ar file-interval .
        .Sh EXIT STATUS
        .Nm
       -exits 0 on succes, and >0 if a runtime error occurs:
       +exits 0 on success, and >0 if a runtime error occurs:
        .Pp
        .Bl -tag -compact
        .It 0
       t@@ -245,6 +245,8 @@ transient solver error
        fluid solver error
        .It 12
        granular solver error
       +.It 20
       +time step error
        .El
        .\" .Sh EXAMPLES
        .\" .Sh DIAGNOSTICS
 (DIR) diff --git a/1d_fd_simple_shear.c b/1d_fd_simple_shear.c
       t@@ -235,7 +235,10 @@ main(int argc, char* argv[])
        
                if (sim.fluid) {
                        hydrostatic_fluid_pressure_distribution(&sim);
       -                set_largest_fluid_timestep(&sim, 0.5);
       +                if (set_largest_fluid_timestep(&sim, 0.5)) {
       +                        free_arrays(&sim);
       +                        return 20;
       +                }
                }
                compute_effective_stress(&sim);
        
       t@@ -249,8 +252,10 @@ main(int argc, char* argv[])
                        t_begin = clock();
        #endif
        
       -                if (coupled_shear_solver(&sim, MAX_ITER_1D_FD_SIMPLE_SHEAR, RTOL))
       +                if (coupled_shear_solver(&sim, MAX_ITER_1D_FD_SIMPLE_SHEAR, RTOL)) {
       +                        free_arrays(&sim);
                                exit(10);
       +                }
        
        #ifdef BENCHMARK_PERFORMANCE
                        t_end = clock();
 (DIR) diff --git a/fluid.c b/fluid.c
       t@@ -18,7 +18,7 @@ hydrostatic_fluid_pressure_distribution(struct simulation *sim)
         * that the time step should be recalculated if cell sizes or diffusivities 
         * (i.e., permeabilities, porosities, viscosities, or compressibilities)
         * change. The safety factor should be in ]0;1] */
       -void
       +int
        set_largest_fluid_timestep(struct simulation *sim, const double safety)
        {
                int i;
       t@@ -32,7 +32,7 @@ set_largest_fluid_timestep(struct simulation *sim, const double safety)
                                fprintf(stderr, "error: cell spacing negative (%g) in cell %d\n",
                                                dx[i], i);
                                free(dx);
       -                        exit(1);
       +                        return 1;
                        }
                        if (dx[i] < dx_min) dx_min = dx[i];
                }
       t@@ -47,6 +47,7 @@ set_largest_fluid_timestep(struct simulation *sim, const double safety)
                sim->dt = safety*0.5*dx_min*dx_min/diff_max;
                if (sim->file_dt*0.5 < sim->dt)
                        sim->dt = sim->file_dt;
       +        return 0;
        }
        
        static double
 (DIR) diff --git a/fluid.h b/fluid.h
       t@@ -7,7 +7,7 @@ extern struct simulation sim;
        
        void hydrostatic_fluid_pressure_distribution(struct simulation *sim);
        
       -void set_largest_fluid_timestep(struct simulation *sim, const double safety);
       +int set_largest_fluid_timestep(struct simulation *sim, const double safety);
        
        int darcy_solver_1d(struct simulation *sim,
                            const int max_iter,
 (DIR) diff --git a/max_depth_simple_shear.1 b/max_depth_simple_shear.1
       t@@ -68,7 +68,20 @@ skin depth [m]
        .El
        .Sh EXIT STATUS
        .Nm
       -exits 0 on succes, and >0 if an error occurs.
       +exits 0 on succes, and >0 if a runtime error occurs:
       +.Pp
       +.Bl -tag -compact
       +.It 0
       +successful exit
       +.It 1
       +unspecified error
       +.It 2
       +.Xr pledge 2
       +error (OpenBSD only)
       +.It 10
       +skin depth vs. depth error
       +.It 11
       +no stress minima found in search range
        .\" .Sh EXAMPLES
        .\" .Sh DIAGNOSTICS
        .\" For sections 1, 4, 6, 7, 8, and 9 printf/stderr messages only.
 (DIR) diff --git a/max_depth_simple_shear.c b/max_depth_simple_shear.c
       t@@ -53,12 +53,13 @@ skin_depth(const struct simulation *sim)
        
        /* using alternate form: sin(x) + cos(x) = sqrt(2)*sin(x + pi/4) */
        static double
       -eff_normal_stress_gradient(const struct simulation *sim, double d_s, double z_)
       +eff_normal_stress_gradient(struct simulation *sim, double d_s, double z_)
        {
                if (z_/d_s > 10.0) {
                        fprintf(stderr, "error: %s: unrealistic depth: %g m "
                                        "relative to skin depth %g m\n", __func__, z_, d_s);
       -                exit(1);
       +                free_arrays(sim);
       +                exit(10);
                }
        
                return sqrt(2.0)*sin((3.0*M_PI/2.0 - z_/d_s) + M_PI/4.0)
       t@@ -69,7 +70,7 @@ eff_normal_stress_gradient(const struct simulation *sim, double d_s, double z_)
        static double
        zbrent(struct simulation *sim,
               double d_s,
       -       double (*f)(const struct simulation *sim, double, double),
       +       double (*f)(struct simulation *sim, double, double),
               double x1,
               double x2,
               double tol)
       t@@ -87,7 +88,8 @@ zbrent(struct simulation *sim,
                        fprintf(stderr,
                                        "error: %s: no root in range %g,%g m\n",
                                        __func__, x1, x2);
       -                exit(1);
       +                free_arrays(sim);
       +                exit(11);
                }
                fc = fb;
                for (iter=0; iter<MAX_ITER; iter++) {
       t@@ -147,7 +149,8 @@ zbrent(struct simulation *sim,
                fprintf(stderr,
                                "error: %s: exceeded maximum number of iterations",
                                __func__);
       -        exit(10);
       +        free_arrays(sim);
       +        exit(12);
                return NAN;
        }
        
       t@@ -166,7 +169,7 @@ main(int argc, char* argv[])
        #ifdef __OpenBSD__
                if (pledge("stdio", NULL) == -1) {
                        fprintf(stderr, "error: pledge failed");
       -                exit(1);
       +                exit(2);
                }
        #endif
        
       t@@ -249,7 +252,7 @@ main(int argc, char* argv[])
        
                        depth = zbrent(&sim,
                                                   d_s,
       -                       (double (*)(const struct simulation*, double, double))
       +                       (double (*)(struct simulation*, double, double))
                               eff_normal_stress_gradient,
                               depth_limit1,
                               depth_limit2,
 (DIR) diff --git a/shear_flux.1 b/shear_flux.1
       t@@ -37,6 +37,17 @@ into
        .Dl 0.0017711059751388718
        .Dl 0.0017971161104479135
        .Dl 0.0017971161104479135
       +.Sx EXIT STATUS
       +.Nm
       +.Pp
       +.Bl -tag -compact
       +.It 0
       +successful exit
       +.It 1
       +unspecified error
       +.It 2
       +.Xr pledge 2
       +error (OpenBSD only)
        .Sh SEE ALSO
        .Xr 1d_fd_simple_shear 1
        .Xr max_depth_simple_shear 1
 (DIR) diff --git a/shear_flux.c b/shear_flux.c
       t@@ -48,7 +48,7 @@ main(int argc, char* argv[])
        #ifdef __OpenBSD__
                if (pledge("stdio rpath", NULL) == -1) {
                        fprintf(stderr, "error: pledge failed");
       -                exit(1);
       +                exit(2);
                }
        #endif