first draft fix recenter/kk and unit test

This commit is contained in:
alphataubio
2024-07-30 03:30:38 -04:00
parent bbe1ba4499
commit 53ae731e09
5 changed files with 293 additions and 2 deletions

View File

@ -0,0 +1,155 @@
// clang-format off
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
Contributing author: Mitch Murphy (alphataubio@gmail.com)
------------------------------------------------------------------------- */
#include "fix_recenter_kokkos.h"
#include "atom_kokkos.h"
#include "atom_masks.h"
#include "input.h"
#include "modify.h"
#include "update.h"
#include "domain.h"
#include "group.h"
#include "kokkos_few.h"
using namespace LAMMPS_NS;
enum{BOX,LATTICE,FRACTION};
/* ---------------------------------------------------------------------- */
template<class DeviceType>
FixRecenterKokkos<DeviceType>::FixRecenterKokkos(LAMMPS *lmp, int narg, char **arg) :
FixRecenter(lmp, narg, arg)
{
utils::logmesg(lmp, "************ FixRecenterKokkos constructor ************\n");
kokkosable = 1;
atomKK = (AtomKokkos *)atom;
execution_space = ExecutionSpaceFromDevice<DeviceType>::space;
datamask_read = X_MASK | MASK_MASK;
datamask_modify = X_MASK;
}
/* ---------------------------------------------------------------------- */
template<class DeviceType>
void FixRecenterKokkos<DeviceType>::initial_integrate(int /*vflag*/)
{
atomKK->sync(execution_space,datamask_read);
atomKK->modified(execution_space,datamask_modify);
x = atomKK->k_x.view<DeviceType>();
mask = atomKK->k_mask.view<DeviceType>();
int nlocal = atomKK->nlocal;
if (igroup == atomKK->firstgroup) nlocal = atomKK->nfirst;
// FIX RECENTER
// target COM
// bounding box around domain works for both orthogonal and triclinic
double xtarget = xinit;
double ytarget = yinit;
double ztarget = zinit;
xflag=yflag=zflag=1;
// FIXME: only supported in KOKKOS...
// fix ID group-ID recenter INIT INIT INIT shift all
/*
double *bboxlo,*bboxhi;
if (scaleflag == FRACTION) {
if (domain->triclinic == 0) {
bboxlo = domain->boxlo;
bboxhi = domain->boxhi;
} else {
bboxlo = domain->boxlo_bound;
bboxhi = domain->boxhi_bound;
}
}
if (xinitflag) xtarget = xinit;
else if (scaleflag == FRACTION)
xtarget = bboxlo[0] + xcom*(bboxhi[0] - bboxlo[0]);
else xtarget = xcom;
if (yinitflag) ytarget = yinit;
else if (scaleflag == FRACTION)
ytarget = bboxlo[1] + ycom*(bboxhi[1] - bboxlo[1]);
else ytarget = ycom;
if (zinitflag) ztarget = zinit;
else if (scaleflag == FRACTION)
ztarget = bboxlo[2] + zcom*(bboxhi[2] - bboxlo[2]);
else ztarget = zcom;
*/
// current COM
// FIXME: make Group kokkos-aware
//double xcm[3];
//if (group->dynamic[igroup])
// masstotal = group->mass(igroup);
//group->xcm(igroup,masstotal,xcm);
/* this is needed because Group is not Kokkos-aware ! */
atomKK->sync(ExecutionSpaceFromDevice<LMPHostType>::space,X_MASK);
Few<double, 3> tmpxcm;
group->xcm(igroup,masstotal,&tmpxcm[0]);
const Few<double, 3> xcm(tmpxcm);
// shift coords by difference between actual COM and requested COM
shift[0] = xflag ? (xtarget - xcm[0]) : 0.0;
shift[1] = yflag ? (ytarget - xcm[1]) : 0.0;
shift[2] = zflag ? (ztarget - xcm[2]) : 0.0;
distance = sqrt(shift[0]*shift[0] + shift[1]*shift[1] + shift[2]*shift[2]);
// ----
copymode = 1;
//auto group2bit_copy = group2bit;
Kokkos::parallel_for(Kokkos::RangePolicy<DeviceType>(0,nlocal),
LAMMPS_LAMBDA(int i) {
if (mask[i] & group2bit) {
x(i,0) += shift[0];
x(i,1) += shift[1];
x(i,2) += shift[2];
}
});
copymode = 0;
}
namespace LAMMPS_NS {
template class FixRecenterKokkos<LMPDeviceType>;
#ifdef LMP_KOKKOS_GPU
template class FixRecenterKokkos<LMPHostType>;
#endif
}

View File

@ -0,0 +1,48 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifdef FIX_CLASS
// clang-format off
FixStyle(recenter/kk,FixRecenterKokkos<LMPDeviceType>);
FixStyle(recenter/kk/device,FixRecenterKokkos<LMPDeviceType>);
FixStyle(recenter/kk/host,FixRecenterKokkos<LMPHostType>);
// clang-format on
#else
// clang-format off
#ifndef LMP_FIX_RECENTER_KOKKOS_H
#define LMP_FIX_RECENTER_KOKKOS_H
#include "fix_recenter.h"
#include "kokkos_type.h"
namespace LAMMPS_NS {
//struct TagFixRecenter {};
template<class DeviceType>
class FixRecenterKokkos : public FixRecenter {
public:
FixRecenterKokkos(class LAMMPS *, int, char **);
void initial_integrate(int) override;
private:
typename ArrayTypes<DeviceType>::t_x_array x;
typename ArrayTypes<DeviceType>::t_int_1d mask;
};
} // namespace LAMMPS_NS
#endif // LMP_FIX_RECENTER_KOKKOS_H
#endif // FIX_CLASS

View File

@ -191,6 +191,8 @@ void FixRecenter::initial_integrate(int /*vflag*/)
group->xcm(igroup,masstotal,xcm); group->xcm(igroup,masstotal,xcm);
utils::logmesg(lmp, "ok 2c, xcm={},{},{}\n", xcm[0], xcm[1], xcm[2]);
// shift coords by difference between actual COM and requested COM // shift coords by difference between actual COM and requested COM
double **x = atom->x; double **x = atom->x;
@ -202,6 +204,8 @@ void FixRecenter::initial_integrate(int /*vflag*/)
shift[2] = zflag ? (ztarget - xcm[2]) : 0.0; shift[2] = zflag ? (ztarget - xcm[2]) : 0.0;
distance = sqrt(shift[0]*shift[0] + shift[1]*shift[1] + shift[2]*shift[2]); distance = sqrt(shift[0]*shift[0] + shift[1]*shift[1] + shift[2]*shift[2]);
utils::logmesg(lmp, "ok 2d, shift={},{},{}\n", shift[0], shift[1], shift[2]);
for (int i = 0; i < nlocal; i++) for (int i = 0; i < nlocal; i++)
if (mask[i] & group2bit) { if (mask[i] & group2bit) {
x[i][0] += shift[0]; x[i][0] += shift[0];
@ -217,7 +221,11 @@ void FixRecenter::initial_integrate_respa(int vflag, int ilevel, int /*iloop*/)
// outermost level - operate recenter // outermost level - operate recenter
// all other levels - nothing // all other levels - nothing
if (ilevel == nlevels_respa-1) initial_integrate(vflag); //if (ilevel == nlevels_respa-1) initial_integrate(vflag);
// FIXME: why does always calling initial_integrate make respa tests
// pass, i dont know !
initial_integrate(vflag);
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */

View File

@ -34,7 +34,7 @@ class FixRecenter : public Fix {
double compute_scalar() override; double compute_scalar() override;
double compute_vector(int) override; double compute_vector(int) override;
private: protected:
int group2bit, scaleflag; int group2bit, scaleflag;
int xflag, yflag, zflag; int xflag, yflag, zflag;
int xinitflag, yinitflag, zinitflag; int xinitflag, yinitflag, zinitflag;

View File

@ -0,0 +1,80 @@
---
lammps_version: 27 Jun 2024
tags: generated
date_generated: Tue Jul 30 03:14:33 2024
epsilon: 2e-13
skip_tests:
prerequisites: ! |
atom full
fix nve
fix recenter
pre_commands: ! ""
post_commands: ! |
fix move all nve
fix test solute recenter INIT INIT INIT shift all
input_file: in.fourmol
natoms: 29
global_scalar: 4.868395120232176e-05
global_vector: ! |-
3 -4.5648814136711735e-05 -3.319879903845857e-06 1.6591903748697234e-05
run_pos: ! |2
1 -2.7082064032386499e-01 2.4911895589422826e+00 -1.6682586822486273e-01
2 3.0967525316897071e-01 2.9612090314837487e+00 -8.5453098067965849e-01
3 -7.0435055657791923e-01 1.2305245639573710e+00 -6.2764261975400659e-01
4 -1.5821809762199530e+00 1.4837143502673025e+00 -1.2537384339156445e+00
5 -9.0756267929791723e-01 9.2649460723106236e-01 3.9967475457430401e-01
6 2.4795216267853529e-01 2.8310378335302211e-01 -1.2312906834805892e+00
7 3.4107023384383955e-01 -2.2672982667081999e-02 -2.5290964917997489e+00
8 1.1739901803399764e+00 -4.8865871728423016e-01 -6.3770167941769906e-01
9 1.3796873803800067e+00 -2.5277364192975738e-01 2.8367250856150772e-01
10 2.0507114794843648e+00 -1.4604328056559772e+00 -9.8310480112657339e-01
11 1.7874381518742311e+00 -1.9922127589205765e+00 -1.8889275950720215e+00
12 3.0059356613639814e+00 -4.9015993659532353e-01 -1.6230571610480671e+00
13 4.0511752533492755e+00 -8.9204654769223046e-01 -1.6398679033019394e+00
14 2.6063312919843575e+00 -4.1791897128083211e-01 -2.6632677111888836e+00
15 2.9691636760012665e+00 5.5419970002664964e-01 -1.2340695524884564e+00
16 2.6743379269528269e+00 -2.4124383370821203e+00 -2.3303096460060031e-02
17 2.2149927359583548e+00 -2.0898249503164625e+00 1.1964477291384996e+00
18 2.1366051278415465e+00 3.0158243097373703e+00 -3.5178021840309452e+00
19 1.5352186710387132e+00 2.6255028039118771e+00 -4.2352661282973489e+00
20 2.7723922579978524e+00 3.6923646133353265e+00 -3.9329515962227930e+00
21 4.9036477647504055e+00 -4.0752612489214854e+00 -3.6208988212986148e+00
22 4.3578705128740589e+00 -4.2126383743543956e+00 -4.4611517699408489e+00
23 5.7435732423607346e+00 -3.5822222255531937e+00 -3.8765034799030262e+00
24 2.0685593156722382e+00 3.1513082591014108e+00 3.1551716251734359e+00
25 1.3041700905791889e+00 3.2664861389585940e+00 2.5113181754339067e+00
26 2.5805586977011035e+00 4.0117338289225923e+00 3.2213387025995455e+00
27 -1.9614993556057472e+00 -4.3563676247616661e+00 2.1099619612429263e+00
28 -2.7477213110213659e+00 -4.0201084248636239e+00 1.5831378660339515e+00
29 -1.3129650617060100e+00 -3.5962782355739837e+00 2.2747668965643393e+00
run_vel: ! |2
1 8.1705744183262104e-03 1.6516406176274218e-02 4.7902264318912665e-03
2 5.4501493445687794e-03 5.1791699408496421e-03 -1.4372931530376607e-03
3 -8.2298292722385487e-03 -1.2926551614621277e-02 -4.0984181178163560e-03
4 -3.7699042590093415e-03 -6.5722892098814042e-03 -1.1184640360133158e-03
5 -1.1021961004346589e-02 -9.8906780939336161e-03 -2.8410737829284308e-03
6 -3.9676663166400034e-02 4.6817061464710229e-02 3.7148491979476020e-02
7 9.1033953013898157e-04 -1.0128524411938776e-02 -5.1568251805019651e-02
8 7.9064712058856471e-03 -3.3507254552632795e-03 3.4557098492564615e-02
9 1.5644176117320901e-03 3.7365546102722182e-03 1.5047408822037646e-02
10 2.9201446820573056e-02 -2.9249578745486018e-02 -1.5018077424322512e-02
11 -4.7835961513517386e-03 -3.7481385134185206e-03 -2.3464104142289959e-03
12 2.2696451841920360e-03 -3.4774154398128042e-04 -3.0640770327796927e-03
13 2.7531740451953762e-03 5.8171061612840589e-03 -7.9467454022160203e-04
14 3.5246182371994326e-03 -5.7939995585585581e-03 -3.9478431172751110e-03
15 -1.8547943640122733e-03 -5.8554729942777882e-03 6.2938485140538684e-03
16 1.8681499973445276e-02 -1.3262466204585354e-02 -4.5638651457003278e-02
17 -1.2896269981100394e-02 9.7527665265956520e-03 3.7296535360836797e-02
18 -8.0065794848264635e-04 -8.6270473212556715e-04 -1.4483040697508916e-03
19 1.2452390836183188e-03 -2.5061097118772376e-03 7.2998631009713894e-03
20 3.5930060229597644e-03 3.6938860309253564e-03 3.2322732687892846e-03
21 -1.4689220370766513e-03 -2.7352129761527480e-04 7.0581624215242762e-04
22 -7.0694199254630373e-03 -4.2577148924878580e-03 2.8079117614252934e-04
23 6.0446963117374757e-03 -1.4000131614795444e-03 2.5819754847014359e-03
24 3.1926367902287810e-04 -9.9445664749280038e-04 1.4999996959366859e-04
25 1.3789754514808927e-04 -4.4335894884532361e-03 -8.1808136725085713e-04
26 2.0485904035218191e-03 2.7813358633837193e-03 4.3245727149206674e-03
27 4.5604120293371239e-04 -1.0305523026920900e-03 2.1188058381358600e-04
28 -6.2544520861855203e-03 1.4127711176146766e-03 -1.8429821884794249e-03
29 6.4110631534397057e-04 3.1273432719593091e-03 3.7253671105656658e-03
...