Commit 69c58ef0 authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for bond style creation

parent 95ee6440
Loading
Loading
Loading
Loading
+33 −25
Original line number Diff line number Diff line
@@ -86,6 +86,15 @@ Force::Force(LAMMPS *lmp) : Pointers(lmp)
#include "style_pair.h"
#undef PairStyle
#undef PAIR_CLASS

  bond_map = new BondCreatorMap();

#define BOND_CLASS
#define BondStyle(key,Class) \
  (*bond_map)[#key] = &bond_creator<Class>;
#include "style_bond.h"
#undef BondStyle
#undef BOND_CLASS
}

/* ---------------------------------------------------------------------- */
@@ -114,6 +123,7 @@ Force::~Force()
  kspace = NULL;

  delete pair_map;
  delete bond_map;
}

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

      if (0) return NULL;

#define BOND_CLASS
#define BondStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_bond.h"
#undef BondStyle
#undef BOND_CLASS
      if (bond_map->find(estyle) != bond_map->end()) {
        BondCreator bond_creator = (*bond_map)[estyle];
        return bond_creator(lmp);
      }
    }

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

      if (0) return NULL;

#define BOND_CLASS
#define BondStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_bond.h"
#undef BondStyle
#undef BOND_CLASS
      if (bond_map->find(estyle) != bond_map->end()) {
        BondCreator bond_creator = (*bond_map)[estyle];
        return bond_creator(lmp);
      }
    }
  }

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

#define BOND_CLASS
#define BondStyle(key,Class) \
  else if (strcmp(style,#key) == 0) return new Class(lmp);
#include "style_bond.h"
#undef BOND_CLASS

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

/* ----------------------------------------------------------------------
   one instance per bond style in style_bond.h
------------------------------------------------------------------------- */

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

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

  typedef Bond *(*BondCreator)(LAMMPS *);
  typedef std::map<std::string,BondCreator> BondCreatorMap;
  BondCreatorMap *bond_map;

  class Angle *angle;
  char *angle_style;

@@ -122,6 +126,7 @@ class Force : protected Pointers {

 private:
  template <typename T> static Pair *pair_creator(LAMMPS *);
  template <typename T> static Bond *bond_creator(LAMMPS *);
};

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

  vector<string> styles;
#define BOND_CLASS
#define BondStyle(key,Class) styles.push_back(#key);
#include "style_bond.h"
#undef BOND_CLASS

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

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