Unverified Commit 05ff3520 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer
Browse files

add utils::open_potential() function to utils namespace

parent 7413dc78
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
@@ -921,6 +921,62 @@ double utils::get_conversion_factor(const int property, const int conversion)
  return 0.0;
}

/* ----------------------------------------------------------------------
   open a potential file as specified by name
   if fails, search in dir specified by env variable LAMMPS_POTENTIALS
------------------------------------------------------------------------- */

FILE *utils::open_potential(const char *name, LAMMPS *lmp, int *auto_convert)
{
  auto error = lmp->error;
  auto me = lmp->comm->me;

  std::string filepath = get_potential_file_path(name);

  if(!filepath.empty()) {
    std::string unit_style = lmp->update->unit_style;
    std::string date       = get_potential_date(filepath, "potential");
    std::string units      = get_potential_units(filepath, "potential");

    if(!date.empty() && (me == 0)) {
      logmesg(lmp, fmt::format("Reading potential file {} "
                               "with DATE: {}\n", name, date));
    }

    if (auto_convert == nullptr) {
      if (!units.empty() && (units != unit_style) && (me == 0)) {
        error->one(FLERR, fmt::format("Potential file {} requires {} units "
                                      "but {} units are in use", name, units,
                                      unit_style));
        return nullptr;
      }
    } else {
      if (units.empty() || units == unit_style) {
        *auto_convert = NOCONVERT;
      } else {
        if ((units == "metal") && (unit_style == "real")
            && (*auto_convert & METAL2REAL)) {
          *auto_convert = METAL2REAL;
        } else if ((units == "real") && (unit_style == "metal")
            && (*auto_convert & REAL2METAL)) {
          *auto_convert = REAL2METAL;
        } else {
          error->one(FLERR, fmt::format("Potential file {} requires {} units "
                                        "but {} units are in use", name,
                                        units, unit_style));
          return nullptr;
        }
      }
      if ((*auto_convert != NOCONVERT) && (me == 0))
        error->warning(FLERR, fmt::format("Converting potential file in "
                                          "{} units to {} units",
                                          units, unit_style));
    }
    return fopen(filepath.c_str(), "r");
  }
  return nullptr;
}

/* ----------------------------------------------------------------------
   convert a timespec ([[HH:]MM:]SS) to seconds
   the strings "off" and "unlimited" result in -1.0;
+18 −0
Original line number Diff line number Diff line
@@ -367,6 +367,24 @@ and *nhi* according to the following five cases:
     */
    double get_conversion_factor(const int property, const int conversion);

    /** Open a potential file as specified by *name*
     *
\verbatim embed:rst
If opening the file fails, it will search for it in the folder(s)
pointed to by the environment variable LAMMPS_POTENTIALS.

If the potential file has a ``UNITS`` tag in the first line, its
value is compared to the current :doc:`unit style <units>` setting.
\endverbatim
     *
     *  \param name          file- or pathname of the potential file
     *  \param lmp           pointer to top-level LAMMPS class instance
     *  \param auto_convert  pointer to automatic unit conversion bitmask
     *  \return              FILE pointer of the opened potential file or NULL
     */
    FILE *open_potential(const char *name, LAMMPS *lmp,
                         int *auto_convert=nullptr);

    /** Convert a time string to seconds
     *
     * The strings "off" and "unlimited" result in -1