| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2019 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_differentiation_sd_symengine_utilities_h |
| 16 |
#define dealii_differentiation_sd_symengine_utilities_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#ifdef DEAL_II_WITH_SYMENGINE |
| 21 |
|
| 22 |
# include <deal.II/differentiation/sd/symengine_number_types.h> |
| 23 |
# include <deal.II/differentiation/sd/symengine_types.h> |
| 24 |
|
| 25 |
# include <symengine/basic.h> |
| 26 |
# include <symengine/dict.h> |
| 27 |
|
| 28 |
DEAL_II_NAMESPACE_OPEN |
| 29 |
|
| 30 |
namespace Differentiation |
| 31 |
{ |
| 32 |
namespace SD |
| 33 |
{ |
| 34 |
namespace Utilities |
| 35 |
{ |
| 36 |
/** |
| 37 |
* Convert a map of Expressions to its SymEngine counterpart. |
| 38 |
*/ |
| 39 |
SymEngine::map_basic_basic |
| 40 |
convert_expression_map_to_basic_map( |
| 41 |
const SD::types::substitution_map &substitution_map); |
| 42 |
|
| 43 |
/** |
| 44 |
* Convert to a map of Expressions from its SymEngine counterpart. |
| 45 |
*/ |
| 46 |
SD::types::substitution_map |
| 47 |
convert_basic_map_to_expression_map( |
| 48 |
const SymEngine::map_basic_basic &substitution_map); |
| 49 |
|
| 50 |
/** |
| 51 |
* Convert a vector of Expressions to its SymEngine counterpart. |
| 52 |
*/ |
| 53 |
SymEngine::vec_basic |
| 54 |
convert_expression_vector_to_basic_vector( |
| 55 |
const SD::types::symbol_vector &symbol_vector); |
| 56 |
|
| 57 |
/** |
| 58 |
* Convert to a vector of Expressions from its SymEngine counterpart. |
| 59 |
*/ |
| 60 |
SD::types::symbol_vector |
| 61 |
convert_basic_vector_to_expression_vector( |
| 62 |
const SymEngine::vec_basic &symbol_vector); |
| 63 |
|
| 64 |
/** |
| 65 |
* Convert to a vector of pairs of Expressions from its SymEngine |
| 66 |
* counterpart. |
| 67 |
*/ |
| 68 |
std::vector<std::pair<Expression, Expression>> |
| 69 |
convert_basic_pair_vector_to_expression_pair_vector( |
| 70 |
const SymEngine::vec_pair &symbol_value_vector); |
| 71 |
|
| 72 |
/** |
| 73 |
* Extract the symbols (key entries) from a substitution map. |
| 74 |
* |
| 75 |
* @note It is guaranteed that the order of extraction of data into the |
| 76 |
* output vector is the same as that for extract_values(). |
| 77 |
* That is to say that the unzipped key and value pairs as given by |
| 78 |
* extract_symbols() and extract_values() always have a 1:1 |
| 79 |
* correspondence. |
| 80 |
*/ |
| 81 |
SD::types::symbol_vector |
| 82 |
extract_symbols(const SD::types::substitution_map &substitution_values); |
| 83 |
|
| 84 |
/** |
| 85 |
* Extract the values from a substitution map. |
| 86 |
* The value entries will be converted into the @p NumberType given |
| 87 |
* as a template parameter to this function via the @p ExpressionType. |
| 88 |
* |
| 89 |
* @note It is guaranteed that the order of extraction of data into the |
| 90 |
* output vector is the same as that for extract_symbols(). |
| 91 |
* That is to say that the unzipped key and value pairs as given by |
| 92 |
* extract_symbols() and extract_values() always have a 1:1 |
| 93 |
* correspondence. |
| 94 |
*/ |
| 95 |
template <typename NumberType, typename ExpressionType = SD::Expression> |
| 96 |
std::vector<NumberType> |
| 97 |
extract_values(const SD::types::substitution_map &substitution_values); |
| 98 |
|
| 99 |
/** |
| 100 |
* Print the key and value pairs stored in a substitution map. |
| 101 |
*/ |
| 102 |
template <typename StreamType> |
| 103 |
StreamType & |
| 104 |
print_substitution_map( |
| 105 |
StreamType &stream, |
| 106 |
const SD::types::substitution_map &symbol_value_map); |
| 107 |
|
| 108 |
} // namespace Utilities |
| 109 |
|
| 110 |
} // namespace SD |
| 111 |
} // namespace Differentiation |
| 112 |
|
| 113 |
|
| 114 |
/* -------------------- inline and template functions ------------------ */ |
| 115 |
|
| 116 |
|
| 117 |
# ifndef DOXYGEN |
| 118 |
|
| 119 |
|
| 120 |
namespace Differentiation |
| 121 |
{ |
| 122 |
namespace SD |
| 123 |
{ |
| 124 |
namespace Utilities |
| 125 |
{ |
| 126 |
template <typename NumberType, typename ExpressionType> |
| 127 |
std::vector<NumberType> |
| 128 |
extract_values(const SD::types::substitution_map &substitution_values) |
| 129 |
{ |
| 130 |
std::vector<NumberType> values; |
| 131 |
values.reserve(substitution_values.size()); |
| 132 |
|
| 133 |
for (const auto &entry : substitution_values) |
| 134 |
values.push_back( |
| 135 |
static_cast<NumberType>(ExpressionType(entry.second))); |
| 136 |
|
| 137 |
return values; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
template <typename StreamType> |
| 142 |
StreamType & |
| 143 |
print_substitution_map( |
| 144 |
StreamType &stream, |
| 145 |
const SD::types::substitution_map &symbol_value_map) |
| 146 |
{ |
| 147 |
for (const auto &entry : symbol_value_map) |
| 148 |
stream << entry.first << " = " << entry.second << '\n'; |
| 149 |
|
| 150 |
stream << std::flush; |
| 151 |
return stream; |
| 152 |
} |
| 153 |
|
| 154 |
} // namespace Utilities |
| 155 |
|
| 156 |
} // namespace SD |
| 157 |
} // namespace Differentiation |
| 158 |
|
| 159 |
|
| 160 |
# endif // DOXYGEN |
| 161 |
|
| 162 |
|
| 163 |
DEAL_II_NAMESPACE_CLOSE |
| 164 |
|
| 165 |
#else |
| 166 |
|
| 167 |
// Make sure the scripts that create the C++20 module input files have |
| 168 |
// something to latch on if the preprocessor #ifdef above would |
| 169 |
// otherwise lead to an empty content of the file. |
| 170 |
DEAL_II_NAMESPACE_OPEN |
| 171 |
DEAL_II_NAMESPACE_CLOSE |
| 172 |
|
| 173 |
#endif // DEAL_II_WITH_SYMENGINE |
| 174 |
|
| 175 |
#endif // dealii_differentiation_sd_symengine_utilities_h |