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>
59 : method(other.method)
61 , independent_variables_symbols(other.independent_variables_symbols)
62 , dependent_variables_functions(other.dependent_variables_functions)
63 , dependent_variables_output(0)
64 , map_dep_expr_vec_entry(other.map_dep_expr_vec_entry)
65 , ready_for_value_extraction(false)
70 template <
typename ReturnType>
79 "Cannot call set_optimization_method() once the optimizer is finalized."));
81 # ifndef HAVE_SYMENGINE_LLVM
87 method = optimization_method;
88 flags = optimization_flags;
93 template <
typename ReturnType>
102 template <
typename ReturnType>
111 template <
typename ReturnType>
120 template <
typename ReturnType>
124 if (dependent_variables_output.size() > 0)
126 Assert(dependent_variables_output.size() ==
127 dependent_variables_functions.size(),
137 template <
typename ReturnType>
141 return ready_for_value_extraction;
146 template <
typename ReturnType>
151 Assert(optimized() ==
false,
153 "Cannot register symbols once the optimizer is finalized."));
161 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
162 ExcMessage(
"Key entry in map is not a symbol."));
173 template <
typename ReturnType>
184 template <
typename ReturnType>
189 Assert(optimized() ==
false,
191 "Cannot register symbols once the optimizer is finalized."));
193 for (
const auto &symbol : symbols)
195 Assert(independent_variables_symbols.find(symbol) ==
196 independent_variables_symbols.end(),
198 independent_variables_symbols.insert(
205 template <
typename ReturnType>
208 const SymEngine::vec_basic &symbols)
216 template <
typename ReturnType>
225 template <
typename ReturnType>
229 return independent_variables_symbols.size();
234 template <
typename ReturnType>
238 Assert(optimized() ==
false,
240 "Cannot register functions once the optimizer is finalized."));
242 register_scalar_function(
function);
247 template <
typename ReturnType>
252 Assert(optimized() ==
false,
254 "Cannot register functions once the optimizer is finalized."));
261 template <
typename ReturnType>
272 template <
typename ReturnType>
276 return dependent_variables_functions;
281 template <
typename ReturnType>
285 if (has_been_serialized ==
false)
289 Assert(map_dep_expr_vec_entry.size() ==
290 dependent_variables_functions.size(),
293 return dependent_variables_functions.size();
298 template <
typename ReturnType>
302 Assert(optimized() ==
false,
303 ExcMessage(
"Cannot call optimize() more than once."));
306 create_optimizer(optimizer);
323 dependent_variables_functions),
324 optimization_flags());
338 dependent_variables_functions),
339 optimization_flags());
341 # ifdef HAVE_SYMENGINE_LLVM
343 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
349 internal::LLVMOptimizer<ReturnType>>::
354 dependent_variables_functions),
355 optimization_flags());
370 dependent_variables_output.resize(n_dependent_variables());
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."));
463 Assert(substitution_values.size() == independent_variables_symbols.size(),
465 independent_variables_symbols.size()));
475 substitute(opt, dependent_variables_output, substitution_values);
485 substitute(opt, dependent_variables_output, substitution_values);
487 # ifdef HAVE_SYMENGINE_LLVM
489 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
495 internal::LLVMOptimizer<ReturnType>>
::
496 substitute(opt, dependent_variables_output, substitution_values);
504 ready_for_value_extraction =
true;
509 template <
typename ReturnType>
510 const std::vector<ReturnType> &
514 values_substituted() ==
true,
516 "The optimizer is not configured to perform evaluation. "
517 "This action can only performed after substitute() has been called."));
519 return dependent_variables_output;
524 template <
typename ReturnType>
529 values_substituted() ==
true,
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
542 it = map_dep_expr_vec_entry.find(func);
546 if (has_been_serialized && it == map_dep_expr_vec_entry.end())
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);
589 for (
const auto &
e : map_dep_expr_vec_entry)
592 serialize_and_deserialize_expression(
e.first);
601 map_dep_expr_vec_entry[func] =
e.second;
602 return evaluate(func);
609 "Still cannot find map entry, and there's no hope to recover from this situation."));
612 Assert(it != map_dep_expr_vec_entry.end(),
613 ExcMessage(
"Function has not been registered."));
616 return dependent_variables_output[it->second];
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)
630 out.emplace_back(evaluate(func));
637 template <
typename ReturnType>
642 return is_valid_nonunique_dependent_variable(func.
get_RCP());
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>
686 dependent_variables_output.size() == 0,
688 "Cannot register function as the optimizer has already been finalized."));
689 dependent_variables_output.reserve(n_dependent_variables() + 1);
690 const bool entry_registered =
691 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
693 if (entry_registered ==
true &&
694 is_valid_nonunique_dependent_variable(func) ==
false)
696 ExcMessage(
"Function has already been registered."));
698 if (entry_registered ==
false)
700 dependent_variables_functions.push_back(func);
701 map_dep_expr_vec_entry[func] =
702 dependent_variables_functions.size() - 1;
708 template <
typename ReturnType>
714 dependent_variables_output.size() == 0,
716 "Cannot register function as the optimizer has already been finalized."));
717 const std::size_t n_dependents_old = n_dependent_variables();
718 dependent_variables_output.reserve(n_dependents_old + funcs.size());
719 dependent_variables_functions.reserve(n_dependents_old + funcs.size());
721 for (
const auto &func : funcs)
723 const bool entry_registered =
724 (map_dep_expr_vec_entry.find(func) != map_dep_expr_vec_entry.end());
726 if (entry_registered ==
true &&
727 is_valid_nonunique_dependent_variable(func) ==
false)
729 ExcMessage(
"Function has already been registered."));
731 if (entry_registered ==
false)
733 dependent_variables_functions.push_back(func);
734 map_dep_expr_vec_entry[func] =
735 dependent_variables_functions.size() - 1;
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