Commit 0745a9f3 authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for improper style creation

parent 906c5022
Loading
Loading
Loading
Loading
+33 −25
Original line number Diff line number Diff line
@@ -113,6 +113,15 @@ Force::Force(LAMMPS *lmp) : Pointers(lmp)
#include "style_dihedral.h"
#undef DihedralStyle
#undef DIHEDRAL_CLASS

  improper_map = new ImproperCreatorMap();

#define IMPROPER_CLASS
#define ImproperStyle(key,Class) \
  (*improper_map)[#key] = &improper_creator<Class>;
#include "style_improper.h"
#undef ImproperStyle
#undef IMPROPER_CLASS
}

/* ---------------------------------------------------------------------- */
@@ -144,6 +153,7 @@ Force::~Force()
  delete bond_map;
  delete angle_map;
  delete dihedral_map;
  delete improper_map;
}

/* ---------------------------------------------------------------------- */
@@ -553,46 +563,44 @@ Improper *Force::new_improper(const char *style, int trysuffix, int &sflag)
      sflag = 1;
      char estyle[256];
      sprintf(estyle,"%s/%s",style,lmp->suffix);

      if (0) return NULL;

#define IMPROPER_CLASS
#define ImproperStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_improper.h"
#undef ImproperStyle
#undef IMPROPER_CLASS
      if (improper_map->find(estyle) != improper_map->end()) {
        ImproperCreator improper_creator = (*improper_map)[estyle];
        return improper_creator(lmp);
      }
    }

    if (lmp->suffix2) {
      sflag = 2;
      char estyle[256];
      sprintf(estyle,"%s/%s",style,lmp->suffix2);

      if (0) return NULL;

#define IMPROPER_CLASS
#define ImproperStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_improper.h"
#undef ImproperStyle
#undef IMPROPER_CLASS
      if (improper_map->find(estyle) != improper_map->end()) {
        ImproperCreator improper_creator = (*improper_map)[estyle];
        return improper_creator(lmp);
      }
    }
  }

  sflag = 0;
  if (strcmp(style,"none") == 0) return NULL;
  if (improper_map->find(style) != improper_map->end()) {
    ImproperCreator improper_creator = (*improper_map)[style];
    return improper_creator(lmp);
  }

#define IMPROPER_CLASS
#define ImproperStyle(key,Class) \
  else if (strcmp(style,#key) == 0) return new Class(lmp);
#include "style_improper.h"
#undef IMPROPER_CLASS

  else error->all(FLERR,"Unknown improper style");
  error->all(FLERR,"Unknown improper style");
  return NULL;
}

/* ----------------------------------------------------------------------
   one instance per improper style in style_improper.h
------------------------------------------------------------------------- */

template <typename T>
Improper *Force::improper_creator(LAMMPS *lmp)
{
  return new T(lmp);
}

/* ----------------------------------------------------------------------
   return ptr to current improper class or hybrid sub-class if matches style
------------------------------------------------------------------------- */
+5 −0
Original line number Diff line number Diff line
@@ -76,6 +76,10 @@ class Force : protected Pointers {
  class Improper *improper;
  char *improper_style;

  typedef Improper *(*ImproperCreator)(LAMMPS *);
  typedef std::map<std::string,ImproperCreator> ImproperCreatorMap;
  ImproperCreatorMap *improper_map;

  class KSpace *kspace;
  char *kspace_style;
                             // index [0] is not used in these arrays
@@ -137,6 +141,7 @@ class Force : protected Pointers {
  template <typename T> static Bond *bond_creator(LAMMPS *);
  template <typename T> static Angle *angle_creator(LAMMPS *);
  template <typename T> static Dihedral *dihedral_creator(LAMMPS *);
  template <typename T> static Improper *improper_creator(LAMMPS *);
};

}
+5 −4
Original line number Diff line number Diff line
@@ -663,10 +663,11 @@ void Info::improper_styles(FILE * out)
  fprintf(out, "\nImproper styles:\n");

  vector<string> styles;
#define IMPROPER_CLASS
#define ImproperStyle(key,Class) styles.push_back(#key);
#include "style_improper.h"
#undef IMPROPER_CLASS

  for(Force::ImproperCreatorMap::iterator it = force->improper_map->begin(); it != force->improper_map->end(); ++it) {
    styles.push_back(it->first);
  }

  print_columns(out, styles);
  fprintf(out, "\n\n\n");
}