🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
ODEStateObserver.hpp
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/TypeTraits.hpp"
25
26AST_NAMESPACE_BEGIN
27
28class ODEIntegrator;
29
30enum EODEAction
31{
32 eContinue = 0,
33 eStop = 1,
34};
35
38class AST_MATH_API ODEStateObserver
39{
40public:
41 virtual ~ODEStateObserver() = default;
42
46 virtual EODEAction onStateUpdate(double* y, double& x, ODEIntegrator* integrator) = 0;
47
48};
49
50
53{
54public:
55 // 版本1: 支持 func_(y, x, integrator)
56 template<typename F>
57 static A_ALWAYS_INLINE auto call_func(F& func, double* y, double& x, ODEIntegrator* integrator)
58 -> typename std::enable_if<is_callable<F, double*, double&, ODEIntegrator*>::value, decltype(std::declval<F>()(y, x, integrator))>::type
59 {
60 return func(y, x, integrator);
61 }
62
63 // 版本2: 支持 func_(y, x)
64 template<typename F>
65 static A_ALWAYS_INLINE auto call_func(F& func, double* y, double& x, ODEIntegrator* integrator)
66 -> typename std::enable_if<is_callable<F, double*, double&>::value, decltype(std::declval<F>()(y, x))>::type
67 {
68 return func(y, x);
69 }
70
71 // 版本3: 支持 func_(y)
72 template<typename F>
73 static A_ALWAYS_INLINE auto call_func(F& func, double* y, double& x, ODEIntegrator* integrator)
74 -> typename std::enable_if<is_callable<F, double*>::value, decltype(std::declval<F>()(y))>::type
75 {
76 return func(y);
77 }
78};
79
81template<typename Func>
83{
84public:
86 using FuncType = Func;
87 explicit ODEStateObserverGeneric(Func func)
88 : func_(std::move(func))
89 {}
90
91 EODEAction onStateUpdate(double* y, double& x, ODEIntegrator* integrator) override {
92 return this->operator()(y, x, integrator);
93 }
94
95private:
96 Func func_;
97private:
98 // 处理不同的返回类型
99 template<typename F = FuncType>
100 A_ALWAYS_INLINE
101 typename std::enable_if<!std::is_void<decltype(
102 ODEStateObserverGenericHelper::call_func
103 (std::declval<F&>(), std::declval<double*>(), std::declval<double&>(), std::declval<ODEIntegrator*>()))>::value, EODEAction>::type
104 operator()(double* y, double& x, ODEIntegrator* integrator) {
105 return ODEStateObserverGenericHelper::call_func(func_, y, x, integrator);
106 }
107
108 template<typename F = FuncType>
109 A_ALWAYS_INLINE
110 typename std::enable_if<std::is_void<decltype(
111 ODEStateObserverGenericHelper::call_func
112 (std::declval<F&>(), std::declval<double*>(), std::declval<double&>(), std::declval<ODEIntegrator*>()))>::value, EODEAction>::type
113 operator()(double* y, double& x, ODEIntegrator* integrator) {
114 ODEStateObserverGenericHelper::call_func(func_, y, x, integrator);
115 return EODEAction::eContinue;
116 }
117};
118
119AST_NAMESPACE_END
ODE 积分器
定义 ODEIntegrator.hpp:81
泛型ODE状态量观察者助手类
定义 ODEStateObserver.hpp:53
泛型ODE状态量观察者
定义 ODEStateObserver.hpp:83
EODEAction onStateUpdate(double *y, double &x, ODEIntegrator *integrator) override
状态更新处理函数
定义 ODEStateObserver.hpp:91
ODE状态量观察者
定义 ODEStateObserver.hpp:39
virtual EODEAction onStateUpdate(double *y, double &x, ODEIntegrator *integrator)=0
状态更新处理函数