CEPS  24.01
Cardiac ElectroPhysiology Simulator
SourceTermManager.cpp
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
33 
35  CepsObject(),
36  m_dofs(nullptr),
37  m_manager(ceps::getNew<Manager>()),
38  m_dictionary(dico)
39 {}
40 
42 {
43  for (auto [name,st]: *m_manager)
46 }
47 
48 void
50 {
51  m_dofs = dofs;
52  return;
53 }
54 
55 void
56 SourceTermManager::add(const CepsString &params,CepsBool computeSupport)
57 {
58  // -------------------------------------------------- //
59  // simple parse of params to deduce key
60  // -------------------------------------------------- //
61 
62  CepsString::size_type pos = params.find(" ");
63  CepsString key = ceps::toKey(params.substr(0,pos));
64  CepsString rest = pos == std::string::npos ? "" : params.substr(pos+1);
65 
66  CEPS_ABORT_IF(rest.empty(),
67  "When adding source term, no parameters found after key " << key
68  );
69 
70  add(key,rest,computeSupport);
71  return;
72 }
73 
74 void
75 SourceTermManager::add(const CepsString &key, const CepsString &params, CepsBool computeSupport)
76 {
77  // -------------------------------------------------- //
78  // deduce the dico_key or the value
79  // -------------------------------------------------- //
80 
81  CepsString::size_type pos = params.find(" ");
82  CepsString dicokey = ceps::toKey(params.substr(0,pos));
83  CepsString rest = pos == std::string::npos ? "" : params.substr(pos+1);
84 
85  CEPS_ABORT_IF(not ceps::isNumber(dicokey) and rest.empty(),
86  "When adding source term, no parameters found after function name " << key << " " << dicokey
87  );
88 
89  add(key,dicokey,rest,computeSupport);
90  return;
91 }
92 
93 void
95  const CepsString& params, CepsBool computeSupport)
96 {
97  // -------------------------------------------------- //
98  // extract dico_tag or value
99  // -------------------------------------------------- //
100 
101  ScalarSAFunc* func;
102 
103  if (not m_dictionary->hasScalar(dicokey))
104  {
105  CepsString ndicokey = key + "_CONSTANT";
106  m_dictionary->addConstant(ndicokey,dicokey);
107  func = ceps::runtimeCast<ScalarSAFunc*>(m_dictionary->getScalar(ndicokey));
108  }
109  else
110  func = ceps::runtimeCast<ScalarSAFunc*>(m_dictionary->getScalar(dicokey));
111 
112  add(key,params,func,computeSupport);
113  return;
114 }
115 
116 void
118  const CepsString& key,
119  const CepsString& params,
120  ScalarSAFunc* functor,
121  CepsBool computeSupport
122 )
123 {
124  // -------------------------------------------------- //
125  // Read parameters. Currently supported keywords :
126  // - COMPONENT
127  // - ATTRIBUTE(S)
128  // - SCALE
129  // - TYPE [IAPP, KTERM, DEFAULT]
130  // - UNIT
131  // -------------------------------------------------- //
132 
133  const CepsString& COMPONENT = "UNKNOWN";
134  const CepsString& ATTRIBUTE = "ATTRIBUTE";
135  const CepsString& ATTRIBUTES = "ATTRIBUTES";
136  const CepsString& SCALE = "SCALE";
137  const CepsString& UNIT = "UNIT";
138  const CepsString& TYPE = "TYPE";
139 
140  auto options = ceps::splitByKeyword(params,{COMPONENT,ATTRIBUTE,ATTRIBUTES,SCALE,UNIT,TYPE});
141 
142  // Unknown (previously called component)
143  CepsUnknownIndex component = 0;
144  if (options.contains(COMPONENT))
145  {
146  CEPS_ABORT_IF(options[COMPONENT].empty(),
147  "When adding source term, missing unknown identifier after "<< COMPONENT << " keyword"
148  );
149  component = CepsUnknownIndex(ceps::toInt(options[COMPONENT][0]));
150  }
151 
152  // Attributes
153  CepsSet<CepsUInt> attributes = {};
154 
155  for (auto label : {ATTRIBUTE, ATTRIBUTES})
156  if (options.contains(label))
157  {
158  CEPS_ABORT_IF(options[label].empty(),
159  "When adding source term, could not find any attribute after " << label << " keyword"
160  );
161  for (CepsString s : options[label])
162  attributes.insert(CepsAttribute(ceps::toInt(s)));
163  }
164 
165  if (attributes.empty())
166  attributes.insert(CepsUniversal);
167 
168  // Scaling factor
169  CepsReal scale = 1.;
170  if (options.contains(SCALE))
171  {
172  CEPS_ABORT_IF(options[SCALE].empty() or not ceps::isNumber(options[SCALE][0]),
173  "When adding source term, could not find real number after SCALE keyword"
174  );
175  scale = ceps::toReal(options[SCALE].at(0));
176  }
177 
178  // Type of source term
180 
181  if (options.contains(TYPE))
182  {
183  CEPS_ABORT_IF(options[TYPE].empty(),
184  "When adding source term, could not find any entry after TYPE keyword"
185  );
186  if (options[TYPE].at(0) == "KTERM")
188  else if (options[TYPE].at(0) == "IAPP")
190  else
191  {
192  CEPS_ABORT_IF(options[TYPE].at(0)!= "DEFAULT", CepsString(
193  "When adding source term, invalid entry after TYPE keyword. Valid types are\n") +
194  " KTERM (for source terms to be mulitplied XXX FIXME XXX )\n" +
195  " IAPP (volumic source term specific to cardiac PDEs)\n" +
196  " DEFAULT (regular source term)"
197  );
198  }
199  }
200 
201  // Now create the field
202  ScalarSourceTerm* st = ceps::getNew<ScalarSourceTerm>(functor,&(m_dofs->getOwned()),component);
203  st->setScaleFactor(scale);
204  st->setFlag (flag);
205 
206  if (computeSupport)
207  {
208  // Compute support of source term
209 
210  // If there is a defined region
211  if (not attributes.empty())
212  {
214  selec.setAttributes(attributes.begin(),attributes.end());
215  selec.onlyOnThisProc(true);
216  selec.onlyOnBoundary(false);
217  st->computeSupport(&selec);
218  }
219 
220  // Then remove dofs for other unknowns
221  std::function<CepsBool(DegreeOfFreedom*)> unknownSelector = [&] (DegreeOfFreedom* d) -> CepsBool {
222  return d->getUnknown()->getIdentifier() == component;
223  };
225  selec2.onlyOnThisProc(true);
226  selec2.onlyOnBoundary(false);
227  st->computeSupport(&selec2,true);
228 
229  }
230 
231  add(key,st);
232  return;
233 }
234 
235 void
237 {
238  CepsString nkey = ceps::toKey(key);
239 
240  if (m_manager->contains(nkey))
241  {
242  CEPS_WARNS ("already contains a source term with the key " << nkey << ". Ignoring.");
244  return;
245  }
246 
247  m_manager->insert(std::make_pair(nkey,st));
248 
249  CEPS_SAYS (" Added new source term : " << key);
250  return;
251 }
252 
253 void
255 {
256  for (auto obj : *m_manager)
257  obj.second->actualize(time);
258 
259  return;
260 }
261 
264 {
265  CepsString KEY = ceps::toKey(label);
266  CEPS_ABORT_IF(not m_manager->contains(KEY),
267  "Source term manager has no source term with label " + label
268  );
269  return m_manager->at(KEY);
270 }
271 
274 {
275  return ceps::valuesOf(*m_manager);
276 }
277 
278 typename SourceTermManager::Manager* const
280 {
281  return m_manager;
282 }
283 
284 CepsUInt
286 {
288 }
289 
290 
291 CepsUInt
293 {
294  CepsUInt res = 0;
295  for (auto it: *m_manager)
296  {
297  if (isEnabledOption(flag, it.second->getFlag()))
298  res++;
299  }
300  return res;
301 }
CepsSourceTermFlag
Source terms flags.
Definition: CepsEnums.hpp:163
@ Laplace
A source term that multiplies grad phi (for FE)
@ Default
Simply add the source term.
@ Stimulation
Apply cardiac specific treatment before adding.
#define CEPS_SAYS(message)
Writes a message in the debug log and in the terminal (stdio).
#define CEPS_ABORT_IF(condition, message)
Stops the execution with a message if condition is true. If testing is enabled, only throws a runtime...
#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
CepsIndex CepsUnknownIndex
For unknowns.
Definition: CepsTypes.hpp:217
int CepsEnum
Enum type.
Definition: CepsTypes.hpp:216
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
CepsInt CepsAttribute
Used to define regions.
Definition: CepsTypes.hpp:215
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
constexpr CepsAttribute CepsUniversal
This attribute means "everywhere".
Definition: CepsTypes.hpp:232
void onlyOnThisProc(CepsBool value=true)
Choose only object that belongs to this proc.
void onlyOnBoundary(CepsBool value=true)
Choose only objects that are on boundary.
Base class for other (big) CEPS classes. All classes can get a pointer to this base class and also co...
Definition: CepsObject.hpp:40
A degree of freedom for any kind of problem The dof can be associated to a geometrical element or not...
const CepsVector< _Type > & getOwned() const
Get data owned by the processor, const.
void setScaleFactor(CepsReal scaleFactor)
Scale the result.
void computeSupport(AbstractSelector< _Iterator > *selector, CepsBool canStartEmpty=false)
Computes the support in the domains of definition, using the given selector.
FunctionDictionary that holds functions which can be used to define source terms, boundary conditions...
void addConstant(const CepsString &label, const CepsString &params)
Add a constant function through parameter string.
CepsBool hasScalar(const CepsString &label) const
Tells if function "label" is registered.
const ScalarEntry getScalar(const CepsString &label) const
Get a single scalar entry, const version.
A SAFunc is a ceps::Function that uses CepsStandardArgs as argument of call operator (),...
Definition: SAFunc.hpp:100
~SourceTermManager() final
Destroy the Source Term Manager object : default destructor.
void actualizeAll(CepsReal time)
Actualize all data inside.
FunctionDictionary * m_dictionary
the dictionary
SourceTermManager(FunctionDictionary *dico)
Construct a new Source Term Manager object, linked with dictionary of functions.
DistributedInfos< DegreeOfFreedom * > * m_dofs
dofs
Manager * m_manager
the manager
void add(const CepsString &params, CepsBool computeSupport=false)
Add a source term from parameters.
void setDofsInfos(DistributedInfos< DegreeOfFreedom * > *dofs)
Set dofs infos in this class.
CepsMap< CepsString, ScalarSourceTerm * > Manager
Alias for manager inside.
Manager *const getManager() const
Get a map of all source terms.
ScalarSourceTerm * getSourceTerm(CepsString label) const
Get a source term, nullptr if not found.
CepsUInt getNbOfSourceTermsOf(CepsSourceTermFlag flag) const
Number of registered source terms of type flag.
CepsVector< ScalarSourceTerm * > asVector() const
Get a vector of all source terms.
Source term, essentially a ScalarField.
Definition: SourceTerm.hpp:50
void setFlag(const CepsSourceTermFlag &flag)
Set the source term flag.
void setAttributes(const CepsVector< CepsAttribute > &attributes)
Sets the attributes of the entity.
A namespace for all utility methods.
CepsString toKey(const CepsString &s)
Transform to key type a std::string, upper case and no spaces.
Definition: CepsString.cpp:157
CepsMap< CepsString, CepsVector< CepsString > > splitByKeyword(const CepsString &s, const CepsSet< CepsString > &tags)
Extract parameters in the CepsString given by tags. Builds a map of keywords and parameter words acco...
Definition: CepsString.cpp:384
_Type * getNew(_Args... args)
Allocates memory for one object. Be careful, the expansion of arguments may produce some weird result...
Definition: CepsMemory.hpp:170
constexpr auto toIntegral(_Enum e) -> typename std::underlying_type< _Enum >::type
Converts an enum type variable to an integer.
Definition: CepsEnums.hpp:42
CepsVector< _Tp > valuesOf(const CepsMap< _Key, _Tp, _Comp, _Alloc > &m)
Get the values of a map as a vector.
Definition: CepsMap.hpp:57
CepsInt toInt(const CepsString &s)
Cast CepsString to CepsInt.
Definition: CepsString.cpp:270
CepsBool isNumber(const CepsString &s)
Check if the string contains only digit numbers.
Definition: CepsString.cpp:322
void destroyObject(_Type &)
Destroy[delete] any type.
Definition: CepsMemory.hpp:116
CepsReal toReal(const CepsString &s)
Cast CepsString to CepsReal.
Definition: CepsString.cpp:193