tadd granularenergy program and include in example - granular - granular dynamics simulation
 (HTM) git clone git://src.adamsgaard.dk/granular
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit b52aa0ae6674ec667526bffdae46b8ebd959c272
 (DIR) parent 500b39e44f515253bc271988f5f922a9e9c3f36a
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Wed,  5 May 2021 15:09:16 +0200
       
       add granularenergy program and include in example
       
       Diffstat:
         M Makefile                            |       4 ++++
         M examples/two-grain-collision.sh     |      11 +++++++++++
         M grain.c                             |       6 ++++++
         M grain.h                             |       1 +
         M grains.c                            |      19 +++++++++++++++++++
         M grains.h                            |       1 +
         A granularenergy.1                    |      45 +++++++++++++++++++++++++++++++
         A granularenergy.c                    |      21 +++++++++++++++++++++
         M simulation.c                        |       6 ++++++
         M simulation.h                        |       1 +
       
       10 files changed, 115 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/Makefile b/Makefile
       t@@ -11,6 +11,7 @@ DOCPREFIX = ${PREFIX}/share/doc/${NAME}
        BIN = \
                granular\
                granular2vtu\
       +        granularenergy\
                granulargrain\
                granularpacking
        SCRIPTS = \
       t@@ -68,6 +69,9 @@ granular: granular.o ${OBJ}
        granular2vtu: granular2vtu.o ${OBJ}
                ${CC} granular2vtu.o ${OBJ} -o $@ ${GRANULAR_LDFLAGS}
        
       +granularenergy: granularenergy.o ${OBJ}
       +        ${CC} granularenergy.o ${OBJ} -o $@ ${GRANULAR_LDFLAGS}
       +
        granulargrain: granulargrain.o ${OBJ}
                ${CC} granulargrain.o ${OBJ} -o $@ ${GRANULAR_LDFLAGS}
        
 (DIR) diff --git a/examples/two-grain-collision.sh b/examples/two-grain-collision.sh
       t@@ -16,3 +16,14 @@ done
        ffmpeg -y -framerate 5 -i ${id}.grains.%05d.png \
                -c:v libx264 -r 30 -pix_fmt yuv420p ${id}.mp4
        xdg-open ${id}.mp4 
       +
       +> "${id}.energy.tsv"
       +for f in ${id}.grains.*.tsv; do
       +        granularenergy < "$f" >> "${id}.energy.tsv"
       +done
       +gnuplot -e "set term png;\
       +            set xlabel 'time step';\
       +            set ylabel 'Energy [J]';\
       +                        plot '-' u 0:1 w lp t 'Total energy'" \
       +                         < "${id}.energy.tsv" > "${id}.energy.png"
       +xdg-open "${id}.energy.png"
 (DIR) diff --git a/grain.c b/grain.c
       t@@ -319,6 +319,12 @@ grain_kinetic_energy(const struct grain *g)
                        grain_rotational_kinetic_energy(g);
        }
        
       +double
       +grain_thermal_energy(const struct grain *g)
       +{
       +        return g->thermal_energy;
       +}
       +
        static void
        grain_temporal_integration_two_term_taylor(struct grain *g, double dt)
        {
 (DIR) diff --git a/grain.h b/grain.h
       t@@ -56,6 +56,7 @@ void grain_force_reset(struct grain *g);
        double grain_translational_kinetic_energy(const struct grain *g);
        double grain_rotational_kinetic_energy(const struct grain *g);
        double grain_kinetic_energy(const struct grain *g);
       +double grain_thermal_energy(const struct grain *g);
        void grain_temporal_integration(struct grain *g, double dt);
        
        void grain_register_contact(struct grain *g, size_t i, size_t j,
 (DIR) diff --git a/grains.c b/grains.c
       t@@ -97,6 +97,25 @@ grains_print_vtk(FILE *stream, const struct grain *grains, size_t ng)
                        "</VTKFile>\n");
        }
        
       +void
       +grains_print_energy(FILE *stream, const struct grain *grains, size_t ng)
       +{
       +        size_t i;
       +        double E_kin = 0.0,
       +               E_rot = 0.0,
       +               E_thermal = 0.0;
       +        
       +        for (i = 0; i < ng; i++) {
       +                E_kin += grain_translational_kinetic_energy(&grains[i]);
       +                E_rot += grain_rotational_kinetic_energy(&grains[i]);
       +                E_thermal += grain_thermal_energy(&grains[i]);
       +        }
       +
       +        fprintf(stream, "%.17g\t%.17g\t%.17g\t%.17g\n",
       +                E_kin + E_rot + E_thermal,
       +                E_kin, E_rot, E_thermal);
       +}
       +
        double
        grains_minimum_grain_edge(const struct grain *grains, size_t ng, int d)
        {
 (DIR) diff --git a/grains.h b/grains.h
       t@@ -5,6 +5,7 @@
        
        void grains_print(FILE *stream, const struct grain *grains, size_t ng);
        void grains_print_vtk(FILE *stream, const struct grain *grains, size_t ng);
       +void grains_print_energy(FILE *stream, const struct grain *grains, size_t ng);
        
        double sim_minimum_grain_edge(const struct grain *grains, size_t ng, int d);
        double sim_maximum_grain_edge(const struct grain *grains, size_t ng, int d);
 (DIR) diff --git a/granularenergy.1 b/granularenergy.1
       t@@ -0,0 +1,45 @@
       +.Dd $Mdocdate$
       +.Dt GRANULARENERGY 1
       +.Os
       +.Sh NAME
       +.Nm granularenergy
       +.Nd calculate energy components from granular data
       +.Sh SYNOPSIS
       +.Nm
       +.Sh DESCRIPTION
       +The
       +.Nm
       +utility reads
       +.Xr granular 5
       +formatted data and outputs the total and individual energy components
       +for the entire granular assemblage as tab-separated values:
       +.Pp
       +.Bl -enum -width Ss -compact
       +.It
       +Total energy [J]
       +.It
       +Translational (linear) kinetic energy [J]
       +.It
       +Rotational (angular) kinetic energy [J]
       +.It
       +Thermal energy [J]
       +.El
       +.Sh EXIT STATUS
       +.Nm
       +exits 0 on success, and >0 if a runtime error occurs:
       +.Pp
       +.Bl -tag -width Ds -compact
       +.It 0
       +successful exit
       +.It 1
       +unspecified error
       +.It 2
       +.Xr pledge 2
       +error
       +.El
       +.Sh SEE ALSO
       +.Xr granular2img 1 ,
       +.Xr granular2vtk 1 ,
       +.Xr granular 5
       +.Sh AUTHORS
       +.An Anders Damsgaard Aq Mt anders@adamsgaard.dk
 (DIR) diff --git a/granularenergy.c b/granularenergy.c
       t@@ -0,0 +1,21 @@
       +#include <stdlib.h>
       +#include <unistd.h>
       +#include <err.h>
       +#include "granular.h"
       +
       +int
       +main(void)
       +{
       +        struct simulation sim = sim_new();
       +
       +#ifdef __OpenBSD__
       +        if (pledge("stdio", NULL) == -1)
       +                err(2, "pledge failed");
       +#endif
       +
       +        sim_read_grains(&sim, stdin);
       +        sim_print_grains_energy(stdout, &sim);
       +        sim_free(&sim);
       +
       +        return 0;
       +}
 (DIR) diff --git a/simulation.c b/simulation.c
       t@@ -92,6 +92,12 @@ sim_print_grains_vtk(FILE *stream, const struct simulation *sim)
        }
        
        void
       +sim_print_grains_energy(FILE *stream, const struct simulation *sim)
       +{
       +        grains_print_energy(stream, sim->grains, sim->ng);
       +}
       +
       +void
        sim_write_output_files(struct simulation *sim)
        {
                int ret;
 (DIR) diff --git a/simulation.h b/simulation.h
       t@@ -40,6 +40,7 @@ void sim_read_grains(struct simulation *sim, FILE *stream);
        
        void sim_print_grains(FILE *stream, const struct simulation *sim);
        void sim_print_grains_vtk(FILE *stream, const struct simulation *sim);
       +void sim_print_grains_energy(FILE *stream, const struct simulation *sim);
        void sim_write_output_files(struct simulation *sim);
        
        void sim_force_reset(struct simulation *sim);