]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Merge pull request #8008 from jppelteret/sd-map_utilities_04
authorJean-Paul Pelteret <jppelteret@gmail.com>
Sat, 11 May 2019 15:37:04 +0000 (17:37 +0200)
committerGitHub <noreply@github.com>
Sat, 11 May 2019 15:37:04 +0000 (17:37 +0200)
SD scalar and tensor operations: Make, add to, and set value in symbol map

1  2 
include/deal.II/differentiation/sd/symengine_scalar_operations.h
source/differentiation/sd/symengine_scalar_operations.cc

index 692273a65eb7abbd851a4aca450ebbd9ed3eed86,ab8029fa390d5b2d512f9591c05ae8a85f25d03c..3a3334adc6c3e0bedd77a70160511a5c5d56a5b5
@@@ -768,12 -1109,193 +1193,193 @@@ namespace Differentiatio
  {
    namespace SD
    {
-     /* ---------------- Symbolic substitution map creation --------------*/
+     /* ---------------- Symbol map creation and manipulation --------------*/
+     template <bool ignore_invalid_symbols,
+               typename ValueType,
+               typename SymbolicType>
+     types::substitution_map
+     make_symbol_map(const SymbolicType &symbol)
+     {
+       types::substitution_map symbol_map;
+       add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
+       return symbol_map;
+     }
+     template <bool ignore_invalid_symbols,
+               typename ValueType,
+               typename SymbolicType,
+               typename... Args>
+     types::substitution_map
+     make_symbol_map(const SymbolicType &symbol, const Args &... other_symbols)
+     {
+       types::substitution_map symbol_map;
+       add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
+                                                            symbol,
+                                                            other_symbols...);
+       return symbol_map;
+     }
+     template <bool ignore_invalid_symbols, typename ValueType>
+     void
+     add_to_symbol_map(types::substitution_map &symbol_map,
+                       const Expression &       symbol)
+     {
+       // Call the above function
+       add_to_substitution_map<ignore_invalid_symbols>(
+         symbol_map,
+         symbol,
+         dealii::internal::NumberType<ValueType>::value(0.0));
+     }
+     template <bool ignore_invalid_symbols,
+               typename ValueType,
+               typename SymbolicType,
+               typename>
+     void
+     add_to_symbol_map(types::substitution_map &symbol_map,
+                       const SymbolicType &     symbol)
+     {
+       // Call the above function
+       using SE_RCP_Basic = const SymEngine::RCP<const SymEngine::Basic> &;
+       add_to_substitution_map<ignore_invalid_symbols>(
+         symbol_map,
+         static_cast<SE_RCP_Basic>(symbol),
+         dealii::internal::NumberType<ValueType>::value(0.0));
+     }
+     template <bool ignore_invalid_symbols,
+               typename ValueType,
+               typename SymbolicType>
+     void
+     add_to_symbol_map(types::substitution_map &        symbol_map,
+                       const std::vector<SymbolicType> &symbols)
+     {
+       for (const auto &symbol : symbols)
+         add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
+                                                              symbol);
+     }
+     template <bool ignore_invalid_symbols, typename ValueType>
+     void
+     add_to_symbol_map(types::substitution_map &      symbol_map,
+                       const types::substitution_map &other_symbols)
+     {
+       // We should be cautious as to whether or not the user has
+       // hand-made the other_symbols map to be "merged" in.
+       // So instead of blindly merging using the merge_substitution_maps()
+       // function, we do it by hand and check for invalid symbols
+       // if required.
+       for (types::substitution_map::const_iterator it = other_symbols.begin();
+            it != other_symbols.end();
+            ++it)
+         {
+           Assert(symbol_map.find(it->first) == symbol_map.end(),
+                  ExcMessage("Entry already exists in symbol map"));
+           add_to_symbol_map<ignore_invalid_symbols, ValueType>(
+             symbol_map, Expression(it->first));
+         }
+     }
+     template <bool ignore_invalid_symbols,
+               typename ValueType,
+               typename SymbolicType,
+               typename... Args>
+     void
+     add_to_symbol_map(types::substitution_map &symbol_map,
+                       const SymbolicType &     symbol,
+                       const Args &... other_symbols)
+     {
+       add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
+       add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
+                                                            other_symbols...);
+     }
+     template <typename ExpressionType, typename ValueType, typename>
+     void
+     set_value_in_symbol_map(types::substitution_map &substitution_map,
+                             const ExpressionType &   symbol,
+                             const ValueType &        value)
+     {
+       // Call the above function
+       using SE_RCP_Basic = const SymEngine::RCP<const SymEngine::Basic> &;
+       internal::set_value_in_symbol_map(substitution_map,
+                                         static_cast<SE_RCP_Basic>(symbol),
+                                         static_cast<SE_RCP_Basic>(
+                                           ExpressionType(value)));
+     }
+     template <typename SymbolicType, typename ValueType>
+     void
+     set_value_in_symbol_map(types::substitution_map &        substitution_map,
+                             const std::vector<SymbolicType> &symbols,
+                             const std::vector<ValueType> &   values)
+     {
+       Assert(symbols.size() == values.size(),
+              ExcDimensionMismatch(symbols.size(), values.size()));
+       typename std::vector<SymbolicType>::const_iterator it_symb =
+         symbols.begin();
+       typename std::vector<ValueType>::const_iterator it_val = values.begin();
+       for (; it_symb != symbols.end(); ++it_symb, ++it_val)
+         {
+           Assert(it_val != values.end(), ExcInternalError());
+           set_value_in_symbol_map(substitution_map, *it_symb, *it_val);
+         }
+     }
+     template <typename SymbolicType, typename ValueType>
+     void
+     set_value_in_symbol_map(
+       types::substitution_map &                 substitution_map,
+       const std::pair<SymbolicType, ValueType> &symbol_value)
+     {
+       set_value_in_symbol_map(substitution_map,
+                               symbol_value.first,
+                               symbol_value.second);
+     }
+     template <typename SymbolicType, typename ValueType, typename... Args>
+     void
+     set_value_in_symbol_map(
+       types::substitution_map &                 substitution_map,
+       const std::pair<SymbolicType, ValueType> &symbol_value,
+       const Args &... other_symbol_values)
+     {
+       set_value_in_symbol_map(substitution_map, symbol_value);
+       set_value_in_symbol_map(substitution_map, other_symbol_values...);
+     }
+     template <typename SymbolicType, typename ValueType>
+     void
+     set_value_in_symbol_map(
+       types::substitution_map &                              substitution_map,
+       const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values)
+     {
+       // Call the above function
+       for (const auto &entry : symbol_values)
+         set_value_in_symbol_map(substitution_map, entry.first, entry.second);
+     }
+     /* ------------------ Symbol substitution map creation ----------------*/
  
  
 -    template <typename ValueType>
 +    template <typename ExpressionType, typename ValueType, typename>
      types::substitution_map
 -    make_substitution_map(const Expression &symbol, const ValueType &value)
 +    make_substitution_map(const ExpressionType &symbol, const ValueType &value)
      {
        types::substitution_map substitution_map;
        add_to_substitution_map(substitution_map, symbol, value);

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.