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 <string>
22 #include <vector>
23 #include <utility>
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  using NamedOper = std::pair<std::string,OperationCB>;
138 
139 public:
141  static void addOperation(const std::string& name, OperationCB operation)
142  {
143  static std::vector<NamedOper> ops;
144  ops.emplace_back(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 NamedOper& oper : *ourOperations)
156  names.push_back(oper.first);
157  }
158  }
159 
161  static const std::string& getDefaultOperation()
162  {
163  if (ourOperations && !ourOperations->empty())
164  return ourOperations->front().first;
165 
166  static const std::string noop;
167  return noop;
168  }
169 
170 protected:
173 
178  void selectOperation(const std::string& name)
179  {
180  if (ourOperations && !name.empty())
181  for (const NamedOper& oper : *ourOperations)
182  if (oper.first == name)
183  {
184  myOperationCB = oper.second;
185  return;
186  }
187 
188  // The selected operation is not defined
189  myOperationCB.erase();
190  }
191 
192  OperationCB myOperationCB;
193 
194 private:
196  static std::vector<NamedOper>* ourOperations;
197 };
198 
199 template <class OperationCB>
200 std::vector< std::pair<std::string,OperationCB> >*
202 
203 
208 template <class RetType, class PrmType = RetType>
209 class FFaUnaryOp : public FFaOperation<RetType>,
210  public FFaOperationContainer< FFaDynCB2<RetType&,const PrmType&> >
211 {
212 public:
214  FFaUnaryOp(FFaOperation<PrmType>* param, const std::string& name = "")
215  {
216  if ((myParam = param))
217  myParam->ref();
218 
219  if (!name.empty())
220  this->selectOperation(name);
221  }
222 
224  virtual void invalidate()
225  {
226  if (this->IAmEvaluated && myParam)
227  myParam->invalidate();
228 
229  this->IAmEvaluated = false;
230  }
231 
233  virtual bool hasData() const
234  {
235  return myParam ? myParam->hasData() : false;
236  }
237 
239  virtual bool evaluate(RetType& value)
240  {
241  bool ok = false;
242  if (myParam && !this->myOperationCB.empty())
243  {
244  PrmType param;
245  ok = myParam->invoke(param);
246  this->myOperationCB.invoke(value,param);
247  }
248 
249  return ok;
250  }
251 
252 protected:
254  virtual ~FFaUnaryOp()
255  {
256  if (myParam) myParam->unref();
257  }
258 
259 private:
261 
262 #ifdef FT_USE_MEMPOOL
263  FFA_MAKE_MEMPOOL;
264 #endif
265 };
266 
267 #ifdef FT_USE_MEMPOOL
269 template <class RetType, class PrmType>
271  FFaOperationBase::getMemPoolMgr());
273 #endif
274 
275 
280 template <class RetType>
281 class FFaNToOneOp : public FFaOperation<RetType>,
282  public FFaOperationContainer< FFaDynCB2<RetType&,const std::vector<RetType>&> >
283 {
284 public:
286  FFaNToOneOp(const std::vector<FFaOperation<RetType>*>& params,
287  const std::string& name = "")
288  {
289  myParams = params;
291  if (p) p->ref();
292 
293  if (!name.empty())
294  this->selectOperation(name);
295  }
296 
298  virtual void invalidate()
299  {
300  if (this->IAmEvaluated)
302  if (p) p->invalidate();
303 
304  this->IAmEvaluated = false;
305  }
306 
308  virtual bool hasData() const
309  {
311  if (p && p->hasData())
312  return true;
313 
314  return false;
315  }
316 
318  virtual bool evaluate(RetType& value)
319  {
320  bool ok = !this->myOperationCB.empty();
321  if (ok)
322  {
323  std::vector<RetType> pVals(myParams.size());
324  for (size_t i = 0; i < myParams.size(); i++)
325  if (myParams[i])
326  ok &= myParams[i]->invoke(pVals[i]);
327 
328  this->myOperationCB.invoke(value,pVals);
329  }
330 
331  return ok;
332  }
333 
334 protected:
336  virtual ~FFaNToOneOp()
337  {
339  if (p) p->unref();
340  }
341 
342 private:
343  std::vector<FFaOperation<RetType>*> myParams;
344 
345 #ifdef FT_USE_MEMPOOL
346  FFA_MAKE_MEMPOOL;
347 #endif
348 };
349 
350 #ifdef FT_USE_MEMPOOL
352 template <class RetType>
353 FFaMemPool FFaNToOneOp<RetType>::ourMemPool = FFaMemPool(sizeof(FFaNToOneOp<RetType>),
354  FFaOperationBase::getMemPoolMgr());
356 #endif
357 
358 
359 namespace FFa
360 {
366  void setSpecialResultValue(double value, double equiv);
367 }
368 
369 #endif
Macros for definition of dynamic callback objects.
A class for vector operations.
Definition: FFaOperation.H:283
virtual ~FFaNToOneOp()
The destructor deletes the child operations, if any.
Definition: FFaOperation.H:336
std::vector< FFaOperation< RetType > * > myParams
Parameter child operations.
Definition: FFaOperation.H:343
virtual void invalidate()
Invalidates the cached value of this operation.
Definition: FFaOperation.H:298
virtual bool evaluate(RetType &value)
Invokes the actual operation.
Definition: FFaOperation.H:318
virtual bool hasData() const
Checks if this operation will return any data.
Definition: FFaOperation.H:308
FFaNToOneOp(const std::vector< FFaOperation< RetType > * > &params, const std::string &name="")
The constructor selects the actual operation to use from name.
Definition: FFaOperation.H:286
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 const std::string & getDefaultOperation()
Returns the default operation of type OperationCB.
Definition: FFaOperation.H:161
OperationCB myOperationCB
The selected operation.
Definition: FFaOperation.H:192
static void getOperations(std::vector< std::string > &names)
Returns a list of all operations of type OperationCB.
Definition: FFaOperation.H:149
void selectOperation(const std::string &name)
Selects the named operation to use.
Definition: FFaOperation.H:178
std::pair< std::string, OperationCB > NamedOper
Convenience type alias for a named operations.
Definition: FFaOperation.H:137
FFaOperationContainer()
The default constructor selects the default operation.
Definition: FFaOperation.H:172
static std::vector< NamedOper > * ourOperations
Static container for operations of type OperationCB.
Definition: FFaOperation.H:196
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:211
virtual ~FFaUnaryOp()
The destructor deletes the child operation, if any.
Definition: FFaOperation.H:254
FFaUnaryOp(FFaOperation< PrmType > *param, const std::string &name="")
The constructor selects the actual operation to use from name.
Definition: FFaOperation.H:214
virtual bool evaluate(RetType &value)
Invokes the actual operation.
Definition: FFaOperation.H:239
virtual bool hasData() const
Checks if this operation will return any data.
Definition: FFaOperation.H:233
FFaOperation< PrmType > * myParam
Parameter child operation.
Definition: FFaOperation.H:260
virtual void invalidate()
Invalidates the cached value of this operation.
Definition: FFaOperation.H:224
Various math utilities.
Definition: FFaMath.H:58
void setSpecialResultValue(double value, double equiv)
Defines a special result value and its associated equivalent value.
Definition: FFaBasicOperations.C:334