Use factory for kspace style creation

This commit is contained in:
Richard Berger 2016-09-07 22:51:50 -04:00
parent 0745a9f33f
commit 1d03913aa3
3 changed files with 43 additions and 29 deletions

View File

@ -122,6 +122,15 @@ Force::Force(LAMMPS *lmp) : Pointers(lmp)
#include "style_improper.h"
#undef ImproperStyle
#undef IMPROPER_CLASS
kspace_map = new KSpaceCreatorMap();
#define KSPACE_CLASS
#define KSpaceStyle(key,Class) \
(*kspace_map)[#key] = &kspace_creator<Class>;
#include "style_kspace.h"
#undef KSpaceStyle
#undef KSPACE_CLASS
}
/* ---------------------------------------------------------------------- */
@ -645,46 +654,44 @@ KSpace *Force::new_kspace(int narg, char **arg, int trysuffix, int &sflag)
sflag = 1;
char estyle[256];
sprintf(estyle,"%s/%s",arg[0],lmp->suffix);
if (0) return NULL;
#define KSPACE_CLASS
#define KSpaceStyle(key,Class) \
else if (strcmp(estyle,#key) == 0) return new Class(lmp,narg-1,&arg[1]);
#include "style_kspace.h"
#undef KSpaceStyle
#undef KSPACE_CLASS
if (kspace_map->find(estyle) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[estyle];
return kspace_creator(lmp, narg-1, &arg[1]);
}
}
if (lmp->suffix2) {
sflag = 1;
char estyle[256];
sprintf(estyle,"%s/%s",arg[0],lmp->suffix2);
if (0) return NULL;
#define KSPACE_CLASS
#define KSpaceStyle(key,Class) \
else if (strcmp(estyle,#key) == 0) return new Class(lmp,narg-1,&arg[1]);
#include "style_kspace.h"
#undef KSpaceStyle
#undef KSPACE_CLASS
if (kspace_map->find(estyle) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[estyle];
return kspace_creator(lmp, narg-1, &arg[1]);
}
}
}
sflag = 0;
if (strcmp(arg[0],"none") == 0) return NULL;
if (kspace_map->find(arg[0]) != kspace_map->end()) {
KSpaceCreator kspace_creator = (*kspace_map)[arg[0]];
return kspace_creator(lmp, narg-1, &arg[1]);
}
#define KSPACE_CLASS
#define KSpaceStyle(key,Class) \
else if (strcmp(arg[0],#key) == 0) return new Class(lmp,narg-1,&arg[1]);
#include "style_kspace.h"
#undef KSPACE_CLASS
else error->all(FLERR,"Unknown kspace style");
error->all(FLERR,"Unknown kspace style");
return NULL;
}
/* ----------------------------------------------------------------------
one instance per kspace style in style_kspace.h
------------------------------------------------------------------------- */
template <typename T>
KSpace *Force::kspace_creator(LAMMPS *lmp, int narg, char ** arg)
{
return new T(lmp, narg, arg);
}
/* ----------------------------------------------------------------------
return ptr to Kspace class if matches word
if exact, then style name must be exact match to word

View File

@ -82,6 +82,11 @@ class Force : protected Pointers {
class KSpace *kspace;
char *kspace_style;
typedef KSpace *(*KSpaceCreator)(LAMMPS *,int,char**);
typedef std::map<std::string,KSpaceCreator> KSpaceCreatorMap;
KSpaceCreatorMap *kspace_map;
// index [0] is not used in these arrays
double special_lj[4]; // 1-2, 1-3, 1-4 prefactors for LJ
double special_coul[4]; // 1-2, 1-3, 1-4 prefactors for Coulombics
@ -142,6 +147,7 @@ class Force : protected Pointers {
template <typename T> static Angle *angle_creator(LAMMPS *);
template <typename T> static Dihedral *dihedral_creator(LAMMPS *);
template <typename T> static Improper *improper_creator(LAMMPS *);
template <typename T> static KSpace *kspace_creator(LAMMPS *, int, char **);
};
}

View File

@ -677,10 +677,11 @@ void Info::kspace_styles(FILE * out)
fprintf(out, "\nKSpace styles:\n");
vector<string> styles;
#define KSPACE_CLASS
#define KSpaceStyle(key,Class) styles.push_back(#key);
#include "style_kspace.h"
#undef KSPACE_CLASS
for(Force::KSpaceCreatorMap::iterator it = force->kspace_map->begin(); it != force->kspace_map->end(); ++it) {
styles.push_back(it->first);
}
print_columns(out, styles);
fprintf(out, "\n\n\n");
}