1 /* ----------------------------------------------------------------------
2     This is the
3 
4     ██╗     ██╗ ██████╗  ██████╗  ██████╗ ██╗  ██╗████████╗███████╗
5     ██║     ██║██╔════╝ ██╔════╝ ██╔════╝ ██║  ██║╚══██╔══╝██╔════╝
6     ██║     ██║██║  ███╗██║  ███╗██║  ███╗███████║   ██║   ███████╗
7     ██║     ██║██║   ██║██║   ██║██║   ██║██╔══██║   ██║   ╚════██║
8     ███████╗██║╚██████╔╝╚██████╔╝╚██████╔╝██║  ██║   ██║   ███████║
9     ╚══════╝╚═╝ ╚═════╝  ╚═════╝  ╚═════╝ ╚═╝  ╚═╝   ╚═╝   ╚══════╝®
10 
11     DEM simulation engine, released by
12     DCS Computing Gmbh, Linz, Austria
13     http://www.dcs-computing.com, office@dcs-computing.com
14 
15     LIGGGHTS® is part of CFDEM®project:
16     http://www.liggghts.com | http://www.cfdem.com
17 
18     Core developer and main author:
19     Christoph Kloss, christoph.kloss@dcs-computing.com
20 
21     LIGGGHTS® is open-source, distributed under the terms of the GNU Public
22     License, version 2 or later. It is distributed in the hope that it will
23     be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. You should have
25     received a copy of the GNU General Public License along with LIGGGHTS®.
26     If not, see http://www.gnu.org/licenses . See also top-level README
27     and LICENSE files.
28 
29     LIGGGHTS® and CFDEM® are registered trade marks of DCS Computing GmbH,
30     the producer of the LIGGGHTS® software and the CFDEM®coupling software
31     See http://www.cfdem.com/terms-trademark-policy for details.
32 
33 -------------------------------------------------------------------------
34     Contributing author and copyright for this file:
35     (if not contributing author is listed, this file has been contributed
36     by the core developer)
37 
38     Christoph Kloss (DCS Computing GmbH, Linz)
39     Christoph Kloss (JKU Linz)
40     Richard Berger (JKU Linz)
41 
42     Copyright 2012-     DCS Computing GmbH, Linz
43     Copyright 2009-2012 JKU Linz
44 ------------------------------------------------------------------------- */
45 
46 #include "pointers.h"
47 #include "lammps.h"
48 #include "fix_property_global.h"
49 #include <map>
50 #include <set>
51 #include <string>
52 #include "properties.h"
53 #include "error.h"
54 #include "modify.h"
55 #include "property_registry.h"
56 
57 using namespace std;
58 using namespace LAMMPS_NS;
59 
PropertyRegistry(LAMMPS * lmp)60 PropertyRegistry::PropertyRegistry(LAMMPS* lmp) : Pointers(lmp), properties(lmp)
61 {
62 }
63 
~PropertyRegistry()64 PropertyRegistry::~PropertyRegistry()
65 {
66   init();
67 }
68 
max_type()69 int PropertyRegistry::max_type()
70 {
71   return properties.max_type();
72 }
73 
min_radius()74 double PropertyRegistry::min_radius()
75 {
76   return properties.min_radius();
77 }
78 
max_radius()79 double PropertyRegistry::max_radius()
80 {
81   return properties.max_radius();
82 }
83 
getLAMMPS()84 LAMMPS * PropertyRegistry::getLAMMPS()
85 {
86   return lmp;
87 }
88 
getGlobalProperty(const char * varname,const char * style,const char * svmstyle,int len1,int len2,const char * caller)89 FixPropertyGlobal* PropertyRegistry::getGlobalProperty(const char *varname, const char *style, const char *svmstyle, int len1, int len2, const char *caller)
90 {
91   return static_cast<FixPropertyGlobal*>(modify->find_fix_property(varname, style, svmstyle, len1, len2, caller));
92 }
93 
getScalarProperty(string varname,const char * caller)94 ScalarProperty * PropertyRegistry::getScalarProperty(string varname,const char *caller)
95 {
96   if(scalars.find(varname) == scalars.end()) {
97     if(scalar_creators.find(varname) != scalar_creators.end()) {
98       scalars[varname] = (*scalar_creators[varname])(*this, caller, use_sanity_checks[varname]);
99     } else {
100       error->message(FLERR, "unknown scalar property");
101     }
102   }
103   return scalars[varname];
104 }
105 
getVectorProperty(string varname,const char * caller)106 VectorProperty * PropertyRegistry::getVectorProperty(string varname,const char *caller)
107 {
108   if(vectors.find(varname) == vectors.end()) {
109     if(vector_creators.find(varname) != vector_creators.end()) {
110       vectors[varname] = (*vector_creators[varname])(*this, caller, use_sanity_checks[varname]);
111     } else {
112       error->message(FLERR, "unknown vector property");
113     }
114   }
115   return vectors[varname];
116 }
117 
getMatrixProperty(string varname,const char * caller)118 MatrixProperty * PropertyRegistry::getMatrixProperty(string varname,const char *caller)
119 {
120   if(matrices.find(varname) == matrices.end()) {
121     if(matrix_creators.find(varname) != matrix_creators.end()) {
122       matrices[varname] = (*matrix_creators[varname])(*this, caller, use_sanity_checks[varname]);
123     } else {
124       error->message(FLERR, "unknown matrix property");
125     }
126   }
127   return matrices[varname];
128 }
129 
registerProperty(string varname,ScalarPropertyCreator creator,bool sanity_checks)130 void PropertyRegistry::registerProperty(string varname, ScalarPropertyCreator creator, bool sanity_checks)
131 {
132   if(scalar_creators.find(varname) == scalar_creators.end()) {
133     scalar_creators[varname] = creator;
134     use_sanity_checks[varname] = sanity_checks;
135   } else if(scalar_creators[varname] != creator) {
136     error->message(FLERR, "property with the same name, but different implementation registered");
137   }
138 }
139 
registerProperty(string varname,VectorPropertyCreator creator,bool sanity_checks)140 void PropertyRegistry::registerProperty(string varname, VectorPropertyCreator creator, bool sanity_checks)
141 {
142   if(vector_creators.find(varname) == vector_creators.end()) {
143     vector_creators[varname] = creator;
144     use_sanity_checks[varname] = sanity_checks;
145   } else if(vector_creators[varname] != creator) {
146     error->message(FLERR, "property with the same name, but different implementation registered");
147   }
148 }
149 
registerProperty(string varname,MatrixPropertyCreator creator,bool sanity_checks)150 void PropertyRegistry::registerProperty(string varname, MatrixPropertyCreator creator, bool sanity_checks)
151 {
152   if(matrix_creators.find(varname) == matrix_creators.end()) {
153     matrix_creators[varname] = creator;
154     use_sanity_checks[varname] = sanity_checks;
155   } else if(matrix_creators[varname] != creator) {
156     error->message(FLERR, "property with the same name, but different implementation registered");
157   }
158 }
159 
connect(string varname,double ** & variable,const char * caller)160 void PropertyRegistry::connect(string varname, double ** & variable, const char *caller)
161 {
162   if(matrices.find(varname) == matrices.end()) {
163     if(matrix_creators.find(varname) != matrix_creators.end()) {
164       matrices[varname] = (*matrix_creators[varname])(*this, caller, use_sanity_checks[varname]);
165     } else {
166       // ERROR unknown property
167       error->message(FLERR, "unknown matrix property");
168     }
169   }
170   matrices[varname]->connect(variable);
171 }
172 
connect(string varname,double * & variable,const char * caller)173 void PropertyRegistry::connect(string varname, double * & variable, const char *caller)
174 {
175   if(vectors.find(varname) == vectors.end()) {
176     if(vector_creators.find(varname) != vector_creators.end()) {
177       vectors[varname] = (*vector_creators[varname])(*this, caller, use_sanity_checks[varname]);
178     } else {
179       // ERROR unknown property
180       error->message(FLERR, "unknown vector property");
181     }
182   }
183   vectors[varname]->connect(variable);
184 }
185 
connect(string varname,double & variable,const char * caller)186 void PropertyRegistry::connect(string varname, double & variable, const char *caller)
187 {
188   if(scalars.find(varname) == scalars.end()) {
189     if(scalar_creators.find(varname) != scalar_creators.end()) {
190       scalars[varname] = (*scalar_creators[varname])(*this, caller, use_sanity_checks[varname]);
191     } else {
192       // ERROR unknown property
193       error->message(FLERR, "unknown scalar property");
194     }
195   }
196   scalars[varname]->connect(variable);
197 }
198 
init()199 void PropertyRegistry::init()
200 {
201   for(std::map<string,ScalarProperty*>::iterator it = scalars.begin(); it != scalars.end(); ++it) {
202       delete it->second;
203   }
204   for(std::map<string,VectorProperty*>::iterator it = vectors.begin(); it != vectors.end(); ++it) {
205       delete it->second;
206   }
207   for(std::map<string,MatrixProperty*>::iterator it = matrices.begin(); it != matrices.end(); ++it) {
208       delete it->second;
209   }
210   scalars.clear();
211   vectors.clear();
212   matrices.clear();
213 }
214 
print_all(FILE * out)215 void PropertyRegistry::print_all(FILE * out)
216 {
217   for(std::map<string,ScalarProperty*>::iterator it = scalars.begin(); it != scalars.end(); ++it) {
218       fprintf(out, " %s = ", it->first.c_str());
219       it->second->print_value(out);
220       fprintf(out, "\n");
221   }
222   for(std::map<string,VectorProperty*>::iterator it = vectors.begin(); it != vectors.end(); ++it) {
223       fprintf(out, " %s = ", it->first.c_str());
224       it->second->print_value(out);
225       fprintf(out, "\n");
226   }
227   for(std::map<string,MatrixProperty*>::iterator it = matrices.begin(); it != matrices.end(); ++it) {
228       fprintf(out, " %s = ", it->first.c_str());
229       it->second->print_value(out);
230       fprintf(out, "\n");
231   }
232 }
233