Commit 906c5022 authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for dihedral style creation

parent 35bdeb63
Loading
Loading
Loading
Loading
+33 −26
Original line number Diff line number Diff line
@@ -104,6 +104,15 @@ Force::Force(LAMMPS *lmp) : Pointers(lmp)
#include "style_angle.h"
#undef AngleStyle
#undef ANGLE_CLASS

  dihedral_map = new DihedralCreatorMap();

#define DIHEDRAL_CLASS
#define DihedralStyle(key,Class) \
  (*dihedral_map)[#key] = &dihedral_creator<Class>;
#include "style_dihedral.h"
#undef DihedralStyle
#undef DIHEDRAL_CLASS
}

/* ---------------------------------------------------------------------- */
@@ -134,6 +143,7 @@ Force::~Force()
  delete pair_map;
  delete bond_map;
  delete angle_map;
  delete dihedral_map;
}

/* ---------------------------------------------------------------------- */
@@ -465,47 +475,44 @@ Dihedral *Force::new_dihedral(const char *style, int trysuffix, int &sflag)
      sflag = 1;
      char estyle[256];
      sprintf(estyle,"%s/%s",style,lmp->suffix);

      if (0) return NULL;

#define DIHEDRAL_CLASS
#define DihedralStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_dihedral.h"
#undef DihedralStyle
#undef DIHEDRAL_CLASS
      if (dihedral_map->find(estyle) != dihedral_map->end()) {
        DihedralCreator dihedral_creator = (*dihedral_map)[estyle];
        return dihedral_creator(lmp);
      }
    }

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

      if (0) return NULL;

#define DIHEDRAL_CLASS
#define DihedralStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_dihedral.h"
#undef DihedralStyle
#undef DIHEDRAL_CLASS
      if (dihedral_map->find(estyle) != dihedral_map->end()) {
        DihedralCreator dihedral_creator = (*dihedral_map)[estyle];
        return dihedral_creator(lmp);
      }
    }
  }

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

#define DIHEDRAL_CLASS
#define DihedralStyle(key,Class) \
  else if (strcmp(style,#key) == 0) return new Class(lmp);
#include "style_dihedral.h"
#undef DihedralStyle
#undef DIHEDRAL_CLASS

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

/* ----------------------------------------------------------------------
   one instance per dihedral style in style_dihedral.h
------------------------------------------------------------------------- */

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

/* ----------------------------------------------------------------------
   return ptr to current angle class or hybrid sub-class if matches style
------------------------------------------------------------------------- */
+5 −0
Original line number Diff line number Diff line
@@ -69,6 +69,10 @@ class Force : protected Pointers {
  class Dihedral *dihedral;
  char *dihedral_style;

  typedef Dihedral *(*DihedralCreator)(LAMMPS *);
  typedef std::map<std::string,DihedralCreator> DihedralCreatorMap;
  DihedralCreatorMap *dihedral_map;

  class Improper *improper;
  char *improper_style;

@@ -132,6 +136,7 @@ class Force : protected Pointers {
  template <typename T> static Pair *pair_creator(LAMMPS *);
  template <typename T> static Bond *bond_creator(LAMMPS *);
  template <typename T> static Angle *angle_creator(LAMMPS *);
  template <typename T> static Dihedral *dihedral_creator(LAMMPS *);
};

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

  vector<string> styles;
#define DIHEDRAL_CLASS
#define DihedralStyle(key,Class) styles.push_back(#key);
#include "style_dihedral.h"
#undef DIHEDRAL_CLASS

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

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