Unverified Commit 96a6afd4 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer Committed by GitHub
Browse files

Merge pull request #2148 from akohlmey/kim-echo-log-refactor

KIM package echo/log refactor and bugfix
parents 765b547a 505cc103
Loading
Loading
Loading
Loading
+24 −68
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@
#include "variable.h"
#include "citeme.h"
#include "utils.h"
#include "fmt/format.h"

extern "C" {
#include "KIM_SimulatorHeaders.h"
@@ -303,7 +304,7 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
  fix_store->setptr("model_units", (void *) model_units);

  // Begin output to log file
  kim_init_log_delimiter("begin");
  input->write_echo("#=== BEGIN kim-init ==========================================\n");

  int kimerror;
  KIM_SimulatorModel * simulatorModel;
@@ -423,37 +424,14 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM
        mesg += "\n";
      }
    }
    else
      mesg += "No mutable parameters. \n";
    else mesg += "No mutable parameters. \n";

    KIM_Model_Destroy(&pkim);

    if (comm->me == 0)
    {
      input->write_echo(mesg.c_str());
    }
    input->write_echo(mesg);
  }

  // End output to log file
  kim_init_log_delimiter("end");

}

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

void KimInit::kim_init_log_delimiter(std::string const &begin_end) const
{
  if (comm->me == 0) {
    std::string mesg;
    if (begin_end == "begin")
      mesg =
          "#=== BEGIN kim-init ==========================================\n";
    else if (begin_end == "end")
      mesg =
          "#=== END kim-init ============================================\n\n";

    input->write_echo(mesg.c_str());
  }
  input->write_echo("#=== END kim-init ============================================\n\n");
}

/* ---------------------------------------------------------------------- */
@@ -493,14 +471,8 @@ void KimInit::do_variables(char *user_units, char *model_units)
                         (char *)"efield",
                         (char *)"density"};

  if (comm->me == 0) {
    std::string mesg("# Conversion factors from ");
    mesg += from;
    mesg += " to ";
    mesg += to;
    mesg += ":\n";
    input->write_echo(mesg.c_str());
  }
  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]);
@@ -514,58 +486,43 @@ void KimInit::do_variables(char *user_units, char *model_units)
                                 from,
                                 to,
                                 conversion_factor);
    if (ier != 0) {
      std::string err = std::string("Unable to obtain conversion factor: ") +
                        "unit = " + units[i] + "; "
                        "from = " + from + "; "
                        "to = " + to + ".";
      error->all(FLERR,err);
    }
    if (ier != 0)
      error->all(FLERR,fmt::format("Unable to obtain conversion factor: "
                                   "unit = {}; from = {}; to = {}.",
                                   units[i], from, to));

    variable->internal_set(v_unit,conversion_factor);
    if (comm->me == 0) {
      std::stringstream mesg;
      mesg << "variable " << std::setw(15) << std::left << var_str
           << " internal "
           << std::setprecision(12) << std::scientific << conversion_factor
           << std::endl;
      input->write_echo(mesg.str().c_str());
    }
    input->write_echo(fmt::format("variable {:<15s} internal {:<15.12e}\n",
                                  var_str, conversion_factor));
  }
  if (comm->me == 0) input->write_echo("#\n");
  input->write_echo("#\n");
}

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

void KimInit::write_log_cite(char * model_name)
void KimInit::write_log_cite(const std::string &model_name)
{
  KIM_Collections * coll;
  int err = KIM_Collections_Create(&coll);
  if (err) return;

  int extent;
  if (model_type == MO)
  {
  if (model_type == MO) {
    err = KIM_Collections_CacheListOfItemMetadataFiles(
        coll,KIM_COLLECTION_ITEM_TYPE_portableModel,model_name,&extent);
  }
  else if (model_type == SM)
  {
      coll,KIM_COLLECTION_ITEM_TYPE_portableModel,model_name.c_str(),&extent);
  } else if (model_type == SM) {
    err = KIM_Collections_CacheListOfItemMetadataFiles(
        coll,KIM_COLLECTION_ITEM_TYPE_simulatorModel,model_name,&extent);
  }
  else
  {
      coll,KIM_COLLECTION_ITEM_TYPE_simulatorModel,model_name.c_str(),&extent);
  } else {
    error->all(FLERR,"Unknown model type.");
  }

  if (err)
  {
  if (err) {
    KIM_Collections_Destroy(&coll);
    return;
  }

  for (int i = 0; i < extent;++i)
  {
  for (int i = 0; i < extent;++i) {
    char const * fileName;
    int availableAsString;
    char const * fileString;
@@ -573,8 +530,7 @@ void KimInit::write_log_cite(char * model_name)
        coll,i,&fileName,NULL,NULL,&availableAsString,&fileString);
    if (err) continue;

    if (0 == strncmp("kimcite",fileName,7))
    {
    if (0 == strncmp("kimcite",fileName,7)) {
      if ((lmp->citeme) && (availableAsString)) lmp->citeme->add(fileString);
    }
  }
+1 −2
Original line number Diff line number Diff line
@@ -83,10 +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(char *);
  void write_log_cite(const std::string &);
  void do_init(char *, char *, char *, KIM_Model *&);
  void do_variables(char*, char*);
  void kim_init_log_delimiter(std::string const &begin_end) const;
};

}
+4 −23
Original line number Diff line number Diff line
@@ -99,24 +99,6 @@ void KimInteractions::command(int narg, char **arg)

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

void KimInteractions::kim_interactions_log_delimiter(
    std::string const begin_end) const
{
  if (comm->me == 0) {
    std::string mesg;
    if (begin_end == "begin")
      mesg =
          "#=== BEGIN kim_interactions ==================================\n";
    else if (begin_end == "end")
      mesg =
          "#=== END kim_interactions ====================================\n\n";

    input->write_echo(mesg.c_str());
  }
}

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

void KimInteractions::do_setup(int narg, char **arg)
{
  bool fixed_types;
@@ -145,7 +127,7 @@ void KimInteractions::do_setup(int narg, char **arg)
  } else error->all(FLERR,"Must use 'kim_init' before 'kim_interactions'");

  // Begin output to log file
  kim_interactions_log_delimiter("begin");
  input->write_echo("#=== BEGIN kim_interactions ==================================\n");

  if (simulatorModel) {

@@ -167,7 +149,7 @@ void KimInteractions::do_setup(int narg, char **arg)
          simulatorModel,"atom-type-num-list",atom_type_num_list.c_str());
      KIM_SimulatorModel_CloseTemplateMap(simulatorModel);

      int len = strlen(atom_type_sym_list.c_str())+1;
      int len = atom_type_sym_list.size()+1;
      char *strbuf = new char[len];
      char *strword;

@@ -281,8 +263,7 @@ void KimInteractions::do_setup(int narg, char **arg)
  }

  // End output to log file
  kim_interactions_log_delimiter("end");

  input->write_echo("#=== END kim_interactions ====================================\n\n");
}

/* ---------------------------------------------------------------------- */
@@ -377,7 +358,7 @@ void KimInteractions::KIM_SET_TYPE_PARAMETERS(char const *const input_line) cons

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

int KimInteractions::species_to_atomic_no(std::string const species) const
int KimInteractions::species_to_atomic_no(const std::string &species) const
{
  if (species == "H") return 1;
  else if (species == "He") return 2;
+1 −2
Original line number Diff line number Diff line
@@ -76,9 +76,8 @@ class KimInteractions : protected Pointers {
  void command(int, char **);
 private:
  void do_setup(int, char **);
  int species_to_atomic_no(std::string const species) const;
  int species_to_atomic_no(const std::string &species) const;
  void KIM_SET_TYPE_PARAMETERS(char const *const input_line) const;
  void kim_interactions_log_delimiter(std::string const begin_end) const;
};

}
+16 −36
Original line number Diff line number Diff line
@@ -56,6 +56,8 @@
------------------------------------------------------------------------- */

#include "kim_param.h"
#include "fix_store_kim.h"
#include "pair_kim.h"
#include <mpi.h>
#include <cstring>
#include <string>
@@ -66,8 +68,7 @@
#include "modify.h"
#include "variable.h"
#include "force.h"
#include "fix_store_kim.h"
#include "pair_kim.h"
#include "fmt/format.h"

extern "C"
{
@@ -157,10 +158,9 @@ void KimParam::command(int narg, char **arg)
  if (narg < 4)
    error->all(FLERR, "Illegal kim_param command");

  kim_param_get = (strcmp(arg[0], "get") == 0);
  kim_param_set = (strcmp(arg[0], "set") == 0);
  std::string kim_param_get_set = arg[0];

  if (!kim_param_get && !kim_param_set) {
  if ((kim_param_get_set != "get") && (kim_param_get_set != "set")) {
    std::string msg("Incorrect arguments in kim_param command.\n");
    msg += "'kim_param get/set' is mandatory.";
    error->all(FLERR, msg);
@@ -193,7 +193,8 @@ void KimParam::command(int narg, char **arg)
  else
    error->all(FLERR, "Must use 'kim_init' before 'kim_param'");

  kim_param_log_delimiter("begin");
  input->write_echo(fmt::format("#=== BEGIN kim-param {} ==================="
                                "==================\n",kim_param_get_set));

  KIM_Model *pkim = NULL;

@@ -211,7 +212,7 @@ void KimParam::command(int narg, char **arg)
      if (!pkim)
        error->all(FLERR, "Unable to get the KIM Portable Model.");

      if (kim_param_set) {
      if (kim_param_get_set == "set") {
        atom_type_list = pairKIM->get_atom_type_list();
        if (atom_type_list.empty())
          error->all(FLERR, "The requested atom type list is empty.");
@@ -220,7 +221,7 @@ void KimParam::command(int narg, char **arg)
      error->all(FLERR, "Pair style is defined,"
                        " but there is no match for kim style in lammps.");
  } else {
    if (kim_param_set) {
    if (kim_param_get_set == "set") {
      std::string msg("Wrong kim_param set command.\n");
      msg += "To set the new parameter values, pair style must be assigned.\n";
      msg += "Must use 'kim_interactions' or";
@@ -259,7 +260,7 @@ void KimParam::command(int narg, char **arg)
  KIM_Model_GetNumberOfParameters(pkim, &numberOfParameters);
  if (numberOfParameters) {
    // Get the parameters
    if (kim_param_get) {
    if (kim_param_get_set == "get") {
      // Parameter name
      char *paramname = NULL;
      // Variable name
@@ -532,38 +533,17 @@ void KimParam::command(int narg, char **arg)
  if (!isPairStyleAssigned)
    KIM_Model_Destroy(&pkim);

  kim_param_log_delimiter("end");
}

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

void KimParam::kim_param_log_delimiter(std::string const &begin_end) const
{
  if (comm->me == 0) {
    std::string msg;
    if (begin_end == "begin") {
      msg = "#=== BEGIN kim-param ";
      msg += kim_param_get ? "get " : "set ";
      msg += "=====================================\n";
    } else if (begin_end == "end") {
      msg = "#=== END kim-param ";
      msg += kim_param_get ? "get " : "set ";
      msg += "=======================================\n\n";
    }
    input->write_echo(msg.c_str());
  }
  input->write_echo(fmt::format("#=== END kim-param {} ====================="
                                "==================\n",kim_param_get_set));
}

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

void KimParam::echo_var_assign(std::string const &name,
                               std::string const &value) const
void KimParam::echo_var_assign(const std::string &name,
                               const std::string &value) const
{
  if (comm->me == 0) {
    std::string msg;
    msg += "variable " + name + " string " + value + "\n";
    input->write_echo(msg.c_str());
  }
  input->write_echo(fmt::format("variable {} string {}\n",
                                name, value));
}

#undef SNUM
Loading