c++ - 为什么新版本的 odeint 会失败?

标签 c++ boost odeint boost-multiprecision

我在新版本的 boost 上遇到了困难。我正在使用具有多重精度的 odeint 。下面这段代码可以使用boost版本1.67.0成功编译。但是,从版本 1.68.0 及更高版本开始,我无法再编译。在版本 1.68.0 中引入了复杂的多精度数字,我也想在我的代码中使用此数据类型。有人可以告诉我我的代码中的错误在哪里吗?

#include <iostream>

#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
#include <boost/multiprecision/gmp.hpp>

using namespace std;
using namespace boost::numeric::odeint;

typedef boost::multiprecision::mpf_float_100 mpf;
typedef boost::array< mpf , 1 > state_type;

void rhs( const state_type &x , state_type &dxdt , const mpf t )
{
    dxdt[0] = ( - x[0] * sin( t ) + 2.0 * tan( t ) ) * x[0];
}

void write_out( const state_type &x , const mpf t )
{
    cout.precision(50);
    cout << t << '\t' << x[0] << endl;
}

int main()
{
    bulirsch_stoer< state_type , mpf , state_type , mpf > stepper( 1E-20 , 0 , 0 , 0 );

    state_type x;

    mpf t = mpf("0.2");
    mpf dt = mpf("0.01");
    mpf t_end = mpf("1.5");

    x[0] = 1.15;

    integrate_adaptive( stepper , rhs , x , t , t_end , dt , write_out );
} 

我正在使用opensuse,用icpc或g++ 7.4.1编译。我用 icpc 收到的错误消息很长。

icpc -lgmp -I/userdir/boost_1_71_0/ code.cpp
/userdir/boost_1_71_0/boost/numeric/odeint/algebra/detail/extract_value_type.hpp(47): error: class "boost::numeric::odeint::detail::extract_value_type<mpf, void>" has no member "type"
      typedef typename extract_value_type< typename S::value_type >::type type;
                                                                     ^
          detected during:
            instantiation of class "boost::numeric::odeint::detail::extract_value_type<S, boost::enable_if<has_value_type<S, boost::mpl::bool_<false>>, void>::type> [with S=mpf]" at line 47
            instantiation of class "boost::numeric::odeint::detail::extract_value_type<S, boost::enable_if<has_value_type<S, boost::mpl::bool_<false>>, void>::type> [with S=boost::array<mpf, 1UL>]" at line 28 of "/userdir/boost_1_71_0/boost/numeric/odeint/algebra/norm_result_type.hpp"
            instantiation of class "boost::numeric::odeint::norm_result_type<S, Enabler> [with S=boost::array<mpf, 1UL>, Enabler=void]" at line 89 of "/userdir/boost_1_71_0/boost/numeric/odeint/stepper/controlled_runge_kutta.hpp"
            instantiation of "boost::numeric::odeint::default_error_checker<Value, Algebra, Operations>::value_type boost::numeric::odeint::default_error_checker<Value, Algebra, Operations>::error(boost::numeric::odeint::default_error_checker<Value, Algebra, Operations>::algebra_type &, const State &, const Deriv &, Err &, Time) const [with Value=mpf, Algebra=boost::numeric::odeint::array_algebra, Operations=boost::numeric::odeint::default_operations, State=state_type, Deriv=state_type,
                      Err=state_type, Time=mpf]" at line 235 of "/userdir/boost_1_71_0/boost/numeric/odeint/stepper/bulirsch_stoer.hpp"
            instantiation of "boost::numeric::odeint::controlled_step_result={boost::numeric::odeint::controlled_step_result} boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::try_step(System, const StateIn &, const DerivIn &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &, StateOut &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations,
                      Resizer>::time_type &) [with State=state_type, Value=mpf, Deriv=state_type, Time=mpf, Algebra=boost::numeric::odeint::array_algebra, Operations=boost::numeric::odeint::default_operations, Resizer=boost::numeric::odeint::initially_resizer, System=void (*)(const state_type &, state_type &, mpf), StateIn=state_type, DerivIn=state_type, StateOut=state_type]" at line 156 of "/userdir/boost_1_71_0/boost/numeric/odeint/stepper/bulirsch_stoer.hpp"
            instantiation of "boost::numeric::odeint::controlled_step_result={boost::numeric::odeint::controlled_step_result} boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::try_step(System, StateInOut &, const DerivIn &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &) [with
                      State=state_type, Value=mpf, Deriv=state_type, Time=mpf, Algebra=boost::numeric::odeint::array_algebra, Operations=boost::numeric::odeint::default_operations, Resizer=boost::numeric::odeint::initially_resizer, System=void (*)(const state_type &, state_type &, mpf), StateInOut=state_type, DerivIn=state_type]" at line 393 of "/userdir/boost_1_71_0/boost/numeric/odeint/stepper/bulirsch_stoer.hpp"
            instantiation of "boost::numeric::odeint::controlled_step_result={boost::numeric::odeint::controlled_step_result} boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::try_step_v1(System, StateInOut &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &) [with State=state_type,
                      Value=mpf, Deriv=state_type, Time=mpf, Algebra=boost::numeric::odeint::array_algebra, Operations=boost::numeric::odeint::default_operations, Resizer=boost::numeric::odeint::initially_resizer, System=void (*)(const state_type &, state_type &, mpf), StateInOut=state_type]" at line 135 of "/userdir/boost_1_71_0/boost/numeric/odeint/stepper/bulirsch_stoer.hpp"
            instantiation of "boost::numeric::odeint::controlled_step_result={boost::numeric::odeint::controlled_step_result} boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::try_step(System, StateInOut &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &, boost::numeric::odeint::bulirsch_stoer<State, Value, Deriv, Time, Algebra, Operations, Resizer>::time_type &) [with State=state_type,
                      Value=mpf, Deriv=state_type, Time=mpf, Algebra=boost::numeric::odeint::array_algebra, Operations=boost::numeric::odeint::default_operations, Resizer=boost::numeric::odeint::initially_resizer, System=void (*)(const state_type &, state_type &, mpf), StateInOut=state_type]" at line 103 of "/userdir/boost_1_71_0/boost/numeric/odeint/integrate/detail/integrate_adaptive.hpp"
            instantiation of "size_t={unsigned long} boost::numeric::odeint::detail::integrate_adaptive(Stepper, System, State &, Time &, Time, Time &, Observer, boost::numeric::odeint::controlled_stepper_tag) [with Stepper=boost::numeric::odeint::bulirsch_stoer<state_type, mpf, state_type, mpf, boost::numeric::odeint::array_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>, System=void (*)(const state_type &, state_type &, mpf), State=state_type,
                      Time=mpf, Observer=void (*)(const state_type &, mpf)]" at line 45 of "/userdir/boost_1_71_0/boost/numeric/odeint/integrate/integrate_adaptive.hpp"
            instantiation of "size_t={unsigned long} boost::numeric::odeint::integrate_adaptive(Stepper, System, State &, Time, Time, Time, Observer) [with Stepper=boost::numeric::odeint::bulirsch_stoer<state_type, mpf, state_type, mpf, boost::numeric::odeint::array_algebra, boost::numeric::odeint::default_operations, boost::numeric::odeint::initially_resizer>, System=void (*)(const state_type &, state_type &, mpf), State=state_type, Time=mpf, Observer=void (*)(const state_type &, mpf)]" at line
                      36 of "code.cpp"

compilation aborted for code.cpp (code 2)

最佳答案

问题似乎是 boost::multi precision::number 使 mpf_float_100 (以及所有其他 Boost.Multi precision 类型)工作,有一个关联的 value_type 自 Boost 1.68 起,因此 Boost.Numeric.Odeint 将其视为容器,而实际上并非如此。 Odeint 检查类型是否为容器的方式是使用特征:has_value_type,将该特征专门化为 number 应该有效:

#include <iostream>

#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>

#include <boost/multiprecision/gmp.hpp>

template< typename Backend, boost::multiprecision::expression_template_option Option >
struct has_value_type<boost::multiprecision::number<Backend,Option> >:boost::mpl::false_{}; //ADDED

using namespace std;
using namespace boost::numeric::odeint;

namespace mp=boost::multiprecision;

typedef boost::multiprecision::mpf_float_100 mpf;
typedef boost::array< mpf , 1 > state_type;

void rhs( const state_type &x , state_type &dxdt , const mpf t )
{
    dxdt[0] = ( - x[0] * sin( t ) + 2.0 * tan( t ) ) * x[0];
}

void write_out( const state_type &x , const mpf t )
{
    cout.precision(50);
    cout << t << '\t' << x[0] << endl;
}

int main()
{
    bulirsch_stoer< state_type , mpf , state_type , mpf > stepper( 1E-20 , 0 , 0 , 0 );

    state_type x;

    mpf t = mpf("0.2");
    mpf dt = mpf("0.01");
    mpf t_end = mpf("1.5");

    x[0] = 1.15;

    integrate_adaptive( stepper , rhs , x , t , t_end , dt , write_out );
} 

关于c++ - 为什么新版本的 odeint 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58324974/

相关文章:

c++ - 等待多个互斥锁的条件变量

c++ - boost 获取锁失败

python - 用 Python 构建 caffe(找不到 -lboost_python3)

c++ - boost odeint有蛙跳算法吗?

c++ - QTableWidget:信号itemChanged触发得太晚了(在发出其他 Action 的信号,关闭等之后)

c++ - OCI 预取不工作

c++ - 如何将可调用对象放入 C++ 中的映射中?

c++ - 使用 Boost::tokenizer 的两个相同程序,但有两个不同的输出

c++ - 在 ODEINT + THRUST 观察器中,接收到表达式必须是可修改左值的错误

c++ - 如何在 C++ 中使用 Thrust 和 odeint 求解简单的 ODE