1 // clang-format off
2 /* ----------------------------------------------------------------------
3    LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
4    https://www.lammps.org/, Sandia National Laboratories
5    Steve Plimpton, sjplimp@sandia.gov
6 
7    Copyright (2003) Sandia Corporation.  Under the terms of Contract
8    DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
9    certain rights in this software.  This software is distributed under
10    the GNU General Public License.
11 
12    See the README file in the top-level LAMMPS directory.
13 ------------------------------------------------------------------------- */
14 
15 #include "compute_bond.h"
16 
17 #include "bond.h"
18 #include "bond_hybrid.h"
19 #include "error.h"
20 #include "force.h"
21 #include "update.h"
22 
23 using namespace LAMMPS_NS;
24 
25 /* ---------------------------------------------------------------------- */
26 
ComputeBond(LAMMPS * lmp,int narg,char ** arg)27 ComputeBond::ComputeBond(LAMMPS *lmp, int narg, char **arg) :
28   Compute(lmp, narg, arg),
29   emine(nullptr)
30 {
31   if (narg != 3) error->all(FLERR,"Illegal compute bond command");
32 
33   vector_flag = 1;
34   extvector = 1;
35   peflag = 1;
36   timeflag = 1;
37 
38   // check if bond style hybrid exists
39 
40   bond = (BondHybrid *) force->bond_match("hybrid");
41   if (!bond)
42     error->all(FLERR,"Bond style for compute bond command is not hybrid");
43   size_vector = nsub = bond->nstyles;
44 
45   emine = new double[nsub];
46   vector = new double[nsub];
47 }
48 
49 /* ---------------------------------------------------------------------- */
50 
~ComputeBond()51 ComputeBond::~ComputeBond()
52 {
53   delete [] emine;
54   delete [] vector;
55 }
56 
57 /* ---------------------------------------------------------------------- */
58 
init()59 void ComputeBond::init()
60 {
61   // recheck bond style in case it has been changed
62 
63   bond = (BondHybrid *) force->bond_match("hybrid");
64   if (!bond)
65     error->all(FLERR,"Bond style for compute bond command is not hybrid");
66   if (bond->nstyles != nsub)
67     error->all(FLERR,"Bond style for compute bond command has changed");
68 }
69 
70 /* ---------------------------------------------------------------------- */
71 
compute_vector()72 void ComputeBond::compute_vector()
73 {
74   invoked_vector = update->ntimestep;
75   if (update->eflag_global != invoked_vector)
76     error->all(FLERR,"Energy was not tallied on needed timestep");
77 
78   for (int i = 0; i < nsub; i++)
79     emine[i] = bond->styles[i]->energy;
80 
81   MPI_Allreduce(emine,vector,nsub,MPI_DOUBLE,MPI_SUM,world);
82 }
83