我开发了一个科学应用程序(模拟在细胞核中移动的染色体)。染色体被分成小片段,使用4x4旋转矩阵围绕随机轴旋转。解决浮点舍入问题C++
问题是模拟执行了数千亿次的旋转,因此浮点舍入误差会堆积并呈指数级增长,所以碎片会随着时间流逝而“飘离”并与染色体的其余部分分离。
我使用C++的双精度。目前在CPU上软运行,但将被移植到CUDA,并且仿真最多可持续1个月。因为所有的片段都被链接在一起(你可以看到它是一个双向链表),但我认为如果可能的话,这将是最好的想法。
你有什么建议吗?我感觉有点失落。
非常感谢你,
H.
编辑: 增加了一个简单的示例代码。 你可以假设所有的矩阵数学都是经典的实现。
// Rotate 1000000 times
for (int i = 0; i < 1000000; ++i)
{
// Pick a random section start
int istart = rand() % chromosome->length;
// Pick the end 20 segments further (cyclic)
int iend = (istart + 20) % chromosome->length;
// Build rotation axis
Vector4 axis = chromosome->segments[istart].position - chromosome->segments[iend].position;
axis.normalize();
// Build rotation matrix and translation vector
Matrix4 rotm(axis, rand()/float(RAND_MAX));
Vector4 oldpos = chromosome->segments[istart].position;
// Rotate each segment between istart and iend using rotm
for (int j = (istart + 1) % chromosome->length; j != iend; ++j, j %= chromosome->length)
{
chromosome->segments[j].position -= oldpos;
chromosome->segments[j].position.transform(rotm);
chromosome->segments[j].position += oldpos;
}
}
数值分析和稳定性是一个巨大的领域。没有一个正确的答案。没有看到一些示例代码,很难给出任何具体的建议。 – 2011-04-11 22:04:09
你说得对,我添加了一些代码,如果这可能有帮助。 – 2011-04-11 22:17:26
顺便说一句,这听起来像一个很酷的项目。 – 2011-04-11 22:26:26