Commit 9952d8a2 authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for minimize style creation

parent 85c13294
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -596,10 +596,11 @@ void Info::minimize_styles(FILE * out)
  fprintf(out, "\nMinimize styles:\n");

  vector<string> styles;
#define MINIMIZE_CLASS
#define MinimizeStyle(key,Class) styles.push_back(#key);
#include "style_minimize.h"
#undef MINIMIZE_CLASS

  for(Update::MinimizeCreatorMap::iterator it = update->minimize_map->begin(); it != update->minimize_map->end(); ++it) {
    styles.push_back(it->first);
  }

  print_columns(out, styles);
  fprintf(out, "\n\n\n");
}
+24 −8
Original line number Diff line number Diff line
@@ -70,6 +70,15 @@ Update::Update(LAMMPS *lmp) : Pointers(lmp)
#undef IntegrateStyle
#undef INTEGRATE_CLASS

  minimize_map = new MinimizeCreatorMap();

#define MINIMIZE_CLASS
#define MinimizeStyle(key,Class) \
  (*minimize_map)[#key] = &minimize_creator<Class>;
#include "style_minimize.h"
#undef MinimizeStyle
#undef MINIMIZE_CLASS

  str = (char *) "verlet";
  create_integrate(1,&str,1);

@@ -90,6 +99,7 @@ Update::~Update()
  delete minimize;

  delete integrate_map;
  delete minimize_map;
}

/* ---------------------------------------------------------------------- */
@@ -378,14 +388,10 @@ void Update::create_minimize(int narg, char **arg)
  delete [] minimize_style;
  delete minimize;

  if (0) return;      // dummy line to enable else-if macro expansion

#define MINIMIZE_CLASS
#define MinimizeStyle(key,Class) \
  else if (strcmp(arg[0],#key) == 0) minimize = new Class(lmp);
#include "style_minimize.h"
#undef MINIMIZE_CLASS

  if (minimize_map->find(arg[0]) != minimize_map->end()) {
    MinimizeCreator minimize_creator = (*minimize_map)[arg[0]];
    minimize = minimize_creator(lmp);
  }
  else error->all(FLERR,"Illegal min_style command");

  int n = strlen(arg[0]) + 1;
@@ -393,6 +399,16 @@ void Update::create_minimize(int narg, char **arg)
  strcpy(minimize_style,arg[0]);
}

/* ----------------------------------------------------------------------
   one instance per minimize style in style_minimize.h
------------------------------------------------------------------------- */

template <typename T>
Min *Update::minimize_creator(LAMMPS *lmp)
{
  return new T(lmp);
}

/* ----------------------------------------------------------------------
   reset timestep as called from input script
------------------------------------------------------------------------- */
+6 −0
Original line number Diff line number Diff line
@@ -49,8 +49,13 @@ class Update : protected Pointers {
  char *minimize_style;

  typedef Integrate *(*IntegrateCreator)(LAMMPS *,int,char**);
  typedef Min *(*MinimizeCreator)(LAMMPS *);

  typedef std::map<std::string,IntegrateCreator> IntegrateCreatorMap;
  typedef std::map<std::string,MinimizeCreator> MinimizeCreatorMap;

  IntegrateCreatorMap *integrate_map;
  MinimizeCreatorMap *minimize_map;

  Update(class LAMMPS *);
  ~Update();
@@ -67,6 +72,7 @@ class Update : protected Pointers {
  void new_integrate(char *, int, char **, int, int &);

  template <typename T> static Integrate *integrate_creator(LAMMPS *, int, char **);
  template <typename T> static Min *minimize_creator(LAMMPS *);
};

}