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 #ifndef PROPERTY_REGISTRY_H_
47 #define PROPERTY_REGISTRY_H_
48 
49 #include "pointers.h"
50 #include "lammps.h"
51 #include "fix_property_global.h"
52 #include <map>
53 #include <set>
54 #include <string>
55 #include "properties.h"
56 #include "error.h"
57 #include "modify.h"
58 
59 using namespace LAMMPS_NS;
60 
61 class PropertyRegistry;
62 
63 template<typename T>
64 class Property {
65 public:
Property()66   Property() : data(0) {}
~Property()67   virtual ~Property(){}
68 
connect(T & variable)69   void connect(T & variable) {
70     listeners.insert(&variable);
71     variable = data;
72   }
73 
disconnect(T & variable)74   void disconnect(T & variable) {
75     typename std::set<T*>::iterator it = listeners.find(&variable);
76     listeners.erase(it);
77     variable = NULL;
78   }
79 
updateAll()80   void updateAll() {
81     for(typename std::set<T*>::iterator it = listeners.begin(); it != listeners.end(); ++it) {
82       *(*it) = data;
83     }
84   }
85 
86   T data;
87   std::set<T*> listeners;
88 
print_value(FILE * out)89   void print_value(FILE* out) {
90     fprintf(out, "%g", double(data));
91   }
92 };
93 
94 typedef Property<double> ScalarProperty;
95 
96 class VectorProperty : public Property<double*>
97 {
98 public:
99   int cols;
100 
VectorProperty(const int N)101   VectorProperty(const int N) :
102     cols(N)
103   {
104     data = new double[N];
105     for(int col = 0; col < N; col++) {
106       data[col] = 0.0;
107     }
108   }
109 
~VectorProperty()110   virtual ~VectorProperty(){
111     delete [] data;
112   }
113 
print_value(FILE * out)114   void print_value(FILE* out) {
115     fprintf(out, "[");
116     for(int col = 1; col < cols; col++) {
117       fprintf(out, "%g", data[col]);
118       if((col+1) < cols) fprintf(out, " ");
119     }
120     fprintf(out, "]");
121   }
122 };
123 
124 class MatrixProperty : public Property<double**>
125 {
126 public:
127   int rows;
128   int cols;
129 
MatrixProperty(const int N,const int M)130   MatrixProperty(const int N, const int M) :
131     rows(N),
132     cols(M)
133   {
134     double * array = new double[N*M];
135     data = new double*[N];
136 
137     for(int row = 0; row < N; row++) {
138       data[row] = &array[row*M];
139 
140       for(int col = 0; col < M; col++) {
141         data[row][col] = 0.0;
142       }
143     }
144   }
145 
~MatrixProperty()146   virtual ~MatrixProperty() {
147     delete [] data[0];
148     delete [] data;
149   }
150 
print_value(FILE * out)151   void print_value(FILE* out) {
152     if (!out)  // out == NULL
153       return;
154 
155     fprintf(out, "[");
156     for(int row = 1; row < rows; row++) {
157       for(int col = 1; col < cols; col++) {
158         fprintf(out, "%g", data[row][col]);
159         if((col+1) < cols) fprintf(out, " ");
160       }
161       if((row+1) < rows) fprintf(out, "; ");
162     }
163     fprintf(out, "]");
164   }
165 };
166 
167 // -------------------------------------------------------------------
168 
169 typedef ScalarProperty* (*ScalarPropertyCreator)(PropertyRegistry & registry, const char * caller, bool sanity_checks);
170 typedef VectorProperty* (*VectorPropertyCreator)(PropertyRegistry & registry, const char * caller, bool sanity_checks);
171 typedef MatrixProperty* (*MatrixPropertyCreator)(PropertyRegistry & registry, const char * caller, bool sanity_checks);
172 
173 class PropertyRegistry : protected Pointers {
174   Properties properties;
175 
176 public:
177   PropertyRegistry(LAMMPS* lmp);
178   ~PropertyRegistry();
179   int max_type();
180   double min_radius();
181   double max_radius();
182   LAMMPS * getLAMMPS();
183 
184   FixPropertyGlobal* getGlobalProperty(const char *varname, const char *style, const char *svmstyle, int len1, int len2, const char *caller);
185 
186   ScalarProperty * getScalarProperty(std::string varname,const char *caller);
187   VectorProperty * getVectorProperty(std::string varname,const char *caller);
188   MatrixProperty * getMatrixProperty(std::string varname,const char *caller);
189 
190   void registerProperty(std::string varname, ScalarPropertyCreator creator, bool sanity_checks = true);
191   void registerProperty(std::string varname, VectorPropertyCreator creator, bool sanity_checks = true);
192   void registerProperty(std::string varname, MatrixPropertyCreator creator, bool sanity_checks = true);
193 
194   void connect(std::string varname, double ** & variable, const char *caller);
195   void connect(std::string varname, double * & variable, const char *caller);
196   void connect(std::string varname, double & variable, const char *caller);
197 
198   void init();
199 
200   void print_all(FILE* out);
201 
202 private:
203   std::map<std::string, ScalarPropertyCreator> scalar_creators;
204   std::map<std::string, VectorPropertyCreator> vector_creators;
205   std::map<std::string, MatrixPropertyCreator> matrix_creators;
206 
207   std::map<std::string, ScalarProperty*> scalars;
208   std::map<std::string, VectorProperty*> vectors;
209   std::map<std::string, MatrixProperty*> matrices;
210 
211   std::map<std::string, bool> use_sanity_checks;
212 };
213 
214 #endif /* PROPERTY_REGISTRY_H_ */
215