Unverified Commit 472de2e8 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer Committed by GitHub
Browse files

Merge pull request #2238 from yafshar/kim_fix

KIM package corrections and refactoring
parents 39cefc4e cbdf1d3a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ above, create a test.py file containing these lines:
   pypar.finalize()

To run LAMMPS in parallel, assuming you have installed the
`mpi4py <https://bitbucket.org/mpi4py/mpi4py>`_ package as discussed
`mpi4py <https://mpi4py.readthedocs.io>`_ package as discussed
above, create a test.py file containing these lines:

.. code-block:: python
+1 −1
Original line number Diff line number Diff line
@@ -96,6 +96,7 @@ FixStoreKIM::~FixStoreKIM()
    delete[] mu;
    model_units = NULL;
  }

  if (user_units) {
    char *uu = (char *)user_units;
    delete[] uu;
@@ -111,7 +112,6 @@ int FixStoreKIM::setmask()
  return mask;
}


/* ---------------------------------------------------------------------- */

void FixStoreKIM::setptr(const char *name, void *ptr)
+56 −90
Original line number Diff line number Diff line
@@ -80,14 +80,6 @@ extern "C" {
#include "KIM_SimulatorHeaders.h"
}

#ifdef SNUM
#undef SNUM
#endif

#define SNUM(x)                                                \
  static_cast<std::ostringstream const &>(std::ostringstream() \
                                          << std::dec << x).str()

using namespace LAMMPS_NS;

/* ---------------------------------------------------------------------- */
@@ -111,6 +103,11 @@ void KimInit::command(int narg, char **arg)
  char *model_units;
  KIM_Model *pkim = NULL;

  if (universe->me == 0)
    std::remove("kim.log");
  if (universe->nprocs > 1)
    MPI_Barrier(universe->uworld);

  determine_model_type_and_units(model_name, user_units, &model_units, pkim);

  write_log_cite(model_name);
@@ -118,8 +115,8 @@ void KimInit::command(int narg, char **arg)
  do_init(model_name, user_units, model_units, pkim);
}


/* ---------------------------------------------------------------------- */

namespace {
void get_kim_unit_names(
    char const * const system,
@@ -167,6 +164,7 @@ void get_kim_unit_names(
  }
}
}  // namespace

void KimInit::determine_model_type_and_units(char * model_name,
                                             char * user_units,
                                             char ** model_units,
@@ -193,8 +191,7 @@ void KimInit::determine_model_type_and_units(char * model_name,
  KIM_Collections_Destroy(&kim_Coll);

  if (KIM_CollectionItemType_Equal(itemType,
                                   KIM_COLLECTION_ITEM_TYPE_portableModel))
  {
                                   KIM_COLLECTION_ITEM_TYPE_portableModel)) {
    get_kim_unit_names(user_units, lengthUnit, energyUnit,
                       chargeUnit, temperatureUnit, timeUnit, error);
    int kim_error = KIM_Model_Create(KIM_NUMBERING_zeroBased,
@@ -245,8 +242,7 @@ void KimInit::determine_model_type_and_units(char * model_name,
      KIM_Model_Destroy(&pkim);
      error->all(FLERR,"KIM Model does not support the requested unit system");
    }
  }
  else if (KIM_CollectionItemType_Equal(
  } else if (KIM_CollectionItemType_Equal(
               itemType, KIM_COLLECTION_ITEM_TYPE_simulatorModel)) {
    KIM_SimulatorModel * kim_SM;
    kim_error = KIM_SimulatorModel_Create(model_name, &kim_SM);
@@ -282,7 +278,6 @@ void KimInit::determine_model_type_and_units(char * model_name,
  }
}


/* ---------------------------------------------------------------------- */

void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM_Model *&pkim)
@@ -302,11 +297,12 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
  // Begin output to log file
  input->write_echo("#=== BEGIN kim-init ==========================================\n");

  int kimerror;
  KIM_SimulatorModel * simulatorModel;
  if (model_type == SM)
  {
    kimerror = KIM_SimulatorModel_Create(model_name,&simulatorModel);
  if (model_type == SM) {
    int kim_error =
      KIM_SimulatorModel_Create(model_name,&simulatorModel);
    if (kim_error)
      error->all(FLERR,"Unable to load KIM Simulator Model.");

    char const *sim_name, *sim_version;
    KIM_SimulatorModel_GetSimulatorNameAndVersion(
@@ -367,60 +363,38 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM

    // reset template map.
    KIM_SimulatorModel_OpenAndInitializeTemplateMap(simulatorModel);
  }
  else if (model_type == MO)
  {
  } else if (model_type == MO) {
    int numberOfParameters;
    KIM_Model_GetNumberOfParameters(pkim, &numberOfParameters);

    std::string mesg = "\nThis model has ";
    if (numberOfParameters)
    {
    if (numberOfParameters) {
      KIM_DataType kim_DataType;
      int extent;
      char const *str_name = NULL;
      char const *str_desc = NULL;

      mesg += SNUM(numberOfParameters);
      mesg += " mutable parameters. \n";
      mesg += std::to_string(numberOfParameters) + " mutable parameters. \n";

      int max_len(0);
      for (int i = 0; i < numberOfParameters; ++i)
      {
      for (int i = 0; i < numberOfParameters; ++i) {
        KIM_Model_GetParameterMetadata(pkim, i, &kim_DataType,
        &extent, &str_name, &str_desc);
        max_len = MAX(max_len, strlen(str_name));
        max_len = MAX(max_len, (int)strlen(str_name));
      }
      ++max_len;
      mesg += "No.     |  Parameter name  ";
      for (int i = 18; i < max_len; ++i)
        mesg += " ";
      mesg += "|  data type  |  extent\n";
      for (int i = 0; i < 8 + MAX(18, max_len); ++i)
        mesg += "-";
      mesg += "-----------------------\n";
      for (int i = 0; i < numberOfParameters; ++i)
      {
      max_len = MAX(18,max_len+1);
      mesg += fmt::format(" No.      | {:<{}} | data type  | extent\n",
                          "Parameter name", max_len);
      mesg += fmt::format("{:-<{}}\n","-",max_len+35);
      for (int i = 0; i < numberOfParameters; ++i) {
        KIM_Model_GetParameterMetadata(pkim, i, &kim_DataType,
        &extent, &str_name, &str_desc);
        mesg += SNUM(i+1);
        for (int j = SNUM(i+1).size(); j < 8; ++j)
          mesg += " ";
        mesg += "| ";
        mesg += str_name;
        for (int j = strlen(str_name) + 1; j < MAX(18, strlen(str_name) + 1); ++j)
          mesg += " ";
        mesg += "|  \"";
        mesg += KIM_DataType_ToString(kim_DataType);
        if (KIM_DataType_Equal(kim_DataType, KIM_DATA_TYPE_Integer))
          mesg += "\"  | ";
        else
          mesg += "\"   | ";
        mesg += SNUM(extent);
        mesg += "\n";
      }
        auto data_type = std::string("\"");
        data_type += KIM_DataType_ToString(kim_DataType) + std::string("\"");
        mesg += fmt::format(" {:<8} | {:<{}} | {:<10} | {}\n",i+1,str_name,
                            max_len,data_type,extent);
      }
    else mesg += "No mutable parameters. \n";
    } else mesg += "No mutable parameters. \n";

    KIM_Model_Destroy(&pkim);
    input->write_echo(mesg);
@@ -432,51 +406,45 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM

/* ---------------------------------------------------------------------- */

void KimInit::do_variables(char *user_units, char *model_units)
void KimInit::do_variables(const std::string &from, const std::string &to)
{
  char *from = user_units, *to = model_units;
  Variable *variable = input->variable;

  // refuse conversion from or to reduced units

  if ((strcmp(from,"lj") == 0) || (strcmp(to,"lj") == 0))
  if ((from == "lj") || (to == "lj"))
    error->all(FLERR,"Cannot set up conversion variables for 'lj' units");

  // get index to internal style variables. create, if needed.
  // set conversion factors for newly created variables.
  double conversion_factor;
  int ier;
  char *args[3];
  std::string var_str;
  args[1] = (char *)"internal";
  args[2] = (char *)"1.0";
  int v_unit;
  int const nunits = 14;
  char *units[nunits] = {(char *)"mass",
                         (char *)"distance",
                         (char *)"time",
                         (char *)"energy",
                         (char *)"velocity",
                         (char *)"force",
                         (char *)"torque",
                         (char *)"temperature",
                         (char *)"pressure",
                         (char *)"viscosity",
                         (char *)"charge",
                         (char *)"dipole",
                         (char *)"efield",
                         (char *)"density"};
  const char *units[] = {"mass",
                         "distance",
                         "time",
                         "energy",
                         "velocity",
                         "force",
                         "torque",
                         "temperature",
                         "pressure",
                         "viscosity",
                         "charge",
                         "dipole",
                         "efield",
                         "density",
                         nullptr};

  input->write_echo(fmt::format("# Conversion factors from {} to {}:\n",
                                from,to));

  for (int i = 0; i < nunits; i++) {
    var_str = std::string("_u_") + std::string(units[i]);
    args[0] = (char *)var_str.c_str();
    v_unit = variable->find(args[0]);
  auto variable = input->variable;
  for (int i = 0; units[i] != nullptr; ++i) {
    var_str = std::string("_u_") + units[i];
    v_unit = variable->find(var_str.c_str());
    if (v_unit < 0) {
      variable->set(3,args);
      v_unit = variable->find(args[0]);
      variable->set(var_str + " internal 1.0");
      v_unit = variable->find(var_str.c_str());
    }
    ier = lammps_unit_conversion(units[i],
                                 from,
@@ -496,7 +464,7 @@ void KimInit::do_variables(char *user_units, char *model_units)

/* ---------------------------------------------------------------------- */

void KimInit::write_log_cite(const std::string &model_name)
void KimInit::write_log_cite(char *model_name)
{
  KIM_Collections * coll;
  int err = KIM_Collections_Create(&coll);
@@ -505,10 +473,10 @@ void KimInit::write_log_cite(const std::string &model_name)
  int extent;
  if (model_type == MO) {
    err = KIM_Collections_CacheListOfItemMetadataFiles(
      coll,KIM_COLLECTION_ITEM_TYPE_portableModel,model_name.c_str(),&extent);
      coll,KIM_COLLECTION_ITEM_TYPE_portableModel,model_name,&extent);
  } else if (model_type == SM) {
    err = KIM_Collections_CacheListOfItemMetadataFiles(
      coll,KIM_COLLECTION_ITEM_TYPE_simulatorModel,model_name.c_str(),&extent);
      coll,KIM_COLLECTION_ITEM_TYPE_simulatorModel,model_name,&extent);
  } else {
    error->all(FLERR,"Unknown model type.");
  }
@@ -533,5 +501,3 @@ void KimInit::write_log_cite(const std::string &model_name)

  KIM_Collections_Destroy(&coll);
}

#undef SNUM
+2 −2
Original line number Diff line number Diff line
@@ -83,9 +83,9 @@ class KimInit : protected Pointers {
  bool unit_conversion_mode;

  void determine_model_type_and_units(char *, char *, char **, KIM_Model *&);
  void write_log_cite(const std::string &);
  void write_log_cite(char *);
  void do_init(char *, char *, char *, KIM_Model *&);
  void do_variables(char*, char*);
  void do_variables(const std::string &, const std::string &);
};

}
+26 −63
Original line number Diff line number Diff line
@@ -73,15 +73,12 @@
#include "variable.h"
#include "utils.h"
#include "fix_store_kim.h"
#include "fmt/format.h"

extern "C" {
#include "KIM_SimulatorHeaders.h"
}

#define SNUM(x)                                                \
  static_cast<std::ostringstream const &>(std::ostringstream() \
                                          << std::dec << x).str()

using namespace LAMMPS_NS;

#define MAXLINE 1024
@@ -105,11 +102,9 @@ void KimInteractions::do_setup(int narg, char **arg)
  bool fixed_types;
  if ((narg == 1) && (0 == strcmp("fixed_types",arg[0]))) {
    fixed_types = true;
  }
  else if (narg != atom->ntypes) {
  } else if (narg != atom->ntypes) {
    error->all(FLERR,"Illegal kim_interactions command");
  }
  else {
  } else {
    fixed_types = false;
  }

@@ -137,10 +132,9 @@ void KimInteractions::do_setup(int narg, char **arg)
      std::string atom_type_sym_list;
      std::string atom_type_num_list;

      for (int i = 0; i < narg; i++)
      {
      for (int i = 0; i < narg; i++) {
        atom_type_sym_list += delimiter + arg[i];
        atom_type_num_list += delimiter + SNUM(species_to_atomic_no(arg[i]));
        atom_type_num_list += delimiter + std::to_string(species_to_atomic_no(arg[i]));
        delimiter = " ";
      }

@@ -254,33 +248,23 @@ void KimInteractions::do_setup(int narg, char **arg)

/* ---------------------------------------------------------------------- */

void KimInteractions::KIM_SET_TYPE_PARAMETERS(char const *const input_line) const
void KimInteractions::KIM_SET_TYPE_PARAMETERS(const std::string &input_line) const
{
  char strbuf[MAXLINE];
  strcpy(strbuf,input_line);
  char *cmd, *key, *filename;
  int nocomment;
  cmd = strtok(strbuf," \t");
  key = strtok(NULL," \t");
  filename = strtok(NULL," \t");
  auto words = utils::split_words(input_line);

  std::string key = words[1];
  std::string filename = words[2];
  std::vector<std::string> species(words.begin()+3,words.end());
  if (species.size() != atom->ntypes)
    error->one(FLERR,"Incorrect args for KIM_SET_TYPE_PARAMETERS command");

  FILE *fp;
  fp = fopen(filename,"r");
  fp = fopen(filename.c_str(),"r");
  if (fp == NULL) {
    error->one(FLERR,"Parameter file not found");
  }

  char *species1, *species2, *the_rest;
  std::vector<char *> species;
  for (int i = 0; i < atom->ntypes; ++i)
  {
    char *str;
    str = strtok(NULL," \t");
    if (str == NULL)
      error->one(FLERR,"Incorrect args for KIM_SET_TYPE_PARAMETERS command");
    species.push_back(str);
  }

  char line[MAXLINE],*ptr;
  int n, eof = 0;

@@ -301,40 +285,19 @@ void KimInteractions::KIM_SET_TYPE_PARAMETERS(char const *const input_line) cons
    nocomment = line[0] != '#';

    if(nocomment) {
      if (strcmp(key,"pair") == 0) {
        species1 = strtok(ptr," \t");
        species2 = strtok(NULL," \t");
        the_rest = strtok(NULL,"\n");

        for (int type_a = 0; type_a < atom->ntypes; ++type_a) {
          for (int type_b = type_a; type_b < atom->ntypes; ++type_b) {
            if(((strcmp(species[type_a],species1) == 0) &&
                (strcmp(species[type_b],species2) == 0))
               ||
               ((strcmp(species[type_b],species1) == 0) &&
                (strcmp(species[type_a],species2) == 0))
               ) {
              char pair_command[MAXLINE];
              sprintf(pair_command,"pair_coeff %i %i %s",type_a+1,type_b+1,
                      the_rest);
              input->one(pair_command);
            }
          }
        }
      }
      else if (strcmp(key,"charge") == 0) {
        species1 = strtok(ptr," \t");
        the_rest = strtok(NULL,"\n");

        for (int type_a = 0; type_a < atom->ntypes; ++type_a) {
          if(strcmp(species[type_a],species1) == 0) {
            char pair_command[MAXLINE];
            sprintf(pair_command,"set type %i charge %s",type_a+1,the_rest);
            input->one(pair_command);
          }
        }
      }
      else{
      words = utils::split_words(line);
      if (key == "pair") {
        for (int ia = 0; ia < atom->ntypes; ++ia) {
          for (int ib = ia; ib < atom->ntypes; ++ib)
            if (((species[ia] == words[0]) && (species[ib] == words[1]))
                || ((species[ib] == words[0]) && (species[ia] == words[1])))
              input->one(fmt::format("pair_coeff {} {} {}",ia+1,ib+1,words[2]));
        }
      } else if (key == "charge") {
        for (int ia = 0; ia < atom->ntypes; ++ia)
          if (species[ia] == words[0])
            input->one(fmt::format("set type {} charge {}",ia+1,words[1]));
      } else {
        error->one(FLERR,"Unrecognized KEY for KIM_SET_TYPE_PARAMETERS command");
      }
    }
Loading