🛰️航天仿真算法库 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#ifndef SWIG
51
54{
55public:
56 // 版本1: 支持 func_(y, x, integrator)
57 template<typename F>
58 static A_ALWAYS_INLINE auto call_func(F& func, double* y, double& x, ODEIntegrator* integrator)
59 -> typename std::enable_if<is_callable<F, double*, double&, ODEIntegrator*>::value, decltype(std::declval<F>()(y, x, integrator))>::type
60 {
61 return func(y, x, integrator);
62 }
63
64 // 版本2: 支持 func_(y, x)
65 template<typename F>
66 static A_ALWAYS_INLINE auto call_func(F& func, double* y, double& x, ODEIntegrator* integrator)
67 -> typename std::enable_if<is_callable<F, double*, double&>::value, decltype(std::declval<F>()(y, x))>::type
68 {
69 return func(y, x);
70 }
71
72 // 版本3: 支持 func_(y)
73 template<typename F>
74 static A_ALWAYS_INLINE auto call_func(F& func, double* y, double& x, ODEIntegrator* integrator)
75 -> typename std::enable_if<is_callable<F, double*>::value, decltype(std::declval<F>()(y))>::type
76 {
77 return func(y);
78 }
79};
80
82template<typename Func>
84{
85public:
87 using FuncType = Func;
88 explicit ODEStateObserverGeneric(Func func)
89 : func_(std::move(func))
90 {}
91
92 EODEAction onStateUpdate(double* y, double& x, ODEIntegrator* integrator) override {
93 return this->operator()(y, x, integrator);
94 }
95
96private:
97 Func func_;
98private:
99 // 处理不同的返回类型
100 template<typename F = FuncType>
101 A_ALWAYS_INLINE
102 typename std::enable_if<!std::is_void<decltype(
103 ODEStateObserverGenericHelper::call_func
104 (std::declval<F&>(), std::declval<double*>(), std::declval<double&>(), std::declval<ODEIntegrator*>()))>::value, EODEAction>::type
105 operator()(double* y, double& x, ODEIntegrator* integrator) {
106 return ODEStateObserverGenericHelper::call_func(func_, y, x, integrator);
107 }
108
109 template<typename F = FuncType>
110 A_ALWAYS_INLINE
111 typename std::enable_if<std::is_void<decltype(
112 ODEStateObserverGenericHelper::call_func
113 (std::declval<F&>(), std::declval<double*>(), std::declval<double&>(), std::declval<ODEIntegrator*>()))>::value, EODEAction>::type
114 operator()(double* y, double& x, ODEIntegrator* integrator) {
115 ODEStateObserverGenericHelper::call_func(func_, y, x, integrator);
116 return EODEAction::eContinue;
117 }
118};
119
120#endif
121
122AST_NAMESPACE_END
ODE 积分器
定义 ODEIntegrator.hpp:83
泛型ODE状态量观察者助手类
定义 ODEStateObserver.hpp:54
泛型ODE状态量观察者
定义 ODEStateObserver.hpp:84
EODEAction onStateUpdate(double *y, double &x, ODEIntegrator *integrator) override
状态更新处理函数
定义 ODEStateObserver.hpp:92
ODE状态量观察者
定义 ODEStateObserver.hpp:39
virtual EODEAction onStateUpdate(double *y, double &x, ODEIntegrator *integrator)=0
状态更新处理函数