Files
pip/libs/main/math/pimathsolver.h
2026-03-12 14:46:57 +03:00

158 lines
9.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! \~\file pimathsolver.h
//! \~\ingroup Math
//! \~\brief
//! \~english Transfer-function-based differential solver
//! \~russian Решатель дифференциальной модели на основе передаточной функции
//!
//! \~\details
//! \~english
//! Declares a solver that builds an internal state representation from a
//! transfer function and advances it with one of several numerical methods.
//! \~russian
//! Объявляет решатель, который строит внутреннее представление состояния из
//! передаточной функции и продвигает его одним из нескольких численных методов.
/*
PIP - Platform Independent Primitives
PIMathSolver
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIMATHSOLVER_H
#define PIMATHSOLVER_H
#include "pimathmatrix.h"
//! \~\ingroup Math
//! \~\brief
//! \~english Transfer-function coefficient storage.
//! \~russian Хранилище коэффициентов передаточной функции.
struct PIP_EXPORT TransferFunction {
//! \~english Numerator coefficients in the order expected by \a fromTF().
//! \~russian Коэффициенты числителя в порядке, ожидаемом \a fromTF().
PIVector<double> vector_Bm;
//! \~english Denominator coefficients in the order expected by \a fromTF().
//! \~russian Коэффициенты знаменателя в порядке, ожидаемом \a fromTF().
PIVector<double> vector_An;
};
//! \~\ingroup Math
//! \~\brief
//! \~english Numerical solver for transfer-function models.
//! \~russian Численный решатель для моделей в виде передаточной функции.
class PIP_EXPORT PIMathSolver {
public:
//! \~english Integration method selector.
//! \~russian Выбор метода интегрирования.
enum Method {
Global = -1 /** \~english Use the global default method \~russian Использовать глобальный метод по умолчанию */,
Eyler_1 = 01 /** \~english First-order Euler method \~russian Метод Эйлера первого порядка */,
Eyler_2 = 02 /** \~english Second-order Euler method \~russian Метод Эйлера второго порядка */,
EylerKoshi = 03 /** \~english Euler-Cauchy method identifier \~russian Идентификатор метода Эйлера-Коши */,
RungeKutta_4 = 14 /** \~english Fourth-order Runge-Kutta method \~russian Метод Рунге-Кутты четвертого порядка */,
AdamsBashfortMoulton_2 =
22 /** \~english Second-order Adams-Bashforth-Moulton method \~russian Метод Адамса-Башфорта-Моултона второго порядка */,
AdamsBashfortMoulton_3 =
23 /** \~english Third-order Adams-Bashforth-Moulton method \~russian Метод Адамса-Башфорта-Моултона третьего порядка */,
AdamsBashfortMoulton_4 =
24 /** \~english Fourth-order Adams-Bashforth-Moulton method \~russian Метод Адамса-Башфорта-Моултона четвертого порядка */,
PolynomialApproximation_2 =
32 /** \~english Polynomial approximation of degree 2 \~russian Полиномиальная аппроксимация степени 2 */,
PolynomialApproximation_3 =
33 /** \~english Polynomial approximation of degree 3 \~russian Полиномиальная аппроксимация степени 3 */,
PolynomialApproximation_4 =
34 /** \~english Polynomial approximation of degree 4 \~russian Полиномиальная аппроксимация степени 4 */,
PolynomialApproximation_5 =
35 /** \~english Polynomial approximation of degree 5 \~russian Полиномиальная аппроксимация степени 5 */
};
//! \~english Constructs an empty solver.
//! \~russian Создает пустой решатель.
PIMathSolver();
//! \~english Performs one solver step for input \a u and step \a h.
//! \~russian Выполняет один шаг решателя для входа \a u и шага \a h.
void solve(double u, double h);
//! \~english Initializes the internal model from a transfer function.
//! \~russian Инициализирует внутреннюю модель из передаточной функции.
void fromTF(const TransferFunction & TF);
//! \~english Sets the method used by \a solve().
//! \~russian Устанавливает метод, используемый \a solve().
void setMethod(Method m) { method = m; }
//! \~english Updates stored time history used by polynomial methods.
//! \~russian Обновляет историю времени, используемую полиномиальными методами.
void setTime(double time);
//! \~english Performs one step with the first-order Euler method.
//! \~russian Выполняет один шаг методом Эйлера первого порядка.
void solveEyler1(double u, double h);
//! \~english Performs one step with the second-order Euler method.
//! \~russian Выполняет один шаг методом Эйлера второго порядка.
void solveEyler2(double u, double h);
//! \~english Performs one step with the fourth-order Runge-Kutta method.
//! \~russian Выполняет один шаг методом Рунге-Кутты четвертого порядка.
void solveRK4(double u, double h);
//! \~english Performs one step with the second-order Adams-Bashforth-Moulton method.
//! \~russian Выполняет один шаг методом Адамса-Башфорта-Моултона второго порядка.
void solveABM2(double u, double h);
//! \~english Performs one step with the third-order Adams-Bashforth-Moulton method.
//! \~russian Выполняет один шаг методом Адамса-Башфорта-Моултона третьего порядка.
void solveABM3(double u, double h);
//! \~english Performs one step with the fourth-order Adams-Bashforth-Moulton method.
//! \~russian Выполняет один шаг методом Адамса-Башфорта-Моултона четвертого порядка.
void solveABM4(double u, double h);
//! \~english Performs one step with a polynomial approximation of degree \a deg.
//! \~russian Выполняет один шаг с полиномиальной аппроксимацией степени \a deg.
void solvePA(double u, double h, uint deg);
//! \~english Performs one step with degree-2 polynomial approximation.
//! \~russian Выполняет один шаг с полиномиальной аппроксимацией степени 2.
void solvePA2(double u, double h);
//! \~english Performs one step with degree-3 polynomial approximation.
//! \~russian Выполняет один шаг с полиномиальной аппроксимацией степени 3.
void solvePA3(double u, double h);
//! \~english Performs one step with degree-4 polynomial approximation.
//! \~russian Выполняет один шаг с полиномиальной аппроксимацией степени 4.
void solvePA4(double u, double h);
//! \~english Performs one step with degree-5 polynomial approximation.
//! \~russian Выполняет один шаг с полиномиальной аппроксимацией степени 5.
void solvePA5(double u, double h);
//! \~english Current solver state vector.
//! \~russian Текущий вектор состояния решателя.
PIMathVectord X;
//! \~english Global default method used when \a Method is \a Global.
//! \~russian Глобальный метод по умолчанию, используемый при \a Method::Global.
static Method method_global;
//! \~english Text description of available methods.
//! \~russian Текстовое описание доступных методов.
static const char methods_desc[];
private:
void moveF();
PIMathMatrixd A, M;
PIMathVectord d, a1, b1;
PIMathVectord k1, k2, k3, k4, xx;
PIMathVectord XX, Y, pY;
PIVector<PIMathVectord> F;
PIVector<double> times;
uint size = 0, step = 0;
Method method = Global;
double sum = 0., td = 0., ct = 0., lp = 0., dh = 0., t = 0., x1 = 0., x0 = 0.;
bool ok = false;
};
#endif // PIMATHSOLVER_H