From 3c41295e7077b592351676c19e0ff45779553f91 Mon Sep 17 00:00:00 2001
From: Axel Kohlmeyer <akohlmey@gmail.com>
Date: Mon, 8 Oct 2018 17:23:22 +0200
Subject: [PATCH] change implementation to be consistent with pair_coeff and
 remain backward compatible

---
 doc/src/compute_pair.txt | 27 ++++++++++-----------------
 src/compute_pair.cpp     | 32 +++++++++++++++++---------------
 2 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/doc/src/compute_pair.txt b/doc/src/compute_pair.txt
index 1a3ec06508..b36d61f71c 100644
--- a/doc/src/compute_pair.txt
+++ b/doc/src/compute_pair.txt
@@ -10,22 +10,20 @@ compute pair command :h3
 
 [Syntax:]
 
-compute ID group-ID pair pstyle keyword value :pre
+compute ID group-ID pair pstyle \[nstyle\] \[evalue\]  :pre
 
 ID, group-ID are documented in "compute"_compute.html command :ulb,l
 pair = style name of this compute command :l
 pstyle = style name of a pair style that calculates additional values :l
-zero or more keyword/value pairs may be appended :l
-keyword = {evalue} or {nsub}
-  {evalue} arg = {epair} or {evdwl} or {ecoul}
-  {nsub} n = use the {n}-th instance of a sub-style in a pair_style hybrid or hybrid/overlay command :pre
+nsub = {n}-instance of a substyle, if a pair style is used multiple times in a hybrid style :l
+{evalue} = {epair} or {evdwl} or {ecoul} or blank (optional) :l
 :ule
 
 [Examples:]
 
 compute 1 all pair gauss
-compute 1 all pair lj/cut/coul/cut evalue ecoul
-compute 1 all pair tersoff nsub 2
+compute 1 all pair lj/cut/coul/cut ecoul
+compute 1 all pair tersoff 2 epair
 compute 1 all pair reax :pre
 
 [Description:]
@@ -39,15 +37,10 @@ NOTE: The group specified for this command is [ignored].
 The specified {pstyle} must be a pair style used in your simulation
 either by itself or as a sub-style in a "pair_style hybrid or
 hybrid/overlay"_pair_hybrid.html command. If the sub-style is
-used more than once, an additional keyword {nsub} has to be
-specified in order to choose which instance of the sub-style will
-be used by the compute.
-
-NOTE: The {nsub} keyword should be used only if the sub-style occurs
-multiple times. Its value must be an integer from 1 to M, where M is
-the number of times the sub-style is listed in the "pair_style hybrid
-or hybrid/overlay"_pair_hybrid.html" command. If the pair style is
-used only once, {nsub} must remain zero.
+used more than once, an additional number {nsub} has to be specified
+in order to choose which instance of the sub-style will be used by
+the compute. Not specifying the number in this case will cause the
+compute to fail.
 
 The {evalue} setting is optional.  All
 pair styles tally a potential energy {epair} which may be broken into
@@ -55,7 +48,7 @@ two parts: {evdwl} and {ecoul} such that {epair} = {evdwl} + {ecoul}.
 If the pair style calculates Coulombic interactions, their energy will
 be tallied in {ecoul}.  Everything else (whether it is a Lennard-Jones
 style van der Waals interaction or not) is tallied in {evdwl}.  If
-{evalue} is specified as {epair}, then {epair} is stored
+{evalue} is blank or specified as {epair}, then {epair} is stored
 as a global scalar by this compute.  This is useful when using
 "pair_style hybrid"_pair_hybrid.html if you want to know the portion
 of the total energy contributed by one sub-style.  If {evalue} is
diff --git a/src/compute_pair.cpp b/src/compute_pair.cpp
index 6129f192e2..9690f140c5 100644
--- a/src/compute_pair.cpp
+++ b/src/compute_pair.cpp
@@ -13,6 +13,7 @@
 
 #include <mpi.h>
 #include <cstring>
+#include <cctype>
 #include "compute_pair.h"
 #include "update.h"
 #include "force.h"
@@ -43,23 +44,24 @@ ComputePair::ComputePair(LAMMPS *lmp, int narg, char **arg) :
 
   int iarg = 4;
   nsub = 0;
+  evalue = NPAIR;
+
+  if (narg > iarg) {
+    if (isdigit(arg[iarg][0])) {
+      nsub = force->inumeric(FLERR,arg[iarg]);
+      ++iarg;
+      if (nsub <= 0)
+        error->all(FLERR,"Illegal compute pair command");
+    }
+  }
 
-  while (iarg < narg) {
-    if (strcmp(arg[iarg],"evalue") == 0) {
-      if (iarg+2 > narg) error->all(FLERR,"Illegal compute pair command");
-      if (strcmp(arg[iarg+1],"epair") == 0) evalue = EPAIR;
-      else if (strcmp(arg[iarg+1],"evdwl") == 0) evalue = EVDWL;
-      else if (strcmp(arg[iarg+1],"ecoul") == 0) evalue = ECOUL;
-      else error->all(FLERR, "Unrecognized energy type");
-      iarg += 2;
-    } else if (strcmp(arg[iarg],"nsub") == 0) {
-      if (iarg+2 > narg) error->all(FLERR,"Illegal compute pair command");
-      nsub = force->inumeric(FLERR,arg[iarg+1]);
-      iarg += 2;
-    } else error->all(FLERR,"Illegal compute pair command");
-    
+  if  (narg > iarg) {
+    if (strcmp(arg[iarg],"epair") == 0) evalue = EPAIR;
+    else if (strcmp(arg[iarg],"evdwl") == 0) evalue = EVDWL;
+    else if (strcmp(arg[iarg],"ecoul") == 0) evalue = ECOUL;
+    else error->all(FLERR, "Illegal compute pair command");
+    ++iarg;
   }
-  
 
   // check if pair style with and without suffix exists
 
-- 
GitLab