【发布时间】:2021-12-23 01:37:21
【问题描述】:
我有一个 3D 简单立方晶格,在我的代码中我称之为 Grid,其周期性边界条件大小为 20x20x20(数字是任意的)。我想要做的是种植多个聚合度为 N 的聚合物链(具有 N 个节点的图)不重叠,是自我避免的。
目前,我可以递归地种植一种聚合物。这是我的代码
const std::vector <int> ex{1,0,0}, nex{-1,0,0}, ey{0,1,0}, ney{0,-1,0}, ez{0,0,1}, nez{0,0,-1}; // unit directions
const std::vector <std::vector <int>> drns = {ex, nex, ey, ney, ez, nez}; // vector of unit directions
void Grid::plant_polymer(int DoP, std::vector <std::vector <int>>* loc_list){
// loc_list is the list of places the polymer has been
// for this function, I provide a starting point
if (DoP == 0){
Polymer p (loc_list);
this->polymer_chains.push_back(p); // polymer_chains is the attribute which holds all polymer chains in the grid
return; // once the degree of polymerization hits zero, you are done
};
// until then
// increment final vector in loc_list in a unit direction
std::vector <int> next(3,0);
for (auto v: drns){
next = add_vectors(&((*loc_list).at((*loc_list).size()-1)), &v);
impose_pbc(&next, this->x_len, this->y_len, this->z_len);
if (this->occupied[next]==0){ // occupied is a map which takes in a location, and spits out if it is occupied (1) or not (0)
// occupied is an attribute of the class Grid
dop--; // decrease dop now that a monomer unit has been added
(*loc_list).push_back(next); // add monomer to list
this->occupied[next] == 1;
return plant_polymer(DoP, loc_list);
}
}
std::cout << "no solution found for the self-avoiding random walk...";
return;
这不是一个通用的解决方案。我正在为聚合物提供种子,而且,我只种植一种聚合物。我想让它可以种植多种聚合物,而无需指定种子。每次我想添加聚合物时,是否可以递归地寻找起始位置,然后构建聚合物,同时确保它不与系统中已有的其他聚合物重叠?您的任何建议将不胜感激。
【问题讨论】:
-
为了加速程序,将
std::vector <int> next(3,0);替换为std::array <int, 3>;据我所知,你甚至不需要初始化它。这将需要更改add_vectors、impose_pbc等函数的接口。另外,在for (auto v: drns)中使用与号,即使用for (const auto & v: drns)。此外,(*loc_list).at((*loc_list).size()-1)似乎等同于loc_list->back()。 -
N的典型值是多少?您需要的聚合物的典型密度是多少?也就是说,您需要非常致密的聚合物系统吗?问题描述中的N是否等同于代码中的DoP?代码底部附近的dop是什么?这是一个错字吗?应该写成DoP? -
N 的典型值约为 5。N 通常约为 40。是的,N 相当于 DOP。是的,
dop是一个错字,应该是DoP。感谢您关注我的问题@zkoza!
标签: c++ recursion game-physics random-walk