Documentation of CSL
space.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef SPACE_H_INCLUDED
24 #define SPACE_H_INCLUDED
25 
26 #include "literal.h"
27 #include "numerical.h"
28 #include "indicial.h"
29 #include "vector.h"
30 
31 namespace csl {
32 
33 template<typename T, typename G>
34 typename std::vector<std::pair<T,G>>::iterator findFirstInPair(
35  std::vector<std::pair<T,G>>& v, const T& element)
36 {
37  typename std::vector<std::pair<T,G>>::iterator
38  iter = v.begin();
39  while (iter != v.end() and (*iter).first != element)
40  ++iter;
41 
42  return iter;
43 }
44 
45 template<typename T, typename G>
46 typename std::vector<std::pair<T,G>>::iterator findSecondInPair(
47  std::vector<std::pair<T,G>>& v, const G& element)
48 {
49  typename std::vector<std::pair<T,G>>::iterator
50  iter = v.begin();
51  while (iter != v.end() and (*iter).second != element)
52  ++iter;
53 
54  return iter;
55 }
56 
57 
64 class Space{
65 
66  public:
67 
68  mutable bool keepCycles = false;
69 
70  static bool applyMetric;
71 
72  protected:
73 
77  std::string name;
78 
82  int dim;
83 
90 
91  protected:
92 
93  mutable
94  std::map<char, std::string> nameIndices;
95 
96  mutable
97  std::map<std::string, char> specIndices;
98 
99  mutable
100  std::vector<std::pair<std::string, Index::ID_type>> availableIndices;
101 
102  mutable
103  std::vector<std::pair<std::string, unsigned short>> availableVectors;
104 
105  mutable
106  std::vector<std::string>::const_iterator defaultName;
107 
108  std::vector<std::string> defaultIndexNames;
109 
120  mutable
122 
134  mutable
136 
137  mutable
138  Tensor inverseMetric;
139 
140  mutable
141  Tensor epsilon;
142 
143  public:
144 
152  Space(const std::string& t_name,
153  int t_dim,
154  std::vector<std::string> const& indexNames
155  = {"i", "j", "k", "l"});
156 
171  Space(const std::string& t_name,
172  int t_dim,
173  const std::string& name_metric,
174  const Expr& t_metric,
175  std::vector<std::string> const& indexNames
176  = {"i", "j", "k", "l"});
177 
181  virtual ~Space();
182 
183  virtual
184  void printCode(
185  std::ostream &out,
186  int indentSize
187  ) const;
188 
189  virtual
190  void printDefinition(
191  std::ostream &out,
192  int indentSize,
193  bool header = false
194  ) const;
195 
199  virtual std::string getName() const;
200 
201  inline std::string getNextIndexName() const;
202 
203  inline std::string getIndexName(char spec) const;
204 
205  inline std::string_view getIndexNameView(char spec) const;
206 
207  inline char getSpecFromIndexName(std::string const& t_name) const;
208 
212  inline int getDim() const;
213 
218  inline bool getSignedIndex() const;
219 
220  virtual
221  bool hasSpecialTraceProperty(const csl::vector_expr& tensors) const;
222 
223  csl::vector_expr getSignature() const;
224 
225  Tensor getMetric() const;
226 
227  Tensor getInverseMetric() const;
228 
229  Tensor getDelta() const;
230 
231  Tensor getEpsilon() const;
232 
233  Expr applyMetricOnTensor(Expr const& tensor,
234  size_t axis,
235  bool covariant) const;
236 
245  inline Index generateIndex(const std::string& name) const;
246 
255  inline Index generateIndex() const;
256 
257  inline std::vector<Index> generateIndices(size_t N) const;
258  inline std::vector<Index> generateIndices(
259  size_t N,
260  const std::string& name
261  ) const;
262 
263  inline Index generateSimilar(const Index& model) const;
264 
265  inline void resetIndexNumber() const;
266 
267  inline void refreshIndexName(Index& index) const;
268 
278  Expr generateVector(const std::string& t_name) const;
279 
280  std::string generateVectorName(const std::string& t_name) const;
281 
282  void addIndexNames(const std::string& name) const;
283 
284  void addVectorNames(const std::string& name) const;
285 
286  void addIndexNames(const std::vector<std::string>& names) const;
287 
288  void addVectorNames(const std::vector<std::string>& names) const;
289 
290  virtual
291  Expr calculateTrace(csl::vector_expr tensors) const;
292 
293  private:
294 
295  void buildEpsilon();
296 
297  inline Index::ID_type getID(std::string_view name) const;
298 
299  std::string getProperVectorName(const std::string& initialName) const;
300 };
301 
302 std::string Space::getIndexName(char spec) const
303 {
304  return nameIndices[spec];
305 }
306 
307 std::string_view Space::getIndexNameView(char spec) const
308 {
309  return nameIndices[spec];
310 }
311 
312 char Space::getSpecFromIndexName(std::string const& t_name) const
313 {
314  if (specIndices.find(t_name) == specIndices.end()) {
315  constexpr char maxChar = std::numeric_limits<char>::max();
316  char c = 1;
317  while (true) {
318  if (nameIndices.find(c) == nameIndices.end()) {
319  nameIndices[c] = t_name;
320  specIndices[t_name] = c;
321  return c;
322  }
323  if (c == maxChar)
324  break;
325  ++c;
326  }
327  }
328  return specIndices[t_name];
329 }
330 
331 int Space::getDim() const
332 {
333  return dim;
334 }
335 
337 {
338  return signedIndex;
339 }
340 
341 void Space::resetIndexNumber() const
342 {
343  availableIndices.clear();
344  defaultName = defaultIndexNames.begin();
345 }
346 
347 void Space::refreshIndexName(Index& index) const
348 {
349  index.setID(getID(index.getName()));
350 }
351 
352 Index::ID_type Space::getID(std::string_view t_name) const
353 {
354  for (size_t i = 0; i != availableIndices.size(); ++i)
355  if (availableIndices[i].first == t_name) {
356  return ++availableIndices[i].second;
357  }
358  availableIndices.push_back(make_pair(std::string(t_name), 0));
359  return 0;
360 }
361 
362 Index Space::generateSimilar(const Index& model) const {
363  Index newIndex = model;
364  refreshIndexName(newIndex);
365  return newIndex;
366 }
367 
368 Index Space::generateIndex(const std::string& t_name) const {
369  return Index(t_name,this,getID(t_name));
370 }
371 
372 std::string Space::getNextIndexName() const
373 {
374  auto iter = defaultName;
375  ++defaultName;
376  if (defaultName == defaultIndexNames.end())
377  defaultName = defaultIndexNames.begin();
378  return *iter;
379 }
380 
382  return generateIndex(getNextIndexName());
383 }
384 
385 std::vector<Index> Space::generateIndices(size_t N) const
386 {
387  std::vector<Index> indices(N);
388  for (size_t i = 0; i != N; ++i)
389  indices[i] = generateIndex();
390 
391  return indices;
392 }
393 
394 std::vector<Index> Space::generateIndices(
395  size_t N,
396  const std::string& name
397  ) const
398 {
399  std::vector<Index> indices(N);
400  for (size_t i = 0; i != N; ++i)
401  indices[i] = generateIndex(name);
402 
403  return indices;
404 }
405 
406 inline Expr DMinko = csl::constant_s("D", csl::int_s(4));
407 
408 inline const Space& buildMinkowski()
409 {
410  static const Space Minkowski(
411  "Minko",
412  4,
413  "g",
414  matrix_s({{CSL_1,CSL_0,CSL_0,CSL_0},
415  {CSL_0,CSL_M_1,CSL_0,CSL_0},
416  {CSL_0,CSL_0,CSL_M_1,CSL_0},
417  {CSL_0,CSL_0,CSL_0,CSL_M_1}}),
418  {"\\mu", "\\nu", "\\rho", "\\sigma", "\\lambda", "\\tau"});
419  Minkowski.getDelta()->setTrace(DMinko);
420  Minkowski.getMetric()->setTrace(DMinko);
421  return Minkowski;
422 }
423 
426 
427 void fillEpsilonTensor(Expr& tensor,
428  int dim);
429 
433 inline const Space Euclid_R2("R2", 2);
434 
438 inline const Space Euclid_R3("R3", 3);
439 
443 inline const Space Euclid_R4("R4", 4);
444 
448 inline const Space &Minkowski = buildMinkowski();
449 
450 } // End of namespace csl
451 
452 #endif
Namespace for csl library.
Definition: abreviation.h:34
int dim
Dimension of the space.
Definition: space.h:82
Index object that is used for indicial objects.
Definition: index.h:75
Space(const std::string &t_name, int t_dim, std::vector< std::string > const &indexNames={"i", "j", "k", "l"})
Initializes the name and the dimension of the space, no metric so only the kronecker delta is relevan...
Definition: space.cpp:27
Tensor delta
Kronecker delta, public attribute for readability in usage. Element i,j of the tensor is then accessi...
Definition: space.h:121
virtual std::string getName() const
Definition: space.cpp:162
const Space Euclid_R4("R4", 4)
Space .
int getDim() const
Definition: space.h:331
bool getSignedIndex() const
Definition: space.h:336
bool signedIndex
Property of indices: if true, it means that the metric is non trivial (given by the user at initializ...
Definition: space.h:89
Vector space that has a name, a dimension, a delta tensor and possibly a non-trivial metric...
Definition: space.h:64
Objects handling vectors, matrices and higher-dimensional tensors.
Expr generateVector(const std::string &t_name) const
Generates and returns a Vector in the right Space (itself) of name name, filled with Variable objects...
Definition: space.cpp:237
const Space Euclid_R2("R2", 2)
Space .
Index generateIndex() const
Generates and returns an index in the right Space (itself) of name defined by the program...
Definition: space.h:381
Tensor metric
Public attribute for readability in usage. Element i,j of the tensor is then accessible by space...
Definition: space.h:135
std::string name
Name of the vector space.
Definition: space.h:77
Objects handling indexed expressions in order to perform indicial tensor calculations.
virtual ~Space()
Destructor.
Definition: space.cpp:80
const Space Euclid_R3("R3", 3)
Space .
const Space & Minkowski
Space with a metric g = diag(-1,1,1,1).
Definition: space.h:448
Definition: indicial.h:675
Expression type/.
Definition: abstract.h:1573