From fc216edf10ad7652e34898695ff5bbcf0003e5df Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Wed, 3 Jun 2020 22:36:51 -0400
Subject: [PATCH] simplify create/delete_atoms/bonds

---
 src/create_bonds.cpp | 19 ++++---------
 src/delete_atoms.cpp | 65 +++++++++++++------------------------------
 src/delete_bonds.cpp | 66 +++++++++++++++-----------------------------
 3 files changed, 47 insertions(+), 103 deletions(-)

diff --git a/src/create_bonds.cpp b/src/create_bonds.cpp
index 437f5959db..e210a5e061 100644
--- a/src/create_bonds.cpp
+++ b/src/create_bonds.cpp
@@ -20,6 +20,7 @@
 #include "create_bonds.h"
 #include <mpi.h>
 #include <cstring>
+#include <string>
 #include "atom.h"
 #include "domain.h"
 #include "force.h"
@@ -30,6 +31,8 @@
 #include "group.h"
 #include "special.h"
 #include "error.h"
+#include "utils.h"
+#include "fmt/format.h"
 
 using namespace LAMMPS_NS;
 
@@ -318,19 +321,9 @@ void CreateBonds::many()
 
   bigint nadd_bonds = atom->nbonds - nbonds_previous;
 
-  if (comm->me == 0) {
-    if (screen) {
-      fprintf(screen,"Added " BIGINT_FORMAT
-              " bonds, new total = " BIGINT_FORMAT "\n",
-              nadd_bonds,atom->nbonds);
-    }
-
-    if (logfile) {
-      fprintf(logfile,"Added " BIGINT_FORMAT
-              " bonds, new total = " BIGINT_FORMAT "\n",
-              nadd_bonds,atom->nbonds);
-    }
-  }
+  if (comm->me == 0)
+    utils::logmesg(lmp,fmt::format("Added {} bonds, new total = {}\n",
+                                   nadd_bonds,atom->nbonds));
 }
 
 /* ---------------------------------------------------------------------- */
diff --git a/src/delete_atoms.cpp b/src/delete_atoms.cpp
index e8d9c5d53b..ba8940de95 100644
--- a/src/delete_atoms.cpp
+++ b/src/delete_atoms.cpp
@@ -34,8 +34,11 @@
 #include "random_mars.h"
 #include "memory.h"
 #include "error.h"
+#include "utils.h"
+#include "fmt/format.h"
 
 #include <map>
+#include <string>
 
 using namespace LAMMPS_NS;
 
@@ -176,53 +179,23 @@ void DeleteAtoms::command(int narg, char **arg)
   bigint ndelete_impropers = nimpropers_previous - atom->nimpropers;
 
   if (comm->me == 0) {
-    if (screen) {
-      fprintf(screen,"Deleted " BIGINT_FORMAT
-              " atoms, new total = " BIGINT_FORMAT "\n",
-              ndelete,atom->natoms);
-      if (bond_flag || mol_flag) {
-        if (nbonds_previous)
-          fprintf(screen,"Deleted " BIGINT_FORMAT
-                  " bonds, new total = " BIGINT_FORMAT "\n",
-                  ndelete_bonds,atom->nbonds);
-        if (nangles_previous)
-          fprintf(screen,"Deleted " BIGINT_FORMAT
-                  " angles, new total = " BIGINT_FORMAT "\n",
-                  ndelete_angles,atom->nangles);
-        if (ndihedrals_previous)
-          fprintf(screen,"Deleted " BIGINT_FORMAT
-                  " dihedrals, new total = " BIGINT_FORMAT "\n",
-                  ndelete_dihedrals,atom->ndihedrals);
-        if (nimpropers_previous)
-          fprintf(screen,"Deleted " BIGINT_FORMAT
-                  " impropers, new total = " BIGINT_FORMAT "\n",
-                  ndelete_impropers,atom->nimpropers);
-      }
-    }
-
-    if (logfile) {
-      fprintf(logfile,"Deleted " BIGINT_FORMAT
-              " atoms, new total = " BIGINT_FORMAT "\n",
-              ndelete,atom->natoms);
-      if (bond_flag || mol_flag) {
-        if (nbonds_previous)
-          fprintf(logfile,"Deleted " BIGINT_FORMAT
-                  " bonds, new total = " BIGINT_FORMAT "\n",
-                  ndelete_bonds,atom->nbonds);
-        if (nangles_previous)
-          fprintf(logfile,"Deleted " BIGINT_FORMAT
-                  " angles, new total = " BIGINT_FORMAT "\n",
-                  ndelete_angles,atom->nangles);
-        if (ndihedrals_previous)
-          fprintf(logfile,"Deleted " BIGINT_FORMAT
-                  " dihedrals, new total = " BIGINT_FORMAT "\n",
-                  ndelete_dihedrals,atom->ndihedrals);
-        if (nimpropers_previous)
-          fprintf(logfile,"Deleted " BIGINT_FORMAT
-                  " impropers, new total = " BIGINT_FORMAT "\n",
-                  ndelete_impropers,atom->nimpropers);
-      }
+    std::string mesg = fmt::format("Deleted {} atoms, new total = {}\n",
+                                   ndelete,atom->natoms);
+    if (bond_flag || mol_flag) {
+      if (nbonds_previous)
+        mesg += fmt::format("Deleted {} bonds, new total = {}\n",
+                            ndelete_bonds,atom->nbonds);
+      if (nangles_previous)
+        mesg += fmt::format("Deleted {} angles, new total = {}\n",
+                            ndelete_angles,atom->nangles);
+      if (ndihedrals_previous)
+        mesg += fmt::format("Deleted {} dihedrals, new total = {}\n",
+                            ndelete_dihedrals,atom->ndihedrals);
+      if (nimpropers_previous)
+        mesg += fmt::format("Deleted {} impropers, new total = {}\n",
+                            ndelete_impropers,atom->nimpropers);
     }
+    utils::logmesg(lmp,mesg);
   }
 }
 
diff --git a/src/delete_bonds.cpp b/src/delete_bonds.cpp
index c450f77ee7..99c0f1b951 100644
--- a/src/delete_bonds.cpp
+++ b/src/delete_bonds.cpp
@@ -15,6 +15,7 @@
 #include <mpi.h>
 #include <cstdlib>
 #include <cstring>
+#include <string>
 #include "atom.h"
 #include "atom_vec.h"
 #include "domain.h"
@@ -23,6 +24,8 @@
 #include "group.h"
 #include "special.h"
 #include "error.h"
+#include "utils.h"
+#include "fmt/format.h"
 
 using namespace LAMMPS_NS;
 
@@ -535,50 +538,25 @@ void DeleteBonds::command(int narg, char **arg)
   }
 
   if (comm->me == 0) {
-    if (atom->avec->bonds_allow) {
-      if (screen) fprintf(screen,
-                          "  " BIGINT_FORMAT " total bonds, " BIGINT_FORMAT
-                          " turned on, " BIGINT_FORMAT " turned off\n",
-                          atom->nbonds,bond_on,bond_off);
-      if (logfile) fprintf(logfile,
-                           "  " BIGINT_FORMAT " total bonds, " BIGINT_FORMAT
-                           " turned on, " BIGINT_FORMAT " turned off\n",
-                           atom->nbonds,bond_on,bond_off);
-    }
-    if (atom->avec->angles_allow) {
-      if (screen) fprintf(screen,
-                          "  " BIGINT_FORMAT " total angles, " BIGINT_FORMAT
-                          " turned on, " BIGINT_FORMAT " turned off\n",
-                          atom->nangles,angle_on,angle_off);
-      if (logfile) fprintf(logfile,
-                          "  " BIGINT_FORMAT " total angles, " BIGINT_FORMAT
-                           " turned on, " BIGINT_FORMAT " turned off\n",
-                           atom->nangles,angle_on,angle_off);
-    }
-    if (atom->avec->dihedrals_allow) {
-      if (screen) fprintf(screen,
-                          "  " BIGINT_FORMAT " total dihedrals, "
-                          BIGINT_FORMAT " turned on, " BIGINT_FORMAT
-                          " turned off\n",
-                          atom->ndihedrals,dihedral_on,dihedral_off);
-      if (logfile) fprintf(logfile,
-                          "  " BIGINT_FORMAT " total dihedrals, "
-                          BIGINT_FORMAT " turned on, " BIGINT_FORMAT
-                          " turned off\n",
-                          atom->ndihedrals,dihedral_on,dihedral_off);
-    }
-    if (atom->avec->impropers_allow) {
-      if (screen) fprintf(screen,
-                          "  " BIGINT_FORMAT " total impropers, "
-                          BIGINT_FORMAT " turned on, " BIGINT_FORMAT
-                          " turned off\n",
-                          atom->nimpropers,improper_on,improper_off);
-      if (logfile) fprintf(logfile,
-                          "  " BIGINT_FORMAT " total impropers, "
-                          BIGINT_FORMAT " turned on, " BIGINT_FORMAT
-                          " turned off\n",
-                          atom->nimpropers,improper_on,improper_off);
-    }
+    if (atom->avec->bonds_allow)
+      utils::logmesg(lmp,fmt::format("  {} total bonds, "
+                                     "{} turned on, {} turned off\n",
+                                     atom->nbonds,bond_on,bond_off));
+
+    if (atom->avec->angles_allow)
+      utils::logmesg(lmp,fmt::format("  {} total angles, "
+                                     "{} turned on, {} turned off\n",
+                                     atom->nangles,angle_on,angle_off));
+
+    if (atom->avec->dihedrals_allow)
+      utils::logmesg(lmp,fmt::format("  {} total dihedrals, "
+                                     "{} turned on, {} turned off\n",
+                                     atom->ndihedrals,dihedral_on,dihedral_off));
+
+    if (atom->avec->impropers_allow)
+      utils::logmesg(lmp,fmt::format("  {} total impropers, "
+                                     "{} turned on, {} turned off\n",
+                                     atom->nimpropers,improper_on,improper_off));
   }
 
   // re-compute special list if requested