From 17d1e960e8abd8df85ea62b155d49b71b28fe7e6 Mon Sep 17 00:00:00 2001 From: sjplimp Date: Wed, 1 Feb 2012 22:47:13 +0000 Subject: [PATCH] git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@7656 f3b2605a-c512-4ea7-a41b-209d697bcdaa --- src/USER-MISC/Install.sh | 4 + src/USER-MISC/README | 1 + src/USER-MISC/pair_meam_spline.cpp | 639 +++++++++++++++++++++++++++++ src/USER-MISC/pair_meam_spline.h | 274 +++++++++++++ 4 files changed, 918 insertions(+) create mode 100644 src/USER-MISC/pair_meam_spline.cpp create mode 100644 src/USER-MISC/pair_meam_spline.h diff --git a/src/USER-MISC/Install.sh b/src/USER-MISC/Install.sh index b7f0eb9086..5b86de208c 100644 --- a/src/USER-MISC/Install.sh +++ b/src/USER-MISC/Install.sh @@ -20,6 +20,7 @@ if (test $1 = 1) then cp pair_edip.cpp .. cp pair_gauss_cut.cpp .. cp pair_lj_sf.cpp .. + cp pair_meam_spline.cpp .. cp pair_tersoff_table.cpp .. cp angle_cosine_shift.h .. @@ -40,6 +41,7 @@ if (test $1 = 1) then cp pair_edip.h .. cp pair_gauss_cut.h .. cp pair_lj_sf.h .. + cp pair_meam_spline.h .. cp pair_tersoff_table.h .. elif (test $1 = 0) then @@ -62,6 +64,7 @@ elif (test $1 = 0) then rm -f ../pair_edip.cpp rm -f ../pair_gauss_cut.cpp rm -f ../pair_lj_sf.cpp + rm -f ../pair_meam_spline.cpp rm -f ../pair_tersoff_table.cpp rm -f ../angle_cosine_shift.h @@ -82,6 +85,7 @@ elif (test $1 = 0) then rm -f ../pair_edip.h rm -f ../pair_gauss_cut.h rm -f ../pair_lj_sf.h + rm -f ../pair_meam_spline.h rm -f ../pair_tersoff_table.h fi diff --git a/src/USER-MISC/README b/src/USER-MISC/README index 58f018c730..f15482d225 100644 --- a/src/USER-MISC/README +++ b/src/USER-MISC/README @@ -36,4 +36,5 @@ pair_style edip, Luca Ferraro, luca.ferraro at caspur.it, 15 Sep 11 pair_style eam/cd, Alexander Stukowski, stukowski at mm.tu-darmstadt.de, 7 Nov 09 pair_style gauss/cut, Axel Kohlmeyer, akohlmey at gmail.com, 1 Dec 11 pair_style lj/sf, Laurent Joly (U Lyon), ljoly.ulyon at gmail.com, 8 Aug 11 +pair_style meam/spline, Alexander Stukowski (LLNL), alex at stukowski.com, 1 Feb 12 pair_style tersoff/table, Luca Ferraro, luca.ferraro@caspur.it, 1 Dec 11 diff --git a/src/USER-MISC/pair_meam_spline.cpp b/src/USER-MISC/pair_meam_spline.cpp new file mode 100644 index 0000000000..1eed48879c --- /dev/null +++ b/src/USER-MISC/pair_meam_spline.cpp @@ -0,0 +1,639 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + 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. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + * Spline-based Modified Embedded Atom method (MEAM) potential routine. + * + * Copyright (2011) Lawrence Livermore National Security, LLC. + * Produced at the Lawrence Livermore National Laboratory. + * Written by Alexander Stukowski (). + * LLNL-CODE-525797 All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License (as published by the Free + * Software Foundation) version 2, dated June 1991. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the + * GNU General Public License for more details. + * + * Our Preamble Notice + * A. This notice is required to be provided under our contract with the + * U.S. Department of Energy (DOE). This work was produced at the + * Lawrence Livermore National Laboratory under Contract No. + * DE-AC52-07NA27344 with the DOE. + * + * B. Neither the United States Government nor Lawrence Livermore National + * Security, LLC nor any of their employees, makes any warranty, express or + * implied, or assumes any liability or responsibility for the accuracy, + * completeness, or usefulness of any information, apparatus, product, or + * process disclosed, or represents that its use would not infringe + * privately-owned rights. + * + * C. Also, reference herein to any specific commercial products, process, + * or services by trade name, trademark, manufacturer or otherwise does not + * necessarily constitute or imply its endorsement, recommendation, or + * favoring by the United States Government or Lawrence Livermore National + * Security, LLC. The views and opinions of authors expressed herein do not + * necessarily state or reflect those of the United States Government or + * Lawrence Livermore National Security, LLC, and shall not be used for + * advertising or product endorsement purposes. + * + * File history of changes: + * + * 25-Oct-10 - AS: First code version. + * 17-Feb-11 - AS: Several optimizations (introduced MEAM2Body struct). + * 25-Mar-11 - AS: Fixed calculation of per-atom virial stress. + * 11-Apr-11 - AS: Adapted code to new memory management of LAMMPS. + * 24-Sep-11 - AS: Adapted code to new interface of Error::one() function. +------------------------------------------------------------------------- */ + +#include "math.h" +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +#include "pair_meam_spline.h" +#include "atom.h" +#include "force.h" +#include "comm.h" +#include "memory.h" +#include "neighbor.h" +#include "neigh_list.h" +#include "neigh_request.h" +#include "memory.h" +#include "error.h" + +using namespace LAMMPS_NS; + +/* ---------------------------------------------------------------------- */ + +PairMEAMSpline::PairMEAMSpline(LAMMPS *lmp) : Pair(lmp) +{ + single_enable = 0; + one_coeff = 1; + + Uprime_values = NULL; + nmax = 0; + maxNeighbors = 0; + twoBodyInfo = NULL; + + comm_forward = 1; + comm_reverse = 0; +} + +PairMEAMSpline::~PairMEAMSpline() +{ + delete[] twoBodyInfo; + memory->destroy(Uprime_values); + + if(allocated) { + memory->destroy(setflag); + memory->destroy(cutsq); + } +} + +/* ---------------------------------------------------------------------- */ + +void PairMEAMSpline::compute(int eflag, int vflag) +{ + if(eflag || vflag) ev_setup(eflag, vflag); + else evflag = vflag_fdotr = eflag_global = vflag_global = eflag_atom = vflag_atom = 0; + + double cutforcesq = cutoff*cutoff; + + // Grow per-atom array if necessary. + if(atom->nmax > nmax) { + memory->sfree(Uprime_values); + nmax = atom->nmax; + Uprime_values = (double*)memory->smalloc(nmax*sizeof(double), "pair:Uprime"); + } + + double** const x = atom->x; + double** forces = atom->f; + int nlocal = atom->nlocal; + bool newton_pair = force->newton_pair; + + int inum_full = listfull->inum; + int* ilist_full = listfull->ilist; + int* numneigh_full = listfull->numneigh; + int** firstneigh_full = listfull->firstneigh; + + // Determine the maximum number of neighbors a single atom has. + int newMaxNeighbors = 0; + for(int ii = 0; ii < inum_full; ii++) { + int jnum = numneigh_full[ilist_full[ii]]; + if(jnum > newMaxNeighbors) newMaxNeighbors = jnum; + } + + // Allocate array for temporary bond info. + if(newMaxNeighbors > maxNeighbors) { + maxNeighbors = newMaxNeighbors; + delete[] twoBodyInfo; + twoBodyInfo = new MEAM2Body[maxNeighbors]; + } + + // Sum three-body contributions to charge density and compute embedding energies. + for(int ii = 0; ii < inum_full; ii++) { + int i = ilist_full[ii]; + double xtmp = x[i][0]; + double ytmp = x[i][1]; + double ztmp = x[i][2]; + int* jlist = firstneigh_full[i]; + int jnum = numneigh_full[i]; + double rho_value = 0; + int numBonds = 0; + MEAM2Body* nextTwoBodyInfo = twoBodyInfo; + + for(int jj = 0; jj < jnum; jj++) { + int j = jlist[jj]; + j &= NEIGHMASK; + + double jdelx = x[j][0] - xtmp; + double jdely = x[j][1] - ytmp; + double jdelz = x[j][2] - ztmp; + double rij_sq = jdelx*jdelx + jdely*jdely + jdelz*jdelz; + + if(rij_sq < cutforcesq) { + double rij = sqrt(rij_sq); + double partial_sum = 0; + + nextTwoBodyInfo->tag = j; + nextTwoBodyInfo->r = rij; + nextTwoBodyInfo->f = f.eval(rij, nextTwoBodyInfo->fprime); + nextTwoBodyInfo->del[0] = jdelx / rij; + nextTwoBodyInfo->del[1] = jdely / rij; + nextTwoBodyInfo->del[2] = jdelz / rij; + + for(int kk = 0; kk < numBonds; kk++) { + const MEAM2Body& bondk = twoBodyInfo[kk]; + double cos_theta = (nextTwoBodyInfo->del[0]*bondk.del[0] + nextTwoBodyInfo->del[1]*bondk.del[1] + nextTwoBodyInfo->del[2]*bondk.del[2]); + partial_sum += bondk.f * g.eval(cos_theta); + } + + rho_value += nextTwoBodyInfo->f * partial_sum; + rho_value += rho.eval(rij); + + numBonds++; + nextTwoBodyInfo++; + } + } + + // Compute embedding energy and its derivative. + double Uprime_i; + double embeddingEnergy = U.eval(rho_value, Uprime_i) - zero_atom_energy; + Uprime_values[i] = Uprime_i; + if(eflag) { + if(eflag_global) eng_vdwl += embeddingEnergy; + if(eflag_atom) eatom[i] += embeddingEnergy; + } + + double forces_i[3] = {0, 0, 0}; + + // Compute three-body contributions to force. + for(int jj = 0; jj < numBonds; jj++) { + const MEAM2Body bondj = twoBodyInfo[jj]; + double rij = bondj.r; + int j = bondj.tag; + + double f_rij_prime = bondj.fprime; + double f_rij = bondj.f; + + double forces_j[3] = {0, 0, 0}; + + MEAM2Body const* bondk = twoBodyInfo; + for(int kk = 0; kk < jj; kk++, ++bondk) { + double rik = bondk->r; + + double cos_theta = (bondj.del[0]*bondk->del[0] + bondj.del[1]*bondk->del[1] + bondj.del[2]*bondk->del[2]); + double g_prime; + double g_value = g.eval(cos_theta, g_prime); + double f_rik_prime = bondk->fprime; + double f_rik = bondk->f; + + double fij = -Uprime_i * g_value * f_rik * f_rij_prime; + double fik = -Uprime_i * g_value * f_rij * f_rik_prime; + + double prefactor = Uprime_i * f_rij * f_rik * g_prime; + double prefactor_ij = prefactor / rij; + double prefactor_ik = prefactor / rik; + fij += prefactor_ij * cos_theta; + fik += prefactor_ik * cos_theta; + + double fj[3], fk[3]; + + fj[0] = bondj.del[0] * fij - bondk->del[0] * prefactor_ij; + fj[1] = bondj.del[1] * fij - bondk->del[1] * prefactor_ij; + fj[2] = bondj.del[2] * fij - bondk->del[2] * prefactor_ij; + forces_j[0] += fj[0]; + forces_j[1] += fj[1]; + forces_j[2] += fj[2]; + + fk[0] = bondk->del[0] * fik - bondj.del[0] * prefactor_ik; + fk[1] = bondk->del[1] * fik - bondj.del[1] * prefactor_ik; + fk[2] = bondk->del[2] * fik - bondj.del[2] * prefactor_ik; + forces_i[0] -= fk[0]; + forces_i[1] -= fk[1]; + forces_i[2] -= fk[2]; + + int k = bondk->tag; + forces[k][0] += fk[0]; + forces[k][1] += fk[1]; + forces[k][2] += fk[2]; + + if(evflag) { + double delta_ij[3]; + double delta_ik[3]; + delta_ij[0] = bondj.del[0] * rij; + delta_ij[1] = bondj.del[1] * rij; + delta_ij[2] = bondj.del[2] * rij; + delta_ik[0] = bondk->del[0] * rik; + delta_ik[1] = bondk->del[1] * rik; + delta_ik[2] = bondk->del[2] * rik; + ev_tally3(i, j, k, 0.0, 0.0, fj, fk, delta_ij, delta_ik); + } + } + + forces[i][0] -= forces_j[0]; + forces[i][1] -= forces_j[1]; + forces[i][2] -= forces_j[2]; + forces[j][0] += forces_j[0]; + forces[j][1] += forces_j[1]; + forces[j][2] += forces_j[2]; + } + + forces[i][0] += forces_i[0]; + forces[i][1] += forces_i[1]; + forces[i][2] += forces_i[2]; + } + + // Communicate U'(rho) values. + comm->forward_comm_pair(this); + + int inum_half = listhalf->inum; + int* ilist_half = listhalf->ilist; + int* numneigh_half = listhalf->numneigh; + int** firstneigh_half = listhalf->firstneigh; + + // Compute two-body pair interactions. + for(int ii = 0; ii < inum_half; ii++) { + int i = ilist_half[ii]; + double xtmp = x[i][0]; + double ytmp = x[i][1]; + double ztmp = x[i][2]; + int* jlist = firstneigh_half[i]; + int jnum = numneigh_half[i]; + + for(int jj = 0; jj < jnum; jj++) { + int j = jlist[jj]; + j &= NEIGHMASK; + + double jdel[3]; + jdel[0] = x[j][0] - xtmp; + jdel[1] = x[j][1] - ytmp; + jdel[2] = x[j][2] - ztmp; + double rij_sq = jdel[0]*jdel[0] + jdel[1]*jdel[1] + jdel[2]*jdel[2]; + + if(rij_sq < cutforcesq) { + double rij = sqrt(rij_sq); + + double rho_prime; + rho.eval(rij, rho_prime); + double fpair = rho_prime * (Uprime_values[i] + Uprime_values[j]); + + double pair_pot_deriv; + double pair_pot = phi.eval(rij, pair_pot_deriv); + fpair += pair_pot_deriv; + + // Divide by r_ij to get forces from gradient. + fpair /= rij; + + forces[i][0] += jdel[0]*fpair; + forces[i][1] += jdel[1]*fpair; + forces[i][2] += jdel[2]*fpair; + forces[j][0] -= jdel[0]*fpair; + forces[j][1] -= jdel[1]*fpair; + forces[j][2] -= jdel[2]*fpair; + if(evflag) ev_tally(i, j, nlocal, newton_pair, pair_pot, 0.0, -fpair, jdel[0], jdel[1], jdel[2]); + } + } + } + + if(vflag_fdotr) virial_fdotr_compute(); +} + +void PairMEAMSpline::allocate() +{ + allocated = 1; + int n = atom->ntypes; + + memory->create(setflag,n+1,n+1,"pair:setflag"); + for(int i = 0; i <= n; i++) + for(int j = 0; j <= n; j++) + setflag[i][j] = 0; + + memory->create(cutsq,n+1,n+1,"pair:cutsq"); +} + +/* ---------------------------------------------------------------------- + global settings +------------------------------------------------------------------------- */ +void PairMEAMSpline::settings(int narg, char **arg) +{ + if(narg != 0) error->all(FLERR,"Illegal pair_style command"); +} + +/* ---------------------------------------------------------------------- + set coeffs for one or more type pairs +------------------------------------------------------------------------- */ +void PairMEAMSpline::coeff(int narg, char **arg) +{ + if(narg < 3) error->all(FLERR, "Not enough arguments for meam/spline pair coefficients"); + + if(!allocated) allocate(); + + // insure I,J args are * * + if(strcmp(arg[0],"*") != 0 || strcmp(arg[1],"*") != 0) + error->all(FLERR, "Incorrect args for pair coefficients"); + + read_file(arg[2]); +} + +#define MAXLINE 1024 + +void PairMEAMSpline::read_file(const char* filename) +{ + if(comm->me == 0) { + FILE *fp = fopen(filename, "r"); + if(fp == NULL) { + char str[1024]; + sprintf(str,"Cannot open spline MEAM potential file %s", filename); + error->one(FLERR,str); + } + + // Skip first line of file. + char line[MAXLINE]; + fgets(line, MAXLINE, fp); + + // Parse spline functions. + phi.parse(fp, error); + rho.parse(fp, error); + U.parse(fp, error); + f.parse(fp, error); + g.parse(fp, error); + + fclose(fp); + } + + // Transfer spline functions from master processor to all other processors. + phi.communicate(world, comm->me); + rho.communicate(world, comm->me); + f.communicate(world, comm->me); + U.communicate(world, comm->me); + g.communicate(world, comm->me); + + // Calculate 'zero-point energy' of single atom in vacuum. + zero_atom_energy = U.eval(0.0); + + // Determine maximum cutoff radius of all relevant spline functions. + cutoff = 0.0; + if(phi.cutoff() > cutoff) cutoff = phi.cutoff(); + if(rho.cutoff() > cutoff) cutoff = rho.cutoff(); + if(f.cutoff() > cutoff) cutoff = f.cutoff(); + + // Set LAMMPS pair interaction flags. + for(int i = 1; i <= atom->ntypes; i++) { + for(int j = 1; j <= atom->ntypes; j++) { + setflag[i][j] = 1; + cutsq[i][j] = cutoff; + } + } + + //phi.writeGnuplot("phi.gp", "Phi(r)"); + //rho.writeGnuplot("rho.gp", "Rho(r)"); + //f.writeGnuplot("f.gp", "f(r)"); + //U.writeGnuplot("U.gp", "U(rho)"); + //g.writeGnuplot("g.gp", "g(x)"); +} + +/* ---------------------------------------------------------------------- + init specific to this pair style +------------------------------------------------------------------------- */ +void PairMEAMSpline::init_style() +{ + if(force->newton_pair == 0) + error->all(FLERR,"Pair style meam/spline requires newton pair on"); + + // Need both full and half neighbor list. + int irequest_full = neighbor->request(this); + neighbor->requests[irequest_full]->id = 1; + neighbor->requests[irequest_full]->half = 0; + neighbor->requests[irequest_full]->full = 1; + int irequest_half = neighbor->request(this); + neighbor->requests[irequest_half]->id = 2; + neighbor->requests[irequest_half]->half = 0; + neighbor->requests[irequest_half]->half_from_full = 1; + neighbor->requests[irequest_half]->otherlist = irequest_full; +} + +/* ---------------------------------------------------------------------- + neighbor callback to inform pair style of neighbor list to use + half or full +------------------------------------------------------------------------- */ +void PairMEAMSpline::init_list(int id, NeighList *ptr) +{ + if(id == 1) listfull = ptr; + else if(id == 2) listhalf = ptr; +} + +/* ---------------------------------------------------------------------- + init for one type pair i,j and corresponding j,i +------------------------------------------------------------------------- */ +double PairMEAMSpline::init_one(int i, int j) +{ + return cutoff; +} + +/* ---------------------------------------------------------------------- */ + +int PairMEAMSpline::pack_comm(int n, int *list, double *buf, int pbc_flag, int *pbc) +{ + int* list_iter = list; + int* list_iter_end = list + n; + while(list_iter != list_iter_end) + *buf++ = Uprime_values[*list_iter++]; + return 1; +} + +/* ---------------------------------------------------------------------- */ + +void PairMEAMSpline::unpack_comm(int n, int first, double *buf) +{ + memcpy(&Uprime_values[first], buf, n * sizeof(buf[0])); +} + +/* ---------------------------------------------------------------------- */ + +int PairMEAMSpline::pack_reverse_comm(int n, int first, double *buf) +{ + return 0; +} + +/* ---------------------------------------------------------------------- */ + +void PairMEAMSpline::unpack_reverse_comm(int n, int *list, double *buf) +{ +} + +/* ---------------------------------------------------------------------- + Returns memory usage of local atom-based arrays +------------------------------------------------------------------------- */ +double PairMEAMSpline::memory_usage() +{ + return nmax * sizeof(double); // The Uprime_values array. +} + + +/// Parses the spline knots from a text file. +void PairMEAMSpline::SplineFunction::parse(FILE* fp, Error* error) +{ + char line[MAXLINE]; + + // Parse number of spline knots. + fgets(line, MAXLINE, fp); + int n = atoi(line); + if(n < 2) + error->one(FLERR,"Invalid number of spline knots in MEAM potential file"); + + // Parse first derivatives at beginning and end of spline. + fgets(line, MAXLINE, fp); + double d0 = atof(strtok(line, " \t\n\r\f")); + double dN = atof(strtok(NULL, " \t\n\r\f")); + init(n, d0, dN); + + // Skip line. + fgets(line, MAXLINE, fp); + + // Parse knot coordinates. + for(int i=0; ione(FLERR,"Invalid knot line in MEAM potential file"); + } + setKnot(i, x, y); + } + + prepareSpline(error); +} + +/// Calculates the second derivatives at the knots of the cubic spline. +void PairMEAMSpline::SplineFunction::prepareSpline(Error* error) +{ + xmin = X[0]; + xmax = X[N-1]; + + isGridSpline = true; + h = (xmax-xmin)/(N-1); + hsq = h*h; + + double* u = new double[N]; + Y2[0] = -0.5; + u[0] = (3.0/(X[1]-X[0])) * ((Y[1]-Y[0])/(X[1]-X[0]) - deriv0); + for(int i = 1; i <= N-2; i++) { + double sig = (X[i]-X[i-1]) / (X[i+1]-X[i-1]); + double p = sig * Y2[i-1] + 2.0; + Y2[i] = (sig - 1.0) / p; + u[i] = (Y[i+1]-Y[i]) / (X[i+1]-X[i]) - (Y[i]-Y[i-1])/(X[i]-X[i-1]); + u[i] = (6.0 * u[i]/(X[i+1]-X[i-1]) - sig*u[i-1])/p; + + if(fabs(h*i+xmin - X[i]) > 1e-8) + isGridSpline = false; + } + + double qn = 0.5; + double un = (3.0/(X[N-1]-X[N-2])) * (derivN - (Y[N-1]-Y[N-2])/(X[N-1]-X[N-2])); + Y2[N-1] = (un - qn*u[N-2]) / (qn * Y2[N-2] + 1.0); + for(int k = N-2; k >= 0; k--) { + Y2[k] = Y2[k] * Y2[k+1] + u[k]; + } + + delete[] u; + +#if !SPLINE_MEAM_SUPPORT_NON_GRID_SPLINES + if(!isGridSpline) + error->one(FLERR,"Support for MEAM potentials with non-uniform cubic splines has not been enabled in the MEAM potential code. Set SPLINE_MEAM_SUPPORT_NON_GRID_SPLINES in pair_spline_meam.h to 1 to enable it"); +#endif + + // Shift the spline to X=0 to speed up interpolation. + for(int i = 0; i < N; i++) { + Xs[i] = X[i] - xmin; +#if !SPLINE_MEAM_SUPPORT_NON_GRID_SPLINES + if(i < N-1) Ydelta[i] = (Y[i+1]-Y[i])/h; + Y2[i] /= h*6.0; +#endif + } + xmax_shifted = xmax - xmin; +} + +/// Broadcasts the spline function parameters to all processors. +void PairMEAMSpline::SplineFunction::communicate(MPI_Comm& world, int me) +{ + MPI_Bcast(&N, 1, MPI_INT, 0, world); + MPI_Bcast(&deriv0, 1, MPI_DOUBLE, 0, world); + MPI_Bcast(&derivN, 1, MPI_DOUBLE, 0, world); + MPI_Bcast(&xmin, 1, MPI_DOUBLE, 0, world); + MPI_Bcast(&xmax, 1, MPI_DOUBLE, 0, world); + MPI_Bcast(&xmax_shifted, 1, MPI_DOUBLE, 0, world); + MPI_Bcast(&isGridSpline, 1, MPI_INT, 0, world); + MPI_Bcast(&h, 1, MPI_DOUBLE, 0, world); + MPI_Bcast(&hsq, 1, MPI_DOUBLE, 0, world); + if(me != 0) { + X = new double[N]; + Xs = new double[N]; + Y = new double[N]; + Y2 = new double[N]; + Ydelta = new double[N]; + } + MPI_Bcast(X, N, MPI_DOUBLE, 0, world); + MPI_Bcast(Xs, N, MPI_DOUBLE, 0, world); + MPI_Bcast(Y, N, MPI_DOUBLE, 0, world); + MPI_Bcast(Y2, N, MPI_DOUBLE, 0, world); + MPI_Bcast(Ydelta, N, MPI_DOUBLE, 0, world); +} + +/// Writes a Gnuplot script that plots the spline function. +/// +/// This function is for debugging only! +void PairMEAMSpline::SplineFunction::writeGnuplot(const char* filename, const char* title) const +{ + FILE* fp = fopen(filename, "w"); + fprintf(fp, "#!/usr/bin/env gnuplot\n"); + if(title) fprintf(fp, "set title \"%s\"\n", title); + double tmin = X[0] - (X[N-1] - X[0]) * 0.05; + double tmax = X[N-1] + (X[N-1] - X[0]) * 0.05; + double delta = (tmax - tmin) / (N*200); + fprintf(fp, "set xrange [%f:%f]\n", tmin, tmax); + fprintf(fp, "plot '-' with lines notitle, '-' with points notitle pt 3 lc 3\n"); + for(double x = tmin; x <= tmax+1e-8; x += delta) { + double y = eval(x); + fprintf(fp, "%f %f\n", x, y); + } + fprintf(fp, "e\n"); + for(int i = 0; i < N; i++) { + fprintf(fp, "%f %f\n", X[i], Y[i]); + } + fprintf(fp, "e\n"); + fclose(fp); +} + diff --git a/src/USER-MISC/pair_meam_spline.h b/src/USER-MISC/pair_meam_spline.h new file mode 100644 index 0000000000..d95b26c2e6 --- /dev/null +++ b/src/USER-MISC/pair_meam_spline.h @@ -0,0 +1,274 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + 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. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + * Spline-based Modified Embedded Atom method (MEAM) potential routine. + * + * Copyright (2011) Lawrence Livermore National Security, LLC. + * Produced at the Lawrence Livermore National Laboratory. + * Written by Alexander Stukowski (). + * LLNL-CODE-525797 All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License (as published by the Free + * Software Foundation) version 2, dated June 1991. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the + * GNU General Public License for more details. + * + * Our Preamble Notice + * A. This notice is required to be provided under our contract with the + * U.S. Department of Energy (DOE). This work was produced at the + * Lawrence Livermore National Laboratory under Contract No. + * DE-AC52-07NA27344 with the DOE. + * + * B. Neither the United States Government nor Lawrence Livermore National + * Security, LLC nor any of their employees, makes any warranty, express or + * implied, or assumes any liability or responsibility for the accuracy, + * completeness, or usefulness of any information, apparatus, product, or + * process disclosed, or represents that its use would not infringe + * privately-owned rights. + * + * C. Also, reference herein to any specific commercial products, process, + * or services by trade name, trademark, manufacturer or otherwise does not + * necessarily constitute or imply its endorsement, recommendation, or + * favoring by the United States Government or Lawrence Livermore National + * Security, LLC. The views and opinions of authors expressed herein do not + * necessarily state or reflect those of the United States Government or + * Lawrence Livermore National Security, LLC, and shall not be used for + * advertising or product endorsement purposes. + * + * See file 'pair_spline_meam.cpp' for history of changes. +------------------------------------------------------------------------- */ + +#ifdef PAIR_CLASS + +PairStyle(meam/spline,PairMEAMSpline) + +#else + +#ifndef LMP_PAIR_MEAM_SPLINE_H +#define LMP_PAIR_MEAM_SPLINE_H + +#include "pair.h" + +namespace LAMMPS_NS { + +/// Set this to 1 if you intend to use MEAM potentials with non-uniform spline knots. +/// Set this to 0 if you intend to use only MEAM potentials with spline knots on a uniform grid. +/// +/// With SUPPORT_NON_GRID_SPLINES == 0, the code runs about 50% faster. + +#define SPLINE_MEAM_SUPPORT_NON_GRID_SPLINES 0 + +class PairMEAMSpline : public Pair +{ +public: + PairMEAMSpline(class LAMMPS *); + ~PairMEAMSpline(); + void compute(int, int); + void settings(int, char **); + void coeff(int, char **); + void init_style(); + void init_list(int, class NeighList *); + double init_one(int, int); + + int pack_comm(int, int *, double *, int, int *); + void unpack_comm(int, int, double *); + int pack_reverse_comm(int, int, double *); + void unpack_reverse_comm(int, int *, double *); + double memory_usage(); + +private: + + class SplineFunction { + public: + + /// Default constructor. + SplineFunction() : N(0), X(NULL), Xs(NULL), Y(NULL), Y2(NULL), Ydelta(NULL) {} + + /// Destructor. + ~SplineFunction() { + delete[] X; + delete[] Xs; + delete[] Y; + delete[] Y2; + delete[] Ydelta; + } + + /// Initialization of spline function. + void init(int _N, double _deriv0, double _derivN) { + N = _N; + deriv0 = _deriv0; + derivN = _derivN; + delete[] X; + delete[] Xs; + delete[] Y; + delete[] Y2; + delete[] Ydelta; + X = new double[N]; + Xs = new double[N]; + Y = new double[N]; + Y2 = new double[N]; + Ydelta = new double[N]; + } + + /// Adds a knot to the spline. + void setKnot(int n, double x, double y) { X[n] = x; Y[n] = y; } + + /// Returns the number of knots. + int numKnots() const { return N; } + + /// Parses the spline knots from a text file. + void parse(FILE* fp, Error* error); + + /// Calculates the second derivatives of the cubic spline. + void prepareSpline(Error* error); + + /// Evaluates the spline function at position x. + inline double eval(double x) const + { + x -= xmin; + if(x <= 0.0) { // Left extrapolation. + return Y[0] + deriv0 * x; + } + else if(x >= xmax_shifted) { // Right extrapolation. + return Y[N-1] + derivN * (x - xmax_shifted); + } + else { +#if SPLINE_MEAM_SUPPORT_NON_GRID_SPLINES + // Do interval search. + int klo = 0; + int khi = N-1; + while(khi - klo > 1) { + int k = (khi + klo) / 2; + if(Xs[k] > x) khi = k; + else klo = k; + } + double h = Xs[khi] - Xs[klo]; + // Do spline interpolation. + double a = (Xs[khi] - x)/h; + double b = 1.0 - a; // = (x - X[klo])/h + return a * Y[klo] + b * Y[khi] + ((a*a*a - a) * Y2[klo] + (b*b*b - b) * Y2[khi])*(h*h)/6.0; +#else + // For a spline with grid points, we can directly calculate the interval X is in. + int klo = (int)(x / h); + int khi = klo + 1; + double a = Xs[khi] - x; + double b = h - a; + return Y[khi] - a * Ydelta[klo] + ((a*a - hsq) * a * Y2[klo] + (b*b - hsq) * b * Y2[khi]); +#endif + } + } + + /// Evaluates the spline function and its first derivative at position x. + inline double eval(double x, double& deriv) const + { + x -= xmin; + if(x <= 0.0) { // Left extrapolation. + deriv = deriv0; + return Y[0] + deriv0 * x; + } + else if(x >= xmax_shifted) { // Right extrapolation. + deriv = derivN; + return Y[N-1] + derivN * (x - xmax_shifted); + } + else { +#if SPLINE_MEAM_SUPPORT_NON_GRID_SPLINES + // Do interval search. + int klo = 0; + int khi = N-1; + while(khi - klo > 1) { + int k = (khi + klo) / 2; + if(Xs[k] > x) khi = k; + else klo = k; + } + double h = Xs[khi] - Xs[klo]; + // Do spline interpolation. + double a = (Xs[khi] - x)/h; + double b = 1.0 - a; // = (x - X[klo])/h + deriv = (Y[khi] - Y[klo]) / h + ((3.0*b*b - 1.0) * Y2[khi] - (3.0*a*a - 1.0) * Y2[klo]) * h / 6.0; + return a * Y[klo] + b * Y[khi] + ((a*a*a - a) * Y2[klo] + (b*b*b - b) * Y2[khi]) * (h*h) / 6.0; +#else + // For a spline with grid points, we can directly calculate the interval X is in. + int klo = (int)(x / h); + int khi = klo + 1; + double a = Xs[khi] - x; + double b = h - a; + deriv = Ydelta[klo] + ((3.0*b*b - hsq) * Y2[khi] - (3.0*a*a - hsq) * Y2[klo]); + return Y[khi] - a * Ydelta[klo] + ((a*a - hsq) * a * Y2[klo] + (b*b - hsq) * b * Y2[khi]); +#endif + } + } + + /// Returns the number of bytes used by this function object. + double memory_usage() const { return sizeof(*this) + sizeof(X[0]) * N * 3; } + + /// Returns the cutoff radius of this function. + double cutoff() const { return X[N-1]; } + + /// Writes a Gnuplot script that plots the spline function. + void writeGnuplot(const char* filename, const char* title = NULL) const; + + /// Broadcasts the spline function parameters to all processors. + void communicate(MPI_Comm& world, int me); + + private: + double* X; // Positions of spline knots + double* Xs; // Shifted positions of spline knots + double* Y; // Function values at spline knots + double* Y2; // Second derivatives at spline knots + double* Ydelta; // If this is a grid spline, Ydelta[i] = (Y[i+1]-Y[i])/h + int N; // Number of spline knots + double deriv0; // First derivative at knot 0 + double derivN; // First derivative at knot (N-1) + double xmin; // The beginning of the interval on which the spline function is defined. + double xmax; // The end of the interval on which the spline function is defined. + int isGridSpline; // Indicates that all spline knots are on a regular grid. + double h; // The distance between knots if this is a grid spline with equidistant knots. + double hsq; // The squared distance between knots if this is a grid spline with equidistant knots. + double xmax_shifted; // The end of the spline interval after it has been shifted to begin at X=0. + }; + + /// Helper data structure for potential routine. + struct MEAM2Body { + int tag; + double r; + double f, fprime; + double del[3]; + }; + + SplineFunction phi; // Phi(r_ij) + SplineFunction rho; // Rho(r_ij) + SplineFunction f; // f(r_ij) + SplineFunction U; // U(rho) + SplineFunction g; // g(cos_theta) + double zero_atom_energy; // Shift embedding energy by this value to make it zero for a single atom in vacuum. + + double cutoff; // The cutoff radius + + double* Uprime_values; // Used for temporary storage of U'(rho) values + int nmax; // Size of temporary array. + int maxNeighbors; // The last maximum number of neighbors a single atoms has. + MEAM2Body* twoBodyInfo; // Temporary array. + + void read_file(const char* filename); + void allocate(); +}; + +} + +#endif +#endif