使用自定义张量数据结构 boost::odeint 的错误输出
wrong output of boost::odeint with custom tensor data structure
基于@Piotr Skotnicki 的回答 我定义了以下数据类型:
#ifndef TENSOR_HPP
#define TENSOR_HPP
#include <vector>
#include <array>
#include <boost/numeric/odeint.hpp>
namespace nex {
template <size_t...> struct seq {};
template <size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> { using type = seq<Is...>; };
template<typename T, typename S>
class tensor;
template<typename T, size_t... Is>
class tensor<T, seq<Is...>>
{
typedef std::vector<T> vector;
public:
// constructor
tensor ();
tensor (decltype(Is)...);
// iterators
using iterator = typename vector::iterator;
using const_iterator = typename vector::const_iterator;
iterator begin()
{ return m_v.begin(); }
const_iterator begin() const
{ return m_v.begin(); }
iterator end()
{ return m_v.end(); }
const_iterator end() const
{ return m_v.end(); }
// resizing
size_t size() const;
void resize( const size_t );
// operators
const T& operator() (decltype(Is)...) const;
T& operator() (decltype(Is)...);
private:
// helper functions
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I == 0, size_t>::type
{
return a[I];
}
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I != 0, size_t>::type
{
return index<I-1>(a) * s[I] + a[I];
}
size_t mult(size_t N)
{
return N;
}
template <typename... S>
size_t mult(size_t N, S... Ns)
{
return N * mult(Ns...);
}
vector m_v;
const std::array<size_t, sizeof...(Is)> s;
}; // class tensor
template<typename T, size_t... Is
tensor<T, seq<Is...>> :: tensor ()
: m_v(), s{ { 0 } }
{}
template<typename T, size_t... Is>
tensor<T, seq<Is...>> :: tensor (decltype(Is)... size)
: m_v( mult( size... ) ), s{ { size... } }
{}
template<typename T, size_t... Is>
size_t tensor<T, seq<Is...>> :: size () const
{ return m_v.size(); }
template<typename T, size_t... Is>
void tensor<T, seq<Is...>> :: resize (const size_t n)
{ m_v.resize( n ); }
template<typename T, size_t... Is>
const T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n) const
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
template<typename T, size_t... Is>
T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n)
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
} // namespace nex
namespace boost { namespace numeric { namespace odeint {
template<typename T, size_t... Is>
struct is_resizeable< nex::tensor<T, nex::seq<Is...>> >
{
typedef boost::true_type type;
static const bool value = type::value;
};
} // namespace odeint
} // namespace numeric
} // namespace boost
template <typename T, std::size_t N>
using tensor = nex::tensor<T, typename nex::gen_seq<N>::type>;
#endif // TENSOR_HPP
然后我想在boost::odeint中使用这个数据类型来集成非常基本的问题
#include "tensor.hpp"
typedef tensor<double, 1> state_type;
void lorenz( const state_type &x , state_type &dxdt , const double t )
{
dxdt(0) = x(0);
dxdt(1) = x(1);
dxdt(2) = x(2);
}
void write_lorenz( const state_type &x , const double t )
{
std::cout << t << '\t' << x(0) << '\t' << x(1) << '\t' << x(2) << std::endl;
}
using namespace boost::numeric::odeint;
int main()
{
state_type x(3);
x(0) = 1.0 ; x(1) = 10.0 ; x(2) = 10.0;
integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 1.0 , 0.1, write_lorenz );
}
它工作得很好,即使对于不同的方程组(比如洛伦兹吸引子)也是如此。不幸的是,当我想使用张量的相同容量但定义不同时,一切都出错了
#include "tensor.hpp"
typedef tensor<double, 2> state_type;
void lorenz( const state_type &x , state_type &dxdt , const double t )
{
dxdt(0,0) = x(0,0);
dxdt(1,0) = x(1,0);
dxdt(2,0) = x(2,0);
}
void write_lorenz( const state_type &x , const double t )
{
std::cout << t << '\t' << x(0,0) << '\t' << x(1,0) << '\t' << x(2,0) << std::endl;
}
using namespace boost::numeric::odeint;
int main()
{
state_type x(3,1);
x(0,0) = 1.0 ; x(1,0) = 10.0 ; x(2,0) = 10.0;
integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 1.0 , 0.1, write_lorenz );
}
您可以 运行 自己编写代码并检查 x(1,0) x(2,0) 是否完全没有变化。我认为 operator() 的适当定义足以让 odeint 正常工作。我对 boost::odeint 知之甚少,所以这可能只是一个基本错误。我指望你的帮助。
更新
当我使用 std::array 将张量定义为固定大小的数组时,它工作正常。但是我不需要添加 boost::is_resizeable。我认为调整大小需要不同的定义(我真的不知道)。
的确,问题出在调整大小上。目前,调整张量的大小只会改变基础向量的大小 m_v
。虽然这确保了正确的内存分配,但这还不够。您还需要正确设置维度,即成员 s
。你也可以这样想:只给定一个整数是不可能调整多维张量的大小的,你需要设置每个维度的大小。唯一的例外是一维张量,对于它来说一切正常,对于任何更高的维度,这都必须失败。
以下 tensor.hpp 解决了这个问题并且在我的快速测试中似乎运行良好:
#ifndef TENSOR_HPP
#define TENSOR_HPP
#include <vector>
#include <array>
#include <boost/numeric/odeint.hpp>
namespace nex {
template <size_t...> struct seq {};
template <size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> { using type = seq<Is...>; };
template<typename T, typename S>
class tensor;
template<typename T, size_t... Is>
class tensor<T, seq<Is...>>
{
typedef std::vector<T> vector;
public:
// constructor
tensor ();
tensor (decltype(Is)...);
// iterators
using iterator = typename vector::iterator;
using const_iterator = typename vector::const_iterator;
iterator begin()
{ return m_v.begin(); }
const_iterator begin() const
{ return m_v.begin(); }
iterator end()
{ return m_v.end(); }
const_iterator end() const
{ return m_v.end(); }
// resizing
size_t size() const;
void resize( const tensor<T, seq<Is...>>& );
// operators
const T& operator() (decltype(Is)...) const;
T& operator() (decltype(Is)...);
private:
// helper functions
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I == 0, size_t>::type
{
return a[I];
}
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I != 0, size_t>::type
{
return index<I-1>(a) * s[I] + a[I];
}
size_t mult(size_t N)
{
return N;
}
template <typename... S>
size_t mult(size_t N, S... Ns)
{
return N * mult(Ns...);
}
vector m_v;
std::array<size_t, sizeof...(Is)> s;
}; // class tensor
template<typename T, size_t... Is>
tensor<T, seq<Is...>> :: tensor ()
: m_v(), s{ { 0 } }
{}
template<typename T, size_t... Is>
tensor<T, seq<Is...>> :: tensor (decltype(Is)... size)
: m_v( mult( size... ) ), s{ { size... } }
{}
template<typename T, size_t... Is>
size_t tensor<T, seq<Is...>> :: size () const
{ return m_v.size(); }
template<typename T, size_t... Is>
void tensor<T, seq<Is...>> :: resize (const tensor<T, seq<Is...>> &x)
{
m_v.resize( x.size() );
s = x.s;
}
template<typename T, size_t... Is>
const T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n) const
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
template<typename T, size_t... Is>
T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n)
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
} // namespace nex
namespace boost { namespace numeric { namespace odeint {
template<typename T, size_t... Is>
struct is_resizeable< nex::tensor<T, nex::seq<Is...>> >
{
typedef boost::true_type type;
static const bool value = type::value;
};
template<typename T, size_t... Is>
struct resize_impl< nex::tensor<T, nex::seq<Is...>>, nex::tensor<T, nex::seq<Is...>> >
{
typedef nex::tensor<T, nex::seq<Is...>> state_type;
static void resize( state_type &x1 , const state_type &x2 )
{
x1.resize(x2);
}
};
} // namespace odeint
} // namespace numeric
} // namespace boost
template <typename T, std::size_t N>
using tensor = nex::tensor<T, typename nex::gen_seq<N>::type>;
#endif // TENSOR_HPP
基于@Piotr Skotnicki 的回答
#ifndef TENSOR_HPP
#define TENSOR_HPP
#include <vector>
#include <array>
#include <boost/numeric/odeint.hpp>
namespace nex {
template <size_t...> struct seq {};
template <size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> { using type = seq<Is...>; };
template<typename T, typename S>
class tensor;
template<typename T, size_t... Is>
class tensor<T, seq<Is...>>
{
typedef std::vector<T> vector;
public:
// constructor
tensor ();
tensor (decltype(Is)...);
// iterators
using iterator = typename vector::iterator;
using const_iterator = typename vector::const_iterator;
iterator begin()
{ return m_v.begin(); }
const_iterator begin() const
{ return m_v.begin(); }
iterator end()
{ return m_v.end(); }
const_iterator end() const
{ return m_v.end(); }
// resizing
size_t size() const;
void resize( const size_t );
// operators
const T& operator() (decltype(Is)...) const;
T& operator() (decltype(Is)...);
private:
// helper functions
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I == 0, size_t>::type
{
return a[I];
}
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I != 0, size_t>::type
{
return index<I-1>(a) * s[I] + a[I];
}
size_t mult(size_t N)
{
return N;
}
template <typename... S>
size_t mult(size_t N, S... Ns)
{
return N * mult(Ns...);
}
vector m_v;
const std::array<size_t, sizeof...(Is)> s;
}; // class tensor
template<typename T, size_t... Is
tensor<T, seq<Is...>> :: tensor ()
: m_v(), s{ { 0 } }
{}
template<typename T, size_t... Is>
tensor<T, seq<Is...>> :: tensor (decltype(Is)... size)
: m_v( mult( size... ) ), s{ { size... } }
{}
template<typename T, size_t... Is>
size_t tensor<T, seq<Is...>> :: size () const
{ return m_v.size(); }
template<typename T, size_t... Is>
void tensor<T, seq<Is...>> :: resize (const size_t n)
{ m_v.resize( n ); }
template<typename T, size_t... Is>
const T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n) const
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
template<typename T, size_t... Is>
T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n)
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
} // namespace nex
namespace boost { namespace numeric { namespace odeint {
template<typename T, size_t... Is>
struct is_resizeable< nex::tensor<T, nex::seq<Is...>> >
{
typedef boost::true_type type;
static const bool value = type::value;
};
} // namespace odeint
} // namespace numeric
} // namespace boost
template <typename T, std::size_t N>
using tensor = nex::tensor<T, typename nex::gen_seq<N>::type>;
#endif // TENSOR_HPP
然后我想在boost::odeint中使用这个数据类型来集成非常基本的问题
#include "tensor.hpp"
typedef tensor<double, 1> state_type;
void lorenz( const state_type &x , state_type &dxdt , const double t )
{
dxdt(0) = x(0);
dxdt(1) = x(1);
dxdt(2) = x(2);
}
void write_lorenz( const state_type &x , const double t )
{
std::cout << t << '\t' << x(0) << '\t' << x(1) << '\t' << x(2) << std::endl;
}
using namespace boost::numeric::odeint;
int main()
{
state_type x(3);
x(0) = 1.0 ; x(1) = 10.0 ; x(2) = 10.0;
integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 1.0 , 0.1, write_lorenz );
}
它工作得很好,即使对于不同的方程组(比如洛伦兹吸引子)也是如此。不幸的是,当我想使用张量的相同容量但定义不同时,一切都出错了
#include "tensor.hpp"
typedef tensor<double, 2> state_type;
void lorenz( const state_type &x , state_type &dxdt , const double t )
{
dxdt(0,0) = x(0,0);
dxdt(1,0) = x(1,0);
dxdt(2,0) = x(2,0);
}
void write_lorenz( const state_type &x , const double t )
{
std::cout << t << '\t' << x(0,0) << '\t' << x(1,0) << '\t' << x(2,0) << std::endl;
}
using namespace boost::numeric::odeint;
int main()
{
state_type x(3,1);
x(0,0) = 1.0 ; x(1,0) = 10.0 ; x(2,0) = 10.0;
integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 1.0 , 0.1, write_lorenz );
}
您可以 运行 自己编写代码并检查 x(1,0) x(2,0) 是否完全没有变化。我认为 operator() 的适当定义足以让 odeint 正常工作。我对 boost::odeint 知之甚少,所以这可能只是一个基本错误。我指望你的帮助。
更新
当我使用 std::array 将张量定义为固定大小的数组时,它工作正常。但是我不需要添加 boost::is_resizeable。我认为调整大小需要不同的定义(我真的不知道)。
的确,问题出在调整大小上。目前,调整张量的大小只会改变基础向量的大小 m_v
。虽然这确保了正确的内存分配,但这还不够。您还需要正确设置维度,即成员 s
。你也可以这样想:只给定一个整数是不可能调整多维张量的大小的,你需要设置每个维度的大小。唯一的例外是一维张量,对于它来说一切正常,对于任何更高的维度,这都必须失败。
以下 tensor.hpp 解决了这个问题并且在我的快速测试中似乎运行良好:
#ifndef TENSOR_HPP
#define TENSOR_HPP
#include <vector>
#include <array>
#include <boost/numeric/odeint.hpp>
namespace nex {
template <size_t...> struct seq {};
template <size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...> {};
template <size_t... Is> struct gen_seq<0, Is...> { using type = seq<Is...>; };
template<typename T, typename S>
class tensor;
template<typename T, size_t... Is>
class tensor<T, seq<Is...>>
{
typedef std::vector<T> vector;
public:
// constructor
tensor ();
tensor (decltype(Is)...);
// iterators
using iterator = typename vector::iterator;
using const_iterator = typename vector::const_iterator;
iterator begin()
{ return m_v.begin(); }
const_iterator begin() const
{ return m_v.begin(); }
iterator end()
{ return m_v.end(); }
const_iterator end() const
{ return m_v.end(); }
// resizing
size_t size() const;
void resize( const tensor<T, seq<Is...>>& );
// operators
const T& operator() (decltype(Is)...) const;
T& operator() (decltype(Is)...);
private:
// helper functions
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I == 0, size_t>::type
{
return a[I];
}
template <size_t I>
auto index(const std::array<size_t, sizeof...(Is)>& a) const
-> typename std::enable_if<I != 0, size_t>::type
{
return index<I-1>(a) * s[I] + a[I];
}
size_t mult(size_t N)
{
return N;
}
template <typename... S>
size_t mult(size_t N, S... Ns)
{
return N * mult(Ns...);
}
vector m_v;
std::array<size_t, sizeof...(Is)> s;
}; // class tensor
template<typename T, size_t... Is>
tensor<T, seq<Is...>> :: tensor ()
: m_v(), s{ { 0 } }
{}
template<typename T, size_t... Is>
tensor<T, seq<Is...>> :: tensor (decltype(Is)... size)
: m_v( mult( size... ) ), s{ { size... } }
{}
template<typename T, size_t... Is>
size_t tensor<T, seq<Is...>> :: size () const
{ return m_v.size(); }
template<typename T, size_t... Is>
void tensor<T, seq<Is...>> :: resize (const tensor<T, seq<Is...>> &x)
{
m_v.resize( x.size() );
s = x.s;
}
template<typename T, size_t... Is>
const T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n) const
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
template<typename T, size_t... Is>
T& tensor<T, seq<Is...>> :: operator() (decltype(Is)... n)
{ return m_v.at(index<sizeof...(Is)-1>( { { n... } } ) ); }
} // namespace nex
namespace boost { namespace numeric { namespace odeint {
template<typename T, size_t... Is>
struct is_resizeable< nex::tensor<T, nex::seq<Is...>> >
{
typedef boost::true_type type;
static const bool value = type::value;
};
template<typename T, size_t... Is>
struct resize_impl< nex::tensor<T, nex::seq<Is...>>, nex::tensor<T, nex::seq<Is...>> >
{
typedef nex::tensor<T, nex::seq<Is...>> state_type;
static void resize( state_type &x1 , const state_type &x2 )
{
x1.resize(x2);
}
};
} // namespace odeint
} // namespace numeric
} // namespace boost
template <typename T, std::size_t N>
using tensor = nex::tensor<T, typename nex::gen_seq<N>::type>;
#endif // TENSOR_HPP