Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
symengine_tensor_operations.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/config.h>
18 
19 #ifdef DEAL_II_WITH_SYMENGINE
20 
22 # include <deal.II/base/tensor.h>
23 # include <deal.II/base/utilities.h>
24 
25 # include <deal.II/differentiation/sd/symengine_product_types.h>
26 # include <deal.II/differentiation/sd/symengine_scalar_operations.h>
27 # include <deal.II/differentiation/sd/symengine_tensor_operations.h>
28 # include <deal.II/differentiation/sd/symengine_types.h>
29 # include <deal.II/differentiation/sd/symengine_utilities.h>
30 
31 DEAL_II_NAMESPACE_OPEN
32 
33 namespace Differentiation
34 {
35  namespace SD
36  {
37  // --- Symbolic variable creation ---
38 
39  namespace internal
40  {
41  namespace
42  {
43  template <int rank>
44  std::string
45  make_index_string(const TableIndices<rank> &indices)
46  {
47  std::string out;
48  for (unsigned int i = 0; i < rank; ++i)
49  out += ::Utilities::to_string(indices[i]);
50  return out;
51  }
52 
53 
54  template <int rank, int dim>
55  struct Symbol_Tensor;
56 
57 
58  template <int rank, int dim>
59  struct Symbol_Tensor
60  {
62  create(const std::string &sym)
63  {
65  for (unsigned int i = 0; i < out.n_independent_components; ++i)
66  {
67  const TableIndices<rank> indices(
69  out[indices] = Expression(sym + std::string("_") +
70  internal::make_index_string(indices));
71  }
72  return out;
73  }
74  };
75 
76 
77  template <int dim>
78  struct Symbol_Tensor<0, dim>
79  {
81  create(const std::string &sym)
82  {
83  return make_symbol(sym);
84  }
85  };
86 
87 
88  template <int rank, int dim>
89  struct Symbol_SymmetricTensor;
90 
91 
92  template <int rank, int dim>
93  struct Symbol_SymmetricTensor
94  {
96  create(const std::string &sym)
97  {
99  for (unsigned int i = 0; i < out.n_independent_components; ++i)
100  {
101  const TableIndices<rank> indices(
103  out[indices] = Expression(sym + std::string("_") +
104  internal::make_index_string(indices));
105  }
106  return out;
107  }
108  };
109 
110 
111  template <int dim>
112  struct Symbol_SymmetricTensor<4, dim>
113  {
115  create(const std::string &sym)
116  {
118  for (unsigned int i = 0;
119  i < SymmetricTensor<2, dim>::n_independent_components;
120  ++i)
121  for (unsigned int j = 0;
122  j < SymmetricTensor<2, dim>::n_independent_components;
123  ++j)
124  {
125  const TableIndices<4> indices =
126  make_rank_4_tensor_indices<dim>(i, j);
127  out[indices] =
128  Expression(sym + std::string("_") +
129  internal::make_index_string(indices));
130  }
131  return out;
132  }
133  };
134 
135 
136  template <int rank, int dim>
137  struct Symbol_Function_Tensor;
138 
139 
140  template <int rank, int dim>
141  struct Symbol_Function_Tensor
142  {
144  create(const std::string & sym,
145  const types::substitution_map &arguments)
146  {
148  const types::symbol_vector args =
149  Utilities::extract_symbols(arguments);
150  for (unsigned int i = 0; i < out.n_independent_components; ++i)
151  {
152  const TableIndices<rank> indices(
154  out[indices] =
155  Expression(sym + std::string("_") +
156  internal::make_index_string(indices),
157  args);
158  }
159  return out;
160  }
161  };
162 
163 
164  template <int dim>
165  struct Symbol_Function_Tensor<0, dim>
166  {
168  create(const std::string & sym,
169  const types::substitution_map &arguments)
170  {
171  return make_symbolic_function(sym, arguments);
172  }
173  };
174 
175 
176  template <int rank, int dim>
177  struct Symbol_Function_SymmetricTensor;
178 
179 
180  template <int rank, int dim>
181  struct Symbol_Function_SymmetricTensor
182  {
184  create(const std::string & sym,
185  const types::substitution_map &arguments)
186  {
188  const types::symbol_vector args =
189  Utilities::extract_symbols(arguments);
190  for (unsigned int i = 0; i < out.n_independent_components; ++i)
191  {
192  const TableIndices<rank> indices(
194  out[indices] =
195  Expression(sym + std::string("_") +
196  internal::make_index_string(indices),
197  args);
198  }
199  return out;
200  }
201  };
202 
203 
204  template <int dim>
205  struct Symbol_Function_SymmetricTensor<4, dim>
206  {
208  create(const std::string & sym,
209  const types::substitution_map &arguments)
210  {
212  const types::symbol_vector args =
213  Utilities::extract_symbols(arguments);
214  for (unsigned int i = 0;
215  i < SymmetricTensor<2, dim>::n_independent_components;
216  ++i)
217  for (unsigned int j = 0;
218  j < SymmetricTensor<2, dim>::n_independent_components;
219  ++j)
220  {
221  const TableIndices<4> indices =
222  make_rank_4_tensor_indices<dim>(i, j);
223  out[indices] =
224  Expression(sym + std::string("_") +
225  internal::make_index_string(indices),
226  args);
227  }
228  return out;
229  }
230  };
231  } // namespace
232  } // namespace internal
233 
234 
235  template <int dim>
237  make_vector_of_symbols(const std::string &sym)
238  {
239  return internal::Symbol_Tensor<1, dim>::create(sym);
240  }
241 
242 
243  template <int dim>
245  make_vector_of_symbolic_functions(const std::string & sym,
246  const types::substitution_map &arguments)
247  {
248  return internal::Symbol_Function_Tensor<1, dim>::create(sym, arguments);
249  }
250 
251 
252  template <int rank, int dim>
254  make_tensor_of_symbols(const std::string &sym)
255  {
256  return internal::Symbol_Tensor<rank, dim>::create(sym);
257  }
258 
259 
260  template <int rank, int dim>
262  make_symmetric_tensor_of_symbols(const std::string &sym)
263  {
264  return internal::Symbol_SymmetricTensor<rank, dim>::create(sym);
265  }
266 
267 
268  template <int rank, int dim>
270  make_tensor_of_symbolic_functions(const std::string & sym,
271  const types::substitution_map &arguments)
272  {
273  return internal::Symbol_Function_Tensor<rank, dim>::create(sym,
274  arguments);
275  }
276 
277 
278  template <int rank, int dim>
281  const std::string & sym,
282  const types::substitution_map &arguments)
283  {
284  return internal::Symbol_Function_SymmetricTensor<rank, dim>::create(
285  sym, arguments);
286  }
287 
288  } // namespace SD
289 } // namespace Differentiation
290 
291 /* --- Explicit instantiations --- */
292 
293 # include "symengine_tensor_operations.inst"
294 
295 DEAL_II_NAMESPACE_CLOSE
296 
297 #endif // DEAL_II_WITH_SYMENGINE
SymmetricTensor< rank, dim, Expression > make_symmetric_tensor_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
static constexpr unsigned int n_independent_components
Tensor< 1, dim, Expression > make_vector_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
Tensor< rank, dim, Expression > make_tensor_of_symbolic_functions(const std::string &symbol, const types::substitution_map &arguments)
Expression make_symbolic_function(const std::string &symbol, const types::symbol_vector &arguments)
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:392
static TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
static TableIndices< rank_ > unrolled_to_component_indices(const unsigned int i)
Definition: tensor.h:1416
Expression make_symbol(const std::string &symbol)
Definition: mpi.h:90
Tensor< 1, dim, Expression > make_vector_of_symbols(const std::string &symbol)
SymmetricTensor< rank, dim, Expression > make_symmetric_tensor_of_symbols(const std::string &symbol)
static constexpr unsigned int n_independent_components
Definition: tensor.h:419
Tensor< rank, dim, Expression > make_tensor_of_symbols(const std::string &symbol)