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_dihedral.h"
16 
17 #include "dihedral.h"
18 #include "dihedral_hybrid.h"
19 #include "error.h"
20 #include "force.h"
21 #include "update.h"
22 
23 using namespace LAMMPS_NS;
24 
25 /* ---------------------------------------------------------------------- */
26 
ComputeDihedral(LAMMPS * lmp,int narg,char ** arg)27 ComputeDihedral::ComputeDihedral(LAMMPS *lmp, int narg, char **arg) :
28   Compute(lmp, narg, arg),
29   emine(nullptr)
30 {
31   if (narg != 3) error->all(FLERR,"Illegal compute dihedral command");
32 
33   vector_flag = 1;
34   extvector = 1;
35   peflag = 1;
36   timeflag = 1;
37 
38   // check if dihedral style hybrid exists
39 
40   dihedral = (DihedralHybrid *) force->dihedral_match("hybrid");
41   if (!dihedral)
42     error->all(FLERR,
43                "Dihedral style for compute dihedral command is not hybrid");
44   size_vector = nsub = dihedral->nstyles;
45 
46   emine = new double[nsub];
47   vector = new double[nsub];
48 }
49 
50 /* ---------------------------------------------------------------------- */
51 
~ComputeDihedral()52 ComputeDihedral::~ComputeDihedral()
53 {
54   delete [] emine;
55   delete [] vector;
56 }
57 
58 /* ---------------------------------------------------------------------- */
59 
init()60 void ComputeDihedral::init()
61 {
62   // recheck dihedral style in case it has been changed
63 
64   dihedral = (DihedralHybrid *) force->dihedral_match("hybrid");
65   if (!dihedral)
66     error->all(FLERR,
67                "Dihedral style for compute dihedral command is not hybrid");
68   if (dihedral->nstyles != nsub)
69     error->all(FLERR,"Dihedral style for compute dihedral command has changed");
70 }
71 
72 /* ---------------------------------------------------------------------- */
73 
compute_vector()74 void ComputeDihedral::compute_vector()
75 {
76   invoked_vector = update->ntimestep;
77   if (update->eflag_global != invoked_vector)
78     error->all(FLERR,"Energy was not tallied on needed timestep");
79 
80   for (int i = 0; i < nsub; i++)
81     emine[i] = dihedral->styles[i]->energy;
82 
83   MPI_Allreduce(emine,vector,nsub,MPI_DOUBLE,MPI_SUM,world);
84 }
85