tgranularpacking.c: add powerlaw option and fix diameter options - granular - granular dynamics simulation
 (HTM) git clone git://src.adamsgaard.dk/granular
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 2be85b00f5f100ff705fda0948ac89e973ff9636
 (DIR) parent 39fd8dcf8a449c4d6b3c828267e3cd413b2648e6
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Thu, 25 Mar 2021 20:41:46 +0100
       
       granularpacking.c: add powerlaw option and fix diameter options
       
       Diffstat:
         M granularpacking.1                   |       8 ++++++--
         M granularpacking.c                   |      32 +++++++++++++++++++------------
         M util.h                              |       2 +-
       
       3 files changed, 27 insertions(+), 15 deletions(-)
       ---
 (DIR) diff --git a/granularpacking.1 b/granularpacking.1
       t@@ -6,10 +6,12 @@
        .Nd generate a packing of granular data
        .Sh SYNOPSIS
        .Nm
       -.Op Fl h
       -.Op Fl t
        .Op Fl D Ar max-diameter
        .Op Fl d Ar min-diameter
       +.Op Fl h
       +.Op Fl P
       +.Op Fl p Ar padding-factor
       +.Op Fl t
        .Op Fl X Ar x-offset
        .Op Fl x Ar nx
        .Op Fl Y Ar y-offset
       t@@ -34,6 +36,8 @@ Specify minimum size of generated grains (default 1.0).
        Show help text.
        .It Fl t
        Generate triangular packing (default: rectangular).
       +.It Fl P
       +Draw random diameters using a power-law distribution (default: uniform).
        .It Fl p Ar padding-factor
        Add padding between grains and add corresponding random variation
        to placement with the specified multiplier, respective to the grain
 (DIR) diff --git a/granularpacking.c b/granularpacking.c
       t@@ -13,10 +13,12 @@ char *argv0;
        static void
        usage(void)
        {
       -        errx(1, "usage: %s [-ht] "
       +        errx(1, "usage: %s "
                         "[-D max-diameter] "
                         "[-d min-diameter] "
       +                 "[-hP] "
                         "[-p padding-factor] "
       +                 "[-t] "
                         "[-X x-offset] "
                         "[-x nx] "
                         "[-Y y-offset] "
       t@@ -31,26 +33,30 @@ main(int argc, char *argv[])
                size_t n[] = {10, 10, 1};
                double origo[] = {0.0, 0.0, 0.0};
                double padding = 0.0;
       -        double r_max = 1.0, r_min = 1.0;
       +        double d_max = 1.0, d_min = 1.0;
                struct simulation sim = sim_new();
                int packing = 0;
       +        double (*sizefunc)(double min, double max) = random_value_uniform;
        
                ARGBEGIN {
       +        case 'D':
       +                d_max = atof(EARGF(usage()));
       +                break;
       +        case 'd':
       +                d_min = atof(EARGF(usage()));
       +                break;
                case 'h':
                        usage();
                        break;
       +        case 'P':
       +                sizefunc = (*random_value_powerlaw);
       +                break;
                case 'p':
                        padding = atof(EARGF(usage()));
                        break;
                case 't':
                        packing = 1;
                        break;
       -        case 'R':
       -                r_max = atof(EARGF(usage()));
       -                break;
       -        case 'r':
       -                r_min = atof(EARGF(usage()));
       -                break;
                case 'X':
                        origo[0] = atof(EARGF(usage()));
                        break;
       t@@ -78,12 +84,14 @@ main(int argc, char *argv[])
                
                switch (packing) {
                case 0:
       -                sim.np += rectangular_packing(&sim.grains, n, r_min, r_max,
       -                                              random_value_uniform, padding, origo);
       +                sim.np += rectangular_packing(&sim.grains, n,
       +                                              d_min / 2.0, d_max / 2.0,
       +                                              sizefunc, padding, origo);
                        break;
                case 1:
       -                sim.np += triangular_packing(&sim.grains, n, r_min, r_max,
       -                                             random_value_uniform, padding, origo);
       +                sim.np += triangular_packing(&sim.grains, n,
       +                                             d_min / 2.0, d_max / 2.0,
       +                                             sizefunc, padding, origo);
                        break;
                default:
                        errx(1, "unknown packing mode");
 (DIR) diff --git a/util.h b/util.h
       t@@ -13,7 +13,7 @@ void check_int_non_negative(const char name[], const int value, int *status);
        double residual(const double new_val, const double old_val);
        
        double random_value_uniform(double min, double max);
       -double random_value_powerlaw(double power, double min, double max);
       +double random_value_powerlaw(double min, double max);
        
        void * xreallocarray(void *m, size_t n, size_t s);