Unverified Commit b29b3d52 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

initial implementation of automated unit conversion.

this includes a tester program and implementation into pair style tersoff
parent f5a31fef
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ PairTersoff::PairTersoff(LAMMPS *lmp) : Pair(lmp)
  restartinfo = 0;
  one_coeff = 1;
  manybody_flag = 1;
  unit_convert_flag = utils::get_supported_conversions(utils::ENERGY);

  nelements = 0;
  elements = NULL;
@@ -400,9 +401,17 @@ void PairTersoff::read_file(char *file)
  // open file on proc 0

  if (comm->me == 0) {
    PotentialFileReader reader(lmp, file, "Tersoff");
    PotentialFileReader reader(lmp, file, "Tersoff", unit_convert_flag);
    char *line;

    // transparently convert units for supported conversions

    int unit_convert = reader.get_unit_convert();
    double conversion_factor = utils::get_conversion_factor(utils::ENERGY,
                                                            unit_convert);
    printf("unit_conver=%d\n",unit_convert);
    printf("conversion_factor=%g\n",conversion_factor);

    while((line = reader.next_line(NPARAMS_PER_LINE))) {
      try {
        ValueTokenizer values(line);
@@ -453,6 +462,11 @@ void PairTersoff::read_file(char *file)
        params[nparams].lam1      = values.next_double();
        params[nparams].biga      = values.next_double();
        params[nparams].powermint = int(params[nparams].powerm);

        if (unit_convert) {
          params[nparams].biga *= conversion_factor;
          params[nparams].bigb *= conversion_factor;
        }
      } catch (TokenizerException & e) {
        error->one(FLERR, e.what());
      }
+1 −0
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ Pair::Pair(LAMMPS *lmp) : Pointers(lmp)
  no_virial_fdotr_compute = 0;
  writedata = 0;
  ghostneigh = 0;
  unit_convert_flag = utils::NOCONVERT;

  nextra = 0;
  pvector = NULL;
+1 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ class Pair : protected Pointers {
  int respa_enable;              // 1 if inner/middle/outer rRESPA routines
  int one_coeff;                 // 1 if allows only one coeff * * call
  int manybody_flag;             // 1 if a manybody potential
  int unit_convert_flag;         // value != 0 indicates support for unit conversion.
  int no_virial_fdotr_compute;   // 1 if does not invoke virial_fdotr_compute()
  int writedata;                 // 1 if writes coeffs to data file
  int ghostneigh;                // 1 if pair style needs neighbors of ghosts
+19 −5
Original line number Diff line number Diff line
@@ -31,11 +31,13 @@ using namespace LAMMPS_NS;

PotentialFileReader::PotentialFileReader(LAMMPS *lmp,
                                         const std::string &filename,
                                         const std::string &potential_name) :
                                         const std::string &potential_name,
                                         const int auto_convert) :
  Pointers(lmp),
  reader(nullptr),
  filename(filename),
  filetype(potential_name + " potential")
  filetype(potential_name + " potential"),
  unit_convert(auto_convert)
{
  if (comm->me != 0) {
    error->one(FLERR, "FileReader should only be called by proc 0!");
@@ -155,10 +157,22 @@ TextFileReader * PotentialFileReader::open_potential(const std::string& path) {
      utils::logmesg(lmp, fmt::format("Reading potential file {} with DATE: {}\n", filename, date));
    }

    if (!units.empty() && (units != unit_style)) {
    if (units.empty()) {
      unit_convert == utils::NOCONVERT;
    } else {
      if (units == unit_style) {
        unit_convert = utils::NOCONVERT;
      } else {
        if ((units == "metal") && (unit_style == "real") && (unit_convert | utils::METAL2REAL)) {
          unit_convert = utils::METAL2REAL;
        } else if ((units == "real") && (unit_style == "metal") && (unit_convert | utils::REAL2METAL)) {
          unit_convert = utils::REAL2METAL;
        } else {
          lmp->error->one(FLERR, fmt::format("Potential file {} requires {} units "
                                             "but {} units are in use",filename, units, unit_style));
        }
      }
    }

    return new TextFileReader(filepath, filetype);
  }
+12 −6
Original line number Diff line number Diff line
@@ -31,11 +31,14 @@ namespace LAMMPS_NS
    TextFileReader *reader;
    std::string filename;
    std::string filetype;
    int unit_convert;

    TextFileReader *open_potential(const std::string& path);

  public:
    PotentialFileReader(class LAMMPS *lmp, const std::string &filename, const std::string &potential_name);
    PotentialFileReader(class LAMMPS *lmp, const std::string &filename,
                        const std::string &potential_name,
                        const int auto_convert = 0);
    virtual ~PotentialFileReader();

    void ignore_comments(bool value);
@@ -51,6 +54,9 @@ namespace LAMMPS_NS
    tagint next_tagint();
    bigint next_bigint();
    std::string next_string();

    // unit conversion info
    int get_unit_convert() const { return unit_convert; }
  };

} // namespace LAMMPS_NS
Loading