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_inertia_molecule.h"
21
using namespace LAMMPS_NS;
23
/* ---------------------------------------------------------------------- */
25
ComputeInertiaMolecule::
26
ComputeInertiaMolecule(LAMMPS *lmp, int narg, char **arg) :
27
Compute(lmp, narg, arg)
29
if (narg != 3) error->all(FLERR,"Illegal compute inertia/molecule command");
31
if (atom->molecular == 0)
32
error->all(FLERR,"Compute inertia/molecule requires molecular atom style");
38
// setup molecule-based data
40
nmolecules = molecules_in_group(idlo,idhi);
41
size_array_rows = nmolecules;
43
memory->create(massproc,nmolecules,"inertia/molecule:massproc");
44
memory->create(masstotal,nmolecules,"inertia/molecule:masstotal");
45
memory->create(com,nmolecules,3,"inertia/molecule:com");
46
memory->create(comall,nmolecules,3,"inertia/molecule:comall");
47
memory->create(inertia,nmolecules,6,"inertia/molecule:inertia");
48
memory->create(inertiaall,nmolecules,6,"inertia/molecule:inertiaall");
51
// compute masstotal for each molecule
53
int *mask = atom->mask;
54
tagint *molecule = atom->molecule;
55
int *type = atom->type;
56
double *mass = atom->mass;
57
double *rmass = atom->rmass;
58
int nlocal = atom->nlocal;
63
for (int i = 0; i < nmolecules; i++) massproc[i] = 0.0;
65
for (int i = 0; i < nlocal; i++)
66
if (mask[i] & groupbit) {
67
if (rmass) massone = rmass[i];
68
else massone = mass[type[i]];
70
if (molmap) imol = molmap[imol-idlo];
72
massproc[imol] += massone;
75
MPI_Allreduce(massproc,masstotal,nmolecules,MPI_DOUBLE,MPI_SUM,world);
78
/* ---------------------------------------------------------------------- */
80
ComputeInertiaMolecule::~ComputeInertiaMolecule()
82
memory->destroy(massproc);
83
memory->destroy(masstotal);
85
memory->destroy(comall);
86
memory->destroy(inertia);
87
memory->destroy(inertiaall);
90
/* ---------------------------------------------------------------------- */
92
void ComputeInertiaMolecule::init()
94
int ntmp = molecules_in_group(idlo,idhi);
95
if (ntmp != nmolecules)
96
error->all(FLERR,"Molecule count changed in compute inertia/molecule");
99
/* ---------------------------------------------------------------------- */
101
void ComputeInertiaMolecule::compute_array()
105
double dx,dy,dz,massone;
108
invoked_array = update->ntimestep;
110
double **x = atom->x;
111
int *mask = atom->mask;
112
tagint *molecule = atom->molecule;
113
int *type = atom->type;
114
imageint *image = atom->image;
115
double *mass = atom->mass;
116
double *rmass = atom->rmass;
117
int nlocal = atom->nlocal;
119
// center-of-mass for each molecule
121
for (i = 0; i < nmolecules; i++)
122
com[i][0] = com[i][1] = com[i][2] = 0.0;
124
for (int i = 0; i < nlocal; i++)
125
if (mask[i] & groupbit) {
126
if (rmass) massone = rmass[i];
127
else massone = mass[type[i]];
129
if (molmap) imol = molmap[imol-idlo];
131
domain->unmap(x[i],image[i],unwrap);
132
com[imol][0] += unwrap[0] * massone;
133
com[imol][1] += unwrap[1] * massone;
134
com[imol][2] += unwrap[2] * massone;
137
MPI_Allreduce(&com[0][0],&comall[0][0],3*nmolecules,
138
MPI_DOUBLE,MPI_SUM,world);
139
for (i = 0; i < nmolecules; i++) {
140
comall[i][0] /= masstotal[i];
141
comall[i][1] /= masstotal[i];
142
comall[i][2] /= masstotal[i];
145
// inertia tensor for each molecule
147
for (i = 0; i < nmolecules; i++)
148
for (j = 0; j < 6; j++)
151
for (i = 0; i < nlocal; i++)
152
if (mask[i] & groupbit) {
153
if (rmass) massone = rmass[i];
154
else massone = mass[type[i]];
156
if (molmap) imol = molmap[imol-idlo];
158
domain->unmap(x[i],image[i],unwrap);
159
dx = unwrap[0] - comall[imol][0];
160
dy = unwrap[1] - comall[imol][1];
161
dz = unwrap[2] - comall[imol][2];
162
inertia[imol][0] += massone * (dy*dy + dz*dz);
163
inertia[imol][1] += massone * (dx*dx + dz*dz);
164
inertia[imol][2] += massone * (dx*dx + dy*dy);
165
inertia[imol][3] -= massone * dx*dy;
166
inertia[imol][4] -= massone * dy*dz;
167
inertia[imol][5] -= massone * dx*dz;
170
MPI_Allreduce(&inertia[0][0],&inertiaall[0][0],6*nmolecules,
171
MPI_DOUBLE,MPI_SUM,world);
174
/* ----------------------------------------------------------------------
175
memory usage of local data
176
------------------------------------------------------------------------- */
178
double ComputeInertiaMolecule::memory_usage()
180
double bytes = (bigint) nmolecules * 2 * sizeof(double);
181
if (molmap) bytes += (idhi-idlo+1) * sizeof(int);
182
bytes += (bigint) nmolecules * 2*3 * sizeof(double);
183
bytes += (bigint) nmolecules * 2*6 * sizeof(double);