From ce46d52c8a9ab27383d797fc29c9108f4ed59ccd Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 24 May 2019 10:23:26 -0400 Subject: [PATCH] add new 'kim_style' command as front end for KIM simulator (and regular) models --- examples/kim/in.kim.simulator-model | 4 +- src/KIM/fix_store_kim.cpp | 127 ++++++++++++ src/KIM/fix_store_kim.h | 98 ++++++++++ src/KIM/kim_query.cpp | 1 - src/KIM/kim_style.cpp | 286 ++++++++++++++++++++++++++++ src/KIM/kim_style.h | 87 +++++++++ src/modify.cpp | 2 +- 7 files changed, 600 insertions(+), 5 deletions(-) create mode 100644 src/KIM/fix_store_kim.cpp create mode 100644 src/KIM/fix_store_kim.h create mode 100644 src/KIM/kim_style.cpp create mode 100644 src/KIM/kim_style.h diff --git a/examples/kim/in.kim.simulator-model b/examples/kim/in.kim.simulator-model index 109711fffc..1e6e9dec89 100644 --- a/examples/kim/in.kim.simulator-model +++ b/examples/kim/in.kim.simulator-model @@ -16,7 +16,6 @@ variable zz equal 20*$z units metal atom_style atomic -newton off lattice fcc 4.4300 region box block 0 ${xx} 0 ${yy} 0 ${zz} @@ -26,8 +25,7 @@ create_atoms 1 box #pair_style lj/cut 8.1500 #pair_coeff 1 1 0.0104 3.4000 -pair_style kim ex_sim_model_Si_mod_tersoff -pair_coeff * * Si Si +kim_style define ex_sim_model_Si_mod_tersoff Si Si mass * 39.95 velocity all create 200.0 232345 loop geom diff --git a/src/KIM/fix_store_kim.cpp b/src/KIM/fix_store_kim.cpp new file mode 100644 index 0000000000..8e9946c20d --- /dev/null +++ b/src/KIM/fix_store_kim.cpp @@ -0,0 +1,127 @@ +/* ---------------------------------------------------------------------- + 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. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing authors: Axel Kohlmeyer (Temple U), + Ryan S. Elliott (UMN) +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + 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; either version 2 of the License, or (at your option) + any later version. + + 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 GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along with + this program; if not, see . + + Linking LAMMPS statically or dynamically with other modules is making a + combined work based on LAMMPS. Thus, the terms and conditions of the GNU + General Public License cover the whole combination. + + In addition, as a special exception, the copyright holders of LAMMPS give + you permission to combine LAMMPS with free software programs or libraries + that are released under the GNU LGPL and with code included in the standard + release of the "kim-api" under the CDDL (or modified versions of such code, + with unchanged license). You may copy and distribute such a system following + the terms of the GNU GPL for LAMMPS and the licenses of the other code + concerned, provided that you include the source code of that other code + when and as the GNU GPL requires distribution of source code. + + Note that people who make modified versions of LAMMPS are not obligated to + grant this special exception for their modified versions; it is their choice + whether to do so. The GNU General Public License gives permission to release + a modified version without this exception; this exception also makes it + possible to release a modified version which carries forward this exception. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Designed for use with the kim-api-2.0.2 (and newer) package +------------------------------------------------------------------------- */ + +#include +#include "fix_store_kim.h" +#include "KIM_SimulatorModel.hpp" +#include "error.h" + +using namespace LAMMPS_NS; +using namespace FixConst; + +/* ---------------------------------------------------------------------- */ + +FixStoreKIM::FixStoreKIM(LAMMPS *lmp, int narg, char **arg) + : Fix(lmp, narg, arg), simulator_model(NULL), model_name(NULL) +{ + if (narg != 3) error->all(FLERR,"Illegal fix STORE/KIM command"); +} + +/* ---------------------------------------------------------------------- */ + +FixStoreKIM::~FixStoreKIM() +{ + // free associated storage + + if (simulator_model) { + KIM::SimulatorModel *sm = (KIM::SimulatorModel *)simulator_model; + KIM::SimulatorModel::Destroy(&sm); + simulator_model = NULL; + } + + if (model_name) { + char *mn = (char *)model_name; + delete[] mn; + model_name = NULL; + } +} + +/* ---------------------------------------------------------------------- */ + +int FixStoreKIM::setmask() +{ + int mask = 0; + return mask; +} + + +/* ---------------------------------------------------------------------- */ + +void FixStoreKIM::setptr(const char *name, void *ptr) +{ + if (strcmp(name,"simulator_model") == 0) { + if (simulator_model) { + KIM::SimulatorModel *sm = (KIM::SimulatorModel *)simulator_model; + KIM::SimulatorModel::Destroy(&sm); + } + simulator_model = ptr; + } else if (strcmp(name,"model_name") == 0) { + if (model_name) { + char *mn = (char *)model_name; + delete[] mn; + } + model_name = ptr; + } +} + +/* ---------------------------------------------------------------------- */ + +void *FixStoreKIM::getptr(const char *name) +{ + if (strcmp(name,"simulator_model") == 0) return simulator_model; + else if (strcmp(name,"model_name") == 0) return model_name; + else return NULL; +} diff --git a/src/KIM/fix_store_kim.h b/src/KIM/fix_store_kim.h new file mode 100644 index 0000000000..04081fd6dc --- /dev/null +++ b/src/KIM/fix_store_kim.h @@ -0,0 +1,98 @@ +/* -*- c++ -*- ---------------------------------------------------------- + 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. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing authors: Axel Kohlmeyer (Temple U), + Ryan S. Elliott (UMN) +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + 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; either version 2 of the License, or (at your option) + any later version. + + 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 GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along with + this program; if not, see . + + Linking LAMMPS statically or dynamically with other modules is making a + combined work based on LAMMPS. Thus, the terms and conditions of the GNU + General Public License cover the whole combination. + + In addition, as a special exception, the copyright holders of LAMMPS give + you permission to combine LAMMPS with free software programs or libraries + that are released under the GNU LGPL and with code included in the standard + release of the "kim-api" under the CDDL (or modified versions of such code, + with unchanged license). You may copy and distribute such a system following + the terms of the GNU GPL for LAMMPS and the licenses of the other code + concerned, provided that you include the source code of that other code + when and as the GNU GPL requires distribution of source code. + + Note that people who make modified versions of LAMMPS are not obligated to + grant this special exception for their modified versions; it is their choice + whether to do so. The GNU General Public License gives permission to release + a modified version without this exception; this exception also makes it + possible to release a modified version which carries forward this exception. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Designed for use with the kim-api-2.0.2 (and newer) package +------------------------------------------------------------------------- */ + +#ifdef FIX_CLASS + +FixStyle(STORE/KIM,FixStoreKIM) + +#else + +#ifndef LMP_FIX_STORE_KIM_H +#define LMP_FIX_STORE_KIM_H + +#include +#include "fix.h" + +namespace LAMMPS_NS { + +class FixStoreKIM : public Fix { + public: + FixStoreKIM(class LAMMPS *, int, char **); + ~FixStoreKIM(); + int setmask(); + + void setptr(const char *, void *); + void *getptr(const char *); + + private: + void *simulator_model; // pointer to KIM simulator model class + void *model_name; // string of KIM model name +}; + +} + +#endif +#endif + +/* ERROR/WARNING messages: + +E: Illegal ... command + +Self-explanatory. Check the input script syntax and compare to the +documentation for the command. You can use -echo screen as a +command-line option when running LAMMPS to see the offending line. + +*/ diff --git a/src/KIM/kim_query.cpp b/src/KIM/kim_query.cpp index fedc976110..e4818abc94 100644 --- a/src/KIM/kim_query.cpp +++ b/src/KIM/kim_query.cpp @@ -11,7 +11,6 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ - /* ---------------------------------------------------------------------- Contributing authors: Axel Kohlmeyer (Temple U), Ryan S. Elliott (UMN) diff --git a/src/KIM/kim_style.cpp b/src/KIM/kim_style.cpp new file mode 100644 index 0000000000..865f6827ab --- /dev/null +++ b/src/KIM/kim_style.cpp @@ -0,0 +1,286 @@ +/* ---------------------------------------------------------------------- + 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. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing authors: Axel Kohlmeyer (Temple U), + Ryan S. Elliott (UMN) +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + 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; either version 2 of the License, or (at your option) + any later version. + + 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 GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along with + this program; if not, see . + + Linking LAMMPS statically or dynamically with other modules is making a + combined work based on LAMMPS. Thus, the terms and conditions of the GNU + General Public License cover the whole combination. + + In addition, as a special exception, the copyright holders of LAMMPS give + you permission to combine LAMMPS with free software programs or libraries + that are released under the GNU LGPL and with code included in the standard + release of the "kim-api" under the CDDL (or modified versions of such code, + with unchanged license). You may copy and distribute such a system following + the terms of the GNU GPL for LAMMPS and the licenses of the other code + concerned, provided that you include the source code of that other code + when and as the GNU GPL requires distribution of source code. + + Note that people who make modified versions of LAMMPS are not obligated to + grant this special exception for their modified versions; it is their choice + whether to do so. The GNU General Public License gives permission to release + a modified version without this exception; this exception also makes it + possible to release a modified version which carries forward this exception. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Designed for use with the kim-api-2.0.2 (and newer) package +------------------------------------------------------------------------- */ + +#include +#include +#include "kim_style.h" +#include "error.h" +#include "atom.h" +#include "comm.h" +#include "domain.h" +#include "modify.h" +#include "update.h" +#include "universe.h" +#include "input.h" +#include "fix_store_kim.h" + +#include "KIM_SimulatorModel.hpp" + + +using namespace LAMMPS_NS; + +/* ---------------------------------------------------------------------- */ + +void KimStyle::command(int narg, char **arg) +{ + if (narg < 2) error->all(FLERR,"Illegal kim_style command"); + + if (strcmp(arg[0],"init") == 0) { + if (narg > 2) error->all(FLERR,"Illegal kim_style init command"); + if (domain->box_exist) + error->all(FLERR,"Must use 'kim_style init' command before " + "simulation box is defined"); + int len = strlen(arg[1])+1; + char *model = new char[len]; + strcpy(model,arg[1]); + do_init(model); + } else if (strcmp(arg[0],"define") == 0) { + if (!domain->box_exist) + error->all(FLERR,"Must use 'kim_style define' command after " + "simulation box is defined"); + do_defn(narg-1,arg+1); + } else error->all(FLERR,"Illegal kim_style command"); +} + + +/* ---------------------------------------------------------------------- */ + +void KimStyle::do_init(char *model) +{ + // create storage proxy fix. delete existing fix, if needed. + + int ifix = modify->find_fix("KIM_MODEL_STORE"); + if (ifix >= 0) modify->delete_fix(ifix); + input->one("fix KIM_MODEL_STORE all STORE/KIM"); + ifix = modify->find_fix("KIM_MODEL_STORE"); + + FixStoreKIM *fix_store = (FixStoreKIM *) modify->fix[ifix]; + fix_store->setptr("model_name", (void *) model); + + int kimerror; + KIM::SimulatorModel * simulatorModel; + kimerror = KIM::SimulatorModel::Create(model,&simulatorModel); + + // not a Kim Simulator Model; nothing else to do here. + if (kimerror) return; + + fix_store->setptr("simulator_model", (void *) simulatorModel); + + // need to call this to have access to (some) simulator model init data. + simulatorModel->CloseTemplateMap(); + + int sim_fields, sim_lines; + const std::string *sim_field, *sim_value; + simulatorModel->GetNumberOfSimulatorFields(&sim_fields); + + // set units + for (int i=0; i < sim_fields; ++i) { + simulatorModel->GetSimulatorFieldMetadata(i,&sim_lines,&sim_field); + if (*sim_field == "units") { + simulatorModel->GetSimulatorFieldLine(i,0,&sim_value); + std::string cmd("units "); + cmd += *sim_value; + input->one(cmd.c_str()); + break; + } + } + + // init model + for (int i=0; i < sim_fields; ++i) { + simulatorModel->GetSimulatorFieldMetadata(i,&sim_lines,&sim_field); + if (*sim_field == "model-init") { + for (int j=0; j < sim_lines; ++j) { + simulatorModel->GetSimulatorFieldLine(i,j,&sim_value); + input->one(sim_value->c_str()); + } + break; + } + } + + // reset template map. + simulatorModel->ClearTemplateMap(); +} + +/* ---------------------------------------------------------------------- */ + +void KimStyle::do_defn(int narg, char **arg) +{ + if (narg != atom->ntypes + 1) + error->all(FLERR,"Incorrect number of arguments for kim_style define command"); + + char *model = arg[0]; + KIM::SimulatorModel *simulatorModel(NULL); + int kimerror; + + // check if we had a kim_style init command by finding fix STORE/KIM + // retrieve model name and pointer to simulator model class instance. + // validate model name if not given as NULL. + // if kim_style init wasn't run try to initialize simulator model now. + + int ifix = modify->find_fix("KIM_MODEL_STORE"); + if (ifix >= 0) { + FixStoreKIM *fix_store = (FixStoreKIM *) modify->fix[ifix]; + if (strcmp(model,"NULL") == 0) + model = (char *)fix_store->getptr("model_name"); + else if (strcmp(model,(const char*)fix_store->getptr("model_name")) != 0) + error->all(FLERR,"Inconsistent KIM model name"); + + simulatorModel = (KIM::SimulatorModel *)fix_store->getptr("simulator_model"); + } else { + + kimerror = KIM::SimulatorModel::Create(model,&simulatorModel); + if (kimerror) simulatorModel = NULL; + } + + if (simulatorModel) { + + const std::string *sim_name, *sim_version; + std::string atom_type_sym_list; + + simulatorModel->GetSimulatorName(&sim_name); + simulatorModel->GetSimulatorVersion(&sim_version); + + if (comm->me == 0) { + std::string mesg("Using KIM Simulator Model : "); + mesg += model; + mesg += "\n"; + mesg += "For Simulator : "; + mesg += *sim_name + " " + *sim_version + "\n"; + mesg += "Running on : LAMMPS "; + mesg += universe->version; + mesg += "\n"; + + if (screen) fputs(mesg.c_str(),screen); + if (logfile) fputs(mesg.c_str(),logfile); + } + + if (*sim_name != "LAMMPS") + error->all(FLERR,"Incompatible KIM Simulator Model"); + + for (int i = 1; i < narg; i++) + atom_type_sym_list += std::string(" ") + arg[i]; + + simulatorModel->AddTemplateMap("atom-type-sym-list",atom_type_sym_list); + simulatorModel->CloseTemplateMap(); + + int len = strlen(atom_type_sym_list.c_str())+1; + char *strbuf = new char[len]; + char *strword; + + // validate species selection + + int sim_num_species; + const std::string *sim_species; + simulatorModel->GetNumberOfSupportedSpecies(&sim_num_species); + for (int i=0; i < sim_num_species; ++i) { + simulatorModel->GetSupportedSpecies(i, &sim_species); + strcpy(strbuf,atom_type_sym_list.c_str()); + strword = strtok(strbuf," \t"); + while (strword) { + if ((strcmp(strword,"NULL") != 0) && (strcmp(sim_species->c_str(),strword) != 0)) + error->all(FLERR,"Species not supported by KIM Simulator Model"); + strword = strtok(NULL," \t"); + } + } + delete[] strbuf; + + int sim_fields, sim_lines; + const std::string *sim_field, *sim_value; + simulatorModel->GetNumberOfSimulatorFields(&sim_fields); + for (int i=0; i < sim_fields; ++i) { + simulatorModel->GetSimulatorFieldMetadata(i,&sim_lines,&sim_field); + if (*sim_field == "units") { + simulatorModel->GetSimulatorFieldLine(i,0,&sim_value); + if (*sim_value != update->unit_style) + error->all(FLERR,"Incompatible units for KIM Simulator Model"); + break; + } + } + + int sim_model_idx=-1; + for (int i=0; i < sim_fields; ++i) { + simulatorModel->GetSimulatorFieldMetadata(i,&sim_lines,&sim_field); + if (*sim_field == "model-defn") { + sim_model_idx = i; + for (int j=0; j < sim_lines; ++j) { + simulatorModel->GetSimulatorFieldLine(sim_model_idx,j,&sim_value); + input->one(sim_value->c_str()); + } + } + } + + if (sim_model_idx < 0) + error->all(FLERR,"KIM Simulator Model has no Model definition"); + + } else { + + // not a simulator model. issue pair_style and pair_coeff commands. + // NOTE: all references to arg must appear before calls to input->one() + // as that will reset the argument vector. + + std::string cmd1("pair_style kim "); + cmd1 += model; + + std::string cmd2("pair_coeff * * "); + for (int i=1; i < narg; ++i) { + cmd2 += arg[i]; + cmd2 += " "; + } + + input->one(cmd1.c_str()); + input->one(cmd2.c_str()); + } +} diff --git a/src/KIM/kim_style.h b/src/KIM/kim_style.h new file mode 100644 index 0000000000..588de6e620 --- /dev/null +++ b/src/KIM/kim_style.h @@ -0,0 +1,87 @@ +/* -*- c++ -*- ---------------------------------------------------------- + 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. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Contributing authors: Axel Kohlmeyer (Temple U), + Ryan S. Elliott (UMN) +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + 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; either version 2 of the License, or (at your option) + any later version. + + 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 GNU General Public License for + more details. + + You should have received a copy of the GNU General Public License along with + this program; if not, see . + + Linking LAMMPS statically or dynamically with other modules is making a + combined work based on LAMMPS. Thus, the terms and conditions of the GNU + General Public License cover the whole combination. + + In addition, as a special exception, the copyright holders of LAMMPS give + you permission to combine LAMMPS with free software programs or libraries + that are released under the GNU LGPL and with code included in the standard + release of the "kim-api" under the CDDL (or modified versions of such code, + with unchanged license). You may copy and distribute such a system following + the terms of the GNU GPL for LAMMPS and the licenses of the other code + concerned, provided that you include the source code of that other code + when and as the GNU GPL requires distribution of source code. + + Note that people who make modified versions of LAMMPS are not obligated to + grant this special exception for their modified versions; it is their choice + whether to do so. The GNU General Public License gives permission to release + a modified version without this exception; this exception also makes it + possible to release a modified version which carries forward this exception. +------------------------------------------------------------------------- */ + +/* ---------------------------------------------------------------------- + Designed for use with the kim-api-2.0.2 (and newer) package +------------------------------------------------------------------------- */ + +#ifdef COMMAND_CLASS + +CommandStyle(kim_style,KimStyle) + +#else + +#ifndef LMP_KIM_STYLE_H +#define LMP_KIM_STYLE_H + +#include "pointers.h" + +namespace LAMMPS_NS { + +class KimStyle : protected Pointers { + public: + KimStyle(class LAMMPS *lmp) : Pointers(lmp) {}; + void command(int, char **); + private: + void do_init(char *); + void do_defn(int, char **); +}; + +} + +#endif +#endif + +/* ERROR/WARNING messages: + + +*/ diff --git a/src/modify.cpp b/src/modify.cpp index 7f43f035d2..9f0457c9d0 100644 --- a/src/modify.cpp +++ b/src/modify.cpp @@ -793,7 +793,7 @@ void Modify::add_fix(int narg, char **arg, int trysuffix) const char *exceptions[] = {"GPU", "OMP", "INTEL", "property/atom", "cmap", "cmap3", "rx", - "deprecated", NULL}; + "deprecated", "STORE/KIM", NULL}; if (domain->box_exist == 0) { int m;