18 #ifdef DEAL_II_WITH_SYMENGINE 23 # include <boost/archive/text_iarchive.hpp> 24 # include <boost/archive/text_oarchive.hpp> 35 template <
typename ReturnType>
39 , ready_for_value_extraction(false)
40 , has_been_serialized(false)
45 template <
typename ReturnType>
56 template <
typename ReturnType>
70 template <
typename ReturnType>
79 "Cannot call set_optimization_method() once the optimizer is finalized."));
81 # ifndef HAVE_SYMENGINE_LLVM 93 template <
typename ReturnType>
102 template <
typename ReturnType>
111 template <
typename ReturnType>
120 template <
typename ReturnType>
137 template <
typename ReturnType>
146 template <
typename ReturnType>
153 "Cannot register symbols once the optimizer is finalized."));
158 for (
const auto &entry : substitution_map)
161 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
162 ExcMessage(
"Key entry in map is not a symbol."));
168 substitution_map.end());
173 template <
typename ReturnType>
184 template <
typename ReturnType>
191 "Cannot register symbols once the optimizer is finalized."));
193 for (
const auto &symbol : symbols)
205 template <
typename ReturnType>
208 const SymEngine::vec_basic &symbols)
216 template <
typename ReturnType>
225 template <
typename ReturnType>
234 template <
typename ReturnType>
240 "Cannot register functions once the optimizer is finalized."));
247 template <
typename ReturnType>
254 "Cannot register functions once the optimizer is finalized."));
261 template <
typename ReturnType>
272 template <
typename ReturnType>
281 template <
typename ReturnType>
298 template <
typename ReturnType>
303 ExcMessage(
"Cannot call optimize() more than once."));
341 # ifdef HAVE_SYMENGINE_LLVM 343 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
349 internal::LLVMOptimizer<ReturnType>>::
375 template <
typename ReturnType>
383 "The optimizer is not configured to perform substitution. " 384 "This action can only performed after optimize() has been called."));
394 Assert(symbol_sub_vec.size() == symbol_vec.size(),
396 for (
unsigned int i = 0; i < symbol_sub_vec.size(); ++i)
400 "The input substitution map is either incomplete, or does " 401 "not match that used in the register_symbols() call."));
407 const std::vector<ReturnType> values =
414 template <
typename ReturnType>
425 template <
typename ReturnType>
429 const std::vector<ReturnType> & values)
const 439 template <
typename ReturnType>
442 const SymEngine::vec_basic & symbols,
443 const std::vector<ReturnType> &values)
const 452 template <
typename ReturnType>
455 const std::vector<ReturnType> &substitution_values)
const 460 "The optimizer is not configured to perform substitution. " 461 "This action can only performed after optimize() has been called."));
487 # ifdef HAVE_SYMENGINE_LLVM 489 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
495 internal::LLVMOptimizer<ReturnType>>
:: 509 template <
typename ReturnType>
510 const std::vector<ReturnType> &
516 "The optimizer is not configured to perform evaluation. " 517 "This action can only performed after substitute() has been called."));
524 template <
typename ReturnType>
531 "The optimizer is not configured to perform evaluation. " 532 "This action can only performed after substitute() has been called."));
541 const typename map_dependent_expression_to_vector_entry_t::const_iterator
560 auto serialize_and_deserialize_expression =
562 std::ostringstream oss;
564 boost::archive::text_oarchive oa(oss,
565 boost::archive::no_header);
571 std::istringstream iss(oss.str());
572 boost::archive::text_iarchive ia(iss,
573 boost::archive::no_header);
582 serialize_and_deserialize_expression(func);
592 serialize_and_deserialize_expression(
e.first);
601 map_dep_expr_vec_entry[func] =
e.second;
609 "Still cannot find map entry, and there's no hope to recover from this situation."));
613 ExcMessage(
"Function has not been registered."));
621 template <
typename ReturnType>
622 std::vector<ReturnType>
624 const std::vector<Expression> &funcs)
const 626 std::vector<ReturnType> out;
627 out.reserve(funcs.size());
629 for (
const auto &func : funcs)
637 template <
typename ReturnType>
647 template <
typename ReturnType>
650 const SymEngine::RCP<const SymEngine::Basic> &func)
const 656 if (SymEngine::is_a<SymEngine::Constant>(*func))
662 if (&*func == &*SymEngine::minus_one)
664 if (&*func == &*SymEngine::I)
666 if (&*func == &*SymEngine::Inf)
668 if (&*func == &*SymEngine::NegInf)
670 if (&*func == &*SymEngine::ComplexInf)
672 if (&*func == &*SymEngine::Nan)
680 template <
typename ReturnType>
688 "Cannot register function as the optimizer has already been finalized."));
690 const bool entry_registered =
693 if (entry_registered ==
true &&
696 ExcMessage(
"Function has already been registered."));
698 if (entry_registered ==
false)
708 template <
typename ReturnType>
716 "Cannot register function as the optimizer has already been finalized."));
721 for (
const auto &func : funcs)
723 const bool entry_registered =
726 if (entry_registered ==
true &&
729 ExcMessage(
"Function has already been registered."));
731 if (entry_registered ==
false)
742 template <
typename ReturnType>
745 std::unique_ptr<SymEngine::Visitor> &
optimizer)
754 optimizer.reset(
new Optimizer_t());
760 optimizer.reset(
new Optimizer_t());
764 # ifdef HAVE_SYMENGINE_LLVM 765 if (internal::LLVMOptimizer<ReturnType>::supported_by_LLVM)
769 optimizer.reset(
new Optimizer_t());
790 # include "symengine_optimizer.inst" 795 #endif // DEAL_II_WITH_SYMENGINE enum OptimizationFlags flags
std::size_t n_dependent_variables() const
const std::vector< ReturnType > & evaluate() const
bool ready_for_value_extraction
constexpr bool values_are_equal(const Number1 &value_1, const Number2 &value_2)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
bool use_symbolic_CSE(const enum OptimizationFlags &flags)
SD::types::symbol_vector convert_basic_vector_to_expression_vector(const SymEngine::vec_basic &symbol_vector)
const types::symbol_vector & get_dependent_functions() const
void register_scalar_function(const SD::Expression &function)
static const types::blas_int one
static ::ExceptionBase & ExcNotInitialized()
#define AssertThrow(cond, exc)
enum OptimizationFlags optimization_flags() const
types::symbol_vector get_independent_symbols() const
const SymEngine::RCP< const SymEngine::Basic > & get_RCP() const
void register_functions(const types::symbol_vector &functions)
std::vector< ReturnType > dependent_variables_output
static ::ExceptionBase & ExcMessage(std::string arg1)
enum OptimizerType optimization_method() const
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void register_function(const Expression &function)
void substitute(const types::substitution_map &substitution_map) const
static ::ExceptionBase & ExcSymEngineLLVMReturnTypeNotSupported()
const SymEngine::Basic & get_value() const
#define DEAL_II_NAMESPACE_CLOSE
SymEngine::vec_basic convert_expression_vector_to_basic_vector(const SD::types::symbol_vector &symbol_vector)
bool use_symbolic_CSE() const
map_dependent_expression_to_vector_entry_t map_dep_expr_vec_entry
std::vector< SD::Expression > symbol_vector
SD::types::symbol_vector extract_symbols(const SD::types::substitution_map &substitution_values)
types::substitution_map independent_variables_symbols
types::substitution_map make_substitution_map(const Expression &symbol, const Expression &value)
static ::ExceptionBase & ExcSymEngineLLVMNotAvailable()
void register_vector_functions(const types::symbol_vector &functions)
void create_optimizer(std::unique_ptr< SymEngine::Visitor > &optimizer)
std::unique_ptr< SymEngine::Visitor > optimizer
#define DEAL_II_NAMESPACE_OPEN
bool is_valid_nonunique_dependent_variable(const SD::Expression &function) const
enum OptimizerType method
bool values_substituted() const
SD::types::substitution_map convert_basic_map_to_expression_map(const SymEngine::map_basic_basic &substitution_map)
void register_symbols(const types::substitution_map &substitution_map)
void set_optimization_method(const enum OptimizerType &optimization_method, const enum OptimizationFlags &optimization_flags=OptimizationFlags::optimize_all)
std::size_t n_independent_variables() const
static ::ExceptionBase & ExcNotImplemented()
static const types::blas_int zero
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
types::symbol_vector dependent_variables_functions
int(&) functions(const void *v1, const void *v2)
static ::ExceptionBase & ExcInternalError()