我開發了一個科學應用程序(模擬在細胞核中移動的染色體)。染色體被分成小片段,使用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