1
/* ----------------------------------------------------------------------
2
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
3
http://lammps.sandia.gov, Sandia National Laboratories
4
Steve Plimpton, sjplimp@sandia.gov
6
Copyright (2003) Sandia Corporation. Under the terms of Contract
7
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
8
certain rights in this software. This software is distributed under
9
the GNU General Public License.
11
See the README file in the top-level LAMMPS directory.
12
------------------------------------------------------------------------- */
14
#include "compute_com_molecule.h"
21
using namespace LAMMPS_NS;
23
/* ---------------------------------------------------------------------- */
25
ComputeCOMMolecule::ComputeCOMMolecule(LAMMPS *lmp, int narg, char **arg) :
26
Compute(lmp, narg, arg)
28
if (narg != 3) error->all(FLERR,"Illegal compute com/molecule command");
30
if (atom->molecular == 0)
31
error->all(FLERR,"Compute com/molecule requires molecular atom style");
37
// setup molecule-based data
39
nmolecules = molecules_in_group(idlo,idhi);
40
size_array_rows = nmolecules;
42
memory->create(massproc,nmolecules,"com/molecule:massproc");
43
memory->create(masstotal,nmolecules,"com/molecule:masstotal");
44
memory->create(com,nmolecules,3,"com/molecule:com");
45
memory->create(comall,nmolecules,3,"com/molecule:comall");
48
// compute masstotal for each molecule
50
int *mask = atom->mask;
51
tagint *molecule = atom->molecule;
52
int *type = atom->type;
53
double *mass = atom->mass;
54
double *rmass = atom->rmass;
55
int nlocal = atom->nlocal;
60
for (int i = 0; i < nmolecules; i++) massproc[i] = 0.0;
62
for (int i = 0; i < nlocal; i++)
63
if (mask[i] & groupbit) {
64
if (rmass) massone = rmass[i];
65
else massone = mass[type[i]];
67
if (molmap) imol = molmap[imol-idlo];
69
massproc[imol] += massone;
72
MPI_Allreduce(massproc,masstotal,nmolecules,MPI_DOUBLE,MPI_SUM,world);
75
/* ---------------------------------------------------------------------- */
77
ComputeCOMMolecule::~ComputeCOMMolecule()
79
memory->destroy(massproc);
80
memory->destroy(masstotal);
82
memory->destroy(comall);
85
/* ---------------------------------------------------------------------- */
87
void ComputeCOMMolecule::init()
89
int ntmp = molecules_in_group(idlo,idhi);
90
if (ntmp != nmolecules)
91
error->all(FLERR,"Molecule count changed in compute com/molecule");
94
/* ---------------------------------------------------------------------- */
96
void ComputeCOMMolecule::compute_array()
102
invoked_array = update->ntimestep;
104
for (int i = 0; i < nmolecules; i++)
105
com[i][0] = com[i][1] = com[i][2] = 0.0;
107
double **x = atom->x;
108
int *mask = atom->mask;
109
tagint *molecule = atom->molecule;
110
int *type = atom->type;
111
imageint *image = atom->image;
112
double *mass = atom->mass;
113
double *rmass = atom->rmass;
114
int nlocal = atom->nlocal;
116
for (int i = 0; i < nlocal; i++)
117
if (mask[i] & groupbit) {
118
if (rmass) massone = rmass[i];
119
else massone = mass[type[i]];
121
if (molmap) imol = molmap[imol-idlo];
123
domain->unmap(x[i],image[i],unwrap);
124
com[imol][0] += unwrap[0] * massone;
125
com[imol][1] += unwrap[1] * massone;
126
com[imol][2] += unwrap[2] * massone;
129
MPI_Allreduce(&com[0][0],&comall[0][0],3*nmolecules,
130
MPI_DOUBLE,MPI_SUM,world);
131
for (int i = 0; i < nmolecules; i++) {
132
comall[i][0] /= masstotal[i];
133
comall[i][1] /= masstotal[i];
134
comall[i][2] /= masstotal[i];
138
/* ----------------------------------------------------------------------
139
memory usage of local data
140
------------------------------------------------------------------------- */
142
double ComputeCOMMolecule::memory_usage()
144
double bytes = (bigint) nmolecules * 2 * sizeof(double);
145
if (molmap) bytes += (idhi-idlo+1) * sizeof(int);
146
bytes += (bigint) nmolecules * 2*3 * sizeof(double);