CEPS  24.01
Cardiac ElectroPhysiology Simulator
FunctionDictionary.hpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
30 #pragma once
31 
32 #include "common/CepsCommon.hpp"
35 
46 {
47 
48  public:
49 
52  template <class _Result>
60 
62  template <class _Result>
70 
73 
76 
84  void
85  add(const CepsString& label, const CepsString& params, Geometry* geom = nullptr);
86 
87  // -----------------------------------------------------------------
88 
90  void
91  addConstant(const CepsString& label, const CepsString& params);
92 
94  template <class _Constant>
95  void
96  addConstant(const CepsString& label, _Constant value);
97 
98  // -----------------------------------------------------------------
99 
101  void
102  addCstPiecewise(const CepsString& label, const CepsString& params);
103 
105  void
106  addFctPiecewise (const CepsString& label, const CepsString& params);
107 
108  // -----------------------------------------------------------------
109 
117  void
118  addFromFile (
119  const CepsString& label,
120  const CepsString& params,
121  Geometry* geom,
122  CepsBool neverMind = false
123  );
124 
125  // -----------------------------------------------------------------
126 
128  void
129  addFunction(const CepsString& label, const CepsString& params);
130 
131  // -----------------------------------------------------------------
132 
134  void
135  addOperator(const CepsString& label, const CepsString &params);
136 
139  template <class _Obj1, class _Obj2>
140  void
141  addOperator(const CepsString& label, _Obj1 *a, const CepsChar& op, _Obj2 *b);
142 
143  // ------------------------------------------------------------------
144 
146  void
147  addAlias(const CepsString& label, const CepsString& params);
148 
149  // -----------------------------------------------------------------
150  // Generic Add function
151  // -----------------------------------------------------------------
152 
154  void
155  addEntry (const CepsString& label, ScalarEntry func);
156 
158  void
159  addEntry (const CepsString& label, VectorEntry func);
160 
162  void
163  addEntry (const CepsString& label, TensorEntry func);
164 
165  // -----------------------------------------------------------------
166  // Has function
167  // -----------------------------------------------------------------
168 
170  CepsBool
171  hasScalar (const CepsString& label) const;
172 
174  CepsBool
175  hasVector (const CepsString& label) const;
176 
178  CepsBool
179  hasTensor (const CepsString& label) const;
180 
181  // -----------------------------------------------------------------
182  // Get functions
183  // -----------------------------------------------------------------
184 
186  const ScalarMap&
187  getScalars() const;
188 
190  const VectorMap&
191  getVectors() const;
192 
194  const TensorMap&
195  getTensors() const;
196 
198  const ScalarEntry
199  getScalar(const CepsString& label) const;
200 
202  const VectorEntry
203  getVector(const CepsString& label) const;
204 
206  const TensorEntry
207  getTensor(const CepsString& label) const;
208 
210  const ScalarEntry
211  getScalar(const CepsString& label, const ScalarEntry def) const;
212 
214  const VectorEntry
215  getVector(const CepsString& label, const VectorEntry def) const;
216 
218  const TensorEntry
219  getTensor(const CepsString& label, const TensorEntry def) const;
220 
222  void
223  deleteScalar(const CepsString& label);
224 
226  void
227  deleteVector(const CepsString& label);
228 
230  void
231  deleteTensor(const CepsString& label);
232 
233 
234  private:
235 
240  parseFunction (const CepsString &params);
241 
245 
249 
251  void
252  checkParam(const CepsVector<CepsString>& words, const CepsVector<CepsString>& keys,
253  const CepsVector<CepsUInt>& nToRead={}) const;
254 
256  template <class _Result>
257  void
258  addInternal(const CepsString& label, Entry<_Result> func,
259  Map<_Result>& map, const CepsString& name);
260 
262  template <class _Result>
263  static CepsBool
264  hasInternal(const CepsString &key, const Map<_Result> &map);
265 
267  template <CepsUInt n, typename Reals>
268  void
269  addInterpolationTable(const CepsString& label, std::ifstream& ifs, const CepsString& fileName, CepsBool time);
270 
271 
272  protected:
273 
277 
279 
281  CepsString m_address = " See https://carmen.gitlabpages.inria.fr/ceps/syntax.html for details on syntax";
282 
283 };
284 
285 // template definitions
286 
287 template <class _Constant>
288 void
289 FunctionDictionary::addConstant(const CepsString &label,_Constant value)
290 {
291  return addEntry(label,newConstantSAFunc(value));
292 }
293 
294 template <class _Obj1, class _Obj2>
295 void
296 FunctionDictionary::addOperator(const CepsString &label, _Obj1 *a, const CepsChar &op, _Obj2 *b)
297 {
298  if (op == '+')
299  return addEntry(label,newSumSAFunc(a,b));
300  if (op == '-')
301  return addEntry(label,newDiffSAFunc(a,b));
302  if (op == '*')
303  return addEntry(label,newProductSAFunc(a,b));
304  if (op == '/')
305  return addEntry(label,newDivSAFunc(a,b));
306 
307  CEPS_ABORT("Operator '" << op << "' between functions is not implemented (target " << label << ")");
308 }
309 
310 template <class _Result>
311 void
313  const CepsString& label,
314  Entry<_Result> func,
315  Map<_Result>& map,
316  const CepsString& name
317 )
318 {
319  if (map.contains(ceps::toKey(label)))
320  {
321  CEPS_WARNS (
322  "dictionary already contains an object with the label " << label <<
323  "\n of type '" << name << "'. Ignoring"
324  );
325  ceps::destroyObject(func);
326  return;
327  }
328  map.insert(std::make_pair(ceps::toKey(label),func));
329  return;
330 }
331 
332 template <class _Result>
333 CepsBool
335 {
336  return map.contains(ceps::toKey(label));
337 }
338 
339 template <CepsUInt n, typename Reals>
340 void
342  const CepsString& label,
343  std::ifstream& ifs,
344  const CepsString& fileName,
345  CepsBool time
346 )
347 {
348  ifs.seekg(0,std::ios::beg);
349  Reals value;
350  CepsReal x{0.};
351  CepsUInt lc = 1;
352  CepsString line;
354  while(std::getline(ifs,line))
355  {
356  std::istringstream iss(line);
357  CepsString errmsg(fileName+", line "+std::to_string(lc)+": cannot read real ");
358  x = ceps::readReal(iss,errmsg+"in first column");
359  value = ceps::convertReal<Reals>(ceps::readReals<n>(iss,errmsg).data());
360  itpMap[x] = value;
361  lc++;
362  }
363  ScalarSAFunc* selector = nullptr;
364  if (time)
365  selector = getScalar("dicofunc_time_selector");
366  return addEntry(label,new CoeffInterpolatorSAFunc(itpMap,selector));
367 
368 }
369 
370 // ===================================================================
371 
375 {
377  CepsEnum
378  getFlags() const override;
379 
381  CepsReal
382  eval(CepsStandardArgs) override;
383 };
#define CEPS_ABORT(message)
Stops the execution with a message. If testing is enabled, only throws a runtime_error.
#define CEPS_WARNS(message)
Writes a warning in the debug log and in the terminal (stderr).
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
std::map< _Key, _Tp, _Compare, _Alloc > CepsMap
C++ map.
Definition: CepsTypes.hpp:196
int CepsEnum
Enum type.
Definition: CepsTypes.hpp:216
char CepsChar
Char.
Definition: CepsTypes.hpp:125
std::set< _Type, _Compare, _Alloc > CepsSet
C++ set.
Definition: CepsTypes.hpp:209
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
float CepsReal
Need single precision floating point.
Definition: CepsTypes.hpp:100
ConstantSAFunc< _Result > * newConstantSAFunc(_Result val)
Standard way to build a ConstantSAFunc. Only pass the value, or a pointer to it.
OperatorSAFunc<'-', _Res1, _Res2 > * newDiffSAFunc(SAFunc< _Res1 > *, SAFunc< _Res2 > *)
Get a new substraction object without having to look after return types of fn1 and fn2.
OperatorSAFunc<'+', _Res1, _Res2 > * newSumSAFunc(SAFunc< _Res1 > *, SAFunc< _Res2 > *)
Get a new addition object without having to look after return types of fn1 and fn2.
OperatorSAFunc<'/', _Res1, _Res2 > * newDivSAFunc(SAFunc< _Res1 > *, SAFunc< _Res2 > *)
Get a new division object without having to look after return types of fn1 and fn2.
OperatorSAFunc<' *', _Res1, _Res2 > * newProductSAFunc(SAFunc< _Res1 > *, SAFunc< _Res2 > *)
Get a new product object without having to look after return types of fn1 and fn2.
Base class for other (big) CEPS classes. All classes can get a pointer to this base class and also co...
Definition: CepsObject.hpp:40
Functions with result that is deduced from a coefficient and an interpolation map.
A simple SA func that extracts time from arguments, will be added to the dictionary upon creation.
CepsReal eval(CepsStandardArgs) override
Returns args.t.
CepsEnum getFlags() const override
Time flags.
FunctionDictionary that holds functions which can be used to define source terms, boundary conditions...
void addFromFile(const CepsString &label, const CepsString &params, Geometry *geom, CepsBool neverMind=false)
Add a function read from data file, requires geometry. If the extension is compatible with CoeffReade...
void addConstant(const CepsString &label, const CepsString &params)
Add a constant function through parameter string.
void addInterpolationTable(const CepsString &label, std::ifstream &ifs, const CepsString &fileName, CepsBool time)
Internal method to add intepolation tables of scalar, vector and tensors.
Map< CepsMathVertex > VectorMap
Typedef for the map of vector functions.
FunctionDictionary()
Constructor, adds some function by default.
CepsBool hasTensor(const CepsString &label) const
Tells if function "label" is registered.
void addFctPiecewise(const CepsString &label, const CepsString &params)
Add a piecewise function, function on eacg.
ScalarEntry parseSpatialFunction(const CepsVector< CepsString > &params)
Returns a pointer to a function deduced from the string parameters.
Map< CepsMathTensor > TensorMap
Typedef for the map of tensor functions.
void addOperator(const CepsString &label, const CepsString &params)
Add an operation between already registered functions and/or real constants.
void addCstPiecewise(const CepsString &label, const CepsString &params)
Add a piecewise function, constant on each region.
CepsSet< CepsString > m_doNotDelete
Functions that must not be deleted (created by alias)
~FunctionDictionary()
Destructor.
ScalarMap m_scalars
All scalar entries.
void deleteVector(const CepsString &label)
Delete a single vector entry. Be careful if the functor was created outside dictionary.
ScalarEntry parseTimeFunction(const CepsVector< CepsString > &params)
Returns a pointer to a function deduced from the string parameters.
VectorMap m_vectors
All vector entries.
CepsBool hasScalar(const CepsString &label) const
Tells if function "label" is registered.
void addAlias(const CepsString &label, const CepsString &params)
Adds a shortcut to an already existing function.
const TensorEntry getTensor(const CepsString &label) const
Get a single tensor entry, const version.
void checkParam(const CepsVector< CepsString > &words, const CepsVector< CepsString > &keys, const CepsVector< CepsUInt > &nToRead={}) const
Aborts if a function parameter is missing or not a number.
Map< CepsMathScalar > ScalarMap
Typedef for the map of scalar functions.
TensorMap m_tensors
All tensor entries.
CepsMap< CepsString, Entry< _Result > > Map
Typedef for map of functions, any kind.
void addInternal(const CepsString &label, Entry< _Result > func, Map< _Result > &map, const CepsString &name)
Internal adding method.
CepsBool hasVector(const CepsString &label) const
Tells if function "label" is registered.
static CepsBool hasInternal(const CepsString &key, const Map< _Result > &map)
Internal inspecting method.
const TensorMap & getTensors() const
Get all tensor entries, const version.
const VectorMap & getVectors() const
Get all vector entries, const version.
const VectorEntry getVector(const CepsString &label) const
Get a single vector entry, const version.
CepsString m_address
A string that is displayed in error messages.
const ScalarMap & getScalars() const
Get all scalar entries, const version.
void add(const CepsString &label, const CepsString &params, Geometry *geom=nullptr)
Add an object from parameters.
void addFunction(const CepsString &label, const CepsString &params)
Add a function deduced from tags only, works only with scalar return type.
void deleteScalar(const CepsString &label)
Delete a single scalar entry. Be careful if the functor was created outside dictionary.
const ScalarEntry getScalar(const CepsString &label) const
Get a single scalar entry, const version.
void addEntry(const CepsString &label, ScalarEntry func)
Ads an entry to the map of scalar functions.
void deleteTensor(const CepsString &label)
Delete a single tensor entry. Be careful if the functor was created outside dictionary.
ScalarEntry parseFunction(const CepsString &params)
Returns a pointer to a function deduced from the string parameters.
Encapsulates all the geometrical data.
Definition: Geometry.hpp:50
A SAFunc is a ceps::Function that uses CepsStandardArgs as argument of call operator (),...
Definition: SAFunc.hpp:100
CepsString toKey(const CepsString &s)
Transform to key type a std::string, upper case and no spaces.
Definition: CepsString.cpp:157
void destroyObject(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:116
CepsReal readReal(std::istream &file, const CepsString &errorMessage="")
Reads a floating point number from an istream, aborts if conversion fails advances the stream by 1 wo...
Definition: CepsString.cpp:661
Structure used to pass arguments to SAFunc (see pde directory) The flags of the SAFunc allows extract...
Definition: CepsTypes.hpp:239