FEDEM Solver  R8.0
Source code of the dynamics solver
FFaOperation.H
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2023 SAP SE
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // This file is part of FEDEM - https://openfedem.org
7 
18 #ifndef FFA_OPERATION_H
19 #define FFA_OPERATION_H
20 
21 #include <map>
22 #include <vector>
23 #include <string>
24 
26 #ifdef FT_USE_MEMPOOL
27 #include "FFaLib/FFaPatterns/FFaMemPool.H"
28 #endif
29 
30 
38 {
39 protected:
43  virtual ~FFaOperationBase() {}
44 
45 public:
47  void ref() { myRefCount++; }
49  void unref() { if (--myRefCount <= 0) delete this; }
50 
51 #ifdef FT_USE_MEMPOOL
53  static void freeMemPools() { getMemPoolMgr()->freeMemPools(); }
55  static FFaMemPoolMgr* getMemPoolMgr()
56  {
57  static FFaMemPoolMgr memPools;
58  return &memPools;
59  }
60 #else
62  static void freeMemPools() {}
63 #endif
64 
65 private:
66  short int myRefCount;
67 };
68 
69 
74 template <class RetType>
76 {
77 protected:
79  FFaOperation() : IAmEvaluated(false) {}
81  virtual ~FFaOperation() {}
82 
83 public:
85  bool invoke(RetType& value)
86  {
87  bool ok = true;
88  if (IAmEvaluated)
89  value = myCachedValue;
90  else
91  {
92  ok = this->evaluate(value);
93  myCachedValue = value;
94  IAmEvaluated = true;
95  }
96 
97  return ok;
98  }
99 
101  virtual void invalidate() { IAmEvaluated = false; }
102 
104  virtual bool hasData() const = 0;
106  virtual bool evaluate(RetType& value) = 0;
107 
108 private:
109  RetType myCachedValue;
110 
111 protected:
113 };
114 
116 template <> inline
117 FFaOperation<int>::FFaOperation() : myCachedValue(0), IAmEvaluated(false) {}
118 
119 template <> inline
120 FFaOperation<float>::FFaOperation() : myCachedValue(0), IAmEvaluated(false) {}
121 
122 template <> inline
123 FFaOperation<double>::FFaOperation() : myCachedValue(0), IAmEvaluated(false) {}
125 
126 
133 template <class OperationCB>
135 {
137  typedef std::map<std::string,OperationCB> OperCBMap;
138 
139 public:
141  static void addOperation(const std::string& name, OperationCB operation)
142  {
143  static std::map<std::string,OperationCB> ops;
144  ops[name] = operation;
145  ourOperations = &ops;
146  }
147 
149  static void getOperations(std::vector<std::string>& names)
150  {
151  names.clear();
152  if (ourOperations)
153  {
154  names.reserve(ourOperations->size());
155  for (const typename OperCBMap::value_type& oper : *ourOperations)
156  names.push_back(oper.first);
157  }
158  }
159 
161  static void setDefaultOperation(const std::string& o) { ourDefaultOper = o; }
163  static const std::string& getDefaultOperation() { return ourDefaultOper; }
164 
165 protected:
168 
173  void selectOperation(const std::string& name)
174  {
175  if (ourOperations && !name.empty())
176  {
177  typename OperCBMap::const_iterator it = ourOperations->find(name);
178  if (it != ourOperations->end())
179  {
180  myOperationCB = it->second;
181  return;
182  }
183  }
184 
185  // The selected operation is not defined
186  myOperationCB.erase();
187  }
188 
189  OperationCB myOperationCB;
190 
191 private:
193  static std::map<std::string,OperationCB>* ourOperations;
195  static std::string ourDefaultOper;
196 };
197 
198 template <class OperationCB>
199 std::map<std::string,OperationCB>* FFaOperationContainer<OperationCB>::ourOperations = NULL;
200 
201 template <class OperationCB>
203 
204 
209 template <class RetType, class PrmType = RetType>
210 class FFaUnaryOp : public FFaOperation<RetType>,
211  public FFaOperationContainer< FFaDynCB2<RetType&,const PrmType&> >
212 {
213 public:
215  FFaUnaryOp(FFaOperation<PrmType>* param, const std::string& name = "")
216  {
217  if ((myParam = param))
218  myParam->ref();
219 
220  if (!name.empty())
221  this->selectOperation(name);
222  }
223 
225  virtual void invalidate()
226  {
227  if (this->IAmEvaluated && myParam)
228  myParam->invalidate();
229 
230  this->IAmEvaluated = false;
231  }
232 
234  virtual bool hasData() const
235  {
236  return myParam ? myParam->hasData() : false;
237  }
238 
240  virtual bool evaluate(RetType& value)
241  {
242  bool ok = false;
243  if (myParam && !this->myOperationCB.empty())
244  {
245  PrmType param;
246  ok = myParam->invoke(param);
247  this->myOperationCB.invoke(value,param);
248  }
249 
250  return ok;
251  }
252 
253 protected:
255  virtual ~FFaUnaryOp()
256  {
257  if (myParam) myParam->unref();
258  }
259 
260 private:
262 
263 #ifdef FT_USE_MEMPOOL
264  FFA_MAKE_MEMPOOL;
265 #endif
266 };
267 
268 #ifdef FT_USE_MEMPOOL
270 template <class RetType, class PrmType>
272  FFaOperationBase::getMemPoolMgr());
274 #endif
275 
276 
281 template <class RetType>
282 class FFaNToOneOp : public FFaOperation<RetType>,
283  public FFaOperationContainer< FFaDynCB2<RetType&,const std::vector<RetType>&> >
284 {
285 public:
287  FFaNToOneOp(const std::vector<FFaOperation<RetType>*>& params,
288  const std::string& name = "")
289  {
290  myParams = params;
292  if (p) p->ref();
293 
294  if (!name.empty())
295  this->selectOperation(name);
296  }
297 
299  virtual void invalidate()
300  {
301  if (this->IAmEvaluated)
303  if (p) p->invalidate();
304 
305  this->IAmEvaluated = false;
306  }
307 
309  virtual bool hasData() const
310  {
312  if (p && p->hasData())
313  return true;
314 
315  return false;
316  }
317 
319  virtual bool evaluate(RetType& value)
320  {
321  bool ok = !this->myOperationCB.empty();
322  if (ok)
323  {
324  std::vector<RetType> pVals(myParams.size());
325  for (size_t i = 0; i < myParams.size(); i++)
326  if (myParams[i])
327  ok &= myParams[i]->invoke(pVals[i]);
328 
329  this->myOperationCB.invoke(value,pVals);
330  }
331 
332  return ok;
333  }
334 
335 protected:
337  virtual ~FFaNToOneOp()
338  {
340  if (p) p->unref();
341  }
342 
343 private:
344  std::vector<FFaOperation<RetType>*> myParams;
345 
346 #ifdef FT_USE_MEMPOOL
347  FFA_MAKE_MEMPOOL;
348 #endif
349 };
350 
351 #ifdef FT_USE_MEMPOOL
353 template <class RetType>
354 FFaMemPool FFaNToOneOp<RetType>::ourMemPool = FFaMemPool(sizeof(FFaNToOneOp<RetType>),
355  FFaOperationBase::getMemPoolMgr());
357 #endif
358 
359 
360 namespace FFa
361 {
367  void setSpecialResultValue(double value, double equiv);
368 }
369 
370 #endif
Macros for definition of dynamic callback objects.
A class for vector operations.
Definition: FFaOperation.H:284
virtual ~FFaNToOneOp()
The destructor deletes the child operations, if any.
Definition: FFaOperation.H:337
std::vector< FFaOperation< RetType > * > myParams
Parameter child operations.
Definition: FFaOperation.H:344
virtual void invalidate()
Invalidates the cached value of this operation.
Definition: FFaOperation.H:299
virtual bool evaluate(RetType &value)
Invokes the actual operation.
Definition: FFaOperation.H:319
virtual bool hasData() const
Checks if this operation will return any data.
Definition: FFaOperation.H:309
FFaNToOneOp(const std::vector< FFaOperation< RetType > * > &params, const std::string &name="")
The constructor selects the actual operation to use from name.
Definition: FFaOperation.H:287
Base class for all operations.
Definition: FFaOperation.H:38
virtual ~FFaOperationBase()
Empty destructor.
Definition: FFaOperation.H:43
short int myRefCount
Reference counter.
Definition: FFaOperation.H:66
void ref()
Increments the reference counter.
Definition: FFaOperation.H:47
FFaOperationBase()
Default constructor.
Definition: FFaOperation.H:41
static void freeMemPools()
Dummy method doing nothing.
Definition: FFaOperation.H:62
void unref()
Decrements the reference counter, and deletes *this if zero.
Definition: FFaOperation.H:49
A class used to manage and store callbacks of a certain type.
Definition: FFaOperation.H:135
static std::string ourDefaultOper
Name of the default operation type OperationCB.
Definition: FFaOperation.H:195
static const std::string & getDefaultOperation()
Returns the default operation of type OperationCB.
Definition: FFaOperation.H:163
OperationCB myOperationCB
The selected operation.
Definition: FFaOperation.H:189
static void getOperations(std::vector< std::string > &names)
Returns a list of all operations of type OperationCB.
Definition: FFaOperation.H:149
std::map< std::string, OperationCB > OperCBMap
Convenience type definition, name-to-operation mapping.
Definition: FFaOperation.H:137
void selectOperation(const std::string &name)
Selects the named operation to use.
Definition: FFaOperation.H:173
static std::map< std::string, OperationCB > * ourOperations
Static container for operations of type OperationCB.
Definition: FFaOperation.H:193
FFaOperationContainer()
The default constructor defines the default operation.
Definition: FFaOperation.H:167
static void setDefaultOperation(const std::string &o)
Defines the default operation of type OperationCB.
Definition: FFaOperation.H:161
static void addOperation(const std::string &name, OperationCB operation)
Adds a named operation to the OperationCB container.
Definition: FFaOperation.H:141
The base class used as argument in other operations.
Definition: FFaOperation.H:76
virtual bool evaluate(RetType &value)=0
Invokes the actual operation.
bool IAmEvaluated
If true, the cached value will be used.
Definition: FFaOperation.H:112
virtual void invalidate()
Invalidates the cached value of this operation.
Definition: FFaOperation.H:101
FFaOperation()
Default constructor.
Definition: FFaOperation.H:79
RetType myCachedValue
Cached operation value, to avoid re-evaluation.
Definition: FFaOperation.H:109
virtual bool hasData() const =0
Checks if this operation will return any data.
bool invoke(RetType &value)
Returns current value of this operation.
Definition: FFaOperation.H:85
virtual ~FFaOperation()
Empty destructor.
Definition: FFaOperation.H:81
A class for scalar operations.
Definition: FFaOperation.H:212
virtual ~FFaUnaryOp()
The destructor deletes the child operation, if any.
Definition: FFaOperation.H:255
FFaUnaryOp(FFaOperation< PrmType > *param, const std::string &name="")
The constructor selects the actual operation to use from name.
Definition: FFaOperation.H:215
virtual bool evaluate(RetType &value)
Invokes the actual operation.
Definition: FFaOperation.H:240
virtual bool hasData() const
Checks if this operation will return any data.
Definition: FFaOperation.H:234
FFaOperation< PrmType > * myParam
Parameter child operation.
Definition: FFaOperation.H:261
virtual void invalidate()
Invalidates the cached value of this operation.
Definition: FFaOperation.H:225
Various math utilities.
Definition: FFaMath.H:52
void setSpecialResultValue(double value, double equiv)
Defines a special result value and its associated equivalent value.
Definition: FFaBasicOperations.C:352