Line data Source code
1 0 : // Distributed under the MIT License. 2 : // See LICENSE.txt for details. 3 : 4 : #pragma once 5 : 6 : #include <array> 7 : #include <cstddef> 8 : #include <limits> 9 : #include <memory> 10 : #include <ostream> 11 : #include <pup.h> 12 : #include <vector> 13 : 14 : #include "DataStructures/DataVector.hpp" // IWYU pragma: keep 15 : #include "Domain/FunctionsOfTime/FunctionOfTime.hpp" 16 : #include "Domain/FunctionsOfTime/FunctionOfTimeHelpers.hpp" 17 : #include "Utilities/Serialization/CharmPupable.hpp" 18 : 19 : namespace domain { 20 : namespace FunctionsOfTime { 21 : /// \ingroup ComputationalDomainGroup 22 : /// \brief A function that has a piecewise-constant `MaxDeriv`th derivative. 23 : template <size_t MaxDeriv> 24 1 : class PiecewisePolynomial : public FunctionOfTime { 25 : public: 26 0 : PiecewisePolynomial() = default; 27 0 : PiecewisePolynomial( 28 : double t, std::array<DataVector, MaxDeriv + 1> initial_func_and_derivs, 29 : double expiration_time); 30 : 31 0 : ~PiecewisePolynomial() override = default; 32 0 : PiecewisePolynomial(PiecewisePolynomial&&) = default; 33 0 : PiecewisePolynomial& operator=(PiecewisePolynomial&&) = default; 34 0 : PiecewisePolynomial(const PiecewisePolynomial&) = default; 35 0 : PiecewisePolynomial& operator=(const PiecewisePolynomial&) = default; 36 : 37 0 : explicit PiecewisePolynomial(CkMigrateMessage* /*unused*/) {} 38 : 39 0 : auto get_clone() const -> std::unique_ptr<FunctionOfTime> override; 40 : 41 : // clang-tidy: google-runtime-references 42 : // clang-tidy: cppcoreguidelines-owning-memory,-warnings-as-errors 43 0 : WRAPPED_PUPable_decl_template(PiecewisePolynomial<MaxDeriv>); // NOLINT 44 : 45 : /// Returns the function at an arbitrary time `t`. If `MaxDeriv` is 46 : /// 0 and `update` has been called for time `t`, the updated value 47 : /// is ignored. 48 1 : std::array<DataVector, 1> func(double t) const override { 49 : return func_and_derivs<0>(t); 50 : } 51 : /// Returns the function and its first derivative at an arbitrary time `t`. 52 : /// If `MaxDeriv` is 1 and `update` has been called for time `t`, the updated 53 : /// value is ignored. 54 1 : std::array<DataVector, 2> func_and_deriv(double t) const override { 55 : return func_and_derivs<1>(t); 56 : } 57 : /// Returns the function and the first two derivatives at an arbitrary time 58 : /// `t`. If `MaxDeriv` is 2 and `update` has been called for time `t`, the 59 : /// updated value is ignored. 60 1 : std::array<DataVector, 3> func_and_2_derivs(double t) const override { 61 : return func_and_derivs<2>(t); 62 : } 63 : 64 : /// Updates the `MaxDeriv`th derivative of the function at the given time. 65 : /// `updated_max_deriv` is a vector of the `MaxDeriv`ths for each component. 66 : /// `next_expiration_time` is the next expiration time. 67 1 : void update(double time_of_update, DataVector updated_max_deriv, 68 : double next_expiration_time) override; 69 : 70 : /// Resets the expiration time to a later time. 71 1 : void reset_expiration_time(double next_expiration_time) override; 72 : 73 : /// Returns the domain of validity of the function, 74 : /// including the extrapolation region. 75 1 : std::array<double, 2> time_bounds() const override { 76 : return {{deriv_info_at_update_times_.front().time, expiration_time_}}; 77 : } 78 : 79 : /// Return a const reference to the stored deriv info so external classes can 80 : /// read the stored times and derivatives (mostly for 81 : /// QuaternionFunctionOfTime). 82 : const std::vector<FunctionOfTimeHelpers::StoredInfo<MaxDeriv + 1>>& 83 1 : get_deriv_info() const { 84 : return deriv_info_at_update_times_; 85 : } 86 : 87 : // NOLINTNEXTLINE(google-runtime-references) 88 0 : void pup(PUP::er& p) override; 89 : 90 : private: 91 : template <size_t LocalMaxDeriv> 92 0 : friend bool operator==( // NOLINT(readability-redundant-declaration) 93 : const PiecewisePolynomial<LocalMaxDeriv>& lhs, 94 : const PiecewisePolynomial<LocalMaxDeriv>& rhs); 95 : 96 : template <size_t LocalMaxDeriv> 97 0 : friend std::ostream& operator<<( // NOLINT(readability-redundant-declaration 98 : std::ostream& os, 99 : const PiecewisePolynomial<LocalMaxDeriv>& piecewise_polynomial); 100 : 101 : /// Returns the function and `MaxDerivReturned` derivatives at 102 : /// an arbitrary time `t`. 103 : /// The function has multiple components. 104 : template <size_t MaxDerivReturned = MaxDeriv> 105 1 : std::array<DataVector, MaxDerivReturned + 1> func_and_derivs(double t) const; 106 : 107 : // There exists a DataVector for each deriv order that contains 108 : // the values of that deriv order for all components. 109 0 : using value_type = std::array<DataVector, MaxDeriv + 1>; 110 : 111 : std::vector<FunctionOfTimeHelpers::StoredInfo<MaxDeriv + 1>> 112 0 : deriv_info_at_update_times_; 113 0 : double expiration_time_{std::numeric_limits<double>::lowest()}; 114 : }; 115 : 116 : template <size_t MaxDeriv> 117 0 : bool operator!=(const PiecewisePolynomial<MaxDeriv>& lhs, 118 : const PiecewisePolynomial<MaxDeriv>& rhs); 119 : 120 : /// \cond 121 : template <size_t MaxDeriv> 122 : PUP::able::PUP_ID PiecewisePolynomial<MaxDeriv>::my_PUP_ID = 0; // NOLINT 123 : /// \endcond 124 : } // namespace FunctionsOfTime 125 : } // namespace domain